1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
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>
35 #include "conf-parser.h"
36 #include "load-fragment.h"
39 #include "securebits.h"
41 #include "unit-name.h"
42 #include "bus-errors.h"
44 #define COMMENTS "#;\n"
46 static int config_parse_deps(
55 UnitDependency d = PTR_TO_UINT(data);
65 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
69 if (!(t = strndup(w, l)))
72 k = unit_name_printf(u, t);
78 r = unit_add_dependency_by_name(u, d, k, NULL, true);
88 static int config_parse_names(
107 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
111 if (!(t = strndup(w, l)))
114 k = unit_name_printf(u, t);
120 r = unit_merge_by_name(u, k);
130 static int config_parse_string_printf(
131 const char *filename,
149 if (!(k = unit_full_printf(u, rvalue)))
163 static int config_parse_listen(
164 const char *filename,
183 if (!(p = new0(SocketPort, 1)))
186 if (streq(lvalue, "ListenFIFO")) {
187 p->type = SOCKET_FIFO;
189 if (!(p->path = strdup(rvalue))) {
194 path_kill_slashes(p->path);
196 p->type = SOCKET_SOCKET;
198 if ((r = socket_address_parse(&p->address, rvalue)) < 0) {
199 log_error("[%s:%u] Failed to parse address value: %s", filename, line, rvalue);
204 if (streq(lvalue, "ListenStream"))
205 p->address.type = SOCK_STREAM;
206 else if (streq(lvalue, "ListenDatagram"))
207 p->address.type = SOCK_DGRAM;
209 assert(streq(lvalue, "ListenSequentialPacket"));
210 p->address.type = SOCK_SEQPACKET;
213 if (socket_address_family(&p->address) != AF_LOCAL && p->address.type == SOCK_SEQPACKET) {
215 return -EPROTONOSUPPORT;
220 LIST_PREPEND(SocketPort, port, s->ports, p);
225 static int config_parse_socket_bind(
226 const char *filename,
235 SocketAddressBindIPv6Only b;
244 if ((b = socket_address_bind_ipv6_only_from_string(rvalue)) < 0) {
247 if ((r = parse_boolean(rvalue)) < 0) {
248 log_error("[%s:%u] Failed to parse bind IPv6 only value: %s", filename, line, rvalue);
252 s->bind_ipv6_only = r ? SOCKET_ADDRESS_IPV6_ONLY : SOCKET_ADDRESS_BOTH;
254 s->bind_ipv6_only = b;
259 static int config_parse_nice(
260 const char *filename,
268 ExecContext *c = data;
276 if ((r = safe_atoi(rvalue, &priority)) < 0) {
277 log_error("[%s:%u] Failed to parse nice priority: %s", filename, line, rvalue);
281 if (priority < PRIO_MIN || priority >= PRIO_MAX) {
282 log_error("[%s:%u] Nice priority out of range: %s", filename, line, rvalue);
292 static int config_parse_oom_adjust(
293 const char *filename,
301 ExecContext *c = data;
309 if ((r = safe_atoi(rvalue, &oa)) < 0) {
310 log_error("[%s:%u] Failed to parse OOM adjust value: %s", filename, line, rvalue);
314 if (oa < OOM_DISABLE || oa > OOM_ADJUST_MAX) {
315 log_error("[%s:%u] OOM adjust value out of range: %s", filename, line, rvalue);
320 c->oom_adjust_set = true;
325 static int config_parse_mode(
326 const char *filename,
344 l = strtol(rvalue, &x, 8);
345 if (!x || *x || errno) {
346 log_error("[%s:%u] Failed to parse mode value: %s", filename, line, rvalue);
347 return errno ? -errno : -EINVAL;
350 if (l < 0000 || l > 07777) {
351 log_error("[%s:%u] mode value out of range: %s", filename, line, rvalue);
359 static int config_parse_exec(
360 const char *filename,
368 ExecCommand **e = data, *nce;
377 /* We accept an absolute path as first argument, or
378 * alternatively an absolute prefixed with @ to allow
379 * overriding of argv[0]. */
385 bool honour_argv0 = false, ignore = false;
391 rvalue += strspn(rvalue, WHITESPACE);
396 if (rvalue[0] == '-') {
401 if (rvalue[0] == '@') {
406 if (*rvalue != '/') {
407 log_error("[%s:%u] Invalid executable path in command line: %s", filename, line, rvalue);
412 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
413 if (strncmp(w, ";", l) == 0)
419 if (!(n = new(char*, k + !honour_argv0)))
423 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
424 if (strncmp(w, ";", l) == 0)
427 if (honour_argv0 && w == rvalue) {
429 if (!(path = cunescape_length(w, l)))
432 if (!(n[k++] = cunescape_length(w, l)))
440 log_error("[%s:%u] Invalid command line: %s", filename, line, rvalue);
446 if (!(path = strdup(n[0])))
449 assert(path_is_absolute(path));
451 if (!(nce = new0(ExecCommand, 1)))
456 nce->ignore = ignore;
458 path_kill_slashes(nce->path);
460 exec_command_append_list(e, nce);
476 static int config_parse_usec(
477 const char *filename,
493 if ((r = parse_usec(rvalue, usec)) < 0) {
494 log_error("[%s:%u] Failed to parse time value: %s", filename, line, rvalue);
501 static DEFINE_CONFIG_PARSE_ENUM(config_parse_service_type, service_type, ServiceType, "Failed to parse service type");
502 static DEFINE_CONFIG_PARSE_ENUM(config_parse_service_restart, service_restart, ServiceRestart, "Failed to parse service restart specifier");
504 static int config_parse_bindtodevice(
505 const char *filename,
521 if (rvalue[0] && !streq(rvalue, "*")) {
522 if (!(n = strdup(rvalue)))
527 free(s->bind_to_device);
528 s->bind_to_device = n;
533 static DEFINE_CONFIG_PARSE_ENUM(config_parse_output, exec_output, ExecOutput, "Failed to parse output specifier");
534 static DEFINE_CONFIG_PARSE_ENUM(config_parse_input, exec_input, ExecInput, "Failed to parse input specifier");
536 static int config_parse_facility(
537 const char *filename,
553 if ((x = log_facility_from_string(rvalue)) < 0) {
554 log_error("[%s:%u] Failed to parse log facility: %s", filename, line, rvalue);
558 *o = LOG_MAKEPRI(x, LOG_PRI(*o));
563 static int config_parse_level(
564 const char *filename,
580 if ((x = log_level_from_string(rvalue)) < 0) {
581 log_error("[%s:%u] Failed to parse log level: %s", filename, line, rvalue);
585 *o = LOG_MAKEPRI(LOG_FAC(*o), x);
589 static int config_parse_io_class(
590 const char *filename,
598 ExecContext *c = data;
606 if ((x = ioprio_class_from_string(rvalue)) < 0) {
607 log_error("[%s:%u] Failed to parse IO scheduling class: %s", filename, line, rvalue);
611 c->ioprio = IOPRIO_PRIO_VALUE(x, IOPRIO_PRIO_DATA(c->ioprio));
612 c->ioprio_set = true;
617 static int config_parse_io_priority(
618 const char *filename,
626 ExecContext *c = data;
634 if (safe_atoi(rvalue, &i) < 0 || i < 0 || i >= IOPRIO_BE_NR) {
635 log_error("[%s:%u] Failed to parse io priority: %s", filename, line, rvalue);
639 c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_PRIO_CLASS(c->ioprio), i);
640 c->ioprio_set = true;
645 static int config_parse_cpu_sched_policy(
646 const char *filename,
655 ExecContext *c = data;
663 if ((x = sched_policy_from_string(rvalue)) < 0) {
664 log_error("[%s:%u] Failed to parse CPU scheduling policy: %s", filename, line, rvalue);
668 c->cpu_sched_policy = x;
669 c->cpu_sched_set = true;
674 static int config_parse_cpu_sched_prio(
675 const char *filename,
683 ExecContext *c = data;
691 /* On Linux RR/FIFO have the same range */
692 if (safe_atoi(rvalue, &i) < 0 || i < sched_get_priority_min(SCHED_RR) || i > sched_get_priority_max(SCHED_RR)) {
693 log_error("[%s:%u] Failed to parse CPU scheduling priority: %s", filename, line, rvalue);
697 c->cpu_sched_priority = i;
698 c->cpu_sched_set = true;
703 static int config_parse_cpu_affinity(
704 const char *filename,
712 ExecContext *c = data;
722 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
727 if (!(t = strndup(w, l)))
730 r = safe_atou(t, &cpu);
734 if (!(c->cpuset = cpu_set_malloc(&c->cpuset_ncpus)))
737 if (r < 0 || cpu >= c->cpuset_ncpus) {
738 log_error("[%s:%u] Failed to parse CPU affinity: %s", filename, line, rvalue);
742 CPU_SET_S(cpu, CPU_ALLOC_SIZE(c->cpuset_ncpus), c->cpuset);
748 static int config_parse_capabilities(
749 const char *filename,
757 ExecContext *c = data;
765 if (!(cap = cap_from_text(rvalue))) {
769 log_error("[%s:%u] Failed to parse capabilities: %s", filename, line, rvalue);
774 cap_free(c->capabilities);
775 c->capabilities = cap;
780 static int config_parse_secure_bits(
781 const char *filename,
789 ExecContext *c = data;
799 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
800 if (first_word(w, "keep-caps"))
801 c->secure_bits |= SECURE_KEEP_CAPS;
802 else if (first_word(w, "keep-caps-locked"))
803 c->secure_bits |= SECURE_KEEP_CAPS_LOCKED;
804 else if (first_word(w, "no-setuid-fixup"))
805 c->secure_bits |= SECURE_NO_SETUID_FIXUP;
806 else if (first_word(w, "no-setuid-fixup-locked"))
807 c->secure_bits |= SECURE_NO_SETUID_FIXUP_LOCKED;
808 else if (first_word(w, "noroot"))
809 c->secure_bits |= SECURE_NOROOT;
810 else if (first_word(w, "noroot-locked"))
811 c->secure_bits |= SECURE_NOROOT_LOCKED;
813 log_error("[%s:%u] Failed to parse secure bits: %s", filename, line, rvalue);
821 static int config_parse_bounding_set(
822 const char *filename,
830 ExecContext *c = data;
840 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
845 if (!(t = strndup(w, l)))
848 r = cap_from_name(t, &cap);
852 log_error("[%s:%u] Failed to parse capability bounding set: %s", filename, line, rvalue);
856 c->capability_bounding_set_drop |= 1 << cap;
862 static int config_parse_timer_slack_nsec(
863 const char *filename,
871 ExecContext *c = data;
880 if ((r = safe_atolu(rvalue, &u)) < 0) {
881 log_error("[%s:%u] Failed to parse time slack value: %s", filename, line, rvalue);
885 c->timer_slack_nsec = u;
890 static int config_parse_limit(
891 const char *filename,
899 struct rlimit **rl = data;
900 unsigned long long u;
908 if ((r = safe_atollu(rvalue, &u)) < 0) {
909 log_error("[%s:%u] Failed to parse resource value: %s", filename, line, rvalue);
914 if (!(*rl = new(struct rlimit, 1)))
917 (*rl)->rlim_cur = (*rl)->rlim_max = (rlim_t) u;
921 static int config_parse_cgroup(
922 const char *filename,
935 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
939 if (!(t = cunescape_length(w, l)))
942 r = unit_add_cgroup_from_text(u, t);
952 static int config_parse_sysv_priority(
953 const char *filename,
961 int *priority = data;
969 if ((r = safe_atoi(rvalue, &i)) < 0 || i < 0) {
970 log_error("[%s:%u] Failed to parse SysV start priority: %s", filename, line, rvalue);
978 static DEFINE_CONFIG_PARSE_ENUM(config_parse_kill_mode, kill_mode, KillMode, "Failed to parse kill mode");
980 static int config_parse_kill_signal(
981 const char *filename,
997 if ((r = signal_from_string(rvalue)) <= 0)
998 if (startswith(rvalue, "SIG"))
999 r = signal_from_string(rvalue+3);
1002 log_error("[%s:%u] Failed to parse kill signal: %s", filename, line, rvalue);
1010 static int config_parse_mount_flags(
1011 const char *filename,
1013 const char *section,
1019 ExecContext *c = data;
1023 unsigned long flags = 0;
1030 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
1031 if (strncmp(w, "shared", l) == 0)
1033 else if (strncmp(w, "slave", l) == 0)
1035 else if (strncmp(w, "private", l) == 0)
1036 flags |= MS_PRIVATE;
1038 log_error("[%s:%u] Failed to parse mount flags: %s", filename, line, rvalue);
1043 c->mount_flags = flags;
1047 static int config_parse_timer(
1048 const char *filename,
1050 const char *section,
1067 if ((b = timer_base_from_string(lvalue)) < 0) {
1068 log_error("[%s:%u] Failed to parse timer base: %s", filename, line, lvalue);
1072 if ((r = parse_usec(rvalue, &u)) < 0) {
1073 log_error("[%s:%u] Failed to parse timer value: %s", filename, line, rvalue);
1077 if (!(v = new0(TimerValue, 1)))
1083 LIST_PREPEND(TimerValue, value, t->values, v);
1088 static int config_parse_timer_unit(
1089 const char *filename,
1091 const char *section,
1106 dbus_error_init(&error);
1108 if (endswith(rvalue, ".timer")) {
1109 log_error("[%s:%u] Unit cannot be of type timer: %s", filename, line, rvalue);
1113 if ((r = manager_load_unit(t->meta.manager, rvalue, NULL, NULL, &t->unit)) < 0) {
1114 log_error("[%s:%u] Failed to load unit %s: %s", filename, line, rvalue, bus_error(&error, r));
1115 dbus_error_free(&error);
1122 static int config_parse_path_spec(
1123 const char *filename,
1125 const char *section,
1140 if ((b = path_type_from_string(lvalue)) < 0) {
1141 log_error("[%s:%u] Failed to parse path type: %s", filename, line, lvalue);
1145 if (!path_is_absolute(rvalue)) {
1146 log_error("[%s:%u] Path is not absolute: %s", filename, line, rvalue);
1150 if (!(s = new0(PathSpec, 1)))
1153 if (!(s->path = strdup(rvalue))) {
1158 path_kill_slashes(s->path);
1163 LIST_PREPEND(PathSpec, spec, p->specs, s);
1168 static int config_parse_path_unit(
1169 const char *filename,
1171 const char *section,
1186 dbus_error_init(&error);
1188 if (endswith(rvalue, ".path")) {
1189 log_error("[%s:%u] Unit cannot be of type path: %s", filename, line, rvalue);
1193 if ((r = manager_load_unit(t->meta.manager, rvalue, NULL, &error, &t->unit)) < 0) {
1194 log_error("[%s:%u] Failed to load unit %s: %s", filename, line, rvalue, bus_error(&error, r));
1195 dbus_error_free(&error);
1202 static int config_parse_env_file(
1203 const char *filename,
1205 const char *section,
1220 if (!(f = fopen(rvalue, "re"))) {
1221 log_error("[%s:%u] Failed to open environment file '%s': %m", filename, line, rvalue);
1226 char l[LINE_MAX], *p;
1229 if (!fgets(l, sizeof(l), f)) {
1234 log_error("[%s:%u] Failed to read environment file '%s': %m", filename, line, rvalue);
1243 if (strchr(COMMENTS, *p))
1246 t = strv_env_set(*env, p);
1260 static int config_parse_ip_tos(
1261 const char *filename,
1263 const char *section,
1269 int *ip_tos = data, x;
1277 if ((x = ip_tos_from_string(rvalue)) < 0)
1278 if ((r = safe_atoi(rvalue, &x)) < 0) {
1279 log_error("[%s:%u] Failed to parse IP TOS value: %s", filename, line, rvalue);
1287 static DEFINE_CONFIG_PARSE_ENUM(config_parse_notify_access, notify_access, NotifyAccess, "Failed to parse notify access specifier");
1289 #define FOLLOW_MAX 8
1291 static int open_follow(char **filename, FILE **_f, Set *names, char **_final) {
1302 /* This will update the filename pointer if the loaded file is
1303 * reached by a symlink. The old string will be freed. */
1306 char *target, *name;
1308 if (c++ >= FOLLOW_MAX)
1311 path_kill_slashes(*filename);
1313 /* Add the file name we are currently looking at to
1314 * the names of this unit */
1315 name = file_name_from_path(*filename);
1316 if (!(id = set_get(names, name))) {
1318 if (!(id = strdup(name)))
1321 if ((r = set_put(names, id)) < 0) {
1327 /* Try to open the file name, but don't if its a symlink */
1328 if ((fd = open(*filename, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW)) >= 0)
1334 /* Hmm, so this is a symlink. Let's read the name, and follow it manually */
1335 if ((r = readlink_and_make_absolute(*filename, &target)) < 0)
1342 if (!(f = fdopen(fd, "r"))) {
1344 close_nointr_nofail(fd);
1353 static int merge_by_names(Unit **u, Set *names, const char *id) {
1361 /* Let's try to add in all symlink names we found */
1362 while ((k = set_steal_first(names))) {
1364 /* First try to merge in the other name into our
1366 if ((r = unit_merge_by_name(*u, k)) < 0) {
1369 /* Hmm, we couldn't merge the other unit into
1370 * ours? Then let's try it the other way
1373 other = manager_get_unit((*u)->meta.manager, k);
1377 if ((r = unit_merge(other, *u)) >= 0) {
1379 return merge_by_names(u, names, NULL);
1386 unit_choose_id(*u, id);
1394 static void dump_items(FILE *f, const ConfigItem *items) {
1395 const ConfigItem *i;
1396 const char *prev_section = NULL;
1397 bool not_first = false;
1400 ConfigParserCallback callback;
1403 { config_parse_int, "INTEGER" },
1404 { config_parse_unsigned, "UNSIGNED" },
1405 { config_parse_size, "SIZE" },
1406 { config_parse_bool, "BOOLEAN" },
1407 { config_parse_string, "STRING" },
1408 { config_parse_path, "PATH" },
1409 { config_parse_strv, "STRING [...]" },
1410 { config_parse_nice, "NICE" },
1411 { config_parse_oom_adjust, "OOMADJUST" },
1412 { config_parse_io_class, "IOCLASS" },
1413 { config_parse_io_priority, "IOPRIORITY" },
1414 { config_parse_cpu_sched_policy, "CPUSCHEDPOLICY" },
1415 { config_parse_cpu_sched_prio, "CPUSCHEDPRIO" },
1416 { config_parse_cpu_affinity, "CPUAFFINITY" },
1417 { config_parse_mode, "MODE" },
1418 { config_parse_env_file, "FILE" },
1419 { config_parse_output, "OUTPUT" },
1420 { config_parse_input, "INPUT" },
1421 { config_parse_facility, "FACILITY" },
1422 { config_parse_level, "LEVEL" },
1423 { config_parse_capabilities, "CAPABILITIES" },
1424 { config_parse_secure_bits, "SECUREBITS" },
1425 { config_parse_bounding_set, "BOUNDINGSET" },
1426 { config_parse_timer_slack_nsec, "TIMERSLACK" },
1427 { config_parse_limit, "LIMIT" },
1428 { config_parse_cgroup, "CGROUP [...]" },
1429 { config_parse_deps, "UNIT [...]" },
1430 { config_parse_names, "UNIT [...]" },
1431 { config_parse_exec, "PATH [ARGUMENT [...]]" },
1432 { config_parse_service_type, "SERVICETYPE" },
1433 { config_parse_service_restart, "SERVICERESTART" },
1434 { config_parse_sysv_priority, "SYSVPRIORITY" },
1435 { config_parse_kill_mode, "KILLMODE" },
1436 { config_parse_kill_signal, "SIGNAL" },
1437 { config_parse_listen, "SOCKET [...]" },
1438 { config_parse_socket_bind, "SOCKETBIND" },
1439 { config_parse_bindtodevice, "NETWORKINTERFACE" },
1440 { config_parse_usec, "SECONDS" },
1441 { config_parse_path_strv, "PATH [...]" },
1442 { config_parse_mount_flags, "MOUNTFLAG [...]" },
1443 { config_parse_string_printf, "STRING" },
1444 { config_parse_timer, "TIMER" },
1445 { config_parse_timer_unit, "NAME" },
1446 { config_parse_path_spec, "PATH" },
1447 { config_parse_path_unit, "UNIT" },
1448 { config_parse_notify_access, "ACCESS" },
1449 { config_parse_ip_tos, "TOS" },
1455 for (i = items; i->lvalue; i++) {
1457 const char *rvalue = "OTHER";
1459 if (!streq_ptr(i->section, prev_section)) {
1465 fprintf(f, "[%s]\n", i->section);
1466 prev_section = i->section;
1469 for (j = 0; j < ELEMENTSOF(table); j++)
1470 if (i->parse == table[j].callback) {
1471 rvalue = table[j].rvalue;
1475 fprintf(f, "%s=%s\n", i->lvalue, rvalue);
1479 static int load_from_path(Unit *u, const char *path) {
1481 static const char* const section_table[_UNIT_TYPE_MAX] = {
1482 [UNIT_SERVICE] = "Service",
1483 [UNIT_TIMER] = "Timer",
1484 [UNIT_SOCKET] = "Socket",
1485 [UNIT_TARGET] = "Target",
1486 [UNIT_DEVICE] = "Device",
1487 [UNIT_MOUNT] = "Mount",
1488 [UNIT_AUTOMOUNT] = "Automount",
1489 [UNIT_SNAPSHOT] = "Snapshot",
1490 [UNIT_SWAP] = "Swap",
1491 [UNIT_PATH] = "Path"
1494 #define EXEC_CONTEXT_CONFIG_ITEMS(context, section) \
1495 { "WorkingDirectory", config_parse_path, &(context).working_directory, section }, \
1496 { "RootDirectory", config_parse_path, &(context).root_directory, section }, \
1497 { "User", config_parse_string_printf, &(context).user, section }, \
1498 { "Group", config_parse_string_printf, &(context).group, section }, \
1499 { "SupplementaryGroups", config_parse_strv, &(context).supplementary_groups, section }, \
1500 { "Nice", config_parse_nice, &(context), section }, \
1501 { "OOMAdjust", config_parse_oom_adjust, &(context), section }, \
1502 { "IOSchedulingClass", config_parse_io_class, &(context), section }, \
1503 { "IOSchedulingPriority", config_parse_io_priority, &(context), section }, \
1504 { "CPUSchedulingPolicy", config_parse_cpu_sched_policy,&(context), section }, \
1505 { "CPUSchedulingPriority", config_parse_cpu_sched_prio, &(context), section }, \
1506 { "CPUSchedulingResetOnFork", config_parse_bool, &(context).cpu_sched_reset_on_fork, section }, \
1507 { "CPUAffinity", config_parse_cpu_affinity, &(context), section }, \
1508 { "UMask", config_parse_mode, &(context).umask, section }, \
1509 { "Environment", config_parse_strv, &(context).environment, section }, \
1510 { "EnvironmentFile", config_parse_env_file, &(context).environment, section }, \
1511 { "StandardInput", config_parse_input, &(context).std_input, section }, \
1512 { "StandardOutput", config_parse_output, &(context).std_output, section }, \
1513 { "StandardError", config_parse_output, &(context).std_error, section }, \
1514 { "TTYPath", config_parse_path, &(context).tty_path, section }, \
1515 { "SyslogIdentifier", config_parse_string_printf, &(context).syslog_identifier, section }, \
1516 { "SyslogFacility", config_parse_facility, &(context).syslog_priority, section }, \
1517 { "SyslogLevel", config_parse_level, &(context).syslog_priority, section }, \
1518 { "SyslogLevelPrefix", config_parse_bool, &(context).syslog_level_prefix, section }, \
1519 { "Capabilities", config_parse_capabilities, &(context), section }, \
1520 { "SecureBits", config_parse_secure_bits, &(context), section }, \
1521 { "CapabilityBoundingSetDrop", config_parse_bounding_set, &(context), section }, \
1522 { "TimerSlackNSec", config_parse_timer_slack_nsec,&(context), section }, \
1523 { "LimitCPU", config_parse_limit, &(context).rlimit[RLIMIT_CPU], section }, \
1524 { "LimitFSIZE", config_parse_limit, &(context).rlimit[RLIMIT_FSIZE], section }, \
1525 { "LimitDATA", config_parse_limit, &(context).rlimit[RLIMIT_DATA], section }, \
1526 { "LimitSTACK", config_parse_limit, &(context).rlimit[RLIMIT_STACK], section }, \
1527 { "LimitCORE", config_parse_limit, &(context).rlimit[RLIMIT_CORE], section }, \
1528 { "LimitRSS", config_parse_limit, &(context).rlimit[RLIMIT_RSS], section }, \
1529 { "LimitNOFILE", config_parse_limit, &(context).rlimit[RLIMIT_NOFILE], section }, \
1530 { "LimitAS", config_parse_limit, &(context).rlimit[RLIMIT_AS], section }, \
1531 { "LimitNPROC", config_parse_limit, &(context).rlimit[RLIMIT_NPROC], section }, \
1532 { "LimitMEMLOCK", config_parse_limit, &(context).rlimit[RLIMIT_MEMLOCK], section }, \
1533 { "LimitLOCKS", config_parse_limit, &(context).rlimit[RLIMIT_LOCKS], section }, \
1534 { "LimitSIGPENDING", config_parse_limit, &(context).rlimit[RLIMIT_SIGPENDING], section }, \
1535 { "LimitMSGQUEUE", config_parse_limit, &(context).rlimit[RLIMIT_MSGQUEUE], section }, \
1536 { "LimitNICE", config_parse_limit, &(context).rlimit[RLIMIT_NICE], section }, \
1537 { "LimitRTPRIO", config_parse_limit, &(context).rlimit[RLIMIT_RTPRIO], section }, \
1538 { "LimitRTTIME", config_parse_limit, &(context).rlimit[RLIMIT_RTTIME], section }, \
1539 { "ControlGroup", config_parse_cgroup, u, section }, \
1540 { "ReadWriteDirectories", config_parse_path_strv, &(context).read_write_dirs, section }, \
1541 { "ReadOnlyDirectories", config_parse_path_strv, &(context).read_only_dirs, section }, \
1542 { "InaccessibleDirectories",config_parse_path_strv, &(context).inaccessible_dirs, section }, \
1543 { "PrivateTmp", config_parse_bool, &(context).private_tmp, section }, \
1544 { "MountFlags", config_parse_mount_flags, &(context), section }, \
1545 { "TCPWrapName", config_parse_string_printf, &(context).tcpwrap_name, section }, \
1546 { "PAMName", config_parse_string_printf, &(context).pam_name, section }, \
1547 { "KillMode", config_parse_kill_mode, &(context).kill_mode, section }, \
1548 { "KillSignal", config_parse_kill_signal, &(context).kill_signal, section }
1550 const ConfigItem items[] = {
1551 { "Names", config_parse_names, u, "Unit" },
1552 { "Description", config_parse_string_printf, &u->meta.description, "Unit" },
1553 { "Requires", config_parse_deps, UINT_TO_PTR(UNIT_REQUIRES), "Unit" },
1554 { "RequiresOverridable", config_parse_deps, UINT_TO_PTR(UNIT_REQUIRES_OVERRIDABLE), "Unit" },
1555 { "Requisite", config_parse_deps, UINT_TO_PTR(UNIT_REQUISITE), "Unit" },
1556 { "RequisiteOverridable", config_parse_deps, UINT_TO_PTR(UNIT_REQUISITE_OVERRIDABLE), "Unit" },
1557 { "Wants", config_parse_deps, UINT_TO_PTR(UNIT_WANTS), "Unit" },
1558 { "Conflicts", config_parse_deps, UINT_TO_PTR(UNIT_CONFLICTS), "Unit" },
1559 { "Before", config_parse_deps, UINT_TO_PTR(UNIT_BEFORE), "Unit" },
1560 { "After", config_parse_deps, UINT_TO_PTR(UNIT_AFTER), "Unit" },
1561 { "RecursiveStop", config_parse_bool, &u->meta.recursive_stop, "Unit" },
1562 { "StopWhenUnneeded", config_parse_bool, &u->meta.stop_when_unneeded, "Unit" },
1563 { "OnlyByDependency", config_parse_bool, &u->meta.only_by_dependency, "Unit" },
1564 { "DefaultDependencies", config_parse_bool, &u->meta.default_dependencies, "Unit" },
1565 { "IgnoreDependencyFailure",config_parse_bool, &u->meta.ignore_dependency_failure, "Unit" },
1567 { "PIDFile", config_parse_path, &u->service.pid_file, "Service" },
1568 { "ExecStartPre", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START_PRE, "Service" },
1569 { "ExecStart", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START, "Service" },
1570 { "ExecStartPost", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START_POST, "Service" },
1571 { "ExecReload", config_parse_exec, u->service.exec_command+SERVICE_EXEC_RELOAD, "Service" },
1572 { "ExecStop", config_parse_exec, u->service.exec_command+SERVICE_EXEC_STOP, "Service" },
1573 { "ExecStopPost", config_parse_exec, u->service.exec_command+SERVICE_EXEC_STOP_POST, "Service" },
1574 { "RestartSec", config_parse_usec, &u->service.restart_usec, "Service" },
1575 { "TimeoutSec", config_parse_usec, &u->service.timeout_usec, "Service" },
1576 { "Type", config_parse_service_type, &u->service.type, "Service" },
1577 { "Restart", config_parse_service_restart, &u->service.restart, "Service" },
1578 { "PermissionsStartOnly", config_parse_bool, &u->service.permissions_start_only, "Service" },
1579 { "RootDirectoryStartOnly", config_parse_bool, &u->service.root_directory_start_only, "Service" },
1580 { "ValidNoProcess", config_parse_bool, &u->service.valid_no_process, "Service" },
1581 { "SysVStartPriority", config_parse_sysv_priority, &u->service.sysv_start_priority, "Service" },
1582 { "NonBlocking", config_parse_bool, &u->service.exec_context.non_blocking, "Service" },
1583 { "BusName", config_parse_string_printf, &u->service.bus_name, "Service" },
1584 { "NotifyAccess", config_parse_notify_access, &u->service.notify_access, "Service" },
1585 EXEC_CONTEXT_CONFIG_ITEMS(u->service.exec_context, "Service"),
1587 { "ListenStream", config_parse_listen, &u->socket, "Socket" },
1588 { "ListenDatagram", config_parse_listen, &u->socket, "Socket" },
1589 { "ListenSequentialPacket", config_parse_listen, &u->socket, "Socket" },
1590 { "ListenFIFO", config_parse_listen, &u->socket, "Socket" },
1591 { "BindIPv6Only", config_parse_socket_bind, &u->socket, "Socket" },
1592 { "Backlog", config_parse_unsigned, &u->socket.backlog, "Socket" },
1593 { "BindToDevice", config_parse_bindtodevice, &u->socket, "Socket" },
1594 { "ExecStartPre", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_START_PRE, "Socket" },
1595 { "ExecStartPost", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_START_POST, "Socket" },
1596 { "ExecStopPre", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_STOP_PRE, "Socket" },
1597 { "ExecStopPost", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_STOP_POST, "Socket" },
1598 { "TimeoutSec", config_parse_usec, &u->socket.timeout_usec, "Socket" },
1599 { "DirectoryMode", config_parse_mode, &u->socket.directory_mode, "Socket" },
1600 { "SocketMode", config_parse_mode, &u->socket.socket_mode, "Socket" },
1601 { "Accept", config_parse_bool, &u->socket.accept, "Socket" },
1602 { "MaxConnections", config_parse_unsigned, &u->socket.max_connections, "Socket" },
1603 { "KeepAlive", config_parse_bool, &u->socket.keep_alive, "Socket" },
1604 { "Priority", config_parse_int, &u->socket.priority, "Socket" },
1605 { "ReceiveBuffer", config_parse_size, &u->socket.receive_buffer, "Socket" },
1606 { "SendBuffer", config_parse_size, &u->socket.send_buffer, "Socket" },
1607 { "IPTOS", config_parse_ip_tos, &u->socket.ip_tos, "Socket" },
1608 { "IPTTL", config_parse_int, &u->socket.ip_ttl, "Socket" },
1609 { "Mark", config_parse_int, &u->socket.mark, "Socket" },
1610 { "PipeSize", config_parse_size, &u->socket.pipe_size, "Socket" },
1611 { "FreeBind", config_parse_bool, &u->socket.free_bind, "Socket" },
1612 EXEC_CONTEXT_CONFIG_ITEMS(u->socket.exec_context, "Socket"),
1614 { "What", config_parse_string, &u->mount.parameters_fragment.what, "Mount" },
1615 { "Where", config_parse_path, &u->mount.where, "Mount" },
1616 { "Options", config_parse_string, &u->mount.parameters_fragment.options, "Mount" },
1617 { "Type", config_parse_string, &u->mount.parameters_fragment.fstype, "Mount" },
1618 { "TimeoutSec", config_parse_usec, &u->mount.timeout_usec, "Mount" },
1619 { "DirectoryMode", config_parse_mode, &u->mount.directory_mode, "Mount" },
1620 EXEC_CONTEXT_CONFIG_ITEMS(u->mount.exec_context, "Mount"),
1622 { "Where", config_parse_path, &u->automount.where, "Automount" },
1623 { "DirectoryMode", config_parse_mode, &u->automount.directory_mode, "Automount" },
1625 { "What", config_parse_path, &u->swap.parameters_fragment.what, "Swap" },
1626 { "Priority", config_parse_int, &u->swap.parameters_fragment.priority, "Swap" },
1628 { "OnActiveSec", config_parse_timer, &u->timer, "Timer" },
1629 { "OnBootSec", config_parse_timer, &u->timer, "Timer" },
1630 { "OnStartupSec", config_parse_timer, &u->timer, "Timer" },
1631 { "OnUnitActiveSec", config_parse_timer, &u->timer, "Timer" },
1632 { "OnUnitInactiveSec", config_parse_timer, &u->timer, "Timer" },
1633 { "Unit", config_parse_timer_unit, &u->timer, "Timer" },
1635 { "PathExists", config_parse_path_spec, &u->path, "Path" },
1636 { "PathChanged", config_parse_path_spec, &u->path, "Path" },
1637 { "DirectoryNotEmpty", config_parse_path_spec, &u->path, "Path" },
1638 { "Unit", config_parse_path_unit, &u->path, "Path" },
1640 /* The [Install] section is ignored here. */
1641 { "Alias", NULL, NULL, "Install" },
1642 { "WantedBy", NULL, NULL, "Install" },
1643 { "Also", NULL, NULL, "Install" },
1645 { NULL, NULL, NULL, NULL }
1648 #undef EXEC_CONTEXT_CONFIG_ITEMS
1650 const char *sections[4];
1654 char *filename = NULL, *id = NULL;
1658 /* Dirty dirty hack. */
1659 dump_items((FILE*) path, items);
1666 sections[0] = "Unit";
1667 sections[1] = section_table[u->meta.type];
1668 sections[2] = "Install";
1671 if (!(symlink_names = set_new(string_hash_func, string_compare_func)))
1674 if (path_is_absolute(path)) {
1676 if (!(filename = strdup(path))) {
1681 if ((r = open_follow(&filename, &f, symlink_names, &id)) < 0) {
1692 STRV_FOREACH(p, u->meta.manager->lookup_paths.unit_path) {
1694 /* Instead of opening the path right away, we manually
1695 * follow all symlinks and add their name to our unit
1696 * name set while doing so */
1697 if (!(filename = path_make_absolute(path, *p))) {
1702 if (u->meta.manager->unit_path_cache &&
1703 !set_get(u->meta.manager->unit_path_cache, filename))
1706 r = open_follow(&filename, &f, symlink_names, &id);
1717 /* Empty the symlink names for the next run */
1718 while ((sn = set_steal_first(symlink_names)))
1734 if ((r = merge_by_names(&merged, symlink_names, id)) < 0)
1738 u->meta.load_state = UNIT_MERGED;
1743 /* Now, parse the file contents */
1744 if ((r = config_parse(filename, f, sections, items, false, u)) < 0)
1747 free(u->meta.fragment_path);
1748 u->meta.fragment_path = filename;
1751 u->meta.load_state = UNIT_LOADED;
1755 set_free_free(symlink_names);
1764 int unit_load_fragment(Unit *u) {
1769 if (u->meta.fragment_path) {
1771 if ((r = load_from_path(u, u->meta.fragment_path)) < 0)
1778 /* Try to find the unit under its id */
1779 if ((r = load_from_path(u, u->meta.id)) < 0)
1782 /* Try to find an alias we can load this with */
1783 if (u->meta.load_state == UNIT_STUB)
1784 SET_FOREACH(t, u->meta.names, i) {
1786 if (t == u->meta.id)
1789 if ((r = load_from_path(u, t)) < 0)
1792 if (u->meta.load_state != UNIT_STUB)
1796 /* Now, follow the same logic, but look for a template */
1797 if (u->meta.load_state == UNIT_STUB && u->meta.instance) {
1800 if (!(k = unit_name_template(u->meta.id)))
1803 r = load_from_path(u, k);
1809 if (u->meta.load_state == UNIT_STUB)
1810 SET_FOREACH(t, u->meta.names, i) {
1812 if (t == u->meta.id)
1815 if (!(k = unit_name_template(t)))
1818 r = load_from_path(u, k);
1824 if (u->meta.load_state != UNIT_STUB)
1833 void unit_dump_config_items(FILE *f) {
1834 /* OK, this wins a prize for extreme ugliness. */
1836 load_from_path(NULL, (const void*) f);