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 #define COMMENTS "#;\n"
47 static int config_parse_deps(
56 UnitDependency d = PTR_TO_UINT(data);
66 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
70 if (!(t = strndup(w, l)))
73 k = unit_name_printf(u, t);
79 r = unit_add_dependency_by_name(u, d, k, NULL, true);
82 log_error("Failed to add dependency on %s, ignoring: %s", k, strerror(-r));
93 static int config_parse_names(
112 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
116 if (!(t = strndup(w, l)))
119 k = unit_name_printf(u, t);
125 r = unit_merge_by_name(u, k);
128 log_error("Failed to add name %s, ignoring: %s", k, strerror(-r));
139 static int config_parse_string_printf(
140 const char *filename,
158 if (!(k = unit_full_printf(u, rvalue)))
172 static int config_parse_listen(
173 const char *filename,
192 if (!(p = new0(SocketPort, 1)))
195 if (streq(lvalue, "ListenFIFO")) {
196 p->type = SOCKET_FIFO;
198 if (!(p->path = strdup(rvalue))) {
203 path_kill_slashes(p->path);
205 p->type = SOCKET_SOCKET;
207 if ((r = socket_address_parse(&p->address, rvalue)) < 0) {
208 log_error("[%s:%u] Failed to parse address value, ignoring: %s", filename, line, rvalue);
213 if (streq(lvalue, "ListenStream"))
214 p->address.type = SOCK_STREAM;
215 else if (streq(lvalue, "ListenDatagram"))
216 p->address.type = SOCK_DGRAM;
218 assert(streq(lvalue, "ListenSequentialPacket"));
219 p->address.type = SOCK_SEQPACKET;
222 if (socket_address_family(&p->address) != AF_LOCAL && p->address.type == SOCK_SEQPACKET) {
223 log_error("[%s:%u] Address family not supported, ignoring: %s", filename, line, rvalue);
230 LIST_PREPEND(SocketPort, port, s->ports, p);
235 static int config_parse_socket_bind(
236 const char *filename,
245 SocketAddressBindIPv6Only b;
254 if ((b = socket_address_bind_ipv6_only_from_string(rvalue)) < 0) {
257 if ((r = parse_boolean(rvalue)) < 0) {
258 log_error("[%s:%u] Failed to parse bind IPv6 only value, ignoring: %s", filename, line, rvalue);
262 s->bind_ipv6_only = r ? SOCKET_ADDRESS_IPV6_ONLY : SOCKET_ADDRESS_BOTH;
264 s->bind_ipv6_only = b;
269 static int config_parse_nice(
270 const char *filename,
278 ExecContext *c = data;
286 if ((r = safe_atoi(rvalue, &priority)) < 0) {
287 log_error("[%s:%u] Failed to parse nice priority, ignoring: %s. ", filename, line, rvalue);
291 if (priority < PRIO_MIN || priority >= PRIO_MAX) {
292 log_error("[%s:%u] Nice priority out of range, ignoring: %s", filename, line, rvalue);
302 static int config_parse_oom_adjust(
303 const char *filename,
311 ExecContext *c = data;
319 if ((r = safe_atoi(rvalue, &oa)) < 0) {
320 log_error("[%s:%u] Failed to parse OOM adjust value, ignoring: %s", filename, line, rvalue);
324 if (oa < OOM_DISABLE || oa > OOM_ADJUST_MAX) {
325 log_error("[%s:%u] OOM adjust value out of range, ignoring: %s", filename, line, rvalue);
330 c->oom_adjust_set = true;
335 static int config_parse_mode(
336 const char *filename,
354 l = strtol(rvalue, &x, 8);
355 if (!x || *x || errno) {
356 log_error("[%s:%u] Failed to parse mode value, ignoring: %s", filename, line, rvalue);
360 if (l < 0000 || l > 07777) {
361 log_error("[%s:%u] mode value out of range, ignoring: %s", filename, line, rvalue);
369 static int config_parse_exec(
370 const char *filename,
378 ExecCommand **e = data, *nce;
387 /* We accept an absolute path as first argument, or
388 * alternatively an absolute prefixed with @ to allow
389 * overriding of argv[0]. */
395 bool honour_argv0 = false, ignore = false;
401 rvalue += strspn(rvalue, WHITESPACE);
406 if (rvalue[0] == '-') {
411 if (rvalue[0] == '@') {
416 if (*rvalue != '/') {
417 log_error("[%s:%u] Invalid executable path in command line, ignoring: %s", filename, line, rvalue);
422 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
423 if (strncmp(w, ";", l) == 0)
429 if (!(n = new(char*, k + !honour_argv0)))
433 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
434 if (strncmp(w, ";", l) == 0)
437 if (honour_argv0 && w == rvalue) {
439 if (!(path = cunescape_length(w, l)))
442 if (!(n[k++] = cunescape_length(w, l)))
450 log_error("[%s:%u] Invalid command line, ignoring: %s", filename, line, rvalue);
456 if (!(path = strdup(n[0])))
459 assert(path_is_absolute(path));
461 if (!(nce = new0(ExecCommand, 1)))
466 nce->ignore = ignore;
468 path_kill_slashes(nce->path);
470 exec_command_append_list(e, nce);
486 static int config_parse_usec(
487 const char *filename,
503 if ((r = parse_usec(rvalue, usec)) < 0) {
504 log_error("[%s:%u] Failed to parse time value, ignoring: %s", filename, line, rvalue);
511 static DEFINE_CONFIG_PARSE_ENUM(config_parse_service_type, service_type, ServiceType, "Failed to parse service type");
512 static DEFINE_CONFIG_PARSE_ENUM(config_parse_service_restart, service_restart, ServiceRestart, "Failed to parse service restart specifier");
514 static int config_parse_bindtodevice(
515 const char *filename,
531 if (rvalue[0] && !streq(rvalue, "*")) {
532 if (!(n = strdup(rvalue)))
537 free(s->bind_to_device);
538 s->bind_to_device = n;
543 static DEFINE_CONFIG_PARSE_ENUM(config_parse_output, exec_output, ExecOutput, "Failed to parse output specifier");
544 static DEFINE_CONFIG_PARSE_ENUM(config_parse_input, exec_input, ExecInput, "Failed to parse input specifier");
546 static int config_parse_facility(
547 const char *filename,
563 if ((x = log_facility_from_string(rvalue)) < 0) {
564 log_error("[%s:%u] Failed to parse log facility, ignoring: %s", filename, line, rvalue);
568 *o = LOG_MAKEPRI(x, LOG_PRI(*o));
573 static int config_parse_level(
574 const char *filename,
590 if ((x = log_level_from_string(rvalue)) < 0) {
591 log_error("[%s:%u] Failed to parse log level, ignoring: %s", filename, line, rvalue);
595 *o = LOG_MAKEPRI(LOG_FAC(*o), x);
599 static int config_parse_io_class(
600 const char *filename,
608 ExecContext *c = data;
616 if ((x = ioprio_class_from_string(rvalue)) < 0) {
617 log_error("[%s:%u] Failed to parse IO scheduling class, ignoring: %s", filename, line, rvalue);
621 c->ioprio = IOPRIO_PRIO_VALUE(x, IOPRIO_PRIO_DATA(c->ioprio));
622 c->ioprio_set = true;
627 static int config_parse_io_priority(
628 const char *filename,
636 ExecContext *c = data;
644 if (safe_atoi(rvalue, &i) < 0 || i < 0 || i >= IOPRIO_BE_NR) {
645 log_error("[%s:%u] Failed to parse io priority, ignoring: %s", filename, line, rvalue);
649 c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_PRIO_CLASS(c->ioprio), i);
650 c->ioprio_set = true;
655 static int config_parse_cpu_sched_policy(
656 const char *filename,
665 ExecContext *c = data;
673 if ((x = sched_policy_from_string(rvalue)) < 0) {
674 log_error("[%s:%u] Failed to parse CPU scheduling policy, ignoring: %s", filename, line, rvalue);
678 c->cpu_sched_policy = x;
679 c->cpu_sched_set = true;
684 static int config_parse_cpu_sched_prio(
685 const char *filename,
693 ExecContext *c = data;
701 /* On Linux RR/FIFO have the same range */
702 if (safe_atoi(rvalue, &i) < 0 || i < sched_get_priority_min(SCHED_RR) || i > sched_get_priority_max(SCHED_RR)) {
703 log_error("[%s:%u] Failed to parse CPU scheduling priority, ignoring: %s", filename, line, rvalue);
707 c->cpu_sched_priority = i;
708 c->cpu_sched_set = true;
713 static int config_parse_cpu_affinity(
714 const char *filename,
722 ExecContext *c = data;
732 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
737 if (!(t = strndup(w, l)))
740 r = safe_atou(t, &cpu);
744 if (!(c->cpuset = cpu_set_malloc(&c->cpuset_ncpus)))
747 if (r < 0 || cpu >= c->cpuset_ncpus) {
748 log_error("[%s:%u] Failed to parse CPU affinity, ignoring: %s", filename, line, rvalue);
752 CPU_SET_S(cpu, CPU_ALLOC_SIZE(c->cpuset_ncpus), c->cpuset);
758 static int config_parse_capabilities(
759 const char *filename,
767 ExecContext *c = data;
775 if (!(cap = cap_from_text(rvalue))) {
779 log_error("[%s:%u] Failed to parse capabilities, ignoring: %s", filename, line, rvalue);
784 cap_free(c->capabilities);
785 c->capabilities = cap;
790 static int config_parse_secure_bits(
791 const char *filename,
799 ExecContext *c = data;
809 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
810 if (first_word(w, "keep-caps"))
811 c->secure_bits |= SECURE_KEEP_CAPS;
812 else if (first_word(w, "keep-caps-locked"))
813 c->secure_bits |= SECURE_KEEP_CAPS_LOCKED;
814 else if (first_word(w, "no-setuid-fixup"))
815 c->secure_bits |= SECURE_NO_SETUID_FIXUP;
816 else if (first_word(w, "no-setuid-fixup-locked"))
817 c->secure_bits |= SECURE_NO_SETUID_FIXUP_LOCKED;
818 else if (first_word(w, "noroot"))
819 c->secure_bits |= SECURE_NOROOT;
820 else if (first_word(w, "noroot-locked"))
821 c->secure_bits |= SECURE_NOROOT_LOCKED;
823 log_error("[%s:%u] Failed to parse secure bits, ignoring: %s", filename, line, rvalue);
831 static int config_parse_bounding_set(
832 const char *filename,
840 ExecContext *c = data;
850 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
855 if (!(t = strndup(w, l)))
858 r = cap_from_name(t, &cap);
862 log_error("[%s:%u] Failed to parse capability bounding set, ignoring: %s", filename, line, rvalue);
866 c->capability_bounding_set_drop |= 1 << cap;
872 static int config_parse_timer_slack_nsec(
873 const char *filename,
881 ExecContext *c = data;
890 if ((r = safe_atolu(rvalue, &u)) < 0) {
891 log_error("[%s:%u] Failed to parse time slack value, ignoring: %s", filename, line, rvalue);
895 c->timer_slack_nsec = u;
900 static int config_parse_limit(
901 const char *filename,
909 struct rlimit **rl = data;
910 unsigned long long u;
918 if ((r = safe_atollu(rvalue, &u)) < 0) {
919 log_error("[%s:%u] Failed to parse resource value, ignoring: %s", filename, line, rvalue);
924 if (!(*rl = new(struct rlimit, 1)))
927 (*rl)->rlim_cur = (*rl)->rlim_max = (rlim_t) u;
931 static int config_parse_cgroup(
932 const char *filename,
945 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
949 if (!(t = cunescape_length(w, l)))
952 r = unit_add_cgroup_from_text(u, t);
956 log_error("[%s:%u] Failed to parse cgroup value, ignoring: %s", filename, line, rvalue);
964 static int config_parse_sysv_priority(
965 const char *filename,
973 int *priority = data;
981 if ((r = safe_atoi(rvalue, &i)) < 0 || i < 0) {
982 log_error("[%s:%u] Failed to parse SysV start priority, ignoring: %s", filename, line, rvalue);
990 static DEFINE_CONFIG_PARSE_ENUM(config_parse_kill_mode, kill_mode, KillMode, "Failed to parse kill mode");
992 static int config_parse_kill_signal(
993 const char *filename,
1009 if ((r = signal_from_string(rvalue)) <= 0)
1010 if (startswith(rvalue, "SIG"))
1011 r = signal_from_string(rvalue+3);
1014 log_error("[%s:%u] Failed to parse kill signal, ignoring: %s", filename, line, rvalue);
1022 static int config_parse_mount_flags(
1023 const char *filename,
1025 const char *section,
1031 ExecContext *c = data;
1035 unsigned long flags = 0;
1042 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
1043 if (strncmp(w, "shared", l) == 0)
1045 else if (strncmp(w, "slave", l) == 0)
1047 else if (strncmp(w, "private", l) == 0)
1048 flags |= MS_PRIVATE;
1050 log_error("[%s:%u] Failed to parse mount flags, ignoring: %s", filename, line, rvalue);
1055 c->mount_flags = flags;
1059 static int config_parse_timer(
1060 const char *filename,
1062 const char *section,
1079 if ((b = timer_base_from_string(lvalue)) < 0) {
1080 log_error("[%s:%u] Failed to parse timer base, ignoring: %s", filename, line, lvalue);
1084 if ((r = parse_usec(rvalue, &u)) < 0) {
1085 log_error("[%s:%u] Failed to parse timer value, ignoring: %s", filename, line, rvalue);
1089 if (!(v = new0(TimerValue, 1)))
1095 LIST_PREPEND(TimerValue, value, t->values, v);
1100 static int config_parse_timer_unit(
1101 const char *filename,
1103 const char *section,
1118 dbus_error_init(&error);
1120 if (endswith(rvalue, ".timer")) {
1121 log_error("[%s:%u] Unit cannot be of type timer, ignoring: %s", filename, line, rvalue);
1125 if ((r = manager_load_unit(t->meta.manager, rvalue, NULL, NULL, &t->unit)) < 0) {
1126 log_error("[%s:%u] Failed to load unit %s, ignoring: %s", filename, line, rvalue, bus_error(&error, r));
1127 dbus_error_free(&error);
1134 static int config_parse_path_spec(
1135 const char *filename,
1137 const char *section,
1152 if ((b = path_type_from_string(lvalue)) < 0) {
1153 log_error("[%s:%u] Failed to parse path type, ignoring: %s", filename, line, lvalue);
1157 if (!path_is_absolute(rvalue)) {
1158 log_error("[%s:%u] Path is not absolute, ignoring: %s", filename, line, rvalue);
1162 if (!(s = new0(PathSpec, 1)))
1165 if (!(s->path = strdup(rvalue))) {
1170 path_kill_slashes(s->path);
1175 LIST_PREPEND(PathSpec, spec, p->specs, s);
1180 static int config_parse_path_unit(
1181 const char *filename,
1183 const char *section,
1198 dbus_error_init(&error);
1200 if (endswith(rvalue, ".path")) {
1201 log_error("[%s:%u] Unit cannot be of type path, ignoring: %s", filename, line, rvalue);
1205 if ((r = manager_load_unit(t->meta.manager, rvalue, NULL, &error, &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_env_file(
1215 const char *filename,
1217 const char *section,
1232 if (!(f = fopen(rvalue, "re"))) {
1233 log_error("[%s:%u] Failed to open environment file '%s', ignoring: %m", filename, line, rvalue);
1238 char l[LINE_MAX], *p;
1241 if (!fgets(l, sizeof(l), f)) {
1246 log_error("[%s:%u] Failed to read environment file '%s', ignoring: %m", filename, line, rvalue);
1256 if (strchr(COMMENTS, *p))
1259 t = strv_env_set(*env, p);
1273 static int config_parse_ip_tos(
1274 const char *filename,
1276 const char *section,
1282 int *ip_tos = data, x;
1290 if ((x = ip_tos_from_string(rvalue)) < 0)
1291 if ((r = safe_atoi(rvalue, &x)) < 0) {
1292 log_error("[%s:%u] Failed to parse IP TOS value, ignoring: %s", filename, line, rvalue);
1300 static DEFINE_CONFIG_PARSE_ENUM(config_parse_notify_access, notify_access, NotifyAccess, "Failed to parse notify access specifier");
1302 #define FOLLOW_MAX 8
1304 static int open_follow(char **filename, FILE **_f, Set *names, char **_final) {
1315 /* This will update the filename pointer if the loaded file is
1316 * reached by a symlink. The old string will be freed. */
1319 char *target, *name;
1321 if (c++ >= FOLLOW_MAX)
1324 path_kill_slashes(*filename);
1326 /* Add the file name we are currently looking at to
1327 * the names of this unit, but only if it is a valid
1329 name = file_name_from_path(*filename);
1331 if (unit_name_is_valid(name)) {
1332 if (!(id = set_get(names, name))) {
1334 if (!(id = strdup(name)))
1337 if ((r = set_put(names, id)) < 0) {
1344 /* Try to open the file name, but don't if its a symlink */
1345 if ((fd = open(*filename, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW)) >= 0)
1351 /* Hmm, so this is a symlink. Let's read the name, and follow it manually */
1352 if ((r = readlink_and_make_absolute(*filename, &target)) < 0)
1359 if (!(f = fdopen(fd, "re"))) {
1361 close_nointr_nofail(fd);
1370 static int merge_by_names(Unit **u, Set *names, const char *id) {
1378 /* Let's try to add in all symlink names we found */
1379 while ((k = set_steal_first(names))) {
1381 /* First try to merge in the other name into our
1383 if ((r = unit_merge_by_name(*u, k)) < 0) {
1386 /* Hmm, we couldn't merge the other unit into
1387 * ours? Then let's try it the other way
1390 other = manager_get_unit((*u)->meta.manager, k);
1394 if ((r = unit_merge(other, *u)) >= 0) {
1396 return merge_by_names(u, names, NULL);
1403 unit_choose_id(*u, id);
1411 static void dump_items(FILE *f, const ConfigItem *items) {
1412 const ConfigItem *i;
1413 const char *prev_section = NULL;
1414 bool not_first = false;
1417 ConfigParserCallback callback;
1420 { config_parse_int, "INTEGER" },
1421 { config_parse_unsigned, "UNSIGNED" },
1422 { config_parse_size, "SIZE" },
1423 { config_parse_bool, "BOOLEAN" },
1424 { config_parse_string, "STRING" },
1425 { config_parse_path, "PATH" },
1426 { config_parse_strv, "STRING [...]" },
1427 { config_parse_nice, "NICE" },
1428 { config_parse_oom_adjust, "OOMADJUST" },
1429 { config_parse_io_class, "IOCLASS" },
1430 { config_parse_io_priority, "IOPRIORITY" },
1431 { config_parse_cpu_sched_policy, "CPUSCHEDPOLICY" },
1432 { config_parse_cpu_sched_prio, "CPUSCHEDPRIO" },
1433 { config_parse_cpu_affinity, "CPUAFFINITY" },
1434 { config_parse_mode, "MODE" },
1435 { config_parse_env_file, "FILE" },
1436 { config_parse_output, "OUTPUT" },
1437 { config_parse_input, "INPUT" },
1438 { config_parse_facility, "FACILITY" },
1439 { config_parse_level, "LEVEL" },
1440 { config_parse_capabilities, "CAPABILITIES" },
1441 { config_parse_secure_bits, "SECUREBITS" },
1442 { config_parse_bounding_set, "BOUNDINGSET" },
1443 { config_parse_timer_slack_nsec, "TIMERSLACK" },
1444 { config_parse_limit, "LIMIT" },
1445 { config_parse_cgroup, "CGROUP [...]" },
1446 { config_parse_deps, "UNIT [...]" },
1447 { config_parse_names, "UNIT [...]" },
1448 { config_parse_exec, "PATH [ARGUMENT [...]]" },
1449 { config_parse_service_type, "SERVICETYPE" },
1450 { config_parse_service_restart, "SERVICERESTART" },
1451 { config_parse_sysv_priority, "SYSVPRIORITY" },
1452 { config_parse_kill_mode, "KILLMODE" },
1453 { config_parse_kill_signal, "SIGNAL" },
1454 { config_parse_listen, "SOCKET [...]" },
1455 { config_parse_socket_bind, "SOCKETBIND" },
1456 { config_parse_bindtodevice, "NETWORKINTERFACE" },
1457 { config_parse_usec, "SECONDS" },
1458 { config_parse_path_strv, "PATH [...]" },
1459 { config_parse_mount_flags, "MOUNTFLAG [...]" },
1460 { config_parse_string_printf, "STRING" },
1461 { config_parse_timer, "TIMER" },
1462 { config_parse_timer_unit, "NAME" },
1463 { config_parse_path_spec, "PATH" },
1464 { config_parse_path_unit, "UNIT" },
1465 { config_parse_notify_access, "ACCESS" },
1466 { config_parse_ip_tos, "TOS" },
1472 for (i = items; i->lvalue; i++) {
1474 const char *rvalue = "OTHER";
1476 if (!streq_ptr(i->section, prev_section)) {
1482 fprintf(f, "[%s]\n", i->section);
1483 prev_section = i->section;
1486 for (j = 0; j < ELEMENTSOF(table); j++)
1487 if (i->parse == table[j].callback) {
1488 rvalue = table[j].rvalue;
1492 fprintf(f, "%s=%s\n", i->lvalue, rvalue);
1496 static int load_from_path(Unit *u, const char *path) {
1498 static const char* const section_table[_UNIT_TYPE_MAX] = {
1499 [UNIT_SERVICE] = "Service",
1500 [UNIT_TIMER] = "Timer",
1501 [UNIT_SOCKET] = "Socket",
1502 [UNIT_TARGET] = "Target",
1503 [UNIT_DEVICE] = "Device",
1504 [UNIT_MOUNT] = "Mount",
1505 [UNIT_AUTOMOUNT] = "Automount",
1506 [UNIT_SNAPSHOT] = "Snapshot",
1507 [UNIT_SWAP] = "Swap",
1508 [UNIT_PATH] = "Path"
1511 #define EXEC_CONTEXT_CONFIG_ITEMS(context, section) \
1512 { "WorkingDirectory", config_parse_path, &(context).working_directory, section }, \
1513 { "RootDirectory", config_parse_path, &(context).root_directory, section }, \
1514 { "User", config_parse_string_printf, &(context).user, section }, \
1515 { "Group", config_parse_string_printf, &(context).group, section }, \
1516 { "SupplementaryGroups", config_parse_strv, &(context).supplementary_groups, section }, \
1517 { "Nice", config_parse_nice, &(context), section }, \
1518 { "OOMAdjust", config_parse_oom_adjust, &(context), section }, \
1519 { "IOSchedulingClass", config_parse_io_class, &(context), section }, \
1520 { "IOSchedulingPriority", config_parse_io_priority, &(context), section }, \
1521 { "CPUSchedulingPolicy", config_parse_cpu_sched_policy,&(context), section }, \
1522 { "CPUSchedulingPriority", config_parse_cpu_sched_prio, &(context), section }, \
1523 { "CPUSchedulingResetOnFork", config_parse_bool, &(context).cpu_sched_reset_on_fork, section }, \
1524 { "CPUAffinity", config_parse_cpu_affinity, &(context), section }, \
1525 { "UMask", config_parse_mode, &(context).umask, section }, \
1526 { "Environment", config_parse_strv, &(context).environment, section }, \
1527 { "EnvironmentFile", config_parse_env_file, &(context).environment, section }, \
1528 { "StandardInput", config_parse_input, &(context).std_input, section }, \
1529 { "StandardOutput", config_parse_output, &(context).std_output, section }, \
1530 { "StandardError", config_parse_output, &(context).std_error, section }, \
1531 { "TTYPath", config_parse_path, &(context).tty_path, section }, \
1532 { "SyslogIdentifier", config_parse_string_printf, &(context).syslog_identifier, section }, \
1533 { "SyslogFacility", config_parse_facility, &(context).syslog_priority, section }, \
1534 { "SyslogLevel", config_parse_level, &(context).syslog_priority, section }, \
1535 { "SyslogLevelPrefix", config_parse_bool, &(context).syslog_level_prefix, section }, \
1536 { "Capabilities", config_parse_capabilities, &(context), section }, \
1537 { "SecureBits", config_parse_secure_bits, &(context), section }, \
1538 { "CapabilityBoundingSetDrop", config_parse_bounding_set, &(context), section }, \
1539 { "TimerSlackNSec", config_parse_timer_slack_nsec,&(context), section }, \
1540 { "LimitCPU", config_parse_limit, &(context).rlimit[RLIMIT_CPU], section }, \
1541 { "LimitFSIZE", config_parse_limit, &(context).rlimit[RLIMIT_FSIZE], section }, \
1542 { "LimitDATA", config_parse_limit, &(context).rlimit[RLIMIT_DATA], section }, \
1543 { "LimitSTACK", config_parse_limit, &(context).rlimit[RLIMIT_STACK], section }, \
1544 { "LimitCORE", config_parse_limit, &(context).rlimit[RLIMIT_CORE], section }, \
1545 { "LimitRSS", config_parse_limit, &(context).rlimit[RLIMIT_RSS], section }, \
1546 { "LimitNOFILE", config_parse_limit, &(context).rlimit[RLIMIT_NOFILE], section }, \
1547 { "LimitAS", config_parse_limit, &(context).rlimit[RLIMIT_AS], section }, \
1548 { "LimitNPROC", config_parse_limit, &(context).rlimit[RLIMIT_NPROC], section }, \
1549 { "LimitMEMLOCK", config_parse_limit, &(context).rlimit[RLIMIT_MEMLOCK], section }, \
1550 { "LimitLOCKS", config_parse_limit, &(context).rlimit[RLIMIT_LOCKS], section }, \
1551 { "LimitSIGPENDING", config_parse_limit, &(context).rlimit[RLIMIT_SIGPENDING], section }, \
1552 { "LimitMSGQUEUE", config_parse_limit, &(context).rlimit[RLIMIT_MSGQUEUE], section }, \
1553 { "LimitNICE", config_parse_limit, &(context).rlimit[RLIMIT_NICE], section }, \
1554 { "LimitRTPRIO", config_parse_limit, &(context).rlimit[RLIMIT_RTPRIO], section }, \
1555 { "LimitRTTIME", config_parse_limit, &(context).rlimit[RLIMIT_RTTIME], section }, \
1556 { "ControlGroup", config_parse_cgroup, u, section }, \
1557 { "ReadWriteDirectories", config_parse_path_strv, &(context).read_write_dirs, section }, \
1558 { "ReadOnlyDirectories", config_parse_path_strv, &(context).read_only_dirs, section }, \
1559 { "InaccessibleDirectories",config_parse_path_strv, &(context).inaccessible_dirs, section }, \
1560 { "PrivateTmp", config_parse_bool, &(context).private_tmp, section }, \
1561 { "MountFlags", config_parse_mount_flags, &(context), section }, \
1562 { "TCPWrapName", config_parse_string_printf, &(context).tcpwrap_name, section }, \
1563 { "PAMName", config_parse_string_printf, &(context).pam_name, section }, \
1564 { "KillMode", config_parse_kill_mode, &(context).kill_mode, section }, \
1565 { "KillSignal", config_parse_kill_signal, &(context).kill_signal, section }
1567 const ConfigItem items[] = {
1568 { "Names", config_parse_names, u, "Unit" },
1569 { "Description", config_parse_string_printf, &u->meta.description, "Unit" },
1570 { "Requires", config_parse_deps, UINT_TO_PTR(UNIT_REQUIRES), "Unit" },
1571 { "RequiresOverridable", config_parse_deps, UINT_TO_PTR(UNIT_REQUIRES_OVERRIDABLE), "Unit" },
1572 { "Requisite", config_parse_deps, UINT_TO_PTR(UNIT_REQUISITE), "Unit" },
1573 { "RequisiteOverridable", config_parse_deps, UINT_TO_PTR(UNIT_REQUISITE_OVERRIDABLE), "Unit" },
1574 { "Wants", config_parse_deps, UINT_TO_PTR(UNIT_WANTS), "Unit" },
1575 { "Conflicts", config_parse_deps, UINT_TO_PTR(UNIT_CONFLICTS), "Unit" },
1576 { "Before", config_parse_deps, UINT_TO_PTR(UNIT_BEFORE), "Unit" },
1577 { "After", config_parse_deps, UINT_TO_PTR(UNIT_AFTER), "Unit" },
1578 { "OnFailure", config_parse_deps, UINT_TO_PTR(UNIT_ON_FAILURE), "Unit" },
1579 { "RecursiveStop", config_parse_bool, &u->meta.recursive_stop, "Unit" },
1580 { "StopWhenUnneeded", config_parse_bool, &u->meta.stop_when_unneeded, "Unit" },
1581 { "RefuseManualStart", config_parse_bool, &u->meta.refuse_manual_start, "Unit" },
1582 { "RefuseManualStop", config_parse_bool, &u->meta.refuse_manual_stop, "Unit" },
1583 { "DefaultDependencies", config_parse_bool, &u->meta.default_dependencies, "Unit" },
1584 { "IgnoreDependencyFailure",config_parse_bool, &u->meta.ignore_dependency_failure, "Unit" },
1585 { "JobTimeoutSec", config_parse_usec, &u->meta.job_timeout, "Unit" },
1587 { "PIDFile", config_parse_path, &u->service.pid_file, "Service" },
1588 { "ExecStartPre", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START_PRE, "Service" },
1589 { "ExecStart", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START, "Service" },
1590 { "ExecStartPost", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START_POST, "Service" },
1591 { "ExecReload", config_parse_exec, u->service.exec_command+SERVICE_EXEC_RELOAD, "Service" },
1592 { "ExecStop", config_parse_exec, u->service.exec_command+SERVICE_EXEC_STOP, "Service" },
1593 { "ExecStopPost", config_parse_exec, u->service.exec_command+SERVICE_EXEC_STOP_POST, "Service" },
1594 { "RestartSec", config_parse_usec, &u->service.restart_usec, "Service" },
1595 { "TimeoutSec", config_parse_usec, &u->service.timeout_usec, "Service" },
1596 { "Type", config_parse_service_type, &u->service.type, "Service" },
1597 { "Restart", config_parse_service_restart, &u->service.restart, "Service" },
1598 { "PermissionsStartOnly", config_parse_bool, &u->service.permissions_start_only, "Service" },
1599 { "RootDirectoryStartOnly", config_parse_bool, &u->service.root_directory_start_only, "Service" },
1600 { "RemainAfterExit", config_parse_bool, &u->service.remain_after_exit, "Service" },
1601 { "SysVStartPriority", config_parse_sysv_priority, &u->service.sysv_start_priority, "Service" },
1602 { "NonBlocking", config_parse_bool, &u->service.exec_context.non_blocking, "Service" },
1603 { "BusName", config_parse_string_printf, &u->service.bus_name, "Service" },
1604 { "NotifyAccess", config_parse_notify_access, &u->service.notify_access, "Service" },
1605 EXEC_CONTEXT_CONFIG_ITEMS(u->service.exec_context, "Service"),
1607 { "ListenStream", config_parse_listen, &u->socket, "Socket" },
1608 { "ListenDatagram", config_parse_listen, &u->socket, "Socket" },
1609 { "ListenSequentialPacket", config_parse_listen, &u->socket, "Socket" },
1610 { "ListenFIFO", config_parse_listen, &u->socket, "Socket" },
1611 { "BindIPv6Only", config_parse_socket_bind, &u->socket, "Socket" },
1612 { "Backlog", config_parse_unsigned, &u->socket.backlog, "Socket" },
1613 { "BindToDevice", config_parse_bindtodevice, &u->socket, "Socket" },
1614 { "ExecStartPre", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_START_PRE, "Socket" },
1615 { "ExecStartPost", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_START_POST, "Socket" },
1616 { "ExecStopPre", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_STOP_PRE, "Socket" },
1617 { "ExecStopPost", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_STOP_POST, "Socket" },
1618 { "TimeoutSec", config_parse_usec, &u->socket.timeout_usec, "Socket" },
1619 { "DirectoryMode", config_parse_mode, &u->socket.directory_mode, "Socket" },
1620 { "SocketMode", config_parse_mode, &u->socket.socket_mode, "Socket" },
1621 { "Accept", config_parse_bool, &u->socket.accept, "Socket" },
1622 { "MaxConnections", config_parse_unsigned, &u->socket.max_connections, "Socket" },
1623 { "KeepAlive", config_parse_bool, &u->socket.keep_alive, "Socket" },
1624 { "Priority", config_parse_int, &u->socket.priority, "Socket" },
1625 { "ReceiveBuffer", config_parse_size, &u->socket.receive_buffer, "Socket" },
1626 { "SendBuffer", config_parse_size, &u->socket.send_buffer, "Socket" },
1627 { "IPTOS", config_parse_ip_tos, &u->socket.ip_tos, "Socket" },
1628 { "IPTTL", config_parse_int, &u->socket.ip_ttl, "Socket" },
1629 { "Mark", config_parse_int, &u->socket.mark, "Socket" },
1630 { "PipeSize", config_parse_size, &u->socket.pipe_size, "Socket" },
1631 { "FreeBind", config_parse_bool, &u->socket.free_bind, "Socket" },
1632 { "TCPCongestion", config_parse_string, &u->socket.tcp_congestion, "Socket" },
1633 EXEC_CONTEXT_CONFIG_ITEMS(u->socket.exec_context, "Socket"),
1635 { "What", config_parse_string, &u->mount.parameters_fragment.what, "Mount" },
1636 { "Where", config_parse_path, &u->mount.where, "Mount" },
1637 { "Options", config_parse_string, &u->mount.parameters_fragment.options, "Mount" },
1638 { "Type", config_parse_string, &u->mount.parameters_fragment.fstype, "Mount" },
1639 { "TimeoutSec", config_parse_usec, &u->mount.timeout_usec, "Mount" },
1640 { "DirectoryMode", config_parse_mode, &u->mount.directory_mode, "Mount" },
1641 EXEC_CONTEXT_CONFIG_ITEMS(u->mount.exec_context, "Mount"),
1643 { "Where", config_parse_path, &u->automount.where, "Automount" },
1644 { "DirectoryMode", config_parse_mode, &u->automount.directory_mode, "Automount" },
1646 { "What", config_parse_path, &u->swap.parameters_fragment.what, "Swap" },
1647 { "Priority", config_parse_int, &u->swap.parameters_fragment.priority, "Swap" },
1649 { "OnActiveSec", config_parse_timer, &u->timer, "Timer" },
1650 { "OnBootSec", config_parse_timer, &u->timer, "Timer" },
1651 { "OnStartupSec", config_parse_timer, &u->timer, "Timer" },
1652 { "OnUnitActiveSec", config_parse_timer, &u->timer, "Timer" },
1653 { "OnUnitInactiveSec", config_parse_timer, &u->timer, "Timer" },
1654 { "Unit", config_parse_timer_unit, &u->timer, "Timer" },
1656 { "PathExists", config_parse_path_spec, &u->path, "Path" },
1657 { "PathChanged", config_parse_path_spec, &u->path, "Path" },
1658 { "DirectoryNotEmpty", config_parse_path_spec, &u->path, "Path" },
1659 { "Unit", config_parse_path_unit, &u->path, "Path" },
1661 /* The [Install] section is ignored here. */
1662 { "Alias", NULL, NULL, "Install" },
1663 { "WantedBy", NULL, NULL, "Install" },
1664 { "Also", NULL, NULL, "Install" },
1666 { NULL, NULL, NULL, NULL }
1669 #undef EXEC_CONTEXT_CONFIG_ITEMS
1671 const char *sections[4];
1675 char *filename = NULL, *id = NULL;
1680 /* Dirty dirty hack. */
1681 dump_items((FILE*) path, items);
1688 sections[0] = "Unit";
1689 sections[1] = section_table[u->meta.type];
1690 sections[2] = "Install";
1693 if (!(symlink_names = set_new(string_hash_func, string_compare_func)))
1696 if (path_is_absolute(path)) {
1698 if (!(filename = strdup(path))) {
1703 if ((r = open_follow(&filename, &f, symlink_names, &id)) < 0) {
1714 STRV_FOREACH(p, u->meta.manager->lookup_paths.unit_path) {
1716 /* Instead of opening the path right away, we manually
1717 * follow all symlinks and add their name to our unit
1718 * name set while doing so */
1719 if (!(filename = path_make_absolute(path, *p))) {
1724 if (u->meta.manager->unit_path_cache &&
1725 !set_get(u->meta.manager->unit_path_cache, filename))
1728 r = open_follow(&filename, &f, symlink_names, &id);
1739 /* Empty the symlink names for the next run */
1740 while ((sn = set_steal_first(symlink_names)))
1751 /* Hmm, no suitable file found? */
1757 if ((r = merge_by_names(&merged, symlink_names, id)) < 0)
1761 u->meta.load_state = UNIT_MERGED;
1767 if (fstat(fileno(f), &st) < 0) {
1772 /* Now, parse the file contents */
1773 if ((r = config_parse(filename, f, sections, items, false, u)) < 0)
1776 free(u->meta.fragment_path);
1777 u->meta.fragment_path = filename;
1780 u->meta.fragment_mtime = timespec_load(&st.st_mtim);
1782 u->meta.load_state = UNIT_LOADED;
1786 set_free_free(symlink_names);
1795 int unit_load_fragment(Unit *u) {
1801 assert(u->meta.load_state == UNIT_STUB);
1804 /* First, try to find the unit under its id. We always look
1805 * for unit files in the default directories, to make it easy
1806 * to override things by placing things in /etc/systemd/system */
1807 if ((r = load_from_path(u, u->meta.id)) < 0)
1810 /* Try to find an alias we can load this with */
1811 if (u->meta.load_state == UNIT_STUB)
1812 SET_FOREACH(t, u->meta.names, i) {
1814 if (t == u->meta.id)
1817 if ((r = load_from_path(u, t)) < 0)
1820 if (u->meta.load_state != UNIT_STUB)
1824 /* And now, try looking for it under the suggested (originally linked) path */
1825 if (u->meta.load_state == UNIT_STUB && u->meta.fragment_path)
1826 if ((r = load_from_path(u, u->meta.fragment_path)) < 0)
1829 /* Look for a template */
1830 if (u->meta.load_state == UNIT_STUB && u->meta.instance) {
1833 if (!(k = unit_name_template(u->meta.id)))
1836 r = load_from_path(u, k);
1842 if (u->meta.load_state == UNIT_STUB)
1843 SET_FOREACH(t, u->meta.names, i) {
1845 if (t == u->meta.id)
1848 if (!(k = unit_name_template(t)))
1851 r = load_from_path(u, k);
1857 if (u->meta.load_state != UNIT_STUB)
1865 void unit_dump_config_items(FILE *f) {
1866 /* OK, this wins a prize for extreme ugliness. */
1868 load_from_path(NULL, (const void*) f);