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
);
322 int config_parse_socket_listen(const char *unit
,
323 const char *filename
,
326 unsigned section_line
,
333 _cleanup_free_ SocketPort
*p
= NULL
;
345 if (isempty(rvalue
)) {
346 /* An empty assignment removes all ports */
347 socket_free_ports(s
);
351 p
= new0(SocketPort
, 1);
355 if (ltype
!= SOCKET_SOCKET
) {
356 _cleanup_free_
char *k
= NULL
;
358 r
= unit_full_printf(UNIT(s
), rvalue
, &k
);
360 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue
);
364 r
= path_simplify_and_warn(k
, PATH_CHECK_ABSOLUTE
, unit
, filename
, line
, lvalue
);
368 free_and_replace(p
->path
, k
);
371 } else if (streq(lvalue
, "ListenNetlink")) {
372 _cleanup_free_
char *k
= NULL
;
374 r
= unit_full_printf(UNIT(s
), rvalue
, &k
);
376 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue
);
380 r
= socket_address_parse_netlink(&p
->address
, k
);
382 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse address value in '%s', ignoring: %m", k
);
386 p
->type
= SOCKET_SOCKET
;
389 _cleanup_free_
char *k
= NULL
;
391 r
= unit_full_printf(UNIT(s
), rvalue
, &k
);
393 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue
);
397 r
= socket_address_parse_and_warn(&p
->address
, k
);
399 if (r
!= -EAFNOSUPPORT
)
400 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse address value in '%s', ignoring: %m", k
);
404 if (streq(lvalue
, "ListenStream"))
405 p
->address
.type
= SOCK_STREAM
;
406 else if (streq(lvalue
, "ListenDatagram"))
407 p
->address
.type
= SOCK_DGRAM
;
409 assert(streq(lvalue
, "ListenSequentialPacket"));
410 p
->address
.type
= SOCK_SEQPACKET
;
413 if (socket_address_family(&p
->address
) != AF_LOCAL
&& p
->address
.type
== SOCK_SEQPACKET
) {
414 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Address family not supported, ignoring: %s", rvalue
);
418 p
->type
= SOCKET_SOCKET
;
422 p
->auxiliary_fds
= NULL
;
423 p
->n_auxiliary_fds
= 0;
426 LIST_FIND_TAIL(port
, s
->ports
, tail
);
427 LIST_INSERT_AFTER(port
, s
->ports
, tail
, p
);
434 int config_parse_exec_nice(
436 const char *filename
,
439 unsigned section_line
,
446 ExecContext
*c
= data
;
454 if (isempty(rvalue
)) {
459 r
= parse_nice(rvalue
, &priority
);
462 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Nice priority out of range, ignoring: %s", rvalue
);
464 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse nice priority '%s', ignoring: %m", rvalue
);
474 int config_parse_exec_oom_score_adjust(
476 const char *filename
,
479 unsigned section_line
,
486 ExecContext
*c
= data
;
494 if (isempty(rvalue
)) {
495 c
->oom_score_adjust_set
= false;
499 r
= parse_oom_score_adjust(rvalue
, &oa
);
502 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "OOM score adjust value out of range, ignoring: %s", rvalue
);
504 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse the OOM score adjust value '%s', ignoring: %m", rvalue
);
508 c
->oom_score_adjust
= oa
;
509 c
->oom_score_adjust_set
= true;
514 int config_parse_exec(
516 const char *filename
,
519 unsigned section_line
,
526 ExecCommand
**e
= data
;
538 rvalue
+= strspn(rvalue
, WHITESPACE
);
540 if (isempty(rvalue
)) {
541 /* An empty assignment resets the list */
542 *e
= exec_command_free_list(*e
);
548 _cleanup_free_
char *path
= NULL
, *firstword
= NULL
;
549 ExecCommandFlags flags
= 0;
550 bool ignore
= false, separate_argv0
= false;
551 _cleanup_free_ ExecCommand
*nce
= NULL
;
552 _cleanup_strv_free_
char **n
= NULL
;
553 size_t nlen
= 0, nbufsize
= 0;
558 r
= extract_first_word_and_warn(&p
, &firstword
, NULL
, EXTRACT_QUOTES
|EXTRACT_CUNESCAPE
, unit
, filename
, line
, rvalue
);
564 /* We accept an absolute path as first argument. If it's prefixed with - and the path doesn't
565 * exist, we ignore it instead of erroring out; if it's prefixed with @, we allow overriding of
566 * argv[0]; if it's prefixed with :, we will not do environment variable substitution;
567 * if it's prefixed with +, it will be run with full privileges and no sandboxing; if
568 * it's prefixed with '!' we apply sandboxing, but do not change user/group credentials; if
569 * it's prefixed with '!!', then we apply user/group credentials if the kernel supports ambient
570 * capabilities -- if it doesn't we don't apply the credentials themselves, but do apply most
571 * other sandboxing, with some special exceptions for changing UID.
573 * The idea is that '!!' may be used to write services that can take benefit of systemd's
574 * UID/GID dropping if the kernel supports ambient creds, but provide an automatic fallback to
575 * privilege dropping within the daemon if the kernel does not offer that. */
577 if (*f
== '-' && !(flags
& EXEC_COMMAND_IGNORE_FAILURE
)) {
578 flags
|= EXEC_COMMAND_IGNORE_FAILURE
;
580 } else if (*f
== '@' && !separate_argv0
)
581 separate_argv0
= true;
582 else if (*f
== ':' && !(flags
& EXEC_COMMAND_NO_ENV_EXPAND
))
583 flags
|= EXEC_COMMAND_NO_ENV_EXPAND
;
584 else if (*f
== '+' && !(flags
& (EXEC_COMMAND_FULLY_PRIVILEGED
|EXEC_COMMAND_NO_SETUID
|EXEC_COMMAND_AMBIENT_MAGIC
)))
585 flags
|= EXEC_COMMAND_FULLY_PRIVILEGED
;
586 else if (*f
== '!' && !(flags
& (EXEC_COMMAND_FULLY_PRIVILEGED
|EXEC_COMMAND_NO_SETUID
|EXEC_COMMAND_AMBIENT_MAGIC
)))
587 flags
|= EXEC_COMMAND_NO_SETUID
;
588 else if (*f
== '!' && !(flags
& (EXEC_COMMAND_FULLY_PRIVILEGED
|EXEC_COMMAND_AMBIENT_MAGIC
))) {
589 flags
&= ~EXEC_COMMAND_NO_SETUID
;
590 flags
|= EXEC_COMMAND_AMBIENT_MAGIC
;
596 r
= unit_full_printf(u
, f
, &path
);
598 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
599 "Failed to resolve unit specifiers in '%s'%s: %m",
600 f
, ignore
? ", ignoring" : "");
601 return ignore
? 0 : -ENOEXEC
;
605 /* First word is either "-" or "@" with no command. */
606 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
607 "Empty path in command line%s: '%s'",
608 ignore
? ", ignoring" : "", rvalue
);
609 return ignore
? 0 : -ENOEXEC
;
611 if (!string_is_safe(path
)) {
612 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
613 "Executable name contains special characters%s: %s",
614 ignore
? ", ignoring" : "", path
);
615 return ignore
? 0 : -ENOEXEC
;
617 if (endswith(path
, "/")) {
618 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
619 "Executable path specifies a directory%s: %s",
620 ignore
? ", ignoring" : "", path
);
621 return ignore
? 0 : -ENOEXEC
;
624 if (!path_is_absolute(path
)) {
628 if (!filename_is_valid(path
)) {
629 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
630 "Neither a valid executable name nor an absolute path%s: %s",
631 ignore
? ", ignoring" : "", path
);
632 return ignore
? 0 : -ENOEXEC
;
635 /* Resolve a single-component name to a full path */
636 NULSTR_FOREACH(prefix
, DEFAULT_PATH_NULSTR
) {
637 _cleanup_free_
char *fullpath
= NULL
;
639 fullpath
= strjoin(prefix
, "/", path
);
643 if (access(fullpath
, F_OK
) >= 0) {
644 free_and_replace(path
, fullpath
);
651 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
652 "Executable \"%s\" not found in path \"%s\"%s",
653 path
, DEFAULT_PATH
, ignore
? ", ignoring" : "");
654 return ignore
? 0 : -ENOEXEC
;
658 if (!separate_argv0
) {
661 if (!GREEDY_REALLOC(n
, nbufsize
, nlen
+ 2))
671 path_simplify(path
, false);
673 while (!isempty(p
)) {
674 _cleanup_free_
char *word
= NULL
, *resolved
= NULL
;
676 /* Check explicitly for an unquoted semicolon as
677 * command separator token. */
678 if (p
[0] == ';' && (!p
[1] || strchr(WHITESPACE
, p
[1]))) {
680 p
+= strspn(p
, WHITESPACE
);
685 /* Check for \; explicitly, to not confuse it with \\; or "\;" or "\\;" etc.
686 * extract_first_word() would return the same for all of those. */
687 if (p
[0] == '\\' && p
[1] == ';' && (!p
[2] || strchr(WHITESPACE
, p
[2]))) {
691 p
+= strspn(p
, WHITESPACE
);
693 if (!GREEDY_REALLOC(n
, nbufsize
, nlen
+ 2))
704 r
= extract_first_word_and_warn(&p
, &word
, NULL
, EXTRACT_QUOTES
|EXTRACT_CUNESCAPE
, unit
, filename
, line
, rvalue
);
708 return ignore
? 0 : -ENOEXEC
;
710 r
= unit_full_printf(u
, word
, &resolved
);
712 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
713 "Failed to resolve unit specifiers in %s%s: %m",
714 word
, ignore
? ", ignoring" : "");
715 return ignore
? 0 : -ENOEXEC
;
718 if (!GREEDY_REALLOC(n
, nbufsize
, nlen
+ 2))
721 n
[nlen
++] = TAKE_PTR(resolved
);
726 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
727 "Empty executable name or zeroeth argument%s: %s",
728 ignore
? ", ignoring" : "", rvalue
);
729 return ignore
? 0 : -ENOEXEC
;
732 nce
= new0(ExecCommand
, 1);
736 nce
->argv
= TAKE_PTR(n
);
737 nce
->path
= TAKE_PTR(path
);
740 exec_command_append_list(e
, nce
);
742 /* Do not _cleanup_free_ these. */
751 int config_parse_socket_bindtodevice(
753 const char *filename
,
756 unsigned section_line
,
770 if (isempty(rvalue
) || streq(rvalue
, "*")) {
771 s
->bind_to_device
= mfree(s
->bind_to_device
);
775 if (!ifname_valid(rvalue
)) {
776 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid interface name, ignoring: %s", rvalue
);
780 if (free_and_strdup(&s
->bind_to_device
, rvalue
) < 0)
786 int config_parse_exec_input(
788 const char *filename
,
791 unsigned section_line
,
798 ExecContext
*c
= data
;
809 n
= startswith(rvalue
, "fd:");
811 _cleanup_free_
char *resolved
= NULL
;
813 r
= unit_full_printf(u
, n
, &resolved
);
815 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in '%s': %m", n
);
817 if (isempty(resolved
))
818 resolved
= mfree(resolved
);
819 else if (!fdname_is_valid(resolved
)) {
820 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid file descriptor name: %s", resolved
);
824 free_and_replace(c
->stdio_fdname
[STDIN_FILENO
], resolved
);
826 ei
= EXEC_INPUT_NAMED_FD
;
828 } else if ((n
= startswith(rvalue
, "file:"))) {
829 _cleanup_free_
char *resolved
= NULL
;
831 r
= unit_full_printf(u
, n
, &resolved
);
833 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in '%s': %m", n
);
835 r
= path_simplify_and_warn(resolved
, PATH_CHECK_ABSOLUTE
| PATH_CHECK_FATAL
, unit
, filename
, line
, lvalue
);
839 free_and_replace(c
->stdio_file
[STDIN_FILENO
], resolved
);
841 ei
= EXEC_INPUT_FILE
;
844 ei
= exec_input_from_string(rvalue
);
846 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse input specifier, ignoring: %s", rvalue
);
855 int config_parse_exec_input_text(
857 const char *filename
,
860 unsigned section_line
,
867 _cleanup_free_
char *unescaped
= NULL
, *resolved
= NULL
;
868 ExecContext
*c
= data
;
879 if (isempty(rvalue
)) {
880 /* Reset if the empty string is assigned */
881 c
->stdin_data
= mfree(c
->stdin_data
);
882 c
->stdin_data_size
= 0;
886 r
= cunescape(rvalue
, 0, &unescaped
);
888 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to decode C escaped text '%s': %m", rvalue
);
890 r
= unit_full_printf(u
, unescaped
, &resolved
);
892 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in '%s': %m", unescaped
);
894 sz
= strlen(resolved
);
895 if (c
->stdin_data_size
+ sz
+ 1 < c
->stdin_data_size
|| /* check for overflow */
896 c
->stdin_data_size
+ sz
+ 1 > EXEC_STDIN_DATA_MAX
) {
897 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
);
901 p
= realloc(c
->stdin_data
, c
->stdin_data_size
+ sz
+ 1);
905 *((char*) mempcpy((char*) p
+ c
->stdin_data_size
, resolved
, sz
)) = '\n';
908 c
->stdin_data_size
+= sz
+ 1;
913 int config_parse_exec_input_data(
915 const char *filename
,
918 unsigned section_line
,
925 _cleanup_free_
void *p
= NULL
;
926 ExecContext
*c
= data
;
936 if (isempty(rvalue
)) {
937 /* Reset if the empty string is assigned */
938 c
->stdin_data
= mfree(c
->stdin_data
);
939 c
->stdin_data_size
= 0;
943 r
= unbase64mem(rvalue
, (size_t) -1, &p
, &sz
);
945 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to decode base64 data, ignoring: %s", rvalue
);
949 if (c
->stdin_data_size
+ sz
< c
->stdin_data_size
|| /* check for overflow */
950 c
->stdin_data_size
+ sz
> EXEC_STDIN_DATA_MAX
) {
951 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
);
955 q
= realloc(c
->stdin_data
, c
->stdin_data_size
+ sz
);
959 memcpy((uint8_t*) q
+ c
->stdin_data_size
, p
, sz
);
962 c
->stdin_data_size
+= sz
;
967 int config_parse_exec_output(
969 const char *filename
,
972 unsigned section_line
,
979 _cleanup_free_
char *resolved
= NULL
;
981 ExecContext
*c
= data
;
992 n
= startswith(rvalue
, "fd:");
994 r
= unit_full_printf(u
, n
, &resolved
);
996 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in %s: %m", n
);
998 if (isempty(resolved
))
999 resolved
= mfree(resolved
);
1000 else if (!fdname_is_valid(resolved
)) {
1001 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid file descriptor name: %s", resolved
);
1005 eo
= EXEC_OUTPUT_NAMED_FD
;
1007 } else if ((n
= startswith(rvalue
, "file:"))) {
1009 r
= unit_full_printf(u
, n
, &resolved
);
1011 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in %s: %m", n
);
1013 r
= path_simplify_and_warn(resolved
, PATH_CHECK_ABSOLUTE
| PATH_CHECK_FATAL
, unit
, filename
, line
, lvalue
);
1017 eo
= EXEC_OUTPUT_FILE
;
1019 } else if ((n
= startswith(rvalue
, "append:"))) {
1021 r
= unit_full_printf(u
, n
, &resolved
);
1023 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in %s: %m", n
);
1025 r
= path_simplify_and_warn(resolved
, PATH_CHECK_ABSOLUTE
| PATH_CHECK_FATAL
, unit
, filename
, line
, lvalue
);
1029 eo
= EXEC_OUTPUT_FILE_APPEND
;
1031 eo
= exec_output_from_string(rvalue
);
1033 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse output specifier, ignoring: %s", rvalue
);
1038 if (streq(lvalue
, "StandardOutput")) {
1039 if (eo
== EXEC_OUTPUT_NAMED_FD
)
1040 free_and_replace(c
->stdio_fdname
[STDOUT_FILENO
], resolved
);
1042 free_and_replace(c
->stdio_file
[STDOUT_FILENO
], resolved
);
1047 assert(streq(lvalue
, "StandardError"));
1049 if (eo
== EXEC_OUTPUT_NAMED_FD
)
1050 free_and_replace(c
->stdio_fdname
[STDERR_FILENO
], resolved
);
1052 free_and_replace(c
->stdio_file
[STDERR_FILENO
], resolved
);
1060 int config_parse_exec_io_class(const char *unit
,
1061 const char *filename
,
1063 const char *section
,
1064 unsigned section_line
,
1071 ExecContext
*c
= data
;
1079 if (isempty(rvalue
)) {
1080 c
->ioprio_set
= false;
1081 c
->ioprio
= IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE
, 0);
1085 x
= ioprio_class_from_string(rvalue
);
1087 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse IO scheduling class, ignoring: %s", rvalue
);
1091 c
->ioprio
= IOPRIO_PRIO_VALUE(x
, IOPRIO_PRIO_DATA(c
->ioprio
));
1092 c
->ioprio_set
= true;
1097 int config_parse_exec_io_priority(const char *unit
,
1098 const char *filename
,
1100 const char *section
,
1101 unsigned section_line
,
1108 ExecContext
*c
= data
;
1116 if (isempty(rvalue
)) {
1117 c
->ioprio_set
= false;
1118 c
->ioprio
= IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE
, 0);
1122 r
= ioprio_parse_priority(rvalue
, &i
);
1124 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse IO priority, ignoring: %s", rvalue
);
1128 c
->ioprio
= IOPRIO_PRIO_VALUE(IOPRIO_PRIO_CLASS(c
->ioprio
), i
);
1129 c
->ioprio_set
= true;
1134 int config_parse_exec_cpu_sched_policy(const char *unit
,
1135 const char *filename
,
1137 const char *section
,
1138 unsigned section_line
,
1145 ExecContext
*c
= data
;
1153 if (isempty(rvalue
)) {
1154 c
->cpu_sched_set
= false;
1155 c
->cpu_sched_policy
= SCHED_OTHER
;
1156 c
->cpu_sched_priority
= 0;
1160 x
= sched_policy_from_string(rvalue
);
1162 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse CPU scheduling policy, ignoring: %s", rvalue
);
1166 c
->cpu_sched_policy
= x
;
1167 /* Moving to or from real-time policy? We need to adjust the priority */
1168 c
->cpu_sched_priority
= CLAMP(c
->cpu_sched_priority
, sched_get_priority_min(x
), sched_get_priority_max(x
));
1169 c
->cpu_sched_set
= true;
1174 int config_parse_exec_cpu_sched_prio(const char *unit
,
1175 const char *filename
,
1177 const char *section
,
1178 unsigned section_line
,
1185 ExecContext
*c
= data
;
1193 r
= safe_atoi(rvalue
, &i
);
1195 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse CPU scheduling priority, ignoring: %s", rvalue
);
1199 /* On Linux RR/FIFO range from 1 to 99 and OTHER/BATCH may only be 0 */
1200 min
= sched_get_priority_min(c
->cpu_sched_policy
);
1201 max
= sched_get_priority_max(c
->cpu_sched_policy
);
1203 if (i
< min
|| i
> max
) {
1204 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "CPU scheduling priority is out of range, ignoring: %s", rvalue
);
1208 c
->cpu_sched_priority
= i
;
1209 c
->cpu_sched_set
= true;
1214 int config_parse_exec_cpu_affinity(const char *unit
,
1215 const char *filename
,
1217 const char *section
,
1218 unsigned section_line
,
1225 ExecContext
*c
= data
;
1226 _cleanup_cpu_free_ cpu_set_t
*cpuset
= NULL
;
1234 ncpus
= parse_cpu_set_and_warn(rvalue
, &cpuset
, unit
, filename
, line
, lvalue
);
1239 /* An empty assignment resets the CPU list */
1240 c
->cpuset
= cpu_set_mfree(c
->cpuset
);
1241 c
->cpuset_ncpus
= 0;
1246 c
->cpuset
= TAKE_PTR(cpuset
);
1247 c
->cpuset_ncpus
= (unsigned) ncpus
;
1251 if (c
->cpuset_ncpus
< (unsigned) ncpus
) {
1252 CPU_OR_S(CPU_ALLOC_SIZE(c
->cpuset_ncpus
), cpuset
, c
->cpuset
, cpuset
);
1253 CPU_FREE(c
->cpuset
);
1254 c
->cpuset
= TAKE_PTR(cpuset
);
1255 c
->cpuset_ncpus
= (unsigned) ncpus
;
1259 CPU_OR_S(CPU_ALLOC_SIZE((unsigned) ncpus
), c
->cpuset
, c
->cpuset
, cpuset
);
1264 int config_parse_capability_set(
1266 const char *filename
,
1268 const char *section
,
1269 unsigned section_line
,
1276 uint64_t *capability_set
= data
;
1277 uint64_t sum
= 0, initial
= 0;
1278 bool invert
= false;
1286 if (rvalue
[0] == '~') {
1291 if (streq(lvalue
, "CapabilityBoundingSet"))
1292 initial
= CAP_ALL
; /* initialized to all bits on */
1293 /* else "AmbientCapabilities" initialized to all bits off */
1295 r
= capability_set_from_string(rvalue
, &sum
);
1297 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse %s= specifier '%s', ignoring: %m", lvalue
, rvalue
);
1301 if (sum
== 0 || *capability_set
== initial
)
1302 /* "", "~" or uninitialized data -> replace */
1303 *capability_set
= invert
? ~sum
: sum
;
1305 /* previous data -> merge */
1307 *capability_set
&= ~sum
;
1309 *capability_set
|= sum
;
1315 int config_parse_exec_selinux_context(
1317 const char *filename
,
1319 const char *section
,
1320 unsigned section_line
,
1327 ExecContext
*c
= data
;
1338 if (isempty(rvalue
)) {
1339 c
->selinux_context
= mfree(c
->selinux_context
);
1340 c
->selinux_context_ignore
= false;
1344 if (rvalue
[0] == '-') {
1350 r
= unit_full_printf(u
, rvalue
, &k
);
1352 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1353 "Failed to resolve unit specifiers in '%s'%s: %m",
1354 rvalue
, ignore
? ", ignoring" : "");
1355 return ignore
? 0 : -ENOEXEC
;
1358 free_and_replace(c
->selinux_context
, k
);
1359 c
->selinux_context_ignore
= ignore
;
1364 int config_parse_exec_apparmor_profile(
1366 const char *filename
,
1368 const char *section
,
1369 unsigned section_line
,
1376 ExecContext
*c
= data
;
1387 if (isempty(rvalue
)) {
1388 c
->apparmor_profile
= mfree(c
->apparmor_profile
);
1389 c
->apparmor_profile_ignore
= false;
1393 if (rvalue
[0] == '-') {
1399 r
= unit_full_printf(u
, rvalue
, &k
);
1401 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1402 "Failed to resolve unit specifiers in '%s'%s: %m",
1403 rvalue
, ignore
? ", ignoring" : "");
1404 return ignore
? 0 : -ENOEXEC
;
1407 free_and_replace(c
->apparmor_profile
, k
);
1408 c
->apparmor_profile_ignore
= ignore
;
1413 int config_parse_exec_smack_process_label(
1415 const char *filename
,
1417 const char *section
,
1418 unsigned section_line
,
1425 ExecContext
*c
= data
;
1436 if (isempty(rvalue
)) {
1437 c
->smack_process_label
= mfree(c
->smack_process_label
);
1438 c
->smack_process_label_ignore
= false;
1442 if (rvalue
[0] == '-') {
1448 r
= unit_full_printf(u
, rvalue
, &k
);
1450 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1451 "Failed to resolve unit specifiers in '%s'%s: %m",
1452 rvalue
, ignore
? ", ignoring" : "");
1453 return ignore
? 0 : -ENOEXEC
;
1456 free_and_replace(c
->smack_process_label
, k
);
1457 c
->smack_process_label_ignore
= ignore
;
1462 int config_parse_timer(const char *unit
,
1463 const char *filename
,
1465 const char *section
,
1466 unsigned section_line
,
1477 _cleanup_(calendar_spec_freep
) CalendarSpec
*c
= NULL
;
1479 _cleanup_free_
char *k
= NULL
;
1487 if (isempty(rvalue
)) {
1488 /* Empty assignment resets list */
1489 timer_free_values(t
);
1493 b
= timer_base_from_string(lvalue
);
1495 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse timer base, ignoring: %s", lvalue
);
1499 r
= unit_full_printf(u
, rvalue
, &k
);
1501 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue
);
1505 if (b
== TIMER_CALENDAR
) {
1506 if (calendar_spec_from_string(k
, &c
) < 0) {
1507 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse calendar specification, ignoring: %s", k
);
1511 if (parse_sec(k
, &usec
) < 0) {
1512 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse timer value, ignoring: %s", k
);
1516 v
= new0(TimerValue
, 1);
1522 v
->calendar_spec
= TAKE_PTR(c
);
1524 LIST_PREPEND(value
, t
->values
, v
);
1529 int config_parse_trigger_unit(
1531 const char *filename
,
1533 const char *section
,
1534 unsigned section_line
,
1541 _cleanup_free_
char *p
= NULL
;
1551 if (!hashmap_isempty(u
->dependencies
[UNIT_TRIGGERS
])) {
1552 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Multiple units to trigger specified, ignoring: %s", rvalue
);
1556 r
= unit_name_printf(u
, rvalue
, &p
);
1558 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue
);
1562 type
= unit_name_to_type(p
);
1564 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Unit type not valid, ignoring: %s", rvalue
);
1567 if (unit_has_name(u
, p
)) {
1568 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Units cannot trigger themselves, ignoring: %s", rvalue
);
1572 r
= unit_add_two_dependencies_by_name(u
, UNIT_BEFORE
, UNIT_TRIGGERS
, p
, true, UNIT_DEPENDENCY_FILE
);
1574 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to add trigger on %s, ignoring: %m", p
);
1581 int config_parse_path_spec(const char *unit
,
1582 const char *filename
,
1584 const char *section
,
1585 unsigned section_line
,
1595 _cleanup_free_
char *k
= NULL
;
1603 if (isempty(rvalue
)) {
1604 /* Empty assignment clears list */
1609 b
= path_type_from_string(lvalue
);
1611 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse path type, ignoring: %s", lvalue
);
1615 r
= unit_full_printf(UNIT(p
), rvalue
, &k
);
1617 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue
);
1621 r
= path_simplify_and_warn(k
, PATH_CHECK_ABSOLUTE
, unit
, filename
, line
, lvalue
);
1625 s
= new0(PathSpec
, 1);
1630 s
->path
= TAKE_PTR(k
);
1634 LIST_PREPEND(spec
, p
->specs
, s
);
1639 int config_parse_socket_service(
1641 const char *filename
,
1643 const char *section
,
1644 unsigned section_line
,
1651 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1652 _cleanup_free_
char *p
= NULL
;
1662 r
= unit_name_printf(UNIT(s
), rvalue
, &p
);
1664 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in %s: %m", rvalue
);
1668 if (!endswith(p
, ".service")) {
1669 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Unit must be of type service: %s", rvalue
);
1673 r
= manager_load_unit(UNIT(s
)->manager
, p
, NULL
, &error
, &x
);
1675 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to load unit %s: %s", rvalue
, bus_error_message(&error
, r
));
1679 unit_ref_set(&s
->service
, UNIT(s
), x
);
1684 int config_parse_fdname(
1686 const char *filename
,
1688 const char *section
,
1689 unsigned section_line
,
1696 _cleanup_free_
char *p
= NULL
;
1705 if (isempty(rvalue
)) {
1706 s
->fdname
= mfree(s
->fdname
);
1710 r
= unit_full_printf(UNIT(s
), rvalue
, &p
);
1712 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue
);
1716 if (!fdname_is_valid(p
)) {
1717 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid file descriptor name, ignoring: %s", p
);
1721 return free_and_replace(s
->fdname
, p
);
1724 int config_parse_service_sockets(
1726 const char *filename
,
1728 const char *section
,
1729 unsigned section_line
,
1747 _cleanup_free_
char *word
= NULL
, *k
= NULL
;
1749 r
= extract_first_word(&p
, &word
, NULL
, 0);
1755 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Trailing garbage in sockets, ignoring: %s", rvalue
);
1759 r
= unit_name_printf(UNIT(s
), word
, &k
);
1761 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in '%s', ignoring: %m", word
);
1765 if (!endswith(k
, ".socket")) {
1766 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Unit must be of type socket, ignoring: %s", k
);
1770 r
= unit_add_two_dependencies_by_name(UNIT(s
), UNIT_WANTS
, UNIT_AFTER
, k
, true, UNIT_DEPENDENCY_FILE
);
1772 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to add dependency on %s, ignoring: %m", k
);
1774 r
= unit_add_dependency_by_name(UNIT(s
), UNIT_TRIGGERED_BY
, k
, true, UNIT_DEPENDENCY_FILE
);
1776 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to add dependency on %s, ignoring: %m", k
);
1782 int config_parse_bus_name(
1784 const char *filename
,
1786 const char *section
,
1787 unsigned section_line
,
1794 _cleanup_free_
char *k
= NULL
;
1803 r
= unit_full_printf(u
, rvalue
, &k
);
1805 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue
);
1809 if (!service_name_is_valid(k
)) {
1810 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid bus name, ignoring: %s", k
);
1814 return config_parse_string(unit
, filename
, line
, section
, section_line
, lvalue
, ltype
, k
, data
, userdata
);
1817 int config_parse_service_timeout(
1819 const char *filename
,
1821 const char *section
,
1822 unsigned section_line
,
1829 Service
*s
= userdata
;
1838 /* This is called for two cases: TimeoutSec= and TimeoutStartSec=. */
1840 /* Traditionally, these options accepted 0 to disable the timeouts. However, a timeout of 0 suggests it happens
1841 * immediately, hence fix this to become USEC_INFINITY instead. This is in-line with how we internally handle
1842 * all other timeouts. */
1843 r
= parse_sec_fix_0(rvalue
, &usec
);
1845 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse %s= parameter, ignoring: %s", lvalue
, rvalue
);
1849 s
->start_timeout_defined
= true;
1850 s
->timeout_start_usec
= usec
;
1852 if (streq(lvalue
, "TimeoutSec"))
1853 s
->timeout_stop_usec
= usec
;
1858 int config_parse_sec_fix_0(
1860 const char *filename
,
1862 const char *section
,
1863 unsigned section_line
,
1870 usec_t
*usec
= data
;
1878 /* This is pretty much like config_parse_sec(), except that this treats a time of 0 as infinity, for
1879 * compatibility with older versions of systemd where 0 instead of infinity was used as indicator to turn off a
1882 r
= parse_sec_fix_0(rvalue
, usec
);
1884 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse %s= parameter, ignoring: %s", lvalue
, rvalue
);
1891 int config_parse_user_group(
1893 const char *filename
,
1895 const char *section
,
1896 unsigned section_line
,
1903 _cleanup_free_
char *k
= NULL
;
1913 if (isempty(rvalue
)) {
1914 *user
= mfree(*user
);
1918 r
= unit_full_printf(u
, rvalue
, &k
);
1920 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in %s: %m", rvalue
);
1924 if (!valid_user_group_name_or_id(k
)) {
1925 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid user/group name or numeric ID: %s", k
);
1929 return free_and_replace(*user
, k
);
1932 int config_parse_user_group_strv(
1934 const char *filename
,
1936 const char *section
,
1937 unsigned section_line
,
1944 char ***users
= data
;
1946 const char *p
= rvalue
;
1954 if (isempty(rvalue
)) {
1955 *users
= strv_free(*users
);
1960 _cleanup_free_
char *word
= NULL
, *k
= NULL
;
1962 r
= extract_first_word(&p
, &word
, NULL
, 0);
1968 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid syntax: %s", rvalue
);
1972 r
= unit_full_printf(u
, word
, &k
);
1974 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in %s: %m", word
);
1978 if (!valid_user_group_name_or_id(k
)) {
1979 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid user/group name or numeric ID: %s", k
);
1983 r
= strv_push(users
, k
);
1993 int config_parse_working_directory(
1995 const char *filename
,
1997 const char *section
,
1998 unsigned section_line
,
2005 ExecContext
*c
= data
;
2016 if (isempty(rvalue
)) {
2017 c
->working_directory_home
= false;
2018 c
->working_directory
= mfree(c
->working_directory
);
2022 if (rvalue
[0] == '-') {
2028 if (streq(rvalue
, "~")) {
2029 c
->working_directory_home
= true;
2030 c
->working_directory
= mfree(c
->working_directory
);
2032 _cleanup_free_
char *k
= NULL
;
2034 r
= unit_full_printf(u
, rvalue
, &k
);
2036 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
2037 "Failed to resolve unit specifiers in working directory path '%s'%s: %m",
2038 rvalue
, missing_ok
? ", ignoring" : "");
2039 return missing_ok
? 0 : -ENOEXEC
;
2042 r
= path_simplify_and_warn(k
, PATH_CHECK_ABSOLUTE
| (missing_ok
? 0 : PATH_CHECK_FATAL
), unit
, filename
, line
, lvalue
);
2044 return missing_ok
? 0 : -ENOEXEC
;
2046 c
->working_directory_home
= false;
2047 free_and_replace(c
->working_directory
, k
);
2050 c
->working_directory_missing_ok
= missing_ok
;
2054 int config_parse_unit_env_file(const char *unit
,
2055 const char *filename
,
2057 const char *section
,
2058 unsigned section_line
,
2067 _cleanup_free_
char *n
= NULL
;
2075 if (isempty(rvalue
)) {
2076 /* Empty assignment frees the list */
2077 *env
= strv_free(*env
);
2081 r
= unit_full_printf(u
, rvalue
, &n
);
2083 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue
);
2087 r
= path_simplify_and_warn(n
[0] == '-' ? n
+ 1 : n
, PATH_CHECK_ABSOLUTE
, unit
, filename
, line
, lvalue
);
2091 r
= strv_push(env
, n
);
2100 int config_parse_environ(
2102 const char *filename
,
2104 const char *section
,
2105 unsigned section_line
,
2122 if (isempty(rvalue
)) {
2123 /* Empty assignment resets the list */
2124 *env
= strv_free(*env
);
2128 for (p
= rvalue
;; ) {
2129 _cleanup_free_
char *word
= NULL
, *k
= NULL
;
2131 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_CUNESCAPE
|EXTRACT_QUOTES
);
2137 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
2138 "Invalid syntax, ignoring: %s", rvalue
);
2143 r
= unit_full_printf(u
, word
, &k
);
2145 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
2146 "Failed to resolve unit specifiers in %s, ignoring: %m", word
);
2152 if (!env_assignment_is_valid(k
)) {
2153 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
2154 "Invalid environment assignment, ignoring: %s", k
);
2158 r
= strv_env_replace(env
, k
);
2166 int config_parse_pass_environ(
2168 const char *filename
,
2170 const char *section
,
2171 unsigned section_line
,
2178 _cleanup_strv_free_
char **n
= NULL
;
2179 size_t nlen
= 0, nbufsize
= 0;
2180 char*** passenv
= data
;
2181 const char *p
= rvalue
;
2190 if (isempty(rvalue
)) {
2191 /* Empty assignment resets the list */
2192 *passenv
= strv_free(*passenv
);
2197 _cleanup_free_
char *word
= NULL
, *k
= NULL
;
2199 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
);
2205 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
2206 "Trailing garbage in %s, ignoring: %s", lvalue
, rvalue
);
2211 r
= unit_full_printf(u
, word
, &k
);
2213 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
2214 "Failed to resolve specifiers in %s, ignoring: %m", word
);
2220 if (!env_name_is_valid(k
)) {
2221 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
2222 "Invalid environment name for %s, ignoring: %s", lvalue
, k
);
2226 if (!GREEDY_REALLOC(n
, nbufsize
, nlen
+ 2))
2229 n
[nlen
++] = TAKE_PTR(k
);
2234 r
= strv_extend_strv(passenv
, n
, true);
2242 int config_parse_unset_environ(
2244 const char *filename
,
2246 const char *section
,
2247 unsigned section_line
,
2254 _cleanup_strv_free_
char **n
= NULL
;
2255 size_t nlen
= 0, nbufsize
= 0;
2256 char*** unsetenv
= data
;
2257 const char *p
= rvalue
;
2266 if (isempty(rvalue
)) {
2267 /* Empty assignment resets the list */
2268 *unsetenv
= strv_free(*unsetenv
);
2273 _cleanup_free_
char *word
= NULL
, *k
= NULL
;
2275 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_CUNESCAPE
|EXTRACT_QUOTES
);
2281 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
2282 "Trailing garbage in %s, ignoring: %s", lvalue
, rvalue
);
2287 r
= unit_full_printf(u
, word
, &k
);
2289 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
2290 "Failed to resolve unit specifiers in %s, ignoring: %m", word
);
2296 if (!env_assignment_is_valid(k
) && !env_name_is_valid(k
)) {
2297 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
2298 "Invalid environment name or assignment %s, ignoring: %s", lvalue
, k
);
2302 if (!GREEDY_REALLOC(n
, nbufsize
, nlen
+ 2))
2305 n
[nlen
++] = TAKE_PTR(k
);
2310 r
= strv_extend_strv(unsetenv
, n
, true);
2318 int config_parse_log_extra_fields(
2320 const char *filename
,
2322 const char *section
,
2323 unsigned section_line
,
2330 ExecContext
*c
= data
;
2332 const char *p
= rvalue
;
2340 if (isempty(rvalue
)) {
2341 exec_context_free_log_extra_fields(c
);
2346 _cleanup_free_
char *word
= NULL
, *k
= NULL
;
2350 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_CUNESCAPE
|EXTRACT_QUOTES
);
2356 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Invalid syntax, ignoring: %s", rvalue
);
2360 r
= unit_full_printf(u
, word
, &k
);
2362 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in %s, ignoring: %m", word
);
2366 eq
= strchr(k
, '=');
2368 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Log field lacks '=' character, ignoring: %s", k
);
2372 if (!journal_field_valid(k
, eq
-k
, false)) {
2373 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Log field name is invalid, ignoring: %s", k
);
2377 t
= reallocarray(c
->log_extra_fields
, c
->n_log_extra_fields
+1, sizeof(struct iovec
));
2381 c
->log_extra_fields
= t
;
2382 c
->log_extra_fields
[c
->n_log_extra_fields
++] = IOVEC_MAKE_STRING(k
);
2388 int config_parse_unit_condition_path(
2390 const char *filename
,
2392 const char *section
,
2393 unsigned section_line
,
2400 _cleanup_free_
char *p
= NULL
;
2401 Condition
**list
= data
, *c
;
2402 ConditionType t
= ltype
;
2403 bool trigger
, negate
;
2412 if (isempty(rvalue
)) {
2413 /* Empty assignment resets the list */
2414 *list
= condition_free_list(*list
);
2418 trigger
= rvalue
[0] == '|';
2422 negate
= rvalue
[0] == '!';
2426 r
= unit_full_printf(u
, rvalue
, &p
);
2428 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue
);
2432 r
= path_simplify_and_warn(p
, PATH_CHECK_ABSOLUTE
, unit
, filename
, line
, lvalue
);
2436 c
= condition_new(t
, p
, trigger
, negate
);
2440 LIST_PREPEND(conditions
, *list
, c
);
2444 int config_parse_unit_condition_string(
2446 const char *filename
,
2448 const char *section
,
2449 unsigned section_line
,
2456 _cleanup_free_
char *s
= NULL
;
2457 Condition
**list
= data
, *c
;
2458 ConditionType t
= ltype
;
2459 bool trigger
, negate
;
2468 if (isempty(rvalue
)) {
2469 /* Empty assignment resets the list */
2470 *list
= condition_free_list(*list
);
2474 trigger
= rvalue
[0] == '|';
2478 negate
= rvalue
[0] == '!';
2482 r
= unit_full_printf(u
, rvalue
, &s
);
2484 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue
);
2488 c
= condition_new(t
, s
, trigger
, negate
);
2492 LIST_PREPEND(conditions
, *list
, c
);
2496 int config_parse_unit_condition_null(
2498 const char *filename
,
2500 const char *section
,
2501 unsigned section_line
,
2508 Condition
**list
= data
, *c
;
2509 bool trigger
, negate
;
2517 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "%s= is deprecated, please do not use.", lvalue
);
2519 if (isempty(rvalue
)) {
2520 /* Empty assignment resets the list */
2521 *list
= condition_free_list(*list
);
2525 trigger
= rvalue
[0] == '|';
2529 negate
= rvalue
[0] == '!';
2533 b
= parse_boolean(rvalue
);
2535 log_syntax(unit
, LOG_ERR
, filename
, line
, b
, "Failed to parse boolean value in condition, ignoring: %s", rvalue
);
2542 c
= condition_new(CONDITION_NULL
, NULL
, trigger
, negate
);
2546 LIST_PREPEND(conditions
, *list
, c
);
2550 int config_parse_unit_requires_mounts_for(
2552 const char *filename
,
2554 const char *section
,
2555 unsigned section_line
,
2562 const char *p
= rvalue
;
2572 _cleanup_free_
char *word
= NULL
, *resolved
= NULL
;
2574 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
);
2580 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
2581 "Invalid syntax, ignoring: %s", rvalue
);
2585 r
= unit_full_printf(u
, word
, &resolved
);
2587 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in '%s', ignoring: %m", word
);
2591 r
= path_simplify_and_warn(resolved
, PATH_CHECK_ABSOLUTE
, unit
, filename
, line
, lvalue
);
2595 r
= unit_require_mounts_for(u
, resolved
, UNIT_DEPENDENCY_FILE
);
2597 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to add required mount '%s', ignoring: %m", resolved
);
2603 int config_parse_documentation(const char *unit
,
2604 const char *filename
,
2606 const char *section
,
2607 unsigned section_line
,
2623 if (isempty(rvalue
)) {
2624 /* Empty assignment resets the list */
2625 u
->documentation
= strv_free(u
->documentation
);
2629 r
= config_parse_unit_strv_printf(unit
, filename
, line
, section
, section_line
, lvalue
, ltype
,
2630 rvalue
, data
, userdata
);
2634 for (a
= b
= u
->documentation
; a
&& *a
; a
++) {
2636 if (documentation_url_is_valid(*a
))
2639 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid URL, ignoring: %s", *a
);
2650 int config_parse_syscall_filter(
2652 const char *filename
,
2654 const char *section
,
2655 unsigned section_line
,
2662 ExecContext
*c
= data
;
2664 bool invert
= false;
2673 if (isempty(rvalue
)) {
2674 /* Empty assignment resets the list */
2675 c
->syscall_filter
= hashmap_free(c
->syscall_filter
);
2676 c
->syscall_whitelist
= false;
2680 if (rvalue
[0] == '~') {
2685 if (!c
->syscall_filter
) {
2686 c
->syscall_filter
= hashmap_new(NULL
);
2687 if (!c
->syscall_filter
)
2691 /* Allow everything but the ones listed */
2692 c
->syscall_whitelist
= false;
2694 /* Allow nothing but the ones listed */
2695 c
->syscall_whitelist
= true;
2697 /* Accept default syscalls if we are on a whitelist */
2698 r
= seccomp_parse_syscall_filter(
2699 "@default", -1, c
->syscall_filter
,
2700 SECCOMP_PARSE_PERMISSIVE
|SECCOMP_PARSE_WHITELIST
);
2708 _cleanup_free_
char *word
= NULL
, *name
= NULL
;
2711 r
= extract_first_word(&p
, &word
, NULL
, 0);
2717 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Invalid syntax, ignoring: %s", rvalue
);
2721 r
= parse_syscall_and_errno(word
, &name
, &num
);
2723 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Failed to parse syscall:errno, ignoring: %s", word
);
2727 r
= seccomp_parse_syscall_filter_full(name
, num
, c
->syscall_filter
,
2728 SECCOMP_PARSE_LOG
|SECCOMP_PARSE_PERMISSIVE
|(invert
? SECCOMP_PARSE_INVERT
: 0)|(c
->syscall_whitelist
? SECCOMP_PARSE_WHITELIST
: 0),
2729 unit
, filename
, line
);
2735 int config_parse_syscall_archs(
2737 const char *filename
,
2739 const char *section
,
2740 unsigned section_line
,
2747 const char *p
= rvalue
;
2751 if (isempty(rvalue
)) {
2752 *archs
= set_free(*archs
);
2756 r
= set_ensure_allocated(archs
, NULL
);
2761 _cleanup_free_
char *word
= NULL
;
2764 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
);
2770 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
2771 "Invalid syntax, ignoring: %s", rvalue
);
2775 r
= seccomp_arch_from_string(word
, &a
);
2777 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
2778 "Failed to parse system call architecture \"%s\", ignoring: %m", word
);
2782 r
= set_put(*archs
, UINT32_TO_PTR(a
+ 1));
2788 int config_parse_syscall_errno(
2790 const char *filename
,
2792 const char *section
,
2793 unsigned section_line
,
2800 ExecContext
*c
= data
;
2807 if (isempty(rvalue
)) {
2808 /* Empty assignment resets to KILL */
2809 c
->syscall_errno
= 0;
2813 e
= parse_errno(rvalue
);
2815 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse error number, ignoring: %s", rvalue
);
2819 c
->syscall_errno
= e
;
2823 int config_parse_address_families(
2825 const char *filename
,
2827 const char *section
,
2828 unsigned section_line
,
2835 ExecContext
*c
= data
;
2836 bool invert
= false;
2844 if (isempty(rvalue
)) {
2845 /* Empty assignment resets the list */
2846 c
->address_families
= set_free(c
->address_families
);
2847 c
->address_families_whitelist
= false;
2851 if (rvalue
[0] == '~') {
2856 if (!c
->address_families
) {
2857 c
->address_families
= set_new(NULL
);
2858 if (!c
->address_families
)
2861 c
->address_families_whitelist
= !invert
;
2864 for (p
= rvalue
;;) {
2865 _cleanup_free_
char *word
= NULL
;
2868 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
);
2874 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
2875 "Invalid syntax, ignoring: %s", rvalue
);
2879 af
= af_from_name(word
);
2881 log_syntax(unit
, LOG_ERR
, filename
, line
, af
,
2882 "Failed to parse address family, ignoring: %s", word
);
2886 /* If we previously wanted to forbid an address family and now
2887 * we want to allow it, then just remove it from the list.
2889 if (!invert
== c
->address_families_whitelist
) {
2890 r
= set_put(c
->address_families
, INT_TO_PTR(af
));
2894 set_remove(c
->address_families
, INT_TO_PTR(af
));
2898 int config_parse_restrict_namespaces(
2900 const char *filename
,
2902 const char *section
,
2903 unsigned section_line
,
2910 ExecContext
*c
= data
;
2911 unsigned long flags
;
2912 bool invert
= false;
2915 if (isempty(rvalue
)) {
2916 /* Reset to the default. */
2917 c
->restrict_namespaces
= NAMESPACE_FLAGS_INITIAL
;
2921 /* Boolean parameter ignores the previous settings */
2922 r
= parse_boolean(rvalue
);
2924 c
->restrict_namespaces
= 0;
2926 } else if (r
== 0) {
2927 c
->restrict_namespaces
= NAMESPACE_FLAGS_ALL
;
2931 if (rvalue
[0] == '~') {
2936 /* Not a boolean argument, in this case it's a list of namespace types. */
2937 r
= namespace_flags_from_string(rvalue
, &flags
);
2939 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse namespace type string, ignoring: %s", rvalue
);
2943 if (c
->restrict_namespaces
== NAMESPACE_FLAGS_INITIAL
)
2944 /* Initial assignment. Just set the value. */
2945 c
->restrict_namespaces
= invert
? (~flags
) & NAMESPACE_FLAGS_ALL
: flags
;
2947 /* Merge the value with the previous one. */
2948 SET_FLAG(c
->restrict_namespaces
, flags
, !invert
);
2954 int config_parse_unit_slice(
2956 const char *filename
,
2958 const char *section
,
2959 unsigned section_line
,
2966 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2967 _cleanup_free_
char *k
= NULL
;
2968 Unit
*u
= userdata
, *slice
= NULL
;
2976 r
= unit_name_printf(u
, rvalue
, &k
);
2978 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue
);
2982 r
= manager_load_unit(u
->manager
, k
, NULL
, &error
, &slice
);
2984 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to load slice unit %s, ignoring: %s", k
, bus_error_message(&error
, r
));
2988 r
= unit_set_slice(u
, slice
);
2990 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to assign slice %s to unit %s, ignoring: %m", slice
->id
, u
->id
);
2997 int config_parse_cpu_quota(
2999 const char *filename
,
3001 const char *section
,
3002 unsigned section_line
,
3009 CGroupContext
*c
= data
;
3016 if (isempty(rvalue
)) {
3017 c
->cpu_quota_per_sec_usec
= USEC_INFINITY
;
3021 r
= parse_permille_unbounded(rvalue
);
3023 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid CPU quota '%s', ignoring.", rvalue
);
3027 c
->cpu_quota_per_sec_usec
= ((usec_t
) r
* USEC_PER_SEC
) / 1000U;
3031 int config_parse_memory_limit(
3033 const char *filename
,
3035 const char *section
,
3036 unsigned section_line
,
3043 CGroupContext
*c
= data
;
3044 uint64_t bytes
= CGROUP_LIMIT_MAX
;
3047 if (!isempty(rvalue
) && !streq(rvalue
, "infinity")) {
3049 r
= parse_permille(rvalue
);
3051 r
= parse_size(rvalue
, 1024, &bytes
);
3053 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid memory limit '%s', ignoring: %m", rvalue
);
3057 bytes
= physical_memory_scale(r
, 1000U);
3059 if (bytes
>= UINT64_MAX
||
3060 (bytes
<= 0 && !streq(lvalue
, "MemorySwapMax"))) {
3061 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Memory limit '%s' out of range, ignoring.", rvalue
);
3066 if (streq(lvalue
, "MemoryMin"))
3067 c
->memory_min
= bytes
;
3068 else if (streq(lvalue
, "MemoryLow"))
3069 c
->memory_low
= bytes
;
3070 else if (streq(lvalue
, "MemoryHigh"))
3071 c
->memory_high
= bytes
;
3072 else if (streq(lvalue
, "MemoryMax"))
3073 c
->memory_max
= bytes
;
3074 else if (streq(lvalue
, "MemorySwapMax"))
3075 c
->memory_swap_max
= bytes
;
3076 else if (streq(lvalue
, "MemoryLimit"))
3077 c
->memory_limit
= bytes
;
3084 int config_parse_tasks_max(
3086 const char *filename
,
3088 const char *section
,
3089 unsigned section_line
,
3096 uint64_t *tasks_max
= data
, v
;
3100 if (isempty(rvalue
)) {
3101 *tasks_max
= u
? u
->manager
->default_tasks_max
: UINT64_MAX
;
3105 if (streq(rvalue
, "infinity")) {
3106 *tasks_max
= CGROUP_LIMIT_MAX
;
3110 r
= parse_permille(rvalue
);
3112 r
= safe_atou64(rvalue
, &v
);
3114 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid maximum tasks value '%s', ignoring: %m", rvalue
);
3118 v
= system_tasks_max_scale(r
, 1000U);
3120 if (v
<= 0 || v
>= UINT64_MAX
) {
3121 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Maximum tasks value '%s' out of range, ignoring.", rvalue
);
3129 int config_parse_delegate(
3131 const char *filename
,
3133 const char *section
,
3134 unsigned section_line
,
3141 CGroupContext
*c
= data
;
3145 t
= unit_name_to_type(unit
);
3146 assert(t
!= _UNIT_TYPE_INVALID
);
3148 if (!unit_vtable
[t
]->can_delegate
) {
3149 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Delegate= setting not supported for this unit type, ignoring.");
3153 /* We either accept a boolean value, which may be used to turn on delegation for all controllers, or turn it
3154 * off for all. Or it takes a list of controller names, in which case we add the specified controllers to the
3155 * mask to delegate. */
3157 if (isempty(rvalue
)) {
3158 /* An empty string resets controllers and set Delegate=yes. */
3160 c
->delegate_controllers
= 0;
3164 r
= parse_boolean(rvalue
);
3166 const char *p
= rvalue
;
3167 CGroupMask mask
= 0;
3170 _cleanup_free_
char *word
= NULL
;
3171 CGroupController cc
;
3173 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
);
3179 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid syntax, ignoring: %s", rvalue
);
3183 cc
= cgroup_controller_from_string(word
);
3185 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid controller name '%s', ignoring", word
);
3189 mask
|= CGROUP_CONTROLLER_TO_MASK(cc
);
3193 c
->delegate_controllers
|= mask
;
3197 c
->delegate_controllers
= _CGROUP_MASK_ALL
;
3199 c
->delegate
= false;
3200 c
->delegate_controllers
= 0;
3206 int config_parse_device_allow(
3208 const char *filename
,
3210 const char *section
,
3211 unsigned section_line
,
3218 _cleanup_free_
char *path
= NULL
, *resolved
= NULL
;
3219 CGroupContext
*c
= data
;
3220 const char *p
= rvalue
;
3223 if (isempty(rvalue
)) {
3224 while (c
->device_allow
)
3225 cgroup_context_free_device_allow(c
, c
->device_allow
);
3230 r
= extract_first_word(&p
, &path
, NULL
, EXTRACT_QUOTES
);
3234 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
3235 "Invalid syntax, ignoring: %s", rvalue
);
3239 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
3240 "Failed to extract device path and rights from '%s', ignoring.", rvalue
);
3244 r
= unit_full_printf(userdata
, path
, &resolved
);
3246 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
3247 "Failed to resolve unit specifiers in '%s', ignoring: %m", path
);
3251 if (!STARTSWITH_SET(resolved
, "block-", "char-")) {
3253 r
= path_simplify_and_warn(resolved
, 0, unit
, filename
, line
, lvalue
);
3257 if (!valid_device_node_path(resolved
)) {
3258 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid device node path '%s', ignoring.", resolved
);
3263 if (!isempty(p
) && !in_charset(p
, "rwm")) {
3264 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid device rights '%s', ignoring.", p
);
3268 return cgroup_add_device_allow(c
, resolved
, p
);
3271 int config_parse_io_device_weight(
3273 const char *filename
,
3275 const char *section
,
3276 unsigned section_line
,
3283 _cleanup_free_
char *path
= NULL
, *resolved
= NULL
;
3284 CGroupIODeviceWeight
*w
;
3285 CGroupContext
*c
= data
;
3286 const char *p
= rvalue
;
3294 if (isempty(rvalue
)) {
3295 while (c
->io_device_weights
)
3296 cgroup_context_free_io_device_weight(c
, c
->io_device_weights
);
3301 r
= extract_first_word(&p
, &path
, NULL
, EXTRACT_QUOTES
);
3305 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
3306 "Invalid syntax, ignoring: %s", rvalue
);
3309 if (r
== 0 || isempty(p
)) {
3310 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
3311 "Failed to extract device path and weight from '%s', ignoring.", rvalue
);
3315 r
= unit_full_printf(userdata
, path
, &resolved
);
3317 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
3318 "Failed to resolve unit specifiers in '%s', ignoring: %m", path
);
3322 r
= path_simplify_and_warn(resolved
, 0, unit
, filename
, line
, lvalue
);
3326 r
= cg_weight_parse(p
, &u
);
3328 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "IO weight '%s' invalid, ignoring: %m", p
);
3332 assert(u
!= CGROUP_WEIGHT_INVALID
);
3334 w
= new0(CGroupIODeviceWeight
, 1);
3338 w
->path
= TAKE_PTR(resolved
);
3341 LIST_PREPEND(device_weights
, c
->io_device_weights
, w
);
3345 int config_parse_io_device_latency(
3347 const char *filename
,
3349 const char *section
,
3350 unsigned section_line
,
3357 _cleanup_free_
char *path
= NULL
, *resolved
= NULL
;
3358 CGroupIODeviceLatency
*l
;
3359 CGroupContext
*c
= data
;
3360 const char *p
= rvalue
;
3368 if (isempty(rvalue
)) {
3369 while (c
->io_device_latencies
)
3370 cgroup_context_free_io_device_latency(c
, c
->io_device_latencies
);
3375 r
= extract_first_word(&p
, &path
, NULL
, EXTRACT_QUOTES
);
3379 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
3380 "Invalid syntax, ignoring: %s", rvalue
);
3383 if (r
== 0 || isempty(p
)) {
3384 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
3385 "Failed to extract device path and latency from '%s', ignoring.", rvalue
);
3389 r
= unit_full_printf(userdata
, path
, &resolved
);
3391 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
3392 "Failed to resolve unit specifiers in '%s', ignoring: %m", path
);
3396 r
= path_simplify_and_warn(resolved
, 0, unit
, filename
, line
, lvalue
);
3400 if (parse_sec(p
, &usec
) < 0) {
3401 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse timer value, ignoring: %s", p
);
3405 l
= new0(CGroupIODeviceLatency
, 1);
3409 l
->path
= TAKE_PTR(resolved
);
3410 l
->target_usec
= usec
;
3412 LIST_PREPEND(device_latencies
, c
->io_device_latencies
, l
);
3416 int config_parse_io_limit(
3418 const char *filename
,
3420 const char *section
,
3421 unsigned section_line
,
3428 _cleanup_free_
char *path
= NULL
, *resolved
= NULL
;
3429 CGroupIODeviceLimit
*l
= NULL
, *t
;
3430 CGroupContext
*c
= data
;
3431 CGroupIOLimitType type
;
3432 const char *p
= rvalue
;
3440 type
= cgroup_io_limit_type_from_string(lvalue
);
3443 if (isempty(rvalue
)) {
3444 LIST_FOREACH(device_limits
, l
, c
->io_device_limits
)
3445 l
->limits
[type
] = cgroup_io_limit_defaults
[type
];
3449 r
= extract_first_word(&p
, &path
, NULL
, EXTRACT_QUOTES
);
3453 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
3454 "Invalid syntax, ignoring: %s", rvalue
);
3457 if (r
== 0 || isempty(p
)) {
3458 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
3459 "Failed to extract device node and bandwidth from '%s', ignoring.", rvalue
);
3463 r
= unit_full_printf(userdata
, path
, &resolved
);
3465 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
3466 "Failed to resolve unit specifiers in '%s', ignoring: %m", path
);
3470 r
= path_simplify_and_warn(resolved
, 0, unit
, filename
, line
, lvalue
);
3474 if (streq("infinity", p
))
3475 num
= CGROUP_LIMIT_MAX
;
3477 r
= parse_size(p
, 1000, &num
);
3478 if (r
< 0 || num
<= 0) {
3479 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid IO limit '%s', ignoring.", p
);
3484 LIST_FOREACH(device_limits
, t
, c
->io_device_limits
) {
3485 if (path_equal(resolved
, t
->path
)) {
3492 CGroupIOLimitType ttype
;
3494 l
= new0(CGroupIODeviceLimit
, 1);
3498 l
->path
= TAKE_PTR(resolved
);
3499 for (ttype
= 0; ttype
< _CGROUP_IO_LIMIT_TYPE_MAX
; ttype
++)
3500 l
->limits
[ttype
] = cgroup_io_limit_defaults
[ttype
];
3502 LIST_PREPEND(device_limits
, c
->io_device_limits
, l
);
3505 l
->limits
[type
] = num
;
3510 int config_parse_blockio_device_weight(
3512 const char *filename
,
3514 const char *section
,
3515 unsigned section_line
,
3522 _cleanup_free_
char *path
= NULL
, *resolved
= NULL
;
3523 CGroupBlockIODeviceWeight
*w
;
3524 CGroupContext
*c
= data
;
3525 const char *p
= rvalue
;
3533 if (isempty(rvalue
)) {
3534 while (c
->blockio_device_weights
)
3535 cgroup_context_free_blockio_device_weight(c
, c
->blockio_device_weights
);
3540 r
= extract_first_word(&p
, &path
, NULL
, EXTRACT_QUOTES
);
3544 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
3545 "Invalid syntax, ignoring: %s", rvalue
);
3548 if (r
== 0 || isempty(p
)) {
3549 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
3550 "Failed to extract device node and weight from '%s', ignoring.", rvalue
);
3554 r
= unit_full_printf(userdata
, path
, &resolved
);
3556 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
3557 "Failed to resolve unit specifiers in '%s', ignoring: %m", path
);
3561 r
= path_simplify_and_warn(resolved
, 0, unit
, filename
, line
, lvalue
);
3565 r
= cg_blkio_weight_parse(p
, &u
);
3567 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid block IO weight '%s', ignoring: %m", p
);
3571 assert(u
!= CGROUP_BLKIO_WEIGHT_INVALID
);
3573 w
= new0(CGroupBlockIODeviceWeight
, 1);
3577 w
->path
= TAKE_PTR(resolved
);
3580 LIST_PREPEND(device_weights
, c
->blockio_device_weights
, w
);
3584 int config_parse_blockio_bandwidth(
3586 const char *filename
,
3588 const char *section
,
3589 unsigned section_line
,
3596 _cleanup_free_
char *path
= NULL
, *resolved
= NULL
;
3597 CGroupBlockIODeviceBandwidth
*b
= NULL
, *t
;
3598 CGroupContext
*c
= data
;
3599 const char *p
= rvalue
;
3608 read
= streq("BlockIOReadBandwidth", lvalue
);
3610 if (isempty(rvalue
)) {
3611 LIST_FOREACH(device_bandwidths
, b
, c
->blockio_device_bandwidths
) {
3612 b
->rbps
= CGROUP_LIMIT_MAX
;
3613 b
->wbps
= CGROUP_LIMIT_MAX
;
3618 r
= extract_first_word(&p
, &path
, NULL
, EXTRACT_QUOTES
);
3622 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
3623 "Invalid syntax, ignoring: %s", rvalue
);
3626 if (r
== 0 || isempty(p
)) {
3627 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
3628 "Failed to extract device node and bandwidth from '%s', ignoring.", rvalue
);
3632 r
= unit_full_printf(userdata
, path
, &resolved
);
3634 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
3635 "Failed to resolve unit specifiers in '%s', ignoring: %m", path
);
3639 r
= path_simplify_and_warn(resolved
, 0, unit
, filename
, line
, lvalue
);
3643 r
= parse_size(p
, 1000, &bytes
);
3644 if (r
< 0 || bytes
<= 0) {
3645 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid Block IO Bandwidth '%s', ignoring.", p
);
3649 LIST_FOREACH(device_bandwidths
, t
, c
->blockio_device_bandwidths
) {
3650 if (path_equal(resolved
, t
->path
)) {
3657 b
= new0(CGroupBlockIODeviceBandwidth
, 1);
3661 b
->path
= TAKE_PTR(resolved
);
3662 b
->rbps
= CGROUP_LIMIT_MAX
;
3663 b
->wbps
= CGROUP_LIMIT_MAX
;
3665 LIST_PREPEND(device_bandwidths
, c
->blockio_device_bandwidths
, b
);
3676 int config_parse_job_mode_isolate(
3678 const char *filename
,
3680 const char *section
,
3681 unsigned section_line
,
3695 r
= parse_boolean(rvalue
);
3697 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse boolean, ignoring: %s", rvalue
);
3701 log_notice("%s is deprecated. Please use OnFailureJobMode= instead", lvalue
);
3703 *m
= r
? JOB_ISOLATE
: JOB_REPLACE
;
3707 int config_parse_exec_directories(
3709 const char *filename
,
3711 const char *section
,
3712 unsigned section_line
,
3729 if (isempty(rvalue
)) {
3730 /* Empty assignment resets the list */
3731 *rt
= strv_free(*rt
);
3735 for (p
= rvalue
;;) {
3736 _cleanup_free_
char *word
= NULL
, *k
= NULL
;
3738 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
);
3742 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
3743 "Invalid syntax, ignoring: %s", rvalue
);
3749 r
= unit_full_printf(u
, word
, &k
);
3751 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
3752 "Failed to resolve unit specifiers in \"%s\", ignoring: %m", word
);
3756 r
= path_simplify_and_warn(k
, PATH_CHECK_RELATIVE
, unit
, filename
, line
, lvalue
);
3760 if (path_startswith(k
, "private")) {
3761 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
3762 "%s= path can't be 'private', ignoring assignment: %s", lvalue
, word
);
3766 r
= strv_push(rt
, k
);
3773 int config_parse_set_status(
3775 const char *filename
,
3777 const char *section
,
3778 unsigned section_line
,
3786 const char *word
, *state
;
3788 ExitStatusSet
*status_set
= data
;
3795 /* Empty assignment resets the list */
3796 if (isempty(rvalue
)) {
3797 exit_status_set_free(status_set
);
3801 FOREACH_WORD(word
, l
, rvalue
, state
) {
3802 _cleanup_free_
char *temp
;
3806 temp
= strndup(word
, l
);
3810 r
= safe_atoi(temp
, &val
);
3812 val
= signal_from_string(temp
);
3815 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse value, ignoring: %s", word
);
3818 set
= &status_set
->signal
;
3820 if (val
< 0 || val
> 255) {
3821 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Value %d is outside range 0-255, ignoring", val
);
3824 set
= &status_set
->status
;
3827 r
= set_ensure_allocated(set
, NULL
);
3831 r
= set_put(*set
, INT_TO_PTR(val
));
3835 if (!isempty(state
))
3836 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Trailing garbage, ignoring.");
3841 int config_parse_namespace_path_strv(
3843 const char *filename
,
3845 const char *section
,
3846 unsigned section_line
,
3855 const char *p
= rvalue
;
3863 if (isempty(rvalue
)) {
3864 /* Empty assignment resets the list */
3865 *sv
= strv_free(*sv
);
3870 _cleanup_free_
char *word
= NULL
, *resolved
= NULL
, *joined
= NULL
;
3872 bool ignore_enoent
= false, shall_prefix
= false;
3874 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
);
3880 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to extract first word, ignoring: %s", rvalue
);
3885 if (startswith(w
, "-")) {
3886 ignore_enoent
= true;
3889 if (startswith(w
, "+")) {
3890 shall_prefix
= true;
3894 r
= unit_full_printf(u
, w
, &resolved
);
3896 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in %s: %m", w
);
3900 r
= path_simplify_and_warn(resolved
, PATH_CHECK_ABSOLUTE
, unit
, filename
, line
, lvalue
);
3904 joined
= strjoin(ignore_enoent
? "-" : "",
3905 shall_prefix
? "+" : "",
3908 r
= strv_push(sv
, joined
);
3918 int config_parse_temporary_filesystems(
3920 const char *filename
,
3922 const char *section
,
3923 unsigned section_line
,
3931 ExecContext
*c
= data
;
3932 const char *p
= rvalue
;
3940 if (isempty(rvalue
)) {
3941 /* Empty assignment resets the list */
3942 temporary_filesystem_free_many(c
->temporary_filesystems
, c
->n_temporary_filesystems
);
3943 c
->temporary_filesystems
= NULL
;
3944 c
->n_temporary_filesystems
= 0;
3949 _cleanup_free_
char *word
= NULL
, *path
= NULL
, *resolved
= NULL
;
3952 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
);
3958 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to extract first word, ignoring: %s", rvalue
);
3963 r
= extract_first_word(&w
, &path
, ":", EXTRACT_DONT_COALESCE_SEPARATORS
);
3967 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to extract first word, ignoring: %s", word
);
3971 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid syntax, ignoring: %s", word
);
3975 r
= unit_full_printf(u
, path
, &resolved
);
3977 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in %s, ignoring: %m", path
);
3981 r
= path_simplify_and_warn(resolved
, PATH_CHECK_ABSOLUTE
, unit
, filename
, line
, lvalue
);
3985 r
= temporary_filesystem_add(&c
->temporary_filesystems
, &c
->n_temporary_filesystems
, resolved
, w
);
3991 int config_parse_bind_paths(
3993 const char *filename
,
3995 const char *section
,
3996 unsigned section_line
,
4003 ExecContext
*c
= data
;
4013 if (isempty(rvalue
)) {
4014 /* Empty assignment resets the list */
4015 bind_mount_free_many(c
->bind_mounts
, c
->n_bind_mounts
);
4016 c
->bind_mounts
= NULL
;
4017 c
->n_bind_mounts
= 0;
4023 _cleanup_free_
char *source
= NULL
, *destination
= NULL
;
4024 _cleanup_free_
char *sresolved
= NULL
, *dresolved
= NULL
;
4025 char *s
= NULL
, *d
= NULL
;
4026 bool rbind
= true, ignore_enoent
= false;
4028 r
= extract_first_word(&p
, &source
, ":" WHITESPACE
, EXTRACT_QUOTES
|EXTRACT_DONT_COALESCE_SEPARATORS
);
4034 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse %s, ignoring: %s", lvalue
, rvalue
);
4038 r
= unit_full_printf(u
, source
, &sresolved
);
4040 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
4041 "Failed to resolved unit specifiers in \"%s\", ignoring: %m", source
);
4047 ignore_enoent
= true;
4051 r
= path_simplify_and_warn(s
, PATH_CHECK_ABSOLUTE
, unit
, filename
, line
, lvalue
);
4055 /* Optionally, the destination is specified. */
4056 if (p
&& p
[-1] == ':') {
4057 r
= extract_first_word(&p
, &destination
, ":" WHITESPACE
, EXTRACT_QUOTES
|EXTRACT_DONT_COALESCE_SEPARATORS
);
4061 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse %s, ignoring: %s", lvalue
, rvalue
);
4065 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Missing argument after ':', ignoring: %s", s
);
4069 r
= unit_full_printf(u
, destination
, &dresolved
);
4071 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
4072 "Failed to resolved specifiers in \"%s\", ignoring: %m", destination
);
4076 r
= path_simplify_and_warn(dresolved
, PATH_CHECK_ABSOLUTE
, unit
, filename
, line
, lvalue
);
4082 /* Optionally, there's also a short option string specified */
4083 if (p
&& p
[-1] == ':') {
4084 _cleanup_free_
char *options
= NULL
;
4086 r
= extract_first_word(&p
, &options
, NULL
, EXTRACT_QUOTES
);
4090 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse %s: %s", lvalue
, rvalue
);
4094 if (isempty(options
) || streq(options
, "rbind"))
4096 else if (streq(options
, "norbind"))
4099 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid option string, ignoring setting: %s", options
);
4106 r
= bind_mount_add(&c
->bind_mounts
, &c
->n_bind_mounts
,
4110 .read_only
= !!strstr(lvalue
, "ReadOnly"),
4112 .ignore_enoent
= ignore_enoent
,
4121 int config_parse_job_timeout_sec(
4123 const char *filename
,
4125 const char *section
,
4126 unsigned section_line
,
4142 r
= parse_sec_fix_0(rvalue
, &usec
);
4144 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse JobTimeoutSec= parameter, ignoring: %s", rvalue
);
4148 /* If the user explicitly changed JobTimeoutSec= also change JobRunningTimeoutSec=, for compatibility with old
4149 * versions. If JobRunningTimeoutSec= was explicitly set, avoid this however as whatever the user picked should
4152 if (!u
->job_running_timeout_set
)
4153 u
->job_running_timeout
= usec
;
4155 u
->job_timeout
= usec
;
4160 int config_parse_job_running_timeout_sec(
4162 const char *filename
,
4164 const char *section
,
4165 unsigned section_line
,
4181 r
= parse_sec_fix_0(rvalue
, &usec
);
4183 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse JobRunningTimeoutSec= parameter, ignoring: %s", rvalue
);
4187 u
->job_running_timeout
= usec
;
4188 u
->job_running_timeout_set
= true;
4193 int config_parse_emergency_action(
4195 const char *filename
,
4197 const char *section
,
4198 unsigned section_line
,
4206 EmergencyAction
*x
= data
;
4215 m
= ((Unit
*) userdata
)->manager
;
4219 r
= parse_emergency_action(rvalue
, MANAGER_IS_SYSTEM(m
), x
);
4221 if (r
== -EOPNOTSUPP
&& MANAGER_IS_USER(m
)) {
4222 /* Compat mode: remove for systemd 241. */
4224 log_syntax(unit
, LOG_INFO
, filename
, line
, r
,
4225 "%s= in user mode specified as \"%s\", using \"exit-force\" instead.",
4227 *x
= EMERGENCY_ACTION_EXIT_FORCE
;
4231 if (r
== -EOPNOTSUPP
)
4232 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
4233 "%s= specified as %s mode action, ignoring: %s",
4234 lvalue
, MANAGER_IS_SYSTEM(m
) ? "user" : "system", rvalue
);
4236 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
4237 "Failed to parse %s=, ignoring: %s", lvalue
, rvalue
);
4244 int config_parse_pid_file(
4246 const char *filename
,
4248 const char *section
,
4249 unsigned section_line
,
4256 _cleanup_free_
char *k
= NULL
, *n
= NULL
;
4267 if (isempty(rvalue
)) {
4268 /* An empty assignment removes already set value. */
4273 r
= unit_full_printf(u
, rvalue
, &k
);
4275 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue
);
4279 /* If this is a relative path make it absolute by prefixing the /run */
4280 n
= path_make_absolute(k
, u
->manager
->prefix
[EXEC_DIRECTORY_RUNTIME
]);
4284 /* Check that the result is a sensible path */
4285 r
= path_simplify_and_warn(n
, PATH_CHECK_ABSOLUTE
, unit
, filename
, line
, lvalue
);
4289 e
= path_startswith(n
, "/var/run/");
4293 z
= strjoin("/run/", e
);
4297 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
);
4299 free_and_replace(*s
, z
);
4301 free_and_replace(*s
, n
);
4306 int config_parse_exit_status(
4308 const char *filename
,
4310 const char *section
,
4311 unsigned section_line
,
4318 int *exit_status
= data
, r
;
4324 assert(exit_status
);
4326 if (isempty(rvalue
)) {
4331 r
= safe_atou8(rvalue
, &u
);
4333 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse exit status '%s', ignoring: %m", rvalue
);
4341 int config_parse_disable_controllers(
4343 const char *filename
,
4345 const char *section
,
4346 unsigned section_line
,
4354 CGroupContext
*c
= data
;
4355 CGroupMask disabled_mask
;
4357 /* 1. If empty, make all controllers eligible for use again.
4358 * 2. If non-empty, merge all listed controllers, space separated. */
4360 if (isempty(rvalue
)) {
4361 c
->disable_controllers
= 0;
4365 r
= cg_mask_from_string(rvalue
, &disabled_mask
);
4366 if (r
< 0 || disabled_mask
<= 0) {
4367 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid cgroup string: %s, ignoring", rvalue
);
4371 c
->disable_controllers
|= disabled_mask
;
4376 #define FOLLOW_MAX 8
4378 static int open_follow(char **filename
, FILE **_f
, Set
*names
, char **_final
) {
4389 /* This will update the filename pointer if the loaded file is
4390 * reached by a symlink. The old string will be freed. */
4393 char *target
, *name
;
4395 if (c
++ >= FOLLOW_MAX
)
4398 path_simplify(*filename
, false);
4400 /* Add the file name we are currently looking at to
4401 * the names of this unit, but only if it is a valid
4403 name
= basename(*filename
);
4404 if (unit_name_is_valid(name
, UNIT_NAME_ANY
)) {
4406 id
= set_get(names
, name
);
4412 r
= set_consume(names
, id
);
4418 /* Try to open the file name, but don't if its a symlink */
4419 fd
= open(*filename
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
4426 /* Hmm, so this is a symlink. Let's read the name, and follow it manually */
4427 r
= readlink_and_make_absolute(*filename
, &target
);
4431 free_and_replace(*filename
, target
);
4434 f
= fdopen(fd
, "r");
4446 static int merge_by_names(Unit
**u
, Set
*names
, const char *id
) {
4454 /* Let's try to add in all symlink names we found */
4455 while ((k
= set_steal_first(names
))) {
4457 /* First try to merge in the other name into our
4459 r
= unit_merge_by_name(*u
, k
);
4463 /* Hmm, we couldn't merge the other unit into
4464 * ours? Then let's try it the other way
4467 /* If the symlink name we are looking at is unit template, then
4468 we must search for instance of this template */
4469 if (unit_name_is_valid(k
, UNIT_NAME_TEMPLATE
) && (*u
)->instance
) {
4470 _cleanup_free_
char *instance
= NULL
;
4472 r
= unit_name_replace_instance(k
, (*u
)->instance
, &instance
);
4476 other
= manager_get_unit((*u
)->manager
, instance
);
4478 other
= manager_get_unit((*u
)->manager
, k
);
4483 r
= unit_merge(other
, *u
);
4486 return merge_by_names(u
, names
, NULL
);
4494 unit_choose_id(*u
, id
);
4502 static int load_from_path(Unit
*u
, const char *path
) {
4503 _cleanup_set_free_free_ Set
*symlink_names
= NULL
;
4504 _cleanup_fclose_
FILE *f
= NULL
;
4505 _cleanup_free_
char *filename
= NULL
;
4514 symlink_names
= set_new(&string_hash_ops
);
4518 if (path_is_absolute(path
)) {
4520 filename
= strdup(path
);
4524 r
= open_follow(&filename
, &f
, symlink_names
, &id
);
4526 filename
= mfree(filename
);
4534 STRV_FOREACH(p
, u
->manager
->lookup_paths
.search_path
) {
4536 /* Instead of opening the path right away, we manually
4537 * follow all symlinks and add their name to our unit
4538 * name set while doing so */
4539 filename
= path_make_absolute(path
, *p
);
4543 if (u
->manager
->unit_path_cache
&&
4544 !set_get(u
->manager
->unit_path_cache
, filename
))
4547 r
= open_follow(&filename
, &f
, symlink_names
, &id
);
4551 /* ENOENT means that the file is missing or is a dangling symlink.
4552 * ENOTDIR means that one of paths we expect to be is a directory
4553 * is not a directory, we should just ignore that.
4554 * EACCES means that the directory or file permissions are wrong.
4557 log_debug_errno(r
, "Cannot access \"%s\": %m", filename
);
4558 else if (!IN_SET(r
, -ENOENT
, -ENOTDIR
))
4561 filename
= mfree(filename
);
4562 /* Empty the symlink names for the next run */
4563 set_clear_free(symlink_names
);
4568 /* Hmm, no suitable file found? */
4571 if (!unit_type_may_alias(u
->type
) && set_size(symlink_names
) > 1) {
4572 log_unit_warning(u
, "Unit type of %s does not support alias names, refusing loading via symlink.", u
->id
);
4577 r
= merge_by_names(&merged
, symlink_names
, id
);
4582 u
->load_state
= UNIT_MERGED
;
4586 if (fstat(fileno(f
), &st
) < 0)
4589 if (null_or_empty(&st
)) {
4590 u
->load_state
= UNIT_MASKED
;
4591 u
->fragment_mtime
= 0;
4593 u
->load_state
= UNIT_LOADED
;
4594 u
->fragment_mtime
= timespec_load(&st
.st_mtim
);
4596 /* Now, parse the file contents */
4597 r
= config_parse(u
->id
, filename
, f
,
4598 UNIT_VTABLE(u
)->sections
,
4599 config_item_perf_lookup
, load_fragment_gperf_lookup
,
4600 CONFIG_PARSE_ALLOW_INCLUDE
, u
);
4605 free_and_replace(u
->fragment_path
, filename
);
4607 if (u
->source_path
) {
4608 if (stat(u
->source_path
, &st
) >= 0)
4609 u
->source_mtime
= timespec_load(&st
.st_mtim
);
4611 u
->source_mtime
= 0;
4617 int unit_load_fragment(Unit
*u
) {
4623 assert(u
->load_state
== UNIT_STUB
);
4627 u
->load_state
= UNIT_LOADED
;
4631 /* First, try to find the unit under its id. We always look
4632 * for unit files in the default directories, to make it easy
4633 * to override things by placing things in /etc/systemd/system */
4634 r
= load_from_path(u
, u
->id
);
4638 /* Try to find an alias we can load this with */
4639 if (u
->load_state
== UNIT_STUB
) {
4640 SET_FOREACH(t
, u
->names
, i
) {
4645 r
= load_from_path(u
, t
);
4649 if (u
->load_state
!= UNIT_STUB
)
4654 /* And now, try looking for it under the suggested (originally linked) path */
4655 if (u
->load_state
== UNIT_STUB
&& u
->fragment_path
) {
4657 r
= load_from_path(u
, u
->fragment_path
);
4661 if (u
->load_state
== UNIT_STUB
)
4662 /* Hmm, this didn't work? Then let's get rid
4663 * of the fragment path stored for us, so that
4664 * we don't point to an invalid location. */
4665 u
->fragment_path
= mfree(u
->fragment_path
);
4668 /* Look for a template */
4669 if (u
->load_state
== UNIT_STUB
&& u
->instance
) {
4670 _cleanup_free_
char *k
= NULL
;
4672 r
= unit_name_template(u
->id
, &k
);
4676 r
= load_from_path(u
, k
);
4679 log_unit_notice(u
, "Unit configuration has fatal error, unit will not be started.");
4683 if (u
->load_state
== UNIT_STUB
) {
4684 SET_FOREACH(t
, u
->names
, i
) {
4685 _cleanup_free_
char *z
= NULL
;
4690 r
= unit_name_template(t
, &z
);
4694 r
= load_from_path(u
, z
);
4698 if (u
->load_state
!= UNIT_STUB
)
4707 void unit_dump_config_items(FILE *f
) {
4708 static const struct {
4709 const ConfigParserCallback callback
;
4712 { config_parse_warn_compat
, "NOTSUPPORTED" },
4713 { config_parse_int
, "INTEGER" },
4714 { config_parse_unsigned
, "UNSIGNED" },
4715 { config_parse_iec_size
, "SIZE" },
4716 { config_parse_iec_uint64
, "SIZE" },
4717 { config_parse_si_size
, "SIZE" },
4718 { config_parse_bool
, "BOOLEAN" },
4719 { config_parse_string
, "STRING" },
4720 { config_parse_path
, "PATH" },
4721 { config_parse_unit_path_printf
, "PATH" },
4722 { config_parse_strv
, "STRING [...]" },
4723 { config_parse_exec_nice
, "NICE" },
4724 { config_parse_exec_oom_score_adjust
, "OOMSCOREADJUST" },
4725 { config_parse_exec_io_class
, "IOCLASS" },
4726 { config_parse_exec_io_priority
, "IOPRIORITY" },
4727 { config_parse_exec_cpu_sched_policy
, "CPUSCHEDPOLICY" },
4728 { config_parse_exec_cpu_sched_prio
, "CPUSCHEDPRIO" },
4729 { config_parse_exec_cpu_affinity
, "CPUAFFINITY" },
4730 { config_parse_mode
, "MODE" },
4731 { config_parse_unit_env_file
, "FILE" },
4732 { config_parse_exec_output
, "OUTPUT" },
4733 { config_parse_exec_input
, "INPUT" },
4734 { config_parse_log_facility
, "FACILITY" },
4735 { config_parse_log_level
, "LEVEL" },
4736 { config_parse_exec_secure_bits
, "SECUREBITS" },
4737 { config_parse_capability_set
, "BOUNDINGSET" },
4738 { config_parse_rlimit
, "LIMIT" },
4739 { config_parse_unit_deps
, "UNIT [...]" },
4740 { config_parse_exec
, "PATH [ARGUMENT [...]]" },
4741 { config_parse_service_type
, "SERVICETYPE" },
4742 { config_parse_service_restart
, "SERVICERESTART" },
4743 { config_parse_kill_mode
, "KILLMODE" },
4744 { config_parse_signal
, "SIGNAL" },
4745 { config_parse_socket_listen
, "SOCKET [...]" },
4746 { config_parse_socket_bind
, "SOCKETBIND" },
4747 { config_parse_socket_bindtodevice
, "NETWORKINTERFACE" },
4748 { config_parse_sec
, "SECONDS" },
4749 { config_parse_nsec
, "NANOSECONDS" },
4750 { config_parse_namespace_path_strv
, "PATH [...]" },
4751 { config_parse_bind_paths
, "PATH[:PATH[:OPTIONS]] [...]" },
4752 { config_parse_unit_requires_mounts_for
, "PATH [...]" },
4753 { config_parse_exec_mount_flags
, "MOUNTFLAG [...]" },
4754 { config_parse_unit_string_printf
, "STRING" },
4755 { config_parse_trigger_unit
, "UNIT" },
4756 { config_parse_timer
, "TIMER" },
4757 { config_parse_path_spec
, "PATH" },
4758 { config_parse_notify_access
, "ACCESS" },
4759 { config_parse_ip_tos
, "TOS" },
4760 { config_parse_unit_condition_path
, "CONDITION" },
4761 { config_parse_unit_condition_string
, "CONDITION" },
4762 { config_parse_unit_condition_null
, "CONDITION" },
4763 { config_parse_unit_slice
, "SLICE" },
4764 { config_parse_documentation
, "URL" },
4765 { config_parse_service_timeout
, "SECONDS" },
4766 { config_parse_emergency_action
, "ACTION" },
4767 { config_parse_set_status
, "STATUS" },
4768 { config_parse_service_sockets
, "SOCKETS" },
4769 { config_parse_environ
, "ENVIRON" },
4771 { config_parse_syscall_filter
, "SYSCALLS" },
4772 { config_parse_syscall_archs
, "ARCHS" },
4773 { config_parse_syscall_errno
, "ERRNO" },
4774 { config_parse_address_families
, "FAMILIES" },
4775 { config_parse_restrict_namespaces
, "NAMESPACES" },
4777 { config_parse_cpu_shares
, "SHARES" },
4778 { config_parse_cg_weight
, "WEIGHT" },
4779 { config_parse_memory_limit
, "LIMIT" },
4780 { config_parse_device_allow
, "DEVICE" },
4781 { config_parse_device_policy
, "POLICY" },
4782 { config_parse_io_limit
, "LIMIT" },
4783 { config_parse_io_device_weight
, "DEVICEWEIGHT" },
4784 { config_parse_io_device_latency
, "DEVICELATENCY" },
4785 { config_parse_blockio_bandwidth
, "BANDWIDTH" },
4786 { config_parse_blockio_weight
, "WEIGHT" },
4787 { config_parse_blockio_device_weight
, "DEVICEWEIGHT" },
4788 { config_parse_long
, "LONG" },
4789 { config_parse_socket_service
, "SERVICE" },
4791 { config_parse_exec_selinux_context
, "LABEL" },
4793 { config_parse_job_mode
, "MODE" },
4794 { config_parse_job_mode_isolate
, "BOOLEAN" },
4795 { config_parse_personality
, "PERSONALITY" },
4798 const char *prev
= NULL
;
4803 NULSTR_FOREACH(i
, load_fragment_gperf_nulstr
) {
4804 const char *rvalue
= "OTHER", *lvalue
;
4805 const ConfigPerfItem
*p
;
4810 assert_se(p
= load_fragment_gperf_lookup(i
, strlen(i
)));
4812 /* Hide legacy settings */
4813 if (p
->parse
== config_parse_warn_compat
&&
4814 p
->ltype
== DISABLED_LEGACY
)
4817 for (j
= 0; j
< ELEMENTSOF(table
); j
++)
4818 if (p
->parse
== table
[j
].callback
) {
4819 rvalue
= table
[j
].rvalue
;
4823 dot
= strchr(i
, '.');
4824 lvalue
= dot
? dot
+ 1 : i
;
4828 if (!prev
|| !strneq(prev
, i
, prefix_len
+1)) {
4832 fprintf(f
, "[%.*s]\n", (int) prefix_len
, i
);
4835 fprintf(f
, "%s=%s\n", lvalue
, rvalue
);