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 COMMENTS "#;\n"
45 static int config_parse_deps(
54 UnitDependency d
= PTR_TO_UINT(data
);
64 FOREACH_WORD_QUOTED(w
, l
, rvalue
, state
) {
68 if (!(t
= strndup(w
, l
)))
71 k
= unit_name_printf(u
, t
);
77 r
= unit_add_dependency_by_name(u
, d
, k
, NULL
, true);
87 static int config_parse_names(
106 FOREACH_WORD_QUOTED(w
, l
, rvalue
, state
) {
110 if (!(t
= strndup(w
, l
)))
113 k
= unit_name_printf(u
, t
);
119 r
= unit_merge_by_name(u
, k
);
129 static int config_parse_string_printf(
130 const char *filename
,
148 if (!(k
= unit_full_printf(u
, rvalue
)))
162 static int config_parse_listen(
163 const char *filename
,
182 if (!(p
= new0(SocketPort
, 1)))
185 if (streq(lvalue
, "ListenFIFO")) {
186 p
->type
= SOCKET_FIFO
;
188 if (!(p
->path
= strdup(rvalue
))) {
193 path_kill_slashes(p
->path
);
195 p
->type
= SOCKET_SOCKET
;
197 if ((r
= socket_address_parse(&p
->address
, rvalue
)) < 0) {
198 log_error("[%s:%u] Failed to parse address value: %s", filename
, line
, rvalue
);
203 if (streq(lvalue
, "ListenStream"))
204 p
->address
.type
= SOCK_STREAM
;
205 else if (streq(lvalue
, "ListenDatagram"))
206 p
->address
.type
= SOCK_DGRAM
;
208 assert(streq(lvalue
, "ListenSequentialPacket"));
209 p
->address
.type
= SOCK_SEQPACKET
;
212 if (socket_address_family(&p
->address
) != AF_LOCAL
&& p
->address
.type
== SOCK_SEQPACKET
) {
214 return -EPROTONOSUPPORT
;
219 LIST_PREPEND(SocketPort
, port
, s
->ports
, p
);
224 static int config_parse_socket_bind(
225 const char *filename
,
234 SocketAddressBindIPv6Only b
;
243 if ((b
= socket_address_bind_ipv6_only_from_string(rvalue
)) < 0) {
246 if ((r
= parse_boolean(rvalue
)) < 0) {
247 log_error("[%s:%u] Failed to parse bind IPv6 only value: %s", filename
, line
, rvalue
);
251 s
->bind_ipv6_only
= r
? SOCKET_ADDRESS_IPV6_ONLY
: SOCKET_ADDRESS_BOTH
;
253 s
->bind_ipv6_only
= b
;
258 static int config_parse_nice(
259 const char *filename
,
267 ExecContext
*c
= data
;
275 if ((r
= safe_atoi(rvalue
, &priority
)) < 0) {
276 log_error("[%s:%u] Failed to parse nice priority: %s", filename
, line
, rvalue
);
280 if (priority
< PRIO_MIN
|| priority
>= PRIO_MAX
) {
281 log_error("[%s:%u] Nice priority out of range: %s", filename
, line
, rvalue
);
291 static int config_parse_oom_adjust(
292 const char *filename
,
300 ExecContext
*c
= data
;
308 if ((r
= safe_atoi(rvalue
, &oa
)) < 0) {
309 log_error("[%s:%u] Failed to parse OOM adjust value: %s", filename
, line
, rvalue
);
313 if (oa
< OOM_DISABLE
|| oa
> OOM_ADJUST_MAX
) {
314 log_error("[%s:%u] OOM adjust value out of range: %s", filename
, line
, rvalue
);
319 c
->oom_adjust_set
= true;
324 static int config_parse_mode(
325 const char *filename
,
343 l
= strtol(rvalue
, &x
, 8);
344 if (!x
|| *x
|| errno
) {
345 log_error("[%s:%u] Failed to parse mode value: %s", filename
, line
, rvalue
);
346 return errno
? -errno
: -EINVAL
;
349 if (l
< 0000 || l
> 07777) {
350 log_error("[%s:%u] mode value out of range: %s", filename
, line
, rvalue
);
358 static int config_parse_exec(
359 const char *filename
,
367 ExecCommand
**e
= data
, *nce
;
376 /* We accept an absolute path as first argument, or
377 * alternatively an absolute prefixed with @ to allow
378 * overriding of argv[0]. */
384 bool honour_argv0
, write_to_path
;
390 rvalue
+= strspn(rvalue
, WHITESPACE
);
395 honour_argv0
= rvalue
[0] == '@';
397 if (rvalue
[honour_argv0
? 1 : 0] != '/') {
398 log_error("[%s:%u] Invalid executable path in command line: %s", filename
, line
, rvalue
);
403 FOREACH_WORD_QUOTED(w
, l
, rvalue
, state
) {
404 if (strncmp(w
, ";", l
) == 0)
410 if (!(n
= new(char*, k
+ (honour_argv0
? 0 : 1))))
414 write_to_path
= honour_argv0
;
415 FOREACH_WORD_QUOTED(w
, l
, rvalue
, state
) {
416 if (strncmp(w
, ";", l
) == 0)
420 if (!(path
= cunescape_length(w
+1, l
-1)))
422 write_to_path
= false;
424 if (!(n
[k
++] = cunescape_length(w
, l
)))
432 log_error("[%s:%u] Invalid command line: %s", filename
, line
, rvalue
);
438 if (!(path
= strdup(n
[0])))
441 assert(path_is_absolute(path
));
443 if (!(nce
= new0(ExecCommand
, 1)))
449 path_kill_slashes(nce
->path
);
451 exec_command_append_list(e
, nce
);
467 static int config_parse_usec(
468 const char *filename
,
484 if ((r
= parse_usec(rvalue
, usec
)) < 0) {
485 log_error("[%s:%u] Failed to parse time value: %s", filename
, line
, rvalue
);
492 static DEFINE_CONFIG_PARSE_ENUM(config_parse_service_type
, service_type
, ServiceType
, "Failed to parse service type");
493 static DEFINE_CONFIG_PARSE_ENUM(config_parse_service_restart
, service_restart
, ServiceRestart
, "Failed to parse service restart specifier");
495 static int config_parse_bindtodevice(
496 const char *filename
,
512 if (rvalue
[0] && !streq(rvalue
, "*")) {
513 if (!(n
= strdup(rvalue
)))
518 free(s
->bind_to_device
);
519 s
->bind_to_device
= n
;
524 static DEFINE_CONFIG_PARSE_ENUM(config_parse_output
, exec_output
, ExecOutput
, "Failed to parse output specifier");
525 static DEFINE_CONFIG_PARSE_ENUM(config_parse_input
, exec_input
, ExecInput
, "Failed to parse input specifier");
527 static int config_parse_facility(
528 const char *filename
,
544 if ((x
= log_facility_from_string(rvalue
)) < 0) {
545 log_error("[%s:%u] Failed to parse log facility: %s", filename
, line
, rvalue
);
549 *o
= LOG_MAKEPRI(x
, LOG_PRI(*o
));
554 static int config_parse_level(
555 const char *filename
,
571 if ((x
= log_level_from_string(rvalue
)) < 0) {
572 log_error("[%s:%u] Failed to parse log level: %s", filename
, line
, rvalue
);
576 *o
= LOG_MAKEPRI(LOG_FAC(*o
), x
);
580 static int config_parse_io_class(
581 const char *filename
,
589 ExecContext
*c
= data
;
597 if ((x
= ioprio_class_from_string(rvalue
)) < 0) {
598 log_error("[%s:%u] Failed to parse IO scheduling class: %s", filename
, line
, rvalue
);
602 c
->ioprio
= IOPRIO_PRIO_VALUE(x
, IOPRIO_PRIO_DATA(c
->ioprio
));
603 c
->ioprio_set
= true;
608 static int config_parse_io_priority(
609 const char *filename
,
617 ExecContext
*c
= data
;
625 if (safe_atoi(rvalue
, &i
) < 0 || i
< 0 || i
>= IOPRIO_BE_NR
) {
626 log_error("[%s:%u] Failed to parse io priority: %s", filename
, line
, rvalue
);
630 c
->ioprio
= IOPRIO_PRIO_VALUE(IOPRIO_PRIO_CLASS(c
->ioprio
), i
);
631 c
->ioprio_set
= true;
636 static int config_parse_cpu_sched_policy(
637 const char *filename
,
646 ExecContext
*c
= data
;
654 if ((x
= sched_policy_from_string(rvalue
)) < 0) {
655 log_error("[%s:%u] Failed to parse CPU scheduling policy: %s", filename
, line
, rvalue
);
659 c
->cpu_sched_policy
= x
;
660 c
->cpu_sched_set
= true;
665 static int config_parse_cpu_sched_prio(
666 const char *filename
,
674 ExecContext
*c
= data
;
682 /* On Linux RR/FIFO have the same range */
683 if (safe_atoi(rvalue
, &i
) < 0 || i
< sched_get_priority_min(SCHED_RR
) || i
> sched_get_priority_max(SCHED_RR
)) {
684 log_error("[%s:%u] Failed to parse CPU scheduling priority: %s", filename
, line
, rvalue
);
688 c
->cpu_sched_priority
= i
;
689 c
->cpu_sched_set
= true;
694 static int config_parse_cpu_affinity(
695 const char *filename
,
703 ExecContext
*c
= data
;
713 FOREACH_WORD_QUOTED(w
, l
, rvalue
, state
) {
718 if (!(t
= strndup(w
, l
)))
721 r
= safe_atou(t
, &cpu
);
725 if (!(c
->cpuset
= cpu_set_malloc(&c
->cpuset_ncpus
)))
728 if (r
< 0 || cpu
>= c
->cpuset_ncpus
) {
729 log_error("[%s:%u] Failed to parse CPU affinity: %s", filename
, line
, rvalue
);
733 CPU_SET_S(cpu
, CPU_ALLOC_SIZE(c
->cpuset_ncpus
), c
->cpuset
);
739 static int config_parse_capabilities(
740 const char *filename
,
748 ExecContext
*c
= data
;
756 if (!(cap
= cap_from_text(rvalue
))) {
760 log_error("[%s:%u] Failed to parse capabilities: %s", filename
, line
, rvalue
);
765 cap_free(c
->capabilities
);
766 c
->capabilities
= cap
;
771 static int config_parse_secure_bits(
772 const char *filename
,
780 ExecContext
*c
= data
;
790 FOREACH_WORD_QUOTED(w
, l
, rvalue
, state
) {
791 if (first_word(w
, "keep-caps"))
792 c
->secure_bits
|= SECURE_KEEP_CAPS
;
793 else if (first_word(w
, "keep-caps-locked"))
794 c
->secure_bits
|= SECURE_KEEP_CAPS_LOCKED
;
795 else if (first_word(w
, "no-setuid-fixup"))
796 c
->secure_bits
|= SECURE_NO_SETUID_FIXUP
;
797 else if (first_word(w
, "no-setuid-fixup-locked"))
798 c
->secure_bits
|= SECURE_NO_SETUID_FIXUP_LOCKED
;
799 else if (first_word(w
, "noroot"))
800 c
->secure_bits
|= SECURE_NOROOT
;
801 else if (first_word(w
, "noroot-locked"))
802 c
->secure_bits
|= SECURE_NOROOT_LOCKED
;
804 log_error("[%s:%u] Failed to parse secure bits: %s", filename
, line
, rvalue
);
812 static int config_parse_bounding_set(
813 const char *filename
,
821 ExecContext
*c
= data
;
831 FOREACH_WORD_QUOTED(w
, l
, rvalue
, state
) {
836 if (!(t
= strndup(w
, l
)))
839 r
= cap_from_name(t
, &cap
);
843 log_error("[%s:%u] Failed to parse capability bounding set: %s", filename
, line
, rvalue
);
847 c
->capability_bounding_set_drop
|= 1 << cap
;
853 static int config_parse_timer_slack_nsec(
854 const char *filename
,
862 ExecContext
*c
= data
;
871 if ((r
= safe_atolu(rvalue
, &u
)) < 0) {
872 log_error("[%s:%u] Failed to parse time slack value: %s", filename
, line
, rvalue
);
876 c
->timer_slack_nsec
= u
;
881 static int config_parse_limit(
882 const char *filename
,
890 struct rlimit
**rl
= data
;
891 unsigned long long u
;
899 if ((r
= safe_atollu(rvalue
, &u
)) < 0) {
900 log_error("[%s:%u] Failed to parse resource value: %s", filename
, line
, rvalue
);
905 if (!(*rl
= new(struct rlimit
, 1)))
908 (*rl
)->rlim_cur
= (*rl
)->rlim_max
= (rlim_t
) u
;
912 static int config_parse_cgroup(
913 const char *filename
,
926 FOREACH_WORD_QUOTED(w
, l
, rvalue
, state
) {
930 if (!(t
= cunescape_length(w
, l
)))
933 r
= unit_add_cgroup_from_text(u
, t
);
943 static int config_parse_sysv_priority(
944 const char *filename
,
952 int *priority
= data
;
960 if ((r
= safe_atoi(rvalue
, &i
)) < 0 || i
< 0) {
961 log_error("[%s:%u] Failed to parse SysV start priority: %s", filename
, line
, rvalue
);
969 static DEFINE_CONFIG_PARSE_ENUM(config_parse_kill_mode
, kill_mode
, KillMode
, "Failed to parse kill mode");
971 static int config_parse_mount_flags(
972 const char *filename
,
980 ExecContext
*c
= data
;
984 unsigned long flags
= 0;
991 FOREACH_WORD_QUOTED(w
, l
, rvalue
, state
) {
992 if (strncmp(w
, "shared", l
) == 0)
994 else if (strncmp(w
, "slave", l
) == 0)
996 else if (strncmp(w
, "private", l
) == 0)
999 log_error("[%s:%u] Failed to parse mount flags: %s", filename
, line
, rvalue
);
1004 c
->mount_flags
= flags
;
1008 static int config_parse_timer(
1009 const char *filename
,
1011 const char *section
,
1028 if ((b
= timer_base_from_string(lvalue
)) < 0) {
1029 log_error("[%s:%u] Failed to parse timer base: %s", filename
, line
, lvalue
);
1033 if ((r
= parse_usec(rvalue
, &u
)) < 0) {
1034 log_error("[%s:%u] Failed to parse timer value: %s", filename
, line
, rvalue
);
1038 if (!(v
= new0(TimerValue
, 1)))
1044 LIST_PREPEND(TimerValue
, value
, t
->values
, v
);
1049 static int config_parse_timer_unit(
1050 const char *filename
,
1052 const char *section
,
1061 if (endswith(rvalue
, ".timer")) {
1062 log_error("[%s:%u] Unit cannot be of type timer: %s", filename
, line
, rvalue
);
1066 if ((r
= manager_load_unit(t
->meta
.manager
, rvalue
, NULL
, &t
->unit
)) < 0) {
1067 log_error("[%s:%u] Failed to load unit: %s", filename
, line
, rvalue
);
1074 static int config_parse_path_spec(
1075 const char *filename
,
1077 const char *section
,
1092 if ((b
= path_type_from_string(lvalue
)) < 0) {
1093 log_error("[%s:%u] Failed to parse path type: %s", filename
, line
, lvalue
);
1097 if (!path_is_absolute(rvalue
)) {
1098 log_error("[%s:%u] Path is not absolute: %s", filename
, line
, rvalue
);
1102 if (!(s
= new0(PathSpec
, 1)))
1105 if (!(s
->path
= strdup(rvalue
))) {
1110 path_kill_slashes(s
->path
);
1115 LIST_PREPEND(PathSpec
, spec
, p
->specs
, s
);
1120 static int config_parse_path_unit(
1121 const char *filename
,
1123 const char *section
,
1132 if (endswith(rvalue
, ".path")) {
1133 log_error("[%s:%u] Unit cannot be of type path: %s", filename
, line
, rvalue
);
1137 if ((r
= manager_load_unit(t
->meta
.manager
, rvalue
, NULL
, &t
->unit
)) < 0) {
1138 log_error("[%s:%u] Failed to load unit: %s", filename
, line
, rvalue
);
1145 static int config_parse_env_file(
1146 const char *filename
,
1148 const char *section
,
1163 if (!(f
= fopen(rvalue
, "re"))) {
1164 log_error("[%s:%u] Failed to open environment file '%s': %m", filename
, line
, rvalue
);
1169 char l
[LINE_MAX
], *p
;
1172 if (!fgets(l
, sizeof(l
), f
)) {
1177 log_error("[%s:%u] Failed to read environment file '%s': %m", filename
, line
, rvalue
);
1186 if (strchr(COMMENTS
, *p
))
1189 t
= strv_env_set(*env
, p
);
1203 static int config_parse_ip_tos(
1204 const char *filename
,
1206 const char *section
,
1212 int *ip_tos
= data
, x
;
1220 if ((x
= ip_tos_from_string(rvalue
)) < 0)
1221 if ((r
= safe_atoi(rvalue
, &x
)) < 0) {
1222 log_error("[%s:%u] Failed to parse IP TOS value: %s", filename
, line
, rvalue
);
1230 static DEFINE_CONFIG_PARSE_ENUM(config_parse_notify_access
, notify_access
, NotifyAccess
, "Failed to parse notify access specifier");
1232 #define FOLLOW_MAX 8
1234 static int open_follow(char **filename
, FILE **_f
, Set
*names
, char **_final
) {
1245 /* This will update the filename pointer if the loaded file is
1246 * reached by a symlink. The old string will be freed. */
1249 char *target
, *name
;
1251 if (c
++ >= FOLLOW_MAX
)
1254 path_kill_slashes(*filename
);
1256 /* Add the file name we are currently looking at to
1257 * the names of this unit */
1258 name
= file_name_from_path(*filename
);
1259 if (!(id
= set_get(names
, name
))) {
1261 if (!(id
= strdup(name
)))
1264 if ((r
= set_put(names
, id
)) < 0) {
1270 /* Try to open the file name, but don't if its a symlink */
1271 if ((fd
= open(*filename
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
)) >= 0)
1277 /* Hmm, so this is a symlink. Let's read the name, and follow it manually */
1278 if ((r
= readlink_and_make_absolute(*filename
, &target
)) < 0)
1285 if (!(f
= fdopen(fd
, "r"))) {
1287 close_nointr_nofail(fd
);
1296 static int merge_by_names(Unit
**u
, Set
*names
, const char *id
) {
1304 /* Let's try to add in all symlink names we found */
1305 while ((k
= set_steal_first(names
))) {
1307 /* First try to merge in the other name into our
1309 if ((r
= unit_merge_by_name(*u
, k
)) < 0) {
1312 /* Hmm, we couldn't merge the other unit into
1313 * ours? Then let's try it the other way
1316 other
= manager_get_unit((*u
)->meta
.manager
, k
);
1320 if ((r
= unit_merge(other
, *u
)) >= 0) {
1322 return merge_by_names(u
, names
, NULL
);
1329 unit_choose_id(*u
, id
);
1337 static void dump_items(FILE *f
, const ConfigItem
*items
) {
1338 const ConfigItem
*i
;
1339 const char *prev_section
= NULL
;
1340 bool not_first
= false;
1343 ConfigParserCallback callback
;
1346 { config_parse_int
, "INTEGER" },
1347 { config_parse_unsigned
, "UNSIGNED" },
1348 { config_parse_size
, "SIZE" },
1349 { config_parse_bool
, "BOOLEAN" },
1350 { config_parse_string
, "STRING" },
1351 { config_parse_path
, "PATH" },
1352 { config_parse_strv
, "STRING [...]" },
1353 { config_parse_nice
, "NICE" },
1354 { config_parse_oom_adjust
, "OOMADJUST" },
1355 { config_parse_io_class
, "IOCLASS" },
1356 { config_parse_io_priority
, "IOPRIORITY" },
1357 { config_parse_cpu_sched_policy
, "CPUSCHEDPOLICY" },
1358 { config_parse_cpu_sched_prio
, "CPUSCHEDPRIO" },
1359 { config_parse_cpu_affinity
, "CPUAFFINITY" },
1360 { config_parse_mode
, "MODE" },
1361 { config_parse_env_file
, "FILE" },
1362 { config_parse_output
, "OUTPUT" },
1363 { config_parse_input
, "INPUT" },
1364 { config_parse_facility
, "FACILITY" },
1365 { config_parse_level
, "LEVEL" },
1366 { config_parse_capabilities
, "CAPABILITIES" },
1367 { config_parse_secure_bits
, "SECUREBITS" },
1368 { config_parse_bounding_set
, "BOUNDINGSET" },
1369 { config_parse_timer_slack_nsec
, "TIMERSLACK" },
1370 { config_parse_limit
, "LIMIT" },
1371 { config_parse_cgroup
, "CGROUP [...]" },
1372 { config_parse_deps
, "UNIT [...]" },
1373 { config_parse_names
, "UNIT [...]" },
1374 { config_parse_exec
, "PATH [ARGUMENT [...]]" },
1375 { config_parse_service_type
, "SERVICETYPE" },
1376 { config_parse_service_restart
, "SERVICERESTART" },
1377 { config_parse_sysv_priority
, "SYSVPRIORITY" },
1378 { config_parse_kill_mode
, "KILLMODE" },
1379 { config_parse_listen
, "SOCKET [...]" },
1380 { config_parse_socket_bind
, "SOCKETBIND" },
1381 { config_parse_bindtodevice
, "NETWORKINTERFACE" },
1382 { config_parse_usec
, "SECONDS" },
1383 { config_parse_path_strv
, "PATH [...]" },
1384 { config_parse_mount_flags
, "MOUNTFLAG [...]" },
1385 { config_parse_string_printf
, "STRING" },
1386 { config_parse_timer
, "TIMER" },
1387 { config_parse_timer_unit
, "NAME" },
1388 { config_parse_path_spec
, "PATH" },
1389 { config_parse_path_unit
, "UNIT" },
1390 { config_parse_notify_access
, "ACCESS" },
1391 { config_parse_ip_tos
, "TOS" },
1397 for (i
= items
; i
->lvalue
; i
++) {
1399 const char *rvalue
= "OTHER";
1401 if (!streq_ptr(i
->section
, prev_section
)) {
1407 fprintf(f
, "[%s]\n", i
->section
);
1408 prev_section
= i
->section
;
1411 for (j
= 0; j
< ELEMENTSOF(table
); j
++)
1412 if (i
->parse
== table
[j
].callback
) {
1413 rvalue
= table
[j
].rvalue
;
1417 fprintf(f
, "%s=%s\n", i
->lvalue
, rvalue
);
1421 static int load_from_path(Unit
*u
, const char *path
) {
1423 static const char* const section_table
[_UNIT_TYPE_MAX
] = {
1424 [UNIT_SERVICE
] = "Service",
1425 [UNIT_TIMER
] = "Timer",
1426 [UNIT_SOCKET
] = "Socket",
1427 [UNIT_TARGET
] = "Target",
1428 [UNIT_DEVICE
] = "Device",
1429 [UNIT_MOUNT
] = "Mount",
1430 [UNIT_AUTOMOUNT
] = "Automount",
1431 [UNIT_SNAPSHOT
] = "Snapshot",
1432 [UNIT_SWAP
] = "Swap",
1433 [UNIT_PATH
] = "Path"
1436 #define EXEC_CONTEXT_CONFIG_ITEMS(context, section) \
1437 { "WorkingDirectory", config_parse_path, &(context).working_directory, section }, \
1438 { "RootDirectory", config_parse_path, &(context).root_directory, section }, \
1439 { "User", config_parse_string_printf, &(context).user, section }, \
1440 { "Group", config_parse_string_printf, &(context).group, section }, \
1441 { "SupplementaryGroups", config_parse_strv, &(context).supplementary_groups, section }, \
1442 { "Nice", config_parse_nice, &(context), section }, \
1443 { "OOMAdjust", config_parse_oom_adjust, &(context), section }, \
1444 { "IOSchedulingClass", config_parse_io_class, &(context), section }, \
1445 { "IOSchedulingPriority", config_parse_io_priority, &(context), section }, \
1446 { "CPUSchedulingPolicy", config_parse_cpu_sched_policy,&(context), section }, \
1447 { "CPUSchedulingPriority", config_parse_cpu_sched_prio, &(context), section }, \
1448 { "CPUSchedulingResetOnFork", config_parse_bool, &(context).cpu_sched_reset_on_fork, section }, \
1449 { "CPUAffinity", config_parse_cpu_affinity, &(context), section }, \
1450 { "UMask", config_parse_mode, &(context).umask, section }, \
1451 { "Environment", config_parse_strv, &(context).environment, section }, \
1452 { "EnvironmentFile", config_parse_env_file, &(context).environment, section }, \
1453 { "StandardInput", config_parse_input, &(context).std_input, section }, \
1454 { "StandardOutput", config_parse_output, &(context).std_output, section }, \
1455 { "StandardError", config_parse_output, &(context).std_error, section }, \
1456 { "TTYPath", config_parse_path, &(context).tty_path, section }, \
1457 { "SyslogIdentifier", config_parse_string_printf, &(context).syslog_identifier, section }, \
1458 { "SyslogFacility", config_parse_facility, &(context).syslog_priority, section }, \
1459 { "SyslogLevel", config_parse_level, &(context).syslog_priority, section }, \
1460 { "SyslogLevelPrefix", config_parse_bool, &(context).syslog_level_prefix, section }, \
1461 { "Capabilities", config_parse_capabilities, &(context), section }, \
1462 { "SecureBits", config_parse_secure_bits, &(context), section }, \
1463 { "CapabilityBoundingSetDrop", config_parse_bounding_set, &(context), section }, \
1464 { "TimerSlackNSec", config_parse_timer_slack_nsec,&(context), section }, \
1465 { "LimitCPU", config_parse_limit, &(context).rlimit[RLIMIT_CPU], section }, \
1466 { "LimitFSIZE", config_parse_limit, &(context).rlimit[RLIMIT_FSIZE], section }, \
1467 { "LimitDATA", config_parse_limit, &(context).rlimit[RLIMIT_DATA], section }, \
1468 { "LimitSTACK", config_parse_limit, &(context).rlimit[RLIMIT_STACK], section }, \
1469 { "LimitCORE", config_parse_limit, &(context).rlimit[RLIMIT_CORE], section }, \
1470 { "LimitRSS", config_parse_limit, &(context).rlimit[RLIMIT_RSS], section }, \
1471 { "LimitNOFILE", config_parse_limit, &(context).rlimit[RLIMIT_NOFILE], section }, \
1472 { "LimitAS", config_parse_limit, &(context).rlimit[RLIMIT_AS], section }, \
1473 { "LimitNPROC", config_parse_limit, &(context).rlimit[RLIMIT_NPROC], section }, \
1474 { "LimitMEMLOCK", config_parse_limit, &(context).rlimit[RLIMIT_MEMLOCK], section }, \
1475 { "LimitLOCKS", config_parse_limit, &(context).rlimit[RLIMIT_LOCKS], section }, \
1476 { "LimitSIGPENDING", config_parse_limit, &(context).rlimit[RLIMIT_SIGPENDING], section }, \
1477 { "LimitMSGQUEUE", config_parse_limit, &(context).rlimit[RLIMIT_MSGQUEUE], section }, \
1478 { "LimitNICE", config_parse_limit, &(context).rlimit[RLIMIT_NICE], section }, \
1479 { "LimitRTPRIO", config_parse_limit, &(context).rlimit[RLIMIT_RTPRIO], section }, \
1480 { "LimitRTTIME", config_parse_limit, &(context).rlimit[RLIMIT_RTTIME], section }, \
1481 { "ControlGroup", config_parse_cgroup, u, section }, \
1482 { "ReadWriteDirectories", config_parse_path_strv, &(context).read_write_dirs, section }, \
1483 { "ReadOnlyDirectories", config_parse_path_strv, &(context).read_only_dirs, section }, \
1484 { "InaccessibleDirectories",config_parse_path_strv, &(context).inaccessible_dirs, section }, \
1485 { "PrivateTmp", config_parse_bool, &(context).private_tmp, section }, \
1486 { "MountFlags", config_parse_mount_flags, &(context), section }, \
1487 { "TCPWrapName", config_parse_string_printf, &(context).tcpwrap_name, section }, \
1488 { "PAMName", config_parse_string_printf, &(context).pam_name, section }
1490 const ConfigItem items
[] = {
1491 { "Names", config_parse_names
, u
, "Unit" },
1492 { "Description", config_parse_string_printf
, &u
->meta
.description
, "Unit" },
1493 { "Requires", config_parse_deps
, UINT_TO_PTR(UNIT_REQUIRES
), "Unit" },
1494 { "RequiresOverridable", config_parse_deps
, UINT_TO_PTR(UNIT_REQUIRES_OVERRIDABLE
), "Unit" },
1495 { "Requisite", config_parse_deps
, UINT_TO_PTR(UNIT_REQUISITE
), "Unit" },
1496 { "RequisiteOverridable", config_parse_deps
, UINT_TO_PTR(UNIT_REQUISITE_OVERRIDABLE
), "Unit" },
1497 { "Wants", config_parse_deps
, UINT_TO_PTR(UNIT_WANTS
), "Unit" },
1498 { "Conflicts", config_parse_deps
, UINT_TO_PTR(UNIT_CONFLICTS
), "Unit" },
1499 { "Before", config_parse_deps
, UINT_TO_PTR(UNIT_BEFORE
), "Unit" },
1500 { "After", config_parse_deps
, UINT_TO_PTR(UNIT_AFTER
), "Unit" },
1501 { "RecursiveStop", config_parse_bool
, &u
->meta
.recursive_stop
, "Unit" },
1502 { "StopWhenUnneeded", config_parse_bool
, &u
->meta
.stop_when_unneeded
, "Unit" },
1503 { "OnlyByDependency", config_parse_bool
, &u
->meta
.only_by_dependency
, "Unit" },
1504 { "DefaultDependencies", config_parse_bool
, &u
->meta
.default_dependencies
, "Unit" },
1506 { "PIDFile", config_parse_path
, &u
->service
.pid_file
, "Service" },
1507 { "ExecStartPre", config_parse_exec
, u
->service
.exec_command
+SERVICE_EXEC_START_PRE
, "Service" },
1508 { "ExecStart", config_parse_exec
, u
->service
.exec_command
+SERVICE_EXEC_START
, "Service" },
1509 { "ExecStartPost", config_parse_exec
, u
->service
.exec_command
+SERVICE_EXEC_START_POST
, "Service" },
1510 { "ExecReload", config_parse_exec
, u
->service
.exec_command
+SERVICE_EXEC_RELOAD
, "Service" },
1511 { "ExecStop", config_parse_exec
, u
->service
.exec_command
+SERVICE_EXEC_STOP
, "Service" },
1512 { "ExecStopPost", config_parse_exec
, u
->service
.exec_command
+SERVICE_EXEC_STOP_POST
, "Service" },
1513 { "RestartSec", config_parse_usec
, &u
->service
.restart_usec
, "Service" },
1514 { "TimeoutSec", config_parse_usec
, &u
->service
.timeout_usec
, "Service" },
1515 { "Type", config_parse_service_type
, &u
->service
.type
, "Service" },
1516 { "Restart", config_parse_service_restart
, &u
->service
.restart
, "Service" },
1517 { "PermissionsStartOnly", config_parse_bool
, &u
->service
.permissions_start_only
, "Service" },
1518 { "RootDirectoryStartOnly", config_parse_bool
, &u
->service
.root_directory_start_only
, "Service" },
1519 { "ValidNoProcess", config_parse_bool
, &u
->service
.valid_no_process
, "Service" },
1520 { "SysVStartPriority", config_parse_sysv_priority
, &u
->service
.sysv_start_priority
, "Service" },
1521 { "KillMode", config_parse_kill_mode
, &u
->service
.kill_mode
, "Service" },
1522 { "NonBlocking", config_parse_bool
, &u
->service
.exec_context
.non_blocking
, "Service" },
1523 { "BusName", config_parse_string_printf
, &u
->service
.bus_name
, "Service" },
1524 { "NotifyAccess", config_parse_notify_access
, &u
->service
.notify_access
, "Service" },
1525 EXEC_CONTEXT_CONFIG_ITEMS(u
->service
.exec_context
, "Service"),
1527 { "ListenStream", config_parse_listen
, &u
->socket
, "Socket" },
1528 { "ListenDatagram", config_parse_listen
, &u
->socket
, "Socket" },
1529 { "ListenSequentialPacket", config_parse_listen
, &u
->socket
, "Socket" },
1530 { "ListenFIFO", config_parse_listen
, &u
->socket
, "Socket" },
1531 { "BindIPv6Only", config_parse_socket_bind
, &u
->socket
, "Socket" },
1532 { "Backlog", config_parse_unsigned
, &u
->socket
.backlog
, "Socket" },
1533 { "BindToDevice", config_parse_bindtodevice
, &u
->socket
, "Socket" },
1534 { "ExecStartPre", config_parse_exec
, u
->socket
.exec_command
+SOCKET_EXEC_START_PRE
, "Socket" },
1535 { "ExecStartPost", config_parse_exec
, u
->socket
.exec_command
+SOCKET_EXEC_START_POST
, "Socket" },
1536 { "ExecStopPre", config_parse_exec
, u
->socket
.exec_command
+SOCKET_EXEC_STOP_PRE
, "Socket" },
1537 { "ExecStopPost", config_parse_exec
, u
->socket
.exec_command
+SOCKET_EXEC_STOP_POST
, "Socket" },
1538 { "TimeoutSec", config_parse_usec
, &u
->socket
.timeout_usec
, "Socket" },
1539 { "DirectoryMode", config_parse_mode
, &u
->socket
.directory_mode
, "Socket" },
1540 { "SocketMode", config_parse_mode
, &u
->socket
.socket_mode
, "Socket" },
1541 { "KillMode", config_parse_kill_mode
, &u
->socket
.kill_mode
, "Socket" },
1542 { "Accept", config_parse_bool
, &u
->socket
.accept
, "Socket" },
1543 { "MaxConnections", config_parse_unsigned
, &u
->socket
.max_connections
, "Socket" },
1544 { "KeepAlive", config_parse_bool
, &u
->socket
.keep_alive
, "Socket" },
1545 { "Priority", config_parse_int
, &u
->socket
.priority
, "Socket" },
1546 { "ReceiveBuffer", config_parse_size
, &u
->socket
.receive_buffer
, "Socket" },
1547 { "SendBuffer", config_parse_size
, &u
->socket
.send_buffer
, "Socket" },
1548 { "IPTOS", config_parse_ip_tos
, &u
->socket
.ip_tos
, "Socket" },
1549 { "IPTTL", config_parse_int
, &u
->socket
.ip_ttl
, "Socket" },
1550 { "Mark", config_parse_int
, &u
->socket
.mark
, "Socket" },
1551 { "PipeSize", config_parse_size
, &u
->socket
.pipe_size
, "Socket" },
1552 { "FreeBind", config_parse_bool
, &u
->socket
.free_bind
, "Socket" },
1553 EXEC_CONTEXT_CONFIG_ITEMS(u
->socket
.exec_context
, "Socket"),
1555 { "What", config_parse_string
, &u
->mount
.parameters_fragment
.what
, "Mount" },
1556 { "Where", config_parse_path
, &u
->mount
.where
, "Mount" },
1557 { "Options", config_parse_string
, &u
->mount
.parameters_fragment
.options
, "Mount" },
1558 { "Type", config_parse_string
, &u
->mount
.parameters_fragment
.fstype
, "Mount" },
1559 { "TimeoutSec", config_parse_usec
, &u
->mount
.timeout_usec
, "Mount" },
1560 { "KillMode", config_parse_kill_mode
, &u
->mount
.kill_mode
, "Mount" },
1561 { "DirectoryMode", config_parse_mode
, &u
->mount
.directory_mode
, "Mount" },
1562 EXEC_CONTEXT_CONFIG_ITEMS(u
->mount
.exec_context
, "Mount"),
1564 { "Where", config_parse_path
, &u
->automount
.where
, "Automount" },
1565 { "DirectoryMode", config_parse_mode
, &u
->automount
.directory_mode
, "Automount" },
1567 { "What", config_parse_path
, &u
->swap
.parameters_fragment
.what
, "Swap" },
1568 { "Priority", config_parse_int
, &u
->swap
.parameters_fragment
.priority
, "Swap" },
1570 { "OnActiveSec", config_parse_timer
, &u
->timer
, "Timer" },
1571 { "OnBootSec", config_parse_timer
, &u
->timer
, "Timer" },
1572 { "OnStartupSec", config_parse_timer
, &u
->timer
, "Timer" },
1573 { "OnUnitActiveSec", config_parse_timer
, &u
->timer
, "Timer" },
1574 { "OnUnitInactiveSec", config_parse_timer
, &u
->timer
, "Timer" },
1575 { "Unit", config_parse_timer_unit
, &u
->timer
, "Timer" },
1577 { "PathExists", config_parse_path_spec
, &u
->path
, "Path" },
1578 { "PathChanged", config_parse_path_spec
, &u
->path
, "Path" },
1579 { "DirectoryNotEmpty", config_parse_path_spec
, &u
->path
, "Path" },
1580 { "Unit", config_parse_path_unit
, &u
->path
, "Path" },
1582 /* The [Install] section is ignored here. */
1583 { "Alias", NULL
, NULL
, "Install" },
1584 { "WantedBy", NULL
, NULL
, "Install" },
1585 { "Also", NULL
, NULL
, "Install" },
1587 { NULL
, NULL
, NULL
, NULL
}
1590 #undef EXEC_CONTEXT_CONFIG_ITEMS
1592 const char *sections
[4];
1596 char *filename
= NULL
, *id
= NULL
;
1600 /* Dirty dirty hack. */
1601 dump_items((FILE*) path
, items
);
1608 sections
[0] = "Unit";
1609 sections
[1] = section_table
[u
->meta
.type
];
1610 sections
[2] = "Install";
1613 if (!(symlink_names
= set_new(string_hash_func
, string_compare_func
)))
1616 if (path_is_absolute(path
)) {
1618 if (!(filename
= strdup(path
))) {
1623 if ((r
= open_follow(&filename
, &f
, symlink_names
, &id
)) < 0) {
1634 STRV_FOREACH(p
, u
->meta
.manager
->lookup_paths
.unit_path
) {
1636 /* Instead of opening the path right away, we manually
1637 * follow all symlinks and add their name to our unit
1638 * name set while doing so */
1639 if (!(filename
= path_make_absolute(path
, *p
))) {
1644 if ((r
= open_follow(&filename
, &f
, symlink_names
, &id
)) < 0) {
1653 /* Empty the symlink names for the next run */
1654 while ((sn
= set_steal_first(symlink_names
)))
1670 if ((r
= merge_by_names(&merged
, symlink_names
, id
)) < 0)
1674 u
->meta
.load_state
= UNIT_MERGED
;
1679 /* Now, parse the file contents */
1680 if ((r
= config_parse(filename
, f
, sections
, items
, false, u
)) < 0)
1683 free(u
->meta
.fragment_path
);
1684 u
->meta
.fragment_path
= filename
;
1687 u
->meta
.load_state
= UNIT_LOADED
;
1691 set_free_free(symlink_names
);
1700 int unit_load_fragment(Unit
*u
) {
1705 if (u
->meta
.fragment_path
) {
1707 if ((r
= load_from_path(u
, u
->meta
.fragment_path
)) < 0)
1714 /* Try to find the unit under its id */
1715 if ((r
= load_from_path(u
, u
->meta
.id
)) < 0)
1718 /* Try to find an alias we can load this with */
1719 if (u
->meta
.load_state
== UNIT_STUB
)
1720 SET_FOREACH(t
, u
->meta
.names
, i
) {
1722 if (t
== u
->meta
.id
)
1725 if ((r
= load_from_path(u
, t
)) < 0)
1728 if (u
->meta
.load_state
!= UNIT_STUB
)
1732 /* Now, follow the same logic, but look for a template */
1733 if (u
->meta
.load_state
== UNIT_STUB
&& u
->meta
.instance
) {
1736 if (!(k
= unit_name_template(u
->meta
.id
)))
1739 r
= load_from_path(u
, k
);
1745 if (u
->meta
.load_state
== UNIT_STUB
)
1746 SET_FOREACH(t
, u
->meta
.names
, i
) {
1748 if (t
== u
->meta
.id
)
1751 if (!(k
= unit_name_template(t
)))
1754 r
= load_from_path(u
, k
);
1760 if (u
->meta
.load_state
!= UNIT_STUB
)
1769 void unit_dump_config_items(FILE *f
) {
1770 /* OK, this wins a prize for extreme ugliness. */
1772 load_from_path(NULL
, (const void*) f
);