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 if (calendar_spec_from_string(k
, &c
) < 0) {
1540 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse calendar specification, ignoring: %s", k
);
1544 if (parse_sec(k
, &usec
) < 0) {
1545 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse timer value, ignoring: %s", k
);
1549 v
= new(TimerValue
, 1);
1556 .calendar_spec
= TAKE_PTR(c
),
1559 LIST_PREPEND(value
, t
->values
, v
);
1564 int config_parse_trigger_unit(
1566 const char *filename
,
1568 const char *section
,
1569 unsigned section_line
,
1576 _cleanup_free_
char *p
= NULL
;
1586 if (!hashmap_isempty(u
->dependencies
[UNIT_TRIGGERS
])) {
1587 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Multiple units to trigger specified, ignoring: %s", rvalue
);
1591 r
= unit_name_printf(u
, rvalue
, &p
);
1593 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue
);
1597 type
= unit_name_to_type(p
);
1599 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Unit type not valid, ignoring: %s", rvalue
);
1602 if (unit_has_name(u
, p
)) {
1603 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Units cannot trigger themselves, ignoring: %s", rvalue
);
1607 r
= unit_add_two_dependencies_by_name(u
, UNIT_BEFORE
, UNIT_TRIGGERS
, p
, true, UNIT_DEPENDENCY_FILE
);
1609 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to add trigger on %s, ignoring: %m", p
);
1616 int config_parse_path_spec(const char *unit
,
1617 const char *filename
,
1619 const char *section
,
1620 unsigned section_line
,
1630 _cleanup_free_
char *k
= NULL
;
1638 if (isempty(rvalue
)) {
1639 /* Empty assignment clears list */
1644 b
= path_type_from_string(lvalue
);
1646 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse path type, ignoring: %s", lvalue
);
1650 r
= unit_full_printf(UNIT(p
), rvalue
, &k
);
1652 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue
);
1656 r
= path_simplify_and_warn(k
, PATH_CHECK_ABSOLUTE
, unit
, filename
, line
, lvalue
);
1660 s
= new0(PathSpec
, 1);
1665 s
->path
= TAKE_PTR(k
);
1669 LIST_PREPEND(spec
, p
->specs
, s
);
1674 int config_parse_socket_service(
1676 const char *filename
,
1678 const char *section
,
1679 unsigned section_line
,
1686 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1687 _cleanup_free_
char *p
= NULL
;
1697 r
= unit_name_printf(UNIT(s
), rvalue
, &p
);
1699 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in %s: %m", rvalue
);
1703 if (!endswith(p
, ".service")) {
1704 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Unit must be of type service: %s", rvalue
);
1708 r
= manager_load_unit(UNIT(s
)->manager
, p
, NULL
, &error
, &x
);
1710 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to load unit %s: %s", rvalue
, bus_error_message(&error
, r
));
1714 unit_ref_set(&s
->service
, UNIT(s
), x
);
1719 int config_parse_fdname(
1721 const char *filename
,
1723 const char *section
,
1724 unsigned section_line
,
1731 _cleanup_free_
char *p
= NULL
;
1740 if (isempty(rvalue
)) {
1741 s
->fdname
= mfree(s
->fdname
);
1745 r
= unit_full_printf(UNIT(s
), rvalue
, &p
);
1747 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue
);
1751 if (!fdname_is_valid(p
)) {
1752 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid file descriptor name, ignoring: %s", p
);
1756 return free_and_replace(s
->fdname
, p
);
1759 int config_parse_service_sockets(
1761 const char *filename
,
1763 const char *section
,
1764 unsigned section_line
,
1782 _cleanup_free_
char *word
= NULL
, *k
= NULL
;
1784 r
= extract_first_word(&p
, &word
, NULL
, 0);
1790 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Trailing garbage in sockets, ignoring: %s", rvalue
);
1794 r
= unit_name_printf(UNIT(s
), word
, &k
);
1796 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in '%s', ignoring: %m", word
);
1800 if (!endswith(k
, ".socket")) {
1801 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Unit must be of type socket, ignoring: %s", k
);
1805 r
= unit_add_two_dependencies_by_name(UNIT(s
), UNIT_WANTS
, UNIT_AFTER
, k
, true, UNIT_DEPENDENCY_FILE
);
1807 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to add dependency on %s, ignoring: %m", k
);
1809 r
= unit_add_dependency_by_name(UNIT(s
), UNIT_TRIGGERED_BY
, k
, true, UNIT_DEPENDENCY_FILE
);
1811 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to add dependency on %s, ignoring: %m", k
);
1817 int config_parse_bus_name(
1819 const char *filename
,
1821 const char *section
,
1822 unsigned section_line
,
1829 _cleanup_free_
char *k
= NULL
;
1838 r
= unit_full_printf(u
, rvalue
, &k
);
1840 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue
);
1844 if (!service_name_is_valid(k
)) {
1845 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid bus name, ignoring: %s", k
);
1849 return config_parse_string(unit
, filename
, line
, section
, section_line
, lvalue
, ltype
, k
, data
, userdata
);
1852 int config_parse_service_timeout(
1854 const char *filename
,
1856 const char *section
,
1857 unsigned section_line
,
1864 Service
*s
= userdata
;
1873 /* This is called for two cases: TimeoutSec= and TimeoutStartSec=. */
1875 /* Traditionally, these options accepted 0 to disable the timeouts. However, a timeout of 0 suggests it happens
1876 * immediately, hence fix this to become USEC_INFINITY instead. This is in-line with how we internally handle
1877 * all other timeouts. */
1878 r
= parse_sec_fix_0(rvalue
, &usec
);
1880 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse %s= parameter, ignoring: %s", lvalue
, rvalue
);
1884 s
->start_timeout_defined
= true;
1885 s
->timeout_start_usec
= usec
;
1887 if (streq(lvalue
, "TimeoutSec"))
1888 s
->timeout_stop_usec
= usec
;
1893 int config_parse_sec_fix_0(
1895 const char *filename
,
1897 const char *section
,
1898 unsigned section_line
,
1905 usec_t
*usec
= data
;
1913 /* This is pretty much like config_parse_sec(), except that this treats a time of 0 as infinity, for
1914 * compatibility with older versions of systemd where 0 instead of infinity was used as indicator to turn off a
1917 r
= parse_sec_fix_0(rvalue
, usec
);
1919 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse %s= parameter, ignoring: %s", lvalue
, rvalue
);
1926 int config_parse_user_group(
1928 const char *filename
,
1930 const char *section
,
1931 unsigned section_line
,
1938 _cleanup_free_
char *k
= NULL
;
1948 if (isempty(rvalue
)) {
1949 *user
= mfree(*user
);
1953 r
= unit_full_printf(u
, rvalue
, &k
);
1955 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in %s: %m", rvalue
);
1959 if (!valid_user_group_name_or_id(k
)) {
1960 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid user/group name or numeric ID: %s", k
);
1964 return free_and_replace(*user
, k
);
1967 int config_parse_user_group_strv(
1969 const char *filename
,
1971 const char *section
,
1972 unsigned section_line
,
1979 char ***users
= data
;
1981 const char *p
= rvalue
;
1989 if (isempty(rvalue
)) {
1990 *users
= strv_free(*users
);
1995 _cleanup_free_
char *word
= NULL
, *k
= NULL
;
1997 r
= extract_first_word(&p
, &word
, NULL
, 0);
2003 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid syntax: %s", rvalue
);
2007 r
= unit_full_printf(u
, word
, &k
);
2009 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in %s: %m", word
);
2013 if (!valid_user_group_name_or_id(k
)) {
2014 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid user/group name or numeric ID: %s", k
);
2018 r
= strv_push(users
, k
);
2028 int config_parse_working_directory(
2030 const char *filename
,
2032 const char *section
,
2033 unsigned section_line
,
2040 ExecContext
*c
= data
;
2051 if (isempty(rvalue
)) {
2052 c
->working_directory_home
= false;
2053 c
->working_directory
= mfree(c
->working_directory
);
2057 if (rvalue
[0] == '-') {
2063 if (streq(rvalue
, "~")) {
2064 c
->working_directory_home
= true;
2065 c
->working_directory
= mfree(c
->working_directory
);
2067 _cleanup_free_
char *k
= NULL
;
2069 r
= unit_full_printf(u
, rvalue
, &k
);
2071 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
2072 "Failed to resolve unit specifiers in working directory path '%s'%s: %m",
2073 rvalue
, missing_ok
? ", ignoring" : "");
2074 return missing_ok
? 0 : -ENOEXEC
;
2077 r
= path_simplify_and_warn(k
, PATH_CHECK_ABSOLUTE
| (missing_ok
? 0 : PATH_CHECK_FATAL
), unit
, filename
, line
, lvalue
);
2079 return missing_ok
? 0 : -ENOEXEC
;
2081 c
->working_directory_home
= false;
2082 free_and_replace(c
->working_directory
, k
);
2085 c
->working_directory_missing_ok
= missing_ok
;
2089 int config_parse_unit_env_file(const char *unit
,
2090 const char *filename
,
2092 const char *section
,
2093 unsigned section_line
,
2102 _cleanup_free_
char *n
= NULL
;
2110 if (isempty(rvalue
)) {
2111 /* Empty assignment frees the list */
2112 *env
= strv_free(*env
);
2116 r
= unit_full_printf(u
, rvalue
, &n
);
2118 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue
);
2122 r
= path_simplify_and_warn(n
[0] == '-' ? n
+ 1 : n
, PATH_CHECK_ABSOLUTE
, unit
, filename
, line
, lvalue
);
2126 r
= strv_push(env
, n
);
2135 int config_parse_environ(
2137 const char *filename
,
2139 const char *section
,
2140 unsigned section_line
,
2157 if (isempty(rvalue
)) {
2158 /* Empty assignment resets the list */
2159 *env
= strv_free(*env
);
2163 for (p
= rvalue
;; ) {
2164 _cleanup_free_
char *word
= NULL
, *k
= NULL
;
2166 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_CUNESCAPE
|EXTRACT_QUOTES
);
2172 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
2173 "Invalid syntax, ignoring: %s", rvalue
);
2178 r
= unit_full_printf(u
, word
, &k
);
2180 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
2181 "Failed to resolve unit specifiers in %s, ignoring: %m", word
);
2187 if (!env_assignment_is_valid(k
)) {
2188 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
2189 "Invalid environment assignment, ignoring: %s", k
);
2193 r
= strv_env_replace(env
, k
);
2201 int config_parse_pass_environ(
2203 const char *filename
,
2205 const char *section
,
2206 unsigned section_line
,
2213 _cleanup_strv_free_
char **n
= NULL
;
2214 size_t nlen
= 0, nbufsize
= 0;
2215 char*** passenv
= data
;
2216 const char *p
= rvalue
;
2225 if (isempty(rvalue
)) {
2226 /* Empty assignment resets the list */
2227 *passenv
= strv_free(*passenv
);
2232 _cleanup_free_
char *word
= NULL
, *k
= NULL
;
2234 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
);
2240 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
2241 "Trailing garbage in %s, ignoring: %s", lvalue
, rvalue
);
2246 r
= unit_full_printf(u
, word
, &k
);
2248 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
2249 "Failed to resolve specifiers in %s, ignoring: %m", word
);
2255 if (!env_name_is_valid(k
)) {
2256 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
2257 "Invalid environment name for %s, ignoring: %s", lvalue
, k
);
2261 if (!GREEDY_REALLOC(n
, nbufsize
, nlen
+ 2))
2264 n
[nlen
++] = TAKE_PTR(k
);
2269 r
= strv_extend_strv(passenv
, n
, true);
2277 int config_parse_unset_environ(
2279 const char *filename
,
2281 const char *section
,
2282 unsigned section_line
,
2289 _cleanup_strv_free_
char **n
= NULL
;
2290 size_t nlen
= 0, nbufsize
= 0;
2291 char*** unsetenv
= data
;
2292 const char *p
= rvalue
;
2301 if (isempty(rvalue
)) {
2302 /* Empty assignment resets the list */
2303 *unsetenv
= strv_free(*unsetenv
);
2308 _cleanup_free_
char *word
= NULL
, *k
= NULL
;
2310 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_CUNESCAPE
|EXTRACT_QUOTES
);
2316 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
2317 "Trailing garbage in %s, ignoring: %s", lvalue
, rvalue
);
2322 r
= unit_full_printf(u
, word
, &k
);
2324 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
2325 "Failed to resolve unit specifiers in %s, ignoring: %m", word
);
2331 if (!env_assignment_is_valid(k
) && !env_name_is_valid(k
)) {
2332 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
2333 "Invalid environment name or assignment %s, ignoring: %s", lvalue
, k
);
2337 if (!GREEDY_REALLOC(n
, nbufsize
, nlen
+ 2))
2340 n
[nlen
++] = TAKE_PTR(k
);
2345 r
= strv_extend_strv(unsetenv
, n
, true);
2353 int config_parse_log_extra_fields(
2355 const char *filename
,
2357 const char *section
,
2358 unsigned section_line
,
2365 ExecContext
*c
= data
;
2367 const char *p
= rvalue
;
2375 if (isempty(rvalue
)) {
2376 exec_context_free_log_extra_fields(c
);
2381 _cleanup_free_
char *word
= NULL
, *k
= NULL
;
2385 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_CUNESCAPE
|EXTRACT_QUOTES
);
2391 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Invalid syntax, ignoring: %s", rvalue
);
2395 r
= unit_full_printf(u
, word
, &k
);
2397 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in %s, ignoring: %m", word
);
2401 eq
= strchr(k
, '=');
2403 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Log field lacks '=' character, ignoring: %s", k
);
2407 if (!journal_field_valid(k
, eq
-k
, false)) {
2408 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Log field name is invalid, ignoring: %s", k
);
2412 t
= reallocarray(c
->log_extra_fields
, c
->n_log_extra_fields
+1, sizeof(struct iovec
));
2416 c
->log_extra_fields
= t
;
2417 c
->log_extra_fields
[c
->n_log_extra_fields
++] = IOVEC_MAKE_STRING(k
);
2423 int config_parse_unit_condition_path(
2425 const char *filename
,
2427 const char *section
,
2428 unsigned section_line
,
2435 _cleanup_free_
char *p
= NULL
;
2436 Condition
**list
= data
, *c
;
2437 ConditionType t
= ltype
;
2438 bool trigger
, negate
;
2447 if (isempty(rvalue
)) {
2448 /* Empty assignment resets the list */
2449 *list
= condition_free_list(*list
);
2453 trigger
= rvalue
[0] == '|';
2457 negate
= rvalue
[0] == '!';
2461 r
= unit_full_printf(u
, rvalue
, &p
);
2463 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue
);
2467 r
= path_simplify_and_warn(p
, PATH_CHECK_ABSOLUTE
, unit
, filename
, line
, lvalue
);
2471 c
= condition_new(t
, p
, trigger
, negate
);
2475 LIST_PREPEND(conditions
, *list
, c
);
2479 int config_parse_unit_condition_string(
2481 const char *filename
,
2483 const char *section
,
2484 unsigned section_line
,
2491 _cleanup_free_
char *s
= NULL
;
2492 Condition
**list
= data
, *c
;
2493 ConditionType t
= ltype
;
2494 bool trigger
, negate
;
2503 if (isempty(rvalue
)) {
2504 /* Empty assignment resets the list */
2505 *list
= condition_free_list(*list
);
2509 trigger
= rvalue
[0] == '|';
2513 negate
= rvalue
[0] == '!';
2517 r
= unit_full_printf(u
, rvalue
, &s
);
2519 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue
);
2523 c
= condition_new(t
, s
, trigger
, negate
);
2527 LIST_PREPEND(conditions
, *list
, c
);
2531 int config_parse_unit_condition_null(
2533 const char *filename
,
2535 const char *section
,
2536 unsigned section_line
,
2543 Condition
**list
= data
, *c
;
2544 bool trigger
, negate
;
2552 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "%s= is deprecated, please do not use.", lvalue
);
2554 if (isempty(rvalue
)) {
2555 /* Empty assignment resets the list */
2556 *list
= condition_free_list(*list
);
2560 trigger
= rvalue
[0] == '|';
2564 negate
= rvalue
[0] == '!';
2568 b
= parse_boolean(rvalue
);
2570 log_syntax(unit
, LOG_ERR
, filename
, line
, b
, "Failed to parse boolean value in condition, ignoring: %s", rvalue
);
2577 c
= condition_new(CONDITION_NULL
, NULL
, trigger
, negate
);
2581 LIST_PREPEND(conditions
, *list
, c
);
2585 int config_parse_unit_requires_mounts_for(
2587 const char *filename
,
2589 const char *section
,
2590 unsigned section_line
,
2597 const char *p
= rvalue
;
2607 _cleanup_free_
char *word
= NULL
, *resolved
= NULL
;
2609 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
);
2615 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
2616 "Invalid syntax, ignoring: %s", rvalue
);
2620 r
= unit_full_printf(u
, word
, &resolved
);
2622 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in '%s', ignoring: %m", word
);
2626 r
= path_simplify_and_warn(resolved
, PATH_CHECK_ABSOLUTE
, unit
, filename
, line
, lvalue
);
2630 r
= unit_require_mounts_for(u
, resolved
, UNIT_DEPENDENCY_FILE
);
2632 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to add required mount '%s', ignoring: %m", resolved
);
2638 int config_parse_documentation(const char *unit
,
2639 const char *filename
,
2641 const char *section
,
2642 unsigned section_line
,
2658 if (isempty(rvalue
)) {
2659 /* Empty assignment resets the list */
2660 u
->documentation
= strv_free(u
->documentation
);
2664 r
= config_parse_unit_strv_printf(unit
, filename
, line
, section
, section_line
, lvalue
, ltype
,
2665 rvalue
, data
, userdata
);
2669 for (a
= b
= u
->documentation
; a
&& *a
; a
++) {
2671 if (documentation_url_is_valid(*a
))
2674 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid URL, ignoring: %s", *a
);
2685 int config_parse_syscall_filter(
2687 const char *filename
,
2689 const char *section
,
2690 unsigned section_line
,
2697 ExecContext
*c
= data
;
2699 bool invert
= false;
2708 if (isempty(rvalue
)) {
2709 /* Empty assignment resets the list */
2710 c
->syscall_filter
= hashmap_free(c
->syscall_filter
);
2711 c
->syscall_whitelist
= false;
2715 if (rvalue
[0] == '~') {
2720 if (!c
->syscall_filter
) {
2721 c
->syscall_filter
= hashmap_new(NULL
);
2722 if (!c
->syscall_filter
)
2726 /* Allow everything but the ones listed */
2727 c
->syscall_whitelist
= false;
2729 /* Allow nothing but the ones listed */
2730 c
->syscall_whitelist
= true;
2732 /* Accept default syscalls if we are on a whitelist */
2733 r
= seccomp_parse_syscall_filter(
2734 "@default", -1, c
->syscall_filter
,
2735 SECCOMP_PARSE_PERMISSIVE
|SECCOMP_PARSE_WHITELIST
);
2743 _cleanup_free_
char *word
= NULL
, *name
= NULL
;
2746 r
= extract_first_word(&p
, &word
, NULL
, 0);
2752 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Invalid syntax, ignoring: %s", rvalue
);
2756 r
= parse_syscall_and_errno(word
, &name
, &num
);
2758 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Failed to parse syscall:errno, ignoring: %s", word
);
2762 r
= seccomp_parse_syscall_filter_full(
2763 name
, num
, c
->syscall_filter
,
2764 SECCOMP_PARSE_LOG
|SECCOMP_PARSE_PERMISSIVE
|
2765 (invert
? SECCOMP_PARSE_INVERT
: 0)|
2766 (c
->syscall_whitelist
? SECCOMP_PARSE_WHITELIST
: 0),
2767 unit
, filename
, line
);
2773 int config_parse_syscall_archs(
2775 const char *filename
,
2777 const char *section
,
2778 unsigned section_line
,
2785 const char *p
= rvalue
;
2789 if (isempty(rvalue
)) {
2790 *archs
= set_free(*archs
);
2794 r
= set_ensure_allocated(archs
, NULL
);
2799 _cleanup_free_
char *word
= NULL
;
2802 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
);
2808 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
2809 "Invalid syntax, ignoring: %s", rvalue
);
2813 r
= seccomp_arch_from_string(word
, &a
);
2815 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
2816 "Failed to parse system call architecture \"%s\", ignoring: %m", word
);
2820 r
= set_put(*archs
, UINT32_TO_PTR(a
+ 1));
2826 int config_parse_syscall_errno(
2828 const char *filename
,
2830 const char *section
,
2831 unsigned section_line
,
2838 ExecContext
*c
= data
;
2845 if (isempty(rvalue
)) {
2846 /* Empty assignment resets to KILL */
2847 c
->syscall_errno
= 0;
2851 e
= parse_errno(rvalue
);
2853 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse error number, ignoring: %s", rvalue
);
2857 c
->syscall_errno
= e
;
2861 int config_parse_address_families(
2863 const char *filename
,
2865 const char *section
,
2866 unsigned section_line
,
2873 ExecContext
*c
= data
;
2874 bool invert
= false;
2882 if (isempty(rvalue
)) {
2883 /* Empty assignment resets the list */
2884 c
->address_families
= set_free(c
->address_families
);
2885 c
->address_families_whitelist
= false;
2889 if (rvalue
[0] == '~') {
2894 if (!c
->address_families
) {
2895 c
->address_families
= set_new(NULL
);
2896 if (!c
->address_families
)
2899 c
->address_families_whitelist
= !invert
;
2902 for (p
= rvalue
;;) {
2903 _cleanup_free_
char *word
= NULL
;
2906 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
);
2912 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
2913 "Invalid syntax, ignoring: %s", rvalue
);
2917 af
= af_from_name(word
);
2919 log_syntax(unit
, LOG_ERR
, filename
, line
, af
,
2920 "Failed to parse address family, ignoring: %s", word
);
2924 /* If we previously wanted to forbid an address family and now
2925 * we want to allow it, then just remove it from the list.
2927 if (!invert
== c
->address_families_whitelist
) {
2928 r
= set_put(c
->address_families
, INT_TO_PTR(af
));
2932 set_remove(c
->address_families
, INT_TO_PTR(af
));
2936 int config_parse_restrict_namespaces(
2938 const char *filename
,
2940 const char *section
,
2941 unsigned section_line
,
2948 ExecContext
*c
= data
;
2949 unsigned long flags
;
2950 bool invert
= false;
2953 if (isempty(rvalue
)) {
2954 /* Reset to the default. */
2955 c
->restrict_namespaces
= NAMESPACE_FLAGS_INITIAL
;
2959 /* Boolean parameter ignores the previous settings */
2960 r
= parse_boolean(rvalue
);
2962 c
->restrict_namespaces
= 0;
2964 } else if (r
== 0) {
2965 c
->restrict_namespaces
= NAMESPACE_FLAGS_ALL
;
2969 if (rvalue
[0] == '~') {
2974 /* Not a boolean argument, in this case it's a list of namespace types. */
2975 r
= namespace_flags_from_string(rvalue
, &flags
);
2977 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse namespace type string, ignoring: %s", rvalue
);
2981 if (c
->restrict_namespaces
== NAMESPACE_FLAGS_INITIAL
)
2982 /* Initial assignment. Just set the value. */
2983 c
->restrict_namespaces
= invert
? (~flags
) & NAMESPACE_FLAGS_ALL
: flags
;
2985 /* Merge the value with the previous one. */
2986 SET_FLAG(c
->restrict_namespaces
, flags
, !invert
);
2992 int config_parse_unit_slice(
2994 const char *filename
,
2996 const char *section
,
2997 unsigned section_line
,
3004 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
3005 _cleanup_free_
char *k
= NULL
;
3006 Unit
*u
= userdata
, *slice
= NULL
;
3014 r
= unit_name_printf(u
, rvalue
, &k
);
3016 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue
);
3020 r
= manager_load_unit(u
->manager
, k
, NULL
, &error
, &slice
);
3022 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to load slice unit %s, ignoring: %s", k
, bus_error_message(&error
, r
));
3026 r
= unit_set_slice(u
, slice
);
3028 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to assign slice %s to unit %s, ignoring: %m", slice
->id
, u
->id
);
3035 int config_parse_cpu_quota(
3037 const char *filename
,
3039 const char *section
,
3040 unsigned section_line
,
3047 CGroupContext
*c
= data
;
3054 if (isempty(rvalue
)) {
3055 c
->cpu_quota_per_sec_usec
= USEC_INFINITY
;
3059 r
= parse_permille_unbounded(rvalue
);
3061 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid CPU quota '%s', ignoring.", rvalue
);
3065 c
->cpu_quota_per_sec_usec
= ((usec_t
) r
* USEC_PER_SEC
) / 1000U;
3069 int config_parse_memory_limit(
3071 const char *filename
,
3073 const char *section
,
3074 unsigned section_line
,
3081 CGroupContext
*c
= data
;
3082 uint64_t bytes
= CGROUP_LIMIT_MAX
;
3085 if (!isempty(rvalue
) && !streq(rvalue
, "infinity")) {
3087 r
= parse_permille(rvalue
);
3089 r
= parse_size(rvalue
, 1024, &bytes
);
3091 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid memory limit '%s', ignoring: %m", rvalue
);
3095 bytes
= physical_memory_scale(r
, 1000U);
3097 if (bytes
>= UINT64_MAX
||
3098 (bytes
<= 0 && !streq(lvalue
, "MemorySwapMax"))) {
3099 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Memory limit '%s' out of range, ignoring.", rvalue
);
3104 if (streq(lvalue
, "MemoryMin"))
3105 c
->memory_min
= bytes
;
3106 else if (streq(lvalue
, "MemoryLow"))
3107 c
->memory_low
= bytes
;
3108 else if (streq(lvalue
, "MemoryHigh"))
3109 c
->memory_high
= bytes
;
3110 else if (streq(lvalue
, "MemoryMax"))
3111 c
->memory_max
= bytes
;
3112 else if (streq(lvalue
, "MemorySwapMax"))
3113 c
->memory_swap_max
= bytes
;
3114 else if (streq(lvalue
, "MemoryLimit"))
3115 c
->memory_limit
= bytes
;
3122 int config_parse_tasks_max(
3124 const char *filename
,
3126 const char *section
,
3127 unsigned section_line
,
3134 uint64_t *tasks_max
= data
, v
;
3138 if (isempty(rvalue
)) {
3139 *tasks_max
= u
? u
->manager
->default_tasks_max
: UINT64_MAX
;
3143 if (streq(rvalue
, "infinity")) {
3144 *tasks_max
= CGROUP_LIMIT_MAX
;
3148 r
= parse_permille(rvalue
);
3150 r
= safe_atou64(rvalue
, &v
);
3152 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid maximum tasks value '%s', ignoring: %m", rvalue
);
3156 v
= system_tasks_max_scale(r
, 1000U);
3158 if (v
<= 0 || v
>= UINT64_MAX
) {
3159 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Maximum tasks value '%s' out of range, ignoring.", rvalue
);
3167 int config_parse_delegate(
3169 const char *filename
,
3171 const char *section
,
3172 unsigned section_line
,
3179 CGroupContext
*c
= data
;
3183 t
= unit_name_to_type(unit
);
3184 assert(t
!= _UNIT_TYPE_INVALID
);
3186 if (!unit_vtable
[t
]->can_delegate
) {
3187 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Delegate= setting not supported for this unit type, ignoring.");
3191 /* We either accept a boolean value, which may be used to turn on delegation for all controllers, or turn it
3192 * off for all. Or it takes a list of controller names, in which case we add the specified controllers to the
3193 * mask to delegate. */
3195 if (isempty(rvalue
)) {
3196 /* An empty string resets controllers and set Delegate=yes. */
3198 c
->delegate_controllers
= 0;
3202 r
= parse_boolean(rvalue
);
3204 const char *p
= rvalue
;
3205 CGroupMask mask
= 0;
3208 _cleanup_free_
char *word
= NULL
;
3209 CGroupController cc
;
3211 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
);
3217 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid syntax, ignoring: %s", rvalue
);
3221 cc
= cgroup_controller_from_string(word
);
3223 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid controller name '%s', ignoring", word
);
3227 mask
|= CGROUP_CONTROLLER_TO_MASK(cc
);
3231 c
->delegate_controllers
|= mask
;
3235 c
->delegate_controllers
= _CGROUP_MASK_ALL
;
3237 c
->delegate
= false;
3238 c
->delegate_controllers
= 0;
3244 int config_parse_device_allow(
3246 const char *filename
,
3248 const char *section
,
3249 unsigned section_line
,
3256 _cleanup_free_
char *path
= NULL
, *resolved
= NULL
;
3257 CGroupContext
*c
= data
;
3258 const char *p
= rvalue
;
3261 if (isempty(rvalue
)) {
3262 while (c
->device_allow
)
3263 cgroup_context_free_device_allow(c
, c
->device_allow
);
3268 r
= extract_first_word(&p
, &path
, NULL
, EXTRACT_QUOTES
);
3272 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
3273 "Invalid syntax, ignoring: %s", rvalue
);
3277 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
3278 "Failed to extract device path and rights from '%s', ignoring.", rvalue
);
3282 r
= unit_full_printf(userdata
, path
, &resolved
);
3284 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
3285 "Failed to resolve unit specifiers in '%s', ignoring: %m", path
);
3289 if (!STARTSWITH_SET(resolved
, "block-", "char-")) {
3291 r
= path_simplify_and_warn(resolved
, 0, unit
, filename
, line
, lvalue
);
3295 if (!valid_device_node_path(resolved
)) {
3296 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid device node path '%s', ignoring.", resolved
);
3301 if (!isempty(p
) && !in_charset(p
, "rwm")) {
3302 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid device rights '%s', ignoring.", p
);
3306 return cgroup_add_device_allow(c
, resolved
, p
);
3309 int config_parse_io_device_weight(
3311 const char *filename
,
3313 const char *section
,
3314 unsigned section_line
,
3321 _cleanup_free_
char *path
= NULL
, *resolved
= NULL
;
3322 CGroupIODeviceWeight
*w
;
3323 CGroupContext
*c
= data
;
3324 const char *p
= rvalue
;
3332 if (isempty(rvalue
)) {
3333 while (c
->io_device_weights
)
3334 cgroup_context_free_io_device_weight(c
, c
->io_device_weights
);
3339 r
= extract_first_word(&p
, &path
, NULL
, EXTRACT_QUOTES
);
3343 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
3344 "Invalid syntax, ignoring: %s", rvalue
);
3347 if (r
== 0 || isempty(p
)) {
3348 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
3349 "Failed to extract device path and weight from '%s', ignoring.", rvalue
);
3353 r
= unit_full_printf(userdata
, path
, &resolved
);
3355 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
3356 "Failed to resolve unit specifiers in '%s', ignoring: %m", path
);
3360 r
= path_simplify_and_warn(resolved
, 0, unit
, filename
, line
, lvalue
);
3364 r
= cg_weight_parse(p
, &u
);
3366 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "IO weight '%s' invalid, ignoring: %m", p
);
3370 assert(u
!= CGROUP_WEIGHT_INVALID
);
3372 w
= new0(CGroupIODeviceWeight
, 1);
3376 w
->path
= TAKE_PTR(resolved
);
3379 LIST_PREPEND(device_weights
, c
->io_device_weights
, w
);
3383 int config_parse_io_device_latency(
3385 const char *filename
,
3387 const char *section
,
3388 unsigned section_line
,
3395 _cleanup_free_
char *path
= NULL
, *resolved
= NULL
;
3396 CGroupIODeviceLatency
*l
;
3397 CGroupContext
*c
= data
;
3398 const char *p
= rvalue
;
3406 if (isempty(rvalue
)) {
3407 while (c
->io_device_latencies
)
3408 cgroup_context_free_io_device_latency(c
, c
->io_device_latencies
);
3413 r
= extract_first_word(&p
, &path
, NULL
, EXTRACT_QUOTES
);
3417 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
3418 "Invalid syntax, ignoring: %s", rvalue
);
3421 if (r
== 0 || isempty(p
)) {
3422 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
3423 "Failed to extract device path and latency from '%s', ignoring.", rvalue
);
3427 r
= unit_full_printf(userdata
, path
, &resolved
);
3429 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
3430 "Failed to resolve unit specifiers in '%s', ignoring: %m", path
);
3434 r
= path_simplify_and_warn(resolved
, 0, unit
, filename
, line
, lvalue
);
3438 if (parse_sec(p
, &usec
) < 0) {
3439 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse timer value, ignoring: %s", p
);
3443 l
= new0(CGroupIODeviceLatency
, 1);
3447 l
->path
= TAKE_PTR(resolved
);
3448 l
->target_usec
= usec
;
3450 LIST_PREPEND(device_latencies
, c
->io_device_latencies
, l
);
3454 int config_parse_io_limit(
3456 const char *filename
,
3458 const char *section
,
3459 unsigned section_line
,
3466 _cleanup_free_
char *path
= NULL
, *resolved
= NULL
;
3467 CGroupIODeviceLimit
*l
= NULL
, *t
;
3468 CGroupContext
*c
= data
;
3469 CGroupIOLimitType type
;
3470 const char *p
= rvalue
;
3478 type
= cgroup_io_limit_type_from_string(lvalue
);
3481 if (isempty(rvalue
)) {
3482 LIST_FOREACH(device_limits
, l
, c
->io_device_limits
)
3483 l
->limits
[type
] = cgroup_io_limit_defaults
[type
];
3487 r
= extract_first_word(&p
, &path
, NULL
, EXTRACT_QUOTES
);
3491 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
3492 "Invalid syntax, ignoring: %s", rvalue
);
3495 if (r
== 0 || isempty(p
)) {
3496 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
3497 "Failed to extract device node and bandwidth from '%s', ignoring.", rvalue
);
3501 r
= unit_full_printf(userdata
, path
, &resolved
);
3503 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
3504 "Failed to resolve unit specifiers in '%s', ignoring: %m", path
);
3508 r
= path_simplify_and_warn(resolved
, 0, unit
, filename
, line
, lvalue
);
3512 if (streq("infinity", p
))
3513 num
= CGROUP_LIMIT_MAX
;
3515 r
= parse_size(p
, 1000, &num
);
3516 if (r
< 0 || num
<= 0) {
3517 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid IO limit '%s', ignoring.", p
);
3522 LIST_FOREACH(device_limits
, t
, c
->io_device_limits
) {
3523 if (path_equal(resolved
, t
->path
)) {
3530 CGroupIOLimitType ttype
;
3532 l
= new0(CGroupIODeviceLimit
, 1);
3536 l
->path
= TAKE_PTR(resolved
);
3537 for (ttype
= 0; ttype
< _CGROUP_IO_LIMIT_TYPE_MAX
; ttype
++)
3538 l
->limits
[ttype
] = cgroup_io_limit_defaults
[ttype
];
3540 LIST_PREPEND(device_limits
, c
->io_device_limits
, l
);
3543 l
->limits
[type
] = num
;
3548 int config_parse_blockio_device_weight(
3550 const char *filename
,
3552 const char *section
,
3553 unsigned section_line
,
3560 _cleanup_free_
char *path
= NULL
, *resolved
= NULL
;
3561 CGroupBlockIODeviceWeight
*w
;
3562 CGroupContext
*c
= data
;
3563 const char *p
= rvalue
;
3571 if (isempty(rvalue
)) {
3572 while (c
->blockio_device_weights
)
3573 cgroup_context_free_blockio_device_weight(c
, c
->blockio_device_weights
);
3578 r
= extract_first_word(&p
, &path
, NULL
, EXTRACT_QUOTES
);
3582 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
3583 "Invalid syntax, ignoring: %s", rvalue
);
3586 if (r
== 0 || isempty(p
)) {
3587 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
3588 "Failed to extract device node and weight from '%s', ignoring.", rvalue
);
3592 r
= unit_full_printf(userdata
, path
, &resolved
);
3594 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
3595 "Failed to resolve unit specifiers in '%s', ignoring: %m", path
);
3599 r
= path_simplify_and_warn(resolved
, 0, unit
, filename
, line
, lvalue
);
3603 r
= cg_blkio_weight_parse(p
, &u
);
3605 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid block IO weight '%s', ignoring: %m", p
);
3609 assert(u
!= CGROUP_BLKIO_WEIGHT_INVALID
);
3611 w
= new0(CGroupBlockIODeviceWeight
, 1);
3615 w
->path
= TAKE_PTR(resolved
);
3618 LIST_PREPEND(device_weights
, c
->blockio_device_weights
, w
);
3622 int config_parse_blockio_bandwidth(
3624 const char *filename
,
3626 const char *section
,
3627 unsigned section_line
,
3634 _cleanup_free_
char *path
= NULL
, *resolved
= NULL
;
3635 CGroupBlockIODeviceBandwidth
*b
= NULL
, *t
;
3636 CGroupContext
*c
= data
;
3637 const char *p
= rvalue
;
3646 read
= streq("BlockIOReadBandwidth", lvalue
);
3648 if (isempty(rvalue
)) {
3649 LIST_FOREACH(device_bandwidths
, b
, c
->blockio_device_bandwidths
) {
3650 b
->rbps
= CGROUP_LIMIT_MAX
;
3651 b
->wbps
= CGROUP_LIMIT_MAX
;
3656 r
= extract_first_word(&p
, &path
, NULL
, EXTRACT_QUOTES
);
3660 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
3661 "Invalid syntax, ignoring: %s", rvalue
);
3664 if (r
== 0 || isempty(p
)) {
3665 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
3666 "Failed to extract device node and bandwidth from '%s', ignoring.", rvalue
);
3670 r
= unit_full_printf(userdata
, path
, &resolved
);
3672 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
3673 "Failed to resolve unit specifiers in '%s', ignoring: %m", path
);
3677 r
= path_simplify_and_warn(resolved
, 0, unit
, filename
, line
, lvalue
);
3681 r
= parse_size(p
, 1000, &bytes
);
3682 if (r
< 0 || bytes
<= 0) {
3683 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid Block IO Bandwidth '%s', ignoring.", p
);
3687 LIST_FOREACH(device_bandwidths
, t
, c
->blockio_device_bandwidths
) {
3688 if (path_equal(resolved
, t
->path
)) {
3695 b
= new0(CGroupBlockIODeviceBandwidth
, 1);
3699 b
->path
= TAKE_PTR(resolved
);
3700 b
->rbps
= CGROUP_LIMIT_MAX
;
3701 b
->wbps
= CGROUP_LIMIT_MAX
;
3703 LIST_PREPEND(device_bandwidths
, c
->blockio_device_bandwidths
, b
);
3714 int config_parse_job_mode_isolate(
3716 const char *filename
,
3718 const char *section
,
3719 unsigned section_line
,
3733 r
= parse_boolean(rvalue
);
3735 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse boolean, ignoring: %s", rvalue
);
3739 log_notice("%s is deprecated. Please use OnFailureJobMode= instead", lvalue
);
3741 *m
= r
? JOB_ISOLATE
: JOB_REPLACE
;
3745 int config_parse_exec_directories(
3747 const char *filename
,
3749 const char *section
,
3750 unsigned section_line
,
3767 if (isempty(rvalue
)) {
3768 /* Empty assignment resets the list */
3769 *rt
= strv_free(*rt
);
3773 for (p
= rvalue
;;) {
3774 _cleanup_free_
char *word
= NULL
, *k
= NULL
;
3776 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
);
3780 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
3781 "Invalid syntax, ignoring: %s", rvalue
);
3787 r
= unit_full_printf(u
, word
, &k
);
3789 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
3790 "Failed to resolve unit specifiers in \"%s\", ignoring: %m", word
);
3794 r
= path_simplify_and_warn(k
, PATH_CHECK_RELATIVE
, unit
, filename
, line
, lvalue
);
3798 if (path_startswith(k
, "private")) {
3799 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
3800 "%s= path can't be 'private', ignoring assignment: %s", lvalue
, word
);
3804 r
= strv_push(rt
, k
);
3811 int config_parse_set_status(
3813 const char *filename
,
3815 const char *section
,
3816 unsigned section_line
,
3824 const char *word
, *state
;
3826 ExitStatusSet
*status_set
= data
;
3833 /* Empty assignment resets the list */
3834 if (isempty(rvalue
)) {
3835 exit_status_set_free(status_set
);
3839 FOREACH_WORD(word
, l
, rvalue
, state
) {
3840 _cleanup_free_
char *temp
;
3844 temp
= strndup(word
, l
);
3848 r
= safe_atoi(temp
, &val
);
3850 val
= signal_from_string(temp
);
3853 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse value, ignoring: %s", word
);
3856 set
= &status_set
->signal
;
3858 if (val
< 0 || val
> 255) {
3859 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Value %d is outside range 0-255, ignoring", val
);
3862 set
= &status_set
->status
;
3865 r
= set_ensure_allocated(set
, NULL
);
3869 r
= set_put(*set
, INT_TO_PTR(val
));
3873 if (!isempty(state
))
3874 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Trailing garbage, ignoring.");
3879 int config_parse_namespace_path_strv(
3881 const char *filename
,
3883 const char *section
,
3884 unsigned section_line
,
3893 const char *p
= rvalue
;
3901 if (isempty(rvalue
)) {
3902 /* Empty assignment resets the list */
3903 *sv
= strv_free(*sv
);
3908 _cleanup_free_
char *word
= NULL
, *resolved
= NULL
, *joined
= NULL
;
3910 bool ignore_enoent
= false, shall_prefix
= false;
3912 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
);
3918 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to extract first word, ignoring: %s", rvalue
);
3923 if (startswith(w
, "-")) {
3924 ignore_enoent
= true;
3927 if (startswith(w
, "+")) {
3928 shall_prefix
= true;
3932 r
= unit_full_printf(u
, w
, &resolved
);
3934 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in %s: %m", w
);
3938 r
= path_simplify_and_warn(resolved
, PATH_CHECK_ABSOLUTE
, unit
, filename
, line
, lvalue
);
3942 joined
= strjoin(ignore_enoent
? "-" : "",
3943 shall_prefix
? "+" : "",
3946 r
= strv_push(sv
, joined
);
3956 int config_parse_temporary_filesystems(
3958 const char *filename
,
3960 const char *section
,
3961 unsigned section_line
,
3969 ExecContext
*c
= data
;
3970 const char *p
= rvalue
;
3978 if (isempty(rvalue
)) {
3979 /* Empty assignment resets the list */
3980 temporary_filesystem_free_many(c
->temporary_filesystems
, c
->n_temporary_filesystems
);
3981 c
->temporary_filesystems
= NULL
;
3982 c
->n_temporary_filesystems
= 0;
3987 _cleanup_free_
char *word
= NULL
, *path
= NULL
, *resolved
= NULL
;
3990 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
);
3996 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to extract first word, ignoring: %s", rvalue
);
4001 r
= extract_first_word(&w
, &path
, ":", EXTRACT_DONT_COALESCE_SEPARATORS
);
4005 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to extract first word, ignoring: %s", word
);
4009 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid syntax, ignoring: %s", word
);
4013 r
= unit_full_printf(u
, path
, &resolved
);
4015 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in %s, ignoring: %m", path
);
4019 r
= path_simplify_and_warn(resolved
, PATH_CHECK_ABSOLUTE
, unit
, filename
, line
, lvalue
);
4023 r
= temporary_filesystem_add(&c
->temporary_filesystems
, &c
->n_temporary_filesystems
, resolved
, w
);
4029 int config_parse_bind_paths(
4031 const char *filename
,
4033 const char *section
,
4034 unsigned section_line
,
4041 ExecContext
*c
= data
;
4051 if (isempty(rvalue
)) {
4052 /* Empty assignment resets the list */
4053 bind_mount_free_many(c
->bind_mounts
, c
->n_bind_mounts
);
4054 c
->bind_mounts
= NULL
;
4055 c
->n_bind_mounts
= 0;
4061 _cleanup_free_
char *source
= NULL
, *destination
= NULL
;
4062 _cleanup_free_
char *sresolved
= NULL
, *dresolved
= NULL
;
4063 char *s
= NULL
, *d
= NULL
;
4064 bool rbind
= true, ignore_enoent
= false;
4066 r
= extract_first_word(&p
, &source
, ":" WHITESPACE
, EXTRACT_QUOTES
|EXTRACT_DONT_COALESCE_SEPARATORS
);
4072 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse %s, ignoring: %s", lvalue
, rvalue
);
4076 r
= unit_full_printf(u
, source
, &sresolved
);
4078 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
4079 "Failed to resolved unit specifiers in \"%s\", ignoring: %m", source
);
4085 ignore_enoent
= true;
4089 r
= path_simplify_and_warn(s
, PATH_CHECK_ABSOLUTE
, unit
, filename
, line
, lvalue
);
4093 /* Optionally, the destination is specified. */
4094 if (p
&& p
[-1] == ':') {
4095 r
= extract_first_word(&p
, &destination
, ":" WHITESPACE
, EXTRACT_QUOTES
|EXTRACT_DONT_COALESCE_SEPARATORS
);
4099 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse %s, ignoring: %s", lvalue
, rvalue
);
4103 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Missing argument after ':', ignoring: %s", s
);
4107 r
= unit_full_printf(u
, destination
, &dresolved
);
4109 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
4110 "Failed to resolved specifiers in \"%s\", ignoring: %m", destination
);
4114 r
= path_simplify_and_warn(dresolved
, PATH_CHECK_ABSOLUTE
, unit
, filename
, line
, lvalue
);
4120 /* Optionally, there's also a short option string specified */
4121 if (p
&& p
[-1] == ':') {
4122 _cleanup_free_
char *options
= NULL
;
4124 r
= extract_first_word(&p
, &options
, NULL
, EXTRACT_QUOTES
);
4128 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse %s: %s", lvalue
, rvalue
);
4132 if (isempty(options
) || streq(options
, "rbind"))
4134 else if (streq(options
, "norbind"))
4137 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid option string, ignoring setting: %s", options
);
4144 r
= bind_mount_add(&c
->bind_mounts
, &c
->n_bind_mounts
,
4148 .read_only
= !!strstr(lvalue
, "ReadOnly"),
4150 .ignore_enoent
= ignore_enoent
,
4159 int config_parse_job_timeout_sec(
4161 const char *filename
,
4163 const char *section
,
4164 unsigned section_line
,
4180 r
= parse_sec_fix_0(rvalue
, &usec
);
4182 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse JobTimeoutSec= parameter, ignoring: %s", rvalue
);
4186 /* If the user explicitly changed JobTimeoutSec= also change JobRunningTimeoutSec=, for compatibility with old
4187 * versions. If JobRunningTimeoutSec= was explicitly set, avoid this however as whatever the user picked should
4190 if (!u
->job_running_timeout_set
)
4191 u
->job_running_timeout
= usec
;
4193 u
->job_timeout
= usec
;
4198 int config_parse_job_running_timeout_sec(
4200 const char *filename
,
4202 const char *section
,
4203 unsigned section_line
,
4219 r
= parse_sec_fix_0(rvalue
, &usec
);
4221 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse JobRunningTimeoutSec= parameter, ignoring: %s", rvalue
);
4225 u
->job_running_timeout
= usec
;
4226 u
->job_running_timeout_set
= true;
4231 int config_parse_emergency_action(
4233 const char *filename
,
4235 const char *section
,
4236 unsigned section_line
,
4244 EmergencyAction
*x
= data
;
4253 m
= ((Unit
*) userdata
)->manager
;
4257 r
= parse_emergency_action(rvalue
, MANAGER_IS_SYSTEM(m
), x
);
4259 if (r
== -EOPNOTSUPP
&& MANAGER_IS_USER(m
)) {
4260 /* Compat mode: remove for systemd 241. */
4262 log_syntax(unit
, LOG_INFO
, filename
, line
, r
,
4263 "%s= in user mode specified as \"%s\", using \"exit-force\" instead.",
4265 *x
= EMERGENCY_ACTION_EXIT_FORCE
;
4269 if (r
== -EOPNOTSUPP
)
4270 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
4271 "%s= specified as %s mode action, ignoring: %s",
4272 lvalue
, MANAGER_IS_SYSTEM(m
) ? "user" : "system", rvalue
);
4274 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
4275 "Failed to parse %s=, ignoring: %s", lvalue
, rvalue
);
4282 int config_parse_pid_file(
4284 const char *filename
,
4286 const char *section
,
4287 unsigned section_line
,
4294 _cleanup_free_
char *k
= NULL
, *n
= NULL
;
4304 if (isempty(rvalue
)) {
4305 /* An empty assignment removes already set value. */
4310 r
= unit_full_printf(u
, rvalue
, &k
);
4312 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue
);
4316 /* If this is a relative path make it absolute by prefixing the /run */
4317 n
= path_make_absolute(k
, u
->manager
->prefix
[EXEC_DIRECTORY_RUNTIME
]);
4321 /* Check that the result is a sensible path */
4322 r
= path_simplify_and_warn(n
, PATH_CHECK_ABSOLUTE
, unit
, filename
, line
, lvalue
);
4326 r
= patch_var_run(unit
, filename
, line
, lvalue
, &n
);
4330 free_and_replace(*s
, n
);
4334 int config_parse_exit_status(
4336 const char *filename
,
4338 const char *section
,
4339 unsigned section_line
,
4346 int *exit_status
= data
, r
;
4352 assert(exit_status
);
4354 if (isempty(rvalue
)) {
4359 r
= safe_atou8(rvalue
, &u
);
4361 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse exit status '%s', ignoring: %m", rvalue
);
4369 int config_parse_disable_controllers(
4371 const char *filename
,
4373 const char *section
,
4374 unsigned section_line
,
4382 CGroupContext
*c
= data
;
4383 CGroupMask disabled_mask
;
4385 /* 1. If empty, make all controllers eligible for use again.
4386 * 2. If non-empty, merge all listed controllers, space separated. */
4388 if (isempty(rvalue
)) {
4389 c
->disable_controllers
= 0;
4393 r
= cg_mask_from_string(rvalue
, &disabled_mask
);
4394 if (r
< 0 || disabled_mask
<= 0) {
4395 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid cgroup string: %s, ignoring", rvalue
);
4399 c
->disable_controllers
|= disabled_mask
;
4404 #define FOLLOW_MAX 8
4406 static int open_follow(char **filename
, FILE **_f
, Set
*names
, char **_final
) {
4417 /* This will update the filename pointer if the loaded file is
4418 * reached by a symlink. The old string will be freed. */
4421 char *target
, *name
;
4423 if (c
++ >= FOLLOW_MAX
)
4426 path_simplify(*filename
, false);
4428 /* Add the file name we are currently looking at to
4429 * the names of this unit, but only if it is a valid
4431 name
= basename(*filename
);
4432 if (unit_name_is_valid(name
, UNIT_NAME_ANY
)) {
4434 id
= set_get(names
, name
);
4440 r
= set_consume(names
, id
);
4446 /* Try to open the file name, but don't if its a symlink */
4447 fd
= open(*filename
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
4454 /* Hmm, so this is a symlink. Let's read the name, and follow it manually */
4455 r
= readlink_and_make_absolute(*filename
, &target
);
4459 free_and_replace(*filename
, target
);
4462 f
= fdopen(fd
, "r");
4474 static int merge_by_names(Unit
**u
, Set
*names
, const char *id
) {
4482 /* Let's try to add in all symlink names we found */
4483 while ((k
= set_steal_first(names
))) {
4485 /* First try to merge in the other name into our
4487 r
= unit_merge_by_name(*u
, k
);
4491 /* Hmm, we couldn't merge the other unit into
4492 * ours? Then let's try it the other way
4495 /* If the symlink name we are looking at is unit template, then
4496 we must search for instance of this template */
4497 if (unit_name_is_valid(k
, UNIT_NAME_TEMPLATE
) && (*u
)->instance
) {
4498 _cleanup_free_
char *instance
= NULL
;
4500 r
= unit_name_replace_instance(k
, (*u
)->instance
, &instance
);
4504 other
= manager_get_unit((*u
)->manager
, instance
);
4506 other
= manager_get_unit((*u
)->manager
, k
);
4511 r
= unit_merge(other
, *u
);
4514 return merge_by_names(u
, names
, NULL
);
4522 unit_choose_id(*u
, id
);
4530 static int load_from_path(Unit
*u
, const char *path
) {
4531 _cleanup_set_free_free_ Set
*symlink_names
= NULL
;
4532 _cleanup_fclose_
FILE *f
= NULL
;
4533 _cleanup_free_
char *filename
= NULL
;
4542 symlink_names
= set_new(&string_hash_ops
);
4546 if (path_is_absolute(path
)) {
4548 filename
= strdup(path
);
4552 r
= open_follow(&filename
, &f
, symlink_names
, &id
);
4554 filename
= mfree(filename
);
4562 STRV_FOREACH(p
, u
->manager
->lookup_paths
.search_path
) {
4564 /* Instead of opening the path right away, we manually
4565 * follow all symlinks and add their name to our unit
4566 * name set while doing so */
4567 filename
= path_make_absolute(path
, *p
);
4571 if (u
->manager
->unit_path_cache
&&
4572 !set_get(u
->manager
->unit_path_cache
, filename
))
4575 r
= open_follow(&filename
, &f
, symlink_names
, &id
);
4579 /* ENOENT means that the file is missing or is a dangling symlink.
4580 * ENOTDIR means that one of paths we expect to be is a directory
4581 * is not a directory, we should just ignore that.
4582 * EACCES means that the directory or file permissions are wrong.
4585 log_debug_errno(r
, "Cannot access \"%s\": %m", filename
);
4586 else if (!IN_SET(r
, -ENOENT
, -ENOTDIR
))
4589 filename
= mfree(filename
);
4590 /* Empty the symlink names for the next run */
4591 set_clear_free(symlink_names
);
4596 /* Hmm, no suitable file found? */
4599 if (!unit_type_may_alias(u
->type
) && set_size(symlink_names
) > 1) {
4600 log_unit_warning(u
, "Unit type of %s does not support alias names, refusing loading via symlink.", u
->id
);
4605 r
= merge_by_names(&merged
, symlink_names
, id
);
4610 u
->load_state
= UNIT_MERGED
;
4614 if (fstat(fileno(f
), &st
) < 0)
4617 if (null_or_empty(&st
)) {
4618 u
->load_state
= UNIT_MASKED
;
4619 u
->fragment_mtime
= 0;
4621 u
->load_state
= UNIT_LOADED
;
4622 u
->fragment_mtime
= timespec_load(&st
.st_mtim
);
4624 /* Now, parse the file contents */
4625 r
= config_parse(u
->id
, filename
, f
,
4626 UNIT_VTABLE(u
)->sections
,
4627 config_item_perf_lookup
, load_fragment_gperf_lookup
,
4628 CONFIG_PARSE_ALLOW_INCLUDE
, u
);
4633 free_and_replace(u
->fragment_path
, filename
);
4635 if (u
->source_path
) {
4636 if (stat(u
->source_path
, &st
) >= 0)
4637 u
->source_mtime
= timespec_load(&st
.st_mtim
);
4639 u
->source_mtime
= 0;
4645 int unit_load_fragment(Unit
*u
) {
4651 assert(u
->load_state
== UNIT_STUB
);
4655 u
->load_state
= UNIT_LOADED
;
4659 /* First, try to find the unit under its id. We always look
4660 * for unit files in the default directories, to make it easy
4661 * to override things by placing things in /etc/systemd/system */
4662 r
= load_from_path(u
, u
->id
);
4666 /* Try to find an alias we can load this with */
4667 if (u
->load_state
== UNIT_STUB
) {
4668 SET_FOREACH(t
, u
->names
, i
) {
4673 r
= load_from_path(u
, t
);
4677 if (u
->load_state
!= UNIT_STUB
)
4682 /* And now, try looking for it under the suggested (originally linked) path */
4683 if (u
->load_state
== UNIT_STUB
&& u
->fragment_path
) {
4685 r
= load_from_path(u
, u
->fragment_path
);
4689 if (u
->load_state
== UNIT_STUB
)
4690 /* Hmm, this didn't work? Then let's get rid
4691 * of the fragment path stored for us, so that
4692 * we don't point to an invalid location. */
4693 u
->fragment_path
= mfree(u
->fragment_path
);
4696 /* Look for a template */
4697 if (u
->load_state
== UNIT_STUB
&& u
->instance
) {
4698 _cleanup_free_
char *k
= NULL
;
4700 r
= unit_name_template(u
->id
, &k
);
4704 r
= load_from_path(u
, k
);
4707 log_unit_notice(u
, "Unit configuration has fatal error, unit will not be started.");
4711 if (u
->load_state
== UNIT_STUB
) {
4712 SET_FOREACH(t
, u
->names
, i
) {
4713 _cleanup_free_
char *z
= NULL
;
4718 r
= unit_name_template(t
, &z
);
4722 r
= load_from_path(u
, z
);
4726 if (u
->load_state
!= UNIT_STUB
)
4735 void unit_dump_config_items(FILE *f
) {
4736 static const struct {
4737 const ConfigParserCallback callback
;
4740 { config_parse_warn_compat
, "NOTSUPPORTED" },
4741 { config_parse_int
, "INTEGER" },
4742 { config_parse_unsigned
, "UNSIGNED" },
4743 { config_parse_iec_size
, "SIZE" },
4744 { config_parse_iec_uint64
, "SIZE" },
4745 { config_parse_si_size
, "SIZE" },
4746 { config_parse_bool
, "BOOLEAN" },
4747 { config_parse_string
, "STRING" },
4748 { config_parse_path
, "PATH" },
4749 { config_parse_unit_path_printf
, "PATH" },
4750 { config_parse_strv
, "STRING [...]" },
4751 { config_parse_exec_nice
, "NICE" },
4752 { config_parse_exec_oom_score_adjust
, "OOMSCOREADJUST" },
4753 { config_parse_exec_io_class
, "IOCLASS" },
4754 { config_parse_exec_io_priority
, "IOPRIORITY" },
4755 { config_parse_exec_cpu_sched_policy
, "CPUSCHEDPOLICY" },
4756 { config_parse_exec_cpu_sched_prio
, "CPUSCHEDPRIO" },
4757 { config_parse_exec_cpu_affinity
, "CPUAFFINITY" },
4758 { config_parse_mode
, "MODE" },
4759 { config_parse_unit_env_file
, "FILE" },
4760 { config_parse_exec_output
, "OUTPUT" },
4761 { config_parse_exec_input
, "INPUT" },
4762 { config_parse_log_facility
, "FACILITY" },
4763 { config_parse_log_level
, "LEVEL" },
4764 { config_parse_exec_secure_bits
, "SECUREBITS" },
4765 { config_parse_capability_set
, "BOUNDINGSET" },
4766 { config_parse_rlimit
, "LIMIT" },
4767 { config_parse_unit_deps
, "UNIT [...]" },
4768 { config_parse_exec
, "PATH [ARGUMENT [...]]" },
4769 { config_parse_service_type
, "SERVICETYPE" },
4770 { config_parse_service_restart
, "SERVICERESTART" },
4771 { config_parse_kill_mode
, "KILLMODE" },
4772 { config_parse_signal
, "SIGNAL" },
4773 { config_parse_socket_listen
, "SOCKET [...]" },
4774 { config_parse_socket_bind
, "SOCKETBIND" },
4775 { config_parse_socket_bindtodevice
, "NETWORKINTERFACE" },
4776 { config_parse_sec
, "SECONDS" },
4777 { config_parse_nsec
, "NANOSECONDS" },
4778 { config_parse_namespace_path_strv
, "PATH [...]" },
4779 { config_parse_bind_paths
, "PATH[:PATH[:OPTIONS]] [...]" },
4780 { config_parse_unit_requires_mounts_for
, "PATH [...]" },
4781 { config_parse_exec_mount_flags
, "MOUNTFLAG [...]" },
4782 { config_parse_unit_string_printf
, "STRING" },
4783 { config_parse_trigger_unit
, "UNIT" },
4784 { config_parse_timer
, "TIMER" },
4785 { config_parse_path_spec
, "PATH" },
4786 { config_parse_notify_access
, "ACCESS" },
4787 { config_parse_ip_tos
, "TOS" },
4788 { config_parse_unit_condition_path
, "CONDITION" },
4789 { config_parse_unit_condition_string
, "CONDITION" },
4790 { config_parse_unit_condition_null
, "CONDITION" },
4791 { config_parse_unit_slice
, "SLICE" },
4792 { config_parse_documentation
, "URL" },
4793 { config_parse_service_timeout
, "SECONDS" },
4794 { config_parse_emergency_action
, "ACTION" },
4795 { config_parse_set_status
, "STATUS" },
4796 { config_parse_service_sockets
, "SOCKETS" },
4797 { config_parse_environ
, "ENVIRON" },
4799 { config_parse_syscall_filter
, "SYSCALLS" },
4800 { config_parse_syscall_archs
, "ARCHS" },
4801 { config_parse_syscall_errno
, "ERRNO" },
4802 { config_parse_address_families
, "FAMILIES" },
4803 { config_parse_restrict_namespaces
, "NAMESPACES" },
4805 { config_parse_cpu_shares
, "SHARES" },
4806 { config_parse_cg_weight
, "WEIGHT" },
4807 { config_parse_memory_limit
, "LIMIT" },
4808 { config_parse_device_allow
, "DEVICE" },
4809 { config_parse_device_policy
, "POLICY" },
4810 { config_parse_io_limit
, "LIMIT" },
4811 { config_parse_io_device_weight
, "DEVICEWEIGHT" },
4812 { config_parse_io_device_latency
, "DEVICELATENCY" },
4813 { config_parse_blockio_bandwidth
, "BANDWIDTH" },
4814 { config_parse_blockio_weight
, "WEIGHT" },
4815 { config_parse_blockio_device_weight
, "DEVICEWEIGHT" },
4816 { config_parse_long
, "LONG" },
4817 { config_parse_socket_service
, "SERVICE" },
4819 { config_parse_exec_selinux_context
, "LABEL" },
4821 { config_parse_job_mode
, "MODE" },
4822 { config_parse_job_mode_isolate
, "BOOLEAN" },
4823 { config_parse_personality
, "PERSONALITY" },
4826 const char *prev
= NULL
;
4831 NULSTR_FOREACH(i
, load_fragment_gperf_nulstr
) {
4832 const char *rvalue
= "OTHER", *lvalue
;
4833 const ConfigPerfItem
*p
;
4838 assert_se(p
= load_fragment_gperf_lookup(i
, strlen(i
)));
4840 /* Hide legacy settings */
4841 if (p
->parse
== config_parse_warn_compat
&&
4842 p
->ltype
== DISABLED_LEGACY
)
4845 for (j
= 0; j
< ELEMENTSOF(table
); j
++)
4846 if (p
->parse
== table
[j
].callback
) {
4847 rvalue
= table
[j
].rvalue
;
4851 dot
= strchr(i
, '.');
4852 lvalue
= dot
? dot
+ 1 : i
;
4856 if (!prev
|| !strneq(prev
, i
, prefix_len
+1)) {
4860 fprintf(f
, "[%.*s]\n", (int) prefix_len
, i
);
4863 fprintf(f
, "%s=%s\n", lvalue
, rvalue
);