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(
55 log_debug("[%s:%u] Support for option %s= has been disabled at compile time and is ignored", filename, line, lvalue);
60 static int config_parse_deps(
69 UnitDependency d = PTR_TO_UINT(data);
79 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
83 if (!(t = strndup(w, l)))
86 k = unit_name_printf(u, t);
92 r = unit_add_dependency_by_name(u, d, k, NULL, true);
95 log_error("Failed to add dependency on %s, ignoring: %s", k, strerror(-r));
106 static int config_parse_names(
107 const char *filename,
125 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
129 if (!(t = strndup(w, l)))
132 k = unit_name_printf(u, t);
138 r = unit_merge_by_name(u, k);
141 log_error("Failed to add name %s, ignoring: %s", k, strerror(-r));
152 static int config_parse_string_printf(
153 const char *filename,
171 if (!(k = unit_full_printf(u, rvalue)))
185 static int config_parse_listen(
186 const char *filename,
205 if (!(p = new0(SocketPort, 1)))
208 if (streq(lvalue, "ListenFIFO")) {
209 p->type = SOCKET_FIFO;
211 if (!(p->path = strdup(rvalue))) {
216 path_kill_slashes(p->path);
218 p->type = SOCKET_SOCKET;
220 if ((r = socket_address_parse(&p->address, rvalue)) < 0) {
221 log_error("[%s:%u] Failed to parse address value, ignoring: %s", filename, line, rvalue);
226 if (streq(lvalue, "ListenStream"))
227 p->address.type = SOCK_STREAM;
228 else if (streq(lvalue, "ListenDatagram"))
229 p->address.type = SOCK_DGRAM;
231 assert(streq(lvalue, "ListenSequentialPacket"));
232 p->address.type = SOCK_SEQPACKET;
235 if (socket_address_family(&p->address) != AF_LOCAL && p->address.type == SOCK_SEQPACKET) {
236 log_error("[%s:%u] Address family not supported, ignoring: %s", filename, line, rvalue);
243 LIST_PREPEND(SocketPort, port, s->ports, p);
248 static int config_parse_socket_bind(
249 const char *filename,
258 SocketAddressBindIPv6Only b;
267 if ((b = socket_address_bind_ipv6_only_from_string(rvalue)) < 0) {
270 if ((r = parse_boolean(rvalue)) < 0) {
271 log_error("[%s:%u] Failed to parse bind IPv6 only value, ignoring: %s", filename, line, rvalue);
275 s->bind_ipv6_only = r ? SOCKET_ADDRESS_IPV6_ONLY : SOCKET_ADDRESS_BOTH;
277 s->bind_ipv6_only = b;
282 static int config_parse_nice(
283 const char *filename,
291 ExecContext *c = data;
299 if ((r = safe_atoi(rvalue, &priority)) < 0) {
300 log_error("[%s:%u] Failed to parse nice priority, ignoring: %s. ", filename, line, rvalue);
304 if (priority < PRIO_MIN || priority >= PRIO_MAX) {
305 log_error("[%s:%u] Nice priority out of range, ignoring: %s", filename, line, rvalue);
315 static int config_parse_oom_score_adjust(
316 const char *filename,
324 ExecContext *c = data;
332 if ((r = safe_atoi(rvalue, &oa)) < 0) {
333 log_error("[%s:%u] Failed to parse the OOM score adjust value, ignoring: %s", filename, line, rvalue);
337 if (oa < OOM_SCORE_ADJ_MIN || oa > OOM_SCORE_ADJ_MAX) {
338 log_error("[%s:%u] OOM score adjust value out of range, ignoring: %s", filename, line, rvalue);
342 c->oom_score_adjust = oa;
343 c->oom_score_adjust_set = true;
348 static int config_parse_mode(
349 const char *filename,
367 l = strtol(rvalue, &x, 8);
368 if (!x || *x || errno) {
369 log_error("[%s:%u] Failed to parse mode value, ignoring: %s", filename, line, rvalue);
373 if (l < 0000 || l > 07777) {
374 log_error("[%s:%u] mode value out of range, ignoring: %s", filename, line, rvalue);
382 static int config_parse_exec(
383 const char *filename,
391 ExecCommand **e = data, *nce;
400 /* We accept an absolute path as first argument, or
401 * alternatively an absolute prefixed with @ to allow
402 * overriding of argv[0]. */
408 bool honour_argv0 = false, ignore = false;
414 rvalue += strspn(rvalue, WHITESPACE);
419 if (rvalue[0] == '-') {
424 if (rvalue[0] == '@') {
429 if (*rvalue != '/') {
430 log_error("[%s:%u] Invalid executable path in command line, ignoring: %s", filename, line, rvalue);
435 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
436 if (strncmp(w, ";", l) == 0)
442 if (!(n = new(char*, k + !honour_argv0)))
446 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
447 if (strncmp(w, ";", l) == 0)
450 if (honour_argv0 && w == rvalue) {
452 if (!(path = cunescape_length(w, l)))
455 if (!(n[k++] = cunescape_length(w, l)))
463 log_error("[%s:%u] Invalid command line, ignoring: %s", filename, line, rvalue);
469 if (!(path = strdup(n[0])))
472 assert(path_is_absolute(path));
474 if (!(nce = new0(ExecCommand, 1)))
479 nce->ignore = ignore;
481 path_kill_slashes(nce->path);
483 exec_command_append_list(e, nce);
499 static int config_parse_usec(
500 const char *filename,
516 if ((r = parse_usec(rvalue, usec)) < 0) {
517 log_error("[%s:%u] Failed to parse time value, ignoring: %s", filename, line, rvalue);
524 static DEFINE_CONFIG_PARSE_ENUM(config_parse_service_type, service_type, ServiceType, "Failed to parse service type");
525 static DEFINE_CONFIG_PARSE_ENUM(config_parse_service_restart, service_restart, ServiceRestart, "Failed to parse service restart specifier");
527 static int config_parse_bindtodevice(
528 const char *filename,
544 if (rvalue[0] && !streq(rvalue, "*")) {
545 if (!(n = strdup(rvalue)))
550 free(s->bind_to_device);
551 s->bind_to_device = n;
556 static DEFINE_CONFIG_PARSE_ENUM(config_parse_output, exec_output, ExecOutput, "Failed to parse output specifier");
557 static DEFINE_CONFIG_PARSE_ENUM(config_parse_input, exec_input, ExecInput, "Failed to parse input specifier");
559 static int config_parse_facility(
560 const char *filename,
576 if ((x = log_facility_from_string(rvalue)) < 0) {
577 log_error("[%s:%u] Failed to parse log facility, ignoring: %s", filename, line, rvalue);
581 *o = LOG_MAKEPRI(x, LOG_PRI(*o));
586 static int config_parse_level(
587 const char *filename,
603 if ((x = log_level_from_string(rvalue)) < 0) {
604 log_error("[%s:%u] Failed to parse log level, ignoring: %s", filename, line, rvalue);
608 *o = LOG_MAKEPRI(LOG_FAC(*o), x);
612 static int config_parse_io_class(
613 const char *filename,
621 ExecContext *c = data;
629 if ((x = ioprio_class_from_string(rvalue)) < 0) {
630 log_error("[%s:%u] Failed to parse IO scheduling class, ignoring: %s", filename, line, rvalue);
634 c->ioprio = IOPRIO_PRIO_VALUE(x, IOPRIO_PRIO_DATA(c->ioprio));
635 c->ioprio_set = true;
640 static int config_parse_io_priority(
641 const char *filename,
649 ExecContext *c = data;
657 if (safe_atoi(rvalue, &i) < 0 || i < 0 || i >= IOPRIO_BE_NR) {
658 log_error("[%s:%u] Failed to parse io priority, ignoring: %s", filename, line, rvalue);
662 c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_PRIO_CLASS(c->ioprio), i);
663 c->ioprio_set = true;
668 static int config_parse_cpu_sched_policy(
669 const char *filename,
678 ExecContext *c = data;
686 if ((x = sched_policy_from_string(rvalue)) < 0) {
687 log_error("[%s:%u] Failed to parse CPU scheduling policy, ignoring: %s", filename, line, rvalue);
691 c->cpu_sched_policy = x;
692 c->cpu_sched_set = true;
697 static int config_parse_cpu_sched_prio(
698 const char *filename,
706 ExecContext *c = data;
714 /* On Linux RR/FIFO have the same range */
715 if (safe_atoi(rvalue, &i) < 0 || i < sched_get_priority_min(SCHED_RR) || i > sched_get_priority_max(SCHED_RR)) {
716 log_error("[%s:%u] Failed to parse CPU scheduling priority, ignoring: %s", filename, line, rvalue);
720 c->cpu_sched_priority = i;
721 c->cpu_sched_set = true;
726 static int config_parse_cpu_affinity(
727 const char *filename,
735 ExecContext *c = data;
745 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
750 if (!(t = strndup(w, l)))
753 r = safe_atou(t, &cpu);
757 if (!(c->cpuset = cpu_set_malloc(&c->cpuset_ncpus)))
760 if (r < 0 || cpu >= c->cpuset_ncpus) {
761 log_error("[%s:%u] Failed to parse CPU affinity, ignoring: %s", filename, line, rvalue);
765 CPU_SET_S(cpu, CPU_ALLOC_SIZE(c->cpuset_ncpus), c->cpuset);
771 static int config_parse_capabilities(
772 const char *filename,
780 ExecContext *c = data;
788 if (!(cap = cap_from_text(rvalue))) {
792 log_error("[%s:%u] Failed to parse capabilities, ignoring: %s", filename, line, rvalue);
797 cap_free(c->capabilities);
798 c->capabilities = cap;
803 static int config_parse_secure_bits(
804 const char *filename,
812 ExecContext *c = data;
822 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
823 if (first_word(w, "keep-caps"))
824 c->secure_bits |= SECURE_KEEP_CAPS;
825 else if (first_word(w, "keep-caps-locked"))
826 c->secure_bits |= SECURE_KEEP_CAPS_LOCKED;
827 else if (first_word(w, "no-setuid-fixup"))
828 c->secure_bits |= SECURE_NO_SETUID_FIXUP;
829 else if (first_word(w, "no-setuid-fixup-locked"))
830 c->secure_bits |= SECURE_NO_SETUID_FIXUP_LOCKED;
831 else if (first_word(w, "noroot"))
832 c->secure_bits |= SECURE_NOROOT;
833 else if (first_word(w, "noroot-locked"))
834 c->secure_bits |= SECURE_NOROOT_LOCKED;
836 log_error("[%s:%u] Failed to parse secure bits, ignoring: %s", filename, line, rvalue);
844 static int config_parse_bounding_set(
845 const char *filename,
853 ExecContext *c = data;
863 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
868 if (!(t = strndup(w, l)))
871 r = cap_from_name(t, &cap);
875 log_error("[%s:%u] Failed to parse capability bounding set, ignoring: %s", filename, line, rvalue);
879 c->capability_bounding_set_drop |= 1 << cap;
885 static int config_parse_timer_slack_nsec(
886 const char *filename,
894 ExecContext *c = data;
903 if ((r = safe_atolu(rvalue, &u)) < 0) {
904 log_error("[%s:%u] Failed to parse time slack value, ignoring: %s", filename, line, rvalue);
908 c->timer_slack_nsec = u;
913 static int config_parse_limit(
914 const char *filename,
922 struct rlimit **rl = data;
923 unsigned long long u;
931 if ((r = safe_atollu(rvalue, &u)) < 0) {
932 log_error("[%s:%u] Failed to parse resource value, ignoring: %s", filename, line, rvalue);
937 if (!(*rl = new(struct rlimit, 1)))
940 (*rl)->rlim_cur = (*rl)->rlim_max = (rlim_t) u;
944 static int config_parse_cgroup(
945 const char *filename,
958 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
962 if (!(t = cunescape_length(w, l)))
965 r = unit_add_cgroup_from_text(u, t);
969 log_error("[%s:%u] Failed to parse cgroup value, ignoring: %s", filename, line, rvalue);
977 #ifdef HAVE_SYSV_COMPAT
978 static int config_parse_sysv_priority(
979 const char *filename,
987 int *priority = data;
995 if ((r = safe_atoi(rvalue, &i)) < 0 || i < 0) {
996 log_error("[%s:%u] Failed to parse SysV start priority, ignoring: %s", filename, line, rvalue);
1000 *priority = (int) i;
1005 static DEFINE_CONFIG_PARSE_ENUM(config_parse_kill_mode, kill_mode, KillMode, "Failed to parse kill mode");
1007 static int config_parse_kill_signal(
1008 const char *filename,
1010 const char *section,
1024 if ((r = signal_from_string(rvalue)) <= 0)
1025 if (startswith(rvalue, "SIG"))
1026 r = signal_from_string(rvalue+3);
1029 log_error("[%s:%u] Failed to parse kill signal, ignoring: %s", filename, line, rvalue);
1037 static int config_parse_mount_flags(
1038 const char *filename,
1040 const char *section,
1046 ExecContext *c = data;
1050 unsigned long flags = 0;
1057 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
1058 if (strncmp(w, "shared", l) == 0)
1060 else if (strncmp(w, "slave", l) == 0)
1062 else if (strncmp(w, "private", l) == 0)
1063 flags |= MS_PRIVATE;
1065 log_error("[%s:%u] Failed to parse mount flags, ignoring: %s", filename, line, rvalue);
1070 c->mount_flags = flags;
1074 static int config_parse_timer(
1075 const char *filename,
1077 const char *section,
1094 if ((b = timer_base_from_string(lvalue)) < 0) {
1095 log_error("[%s:%u] Failed to parse timer base, ignoring: %s", filename, line, lvalue);
1099 if ((r = parse_usec(rvalue, &u)) < 0) {
1100 log_error("[%s:%u] Failed to parse timer value, ignoring: %s", filename, line, rvalue);
1104 if (!(v = new0(TimerValue, 1)))
1110 LIST_PREPEND(TimerValue, value, t->values, v);
1115 static int config_parse_timer_unit(
1116 const char *filename,
1118 const char *section,
1133 dbus_error_init(&error);
1135 if (endswith(rvalue, ".timer")) {
1136 log_error("[%s:%u] Unit cannot be of type timer, ignoring: %s", filename, line, rvalue);
1140 if ((r = manager_load_unit(t->meta.manager, rvalue, NULL, NULL, &t->unit)) < 0) {
1141 log_error("[%s:%u] Failed to load unit %s, ignoring: %s", filename, line, rvalue, bus_error(&error, r));
1142 dbus_error_free(&error);
1149 static int config_parse_path_spec(
1150 const char *filename,
1152 const char *section,
1167 if ((b = path_type_from_string(lvalue)) < 0) {
1168 log_error("[%s:%u] Failed to parse path type, ignoring: %s", filename, line, lvalue);
1172 if (!path_is_absolute(rvalue)) {
1173 log_error("[%s:%u] Path is not absolute, ignoring: %s", filename, line, rvalue);
1177 if (!(s = new0(PathSpec, 1)))
1180 if (!(s->path = strdup(rvalue))) {
1185 path_kill_slashes(s->path);
1190 LIST_PREPEND(PathSpec, spec, p->specs, s);
1195 static int config_parse_path_unit(
1196 const char *filename,
1198 const char *section,
1213 dbus_error_init(&error);
1215 if (endswith(rvalue, ".path")) {
1216 log_error("[%s:%u] Unit cannot be of type path, ignoring: %s", filename, line, rvalue);
1220 if ((r = manager_load_unit(t->meta.manager, rvalue, NULL, &error, &t->unit)) < 0) {
1221 log_error("[%s:%u] Failed to load unit %s, ignoring: %s", filename, line, rvalue, bus_error(&error, r));
1222 dbus_error_free(&error);
1229 static int config_parse_env_file(
1230 const char *filename,
1232 const char *section,
1247 if (!(f = fopen(rvalue, "re"))) {
1248 log_error("[%s:%u] Failed to open environment file '%s', ignoring: %m", filename, line, rvalue);
1253 char l[LINE_MAX], *p;
1256 if (!fgets(l, sizeof(l), f)) {
1261 log_error("[%s:%u] Failed to read environment file '%s', ignoring: %m", filename, line, rvalue);
1271 if (strchr(COMMENTS, *p))
1274 t = strv_env_set(*env, p);
1288 static int config_parse_ip_tos(
1289 const char *filename,
1291 const char *section,
1297 int *ip_tos = data, x;
1305 if ((x = ip_tos_from_string(rvalue)) < 0)
1306 if ((r = safe_atoi(rvalue, &x)) < 0) {
1307 log_error("[%s:%u] Failed to parse IP TOS value, ignoring: %s", filename, line, rvalue);
1315 static DEFINE_CONFIG_PARSE_ENUM(config_parse_notify_access, notify_access, NotifyAccess, "Failed to parse notify access specifier");
1317 #define FOLLOW_MAX 8
1319 static int open_follow(char **filename, FILE **_f, Set *names, char **_final) {
1330 /* This will update the filename pointer if the loaded file is
1331 * reached by a symlink. The old string will be freed. */
1334 char *target, *name;
1336 if (c++ >= FOLLOW_MAX)
1339 path_kill_slashes(*filename);
1341 /* Add the file name we are currently looking at to
1342 * the names of this unit, but only if it is a valid
1344 name = file_name_from_path(*filename);
1346 if (unit_name_is_valid(name)) {
1347 if (!(id = set_get(names, name))) {
1349 if (!(id = strdup(name)))
1352 if ((r = set_put(names, id)) < 0) {
1359 /* Try to open the file name, but don't if its a symlink */
1360 if ((fd = open(*filename, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW)) >= 0)
1366 /* Hmm, so this is a symlink. Let's read the name, and follow it manually */
1367 if ((r = readlink_and_make_absolute(*filename, &target)) < 0)
1374 if (!(f = fdopen(fd, "re"))) {
1376 close_nointr_nofail(fd);
1385 static int merge_by_names(Unit **u, Set *names, const char *id) {
1393 /* Let's try to add in all symlink names we found */
1394 while ((k = set_steal_first(names))) {
1396 /* First try to merge in the other name into our
1398 if ((r = unit_merge_by_name(*u, k)) < 0) {
1401 /* Hmm, we couldn't merge the other unit into
1402 * ours? Then let's try it the other way
1405 other = manager_get_unit((*u)->meta.manager, k);
1409 if ((r = unit_merge(other, *u)) >= 0) {
1411 return merge_by_names(u, names, NULL);
1418 unit_choose_id(*u, id);
1426 static void dump_items(FILE *f, const ConfigItem *items) {
1427 const ConfigItem *i;
1428 const char *prev_section = NULL;
1429 bool not_first = false;
1432 ConfigParserCallback callback;
1435 { config_parse_int, "INTEGER" },
1436 { config_parse_unsigned, "UNSIGNED" },
1437 { config_parse_size, "SIZE" },
1438 { config_parse_bool, "BOOLEAN" },
1439 { config_parse_string, "STRING" },
1440 { config_parse_path, "PATH" },
1441 { config_parse_strv, "STRING [...]" },
1442 { config_parse_nice, "NICE" },
1443 { config_parse_oom_score_adjust, "OOMSCOREADJUST" },
1444 { config_parse_io_class, "IOCLASS" },
1445 { config_parse_io_priority, "IOPRIORITY" },
1446 { config_parse_cpu_sched_policy, "CPUSCHEDPOLICY" },
1447 { config_parse_cpu_sched_prio, "CPUSCHEDPRIO" },
1448 { config_parse_cpu_affinity, "CPUAFFINITY" },
1449 { config_parse_mode, "MODE" },
1450 { config_parse_env_file, "FILE" },
1451 { config_parse_output, "OUTPUT" },
1452 { config_parse_input, "INPUT" },
1453 { config_parse_facility, "FACILITY" },
1454 { config_parse_level, "LEVEL" },
1455 { config_parse_capabilities, "CAPABILITIES" },
1456 { config_parse_secure_bits, "SECUREBITS" },
1457 { config_parse_bounding_set, "BOUNDINGSET" },
1458 { config_parse_timer_slack_nsec, "TIMERSLACK" },
1459 { config_parse_limit, "LIMIT" },
1460 { config_parse_cgroup, "CGROUP [...]" },
1461 { config_parse_deps, "UNIT [...]" },
1462 { config_parse_names, "UNIT [...]" },
1463 { config_parse_exec, "PATH [ARGUMENT [...]]" },
1464 { config_parse_service_type, "SERVICETYPE" },
1465 { config_parse_service_restart, "SERVICERESTART" },
1466 #ifdef HAVE_SYSV_COMPAT
1467 { config_parse_sysv_priority, "SYSVPRIORITY" },
1469 { config_parse_warn_compat, "NOTSUPPORTED" },
1471 { config_parse_kill_mode, "KILLMODE" },
1472 { config_parse_kill_signal, "SIGNAL" },
1473 { config_parse_listen, "SOCKET [...]" },
1474 { config_parse_socket_bind, "SOCKETBIND" },
1475 { config_parse_bindtodevice, "NETWORKINTERFACE" },
1476 { config_parse_usec, "SECONDS" },
1477 { config_parse_path_strv, "PATH [...]" },
1478 { config_parse_mount_flags, "MOUNTFLAG [...]" },
1479 { config_parse_string_printf, "STRING" },
1480 { config_parse_timer, "TIMER" },
1481 { config_parse_timer_unit, "NAME" },
1482 { config_parse_path_spec, "PATH" },
1483 { config_parse_path_unit, "UNIT" },
1484 { config_parse_notify_access, "ACCESS" },
1485 { config_parse_ip_tos, "TOS" },
1491 for (i = items; i->lvalue; i++) {
1493 const char *rvalue = "OTHER";
1495 if (!streq_ptr(i->section, prev_section)) {
1501 fprintf(f, "[%s]\n", i->section);
1502 prev_section = i->section;
1505 for (j = 0; j < ELEMENTSOF(table); j++)
1506 if (i->parse == table[j].callback) {
1507 rvalue = table[j].rvalue;
1511 fprintf(f, "%s=%s\n", i->lvalue, rvalue);
1515 static int load_from_path(Unit *u, const char *path) {
1517 static const char* const section_table[_UNIT_TYPE_MAX] = {
1518 [UNIT_SERVICE] = "Service",
1519 [UNIT_TIMER] = "Timer",
1520 [UNIT_SOCKET] = "Socket",
1521 [UNIT_TARGET] = "Target",
1522 [UNIT_DEVICE] = "Device",
1523 [UNIT_MOUNT] = "Mount",
1524 [UNIT_AUTOMOUNT] = "Automount",
1525 [UNIT_SNAPSHOT] = "Snapshot",
1526 [UNIT_SWAP] = "Swap",
1527 [UNIT_PATH] = "Path"
1530 #define EXEC_CONTEXT_CONFIG_ITEMS(context, section) \
1531 { "WorkingDirectory", config_parse_path, &(context).working_directory, section }, \
1532 { "RootDirectory", config_parse_path, &(context).root_directory, section }, \
1533 { "User", config_parse_string_printf, &(context).user, section }, \
1534 { "Group", config_parse_string_printf, &(context).group, section }, \
1535 { "SupplementaryGroups", config_parse_strv, &(context).supplementary_groups, section }, \
1536 { "Nice", config_parse_nice, &(context), section }, \
1537 { "OOMScoreAdjust", config_parse_oom_score_adjust,&(context), section }, \
1538 { "IOSchedulingClass", config_parse_io_class, &(context), section }, \
1539 { "IOSchedulingPriority", config_parse_io_priority, &(context), section }, \
1540 { "CPUSchedulingPolicy", config_parse_cpu_sched_policy,&(context), section }, \
1541 { "CPUSchedulingPriority", config_parse_cpu_sched_prio, &(context), section }, \
1542 { "CPUSchedulingResetOnFork", config_parse_bool, &(context).cpu_sched_reset_on_fork, section }, \
1543 { "CPUAffinity", config_parse_cpu_affinity, &(context), section }, \
1544 { "UMask", config_parse_mode, &(context).umask, section }, \
1545 { "Environment", config_parse_strv, &(context).environment, section }, \
1546 { "EnvironmentFile", config_parse_env_file, &(context).environment, section }, \
1547 { "StandardInput", config_parse_input, &(context).std_input, section }, \
1548 { "StandardOutput", config_parse_output, &(context).std_output, section }, \
1549 { "StandardError", config_parse_output, &(context).std_error, section }, \
1550 { "TTYPath", config_parse_path, &(context).tty_path, section }, \
1551 { "SyslogIdentifier", config_parse_string_printf, &(context).syslog_identifier, section }, \
1552 { "SyslogFacility", config_parse_facility, &(context).syslog_priority, section }, \
1553 { "SyslogLevel", config_parse_level, &(context).syslog_priority, section }, \
1554 { "SyslogLevelPrefix", config_parse_bool, &(context).syslog_level_prefix, section }, \
1555 { "Capabilities", config_parse_capabilities, &(context), section }, \
1556 { "SecureBits", config_parse_secure_bits, &(context), section }, \
1557 { "CapabilityBoundingSetDrop", config_parse_bounding_set, &(context), section }, \
1558 { "TimerSlackNSec", config_parse_timer_slack_nsec,&(context), section }, \
1559 { "LimitCPU", config_parse_limit, &(context).rlimit[RLIMIT_CPU], section }, \
1560 { "LimitFSIZE", config_parse_limit, &(context).rlimit[RLIMIT_FSIZE], section }, \
1561 { "LimitDATA", config_parse_limit, &(context).rlimit[RLIMIT_DATA], section }, \
1562 { "LimitSTACK", config_parse_limit, &(context).rlimit[RLIMIT_STACK], section }, \
1563 { "LimitCORE", config_parse_limit, &(context).rlimit[RLIMIT_CORE], section }, \
1564 { "LimitRSS", config_parse_limit, &(context).rlimit[RLIMIT_RSS], section }, \
1565 { "LimitNOFILE", config_parse_limit, &(context).rlimit[RLIMIT_NOFILE], section }, \
1566 { "LimitAS", config_parse_limit, &(context).rlimit[RLIMIT_AS], section }, \
1567 { "LimitNPROC", config_parse_limit, &(context).rlimit[RLIMIT_NPROC], section }, \
1568 { "LimitMEMLOCK", config_parse_limit, &(context).rlimit[RLIMIT_MEMLOCK], section }, \
1569 { "LimitLOCKS", config_parse_limit, &(context).rlimit[RLIMIT_LOCKS], section }, \
1570 { "LimitSIGPENDING", config_parse_limit, &(context).rlimit[RLIMIT_SIGPENDING], section }, \
1571 { "LimitMSGQUEUE", config_parse_limit, &(context).rlimit[RLIMIT_MSGQUEUE], section }, \
1572 { "LimitNICE", config_parse_limit, &(context).rlimit[RLIMIT_NICE], section }, \
1573 { "LimitRTPRIO", config_parse_limit, &(context).rlimit[RLIMIT_RTPRIO], section }, \
1574 { "LimitRTTIME", config_parse_limit, &(context).rlimit[RLIMIT_RTTIME], section }, \
1575 { "ControlGroup", config_parse_cgroup, u, section }, \
1576 { "ReadWriteDirectories", config_parse_path_strv, &(context).read_write_dirs, section }, \
1577 { "ReadOnlyDirectories", config_parse_path_strv, &(context).read_only_dirs, section }, \
1578 { "InaccessibleDirectories",config_parse_path_strv, &(context).inaccessible_dirs, section }, \
1579 { "PrivateTmp", config_parse_bool, &(context).private_tmp, section }, \
1580 { "MountFlags", config_parse_mount_flags, &(context), section }, \
1581 { "TCPWrapName", config_parse_string_printf, &(context).tcpwrap_name, section }, \
1582 { "PAMName", config_parse_string_printf, &(context).pam_name, section }, \
1583 { "KillMode", config_parse_kill_mode, &(context).kill_mode, section }, \
1584 { "KillSignal", config_parse_kill_signal, &(context).kill_signal, section }
1586 const ConfigItem items[] = {
1587 { "Names", config_parse_names, u, "Unit" },
1588 { "Description", config_parse_string_printf, &u->meta.description, "Unit" },
1589 { "Requires", config_parse_deps, UINT_TO_PTR(UNIT_REQUIRES), "Unit" },
1590 { "RequiresOverridable", config_parse_deps, UINT_TO_PTR(UNIT_REQUIRES_OVERRIDABLE), "Unit" },
1591 { "Requisite", config_parse_deps, UINT_TO_PTR(UNIT_REQUISITE), "Unit" },
1592 { "RequisiteOverridable", config_parse_deps, UINT_TO_PTR(UNIT_REQUISITE_OVERRIDABLE), "Unit" },
1593 { "Wants", config_parse_deps, UINT_TO_PTR(UNIT_WANTS), "Unit" },
1594 { "Conflicts", config_parse_deps, UINT_TO_PTR(UNIT_CONFLICTS), "Unit" },
1595 { "Before", config_parse_deps, UINT_TO_PTR(UNIT_BEFORE), "Unit" },
1596 { "After", config_parse_deps, UINT_TO_PTR(UNIT_AFTER), "Unit" },
1597 { "OnFailure", config_parse_deps, UINT_TO_PTR(UNIT_ON_FAILURE), "Unit" },
1598 { "RecursiveStop", config_parse_bool, &u->meta.recursive_stop, "Unit" },
1599 { "StopWhenUnneeded", config_parse_bool, &u->meta.stop_when_unneeded, "Unit" },
1600 { "RefuseManualStart", config_parse_bool, &u->meta.refuse_manual_start, "Unit" },
1601 { "RefuseManualStop", config_parse_bool, &u->meta.refuse_manual_stop, "Unit" },
1602 { "AllowIsolate", config_parse_bool, &u->meta.allow_isolate, "Unit" },
1603 { "DefaultDependencies", config_parse_bool, &u->meta.default_dependencies, "Unit" },
1604 { "IgnoreDependencyFailure",config_parse_bool, &u->meta.ignore_dependency_failure, "Unit" },
1605 { "JobTimeoutSec", config_parse_usec, &u->meta.job_timeout, "Unit" },
1607 { "PIDFile", config_parse_path, &u->service.pid_file, "Service" },
1608 { "ExecStartPre", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START_PRE, "Service" },
1609 { "ExecStart", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START, "Service" },
1610 { "ExecStartPost", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START_POST, "Service" },
1611 { "ExecReload", config_parse_exec, u->service.exec_command+SERVICE_EXEC_RELOAD, "Service" },
1612 { "ExecStop", config_parse_exec, u->service.exec_command+SERVICE_EXEC_STOP, "Service" },
1613 { "ExecStopPost", config_parse_exec, u->service.exec_command+SERVICE_EXEC_STOP_POST, "Service" },
1614 { "RestartSec", config_parse_usec, &u->service.restart_usec, "Service" },
1615 { "TimeoutSec", config_parse_usec, &u->service.timeout_usec, "Service" },
1616 { "Type", config_parse_service_type, &u->service.type, "Service" },
1617 { "Restart", config_parse_service_restart, &u->service.restart, "Service" },
1618 { "PermissionsStartOnly", config_parse_bool, &u->service.permissions_start_only, "Service" },
1619 { "RootDirectoryStartOnly", config_parse_bool, &u->service.root_directory_start_only, "Service" },
1620 { "RemainAfterExit", config_parse_bool, &u->service.remain_after_exit, "Service" },
1621 #ifdef HAVE_SYSV_COMPAT
1622 { "SysVStartPriority", config_parse_sysv_priority, &u->service.sysv_start_priority, "Service" },
1624 { "SysVStartPriority", config_parse_warn_compat, NULL, "Service" },
1626 { "NonBlocking", config_parse_bool, &u->service.exec_context.non_blocking, "Service" },
1627 { "BusName", config_parse_string_printf, &u->service.bus_name, "Service" },
1628 { "NotifyAccess", config_parse_notify_access, &u->service.notify_access, "Service" },
1629 EXEC_CONTEXT_CONFIG_ITEMS(u->service.exec_context, "Service"),
1631 { "ListenStream", config_parse_listen, &u->socket, "Socket" },
1632 { "ListenDatagram", config_parse_listen, &u->socket, "Socket" },
1633 { "ListenSequentialPacket", config_parse_listen, &u->socket, "Socket" },
1634 { "ListenFIFO", config_parse_listen, &u->socket, "Socket" },
1635 { "BindIPv6Only", config_parse_socket_bind, &u->socket, "Socket" },
1636 { "Backlog", config_parse_unsigned, &u->socket.backlog, "Socket" },
1637 { "BindToDevice", config_parse_bindtodevice, &u->socket, "Socket" },
1638 { "ExecStartPre", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_START_PRE, "Socket" },
1639 { "ExecStartPost", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_START_POST, "Socket" },
1640 { "ExecStopPre", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_STOP_PRE, "Socket" },
1641 { "ExecStopPost", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_STOP_POST, "Socket" },
1642 { "TimeoutSec", config_parse_usec, &u->socket.timeout_usec, "Socket" },
1643 { "DirectoryMode", config_parse_mode, &u->socket.directory_mode, "Socket" },
1644 { "SocketMode", config_parse_mode, &u->socket.socket_mode, "Socket" },
1645 { "Accept", config_parse_bool, &u->socket.accept, "Socket" },
1646 { "MaxConnections", config_parse_unsigned, &u->socket.max_connections, "Socket" },
1647 { "KeepAlive", config_parse_bool, &u->socket.keep_alive, "Socket" },
1648 { "Priority", config_parse_int, &u->socket.priority, "Socket" },
1649 { "ReceiveBuffer", config_parse_size, &u->socket.receive_buffer, "Socket" },
1650 { "SendBuffer", config_parse_size, &u->socket.send_buffer, "Socket" },
1651 { "IPTOS", config_parse_ip_tos, &u->socket.ip_tos, "Socket" },
1652 { "IPTTL", config_parse_int, &u->socket.ip_ttl, "Socket" },
1653 { "Mark", config_parse_int, &u->socket.mark, "Socket" },
1654 { "PipeSize", config_parse_size, &u->socket.pipe_size, "Socket" },
1655 { "FreeBind", config_parse_bool, &u->socket.free_bind, "Socket" },
1656 { "TCPCongestion", config_parse_string, &u->socket.tcp_congestion, "Socket" },
1657 EXEC_CONTEXT_CONFIG_ITEMS(u->socket.exec_context, "Socket"),
1659 { "What", config_parse_string, &u->mount.parameters_fragment.what, "Mount" },
1660 { "Where", config_parse_path, &u->mount.where, "Mount" },
1661 { "Options", config_parse_string, &u->mount.parameters_fragment.options, "Mount" },
1662 { "Type", config_parse_string, &u->mount.parameters_fragment.fstype, "Mount" },
1663 { "TimeoutSec", config_parse_usec, &u->mount.timeout_usec, "Mount" },
1664 { "DirectoryMode", config_parse_mode, &u->mount.directory_mode, "Mount" },
1665 EXEC_CONTEXT_CONFIG_ITEMS(u->mount.exec_context, "Mount"),
1667 { "Where", config_parse_path, &u->automount.where, "Automount" },
1668 { "DirectoryMode", config_parse_mode, &u->automount.directory_mode, "Automount" },
1670 { "What", config_parse_path, &u->swap.parameters_fragment.what, "Swap" },
1671 { "Priority", config_parse_int, &u->swap.parameters_fragment.priority, "Swap" },
1673 { "OnActiveSec", config_parse_timer, &u->timer, "Timer" },
1674 { "OnBootSec", config_parse_timer, &u->timer, "Timer" },
1675 { "OnStartupSec", config_parse_timer, &u->timer, "Timer" },
1676 { "OnUnitActiveSec", config_parse_timer, &u->timer, "Timer" },
1677 { "OnUnitInactiveSec", config_parse_timer, &u->timer, "Timer" },
1678 { "Unit", config_parse_timer_unit, &u->timer, "Timer" },
1680 { "PathExists", config_parse_path_spec, &u->path, "Path" },
1681 { "PathChanged", config_parse_path_spec, &u->path, "Path" },
1682 { "DirectoryNotEmpty", config_parse_path_spec, &u->path, "Path" },
1683 { "Unit", config_parse_path_unit, &u->path, "Path" },
1685 /* The [Install] section is ignored here. */
1686 { "Alias", NULL, NULL, "Install" },
1687 { "WantedBy", NULL, NULL, "Install" },
1688 { "Also", NULL, NULL, "Install" },
1690 { NULL, NULL, NULL, NULL }
1693 #undef EXEC_CONTEXT_CONFIG_ITEMS
1695 const char *sections[4];
1699 char *filename = NULL, *id = NULL;
1704 /* Dirty dirty hack. */
1705 dump_items((FILE*) path, items);
1712 sections[0] = "Unit";
1713 sections[1] = section_table[u->meta.type];
1714 sections[2] = "Install";
1717 if (!(symlink_names = set_new(string_hash_func, string_compare_func)))
1720 if (path_is_absolute(path)) {
1722 if (!(filename = strdup(path))) {
1727 if ((r = open_follow(&filename, &f, symlink_names, &id)) < 0) {
1738 STRV_FOREACH(p, u->meta.manager->lookup_paths.unit_path) {
1740 /* Instead of opening the path right away, we manually
1741 * follow all symlinks and add their name to our unit
1742 * name set while doing so */
1743 if (!(filename = path_make_absolute(path, *p))) {
1748 if (u->meta.manager->unit_path_cache &&
1749 !set_get(u->meta.manager->unit_path_cache, filename))
1752 r = open_follow(&filename, &f, symlink_names, &id);
1763 /* Empty the symlink names for the next run */
1764 while ((sn = set_steal_first(symlink_names)))
1775 /* Hmm, no suitable file found? */
1781 if ((r = merge_by_names(&merged, symlink_names, id)) < 0)
1785 u->meta.load_state = UNIT_MERGED;
1791 if (fstat(fileno(f), &st) < 0) {
1796 /* Now, parse the file contents */
1797 if ((r = config_parse(filename, f, sections, items, false, u)) < 0)
1800 free(u->meta.fragment_path);
1801 u->meta.fragment_path = filename;
1804 u->meta.fragment_mtime = timespec_load(&st.st_mtim);
1806 u->meta.load_state = UNIT_LOADED;
1810 set_free_free(symlink_names);
1819 int unit_load_fragment(Unit *u) {
1825 assert(u->meta.load_state == UNIT_STUB);
1828 /* First, try to find the unit under its id. We always look
1829 * for unit files in the default directories, to make it easy
1830 * to override things by placing things in /etc/systemd/system */
1831 if ((r = load_from_path(u, u->meta.id)) < 0)
1834 /* Try to find an alias we can load this with */
1835 if (u->meta.load_state == UNIT_STUB)
1836 SET_FOREACH(t, u->meta.names, i) {
1838 if (t == u->meta.id)
1841 if ((r = load_from_path(u, t)) < 0)
1844 if (u->meta.load_state != UNIT_STUB)
1848 /* And now, try looking for it under the suggested (originally linked) path */
1849 if (u->meta.load_state == UNIT_STUB && u->meta.fragment_path) {
1851 if ((r = load_from_path(u, u->meta.fragment_path)) < 0)
1854 if (u->meta.load_state == UNIT_STUB) {
1855 /* Hmm, this didn't work? Then let's get rid
1856 * of the fragment path stored for us, so that
1857 * we don't point to an invalid location. */
1858 free(u->meta.fragment_path);
1859 u->meta.fragment_path = NULL;
1863 /* Look for a template */
1864 if (u->meta.load_state == UNIT_STUB && u->meta.instance) {
1867 if (!(k = unit_name_template(u->meta.id)))
1870 r = load_from_path(u, k);
1876 if (u->meta.load_state == UNIT_STUB)
1877 SET_FOREACH(t, u->meta.names, i) {
1879 if (t == u->meta.id)
1882 if (!(k = unit_name_template(t)))
1885 r = load_from_path(u, k);
1891 if (u->meta.load_state != UNIT_STUB)
1899 void unit_dump_config_items(FILE *f) {
1900 /* OK, this wins a prize for extreme ugliness. */
1902 load_from_path(NULL, (const void*) f);