1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright © 2012 Holger Hans Peter Freyther
15 #include <sys/resource.h>
19 #include "alloc-util.h"
20 #include "all-units.h"
21 #include "bus-error.h"
22 #include "bus-internal.h"
25 #include "capability-util.h"
27 #include "conf-parser.h"
28 #include "cpu-set-util.h"
30 #include "errno-list.h"
34 #include "hexdecoct.h"
37 #include "ip-protocol-list.h"
38 #include "journal-util.h"
39 #include "load-fragment.h"
42 #include "mountpoint-util.h"
43 #include "parse-util.h"
44 #include "path-util.h"
45 #include "process-util.h"
47 #include "seccomp-util.h"
49 #include "securebits-util.h"
50 #include "signal-util.h"
51 #include "stat-util.h"
52 #include "string-util.h"
54 #include "unit-name.h"
55 #include "unit-printf.h"
56 #include "user-util.h"
59 static int parse_socket_protocol(const char *s
) {
62 r
= parse_ip_protocol(s
);
65 if (!IN_SET(r
, IPPROTO_UDPLITE
, IPPROTO_SCTP
))
66 return -EPROTONOSUPPORT
;
71 DEFINE_CONFIG_PARSE(config_parse_socket_protocol
, parse_socket_protocol
, "Failed to parse socket protocol");
72 DEFINE_CONFIG_PARSE(config_parse_exec_secure_bits
, secure_bits_from_string
, "Failed to parse secure bits");
73 DEFINE_CONFIG_PARSE_ENUM(config_parse_collect_mode
, collect_mode
, CollectMode
, "Failed to parse garbage collection mode");
74 DEFINE_CONFIG_PARSE_ENUM(config_parse_device_policy
, cgroup_device_policy
, CGroupDevicePolicy
, "Failed to parse device policy");
75 DEFINE_CONFIG_PARSE_ENUM(config_parse_exec_keyring_mode
, exec_keyring_mode
, ExecKeyringMode
, "Failed to parse keyring mode");
76 DEFINE_CONFIG_PARSE_ENUM(config_parse_exec_utmp_mode
, exec_utmp_mode
, ExecUtmpMode
, "Failed to parse utmp mode");
77 DEFINE_CONFIG_PARSE_ENUM(config_parse_job_mode
, job_mode
, JobMode
, "Failed to parse job mode");
78 DEFINE_CONFIG_PARSE_ENUM(config_parse_kill_mode
, kill_mode
, KillMode
, "Failed to parse kill mode");
79 DEFINE_CONFIG_PARSE_ENUM(config_parse_notify_access
, notify_access
, NotifyAccess
, "Failed to parse notify access specifier");
80 DEFINE_CONFIG_PARSE_ENUM(config_parse_protect_home
, protect_home
, ProtectHome
, "Failed to parse protect home value");
81 DEFINE_CONFIG_PARSE_ENUM(config_parse_protect_system
, protect_system
, ProtectSystem
, "Failed to parse protect system value");
82 DEFINE_CONFIG_PARSE_ENUM(config_parse_runtime_preserve_mode
, exec_preserve_mode
, ExecPreserveMode
, "Failed to parse runtime directory preserve mode");
83 DEFINE_CONFIG_PARSE_ENUM(config_parse_service_type
, service_type
, ServiceType
, "Failed to parse service type");
84 DEFINE_CONFIG_PARSE_ENUM(config_parse_service_restart
, service_restart
, ServiceRestart
, "Failed to parse service restart specifier");
85 DEFINE_CONFIG_PARSE_ENUM(config_parse_socket_bind
, socket_address_bind_ipv6_only_or_bool
, SocketAddressBindIPv6Only
, "Failed to parse bind IPv6 only value");
86 DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT(config_parse_ip_tos
, ip_tos
, int, -1, "Failed to parse IP TOS value");
87 DEFINE_CONFIG_PARSE_PTR(config_parse_blockio_weight
, cg_blkio_weight_parse
, uint64_t, "Invalid block IO weight");
88 DEFINE_CONFIG_PARSE_PTR(config_parse_cg_weight
, cg_weight_parse
, uint64_t, "Invalid weight");
89 DEFINE_CONFIG_PARSE_PTR(config_parse_cpu_shares
, cg_cpu_shares_parse
, uint64_t, "Invalid CPU shares");
90 DEFINE_CONFIG_PARSE_PTR(config_parse_exec_mount_flags
, mount_propagation_flags_from_string
, unsigned long, "Failed to parse mount flag");
92 int config_parse_unit_deps(
97 unsigned section_line
,
104 UnitDependency d
= ltype
;
114 _cleanup_free_
char *word
= NULL
, *k
= NULL
;
117 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_RETAIN_ESCAPE
);
123 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid syntax, ignoring: %s", rvalue
);
127 r
= unit_name_printf(u
, word
, &k
);
129 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in '%s', ignoring: %m", word
);
133 r
= unit_add_dependency_by_name(u
, d
, k
, true, UNIT_DEPENDENCY_FILE
);
135 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to add dependency on %s, ignoring: %m", k
);
141 int config_parse_obsolete_unit_deps(
143 const char *filename
,
146 unsigned section_line
,
153 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
154 "Unit dependency type %s= is obsolete, replacing by %s=, please update your unit file", lvalue
, unit_dependency_to_string(ltype
));
156 return config_parse_unit_deps(unit
, filename
, line
, section
, section_line
, lvalue
, ltype
, rvalue
, data
, userdata
);
159 int config_parse_unit_string_printf(
161 const char *filename
,
164 unsigned section_line
,
171 _cleanup_free_
char *k
= NULL
;
180 r
= unit_full_printf(u
, rvalue
, &k
);
182 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue
);
186 return config_parse_string(unit
, filename
, line
, section
, section_line
, lvalue
, ltype
, k
, data
, userdata
);
189 int config_parse_unit_strv_printf(
191 const char *filename
,
194 unsigned section_line
,
202 _cleanup_free_
char *k
= NULL
;
210 r
= unit_full_printf(u
, rvalue
, &k
);
212 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue
);
216 return config_parse_strv(unit
, filename
, line
, section
, section_line
, lvalue
, ltype
, k
, data
, userdata
);
219 int config_parse_unit_path_printf(
221 const char *filename
,
224 unsigned section_line
,
231 _cleanup_free_
char *k
= NULL
;
241 /* Let's not bother with anything that is too long */
242 if (strlen(rvalue
) >= PATH_MAX
) {
243 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
244 "%s value too long%s.",
245 lvalue
, fatal
? "" : ", ignoring");
246 return fatal
? -ENAMETOOLONG
: 0;
249 r
= unit_full_printf(u
, rvalue
, &k
);
251 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
252 "Failed to resolve unit specifiers in '%s'%s: %m",
253 rvalue
, fatal
? "" : ", ignoring");
254 return fatal
? -ENOEXEC
: 0;
257 return config_parse_path(unit
, filename
, line
, section
, section_line
, lvalue
, ltype
, k
, data
, userdata
);
260 int config_parse_unit_path_strv_printf(
262 const char *filename
,
265 unsigned section_line
,
282 if (isempty(rvalue
)) {
288 _cleanup_free_
char *word
= NULL
, *k
= NULL
;
290 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
);
296 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
297 "Invalid syntax, ignoring: %s", rvalue
);
301 r
= unit_full_printf(u
, word
, &k
);
303 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
304 "Failed to resolve unit specifiers in '%s', ignoring: %m", word
);
308 r
= path_simplify_and_warn(k
, PATH_CHECK_ABSOLUTE
, unit
, filename
, line
, lvalue
);
319 int config_parse_socket_listen(const char *unit
,
320 const char *filename
,
323 unsigned section_line
,
330 _cleanup_free_ SocketPort
*p
= NULL
;
342 if (isempty(rvalue
)) {
343 /* An empty assignment removes all ports */
344 socket_free_ports(s
);
348 p
= new0(SocketPort
, 1);
352 if (ltype
!= SOCKET_SOCKET
) {
353 _cleanup_free_
char *k
= NULL
;
355 r
= unit_full_printf(UNIT(s
), rvalue
, &k
);
357 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue
);
361 r
= path_simplify_and_warn(k
, PATH_CHECK_ABSOLUTE
, unit
, filename
, line
, lvalue
);
365 free_and_replace(p
->path
, k
);
368 } else if (streq(lvalue
, "ListenNetlink")) {
369 _cleanup_free_
char *k
= NULL
;
371 r
= unit_full_printf(UNIT(s
), rvalue
, &k
);
373 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue
);
377 r
= socket_address_parse_netlink(&p
->address
, k
);
379 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse address value in '%s', ignoring: %m", k
);
383 p
->type
= SOCKET_SOCKET
;
386 _cleanup_free_
char *k
= NULL
;
388 r
= unit_full_printf(UNIT(s
), rvalue
, &k
);
390 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue
);
394 r
= socket_address_parse_and_warn(&p
->address
, k
);
396 if (r
!= -EAFNOSUPPORT
)
397 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse address value in '%s', ignoring: %m", k
);
401 if (streq(lvalue
, "ListenStream"))
402 p
->address
.type
= SOCK_STREAM
;
403 else if (streq(lvalue
, "ListenDatagram"))
404 p
->address
.type
= SOCK_DGRAM
;
406 assert(streq(lvalue
, "ListenSequentialPacket"));
407 p
->address
.type
= SOCK_SEQPACKET
;
410 if (socket_address_family(&p
->address
) != AF_LOCAL
&& p
->address
.type
== SOCK_SEQPACKET
) {
411 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Address family not supported, ignoring: %s", rvalue
);
415 p
->type
= SOCKET_SOCKET
;
419 p
->auxiliary_fds
= NULL
;
420 p
->n_auxiliary_fds
= 0;
423 LIST_FIND_TAIL(port
, s
->ports
, tail
);
424 LIST_INSERT_AFTER(port
, s
->ports
, tail
, p
);
431 int config_parse_exec_nice(
433 const char *filename
,
436 unsigned section_line
,
443 ExecContext
*c
= data
;
451 if (isempty(rvalue
)) {
456 r
= parse_nice(rvalue
, &priority
);
459 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Nice priority out of range, ignoring: %s", rvalue
);
461 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse nice priority '%s', ignoring: %m", rvalue
);
471 int config_parse_exec_oom_score_adjust(
473 const char *filename
,
476 unsigned section_line
,
483 ExecContext
*c
= data
;
491 if (isempty(rvalue
)) {
492 c
->oom_score_adjust_set
= false;
496 r
= parse_oom_score_adjust(rvalue
, &oa
);
499 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "OOM score adjust value out of range, ignoring: %s", rvalue
);
501 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse the OOM score adjust value '%s', ignoring: %m", rvalue
);
505 c
->oom_score_adjust
= oa
;
506 c
->oom_score_adjust_set
= true;
511 int config_parse_exec(
513 const char *filename
,
516 unsigned section_line
,
523 ExecCommand
**e
= data
;
535 rvalue
+= strspn(rvalue
, WHITESPACE
);
537 if (isempty(rvalue
)) {
538 /* An empty assignment resets the list */
539 *e
= exec_command_free_list(*e
);
545 _cleanup_free_
char *path
= NULL
, *firstword
= NULL
;
546 ExecCommandFlags flags
= 0;
547 bool ignore
= false, separate_argv0
= false;
548 _cleanup_free_ ExecCommand
*nce
= NULL
;
549 _cleanup_strv_free_
char **n
= NULL
;
550 size_t nlen
= 0, nbufsize
= 0;
555 r
= extract_first_word_and_warn(&p
, &firstword
, NULL
, EXTRACT_QUOTES
|EXTRACT_CUNESCAPE
, unit
, filename
, line
, rvalue
);
561 /* We accept an absolute path as first argument. If it's prefixed with - and the path doesn't
562 * exist, we ignore it instead of erroring out; if it's prefixed with @, we allow overriding of
563 * argv[0]; if it's prefixed with +, it will be run with full privileges and no sandboxing; if
564 * it's prefixed with '!' we apply sandboxing, but do not change user/group credentials; if
565 * it's prefixed with '!!', then we apply user/group credentials if the kernel supports ambient
566 * capabilities -- if it doesn't we don't apply the credentials themselves, but do apply most
567 * other sandboxing, with some special exceptions for changing UID.
569 * The idea is that '!!' may be used to write services that can take benefit of systemd's
570 * UID/GID dropping if the kernel supports ambient creds, but provide an automatic fallback to
571 * privilege dropping within the daemon if the kernel does not offer that. */
573 if (*f
== '-' && !(flags
& EXEC_COMMAND_IGNORE_FAILURE
)) {
574 flags
|= EXEC_COMMAND_IGNORE_FAILURE
;
576 } else if (*f
== '@' && !separate_argv0
)
577 separate_argv0
= true;
578 else if (*f
== '+' && !(flags
& (EXEC_COMMAND_FULLY_PRIVILEGED
|EXEC_COMMAND_NO_SETUID
|EXEC_COMMAND_AMBIENT_MAGIC
)))
579 flags
|= EXEC_COMMAND_FULLY_PRIVILEGED
;
580 else if (*f
== '!' && !(flags
& (EXEC_COMMAND_FULLY_PRIVILEGED
|EXEC_COMMAND_NO_SETUID
|EXEC_COMMAND_AMBIENT_MAGIC
)))
581 flags
|= EXEC_COMMAND_NO_SETUID
;
582 else if (*f
== '!' && !(flags
& (EXEC_COMMAND_FULLY_PRIVILEGED
|EXEC_COMMAND_AMBIENT_MAGIC
))) {
583 flags
&= ~EXEC_COMMAND_NO_SETUID
;
584 flags
|= EXEC_COMMAND_AMBIENT_MAGIC
;
590 r
= unit_full_printf(u
, f
, &path
);
592 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
593 "Failed to resolve unit specifiers in '%s'%s: %m",
594 f
, ignore
? ", ignoring" : "");
595 return ignore
? 0 : -ENOEXEC
;
599 /* First word is either "-" or "@" with no command. */
600 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
601 "Empty path in command line%s: '%s'",
602 ignore
? ", ignoring" : "", rvalue
);
603 return ignore
? 0 : -ENOEXEC
;
605 if (!string_is_safe(path
)) {
606 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
607 "Executable name contains special characters%s: %s",
608 ignore
? ", ignoring" : "", path
);
609 return ignore
? 0 : -ENOEXEC
;
611 if (endswith(path
, "/")) {
612 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
613 "Executable path specifies a directory%s: %s",
614 ignore
? ", ignoring" : "", path
);
615 return ignore
? 0 : -ENOEXEC
;
618 if (!path_is_absolute(path
)) {
622 if (!filename_is_valid(path
)) {
623 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
624 "Neither a valid executable name nor an absolute path%s: %s",
625 ignore
? ", ignoring" : "", path
);
626 return ignore
? 0 : -ENOEXEC
;
629 /* Resolve a single-component name to a full path */
630 NULSTR_FOREACH(prefix
, DEFAULT_PATH_NULSTR
) {
631 _cleanup_free_
char *fullpath
= NULL
;
633 fullpath
= strjoin(prefix
, "/", path
);
637 if (access(fullpath
, F_OK
) >= 0) {
638 free_and_replace(path
, fullpath
);
645 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
646 "Executable \"%s\" not found in path \"%s\"%s",
647 path
, DEFAULT_PATH
, ignore
? ", ignoring" : "");
648 return ignore
? 0 : -ENOEXEC
;
652 if (!separate_argv0
) {
655 if (!GREEDY_REALLOC(n
, nbufsize
, nlen
+ 2))
665 path_simplify(path
, false);
667 while (!isempty(p
)) {
668 _cleanup_free_
char *word
= NULL
, *resolved
= NULL
;
670 /* Check explicitly for an unquoted semicolon as
671 * command separator token. */
672 if (p
[0] == ';' && (!p
[1] || strchr(WHITESPACE
, p
[1]))) {
674 p
+= strspn(p
, WHITESPACE
);
679 /* Check for \; explicitly, to not confuse it with \\; or "\;" or "\\;" etc.
680 * extract_first_word() would return the same for all of those. */
681 if (p
[0] == '\\' && p
[1] == ';' && (!p
[2] || strchr(WHITESPACE
, p
[2]))) {
685 p
+= strspn(p
, WHITESPACE
);
687 if (!GREEDY_REALLOC(n
, nbufsize
, nlen
+ 2))
698 r
= extract_first_word_and_warn(&p
, &word
, NULL
, EXTRACT_QUOTES
|EXTRACT_CUNESCAPE
, unit
, filename
, line
, rvalue
);
702 return ignore
? 0 : -ENOEXEC
;
704 r
= unit_full_printf(u
, word
, &resolved
);
706 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
707 "Failed to resolve unit specifiers in %s%s: %m",
708 word
, ignore
? ", ignoring" : "");
709 return ignore
? 0 : -ENOEXEC
;
712 if (!GREEDY_REALLOC(n
, nbufsize
, nlen
+ 2))
715 n
[nlen
++] = TAKE_PTR(resolved
);
720 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
721 "Empty executable name or zeroeth argument%s: %s",
722 ignore
? ", ignoring" : "", rvalue
);
723 return ignore
? 0 : -ENOEXEC
;
726 nce
= new0(ExecCommand
, 1);
730 nce
->argv
= TAKE_PTR(n
);
731 nce
->path
= TAKE_PTR(path
);
734 exec_command_append_list(e
, nce
);
736 /* Do not _cleanup_free_ these. */
745 int config_parse_socket_bindtodevice(
747 const char *filename
,
750 unsigned section_line
,
764 if (isempty(rvalue
) || streq(rvalue
, "*")) {
765 s
->bind_to_device
= mfree(s
->bind_to_device
);
769 if (!ifname_valid(rvalue
)) {
770 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid interface name, ignoring: %s", rvalue
);
774 if (free_and_strdup(&s
->bind_to_device
, rvalue
) < 0)
780 int config_parse_exec_input(
782 const char *filename
,
785 unsigned section_line
,
792 ExecContext
*c
= data
;
803 n
= startswith(rvalue
, "fd:");
805 _cleanup_free_
char *resolved
= NULL
;
807 r
= unit_full_printf(u
, n
, &resolved
);
809 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in '%s': %m", n
);
811 if (isempty(resolved
))
812 resolved
= mfree(resolved
);
813 else if (!fdname_is_valid(resolved
)) {
814 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid file descriptor name: %s", resolved
);
818 free_and_replace(c
->stdio_fdname
[STDIN_FILENO
], resolved
);
820 ei
= EXEC_INPUT_NAMED_FD
;
822 } else if ((n
= startswith(rvalue
, "file:"))) {
823 _cleanup_free_
char *resolved
= NULL
;
825 r
= unit_full_printf(u
, n
, &resolved
);
827 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in '%s': %m", n
);
829 r
= path_simplify_and_warn(resolved
, PATH_CHECK_ABSOLUTE
| PATH_CHECK_FATAL
, unit
, filename
, line
, lvalue
);
833 free_and_replace(c
->stdio_file
[STDIN_FILENO
], resolved
);
835 ei
= EXEC_INPUT_FILE
;
838 ei
= exec_input_from_string(rvalue
);
840 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse input specifier, ignoring: %s", rvalue
);
849 int config_parse_exec_input_text(
851 const char *filename
,
854 unsigned section_line
,
861 _cleanup_free_
char *unescaped
= NULL
, *resolved
= NULL
;
862 ExecContext
*c
= data
;
873 if (isempty(rvalue
)) {
874 /* Reset if the empty string is assigned */
875 c
->stdin_data
= mfree(c
->stdin_data
);
876 c
->stdin_data_size
= 0;
880 r
= cunescape(rvalue
, 0, &unescaped
);
882 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to decode C escaped text '%s': %m", rvalue
);
884 r
= unit_full_printf(u
, unescaped
, &resolved
);
886 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in '%s': %m", unescaped
);
888 sz
= strlen(resolved
);
889 if (c
->stdin_data_size
+ sz
+ 1 < c
->stdin_data_size
|| /* check for overflow */
890 c
->stdin_data_size
+ sz
+ 1 > EXEC_STDIN_DATA_MAX
) {
891 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Standard input data too large (%zu), maximum of %zu permitted, ignoring.", c
->stdin_data_size
+ sz
, (size_t) EXEC_STDIN_DATA_MAX
);
895 p
= realloc(c
->stdin_data
, c
->stdin_data_size
+ sz
+ 1);
899 *((char*) mempcpy((char*) p
+ c
->stdin_data_size
, resolved
, sz
)) = '\n';
902 c
->stdin_data_size
+= sz
+ 1;
907 int config_parse_exec_input_data(
909 const char *filename
,
912 unsigned section_line
,
919 _cleanup_free_
void *p
= NULL
;
920 ExecContext
*c
= data
;
930 if (isempty(rvalue
)) {
931 /* Reset if the empty string is assigned */
932 c
->stdin_data
= mfree(c
->stdin_data
);
933 c
->stdin_data_size
= 0;
937 r
= unbase64mem(rvalue
, (size_t) -1, &p
, &sz
);
939 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to decode base64 data, ignoring: %s", rvalue
);
943 if (c
->stdin_data_size
+ sz
< c
->stdin_data_size
|| /* check for overflow */
944 c
->stdin_data_size
+ sz
> EXEC_STDIN_DATA_MAX
) {
945 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Standard input data too large (%zu), maximum of %zu permitted, ignoring.", c
->stdin_data_size
+ sz
, (size_t) EXEC_STDIN_DATA_MAX
);
949 q
= realloc(c
->stdin_data
, c
->stdin_data_size
+ sz
);
953 memcpy((uint8_t*) q
+ c
->stdin_data_size
, p
, sz
);
956 c
->stdin_data_size
+= sz
;
961 int config_parse_exec_output(
963 const char *filename
,
966 unsigned section_line
,
973 _cleanup_free_
char *resolved
= NULL
;
975 ExecContext
*c
= data
;
986 n
= startswith(rvalue
, "fd:");
988 r
= unit_full_printf(u
, n
, &resolved
);
990 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in %s: %m", n
);
992 if (isempty(resolved
))
993 resolved
= mfree(resolved
);
994 else if (!fdname_is_valid(resolved
)) {
995 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid file descriptor name: %s", resolved
);
999 eo
= EXEC_OUTPUT_NAMED_FD
;
1001 } else if ((n
= startswith(rvalue
, "file:"))) {
1003 r
= unit_full_printf(u
, n
, &resolved
);
1005 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in %s: %m", n
);
1007 r
= path_simplify_and_warn(resolved
, PATH_CHECK_ABSOLUTE
| PATH_CHECK_FATAL
, unit
, filename
, line
, lvalue
);
1011 eo
= EXEC_OUTPUT_FILE
;
1013 } else if ((n
= startswith(rvalue
, "append:"))) {
1015 r
= unit_full_printf(u
, n
, &resolved
);
1017 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in %s: %m", n
);
1019 r
= path_simplify_and_warn(resolved
, PATH_CHECK_ABSOLUTE
| PATH_CHECK_FATAL
, unit
, filename
, line
, lvalue
);
1023 eo
= EXEC_OUTPUT_FILE_APPEND
;
1025 eo
= exec_output_from_string(rvalue
);
1027 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse output specifier, ignoring: %s", rvalue
);
1032 if (streq(lvalue
, "StandardOutput")) {
1033 if (eo
== EXEC_OUTPUT_NAMED_FD
)
1034 free_and_replace(c
->stdio_fdname
[STDOUT_FILENO
], resolved
);
1036 free_and_replace(c
->stdio_file
[STDOUT_FILENO
], resolved
);
1041 assert(streq(lvalue
, "StandardError"));
1043 if (eo
== EXEC_OUTPUT_NAMED_FD
)
1044 free_and_replace(c
->stdio_fdname
[STDERR_FILENO
], resolved
);
1046 free_and_replace(c
->stdio_file
[STDERR_FILENO
], resolved
);
1054 int config_parse_exec_io_class(const char *unit
,
1055 const char *filename
,
1057 const char *section
,
1058 unsigned section_line
,
1065 ExecContext
*c
= data
;
1073 if (isempty(rvalue
)) {
1074 c
->ioprio_set
= false;
1075 c
->ioprio
= IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE
, 0);
1079 x
= ioprio_class_from_string(rvalue
);
1081 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse IO scheduling class, ignoring: %s", rvalue
);
1085 c
->ioprio
= IOPRIO_PRIO_VALUE(x
, IOPRIO_PRIO_DATA(c
->ioprio
));
1086 c
->ioprio_set
= true;
1091 int config_parse_exec_io_priority(const char *unit
,
1092 const char *filename
,
1094 const char *section
,
1095 unsigned section_line
,
1102 ExecContext
*c
= data
;
1110 if (isempty(rvalue
)) {
1111 c
->ioprio_set
= false;
1112 c
->ioprio
= IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE
, 0);
1116 r
= ioprio_parse_priority(rvalue
, &i
);
1118 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse IO priority, ignoring: %s", rvalue
);
1122 c
->ioprio
= IOPRIO_PRIO_VALUE(IOPRIO_PRIO_CLASS(c
->ioprio
), i
);
1123 c
->ioprio_set
= true;
1128 int config_parse_exec_cpu_sched_policy(const char *unit
,
1129 const char *filename
,
1131 const char *section
,
1132 unsigned section_line
,
1139 ExecContext
*c
= data
;
1147 if (isempty(rvalue
)) {
1148 c
->cpu_sched_set
= false;
1149 c
->cpu_sched_policy
= SCHED_OTHER
;
1150 c
->cpu_sched_priority
= 0;
1154 x
= sched_policy_from_string(rvalue
);
1156 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse CPU scheduling policy, ignoring: %s", rvalue
);
1160 c
->cpu_sched_policy
= x
;
1161 /* Moving to or from real-time policy? We need to adjust the priority */
1162 c
->cpu_sched_priority
= CLAMP(c
->cpu_sched_priority
, sched_get_priority_min(x
), sched_get_priority_max(x
));
1163 c
->cpu_sched_set
= true;
1168 int config_parse_exec_cpu_sched_prio(const char *unit
,
1169 const char *filename
,
1171 const char *section
,
1172 unsigned section_line
,
1179 ExecContext
*c
= data
;
1187 r
= safe_atoi(rvalue
, &i
);
1189 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse CPU scheduling priority, ignoring: %s", rvalue
);
1193 /* On Linux RR/FIFO range from 1 to 99 and OTHER/BATCH may only be 0 */
1194 min
= sched_get_priority_min(c
->cpu_sched_policy
);
1195 max
= sched_get_priority_max(c
->cpu_sched_policy
);
1197 if (i
< min
|| i
> max
) {
1198 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "CPU scheduling priority is out of range, ignoring: %s", rvalue
);
1202 c
->cpu_sched_priority
= i
;
1203 c
->cpu_sched_set
= true;
1208 int config_parse_exec_cpu_affinity(const char *unit
,
1209 const char *filename
,
1211 const char *section
,
1212 unsigned section_line
,
1219 ExecContext
*c
= data
;
1220 _cleanup_cpu_free_ cpu_set_t
*cpuset
= NULL
;
1228 ncpus
= parse_cpu_set_and_warn(rvalue
, &cpuset
, unit
, filename
, line
, lvalue
);
1233 /* An empty assignment resets the CPU list */
1234 c
->cpuset
= cpu_set_mfree(c
->cpuset
);
1235 c
->cpuset_ncpus
= 0;
1240 c
->cpuset
= TAKE_PTR(cpuset
);
1241 c
->cpuset_ncpus
= (unsigned) ncpus
;
1245 if (c
->cpuset_ncpus
< (unsigned) ncpus
) {
1246 CPU_OR_S(CPU_ALLOC_SIZE(c
->cpuset_ncpus
), cpuset
, c
->cpuset
, cpuset
);
1247 CPU_FREE(c
->cpuset
);
1248 c
->cpuset
= TAKE_PTR(cpuset
);
1249 c
->cpuset_ncpus
= (unsigned) ncpus
;
1253 CPU_OR_S(CPU_ALLOC_SIZE((unsigned) ncpus
), c
->cpuset
, c
->cpuset
, cpuset
);
1258 int config_parse_capability_set(
1260 const char *filename
,
1262 const char *section
,
1263 unsigned section_line
,
1270 uint64_t *capability_set
= data
;
1271 uint64_t sum
= 0, initial
= 0;
1272 bool invert
= false;
1280 if (rvalue
[0] == '~') {
1285 if (streq(lvalue
, "CapabilityBoundingSet"))
1286 initial
= CAP_ALL
; /* initialized to all bits on */
1287 /* else "AmbientCapabilities" initialized to all bits off */
1289 r
= capability_set_from_string(rvalue
, &sum
);
1291 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse %s= specifier '%s', ignoring: %m", lvalue
, rvalue
);
1295 if (sum
== 0 || *capability_set
== initial
)
1296 /* "", "~" or uninitialized data -> replace */
1297 *capability_set
= invert
? ~sum
: sum
;
1299 /* previous data -> merge */
1301 *capability_set
&= ~sum
;
1303 *capability_set
|= sum
;
1309 int config_parse_exec_selinux_context(
1311 const char *filename
,
1313 const char *section
,
1314 unsigned section_line
,
1321 ExecContext
*c
= data
;
1332 if (isempty(rvalue
)) {
1333 c
->selinux_context
= mfree(c
->selinux_context
);
1334 c
->selinux_context_ignore
= false;
1338 if (rvalue
[0] == '-') {
1344 r
= unit_full_printf(u
, rvalue
, &k
);
1346 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1347 "Failed to resolve unit specifiers in '%s'%s: %m",
1348 rvalue
, ignore
? ", ignoring" : "");
1349 return ignore
? 0 : -ENOEXEC
;
1352 free_and_replace(c
->selinux_context
, k
);
1353 c
->selinux_context_ignore
= ignore
;
1358 int config_parse_exec_apparmor_profile(
1360 const char *filename
,
1362 const char *section
,
1363 unsigned section_line
,
1370 ExecContext
*c
= data
;
1381 if (isempty(rvalue
)) {
1382 c
->apparmor_profile
= mfree(c
->apparmor_profile
);
1383 c
->apparmor_profile_ignore
= false;
1387 if (rvalue
[0] == '-') {
1393 r
= unit_full_printf(u
, rvalue
, &k
);
1395 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1396 "Failed to resolve unit specifiers in '%s'%s: %m",
1397 rvalue
, ignore
? ", ignoring" : "");
1398 return ignore
? 0 : -ENOEXEC
;
1401 free_and_replace(c
->apparmor_profile
, k
);
1402 c
->apparmor_profile_ignore
= ignore
;
1407 int config_parse_exec_smack_process_label(
1409 const char *filename
,
1411 const char *section
,
1412 unsigned section_line
,
1419 ExecContext
*c
= data
;
1430 if (isempty(rvalue
)) {
1431 c
->smack_process_label
= mfree(c
->smack_process_label
);
1432 c
->smack_process_label_ignore
= false;
1436 if (rvalue
[0] == '-') {
1442 r
= unit_full_printf(u
, rvalue
, &k
);
1444 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1445 "Failed to resolve unit specifiers in '%s'%s: %m",
1446 rvalue
, ignore
? ", ignoring" : "");
1447 return ignore
? 0 : -ENOEXEC
;
1450 free_and_replace(c
->smack_process_label
, k
);
1451 c
->smack_process_label_ignore
= ignore
;
1456 int config_parse_timer(const char *unit
,
1457 const char *filename
,
1459 const char *section
,
1460 unsigned section_line
,
1471 _cleanup_(calendar_spec_freep
) CalendarSpec
*c
= NULL
;
1473 _cleanup_free_
char *k
= NULL
;
1481 if (isempty(rvalue
)) {
1482 /* Empty assignment resets list */
1483 timer_free_values(t
);
1487 b
= timer_base_from_string(lvalue
);
1489 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse timer base, ignoring: %s", lvalue
);
1493 r
= unit_full_printf(u
, rvalue
, &k
);
1495 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue
);
1499 if (b
== TIMER_CALENDAR
) {
1500 if (calendar_spec_from_string(k
, &c
) < 0) {
1501 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse calendar specification, ignoring: %s", k
);
1505 if (parse_sec(k
, &usec
) < 0) {
1506 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse timer value, ignoring: %s", k
);
1510 v
= new0(TimerValue
, 1);
1516 v
->calendar_spec
= TAKE_PTR(c
);
1518 LIST_PREPEND(value
, t
->values
, v
);
1523 int config_parse_trigger_unit(
1525 const char *filename
,
1527 const char *section
,
1528 unsigned section_line
,
1535 _cleanup_free_
char *p
= NULL
;
1545 if (!hashmap_isempty(u
->dependencies
[UNIT_TRIGGERS
])) {
1546 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Multiple units to trigger specified, ignoring: %s", rvalue
);
1550 r
= unit_name_printf(u
, rvalue
, &p
);
1552 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue
);
1556 type
= unit_name_to_type(p
);
1558 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Unit type not valid, ignoring: %s", rvalue
);
1561 if (unit_has_name(u
, p
)) {
1562 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Units cannot trigger themselves, ignoring: %s", rvalue
);
1566 r
= unit_add_two_dependencies_by_name(u
, UNIT_BEFORE
, UNIT_TRIGGERS
, p
, true, UNIT_DEPENDENCY_FILE
);
1568 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to add trigger on %s, ignoring: %m", p
);
1575 int config_parse_path_spec(const char *unit
,
1576 const char *filename
,
1578 const char *section
,
1579 unsigned section_line
,
1589 _cleanup_free_
char *k
= NULL
;
1597 if (isempty(rvalue
)) {
1598 /* Empty assignment clears list */
1603 b
= path_type_from_string(lvalue
);
1605 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse path type, ignoring: %s", lvalue
);
1609 r
= unit_full_printf(UNIT(p
), rvalue
, &k
);
1611 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue
);
1615 r
= path_simplify_and_warn(k
, PATH_CHECK_ABSOLUTE
, unit
, filename
, line
, lvalue
);
1619 s
= new0(PathSpec
, 1);
1624 s
->path
= TAKE_PTR(k
);
1628 LIST_PREPEND(spec
, p
->specs
, s
);
1633 int config_parse_socket_service(
1635 const char *filename
,
1637 const char *section
,
1638 unsigned section_line
,
1645 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1646 _cleanup_free_
char *p
= NULL
;
1656 r
= unit_name_printf(UNIT(s
), rvalue
, &p
);
1658 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in %s: %m", rvalue
);
1662 if (!endswith(p
, ".service")) {
1663 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Unit must be of type service: %s", rvalue
);
1667 r
= manager_load_unit(UNIT(s
)->manager
, p
, NULL
, &error
, &x
);
1669 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to load unit %s: %s", rvalue
, bus_error_message(&error
, r
));
1673 unit_ref_set(&s
->service
, UNIT(s
), x
);
1678 int config_parse_fdname(
1680 const char *filename
,
1682 const char *section
,
1683 unsigned section_line
,
1690 _cleanup_free_
char *p
= NULL
;
1699 if (isempty(rvalue
)) {
1700 s
->fdname
= mfree(s
->fdname
);
1704 r
= unit_full_printf(UNIT(s
), rvalue
, &p
);
1706 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue
);
1710 if (!fdname_is_valid(p
)) {
1711 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid file descriptor name, ignoring: %s", p
);
1715 return free_and_replace(s
->fdname
, p
);
1718 int config_parse_service_sockets(
1720 const char *filename
,
1722 const char *section
,
1723 unsigned section_line
,
1741 _cleanup_free_
char *word
= NULL
, *k
= NULL
;
1743 r
= extract_first_word(&p
, &word
, NULL
, 0);
1749 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Trailing garbage in sockets, ignoring: %s", rvalue
);
1753 r
= unit_name_printf(UNIT(s
), word
, &k
);
1755 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in '%s', ignoring: %m", word
);
1759 if (!endswith(k
, ".socket")) {
1760 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Unit must be of type socket, ignoring: %s", k
);
1764 r
= unit_add_two_dependencies_by_name(UNIT(s
), UNIT_WANTS
, UNIT_AFTER
, k
, true, UNIT_DEPENDENCY_FILE
);
1766 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to add dependency on %s, ignoring: %m", k
);
1768 r
= unit_add_dependency_by_name(UNIT(s
), UNIT_TRIGGERED_BY
, k
, true, UNIT_DEPENDENCY_FILE
);
1770 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to add dependency on %s, ignoring: %m", k
);
1776 int config_parse_bus_name(
1778 const char *filename
,
1780 const char *section
,
1781 unsigned section_line
,
1788 _cleanup_free_
char *k
= NULL
;
1797 r
= unit_full_printf(u
, rvalue
, &k
);
1799 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue
);
1803 if (!service_name_is_valid(k
)) {
1804 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid bus name, ignoring: %s", k
);
1808 return config_parse_string(unit
, filename
, line
, section
, section_line
, lvalue
, ltype
, k
, data
, userdata
);
1811 int config_parse_service_timeout(
1813 const char *filename
,
1815 const char *section
,
1816 unsigned section_line
,
1823 Service
*s
= userdata
;
1832 /* This is called for two cases: TimeoutSec= and TimeoutStartSec=. */
1834 /* Traditionally, these options accepted 0 to disable the timeouts. However, a timeout of 0 suggests it happens
1835 * immediately, hence fix this to become USEC_INFINITY instead. This is in-line with how we internally handle
1836 * all other timeouts. */
1837 r
= parse_sec_fix_0(rvalue
, &usec
);
1839 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse %s= parameter, ignoring: %s", lvalue
, rvalue
);
1843 s
->start_timeout_defined
= true;
1844 s
->timeout_start_usec
= usec
;
1846 if (streq(lvalue
, "TimeoutSec"))
1847 s
->timeout_stop_usec
= usec
;
1852 int config_parse_sec_fix_0(
1854 const char *filename
,
1856 const char *section
,
1857 unsigned section_line
,
1864 usec_t
*usec
= data
;
1872 /* This is pretty much like config_parse_sec(), except that this treats a time of 0 as infinity, for
1873 * compatibility with older versions of systemd where 0 instead of infinity was used as indicator to turn off a
1876 r
= parse_sec_fix_0(rvalue
, usec
);
1878 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse %s= parameter, ignoring: %s", lvalue
, rvalue
);
1885 int config_parse_user_group(
1887 const char *filename
,
1889 const char *section
,
1890 unsigned section_line
,
1897 _cleanup_free_
char *k
= NULL
;
1907 if (isempty(rvalue
)) {
1908 *user
= mfree(*user
);
1912 r
= unit_full_printf(u
, rvalue
, &k
);
1914 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in %s: %m", rvalue
);
1918 if (!valid_user_group_name_or_id(k
)) {
1919 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid user/group name or numeric ID: %s", k
);
1923 return free_and_replace(*user
, k
);
1926 int config_parse_user_group_strv(
1928 const char *filename
,
1930 const char *section
,
1931 unsigned section_line
,
1938 char ***users
= data
;
1940 const char *p
= rvalue
;
1948 if (isempty(rvalue
)) {
1949 *users
= strv_free(*users
);
1954 _cleanup_free_
char *word
= NULL
, *k
= NULL
;
1956 r
= extract_first_word(&p
, &word
, NULL
, 0);
1962 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid syntax: %s", rvalue
);
1966 r
= unit_full_printf(u
, word
, &k
);
1968 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in %s: %m", word
);
1972 if (!valid_user_group_name_or_id(k
)) {
1973 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid user/group name or numeric ID: %s", k
);
1977 r
= strv_push(users
, k
);
1987 int config_parse_working_directory(
1989 const char *filename
,
1991 const char *section
,
1992 unsigned section_line
,
1999 ExecContext
*c
= data
;
2010 if (isempty(rvalue
)) {
2011 c
->working_directory_home
= false;
2012 c
->working_directory
= mfree(c
->working_directory
);
2016 if (rvalue
[0] == '-') {
2022 if (streq(rvalue
, "~")) {
2023 c
->working_directory_home
= true;
2024 c
->working_directory
= mfree(c
->working_directory
);
2026 _cleanup_free_
char *k
= NULL
;
2028 r
= unit_full_printf(u
, rvalue
, &k
);
2030 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
2031 "Failed to resolve unit specifiers in working directory path '%s'%s: %m",
2032 rvalue
, missing_ok
? ", ignoring" : "");
2033 return missing_ok
? 0 : -ENOEXEC
;
2036 r
= path_simplify_and_warn(k
, PATH_CHECK_ABSOLUTE
| (missing_ok
? 0 : PATH_CHECK_FATAL
), unit
, filename
, line
, lvalue
);
2038 return missing_ok
? 0 : -ENOEXEC
;
2040 c
->working_directory_home
= false;
2041 free_and_replace(c
->working_directory
, k
);
2044 c
->working_directory_missing_ok
= missing_ok
;
2048 int config_parse_unit_env_file(const char *unit
,
2049 const char *filename
,
2051 const char *section
,
2052 unsigned section_line
,
2061 _cleanup_free_
char *n
= NULL
;
2069 if (isempty(rvalue
)) {
2070 /* Empty assignment frees the list */
2071 *env
= strv_free(*env
);
2075 r
= unit_full_printf(u
, rvalue
, &n
);
2077 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue
);
2081 r
= path_simplify_and_warn(n
[0] == '-' ? n
+ 1 : n
, PATH_CHECK_ABSOLUTE
, unit
, filename
, line
, lvalue
);
2085 r
= strv_push(env
, n
);
2094 int config_parse_environ(
2096 const char *filename
,
2098 const char *section
,
2099 unsigned section_line
,
2116 if (isempty(rvalue
)) {
2117 /* Empty assignment resets the list */
2118 *env
= strv_free(*env
);
2122 for (p
= rvalue
;; ) {
2123 _cleanup_free_
char *word
= NULL
, *k
= NULL
;
2125 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_CUNESCAPE
|EXTRACT_QUOTES
);
2131 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
2132 "Invalid syntax, ignoring: %s", rvalue
);
2137 r
= unit_full_printf(u
, word
, &k
);
2139 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
2140 "Failed to resolve unit specifiers in %s, ignoring: %m", word
);
2146 if (!env_assignment_is_valid(k
)) {
2147 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
2148 "Invalid environment assignment, ignoring: %s", k
);
2152 r
= strv_env_replace(env
, k
);
2160 int config_parse_pass_environ(
2162 const char *filename
,
2164 const char *section
,
2165 unsigned section_line
,
2172 _cleanup_strv_free_
char **n
= NULL
;
2173 size_t nlen
= 0, nbufsize
= 0;
2174 char*** passenv
= data
;
2175 const char *p
= rvalue
;
2184 if (isempty(rvalue
)) {
2185 /* Empty assignment resets the list */
2186 *passenv
= strv_free(*passenv
);
2191 _cleanup_free_
char *word
= NULL
, *k
= NULL
;
2193 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
);
2199 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
2200 "Trailing garbage in %s, ignoring: %s", lvalue
, rvalue
);
2205 r
= unit_full_printf(u
, word
, &k
);
2207 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
2208 "Failed to resolve specifiers in %s, ignoring: %m", word
);
2214 if (!env_name_is_valid(k
)) {
2215 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
2216 "Invalid environment name for %s, ignoring: %s", lvalue
, k
);
2220 if (!GREEDY_REALLOC(n
, nbufsize
, nlen
+ 2))
2223 n
[nlen
++] = TAKE_PTR(k
);
2228 r
= strv_extend_strv(passenv
, n
, true);
2236 int config_parse_unset_environ(
2238 const char *filename
,
2240 const char *section
,
2241 unsigned section_line
,
2248 _cleanup_strv_free_
char **n
= NULL
;
2249 size_t nlen
= 0, nbufsize
= 0;
2250 char*** unsetenv
= data
;
2251 const char *p
= rvalue
;
2260 if (isempty(rvalue
)) {
2261 /* Empty assignment resets the list */
2262 *unsetenv
= strv_free(*unsetenv
);
2267 _cleanup_free_
char *word
= NULL
, *k
= NULL
;
2269 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_CUNESCAPE
|EXTRACT_QUOTES
);
2275 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
2276 "Trailing garbage in %s, ignoring: %s", lvalue
, rvalue
);
2281 r
= unit_full_printf(u
, word
, &k
);
2283 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
2284 "Failed to resolve unit specifiers in %s, ignoring: %m", word
);
2290 if (!env_assignment_is_valid(k
) && !env_name_is_valid(k
)) {
2291 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
2292 "Invalid environment name or assignment %s, ignoring: %s", lvalue
, k
);
2296 if (!GREEDY_REALLOC(n
, nbufsize
, nlen
+ 2))
2299 n
[nlen
++] = TAKE_PTR(k
);
2304 r
= strv_extend_strv(unsetenv
, n
, true);
2312 int config_parse_log_extra_fields(
2314 const char *filename
,
2316 const char *section
,
2317 unsigned section_line
,
2324 ExecContext
*c
= data
;
2326 const char *p
= rvalue
;
2334 if (isempty(rvalue
)) {
2335 exec_context_free_log_extra_fields(c
);
2340 _cleanup_free_
char *word
= NULL
, *k
= NULL
;
2344 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_CUNESCAPE
|EXTRACT_QUOTES
);
2350 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Invalid syntax, ignoring: %s", rvalue
);
2354 r
= unit_full_printf(u
, word
, &k
);
2356 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in %s, ignoring: %m", word
);
2360 eq
= strchr(k
, '=');
2362 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Log field lacks '=' character, ignoring: %s", k
);
2366 if (!journal_field_valid(k
, eq
-k
, false)) {
2367 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Log field name is invalid, ignoring: %s", k
);
2371 t
= reallocarray(c
->log_extra_fields
, c
->n_log_extra_fields
+1, sizeof(struct iovec
));
2375 c
->log_extra_fields
= t
;
2376 c
->log_extra_fields
[c
->n_log_extra_fields
++] = IOVEC_MAKE_STRING(k
);
2382 int config_parse_unit_condition_path(
2384 const char *filename
,
2386 const char *section
,
2387 unsigned section_line
,
2394 _cleanup_free_
char *p
= NULL
;
2395 Condition
**list
= data
, *c
;
2396 ConditionType t
= ltype
;
2397 bool trigger
, negate
;
2406 if (isempty(rvalue
)) {
2407 /* Empty assignment resets the list */
2408 *list
= condition_free_list(*list
);
2412 trigger
= rvalue
[0] == '|';
2416 negate
= rvalue
[0] == '!';
2420 r
= unit_full_printf(u
, rvalue
, &p
);
2422 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue
);
2426 r
= path_simplify_and_warn(p
, PATH_CHECK_ABSOLUTE
, unit
, filename
, line
, lvalue
);
2430 c
= condition_new(t
, p
, trigger
, negate
);
2434 LIST_PREPEND(conditions
, *list
, c
);
2438 int config_parse_unit_condition_string(
2440 const char *filename
,
2442 const char *section
,
2443 unsigned section_line
,
2450 _cleanup_free_
char *s
= NULL
;
2451 Condition
**list
= data
, *c
;
2452 ConditionType t
= ltype
;
2453 bool trigger
, negate
;
2462 if (isempty(rvalue
)) {
2463 /* Empty assignment resets the list */
2464 *list
= condition_free_list(*list
);
2468 trigger
= rvalue
[0] == '|';
2472 negate
= rvalue
[0] == '!';
2476 r
= unit_full_printf(u
, rvalue
, &s
);
2478 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue
);
2482 c
= condition_new(t
, s
, trigger
, negate
);
2486 LIST_PREPEND(conditions
, *list
, c
);
2490 int config_parse_unit_condition_null(
2492 const char *filename
,
2494 const char *section
,
2495 unsigned section_line
,
2502 Condition
**list
= data
, *c
;
2503 bool trigger
, negate
;
2511 if (isempty(rvalue
)) {
2512 /* Empty assignment resets the list */
2513 *list
= condition_free_list(*list
);
2517 trigger
= rvalue
[0] == '|';
2521 negate
= rvalue
[0] == '!';
2525 b
= parse_boolean(rvalue
);
2527 log_syntax(unit
, LOG_ERR
, filename
, line
, b
, "Failed to parse boolean value in condition, ignoring: %s", rvalue
);
2534 c
= condition_new(CONDITION_NULL
, NULL
, trigger
, negate
);
2538 LIST_PREPEND(conditions
, *list
, c
);
2542 int config_parse_unit_requires_mounts_for(
2544 const char *filename
,
2546 const char *section
,
2547 unsigned section_line
,
2554 const char *p
= rvalue
;
2564 _cleanup_free_
char *word
= NULL
, *resolved
= NULL
;
2566 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
);
2572 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
2573 "Invalid syntax, ignoring: %s", rvalue
);
2577 r
= unit_full_printf(u
, word
, &resolved
);
2579 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in '%s', ignoring: %m", word
);
2583 r
= path_simplify_and_warn(resolved
, PATH_CHECK_ABSOLUTE
, unit
, filename
, line
, lvalue
);
2587 r
= unit_require_mounts_for(u
, resolved
, UNIT_DEPENDENCY_FILE
);
2589 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to add required mount '%s', ignoring: %m", resolved
);
2595 int config_parse_documentation(const char *unit
,
2596 const char *filename
,
2598 const char *section
,
2599 unsigned section_line
,
2615 if (isempty(rvalue
)) {
2616 /* Empty assignment resets the list */
2617 u
->documentation
= strv_free(u
->documentation
);
2621 r
= config_parse_unit_strv_printf(unit
, filename
, line
, section
, section_line
, lvalue
, ltype
,
2622 rvalue
, data
, userdata
);
2626 for (a
= b
= u
->documentation
; a
&& *a
; a
++) {
2628 if (documentation_url_is_valid(*a
))
2631 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid URL, ignoring: %s", *a
);
2642 int config_parse_syscall_filter(
2644 const char *filename
,
2646 const char *section
,
2647 unsigned section_line
,
2654 ExecContext
*c
= data
;
2656 bool invert
= false;
2665 if (isempty(rvalue
)) {
2666 /* Empty assignment resets the list */
2667 c
->syscall_filter
= hashmap_free(c
->syscall_filter
);
2668 c
->syscall_whitelist
= false;
2672 if (rvalue
[0] == '~') {
2677 if (!c
->syscall_filter
) {
2678 c
->syscall_filter
= hashmap_new(NULL
);
2679 if (!c
->syscall_filter
)
2683 /* Allow everything but the ones listed */
2684 c
->syscall_whitelist
= false;
2686 /* Allow nothing but the ones listed */
2687 c
->syscall_whitelist
= true;
2689 /* Accept default syscalls if we are on a whitelist */
2690 r
= seccomp_parse_syscall_filter("@default", -1, c
->syscall_filter
, SECCOMP_PARSE_WHITELIST
);
2698 _cleanup_free_
char *word
= NULL
, *name
= NULL
;
2701 r
= extract_first_word(&p
, &word
, NULL
, 0);
2707 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Invalid syntax, ignoring: %s", rvalue
);
2711 r
= parse_syscall_and_errno(word
, &name
, &num
);
2713 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Failed to parse syscall:errno, ignoring: %s", word
);
2717 r
= seccomp_parse_syscall_filter_full(name
, num
, c
->syscall_filter
,
2718 SECCOMP_PARSE_LOG
|SECCOMP_PARSE_PERMISSIVE
|(invert
? SECCOMP_PARSE_INVERT
: 0)|(c
->syscall_whitelist
? SECCOMP_PARSE_WHITELIST
: 0),
2719 unit
, filename
, line
);
2725 int config_parse_syscall_archs(
2727 const char *filename
,
2729 const char *section
,
2730 unsigned section_line
,
2737 const char *p
= rvalue
;
2741 if (isempty(rvalue
)) {
2742 *archs
= set_free(*archs
);
2746 r
= set_ensure_allocated(archs
, NULL
);
2751 _cleanup_free_
char *word
= NULL
;
2754 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
);
2760 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
2761 "Invalid syntax, ignoring: %s", rvalue
);
2765 r
= seccomp_arch_from_string(word
, &a
);
2767 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
2768 "Failed to parse system call architecture \"%s\", ignoring: %m", word
);
2772 r
= set_put(*archs
, UINT32_TO_PTR(a
+ 1));
2778 int config_parse_syscall_errno(
2780 const char *filename
,
2782 const char *section
,
2783 unsigned section_line
,
2790 ExecContext
*c
= data
;
2797 if (isempty(rvalue
)) {
2798 /* Empty assignment resets to KILL */
2799 c
->syscall_errno
= 0;
2803 e
= parse_errno(rvalue
);
2805 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse error number, ignoring: %s", rvalue
);
2809 c
->syscall_errno
= e
;
2813 int config_parse_address_families(
2815 const char *filename
,
2817 const char *section
,
2818 unsigned section_line
,
2825 ExecContext
*c
= data
;
2826 bool invert
= false;
2834 if (isempty(rvalue
)) {
2835 /* Empty assignment resets the list */
2836 c
->address_families
= set_free(c
->address_families
);
2837 c
->address_families_whitelist
= false;
2841 if (rvalue
[0] == '~') {
2846 if (!c
->address_families
) {
2847 c
->address_families
= set_new(NULL
);
2848 if (!c
->address_families
)
2851 c
->address_families_whitelist
= !invert
;
2854 for (p
= rvalue
;;) {
2855 _cleanup_free_
char *word
= NULL
;
2858 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
);
2864 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
2865 "Invalid syntax, ignoring: %s", rvalue
);
2869 af
= af_from_name(word
);
2871 log_syntax(unit
, LOG_ERR
, filename
, line
, af
,
2872 "Failed to parse address family, ignoring: %s", word
);
2876 /* If we previously wanted to forbid an address family and now
2877 * we want to allow it, then just remove it from the list.
2879 if (!invert
== c
->address_families_whitelist
) {
2880 r
= set_put(c
->address_families
, INT_TO_PTR(af
));
2884 set_remove(c
->address_families
, INT_TO_PTR(af
));
2888 int config_parse_restrict_namespaces(
2890 const char *filename
,
2892 const char *section
,
2893 unsigned section_line
,
2900 ExecContext
*c
= data
;
2901 unsigned long flags
;
2902 bool invert
= false;
2905 if (isempty(rvalue
)) {
2906 /* Reset to the default. */
2907 c
->restrict_namespaces
= NAMESPACE_FLAGS_INITIAL
;
2911 /* Boolean parameter ignores the previous settings */
2912 r
= parse_boolean(rvalue
);
2914 c
->restrict_namespaces
= 0;
2916 } else if (r
== 0) {
2917 c
->restrict_namespaces
= NAMESPACE_FLAGS_ALL
;
2921 if (rvalue
[0] == '~') {
2926 /* Not a boolean argument, in this case it's a list of namespace types. */
2927 r
= namespace_flags_from_string(rvalue
, &flags
);
2929 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse namespace type string, ignoring: %s", rvalue
);
2933 if (c
->restrict_namespaces
== NAMESPACE_FLAGS_INITIAL
)
2934 /* Initial assignment. Just set the value. */
2935 c
->restrict_namespaces
= invert
? (~flags
) & NAMESPACE_FLAGS_ALL
: flags
;
2937 /* Merge the value with the previous one. */
2938 SET_FLAG(c
->restrict_namespaces
, flags
, !invert
);
2944 int config_parse_unit_slice(
2946 const char *filename
,
2948 const char *section
,
2949 unsigned section_line
,
2956 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2957 _cleanup_free_
char *k
= NULL
;
2958 Unit
*u
= userdata
, *slice
= NULL
;
2966 r
= unit_name_printf(u
, rvalue
, &k
);
2968 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue
);
2972 r
= manager_load_unit(u
->manager
, k
, NULL
, &error
, &slice
);
2974 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to load slice unit %s, ignoring: %s", k
, bus_error_message(&error
, r
));
2978 r
= unit_set_slice(u
, slice
);
2980 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to assign slice %s to unit %s, ignoring: %m", slice
->id
, u
->id
);
2987 int config_parse_cpu_quota(
2989 const char *filename
,
2991 const char *section
,
2992 unsigned section_line
,
2999 CGroupContext
*c
= data
;
3006 if (isempty(rvalue
)) {
3007 c
->cpu_quota_per_sec_usec
= USEC_INFINITY
;
3011 r
= parse_permille_unbounded(rvalue
);
3013 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid CPU quota '%s', ignoring.", rvalue
);
3017 c
->cpu_quota_per_sec_usec
= ((usec_t
) r
* USEC_PER_SEC
) / 1000U;
3021 int config_parse_memory_limit(
3023 const char *filename
,
3025 const char *section
,
3026 unsigned section_line
,
3033 CGroupContext
*c
= data
;
3034 uint64_t bytes
= CGROUP_LIMIT_MAX
;
3037 if (!isempty(rvalue
) && !streq(rvalue
, "infinity")) {
3039 r
= parse_permille(rvalue
);
3041 r
= parse_size(rvalue
, 1024, &bytes
);
3043 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid memory limit '%s', ignoring: %m", rvalue
);
3047 bytes
= physical_memory_scale(r
, 1000U);
3049 if (bytes
>= UINT64_MAX
||
3050 (bytes
<= 0 && !streq(lvalue
, "MemorySwapMax"))) {
3051 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Memory limit '%s' out of range, ignoring.", rvalue
);
3056 if (streq(lvalue
, "MemoryMin"))
3057 c
->memory_min
= bytes
;
3058 else if (streq(lvalue
, "MemoryLow"))
3059 c
->memory_low
= bytes
;
3060 else if (streq(lvalue
, "MemoryHigh"))
3061 c
->memory_high
= bytes
;
3062 else if (streq(lvalue
, "MemoryMax"))
3063 c
->memory_max
= bytes
;
3064 else if (streq(lvalue
, "MemorySwapMax"))
3065 c
->memory_swap_max
= bytes
;
3066 else if (streq(lvalue
, "MemoryLimit"))
3067 c
->memory_limit
= bytes
;
3074 int config_parse_tasks_max(
3076 const char *filename
,
3078 const char *section
,
3079 unsigned section_line
,
3086 uint64_t *tasks_max
= data
, v
;
3090 if (isempty(rvalue
)) {
3091 *tasks_max
= u
? u
->manager
->default_tasks_max
: UINT64_MAX
;
3095 if (streq(rvalue
, "infinity")) {
3096 *tasks_max
= CGROUP_LIMIT_MAX
;
3100 r
= parse_permille(rvalue
);
3102 r
= safe_atou64(rvalue
, &v
);
3104 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid maximum tasks value '%s', ignoring: %m", rvalue
);
3108 v
= system_tasks_max_scale(r
, 1000U);
3110 if (v
<= 0 || v
>= UINT64_MAX
) {
3111 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Maximum tasks value '%s' out of range, ignoring.", rvalue
);
3119 int config_parse_delegate(
3121 const char *filename
,
3123 const char *section
,
3124 unsigned section_line
,
3131 CGroupContext
*c
= data
;
3135 t
= unit_name_to_type(unit
);
3136 assert(t
!= _UNIT_TYPE_INVALID
);
3138 if (!unit_vtable
[t
]->can_delegate
) {
3139 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Delegate= setting not supported for this unit type, ignoring.");
3143 /* We either accept a boolean value, which may be used to turn on delegation for all controllers, or turn it
3144 * off for all. Or it takes a list of controller names, in which case we add the specified controllers to the
3145 * mask to delegate. */
3147 if (isempty(rvalue
)) {
3148 /* An empty string resets controllers and set Delegate=yes. */
3150 c
->delegate_controllers
= 0;
3154 r
= parse_boolean(rvalue
);
3156 const char *p
= rvalue
;
3157 CGroupMask mask
= 0;
3160 _cleanup_free_
char *word
= NULL
;
3161 CGroupController cc
;
3163 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
);
3169 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid syntax, ignoring: %s", rvalue
);
3173 cc
= cgroup_controller_from_string(word
);
3175 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid controller name '%s', ignoring", word
);
3179 mask
|= CGROUP_CONTROLLER_TO_MASK(cc
);
3183 c
->delegate_controllers
|= mask
;
3187 c
->delegate_controllers
= _CGROUP_MASK_ALL
;
3189 c
->delegate
= false;
3190 c
->delegate_controllers
= 0;
3196 int config_parse_device_allow(
3198 const char *filename
,
3200 const char *section
,
3201 unsigned section_line
,
3208 _cleanup_free_
char *path
= NULL
, *resolved
= NULL
;
3209 CGroupContext
*c
= data
;
3210 const char *p
= rvalue
;
3213 if (isempty(rvalue
)) {
3214 while (c
->device_allow
)
3215 cgroup_context_free_device_allow(c
, c
->device_allow
);
3220 r
= extract_first_word(&p
, &path
, NULL
, EXTRACT_QUOTES
);
3224 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
3225 "Invalid syntax, ignoring: %s", rvalue
);
3229 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
3230 "Failed to extract device path and rights from '%s', ignoring.", rvalue
);
3234 r
= unit_full_printf(userdata
, path
, &resolved
);
3236 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
3237 "Failed to resolve unit specifiers in '%s', ignoring: %m", path
);
3241 if (!STARTSWITH_SET(resolved
, "block-", "char-")) {
3243 r
= path_simplify_and_warn(resolved
, 0, unit
, filename
, line
, lvalue
);
3247 if (!valid_device_node_path(resolved
)) {
3248 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid device node path '%s', ignoring.", resolved
);
3253 if (!isempty(p
) && !in_charset(p
, "rwm")) {
3254 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid device rights '%s', ignoring.", p
);
3258 return cgroup_add_device_allow(c
, resolved
, p
);
3261 int config_parse_io_device_weight(
3263 const char *filename
,
3265 const char *section
,
3266 unsigned section_line
,
3273 _cleanup_free_
char *path
= NULL
, *resolved
= NULL
;
3274 CGroupIODeviceWeight
*w
;
3275 CGroupContext
*c
= data
;
3276 const char *p
= rvalue
;
3284 if (isempty(rvalue
)) {
3285 while (c
->io_device_weights
)
3286 cgroup_context_free_io_device_weight(c
, c
->io_device_weights
);
3291 r
= extract_first_word(&p
, &path
, NULL
, EXTRACT_QUOTES
);
3295 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
3296 "Invalid syntax, ignoring: %s", rvalue
);
3299 if (r
== 0 || isempty(p
)) {
3300 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
3301 "Failed to extract device path and weight from '%s', ignoring.", rvalue
);
3305 r
= unit_full_printf(userdata
, path
, &resolved
);
3307 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
3308 "Failed to resolve unit specifiers in '%s', ignoring: %m", path
);
3312 r
= path_simplify_and_warn(resolved
, 0, unit
, filename
, line
, lvalue
);
3316 r
= cg_weight_parse(p
, &u
);
3318 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "IO weight '%s' invalid, ignoring: %m", p
);
3322 assert(u
!= CGROUP_WEIGHT_INVALID
);
3324 w
= new0(CGroupIODeviceWeight
, 1);
3328 w
->path
= TAKE_PTR(resolved
);
3331 LIST_PREPEND(device_weights
, c
->io_device_weights
, w
);
3335 int config_parse_io_device_latency(
3337 const char *filename
,
3339 const char *section
,
3340 unsigned section_line
,
3347 _cleanup_free_
char *path
= NULL
, *resolved
= NULL
;
3348 CGroupIODeviceLatency
*l
;
3349 CGroupContext
*c
= data
;
3350 const char *p
= rvalue
;
3358 if (isempty(rvalue
)) {
3359 while (c
->io_device_latencies
)
3360 cgroup_context_free_io_device_latency(c
, c
->io_device_latencies
);
3365 r
= extract_first_word(&p
, &path
, NULL
, EXTRACT_QUOTES
);
3369 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
3370 "Invalid syntax, ignoring: %s", rvalue
);
3373 if (r
== 0 || isempty(p
)) {
3374 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
3375 "Failed to extract device path and latency from '%s', ignoring.", rvalue
);
3379 r
= unit_full_printf(userdata
, path
, &resolved
);
3381 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
3382 "Failed to resolve unit specifiers in '%s', ignoring: %m", path
);
3386 r
= path_simplify_and_warn(resolved
, 0, unit
, filename
, line
, lvalue
);
3390 if (parse_sec(p
, &usec
) < 0) {
3391 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse timer value, ignoring: %s", p
);
3395 l
= new0(CGroupIODeviceLatency
, 1);
3399 l
->path
= TAKE_PTR(resolved
);
3400 l
->target_usec
= usec
;
3402 LIST_PREPEND(device_latencies
, c
->io_device_latencies
, l
);
3406 int config_parse_io_limit(
3408 const char *filename
,
3410 const char *section
,
3411 unsigned section_line
,
3418 _cleanup_free_
char *path
= NULL
, *resolved
= NULL
;
3419 CGroupIODeviceLimit
*l
= NULL
, *t
;
3420 CGroupContext
*c
= data
;
3421 CGroupIOLimitType type
;
3422 const char *p
= rvalue
;
3430 type
= cgroup_io_limit_type_from_string(lvalue
);
3433 if (isempty(rvalue
)) {
3434 LIST_FOREACH(device_limits
, l
, c
->io_device_limits
)
3435 l
->limits
[type
] = cgroup_io_limit_defaults
[type
];
3439 r
= extract_first_word(&p
, &path
, NULL
, EXTRACT_QUOTES
);
3443 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
3444 "Invalid syntax, ignoring: %s", rvalue
);
3447 if (r
== 0 || isempty(p
)) {
3448 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
3449 "Failed to extract device node and bandwidth from '%s', ignoring.", rvalue
);
3453 r
= unit_full_printf(userdata
, path
, &resolved
);
3455 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
3456 "Failed to resolve unit specifiers in '%s', ignoring: %m", path
);
3460 r
= path_simplify_and_warn(resolved
, 0, unit
, filename
, line
, lvalue
);
3464 if (streq("infinity", p
))
3465 num
= CGROUP_LIMIT_MAX
;
3467 r
= parse_size(p
, 1000, &num
);
3468 if (r
< 0 || num
<= 0) {
3469 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid IO limit '%s', ignoring.", p
);
3474 LIST_FOREACH(device_limits
, t
, c
->io_device_limits
) {
3475 if (path_equal(resolved
, t
->path
)) {
3482 CGroupIOLimitType ttype
;
3484 l
= new0(CGroupIODeviceLimit
, 1);
3488 l
->path
= TAKE_PTR(resolved
);
3489 for (ttype
= 0; ttype
< _CGROUP_IO_LIMIT_TYPE_MAX
; ttype
++)
3490 l
->limits
[ttype
] = cgroup_io_limit_defaults
[ttype
];
3492 LIST_PREPEND(device_limits
, c
->io_device_limits
, l
);
3495 l
->limits
[type
] = num
;
3500 int config_parse_blockio_device_weight(
3502 const char *filename
,
3504 const char *section
,
3505 unsigned section_line
,
3512 _cleanup_free_
char *path
= NULL
, *resolved
= NULL
;
3513 CGroupBlockIODeviceWeight
*w
;
3514 CGroupContext
*c
= data
;
3515 const char *p
= rvalue
;
3523 if (isempty(rvalue
)) {
3524 while (c
->blockio_device_weights
)
3525 cgroup_context_free_blockio_device_weight(c
, c
->blockio_device_weights
);
3530 r
= extract_first_word(&p
, &path
, NULL
, EXTRACT_QUOTES
);
3534 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
3535 "Invalid syntax, ignoring: %s", rvalue
);
3538 if (r
== 0 || isempty(p
)) {
3539 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
3540 "Failed to extract device node and weight from '%s', ignoring.", rvalue
);
3544 r
= unit_full_printf(userdata
, path
, &resolved
);
3546 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
3547 "Failed to resolve unit specifiers in '%s', ignoring: %m", path
);
3551 r
= path_simplify_and_warn(resolved
, 0, unit
, filename
, line
, lvalue
);
3555 r
= cg_blkio_weight_parse(p
, &u
);
3557 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid block IO weight '%s', ignoring: %m", p
);
3561 assert(u
!= CGROUP_BLKIO_WEIGHT_INVALID
);
3563 w
= new0(CGroupBlockIODeviceWeight
, 1);
3567 w
->path
= TAKE_PTR(resolved
);
3570 LIST_PREPEND(device_weights
, c
->blockio_device_weights
, w
);
3574 int config_parse_blockio_bandwidth(
3576 const char *filename
,
3578 const char *section
,
3579 unsigned section_line
,
3586 _cleanup_free_
char *path
= NULL
, *resolved
= NULL
;
3587 CGroupBlockIODeviceBandwidth
*b
= NULL
, *t
;
3588 CGroupContext
*c
= data
;
3589 const char *p
= rvalue
;
3598 read
= streq("BlockIOReadBandwidth", lvalue
);
3600 if (isempty(rvalue
)) {
3601 LIST_FOREACH(device_bandwidths
, b
, c
->blockio_device_bandwidths
) {
3602 b
->rbps
= CGROUP_LIMIT_MAX
;
3603 b
->wbps
= CGROUP_LIMIT_MAX
;
3608 r
= extract_first_word(&p
, &path
, NULL
, EXTRACT_QUOTES
);
3612 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
3613 "Invalid syntax, ignoring: %s", rvalue
);
3616 if (r
== 0 || isempty(p
)) {
3617 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
3618 "Failed to extract device node and bandwidth from '%s', ignoring.", rvalue
);
3622 r
= unit_full_printf(userdata
, path
, &resolved
);
3624 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
3625 "Failed to resolve unit specifiers in '%s', ignoring: %m", path
);
3629 r
= path_simplify_and_warn(resolved
, 0, unit
, filename
, line
, lvalue
);
3633 r
= parse_size(p
, 1000, &bytes
);
3634 if (r
< 0 || bytes
<= 0) {
3635 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid Block IO Bandwidth '%s', ignoring.", p
);
3639 LIST_FOREACH(device_bandwidths
, t
, c
->blockio_device_bandwidths
) {
3640 if (path_equal(resolved
, t
->path
)) {
3647 b
= new0(CGroupBlockIODeviceBandwidth
, 1);
3651 b
->path
= TAKE_PTR(resolved
);
3652 b
->rbps
= CGROUP_LIMIT_MAX
;
3653 b
->wbps
= CGROUP_LIMIT_MAX
;
3655 LIST_PREPEND(device_bandwidths
, c
->blockio_device_bandwidths
, b
);
3666 int config_parse_job_mode_isolate(
3668 const char *filename
,
3670 const char *section
,
3671 unsigned section_line
,
3685 r
= parse_boolean(rvalue
);
3687 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse boolean, ignoring: %s", rvalue
);
3691 log_notice("%s is deprecated. Please use OnFailureJobMode= instead", lvalue
);
3693 *m
= r
? JOB_ISOLATE
: JOB_REPLACE
;
3697 int config_parse_exec_directories(
3699 const char *filename
,
3701 const char *section
,
3702 unsigned section_line
,
3719 if (isempty(rvalue
)) {
3720 /* Empty assignment resets the list */
3721 *rt
= strv_free(*rt
);
3725 for (p
= rvalue
;;) {
3726 _cleanup_free_
char *word
= NULL
, *k
= NULL
;
3728 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
);
3732 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
3733 "Invalid syntax, ignoring: %s", rvalue
);
3739 r
= unit_full_printf(u
, word
, &k
);
3741 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
3742 "Failed to resolve unit specifiers in \"%s\", ignoring: %m", word
);
3746 r
= path_simplify_and_warn(k
, PATH_CHECK_RELATIVE
, unit
, filename
, line
, lvalue
);
3750 if (path_startswith(k
, "private")) {
3751 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
3752 "%s= path can't be 'private', ignoring assignment: %s", lvalue
, word
);
3756 r
= strv_push(rt
, k
);
3763 int config_parse_set_status(
3765 const char *filename
,
3767 const char *section
,
3768 unsigned section_line
,
3776 const char *word
, *state
;
3778 ExitStatusSet
*status_set
= data
;
3785 /* Empty assignment resets the list */
3786 if (isempty(rvalue
)) {
3787 exit_status_set_free(status_set
);
3791 FOREACH_WORD(word
, l
, rvalue
, state
) {
3792 _cleanup_free_
char *temp
;
3796 temp
= strndup(word
, l
);
3800 r
= safe_atoi(temp
, &val
);
3802 val
= signal_from_string(temp
);
3805 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse value, ignoring: %s", word
);
3808 set
= &status_set
->signal
;
3810 if (val
< 0 || val
> 255) {
3811 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Value %d is outside range 0-255, ignoring", val
);
3814 set
= &status_set
->status
;
3817 r
= set_ensure_allocated(set
, NULL
);
3821 r
= set_put(*set
, INT_TO_PTR(val
));
3825 if (!isempty(state
))
3826 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Trailing garbage, ignoring.");
3831 int config_parse_namespace_path_strv(
3833 const char *filename
,
3835 const char *section
,
3836 unsigned section_line
,
3845 const char *p
= rvalue
;
3853 if (isempty(rvalue
)) {
3854 /* Empty assignment resets the list */
3855 *sv
= strv_free(*sv
);
3860 _cleanup_free_
char *word
= NULL
, *resolved
= NULL
, *joined
= NULL
;
3862 bool ignore_enoent
= false, shall_prefix
= false;
3864 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
);
3870 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to extract first word, ignoring: %s", rvalue
);
3875 if (startswith(w
, "-")) {
3876 ignore_enoent
= true;
3879 if (startswith(w
, "+")) {
3880 shall_prefix
= true;
3884 r
= unit_full_printf(u
, w
, &resolved
);
3886 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in %s: %m", w
);
3890 r
= path_simplify_and_warn(resolved
, PATH_CHECK_ABSOLUTE
, unit
, filename
, line
, lvalue
);
3894 joined
= strjoin(ignore_enoent
? "-" : "",
3895 shall_prefix
? "+" : "",
3898 r
= strv_push(sv
, joined
);
3908 int config_parse_temporary_filesystems(
3910 const char *filename
,
3912 const char *section
,
3913 unsigned section_line
,
3921 ExecContext
*c
= data
;
3922 const char *p
= rvalue
;
3930 if (isempty(rvalue
)) {
3931 /* Empty assignment resets the list */
3932 temporary_filesystem_free_many(c
->temporary_filesystems
, c
->n_temporary_filesystems
);
3933 c
->temporary_filesystems
= NULL
;
3934 c
->n_temporary_filesystems
= 0;
3939 _cleanup_free_
char *word
= NULL
, *path
= NULL
, *resolved
= NULL
;
3942 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
);
3948 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to extract first word, ignoring: %s", rvalue
);
3953 r
= extract_first_word(&w
, &path
, ":", EXTRACT_DONT_COALESCE_SEPARATORS
);
3957 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to extract first word, ignoring: %s", word
);
3961 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid syntax, ignoring: %s", word
);
3965 r
= unit_full_printf(u
, path
, &resolved
);
3967 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in %s, ignoring: %m", path
);
3971 r
= path_simplify_and_warn(resolved
, PATH_CHECK_ABSOLUTE
, unit
, filename
, line
, lvalue
);
3975 r
= temporary_filesystem_add(&c
->temporary_filesystems
, &c
->n_temporary_filesystems
, resolved
, w
);
3981 int config_parse_bind_paths(
3983 const char *filename
,
3985 const char *section
,
3986 unsigned section_line
,
3993 ExecContext
*c
= data
;
4003 if (isempty(rvalue
)) {
4004 /* Empty assignment resets the list */
4005 bind_mount_free_many(c
->bind_mounts
, c
->n_bind_mounts
);
4006 c
->bind_mounts
= NULL
;
4007 c
->n_bind_mounts
= 0;
4013 _cleanup_free_
char *source
= NULL
, *destination
= NULL
;
4014 _cleanup_free_
char *sresolved
= NULL
, *dresolved
= NULL
;
4015 char *s
= NULL
, *d
= NULL
;
4016 bool rbind
= true, ignore_enoent
= false;
4018 r
= extract_first_word(&p
, &source
, ":" WHITESPACE
, EXTRACT_QUOTES
|EXTRACT_DONT_COALESCE_SEPARATORS
);
4024 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse %s, ignoring: %s", lvalue
, rvalue
);
4028 r
= unit_full_printf(u
, source
, &sresolved
);
4030 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
4031 "Failed to resolved unit specifiers in \"%s\", ignoring: %m", source
);
4037 ignore_enoent
= true;
4041 r
= path_simplify_and_warn(s
, PATH_CHECK_ABSOLUTE
, unit
, filename
, line
, lvalue
);
4045 /* Optionally, the destination is specified. */
4046 if (p
&& p
[-1] == ':') {
4047 r
= extract_first_word(&p
, &destination
, ":" WHITESPACE
, EXTRACT_QUOTES
|EXTRACT_DONT_COALESCE_SEPARATORS
);
4051 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse %s, ignoring: %s", lvalue
, rvalue
);
4055 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Missing argument after ':', ignoring: %s", s
);
4059 r
= unit_full_printf(u
, destination
, &dresolved
);
4061 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
4062 "Failed to resolved specifiers in \"%s\", ignoring: %m", destination
);
4066 r
= path_simplify_and_warn(dresolved
, PATH_CHECK_ABSOLUTE
, unit
, filename
, line
, lvalue
);
4072 /* Optionally, there's also a short option string specified */
4073 if (p
&& p
[-1] == ':') {
4074 _cleanup_free_
char *options
= NULL
;
4076 r
= extract_first_word(&p
, &options
, NULL
, EXTRACT_QUOTES
);
4080 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse %s: %s", lvalue
, rvalue
);
4084 if (isempty(options
) || streq(options
, "rbind"))
4086 else if (streq(options
, "norbind"))
4089 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid option string, ignoring setting: %s", options
);
4096 r
= bind_mount_add(&c
->bind_mounts
, &c
->n_bind_mounts
,
4100 .read_only
= !!strstr(lvalue
, "ReadOnly"),
4102 .ignore_enoent
= ignore_enoent
,
4111 int config_parse_job_timeout_sec(
4113 const char *filename
,
4115 const char *section
,
4116 unsigned section_line
,
4132 r
= parse_sec_fix_0(rvalue
, &usec
);
4134 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse JobTimeoutSec= parameter, ignoring: %s", rvalue
);
4138 /* If the user explicitly changed JobTimeoutSec= also change JobRunningTimeoutSec=, for compatibility with old
4139 * versions. If JobRunningTimeoutSec= was explicitly set, avoid this however as whatever the user picked should
4142 if (!u
->job_running_timeout_set
)
4143 u
->job_running_timeout
= usec
;
4145 u
->job_timeout
= usec
;
4150 int config_parse_job_running_timeout_sec(
4152 const char *filename
,
4154 const char *section
,
4155 unsigned section_line
,
4171 r
= parse_sec_fix_0(rvalue
, &usec
);
4173 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse JobRunningTimeoutSec= parameter, ignoring: %s", rvalue
);
4177 u
->job_running_timeout
= usec
;
4178 u
->job_running_timeout_set
= true;
4183 int config_parse_emergency_action(
4185 const char *filename
,
4187 const char *section
,
4188 unsigned section_line
,
4196 EmergencyAction
*x
= data
;
4205 m
= ((Unit
*) userdata
)->manager
;
4209 r
= parse_emergency_action(rvalue
, MANAGER_IS_SYSTEM(m
), x
);
4211 if (r
== -EOPNOTSUPP
&& MANAGER_IS_USER(m
)) {
4212 /* Compat mode: remove for systemd 241. */
4214 log_syntax(unit
, LOG_INFO
, filename
, line
, r
,
4215 "%s= in user mode specified as \"%s\", using \"exit-force\" instead.",
4217 *x
= EMERGENCY_ACTION_EXIT_FORCE
;
4221 if (r
== -EOPNOTSUPP
)
4222 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
4223 "%s= specified as %s mode action, ignoring: %s",
4224 lvalue
, MANAGER_IS_SYSTEM(m
) ? "user" : "system", rvalue
);
4226 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
4227 "Failed to parse %s=, ignoring: %s", lvalue
, rvalue
);
4234 int config_parse_pid_file(
4236 const char *filename
,
4238 const char *section
,
4239 unsigned section_line
,
4246 _cleanup_free_
char *k
= NULL
, *n
= NULL
;
4257 r
= unit_full_printf(u
, rvalue
, &k
);
4259 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue
);
4263 /* If this is a relative path make it absolute by prefixing the /run */
4264 n
= path_make_absolute(k
, u
->manager
->prefix
[EXEC_DIRECTORY_RUNTIME
]);
4268 /* Check that the result is a sensible path */
4269 r
= path_simplify_and_warn(n
, PATH_CHECK_ABSOLUTE
, unit
, filename
, line
, lvalue
);
4273 e
= path_startswith(n
, "/var/run/");
4277 z
= strjoin("/run/", e
);
4281 log_syntax(unit
, LOG_NOTICE
, filename
, line
, 0, "PIDFile= references path below legacy directory /var/run/, updating %s → %s; please update the unit file accordingly.", n
, z
);
4283 free_and_replace(*s
, z
);
4285 free_and_replace(*s
, n
);
4290 int config_parse_exit_status(
4292 const char *filename
,
4294 const char *section
,
4295 unsigned section_line
,
4302 int *exit_status
= data
, r
;
4308 assert(exit_status
);
4310 if (isempty(rvalue
)) {
4315 r
= safe_atou8(rvalue
, &u
);
4317 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse exit status '%s', ignoring: %m", rvalue
);
4325 int config_parse_disable_controllers(
4327 const char *filename
,
4329 const char *section
,
4330 unsigned section_line
,
4338 CGroupContext
*c
= data
;
4339 CGroupMask disabled_mask
;
4341 /* 1. If empty, make all controllers eligible for use again.
4342 * 2. If non-empty, merge all listed controllers, space separated. */
4344 if (isempty(rvalue
)) {
4345 c
->disable_controllers
= 0;
4349 r
= cg_mask_from_string(rvalue
, &disabled_mask
);
4350 if (r
< 0 || disabled_mask
<= 0) {
4351 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid cgroup string: %s, ignoring", rvalue
);
4355 c
->disable_controllers
|= disabled_mask
;
4360 #define FOLLOW_MAX 8
4362 static int open_follow(char **filename
, FILE **_f
, Set
*names
, char **_final
) {
4373 /* This will update the filename pointer if the loaded file is
4374 * reached by a symlink. The old string will be freed. */
4377 char *target
, *name
;
4379 if (c
++ >= FOLLOW_MAX
)
4382 path_simplify(*filename
, false);
4384 /* Add the file name we are currently looking at to
4385 * the names of this unit, but only if it is a valid
4387 name
= basename(*filename
);
4388 if (unit_name_is_valid(name
, UNIT_NAME_ANY
)) {
4390 id
= set_get(names
, name
);
4396 r
= set_consume(names
, id
);
4402 /* Try to open the file name, but don't if its a symlink */
4403 fd
= open(*filename
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
4410 /* Hmm, so this is a symlink. Let's read the name, and follow it manually */
4411 r
= readlink_and_make_absolute(*filename
, &target
);
4415 free_and_replace(*filename
, target
);
4418 f
= fdopen(fd
, "r");
4430 static int merge_by_names(Unit
**u
, Set
*names
, const char *id
) {
4438 /* Let's try to add in all symlink names we found */
4439 while ((k
= set_steal_first(names
))) {
4441 /* First try to merge in the other name into our
4443 r
= unit_merge_by_name(*u
, k
);
4447 /* Hmm, we couldn't merge the other unit into
4448 * ours? Then let's try it the other way
4451 /* If the symlink name we are looking at is unit template, then
4452 we must search for instance of this template */
4453 if (unit_name_is_valid(k
, UNIT_NAME_TEMPLATE
) && (*u
)->instance
) {
4454 _cleanup_free_
char *instance
= NULL
;
4456 r
= unit_name_replace_instance(k
, (*u
)->instance
, &instance
);
4460 other
= manager_get_unit((*u
)->manager
, instance
);
4462 other
= manager_get_unit((*u
)->manager
, k
);
4467 r
= unit_merge(other
, *u
);
4470 return merge_by_names(u
, names
, NULL
);
4478 unit_choose_id(*u
, id
);
4486 static int load_from_path(Unit
*u
, const char *path
) {
4487 _cleanup_set_free_free_ Set
*symlink_names
= NULL
;
4488 _cleanup_fclose_
FILE *f
= NULL
;
4489 _cleanup_free_
char *filename
= NULL
;
4498 symlink_names
= set_new(&string_hash_ops
);
4502 if (path_is_absolute(path
)) {
4504 filename
= strdup(path
);
4508 r
= open_follow(&filename
, &f
, symlink_names
, &id
);
4510 filename
= mfree(filename
);
4518 STRV_FOREACH(p
, u
->manager
->lookup_paths
.search_path
) {
4520 /* Instead of opening the path right away, we manually
4521 * follow all symlinks and add their name to our unit
4522 * name set while doing so */
4523 filename
= path_make_absolute(path
, *p
);
4527 if (u
->manager
->unit_path_cache
&&
4528 !set_get(u
->manager
->unit_path_cache
, filename
))
4531 r
= open_follow(&filename
, &f
, symlink_names
, &id
);
4535 /* ENOENT means that the file is missing or is a dangling symlink.
4536 * ENOTDIR means that one of paths we expect to be is a directory
4537 * is not a directory, we should just ignore that.
4538 * EACCES means that the directory or file permissions are wrong.
4541 log_debug_errno(r
, "Cannot access \"%s\": %m", filename
);
4542 else if (!IN_SET(r
, -ENOENT
, -ENOTDIR
))
4545 filename
= mfree(filename
);
4546 /* Empty the symlink names for the next run */
4547 set_clear_free(symlink_names
);
4552 /* Hmm, no suitable file found? */
4555 if (!unit_type_may_alias(u
->type
) && set_size(symlink_names
) > 1) {
4556 log_unit_warning(u
, "Unit type of %s does not support alias names, refusing loading via symlink.", u
->id
);
4561 r
= merge_by_names(&merged
, symlink_names
, id
);
4566 u
->load_state
= UNIT_MERGED
;
4570 if (fstat(fileno(f
), &st
) < 0)
4573 if (null_or_empty(&st
)) {
4574 u
->load_state
= UNIT_MASKED
;
4575 u
->fragment_mtime
= 0;
4577 u
->load_state
= UNIT_LOADED
;
4578 u
->fragment_mtime
= timespec_load(&st
.st_mtim
);
4580 /* Now, parse the file contents */
4581 r
= config_parse(u
->id
, filename
, f
,
4582 UNIT_VTABLE(u
)->sections
,
4583 config_item_perf_lookup
, load_fragment_gperf_lookup
,
4584 CONFIG_PARSE_ALLOW_INCLUDE
, u
);
4589 free_and_replace(u
->fragment_path
, filename
);
4591 if (u
->source_path
) {
4592 if (stat(u
->source_path
, &st
) >= 0)
4593 u
->source_mtime
= timespec_load(&st
.st_mtim
);
4595 u
->source_mtime
= 0;
4601 int unit_load_fragment(Unit
*u
) {
4607 assert(u
->load_state
== UNIT_STUB
);
4611 u
->load_state
= UNIT_LOADED
;
4615 /* First, try to find the unit under its id. We always look
4616 * for unit files in the default directories, to make it easy
4617 * to override things by placing things in /etc/systemd/system */
4618 r
= load_from_path(u
, u
->id
);
4622 /* Try to find an alias we can load this with */
4623 if (u
->load_state
== UNIT_STUB
) {
4624 SET_FOREACH(t
, u
->names
, i
) {
4629 r
= load_from_path(u
, t
);
4633 if (u
->load_state
!= UNIT_STUB
)
4638 /* And now, try looking for it under the suggested (originally linked) path */
4639 if (u
->load_state
== UNIT_STUB
&& u
->fragment_path
) {
4641 r
= load_from_path(u
, u
->fragment_path
);
4645 if (u
->load_state
== UNIT_STUB
)
4646 /* Hmm, this didn't work? Then let's get rid
4647 * of the fragment path stored for us, so that
4648 * we don't point to an invalid location. */
4649 u
->fragment_path
= mfree(u
->fragment_path
);
4652 /* Look for a template */
4653 if (u
->load_state
== UNIT_STUB
&& u
->instance
) {
4654 _cleanup_free_
char *k
= NULL
;
4656 r
= unit_name_template(u
->id
, &k
);
4660 r
= load_from_path(u
, k
);
4663 log_unit_notice(u
, "Unit configuration has fatal error, unit will not be started.");
4667 if (u
->load_state
== UNIT_STUB
) {
4668 SET_FOREACH(t
, u
->names
, i
) {
4669 _cleanup_free_
char *z
= NULL
;
4674 r
= unit_name_template(t
, &z
);
4678 r
= load_from_path(u
, z
);
4682 if (u
->load_state
!= UNIT_STUB
)
4691 void unit_dump_config_items(FILE *f
) {
4692 static const struct {
4693 const ConfigParserCallback callback
;
4696 { config_parse_warn_compat
, "NOTSUPPORTED" },
4697 { config_parse_int
, "INTEGER" },
4698 { config_parse_unsigned
, "UNSIGNED" },
4699 { config_parse_iec_size
, "SIZE" },
4700 { config_parse_iec_uint64
, "SIZE" },
4701 { config_parse_si_size
, "SIZE" },
4702 { config_parse_bool
, "BOOLEAN" },
4703 { config_parse_string
, "STRING" },
4704 { config_parse_path
, "PATH" },
4705 { config_parse_unit_path_printf
, "PATH" },
4706 { config_parse_strv
, "STRING [...]" },
4707 { config_parse_exec_nice
, "NICE" },
4708 { config_parse_exec_oom_score_adjust
, "OOMSCOREADJUST" },
4709 { config_parse_exec_io_class
, "IOCLASS" },
4710 { config_parse_exec_io_priority
, "IOPRIORITY" },
4711 { config_parse_exec_cpu_sched_policy
, "CPUSCHEDPOLICY" },
4712 { config_parse_exec_cpu_sched_prio
, "CPUSCHEDPRIO" },
4713 { config_parse_exec_cpu_affinity
, "CPUAFFINITY" },
4714 { config_parse_mode
, "MODE" },
4715 { config_parse_unit_env_file
, "FILE" },
4716 { config_parse_exec_output
, "OUTPUT" },
4717 { config_parse_exec_input
, "INPUT" },
4718 { config_parse_log_facility
, "FACILITY" },
4719 { config_parse_log_level
, "LEVEL" },
4720 { config_parse_exec_secure_bits
, "SECUREBITS" },
4721 { config_parse_capability_set
, "BOUNDINGSET" },
4722 { config_parse_rlimit
, "LIMIT" },
4723 { config_parse_unit_deps
, "UNIT [...]" },
4724 { config_parse_exec
, "PATH [ARGUMENT [...]]" },
4725 { config_parse_service_type
, "SERVICETYPE" },
4726 { config_parse_service_restart
, "SERVICERESTART" },
4727 { config_parse_kill_mode
, "KILLMODE" },
4728 { config_parse_signal
, "SIGNAL" },
4729 { config_parse_socket_listen
, "SOCKET [...]" },
4730 { config_parse_socket_bind
, "SOCKETBIND" },
4731 { config_parse_socket_bindtodevice
, "NETWORKINTERFACE" },
4732 { config_parse_sec
, "SECONDS" },
4733 { config_parse_nsec
, "NANOSECONDS" },
4734 { config_parse_namespace_path_strv
, "PATH [...]" },
4735 { config_parse_bind_paths
, "PATH[:PATH[:OPTIONS]] [...]" },
4736 { config_parse_unit_requires_mounts_for
, "PATH [...]" },
4737 { config_parse_exec_mount_flags
, "MOUNTFLAG [...]" },
4738 { config_parse_unit_string_printf
, "STRING" },
4739 { config_parse_trigger_unit
, "UNIT" },
4740 { config_parse_timer
, "TIMER" },
4741 { config_parse_path_spec
, "PATH" },
4742 { config_parse_notify_access
, "ACCESS" },
4743 { config_parse_ip_tos
, "TOS" },
4744 { config_parse_unit_condition_path
, "CONDITION" },
4745 { config_parse_unit_condition_string
, "CONDITION" },
4746 { config_parse_unit_condition_null
, "CONDITION" },
4747 { config_parse_unit_slice
, "SLICE" },
4748 { config_parse_documentation
, "URL" },
4749 { config_parse_service_timeout
, "SECONDS" },
4750 { config_parse_emergency_action
, "ACTION" },
4751 { config_parse_set_status
, "STATUS" },
4752 { config_parse_service_sockets
, "SOCKETS" },
4753 { config_parse_environ
, "ENVIRON" },
4755 { config_parse_syscall_filter
, "SYSCALLS" },
4756 { config_parse_syscall_archs
, "ARCHS" },
4757 { config_parse_syscall_errno
, "ERRNO" },
4758 { config_parse_address_families
, "FAMILIES" },
4759 { config_parse_restrict_namespaces
, "NAMESPACES" },
4761 { config_parse_cpu_shares
, "SHARES" },
4762 { config_parse_cg_weight
, "WEIGHT" },
4763 { config_parse_memory_limit
, "LIMIT" },
4764 { config_parse_device_allow
, "DEVICE" },
4765 { config_parse_device_policy
, "POLICY" },
4766 { config_parse_io_limit
, "LIMIT" },
4767 { config_parse_io_device_weight
, "DEVICEWEIGHT" },
4768 { config_parse_io_device_latency
, "DEVICELATENCY" },
4769 { config_parse_blockio_bandwidth
, "BANDWIDTH" },
4770 { config_parse_blockio_weight
, "WEIGHT" },
4771 { config_parse_blockio_device_weight
, "DEVICEWEIGHT" },
4772 { config_parse_long
, "LONG" },
4773 { config_parse_socket_service
, "SERVICE" },
4775 { config_parse_exec_selinux_context
, "LABEL" },
4777 { config_parse_job_mode
, "MODE" },
4778 { config_parse_job_mode_isolate
, "BOOLEAN" },
4779 { config_parse_personality
, "PERSONALITY" },
4782 const char *prev
= NULL
;
4787 NULSTR_FOREACH(i
, load_fragment_gperf_nulstr
) {
4788 const char *rvalue
= "OTHER", *lvalue
;
4789 const ConfigPerfItem
*p
;
4794 assert_se(p
= load_fragment_gperf_lookup(i
, strlen(i
)));
4796 /* Hide legacy settings */
4797 if (p
->parse
== config_parse_warn_compat
&&
4798 p
->ltype
== DISABLED_LEGACY
)
4801 for (j
= 0; j
< ELEMENTSOF(table
); j
++)
4802 if (p
->parse
== table
[j
].callback
) {
4803 rvalue
= table
[j
].rvalue
;
4807 dot
= strchr(i
, '.');
4808 lvalue
= dot
? dot
+ 1 : i
;
4812 if (!prev
|| !strneq(prev
, i
, prefix_len
+1)) {
4816 fprintf(f
, "[%.*s]\n", (int) prefix_len
, i
);
4819 fprintf(f
, "%s=%s\n", lvalue
, rvalue
);