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 "limits-util.h"
40 #include "load-fragment.h"
43 #include "mountpoint-util.h"
44 #include "nulstr-util.h"
45 #include "parse-util.h"
46 #include "path-util.h"
47 #include "process-util.h"
49 #include "seccomp-util.h"
51 #include "securebits-util.h"
52 #include "signal-util.h"
53 #include "stat-util.h"
54 #include "string-util.h"
56 #include "unit-name.h"
57 #include "unit-printf.h"
58 #include "user-util.h"
59 #include "time-util.h"
62 static int parse_socket_protocol(const char *s
) {
65 r
= parse_ip_protocol(s
);
68 if (!IN_SET(r
, IPPROTO_UDPLITE
, IPPROTO_SCTP
))
69 return -EPROTONOSUPPORT
;
74 DEFINE_CONFIG_PARSE(config_parse_socket_protocol
, parse_socket_protocol
, "Failed to parse socket protocol");
75 DEFINE_CONFIG_PARSE(config_parse_exec_secure_bits
, secure_bits_from_string
, "Failed to parse secure bits");
76 DEFINE_CONFIG_PARSE_ENUM(config_parse_collect_mode
, collect_mode
, CollectMode
, "Failed to parse garbage collection mode");
77 DEFINE_CONFIG_PARSE_ENUM(config_parse_device_policy
, cgroup_device_policy
, CGroupDevicePolicy
, "Failed to parse device policy");
78 DEFINE_CONFIG_PARSE_ENUM(config_parse_exec_keyring_mode
, exec_keyring_mode
, ExecKeyringMode
, "Failed to parse keyring mode");
79 DEFINE_CONFIG_PARSE_ENUM(config_parse_exec_utmp_mode
, exec_utmp_mode
, ExecUtmpMode
, "Failed to parse utmp mode");
80 DEFINE_CONFIG_PARSE_ENUM(config_parse_job_mode
, job_mode
, JobMode
, "Failed to parse job mode");
81 DEFINE_CONFIG_PARSE_ENUM(config_parse_kill_mode
, kill_mode
, KillMode
, "Failed to parse kill mode");
82 DEFINE_CONFIG_PARSE_ENUM(config_parse_notify_access
, notify_access
, NotifyAccess
, "Failed to parse notify access specifier");
83 DEFINE_CONFIG_PARSE_ENUM(config_parse_protect_home
, protect_home
, ProtectHome
, "Failed to parse protect home value");
84 DEFINE_CONFIG_PARSE_ENUM(config_parse_protect_system
, protect_system
, ProtectSystem
, "Failed to parse protect system value");
85 DEFINE_CONFIG_PARSE_ENUM(config_parse_runtime_preserve_mode
, exec_preserve_mode
, ExecPreserveMode
, "Failed to parse runtime directory preserve mode");
86 DEFINE_CONFIG_PARSE_ENUM(config_parse_service_type
, service_type
, ServiceType
, "Failed to parse service type");
87 DEFINE_CONFIG_PARSE_ENUM(config_parse_service_restart
, service_restart
, ServiceRestart
, "Failed to parse service restart specifier");
88 DEFINE_CONFIG_PARSE_ENUM(config_parse_socket_bind
, socket_address_bind_ipv6_only_or_bool
, SocketAddressBindIPv6Only
, "Failed to parse bind IPv6 only value");
89 DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT(config_parse_ip_tos
, ip_tos
, int, -1, "Failed to parse IP TOS value");
90 DEFINE_CONFIG_PARSE_PTR(config_parse_blockio_weight
, cg_blkio_weight_parse
, uint64_t, "Invalid block IO weight");
91 DEFINE_CONFIG_PARSE_PTR(config_parse_cg_weight
, cg_weight_parse
, uint64_t, "Invalid weight");
92 DEFINE_CONFIG_PARSE_PTR(config_parse_cpu_shares
, cg_cpu_shares_parse
, uint64_t, "Invalid CPU shares");
93 DEFINE_CONFIG_PARSE_PTR(config_parse_exec_mount_flags
, mount_propagation_flags_from_string
, unsigned long, "Failed to parse mount flag");
95 int config_parse_unit_deps(
100 unsigned section_line
,
107 UnitDependency d
= ltype
;
117 _cleanup_free_
char *word
= NULL
, *k
= NULL
;
120 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_RETAIN_ESCAPE
);
126 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid syntax, ignoring: %s", rvalue
);
130 r
= unit_name_printf(u
, word
, &k
);
132 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in '%s', ignoring: %m", word
);
136 r
= unit_add_dependency_by_name(u
, d
, k
, true, UNIT_DEPENDENCY_FILE
);
138 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to add dependency on %s, ignoring: %m", k
);
144 int config_parse_obsolete_unit_deps(
146 const char *filename
,
149 unsigned section_line
,
156 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
157 "Unit dependency type %s= is obsolete, replacing by %s=, please update your unit file", lvalue
, unit_dependency_to_string(ltype
));
159 return config_parse_unit_deps(unit
, filename
, line
, section
, section_line
, lvalue
, ltype
, rvalue
, data
, userdata
);
162 int config_parse_unit_string_printf(
164 const char *filename
,
167 unsigned section_line
,
174 _cleanup_free_
char *k
= NULL
;
183 r
= unit_full_printf(u
, rvalue
, &k
);
185 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue
);
189 return config_parse_string(unit
, filename
, line
, section
, section_line
, lvalue
, ltype
, k
, data
, userdata
);
192 int config_parse_unit_strv_printf(
194 const char *filename
,
197 unsigned section_line
,
205 _cleanup_free_
char *k
= NULL
;
213 r
= unit_full_printf(u
, rvalue
, &k
);
215 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue
);
219 return config_parse_strv(unit
, filename
, line
, section
, section_line
, lvalue
, ltype
, k
, data
, userdata
);
222 int config_parse_unit_path_printf(
224 const char *filename
,
227 unsigned section_line
,
234 _cleanup_free_
char *k
= NULL
;
244 /* Let's not bother with anything that is too long */
245 if (strlen(rvalue
) >= PATH_MAX
) {
246 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
247 "%s value too long%s.",
248 lvalue
, fatal
? "" : ", ignoring");
249 return fatal
? -ENAMETOOLONG
: 0;
252 r
= unit_full_printf(u
, rvalue
, &k
);
254 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
255 "Failed to resolve unit specifiers in '%s'%s: %m",
256 rvalue
, fatal
? "" : ", ignoring");
257 return fatal
? -ENOEXEC
: 0;
260 return config_parse_path(unit
, filename
, line
, section
, section_line
, lvalue
, ltype
, k
, data
, userdata
);
263 int config_parse_unit_path_strv_printf(
265 const char *filename
,
268 unsigned section_line
,
285 if (isempty(rvalue
)) {
291 _cleanup_free_
char *word
= NULL
, *k
= NULL
;
293 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
);
299 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
300 "Invalid syntax, ignoring: %s", rvalue
);
304 r
= unit_full_printf(u
, word
, &k
);
306 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
307 "Failed to resolve unit specifiers in '%s', ignoring: %m", word
);
311 r
= path_simplify_and_warn(k
, PATH_CHECK_ABSOLUTE
, unit
, filename
, line
, lvalue
);
315 r
= strv_consume(x
, TAKE_PTR(k
));
321 static int patch_var_run(
323 const char *filename
,
331 e
= path_startswith(*path
, "/var/run/");
335 z
= path_join("/run/", e
);
339 log_syntax(unit
, LOG_NOTICE
, filename
, line
, 0,
340 "%s= references a path below legacy directory /var/run/, updating %s → %s; "
341 "please update the unit file accordingly.", lvalue
, *path
, z
);
343 free_and_replace(*path
, z
);
348 int config_parse_socket_listen(
350 const char *filename
,
353 unsigned section_line
,
360 _cleanup_free_ SocketPort
*p
= NULL
;
372 if (isempty(rvalue
)) {
373 /* An empty assignment removes all ports */
374 socket_free_ports(s
);
378 p
= new0(SocketPort
, 1);
382 if (ltype
!= SOCKET_SOCKET
) {
383 _cleanup_free_
char *k
= NULL
;
385 r
= unit_full_printf(UNIT(s
), rvalue
, &k
);
387 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue
);
391 r
= path_simplify_and_warn(k
, PATH_CHECK_ABSOLUTE
, unit
, filename
, line
, lvalue
);
395 if (ltype
== SOCKET_FIFO
) {
396 r
= patch_var_run(unit
, filename
, line
, lvalue
, &k
);
401 free_and_replace(p
->path
, k
);
404 } else if (streq(lvalue
, "ListenNetlink")) {
405 _cleanup_free_
char *k
= NULL
;
407 r
= unit_full_printf(UNIT(s
), rvalue
, &k
);
409 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue
);
413 r
= socket_address_parse_netlink(&p
->address
, k
);
415 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse address value in '%s', ignoring: %m", k
);
419 p
->type
= SOCKET_SOCKET
;
422 _cleanup_free_
char *k
= NULL
;
424 r
= unit_full_printf(UNIT(s
), rvalue
, &k
);
426 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue
);
430 if (k
[0] == '/') { /* Only for AF_UNIX file system sockets… */
431 r
= patch_var_run(unit
, filename
, line
, lvalue
, &k
);
436 r
= socket_address_parse_and_warn(&p
->address
, k
);
438 if (r
!= -EAFNOSUPPORT
)
439 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse address value in '%s', ignoring: %m", k
);
443 if (streq(lvalue
, "ListenStream"))
444 p
->address
.type
= SOCK_STREAM
;
445 else if (streq(lvalue
, "ListenDatagram"))
446 p
->address
.type
= SOCK_DGRAM
;
448 assert(streq(lvalue
, "ListenSequentialPacket"));
449 p
->address
.type
= SOCK_SEQPACKET
;
452 if (socket_address_family(&p
->address
) != AF_LOCAL
&& p
->address
.type
== SOCK_SEQPACKET
) {
453 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Address family not supported, ignoring: %s", rvalue
);
457 p
->type
= SOCKET_SOCKET
;
461 p
->auxiliary_fds
= NULL
;
462 p
->n_auxiliary_fds
= 0;
465 LIST_FIND_TAIL(port
, s
->ports
, tail
);
466 LIST_INSERT_AFTER(port
, s
->ports
, tail
, p
);
473 int config_parse_exec_nice(
475 const char *filename
,
478 unsigned section_line
,
485 ExecContext
*c
= data
;
493 if (isempty(rvalue
)) {
498 r
= parse_nice(rvalue
, &priority
);
501 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Nice priority out of range, ignoring: %s", rvalue
);
503 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse nice priority '%s', ignoring: %m", rvalue
);
513 int config_parse_exec_oom_score_adjust(
515 const char *filename
,
518 unsigned section_line
,
525 ExecContext
*c
= data
;
533 if (isempty(rvalue
)) {
534 c
->oom_score_adjust_set
= false;
538 r
= parse_oom_score_adjust(rvalue
, &oa
);
541 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "OOM score adjust value out of range, ignoring: %s", rvalue
);
543 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse the OOM score adjust value '%s', ignoring: %m", rvalue
);
547 c
->oom_score_adjust
= oa
;
548 c
->oom_score_adjust_set
= true;
553 int config_parse_exec(
555 const char *filename
,
558 unsigned section_line
,
565 ExecCommand
**e
= data
;
577 rvalue
+= strspn(rvalue
, WHITESPACE
);
579 if (isempty(rvalue
)) {
580 /* An empty assignment resets the list */
581 *e
= exec_command_free_list(*e
);
587 _cleanup_free_
char *path
= NULL
, *firstword
= NULL
;
588 ExecCommandFlags flags
= 0;
589 bool ignore
= false, separate_argv0
= false;
590 _cleanup_free_ ExecCommand
*nce
= NULL
;
591 _cleanup_strv_free_
char **n
= NULL
;
592 size_t nlen
= 0, nbufsize
= 0;
597 r
= extract_first_word_and_warn(&p
, &firstword
, NULL
, EXTRACT_QUOTES
|EXTRACT_CUNESCAPE
, unit
, filename
, line
, rvalue
);
603 /* We accept an absolute path as first argument. If it's prefixed with - and the path doesn't
604 * exist, we ignore it instead of erroring out; if it's prefixed with @, we allow overriding of
605 * argv[0]; if it's prefixed with :, we will not do environment variable substitution;
606 * if it's prefixed with +, it will be run with full privileges and no sandboxing; if
607 * it's prefixed with '!' we apply sandboxing, but do not change user/group credentials; if
608 * it's prefixed with '!!', then we apply user/group credentials if the kernel supports ambient
609 * capabilities -- if it doesn't we don't apply the credentials themselves, but do apply most
610 * other sandboxing, with some special exceptions for changing UID.
612 * The idea is that '!!' may be used to write services that can take benefit of systemd's
613 * UID/GID dropping if the kernel supports ambient creds, but provide an automatic fallback to
614 * privilege dropping within the daemon if the kernel does not offer that. */
616 if (*f
== '-' && !(flags
& EXEC_COMMAND_IGNORE_FAILURE
)) {
617 flags
|= EXEC_COMMAND_IGNORE_FAILURE
;
619 } else if (*f
== '@' && !separate_argv0
)
620 separate_argv0
= true;
621 else if (*f
== ':' && !(flags
& EXEC_COMMAND_NO_ENV_EXPAND
))
622 flags
|= EXEC_COMMAND_NO_ENV_EXPAND
;
623 else if (*f
== '+' && !(flags
& (EXEC_COMMAND_FULLY_PRIVILEGED
|EXEC_COMMAND_NO_SETUID
|EXEC_COMMAND_AMBIENT_MAGIC
)))
624 flags
|= EXEC_COMMAND_FULLY_PRIVILEGED
;
625 else if (*f
== '!' && !(flags
& (EXEC_COMMAND_FULLY_PRIVILEGED
|EXEC_COMMAND_NO_SETUID
|EXEC_COMMAND_AMBIENT_MAGIC
)))
626 flags
|= EXEC_COMMAND_NO_SETUID
;
627 else if (*f
== '!' && !(flags
& (EXEC_COMMAND_FULLY_PRIVILEGED
|EXEC_COMMAND_AMBIENT_MAGIC
))) {
628 flags
&= ~EXEC_COMMAND_NO_SETUID
;
629 flags
|= EXEC_COMMAND_AMBIENT_MAGIC
;
635 r
= unit_full_printf(u
, f
, &path
);
637 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
638 "Failed to resolve unit specifiers in '%s'%s: %m",
639 f
, ignore
? ", ignoring" : "");
640 return ignore
? 0 : -ENOEXEC
;
644 /* First word is either "-" or "@" with no command. */
645 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
646 "Empty path in command line%s: '%s'",
647 ignore
? ", ignoring" : "", rvalue
);
648 return ignore
? 0 : -ENOEXEC
;
650 if (!string_is_safe(path
)) {
651 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
652 "Executable name contains special characters%s: %s",
653 ignore
? ", ignoring" : "", path
);
654 return ignore
? 0 : -ENOEXEC
;
656 if (endswith(path
, "/")) {
657 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
658 "Executable path specifies a directory%s: %s",
659 ignore
? ", ignoring" : "", path
);
660 return ignore
? 0 : -ENOEXEC
;
663 if (!path_is_absolute(path
)) {
667 if (!filename_is_valid(path
)) {
668 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
669 "Neither a valid executable name nor an absolute path%s: %s",
670 ignore
? ", ignoring" : "", path
);
671 return ignore
? 0 : -ENOEXEC
;
674 /* Resolve a single-component name to a full path */
675 NULSTR_FOREACH(prefix
, DEFAULT_PATH_NULSTR
) {
676 _cleanup_free_
char *fullpath
= NULL
;
678 fullpath
= strjoin(prefix
, "/", path
);
682 if (access(fullpath
, F_OK
) >= 0) {
683 free_and_replace(path
, fullpath
);
690 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
691 "Executable \"%s\" not found in path \"%s\"%s",
692 path
, DEFAULT_PATH
, ignore
? ", ignoring" : "");
693 return ignore
? 0 : -ENOEXEC
;
697 if (!separate_argv0
) {
700 if (!GREEDY_REALLOC(n
, nbufsize
, nlen
+ 2))
710 path_simplify(path
, false);
712 while (!isempty(p
)) {
713 _cleanup_free_
char *word
= NULL
, *resolved
= NULL
;
715 /* Check explicitly for an unquoted semicolon as
716 * command separator token. */
717 if (p
[0] == ';' && (!p
[1] || strchr(WHITESPACE
, p
[1]))) {
719 p
+= strspn(p
, WHITESPACE
);
724 /* Check for \; explicitly, to not confuse it with \\; or "\;" or "\\;" etc.
725 * extract_first_word() would return the same for all of those. */
726 if (p
[0] == '\\' && p
[1] == ';' && (!p
[2] || strchr(WHITESPACE
, p
[2]))) {
730 p
+= strspn(p
, WHITESPACE
);
732 if (!GREEDY_REALLOC(n
, nbufsize
, nlen
+ 2))
743 r
= extract_first_word_and_warn(&p
, &word
, NULL
, EXTRACT_QUOTES
|EXTRACT_CUNESCAPE
, unit
, filename
, line
, rvalue
);
747 return ignore
? 0 : -ENOEXEC
;
749 r
= unit_full_printf(u
, word
, &resolved
);
751 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
752 "Failed to resolve unit specifiers in %s%s: %m",
753 word
, ignore
? ", ignoring" : "");
754 return ignore
? 0 : -ENOEXEC
;
757 if (!GREEDY_REALLOC(n
, nbufsize
, nlen
+ 2))
760 n
[nlen
++] = TAKE_PTR(resolved
);
765 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
766 "Empty executable name or zeroeth argument%s: %s",
767 ignore
? ", ignoring" : "", rvalue
);
768 return ignore
? 0 : -ENOEXEC
;
771 nce
= new0(ExecCommand
, 1);
775 nce
->argv
= TAKE_PTR(n
);
776 nce
->path
= TAKE_PTR(path
);
779 exec_command_append_list(e
, nce
);
781 /* Do not _cleanup_free_ these. */
790 int config_parse_socket_bindtodevice(
792 const char *filename
,
795 unsigned section_line
,
809 if (isempty(rvalue
) || streq(rvalue
, "*")) {
810 s
->bind_to_device
= mfree(s
->bind_to_device
);
814 if (!ifname_valid(rvalue
)) {
815 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid interface name, ignoring: %s", rvalue
);
819 if (free_and_strdup(&s
->bind_to_device
, rvalue
) < 0)
825 int config_parse_exec_input(
827 const char *filename
,
830 unsigned section_line
,
837 ExecContext
*c
= data
;
848 n
= startswith(rvalue
, "fd:");
850 _cleanup_free_
char *resolved
= NULL
;
852 r
= unit_full_printf(u
, n
, &resolved
);
854 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in '%s': %m", n
);
856 if (isempty(resolved
))
857 resolved
= mfree(resolved
);
858 else if (!fdname_is_valid(resolved
)) {
859 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid file descriptor name: %s", resolved
);
863 free_and_replace(c
->stdio_fdname
[STDIN_FILENO
], resolved
);
865 ei
= EXEC_INPUT_NAMED_FD
;
867 } else if ((n
= startswith(rvalue
, "file:"))) {
868 _cleanup_free_
char *resolved
= NULL
;
870 r
= unit_full_printf(u
, n
, &resolved
);
872 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in '%s': %m", n
);
874 r
= path_simplify_and_warn(resolved
, PATH_CHECK_ABSOLUTE
| PATH_CHECK_FATAL
, unit
, filename
, line
, lvalue
);
878 free_and_replace(c
->stdio_file
[STDIN_FILENO
], resolved
);
880 ei
= EXEC_INPUT_FILE
;
883 ei
= exec_input_from_string(rvalue
);
885 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse input specifier, ignoring: %s", rvalue
);
894 int config_parse_exec_input_text(
896 const char *filename
,
899 unsigned section_line
,
906 _cleanup_free_
char *unescaped
= NULL
, *resolved
= NULL
;
907 ExecContext
*c
= data
;
918 if (isempty(rvalue
)) {
919 /* Reset if the empty string is assigned */
920 c
->stdin_data
= mfree(c
->stdin_data
);
921 c
->stdin_data_size
= 0;
925 r
= cunescape(rvalue
, 0, &unescaped
);
927 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to decode C escaped text '%s': %m", rvalue
);
929 r
= unit_full_printf(u
, unescaped
, &resolved
);
931 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in '%s': %m", unescaped
);
933 sz
= strlen(resolved
);
934 if (c
->stdin_data_size
+ sz
+ 1 < c
->stdin_data_size
|| /* check for overflow */
935 c
->stdin_data_size
+ sz
+ 1 > EXEC_STDIN_DATA_MAX
) {
936 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
);
940 p
= realloc(c
->stdin_data
, c
->stdin_data_size
+ sz
+ 1);
944 *((char*) mempcpy((char*) p
+ c
->stdin_data_size
, resolved
, sz
)) = '\n';
947 c
->stdin_data_size
+= sz
+ 1;
952 int config_parse_exec_input_data(
954 const char *filename
,
957 unsigned section_line
,
964 _cleanup_free_
void *p
= NULL
;
965 ExecContext
*c
= data
;
975 if (isempty(rvalue
)) {
976 /* Reset if the empty string is assigned */
977 c
->stdin_data
= mfree(c
->stdin_data
);
978 c
->stdin_data_size
= 0;
982 r
= unbase64mem(rvalue
, (size_t) -1, &p
, &sz
);
984 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to decode base64 data, ignoring: %s", rvalue
);
988 if (c
->stdin_data_size
+ sz
< c
->stdin_data_size
|| /* check for overflow */
989 c
->stdin_data_size
+ sz
> EXEC_STDIN_DATA_MAX
) {
990 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
);
994 q
= realloc(c
->stdin_data
, c
->stdin_data_size
+ sz
);
998 memcpy((uint8_t*) q
+ c
->stdin_data_size
, p
, sz
);
1001 c
->stdin_data_size
+= sz
;
1006 int config_parse_exec_output(
1008 const char *filename
,
1010 const char *section
,
1011 unsigned section_line
,
1018 _cleanup_free_
char *resolved
= NULL
;
1020 ExecContext
*c
= data
;
1031 n
= startswith(rvalue
, "fd:");
1033 r
= unit_full_printf(u
, n
, &resolved
);
1035 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in %s: %m", n
);
1037 if (isempty(resolved
))
1038 resolved
= mfree(resolved
);
1039 else if (!fdname_is_valid(resolved
)) {
1040 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid file descriptor name: %s", resolved
);
1044 eo
= EXEC_OUTPUT_NAMED_FD
;
1046 } else if ((n
= startswith(rvalue
, "file:"))) {
1048 r
= unit_full_printf(u
, n
, &resolved
);
1050 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in %s: %m", n
);
1052 r
= path_simplify_and_warn(resolved
, PATH_CHECK_ABSOLUTE
| PATH_CHECK_FATAL
, unit
, filename
, line
, lvalue
);
1056 eo
= EXEC_OUTPUT_FILE
;
1058 } else if ((n
= startswith(rvalue
, "append:"))) {
1060 r
= unit_full_printf(u
, n
, &resolved
);
1062 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in %s: %m", n
);
1064 r
= path_simplify_and_warn(resolved
, PATH_CHECK_ABSOLUTE
| PATH_CHECK_FATAL
, unit
, filename
, line
, lvalue
);
1068 eo
= EXEC_OUTPUT_FILE_APPEND
;
1070 eo
= exec_output_from_string(rvalue
);
1072 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse output specifier, ignoring: %s", rvalue
);
1077 if (streq(lvalue
, "StandardOutput")) {
1078 if (eo
== EXEC_OUTPUT_NAMED_FD
)
1079 free_and_replace(c
->stdio_fdname
[STDOUT_FILENO
], resolved
);
1081 free_and_replace(c
->stdio_file
[STDOUT_FILENO
], resolved
);
1086 assert(streq(lvalue
, "StandardError"));
1088 if (eo
== EXEC_OUTPUT_NAMED_FD
)
1089 free_and_replace(c
->stdio_fdname
[STDERR_FILENO
], resolved
);
1091 free_and_replace(c
->stdio_file
[STDERR_FILENO
], resolved
);
1099 int config_parse_exec_io_class(const char *unit
,
1100 const char *filename
,
1102 const char *section
,
1103 unsigned section_line
,
1110 ExecContext
*c
= data
;
1118 if (isempty(rvalue
)) {
1119 c
->ioprio_set
= false;
1120 c
->ioprio
= IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE
, 0);
1124 x
= ioprio_class_from_string(rvalue
);
1126 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse IO scheduling class, ignoring: %s", rvalue
);
1130 c
->ioprio
= IOPRIO_PRIO_VALUE(x
, IOPRIO_PRIO_DATA(c
->ioprio
));
1131 c
->ioprio_set
= true;
1136 int config_parse_exec_io_priority(const char *unit
,
1137 const char *filename
,
1139 const char *section
,
1140 unsigned section_line
,
1147 ExecContext
*c
= data
;
1155 if (isempty(rvalue
)) {
1156 c
->ioprio_set
= false;
1157 c
->ioprio
= IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE
, 0);
1161 r
= ioprio_parse_priority(rvalue
, &i
);
1163 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse IO priority, ignoring: %s", rvalue
);
1167 c
->ioprio
= IOPRIO_PRIO_VALUE(IOPRIO_PRIO_CLASS(c
->ioprio
), i
);
1168 c
->ioprio_set
= true;
1173 int config_parse_exec_cpu_sched_policy(const char *unit
,
1174 const char *filename
,
1176 const char *section
,
1177 unsigned section_line
,
1184 ExecContext
*c
= data
;
1192 if (isempty(rvalue
)) {
1193 c
->cpu_sched_set
= false;
1194 c
->cpu_sched_policy
= SCHED_OTHER
;
1195 c
->cpu_sched_priority
= 0;
1199 x
= sched_policy_from_string(rvalue
);
1201 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse CPU scheduling policy, ignoring: %s", rvalue
);
1205 c
->cpu_sched_policy
= x
;
1206 /* Moving to or from real-time policy? We need to adjust the priority */
1207 c
->cpu_sched_priority
= CLAMP(c
->cpu_sched_priority
, sched_get_priority_min(x
), sched_get_priority_max(x
));
1208 c
->cpu_sched_set
= true;
1213 int config_parse_exec_cpu_sched_prio(const char *unit
,
1214 const char *filename
,
1216 const char *section
,
1217 unsigned section_line
,
1224 ExecContext
*c
= data
;
1232 r
= safe_atoi(rvalue
, &i
);
1234 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse CPU scheduling priority, ignoring: %s", rvalue
);
1238 /* On Linux RR/FIFO range from 1 to 99 and OTHER/BATCH may only be 0 */
1239 min
= sched_get_priority_min(c
->cpu_sched_policy
);
1240 max
= sched_get_priority_max(c
->cpu_sched_policy
);
1242 if (i
< min
|| i
> max
) {
1243 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "CPU scheduling priority is out of range, ignoring: %s", rvalue
);
1247 c
->cpu_sched_priority
= i
;
1248 c
->cpu_sched_set
= true;
1253 int config_parse_exec_cpu_affinity(const char *unit
,
1254 const char *filename
,
1256 const char *section
,
1257 unsigned section_line
,
1264 ExecContext
*c
= data
;
1265 _cleanup_cpu_free_ cpu_set_t
*cpuset
= NULL
;
1273 ncpus
= parse_cpu_set_and_warn(rvalue
, &cpuset
, unit
, filename
, line
, lvalue
);
1278 /* An empty assignment resets the CPU list */
1279 c
->cpuset
= cpu_set_mfree(c
->cpuset
);
1280 c
->cpuset_ncpus
= 0;
1285 c
->cpuset
= TAKE_PTR(cpuset
);
1286 c
->cpuset_ncpus
= (unsigned) ncpus
;
1290 if (c
->cpuset_ncpus
< (unsigned) ncpus
) {
1291 CPU_OR_S(CPU_ALLOC_SIZE(c
->cpuset_ncpus
), cpuset
, c
->cpuset
, cpuset
);
1292 CPU_FREE(c
->cpuset
);
1293 c
->cpuset
= TAKE_PTR(cpuset
);
1294 c
->cpuset_ncpus
= (unsigned) ncpus
;
1298 CPU_OR_S(CPU_ALLOC_SIZE((unsigned) ncpus
), c
->cpuset
, c
->cpuset
, cpuset
);
1303 int config_parse_capability_set(
1305 const char *filename
,
1307 const char *section
,
1308 unsigned section_line
,
1315 uint64_t *capability_set
= data
;
1316 uint64_t sum
= 0, initial
= 0;
1317 bool invert
= false;
1325 if (rvalue
[0] == '~') {
1330 if (streq(lvalue
, "CapabilityBoundingSet"))
1331 initial
= CAP_ALL
; /* initialized to all bits on */
1332 /* else "AmbientCapabilities" initialized to all bits off */
1334 r
= capability_set_from_string(rvalue
, &sum
);
1336 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse %s= specifier '%s', ignoring: %m", lvalue
, rvalue
);
1340 if (sum
== 0 || *capability_set
== initial
)
1341 /* "", "~" or uninitialized data -> replace */
1342 *capability_set
= invert
? ~sum
: sum
;
1344 /* previous data -> merge */
1346 *capability_set
&= ~sum
;
1348 *capability_set
|= sum
;
1354 int config_parse_exec_selinux_context(
1356 const char *filename
,
1358 const char *section
,
1359 unsigned section_line
,
1366 ExecContext
*c
= data
;
1377 if (isempty(rvalue
)) {
1378 c
->selinux_context
= mfree(c
->selinux_context
);
1379 c
->selinux_context_ignore
= false;
1383 if (rvalue
[0] == '-') {
1389 r
= unit_full_printf(u
, rvalue
, &k
);
1391 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1392 "Failed to resolve unit specifiers in '%s'%s: %m",
1393 rvalue
, ignore
? ", ignoring" : "");
1394 return ignore
? 0 : -ENOEXEC
;
1397 free_and_replace(c
->selinux_context
, k
);
1398 c
->selinux_context_ignore
= ignore
;
1403 int config_parse_exec_apparmor_profile(
1405 const char *filename
,
1407 const char *section
,
1408 unsigned section_line
,
1415 ExecContext
*c
= data
;
1426 if (isempty(rvalue
)) {
1427 c
->apparmor_profile
= mfree(c
->apparmor_profile
);
1428 c
->apparmor_profile_ignore
= false;
1432 if (rvalue
[0] == '-') {
1438 r
= unit_full_printf(u
, rvalue
, &k
);
1440 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1441 "Failed to resolve unit specifiers in '%s'%s: %m",
1442 rvalue
, ignore
? ", ignoring" : "");
1443 return ignore
? 0 : -ENOEXEC
;
1446 free_and_replace(c
->apparmor_profile
, k
);
1447 c
->apparmor_profile_ignore
= ignore
;
1452 int config_parse_exec_smack_process_label(
1454 const char *filename
,
1456 const char *section
,
1457 unsigned section_line
,
1464 ExecContext
*c
= data
;
1475 if (isempty(rvalue
)) {
1476 c
->smack_process_label
= mfree(c
->smack_process_label
);
1477 c
->smack_process_label_ignore
= false;
1481 if (rvalue
[0] == '-') {
1487 r
= unit_full_printf(u
, rvalue
, &k
);
1489 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1490 "Failed to resolve unit specifiers in '%s'%s: %m",
1491 rvalue
, ignore
? ", ignoring" : "");
1492 return ignore
? 0 : -ENOEXEC
;
1495 free_and_replace(c
->smack_process_label
, k
);
1496 c
->smack_process_label_ignore
= ignore
;
1501 int config_parse_timer(
1503 const char *filename
,
1505 const char *section
,
1506 unsigned section_line
,
1513 _cleanup_(calendar_spec_freep
) CalendarSpec
*c
= NULL
;
1514 _cleanup_free_
char *k
= NULL
;
1526 if (isempty(rvalue
)) {
1527 /* Empty assignment resets list */
1528 timer_free_values(t
);
1532 r
= unit_full_printf(u
, rvalue
, &k
);
1534 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue
);
1538 if (ltype
== TIMER_CALENDAR
) {
1539 r
= calendar_spec_from_string(k
, &c
);
1541 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse calendar specification, ignoring: %s", k
);
1545 r
= parse_sec(k
, &usec
);
1547 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse timer value, ignoring: %s", k
);
1552 v
= new(TimerValue
, 1);
1559 .calendar_spec
= TAKE_PTR(c
),
1562 LIST_PREPEND(value
, t
->values
, v
);
1567 int config_parse_trigger_unit(
1569 const char *filename
,
1571 const char *section
,
1572 unsigned section_line
,
1579 _cleanup_free_
char *p
= NULL
;
1589 if (!hashmap_isempty(u
->dependencies
[UNIT_TRIGGERS
])) {
1590 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Multiple units to trigger specified, ignoring: %s", rvalue
);
1594 r
= unit_name_printf(u
, rvalue
, &p
);
1596 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue
);
1600 type
= unit_name_to_type(p
);
1602 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Unit type not valid, ignoring: %s", rvalue
);
1605 if (unit_has_name(u
, p
)) {
1606 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Units cannot trigger themselves, ignoring: %s", rvalue
);
1610 r
= unit_add_two_dependencies_by_name(u
, UNIT_BEFORE
, UNIT_TRIGGERS
, p
, true, UNIT_DEPENDENCY_FILE
);
1612 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to add trigger on %s, ignoring: %m", p
);
1619 int config_parse_path_spec(const char *unit
,
1620 const char *filename
,
1622 const char *section
,
1623 unsigned section_line
,
1633 _cleanup_free_
char *k
= NULL
;
1641 if (isempty(rvalue
)) {
1642 /* Empty assignment clears list */
1647 b
= path_type_from_string(lvalue
);
1649 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse path type, ignoring: %s", lvalue
);
1653 r
= unit_full_printf(UNIT(p
), rvalue
, &k
);
1655 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue
);
1659 r
= path_simplify_and_warn(k
, PATH_CHECK_ABSOLUTE
, unit
, filename
, line
, lvalue
);
1663 s
= new0(PathSpec
, 1);
1668 s
->path
= TAKE_PTR(k
);
1672 LIST_PREPEND(spec
, p
->specs
, s
);
1677 int config_parse_socket_service(
1679 const char *filename
,
1681 const char *section
,
1682 unsigned section_line
,
1689 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1690 _cleanup_free_
char *p
= NULL
;
1700 r
= unit_name_printf(UNIT(s
), rvalue
, &p
);
1702 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in %s: %m", rvalue
);
1706 if (!endswith(p
, ".service")) {
1707 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Unit must be of type service: %s", rvalue
);
1711 r
= manager_load_unit(UNIT(s
)->manager
, p
, NULL
, &error
, &x
);
1713 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to load unit %s: %s", rvalue
, bus_error_message(&error
, r
));
1717 unit_ref_set(&s
->service
, UNIT(s
), x
);
1722 int config_parse_fdname(
1724 const char *filename
,
1726 const char *section
,
1727 unsigned section_line
,
1734 _cleanup_free_
char *p
= NULL
;
1743 if (isempty(rvalue
)) {
1744 s
->fdname
= mfree(s
->fdname
);
1748 r
= unit_full_printf(UNIT(s
), rvalue
, &p
);
1750 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue
);
1754 if (!fdname_is_valid(p
)) {
1755 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid file descriptor name, ignoring: %s", p
);
1759 return free_and_replace(s
->fdname
, p
);
1762 int config_parse_service_sockets(
1764 const char *filename
,
1766 const char *section
,
1767 unsigned section_line
,
1785 _cleanup_free_
char *word
= NULL
, *k
= NULL
;
1787 r
= extract_first_word(&p
, &word
, NULL
, 0);
1793 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Trailing garbage in sockets, ignoring: %s", rvalue
);
1797 r
= unit_name_printf(UNIT(s
), word
, &k
);
1799 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in '%s', ignoring: %m", word
);
1803 if (!endswith(k
, ".socket")) {
1804 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Unit must be of type socket, ignoring: %s", k
);
1808 r
= unit_add_two_dependencies_by_name(UNIT(s
), UNIT_WANTS
, UNIT_AFTER
, k
, true, UNIT_DEPENDENCY_FILE
);
1810 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to add dependency on %s, ignoring: %m", k
);
1812 r
= unit_add_dependency_by_name(UNIT(s
), UNIT_TRIGGERED_BY
, k
, true, UNIT_DEPENDENCY_FILE
);
1814 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to add dependency on %s, ignoring: %m", k
);
1820 int config_parse_bus_name(
1822 const char *filename
,
1824 const char *section
,
1825 unsigned section_line
,
1832 _cleanup_free_
char *k
= NULL
;
1841 r
= unit_full_printf(u
, rvalue
, &k
);
1843 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue
);
1847 if (!service_name_is_valid(k
)) {
1848 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid bus name, ignoring: %s", k
);
1852 return config_parse_string(unit
, filename
, line
, section
, section_line
, lvalue
, ltype
, k
, data
, userdata
);
1855 int config_parse_service_timeout(
1857 const char *filename
,
1859 const char *section
,
1860 unsigned section_line
,
1867 Service
*s
= userdata
;
1876 /* This is called for two cases: TimeoutSec= and TimeoutStartSec=. */
1878 /* Traditionally, these options accepted 0 to disable the timeouts. However, a timeout of 0 suggests it happens
1879 * immediately, hence fix this to become USEC_INFINITY instead. This is in-line with how we internally handle
1880 * all other timeouts. */
1881 r
= parse_sec_fix_0(rvalue
, &usec
);
1883 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse %s= parameter, ignoring: %s", lvalue
, rvalue
);
1887 s
->start_timeout_defined
= true;
1888 s
->timeout_start_usec
= usec
;
1890 if (streq(lvalue
, "TimeoutSec"))
1891 s
->timeout_stop_usec
= usec
;
1896 int config_parse_sec_fix_0(
1898 const char *filename
,
1900 const char *section
,
1901 unsigned section_line
,
1908 usec_t
*usec
= data
;
1916 /* This is pretty much like config_parse_sec(), except that this treats a time of 0 as infinity, for
1917 * compatibility with older versions of systemd where 0 instead of infinity was used as indicator to turn off a
1920 r
= parse_sec_fix_0(rvalue
, usec
);
1922 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse %s= parameter, ignoring: %s", lvalue
, rvalue
);
1929 int config_parse_user_group(
1931 const char *filename
,
1933 const char *section
,
1934 unsigned section_line
,
1941 _cleanup_free_
char *k
= NULL
;
1951 if (isempty(rvalue
)) {
1952 *user
= mfree(*user
);
1956 r
= unit_full_printf(u
, rvalue
, &k
);
1958 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in %s: %m", rvalue
);
1962 if (!valid_user_group_name_or_id(k
)) {
1963 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid user/group name or numeric ID: %s", k
);
1967 return free_and_replace(*user
, k
);
1970 int config_parse_user_group_strv(
1972 const char *filename
,
1974 const char *section
,
1975 unsigned section_line
,
1982 char ***users
= data
;
1984 const char *p
= rvalue
;
1992 if (isempty(rvalue
)) {
1993 *users
= strv_free(*users
);
1998 _cleanup_free_
char *word
= NULL
, *k
= NULL
;
2000 r
= extract_first_word(&p
, &word
, NULL
, 0);
2006 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid syntax: %s", rvalue
);
2010 r
= unit_full_printf(u
, word
, &k
);
2012 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in %s: %m", word
);
2016 if (!valid_user_group_name_or_id(k
)) {
2017 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid user/group name or numeric ID: %s", k
);
2021 r
= strv_push(users
, k
);
2031 int config_parse_working_directory(
2033 const char *filename
,
2035 const char *section
,
2036 unsigned section_line
,
2043 ExecContext
*c
= data
;
2054 if (isempty(rvalue
)) {
2055 c
->working_directory_home
= false;
2056 c
->working_directory
= mfree(c
->working_directory
);
2060 if (rvalue
[0] == '-') {
2066 if (streq(rvalue
, "~")) {
2067 c
->working_directory_home
= true;
2068 c
->working_directory
= mfree(c
->working_directory
);
2070 _cleanup_free_
char *k
= NULL
;
2072 r
= unit_full_printf(u
, rvalue
, &k
);
2074 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
2075 "Failed to resolve unit specifiers in working directory path '%s'%s: %m",
2076 rvalue
, missing_ok
? ", ignoring" : "");
2077 return missing_ok
? 0 : -ENOEXEC
;
2080 r
= path_simplify_and_warn(k
, PATH_CHECK_ABSOLUTE
| (missing_ok
? 0 : PATH_CHECK_FATAL
), unit
, filename
, line
, lvalue
);
2082 return missing_ok
? 0 : -ENOEXEC
;
2084 c
->working_directory_home
= false;
2085 free_and_replace(c
->working_directory
, k
);
2088 c
->working_directory_missing_ok
= missing_ok
;
2092 int config_parse_unit_env_file(const char *unit
,
2093 const char *filename
,
2095 const char *section
,
2096 unsigned section_line
,
2105 _cleanup_free_
char *n
= NULL
;
2113 if (isempty(rvalue
)) {
2114 /* Empty assignment frees the list */
2115 *env
= strv_free(*env
);
2119 r
= unit_full_printf(u
, rvalue
, &n
);
2121 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue
);
2125 r
= path_simplify_and_warn(n
[0] == '-' ? n
+ 1 : n
, PATH_CHECK_ABSOLUTE
, unit
, filename
, line
, lvalue
);
2129 r
= strv_push(env
, n
);
2138 int config_parse_environ(
2140 const char *filename
,
2142 const char *section
,
2143 unsigned section_line
,
2160 if (isempty(rvalue
)) {
2161 /* Empty assignment resets the list */
2162 *env
= strv_free(*env
);
2166 for (p
= rvalue
;; ) {
2167 _cleanup_free_
char *word
= NULL
, *k
= NULL
;
2169 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_CUNESCAPE
|EXTRACT_QUOTES
);
2175 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
2176 "Invalid syntax, ignoring: %s", rvalue
);
2181 r
= unit_full_printf(u
, word
, &k
);
2183 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
2184 "Failed to resolve unit specifiers in %s, ignoring: %m", word
);
2190 if (!env_assignment_is_valid(k
)) {
2191 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
2192 "Invalid environment assignment, ignoring: %s", k
);
2196 r
= strv_env_replace(env
, k
);
2204 int config_parse_pass_environ(
2206 const char *filename
,
2208 const char *section
,
2209 unsigned section_line
,
2216 _cleanup_strv_free_
char **n
= NULL
;
2217 size_t nlen
= 0, nbufsize
= 0;
2218 char*** passenv
= data
;
2219 const char *p
= rvalue
;
2228 if (isempty(rvalue
)) {
2229 /* Empty assignment resets the list */
2230 *passenv
= strv_free(*passenv
);
2235 _cleanup_free_
char *word
= NULL
, *k
= NULL
;
2237 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
);
2243 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
2244 "Trailing garbage in %s, ignoring: %s", lvalue
, rvalue
);
2249 r
= unit_full_printf(u
, word
, &k
);
2251 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
2252 "Failed to resolve specifiers in %s, ignoring: %m", word
);
2258 if (!env_name_is_valid(k
)) {
2259 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
2260 "Invalid environment name for %s, ignoring: %s", lvalue
, k
);
2264 if (!GREEDY_REALLOC(n
, nbufsize
, nlen
+ 2))
2267 n
[nlen
++] = TAKE_PTR(k
);
2272 r
= strv_extend_strv(passenv
, n
, true);
2280 int config_parse_unset_environ(
2282 const char *filename
,
2284 const char *section
,
2285 unsigned section_line
,
2292 _cleanup_strv_free_
char **n
= NULL
;
2293 size_t nlen
= 0, nbufsize
= 0;
2294 char*** unsetenv
= data
;
2295 const char *p
= rvalue
;
2304 if (isempty(rvalue
)) {
2305 /* Empty assignment resets the list */
2306 *unsetenv
= strv_free(*unsetenv
);
2311 _cleanup_free_
char *word
= NULL
, *k
= NULL
;
2313 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_CUNESCAPE
|EXTRACT_QUOTES
);
2319 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
2320 "Trailing garbage in %s, ignoring: %s", lvalue
, rvalue
);
2325 r
= unit_full_printf(u
, word
, &k
);
2327 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
2328 "Failed to resolve unit specifiers in %s, ignoring: %m", word
);
2334 if (!env_assignment_is_valid(k
) && !env_name_is_valid(k
)) {
2335 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
2336 "Invalid environment name or assignment %s, ignoring: %s", lvalue
, k
);
2340 if (!GREEDY_REALLOC(n
, nbufsize
, nlen
+ 2))
2343 n
[nlen
++] = TAKE_PTR(k
);
2348 r
= strv_extend_strv(unsetenv
, n
, true);
2356 int config_parse_log_extra_fields(
2358 const char *filename
,
2360 const char *section
,
2361 unsigned section_line
,
2368 ExecContext
*c
= data
;
2370 const char *p
= rvalue
;
2378 if (isempty(rvalue
)) {
2379 exec_context_free_log_extra_fields(c
);
2384 _cleanup_free_
char *word
= NULL
, *k
= NULL
;
2388 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_CUNESCAPE
|EXTRACT_QUOTES
);
2394 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Invalid syntax, ignoring: %s", rvalue
);
2398 r
= unit_full_printf(u
, word
, &k
);
2400 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in %s, ignoring: %m", word
);
2404 eq
= strchr(k
, '=');
2406 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Log field lacks '=' character, ignoring: %s", k
);
2410 if (!journal_field_valid(k
, eq
-k
, false)) {
2411 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Log field name is invalid, ignoring: %s", k
);
2415 t
= reallocarray(c
->log_extra_fields
, c
->n_log_extra_fields
+1, sizeof(struct iovec
));
2419 c
->log_extra_fields
= t
;
2420 c
->log_extra_fields
[c
->n_log_extra_fields
++] = IOVEC_MAKE_STRING(k
);
2426 int config_parse_unit_condition_path(
2428 const char *filename
,
2430 const char *section
,
2431 unsigned section_line
,
2438 _cleanup_free_
char *p
= NULL
;
2439 Condition
**list
= data
, *c
;
2440 ConditionType t
= ltype
;
2441 bool trigger
, negate
;
2450 if (isempty(rvalue
)) {
2451 /* Empty assignment resets the list */
2452 *list
= condition_free_list(*list
);
2456 trigger
= rvalue
[0] == '|';
2460 negate
= rvalue
[0] == '!';
2464 r
= unit_full_printf(u
, rvalue
, &p
);
2466 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue
);
2470 r
= path_simplify_and_warn(p
, PATH_CHECK_ABSOLUTE
, unit
, filename
, line
, lvalue
);
2474 c
= condition_new(t
, p
, trigger
, negate
);
2478 LIST_PREPEND(conditions
, *list
, c
);
2482 int config_parse_unit_condition_string(
2484 const char *filename
,
2486 const char *section
,
2487 unsigned section_line
,
2494 _cleanup_free_
char *s
= NULL
;
2495 Condition
**list
= data
, *c
;
2496 ConditionType t
= ltype
;
2497 bool trigger
, negate
;
2506 if (isempty(rvalue
)) {
2507 /* Empty assignment resets the list */
2508 *list
= condition_free_list(*list
);
2512 trigger
= rvalue
[0] == '|';
2516 negate
= rvalue
[0] == '!';
2520 r
= unit_full_printf(u
, rvalue
, &s
);
2522 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue
);
2526 c
= condition_new(t
, s
, trigger
, negate
);
2530 LIST_PREPEND(conditions
, *list
, c
);
2534 int config_parse_unit_condition_null(
2536 const char *filename
,
2538 const char *section
,
2539 unsigned section_line
,
2546 Condition
**list
= data
, *c
;
2547 bool trigger
, negate
;
2555 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "%s= is deprecated, please do not use.", lvalue
);
2557 if (isempty(rvalue
)) {
2558 /* Empty assignment resets the list */
2559 *list
= condition_free_list(*list
);
2563 trigger
= rvalue
[0] == '|';
2567 negate
= rvalue
[0] == '!';
2571 b
= parse_boolean(rvalue
);
2573 log_syntax(unit
, LOG_ERR
, filename
, line
, b
, "Failed to parse boolean value in condition, ignoring: %s", rvalue
);
2580 c
= condition_new(CONDITION_NULL
, NULL
, trigger
, negate
);
2584 LIST_PREPEND(conditions
, *list
, c
);
2588 int config_parse_unit_requires_mounts_for(
2590 const char *filename
,
2592 const char *section
,
2593 unsigned section_line
,
2600 const char *p
= rvalue
;
2610 _cleanup_free_
char *word
= NULL
, *resolved
= NULL
;
2612 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
);
2618 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
2619 "Invalid syntax, ignoring: %s", rvalue
);
2623 r
= unit_full_printf(u
, word
, &resolved
);
2625 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in '%s', ignoring: %m", word
);
2629 r
= path_simplify_and_warn(resolved
, PATH_CHECK_ABSOLUTE
, unit
, filename
, line
, lvalue
);
2633 r
= unit_require_mounts_for(u
, resolved
, UNIT_DEPENDENCY_FILE
);
2635 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to add required mount '%s', ignoring: %m", resolved
);
2641 int config_parse_documentation(const char *unit
,
2642 const char *filename
,
2644 const char *section
,
2645 unsigned section_line
,
2661 if (isempty(rvalue
)) {
2662 /* Empty assignment resets the list */
2663 u
->documentation
= strv_free(u
->documentation
);
2667 r
= config_parse_unit_strv_printf(unit
, filename
, line
, section
, section_line
, lvalue
, ltype
,
2668 rvalue
, data
, userdata
);
2672 for (a
= b
= u
->documentation
; a
&& *a
; a
++) {
2674 if (documentation_url_is_valid(*a
))
2677 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid URL, ignoring: %s", *a
);
2688 int config_parse_syscall_filter(
2690 const char *filename
,
2692 const char *section
,
2693 unsigned section_line
,
2700 ExecContext
*c
= data
;
2702 bool invert
= false;
2711 if (isempty(rvalue
)) {
2712 /* Empty assignment resets the list */
2713 c
->syscall_filter
= hashmap_free(c
->syscall_filter
);
2714 c
->syscall_whitelist
= false;
2718 if (rvalue
[0] == '~') {
2723 if (!c
->syscall_filter
) {
2724 c
->syscall_filter
= hashmap_new(NULL
);
2725 if (!c
->syscall_filter
)
2729 /* Allow everything but the ones listed */
2730 c
->syscall_whitelist
= false;
2732 /* Allow nothing but the ones listed */
2733 c
->syscall_whitelist
= true;
2735 /* Accept default syscalls if we are on a whitelist */
2736 r
= seccomp_parse_syscall_filter(
2737 "@default", -1, c
->syscall_filter
,
2738 SECCOMP_PARSE_PERMISSIVE
|SECCOMP_PARSE_WHITELIST
,
2748 _cleanup_free_
char *word
= NULL
, *name
= NULL
;
2751 r
= extract_first_word(&p
, &word
, NULL
, 0);
2757 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Invalid syntax, ignoring: %s", rvalue
);
2761 r
= parse_syscall_and_errno(word
, &name
, &num
);
2763 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Failed to parse syscall:errno, ignoring: %s", word
);
2767 r
= seccomp_parse_syscall_filter(
2768 name
, num
, c
->syscall_filter
,
2769 SECCOMP_PARSE_LOG
|SECCOMP_PARSE_PERMISSIVE
|
2770 (invert
? SECCOMP_PARSE_INVERT
: 0)|
2771 (c
->syscall_whitelist
? SECCOMP_PARSE_WHITELIST
: 0),
2772 unit
, filename
, line
);
2778 int config_parse_syscall_archs(
2780 const char *filename
,
2782 const char *section
,
2783 unsigned section_line
,
2790 const char *p
= rvalue
;
2794 if (isempty(rvalue
)) {
2795 *archs
= set_free(*archs
);
2799 r
= set_ensure_allocated(archs
, NULL
);
2804 _cleanup_free_
char *word
= NULL
;
2807 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
);
2813 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
2814 "Invalid syntax, ignoring: %s", rvalue
);
2818 r
= seccomp_arch_from_string(word
, &a
);
2820 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
2821 "Failed to parse system call architecture \"%s\", ignoring: %m", word
);
2825 r
= set_put(*archs
, UINT32_TO_PTR(a
+ 1));
2831 int config_parse_syscall_errno(
2833 const char *filename
,
2835 const char *section
,
2836 unsigned section_line
,
2843 ExecContext
*c
= data
;
2850 if (isempty(rvalue
)) {
2851 /* Empty assignment resets to KILL */
2852 c
->syscall_errno
= 0;
2856 e
= parse_errno(rvalue
);
2858 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse error number, ignoring: %s", rvalue
);
2862 c
->syscall_errno
= e
;
2866 int config_parse_address_families(
2868 const char *filename
,
2870 const char *section
,
2871 unsigned section_line
,
2878 ExecContext
*c
= data
;
2879 bool invert
= false;
2887 if (isempty(rvalue
)) {
2888 /* Empty assignment resets the list */
2889 c
->address_families
= set_free(c
->address_families
);
2890 c
->address_families_whitelist
= false;
2894 if (rvalue
[0] == '~') {
2899 if (!c
->address_families
) {
2900 c
->address_families
= set_new(NULL
);
2901 if (!c
->address_families
)
2904 c
->address_families_whitelist
= !invert
;
2907 for (p
= rvalue
;;) {
2908 _cleanup_free_
char *word
= NULL
;
2911 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
);
2917 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
2918 "Invalid syntax, ignoring: %s", rvalue
);
2922 af
= af_from_name(word
);
2924 log_syntax(unit
, LOG_ERR
, filename
, line
, af
,
2925 "Failed to parse address family, ignoring: %s", word
);
2929 /* If we previously wanted to forbid an address family and now
2930 * we want to allow it, then just remove it from the list.
2932 if (!invert
== c
->address_families_whitelist
) {
2933 r
= set_put(c
->address_families
, INT_TO_PTR(af
));
2937 set_remove(c
->address_families
, INT_TO_PTR(af
));
2941 int config_parse_restrict_namespaces(
2943 const char *filename
,
2945 const char *section
,
2946 unsigned section_line
,
2953 ExecContext
*c
= data
;
2954 unsigned long flags
;
2955 bool invert
= false;
2958 if (isempty(rvalue
)) {
2959 /* Reset to the default. */
2960 c
->restrict_namespaces
= NAMESPACE_FLAGS_INITIAL
;
2964 /* Boolean parameter ignores the previous settings */
2965 r
= parse_boolean(rvalue
);
2967 c
->restrict_namespaces
= 0;
2969 } else if (r
== 0) {
2970 c
->restrict_namespaces
= NAMESPACE_FLAGS_ALL
;
2974 if (rvalue
[0] == '~') {
2979 /* Not a boolean argument, in this case it's a list of namespace types. */
2980 r
= namespace_flags_from_string(rvalue
, &flags
);
2982 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse namespace type string, ignoring: %s", rvalue
);
2986 if (c
->restrict_namespaces
== NAMESPACE_FLAGS_INITIAL
)
2987 /* Initial assignment. Just set the value. */
2988 c
->restrict_namespaces
= invert
? (~flags
) & NAMESPACE_FLAGS_ALL
: flags
;
2990 /* Merge the value with the previous one. */
2991 SET_FLAG(c
->restrict_namespaces
, flags
, !invert
);
2997 int config_parse_unit_slice(
2999 const char *filename
,
3001 const char *section
,
3002 unsigned section_line
,
3009 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
3010 _cleanup_free_
char *k
= NULL
;
3011 Unit
*u
= userdata
, *slice
= NULL
;
3019 r
= unit_name_printf(u
, rvalue
, &k
);
3021 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue
);
3025 r
= manager_load_unit(u
->manager
, k
, NULL
, &error
, &slice
);
3027 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to load slice unit %s, ignoring: %s", k
, bus_error_message(&error
, r
));
3031 r
= unit_set_slice(u
, slice
);
3033 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to assign slice %s to unit %s, ignoring: %m", slice
->id
, u
->id
);
3040 int config_parse_cpu_quota(
3042 const char *filename
,
3044 const char *section
,
3045 unsigned section_line
,
3052 CGroupContext
*c
= data
;
3059 if (isempty(rvalue
)) {
3060 c
->cpu_quota_per_sec_usec
= USEC_INFINITY
;
3064 r
= parse_permille_unbounded(rvalue
);
3066 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid CPU quota '%s', ignoring.", rvalue
);
3070 c
->cpu_quota_per_sec_usec
= ((usec_t
) r
* USEC_PER_SEC
) / 1000U;
3074 int config_parse_memory_limit(
3076 const char *filename
,
3078 const char *section
,
3079 unsigned section_line
,
3086 CGroupContext
*c
= data
;
3087 uint64_t bytes
= CGROUP_LIMIT_MAX
;
3090 if (!isempty(rvalue
) && !streq(rvalue
, "infinity")) {
3092 r
= parse_permille(rvalue
);
3094 r
= parse_size(rvalue
, 1024, &bytes
);
3096 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid memory limit '%s', ignoring: %m", rvalue
);
3100 bytes
= physical_memory_scale(r
, 1000U);
3102 if (bytes
>= UINT64_MAX
||
3103 (bytes
<= 0 && !streq(lvalue
, "MemorySwapMax"))) {
3104 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Memory limit '%s' out of range, ignoring.", rvalue
);
3109 if (streq(lvalue
, "MemoryMin"))
3110 c
->memory_min
= bytes
;
3111 else if (streq(lvalue
, "MemoryLow"))
3112 c
->memory_low
= bytes
;
3113 else if (streq(lvalue
, "MemoryHigh"))
3114 c
->memory_high
= bytes
;
3115 else if (streq(lvalue
, "MemoryMax"))
3116 c
->memory_max
= bytes
;
3117 else if (streq(lvalue
, "MemorySwapMax"))
3118 c
->memory_swap_max
= bytes
;
3119 else if (streq(lvalue
, "MemoryLimit"))
3120 c
->memory_limit
= bytes
;
3127 int config_parse_tasks_max(
3129 const char *filename
,
3131 const char *section
,
3132 unsigned section_line
,
3139 uint64_t *tasks_max
= data
, v
;
3143 if (isempty(rvalue
)) {
3144 *tasks_max
= u
? u
->manager
->default_tasks_max
: UINT64_MAX
;
3148 if (streq(rvalue
, "infinity")) {
3149 *tasks_max
= CGROUP_LIMIT_MAX
;
3153 r
= parse_permille(rvalue
);
3155 r
= safe_atou64(rvalue
, &v
);
3157 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid maximum tasks value '%s', ignoring: %m", rvalue
);
3161 v
= system_tasks_max_scale(r
, 1000U);
3163 if (v
<= 0 || v
>= UINT64_MAX
) {
3164 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Maximum tasks value '%s' out of range, ignoring.", rvalue
);
3172 int config_parse_delegate(
3174 const char *filename
,
3176 const char *section
,
3177 unsigned section_line
,
3184 CGroupContext
*c
= data
;
3188 t
= unit_name_to_type(unit
);
3189 assert(t
!= _UNIT_TYPE_INVALID
);
3191 if (!unit_vtable
[t
]->can_delegate
) {
3192 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Delegate= setting not supported for this unit type, ignoring.");
3196 /* We either accept a boolean value, which may be used to turn on delegation for all controllers, or turn it
3197 * off for all. Or it takes a list of controller names, in which case we add the specified controllers to the
3198 * mask to delegate. */
3200 if (isempty(rvalue
)) {
3201 /* An empty string resets controllers and set Delegate=yes. */
3203 c
->delegate_controllers
= 0;
3207 r
= parse_boolean(rvalue
);
3209 const char *p
= rvalue
;
3210 CGroupMask mask
= 0;
3213 _cleanup_free_
char *word
= NULL
;
3214 CGroupController cc
;
3216 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
);
3222 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid syntax, ignoring: %s", rvalue
);
3226 cc
= cgroup_controller_from_string(word
);
3228 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid controller name '%s', ignoring", word
);
3232 mask
|= CGROUP_CONTROLLER_TO_MASK(cc
);
3236 c
->delegate_controllers
|= mask
;
3240 c
->delegate_controllers
= _CGROUP_MASK_ALL
;
3242 c
->delegate
= false;
3243 c
->delegate_controllers
= 0;
3249 int config_parse_device_allow(
3251 const char *filename
,
3253 const char *section
,
3254 unsigned section_line
,
3261 _cleanup_free_
char *path
= NULL
, *resolved
= NULL
;
3262 CGroupContext
*c
= data
;
3263 const char *p
= rvalue
;
3266 if (isempty(rvalue
)) {
3267 while (c
->device_allow
)
3268 cgroup_context_free_device_allow(c
, c
->device_allow
);
3273 r
= extract_first_word(&p
, &path
, NULL
, EXTRACT_QUOTES
);
3277 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
3278 "Invalid syntax, ignoring: %s", rvalue
);
3282 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
3283 "Failed to extract device path and rights from '%s', ignoring.", rvalue
);
3287 r
= unit_full_printf(userdata
, path
, &resolved
);
3289 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
3290 "Failed to resolve unit specifiers in '%s', ignoring: %m", path
);
3294 if (!STARTSWITH_SET(resolved
, "block-", "char-")) {
3296 r
= path_simplify_and_warn(resolved
, 0, unit
, filename
, line
, lvalue
);
3300 if (!valid_device_node_path(resolved
)) {
3301 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid device node path '%s', ignoring.", resolved
);
3306 if (!isempty(p
) && !in_charset(p
, "rwm")) {
3307 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid device rights '%s', ignoring.", p
);
3311 return cgroup_add_device_allow(c
, resolved
, p
);
3314 int config_parse_io_device_weight(
3316 const char *filename
,
3318 const char *section
,
3319 unsigned section_line
,
3326 _cleanup_free_
char *path
= NULL
, *resolved
= NULL
;
3327 CGroupIODeviceWeight
*w
;
3328 CGroupContext
*c
= data
;
3329 const char *p
= rvalue
;
3337 if (isempty(rvalue
)) {
3338 while (c
->io_device_weights
)
3339 cgroup_context_free_io_device_weight(c
, c
->io_device_weights
);
3344 r
= extract_first_word(&p
, &path
, NULL
, EXTRACT_QUOTES
);
3348 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
3349 "Invalid syntax, ignoring: %s", rvalue
);
3352 if (r
== 0 || isempty(p
)) {
3353 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
3354 "Failed to extract device path and weight from '%s', ignoring.", rvalue
);
3358 r
= unit_full_printf(userdata
, path
, &resolved
);
3360 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
3361 "Failed to resolve unit specifiers in '%s', ignoring: %m", path
);
3365 r
= path_simplify_and_warn(resolved
, 0, unit
, filename
, line
, lvalue
);
3369 r
= cg_weight_parse(p
, &u
);
3371 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "IO weight '%s' invalid, ignoring: %m", p
);
3375 assert(u
!= CGROUP_WEIGHT_INVALID
);
3377 w
= new0(CGroupIODeviceWeight
, 1);
3381 w
->path
= TAKE_PTR(resolved
);
3384 LIST_PREPEND(device_weights
, c
->io_device_weights
, w
);
3388 int config_parse_io_device_latency(
3390 const char *filename
,
3392 const char *section
,
3393 unsigned section_line
,
3400 _cleanup_free_
char *path
= NULL
, *resolved
= NULL
;
3401 CGroupIODeviceLatency
*l
;
3402 CGroupContext
*c
= data
;
3403 const char *p
= rvalue
;
3411 if (isempty(rvalue
)) {
3412 while (c
->io_device_latencies
)
3413 cgroup_context_free_io_device_latency(c
, c
->io_device_latencies
);
3418 r
= extract_first_word(&p
, &path
, NULL
, EXTRACT_QUOTES
);
3422 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
3423 "Invalid syntax, ignoring: %s", rvalue
);
3426 if (r
== 0 || isempty(p
)) {
3427 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
3428 "Failed to extract device path and latency from '%s', ignoring.", rvalue
);
3432 r
= unit_full_printf(userdata
, path
, &resolved
);
3434 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
3435 "Failed to resolve unit specifiers in '%s', ignoring: %m", path
);
3439 r
= path_simplify_and_warn(resolved
, 0, unit
, filename
, line
, lvalue
);
3443 if (parse_sec(p
, &usec
) < 0) {
3444 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse timer value, ignoring: %s", p
);
3448 l
= new0(CGroupIODeviceLatency
, 1);
3452 l
->path
= TAKE_PTR(resolved
);
3453 l
->target_usec
= usec
;
3455 LIST_PREPEND(device_latencies
, c
->io_device_latencies
, l
);
3459 int config_parse_io_limit(
3461 const char *filename
,
3463 const char *section
,
3464 unsigned section_line
,
3471 _cleanup_free_
char *path
= NULL
, *resolved
= NULL
;
3472 CGroupIODeviceLimit
*l
= NULL
, *t
;
3473 CGroupContext
*c
= data
;
3474 CGroupIOLimitType type
;
3475 const char *p
= rvalue
;
3483 type
= cgroup_io_limit_type_from_string(lvalue
);
3486 if (isempty(rvalue
)) {
3487 LIST_FOREACH(device_limits
, l
, c
->io_device_limits
)
3488 l
->limits
[type
] = cgroup_io_limit_defaults
[type
];
3492 r
= extract_first_word(&p
, &path
, NULL
, EXTRACT_QUOTES
);
3496 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
3497 "Invalid syntax, ignoring: %s", rvalue
);
3500 if (r
== 0 || isempty(p
)) {
3501 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
3502 "Failed to extract device node and bandwidth from '%s', ignoring.", rvalue
);
3506 r
= unit_full_printf(userdata
, path
, &resolved
);
3508 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
3509 "Failed to resolve unit specifiers in '%s', ignoring: %m", path
);
3513 r
= path_simplify_and_warn(resolved
, 0, unit
, filename
, line
, lvalue
);
3517 if (streq("infinity", p
))
3518 num
= CGROUP_LIMIT_MAX
;
3520 r
= parse_size(p
, 1000, &num
);
3521 if (r
< 0 || num
<= 0) {
3522 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid IO limit '%s', ignoring.", p
);
3527 LIST_FOREACH(device_limits
, t
, c
->io_device_limits
) {
3528 if (path_equal(resolved
, t
->path
)) {
3535 CGroupIOLimitType ttype
;
3537 l
= new0(CGroupIODeviceLimit
, 1);
3541 l
->path
= TAKE_PTR(resolved
);
3542 for (ttype
= 0; ttype
< _CGROUP_IO_LIMIT_TYPE_MAX
; ttype
++)
3543 l
->limits
[ttype
] = cgroup_io_limit_defaults
[ttype
];
3545 LIST_PREPEND(device_limits
, c
->io_device_limits
, l
);
3548 l
->limits
[type
] = num
;
3553 int config_parse_blockio_device_weight(
3555 const char *filename
,
3557 const char *section
,
3558 unsigned section_line
,
3565 _cleanup_free_
char *path
= NULL
, *resolved
= NULL
;
3566 CGroupBlockIODeviceWeight
*w
;
3567 CGroupContext
*c
= data
;
3568 const char *p
= rvalue
;
3576 if (isempty(rvalue
)) {
3577 while (c
->blockio_device_weights
)
3578 cgroup_context_free_blockio_device_weight(c
, c
->blockio_device_weights
);
3583 r
= extract_first_word(&p
, &path
, NULL
, EXTRACT_QUOTES
);
3587 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
3588 "Invalid syntax, ignoring: %s", rvalue
);
3591 if (r
== 0 || isempty(p
)) {
3592 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
3593 "Failed to extract device node and weight from '%s', ignoring.", rvalue
);
3597 r
= unit_full_printf(userdata
, path
, &resolved
);
3599 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
3600 "Failed to resolve unit specifiers in '%s', ignoring: %m", path
);
3604 r
= path_simplify_and_warn(resolved
, 0, unit
, filename
, line
, lvalue
);
3608 r
= cg_blkio_weight_parse(p
, &u
);
3610 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid block IO weight '%s', ignoring: %m", p
);
3614 assert(u
!= CGROUP_BLKIO_WEIGHT_INVALID
);
3616 w
= new0(CGroupBlockIODeviceWeight
, 1);
3620 w
->path
= TAKE_PTR(resolved
);
3623 LIST_PREPEND(device_weights
, c
->blockio_device_weights
, w
);
3627 int config_parse_blockio_bandwidth(
3629 const char *filename
,
3631 const char *section
,
3632 unsigned section_line
,
3639 _cleanup_free_
char *path
= NULL
, *resolved
= NULL
;
3640 CGroupBlockIODeviceBandwidth
*b
= NULL
, *t
;
3641 CGroupContext
*c
= data
;
3642 const char *p
= rvalue
;
3651 read
= streq("BlockIOReadBandwidth", lvalue
);
3653 if (isempty(rvalue
)) {
3654 LIST_FOREACH(device_bandwidths
, b
, c
->blockio_device_bandwidths
) {
3655 b
->rbps
= CGROUP_LIMIT_MAX
;
3656 b
->wbps
= CGROUP_LIMIT_MAX
;
3661 r
= extract_first_word(&p
, &path
, NULL
, EXTRACT_QUOTES
);
3665 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
3666 "Invalid syntax, ignoring: %s", rvalue
);
3669 if (r
== 0 || isempty(p
)) {
3670 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
3671 "Failed to extract device node and bandwidth from '%s', ignoring.", rvalue
);
3675 r
= unit_full_printf(userdata
, path
, &resolved
);
3677 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
3678 "Failed to resolve unit specifiers in '%s', ignoring: %m", path
);
3682 r
= path_simplify_and_warn(resolved
, 0, unit
, filename
, line
, lvalue
);
3686 r
= parse_size(p
, 1000, &bytes
);
3687 if (r
< 0 || bytes
<= 0) {
3688 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid Block IO Bandwidth '%s', ignoring.", p
);
3692 LIST_FOREACH(device_bandwidths
, t
, c
->blockio_device_bandwidths
) {
3693 if (path_equal(resolved
, t
->path
)) {
3700 b
= new0(CGroupBlockIODeviceBandwidth
, 1);
3704 b
->path
= TAKE_PTR(resolved
);
3705 b
->rbps
= CGROUP_LIMIT_MAX
;
3706 b
->wbps
= CGROUP_LIMIT_MAX
;
3708 LIST_PREPEND(device_bandwidths
, c
->blockio_device_bandwidths
, b
);
3719 int config_parse_job_mode_isolate(
3721 const char *filename
,
3723 const char *section
,
3724 unsigned section_line
,
3738 r
= parse_boolean(rvalue
);
3740 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse boolean, ignoring: %s", rvalue
);
3744 log_notice("%s is deprecated. Please use OnFailureJobMode= instead", lvalue
);
3746 *m
= r
? JOB_ISOLATE
: JOB_REPLACE
;
3750 int config_parse_exec_directories(
3752 const char *filename
,
3754 const char *section
,
3755 unsigned section_line
,
3772 if (isempty(rvalue
)) {
3773 /* Empty assignment resets the list */
3774 *rt
= strv_free(*rt
);
3778 for (p
= rvalue
;;) {
3779 _cleanup_free_
char *word
= NULL
, *k
= NULL
;
3781 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
);
3785 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
3786 "Invalid syntax, ignoring: %s", rvalue
);
3792 r
= unit_full_printf(u
, word
, &k
);
3794 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
3795 "Failed to resolve unit specifiers in \"%s\", ignoring: %m", word
);
3799 r
= path_simplify_and_warn(k
, PATH_CHECK_RELATIVE
, unit
, filename
, line
, lvalue
);
3803 if (path_startswith(k
, "private")) {
3804 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
3805 "%s= path can't be 'private', ignoring assignment: %s", lvalue
, word
);
3809 r
= strv_push(rt
, k
);
3816 int config_parse_set_status(
3818 const char *filename
,
3820 const char *section
,
3821 unsigned section_line
,
3829 const char *word
, *state
;
3831 ExitStatusSet
*status_set
= data
;
3838 /* Empty assignment resets the list */
3839 if (isempty(rvalue
)) {
3840 exit_status_set_free(status_set
);
3844 FOREACH_WORD(word
, l
, rvalue
, state
) {
3845 _cleanup_free_
char *temp
;
3849 temp
= strndup(word
, l
);
3853 r
= safe_atoi(temp
, &val
);
3855 val
= signal_from_string(temp
);
3858 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse value, ignoring: %s", word
);
3861 set
= &status_set
->signal
;
3863 if (val
< 0 || val
> 255) {
3864 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Value %d is outside range 0-255, ignoring", val
);
3867 set
= &status_set
->status
;
3870 r
= set_ensure_allocated(set
, NULL
);
3874 r
= set_put(*set
, INT_TO_PTR(val
));
3878 if (!isempty(state
))
3879 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Trailing garbage, ignoring.");
3884 int config_parse_namespace_path_strv(
3886 const char *filename
,
3888 const char *section
,
3889 unsigned section_line
,
3898 const char *p
= rvalue
;
3906 if (isempty(rvalue
)) {
3907 /* Empty assignment resets the list */
3908 *sv
= strv_free(*sv
);
3913 _cleanup_free_
char *word
= NULL
, *resolved
= NULL
, *joined
= NULL
;
3915 bool ignore_enoent
= false, shall_prefix
= false;
3917 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
);
3923 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to extract first word, ignoring: %s", rvalue
);
3928 if (startswith(w
, "-")) {
3929 ignore_enoent
= true;
3932 if (startswith(w
, "+")) {
3933 shall_prefix
= true;
3937 r
= unit_full_printf(u
, w
, &resolved
);
3939 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in %s: %m", w
);
3943 r
= path_simplify_and_warn(resolved
, PATH_CHECK_ABSOLUTE
, unit
, filename
, line
, lvalue
);
3947 joined
= strjoin(ignore_enoent
? "-" : "",
3948 shall_prefix
? "+" : "",
3951 r
= strv_push(sv
, joined
);
3961 int config_parse_temporary_filesystems(
3963 const char *filename
,
3965 const char *section
,
3966 unsigned section_line
,
3974 ExecContext
*c
= data
;
3975 const char *p
= rvalue
;
3983 if (isempty(rvalue
)) {
3984 /* Empty assignment resets the list */
3985 temporary_filesystem_free_many(c
->temporary_filesystems
, c
->n_temporary_filesystems
);
3986 c
->temporary_filesystems
= NULL
;
3987 c
->n_temporary_filesystems
= 0;
3992 _cleanup_free_
char *word
= NULL
, *path
= NULL
, *resolved
= NULL
;
3995 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
);
4001 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to extract first word, ignoring: %s", rvalue
);
4006 r
= extract_first_word(&w
, &path
, ":", EXTRACT_DONT_COALESCE_SEPARATORS
);
4010 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to extract first word, ignoring: %s", word
);
4014 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid syntax, ignoring: %s", word
);
4018 r
= unit_full_printf(u
, path
, &resolved
);
4020 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in %s, ignoring: %m", path
);
4024 r
= path_simplify_and_warn(resolved
, PATH_CHECK_ABSOLUTE
, unit
, filename
, line
, lvalue
);
4028 r
= temporary_filesystem_add(&c
->temporary_filesystems
, &c
->n_temporary_filesystems
, resolved
, w
);
4034 int config_parse_bind_paths(
4036 const char *filename
,
4038 const char *section
,
4039 unsigned section_line
,
4046 ExecContext
*c
= data
;
4056 if (isempty(rvalue
)) {
4057 /* Empty assignment resets the list */
4058 bind_mount_free_many(c
->bind_mounts
, c
->n_bind_mounts
);
4059 c
->bind_mounts
= NULL
;
4060 c
->n_bind_mounts
= 0;
4066 _cleanup_free_
char *source
= NULL
, *destination
= NULL
;
4067 _cleanup_free_
char *sresolved
= NULL
, *dresolved
= NULL
;
4068 char *s
= NULL
, *d
= NULL
;
4069 bool rbind
= true, ignore_enoent
= false;
4071 r
= extract_first_word(&p
, &source
, ":" WHITESPACE
, EXTRACT_QUOTES
|EXTRACT_DONT_COALESCE_SEPARATORS
);
4077 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse %s, ignoring: %s", lvalue
, rvalue
);
4081 r
= unit_full_printf(u
, source
, &sresolved
);
4083 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
4084 "Failed to resolved unit specifiers in \"%s\", ignoring: %m", source
);
4090 ignore_enoent
= true;
4094 r
= path_simplify_and_warn(s
, PATH_CHECK_ABSOLUTE
, unit
, filename
, line
, lvalue
);
4098 /* Optionally, the destination is specified. */
4099 if (p
&& p
[-1] == ':') {
4100 r
= extract_first_word(&p
, &destination
, ":" WHITESPACE
, EXTRACT_QUOTES
|EXTRACT_DONT_COALESCE_SEPARATORS
);
4104 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse %s, ignoring: %s", lvalue
, rvalue
);
4108 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Missing argument after ':', ignoring: %s", s
);
4112 r
= unit_full_printf(u
, destination
, &dresolved
);
4114 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
4115 "Failed to resolved specifiers in \"%s\", ignoring: %m", destination
);
4119 r
= path_simplify_and_warn(dresolved
, PATH_CHECK_ABSOLUTE
, unit
, filename
, line
, lvalue
);
4125 /* Optionally, there's also a short option string specified */
4126 if (p
&& p
[-1] == ':') {
4127 _cleanup_free_
char *options
= NULL
;
4129 r
= extract_first_word(&p
, &options
, NULL
, EXTRACT_QUOTES
);
4133 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse %s: %s", lvalue
, rvalue
);
4137 if (isempty(options
) || streq(options
, "rbind"))
4139 else if (streq(options
, "norbind"))
4142 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid option string, ignoring setting: %s", options
);
4149 r
= bind_mount_add(&c
->bind_mounts
, &c
->n_bind_mounts
,
4153 .read_only
= !!strstr(lvalue
, "ReadOnly"),
4155 .ignore_enoent
= ignore_enoent
,
4164 int config_parse_job_timeout_sec(
4166 const char *filename
,
4168 const char *section
,
4169 unsigned section_line
,
4185 r
= parse_sec_fix_0(rvalue
, &usec
);
4187 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse JobTimeoutSec= parameter, ignoring: %s", rvalue
);
4191 /* If the user explicitly changed JobTimeoutSec= also change JobRunningTimeoutSec=, for compatibility with old
4192 * versions. If JobRunningTimeoutSec= was explicitly set, avoid this however as whatever the user picked should
4195 if (!u
->job_running_timeout_set
)
4196 u
->job_running_timeout
= usec
;
4198 u
->job_timeout
= usec
;
4203 int config_parse_job_running_timeout_sec(
4205 const char *filename
,
4207 const char *section
,
4208 unsigned section_line
,
4224 r
= parse_sec_fix_0(rvalue
, &usec
);
4226 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse JobRunningTimeoutSec= parameter, ignoring: %s", rvalue
);
4230 u
->job_running_timeout
= usec
;
4231 u
->job_running_timeout_set
= true;
4236 int config_parse_emergency_action(
4238 const char *filename
,
4240 const char *section
,
4241 unsigned section_line
,
4249 EmergencyAction
*x
= data
;
4258 m
= ((Unit
*) userdata
)->manager
;
4262 r
= parse_emergency_action(rvalue
, MANAGER_IS_SYSTEM(m
), x
);
4264 if (r
== -EOPNOTSUPP
&& MANAGER_IS_USER(m
)) {
4265 /* Compat mode: remove for systemd 241. */
4267 log_syntax(unit
, LOG_INFO
, filename
, line
, r
,
4268 "%s= in user mode specified as \"%s\", using \"exit-force\" instead.",
4270 *x
= EMERGENCY_ACTION_EXIT_FORCE
;
4274 if (r
== -EOPNOTSUPP
)
4275 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
4276 "%s= specified as %s mode action, ignoring: %s",
4277 lvalue
, MANAGER_IS_SYSTEM(m
) ? "user" : "system", rvalue
);
4279 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
4280 "Failed to parse %s=, ignoring: %s", lvalue
, rvalue
);
4287 int config_parse_pid_file(
4289 const char *filename
,
4291 const char *section
,
4292 unsigned section_line
,
4299 _cleanup_free_
char *k
= NULL
, *n
= NULL
;
4309 if (isempty(rvalue
)) {
4310 /* An empty assignment removes already set value. */
4315 r
= unit_full_printf(u
, rvalue
, &k
);
4317 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue
);
4321 /* If this is a relative path make it absolute by prefixing the /run */
4322 n
= path_make_absolute(k
, u
->manager
->prefix
[EXEC_DIRECTORY_RUNTIME
]);
4326 /* Check that the result is a sensible path */
4327 r
= path_simplify_and_warn(n
, PATH_CHECK_ABSOLUTE
, unit
, filename
, line
, lvalue
);
4331 r
= patch_var_run(unit
, filename
, line
, lvalue
, &n
);
4335 free_and_replace(*s
, n
);
4339 int config_parse_exit_status(
4341 const char *filename
,
4343 const char *section
,
4344 unsigned section_line
,
4351 int *exit_status
= data
, r
;
4357 assert(exit_status
);
4359 if (isempty(rvalue
)) {
4364 r
= safe_atou8(rvalue
, &u
);
4366 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse exit status '%s', ignoring: %m", rvalue
);
4374 int config_parse_disable_controllers(
4376 const char *filename
,
4378 const char *section
,
4379 unsigned section_line
,
4387 CGroupContext
*c
= data
;
4388 CGroupMask disabled_mask
;
4390 /* 1. If empty, make all controllers eligible for use again.
4391 * 2. If non-empty, merge all listed controllers, space separated. */
4393 if (isempty(rvalue
)) {
4394 c
->disable_controllers
= 0;
4398 r
= cg_mask_from_string(rvalue
, &disabled_mask
);
4399 if (r
< 0 || disabled_mask
<= 0) {
4400 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid cgroup string: %s, ignoring", rvalue
);
4404 c
->disable_controllers
|= disabled_mask
;
4409 #define FOLLOW_MAX 8
4411 static int open_follow(char **filename
, FILE **_f
, Set
*names
, char **_final
) {
4422 /* This will update the filename pointer if the loaded file is
4423 * reached by a symlink. The old string will be freed. */
4426 char *target
, *name
;
4428 if (c
++ >= FOLLOW_MAX
)
4431 path_simplify(*filename
, false);
4433 /* Add the file name we are currently looking at to
4434 * the names of this unit, but only if it is a valid
4436 name
= basename(*filename
);
4437 if (unit_name_is_valid(name
, UNIT_NAME_ANY
)) {
4439 id
= set_get(names
, name
);
4445 r
= set_consume(names
, id
);
4451 /* Try to open the file name, but don't if its a symlink */
4452 fd
= open(*filename
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
4459 /* Hmm, so this is a symlink. Let's read the name, and follow it manually */
4460 r
= readlink_and_make_absolute(*filename
, &target
);
4464 free_and_replace(*filename
, target
);
4467 f
= fdopen(fd
, "r");
4479 static int merge_by_names(Unit
**u
, Set
*names
, const char *id
) {
4487 /* Let's try to add in all symlink names we found */
4488 while ((k
= set_steal_first(names
))) {
4490 /* First try to merge in the other name into our
4492 r
= unit_merge_by_name(*u
, k
);
4496 /* Hmm, we couldn't merge the other unit into
4497 * ours? Then let's try it the other way
4500 /* If the symlink name we are looking at is unit template, then
4501 we must search for instance of this template */
4502 if (unit_name_is_valid(k
, UNIT_NAME_TEMPLATE
) && (*u
)->instance
) {
4503 _cleanup_free_
char *instance
= NULL
;
4505 r
= unit_name_replace_instance(k
, (*u
)->instance
, &instance
);
4509 other
= manager_get_unit((*u
)->manager
, instance
);
4511 other
= manager_get_unit((*u
)->manager
, k
);
4516 r
= unit_merge(other
, *u
);
4519 return merge_by_names(u
, names
, NULL
);
4527 unit_choose_id(*u
, id
);
4535 static int load_from_path(Unit
*u
, const char *path
) {
4536 _cleanup_set_free_free_ Set
*symlink_names
= NULL
;
4537 _cleanup_fclose_
FILE *f
= NULL
;
4538 _cleanup_free_
char *filename
= NULL
;
4547 symlink_names
= set_new(&string_hash_ops
);
4551 if (path_is_absolute(path
)) {
4553 filename
= strdup(path
);
4557 r
= open_follow(&filename
, &f
, symlink_names
, &id
);
4559 filename
= mfree(filename
);
4567 STRV_FOREACH(p
, u
->manager
->lookup_paths
.search_path
) {
4569 /* Instead of opening the path right away, we manually
4570 * follow all symlinks and add their name to our unit
4571 * name set while doing so */
4572 filename
= path_make_absolute(path
, *p
);
4576 if (u
->manager
->unit_path_cache
&&
4577 !set_get(u
->manager
->unit_path_cache
, filename
))
4580 r
= open_follow(&filename
, &f
, symlink_names
, &id
);
4584 /* ENOENT means that the file is missing or is a dangling symlink.
4585 * ENOTDIR means that one of paths we expect to be is a directory
4586 * is not a directory, we should just ignore that.
4587 * EACCES means that the directory or file permissions are wrong.
4590 log_debug_errno(r
, "Cannot access \"%s\": %m", filename
);
4591 else if (!IN_SET(r
, -ENOENT
, -ENOTDIR
))
4594 filename
= mfree(filename
);
4595 /* Empty the symlink names for the next run */
4596 set_clear_free(symlink_names
);
4601 /* Hmm, no suitable file found? */
4604 if (!unit_type_may_alias(u
->type
) && set_size(symlink_names
) > 1) {
4605 log_unit_warning(u
, "Unit type of %s does not support alias names, refusing loading via symlink.", u
->id
);
4610 r
= merge_by_names(&merged
, symlink_names
, id
);
4615 u
->load_state
= UNIT_MERGED
;
4619 if (fstat(fileno(f
), &st
) < 0)
4622 if (null_or_empty(&st
)) {
4623 u
->load_state
= UNIT_MASKED
;
4624 u
->fragment_mtime
= 0;
4626 u
->load_state
= UNIT_LOADED
;
4627 u
->fragment_mtime
= timespec_load(&st
.st_mtim
);
4629 /* Now, parse the file contents */
4630 r
= config_parse(u
->id
, filename
, f
,
4631 UNIT_VTABLE(u
)->sections
,
4632 config_item_perf_lookup
, load_fragment_gperf_lookup
,
4633 CONFIG_PARSE_ALLOW_INCLUDE
, u
);
4638 free_and_replace(u
->fragment_path
, filename
);
4640 if (u
->source_path
) {
4641 if (stat(u
->source_path
, &st
) >= 0)
4642 u
->source_mtime
= timespec_load(&st
.st_mtim
);
4644 u
->source_mtime
= 0;
4650 int unit_load_fragment(Unit
*u
) {
4656 assert(u
->load_state
== UNIT_STUB
);
4660 u
->load_state
= UNIT_LOADED
;
4664 /* First, try to find the unit under its id. We always look
4665 * for unit files in the default directories, to make it easy
4666 * to override things by placing things in /etc/systemd/system */
4667 r
= load_from_path(u
, u
->id
);
4671 /* Try to find an alias we can load this with */
4672 if (u
->load_state
== UNIT_STUB
) {
4673 SET_FOREACH(t
, u
->names
, i
) {
4678 r
= load_from_path(u
, t
);
4682 if (u
->load_state
!= UNIT_STUB
)
4687 /* And now, try looking for it under the suggested (originally linked) path */
4688 if (u
->load_state
== UNIT_STUB
&& u
->fragment_path
) {
4690 r
= load_from_path(u
, u
->fragment_path
);
4694 if (u
->load_state
== UNIT_STUB
)
4695 /* Hmm, this didn't work? Then let's get rid
4696 * of the fragment path stored for us, so that
4697 * we don't point to an invalid location. */
4698 u
->fragment_path
= mfree(u
->fragment_path
);
4701 /* Look for a template */
4702 if (u
->load_state
== UNIT_STUB
&& u
->instance
) {
4703 _cleanup_free_
char *k
= NULL
;
4705 r
= unit_name_template(u
->id
, &k
);
4709 r
= load_from_path(u
, k
);
4712 log_unit_notice(u
, "Unit configuration has fatal error, unit will not be started.");
4716 if (u
->load_state
== UNIT_STUB
) {
4717 SET_FOREACH(t
, u
->names
, i
) {
4718 _cleanup_free_
char *z
= NULL
;
4723 r
= unit_name_template(t
, &z
);
4727 r
= load_from_path(u
, z
);
4731 if (u
->load_state
!= UNIT_STUB
)
4740 void unit_dump_config_items(FILE *f
) {
4741 static const struct {
4742 const ConfigParserCallback callback
;
4745 { config_parse_warn_compat
, "NOTSUPPORTED" },
4746 { config_parse_int
, "INTEGER" },
4747 { config_parse_unsigned
, "UNSIGNED" },
4748 { config_parse_iec_size
, "SIZE" },
4749 { config_parse_iec_uint64
, "SIZE" },
4750 { config_parse_si_size
, "SIZE" },
4751 { config_parse_bool
, "BOOLEAN" },
4752 { config_parse_string
, "STRING" },
4753 { config_parse_path
, "PATH" },
4754 { config_parse_unit_path_printf
, "PATH" },
4755 { config_parse_strv
, "STRING [...]" },
4756 { config_parse_exec_nice
, "NICE" },
4757 { config_parse_exec_oom_score_adjust
, "OOMSCOREADJUST" },
4758 { config_parse_exec_io_class
, "IOCLASS" },
4759 { config_parse_exec_io_priority
, "IOPRIORITY" },
4760 { config_parse_exec_cpu_sched_policy
, "CPUSCHEDPOLICY" },
4761 { config_parse_exec_cpu_sched_prio
, "CPUSCHEDPRIO" },
4762 { config_parse_exec_cpu_affinity
, "CPUAFFINITY" },
4763 { config_parse_mode
, "MODE" },
4764 { config_parse_unit_env_file
, "FILE" },
4765 { config_parse_exec_output
, "OUTPUT" },
4766 { config_parse_exec_input
, "INPUT" },
4767 { config_parse_log_facility
, "FACILITY" },
4768 { config_parse_log_level
, "LEVEL" },
4769 { config_parse_exec_secure_bits
, "SECUREBITS" },
4770 { config_parse_capability_set
, "BOUNDINGSET" },
4771 { config_parse_rlimit
, "LIMIT" },
4772 { config_parse_unit_deps
, "UNIT [...]" },
4773 { config_parse_exec
, "PATH [ARGUMENT [...]]" },
4774 { config_parse_service_type
, "SERVICETYPE" },
4775 { config_parse_service_restart
, "SERVICERESTART" },
4776 { config_parse_kill_mode
, "KILLMODE" },
4777 { config_parse_signal
, "SIGNAL" },
4778 { config_parse_socket_listen
, "SOCKET [...]" },
4779 { config_parse_socket_bind
, "SOCKETBIND" },
4780 { config_parse_socket_bindtodevice
, "NETWORKINTERFACE" },
4781 { config_parse_sec
, "SECONDS" },
4782 { config_parse_nsec
, "NANOSECONDS" },
4783 { config_parse_namespace_path_strv
, "PATH [...]" },
4784 { config_parse_bind_paths
, "PATH[:PATH[:OPTIONS]] [...]" },
4785 { config_parse_unit_requires_mounts_for
, "PATH [...]" },
4786 { config_parse_exec_mount_flags
, "MOUNTFLAG [...]" },
4787 { config_parse_unit_string_printf
, "STRING" },
4788 { config_parse_trigger_unit
, "UNIT" },
4789 { config_parse_timer
, "TIMER" },
4790 { config_parse_path_spec
, "PATH" },
4791 { config_parse_notify_access
, "ACCESS" },
4792 { config_parse_ip_tos
, "TOS" },
4793 { config_parse_unit_condition_path
, "CONDITION" },
4794 { config_parse_unit_condition_string
, "CONDITION" },
4795 { config_parse_unit_condition_null
, "CONDITION" },
4796 { config_parse_unit_slice
, "SLICE" },
4797 { config_parse_documentation
, "URL" },
4798 { config_parse_service_timeout
, "SECONDS" },
4799 { config_parse_emergency_action
, "ACTION" },
4800 { config_parse_set_status
, "STATUS" },
4801 { config_parse_service_sockets
, "SOCKETS" },
4802 { config_parse_environ
, "ENVIRON" },
4804 { config_parse_syscall_filter
, "SYSCALLS" },
4805 { config_parse_syscall_archs
, "ARCHS" },
4806 { config_parse_syscall_errno
, "ERRNO" },
4807 { config_parse_address_families
, "FAMILIES" },
4808 { config_parse_restrict_namespaces
, "NAMESPACES" },
4810 { config_parse_cpu_shares
, "SHARES" },
4811 { config_parse_cg_weight
, "WEIGHT" },
4812 { config_parse_memory_limit
, "LIMIT" },
4813 { config_parse_device_allow
, "DEVICE" },
4814 { config_parse_device_policy
, "POLICY" },
4815 { config_parse_io_limit
, "LIMIT" },
4816 { config_parse_io_device_weight
, "DEVICEWEIGHT" },
4817 { config_parse_io_device_latency
, "DEVICELATENCY" },
4818 { config_parse_blockio_bandwidth
, "BANDWIDTH" },
4819 { config_parse_blockio_weight
, "WEIGHT" },
4820 { config_parse_blockio_device_weight
, "DEVICEWEIGHT" },
4821 { config_parse_long
, "LONG" },
4822 { config_parse_socket_service
, "SERVICE" },
4824 { config_parse_exec_selinux_context
, "LABEL" },
4826 { config_parse_job_mode
, "MODE" },
4827 { config_parse_job_mode_isolate
, "BOOLEAN" },
4828 { config_parse_personality
, "PERSONALITY" },
4831 const char *prev
= NULL
;
4836 NULSTR_FOREACH(i
, load_fragment_gperf_nulstr
) {
4837 const char *rvalue
= "OTHER", *lvalue
;
4838 const ConfigPerfItem
*p
;
4843 assert_se(p
= load_fragment_gperf_lookup(i
, strlen(i
)));
4845 /* Hide legacy settings */
4846 if (p
->parse
== config_parse_warn_compat
&&
4847 p
->ltype
== DISABLED_LEGACY
)
4850 for (j
= 0; j
< ELEMENTSOF(table
); j
++)
4851 if (p
->parse
== table
[j
].callback
) {
4852 rvalue
= table
[j
].rvalue
;
4856 dot
= strchr(i
, '.');
4857 lvalue
= dot
? dot
+ 1 : i
;
4861 if (!prev
|| !strneq(prev
, i
, prefix_len
+1)) {
4865 fprintf(f
, "[%.*s]\n", (int) prefix_len
, i
);
4868 fprintf(f
, "%s=%s\n", lvalue
, rvalue
);