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 static 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 static int config_parse_deps(
73 UnitDependency d = PTR_TO_UINT(data);
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 static int config_parse_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 static int config_parse_string_printf(
158 const char *filename,
177 if (!(k = unit_full_printf(u, rvalue)))
191 static int config_parse_listen(
192 const char *filename,
211 if (!(p = new0(SocketPort, 1)))
214 if (streq(lvalue, "ListenFIFO")) {
215 p->type = SOCKET_FIFO;
217 if (!(p->path = strdup(rvalue))) {
222 path_kill_slashes(p->path);
223 } else if (streq(lvalue, "ListenNetlink")) {
224 p->type = SOCKET_SOCKET;
226 if (socket_address_parse_netlink(&p->address, rvalue) < 0) {
227 log_error("[%s:%u] Failed to parse address value, ignoring: %s", filename, line, rvalue);
233 p->type = SOCKET_SOCKET;
235 if (socket_address_parse(&p->address, rvalue) < 0) {
236 log_error("[%s:%u] Failed to parse address value, ignoring: %s", filename, line, rvalue);
241 if (streq(lvalue, "ListenStream"))
242 p->address.type = SOCK_STREAM;
243 else if (streq(lvalue, "ListenDatagram"))
244 p->address.type = SOCK_DGRAM;
246 assert(streq(lvalue, "ListenSequentialPacket"));
247 p->address.type = SOCK_SEQPACKET;
250 if (socket_address_family(&p->address) != AF_LOCAL && p->address.type == SOCK_SEQPACKET) {
251 log_error("[%s:%u] Address family not supported, ignoring: %s", filename, line, rvalue);
258 LIST_PREPEND(SocketPort, port, s->ports, p);
263 static int config_parse_socket_bind(
264 const char *filename,
274 SocketAddressBindIPv6Only b;
283 if ((b = socket_address_bind_ipv6_only_from_string(rvalue)) < 0) {
286 if ((r = parse_boolean(rvalue)) < 0) {
287 log_error("[%s:%u] Failed to parse bind IPv6 only value, ignoring: %s", filename, line, rvalue);
291 s->bind_ipv6_only = r ? SOCKET_ADDRESS_IPV6_ONLY : SOCKET_ADDRESS_BOTH;
293 s->bind_ipv6_only = b;
298 static int config_parse_nice(
299 const char *filename,
308 ExecContext *c = data;
316 if (safe_atoi(rvalue, &priority) < 0) {
317 log_error("[%s:%u] Failed to parse nice priority, ignoring: %s. ", filename, line, rvalue);
321 if (priority < PRIO_MIN || priority >= PRIO_MAX) {
322 log_error("[%s:%u] Nice priority out of range, ignoring: %s", filename, line, rvalue);
332 static int config_parse_oom_score_adjust(
333 const char *filename,
342 ExecContext *c = data;
350 if (safe_atoi(rvalue, &oa) < 0) {
351 log_error("[%s:%u] Failed to parse the OOM score adjust value, ignoring: %s", filename, line, rvalue);
355 if (oa < OOM_SCORE_ADJ_MIN || oa > OOM_SCORE_ADJ_MAX) {
356 log_error("[%s:%u] OOM score adjust value out of range, ignoring: %s", filename, line, rvalue);
360 c->oom_score_adjust = oa;
361 c->oom_score_adjust_set = true;
366 static int config_parse_mode(
367 const char *filename,
386 l = strtol(rvalue, &x, 8);
387 if (!x || *x || errno) {
388 log_error("[%s:%u] Failed to parse mode value, ignoring: %s", filename, line, rvalue);
392 if (l < 0000 || l > 07777) {
393 log_error("[%s:%u] mode value out of range, ignoring: %s", filename, line, rvalue);
401 static int config_parse_exec(
402 const char *filename,
411 ExecCommand **e = data, *nce;
420 /* We accept an absolute path as first argument, or
421 * alternatively an absolute prefixed with @ to allow
422 * overriding of argv[0]. */
428 bool honour_argv0 = false, ignore = false;
434 rvalue += strspn(rvalue, WHITESPACE);
439 if (rvalue[0] == '-') {
444 if (rvalue[0] == '@') {
449 if (*rvalue != '/') {
450 log_error("[%s:%u] Invalid executable path in command line, ignoring: %s", filename, line, rvalue);
455 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
456 if (strncmp(w, ";", MAX(l, 1U)) == 0)
462 if (!(n = new(char*, k + !honour_argv0)))
466 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
467 if (strncmp(w, ";", MAX(l, 1U)) == 0)
470 if (honour_argv0 && w == rvalue) {
472 if (!(path = cunescape_length(w, l)))
475 if (!(n[k++] = cunescape_length(w, l)))
483 log_error("[%s:%u] Invalid command line, ignoring: %s", filename, line, rvalue);
489 if (!(path = strdup(n[0])))
492 assert(path_is_absolute(path));
494 if (!(nce = new0(ExecCommand, 1)))
499 nce->ignore = ignore;
501 path_kill_slashes(nce->path);
503 exec_command_append_list(e, nce);
519 static int config_parse_usec(
520 const char *filename,
536 if (parse_usec(rvalue, usec) < 0) {
537 log_error("[%s:%u] Failed to parse time value, ignoring: %s", filename, line, rvalue);
544 static DEFINE_CONFIG_PARSE_ENUM(config_parse_service_type, service_type, ServiceType, "Failed to parse service type");
545 static DEFINE_CONFIG_PARSE_ENUM(config_parse_service_restart, service_restart, ServiceRestart, "Failed to parse service restart specifier");
547 static int config_parse_bindtodevice(
548 const char *filename,
565 if (rvalue[0] && !streq(rvalue, "*")) {
566 if (!(n = strdup(rvalue)))
571 free(s->bind_to_device);
572 s->bind_to_device = n;
577 static DEFINE_CONFIG_PARSE_ENUM(config_parse_output, exec_output, ExecOutput, "Failed to parse output specifier");
578 static DEFINE_CONFIG_PARSE_ENUM(config_parse_input, exec_input, ExecInput, "Failed to parse input specifier");
580 static int config_parse_facility(
581 const char *filename,
598 if ((x = log_facility_unshifted_from_string(rvalue)) < 0) {
599 log_error("[%s:%u] Failed to parse log facility, ignoring: %s", filename, line, rvalue);
603 *o = (x << 3) | LOG_PRI(*o);
608 static int config_parse_level(
609 const char *filename,
626 if ((x = log_level_from_string(rvalue)) < 0) {
627 log_error("[%s:%u] Failed to parse log level, ignoring: %s", filename, line, rvalue);
631 *o = (*o & LOG_FACMASK) | x;
635 static int config_parse_io_class(
636 const char *filename,
645 ExecContext *c = data;
653 if ((x = ioprio_class_from_string(rvalue)) < 0) {
654 log_error("[%s:%u] Failed to parse IO scheduling class, ignoring: %s", filename, line, rvalue);
658 c->ioprio = IOPRIO_PRIO_VALUE(x, IOPRIO_PRIO_DATA(c->ioprio));
659 c->ioprio_set = true;
664 static int config_parse_io_priority(
665 const char *filename,
674 ExecContext *c = data;
682 if (safe_atoi(rvalue, &i) < 0 || i < 0 || i >= IOPRIO_BE_NR) {
683 log_error("[%s:%u] Failed to parse io priority, ignoring: %s", filename, line, rvalue);
687 c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_PRIO_CLASS(c->ioprio), i);
688 c->ioprio_set = true;
693 static int config_parse_cpu_sched_policy(
694 const char *filename,
704 ExecContext *c = data;
712 if ((x = sched_policy_from_string(rvalue)) < 0) {
713 log_error("[%s:%u] Failed to parse CPU scheduling policy, ignoring: %s", filename, line, rvalue);
717 c->cpu_sched_policy = x;
718 c->cpu_sched_set = true;
723 static int config_parse_cpu_sched_prio(
724 const char *filename,
733 ExecContext *c = data;
741 /* On Linux RR/FIFO have the same range */
742 if (safe_atoi(rvalue, &i) < 0 || i < sched_get_priority_min(SCHED_RR) || i > sched_get_priority_max(SCHED_RR)) {
743 log_error("[%s:%u] Failed to parse CPU scheduling priority, ignoring: %s", filename, line, rvalue);
747 c->cpu_sched_priority = i;
748 c->cpu_sched_set = true;
753 static int config_parse_cpu_affinity(
754 const char *filename,
763 ExecContext *c = data;
773 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
778 if (!(t = strndup(w, l)))
781 r = safe_atou(t, &cpu);
785 if (!(c->cpuset = cpu_set_malloc(&c->cpuset_ncpus)))
788 if (r < 0 || cpu >= c->cpuset_ncpus) {
789 log_error("[%s:%u] Failed to parse CPU affinity, ignoring: %s", filename, line, rvalue);
793 CPU_SET_S(cpu, CPU_ALLOC_SIZE(c->cpuset_ncpus), c->cpuset);
799 static int config_parse_capabilities(
800 const char *filename,
809 ExecContext *c = data;
817 if (!(cap = cap_from_text(rvalue))) {
821 log_error("[%s:%u] Failed to parse capabilities, ignoring: %s", filename, line, rvalue);
826 cap_free(c->capabilities);
827 c->capabilities = cap;
832 static int config_parse_secure_bits(
833 const char *filename,
842 ExecContext *c = data;
852 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
853 if (first_word(w, "keep-caps"))
854 c->secure_bits |= SECURE_KEEP_CAPS;
855 else if (first_word(w, "keep-caps-locked"))
856 c->secure_bits |= SECURE_KEEP_CAPS_LOCKED;
857 else if (first_word(w, "no-setuid-fixup"))
858 c->secure_bits |= SECURE_NO_SETUID_FIXUP;
859 else if (first_word(w, "no-setuid-fixup-locked"))
860 c->secure_bits |= SECURE_NO_SETUID_FIXUP_LOCKED;
861 else if (first_word(w, "noroot"))
862 c->secure_bits |= SECURE_NOROOT;
863 else if (first_word(w, "noroot-locked"))
864 c->secure_bits |= SECURE_NOROOT_LOCKED;
866 log_error("[%s:%u] Failed to parse secure bits, ignoring: %s", filename, line, rvalue);
874 static int config_parse_bounding_set(
875 const char *filename,
884 ExecContext *c = data;
896 if (rvalue[0] == '~') {
901 /* Note that we store this inverted internally, since the
902 * kernel wants it like this. But we actually expose it
903 * non-inverted everywhere to have a fully normalized
906 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
911 if (!(t = strndup(w, l)))
914 r = cap_from_name(t, &cap);
918 log_error("[%s:%u] Failed to parse capability bounding set, ignoring: %s", filename, line, rvalue);
922 sum |= ((uint64_t) 1ULL) << (uint64_t) cap;
926 c->capability_bounding_set_drop |= sum;
928 c->capability_bounding_set_drop |= ~sum;
933 static int config_parse_timer_slack_nsec(
934 const char *filename,
943 ExecContext *c = data;
951 if (safe_atolu(rvalue, &u) < 0) {
952 log_error("[%s:%u] Failed to parse time slack value, ignoring: %s", filename, line, rvalue);
956 c->timer_slack_nsec = u;
961 static int config_parse_limit(
962 const char *filename,
971 struct rlimit **rl = data;
972 unsigned long long u;
979 if (streq(rvalue, "infinity"))
980 u = (unsigned long long) RLIM_INFINITY;
981 else if (safe_atollu(rvalue, &u) < 0) {
982 log_error("[%s:%u] Failed to parse resource value, ignoring: %s", filename, line, rvalue);
987 if (!(*rl = new(struct rlimit, 1)))
990 (*rl)->rlim_cur = (*rl)->rlim_max = (rlim_t) u;
994 static int config_parse_cgroup(
995 const char *filename,
1009 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
1013 if (!(t = cunescape_length(w, l)))
1016 r = unit_add_cgroup_from_text(u, t);
1020 log_error("[%s:%u] Failed to parse cgroup value, ignoring: %s", filename, line, rvalue);
1028 #ifdef HAVE_SYSV_COMPAT
1029 static int config_parse_sysv_priority(
1030 const char *filename,
1032 const char *section,
1039 int *priority = data;
1047 if (safe_atoi(rvalue, &i) < 0 || i < 0) {
1048 log_error("[%s:%u] Failed to parse SysV start priority, ignoring: %s", filename, line, rvalue);
1052 *priority = (int) i;
1057 static int config_parse_fsck_passno(
1058 const char *filename,
1060 const char *section,
1075 if (safe_atoi(rvalue, &i) || i < 0) {
1076 log_error("[%s:%u] Failed to parse fsck pass number, ignoring: %s", filename, line, rvalue);
1084 static DEFINE_CONFIG_PARSE_ENUM(config_parse_kill_mode, kill_mode, KillMode, "Failed to parse kill mode");
1086 static int config_parse_kill_signal(
1087 const char *filename,
1089 const char *section,
1104 if ((r = signal_from_string_try_harder(rvalue)) <= 0) {
1105 log_error("[%s:%u] Failed to parse kill signal, ignoring: %s", filename, line, rvalue);
1113 static int config_parse_mount_flags(
1114 const char *filename,
1116 const char *section,
1123 ExecContext *c = data;
1127 unsigned long flags = 0;
1134 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
1135 if (strncmp(w, "shared", MAX(l, 6U)) == 0)
1137 else if (strncmp(w, "slave", MAX(l, 5U)) == 0)
1139 else if (strncmp(w, "private", MAX(l, 7U)) == 0)
1140 flags |= MS_PRIVATE;
1142 log_error("[%s:%u] Failed to parse mount flags, ignoring: %s", filename, line, rvalue);
1147 c->mount_flags = flags;
1151 static int config_parse_timer(
1152 const char *filename,
1154 const char *section,
1171 if ((b = timer_base_from_string(lvalue)) < 0) {
1172 log_error("[%s:%u] Failed to parse timer base, ignoring: %s", filename, line, lvalue);
1176 if (parse_usec(rvalue, &u) < 0) {
1177 log_error("[%s:%u] Failed to parse timer value, ignoring: %s", filename, line, rvalue);
1181 if (!(v = new0(TimerValue, 1)))
1187 LIST_PREPEND(TimerValue, value, t->values, v);
1192 static int config_parse_timer_unit(
1193 const char *filename,
1195 const char *section,
1211 dbus_error_init(&error);
1213 if (endswith(rvalue, ".timer")) {
1214 log_error("[%s:%u] Unit cannot be of type timer, ignoring: %s", filename, line, rvalue);
1218 if ((r = manager_load_unit(t->meta.manager, rvalue, NULL, NULL, &t->unit)) < 0) {
1219 log_error("[%s:%u] Failed to load unit %s, ignoring: %s", filename, line, rvalue, bus_error(&error, r));
1220 dbus_error_free(&error);
1227 static int config_parse_path_spec(
1228 const char *filename,
1230 const char *section,
1246 if ((b = path_type_from_string(lvalue)) < 0) {
1247 log_error("[%s:%u] Failed to parse path type, ignoring: %s", filename, line, lvalue);
1251 if (!path_is_absolute(rvalue)) {
1252 log_error("[%s:%u] Path is not absolute, ignoring: %s", filename, line, rvalue);
1256 if (!(s = new0(PathSpec, 1)))
1259 if (!(s->path = strdup(rvalue))) {
1264 path_kill_slashes(s->path);
1269 LIST_PREPEND(PathSpec, spec, p->specs, s);
1274 static int config_parse_path_unit(
1275 const char *filename,
1277 const char *section,
1293 dbus_error_init(&error);
1295 if (endswith(rvalue, ".path")) {
1296 log_error("[%s:%u] Unit cannot be of type path, ignoring: %s", filename, line, rvalue);
1300 if ((r = manager_load_unit(t->meta.manager, rvalue, NULL, &error, &t->unit)) < 0) {
1301 log_error("[%s:%u] Failed to load unit %s, ignoring: %s", filename, line, rvalue, bus_error(&error, r));
1302 dbus_error_free(&error);
1309 static int config_parse_socket_service(
1310 const char *filename,
1312 const char *section,
1328 dbus_error_init(&error);
1330 if (!endswith(rvalue, ".service")) {
1331 log_error("[%s:%u] Unit must be of type service, ignoring: %s", filename, line, rvalue);
1335 if ((r = manager_load_unit(s->meta.manager, rvalue, NULL, &error, (Unit**) &s->service)) < 0) {
1336 log_error("[%s:%u] Failed to load unit %s, ignoring: %s", filename, line, rvalue, bus_error(&error, r));
1337 dbus_error_free(&error);
1344 static int config_parse_service_sockets(
1345 const char *filename,
1347 const char *section,
1365 dbus_error_init(&error);
1367 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
1371 if (!(t = strndup(w, l)))
1374 if (!endswith(t, ".socket")) {
1375 log_error("[%s:%u] Unit must be of type socket, ignoring: %s", filename, line, rvalue);
1380 r = manager_load_unit(s->meta.manager, t, NULL, &error, &sock);
1384 log_error("[%s:%u] Failed to load unit %s, ignoring: %s", filename, line, rvalue, bus_error(&error, r));
1385 dbus_error_free(&error);
1389 if ((r = set_ensure_allocated(&s->configured_sockets, trivial_hash_func, trivial_compare_func)) < 0)
1392 if ((r = set_put(s->configured_sockets, sock)) < 0)
1399 static int config_parse_env_file(
1400 const char *filename,
1402 const char *section,
1409 char ***env = data, **k;
1416 if (!path_is_absolute(rvalue[0] == '-' ? rvalue + 1 : rvalue)) {
1417 log_error("[%s:%u] Path '%s' is not absolute, ignoring.", filename, line, rvalue);
1421 if (!(k = strv_append(*env, rvalue)))
1430 static int config_parse_ip_tos(
1431 const char *filename,
1433 const char *section,
1440 int *ip_tos = data, x;
1447 if ((x = ip_tos_from_string(rvalue)) < 0)
1448 if (safe_atoi(rvalue, &x) < 0) {
1449 log_error("[%s:%u] Failed to parse IP TOS value, ignoring: %s", filename, line, rvalue);
1457 static int config_parse_condition_path(
1458 const char *filename,
1460 const char *section,
1467 ConditionType cond = ltype;
1469 bool trigger, negate;
1477 if ((trigger = rvalue[0] == '|'))
1480 if ((negate = rvalue[0] == '!'))
1483 if (!path_is_absolute(rvalue)) {
1484 log_error("[%s:%u] Path in condition not absolute, ignoring: %s", filename, line, rvalue);
1488 if (!(c = condition_new(cond, rvalue, trigger, negate)))
1491 LIST_PREPEND(Condition, conditions, u->meta.conditions, c);
1495 static int config_parse_condition_string(
1496 const char *filename,
1498 const char *section,
1505 ConditionType cond = ltype;
1507 bool trigger, negate;
1515 if ((trigger = rvalue[0] == '|'))
1518 if ((negate = rvalue[0] == '!'))
1521 if (!(c = condition_new(cond, rvalue, trigger, negate)))
1524 LIST_PREPEND(Condition, conditions, u->meta.conditions, c);
1528 static int config_parse_condition_null(
1529 const char *filename,
1531 const char *section,
1540 bool trigger, negate;
1548 if ((trigger = rvalue[0] == '|'))
1551 if ((negate = rvalue[0] == '!'))
1554 if ((b = parse_boolean(rvalue)) < 0) {
1555 log_error("[%s:%u] Failed to parse boolean value in condition, ignoring: %s", filename, line, rvalue);
1562 if (!(c = condition_new(CONDITION_NULL, NULL, trigger, negate)))
1565 LIST_PREPEND(Condition, conditions, u->meta.conditions, c);
1569 static DEFINE_CONFIG_PARSE_ENUM(config_parse_notify_access, notify_access, NotifyAccess, "Failed to parse notify access specifier");
1571 #define FOLLOW_MAX 8
1573 static int open_follow(char **filename, FILE **_f, Set *names, char **_final) {
1584 /* This will update the filename pointer if the loaded file is
1585 * reached by a symlink. The old string will be freed. */
1588 char *target, *name;
1590 if (c++ >= FOLLOW_MAX)
1593 path_kill_slashes(*filename);
1595 /* Add the file name we are currently looking at to
1596 * the names of this unit, but only if it is a valid
1598 name = file_name_from_path(*filename);
1600 if (unit_name_is_valid(name, false)) {
1601 if (!(id = set_get(names, name))) {
1603 if (!(id = strdup(name)))
1606 if ((r = set_put(names, id)) < 0) {
1613 /* Try to open the file name, but don't if its a symlink */
1614 if ((fd = open(*filename, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW)) >= 0)
1620 /* Hmm, so this is a symlink. Let's read the name, and follow it manually */
1621 if ((r = readlink_and_make_absolute(*filename, &target)) < 0)
1628 if (!(f = fdopen(fd, "re"))) {
1630 close_nointr_nofail(fd);
1639 static int merge_by_names(Unit **u, Set *names, const char *id) {
1647 /* Let's try to add in all symlink names we found */
1648 while ((k = set_steal_first(names))) {
1650 /* First try to merge in the other name into our
1652 if ((r = unit_merge_by_name(*u, k)) < 0) {
1655 /* Hmm, we couldn't merge the other unit into
1656 * ours? Then let's try it the other way
1659 other = manager_get_unit((*u)->meta.manager, k);
1663 if ((r = unit_merge(other, *u)) >= 0) {
1665 return merge_by_names(u, names, NULL);
1672 unit_choose_id(*u, id);
1680 static void dump_items(FILE *f, const ConfigItem *items) {
1681 const ConfigItem *i;
1682 const char *prev_section = NULL;
1683 bool not_first = false;
1686 ConfigParserCallback callback;
1689 { config_parse_int, "INTEGER" },
1690 { config_parse_unsigned, "UNSIGNED" },
1691 { config_parse_size, "SIZE" },
1692 { config_parse_bool, "BOOLEAN" },
1693 { config_parse_string, "STRING" },
1694 { config_parse_path, "PATH" },
1695 { config_parse_strv, "STRING [...]" },
1696 { config_parse_nice, "NICE" },
1697 { config_parse_oom_score_adjust, "OOMSCOREADJUST" },
1698 { config_parse_io_class, "IOCLASS" },
1699 { config_parse_io_priority, "IOPRIORITY" },
1700 { config_parse_cpu_sched_policy, "CPUSCHEDPOLICY" },
1701 { config_parse_cpu_sched_prio, "CPUSCHEDPRIO" },
1702 { config_parse_cpu_affinity, "CPUAFFINITY" },
1703 { config_parse_mode, "MODE" },
1704 { config_parse_env_file, "FILE" },
1705 { config_parse_output, "OUTPUT" },
1706 { config_parse_input, "INPUT" },
1707 { config_parse_facility, "FACILITY" },
1708 { config_parse_level, "LEVEL" },
1709 { config_parse_capabilities, "CAPABILITIES" },
1710 { config_parse_secure_bits, "SECUREBITS" },
1711 { config_parse_bounding_set, "BOUNDINGSET" },
1712 { config_parse_timer_slack_nsec, "TIMERSLACK" },
1713 { config_parse_limit, "LIMIT" },
1714 { config_parse_cgroup, "CGROUP [...]" },
1715 { config_parse_deps, "UNIT [...]" },
1716 { config_parse_names, "UNIT [...]" },
1717 { config_parse_exec, "PATH [ARGUMENT [...]]" },
1718 { config_parse_service_type, "SERVICETYPE" },
1719 { config_parse_service_restart, "SERVICERESTART" },
1720 #ifdef HAVE_SYSV_COMPAT
1721 { config_parse_sysv_priority, "SYSVPRIORITY" },
1723 { config_parse_warn_compat, "NOTSUPPORTED" },
1725 { config_parse_kill_mode, "KILLMODE" },
1726 { config_parse_kill_signal, "SIGNAL" },
1727 { config_parse_listen, "SOCKET [...]" },
1728 { config_parse_socket_bind, "SOCKETBIND" },
1729 { config_parse_bindtodevice, "NETWORKINTERFACE" },
1730 { config_parse_usec, "SECONDS" },
1731 { config_parse_path_strv, "PATH [...]" },
1732 { config_parse_mount_flags, "MOUNTFLAG [...]" },
1733 { config_parse_string_printf, "STRING" },
1734 { config_parse_timer, "TIMER" },
1735 { config_parse_timer_unit, "NAME" },
1736 { config_parse_path_spec, "PATH" },
1737 { config_parse_path_unit, "UNIT" },
1738 { config_parse_notify_access, "ACCESS" },
1739 { config_parse_ip_tos, "TOS" },
1740 { config_parse_condition_path, "CONDITION" },
1741 { config_parse_condition_string, "CONDITION" },
1742 { config_parse_condition_null, "CONDITION" },
1748 for (i = items; i->lvalue; i++) {
1750 const char *rvalue = "OTHER";
1752 if (!streq_ptr(i->section, prev_section)) {
1758 fprintf(f, "[%s]\n", i->section);
1759 prev_section = i->section;
1762 for (j = 0; j < ELEMENTSOF(table); j++)
1763 if (i->parse == table[j].callback) {
1764 rvalue = table[j].rvalue;
1768 fprintf(f, "%s=%s\n", i->lvalue, rvalue);
1772 static int load_from_path(Unit *u, const char *path) {
1774 static const char* const section_table[_UNIT_TYPE_MAX] = {
1775 [UNIT_SERVICE] = "Service",
1776 [UNIT_TIMER] = "Timer",
1777 [UNIT_SOCKET] = "Socket",
1778 [UNIT_TARGET] = "Target",
1779 [UNIT_DEVICE] = "Device",
1780 [UNIT_MOUNT] = "Mount",
1781 [UNIT_AUTOMOUNT] = "Automount",
1782 [UNIT_SNAPSHOT] = "Snapshot",
1783 [UNIT_SWAP] = "Swap",
1784 [UNIT_PATH] = "Path"
1787 #define EXEC_CONTEXT_CONFIG_ITEMS(context, section) \
1788 { "WorkingDirectory", config_parse_path, 0, &(context).working_directory, section }, \
1789 { "RootDirectory", config_parse_path, 0, &(context).root_directory, section }, \
1790 { "User", config_parse_string_printf, 0, &(context).user, section }, \
1791 { "Group", config_parse_string_printf, 0, &(context).group, section }, \
1792 { "SupplementaryGroups", config_parse_strv, 0, &(context).supplementary_groups, section }, \
1793 { "Nice", config_parse_nice, 0, &(context), section }, \
1794 { "OOMScoreAdjust", config_parse_oom_score_adjust,0, &(context), section }, \
1795 { "IOSchedulingClass", config_parse_io_class, 0, &(context), section }, \
1796 { "IOSchedulingPriority", config_parse_io_priority, 0, &(context), section }, \
1797 { "CPUSchedulingPolicy", config_parse_cpu_sched_policy,0, &(context), section }, \
1798 { "CPUSchedulingPriority", config_parse_cpu_sched_prio, 0, &(context), section }, \
1799 { "CPUSchedulingResetOnFork", config_parse_bool, 0, &(context).cpu_sched_reset_on_fork, section }, \
1800 { "CPUAffinity", config_parse_cpu_affinity, 0, &(context), section }, \
1801 { "UMask", config_parse_mode, 0, &(context).umask, section }, \
1802 { "Environment", config_parse_strv, 0, &(context).environment, section }, \
1803 { "EnvironmentFile", config_parse_env_file, 0, &(context).environment_files, section }, \
1804 { "StandardInput", config_parse_input, 0, &(context).std_input, section }, \
1805 { "StandardOutput", config_parse_output, 0, &(context).std_output, section }, \
1806 { "StandardError", config_parse_output, 0, &(context).std_error, section }, \
1807 { "TTYPath", config_parse_path, 0, &(context).tty_path, section }, \
1808 { "SyslogIdentifier", config_parse_string_printf, 0, &(context).syslog_identifier, section }, \
1809 { "SyslogFacility", config_parse_facility, 0, &(context).syslog_priority, section }, \
1810 { "SyslogLevel", config_parse_level, 0, &(context).syslog_priority, section }, \
1811 { "SyslogLevelPrefix", config_parse_bool, 0, &(context).syslog_level_prefix, section }, \
1812 { "Capabilities", config_parse_capabilities, 0, &(context), section }, \
1813 { "SecureBits", config_parse_secure_bits, 0, &(context), section }, \
1814 { "CapabilityBoundingSet", config_parse_bounding_set, 0, &(context), section }, \
1815 { "TimerSlackNSec", config_parse_timer_slack_nsec,0, &(context), section }, \
1816 { "LimitCPU", config_parse_limit, 0, &(context).rlimit[RLIMIT_CPU], section }, \
1817 { "LimitFSIZE", config_parse_limit, 0, &(context).rlimit[RLIMIT_FSIZE], section }, \
1818 { "LimitDATA", config_parse_limit, 0, &(context).rlimit[RLIMIT_DATA], section }, \
1819 { "LimitSTACK", config_parse_limit, 0, &(context).rlimit[RLIMIT_STACK], section }, \
1820 { "LimitCORE", config_parse_limit, 0, &(context).rlimit[RLIMIT_CORE], section }, \
1821 { "LimitRSS", config_parse_limit, 0, &(context).rlimit[RLIMIT_RSS], section }, \
1822 { "LimitNOFILE", config_parse_limit, 0, &(context).rlimit[RLIMIT_NOFILE], section }, \
1823 { "LimitAS", config_parse_limit, 0, &(context).rlimit[RLIMIT_AS], section }, \
1824 { "LimitNPROC", config_parse_limit, 0, &(context).rlimit[RLIMIT_NPROC], section }, \
1825 { "LimitMEMLOCK", config_parse_limit, 0, &(context).rlimit[RLIMIT_MEMLOCK], section }, \
1826 { "LimitLOCKS", config_parse_limit, 0, &(context).rlimit[RLIMIT_LOCKS], section }, \
1827 { "LimitSIGPENDING", config_parse_limit, 0, &(context).rlimit[RLIMIT_SIGPENDING], section }, \
1828 { "LimitMSGQUEUE", config_parse_limit, 0, &(context).rlimit[RLIMIT_MSGQUEUE], section }, \
1829 { "LimitNICE", config_parse_limit, 0, &(context).rlimit[RLIMIT_NICE], section }, \
1830 { "LimitRTPRIO", config_parse_limit, 0, &(context).rlimit[RLIMIT_RTPRIO], section }, \
1831 { "LimitRTTIME", config_parse_limit, 0, &(context).rlimit[RLIMIT_RTTIME], section }, \
1832 { "ControlGroup", config_parse_cgroup, 0, u, section }, \
1833 { "ReadWriteDirectories", config_parse_path_strv, 0, &(context).read_write_dirs, section }, \
1834 { "ReadOnlyDirectories", config_parse_path_strv, 0, &(context).read_only_dirs, section }, \
1835 { "InaccessibleDirectories",config_parse_path_strv, 0, &(context).inaccessible_dirs, section }, \
1836 { "PrivateTmp", config_parse_bool, 0, &(context).private_tmp, section }, \
1837 { "MountFlags", config_parse_mount_flags, 0, &(context), section }, \
1838 { "TCPWrapName", config_parse_string_printf, 0, &(context).tcpwrap_name, section }, \
1839 { "PAMName", config_parse_string_printf, 0, &(context).pam_name, section }, \
1840 { "KillMode", config_parse_kill_mode, 0, &(context).kill_mode, section }, \
1841 { "KillSignal", config_parse_kill_signal, 0, &(context).kill_signal, section }, \
1842 { "SendSIGKILL", config_parse_bool, 0, &(context).send_sigkill, section }, \
1843 { "UtmpIdentifier", config_parse_string_printf, 0, &(context).utmp_id, section }
1845 const ConfigItem items[] = {
1846 { "Names", config_parse_names, 0, u, "Unit" },
1847 { "Description", config_parse_string_printf, 0, &u->meta.description, "Unit" },
1848 { "Requires", config_parse_deps, 0, UINT_TO_PTR(UNIT_REQUIRES), "Unit" },
1849 { "RequiresOverridable", config_parse_deps, 0, UINT_TO_PTR(UNIT_REQUIRES_OVERRIDABLE), "Unit" },
1850 { "Requisite", config_parse_deps, 0, UINT_TO_PTR(UNIT_REQUISITE), "Unit" },
1851 { "RequisiteOverridable", config_parse_deps, 0, UINT_TO_PTR(UNIT_REQUISITE_OVERRIDABLE), "Unit" },
1852 { "Wants", config_parse_deps, 0, UINT_TO_PTR(UNIT_WANTS), "Unit" },
1853 { "BindTo", config_parse_deps, 0, UINT_TO_PTR(UNIT_BIND_TO), "Unit" },
1854 { "Conflicts", config_parse_deps, 0, UINT_TO_PTR(UNIT_CONFLICTS), "Unit" },
1855 { "Before", config_parse_deps, 0, UINT_TO_PTR(UNIT_BEFORE), "Unit" },
1856 { "After", config_parse_deps, 0, UINT_TO_PTR(UNIT_AFTER), "Unit" },
1857 { "OnFailure", config_parse_deps, 0, UINT_TO_PTR(UNIT_ON_FAILURE), "Unit" },
1858 { "StopWhenUnneeded", config_parse_bool, 0, &u->meta.stop_when_unneeded, "Unit" },
1859 { "RefuseManualStart", config_parse_bool, 0, &u->meta.refuse_manual_start, "Unit" },
1860 { "RefuseManualStop", config_parse_bool, 0, &u->meta.refuse_manual_stop, "Unit" },
1861 { "AllowIsolate", config_parse_bool, 0, &u->meta.allow_isolate, "Unit" },
1862 { "DefaultDependencies", config_parse_bool, 0, &u->meta.default_dependencies, "Unit" },
1863 { "OnFailureIsolate", config_parse_bool, 0, &u->meta.on_failure_isolate, "Unit" },
1864 { "IgnoreOnIsolate", config_parse_bool, 0, &u->meta.ignore_on_isolate, "Unit" },
1865 { "JobTimeoutSec", config_parse_usec, 0, &u->meta.job_timeout, "Unit" },
1866 { "ConditionPathExists", config_parse_condition_path, CONDITION_PATH_EXISTS, u, "Unit" },
1867 { "ConditionPathIsDirectory", config_parse_condition_path, CONDITION_PATH_IS_DIRECTORY, u, "Unit" },
1868 { "ConditionDirectoryNotEmpty", config_parse_condition_path, CONDITION_DIRECTORY_NOT_EMPTY, u, "Unit" },
1869 { "ConditionKernelCommandLine", config_parse_condition_string, CONDITION_KERNEL_COMMAND_LINE, u, "Unit" },
1870 { "ConditionVirtualization", config_parse_condition_string, CONDITION_VIRTUALIZATION, u, "Unit" },
1871 { "ConditionSecurity", config_parse_condition_string, CONDITION_SECURITY, u, "Unit" },
1872 { "ConditionNull", config_parse_condition_null, 0, u, "Unit" },
1874 { "PIDFile", config_parse_path, 0, &u->service.pid_file, "Service" },
1875 { "ExecStartPre", config_parse_exec, 0, u->service.exec_command+SERVICE_EXEC_START_PRE, "Service" },
1876 { "ExecStart", config_parse_exec, 0, u->service.exec_command+SERVICE_EXEC_START, "Service" },
1877 { "ExecStartPost", config_parse_exec, 0, u->service.exec_command+SERVICE_EXEC_START_POST, "Service" },
1878 { "ExecReload", config_parse_exec, 0, u->service.exec_command+SERVICE_EXEC_RELOAD, "Service" },
1879 { "ExecStop", config_parse_exec, 0, u->service.exec_command+SERVICE_EXEC_STOP, "Service" },
1880 { "ExecStopPost", config_parse_exec, 0, u->service.exec_command+SERVICE_EXEC_STOP_POST, "Service" },
1881 { "RestartSec", config_parse_usec, 0, &u->service.restart_usec, "Service" },
1882 { "TimeoutSec", config_parse_usec, 0, &u->service.timeout_usec, "Service" },
1883 { "Type", config_parse_service_type, 0, &u->service.type, "Service" },
1884 { "Restart", config_parse_service_restart, 0, &u->service.restart, "Service" },
1885 { "PermissionsStartOnly", config_parse_bool, 0, &u->service.permissions_start_only, "Service" },
1886 { "RootDirectoryStartOnly", config_parse_bool, 0, &u->service.root_directory_start_only, "Service" },
1887 { "RemainAfterExit", config_parse_bool, 0, &u->service.remain_after_exit, "Service" },
1888 { "GuessMainPID", config_parse_bool, 0, &u->service.guess_main_pid, "Service" },
1889 #ifdef HAVE_SYSV_COMPAT
1890 { "SysVStartPriority", config_parse_sysv_priority, 0, &u->service.sysv_start_priority, "Service" },
1892 { "SysVStartPriority", config_parse_warn_compat, 0, NULL, "Service" },
1894 { "NonBlocking", config_parse_bool, 0, &u->service.exec_context.non_blocking, "Service" },
1895 { "BusName", config_parse_string_printf, 0, &u->service.bus_name, "Service" },
1896 { "NotifyAccess", config_parse_notify_access, 0, &u->service.notify_access, "Service" },
1897 { "Sockets", config_parse_service_sockets, 0, &u->service, "Service" },
1898 { "FsckPassNo", config_parse_fsck_passno, 0, &u->service.fsck_passno, "Service" },
1899 EXEC_CONTEXT_CONFIG_ITEMS(u->service.exec_context, "Service"),
1901 { "ListenStream", config_parse_listen, 0, &u->socket, "Socket" },
1902 { "ListenDatagram", config_parse_listen, 0, &u->socket, "Socket" },
1903 { "ListenSequentialPacket", config_parse_listen, 0, &u->socket, "Socket" },
1904 { "ListenFIFO", config_parse_listen, 0, &u->socket, "Socket" },
1905 { "ListenNetlink", config_parse_listen, 0, &u->socket, "Socket" },
1906 { "BindIPv6Only", config_parse_socket_bind, 0, &u->socket, "Socket" },
1907 { "Backlog", config_parse_unsigned, 0, &u->socket.backlog, "Socket" },
1908 { "BindToDevice", config_parse_bindtodevice, 0, &u->socket, "Socket" },
1909 { "ExecStartPre", config_parse_exec, 0, u->socket.exec_command+SOCKET_EXEC_START_PRE, "Socket" },
1910 { "ExecStartPost", config_parse_exec, 0, u->socket.exec_command+SOCKET_EXEC_START_POST, "Socket" },
1911 { "ExecStopPre", config_parse_exec, 0, u->socket.exec_command+SOCKET_EXEC_STOP_PRE, "Socket" },
1912 { "ExecStopPost", config_parse_exec, 0, u->socket.exec_command+SOCKET_EXEC_STOP_POST, "Socket" },
1913 { "TimeoutSec", config_parse_usec, 0, &u->socket.timeout_usec, "Socket" },
1914 { "DirectoryMode", config_parse_mode, 0, &u->socket.directory_mode, "Socket" },
1915 { "SocketMode", config_parse_mode, 0, &u->socket.socket_mode, "Socket" },
1916 { "Accept", config_parse_bool, 0, &u->socket.accept, "Socket" },
1917 { "MaxConnections", config_parse_unsigned, 0, &u->socket.max_connections, "Socket" },
1918 { "KeepAlive", config_parse_bool, 0, &u->socket.keep_alive, "Socket" },
1919 { "Priority", config_parse_int, 0, &u->socket.priority, "Socket" },
1920 { "ReceiveBuffer", config_parse_size, 0, &u->socket.receive_buffer, "Socket" },
1921 { "SendBuffer", config_parse_size, 0, &u->socket.send_buffer, "Socket" },
1922 { "IPTOS", config_parse_ip_tos, 0, &u->socket.ip_tos, "Socket" },
1923 { "IPTTL", config_parse_int, 0, &u->socket.ip_ttl, "Socket" },
1924 { "Mark", config_parse_int, 0, &u->socket.mark, "Socket" },
1925 { "PipeSize", config_parse_size, 0, &u->socket.pipe_size, "Socket" },
1926 { "FreeBind", config_parse_bool, 0, &u->socket.free_bind, "Socket" },
1927 { "TCPCongestion", config_parse_string, 0, &u->socket.tcp_congestion, "Socket" },
1928 { "Service", config_parse_socket_service, 0, &u->socket, "Socket" },
1929 EXEC_CONTEXT_CONFIG_ITEMS(u->socket.exec_context, "Socket"),
1931 { "What", config_parse_string, 0, &u->mount.parameters_fragment.what, "Mount" },
1932 { "Where", config_parse_path, 0, &u->mount.where, "Mount" },
1933 { "Options", config_parse_string, 0, &u->mount.parameters_fragment.options, "Mount" },
1934 { "Type", config_parse_string, 0, &u->mount.parameters_fragment.fstype, "Mount" },
1935 { "TimeoutSec", config_parse_usec, 0, &u->mount.timeout_usec, "Mount" },
1936 { "DirectoryMode", config_parse_mode, 0, &u->mount.directory_mode, "Mount" },
1937 EXEC_CONTEXT_CONFIG_ITEMS(u->mount.exec_context, "Mount"),
1939 { "Where", config_parse_path, 0, &u->automount.where, "Automount" },
1940 { "DirectoryMode", config_parse_mode, 0, &u->automount.directory_mode, "Automount" },
1942 { "What", config_parse_path, 0, &u->swap.parameters_fragment.what, "Swap" },
1943 { "Priority", config_parse_int, 0, &u->swap.parameters_fragment.priority, "Swap" },
1944 { "TimeoutSec", config_parse_usec, 0, &u->swap.timeout_usec, "Swap" },
1945 EXEC_CONTEXT_CONFIG_ITEMS(u->swap.exec_context, "Swap"),
1947 { "OnActiveSec", config_parse_timer, 0, &u->timer, "Timer" },
1948 { "OnBootSec", config_parse_timer, 0, &u->timer, "Timer" },
1949 { "OnStartupSec", config_parse_timer, 0, &u->timer, "Timer" },
1950 { "OnUnitActiveSec", config_parse_timer, 0, &u->timer, "Timer" },
1951 { "OnUnitInactiveSec", config_parse_timer, 0, &u->timer, "Timer" },
1952 { "Unit", config_parse_timer_unit, 0, &u->timer, "Timer" },
1954 { "PathExists", config_parse_path_spec, 0, &u->path, "Path" },
1955 { "PathChanged", config_parse_path_spec, 0, &u->path, "Path" },
1956 { "DirectoryNotEmpty", config_parse_path_spec, 0, &u->path, "Path" },
1957 { "Unit", config_parse_path_unit, 0, &u->path, "Path" },
1958 { "MakeDirectory", config_parse_bool, 0, &u->path.make_directory, "Path" },
1959 { "DirectoryMode", config_parse_mode, 0, &u->path.directory_mode, "Path" },
1961 /* The [Install] section is ignored here. */
1962 { "Alias", NULL, 0, NULL, "Install" },
1963 { "WantedBy", NULL, 0, NULL, "Install" },
1964 { "Also", NULL, 0, NULL, "Install" },
1966 { NULL, NULL, 0, NULL, NULL }
1969 #undef EXEC_CONTEXT_CONFIG_ITEMS
1971 const char *sections[4];
1975 char *filename = NULL, *id = NULL;
1980 /* Dirty dirty hack. */
1981 dump_items((FILE*) path, items);
1988 sections[0] = "Unit";
1989 sections[1] = section_table[u->meta.type];
1990 sections[2] = "Install";
1993 if (!(symlink_names = set_new(string_hash_func, string_compare_func)))
1996 if (path_is_absolute(path)) {
1998 if (!(filename = strdup(path))) {
2003 if ((r = open_follow(&filename, &f, symlink_names, &id)) < 0) {
2014 STRV_FOREACH(p, u->meta.manager->lookup_paths.unit_path) {
2016 /* Instead of opening the path right away, we manually
2017 * follow all symlinks and add their name to our unit
2018 * name set while doing so */
2019 if (!(filename = path_make_absolute(path, *p))) {
2024 if (u->meta.manager->unit_path_cache &&
2025 !set_get(u->meta.manager->unit_path_cache, filename))
2028 r = open_follow(&filename, &f, symlink_names, &id);
2039 /* Empty the symlink names for the next run */
2040 while ((sn = set_steal_first(symlink_names)))
2051 /* Hmm, no suitable file found? */
2057 if ((r = merge_by_names(&merged, symlink_names, id)) < 0)
2061 u->meta.load_state = UNIT_MERGED;
2067 if (fstat(fileno(f), &st) < 0) {
2072 if (null_or_empty(&st))
2073 u->meta.load_state = UNIT_MASKED;
2075 /* Now, parse the file contents */
2076 if ((r = config_parse(filename, f, sections, items, false, u)) < 0)
2079 u->meta.load_state = UNIT_LOADED;
2082 free(u->meta.fragment_path);
2083 u->meta.fragment_path = filename;
2086 u->meta.fragment_mtime = timespec_load(&st.st_mtim);
2091 set_free_free(symlink_names);
2100 int unit_load_fragment(Unit *u) {
2106 assert(u->meta.load_state == UNIT_STUB);
2109 /* First, try to find the unit under its id. We always look
2110 * for unit files in the default directories, to make it easy
2111 * to override things by placing things in /etc/systemd/system */
2112 if ((r = load_from_path(u, u->meta.id)) < 0)
2115 /* Try to find an alias we can load this with */
2116 if (u->meta.load_state == UNIT_STUB)
2117 SET_FOREACH(t, u->meta.names, i) {
2119 if (t == u->meta.id)
2122 if ((r = load_from_path(u, t)) < 0)
2125 if (u->meta.load_state != UNIT_STUB)
2129 /* And now, try looking for it under the suggested (originally linked) path */
2130 if (u->meta.load_state == UNIT_STUB && u->meta.fragment_path) {
2132 if ((r = load_from_path(u, u->meta.fragment_path)) < 0)
2135 if (u->meta.load_state == UNIT_STUB) {
2136 /* Hmm, this didn't work? Then let's get rid
2137 * of the fragment path stored for us, so that
2138 * we don't point to an invalid location. */
2139 free(u->meta.fragment_path);
2140 u->meta.fragment_path = NULL;
2144 /* Look for a template */
2145 if (u->meta.load_state == UNIT_STUB && u->meta.instance) {
2148 if (!(k = unit_name_template(u->meta.id)))
2151 r = load_from_path(u, k);
2157 if (u->meta.load_state == UNIT_STUB)
2158 SET_FOREACH(t, u->meta.names, i) {
2160 if (t == u->meta.id)
2163 if (!(k = unit_name_template(t)))
2166 r = load_from_path(u, k);
2172 if (u->meta.load_state != UNIT_STUB)
2180 void unit_dump_config_items(FILE *f) {
2181 /* OK, this wins a prize for extreme ugliness. */
2183 load_from_path(NULL, (const void*) f);