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>
36 #include "conf-parser.h"
37 #include "load-fragment.h"
40 #include "securebits.h"
42 #include "unit-name.h"
43 #include "bus-errors.h"
45 #ifndef HAVE_SYSV_COMPAT
46 static int config_parse_warn_compat(
56 log_debug("[%s:%u] Support for option %s= has been disabled at compile time and is ignored", filename, line, lvalue);
61 static int config_parse_deps(
71 UnitDependency d = PTR_TO_UINT(data);
81 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
85 if (!(t = strndup(w, l)))
88 k = unit_name_printf(u, t);
94 r = unit_add_dependency_by_name(u, d, k, NULL, true);
97 log_error("Failed to add dependency on %s, ignoring: %s", k, strerror(-r));
108 static int config_parse_names(
109 const char *filename,
128 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
132 if (!(t = strndup(w, l)))
135 k = unit_name_printf(u, t);
141 r = unit_merge_by_name(u, k);
144 log_error("Failed to add name %s, ignoring: %s", k, strerror(-r));
155 static int config_parse_string_printf(
156 const char *filename,
175 if (!(k = unit_full_printf(u, rvalue)))
189 static int config_parse_listen(
190 const char *filename,
209 if (!(p = new0(SocketPort, 1)))
212 if (streq(lvalue, "ListenFIFO")) {
213 p->type = SOCKET_FIFO;
215 if (!(p->path = strdup(rvalue))) {
220 path_kill_slashes(p->path);
222 p->type = SOCKET_SOCKET;
224 if (socket_address_parse(&p->address, rvalue) < 0) {
225 log_error("[%s:%u] Failed to parse address value, ignoring: %s", filename, line, rvalue);
230 if (streq(lvalue, "ListenStream"))
231 p->address.type = SOCK_STREAM;
232 else if (streq(lvalue, "ListenDatagram"))
233 p->address.type = SOCK_DGRAM;
235 assert(streq(lvalue, "ListenSequentialPacket"));
236 p->address.type = SOCK_SEQPACKET;
239 if (socket_address_family(&p->address) != AF_LOCAL && p->address.type == SOCK_SEQPACKET) {
240 log_error("[%s:%u] Address family not supported, ignoring: %s", filename, line, rvalue);
247 LIST_PREPEND(SocketPort, port, s->ports, p);
252 static int config_parse_socket_bind(
253 const char *filename,
263 SocketAddressBindIPv6Only b;
272 if ((b = socket_address_bind_ipv6_only_from_string(rvalue)) < 0) {
275 if ((r = parse_boolean(rvalue)) < 0) {
276 log_error("[%s:%u] Failed to parse bind IPv6 only value, ignoring: %s", filename, line, rvalue);
280 s->bind_ipv6_only = r ? SOCKET_ADDRESS_IPV6_ONLY : SOCKET_ADDRESS_BOTH;
282 s->bind_ipv6_only = b;
287 static int config_parse_nice(
288 const char *filename,
297 ExecContext *c = data;
305 if (safe_atoi(rvalue, &priority) < 0) {
306 log_error("[%s:%u] Failed to parse nice priority, ignoring: %s. ", filename, line, rvalue);
310 if (priority < PRIO_MIN || priority >= PRIO_MAX) {
311 log_error("[%s:%u] Nice priority out of range, ignoring: %s", filename, line, rvalue);
321 static int config_parse_oom_score_adjust(
322 const char *filename,
331 ExecContext *c = data;
339 if (safe_atoi(rvalue, &oa) < 0) {
340 log_error("[%s:%u] Failed to parse the OOM score adjust value, ignoring: %s", filename, line, rvalue);
344 if (oa < OOM_SCORE_ADJ_MIN || oa > OOM_SCORE_ADJ_MAX) {
345 log_error("[%s:%u] OOM score adjust value out of range, ignoring: %s", filename, line, rvalue);
349 c->oom_score_adjust = oa;
350 c->oom_score_adjust_set = true;
355 static int config_parse_mode(
356 const char *filename,
375 l = strtol(rvalue, &x, 8);
376 if (!x || *x || errno) {
377 log_error("[%s:%u] Failed to parse mode value, ignoring: %s", filename, line, rvalue);
381 if (l < 0000 || l > 07777) {
382 log_error("[%s:%u] mode value out of range, ignoring: %s", filename, line, rvalue);
390 static int config_parse_exec(
391 const char *filename,
400 ExecCommand **e = data, *nce;
409 /* We accept an absolute path as first argument, or
410 * alternatively an absolute prefixed with @ to allow
411 * overriding of argv[0]. */
417 bool honour_argv0 = false, ignore = false;
423 rvalue += strspn(rvalue, WHITESPACE);
428 if (rvalue[0] == '-') {
433 if (rvalue[0] == '@') {
438 if (*rvalue != '/') {
439 log_error("[%s:%u] Invalid executable path in command line, ignoring: %s", filename, line, rvalue);
444 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
445 if (strncmp(w, ";", MAX(l, 1U)) == 0)
451 if (!(n = new(char*, k + !honour_argv0)))
455 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
456 if (strncmp(w, ";", MAX(l, 1U)) == 0)
459 if (honour_argv0 && w == rvalue) {
461 if (!(path = cunescape_length(w, l)))
464 if (!(n[k++] = cunescape_length(w, l)))
472 log_error("[%s:%u] Invalid command line, ignoring: %s", filename, line, rvalue);
478 if (!(path = strdup(n[0])))
481 assert(path_is_absolute(path));
483 if (!(nce = new0(ExecCommand, 1)))
488 nce->ignore = ignore;
490 path_kill_slashes(nce->path);
492 exec_command_append_list(e, nce);
508 static int config_parse_usec(
509 const char *filename,
525 if (parse_usec(rvalue, usec) < 0) {
526 log_error("[%s:%u] Failed to parse time value, ignoring: %s", filename, line, rvalue);
533 static DEFINE_CONFIG_PARSE_ENUM(config_parse_service_type, service_type, ServiceType, "Failed to parse service type");
534 static DEFINE_CONFIG_PARSE_ENUM(config_parse_service_restart, service_restart, ServiceRestart, "Failed to parse service restart specifier");
536 static int config_parse_bindtodevice(
537 const char *filename,
554 if (rvalue[0] && !streq(rvalue, "*")) {
555 if (!(n = strdup(rvalue)))
560 free(s->bind_to_device);
561 s->bind_to_device = n;
566 static DEFINE_CONFIG_PARSE_ENUM(config_parse_output, exec_output, ExecOutput, "Failed to parse output specifier");
567 static DEFINE_CONFIG_PARSE_ENUM(config_parse_input, exec_input, ExecInput, "Failed to parse input specifier");
569 static int config_parse_facility(
570 const char *filename,
587 if ((x = log_facility_from_string(rvalue)) < 0) {
588 log_error("[%s:%u] Failed to parse log facility, ignoring: %s", filename, line, rvalue);
592 *o = LOG_MAKEPRI(x, LOG_PRI(*o));
597 static int config_parse_level(
598 const char *filename,
615 if ((x = log_level_from_string(rvalue)) < 0) {
616 log_error("[%s:%u] Failed to parse log level, ignoring: %s", filename, line, rvalue);
620 *o = LOG_MAKEPRI(LOG_FAC(*o), x);
624 static int config_parse_io_class(
625 const char *filename,
634 ExecContext *c = data;
642 if ((x = ioprio_class_from_string(rvalue)) < 0) {
643 log_error("[%s:%u] Failed to parse IO scheduling class, ignoring: %s", filename, line, rvalue);
647 c->ioprio = IOPRIO_PRIO_VALUE(x, IOPRIO_PRIO_DATA(c->ioprio));
648 c->ioprio_set = true;
653 static int config_parse_io_priority(
654 const char *filename,
663 ExecContext *c = data;
671 if (safe_atoi(rvalue, &i) < 0 || i < 0 || i >= IOPRIO_BE_NR) {
672 log_error("[%s:%u] Failed to parse io priority, ignoring: %s", filename, line, rvalue);
676 c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_PRIO_CLASS(c->ioprio), i);
677 c->ioprio_set = true;
682 static int config_parse_cpu_sched_policy(
683 const char *filename,
693 ExecContext *c = data;
701 if ((x = sched_policy_from_string(rvalue)) < 0) {
702 log_error("[%s:%u] Failed to parse CPU scheduling policy, ignoring: %s", filename, line, rvalue);
706 c->cpu_sched_policy = x;
707 c->cpu_sched_set = true;
712 static int config_parse_cpu_sched_prio(
713 const char *filename,
722 ExecContext *c = data;
730 /* On Linux RR/FIFO have the same range */
731 if (safe_atoi(rvalue, &i) < 0 || i < sched_get_priority_min(SCHED_RR) || i > sched_get_priority_max(SCHED_RR)) {
732 log_error("[%s:%u] Failed to parse CPU scheduling priority, ignoring: %s", filename, line, rvalue);
736 c->cpu_sched_priority = i;
737 c->cpu_sched_set = true;
742 static int config_parse_cpu_affinity(
743 const char *filename,
752 ExecContext *c = data;
762 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
767 if (!(t = strndup(w, l)))
770 r = safe_atou(t, &cpu);
774 if (!(c->cpuset = cpu_set_malloc(&c->cpuset_ncpus)))
777 if (r < 0 || cpu >= c->cpuset_ncpus) {
778 log_error("[%s:%u] Failed to parse CPU affinity, ignoring: %s", filename, line, rvalue);
782 CPU_SET_S(cpu, CPU_ALLOC_SIZE(c->cpuset_ncpus), c->cpuset);
788 static int config_parse_capabilities(
789 const char *filename,
798 ExecContext *c = data;
806 if (!(cap = cap_from_text(rvalue))) {
810 log_error("[%s:%u] Failed to parse capabilities, ignoring: %s", filename, line, rvalue);
815 cap_free(c->capabilities);
816 c->capabilities = cap;
821 static int config_parse_secure_bits(
822 const char *filename,
831 ExecContext *c = data;
841 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
842 if (first_word(w, "keep-caps"))
843 c->secure_bits |= SECURE_KEEP_CAPS;
844 else if (first_word(w, "keep-caps-locked"))
845 c->secure_bits |= SECURE_KEEP_CAPS_LOCKED;
846 else if (first_word(w, "no-setuid-fixup"))
847 c->secure_bits |= SECURE_NO_SETUID_FIXUP;
848 else if (first_word(w, "no-setuid-fixup-locked"))
849 c->secure_bits |= SECURE_NO_SETUID_FIXUP_LOCKED;
850 else if (first_word(w, "noroot"))
851 c->secure_bits |= SECURE_NOROOT;
852 else if (first_word(w, "noroot-locked"))
853 c->secure_bits |= SECURE_NOROOT_LOCKED;
855 log_error("[%s:%u] Failed to parse secure bits, ignoring: %s", filename, line, rvalue);
863 static int config_parse_bounding_set(
864 const char *filename,
873 ExecContext *c = data;
885 if (rvalue[0] == '~') {
890 /* Note that we store this inverted internally, since the
891 * kernel wants it like this. But we actually expose it
892 * non-inverted everywhere to have a fully normalized
895 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
900 if (!(t = strndup(w, l)))
903 r = cap_from_name(t, &cap);
907 log_error("[%s:%u] Failed to parse capability bounding set, ignoring: %s", filename, line, rvalue);
911 sum |= ((uint64_t) 1ULL) << (uint64_t) cap;
915 c->capability_bounding_set_drop |= sum;
917 c->capability_bounding_set_drop |= ~sum;
922 static int config_parse_timer_slack_nsec(
923 const char *filename,
932 ExecContext *c = data;
940 if (safe_atolu(rvalue, &u) < 0) {
941 log_error("[%s:%u] Failed to parse time slack value, ignoring: %s", filename, line, rvalue);
945 c->timer_slack_nsec = u;
950 static int config_parse_limit(
951 const char *filename,
960 struct rlimit **rl = data;
961 unsigned long long u;
968 if (safe_atollu(rvalue, &u) < 0) {
969 log_error("[%s:%u] Failed to parse resource value, ignoring: %s", filename, line, rvalue);
974 if (!(*rl = new(struct rlimit, 1)))
977 (*rl)->rlim_cur = (*rl)->rlim_max = (rlim_t) u;
981 static int config_parse_cgroup(
982 const char *filename,
996 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
1000 if (!(t = cunescape_length(w, l)))
1003 r = unit_add_cgroup_from_text(u, t);
1007 log_error("[%s:%u] Failed to parse cgroup value, ignoring: %s", filename, line, rvalue);
1015 #ifdef HAVE_SYSV_COMPAT
1016 static int config_parse_sysv_priority(
1017 const char *filename,
1019 const char *section,
1026 int *priority = data;
1034 if (safe_atoi(rvalue, &i) < 0 || i < 0) {
1035 log_error("[%s:%u] Failed to parse SysV start priority, ignoring: %s", filename, line, rvalue);
1039 *priority = (int) i;
1044 static int config_parse_fsck_passno(
1045 const char *filename,
1047 const char *section,
1062 if (safe_atoi(rvalue, &i) || i < 0) {
1063 log_error("[%s:%u] Failed to parse fsck pass number, ignoring: %s", filename, line, rvalue);
1071 static DEFINE_CONFIG_PARSE_ENUM(config_parse_kill_mode, kill_mode, KillMode, "Failed to parse kill mode");
1073 static int config_parse_kill_signal(
1074 const char *filename,
1076 const char *section,
1091 if ((r = signal_from_string_try_harder(rvalue)) <= 0) {
1092 log_error("[%s:%u] Failed to parse kill signal, ignoring: %s", filename, line, rvalue);
1100 static int config_parse_mount_flags(
1101 const char *filename,
1103 const char *section,
1110 ExecContext *c = data;
1114 unsigned long flags = 0;
1121 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
1122 if (strncmp(w, "shared", MAX(l, 6U)) == 0)
1124 else if (strncmp(w, "slave", MAX(l, 5U)) == 0)
1126 else if (strncmp(w, "private", MAX(l, 7U)) == 0)
1127 flags |= MS_PRIVATE;
1129 log_error("[%s:%u] Failed to parse mount flags, ignoring: %s", filename, line, rvalue);
1134 c->mount_flags = flags;
1138 static int config_parse_timer(
1139 const char *filename,
1141 const char *section,
1158 if ((b = timer_base_from_string(lvalue)) < 0) {
1159 log_error("[%s:%u] Failed to parse timer base, ignoring: %s", filename, line, lvalue);
1163 if (parse_usec(rvalue, &u) < 0) {
1164 log_error("[%s:%u] Failed to parse timer value, ignoring: %s", filename, line, rvalue);
1168 if (!(v = new0(TimerValue, 1)))
1174 LIST_PREPEND(TimerValue, value, t->values, v);
1179 static int config_parse_timer_unit(
1180 const char *filename,
1182 const char *section,
1198 dbus_error_init(&error);
1200 if (endswith(rvalue, ".timer")) {
1201 log_error("[%s:%u] Unit cannot be of type timer, ignoring: %s", filename, line, rvalue);
1205 if ((r = manager_load_unit(t->meta.manager, rvalue, NULL, NULL, &t->unit)) < 0) {
1206 log_error("[%s:%u] Failed to load unit %s, ignoring: %s", filename, line, rvalue, bus_error(&error, r));
1207 dbus_error_free(&error);
1214 static int config_parse_path_spec(
1215 const char *filename,
1217 const char *section,
1233 if ((b = path_type_from_string(lvalue)) < 0) {
1234 log_error("[%s:%u] Failed to parse path type, ignoring: %s", filename, line, lvalue);
1238 if (!path_is_absolute(rvalue)) {
1239 log_error("[%s:%u] Path is not absolute, ignoring: %s", filename, line, rvalue);
1243 if (!(s = new0(PathSpec, 1)))
1246 if (!(s->path = strdup(rvalue))) {
1251 path_kill_slashes(s->path);
1256 LIST_PREPEND(PathSpec, spec, p->specs, s);
1261 static int config_parse_path_unit(
1262 const char *filename,
1264 const char *section,
1280 dbus_error_init(&error);
1282 if (endswith(rvalue, ".path")) {
1283 log_error("[%s:%u] Unit cannot be of type path, ignoring: %s", filename, line, rvalue);
1287 if ((r = manager_load_unit(t->meta.manager, rvalue, NULL, &error, &t->unit)) < 0) {
1288 log_error("[%s:%u] Failed to load unit %s, ignoring: %s", filename, line, rvalue, bus_error(&error, r));
1289 dbus_error_free(&error);
1296 static int config_parse_socket_service(
1297 const char *filename,
1299 const char *section,
1315 dbus_error_init(&error);
1317 if (!endswith(rvalue, ".service")) {
1318 log_error("[%s:%u] Unit must be of type service, ignoring: %s", filename, line, rvalue);
1322 if ((r = manager_load_unit(s->meta.manager, rvalue, NULL, &error, (Unit**) &s->service)) < 0) {
1323 log_error("[%s:%u] Failed to load unit %s, ignoring: %s", filename, line, rvalue, bus_error(&error, r));
1324 dbus_error_free(&error);
1331 static int config_parse_service_sockets(
1332 const char *filename,
1334 const char *section,
1352 dbus_error_init(&error);
1354 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
1358 if (!(t = strndup(w, l)))
1361 if (!endswith(t, ".socket")) {
1362 log_error("[%s:%u] Unit must be of type socket, ignoring: %s", filename, line, rvalue);
1367 r = manager_load_unit(s->meta.manager, t, NULL, &error, &sock);
1371 log_error("[%s:%u] Failed to load unit %s, ignoring: %s", filename, line, rvalue, bus_error(&error, r));
1372 dbus_error_free(&error);
1376 if ((r = set_ensure_allocated(&s->configured_sockets, trivial_hash_func, trivial_compare_func)) < 0)
1379 if ((r = set_put(s->configured_sockets, sock)) < 0)
1386 static int config_parse_env_file(
1387 const char *filename,
1389 const char *section,
1396 char ***env = data, **k;
1403 if (!path_is_absolute(rvalue[0] == '-' ? rvalue + 1 : rvalue)) {
1404 log_error("[%s:%u] Path '%s' is not absolute, ignoring.", filename, line, rvalue);
1408 if (!(k = strv_append(*env, rvalue)))
1417 static int config_parse_ip_tos(
1418 const char *filename,
1420 const char *section,
1427 int *ip_tos = data, x;
1434 if ((x = ip_tos_from_string(rvalue)) < 0)
1435 if (safe_atoi(rvalue, &x) < 0) {
1436 log_error("[%s:%u] Failed to parse IP TOS value, ignoring: %s", filename, line, rvalue);
1444 static int config_parse_condition_path(
1445 const char *filename,
1447 const char *section,
1454 ConditionType cond = ltype;
1456 bool trigger, negate;
1464 if ((trigger = rvalue[0] == '|'))
1467 if ((negate = rvalue[0] == '!'))
1470 if (!path_is_absolute(rvalue)) {
1471 log_error("[%s:%u] Path in condition not absolute, ignoring: %s", filename, line, rvalue);
1475 if (!(c = condition_new(cond, rvalue, trigger, negate)))
1478 LIST_PREPEND(Condition, conditions, u->meta.conditions, c);
1482 static int config_parse_condition_kernel(
1483 const char *filename,
1485 const char *section,
1493 bool trigger, negate;
1501 if ((trigger = rvalue[0] == '|'))
1504 if ((negate = rvalue[0] == '!'))
1507 if (!(c = condition_new(CONDITION_KERNEL_COMMAND_LINE, rvalue, trigger, negate)))
1510 LIST_PREPEND(Condition, conditions, u->meta.conditions, c);
1514 static int config_parse_condition_virt(
1515 const char *filename,
1517 const char *section,
1525 bool trigger, negate;
1533 if ((trigger = rvalue[0] == '|'))
1536 if ((negate = rvalue[0] == '!'))
1539 if (!(c = condition_new(CONDITION_VIRTUALIZATION, rvalue, trigger, negate)))
1542 LIST_PREPEND(Condition, conditions, u->meta.conditions, c);
1546 static int config_parse_condition_null(
1547 const char *filename,
1549 const char *section,
1558 bool trigger, negate;
1566 if ((trigger = rvalue[0] == '|'))
1569 if ((negate = rvalue[0] == '!'))
1572 if ((b = parse_boolean(rvalue)) < 0) {
1573 log_error("[%s:%u] Failed to parse boolean value in condition, ignoring: %s", filename, line, rvalue);
1580 if (!(c = condition_new(CONDITION_NULL, NULL, trigger, negate)))
1583 LIST_PREPEND(Condition, conditions, u->meta.conditions, c);
1587 static DEFINE_CONFIG_PARSE_ENUM(config_parse_notify_access, notify_access, NotifyAccess, "Failed to parse notify access specifier");
1589 #define FOLLOW_MAX 8
1591 static int open_follow(char **filename, FILE **_f, Set *names, char **_final) {
1602 /* This will update the filename pointer if the loaded file is
1603 * reached by a symlink. The old string will be freed. */
1606 char *target, *name;
1608 if (c++ >= FOLLOW_MAX)
1611 path_kill_slashes(*filename);
1613 /* Add the file name we are currently looking at to
1614 * the names of this unit, but only if it is a valid
1616 name = file_name_from_path(*filename);
1618 if (unit_name_is_valid(name, false)) {
1619 if (!(id = set_get(names, name))) {
1621 if (!(id = strdup(name)))
1624 if ((r = set_put(names, id)) < 0) {
1631 /* Try to open the file name, but don't if its a symlink */
1632 if ((fd = open(*filename, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW)) >= 0)
1638 /* Hmm, so this is a symlink. Let's read the name, and follow it manually */
1639 if ((r = readlink_and_make_absolute(*filename, &target)) < 0)
1646 if (!(f = fdopen(fd, "re"))) {
1648 close_nointr_nofail(fd);
1657 static int merge_by_names(Unit **u, Set *names, const char *id) {
1665 /* Let's try to add in all symlink names we found */
1666 while ((k = set_steal_first(names))) {
1668 /* First try to merge in the other name into our
1670 if ((r = unit_merge_by_name(*u, k)) < 0) {
1673 /* Hmm, we couldn't merge the other unit into
1674 * ours? Then let's try it the other way
1677 other = manager_get_unit((*u)->meta.manager, k);
1681 if ((r = unit_merge(other, *u)) >= 0) {
1683 return merge_by_names(u, names, NULL);
1690 unit_choose_id(*u, id);
1698 static void dump_items(FILE *f, const ConfigItem *items) {
1699 const ConfigItem *i;
1700 const char *prev_section = NULL;
1701 bool not_first = false;
1704 ConfigParserCallback callback;
1707 { config_parse_int, "INTEGER" },
1708 { config_parse_unsigned, "UNSIGNED" },
1709 { config_parse_size, "SIZE" },
1710 { config_parse_bool, "BOOLEAN" },
1711 { config_parse_string, "STRING" },
1712 { config_parse_path, "PATH" },
1713 { config_parse_strv, "STRING [...]" },
1714 { config_parse_nice, "NICE" },
1715 { config_parse_oom_score_adjust, "OOMSCOREADJUST" },
1716 { config_parse_io_class, "IOCLASS" },
1717 { config_parse_io_priority, "IOPRIORITY" },
1718 { config_parse_cpu_sched_policy, "CPUSCHEDPOLICY" },
1719 { config_parse_cpu_sched_prio, "CPUSCHEDPRIO" },
1720 { config_parse_cpu_affinity, "CPUAFFINITY" },
1721 { config_parse_mode, "MODE" },
1722 { config_parse_env_file, "FILE" },
1723 { config_parse_output, "OUTPUT" },
1724 { config_parse_input, "INPUT" },
1725 { config_parse_facility, "FACILITY" },
1726 { config_parse_level, "LEVEL" },
1727 { config_parse_capabilities, "CAPABILITIES" },
1728 { config_parse_secure_bits, "SECUREBITS" },
1729 { config_parse_bounding_set, "BOUNDINGSET" },
1730 { config_parse_timer_slack_nsec, "TIMERSLACK" },
1731 { config_parse_limit, "LIMIT" },
1732 { config_parse_cgroup, "CGROUP [...]" },
1733 { config_parse_deps, "UNIT [...]" },
1734 { config_parse_names, "UNIT [...]" },
1735 { config_parse_exec, "PATH [ARGUMENT [...]]" },
1736 { config_parse_service_type, "SERVICETYPE" },
1737 { config_parse_service_restart, "SERVICERESTART" },
1738 #ifdef HAVE_SYSV_COMPAT
1739 { config_parse_sysv_priority, "SYSVPRIORITY" },
1741 { config_parse_warn_compat, "NOTSUPPORTED" },
1743 { config_parse_kill_mode, "KILLMODE" },
1744 { config_parse_kill_signal, "SIGNAL" },
1745 { config_parse_listen, "SOCKET [...]" },
1746 { config_parse_socket_bind, "SOCKETBIND" },
1747 { config_parse_bindtodevice, "NETWORKINTERFACE" },
1748 { config_parse_usec, "SECONDS" },
1749 { config_parse_path_strv, "PATH [...]" },
1750 { config_parse_mount_flags, "MOUNTFLAG [...]" },
1751 { config_parse_string_printf, "STRING" },
1752 { config_parse_timer, "TIMER" },
1753 { config_parse_timer_unit, "NAME" },
1754 { config_parse_path_spec, "PATH" },
1755 { config_parse_path_unit, "UNIT" },
1756 { config_parse_notify_access, "ACCESS" },
1757 { config_parse_ip_tos, "TOS" },
1758 { config_parse_condition_path, "CONDITION" },
1759 { config_parse_condition_kernel, "CONDITION" },
1760 { config_parse_condition_null, "CONDITION" },
1761 { config_parse_condition_virt, "CONDITION" },
1767 for (i = items; i->lvalue; i++) {
1769 const char *rvalue = "OTHER";
1771 if (!streq_ptr(i->section, prev_section)) {
1777 fprintf(f, "[%s]\n", i->section);
1778 prev_section = i->section;
1781 for (j = 0; j < ELEMENTSOF(table); j++)
1782 if (i->parse == table[j].callback) {
1783 rvalue = table[j].rvalue;
1787 fprintf(f, "%s=%s\n", i->lvalue, rvalue);
1791 static int load_from_path(Unit *u, const char *path) {
1793 static const char* const section_table[_UNIT_TYPE_MAX] = {
1794 [UNIT_SERVICE] = "Service",
1795 [UNIT_TIMER] = "Timer",
1796 [UNIT_SOCKET] = "Socket",
1797 [UNIT_TARGET] = "Target",
1798 [UNIT_DEVICE] = "Device",
1799 [UNIT_MOUNT] = "Mount",
1800 [UNIT_AUTOMOUNT] = "Automount",
1801 [UNIT_SNAPSHOT] = "Snapshot",
1802 [UNIT_SWAP] = "Swap",
1803 [UNIT_PATH] = "Path"
1806 #define EXEC_CONTEXT_CONFIG_ITEMS(context, section) \
1807 { "WorkingDirectory", config_parse_path, 0, &(context).working_directory, section }, \
1808 { "RootDirectory", config_parse_path, 0, &(context).root_directory, section }, \
1809 { "User", config_parse_string_printf, 0, &(context).user, section }, \
1810 { "Group", config_parse_string_printf, 0, &(context).group, section }, \
1811 { "SupplementaryGroups", config_parse_strv, 0, &(context).supplementary_groups, section }, \
1812 { "Nice", config_parse_nice, 0, &(context), section }, \
1813 { "OOMScoreAdjust", config_parse_oom_score_adjust,0, &(context), section }, \
1814 { "IOSchedulingClass", config_parse_io_class, 0, &(context), section }, \
1815 { "IOSchedulingPriority", config_parse_io_priority, 0, &(context), section }, \
1816 { "CPUSchedulingPolicy", config_parse_cpu_sched_policy,0, &(context), section }, \
1817 { "CPUSchedulingPriority", config_parse_cpu_sched_prio, 0, &(context), section }, \
1818 { "CPUSchedulingResetOnFork", config_parse_bool, 0, &(context).cpu_sched_reset_on_fork, section }, \
1819 { "CPUAffinity", config_parse_cpu_affinity, 0, &(context), section }, \
1820 { "UMask", config_parse_mode, 0, &(context).umask, section }, \
1821 { "Environment", config_parse_strv, 0, &(context).environment, section }, \
1822 { "EnvironmentFile", config_parse_env_file, 0, &(context).environment_files, section }, \
1823 { "StandardInput", config_parse_input, 0, &(context).std_input, section }, \
1824 { "StandardOutput", config_parse_output, 0, &(context).std_output, section }, \
1825 { "StandardError", config_parse_output, 0, &(context).std_error, section }, \
1826 { "TTYPath", config_parse_path, 0, &(context).tty_path, section }, \
1827 { "SyslogIdentifier", config_parse_string_printf, 0, &(context).syslog_identifier, section }, \
1828 { "SyslogFacility", config_parse_facility, 0, &(context).syslog_priority, section }, \
1829 { "SyslogLevel", config_parse_level, 0, &(context).syslog_priority, section }, \
1830 { "SyslogLevelPrefix", config_parse_bool, 0, &(context).syslog_level_prefix, section }, \
1831 { "Capabilities", config_parse_capabilities, 0, &(context), section }, \
1832 { "SecureBits", config_parse_secure_bits, 0, &(context), section }, \
1833 { "CapabilityBoundingSet", config_parse_bounding_set, 0, &(context), section }, \
1834 { "TimerSlackNSec", config_parse_timer_slack_nsec,0, &(context), section }, \
1835 { "LimitCPU", config_parse_limit, 0, &(context).rlimit[RLIMIT_CPU], section }, \
1836 { "LimitFSIZE", config_parse_limit, 0, &(context).rlimit[RLIMIT_FSIZE], section }, \
1837 { "LimitDATA", config_parse_limit, 0, &(context).rlimit[RLIMIT_DATA], section }, \
1838 { "LimitSTACK", config_parse_limit, 0, &(context).rlimit[RLIMIT_STACK], section }, \
1839 { "LimitCORE", config_parse_limit, 0, &(context).rlimit[RLIMIT_CORE], section }, \
1840 { "LimitRSS", config_parse_limit, 0, &(context).rlimit[RLIMIT_RSS], section }, \
1841 { "LimitNOFILE", config_parse_limit, 0, &(context).rlimit[RLIMIT_NOFILE], section }, \
1842 { "LimitAS", config_parse_limit, 0, &(context).rlimit[RLIMIT_AS], section }, \
1843 { "LimitNPROC", config_parse_limit, 0, &(context).rlimit[RLIMIT_NPROC], section }, \
1844 { "LimitMEMLOCK", config_parse_limit, 0, &(context).rlimit[RLIMIT_MEMLOCK], section }, \
1845 { "LimitLOCKS", config_parse_limit, 0, &(context).rlimit[RLIMIT_LOCKS], section }, \
1846 { "LimitSIGPENDING", config_parse_limit, 0, &(context).rlimit[RLIMIT_SIGPENDING], section }, \
1847 { "LimitMSGQUEUE", config_parse_limit, 0, &(context).rlimit[RLIMIT_MSGQUEUE], section }, \
1848 { "LimitNICE", config_parse_limit, 0, &(context).rlimit[RLIMIT_NICE], section }, \
1849 { "LimitRTPRIO", config_parse_limit, 0, &(context).rlimit[RLIMIT_RTPRIO], section }, \
1850 { "LimitRTTIME", config_parse_limit, 0, &(context).rlimit[RLIMIT_RTTIME], section }, \
1851 { "ControlGroup", config_parse_cgroup, 0, u, section }, \
1852 { "ReadWriteDirectories", config_parse_path_strv, 0, &(context).read_write_dirs, section }, \
1853 { "ReadOnlyDirectories", config_parse_path_strv, 0, &(context).read_only_dirs, section }, \
1854 { "InaccessibleDirectories",config_parse_path_strv, 0, &(context).inaccessible_dirs, section }, \
1855 { "PrivateTmp", config_parse_bool, 0, &(context).private_tmp, section }, \
1856 { "MountFlags", config_parse_mount_flags, 0, &(context), section }, \
1857 { "TCPWrapName", config_parse_string_printf, 0, &(context).tcpwrap_name, section }, \
1858 { "PAMName", config_parse_string_printf, 0, &(context).pam_name, section }, \
1859 { "KillMode", config_parse_kill_mode, 0, &(context).kill_mode, section }, \
1860 { "KillSignal", config_parse_kill_signal, 0, &(context).kill_signal, section }, \
1861 { "SendSIGKILL", config_parse_bool, 0, &(context).send_sigkill, section }, \
1862 { "UtmpIdentifier", config_parse_string_printf, 0, &(context).utmp_id, section }
1864 const ConfigItem items[] = {
1865 { "Names", config_parse_names, 0, u, "Unit" },
1866 { "Description", config_parse_string_printf, 0, &u->meta.description, "Unit" },
1867 { "Requires", config_parse_deps, 0, UINT_TO_PTR(UNIT_REQUIRES), "Unit" },
1868 { "RequiresOverridable", config_parse_deps, 0, UINT_TO_PTR(UNIT_REQUIRES_OVERRIDABLE), "Unit" },
1869 { "Requisite", config_parse_deps, 0, UINT_TO_PTR(UNIT_REQUISITE), "Unit" },
1870 { "RequisiteOverridable", config_parse_deps, 0, UINT_TO_PTR(UNIT_REQUISITE_OVERRIDABLE), "Unit" },
1871 { "Wants", config_parse_deps, 0, UINT_TO_PTR(UNIT_WANTS), "Unit" },
1872 { "BindTo", config_parse_deps, 0, UINT_TO_PTR(UNIT_BIND_TO), "Unit" },
1873 { "Conflicts", config_parse_deps, 0, UINT_TO_PTR(UNIT_CONFLICTS), "Unit" },
1874 { "Before", config_parse_deps, 0, UINT_TO_PTR(UNIT_BEFORE), "Unit" },
1875 { "After", config_parse_deps, 0, UINT_TO_PTR(UNIT_AFTER), "Unit" },
1876 { "OnFailure", config_parse_deps, 0, UINT_TO_PTR(UNIT_ON_FAILURE), "Unit" },
1877 { "StopWhenUnneeded", config_parse_bool, 0, &u->meta.stop_when_unneeded, "Unit" },
1878 { "RefuseManualStart", config_parse_bool, 0, &u->meta.refuse_manual_start, "Unit" },
1879 { "RefuseManualStop", config_parse_bool, 0, &u->meta.refuse_manual_stop, "Unit" },
1880 { "AllowIsolate", config_parse_bool, 0, &u->meta.allow_isolate, "Unit" },
1881 { "DefaultDependencies", config_parse_bool, 0, &u->meta.default_dependencies, "Unit" },
1882 { "JobTimeoutSec", config_parse_usec, 0, &u->meta.job_timeout, "Unit" },
1883 { "ConditionPathExists", config_parse_condition_path, CONDITION_PATH_EXISTS, u, "Unit" },
1884 { "ConditionPathIsDirectory", config_parse_condition_path, CONDITION_PATH_IS_DIRECTORY, u, "Unit" },
1885 { "ConditionDirectoryNotEmpty", config_parse_condition_path, CONDITION_DIRECTORY_NOT_EMPTY, u, "Unit" },
1886 { "ConditionKernelCommandLine", config_parse_condition_kernel, 0, u, "Unit" },
1887 { "ConditionVirtualization",config_parse_condition_virt, 0, u, "Unit" },
1888 { "ConditionNull", config_parse_condition_null, 0, u, "Unit" },
1890 { "PIDFile", config_parse_path, 0, &u->service.pid_file, "Service" },
1891 { "ExecStartPre", config_parse_exec, 0, u->service.exec_command+SERVICE_EXEC_START_PRE, "Service" },
1892 { "ExecStart", config_parse_exec, 0, u->service.exec_command+SERVICE_EXEC_START, "Service" },
1893 { "ExecStartPost", config_parse_exec, 0, u->service.exec_command+SERVICE_EXEC_START_POST, "Service" },
1894 { "ExecReload", config_parse_exec, 0, u->service.exec_command+SERVICE_EXEC_RELOAD, "Service" },
1895 { "ExecStop", config_parse_exec, 0, u->service.exec_command+SERVICE_EXEC_STOP, "Service" },
1896 { "ExecStopPost", config_parse_exec, 0, u->service.exec_command+SERVICE_EXEC_STOP_POST, "Service" },
1897 { "RestartSec", config_parse_usec, 0, &u->service.restart_usec, "Service" },
1898 { "TimeoutSec", config_parse_usec, 0, &u->service.timeout_usec, "Service" },
1899 { "Type", config_parse_service_type, 0, &u->service.type, "Service" },
1900 { "Restart", config_parse_service_restart, 0, &u->service.restart, "Service" },
1901 { "PermissionsStartOnly", config_parse_bool, 0, &u->service.permissions_start_only, "Service" },
1902 { "RootDirectoryStartOnly", config_parse_bool, 0, &u->service.root_directory_start_only, "Service" },
1903 { "RemainAfterExit", config_parse_bool, 0, &u->service.remain_after_exit, "Service" },
1904 { "GuessMainPID", config_parse_bool, 0, &u->service.guess_main_pid, "Service" },
1905 #ifdef HAVE_SYSV_COMPAT
1906 { "SysVStartPriority", config_parse_sysv_priority, 0, &u->service.sysv_start_priority, "Service" },
1908 { "SysVStartPriority", config_parse_warn_compat, 0, NULL, "Service" },
1910 { "NonBlocking", config_parse_bool, 0, &u->service.exec_context.non_blocking, "Service" },
1911 { "BusName", config_parse_string_printf, 0, &u->service.bus_name, "Service" },
1912 { "NotifyAccess", config_parse_notify_access, 0, &u->service.notify_access, "Service" },
1913 { "Sockets", config_parse_service_sockets, 0, &u->service, "Service" },
1914 { "FsckPassNo", config_parse_fsck_passno, 0, &u->service.fsck_passno, "Service" },
1915 EXEC_CONTEXT_CONFIG_ITEMS(u->service.exec_context, "Service"),
1917 { "ListenStream", config_parse_listen, 0, &u->socket, "Socket" },
1918 { "ListenDatagram", config_parse_listen, 0, &u->socket, "Socket" },
1919 { "ListenSequentialPacket", config_parse_listen, 0, &u->socket, "Socket" },
1920 { "ListenFIFO", config_parse_listen, 0, &u->socket, "Socket" },
1921 { "BindIPv6Only", config_parse_socket_bind, 0, &u->socket, "Socket" },
1922 { "Backlog", config_parse_unsigned, 0, &u->socket.backlog, "Socket" },
1923 { "BindToDevice", config_parse_bindtodevice, 0, &u->socket, "Socket" },
1924 { "ExecStartPre", config_parse_exec, 0, u->socket.exec_command+SOCKET_EXEC_START_PRE, "Socket" },
1925 { "ExecStartPost", config_parse_exec, 0, u->socket.exec_command+SOCKET_EXEC_START_POST, "Socket" },
1926 { "ExecStopPre", config_parse_exec, 0, u->socket.exec_command+SOCKET_EXEC_STOP_PRE, "Socket" },
1927 { "ExecStopPost", config_parse_exec, 0, u->socket.exec_command+SOCKET_EXEC_STOP_POST, "Socket" },
1928 { "TimeoutSec", config_parse_usec, 0, &u->socket.timeout_usec, "Socket" },
1929 { "DirectoryMode", config_parse_mode, 0, &u->socket.directory_mode, "Socket" },
1930 { "SocketMode", config_parse_mode, 0, &u->socket.socket_mode, "Socket" },
1931 { "Accept", config_parse_bool, 0, &u->socket.accept, "Socket" },
1932 { "MaxConnections", config_parse_unsigned, 0, &u->socket.max_connections, "Socket" },
1933 { "KeepAlive", config_parse_bool, 0, &u->socket.keep_alive, "Socket" },
1934 { "Priority", config_parse_int, 0, &u->socket.priority, "Socket" },
1935 { "ReceiveBuffer", config_parse_size, 0, &u->socket.receive_buffer, "Socket" },
1936 { "SendBuffer", config_parse_size, 0, &u->socket.send_buffer, "Socket" },
1937 { "IPTOS", config_parse_ip_tos, 0, &u->socket.ip_tos, "Socket" },
1938 { "IPTTL", config_parse_int, 0, &u->socket.ip_ttl, "Socket" },
1939 { "Mark", config_parse_int, 0, &u->socket.mark, "Socket" },
1940 { "PipeSize", config_parse_size, 0, &u->socket.pipe_size, "Socket" },
1941 { "FreeBind", config_parse_bool, 0, &u->socket.free_bind, "Socket" },
1942 { "TCPCongestion", config_parse_string, 0, &u->socket.tcp_congestion, "Socket" },
1943 { "Service", config_parse_socket_service, 0, &u->socket, "Socket" },
1944 EXEC_CONTEXT_CONFIG_ITEMS(u->socket.exec_context, "Socket"),
1946 { "What", config_parse_string, 0, &u->mount.parameters_fragment.what, "Mount" },
1947 { "Where", config_parse_path, 0, &u->mount.where, "Mount" },
1948 { "Options", config_parse_string, 0, &u->mount.parameters_fragment.options, "Mount" },
1949 { "Type", config_parse_string, 0, &u->mount.parameters_fragment.fstype, "Mount" },
1950 { "TimeoutSec", config_parse_usec, 0, &u->mount.timeout_usec, "Mount" },
1951 { "DirectoryMode", config_parse_mode, 0, &u->mount.directory_mode, "Mount" },
1952 EXEC_CONTEXT_CONFIG_ITEMS(u->mount.exec_context, "Mount"),
1954 { "Where", config_parse_path, 0, &u->automount.where, "Automount" },
1955 { "DirectoryMode", config_parse_mode, 0, &u->automount.directory_mode, "Automount" },
1957 { "What", config_parse_path, 0, &u->swap.parameters_fragment.what, "Swap" },
1958 { "Priority", config_parse_int, 0, &u->swap.parameters_fragment.priority, "Swap" },
1959 { "TimeoutSec", config_parse_usec, 0, &u->swap.timeout_usec, "Swap" },
1960 EXEC_CONTEXT_CONFIG_ITEMS(u->swap.exec_context, "Swap"),
1962 { "OnActiveSec", config_parse_timer, 0, &u->timer, "Timer" },
1963 { "OnBootSec", config_parse_timer, 0, &u->timer, "Timer" },
1964 { "OnStartupSec", config_parse_timer, 0, &u->timer, "Timer" },
1965 { "OnUnitActiveSec", config_parse_timer, 0, &u->timer, "Timer" },
1966 { "OnUnitInactiveSec", config_parse_timer, 0, &u->timer, "Timer" },
1967 { "Unit", config_parse_timer_unit, 0, &u->timer, "Timer" },
1969 { "PathExists", config_parse_path_spec, 0, &u->path, "Path" },
1970 { "PathChanged", config_parse_path_spec, 0, &u->path, "Path" },
1971 { "DirectoryNotEmpty", config_parse_path_spec, 0, &u->path, "Path" },
1972 { "Unit", config_parse_path_unit, 0, &u->path, "Path" },
1974 /* The [Install] section is ignored here. */
1975 { "Alias", NULL, 0, NULL, "Install" },
1976 { "WantedBy", NULL, 0, NULL, "Install" },
1977 { "Also", NULL, 0, NULL, "Install" },
1979 { NULL, NULL, 0, NULL, NULL }
1982 #undef EXEC_CONTEXT_CONFIG_ITEMS
1984 const char *sections[4];
1988 char *filename = NULL, *id = NULL;
1993 /* Dirty dirty hack. */
1994 dump_items((FILE*) path, items);
2001 sections[0] = "Unit";
2002 sections[1] = section_table[u->meta.type];
2003 sections[2] = "Install";
2006 if (!(symlink_names = set_new(string_hash_func, string_compare_func)))
2009 if (path_is_absolute(path)) {
2011 if (!(filename = strdup(path))) {
2016 if ((r = open_follow(&filename, &f, symlink_names, &id)) < 0) {
2027 STRV_FOREACH(p, u->meta.manager->lookup_paths.unit_path) {
2029 /* Instead of opening the path right away, we manually
2030 * follow all symlinks and add their name to our unit
2031 * name set while doing so */
2032 if (!(filename = path_make_absolute(path, *p))) {
2037 if (u->meta.manager->unit_path_cache &&
2038 !set_get(u->meta.manager->unit_path_cache, filename))
2041 r = open_follow(&filename, &f, symlink_names, &id);
2052 /* Empty the symlink names for the next run */
2053 while ((sn = set_steal_first(symlink_names)))
2064 /* Hmm, no suitable file found? */
2070 if ((r = merge_by_names(&merged, symlink_names, id)) < 0)
2074 u->meta.load_state = UNIT_MERGED;
2080 if (fstat(fileno(f), &st) < 0) {
2085 if (null_or_empty(&st))
2086 u->meta.load_state = UNIT_MASKED;
2088 /* Now, parse the file contents */
2089 if ((r = config_parse(filename, f, sections, items, false, u)) < 0)
2092 u->meta.load_state = UNIT_LOADED;
2095 free(u->meta.fragment_path);
2096 u->meta.fragment_path = filename;
2099 u->meta.fragment_mtime = timespec_load(&st.st_mtim);
2104 set_free_free(symlink_names);
2113 int unit_load_fragment(Unit *u) {
2119 assert(u->meta.load_state == UNIT_STUB);
2122 /* First, try to find the unit under its id. We always look
2123 * for unit files in the default directories, to make it easy
2124 * to override things by placing things in /etc/systemd/system */
2125 if ((r = load_from_path(u, u->meta.id)) < 0)
2128 /* Try to find an alias we can load this with */
2129 if (u->meta.load_state == UNIT_STUB)
2130 SET_FOREACH(t, u->meta.names, i) {
2132 if (t == u->meta.id)
2135 if ((r = load_from_path(u, t)) < 0)
2138 if (u->meta.load_state != UNIT_STUB)
2142 /* And now, try looking for it under the suggested (originally linked) path */
2143 if (u->meta.load_state == UNIT_STUB && u->meta.fragment_path) {
2145 if ((r = load_from_path(u, u->meta.fragment_path)) < 0)
2148 if (u->meta.load_state == UNIT_STUB) {
2149 /* Hmm, this didn't work? Then let's get rid
2150 * of the fragment path stored for us, so that
2151 * we don't point to an invalid location. */
2152 free(u->meta.fragment_path);
2153 u->meta.fragment_path = NULL;
2157 /* Look for a template */
2158 if (u->meta.load_state == UNIT_STUB && u->meta.instance) {
2161 if (!(k = unit_name_template(u->meta.id)))
2164 r = load_from_path(u, k);
2170 if (u->meta.load_state == UNIT_STUB)
2171 SET_FOREACH(t, u->meta.names, i) {
2173 if (t == u->meta.id)
2176 if (!(k = unit_name_template(t)))
2179 r = load_from_path(u, k);
2185 if (u->meta.load_state != UNIT_STUB)
2193 void unit_dump_config_items(FILE *f) {
2194 /* OK, this wins a prize for extreme ugliness. */
2196 load_from_path(NULL, (const void*) f);