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"
46 #define DEFINE_CONFIG_PARSE_ENUM(function,name,type,msg) \
47 static int function( \
48 const char *filename, \
50 const char *section, \
63 if ((x = name##_from_string(rvalue)) < 0) { \
64 log_error("[%s:%u] " msg ": %s", filename, line, rvalue); \
73 static int config_parse_deps(
82 UnitDependency d
= PTR_TO_UINT(data
);
92 FOREACH_WORD(w
, l
, rvalue
, state
) {
96 if (!(t
= strndup(w
, l
)))
99 k
= unit_name_printf(u
, t
);
105 r
= unit_add_dependency_by_name(u
, d
, k
, NULL
, true);
115 static int config_parse_names(
116 const char *filename
,
134 FOREACH_WORD(w
, l
, rvalue
, state
) {
138 if (!(t
= strndup(w
, l
)))
141 k
= unit_name_printf(u
, t
);
147 r
= unit_merge_by_name(u
, k
);
157 static int config_parse_description(
158 const char *filename
,
174 if (!(k
= unit_full_printf(u
, rvalue
)))
177 free(u
->meta
.description
);
180 u
->meta
.description
= k
;
183 u
->meta
.description
= NULL
;
189 static int config_parse_listen(
190 const char *filename
,
209 if (!(p
= new0(SocketPort
, 1)))
212 if (streq(lvalue
, "ListenFIFO")) {
213 p
->type
= SOCKET_FIFO
;
215 if (!(p
->path
= strdup(rvalue
))) {
220 path_kill_slashes(p
->path
);
222 p
->type
= SOCKET_SOCKET
;
224 if ((r
= socket_address_parse(&p
->address
, rvalue
)) < 0) {
225 log_error("[%s:%u] Failed to parse address value: %s", filename
, line
, rvalue
);
230 if (streq(lvalue
, "ListenStream"))
231 p
->address
.type
= SOCK_STREAM
;
232 else if (streq(lvalue
, "ListenDatagram"))
233 p
->address
.type
= SOCK_DGRAM
;
235 assert(streq(lvalue
, "ListenSequentialPacket"));
236 p
->address
.type
= SOCK_SEQPACKET
;
239 if (socket_address_family(&p
->address
) != AF_LOCAL
&& p
->address
.type
== SOCK_SEQPACKET
) {
241 return -EPROTONOSUPPORT
;
246 LIST_PREPEND(SocketPort
, port
, s
->ports
, p
);
251 static int config_parse_socket_bind(
252 const char *filename
,
261 SocketAddressBindIPv6Only b
;
270 if ((b
= socket_address_bind_ipv6_only_from_string(rvalue
)) < 0) {
273 if ((r
= parse_boolean(rvalue
)) < 0) {
274 log_error("[%s:%u] Failed to parse bind IPv6 only value: %s", filename
, line
, rvalue
);
278 s
->bind_ipv6_only
= r
? SOCKET_ADDRESS_IPV6_ONLY
: SOCKET_ADDRESS_BOTH
;
280 s
->bind_ipv6_only
= b
;
285 static int config_parse_nice(
286 const char *filename
,
294 ExecContext
*c
= data
;
302 if ((r
= safe_atoi(rvalue
, &priority
)) < 0) {
303 log_error("[%s:%u] Failed to parse nice priority: %s", filename
, line
, rvalue
);
307 if (priority
< PRIO_MIN
|| priority
>= PRIO_MAX
) {
308 log_error("[%s:%u] Nice priority out of range: %s", filename
, line
, rvalue
);
318 static int config_parse_oom_adjust(
319 const char *filename
,
327 ExecContext
*c
= data
;
335 if ((r
= safe_atoi(rvalue
, &oa
)) < 0) {
336 log_error("[%s:%u] Failed to parse OOM adjust value: %s", filename
, line
, rvalue
);
340 if (oa
< OOM_DISABLE
|| oa
> OOM_ADJUST_MAX
) {
341 log_error("[%s:%u] OOM adjust value out of range: %s", filename
, line
, rvalue
);
346 c
->oom_adjust_set
= true;
351 static int config_parse_mode(
352 const char *filename
,
370 l
= strtol(rvalue
, &x
, 8);
371 if (!x
|| *x
|| errno
) {
372 log_error("[%s:%u] Failed to parse mode value: %s", filename
, line
, rvalue
);
373 return errno
? -errno
: -EINVAL
;
376 if (l
< 0000 || l
> 07777) {
377 log_error("[%s:%u] mode value out of range: %s", filename
, line
, rvalue
);
385 static int config_parse_exec(
386 const char *filename
,
394 ExecCommand
**e
= data
, *nce
= NULL
;
399 char *state
, *path
= NULL
;
400 bool honour_argv0
, write_to_path
;
407 /* We accept an absolute path as first argument, or
408 * alternatively an absolute prefixed with @ to allow
409 * overriding of argv[0]. */
411 honour_argv0
= rvalue
[0] == '@';
413 if (rvalue
[honour_argv0
? 1 : 0] != '/') {
414 log_error("[%s:%u] Invalid executable path in command line: %s", filename
, line
, rvalue
);
419 FOREACH_WORD_QUOTED(w
, l
, rvalue
, state
)
422 if (!(n
= new(char*, k
+ (honour_argv0
? 0 : 1))))
426 write_to_path
= honour_argv0
;
427 FOREACH_WORD_QUOTED(w
, l
, rvalue
, state
) {
429 if (!(path
= strndup(w
+1, l
-1)))
431 write_to_path
= false;
433 if (!(n
[k
++] = strndup(w
, l
)))
441 log_error("[%s:%u] Invalid command line: %s", filename
, line
, rvalue
);
447 if (!(path
= strdup(n
[0])))
450 assert(path_is_absolute(path
));
452 if (!(nce
= new0(ExecCommand
, 1)))
458 path_kill_slashes(nce
->path
);
460 exec_command_append_list(e
, nce
);
473 static int config_parse_usec(
474 const char *filename
,
490 if ((r
= parse_usec(rvalue
, usec
)) < 0) {
491 log_error("[%s:%u] Failed to parse time value: %s", filename
, line
, rvalue
);
498 DEFINE_CONFIG_PARSE_ENUM(config_parse_service_type
, service_type
, ServiceType
, "Failed to parse service type");
499 DEFINE_CONFIG_PARSE_ENUM(config_parse_service_restart
, service_restart
, ServiceRestart
, "Failed to parse service restart specifier");
501 static int config_parse_bindtodevice(
502 const char *filename
,
518 if (rvalue
[0] && !streq(rvalue
, "*")) {
519 if (!(n
= strdup(rvalue
)))
524 free(s
->bind_to_device
);
525 s
->bind_to_device
= n
;
530 DEFINE_CONFIG_PARSE_ENUM(config_parse_output
, exec_output
, ExecOutput
, "Failed to parse output specifier");
531 DEFINE_CONFIG_PARSE_ENUM(config_parse_input
, exec_input
, ExecInput
, "Failed to parse input specifier");
533 static int config_parse_facility(
534 const char *filename
,
550 if ((x
= log_facility_from_string(rvalue
)) < 0) {
551 log_error("[%s:%u] Failed to parse log facility: %s", filename
, line
, rvalue
);
555 *o
= LOG_MAKEPRI(x
, LOG_PRI(*o
));
560 static int config_parse_level(
561 const char *filename
,
577 if ((x
= log_level_from_string(rvalue
)) < 0) {
578 log_error("[%s:%u] Failed to parse log level: %s", filename
, line
, rvalue
);
582 *o
= LOG_MAKEPRI(LOG_FAC(*o
), x
);
586 static int config_parse_io_class(
587 const char *filename
,
595 ExecContext
*c
= data
;
603 if ((x
= ioprio_class_from_string(rvalue
)) < 0) {
604 log_error("[%s:%u] Failed to parse IO scheduling class: %s", filename
, line
, rvalue
);
608 c
->ioprio
= IOPRIO_PRIO_VALUE(x
, IOPRIO_PRIO_DATA(c
->ioprio
));
609 c
->ioprio_set
= true;
614 static int config_parse_io_priority(
615 const char *filename
,
623 ExecContext
*c
= data
;
631 if (safe_atoi(rvalue
, &i
) < 0 || i
< 0 || i
>= IOPRIO_BE_NR
) {
632 log_error("[%s:%u] Failed to parse io priority: %s", filename
, line
, rvalue
);
636 c
->ioprio
= IOPRIO_PRIO_VALUE(IOPRIO_PRIO_CLASS(c
->ioprio
), i
);
637 c
->ioprio_set
= true;
642 static int config_parse_cpu_sched_policy(
643 const char *filename
,
652 ExecContext
*c
= data
;
660 if ((x
= sched_policy_from_string(rvalue
)) < 0) {
661 log_error("[%s:%u] Failed to parse CPU scheduling policy: %s", filename
, line
, rvalue
);
665 c
->cpu_sched_policy
= x
;
666 c
->cpu_sched_set
= true;
671 static int config_parse_cpu_sched_prio(
672 const char *filename
,
680 ExecContext
*c
= data
;
688 /* On Linux RR/FIFO have the same range */
689 if (safe_atoi(rvalue
, &i
) < 0 || i
< sched_get_priority_min(SCHED_RR
) || i
> sched_get_priority_max(SCHED_RR
)) {
690 log_error("[%s:%u] Failed to parse CPU scheduling priority: %s", filename
, line
, rvalue
);
694 c
->cpu_sched_priority
= i
;
695 c
->cpu_sched_set
= true;
700 static int config_parse_cpu_affinity(
701 const char *filename
,
709 ExecContext
*c
= data
;
719 FOREACH_WORD(w
, l
, rvalue
, state
) {
724 if (!(t
= strndup(w
, l
)))
727 r
= safe_atou(t
, &cpu
);
730 if (r
< 0 || cpu
>= CPU_SETSIZE
) {
731 log_error("[%s:%u] Failed to parse CPU affinity: %s", filename
, line
, rvalue
);
735 CPU_SET(cpu
, &c
->cpu_affinity
);
738 c
->cpu_affinity_set
= true;
743 static int config_parse_capabilities(
744 const char *filename
,
752 ExecContext
*c
= data
;
760 if (!(cap
= cap_from_text(rvalue
))) {
764 log_error("[%s:%u] Failed to parse capabilities: %s", filename
, line
, rvalue
);
769 cap_free(c
->capabilities
);
770 c
->capabilities
= cap
;
775 static int config_parse_secure_bits(
776 const char *filename
,
784 ExecContext
*c
= data
;
794 FOREACH_WORD(w
, l
, rvalue
, state
) {
795 if (first_word(w
, "keep-caps"))
796 c
->secure_bits
|= SECURE_KEEP_CAPS
;
797 else if (first_word(w
, "keep-caps-locked"))
798 c
->secure_bits
|= SECURE_KEEP_CAPS_LOCKED
;
799 else if (first_word(w
, "no-setuid-fixup"))
800 c
->secure_bits
|= SECURE_NO_SETUID_FIXUP
;
801 else if (first_word(w
, "no-setuid-fixup-locked"))
802 c
->secure_bits
|= SECURE_NO_SETUID_FIXUP_LOCKED
;
803 else if (first_word(w
, "noroot"))
804 c
->secure_bits
|= SECURE_NOROOT
;
805 else if (first_word(w
, "noroot-locked"))
806 c
->secure_bits
|= SECURE_NOROOT_LOCKED
;
808 log_error("[%s:%u] Failed to parse secure bits: %s", filename
, line
, rvalue
);
816 static int config_parse_bounding_set(
817 const char *filename
,
825 ExecContext
*c
= data
;
835 FOREACH_WORD(w
, l
, rvalue
, state
) {
840 if (!(t
= strndup(w
, l
)))
843 r
= cap_from_name(t
, &cap
);
847 log_error("[%s:%u] Failed to parse capability bounding set: %s", filename
, line
, rvalue
);
851 c
->capability_bounding_set_drop
|= 1 << cap
;
857 static int config_parse_timer_slack_ns(
858 const char *filename
,
866 ExecContext
*c
= data
;
875 if ((r
= safe_atolu(rvalue
, &u
)) < 0) {
876 log_error("[%s:%u] Failed to parse time slack value: %s", filename
, line
, rvalue
);
880 c
->timer_slack_ns
= u
;
885 static int config_parse_limit(
886 const char *filename
,
894 struct rlimit
**rl
= data
;
895 unsigned long long u
;
903 if ((r
= safe_atollu(rvalue
, &u
)) < 0) {
904 log_error("[%s:%u] Failed to parse resource value: %s", filename
, line
, rvalue
);
909 if (!(*rl
= new(struct rlimit
, 1)))
912 (*rl
)->rlim_cur
= (*rl
)->rlim_max
= (rlim_t
) u
;
916 static int config_parse_cgroup(
917 const char *filename
,
930 FOREACH_WORD(w
, l
, rvalue
, state
) {
934 if (!(t
= strndup(w
, l
)))
937 r
= unit_add_cgroup_from_text(u
, t
);
947 static int config_parse_sysv_priority(
948 const char *filename
,
956 int *priority
= data
;
964 if ((r
= safe_atoi(rvalue
, &i
)) < 0 || i
< 0) {
965 log_error("[%s:%u] Failed to parse SysV start priority: %s", filename
, line
, rvalue
);
973 DEFINE_CONFIG_PARSE_ENUM(config_parse_kill_mode
, kill_mode
, KillMode
, "Failed to parse kill mode");
975 static int config_parse_mount_flags(
976 const char *filename
,
984 ExecContext
*c
= data
;
988 unsigned long flags
= 0;
995 FOREACH_WORD(w
, l
, rvalue
, state
) {
996 if (strncmp(w
, "shared", l
) == 0)
998 else if (strncmp(w
, "slave", l
) == 0)
1000 else if (strncmp(w
, "private", l
) == 0)
1001 flags
|= MS_PRIVATE
;
1003 log_error("[%s:%u] Failed to parse mount flags: %s", filename
, line
, rvalue
);
1008 c
->mount_flags
= flags
;
1012 static int config_parse_timer(
1013 const char *filename
,
1015 const char *section
,
1032 if ((b
= timer_base_from_string(lvalue
)) < 0) {
1033 log_error("[%s:%u] Failed to parse timer base: %s", filename
, line
, lvalue
);
1037 if ((r
= parse_usec(rvalue
, &u
)) < 0) {
1038 log_error("[%s:%u] Failed to parse timer value: %s", filename
, line
, rvalue
);
1042 if (!(v
= new0(TimerValue
, 1)))
1048 LIST_PREPEND(TimerValue
, value
, t
->values
, v
);
1053 static int config_parse_timer_unit(
1054 const char *filename
,
1056 const char *section
,
1065 if (endswith(rvalue
, ".timer")) {
1066 log_error("[%s:%u] Unit cannot be of type timer: %s", filename
, line
, rvalue
);
1070 if ((r
= manager_load_unit(t
->meta
.manager
, rvalue
, NULL
, &t
->unit
)) < 0) {
1071 log_error("[%s:%u] Failed to load unit: %s", filename
, line
, rvalue
);
1078 static int config_parse_path_spec(
1079 const char *filename
,
1081 const char *section
,
1096 if ((b
= path_type_from_string(lvalue
)) < 0) {
1097 log_error("[%s:%u] Failed to parse path type: %s", filename
, line
, lvalue
);
1101 if (!path_is_absolute(rvalue
)) {
1102 log_error("[%s:%u] Path is not absolute: %s", filename
, line
, rvalue
);
1106 if (!(s
= new0(PathSpec
, 1)))
1109 if (!(s
->path
= strdup(rvalue
))) {
1114 path_kill_slashes(s
->path
);
1119 LIST_PREPEND(PathSpec
, spec
, p
->specs
, s
);
1124 static int config_parse_path_unit(
1125 const char *filename
,
1127 const char *section
,
1136 if (endswith(rvalue
, ".path")) {
1137 log_error("[%s:%u] Unit cannot be of type path: %s", filename
, line
, rvalue
);
1141 if ((r
= manager_load_unit(t
->meta
.manager
, rvalue
, NULL
, &t
->unit
)) < 0) {
1142 log_error("[%s:%u] Failed to load unit: %s", filename
, line
, rvalue
);
1149 static int config_parse_env_file(
1150 const char *filename
,
1152 const char *section
,
1167 if (!(f
= fopen(rvalue
, "re"))) {
1168 log_error("[%s:%u] Failed to open environment file '%s': %m", filename
, line
, rvalue
);
1173 char l
[LINE_MAX
], *p
;
1176 if (!fgets(l
, sizeof(l
), f
)) {
1181 log_error("[%s:%u] Failed to read environment file '%s': %m", filename
, line
, rvalue
);
1190 if (strchr(COMMENTS
, *p
))
1193 t
= strv_env_set(*env
, p
);
1207 DEFINE_CONFIG_PARSE_ENUM(config_parse_notify_access
, notify_access
, NotifyAccess
, "Failed to parse notify access specifier");
1209 #define FOLLOW_MAX 8
1211 static int open_follow(char **filename
, FILE **_f
, Set
*names
, char **_final
) {
1222 /* This will update the filename pointer if the loaded file is
1223 * reached by a symlink. The old string will be freed. */
1226 char *target
, *name
;
1228 if (c
++ >= FOLLOW_MAX
)
1231 path_kill_slashes(*filename
);
1233 /* Add the file name we are currently looking at to
1234 * the names of this unit */
1235 name
= file_name_from_path(*filename
);
1236 if (!(id
= set_get(names
, name
))) {
1238 if (!(id
= strdup(name
)))
1241 if ((r
= set_put(names
, id
)) < 0) {
1247 /* Try to open the file name, but don't if its a symlink */
1248 if ((fd
= open(*filename
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
)) >= 0)
1254 /* Hmm, so this is a symlink. Let's read the name, and follow it manually */
1255 if ((r
= readlink_and_make_absolute(*filename
, &target
)) < 0)
1262 if (!(f
= fdopen(fd
, "r"))) {
1264 close_nointr_nofail(fd
);
1273 static int merge_by_names(Unit
**u
, Set
*names
, const char *id
) {
1281 /* Let's try to add in all symlink names we found */
1282 while ((k
= set_steal_first(names
))) {
1284 /* First try to merge in the other name into our
1286 if ((r
= unit_merge_by_name(*u
, k
)) < 0) {
1289 /* Hmm, we couldn't merge the other unit into
1290 * ours? Then let's try it the other way
1293 other
= manager_get_unit((*u
)->meta
.manager
, k
);
1297 if ((r
= unit_merge(other
, *u
)) >= 0) {
1299 return merge_by_names(u
, names
, NULL
);
1306 unit_choose_id(*u
, id
);
1314 static void dump_items(FILE *f
, const ConfigItem
*items
) {
1315 const ConfigItem
*i
;
1316 const char *prev_section
= NULL
;
1317 bool not_first
= false;
1320 ConfigParserCallback callback
;
1323 { config_parse_int
, "INTEGER" },
1324 { config_parse_unsigned
, "UNSIGNED" },
1325 { config_parse_size
, "SIZE" },
1326 { config_parse_bool
, "BOOLEAN" },
1327 { config_parse_string
, "STRING" },
1328 { config_parse_path
, "PATH" },
1329 { config_parse_strv
, "STRING [...]" },
1330 { config_parse_nice
, "NICE" },
1331 { config_parse_oom_adjust
, "OOMADJUST" },
1332 { config_parse_io_class
, "IOCLASS" },
1333 { config_parse_io_priority
, "IOPRIORITY" },
1334 { config_parse_cpu_sched_policy
, "CPUSCHEDPOLICY" },
1335 { config_parse_cpu_sched_prio
, "CPUSCHEDPRIO" },
1336 { config_parse_cpu_affinity
, "CPUAFFINITY" },
1337 { config_parse_mode
, "MODE" },
1338 { config_parse_env_file
, "FILE" },
1339 { config_parse_output
, "OUTPUT" },
1340 { config_parse_input
, "INPUT" },
1341 { config_parse_facility
, "FACILITY" },
1342 { config_parse_level
, "LEVEL" },
1343 { config_parse_capabilities
, "CAPABILITIES" },
1344 { config_parse_secure_bits
, "SECUREBITS" },
1345 { config_parse_bounding_set
, "BOUNDINGSET" },
1346 { config_parse_timer_slack_ns
, "TIMERSLACK" },
1347 { config_parse_limit
, "LIMIT" },
1348 { config_parse_cgroup
, "CGROUP [...]" },
1349 { config_parse_deps
, "UNIT [...]" },
1350 { config_parse_names
, "UNIT [...]" },
1351 { config_parse_exec
, "PATH [ARGUMENT [...]]" },
1352 { config_parse_service_type
, "SERVICETYPE" },
1353 { config_parse_service_restart
, "SERVICERESTART" },
1354 { config_parse_sysv_priority
, "SYSVPRIORITY" },
1355 { config_parse_kill_mode
, "KILLMODE" },
1356 { config_parse_listen
, "SOCKET [...]" },
1357 { config_parse_socket_bind
, "SOCKETBIND" },
1358 { config_parse_bindtodevice
, "NETWORKINTERFACE" },
1359 { config_parse_usec
, "SECONDS" },
1360 { config_parse_path_strv
, "PATH [...]" },
1361 { config_parse_mount_flags
, "MOUNTFLAG [...]" },
1362 { config_parse_description
, "DESCRIPTION" },
1363 { config_parse_timer
, "TIMER" },
1364 { config_parse_timer_unit
, "NAME" },
1365 { config_parse_path_spec
, "PATH" },
1366 { config_parse_path_unit
, "UNIT" },
1367 { config_parse_notify_access
, "ACCESS" }
1373 for (i
= items
; i
->lvalue
; i
++) {
1375 const char *rvalue
= "OTHER";
1377 if (!streq_ptr(i
->section
, prev_section
)) {
1383 fprintf(f
, "[%s]\n", i
->section
);
1384 prev_section
= i
->section
;
1387 for (j
= 0; j
< ELEMENTSOF(table
); j
++)
1388 if (i
->parse
== table
[j
].callback
) {
1389 rvalue
= table
[j
].rvalue
;
1393 fprintf(f
, "%s=%s\n", i
->lvalue
, rvalue
);
1397 static int load_from_path(Unit
*u
, const char *path
) {
1399 static const char* const section_table
[_UNIT_TYPE_MAX
] = {
1400 [UNIT_SERVICE
] = "Service",
1401 [UNIT_TIMER
] = "Timer",
1402 [UNIT_SOCKET
] = "Socket",
1403 [UNIT_TARGET
] = "Target",
1404 [UNIT_DEVICE
] = "Device",
1405 [UNIT_MOUNT
] = "Mount",
1406 [UNIT_AUTOMOUNT
] = "Automount",
1407 [UNIT_SNAPSHOT
] = "Snapshot",
1408 [UNIT_SWAP
] = "Swap",
1409 [UNIT_PATH
] = "Path"
1412 #define EXEC_CONTEXT_CONFIG_ITEMS(context, section) \
1413 { "WorkingDirectory", config_parse_path, &(context).working_directory, section }, \
1414 { "RootDirectory", config_parse_path, &(context).root_directory, section }, \
1415 { "User", config_parse_string, &(context).user, section }, \
1416 { "Group", config_parse_string, &(context).group, section }, \
1417 { "SupplementaryGroups", config_parse_strv, &(context).supplementary_groups, section }, \
1418 { "Nice", config_parse_nice, &(context), section }, \
1419 { "OOMAdjust", config_parse_oom_adjust, &(context), section }, \
1420 { "IOSchedulingClass", config_parse_io_class, &(context), section }, \
1421 { "IOSchedulingPriority", config_parse_io_priority, &(context), section }, \
1422 { "CPUSchedulingPolicy", config_parse_cpu_sched_policy,&(context), section }, \
1423 { "CPUSchedulingPriority", config_parse_cpu_sched_prio, &(context), section }, \
1424 { "CPUSchedulingResetOnFork", config_parse_bool, &(context).cpu_sched_reset_on_fork, section }, \
1425 { "CPUAffinity", config_parse_cpu_affinity, &(context), section }, \
1426 { "UMask", config_parse_mode, &(context).umask, section }, \
1427 { "Environment", config_parse_strv, &(context).environment, section }, \
1428 { "EnvironmentFile", config_parse_env_file, &(context).environment, section }, \
1429 { "StandardInput", config_parse_input, &(context).std_input, section }, \
1430 { "StandardOutput", config_parse_output, &(context).std_output, section }, \
1431 { "StandardError", config_parse_output, &(context).std_error, section }, \
1432 { "TTYPath", config_parse_path, &(context).tty_path, section }, \
1433 { "SyslogIdentifier", config_parse_string, &(context).syslog_identifier, section }, \
1434 { "SyslogFacility", config_parse_facility, &(context).syslog_priority, section }, \
1435 { "SyslogLevel", config_parse_level, &(context).syslog_priority, section }, \
1436 { "SyslogNoPrefix", config_parse_bool, &(context).syslog_no_prefix, section }, \
1437 { "Capabilities", config_parse_capabilities, &(context), section }, \
1438 { "SecureBits", config_parse_secure_bits, &(context), section }, \
1439 { "CapabilityBoundingSetDrop", config_parse_bounding_set, &(context), section }, \
1440 { "TimerSlackNS", config_parse_timer_slack_ns, &(context), section }, \
1441 { "LimitCPU", config_parse_limit, &(context).rlimit[RLIMIT_CPU], section }, \
1442 { "LimitFSIZE", config_parse_limit, &(context).rlimit[RLIMIT_FSIZE], section }, \
1443 { "LimitDATA", config_parse_limit, &(context).rlimit[RLIMIT_DATA], section }, \
1444 { "LimitSTACK", config_parse_limit, &(context).rlimit[RLIMIT_STACK], section }, \
1445 { "LimitCORE", config_parse_limit, &(context).rlimit[RLIMIT_CORE], section }, \
1446 { "LimitRSS", config_parse_limit, &(context).rlimit[RLIMIT_RSS], section }, \
1447 { "LimitNOFILE", config_parse_limit, &(context).rlimit[RLIMIT_NOFILE], section }, \
1448 { "LimitAS", config_parse_limit, &(context).rlimit[RLIMIT_AS], section }, \
1449 { "LimitNPROC", config_parse_limit, &(context).rlimit[RLIMIT_NPROC], section }, \
1450 { "LimitMEMLOCK", config_parse_limit, &(context).rlimit[RLIMIT_MEMLOCK], section }, \
1451 { "LimitLOCKS", config_parse_limit, &(context).rlimit[RLIMIT_LOCKS], section }, \
1452 { "LimitSIGPENDING", config_parse_limit, &(context).rlimit[RLIMIT_SIGPENDING], section }, \
1453 { "LimitMSGQUEUE", config_parse_limit, &(context).rlimit[RLIMIT_MSGQUEUE], section }, \
1454 { "LimitNICE", config_parse_limit, &(context).rlimit[RLIMIT_NICE], section }, \
1455 { "LimitRTPRIO", config_parse_limit, &(context).rlimit[RLIMIT_RTPRIO], section }, \
1456 { "LimitRTTIME", config_parse_limit, &(context).rlimit[RLIMIT_RTTIME], section }, \
1457 { "ControlGroup", config_parse_cgroup, u, section }, \
1458 { "ReadWriteDirectories", config_parse_path_strv, &(context).read_write_dirs, section }, \
1459 { "ReadOnlyDirectories", config_parse_path_strv, &(context).read_only_dirs, section }, \
1460 { "InaccessibleDirectories",config_parse_path_strv, &(context).inaccessible_dirs, section }, \
1461 { "PrivateTmp", config_parse_bool, &(context).private_tmp, section }, \
1462 { "MountFlags", config_parse_mount_flags, &(context), section }, \
1463 { "TCPWrapName", config_parse_string, &(context).tcpwrap_name, section }, \
1464 { "PAMName", config_parse_string, &(context).pam_name, section }
1466 const ConfigItem items
[] = {
1467 { "Names", config_parse_names
, u
, "Unit" },
1468 { "Description", config_parse_description
, u
, "Unit" },
1469 { "Requires", config_parse_deps
, UINT_TO_PTR(UNIT_REQUIRES
), "Unit" },
1470 { "RequiresOverridable", config_parse_deps
, UINT_TO_PTR(UNIT_REQUIRES_OVERRIDABLE
), "Unit" },
1471 { "Requisite", config_parse_deps
, UINT_TO_PTR(UNIT_REQUISITE
), "Unit" },
1472 { "RequisiteOverridable", config_parse_deps
, UINT_TO_PTR(UNIT_REQUISITE_OVERRIDABLE
), "Unit" },
1473 { "Wants", config_parse_deps
, UINT_TO_PTR(UNIT_WANTS
), "Unit" },
1474 { "Conflicts", config_parse_deps
, UINT_TO_PTR(UNIT_CONFLICTS
), "Unit" },
1475 { "Before", config_parse_deps
, UINT_TO_PTR(UNIT_BEFORE
), "Unit" },
1476 { "After", config_parse_deps
, UINT_TO_PTR(UNIT_AFTER
), "Unit" },
1477 { "RecursiveStop", config_parse_bool
, &u
->meta
.recursive_stop
, "Unit" },
1478 { "StopWhenUnneeded", config_parse_bool
, &u
->meta
.stop_when_unneeded
, "Unit" },
1479 { "OnlyByDependency", config_parse_bool
, &u
->meta
.only_by_dependency
, "Unit" },
1481 { "PIDFile", config_parse_path
, &u
->service
.pid_file
, "Service" },
1482 { "ExecStartPre", config_parse_exec
, u
->service
.exec_command
+SERVICE_EXEC_START_PRE
, "Service" },
1483 { "ExecStart", config_parse_exec
, u
->service
.exec_command
+SERVICE_EXEC_START
, "Service" },
1484 { "ExecStartPost", config_parse_exec
, u
->service
.exec_command
+SERVICE_EXEC_START_POST
, "Service" },
1485 { "ExecReload", config_parse_exec
, u
->service
.exec_command
+SERVICE_EXEC_RELOAD
, "Service" },
1486 { "ExecStop", config_parse_exec
, u
->service
.exec_command
+SERVICE_EXEC_STOP
, "Service" },
1487 { "ExecStopPost", config_parse_exec
, u
->service
.exec_command
+SERVICE_EXEC_STOP_POST
, "Service" },
1488 { "RestartSec", config_parse_usec
, &u
->service
.restart_usec
, "Service" },
1489 { "TimeoutSec", config_parse_usec
, &u
->service
.timeout_usec
, "Service" },
1490 { "Type", config_parse_service_type
, &u
->service
.type
, "Service" },
1491 { "Restart", config_parse_service_restart
, &u
->service
.restart
, "Service" },
1492 { "PermissionsStartOnly", config_parse_bool
, &u
->service
.permissions_start_only
, "Service" },
1493 { "RootDirectoryStartOnly", config_parse_bool
, &u
->service
.root_directory_start_only
, "Service" },
1494 { "ValidNoProcess", config_parse_bool
, &u
->service
.valid_no_process
, "Service" },
1495 { "SysVStartPriority", config_parse_sysv_priority
, &u
->service
.sysv_start_priority
, "Service" },
1496 { "KillMode", config_parse_kill_mode
, &u
->service
.kill_mode
, "Service" },
1497 { "NonBlocking", config_parse_bool
, &u
->service
.exec_context
.non_blocking
, "Service" },
1498 { "BusName", config_parse_string
, &u
->service
.bus_name
, "Service" },
1499 { "NotifyAccess", config_parse_notify_access
, &u
->service
.notify_access
, "Service" },
1500 EXEC_CONTEXT_CONFIG_ITEMS(u
->service
.exec_context
, "Service"),
1502 { "ListenStream", config_parse_listen
, &u
->socket
, "Socket" },
1503 { "ListenDatagram", config_parse_listen
, &u
->socket
, "Socket" },
1504 { "ListenSequentialPacket", config_parse_listen
, &u
->socket
, "Socket" },
1505 { "ListenFIFO", config_parse_listen
, &u
->socket
, "Socket" },
1506 { "BindIPv6Only", config_parse_socket_bind
, &u
->socket
, "Socket" },
1507 { "Backlog", config_parse_unsigned
, &u
->socket
.backlog
, "Socket" },
1508 { "BindToDevice", config_parse_bindtodevice
, &u
->socket
, "Socket" },
1509 { "ExecStartPre", config_parse_exec
, u
->socket
.exec_command
+SOCKET_EXEC_START_PRE
, "Socket" },
1510 { "ExecStartPost", config_parse_exec
, u
->socket
.exec_command
+SOCKET_EXEC_START_POST
, "Socket" },
1511 { "ExecStopPre", config_parse_exec
, u
->socket
.exec_command
+SOCKET_EXEC_STOP_PRE
, "Socket" },
1512 { "ExecStopPost", config_parse_exec
, u
->socket
.exec_command
+SOCKET_EXEC_STOP_POST
, "Socket" },
1513 { "TimeoutSec", config_parse_usec
, &u
->socket
.timeout_usec
, "Socket" },
1514 { "DirectoryMode", config_parse_mode
, &u
->socket
.directory_mode
, "Socket" },
1515 { "SocketMode", config_parse_mode
, &u
->socket
.socket_mode
, "Socket" },
1516 { "KillMode", config_parse_kill_mode
, &u
->socket
.kill_mode
, "Socket" },
1517 { "Accept", config_parse_bool
, &u
->socket
.accept
, "Socket" },
1518 EXEC_CONTEXT_CONFIG_ITEMS(u
->socket
.exec_context
, "Socket"),
1520 { "What", config_parse_string
, &u
->mount
.parameters_fragment
.what
, "Mount" },
1521 { "Where", config_parse_path
, &u
->mount
.where
, "Mount" },
1522 { "Options", config_parse_string
, &u
->mount
.parameters_fragment
.options
, "Mount" },
1523 { "Type", config_parse_string
, &u
->mount
.parameters_fragment
.fstype
, "Mount" },
1524 { "TimeoutSec", config_parse_usec
, &u
->mount
.timeout_usec
, "Mount" },
1525 { "KillMode", config_parse_kill_mode
, &u
->mount
.kill_mode
, "Mount" },
1526 EXEC_CONTEXT_CONFIG_ITEMS(u
->mount
.exec_context
, "Mount"),
1528 { "Where", config_parse_path
, &u
->automount
.where
, "Automount" },
1530 { "What", config_parse_path
, &u
->swap
.parameters_fragment
.what
, "Swap" },
1531 { "Priority", config_parse_int
, &u
->swap
.parameters_fragment
.priority
, "Swap" },
1533 { "OnActive", config_parse_timer
, &u
->timer
, "Timer" },
1534 { "OnBoot", config_parse_timer
, &u
->timer
, "Timer" },
1535 { "OnStartup", config_parse_timer
, &u
->timer
, "Timer" },
1536 { "OnUnitActive", config_parse_timer
, &u
->timer
, "Timer" },
1537 { "OnUnitInactive", config_parse_timer
, &u
->timer
, "Timer" },
1538 { "Unit", config_parse_timer_unit
, &u
->timer
, "Timer" },
1540 { "PathExists", config_parse_path_spec
, &u
->path
, "Path" },
1541 { "PathChanged", config_parse_path_spec
, &u
->path
, "Path" },
1542 { "DirectoryNotEmpty", config_parse_path_spec
, &u
->path
, "Path" },
1543 { "Unit", config_parse_path_unit
, &u
->path
, "Path" },
1545 /* The [Install] section is ignored here. */
1546 { "Alias", NULL
, NULL
, "Install" },
1547 { "WantedBy", NULL
, NULL
, "Install" },
1548 { "Also", NULL
, NULL
, "Install" },
1550 { NULL
, NULL
, NULL
, NULL
}
1553 #undef EXEC_CONTEXT_CONFIG_ITEMS
1555 const char *sections
[4];
1559 char *filename
= NULL
, *id
= NULL
;
1563 /* Dirty dirty hack. */
1564 dump_items((FILE*) path
, items
);
1571 sections
[0] = "Unit";
1572 sections
[1] = section_table
[u
->meta
.type
];
1573 sections
[2] = "Install";
1576 if (!(symlink_names
= set_new(string_hash_func
, string_compare_func
)))
1579 if (path_is_absolute(path
)) {
1581 if (!(filename
= strdup(path
))) {
1586 if ((r
= open_follow(&filename
, &f
, symlink_names
, &id
)) < 0) {
1597 STRV_FOREACH(p
, u
->meta
.manager
->lookup_paths
.unit_path
) {
1599 /* Instead of opening the path right away, we manually
1600 * follow all symlinks and add their name to our unit
1601 * name set while doing so */
1602 if (!(filename
= path_make_absolute(path
, *p
))) {
1607 if ((r
= open_follow(&filename
, &f
, symlink_names
, &id
)) < 0) {
1616 /* Empty the symlink names for the next run */
1617 while ((sn
= set_steal_first(symlink_names
)))
1633 if ((r
= merge_by_names(&merged
, symlink_names
, id
)) < 0)
1637 u
->meta
.load_state
= UNIT_MERGED
;
1642 /* Now, parse the file contents */
1643 if ((r
= config_parse(filename
, f
, sections
, items
, false, u
)) < 0)
1646 free(u
->meta
.fragment_path
);
1647 u
->meta
.fragment_path
= filename
;
1650 u
->meta
.load_state
= UNIT_LOADED
;
1654 set_free_free(symlink_names
);
1663 int unit_load_fragment(Unit
*u
) {
1668 if (u
->meta
.fragment_path
) {
1670 if ((r
= load_from_path(u
, u
->meta
.fragment_path
)) < 0)
1677 /* Try to find the unit under its id */
1678 if ((r
= load_from_path(u
, u
->meta
.id
)) < 0)
1681 /* Try to find an alias we can load this with */
1682 if (u
->meta
.load_state
== UNIT_STUB
)
1683 SET_FOREACH(t
, u
->meta
.names
, i
) {
1685 if (t
== u
->meta
.id
)
1688 if ((r
= load_from_path(u
, t
)) < 0)
1691 if (u
->meta
.load_state
!= UNIT_STUB
)
1695 /* Now, follow the same logic, but look for a template */
1696 if (u
->meta
.load_state
== UNIT_STUB
&& u
->meta
.instance
) {
1699 if (!(k
= unit_name_template(u
->meta
.id
)))
1702 r
= load_from_path(u
, k
);
1708 if (u
->meta
.load_state
== UNIT_STUB
)
1709 SET_FOREACH(t
, u
->meta
.names
, i
) {
1711 if (t
== u
->meta
.id
)
1714 if (!(k
= unit_name_template(t
)))
1717 r
= load_from_path(u
, k
);
1723 if (u
->meta
.load_state
!= UNIT_STUB
)
1732 void unit_dump_config_items(FILE *f
) {
1733 /* OK, this wins a prize for extreme ugliness. */
1735 load_from_path(NULL
, (const void*) f
);