1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <linux/oom.h>
29 #include <sys/prctl.h>
30 #include <sys/mount.h>
34 #include <sys/resource.h>
38 #include "conf-parser.h"
39 #include "load-fragment.h"
42 #include "securebits.h"
44 #include "unit-name.h"
45 #include "bus-errors.h"
47 #ifndef HAVE_SYSV_COMPAT
48 int config_parse_warn_compat(
58 log_debug("[%s:%u] Support for option %s= has been disabled at compile time and is ignored", filename, line, lvalue);
63 int config_parse_unit_deps(
73 UnitDependency d = ltype;
83 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
87 if (!(t = strndup(w, l)))
90 k = unit_name_printf(u, t);
96 r = unit_add_dependency_by_name(u, d, k, NULL, true);
99 log_error("Failed to add dependency on %s, ignoring: %s", k, strerror(-r));
110 int config_parse_unit_names(
111 const char *filename,
130 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
134 if (!(t = strndup(w, l)))
137 k = unit_name_printf(u, t);
143 r = unit_merge_by_name(u, k);
146 log_error("Failed to add name %s, ignoring: %s", k, strerror(-r));
157 int config_parse_unit_string_printf(
158 const char *filename,
177 if (!(k = unit_full_printf(u, rvalue)))
191 int config_parse_unit_strv_printf(
192 const char *filename,
210 k = unit_full_printf(u, rvalue);
214 r = config_parse_strv(filename, line, section, lvalue, ltype, k, data, userdata);
220 int config_parse_unit_path_printf(
221 const char *filename,
240 if (!(k = unit_full_printf(u, rvalue)))
243 if (!path_is_absolute(k)) {
244 log_error("[%s:%u] Not an absolute path: %s", filename, line, k);
249 path_kill_slashes(k);
257 int config_parse_socket_listen(
258 const char *filename,
267 SocketPort *p, *tail;
277 if (!(p = new0(SocketPort, 1)))
280 if (streq(lvalue, "ListenFIFO")) {
281 p->type = SOCKET_FIFO;
283 if (!(p->path = unit_full_printf(UNIT(s), rvalue))) {
288 path_kill_slashes(p->path);
290 } else if (streq(lvalue, "ListenSpecial")) {
291 p->type = SOCKET_SPECIAL;
293 if (!(p->path = unit_full_printf(UNIT(s), rvalue))) {
298 path_kill_slashes(p->path);
300 } else if (streq(lvalue, "ListenMessageQueue")) {
302 p->type = SOCKET_MQUEUE;
304 if (!(p->path = unit_full_printf(UNIT(s), rvalue))) {
309 path_kill_slashes(p->path);
311 } else if (streq(lvalue, "ListenNetlink")) {
315 p->type = SOCKET_SOCKET;
316 k = unit_full_printf(UNIT(s), rvalue);
317 r = socket_address_parse_netlink(&p->address, k);
321 log_error("[%s:%u] Failed to parse address value, ignoring: %s", filename, line, rvalue);
330 p->type = SOCKET_SOCKET;
331 k = unit_full_printf(UNIT(s), rvalue);
332 r = socket_address_parse(&p->address, k);
336 log_error("[%s:%u] Failed to parse address value, ignoring: %s", filename, line, rvalue);
341 if (streq(lvalue, "ListenStream"))
342 p->address.type = SOCK_STREAM;
343 else if (streq(lvalue, "ListenDatagram"))
344 p->address.type = SOCK_DGRAM;
346 assert(streq(lvalue, "ListenSequentialPacket"));
347 p->address.type = SOCK_SEQPACKET;
350 if (socket_address_family(&p->address) != AF_LOCAL && p->address.type == SOCK_SEQPACKET) {
351 log_error("[%s:%u] Address family not supported, ignoring: %s", filename, line, rvalue);
360 LIST_FIND_TAIL(SocketPort, port, s->ports, tail);
361 LIST_INSERT_AFTER(SocketPort, port, s->ports, tail, p);
363 LIST_PREPEND(SocketPort, port, s->ports, p);
368 int config_parse_socket_bind(
369 const char *filename,
379 SocketAddressBindIPv6Only b;
388 if ((b = socket_address_bind_ipv6_only_from_string(rvalue)) < 0) {
391 if ((r = parse_boolean(rvalue)) < 0) {
392 log_error("[%s:%u] Failed to parse bind IPv6 only value, ignoring: %s", filename, line, rvalue);
396 s->bind_ipv6_only = r ? SOCKET_ADDRESS_IPV6_ONLY : SOCKET_ADDRESS_BOTH;
398 s->bind_ipv6_only = b;
403 int config_parse_exec_nice(
404 const char *filename,
413 ExecContext *c = data;
421 if (safe_atoi(rvalue, &priority) < 0) {
422 log_error("[%s:%u] Failed to parse nice priority, ignoring: %s. ", filename, line, rvalue);
426 if (priority < PRIO_MIN || priority >= PRIO_MAX) {
427 log_error("[%s:%u] Nice priority out of range, ignoring: %s", filename, line, rvalue);
437 int config_parse_exec_oom_score_adjust(
438 const char *filename,
447 ExecContext *c = data;
455 if (safe_atoi(rvalue, &oa) < 0) {
456 log_error("[%s:%u] Failed to parse the OOM score adjust value, ignoring: %s", filename, line, rvalue);
460 if (oa < OOM_SCORE_ADJ_MIN || oa > OOM_SCORE_ADJ_MAX) {
461 log_error("[%s:%u] OOM score adjust value out of range, ignoring: %s", filename, line, rvalue);
465 c->oom_score_adjust = oa;
466 c->oom_score_adjust_set = true;
471 int config_parse_exec(
472 const char *filename,
481 ExecCommand **e = data, *nce;
490 /* We accept an absolute path as first argument, or
491 * alternatively an absolute prefixed with @ to allow
492 * overriding of argv[0]. */
500 bool honour_argv0 = false, ignore = false;
506 rvalue += strspn(rvalue, WHITESPACE);
511 if (rvalue[0] == '-') {
516 if (rvalue[0] == '@') {
521 if (*rvalue != '/') {
522 log_error("[%s:%u] Invalid executable path in command line, ignoring: %s", filename, line, rvalue);
527 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
528 if (strncmp(w, ";", MAX(l, 1U)) == 0)
534 if (!(n = new(char*, k + !honour_argv0)))
538 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
539 if (strncmp(w, ";", MAX(l, 1U)) == 0)
542 if (honour_argv0 && w == rvalue) {
544 if (!(path = cunescape_length(w, l)))
547 if (!(n[k++] = cunescape_length(w, l)))
555 log_error("[%s:%u] Invalid command line, ignoring: %s", filename, line, rvalue);
562 if (!(path = strdup(n[0])))
565 assert(path_is_absolute(path));
567 if (!(nce = new0(ExecCommand, 1)))
572 nce->ignore = ignore;
574 path_kill_slashes(nce->path);
576 exec_command_append_list(e, nce);
592 DEFINE_CONFIG_PARSE_ENUM(config_parse_service_type, service_type, ServiceType, "Failed to parse service type");
593 DEFINE_CONFIG_PARSE_ENUM(config_parse_service_restart, service_restart, ServiceRestart, "Failed to parse service restart specifier");
595 int config_parse_socket_bindtodevice(
596 const char *filename,
613 if (rvalue[0] && !streq(rvalue, "*")) {
614 if (!(n = strdup(rvalue)))
619 free(s->bind_to_device);
620 s->bind_to_device = n;
625 DEFINE_CONFIG_PARSE_ENUM(config_parse_output, exec_output, ExecOutput, "Failed to parse output specifier");
626 DEFINE_CONFIG_PARSE_ENUM(config_parse_input, exec_input, ExecInput, "Failed to parse input specifier");
628 int config_parse_facility(
629 const char *filename,
646 if ((x = log_facility_unshifted_from_string(rvalue)) < 0) {
647 log_error("[%s:%u] Failed to parse log facility, ignoring: %s", filename, line, rvalue);
651 *o = (x << 3) | LOG_PRI(*o);
656 int config_parse_level(
657 const char *filename,
674 if ((x = log_level_from_string(rvalue)) < 0) {
675 log_error("[%s:%u] Failed to parse log level, ignoring: %s", filename, line, rvalue);
679 *o = (*o & LOG_FACMASK) | x;
683 int config_parse_exec_io_class(
684 const char *filename,
693 ExecContext *c = data;
701 if ((x = ioprio_class_from_string(rvalue)) < 0) {
702 log_error("[%s:%u] Failed to parse IO scheduling class, ignoring: %s", filename, line, rvalue);
706 c->ioprio = IOPRIO_PRIO_VALUE(x, IOPRIO_PRIO_DATA(c->ioprio));
707 c->ioprio_set = true;
712 int config_parse_exec_io_priority(
713 const char *filename,
722 ExecContext *c = data;
730 if (safe_atoi(rvalue, &i) < 0 || i < 0 || i >= IOPRIO_BE_NR) {
731 log_error("[%s:%u] Failed to parse io priority, ignoring: %s", filename, line, rvalue);
735 c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_PRIO_CLASS(c->ioprio), i);
736 c->ioprio_set = true;
741 int config_parse_exec_cpu_sched_policy(
742 const char *filename,
752 ExecContext *c = data;
760 if ((x = sched_policy_from_string(rvalue)) < 0) {
761 log_error("[%s:%u] Failed to parse CPU scheduling policy, ignoring: %s", filename, line, rvalue);
765 c->cpu_sched_policy = x;
766 c->cpu_sched_set = true;
771 int config_parse_exec_cpu_sched_prio(
772 const char *filename,
781 ExecContext *c = data;
789 /* On Linux RR/FIFO have the same range */
790 if (safe_atoi(rvalue, &i) < 0 || i < sched_get_priority_min(SCHED_RR) || i > sched_get_priority_max(SCHED_RR)) {
791 log_error("[%s:%u] Failed to parse CPU scheduling priority, ignoring: %s", filename, line, rvalue);
795 c->cpu_sched_priority = i;
796 c->cpu_sched_set = true;
801 int config_parse_exec_cpu_affinity(
802 const char *filename,
811 ExecContext *c = data;
821 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
826 if (!(t = strndup(w, l)))
829 r = safe_atou(t, &cpu);
833 if (!(c->cpuset = cpu_set_malloc(&c->cpuset_ncpus)))
836 if (r < 0 || cpu >= c->cpuset_ncpus) {
837 log_error("[%s:%u] Failed to parse CPU affinity, ignoring: %s", filename, line, rvalue);
841 CPU_SET_S(cpu, CPU_ALLOC_SIZE(c->cpuset_ncpus), c->cpuset);
847 int config_parse_exec_capabilities(
848 const char *filename,
857 ExecContext *c = data;
865 if (!(cap = cap_from_text(rvalue))) {
869 log_error("[%s:%u] Failed to parse capabilities, ignoring: %s", filename, line, rvalue);
874 cap_free(c->capabilities);
875 c->capabilities = cap;
880 int config_parse_exec_secure_bits(
881 const char *filename,
890 ExecContext *c = data;
900 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
901 if (first_word(w, "keep-caps"))
902 c->secure_bits |= SECURE_KEEP_CAPS;
903 else if (first_word(w, "keep-caps-locked"))
904 c->secure_bits |= SECURE_KEEP_CAPS_LOCKED;
905 else if (first_word(w, "no-setuid-fixup"))
906 c->secure_bits |= SECURE_NO_SETUID_FIXUP;
907 else if (first_word(w, "no-setuid-fixup-locked"))
908 c->secure_bits |= SECURE_NO_SETUID_FIXUP_LOCKED;
909 else if (first_word(w, "noroot"))
910 c->secure_bits |= SECURE_NOROOT;
911 else if (first_word(w, "noroot-locked"))
912 c->secure_bits |= SECURE_NOROOT_LOCKED;
914 log_error("[%s:%u] Failed to parse secure bits, ignoring: %s", filename, line, rvalue);
922 int config_parse_exec_bounding_set(
923 const char *filename,
932 ExecContext *c = data;
944 if (rvalue[0] == '~') {
949 /* Note that we store this inverted internally, since the
950 * kernel wants it like this. But we actually expose it
951 * non-inverted everywhere to have a fully normalized
954 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
959 if (!(t = strndup(w, l)))
962 r = cap_from_name(t, &cap);
966 log_error("[%s:%u] Failed to parse capability bounding set, ignoring: %s", filename, line, rvalue);
970 sum |= ((uint64_t) 1ULL) << (uint64_t) cap;
974 c->capability_bounding_set_drop |= sum;
976 c->capability_bounding_set_drop |= ~sum;
981 int config_parse_exec_timer_slack_nsec(
982 const char *filename,
991 ExecContext *c = data;
999 if (safe_atolu(rvalue, &u) < 0) {
1000 log_error("[%s:%u] Failed to parse time slack value, ignoring: %s", filename, line, rvalue);
1004 c->timer_slack_nsec = u;
1009 int config_parse_limit(
1010 const char *filename,
1012 const char *section,
1019 struct rlimit **rl = data;
1020 unsigned long long u;
1029 if (streq(rvalue, "infinity"))
1030 u = (unsigned long long) RLIM_INFINITY;
1031 else if (safe_atollu(rvalue, &u) < 0) {
1032 log_error("[%s:%u] Failed to parse resource value, ignoring: %s", filename, line, rvalue);
1037 if (!(*rl = new(struct rlimit, 1)))
1040 (*rl)->rlim_cur = (*rl)->rlim_max = (rlim_t) u;
1044 int config_parse_unit_cgroup(
1045 const char *filename,
1047 const char *section,
1059 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
1067 k = unit_full_printf(u, t);
1079 r = unit_add_cgroup_from_text(u, t);
1083 log_error("[%s:%u] Failed to parse cgroup value, ignoring: %s", filename, line, rvalue);
1091 #ifdef HAVE_SYSV_COMPAT
1092 int config_parse_sysv_priority(
1093 const char *filename,
1095 const char *section,
1102 int *priority = data;
1110 if (safe_atoi(rvalue, &i) < 0 || i < 0) {
1111 log_error("[%s:%u] Failed to parse SysV start priority, ignoring: %s", filename, line, rvalue);
1115 *priority = (int) i;
1120 int config_parse_fsck_passno(
1121 const char *filename,
1123 const char *section,
1138 if (safe_atoi(rvalue, &i) || i < 0) {
1139 log_error("[%s:%u] Failed to parse fsck pass number, ignoring: %s", filename, line, rvalue);
1147 DEFINE_CONFIG_PARSE_ENUM(config_parse_kill_mode, kill_mode, KillMode, "Failed to parse kill mode");
1149 int config_parse_kill_signal(
1150 const char *filename,
1152 const char *section,
1167 if ((r = signal_from_string_try_harder(rvalue)) <= 0) {
1168 log_error("[%s:%u] Failed to parse kill signal, ignoring: %s", filename, line, rvalue);
1176 int config_parse_exec_mount_flags(
1177 const char *filename,
1179 const char *section,
1186 ExecContext *c = data;
1190 unsigned long flags = 0;
1197 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
1198 if (strncmp(w, "shared", MAX(l, 6U)) == 0)
1200 else if (strncmp(w, "slave", MAX(l, 5U)) == 0)
1202 else if (strncmp(w, "private", MAX(l, 7U)) == 0)
1203 flags |= MS_PRIVATE;
1205 log_error("[%s:%u] Failed to parse mount flags, ignoring: %s", filename, line, rvalue);
1210 c->mount_flags = flags;
1214 int config_parse_timer(
1215 const char *filename,
1217 const char *section,
1234 if ((b = timer_base_from_string(lvalue)) < 0) {
1235 log_error("[%s:%u] Failed to parse timer base, ignoring: %s", filename, line, lvalue);
1239 if (parse_usec(rvalue, &u) < 0) {
1240 log_error("[%s:%u] Failed to parse timer value, ignoring: %s", filename, line, rvalue);
1244 if (!(v = new0(TimerValue, 1)))
1250 LIST_PREPEND(TimerValue, value, t->values, v);
1255 int config_parse_timer_unit(
1256 const char *filename,
1258 const char *section,
1274 dbus_error_init(&error);
1276 if (endswith(rvalue, ".timer")) {
1277 log_error("[%s:%u] Unit cannot be of type timer, ignoring: %s", filename, line, rvalue);
1281 if ((r = manager_load_unit(t->meta.manager, rvalue, NULL, NULL, &t->unit)) < 0) {
1282 log_error("[%s:%u] Failed to load unit %s, ignoring: %s", filename, line, rvalue, bus_error(&error, r));
1283 dbus_error_free(&error);
1290 int config_parse_path_spec(
1291 const char *filename,
1293 const char *section,
1309 if ((b = path_type_from_string(lvalue)) < 0) {
1310 log_error("[%s:%u] Failed to parse path type, ignoring: %s", filename, line, lvalue);
1314 if (!path_is_absolute(rvalue)) {
1315 log_error("[%s:%u] Path is not absolute, ignoring: %s", filename, line, rvalue);
1319 if (!(s = new0(PathSpec, 1)))
1322 if (!(s->path = strdup(rvalue))) {
1327 path_kill_slashes(s->path);
1332 LIST_PREPEND(PathSpec, spec, p->specs, s);
1337 int config_parse_path_unit(
1338 const char *filename,
1340 const char *section,
1356 dbus_error_init(&error);
1358 if (endswith(rvalue, ".path")) {
1359 log_error("[%s:%u] Unit cannot be of type path, ignoring: %s", filename, line, rvalue);
1363 if ((r = manager_load_unit(t->meta.manager, rvalue, NULL, &error, &t->unit)) < 0) {
1364 log_error("[%s:%u] Failed to load unit %s, ignoring: %s", filename, line, rvalue, bus_error(&error, r));
1365 dbus_error_free(&error);
1372 int config_parse_socket_service(
1373 const char *filename,
1375 const char *section,
1391 dbus_error_init(&error);
1393 if (!endswith(rvalue, ".service")) {
1394 log_error("[%s:%u] Unit must be of type service, ignoring: %s", filename, line, rvalue);
1398 if ((r = manager_load_unit(s->meta.manager, rvalue, NULL, &error, (Unit**) &s->service)) < 0) {
1399 log_error("[%s:%u] Failed to load unit %s, ignoring: %s", filename, line, rvalue, bus_error(&error, r));
1400 dbus_error_free(&error);
1407 int config_parse_service_sockets(
1408 const char *filename,
1410 const char *section,
1428 dbus_error_init(&error);
1430 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
1434 if (!(t = strndup(w, l)))
1437 if (!endswith(t, ".socket")) {
1438 log_error("[%s:%u] Unit must be of type socket, ignoring: %s", filename, line, rvalue);
1443 r = manager_load_unit(s->meta.manager, t, NULL, &error, &sock);
1447 log_error("[%s:%u] Failed to load unit %s, ignoring: %s", filename, line, rvalue, bus_error(&error, r));
1448 dbus_error_free(&error);
1452 if ((r = set_ensure_allocated(&s->configured_sockets, trivial_hash_func, trivial_compare_func)) < 0)
1455 if ((r = set_put(s->configured_sockets, sock)) < 0)
1462 int config_parse_unit_env_file(
1463 const char *filename,
1465 const char *section,
1472 char ***env = data, **k;
1481 s = unit_full_printf(u, rvalue);
1485 if (!path_is_absolute(s[0] == '-' ? s + 1 : s)) {
1486 log_error("[%s:%u] Path '%s' is not absolute, ignoring.", filename, line, s);
1491 k = strv_append(*env, s);
1502 int config_parse_ip_tos(
1503 const char *filename,
1505 const char *section,
1512 int *ip_tos = data, x;
1519 if ((x = ip_tos_from_string(rvalue)) < 0)
1520 if (safe_atoi(rvalue, &x) < 0) {
1521 log_error("[%s:%u] Failed to parse IP TOS value, ignoring: %s", filename, line, rvalue);
1529 int config_parse_unit_condition_path(
1530 const char *filename,
1532 const char *section,
1539 ConditionType cond = ltype;
1541 bool trigger, negate;
1549 trigger = rvalue[0] == '|';
1553 negate = rvalue[0] == '!';
1557 if (!path_is_absolute(rvalue)) {
1558 log_error("[%s:%u] Path in condition not absolute, ignoring: %s", filename, line, rvalue);
1562 c = condition_new(cond, rvalue, trigger, negate);
1566 LIST_PREPEND(Condition, conditions, u->meta.conditions, c);
1570 int config_parse_unit_condition_string(
1571 const char *filename,
1573 const char *section,
1580 ConditionType cond = ltype;
1582 bool trigger, negate;
1590 if ((trigger = rvalue[0] == '|'))
1593 if ((negate = rvalue[0] == '!'))
1596 if (!(c = condition_new(cond, rvalue, trigger, negate)))
1599 LIST_PREPEND(Condition, conditions, u->meta.conditions, c);
1603 int config_parse_unit_condition_null(
1604 const char *filename,
1606 const char *section,
1615 bool trigger, negate;
1623 if ((trigger = rvalue[0] == '|'))
1626 if ((negate = rvalue[0] == '!'))
1629 if ((b = parse_boolean(rvalue)) < 0) {
1630 log_error("[%s:%u] Failed to parse boolean value in condition, ignoring: %s", filename, line, rvalue);
1637 if (!(c = condition_new(CONDITION_NULL, NULL, trigger, negate)))
1640 LIST_PREPEND(Condition, conditions, u->meta.conditions, c);
1644 DEFINE_CONFIG_PARSE_ENUM(config_parse_notify_access, notify_access, NotifyAccess, "Failed to parse notify access specifier");
1646 int config_parse_unit_cgroup_attr(
1647 const char *filename,
1649 const char *section,
1665 l = strv_split_quoted(rvalue);
1669 if (strv_length(l) != 2) {
1670 log_error("[%s:%u] Failed to parse cgroup attribute value, ignoring: %s", filename, line, rvalue);
1675 r = unit_add_cgroup_attribute(u, NULL, l[0], l[1], NULL);
1679 log_error("[%s:%u] Failed to add cgroup attribute value, ignoring: %s", filename, line, rvalue);
1686 int config_parse_unit_cpu_shares(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata) {
1697 if (safe_atolu(rvalue, &ul) < 0 || ul < 1) {
1698 log_error("[%s:%u] Failed to parse CPU shares value, ignoring: %s", filename, line, rvalue);
1702 if (asprintf(&t, "%lu", ul) < 0)
1705 r = unit_add_cgroup_attribute(u, "cpu", "cpu.shares", t, NULL);
1709 log_error("[%s:%u] Failed to add cgroup attribute value, ignoring: %s", filename, line, rvalue);
1716 int config_parse_unit_memory_limit(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata) {
1727 if (parse_bytes(rvalue, &sz) < 0 || sz <= 0) {
1728 log_error("[%s:%u] Failed to parse memory limit value, ignoring: %s", filename, line, rvalue);
1732 if (asprintf(&t, "%llu", (unsigned long long) sz) < 0)
1735 r = unit_add_cgroup_attribute(u,
1737 streq(lvalue, "MemorySoftLimit") ? "memory.soft_limit_in_bytes" : "memory.limit_in_bytes",
1742 log_error("[%s:%u] Failed to add cgroup attribute value, ignoring: %s", filename, line, rvalue);
1749 static int device_map(const char *controller, const char *name, const char *value, char **ret) {
1757 l = strv_split_quoted(value);
1761 assert(strv_length(l) >= 1);
1763 if (streq(l[0], "*")) {
1765 if (asprintf(ret, "a *:*%s%s",
1766 isempty(l[1]) ? "" : " ", strempty(l[1])) < 0) {
1774 if (stat(l[0], &st) < 0) {
1775 log_warning("Couldn't stat device %s", l[0]);
1780 if (!S_ISCHR(st.st_mode) && !S_ISBLK(st.st_mode)) {
1781 log_warning("%s is not a device.", l[0]);
1786 if (asprintf(ret, "%c %u:%u%s%s",
1787 S_ISCHR(st.st_mode) ? 'c' : 'b',
1788 major(st.st_rdev), minor(st.st_rdev),
1789 isempty(l[1]) ? "" : " ", strempty(l[1])) < 0) {
1800 int config_parse_unit_device_allow(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata) {
1811 l = strv_split_quoted(rvalue);
1816 if (k < 1 || k > 2) {
1817 log_error("[%s:%u] Failed to parse device value, ignoring: %s", filename, line, rvalue);
1822 if (!streq(l[0], "*") && !path_startswith(l[0], "/dev")) {
1823 log_error("[%s:%u] Device node path not absolute, ignoring: %s", filename, line, rvalue);
1828 if (!isempty(l[1]) && !in_charset(l[1], "rwm")) {
1829 log_error("[%s:%u] Device access string invalid, ignoring: %s", filename, line, rvalue);
1835 r = unit_add_cgroup_attribute(u, "devices",
1836 streq(lvalue, "DeviceAllow") ? "devices.allow" : "devices.deny",
1837 rvalue, device_map);
1840 log_error("[%s:%u] Failed to add cgroup attribute value, ignoring: %s", filename, line, rvalue);
1847 static int blkio_map(const char *controller, const char *name, const char *value, char **ret) {
1857 l = strv_split_quoted(value);
1861 assert(strv_length(l) == 2);
1863 if (stat(l[0], &st) < 0) {
1864 log_warning("Couldn't stat device %s", l[0]);
1869 if (S_ISBLK(st.st_mode))
1871 else if (major(st.st_dev) != 0) {
1872 /* If this is not a device node then find the block
1873 * device this file is stored on */
1876 /* If this is a partition, try to get the originating
1878 block_get_whole_disk(d, &d);
1880 log_warning("%s is not a block device and file system block device cannot be determined or is not local.", l[0]);
1885 if (asprintf(ret, "%u:%u %s", major(d), minor(d), l[1]) < 0) {
1894 int config_parse_unit_blkio_weight(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata) {
1898 const char *device = NULL, *weight;
1907 l = strv_split_quoted(rvalue);
1912 if (k < 1 || k > 2) {
1913 log_error("[%s:%u] Failed to parse weight value, ignoring: %s", filename, line, rvalue);
1925 if (device && !path_is_absolute(device)) {
1926 log_error("[%s:%u] Failed to parse block device node value, ignoring: %s", filename, line, rvalue);
1931 if (safe_atolu(weight, &ul) < 0 || ul < 10 || ul > 1000) {
1932 log_error("[%s:%u] Failed to parse block IO weight value, ignoring: %s", filename, line, rvalue);
1938 r = asprintf(&t, "%s %lu", device, ul);
1940 r = asprintf(&t, "%lu", ul);
1947 r = unit_add_cgroup_attribute(u, "blkio", "blkio.weight_device", t, blkio_map);
1949 r = unit_add_cgroup_attribute(u, "blkio", "blkio.weight", t, NULL);
1953 log_error("[%s:%u] Failed to add cgroup attribute value, ignoring: %s", filename, line, rvalue);
1960 int config_parse_unit_blkio_bandwidth(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata) {
1972 l = strv_split_quoted(rvalue);
1978 log_error("[%s:%u] Failed to parse bandwidth value, ignoring: %s", filename, line, rvalue);
1983 if (!path_is_absolute(l[0])) {
1984 log_error("[%s:%u] Failed to parse block device node value, ignoring: %s", filename, line, rvalue);
1989 if (parse_bytes(l[1], &bytes) < 0 || bytes <= 0) {
1990 log_error("[%s:%u] Failed to parse block IO bandwith value, ignoring: %s", filename, line, rvalue);
1995 r = asprintf(&t, "%s %llu", l[0], (unsigned long long) bytes);
2001 r = unit_add_cgroup_attribute(u, "blkio",
2002 streq(lvalue, "BlockIOReadBandwidth") ? "blkio.read_bps_device" : "blkio.write_bps_device",
2007 log_error("[%s:%u] Failed to add cgroup attribute value, ignoring: %s", filename, line, rvalue);
2015 #define FOLLOW_MAX 8
2017 static int open_follow(char **filename, FILE **_f, Set *names, char **_final) {
2028 /* This will update the filename pointer if the loaded file is
2029 * reached by a symlink. The old string will be freed. */
2032 char *target, *name;
2034 if (c++ >= FOLLOW_MAX)
2037 path_kill_slashes(*filename);
2039 /* Add the file name we are currently looking at to
2040 * the names of this unit, but only if it is a valid
2042 name = file_name_from_path(*filename);
2044 if (unit_name_is_valid(name, true)) {
2046 id = set_get(names, name);
2052 r = set_put(names, id);
2060 /* Try to open the file name, but don't if its a symlink */
2061 if ((fd = open(*filename, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW)) >= 0)
2067 /* Hmm, so this is a symlink. Let's read the name, and follow it manually */
2068 if ((r = readlink_and_make_absolute(*filename, &target)) < 0)
2075 if (!(f = fdopen(fd, "re"))) {
2077 close_nointr_nofail(fd);
2086 static int merge_by_names(Unit **u, Set *names, const char *id) {
2094 /* Let's try to add in all symlink names we found */
2095 while ((k = set_steal_first(names))) {
2097 /* First try to merge in the other name into our
2099 if ((r = unit_merge_by_name(*u, k)) < 0) {
2102 /* Hmm, we couldn't merge the other unit into
2103 * ours? Then let's try it the other way
2106 other = manager_get_unit((*u)->meta.manager, k);
2110 if ((r = unit_merge(other, *u)) >= 0) {
2112 return merge_by_names(u, names, NULL);
2119 unit_choose_id(*u, id);
2127 static int load_from_path(Unit *u, const char *path) {
2131 char *filename = NULL, *id = NULL;
2138 symlink_names = set_new(string_hash_func, string_compare_func);
2142 if (path_is_absolute(path)) {
2144 if (!(filename = strdup(path))) {
2149 if ((r = open_follow(&filename, &f, symlink_names, &id)) < 0) {
2160 STRV_FOREACH(p, u->meta.manager->lookup_paths.unit_path) {
2162 /* Instead of opening the path right away, we manually
2163 * follow all symlinks and add their name to our unit
2164 * name set while doing so */
2165 if (!(filename = path_make_absolute(path, *p))) {
2170 if (u->meta.manager->unit_path_cache &&
2171 !set_get(u->meta.manager->unit_path_cache, filename))
2174 r = open_follow(&filename, &f, symlink_names, &id);
2185 /* Empty the symlink names for the next run */
2186 while ((sn = set_steal_first(symlink_names)))
2197 /* Hmm, no suitable file found? */
2203 if ((r = merge_by_names(&merged, symlink_names, id)) < 0)
2207 u->meta.load_state = UNIT_MERGED;
2213 if (fstat(fileno(f), &st) < 0) {
2218 if (null_or_empty(&st))
2219 u->meta.load_state = UNIT_MASKED;
2221 /* Now, parse the file contents */
2222 r = config_parse(filename, f, UNIT_VTABLE(u)->sections, config_item_perf_lookup, (void*) load_fragment_gperf_lookup, false, u);
2226 u->meta.load_state = UNIT_LOADED;
2229 free(u->meta.fragment_path);
2230 u->meta.fragment_path = filename;
2233 u->meta.fragment_mtime = timespec_load(&st.st_mtim);
2238 set_free_free(symlink_names);
2247 int unit_load_fragment(Unit *u) {
2253 assert(u->meta.load_state == UNIT_STUB);
2256 /* First, try to find the unit under its id. We always look
2257 * for unit files in the default directories, to make it easy
2258 * to override things by placing things in /etc/systemd/system */
2259 if ((r = load_from_path(u, u->meta.id)) < 0)
2262 /* Try to find an alias we can load this with */
2263 if (u->meta.load_state == UNIT_STUB)
2264 SET_FOREACH(t, u->meta.names, i) {
2266 if (t == u->meta.id)
2269 if ((r = load_from_path(u, t)) < 0)
2272 if (u->meta.load_state != UNIT_STUB)
2276 /* And now, try looking for it under the suggested (originally linked) path */
2277 if (u->meta.load_state == UNIT_STUB && u->meta.fragment_path) {
2279 if ((r = load_from_path(u, u->meta.fragment_path)) < 0)
2282 if (u->meta.load_state == UNIT_STUB) {
2283 /* Hmm, this didn't work? Then let's get rid
2284 * of the fragment path stored for us, so that
2285 * we don't point to an invalid location. */
2286 free(u->meta.fragment_path);
2287 u->meta.fragment_path = NULL;
2291 /* Look for a template */
2292 if (u->meta.load_state == UNIT_STUB && u->meta.instance) {
2295 if (!(k = unit_name_template(u->meta.id)))
2298 r = load_from_path(u, k);
2304 if (u->meta.load_state == UNIT_STUB)
2305 SET_FOREACH(t, u->meta.names, i) {
2307 if (t == u->meta.id)
2310 if (!(k = unit_name_template(t)))
2313 r = load_from_path(u, k);
2319 if (u->meta.load_state != UNIT_STUB)
2327 void unit_dump_config_items(FILE *f) {
2328 static const struct {
2329 const ConfigParserCallback callback;
2332 { config_parse_int, "INTEGER" },
2333 { config_parse_unsigned, "UNSIGNED" },
2334 { config_parse_size, "SIZE" },
2335 { config_parse_bool, "BOOLEAN" },
2336 { config_parse_string, "STRING" },
2337 { config_parse_path, "PATH" },
2338 { config_parse_unit_path_printf, "PATH" },
2339 { config_parse_strv, "STRING [...]" },
2340 { config_parse_exec_nice, "NICE" },
2341 { config_parse_exec_oom_score_adjust, "OOMSCOREADJUST" },
2342 { config_parse_exec_io_class, "IOCLASS" },
2343 { config_parse_exec_io_priority, "IOPRIORITY" },
2344 { config_parse_exec_cpu_sched_policy, "CPUSCHEDPOLICY" },
2345 { config_parse_exec_cpu_sched_prio, "CPUSCHEDPRIO" },
2346 { config_parse_exec_cpu_affinity, "CPUAFFINITY" },
2347 { config_parse_mode, "MODE" },
2348 { config_parse_unit_env_file, "FILE" },
2349 { config_parse_output, "OUTPUT" },
2350 { config_parse_input, "INPUT" },
2351 { config_parse_facility, "FACILITY" },
2352 { config_parse_level, "LEVEL" },
2353 { config_parse_exec_capabilities, "CAPABILITIES" },
2354 { config_parse_exec_secure_bits, "SECUREBITS" },
2355 { config_parse_exec_bounding_set, "BOUNDINGSET" },
2356 { config_parse_exec_timer_slack_nsec, "TIMERSLACK" },
2357 { config_parse_limit, "LIMIT" },
2358 { config_parse_unit_cgroup, "CGROUP [...]" },
2359 { config_parse_unit_deps, "UNIT [...]" },
2360 { config_parse_unit_names, "UNIT [...]" },
2361 { config_parse_exec, "PATH [ARGUMENT [...]]" },
2362 { config_parse_service_type, "SERVICETYPE" },
2363 { config_parse_service_restart, "SERVICERESTART" },
2364 #ifdef HAVE_SYSV_COMPAT
2365 { config_parse_sysv_priority, "SYSVPRIORITY" },
2367 { config_parse_warn_compat, "NOTSUPPORTED" },
2369 { config_parse_kill_mode, "KILLMODE" },
2370 { config_parse_kill_signal, "SIGNAL" },
2371 { config_parse_socket_listen, "SOCKET [...]" },
2372 { config_parse_socket_bind, "SOCKETBIND" },
2373 { config_parse_socket_bindtodevice, "NETWORKINTERFACE" },
2374 { config_parse_usec, "SECONDS" },
2375 { config_parse_path_strv, "PATH [...]" },
2376 { config_parse_exec_mount_flags, "MOUNTFLAG [...]" },
2377 { config_parse_unit_string_printf, "STRING" },
2378 { config_parse_timer, "TIMER" },
2379 { config_parse_timer_unit, "NAME" },
2380 { config_parse_path_spec, "PATH" },
2381 { config_parse_path_unit, "UNIT" },
2382 { config_parse_notify_access, "ACCESS" },
2383 { config_parse_ip_tos, "TOS" },
2384 { config_parse_unit_condition_path, "CONDITION" },
2385 { config_parse_unit_condition_string, "CONDITION" },
2386 { config_parse_unit_condition_null, "CONDITION" },
2389 const char *prev = NULL;
2394 NULSTR_FOREACH(i, load_fragment_gperf_nulstr) {
2395 const char *rvalue = "OTHER", *lvalue;
2399 const ConfigPerfItem *p;
2401 assert_se(p = load_fragment_gperf_lookup(i, strlen(i)));
2403 dot = strchr(i, '.');
2404 lvalue = dot ? dot + 1 : i;
2408 if (!prev || strncmp(prev, i, prefix_len+1) != 0) {
2412 fprintf(f, "[%.*s]\n", (int) prefix_len, i);
2415 for (j = 0; j < ELEMENTSOF(table); j++)
2416 if (p->parse == table[j].callback) {
2417 rvalue = table[j].rvalue;
2421 fprintf(f, "%s=%s\n", lvalue, rvalue);