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"
43 #define DEFINE_CONFIG_PARSE_ENUM(function,name,type,msg) \
44 static int function( \
45 const char *filename, \
47 const char *section, \
60 if ((x = name##_from_string(rvalue)) < 0) { \
61 log_error("[%s:%u] " msg ": %s", filename, line, rvalue); \
70 static int config_parse_deps(
79 UnitDependency d = PTR_TO_UINT(data);
89 FOREACH_WORD(w, l, rvalue, state) {
93 if (!(t = strndup(w, l)))
96 k = unit_name_printf(u, t);
102 r = unit_add_dependency_by_name(u, d, k, NULL, true);
112 static int config_parse_names(
113 const char *filename,
131 FOREACH_WORD(w, l, rvalue, state) {
135 if (!(t = strndup(w, l)))
138 k = unit_name_printf(u, t);
144 r = unit_merge_by_name(u, k);
154 static int config_parse_description(
155 const char *filename,
171 if (!(k = unit_full_printf(u, rvalue)))
174 free(u->meta.description);
177 u->meta.description = k;
180 u->meta.description = NULL;
186 static int config_parse_listen(
187 const char *filename,
206 if (!(p = new0(SocketPort, 1)))
209 if (streq(lvalue, "ListenFIFO")) {
210 p->type = SOCKET_FIFO;
212 if (!(p->path = strdup(rvalue))) {
217 p->type = SOCKET_SOCKET;
219 if ((r = socket_address_parse(&p->address, rvalue)) < 0) {
220 log_error("[%s:%u] Failed to parse address value: %s", filename, line, rvalue);
225 if (streq(lvalue, "ListenStream"))
226 p->address.type = SOCK_STREAM;
227 else if (streq(lvalue, "ListenDatagram"))
228 p->address.type = SOCK_DGRAM;
230 assert(streq(lvalue, "ListenSequentialPacket"));
231 p->address.type = SOCK_SEQPACKET;
234 if (socket_address_family(&p->address) != AF_LOCAL && p->address.type == SOCK_SEQPACKET) {
236 return -EPROTONOSUPPORT;
241 LIST_PREPEND(SocketPort, port, s->ports, p);
246 static int config_parse_socket_bind(
247 const char *filename,
256 SocketAddressBindIPv6Only b;
265 if ((b = socket_address_bind_ipv6_only_from_string(rvalue)) < 0) {
268 if ((r = parse_boolean(rvalue)) < 0) {
269 log_error("[%s:%u] Failed to parse bind IPv6 only value: %s", filename, line, rvalue);
273 s->bind_ipv6_only = r ? SOCKET_ADDRESS_IPV6_ONLY : SOCKET_ADDRESS_BOTH;
275 s->bind_ipv6_only = b;
280 static int config_parse_nice(
281 const char *filename,
289 ExecContext *c = data;
297 if ((r = safe_atoi(rvalue, &priority)) < 0) {
298 log_error("[%s:%u] Failed to parse nice priority: %s", filename, line, rvalue);
302 if (priority < PRIO_MIN || priority >= PRIO_MAX) {
303 log_error("[%s:%u] Nice priority out of range: %s", filename, line, rvalue);
313 static int config_parse_oom_adjust(
314 const char *filename,
322 ExecContext *c = data;
330 if ((r = safe_atoi(rvalue, &oa)) < 0) {
331 log_error("[%s:%u] Failed to parse OOM adjust value: %s", filename, line, rvalue);
335 if (oa < OOM_DISABLE || oa > OOM_ADJUST_MAX) {
336 log_error("[%s:%u] OOM adjust value out of range: %s", filename, line, rvalue);
341 c->oom_adjust_set = true;
346 static int config_parse_mode(
347 const char *filename,
365 l = strtol(rvalue, &x, 8);
366 if (!x || *x || errno) {
367 log_error("[%s:%u] Failed to parse mode value: %s", filename, line, rvalue);
368 return errno ? -errno : -EINVAL;
371 if (l < 0000 || l > 07777) {
372 log_error("[%s:%u] mode value out of range: %s", filename, line, rvalue);
380 static int config_parse_exec(
381 const char *filename,
389 ExecCommand **e = data, *nce = NULL;
394 char *state, *path = NULL;
395 bool honour_argv0, write_to_path;
402 /* We accept an absolute path as first argument, or
403 * alternatively an absolute prefixed with @ to allow
404 * overriding of argv[0]. */
406 honour_argv0 = rvalue[0] == '@';
408 if (rvalue[honour_argv0 ? 1 : 0] != '/') {
409 log_error("[%s:%u] Invalid executable path in command line: %s", filename, line, rvalue);
414 FOREACH_WORD_QUOTED(w, l, rvalue, state)
417 if (!(n = new(char*, k + (honour_argv0 ? 0 : 1))))
421 write_to_path = honour_argv0;
422 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
424 if (!(path = strndup(w+1, l-1)))
426 write_to_path = false;
428 if (!(n[k++] = strndup(w, l)))
436 log_error("[%s:%u] Invalid command line: %s", filename, line, rvalue);
442 if (!(path = strdup(n[0])))
445 assert(path_is_absolute(path));
447 if (!(nce = new0(ExecCommand, 1)))
453 exec_command_append_list(e, nce);
466 static int config_parse_usec(
467 const char *filename,
483 if ((r = parse_usec(rvalue, usec)) < 0) {
484 log_error("[%s:%u] Failed to parse time value: %s", filename, line, rvalue);
491 DEFINE_CONFIG_PARSE_ENUM(config_parse_service_type, service_type, ServiceType, "Failed to parse service type");
492 DEFINE_CONFIG_PARSE_ENUM(config_parse_service_restart, service_restart, ServiceRestart, "Failed to parse service restart specifier");
494 static int config_parse_bindtodevice(
495 const char *filename,
511 if (rvalue[0] && !streq(rvalue, "*")) {
512 if (!(n = strdup(rvalue)))
517 free(s->bind_to_device);
518 s->bind_to_device = n;
523 DEFINE_CONFIG_PARSE_ENUM(config_parse_output, exec_output, ExecOutput, "Failed to parse output specifier");
524 DEFINE_CONFIG_PARSE_ENUM(config_parse_input, exec_input, ExecInput, "Failed to parse input specifier");
526 static int config_parse_facility(
527 const char *filename,
543 if ((x = log_facility_from_string(rvalue)) < 0) {
544 log_error("[%s:%u] Failed to parse log facility: %s", filename, line, rvalue);
548 *o = LOG_MAKEPRI(x, LOG_PRI(*o));
553 static int config_parse_level(
554 const char *filename,
570 if ((x = log_level_from_string(rvalue)) < 0) {
571 log_error("[%s:%u] Failed to parse log level: %s", filename, line, rvalue);
575 *o = LOG_MAKEPRI(LOG_FAC(*o), x);
579 static int config_parse_io_class(
580 const char *filename,
588 ExecContext *c = data;
596 if ((x = ioprio_class_from_string(rvalue)) < 0) {
597 log_error("[%s:%u] Failed to parse IO scheduling class: %s", filename, line, rvalue);
601 c->ioprio = IOPRIO_PRIO_VALUE(x, IOPRIO_PRIO_DATA(c->ioprio));
602 c->ioprio_set = true;
607 static int config_parse_io_priority(
608 const char *filename,
616 ExecContext *c = data;
624 if (safe_atoi(rvalue, &i) < 0 || i < 0 || i >= IOPRIO_BE_NR) {
625 log_error("[%s:%u] Failed to parse io priority: %s", filename, line, rvalue);
629 c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_PRIO_CLASS(c->ioprio), i);
630 c->ioprio_set = true;
635 static int config_parse_cpu_sched_policy(
636 const char *filename,
645 ExecContext *c = data;
653 if ((x = sched_policy_from_string(rvalue)) < 0) {
654 log_error("[%s:%u] Failed to parse CPU scheduling policy: %s", filename, line, rvalue);
658 c->cpu_sched_policy = x;
659 c->cpu_sched_set = true;
664 static int config_parse_cpu_sched_prio(
665 const char *filename,
673 ExecContext *c = data;
681 /* On Linux RR/FIFO have the same range */
682 if (safe_atoi(rvalue, &i) < 0 || i < sched_get_priority_min(SCHED_RR) || i > sched_get_priority_max(SCHED_RR)) {
683 log_error("[%s:%u] Failed to parse CPU scheduling priority: %s", filename, line, rvalue);
687 c->cpu_sched_priority = i;
688 c->cpu_sched_set = true;
693 static int config_parse_cpu_affinity(
694 const char *filename,
702 ExecContext *c = data;
712 FOREACH_WORD(w, l, rvalue, state) {
717 if (!(t = strndup(w, l)))
720 r = safe_atou(t, &cpu);
723 if (r < 0 || cpu >= CPU_SETSIZE) {
724 log_error("[%s:%u] Failed to parse CPU affinity: %s", filename, line, rvalue);
728 CPU_SET(cpu, &c->cpu_affinity);
731 c->cpu_affinity_set = true;
736 static int config_parse_capabilities(
737 const char *filename,
745 ExecContext *c = data;
753 if (!(cap = cap_from_text(rvalue))) {
757 log_error("[%s:%u] Failed to parse capabilities: %s", filename, line, rvalue);
762 cap_free(c->capabilities);
763 c->capabilities = cap;
768 static int config_parse_secure_bits(
769 const char *filename,
777 ExecContext *c = data;
787 FOREACH_WORD(w, l, rvalue, state) {
788 if (first_word(w, "keep-caps"))
789 c->secure_bits |= SECURE_KEEP_CAPS;
790 else if (first_word(w, "keep-caps-locked"))
791 c->secure_bits |= SECURE_KEEP_CAPS_LOCKED;
792 else if (first_word(w, "no-setuid-fixup"))
793 c->secure_bits |= SECURE_NO_SETUID_FIXUP;
794 else if (first_word(w, "no-setuid-fixup-locked"))
795 c->secure_bits |= SECURE_NO_SETUID_FIXUP_LOCKED;
796 else if (first_word(w, "noroot"))
797 c->secure_bits |= SECURE_NOROOT;
798 else if (first_word(w, "noroot-locked"))
799 c->secure_bits |= SECURE_NOROOT_LOCKED;
801 log_error("[%s:%u] Failed to parse secure bits: %s", filename, line, rvalue);
809 static int config_parse_bounding_set(
810 const char *filename,
818 ExecContext *c = data;
828 FOREACH_WORD(w, l, rvalue, state) {
833 if (!(t = strndup(w, l)))
836 r = cap_from_name(t, &cap);
840 log_error("[%s:%u] Failed to parse capability bounding set: %s", filename, line, rvalue);
844 c->capability_bounding_set_drop |= 1 << cap;
850 static int config_parse_timer_slack_ns(
851 const char *filename,
859 ExecContext *c = data;
868 if ((r = safe_atolu(rvalue, &u)) < 0) {
869 log_error("[%s:%u] Failed to parse time slack value: %s", filename, line, rvalue);
873 c->timer_slack_ns = u;
878 static int config_parse_limit(
879 const char *filename,
887 struct rlimit **rl = data;
888 unsigned long long u;
896 if ((r = safe_atollu(rvalue, &u)) < 0) {
897 log_error("[%s:%u] Failed to parse resource value: %s", filename, line, rvalue);
902 if (!(*rl = new(struct rlimit, 1)))
905 (*rl)->rlim_cur = (*rl)->rlim_max = (rlim_t) u;
909 static int config_parse_cgroup(
910 const char *filename,
923 FOREACH_WORD(w, l, rvalue, state) {
927 if (!(t = strndup(w, l)))
930 r = unit_add_cgroup_from_text(u, t);
940 static int config_parse_sysv_priority(
941 const char *filename,
949 int *priority = data;
957 if ((r = safe_atoi(rvalue, &i)) < 0 || i < 0) {
958 log_error("[%s:%u] Failed to parse SysV start priority: %s", filename, line, rvalue);
966 DEFINE_CONFIG_PARSE_ENUM(config_parse_kill_mode, kill_mode, KillMode, "Failed to parse kill mode");
968 static int config_parse_mount_flags(
969 const char *filename,
977 ExecContext *c = data;
981 unsigned long flags = 0;
988 FOREACH_WORD(w, l, rvalue, state) {
989 if (strncmp(w, "shared", l) == 0)
991 else if (strncmp(w, "slave", l) == 0)
993 else if (strncmp(w, "private", l) == 0)
996 log_error("[%s:%u] Failed to parse mount flags: %s", filename, line, rvalue);
1001 c->mount_flags = flags;
1005 #define FOLLOW_MAX 8
1007 static int open_follow(char **filename, FILE **_f, Set *names, char **_final) {
1018 /* This will update the filename pointer if the loaded file is
1019 * reached by a symlink. The old string will be freed. */
1022 char *target, *k, *name;
1024 if (c++ >= FOLLOW_MAX)
1027 path_kill_slashes(*filename);
1029 /* Add the file name we are currently looking at to
1030 * the names of this unit */
1031 name = file_name_from_path(*filename);
1032 if (!(id = set_get(names, name))) {
1034 if (!(id = strdup(name)))
1037 if ((r = set_put(names, id)) < 0) {
1043 /* Try to open the file name, but don't if its a symlink */
1044 if ((fd = open(*filename, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW)) >= 0)
1050 /* Hmm, so this is a symlink. Let's read the name, and follow it manually */
1051 if ((r = readlink_malloc(*filename, &target)) < 0)
1054 k = file_in_same_dir(*filename, target);
1064 if (!(f = fdopen(fd, "r"))) {
1066 close_nointr_nofail(fd);
1075 static int merge_by_names(Unit **u, Set *names, const char *id) {
1083 /* Let's try to add in all symlink names we found */
1084 while ((k = set_steal_first(names))) {
1086 /* First try to merge in the other name into our
1088 if ((r = unit_merge_by_name(*u, k)) < 0) {
1091 /* Hmm, we couldn't merge the other unit into
1092 * ours? Then let's try it the other way
1095 other = manager_get_unit((*u)->meta.manager, k);
1099 if ((r = unit_merge(other, *u)) >= 0) {
1101 return merge_by_names(u, names, NULL);
1108 unit_choose_id(*u, id);
1116 static void dump_items(FILE *f, const ConfigItem *items) {
1117 const ConfigItem *i;
1118 const char *prev_section = NULL;
1119 bool not_first = false;
1122 ConfigParserCallback callback;
1125 { config_parse_int, "INTEGER" },
1126 { config_parse_unsigned, "UNSIGNED" },
1127 { config_parse_size, "SIZE" },
1128 { config_parse_bool, "BOOLEAN" },
1129 { config_parse_string, "STRING" },
1130 { config_parse_path, "PATH" },
1131 { config_parse_strv, "STRING [...]" },
1132 { config_parse_nice, "NICE" },
1133 { config_parse_oom_adjust, "OOMADJUST" },
1134 { config_parse_io_class, "IOCLASS" },
1135 { config_parse_io_priority, "IOPRIORITY" },
1136 { config_parse_cpu_sched_policy, "CPUSCHEDPOLICY" },
1137 { config_parse_cpu_sched_prio, "CPUSCHEDPRIO" },
1138 { config_parse_cpu_affinity, "CPUAFFINITY" },
1139 { config_parse_mode, "MODE" },
1140 { config_parse_output, "OUTPUT" },
1141 { config_parse_input, "INPUT" },
1142 { config_parse_facility, "FACILITY" },
1143 { config_parse_level, "LEVEL" },
1144 { config_parse_capabilities, "CAPABILITIES" },
1145 { config_parse_secure_bits, "SECUREBITS" },
1146 { config_parse_bounding_set, "BOUNDINGSET" },
1147 { config_parse_timer_slack_ns, "TIMERSLACK" },
1148 { config_parse_limit, "LIMIT" },
1149 { config_parse_cgroup, "CGROUP [...]" },
1150 { config_parse_deps, "UNIT [...]" },
1151 { config_parse_names, "UNIT [...]" },
1152 { config_parse_exec, "PATH [ARGUMENT [...]]" },
1153 { config_parse_service_type, "SERVICETYPE" },
1154 { config_parse_service_restart, "SERVICERESTART" },
1155 { config_parse_sysv_priority, "SYSVPRIORITY" },
1156 { config_parse_kill_mode, "KILLMODE" },
1157 { config_parse_listen, "SOCKET [...]" },
1158 { config_parse_socket_bind, "SOCKETBIND" },
1159 { config_parse_bindtodevice, "NETWORKINTERFACE" },
1160 { config_parse_usec, "SECONDS" },
1161 { config_parse_path_strv, "PATH [...]" },
1162 { config_parse_mount_flags, "MOUNTFLAG [...]" },
1163 { config_parse_description, "DESCRIPTION" },
1169 for (i = items; i->lvalue; i++) {
1171 const char *rvalue = "OTHER";
1173 if (!streq_ptr(i->section, prev_section)) {
1179 fprintf(f, "[%s]\n", i->section);
1180 prev_section = i->section;
1183 for (j = 0; j < ELEMENTSOF(table); j++)
1184 if (i->parse == table[j].callback) {
1185 rvalue = table[j].rvalue;
1189 fprintf(f, "%s=%s\n", i->lvalue, rvalue);
1193 static int load_from_path(Unit *u, const char *path) {
1195 static const char* const section_table[_UNIT_TYPE_MAX] = {
1196 [UNIT_SERVICE] = "Service",
1197 [UNIT_TIMER] = "Timer",
1198 [UNIT_SOCKET] = "Socket",
1199 [UNIT_TARGET] = "Target",
1200 [UNIT_DEVICE] = "Device",
1201 [UNIT_MOUNT] = "Mount",
1202 [UNIT_AUTOMOUNT] = "Automount",
1203 [UNIT_SNAPSHOT] = "Snapshot",
1204 [UNIT_SWAP] = "Swap"
1207 #define EXEC_CONTEXT_CONFIG_ITEMS(context, section) \
1208 { "WorkingDirectory", config_parse_path, &(context).working_directory, section }, \
1209 { "RootDirectory", config_parse_path, &(context).root_directory, section }, \
1210 { "User", config_parse_string, &(context).user, section }, \
1211 { "Group", config_parse_string, &(context).group, section }, \
1212 { "SupplementaryGroups", config_parse_strv, &(context).supplementary_groups, section }, \
1213 { "Nice", config_parse_nice, &(context), section }, \
1214 { "OOMAdjust", config_parse_oom_adjust, &(context), section }, \
1215 { "IOSchedulingClass", config_parse_io_class, &(context), section }, \
1216 { "IOSchedulingPriority", config_parse_io_priority, &(context), section }, \
1217 { "CPUSchedulingPolicy", config_parse_cpu_sched_policy,&(context), section }, \
1218 { "CPUSchedulingPriority", config_parse_cpu_sched_prio, &(context), section }, \
1219 { "CPUSchedulingResetOnFork", config_parse_bool, &(context).cpu_sched_reset_on_fork, section }, \
1220 { "CPUAffinity", config_parse_cpu_affinity, &(context), section }, \
1221 { "UMask", config_parse_mode, &(context).umask, section }, \
1222 { "Environment", config_parse_strv, &(context).environment, section }, \
1223 { "StandardInput", config_parse_input, &(context).std_input, section }, \
1224 { "StandardOutput", config_parse_output, &(context).std_output, section }, \
1225 { "StandardError", config_parse_output, &(context).std_output, section }, \
1226 { "TTYPath", config_parse_path, &(context).tty_path, section }, \
1227 { "SyslogIdentifier", config_parse_string, &(context).syslog_identifier, section }, \
1228 { "SyslogFacility", config_parse_facility, &(context).syslog_priority, section }, \
1229 { "SyslogLevel", config_parse_level, &(context).syslog_priority, section }, \
1230 { "SyslogNoPrefix", config_parse_bool, &(context).syslog_no_prefix, section }, \
1231 { "Capabilities", config_parse_capabilities, &(context), section }, \
1232 { "SecureBits", config_parse_secure_bits, &(context), section }, \
1233 { "CapabilityBoundingSetDrop", config_parse_bounding_set, &(context), section }, \
1234 { "TimerSlackNS", config_parse_timer_slack_ns, &(context), section }, \
1235 { "LimitCPU", config_parse_limit, &(context).rlimit[RLIMIT_CPU], section }, \
1236 { "LimitFSIZE", config_parse_limit, &(context).rlimit[RLIMIT_FSIZE], section }, \
1237 { "LimitDATA", config_parse_limit, &(context).rlimit[RLIMIT_DATA], section }, \
1238 { "LimitSTACK", config_parse_limit, &(context).rlimit[RLIMIT_STACK], section }, \
1239 { "LimitCORE", config_parse_limit, &(context).rlimit[RLIMIT_CORE], section }, \
1240 { "LimitRSS", config_parse_limit, &(context).rlimit[RLIMIT_RSS], section }, \
1241 { "LimitNOFILE", config_parse_limit, &(context).rlimit[RLIMIT_NOFILE], section }, \
1242 { "LimitAS", config_parse_limit, &(context).rlimit[RLIMIT_AS], section }, \
1243 { "LimitNPROC", config_parse_limit, &(context).rlimit[RLIMIT_NPROC], section }, \
1244 { "LimitMEMLOCK", config_parse_limit, &(context).rlimit[RLIMIT_MEMLOCK], section }, \
1245 { "LimitLOCKS", config_parse_limit, &(context).rlimit[RLIMIT_LOCKS], section }, \
1246 { "LimitSIGPENDING", config_parse_limit, &(context).rlimit[RLIMIT_SIGPENDING], section }, \
1247 { "LimitMSGQUEUE", config_parse_limit, &(context).rlimit[RLIMIT_MSGQUEUE], section }, \
1248 { "LimitNICE", config_parse_limit, &(context).rlimit[RLIMIT_NICE], section }, \
1249 { "LimitRTPRIO", config_parse_limit, &(context).rlimit[RLIMIT_RTPRIO], section }, \
1250 { "LimitRTTIME", config_parse_limit, &(context).rlimit[RLIMIT_RTTIME], section }, \
1251 { "ControlGroup", config_parse_cgroup, u, section }, \
1252 { "ReadWriteDirectories", config_parse_path_strv, &(context).read_write_dirs, section }, \
1253 { "ReadOnlyDirectories", config_parse_path_strv, &(context).read_only_dirs, section }, \
1254 { "InaccessibleDirectories",config_parse_path_strv, &(context).inaccessible_dirs, section }, \
1255 { "PrivateTmp", config_parse_bool, &(context).private_tmp, section }, \
1256 { "MountFlags", config_parse_mount_flags, &(context), section }
1258 const ConfigItem items[] = {
1259 { "Names", config_parse_names, u, "Unit" },
1260 { "Description", config_parse_description, u, "Unit" },
1261 { "Requires", config_parse_deps, UINT_TO_PTR(UNIT_REQUIRES), "Unit" },
1262 { "RequiresOverridable", config_parse_deps, UINT_TO_PTR(UNIT_REQUIRES_OVERRIDABLE), "Unit" },
1263 { "Requisite", config_parse_deps, UINT_TO_PTR(UNIT_REQUISITE), "Unit" },
1264 { "RequisiteOverridable", config_parse_deps, UINT_TO_PTR(UNIT_REQUISITE_OVERRIDABLE), "Unit" },
1265 { "Wants", config_parse_deps, UINT_TO_PTR(UNIT_WANTS), "Unit" },
1266 { "Conflicts", config_parse_deps, UINT_TO_PTR(UNIT_CONFLICTS), "Unit" },
1267 { "Before", config_parse_deps, UINT_TO_PTR(UNIT_BEFORE), "Unit" },
1268 { "After", config_parse_deps, UINT_TO_PTR(UNIT_AFTER), "Unit" },
1269 { "RecursiveStop", config_parse_bool, &u->meta.recursive_stop, "Unit" },
1270 { "StopWhenUnneeded", config_parse_bool, &u->meta.stop_when_unneeded, "Unit" },
1272 { "PIDFile", config_parse_path, &u->service.pid_file, "Service" },
1273 { "ExecStartPre", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START_PRE, "Service" },
1274 { "ExecStart", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START, "Service" },
1275 { "ExecStartPost", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START_POST, "Service" },
1276 { "ExecReload", config_parse_exec, u->service.exec_command+SERVICE_EXEC_RELOAD, "Service" },
1277 { "ExecStop", config_parse_exec, u->service.exec_command+SERVICE_EXEC_STOP, "Service" },
1278 { "ExecStopPost", config_parse_exec, u->service.exec_command+SERVICE_EXEC_STOP_POST, "Service" },
1279 { "RestartSec", config_parse_usec, &u->service.restart_usec, "Service" },
1280 { "TimeoutSec", config_parse_usec, &u->service.timeout_usec, "Service" },
1281 { "Type", config_parse_service_type, &u->service.type, "Service" },
1282 { "Restart", config_parse_service_restart, &u->service.restart, "Service" },
1283 { "PermissionsStartOnly", config_parse_bool, &u->service.permissions_start_only, "Service" },
1284 { "RootDirectoryStartOnly", config_parse_bool, &u->service.root_directory_start_only, "Service" },
1285 { "ValidNoProcess", config_parse_bool, &u->service.valid_no_process, "Service" },
1286 { "SysVStartPriority", config_parse_sysv_priority, &u->service.sysv_start_priority, "Service" },
1287 { "KillMode", config_parse_kill_mode, &u->service.kill_mode, "Service" },
1288 { "NonBlocking", config_parse_bool, &u->service.exec_context.non_blocking, "Service" },
1289 { "BusName", config_parse_string, &u->service.bus_name, "Service" },
1290 EXEC_CONTEXT_CONFIG_ITEMS(u->service.exec_context, "Service"),
1292 { "ListenStream", config_parse_listen, &u->socket, "Socket" },
1293 { "ListenDatagram", config_parse_listen, &u->socket, "Socket" },
1294 { "ListenSequentialPacket", config_parse_listen, &u->socket, "Socket" },
1295 { "ListenFIFO", config_parse_listen, &u->socket, "Socket" },
1296 { "BindIPv6Only", config_parse_socket_bind, &u->socket, "Socket" },
1297 { "Backlog", config_parse_unsigned, &u->socket.backlog, "Socket" },
1298 { "BindToDevice", config_parse_bindtodevice, &u->socket, "Socket" },
1299 { "ExecStartPre", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_START_PRE, "Socket" },
1300 { "ExecStartPost", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_START_POST, "Socket" },
1301 { "ExecStopPre", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_STOP_PRE, "Socket" },
1302 { "ExecStopPost", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_STOP_POST, "Socket" },
1303 { "TimeoutSec", config_parse_usec, &u->socket.timeout_usec, "Socket" },
1304 { "DirectoryMode", config_parse_mode, &u->socket.directory_mode, "Socket" },
1305 { "SocketMode", config_parse_mode, &u->socket.socket_mode, "Socket" },
1306 { "KillMode", config_parse_kill_mode, &u->socket.kill_mode, "Socket" },
1307 { "Accept", config_parse_bool, &u->socket.accept, "Socket" },
1308 EXEC_CONTEXT_CONFIG_ITEMS(u->socket.exec_context, "Socket"),
1310 { "What", config_parse_string, &u->mount.parameters_fragment.what, "Mount" },
1311 { "Where", config_parse_path, &u->mount.where, "Mount" },
1312 { "Options", config_parse_string, &u->mount.parameters_fragment.options, "Mount" },
1313 { "Type", config_parse_string, &u->mount.parameters_fragment.fstype, "Mount" },
1314 { "TimeoutSec", config_parse_usec, &u->mount.timeout_usec, "Mount" },
1315 { "KillMode", config_parse_kill_mode, &u->mount.kill_mode, "Mount" },
1316 EXEC_CONTEXT_CONFIG_ITEMS(u->mount.exec_context, "Mount"),
1318 { "Where", config_parse_path, &u->automount.where, "Automount" },
1320 { "What", config_parse_path, &u->swap.parameters_fragment.what, "Swap" },
1321 { "Priority", config_parse_int, &u->swap.parameters_fragment.priority, "Swap" },
1323 { NULL, NULL, NULL, NULL }
1326 #undef EXEC_CONTEXT_CONFIG_ITEMS
1328 const char *sections[3];
1333 char *filename = NULL, *id = NULL;
1337 /* Dirty dirty hack. */
1338 dump_items((FILE*) path, items);
1345 sections[0] = "Unit";
1346 sections[1] = section_table[u->meta.type];
1349 if (!(symlink_names = set_new(string_hash_func, string_compare_func)))
1352 if (path_is_absolute(path)) {
1354 if (!(filename = strdup(path))) {
1359 if ((r = open_follow(&filename, &f, symlink_names, &id)) < 0) {
1370 STRV_FOREACH(p, u->meta.manager->unit_path) {
1372 /* Instead of opening the path right away, we manually
1373 * follow all symlinks and add their name to our unit
1374 * name set while doing so */
1375 if (!(filename = path_make_absolute(path, *p))) {
1380 if ((r = open_follow(&filename, &f, symlink_names, &id)) < 0) {
1389 /* Empty the symlink names for the next run */
1390 while ((sn = set_steal_first(symlink_names)))
1406 if ((r = merge_by_names(&merged, symlink_names, id)) < 0)
1410 u->meta.load_state = UNIT_MERGED;
1415 /* Now, parse the file contents */
1416 if ((r = config_parse(filename, f, sections, items, u)) < 0)
1419 free(u->meta.fragment_path);
1420 u->meta.fragment_path = filename;
1423 u->meta.load_state = UNIT_LOADED;
1427 while ((k = set_steal_first(symlink_names)))
1430 set_free(symlink_names);
1439 int unit_load_fragment(Unit *u) {
1444 if (u->meta.fragment_path) {
1446 if ((r = load_from_path(u, u->meta.fragment_path)) < 0)
1453 /* Try to find the unit under its id */
1454 if ((r = load_from_path(u, u->meta.id)) < 0)
1457 /* Try to find an alias we can load this with */
1458 if (u->meta.load_state == UNIT_STUB)
1459 SET_FOREACH(t, u->meta.names, i) {
1461 if (t == u->meta.id)
1464 if ((r = load_from_path(u, t)) < 0)
1467 if (u->meta.load_state != UNIT_STUB)
1471 /* Now, follow the same logic, but look for a template */
1472 if (u->meta.load_state == UNIT_STUB && u->meta.instance) {
1475 if (!(k = unit_name_template(u->meta.id)))
1478 r = load_from_path(u, k);
1484 if (u->meta.load_state == UNIT_STUB)
1485 SET_FOREACH(t, u->meta.names, i) {
1487 if (t == u->meta.id)
1490 if (!(k = unit_name_template(t)))
1493 r = load_from_path(u, k);
1499 if (u->meta.load_state != UNIT_STUB)
1508 void unit_dump_config_items(FILE *f) {
1509 /* OK, this wins a prize for extreme ugliness. */
1511 load_from_path(NULL, (const void*) f);