]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/load-fragment.c
pid1: fix setting of DefaultTimeoutAbortSec
[thirdparty/systemd.git] / src / core / load-fragment.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright © 2012 Holger Hans Peter Freyther
4 ***/
5
6 #include <errno.h>
7 #include <fcntl.h>
8 #include <linux/fs.h>
9 #include <linux/oom.h>
10 #if HAVE_SECCOMP
11 #include <seccomp.h>
12 #endif
13 #include <sched.h>
14 #include <sys/resource.h>
15
16 #include "af-list.h"
17 #include "alloc-util.h"
18 #include "all-units.h"
19 #include "bpf-firewall.h"
20 #include "bus-error.h"
21 #include "bus-internal.h"
22 #include "bus-util.h"
23 #include "cap-list.h"
24 #include "capability-util.h"
25 #include "cgroup-setup.h"
26 #include "conf-parser.h"
27 #include "cpu-set-util.h"
28 #include "env-util.h"
29 #include "errno-list.h"
30 #include "escape.h"
31 #include "fd-util.h"
32 #include "fs-util.h"
33 #include "hexdecoct.h"
34 #include "io-util.h"
35 #include "ioprio.h"
36 #include "ip-protocol-list.h"
37 #include "journal-util.h"
38 #include "limits-util.h"
39 #include "load-fragment.h"
40 #include "log.h"
41 #include "mountpoint-util.h"
42 #include "nulstr-util.h"
43 #include "parse-util.h"
44 #include "path-util.h"
45 #include "process-util.h"
46 #if HAVE_SECCOMP
47 #include "seccomp-util.h"
48 #endif
49 #include "securebits-util.h"
50 #include "signal-util.h"
51 #include "stat-util.h"
52 #include "string-util.h"
53 #include "strv.h"
54 #include "unit-name.h"
55 #include "unit-printf.h"
56 #include "user-util.h"
57 #include "time-util.h"
58 #include "web-util.h"
59
60 static int parse_socket_protocol(const char *s) {
61 int r;
62
63 r = parse_ip_protocol(s);
64 if (r < 0)
65 return r;
66 if (!IN_SET(r, IPPROTO_UDPLITE, IPPROTO_SCTP))
67 return -EPROTONOSUPPORT;
68
69 return r;
70 }
71
72 int parse_crash_chvt(const char *value, int *data) {
73 int b;
74
75 if (safe_atoi(value, data) >= 0)
76 return 0;
77
78 b = parse_boolean(value);
79 if (b < 0)
80 return b;
81
82 if (b > 0)
83 *data = 0; /* switch to where kmsg goes */
84 else
85 *data = -1; /* turn off switching */
86
87 return 0;
88 }
89
90 int parse_confirm_spawn(const char *value, char **console) {
91 char *s;
92 int r;
93
94 r = value ? parse_boolean(value) : 1;
95 if (r == 0) {
96 *console = NULL;
97 return 0;
98 } else if (r > 0) /* on with default tty */
99 s = strdup("/dev/console");
100 else if (is_path(value)) /* on with fully qualified path */
101 s = strdup(value);
102 else /* on with only a tty file name, not a fully qualified path */
103 s = path_join("/dev/", value);
104 if (!s)
105 return -ENOMEM;
106
107 *console = s;
108 return 0;
109 }
110
111 DEFINE_CONFIG_PARSE(config_parse_socket_protocol, parse_socket_protocol, "Failed to parse socket protocol");
112 DEFINE_CONFIG_PARSE(config_parse_exec_secure_bits, secure_bits_from_string, "Failed to parse secure bits");
113 DEFINE_CONFIG_PARSE_ENUM(config_parse_collect_mode, collect_mode, CollectMode, "Failed to parse garbage collection mode");
114 DEFINE_CONFIG_PARSE_ENUM(config_parse_device_policy, cgroup_device_policy, CGroupDevicePolicy, "Failed to parse device policy");
115 DEFINE_CONFIG_PARSE_ENUM(config_parse_exec_keyring_mode, exec_keyring_mode, ExecKeyringMode, "Failed to parse keyring mode");
116 DEFINE_CONFIG_PARSE_ENUM(config_parse_exec_utmp_mode, exec_utmp_mode, ExecUtmpMode, "Failed to parse utmp mode");
117 DEFINE_CONFIG_PARSE_ENUM(config_parse_job_mode, job_mode, JobMode, "Failed to parse job mode");
118 DEFINE_CONFIG_PARSE_ENUM(config_parse_kill_mode, kill_mode, KillMode, "Failed to parse kill mode");
119 DEFINE_CONFIG_PARSE_ENUM(config_parse_notify_access, notify_access, NotifyAccess, "Failed to parse notify access specifier");
120 DEFINE_CONFIG_PARSE_ENUM(config_parse_protect_home, protect_home, ProtectHome, "Failed to parse protect home value");
121 DEFINE_CONFIG_PARSE_ENUM(config_parse_protect_system, protect_system, ProtectSystem, "Failed to parse protect system value");
122 DEFINE_CONFIG_PARSE_ENUM(config_parse_runtime_preserve_mode, exec_preserve_mode, ExecPreserveMode, "Failed to parse runtime directory preserve mode");
123 DEFINE_CONFIG_PARSE_ENUM(config_parse_service_type, service_type, ServiceType, "Failed to parse service type");
124 DEFINE_CONFIG_PARSE_ENUM(config_parse_service_restart, service_restart, ServiceRestart, "Failed to parse service restart specifier");
125 DEFINE_CONFIG_PARSE_ENUM(config_parse_socket_bind, socket_address_bind_ipv6_only_or_bool, SocketAddressBindIPv6Only, "Failed to parse bind IPv6 only value");
126 DEFINE_CONFIG_PARSE_ENUM(config_parse_oom_policy, oom_policy, OOMPolicy, "Failed to parse OOM policy");
127 DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT(config_parse_ip_tos, ip_tos, int, -1, "Failed to parse IP TOS value");
128 DEFINE_CONFIG_PARSE_PTR(config_parse_blockio_weight, cg_blkio_weight_parse, uint64_t, "Invalid block IO weight");
129 DEFINE_CONFIG_PARSE_PTR(config_parse_cg_weight, cg_weight_parse, uint64_t, "Invalid weight");
130 DEFINE_CONFIG_PARSE_PTR(config_parse_cpu_shares, cg_cpu_shares_parse, uint64_t, "Invalid CPU shares");
131 DEFINE_CONFIG_PARSE_PTR(config_parse_exec_mount_flags, mount_propagation_flags_from_string, unsigned long, "Failed to parse mount flag");
132 DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT(config_parse_numa_policy, mpol, int, -1, "Invalid NUMA policy type");
133 DEFINE_CONFIG_PARSE_ENUM(config_parse_status_unit_format, status_unit_format, StatusUnitFormat, "Failed to parse status unit format");
134
135 int config_parse_unit_deps(
136 const char *unit,
137 const char *filename,
138 unsigned line,
139 const char *section,
140 unsigned section_line,
141 const char *lvalue,
142 int ltype,
143 const char *rvalue,
144 void *data,
145 void *userdata) {
146
147 UnitDependency d = ltype;
148 Unit *u = userdata;
149 const char *p;
150
151 assert(filename);
152 assert(lvalue);
153 assert(rvalue);
154
155 p = rvalue;
156 for (;;) {
157 _cleanup_free_ char *word = NULL, *k = NULL;
158 int r;
159
160 r = extract_first_word(&p, &word, NULL, EXTRACT_RETAIN_ESCAPE);
161 if (r == 0)
162 break;
163 if (r == -ENOMEM)
164 return log_oom();
165 if (r < 0) {
166 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
167 break;
168 }
169
170 r = unit_name_printf(u, word, &k);
171 if (r < 0) {
172 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", word);
173 continue;
174 }
175
176 r = unit_add_dependency_by_name(u, d, k, true, UNIT_DEPENDENCY_FILE);
177 if (r < 0)
178 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to add dependency on %s, ignoring: %m", k);
179 }
180
181 return 0;
182 }
183
184 int config_parse_obsolete_unit_deps(
185 const char *unit,
186 const char *filename,
187 unsigned line,
188 const char *section,
189 unsigned section_line,
190 const char *lvalue,
191 int ltype,
192 const char *rvalue,
193 void *data,
194 void *userdata) {
195
196 log_syntax(unit, LOG_WARNING, filename, line, 0,
197 "Unit dependency type %s= is obsolete, replacing by %s=, please update your unit file", lvalue, unit_dependency_to_string(ltype));
198
199 return config_parse_unit_deps(unit, filename, line, section, section_line, lvalue, ltype, rvalue, data, userdata);
200 }
201
202 int config_parse_unit_string_printf(
203 const char *unit,
204 const char *filename,
205 unsigned line,
206 const char *section,
207 unsigned section_line,
208 const char *lvalue,
209 int ltype,
210 const char *rvalue,
211 void *data,
212 void *userdata) {
213
214 _cleanup_free_ char *k = NULL;
215 const Unit *u = userdata;
216 int r;
217
218 assert(filename);
219 assert(lvalue);
220 assert(rvalue);
221 assert(u);
222
223 r = unit_full_printf(u, rvalue, &k);
224 if (r < 0) {
225 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
226 return 0;
227 }
228
229 return config_parse_string(unit, filename, line, section, section_line, lvalue, ltype, k, data, userdata);
230 }
231
232 int config_parse_unit_strv_printf(
233 const char *unit,
234 const char *filename,
235 unsigned line,
236 const char *section,
237 unsigned section_line,
238 const char *lvalue,
239 int ltype,
240 const char *rvalue,
241 void *data,
242 void *userdata) {
243
244 const Unit *u = userdata;
245 _cleanup_free_ char *k = NULL;
246 int r;
247
248 assert(filename);
249 assert(lvalue);
250 assert(rvalue);
251 assert(u);
252
253 r = unit_full_printf(u, rvalue, &k);
254 if (r < 0) {
255 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
256 return 0;
257 }
258
259 return config_parse_strv(unit, filename, line, section, section_line, lvalue, ltype, k, data, userdata);
260 }
261
262 int config_parse_unit_path_printf(
263 const char *unit,
264 const char *filename,
265 unsigned line,
266 const char *section,
267 unsigned section_line,
268 const char *lvalue,
269 int ltype,
270 const char *rvalue,
271 void *data,
272 void *userdata) {
273
274 _cleanup_free_ char *k = NULL;
275 const Unit *u = userdata;
276 int r;
277 bool fatal = ltype;
278
279 assert(filename);
280 assert(lvalue);
281 assert(rvalue);
282 assert(u);
283
284 /* Let's not bother with anything that is too long */
285 if (strlen(rvalue) >= PATH_MAX) {
286 log_syntax(unit, LOG_ERR, filename, line, 0,
287 "%s value too long%s.",
288 lvalue, fatal ? "" : ", ignoring");
289 return fatal ? -ENAMETOOLONG : 0;
290 }
291
292 r = unit_full_printf(u, rvalue, &k);
293 if (r < 0) {
294 log_syntax(unit, LOG_ERR, filename, line, r,
295 "Failed to resolve unit specifiers in '%s'%s: %m",
296 rvalue, fatal ? "" : ", ignoring");
297 return fatal ? -ENOEXEC : 0;
298 }
299
300 return config_parse_path(unit, filename, line, section, section_line, lvalue, ltype, k, data, userdata);
301 }
302
303 int config_parse_unit_path_strv_printf(
304 const char *unit,
305 const char *filename,
306 unsigned line,
307 const char *section,
308 unsigned section_line,
309 const char *lvalue,
310 int ltype,
311 const char *rvalue,
312 void *data,
313 void *userdata) {
314
315 char ***x = data;
316 const Unit *u = userdata;
317 int r;
318 const char *p;
319
320 assert(filename);
321 assert(lvalue);
322 assert(rvalue);
323 assert(u);
324
325 if (isempty(rvalue)) {
326 *x = strv_free(*x);
327 return 0;
328 }
329
330 for (p = rvalue;;) {
331 _cleanup_free_ char *word = NULL, *k = NULL;
332
333 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
334 if (r == 0)
335 return 0;
336 if (r == -ENOMEM)
337 return log_oom();
338 if (r < 0) {
339 log_syntax(unit, LOG_WARNING, filename, line, r,
340 "Invalid syntax, ignoring: %s", rvalue);
341 return 0;
342 }
343
344 r = unit_full_printf(u, word, &k);
345 if (r < 0) {
346 log_syntax(unit, LOG_ERR, filename, line, r,
347 "Failed to resolve unit specifiers in '%s', ignoring: %m", word);
348 return 0;
349 }
350
351 r = path_simplify_and_warn(k, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
352 if (r < 0)
353 return 0;
354
355 r = strv_consume(x, TAKE_PTR(k));
356 if (r < 0)
357 return log_oom();
358 }
359 }
360
361 static int patch_var_run(
362 const char *unit,
363 const char *filename,
364 unsigned line,
365 const char *lvalue,
366 char **path) {
367
368 const char *e;
369 char *z;
370
371 e = path_startswith(*path, "/var/run/");
372 if (!e)
373 return 0;
374
375 z = path_join("/run/", e);
376 if (!z)
377 return log_oom();
378
379 log_syntax(unit, LOG_NOTICE, filename, line, 0,
380 "%s= references a path below legacy directory /var/run/, updating %s → %s; "
381 "please update the unit file accordingly.", lvalue, *path, z);
382
383 free_and_replace(*path, z);
384
385 return 1;
386 }
387
388 int config_parse_socket_listen(
389 const char *unit,
390 const char *filename,
391 unsigned line,
392 const char *section,
393 unsigned section_line,
394 const char *lvalue,
395 int ltype,
396 const char *rvalue,
397 void *data,
398 void *userdata) {
399
400 _cleanup_free_ SocketPort *p = NULL;
401 SocketPort *tail;
402 Socket *s;
403 int r;
404
405 assert(filename);
406 assert(lvalue);
407 assert(rvalue);
408 assert(data);
409
410 s = SOCKET(data);
411
412 if (isempty(rvalue)) {
413 /* An empty assignment removes all ports */
414 socket_free_ports(s);
415 return 0;
416 }
417
418 p = new0(SocketPort, 1);
419 if (!p)
420 return log_oom();
421
422 if (ltype != SOCKET_SOCKET) {
423 _cleanup_free_ char *k = NULL;
424
425 r = unit_full_printf(UNIT(s), rvalue, &k);
426 if (r < 0) {
427 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
428 return 0;
429 }
430
431 r = path_simplify_and_warn(k, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
432 if (r < 0)
433 return 0;
434
435 if (ltype == SOCKET_FIFO) {
436 r = patch_var_run(unit, filename, line, lvalue, &k);
437 if (r < 0)
438 return r;
439 }
440
441 free_and_replace(p->path, k);
442 p->type = ltype;
443
444 } else if (streq(lvalue, "ListenNetlink")) {
445 _cleanup_free_ char *k = NULL;
446
447 r = unit_full_printf(UNIT(s), rvalue, &k);
448 if (r < 0) {
449 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
450 return 0;
451 }
452
453 r = socket_address_parse_netlink(&p->address, k);
454 if (r < 0) {
455 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse address value in '%s', ignoring: %m", k);
456 return 0;
457 }
458
459 p->type = SOCKET_SOCKET;
460
461 } else {
462 _cleanup_free_ char *k = NULL;
463
464 r = unit_full_printf(UNIT(s), rvalue, &k);
465 if (r < 0) {
466 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
467 return 0;
468 }
469
470 if (k[0] == '/') { /* Only for AF_UNIX file system sockets… */
471 r = patch_var_run(unit, filename, line, lvalue, &k);
472 if (r < 0)
473 return r;
474 }
475
476 r = socket_address_parse_and_warn(&p->address, k);
477 if (r < 0) {
478 if (r != -EAFNOSUPPORT)
479 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse address value in '%s', ignoring: %m", k);
480 return 0;
481 }
482
483 if (streq(lvalue, "ListenStream"))
484 p->address.type = SOCK_STREAM;
485 else if (streq(lvalue, "ListenDatagram"))
486 p->address.type = SOCK_DGRAM;
487 else {
488 assert(streq(lvalue, "ListenSequentialPacket"));
489 p->address.type = SOCK_SEQPACKET;
490 }
491
492 if (socket_address_family(&p->address) != AF_LOCAL && p->address.type == SOCK_SEQPACKET) {
493 log_syntax(unit, LOG_ERR, filename, line, 0, "Address family not supported, ignoring: %s", rvalue);
494 return 0;
495 }
496
497 p->type = SOCKET_SOCKET;
498 }
499
500 p->fd = -1;
501 p->auxiliary_fds = NULL;
502 p->n_auxiliary_fds = 0;
503 p->socket = s;
504
505 LIST_FIND_TAIL(port, s->ports, tail);
506 LIST_INSERT_AFTER(port, s->ports, tail, p);
507
508 p = NULL;
509
510 return 0;
511 }
512
513 int config_parse_exec_nice(
514 const char *unit,
515 const char *filename,
516 unsigned line,
517 const char *section,
518 unsigned section_line,
519 const char *lvalue,
520 int ltype,
521 const char *rvalue,
522 void *data,
523 void *userdata) {
524
525 ExecContext *c = data;
526 int priority, r;
527
528 assert(filename);
529 assert(lvalue);
530 assert(rvalue);
531 assert(data);
532
533 if (isempty(rvalue)) {
534 c->nice_set = false;
535 return 0;
536 }
537
538 r = parse_nice(rvalue, &priority);
539 if (r < 0) {
540 if (r == -ERANGE)
541 log_syntax(unit, LOG_ERR, filename, line, r, "Nice priority out of range, ignoring: %s", rvalue);
542 else
543 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse nice priority '%s', ignoring: %m", rvalue);
544 return 0;
545 }
546
547 c->nice = priority;
548 c->nice_set = true;
549
550 return 0;
551 }
552
553 int config_parse_exec_oom_score_adjust(
554 const char* unit,
555 const char *filename,
556 unsigned line,
557 const char *section,
558 unsigned section_line,
559 const char *lvalue,
560 int ltype,
561 const char *rvalue,
562 void *data,
563 void *userdata) {
564
565 ExecContext *c = data;
566 int oa, r;
567
568 assert(filename);
569 assert(lvalue);
570 assert(rvalue);
571 assert(data);
572
573 if (isempty(rvalue)) {
574 c->oom_score_adjust_set = false;
575 return 0;
576 }
577
578 r = parse_oom_score_adjust(rvalue, &oa);
579 if (r < 0) {
580 if (r == -ERANGE)
581 log_syntax(unit, LOG_ERR, filename, line, r, "OOM score adjust value out of range, ignoring: %s", rvalue);
582 else
583 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse the OOM score adjust value '%s', ignoring: %m", rvalue);
584 return 0;
585 }
586
587 c->oom_score_adjust = oa;
588 c->oom_score_adjust_set = true;
589
590 return 0;
591 }
592
593 int config_parse_exec(
594 const char *unit,
595 const char *filename,
596 unsigned line,
597 const char *section,
598 unsigned section_line,
599 const char *lvalue,
600 int ltype,
601 const char *rvalue,
602 void *data,
603 void *userdata) {
604
605 ExecCommand **e = data;
606 const Unit *u = userdata;
607 const char *p;
608 bool semicolon;
609 int r;
610
611 assert(filename);
612 assert(lvalue);
613 assert(rvalue);
614 assert(e);
615
616 e += ltype;
617
618 if (isempty(rvalue)) {
619 /* An empty assignment resets the list */
620 *e = exec_command_free_list(*e);
621 return 0;
622 }
623
624 p = rvalue;
625 do {
626 _cleanup_free_ char *path = NULL, *firstword = NULL;
627 ExecCommandFlags flags = 0;
628 bool ignore = false, separate_argv0 = false;
629 _cleanup_free_ ExecCommand *nce = NULL;
630 _cleanup_strv_free_ char **n = NULL;
631 size_t nlen = 0, nbufsize = 0;
632 const char *f;
633
634 semicolon = false;
635
636 r = extract_first_word_and_warn(&p, &firstword, NULL, EXTRACT_UNQUOTE|EXTRACT_CUNESCAPE, unit, filename, line, rvalue);
637 if (r <= 0)
638 return 0;
639
640 f = firstword;
641 for (;;) {
642 /* We accept an absolute path as first argument. If it's prefixed with - and the path doesn't
643 * exist, we ignore it instead of erroring out; if it's prefixed with @, we allow overriding of
644 * argv[0]; if it's prefixed with :, we will not do environment variable substitution;
645 * if it's prefixed with +, it will be run with full privileges and no sandboxing; if
646 * it's prefixed with '!' we apply sandboxing, but do not change user/group credentials; if
647 * it's prefixed with '!!', then we apply user/group credentials if the kernel supports ambient
648 * capabilities -- if it doesn't we don't apply the credentials themselves, but do apply most
649 * other sandboxing, with some special exceptions for changing UID.
650 *
651 * The idea is that '!!' may be used to write services that can take benefit of systemd's
652 * UID/GID dropping if the kernel supports ambient creds, but provide an automatic fallback to
653 * privilege dropping within the daemon if the kernel does not offer that. */
654
655 if (*f == '-' && !(flags & EXEC_COMMAND_IGNORE_FAILURE)) {
656 flags |= EXEC_COMMAND_IGNORE_FAILURE;
657 ignore = true;
658 } else if (*f == '@' && !separate_argv0)
659 separate_argv0 = true;
660 else if (*f == ':' && !(flags & EXEC_COMMAND_NO_ENV_EXPAND))
661 flags |= EXEC_COMMAND_NO_ENV_EXPAND;
662 else if (*f == '+' && !(flags & (EXEC_COMMAND_FULLY_PRIVILEGED|EXEC_COMMAND_NO_SETUID|EXEC_COMMAND_AMBIENT_MAGIC)))
663 flags |= EXEC_COMMAND_FULLY_PRIVILEGED;
664 else if (*f == '!' && !(flags & (EXEC_COMMAND_FULLY_PRIVILEGED|EXEC_COMMAND_NO_SETUID|EXEC_COMMAND_AMBIENT_MAGIC)))
665 flags |= EXEC_COMMAND_NO_SETUID;
666 else if (*f == '!' && !(flags & (EXEC_COMMAND_FULLY_PRIVILEGED|EXEC_COMMAND_AMBIENT_MAGIC))) {
667 flags &= ~EXEC_COMMAND_NO_SETUID;
668 flags |= EXEC_COMMAND_AMBIENT_MAGIC;
669 } else
670 break;
671 f++;
672 }
673
674 r = unit_full_printf(u, f, &path);
675 if (r < 0) {
676 log_syntax(unit, LOG_ERR, filename, line, r,
677 "Failed to resolve unit specifiers in '%s'%s: %m",
678 f, ignore ? ", ignoring" : "");
679 return ignore ? 0 : -ENOEXEC;
680 }
681
682 if (isempty(path)) {
683 /* First word is either "-" or "@" with no command. */
684 log_syntax(unit, LOG_ERR, filename, line, 0,
685 "Empty path in command line%s: '%s'",
686 ignore ? ", ignoring" : "", rvalue);
687 return ignore ? 0 : -ENOEXEC;
688 }
689 if (!string_is_safe(path)) {
690 log_syntax(unit, LOG_ERR, filename, line, 0,
691 "Executable name contains special characters%s: %s",
692 ignore ? ", ignoring" : "", path);
693 return ignore ? 0 : -ENOEXEC;
694 }
695 if (endswith(path, "/")) {
696 log_syntax(unit, LOG_ERR, filename, line, 0,
697 "Executable path specifies a directory%s: %s",
698 ignore ? ", ignoring" : "", path);
699 return ignore ? 0 : -ENOEXEC;
700 }
701
702 if (!path_is_absolute(path)) {
703 const char *prefix;
704 bool found = false;
705
706 if (!filename_is_valid(path)) {
707 log_syntax(unit, LOG_ERR, filename, line, 0,
708 "Neither a valid executable name nor an absolute path%s: %s",
709 ignore ? ", ignoring" : "", path);
710 return ignore ? 0 : -ENOEXEC;
711 }
712
713 /* Resolve a single-component name to a full path */
714 NULSTR_FOREACH(prefix, DEFAULT_PATH_NULSTR) {
715 _cleanup_free_ char *fullpath = NULL;
716
717 fullpath = path_join(prefix, path);
718 if (!fullpath)
719 return log_oom();
720
721 if (access(fullpath, F_OK) >= 0) {
722 free_and_replace(path, fullpath);
723 found = true;
724 break;
725 }
726 }
727
728 if (!found) {
729 log_syntax(unit, LOG_ERR, filename, line, 0,
730 "Executable \"%s\" not found in path \"%s\"%s",
731 path, DEFAULT_PATH, ignore ? ", ignoring" : "");
732 return ignore ? 0 : -ENOEXEC;
733 }
734 }
735
736 if (!separate_argv0) {
737 char *w = NULL;
738
739 if (!GREEDY_REALLOC(n, nbufsize, nlen + 2))
740 return log_oom();
741
742 w = strdup(path);
743 if (!w)
744 return log_oom();
745 n[nlen++] = w;
746 n[nlen] = NULL;
747 }
748
749 path_simplify(path, false);
750
751 while (!isempty(p)) {
752 _cleanup_free_ char *word = NULL, *resolved = NULL;
753
754 /* Check explicitly for an unquoted semicolon as
755 * command separator token. */
756 if (p[0] == ';' && (!p[1] || strchr(WHITESPACE, p[1]))) {
757 p++;
758 p += strspn(p, WHITESPACE);
759 semicolon = true;
760 break;
761 }
762
763 /* Check for \; explicitly, to not confuse it with \\; or "\;" or "\\;" etc.
764 * extract_first_word() would return the same for all of those. */
765 if (p[0] == '\\' && p[1] == ';' && (!p[2] || strchr(WHITESPACE, p[2]))) {
766 char *w;
767
768 p += 2;
769 p += strspn(p, WHITESPACE);
770
771 if (!GREEDY_REALLOC(n, nbufsize, nlen + 2))
772 return log_oom();
773
774 w = strdup(";");
775 if (!w)
776 return log_oom();
777 n[nlen++] = w;
778 n[nlen] = NULL;
779 continue;
780 }
781
782 r = extract_first_word_and_warn(&p, &word, NULL, EXTRACT_UNQUOTE|EXTRACT_CUNESCAPE, unit, filename, line, rvalue);
783 if (r == 0)
784 break;
785 if (r < 0)
786 return ignore ? 0 : -ENOEXEC;
787
788 r = unit_full_printf(u, word, &resolved);
789 if (r < 0) {
790 log_syntax(unit, LOG_ERR, filename, line, r,
791 "Failed to resolve unit specifiers in %s%s: %m",
792 word, ignore ? ", ignoring" : "");
793 return ignore ? 0 : -ENOEXEC;
794 }
795
796 if (!GREEDY_REALLOC(n, nbufsize, nlen + 2))
797 return log_oom();
798
799 n[nlen++] = TAKE_PTR(resolved);
800 n[nlen] = NULL;
801 }
802
803 if (!n || !n[0]) {
804 log_syntax(unit, LOG_ERR, filename, line, 0,
805 "Empty executable name or zeroeth argument%s: %s",
806 ignore ? ", ignoring" : "", rvalue);
807 return ignore ? 0 : -ENOEXEC;
808 }
809
810 nce = new0(ExecCommand, 1);
811 if (!nce)
812 return log_oom();
813
814 nce->argv = TAKE_PTR(n);
815 nce->path = TAKE_PTR(path);
816 nce->flags = flags;
817
818 exec_command_append_list(e, nce);
819
820 /* Do not _cleanup_free_ these. */
821 nce = NULL;
822
823 rvalue = p;
824 } while (semicolon);
825
826 return 0;
827 }
828
829 int config_parse_socket_bindtodevice(
830 const char* unit,
831 const char *filename,
832 unsigned line,
833 const char *section,
834 unsigned section_line,
835 const char *lvalue,
836 int ltype,
837 const char *rvalue,
838 void *data,
839 void *userdata) {
840
841 Socket *s = data;
842
843 assert(filename);
844 assert(lvalue);
845 assert(rvalue);
846 assert(data);
847
848 if (isempty(rvalue) || streq(rvalue, "*")) {
849 s->bind_to_device = mfree(s->bind_to_device);
850 return 0;
851 }
852
853 if (!ifname_valid(rvalue)) {
854 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid interface name, ignoring: %s", rvalue);
855 return 0;
856 }
857
858 if (free_and_strdup(&s->bind_to_device, rvalue) < 0)
859 return log_oom();
860
861 return 0;
862 }
863
864 int config_parse_exec_input(
865 const char *unit,
866 const char *filename,
867 unsigned line,
868 const char *section,
869 unsigned section_line,
870 const char *lvalue,
871 int ltype,
872 const char *rvalue,
873 void *data,
874 void *userdata) {
875
876 ExecContext *c = data;
877 const Unit *u = userdata;
878 const char *n;
879 ExecInput ei;
880 int r;
881
882 assert(data);
883 assert(filename);
884 assert(line);
885 assert(rvalue);
886
887 n = startswith(rvalue, "fd:");
888 if (n) {
889 _cleanup_free_ char *resolved = NULL;
890
891 r = unit_full_printf(u, n, &resolved);
892 if (r < 0)
893 return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s': %m", n);
894
895 if (isempty(resolved))
896 resolved = mfree(resolved);
897 else if (!fdname_is_valid(resolved)) {
898 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid file descriptor name: %s", resolved);
899 return -ENOEXEC;
900 }
901
902 free_and_replace(c->stdio_fdname[STDIN_FILENO], resolved);
903
904 ei = EXEC_INPUT_NAMED_FD;
905
906 } else if ((n = startswith(rvalue, "file:"))) {
907 _cleanup_free_ char *resolved = NULL;
908
909 r = unit_full_printf(u, n, &resolved);
910 if (r < 0)
911 return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s': %m", n);
912
913 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE | PATH_CHECK_FATAL, unit, filename, line, lvalue);
914 if (r < 0)
915 return -ENOEXEC;
916
917 free_and_replace(c->stdio_file[STDIN_FILENO], resolved);
918
919 ei = EXEC_INPUT_FILE;
920
921 } else {
922 ei = exec_input_from_string(rvalue);
923 if (ei < 0) {
924 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse input specifier, ignoring: %s", rvalue);
925 return 0;
926 }
927 }
928
929 c->std_input = ei;
930 return 0;
931 }
932
933 int config_parse_exec_input_text(
934 const char *unit,
935 const char *filename,
936 unsigned line,
937 const char *section,
938 unsigned section_line,
939 const char *lvalue,
940 int ltype,
941 const char *rvalue,
942 void *data,
943 void *userdata) {
944
945 _cleanup_free_ char *unescaped = NULL, *resolved = NULL;
946 ExecContext *c = data;
947 const Unit *u = userdata;
948 size_t sz;
949 void *p;
950 int r;
951
952 assert(data);
953 assert(filename);
954 assert(line);
955 assert(rvalue);
956
957 if (isempty(rvalue)) {
958 /* Reset if the empty string is assigned */
959 c->stdin_data = mfree(c->stdin_data);
960 c->stdin_data_size = 0;
961 return 0;
962 }
963
964 r = cunescape(rvalue, 0, &unescaped);
965 if (r < 0)
966 return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to decode C escaped text '%s': %m", rvalue);
967
968 r = unit_full_printf(u, unescaped, &resolved);
969 if (r < 0)
970 return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s': %m", unescaped);
971
972 sz = strlen(resolved);
973 if (c->stdin_data_size + sz + 1 < c->stdin_data_size || /* check for overflow */
974 c->stdin_data_size + sz + 1 > EXEC_STDIN_DATA_MAX) {
975 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);
976 return -E2BIG;
977 }
978
979 p = realloc(c->stdin_data, c->stdin_data_size + sz + 1);
980 if (!p)
981 return log_oom();
982
983 *((char*) mempcpy((char*) p + c->stdin_data_size, resolved, sz)) = '\n';
984
985 c->stdin_data = p;
986 c->stdin_data_size += sz + 1;
987
988 return 0;
989 }
990
991 int config_parse_exec_input_data(
992 const char *unit,
993 const char *filename,
994 unsigned line,
995 const char *section,
996 unsigned section_line,
997 const char *lvalue,
998 int ltype,
999 const char *rvalue,
1000 void *data,
1001 void *userdata) {
1002
1003 _cleanup_free_ void *p = NULL;
1004 ExecContext *c = data;
1005 size_t sz;
1006 void *q;
1007 int r;
1008
1009 assert(data);
1010 assert(filename);
1011 assert(line);
1012 assert(rvalue);
1013
1014 if (isempty(rvalue)) {
1015 /* Reset if the empty string is assigned */
1016 c->stdin_data = mfree(c->stdin_data);
1017 c->stdin_data_size = 0;
1018 return 0;
1019 }
1020
1021 r = unbase64mem(rvalue, (size_t) -1, &p, &sz);
1022 if (r < 0)
1023 return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to decode base64 data, ignoring: %s", rvalue);
1024
1025 assert(sz > 0);
1026
1027 if (c->stdin_data_size + sz < c->stdin_data_size || /* check for overflow */
1028 c->stdin_data_size + sz > EXEC_STDIN_DATA_MAX) {
1029 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);
1030 return -E2BIG;
1031 }
1032
1033 q = realloc(c->stdin_data, c->stdin_data_size + sz);
1034 if (!q)
1035 return log_oom();
1036
1037 memcpy((uint8_t*) q + c->stdin_data_size, p, sz);
1038
1039 c->stdin_data = q;
1040 c->stdin_data_size += sz;
1041
1042 return 0;
1043 }
1044
1045 int config_parse_exec_output(
1046 const char *unit,
1047 const char *filename,
1048 unsigned line,
1049 const char *section,
1050 unsigned section_line,
1051 const char *lvalue,
1052 int ltype,
1053 const char *rvalue,
1054 void *data,
1055 void *userdata) {
1056
1057 _cleanup_free_ char *resolved = NULL;
1058 const char *n;
1059 ExecContext *c = data;
1060 const Unit *u = userdata;
1061 ExecOutput eo;
1062 int r;
1063
1064 assert(data);
1065 assert(filename);
1066 assert(line);
1067 assert(lvalue);
1068 assert(rvalue);
1069
1070 n = startswith(rvalue, "fd:");
1071 if (n) {
1072 r = unit_full_printf(u, n, &resolved);
1073 if (r < 0)
1074 return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s: %m", n);
1075
1076 if (isempty(resolved))
1077 resolved = mfree(resolved);
1078 else if (!fdname_is_valid(resolved)) {
1079 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid file descriptor name: %s", resolved);
1080 return -ENOEXEC;
1081 }
1082
1083 eo = EXEC_OUTPUT_NAMED_FD;
1084
1085 } else if ((n = startswith(rvalue, "file:"))) {
1086
1087 r = unit_full_printf(u, n, &resolved);
1088 if (r < 0)
1089 return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s: %m", n);
1090
1091 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE | PATH_CHECK_FATAL, unit, filename, line, lvalue);
1092 if (r < 0)
1093 return -ENOEXEC;
1094
1095 eo = EXEC_OUTPUT_FILE;
1096
1097 } else if ((n = startswith(rvalue, "append:"))) {
1098
1099 r = unit_full_printf(u, n, &resolved);
1100 if (r < 0)
1101 return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s: %m", n);
1102
1103 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE | PATH_CHECK_FATAL, unit, filename, line, lvalue);
1104 if (r < 0)
1105 return -ENOEXEC;
1106
1107 eo = EXEC_OUTPUT_FILE_APPEND;
1108 } else {
1109 eo = exec_output_from_string(rvalue);
1110 if (eo < 0) {
1111 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse output specifier, ignoring: %s", rvalue);
1112 return 0;
1113 }
1114 }
1115
1116 if (streq(lvalue, "StandardOutput")) {
1117 if (eo == EXEC_OUTPUT_NAMED_FD)
1118 free_and_replace(c->stdio_fdname[STDOUT_FILENO], resolved);
1119 else
1120 free_and_replace(c->stdio_file[STDOUT_FILENO], resolved);
1121
1122 c->std_output = eo;
1123
1124 } else {
1125 assert(streq(lvalue, "StandardError"));
1126
1127 if (eo == EXEC_OUTPUT_NAMED_FD)
1128 free_and_replace(c->stdio_fdname[STDERR_FILENO], resolved);
1129 else
1130 free_and_replace(c->stdio_file[STDERR_FILENO], resolved);
1131
1132 c->std_error = eo;
1133 }
1134
1135 return 0;
1136 }
1137
1138 int config_parse_exec_io_class(const char *unit,
1139 const char *filename,
1140 unsigned line,
1141 const char *section,
1142 unsigned section_line,
1143 const char *lvalue,
1144 int ltype,
1145 const char *rvalue,
1146 void *data,
1147 void *userdata) {
1148
1149 ExecContext *c = data;
1150 int x;
1151
1152 assert(filename);
1153 assert(lvalue);
1154 assert(rvalue);
1155 assert(data);
1156
1157 if (isempty(rvalue)) {
1158 c->ioprio_set = false;
1159 c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0);
1160 return 0;
1161 }
1162
1163 x = ioprio_class_from_string(rvalue);
1164 if (x < 0) {
1165 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse IO scheduling class, ignoring: %s", rvalue);
1166 return 0;
1167 }
1168
1169 c->ioprio = IOPRIO_PRIO_VALUE(x, IOPRIO_PRIO_DATA(c->ioprio));
1170 c->ioprio_set = true;
1171
1172 return 0;
1173 }
1174
1175 int config_parse_exec_io_priority(const char *unit,
1176 const char *filename,
1177 unsigned line,
1178 const char *section,
1179 unsigned section_line,
1180 const char *lvalue,
1181 int ltype,
1182 const char *rvalue,
1183 void *data,
1184 void *userdata) {
1185
1186 ExecContext *c = data;
1187 int i, r;
1188
1189 assert(filename);
1190 assert(lvalue);
1191 assert(rvalue);
1192 assert(data);
1193
1194 if (isempty(rvalue)) {
1195 c->ioprio_set = false;
1196 c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0);
1197 return 0;
1198 }
1199
1200 r = ioprio_parse_priority(rvalue, &i);
1201 if (r < 0) {
1202 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse IO priority, ignoring: %s", rvalue);
1203 return 0;
1204 }
1205
1206 c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_PRIO_CLASS(c->ioprio), i);
1207 c->ioprio_set = true;
1208
1209 return 0;
1210 }
1211
1212 int config_parse_exec_cpu_sched_policy(const char *unit,
1213 const char *filename,
1214 unsigned line,
1215 const char *section,
1216 unsigned section_line,
1217 const char *lvalue,
1218 int ltype,
1219 const char *rvalue,
1220 void *data,
1221 void *userdata) {
1222
1223 ExecContext *c = data;
1224 int x;
1225
1226 assert(filename);
1227 assert(lvalue);
1228 assert(rvalue);
1229 assert(data);
1230
1231 if (isempty(rvalue)) {
1232 c->cpu_sched_set = false;
1233 c->cpu_sched_policy = SCHED_OTHER;
1234 c->cpu_sched_priority = 0;
1235 return 0;
1236 }
1237
1238 x = sched_policy_from_string(rvalue);
1239 if (x < 0) {
1240 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse CPU scheduling policy, ignoring: %s", rvalue);
1241 return 0;
1242 }
1243
1244 c->cpu_sched_policy = x;
1245 /* Moving to or from real-time policy? We need to adjust the priority */
1246 c->cpu_sched_priority = CLAMP(c->cpu_sched_priority, sched_get_priority_min(x), sched_get_priority_max(x));
1247 c->cpu_sched_set = true;
1248
1249 return 0;
1250 }
1251
1252 int config_parse_numa_mask(const char *unit,
1253 const char *filename,
1254 unsigned line,
1255 const char *section,
1256 unsigned section_line,
1257 const char *lvalue,
1258 int ltype,
1259 const char *rvalue,
1260 void *data,
1261 void *userdata) {
1262 int r;
1263 NUMAPolicy *p = data;
1264
1265 assert(filename);
1266 assert(lvalue);
1267 assert(rvalue);
1268 assert(data);
1269
1270 r = parse_cpu_set_extend(rvalue, &p->nodes, true, unit, filename, line, lvalue);
1271 if (r < 0) {
1272 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse NUMA node mask, ignoring: %s", rvalue);
1273 return 0;
1274 }
1275
1276 return r;
1277 }
1278
1279 int config_parse_exec_cpu_sched_prio(const char *unit,
1280 const char *filename,
1281 unsigned line,
1282 const char *section,
1283 unsigned section_line,
1284 const char *lvalue,
1285 int ltype,
1286 const char *rvalue,
1287 void *data,
1288 void *userdata) {
1289
1290 ExecContext *c = data;
1291 int i, min, max, r;
1292
1293 assert(filename);
1294 assert(lvalue);
1295 assert(rvalue);
1296 assert(data);
1297
1298 r = safe_atoi(rvalue, &i);
1299 if (r < 0) {
1300 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse CPU scheduling priority, ignoring: %s", rvalue);
1301 return 0;
1302 }
1303
1304 /* On Linux RR/FIFO range from 1 to 99 and OTHER/BATCH may only be 0 */
1305 min = sched_get_priority_min(c->cpu_sched_policy);
1306 max = sched_get_priority_max(c->cpu_sched_policy);
1307
1308 if (i < min || i > max) {
1309 log_syntax(unit, LOG_ERR, filename, line, 0, "CPU scheduling priority is out of range, ignoring: %s", rvalue);
1310 return 0;
1311 }
1312
1313 c->cpu_sched_priority = i;
1314 c->cpu_sched_set = true;
1315
1316 return 0;
1317 }
1318
1319 int config_parse_exec_cpu_affinity(const char *unit,
1320 const char *filename,
1321 unsigned line,
1322 const char *section,
1323 unsigned section_line,
1324 const char *lvalue,
1325 int ltype,
1326 const char *rvalue,
1327 void *data,
1328 void *userdata) {
1329
1330 ExecContext *c = data;
1331
1332 assert(filename);
1333 assert(lvalue);
1334 assert(rvalue);
1335 assert(data);
1336
1337 return parse_cpu_set_extend(rvalue, &c->cpu_set, true, unit, filename, line, lvalue);
1338 }
1339
1340 int config_parse_capability_set(
1341 const char *unit,
1342 const char *filename,
1343 unsigned line,
1344 const char *section,
1345 unsigned section_line,
1346 const char *lvalue,
1347 int ltype,
1348 const char *rvalue,
1349 void *data,
1350 void *userdata) {
1351
1352 uint64_t *capability_set = data;
1353 uint64_t sum = 0, initial = 0;
1354 bool invert = false;
1355 int r;
1356
1357 assert(filename);
1358 assert(lvalue);
1359 assert(rvalue);
1360 assert(data);
1361
1362 if (rvalue[0] == '~') {
1363 invert = true;
1364 rvalue++;
1365 }
1366
1367 if (streq(lvalue, "CapabilityBoundingSet"))
1368 initial = CAP_ALL; /* initialized to all bits on */
1369 /* else "AmbientCapabilities" initialized to all bits off */
1370
1371 r = capability_set_from_string(rvalue, &sum);
1372 if (r < 0) {
1373 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse %s= specifier '%s', ignoring: %m", lvalue, rvalue);
1374 return 0;
1375 }
1376
1377 if (sum == 0 || *capability_set == initial)
1378 /* "", "~" or uninitialized data -> replace */
1379 *capability_set = invert ? ~sum : sum;
1380 else {
1381 /* previous data -> merge */
1382 if (invert)
1383 *capability_set &= ~sum;
1384 else
1385 *capability_set |= sum;
1386 }
1387
1388 return 0;
1389 }
1390
1391 int config_parse_exec_selinux_context(
1392 const char *unit,
1393 const char *filename,
1394 unsigned line,
1395 const char *section,
1396 unsigned section_line,
1397 const char *lvalue,
1398 int ltype,
1399 const char *rvalue,
1400 void *data,
1401 void *userdata) {
1402
1403 ExecContext *c = data;
1404 const Unit *u = userdata;
1405 bool ignore;
1406 char *k;
1407 int r;
1408
1409 assert(filename);
1410 assert(lvalue);
1411 assert(rvalue);
1412 assert(data);
1413
1414 if (isempty(rvalue)) {
1415 c->selinux_context = mfree(c->selinux_context);
1416 c->selinux_context_ignore = false;
1417 return 0;
1418 }
1419
1420 if (rvalue[0] == '-') {
1421 ignore = true;
1422 rvalue++;
1423 } else
1424 ignore = false;
1425
1426 r = unit_full_printf(u, rvalue, &k);
1427 if (r < 0) {
1428 log_syntax(unit, LOG_ERR, filename, line, r,
1429 "Failed to resolve unit specifiers in '%s'%s: %m",
1430 rvalue, ignore ? ", ignoring" : "");
1431 return ignore ? 0 : -ENOEXEC;
1432 }
1433
1434 free_and_replace(c->selinux_context, k);
1435 c->selinux_context_ignore = ignore;
1436
1437 return 0;
1438 }
1439
1440 int config_parse_exec_apparmor_profile(
1441 const char *unit,
1442 const char *filename,
1443 unsigned line,
1444 const char *section,
1445 unsigned section_line,
1446 const char *lvalue,
1447 int ltype,
1448 const char *rvalue,
1449 void *data,
1450 void *userdata) {
1451
1452 ExecContext *c = data;
1453 const Unit *u = userdata;
1454 bool ignore;
1455 char *k;
1456 int r;
1457
1458 assert(filename);
1459 assert(lvalue);
1460 assert(rvalue);
1461 assert(data);
1462
1463 if (isempty(rvalue)) {
1464 c->apparmor_profile = mfree(c->apparmor_profile);
1465 c->apparmor_profile_ignore = false;
1466 return 0;
1467 }
1468
1469 if (rvalue[0] == '-') {
1470 ignore = true;
1471 rvalue++;
1472 } else
1473 ignore = false;
1474
1475 r = unit_full_printf(u, rvalue, &k);
1476 if (r < 0) {
1477 log_syntax(unit, LOG_ERR, filename, line, r,
1478 "Failed to resolve unit specifiers in '%s'%s: %m",
1479 rvalue, ignore ? ", ignoring" : "");
1480 return ignore ? 0 : -ENOEXEC;
1481 }
1482
1483 free_and_replace(c->apparmor_profile, k);
1484 c->apparmor_profile_ignore = ignore;
1485
1486 return 0;
1487 }
1488
1489 int config_parse_exec_smack_process_label(
1490 const char *unit,
1491 const char *filename,
1492 unsigned line,
1493 const char *section,
1494 unsigned section_line,
1495 const char *lvalue,
1496 int ltype,
1497 const char *rvalue,
1498 void *data,
1499 void *userdata) {
1500
1501 ExecContext *c = data;
1502 const Unit *u = userdata;
1503 bool ignore;
1504 char *k;
1505 int r;
1506
1507 assert(filename);
1508 assert(lvalue);
1509 assert(rvalue);
1510 assert(data);
1511
1512 if (isempty(rvalue)) {
1513 c->smack_process_label = mfree(c->smack_process_label);
1514 c->smack_process_label_ignore = false;
1515 return 0;
1516 }
1517
1518 if (rvalue[0] == '-') {
1519 ignore = true;
1520 rvalue++;
1521 } else
1522 ignore = false;
1523
1524 r = unit_full_printf(u, rvalue, &k);
1525 if (r < 0) {
1526 log_syntax(unit, LOG_ERR, filename, line, r,
1527 "Failed to resolve unit specifiers in '%s'%s: %m",
1528 rvalue, ignore ? ", ignoring" : "");
1529 return ignore ? 0 : -ENOEXEC;
1530 }
1531
1532 free_and_replace(c->smack_process_label, k);
1533 c->smack_process_label_ignore = ignore;
1534
1535 return 0;
1536 }
1537
1538 int config_parse_timer(
1539 const char *unit,
1540 const char *filename,
1541 unsigned line,
1542 const char *section,
1543 unsigned section_line,
1544 const char *lvalue,
1545 int ltype,
1546 const char *rvalue,
1547 void *data,
1548 void *userdata) {
1549
1550 _cleanup_(calendar_spec_freep) CalendarSpec *c = NULL;
1551 _cleanup_free_ char *k = NULL;
1552 const Unit *u = userdata;
1553 Timer *t = data;
1554 usec_t usec = 0;
1555 TimerValue *v;
1556 int r;
1557
1558 assert(filename);
1559 assert(lvalue);
1560 assert(rvalue);
1561 assert(data);
1562
1563 if (isempty(rvalue)) {
1564 /* Empty assignment resets list */
1565 timer_free_values(t);
1566 return 0;
1567 }
1568
1569 r = unit_full_printf(u, rvalue, &k);
1570 if (r < 0) {
1571 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
1572 return 0;
1573 }
1574
1575 if (ltype == TIMER_CALENDAR) {
1576 r = calendar_spec_from_string(k, &c);
1577 if (r < 0) {
1578 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse calendar specification, ignoring: %s", k);
1579 return 0;
1580 }
1581 } else {
1582 r = parse_sec(k, &usec);
1583 if (r < 0) {
1584 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse timer value, ignoring: %s", k);
1585 return 0;
1586 }
1587 }
1588
1589 v = new(TimerValue, 1);
1590 if (!v)
1591 return log_oom();
1592
1593 *v = (TimerValue) {
1594 .base = ltype,
1595 .value = usec,
1596 .calendar_spec = TAKE_PTR(c),
1597 };
1598
1599 LIST_PREPEND(value, t->values, v);
1600
1601 return 0;
1602 }
1603
1604 int config_parse_trigger_unit(
1605 const char *unit,
1606 const char *filename,
1607 unsigned line,
1608 const char *section,
1609 unsigned section_line,
1610 const char *lvalue,
1611 int ltype,
1612 const char *rvalue,
1613 void *data,
1614 void *userdata) {
1615
1616 _cleanup_free_ char *p = NULL;
1617 Unit *u = data;
1618 UnitType type;
1619 int r;
1620
1621 assert(filename);
1622 assert(lvalue);
1623 assert(rvalue);
1624 assert(data);
1625
1626 if (!hashmap_isempty(u->dependencies[UNIT_TRIGGERS])) {
1627 log_syntax(unit, LOG_ERR, filename, line, 0, "Multiple units to trigger specified, ignoring: %s", rvalue);
1628 return 0;
1629 }
1630
1631 r = unit_name_printf(u, rvalue, &p);
1632 if (r < 0) {
1633 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue);
1634 return 0;
1635 }
1636
1637 type = unit_name_to_type(p);
1638 if (type < 0) {
1639 log_syntax(unit, LOG_ERR, filename, line, 0, "Unit type not valid, ignoring: %s", rvalue);
1640 return 0;
1641 }
1642 if (unit_has_name(u, p)) {
1643 log_syntax(unit, LOG_ERR, filename, line, 0, "Units cannot trigger themselves, ignoring: %s", rvalue);
1644 return 0;
1645 }
1646
1647 r = unit_add_two_dependencies_by_name(u, UNIT_BEFORE, UNIT_TRIGGERS, p, true, UNIT_DEPENDENCY_FILE);
1648 if (r < 0) {
1649 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to add trigger on %s, ignoring: %m", p);
1650 return 0;
1651 }
1652
1653 return 0;
1654 }
1655
1656 int config_parse_path_spec(const char *unit,
1657 const char *filename,
1658 unsigned line,
1659 const char *section,
1660 unsigned section_line,
1661 const char *lvalue,
1662 int ltype,
1663 const char *rvalue,
1664 void *data,
1665 void *userdata) {
1666
1667 Path *p = data;
1668 PathSpec *s;
1669 PathType b;
1670 _cleanup_free_ char *k = NULL;
1671 int r;
1672
1673 assert(filename);
1674 assert(lvalue);
1675 assert(rvalue);
1676 assert(data);
1677
1678 if (isempty(rvalue)) {
1679 /* Empty assignment clears list */
1680 path_free_specs(p);
1681 return 0;
1682 }
1683
1684 b = path_type_from_string(lvalue);
1685 if (b < 0) {
1686 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse path type, ignoring: %s", lvalue);
1687 return 0;
1688 }
1689
1690 r = unit_full_printf(UNIT(p), rvalue, &k);
1691 if (r < 0) {
1692 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue);
1693 return 0;
1694 }
1695
1696 r = path_simplify_and_warn(k, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
1697 if (r < 0)
1698 return 0;
1699
1700 s = new0(PathSpec, 1);
1701 if (!s)
1702 return log_oom();
1703
1704 s->unit = UNIT(p);
1705 s->path = TAKE_PTR(k);
1706 s->type = b;
1707 s->inotify_fd = -1;
1708
1709 LIST_PREPEND(spec, p->specs, s);
1710
1711 return 0;
1712 }
1713
1714 int config_parse_socket_service(
1715 const char *unit,
1716 const char *filename,
1717 unsigned line,
1718 const char *section,
1719 unsigned section_line,
1720 const char *lvalue,
1721 int ltype,
1722 const char *rvalue,
1723 void *data,
1724 void *userdata) {
1725
1726 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1727 _cleanup_free_ char *p = NULL;
1728 Socket *s = data;
1729 Unit *x;
1730 int r;
1731
1732 assert(filename);
1733 assert(lvalue);
1734 assert(rvalue);
1735 assert(data);
1736
1737 r = unit_name_printf(UNIT(s), rvalue, &p);
1738 if (r < 0) {
1739 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s: %m", rvalue);
1740 return -ENOEXEC;
1741 }
1742
1743 if (!endswith(p, ".service")) {
1744 log_syntax(unit, LOG_ERR, filename, line, 0, "Unit must be of type service: %s", rvalue);
1745 return -ENOEXEC;
1746 }
1747
1748 r = manager_load_unit(UNIT(s)->manager, p, NULL, &error, &x);
1749 if (r < 0) {
1750 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to load unit %s: %s", rvalue, bus_error_message(&error, r));
1751 return -ENOEXEC;
1752 }
1753
1754 unit_ref_set(&s->service, UNIT(s), x);
1755
1756 return 0;
1757 }
1758
1759 int config_parse_fdname(
1760 const char *unit,
1761 const char *filename,
1762 unsigned line,
1763 const char *section,
1764 unsigned section_line,
1765 const char *lvalue,
1766 int ltype,
1767 const char *rvalue,
1768 void *data,
1769 void *userdata) {
1770
1771 _cleanup_free_ char *p = NULL;
1772 Socket *s = data;
1773 int r;
1774
1775 assert(filename);
1776 assert(lvalue);
1777 assert(rvalue);
1778 assert(data);
1779
1780 if (isempty(rvalue)) {
1781 s->fdname = mfree(s->fdname);
1782 return 0;
1783 }
1784
1785 r = unit_full_printf(UNIT(s), rvalue, &p);
1786 if (r < 0) {
1787 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
1788 return 0;
1789 }
1790
1791 if (!fdname_is_valid(p)) {
1792 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid file descriptor name, ignoring: %s", p);
1793 return 0;
1794 }
1795
1796 return free_and_replace(s->fdname, p);
1797 }
1798
1799 int config_parse_service_sockets(
1800 const char *unit,
1801 const char *filename,
1802 unsigned line,
1803 const char *section,
1804 unsigned section_line,
1805 const char *lvalue,
1806 int ltype,
1807 const char *rvalue,
1808 void *data,
1809 void *userdata) {
1810
1811 Service *s = data;
1812 const char *p;
1813 int r;
1814
1815 assert(filename);
1816 assert(lvalue);
1817 assert(rvalue);
1818 assert(data);
1819
1820 p = rvalue;
1821 for (;;) {
1822 _cleanup_free_ char *word = NULL, *k = NULL;
1823
1824 r = extract_first_word(&p, &word, NULL, 0);
1825 if (r == 0)
1826 break;
1827 if (r == -ENOMEM)
1828 return log_oom();
1829 if (r < 0) {
1830 log_syntax(unit, LOG_ERR, filename, line, r, "Trailing garbage in sockets, ignoring: %s", rvalue);
1831 break;
1832 }
1833
1834 r = unit_name_printf(UNIT(s), word, &k);
1835 if (r < 0) {
1836 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", word);
1837 continue;
1838 }
1839
1840 if (!endswith(k, ".socket")) {
1841 log_syntax(unit, LOG_ERR, filename, line, 0, "Unit must be of type socket, ignoring: %s", k);
1842 continue;
1843 }
1844
1845 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_WANTS, UNIT_AFTER, k, true, UNIT_DEPENDENCY_FILE);
1846 if (r < 0)
1847 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to add dependency on %s, ignoring: %m", k);
1848
1849 r = unit_add_dependency_by_name(UNIT(s), UNIT_TRIGGERED_BY, k, true, UNIT_DEPENDENCY_FILE);
1850 if (r < 0)
1851 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to add dependency on %s, ignoring: %m", k);
1852 }
1853
1854 return 0;
1855 }
1856
1857 int config_parse_bus_name(
1858 const char *unit,
1859 const char *filename,
1860 unsigned line,
1861 const char *section,
1862 unsigned section_line,
1863 const char *lvalue,
1864 int ltype,
1865 const char *rvalue,
1866 void *data,
1867 void *userdata) {
1868
1869 _cleanup_free_ char *k = NULL;
1870 const Unit *u = userdata;
1871 int r;
1872
1873 assert(filename);
1874 assert(lvalue);
1875 assert(rvalue);
1876 assert(u);
1877
1878 r = unit_full_printf(u, rvalue, &k);
1879 if (r < 0) {
1880 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue);
1881 return 0;
1882 }
1883
1884 if (!service_name_is_valid(k)) {
1885 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid bus name, ignoring: %s", k);
1886 return 0;
1887 }
1888
1889 return config_parse_string(unit, filename, line, section, section_line, lvalue, ltype, k, data, userdata);
1890 }
1891
1892 int config_parse_service_timeout(
1893 const char *unit,
1894 const char *filename,
1895 unsigned line,
1896 const char *section,
1897 unsigned section_line,
1898 const char *lvalue,
1899 int ltype,
1900 const char *rvalue,
1901 void *data,
1902 void *userdata) {
1903
1904 Service *s = userdata;
1905 usec_t usec;
1906 int r;
1907
1908 assert(filename);
1909 assert(lvalue);
1910 assert(rvalue);
1911 assert(s);
1912
1913 /* This is called for two cases: TimeoutSec= and TimeoutStartSec=. */
1914
1915 /* Traditionally, these options accepted 0 to disable the timeouts. However, a timeout of 0 suggests it happens
1916 * immediately, hence fix this to become USEC_INFINITY instead. This is in-line with how we internally handle
1917 * all other timeouts. */
1918 r = parse_sec_fix_0(rvalue, &usec);
1919 if (r < 0) {
1920 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse %s= parameter, ignoring: %s", lvalue, rvalue);
1921 return 0;
1922 }
1923
1924 s->start_timeout_defined = true;
1925 s->timeout_start_usec = usec;
1926
1927 if (streq(lvalue, "TimeoutSec"))
1928 s->timeout_stop_usec = usec;
1929
1930 return 0;
1931 }
1932
1933 int config_parse_timeout_abort(
1934 const char *unit,
1935 const char *filename,
1936 unsigned line,
1937 const char *section,
1938 unsigned section_line,
1939 const char *lvalue,
1940 int ltype,
1941 const char *rvalue,
1942 void *data,
1943 void *userdata) {
1944
1945 usec_t *ret = data;
1946 int r;
1947
1948 assert(filename);
1949 assert(lvalue);
1950 assert(rvalue);
1951 assert(ret);
1952
1953 /* Note: apart from setting the arg, this returns an extra bit of information in the return value. */
1954
1955 if (isempty(rvalue)) {
1956 *ret = 0;
1957 return 0; /* "not set" */
1958 }
1959
1960 r = parse_sec(rvalue, ret);
1961 if (r < 0)
1962 return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse %s= setting, ignoring: %s", lvalue, rvalue);
1963
1964 return 1; /* "set" */
1965 }
1966
1967 int config_parse_service_timeout_abort(
1968 const char *unit,
1969 const char *filename,
1970 unsigned line,
1971 const char *section,
1972 unsigned section_line,
1973 const char *lvalue,
1974 int ltype,
1975 const char *rvalue,
1976 void *data,
1977 void *userdata) {
1978
1979 Service *s = userdata;
1980 int r;
1981
1982 assert(s);
1983
1984 r = config_parse_timeout_abort(unit, filename, line, section, section_line, lvalue, ltype, rvalue,
1985 &s->timeout_abort_usec, s);
1986 if (r >= 0)
1987 s->timeout_abort_set = r;
1988 return 0;
1989 }
1990
1991 int config_parse_sec_fix_0(
1992 const char *unit,
1993 const char *filename,
1994 unsigned line,
1995 const char *section,
1996 unsigned section_line,
1997 const char *lvalue,
1998 int ltype,
1999 const char *rvalue,
2000 void *data,
2001 void *userdata) {
2002
2003 usec_t *usec = data;
2004 int r;
2005
2006 assert(filename);
2007 assert(lvalue);
2008 assert(rvalue);
2009 assert(usec);
2010
2011 /* This is pretty much like config_parse_sec(), except that this treats a time of 0 as infinity, for
2012 * compatibility with older versions of systemd where 0 instead of infinity was used as indicator to turn off a
2013 * timeout. */
2014
2015 r = parse_sec_fix_0(rvalue, usec);
2016 if (r < 0) {
2017 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse %s= parameter, ignoring: %s", lvalue, rvalue);
2018 return 0;
2019 }
2020
2021 return 0;
2022 }
2023
2024 int config_parse_user_group_compat(
2025 const char *unit,
2026 const char *filename,
2027 unsigned line,
2028 const char *section,
2029 unsigned section_line,
2030 const char *lvalue,
2031 int ltype,
2032 const char *rvalue,
2033 void *data,
2034 void *userdata) {
2035
2036 _cleanup_free_ char *k = NULL;
2037 char **user = data;
2038 const Unit *u = userdata;
2039 int r;
2040
2041 assert(filename);
2042 assert(lvalue);
2043 assert(rvalue);
2044 assert(u);
2045
2046 if (isempty(rvalue)) {
2047 *user = mfree(*user);
2048 return 0;
2049 }
2050
2051 r = unit_full_printf(u, rvalue, &k);
2052 if (r < 0) {
2053 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s: %m", rvalue);
2054 return -ENOEXEC;
2055 }
2056
2057 if (!valid_user_group_name_or_id_compat(k)) {
2058 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid user/group name or numeric ID: %s", k);
2059 return -ENOEXEC;
2060 }
2061
2062 return free_and_replace(*user, k);
2063 }
2064
2065 int config_parse_user_group_strv_compat(
2066 const char *unit,
2067 const char *filename,
2068 unsigned line,
2069 const char *section,
2070 unsigned section_line,
2071 const char *lvalue,
2072 int ltype,
2073 const char *rvalue,
2074 void *data,
2075 void *userdata) {
2076
2077 char ***users = data;
2078 const Unit *u = userdata;
2079 const char *p = rvalue;
2080 int r;
2081
2082 assert(filename);
2083 assert(lvalue);
2084 assert(rvalue);
2085 assert(u);
2086
2087 if (isempty(rvalue)) {
2088 *users = strv_free(*users);
2089 return 0;
2090 }
2091
2092 for (;;) {
2093 _cleanup_free_ char *word = NULL, *k = NULL;
2094
2095 r = extract_first_word(&p, &word, NULL, 0);
2096 if (r == 0)
2097 break;
2098 if (r == -ENOMEM)
2099 return log_oom();
2100 if (r < 0) {
2101 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid syntax: %s", rvalue);
2102 return -ENOEXEC;
2103 }
2104
2105 r = unit_full_printf(u, word, &k);
2106 if (r < 0) {
2107 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s: %m", word);
2108 return -ENOEXEC;
2109 }
2110
2111 if (!valid_user_group_name_or_id_compat(k)) {
2112 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid user/group name or numeric ID: %s", k);
2113 return -ENOEXEC;
2114 }
2115
2116 r = strv_push(users, k);
2117 if (r < 0)
2118 return log_oom();
2119
2120 k = NULL;
2121 }
2122
2123 return 0;
2124 }
2125
2126 int config_parse_working_directory(
2127 const char *unit,
2128 const char *filename,
2129 unsigned line,
2130 const char *section,
2131 unsigned section_line,
2132 const char *lvalue,
2133 int ltype,
2134 const char *rvalue,
2135 void *data,
2136 void *userdata) {
2137
2138 ExecContext *c = data;
2139 const Unit *u = userdata;
2140 bool missing_ok;
2141 int r;
2142
2143 assert(filename);
2144 assert(lvalue);
2145 assert(rvalue);
2146 assert(c);
2147 assert(u);
2148
2149 if (isempty(rvalue)) {
2150 c->working_directory_home = false;
2151 c->working_directory = mfree(c->working_directory);
2152 return 0;
2153 }
2154
2155 if (rvalue[0] == '-') {
2156 missing_ok = true;
2157 rvalue++;
2158 } else
2159 missing_ok = false;
2160
2161 if (streq(rvalue, "~")) {
2162 c->working_directory_home = true;
2163 c->working_directory = mfree(c->working_directory);
2164 } else {
2165 _cleanup_free_ char *k = NULL;
2166
2167 r = unit_full_printf(u, rvalue, &k);
2168 if (r < 0) {
2169 log_syntax(unit, LOG_ERR, filename, line, r,
2170 "Failed to resolve unit specifiers in working directory path '%s'%s: %m",
2171 rvalue, missing_ok ? ", ignoring" : "");
2172 return missing_ok ? 0 : -ENOEXEC;
2173 }
2174
2175 r = path_simplify_and_warn(k, PATH_CHECK_ABSOLUTE | (missing_ok ? 0 : PATH_CHECK_FATAL), unit, filename, line, lvalue);
2176 if (r < 0)
2177 return missing_ok ? 0 : -ENOEXEC;
2178
2179 c->working_directory_home = false;
2180 free_and_replace(c->working_directory, k);
2181 }
2182
2183 c->working_directory_missing_ok = missing_ok;
2184 return 0;
2185 }
2186
2187 int config_parse_unit_env_file(const char *unit,
2188 const char *filename,
2189 unsigned line,
2190 const char *section,
2191 unsigned section_line,
2192 const char *lvalue,
2193 int ltype,
2194 const char *rvalue,
2195 void *data,
2196 void *userdata) {
2197
2198 char ***env = data;
2199 const Unit *u = userdata;
2200 _cleanup_free_ char *n = NULL;
2201 int r;
2202
2203 assert(filename);
2204 assert(lvalue);
2205 assert(rvalue);
2206 assert(data);
2207
2208 if (isempty(rvalue)) {
2209 /* Empty assignment frees the list */
2210 *env = strv_free(*env);
2211 return 0;
2212 }
2213
2214 r = unit_full_printf(u, rvalue, &n);
2215 if (r < 0) {
2216 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
2217 return 0;
2218 }
2219
2220 r = path_simplify_and_warn(n[0] == '-' ? n + 1 : n, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
2221 if (r < 0)
2222 return 0;
2223
2224 r = strv_push(env, n);
2225 if (r < 0)
2226 return log_oom();
2227
2228 n = NULL;
2229
2230 return 0;
2231 }
2232
2233 int config_parse_environ(
2234 const char *unit,
2235 const char *filename,
2236 unsigned line,
2237 const char *section,
2238 unsigned section_line,
2239 const char *lvalue,
2240 int ltype,
2241 const char *rvalue,
2242 void *data,
2243 void *userdata) {
2244
2245 const Unit *u = userdata;
2246 char ***env = data;
2247 const char *p;
2248 int r;
2249
2250 assert(filename);
2251 assert(lvalue);
2252 assert(rvalue);
2253 assert(data);
2254
2255 if (isempty(rvalue)) {
2256 /* Empty assignment resets the list */
2257 *env = strv_free(*env);
2258 return 0;
2259 }
2260
2261 for (p = rvalue;; ) {
2262 _cleanup_free_ char *word = NULL, *k = NULL;
2263
2264 r = extract_first_word(&p, &word, NULL, EXTRACT_CUNESCAPE|EXTRACT_UNQUOTE);
2265 if (r == 0)
2266 return 0;
2267 if (r == -ENOMEM)
2268 return log_oom();
2269 if (r < 0) {
2270 log_syntax(unit, LOG_WARNING, filename, line, r,
2271 "Invalid syntax, ignoring: %s", rvalue);
2272 return 0;
2273 }
2274
2275 if (u) {
2276 r = unit_full_printf(u, word, &k);
2277 if (r < 0) {
2278 log_syntax(unit, LOG_ERR, filename, line, r,
2279 "Failed to resolve unit specifiers in %s, ignoring: %m", word);
2280 continue;
2281 }
2282 } else
2283 k = TAKE_PTR(word);
2284
2285 if (!env_assignment_is_valid(k)) {
2286 log_syntax(unit, LOG_ERR, filename, line, 0,
2287 "Invalid environment assignment, ignoring: %s", k);
2288 continue;
2289 }
2290
2291 r = strv_env_replace(env, k);
2292 if (r < 0)
2293 return log_oom();
2294
2295 k = NULL;
2296 }
2297 }
2298
2299 int config_parse_pass_environ(
2300 const char *unit,
2301 const char *filename,
2302 unsigned line,
2303 const char *section,
2304 unsigned section_line,
2305 const char *lvalue,
2306 int ltype,
2307 const char *rvalue,
2308 void *data,
2309 void *userdata) {
2310
2311 _cleanup_strv_free_ char **n = NULL;
2312 size_t nlen = 0, nbufsize = 0;
2313 char*** passenv = data;
2314 const char *p = rvalue;
2315 const Unit *u = userdata;
2316 int r;
2317
2318 assert(filename);
2319 assert(lvalue);
2320 assert(rvalue);
2321 assert(data);
2322
2323 if (isempty(rvalue)) {
2324 /* Empty assignment resets the list */
2325 *passenv = strv_free(*passenv);
2326 return 0;
2327 }
2328
2329 for (;;) {
2330 _cleanup_free_ char *word = NULL, *k = NULL;
2331
2332 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
2333 if (r == 0)
2334 break;
2335 if (r == -ENOMEM)
2336 return log_oom();
2337 if (r < 0) {
2338 log_syntax(unit, LOG_ERR, filename, line, r,
2339 "Trailing garbage in %s, ignoring: %s", lvalue, rvalue);
2340 break;
2341 }
2342
2343 if (u) {
2344 r = unit_full_printf(u, word, &k);
2345 if (r < 0) {
2346 log_syntax(unit, LOG_ERR, filename, line, r,
2347 "Failed to resolve specifiers in %s, ignoring: %m", word);
2348 continue;
2349 }
2350 } else
2351 k = TAKE_PTR(word);
2352
2353 if (!env_name_is_valid(k)) {
2354 log_syntax(unit, LOG_ERR, filename, line, 0,
2355 "Invalid environment name for %s, ignoring: %s", lvalue, k);
2356 continue;
2357 }
2358
2359 if (!GREEDY_REALLOC(n, nbufsize, nlen + 2))
2360 return log_oom();
2361
2362 n[nlen++] = TAKE_PTR(k);
2363 n[nlen] = NULL;
2364 }
2365
2366 if (n) {
2367 r = strv_extend_strv(passenv, n, true);
2368 if (r < 0)
2369 return r;
2370 }
2371
2372 return 0;
2373 }
2374
2375 int config_parse_unset_environ(
2376 const char *unit,
2377 const char *filename,
2378 unsigned line,
2379 const char *section,
2380 unsigned section_line,
2381 const char *lvalue,
2382 int ltype,
2383 const char *rvalue,
2384 void *data,
2385 void *userdata) {
2386
2387 _cleanup_strv_free_ char **n = NULL;
2388 size_t nlen = 0, nbufsize = 0;
2389 char*** unsetenv = data;
2390 const char *p = rvalue;
2391 const Unit *u = userdata;
2392 int r;
2393
2394 assert(filename);
2395 assert(lvalue);
2396 assert(rvalue);
2397 assert(data);
2398
2399 if (isempty(rvalue)) {
2400 /* Empty assignment resets the list */
2401 *unsetenv = strv_free(*unsetenv);
2402 return 0;
2403 }
2404
2405 for (;;) {
2406 _cleanup_free_ char *word = NULL, *k = NULL;
2407
2408 r = extract_first_word(&p, &word, NULL, EXTRACT_CUNESCAPE|EXTRACT_UNQUOTE);
2409 if (r == 0)
2410 break;
2411 if (r == -ENOMEM)
2412 return log_oom();
2413 if (r < 0) {
2414 log_syntax(unit, LOG_ERR, filename, line, r,
2415 "Trailing garbage in %s, ignoring: %s", lvalue, rvalue);
2416 break;
2417 }
2418
2419 if (u) {
2420 r = unit_full_printf(u, word, &k);
2421 if (r < 0) {
2422 log_syntax(unit, LOG_ERR, filename, line, r,
2423 "Failed to resolve unit specifiers in %s, ignoring: %m", word);
2424 continue;
2425 }
2426 } else
2427 k = TAKE_PTR(word);
2428
2429 if (!env_assignment_is_valid(k) && !env_name_is_valid(k)) {
2430 log_syntax(unit, LOG_ERR, filename, line, 0,
2431 "Invalid environment name or assignment %s, ignoring: %s", lvalue, k);
2432 continue;
2433 }
2434
2435 if (!GREEDY_REALLOC(n, nbufsize, nlen + 2))
2436 return log_oom();
2437
2438 n[nlen++] = TAKE_PTR(k);
2439 n[nlen] = NULL;
2440 }
2441
2442 if (n) {
2443 r = strv_extend_strv(unsetenv, n, true);
2444 if (r < 0)
2445 return r;
2446 }
2447
2448 return 0;
2449 }
2450
2451 int config_parse_log_extra_fields(
2452 const char *unit,
2453 const char *filename,
2454 unsigned line,
2455 const char *section,
2456 unsigned section_line,
2457 const char *lvalue,
2458 int ltype,
2459 const char *rvalue,
2460 void *data,
2461 void *userdata) {
2462
2463 ExecContext *c = data;
2464 const Unit *u = userdata;
2465 const char *p = rvalue;
2466 int r;
2467
2468 assert(filename);
2469 assert(lvalue);
2470 assert(rvalue);
2471 assert(c);
2472
2473 if (isempty(rvalue)) {
2474 exec_context_free_log_extra_fields(c);
2475 return 0;
2476 }
2477
2478 for (;;) {
2479 _cleanup_free_ char *word = NULL, *k = NULL;
2480 struct iovec *t;
2481 const char *eq;
2482
2483 r = extract_first_word(&p, &word, NULL, EXTRACT_CUNESCAPE|EXTRACT_UNQUOTE);
2484 if (r == 0)
2485 return 0;
2486 if (r == -ENOMEM)
2487 return log_oom();
2488 if (r < 0) {
2489 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
2490 return 0;
2491 }
2492
2493 r = unit_full_printf(u, word, &k);
2494 if (r < 0) {
2495 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", word);
2496 continue;
2497 }
2498
2499 eq = strchr(k, '=');
2500 if (!eq) {
2501 log_syntax(unit, LOG_ERR, filename, line, 0, "Log field lacks '=' character, ignoring: %s", k);
2502 continue;
2503 }
2504
2505 if (!journal_field_valid(k, eq-k, false)) {
2506 log_syntax(unit, LOG_ERR, filename, line, 0, "Log field name is invalid, ignoring: %s", k);
2507 continue;
2508 }
2509
2510 t = reallocarray(c->log_extra_fields, c->n_log_extra_fields+1, sizeof(struct iovec));
2511 if (!t)
2512 return log_oom();
2513
2514 c->log_extra_fields = t;
2515 c->log_extra_fields[c->n_log_extra_fields++] = IOVEC_MAKE_STRING(k);
2516
2517 k = NULL;
2518 }
2519 }
2520
2521 int config_parse_unit_condition_path(
2522 const char *unit,
2523 const char *filename,
2524 unsigned line,
2525 const char *section,
2526 unsigned section_line,
2527 const char *lvalue,
2528 int ltype,
2529 const char *rvalue,
2530 void *data,
2531 void *userdata) {
2532
2533 _cleanup_free_ char *p = NULL;
2534 Condition **list = data, *c;
2535 ConditionType t = ltype;
2536 bool trigger, negate;
2537 const Unit *u = userdata;
2538 int r;
2539
2540 assert(filename);
2541 assert(lvalue);
2542 assert(rvalue);
2543 assert(data);
2544
2545 if (isempty(rvalue)) {
2546 /* Empty assignment resets the list */
2547 *list = condition_free_list(*list);
2548 return 0;
2549 }
2550
2551 trigger = rvalue[0] == '|';
2552 if (trigger)
2553 rvalue++;
2554
2555 negate = rvalue[0] == '!';
2556 if (negate)
2557 rvalue++;
2558
2559 r = unit_full_printf(u, rvalue, &p);
2560 if (r < 0) {
2561 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue);
2562 return 0;
2563 }
2564
2565 r = path_simplify_and_warn(p, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
2566 if (r < 0)
2567 return 0;
2568
2569 c = condition_new(t, p, trigger, negate);
2570 if (!c)
2571 return log_oom();
2572
2573 LIST_PREPEND(conditions, *list, c);
2574 return 0;
2575 }
2576
2577 int config_parse_unit_condition_string(
2578 const char *unit,
2579 const char *filename,
2580 unsigned line,
2581 const char *section,
2582 unsigned section_line,
2583 const char *lvalue,
2584 int ltype,
2585 const char *rvalue,
2586 void *data,
2587 void *userdata) {
2588
2589 _cleanup_free_ char *s = NULL;
2590 Condition **list = data, *c;
2591 ConditionType t = ltype;
2592 bool trigger, negate;
2593 const Unit *u = userdata;
2594 int r;
2595
2596 assert(filename);
2597 assert(lvalue);
2598 assert(rvalue);
2599 assert(data);
2600
2601 if (isempty(rvalue)) {
2602 /* Empty assignment resets the list */
2603 *list = condition_free_list(*list);
2604 return 0;
2605 }
2606
2607 trigger = *rvalue == '|';
2608 if (trigger)
2609 rvalue += 1 + strspn(rvalue + 1, WHITESPACE);
2610
2611 negate = *rvalue == '!';
2612 if (negate)
2613 rvalue += 1 + strspn(rvalue + 1, WHITESPACE);
2614
2615 r = unit_full_printf(u, rvalue, &s);
2616 if (r < 0) {
2617 log_syntax(unit, LOG_ERR, filename, line, r,
2618 "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
2619 return 0;
2620 }
2621
2622 c = condition_new(t, s, trigger, negate);
2623 if (!c)
2624 return log_oom();
2625
2626 LIST_PREPEND(conditions, *list, c);
2627 return 0;
2628 }
2629
2630 int config_parse_unit_condition_null(
2631 const char *unit,
2632 const char *filename,
2633 unsigned line,
2634 const char *section,
2635 unsigned section_line,
2636 const char *lvalue,
2637 int ltype,
2638 const char *rvalue,
2639 void *data,
2640 void *userdata) {
2641
2642 Condition **list = data, *c;
2643 bool trigger, negate;
2644 int b;
2645
2646 assert(filename);
2647 assert(lvalue);
2648 assert(rvalue);
2649 assert(data);
2650
2651 log_syntax(unit, LOG_WARNING, filename, line, 0, "%s= is deprecated, please do not use.", lvalue);
2652
2653 if (isempty(rvalue)) {
2654 /* Empty assignment resets the list */
2655 *list = condition_free_list(*list);
2656 return 0;
2657 }
2658
2659 trigger = rvalue[0] == '|';
2660 if (trigger)
2661 rvalue++;
2662
2663 negate = rvalue[0] == '!';
2664 if (negate)
2665 rvalue++;
2666
2667 b = parse_boolean(rvalue);
2668 if (b < 0) {
2669 log_syntax(unit, LOG_ERR, filename, line, b, "Failed to parse boolean value in condition, ignoring: %s", rvalue);
2670 return 0;
2671 }
2672
2673 if (!b)
2674 negate = !negate;
2675
2676 c = condition_new(CONDITION_NULL, NULL, trigger, negate);
2677 if (!c)
2678 return log_oom();
2679
2680 LIST_PREPEND(conditions, *list, c);
2681 return 0;
2682 }
2683
2684 int config_parse_unit_requires_mounts_for(
2685 const char *unit,
2686 const char *filename,
2687 unsigned line,
2688 const char *section,
2689 unsigned section_line,
2690 const char *lvalue,
2691 int ltype,
2692 const char *rvalue,
2693 void *data,
2694 void *userdata) {
2695
2696 const char *p = rvalue;
2697 Unit *u = userdata;
2698 int r;
2699
2700 assert(filename);
2701 assert(lvalue);
2702 assert(rvalue);
2703 assert(data);
2704
2705 for (;;) {
2706 _cleanup_free_ char *word = NULL, *resolved = NULL;
2707
2708 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
2709 if (r == 0)
2710 return 0;
2711 if (r == -ENOMEM)
2712 return log_oom();
2713 if (r < 0) {
2714 log_syntax(unit, LOG_WARNING, filename, line, r,
2715 "Invalid syntax, ignoring: %s", rvalue);
2716 return 0;
2717 }
2718
2719 r = unit_full_printf(u, word, &resolved);
2720 if (r < 0) {
2721 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", word);
2722 continue;
2723 }
2724
2725 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
2726 if (r < 0)
2727 continue;
2728
2729 r = unit_require_mounts_for(u, resolved, UNIT_DEPENDENCY_FILE);
2730 if (r < 0) {
2731 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to add required mount '%s', ignoring: %m", resolved);
2732 continue;
2733 }
2734 }
2735 }
2736
2737 int config_parse_documentation(const char *unit,
2738 const char *filename,
2739 unsigned line,
2740 const char *section,
2741 unsigned section_line,
2742 const char *lvalue,
2743 int ltype,
2744 const char *rvalue,
2745 void *data,
2746 void *userdata) {
2747
2748 Unit *u = userdata;
2749 int r;
2750 char **a, **b;
2751
2752 assert(filename);
2753 assert(lvalue);
2754 assert(rvalue);
2755 assert(u);
2756
2757 if (isempty(rvalue)) {
2758 /* Empty assignment resets the list */
2759 u->documentation = strv_free(u->documentation);
2760 return 0;
2761 }
2762
2763 r = config_parse_unit_strv_printf(unit, filename, line, section, section_line, lvalue, ltype,
2764 rvalue, data, userdata);
2765 if (r < 0)
2766 return r;
2767
2768 for (a = b = u->documentation; a && *a; a++) {
2769
2770 if (documentation_url_is_valid(*a))
2771 *(b++) = *a;
2772 else {
2773 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid URL, ignoring: %s", *a);
2774 free(*a);
2775 }
2776 }
2777 if (b)
2778 *b = NULL;
2779
2780 return r;
2781 }
2782
2783 #if HAVE_SECCOMP
2784 int config_parse_syscall_filter(
2785 const char *unit,
2786 const char *filename,
2787 unsigned line,
2788 const char *section,
2789 unsigned section_line,
2790 const char *lvalue,
2791 int ltype,
2792 const char *rvalue,
2793 void *data,
2794 void *userdata) {
2795
2796 ExecContext *c = data;
2797 const Unit *u = userdata;
2798 bool invert = false;
2799 const char *p;
2800 int r;
2801
2802 assert(filename);
2803 assert(lvalue);
2804 assert(rvalue);
2805 assert(u);
2806
2807 if (isempty(rvalue)) {
2808 /* Empty assignment resets the list */
2809 c->syscall_filter = hashmap_free(c->syscall_filter);
2810 c->syscall_whitelist = false;
2811 return 0;
2812 }
2813
2814 if (rvalue[0] == '~') {
2815 invert = true;
2816 rvalue++;
2817 }
2818
2819 if (!c->syscall_filter) {
2820 c->syscall_filter = hashmap_new(NULL);
2821 if (!c->syscall_filter)
2822 return log_oom();
2823
2824 if (invert)
2825 /* Allow everything but the ones listed */
2826 c->syscall_whitelist = false;
2827 else {
2828 /* Allow nothing but the ones listed */
2829 c->syscall_whitelist = true;
2830
2831 /* Accept default syscalls if we are on a whitelist */
2832 r = seccomp_parse_syscall_filter(
2833 "@default", -1, c->syscall_filter,
2834 SECCOMP_PARSE_PERMISSIVE|SECCOMP_PARSE_WHITELIST,
2835 unit,
2836 NULL, 0);
2837 if (r < 0)
2838 return r;
2839 }
2840 }
2841
2842 p = rvalue;
2843 for (;;) {
2844 _cleanup_free_ char *word = NULL, *name = NULL;
2845 int num;
2846
2847 r = extract_first_word(&p, &word, NULL, 0);
2848 if (r == 0)
2849 return 0;
2850 if (r == -ENOMEM)
2851 return log_oom();
2852 if (r < 0) {
2853 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
2854 return 0;
2855 }
2856
2857 r = parse_syscall_and_errno(word, &name, &num);
2858 if (r < 0) {
2859 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse syscall:errno, ignoring: %s", word);
2860 continue;
2861 }
2862
2863 r = seccomp_parse_syscall_filter(
2864 name, num, c->syscall_filter,
2865 SECCOMP_PARSE_LOG|SECCOMP_PARSE_PERMISSIVE|
2866 (invert ? SECCOMP_PARSE_INVERT : 0)|
2867 (c->syscall_whitelist ? SECCOMP_PARSE_WHITELIST : 0),
2868 unit, filename, line);
2869 if (r < 0)
2870 return r;
2871 }
2872 }
2873
2874 int config_parse_syscall_archs(
2875 const char *unit,
2876 const char *filename,
2877 unsigned line,
2878 const char *section,
2879 unsigned section_line,
2880 const char *lvalue,
2881 int ltype,
2882 const char *rvalue,
2883 void *data,
2884 void *userdata) {
2885
2886 const char *p = rvalue;
2887 Set **archs = data;
2888 int r;
2889
2890 if (isempty(rvalue)) {
2891 *archs = set_free(*archs);
2892 return 0;
2893 }
2894
2895 r = set_ensure_allocated(archs, NULL);
2896 if (r < 0)
2897 return log_oom();
2898
2899 for (;;) {
2900 _cleanup_free_ char *word = NULL;
2901 uint32_t a;
2902
2903 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
2904 if (r == 0)
2905 return 0;
2906 if (r == -ENOMEM)
2907 return log_oom();
2908 if (r < 0) {
2909 log_syntax(unit, LOG_WARNING, filename, line, r,
2910 "Invalid syntax, ignoring: %s", rvalue);
2911 return 0;
2912 }
2913
2914 r = seccomp_arch_from_string(word, &a);
2915 if (r < 0) {
2916 log_syntax(unit, LOG_ERR, filename, line, r,
2917 "Failed to parse system call architecture \"%s\", ignoring: %m", word);
2918 continue;
2919 }
2920
2921 r = set_put(*archs, UINT32_TO_PTR(a + 1));
2922 if (r < 0)
2923 return log_oom();
2924 }
2925 }
2926
2927 int config_parse_syscall_errno(
2928 const char *unit,
2929 const char *filename,
2930 unsigned line,
2931 const char *section,
2932 unsigned section_line,
2933 const char *lvalue,
2934 int ltype,
2935 const char *rvalue,
2936 void *data,
2937 void *userdata) {
2938
2939 ExecContext *c = data;
2940 int e;
2941
2942 assert(filename);
2943 assert(lvalue);
2944 assert(rvalue);
2945
2946 if (isempty(rvalue)) {
2947 /* Empty assignment resets to KILL */
2948 c->syscall_errno = 0;
2949 return 0;
2950 }
2951
2952 e = parse_errno(rvalue);
2953 if (e <= 0) {
2954 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse error number, ignoring: %s", rvalue);
2955 return 0;
2956 }
2957
2958 c->syscall_errno = e;
2959 return 0;
2960 }
2961
2962 int config_parse_address_families(
2963 const char *unit,
2964 const char *filename,
2965 unsigned line,
2966 const char *section,
2967 unsigned section_line,
2968 const char *lvalue,
2969 int ltype,
2970 const char *rvalue,
2971 void *data,
2972 void *userdata) {
2973
2974 ExecContext *c = data;
2975 bool invert = false;
2976 const char *p;
2977 int r;
2978
2979 assert(filename);
2980 assert(lvalue);
2981 assert(rvalue);
2982
2983 if (isempty(rvalue)) {
2984 /* Empty assignment resets the list */
2985 c->address_families = set_free(c->address_families);
2986 c->address_families_whitelist = false;
2987 return 0;
2988 }
2989
2990 if (rvalue[0] == '~') {
2991 invert = true;
2992 rvalue++;
2993 }
2994
2995 if (!c->address_families) {
2996 c->address_families = set_new(NULL);
2997 if (!c->address_families)
2998 return log_oom();
2999
3000 c->address_families_whitelist = !invert;
3001 }
3002
3003 for (p = rvalue;;) {
3004 _cleanup_free_ char *word = NULL;
3005 int af;
3006
3007 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
3008 if (r == 0)
3009 return 0;
3010 if (r == -ENOMEM)
3011 return log_oom();
3012 if (r < 0) {
3013 log_syntax(unit, LOG_WARNING, filename, line, r,
3014 "Invalid syntax, ignoring: %s", rvalue);
3015 return 0;
3016 }
3017
3018 af = af_from_name(word);
3019 if (af < 0) {
3020 log_syntax(unit, LOG_ERR, filename, line, af,
3021 "Failed to parse address family, ignoring: %s", word);
3022 continue;
3023 }
3024
3025 /* If we previously wanted to forbid an address family and now
3026 * we want to allow it, then just remove it from the list.
3027 */
3028 if (!invert == c->address_families_whitelist) {
3029 r = set_put(c->address_families, INT_TO_PTR(af));
3030 if (r < 0)
3031 return log_oom();
3032 } else
3033 set_remove(c->address_families, INT_TO_PTR(af));
3034 }
3035 }
3036
3037 int config_parse_restrict_namespaces(
3038 const char *unit,
3039 const char *filename,
3040 unsigned line,
3041 const char *section,
3042 unsigned section_line,
3043 const char *lvalue,
3044 int ltype,
3045 const char *rvalue,
3046 void *data,
3047 void *userdata) {
3048
3049 ExecContext *c = data;
3050 unsigned long flags;
3051 bool invert = false;
3052 int r;
3053
3054 if (isempty(rvalue)) {
3055 /* Reset to the default. */
3056 c->restrict_namespaces = NAMESPACE_FLAGS_INITIAL;
3057 return 0;
3058 }
3059
3060 /* Boolean parameter ignores the previous settings */
3061 r = parse_boolean(rvalue);
3062 if (r > 0) {
3063 c->restrict_namespaces = 0;
3064 return 0;
3065 } else if (r == 0) {
3066 c->restrict_namespaces = NAMESPACE_FLAGS_ALL;
3067 return 0;
3068 }
3069
3070 if (rvalue[0] == '~') {
3071 invert = true;
3072 rvalue++;
3073 }
3074
3075 /* Not a boolean argument, in this case it's a list of namespace types. */
3076 r = namespace_flags_from_string(rvalue, &flags);
3077 if (r < 0) {
3078 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse namespace type string, ignoring: %s", rvalue);
3079 return 0;
3080 }
3081
3082 if (c->restrict_namespaces == NAMESPACE_FLAGS_INITIAL)
3083 /* Initial assignment. Just set the value. */
3084 c->restrict_namespaces = invert ? (~flags) & NAMESPACE_FLAGS_ALL : flags;
3085 else
3086 /* Merge the value with the previous one. */
3087 SET_FLAG(c->restrict_namespaces, flags, !invert);
3088
3089 return 0;
3090 }
3091 #endif
3092
3093 int config_parse_unit_slice(
3094 const char *unit,
3095 const char *filename,
3096 unsigned line,
3097 const char *section,
3098 unsigned section_line,
3099 const char *lvalue,
3100 int ltype,
3101 const char *rvalue,
3102 void *data,
3103 void *userdata) {
3104
3105 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
3106 _cleanup_free_ char *k = NULL;
3107 Unit *u = userdata, *slice;
3108 int r;
3109
3110 assert(filename);
3111 assert(lvalue);
3112 assert(rvalue);
3113 assert(u);
3114
3115 r = unit_name_printf(u, rvalue, &k);
3116 if (r < 0) {
3117 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue);
3118 return 0;
3119 }
3120
3121 r = manager_load_unit(u->manager, k, NULL, &error, &slice);
3122 if (r < 0) {
3123 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to load slice unit %s, ignoring: %s", k, bus_error_message(&error, r));
3124 return 0;
3125 }
3126
3127 r = unit_set_slice(u, slice);
3128 if (r < 0) {
3129 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to assign slice %s to unit %s, ignoring: %m", slice->id, u->id);
3130 return 0;
3131 }
3132
3133 return 0;
3134 }
3135
3136 int config_parse_cpu_quota(
3137 const char *unit,
3138 const char *filename,
3139 unsigned line,
3140 const char *section,
3141 unsigned section_line,
3142 const char *lvalue,
3143 int ltype,
3144 const char *rvalue,
3145 void *data,
3146 void *userdata) {
3147
3148 CGroupContext *c = data;
3149 int r;
3150
3151 assert(filename);
3152 assert(lvalue);
3153 assert(rvalue);
3154
3155 if (isempty(rvalue)) {
3156 c->cpu_quota_per_sec_usec = USEC_INFINITY;
3157 return 0;
3158 }
3159
3160 r = parse_permille_unbounded(rvalue);
3161 if (r <= 0) {
3162 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid CPU quota '%s', ignoring.", rvalue);
3163 return 0;
3164 }
3165
3166 c->cpu_quota_per_sec_usec = ((usec_t) r * USEC_PER_SEC) / 1000U;
3167 return 0;
3168 }
3169
3170 int config_parse_allowed_cpus(
3171 const char *unit,
3172 const char *filename,
3173 unsigned line,
3174 const char *section,
3175 unsigned section_line,
3176 const char *lvalue,
3177 int ltype,
3178 const char *rvalue,
3179 void *data,
3180 void *userdata) {
3181
3182 CGroupContext *c = data;
3183
3184 (void) parse_cpu_set_extend(rvalue, &c->cpuset_cpus, true, unit, filename, line, lvalue);
3185
3186 return 0;
3187 }
3188
3189 int config_parse_allowed_mems(
3190 const char *unit,
3191 const char *filename,
3192 unsigned line,
3193 const char *section,
3194 unsigned section_line,
3195 const char *lvalue,
3196 int ltype,
3197 const char *rvalue,
3198 void *data,
3199 void *userdata) {
3200
3201 CGroupContext *c = data;
3202
3203 (void) parse_cpu_set_extend(rvalue, &c->cpuset_mems, true, unit, filename, line, lvalue);
3204
3205 return 0;
3206 }
3207
3208 int config_parse_memory_limit(
3209 const char *unit,
3210 const char *filename,
3211 unsigned line,
3212 const char *section,
3213 unsigned section_line,
3214 const char *lvalue,
3215 int ltype,
3216 const char *rvalue,
3217 void *data,
3218 void *userdata) {
3219
3220 CGroupContext *c = data;
3221 uint64_t bytes = CGROUP_LIMIT_MAX;
3222 int r;
3223
3224 if (!isempty(rvalue) && !streq(rvalue, "infinity")) {
3225
3226 r = parse_permille(rvalue);
3227 if (r < 0) {
3228 r = parse_size(rvalue, 1024, &bytes);
3229 if (r < 0) {
3230 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid memory limit '%s', ignoring: %m", rvalue);
3231 return 0;
3232 }
3233 } else
3234 bytes = physical_memory_scale(r, 1000U);
3235
3236 if (bytes >= UINT64_MAX ||
3237 (bytes <= 0 && !STR_IN_SET(lvalue, "MemorySwapMax", "MemoryLow", "MemoryMin", "DefaultMemoryLow", "DefaultMemoryMin"))) {
3238 log_syntax(unit, LOG_ERR, filename, line, 0, "Memory limit '%s' out of range, ignoring.", rvalue);
3239 return 0;
3240 }
3241 }
3242
3243 if (streq(lvalue, "DefaultMemoryLow")) {
3244 c->default_memory_low_set = true;
3245 if (isempty(rvalue))
3246 c->default_memory_low = CGROUP_LIMIT_MIN;
3247 else
3248 c->default_memory_low = bytes;
3249 } else if (streq(lvalue, "DefaultMemoryMin")) {
3250 c->default_memory_min_set = true;
3251 if (isempty(rvalue))
3252 c->default_memory_min = CGROUP_LIMIT_MIN;
3253 else
3254 c->default_memory_min = bytes;
3255 } else if (streq(lvalue, "MemoryMin")) {
3256 c->memory_min = bytes;
3257 c->memory_min_set = true;
3258 } else if (streq(lvalue, "MemoryLow")) {
3259 c->memory_low = bytes;
3260 c->memory_low_set = true;
3261 } else if (streq(lvalue, "MemoryHigh"))
3262 c->memory_high = bytes;
3263 else if (streq(lvalue, "MemoryMax"))
3264 c->memory_max = bytes;
3265 else if (streq(lvalue, "MemorySwapMax"))
3266 c->memory_swap_max = bytes;
3267 else if (streq(lvalue, "MemoryLimit"))
3268 c->memory_limit = bytes;
3269 else
3270 return -EINVAL;
3271
3272 return 0;
3273 }
3274
3275 int config_parse_tasks_max(
3276 const char *unit,
3277 const char *filename,
3278 unsigned line,
3279 const char *section,
3280 unsigned section_line,
3281 const char *lvalue,
3282 int ltype,
3283 const char *rvalue,
3284 void *data,
3285 void *userdata) {
3286
3287 const Unit *u = userdata;
3288 TasksMax *tasks_max = data;
3289 uint64_t v;
3290 int r;
3291
3292 if (isempty(rvalue)) {
3293 *tasks_max = u ? u->manager->default_tasks_max : TASKS_MAX_UNSET;
3294 return 0;
3295 }
3296
3297 if (streq(rvalue, "infinity")) {
3298 *tasks_max = TASKS_MAX_UNSET;
3299 return 0;
3300 }
3301
3302 r = parse_permille(rvalue);
3303 if (r >= 0)
3304 *tasks_max = (TasksMax) { r, 1000U }; /* r‰ */
3305 else {
3306 r = safe_atou64(rvalue, &v);
3307 if (r < 0) {
3308 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid maximum tasks value '%s', ignoring: %m", rvalue);
3309 return 0;
3310 }
3311
3312 if (v <= 0 || v >= UINT64_MAX) {
3313 log_syntax(unit, LOG_ERR, filename, line, 0, "Maximum tasks value '%s' out of range, ignoring.", rvalue);
3314 return 0;
3315 }
3316
3317 *tasks_max = (TasksMax) { v };
3318 }
3319
3320 return 0;
3321 }
3322
3323 int config_parse_delegate(
3324 const char *unit,
3325 const char *filename,
3326 unsigned line,
3327 const char *section,
3328 unsigned section_line,
3329 const char *lvalue,
3330 int ltype,
3331 const char *rvalue,
3332 void *data,
3333 void *userdata) {
3334
3335 CGroupContext *c = data;
3336 UnitType t;
3337 int r;
3338
3339 t = unit_name_to_type(unit);
3340 assert(t != _UNIT_TYPE_INVALID);
3341
3342 if (!unit_vtable[t]->can_delegate) {
3343 log_syntax(unit, LOG_ERR, filename, line, 0, "Delegate= setting not supported for this unit type, ignoring.");
3344 return 0;
3345 }
3346
3347 /* We either accept a boolean value, which may be used to turn on delegation for all controllers, or turn it
3348 * off for all. Or it takes a list of controller names, in which case we add the specified controllers to the
3349 * mask to delegate. */
3350
3351 if (isempty(rvalue)) {
3352 /* An empty string resets controllers and set Delegate=yes. */
3353 c->delegate = true;
3354 c->delegate_controllers = 0;
3355 return 0;
3356 }
3357
3358 r = parse_boolean(rvalue);
3359 if (r < 0) {
3360 const char *p = rvalue;
3361 CGroupMask mask = 0;
3362
3363 for (;;) {
3364 _cleanup_free_ char *word = NULL;
3365 CGroupController cc;
3366
3367 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
3368 if (r == 0)
3369 break;
3370 if (r == -ENOMEM)
3371 return log_oom();
3372 if (r < 0) {
3373 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
3374 return 0;
3375 }
3376
3377 cc = cgroup_controller_from_string(word);
3378 if (cc < 0) {
3379 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid controller name '%s', ignoring", word);
3380 continue;
3381 }
3382
3383 mask |= CGROUP_CONTROLLER_TO_MASK(cc);
3384 }
3385
3386 c->delegate = true;
3387 c->delegate_controllers |= mask;
3388
3389 } else if (r > 0) {
3390 c->delegate = true;
3391 c->delegate_controllers = _CGROUP_MASK_ALL;
3392 } else {
3393 c->delegate = false;
3394 c->delegate_controllers = 0;
3395 }
3396
3397 return 0;
3398 }
3399
3400 int config_parse_device_allow(
3401 const char *unit,
3402 const char *filename,
3403 unsigned line,
3404 const char *section,
3405 unsigned section_line,
3406 const char *lvalue,
3407 int ltype,
3408 const char *rvalue,
3409 void *data,
3410 void *userdata) {
3411
3412 _cleanup_free_ char *path = NULL, *resolved = NULL;
3413 CGroupContext *c = data;
3414 const char *p = rvalue;
3415 int r;
3416
3417 if (isempty(rvalue)) {
3418 while (c->device_allow)
3419 cgroup_context_free_device_allow(c, c->device_allow);
3420
3421 return 0;
3422 }
3423
3424 r = extract_first_word(&p, &path, NULL, EXTRACT_UNQUOTE);
3425 if (r == -ENOMEM)
3426 return log_oom();
3427 if (r < 0) {
3428 log_syntax(unit, LOG_WARNING, filename, line, r,
3429 "Invalid syntax, ignoring: %s", rvalue);
3430 return 0;
3431 }
3432 if (r == 0) {
3433 log_syntax(unit, LOG_WARNING, filename, line, 0,
3434 "Failed to extract device path and rights from '%s', ignoring.", rvalue);
3435 return 0;
3436 }
3437
3438 r = unit_full_printf(userdata, path, &resolved);
3439 if (r < 0) {
3440 log_syntax(unit, LOG_WARNING, filename, line, r,
3441 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
3442 return 0;
3443 }
3444
3445 if (!STARTSWITH_SET(resolved, "block-", "char-")) {
3446
3447 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
3448 if (r < 0)
3449 return 0;
3450
3451 if (!valid_device_node_path(resolved)) {
3452 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid device node path '%s', ignoring.", resolved);
3453 return 0;
3454 }
3455 }
3456
3457 if (!isempty(p) && !in_charset(p, "rwm")) {
3458 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid device rights '%s', ignoring.", p);
3459 return 0;
3460 }
3461
3462 return cgroup_add_device_allow(c, resolved, p);
3463 }
3464
3465 int config_parse_io_device_weight(
3466 const char *unit,
3467 const char *filename,
3468 unsigned line,
3469 const char *section,
3470 unsigned section_line,
3471 const char *lvalue,
3472 int ltype,
3473 const char *rvalue,
3474 void *data,
3475 void *userdata) {
3476
3477 _cleanup_free_ char *path = NULL, *resolved = NULL;
3478 CGroupIODeviceWeight *w;
3479 CGroupContext *c = data;
3480 const char *p = rvalue;
3481 uint64_t u;
3482 int r;
3483
3484 assert(filename);
3485 assert(lvalue);
3486 assert(rvalue);
3487
3488 if (isempty(rvalue)) {
3489 while (c->io_device_weights)
3490 cgroup_context_free_io_device_weight(c, c->io_device_weights);
3491
3492 return 0;
3493 }
3494
3495 r = extract_first_word(&p, &path, NULL, EXTRACT_UNQUOTE);
3496 if (r == -ENOMEM)
3497 return log_oom();
3498 if (r < 0) {
3499 log_syntax(unit, LOG_WARNING, filename, line, r,
3500 "Invalid syntax, ignoring: %s", rvalue);
3501 return 0;
3502 }
3503 if (r == 0 || isempty(p)) {
3504 log_syntax(unit, LOG_WARNING, filename, line, 0,
3505 "Failed to extract device path and weight from '%s', ignoring.", rvalue);
3506 return 0;
3507 }
3508
3509 r = unit_full_printf(userdata, path, &resolved);
3510 if (r < 0) {
3511 log_syntax(unit, LOG_WARNING, filename, line, r,
3512 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
3513 return 0;
3514 }
3515
3516 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
3517 if (r < 0)
3518 return 0;
3519
3520 r = cg_weight_parse(p, &u);
3521 if (r < 0) {
3522 log_syntax(unit, LOG_ERR, filename, line, r, "IO weight '%s' invalid, ignoring: %m", p);
3523 return 0;
3524 }
3525
3526 assert(u != CGROUP_WEIGHT_INVALID);
3527
3528 w = new0(CGroupIODeviceWeight, 1);
3529 if (!w)
3530 return log_oom();
3531
3532 w->path = TAKE_PTR(resolved);
3533 w->weight = u;
3534
3535 LIST_PREPEND(device_weights, c->io_device_weights, w);
3536 return 0;
3537 }
3538
3539 int config_parse_io_device_latency(
3540 const char *unit,
3541 const char *filename,
3542 unsigned line,
3543 const char *section,
3544 unsigned section_line,
3545 const char *lvalue,
3546 int ltype,
3547 const char *rvalue,
3548 void *data,
3549 void *userdata) {
3550
3551 _cleanup_free_ char *path = NULL, *resolved = NULL;
3552 CGroupIODeviceLatency *l;
3553 CGroupContext *c = data;
3554 const char *p = rvalue;
3555 usec_t usec;
3556 int r;
3557
3558 assert(filename);
3559 assert(lvalue);
3560 assert(rvalue);
3561
3562 if (isempty(rvalue)) {
3563 while (c->io_device_latencies)
3564 cgroup_context_free_io_device_latency(c, c->io_device_latencies);
3565
3566 return 0;
3567 }
3568
3569 r = extract_first_word(&p, &path, NULL, EXTRACT_UNQUOTE);
3570 if (r == -ENOMEM)
3571 return log_oom();
3572 if (r < 0) {
3573 log_syntax(unit, LOG_WARNING, filename, line, r,
3574 "Invalid syntax, ignoring: %s", rvalue);
3575 return 0;
3576 }
3577 if (r == 0 || isempty(p)) {
3578 log_syntax(unit, LOG_WARNING, filename, line, 0,
3579 "Failed to extract device path and latency from '%s', ignoring.", rvalue);
3580 return 0;
3581 }
3582
3583 r = unit_full_printf(userdata, path, &resolved);
3584 if (r < 0) {
3585 log_syntax(unit, LOG_WARNING, filename, line, r,
3586 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
3587 return 0;
3588 }
3589
3590 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
3591 if (r < 0)
3592 return 0;
3593
3594 if (parse_sec(p, &usec) < 0) {
3595 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse timer value, ignoring: %s", p);
3596 return 0;
3597 }
3598
3599 l = new0(CGroupIODeviceLatency, 1);
3600 if (!l)
3601 return log_oom();
3602
3603 l->path = TAKE_PTR(resolved);
3604 l->target_usec = usec;
3605
3606 LIST_PREPEND(device_latencies, c->io_device_latencies, l);
3607 return 0;
3608 }
3609
3610 int config_parse_io_limit(
3611 const char *unit,
3612 const char *filename,
3613 unsigned line,
3614 const char *section,
3615 unsigned section_line,
3616 const char *lvalue,
3617 int ltype,
3618 const char *rvalue,
3619 void *data,
3620 void *userdata) {
3621
3622 _cleanup_free_ char *path = NULL, *resolved = NULL;
3623 CGroupIODeviceLimit *l = NULL, *t;
3624 CGroupContext *c = data;
3625 CGroupIOLimitType type;
3626 const char *p = rvalue;
3627 uint64_t num;
3628 int r;
3629
3630 assert(filename);
3631 assert(lvalue);
3632 assert(rvalue);
3633
3634 type = cgroup_io_limit_type_from_string(lvalue);
3635 assert(type >= 0);
3636
3637 if (isempty(rvalue)) {
3638 LIST_FOREACH(device_limits, l, c->io_device_limits)
3639 l->limits[type] = cgroup_io_limit_defaults[type];
3640 return 0;
3641 }
3642
3643 r = extract_first_word(&p, &path, NULL, EXTRACT_UNQUOTE);
3644 if (r == -ENOMEM)
3645 return log_oom();
3646 if (r < 0) {
3647 log_syntax(unit, LOG_WARNING, filename, line, r,
3648 "Invalid syntax, ignoring: %s", rvalue);
3649 return 0;
3650 }
3651 if (r == 0 || isempty(p)) {
3652 log_syntax(unit, LOG_WARNING, filename, line, 0,
3653 "Failed to extract device node and bandwidth from '%s', ignoring.", rvalue);
3654 return 0;
3655 }
3656
3657 r = unit_full_printf(userdata, path, &resolved);
3658 if (r < 0) {
3659 log_syntax(unit, LOG_WARNING, filename, line, r,
3660 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
3661 return 0;
3662 }
3663
3664 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
3665 if (r < 0)
3666 return 0;
3667
3668 if (streq("infinity", p))
3669 num = CGROUP_LIMIT_MAX;
3670 else {
3671 r = parse_size(p, 1000, &num);
3672 if (r < 0 || num <= 0) {
3673 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid IO limit '%s', ignoring.", p);
3674 return 0;
3675 }
3676 }
3677
3678 LIST_FOREACH(device_limits, t, c->io_device_limits) {
3679 if (path_equal(resolved, t->path)) {
3680 l = t;
3681 break;
3682 }
3683 }
3684
3685 if (!l) {
3686 CGroupIOLimitType ttype;
3687
3688 l = new0(CGroupIODeviceLimit, 1);
3689 if (!l)
3690 return log_oom();
3691
3692 l->path = TAKE_PTR(resolved);
3693 for (ttype = 0; ttype < _CGROUP_IO_LIMIT_TYPE_MAX; ttype++)
3694 l->limits[ttype] = cgroup_io_limit_defaults[ttype];
3695
3696 LIST_PREPEND(device_limits, c->io_device_limits, l);
3697 }
3698
3699 l->limits[type] = num;
3700
3701 return 0;
3702 }
3703
3704 int config_parse_blockio_device_weight(
3705 const char *unit,
3706 const char *filename,
3707 unsigned line,
3708 const char *section,
3709 unsigned section_line,
3710 const char *lvalue,
3711 int ltype,
3712 const char *rvalue,
3713 void *data,
3714 void *userdata) {
3715
3716 _cleanup_free_ char *path = NULL, *resolved = NULL;
3717 CGroupBlockIODeviceWeight *w;
3718 CGroupContext *c = data;
3719 const char *p = rvalue;
3720 uint64_t u;
3721 int r;
3722
3723 assert(filename);
3724 assert(lvalue);
3725 assert(rvalue);
3726
3727 if (isempty(rvalue)) {
3728 while (c->blockio_device_weights)
3729 cgroup_context_free_blockio_device_weight(c, c->blockio_device_weights);
3730
3731 return 0;
3732 }
3733
3734 r = extract_first_word(&p, &path, NULL, EXTRACT_UNQUOTE);
3735 if (r == -ENOMEM)
3736 return log_oom();
3737 if (r < 0) {
3738 log_syntax(unit, LOG_WARNING, filename, line, r,
3739 "Invalid syntax, ignoring: %s", rvalue);
3740 return 0;
3741 }
3742 if (r == 0 || isempty(p)) {
3743 log_syntax(unit, LOG_WARNING, filename, line, 0,
3744 "Failed to extract device node and weight from '%s', ignoring.", rvalue);
3745 return 0;
3746 }
3747
3748 r = unit_full_printf(userdata, path, &resolved);
3749 if (r < 0) {
3750 log_syntax(unit, LOG_WARNING, filename, line, r,
3751 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
3752 return 0;
3753 }
3754
3755 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
3756 if (r < 0)
3757 return 0;
3758
3759 r = cg_blkio_weight_parse(p, &u);
3760 if (r < 0) {
3761 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid block IO weight '%s', ignoring: %m", p);
3762 return 0;
3763 }
3764
3765 assert(u != CGROUP_BLKIO_WEIGHT_INVALID);
3766
3767 w = new0(CGroupBlockIODeviceWeight, 1);
3768 if (!w)
3769 return log_oom();
3770
3771 w->path = TAKE_PTR(resolved);
3772 w->weight = u;
3773
3774 LIST_PREPEND(device_weights, c->blockio_device_weights, w);
3775 return 0;
3776 }
3777
3778 int config_parse_blockio_bandwidth(
3779 const char *unit,
3780 const char *filename,
3781 unsigned line,
3782 const char *section,
3783 unsigned section_line,
3784 const char *lvalue,
3785 int ltype,
3786 const char *rvalue,
3787 void *data,
3788 void *userdata) {
3789
3790 _cleanup_free_ char *path = NULL, *resolved = NULL;
3791 CGroupBlockIODeviceBandwidth *b = NULL, *t;
3792 CGroupContext *c = data;
3793 const char *p = rvalue;
3794 uint64_t bytes;
3795 bool read;
3796 int r;
3797
3798 assert(filename);
3799 assert(lvalue);
3800 assert(rvalue);
3801
3802 read = streq("BlockIOReadBandwidth", lvalue);
3803
3804 if (isempty(rvalue)) {
3805 LIST_FOREACH(device_bandwidths, b, c->blockio_device_bandwidths) {
3806 b->rbps = CGROUP_LIMIT_MAX;
3807 b->wbps = CGROUP_LIMIT_MAX;
3808 }
3809 return 0;
3810 }
3811
3812 r = extract_first_word(&p, &path, NULL, EXTRACT_UNQUOTE);
3813 if (r == -ENOMEM)
3814 return log_oom();
3815 if (r < 0) {
3816 log_syntax(unit, LOG_WARNING, filename, line, r,
3817 "Invalid syntax, ignoring: %s", rvalue);
3818 return 0;
3819 }
3820 if (r == 0 || isempty(p)) {
3821 log_syntax(unit, LOG_WARNING, filename, line, 0,
3822 "Failed to extract device node and bandwidth from '%s', ignoring.", rvalue);
3823 return 0;
3824 }
3825
3826 r = unit_full_printf(userdata, path, &resolved);
3827 if (r < 0) {
3828 log_syntax(unit, LOG_WARNING, filename, line, r,
3829 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
3830 return 0;
3831 }
3832
3833 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
3834 if (r < 0)
3835 return 0;
3836
3837 r = parse_size(p, 1000, &bytes);
3838 if (r < 0 || bytes <= 0) {
3839 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid Block IO Bandwidth '%s', ignoring.", p);
3840 return 0;
3841 }
3842
3843 LIST_FOREACH(device_bandwidths, t, c->blockio_device_bandwidths) {
3844 if (path_equal(resolved, t->path)) {
3845 b = t;
3846 break;
3847 }
3848 }
3849
3850 if (!t) {
3851 b = new0(CGroupBlockIODeviceBandwidth, 1);
3852 if (!b)
3853 return log_oom();
3854
3855 b->path = TAKE_PTR(resolved);
3856 b->rbps = CGROUP_LIMIT_MAX;
3857 b->wbps = CGROUP_LIMIT_MAX;
3858
3859 LIST_PREPEND(device_bandwidths, c->blockio_device_bandwidths, b);
3860 }
3861
3862 if (read)
3863 b->rbps = bytes;
3864 else
3865 b->wbps = bytes;
3866
3867 return 0;
3868 }
3869
3870 int config_parse_job_mode_isolate(
3871 const char *unit,
3872 const char *filename,
3873 unsigned line,
3874 const char *section,
3875 unsigned section_line,
3876 const char *lvalue,
3877 int ltype,
3878 const char *rvalue,
3879 void *data,
3880 void *userdata) {
3881
3882 JobMode *m = data;
3883 int r;
3884
3885 assert(filename);
3886 assert(lvalue);
3887 assert(rvalue);
3888
3889 r = parse_boolean(rvalue);
3890 if (r < 0) {
3891 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse boolean, ignoring: %s", rvalue);
3892 return 0;
3893 }
3894
3895 log_notice("%s is deprecated. Please use OnFailureJobMode= instead", lvalue);
3896
3897 *m = r ? JOB_ISOLATE : JOB_REPLACE;
3898 return 0;
3899 }
3900
3901 int config_parse_exec_directories(
3902 const char *unit,
3903 const char *filename,
3904 unsigned line,
3905 const char *section,
3906 unsigned section_line,
3907 const char *lvalue,
3908 int ltype,
3909 const char *rvalue,
3910 void *data,
3911 void *userdata) {
3912
3913 char***rt = data;
3914 const Unit *u = userdata;
3915 const char *p;
3916 int r;
3917
3918 assert(filename);
3919 assert(lvalue);
3920 assert(rvalue);
3921 assert(data);
3922
3923 if (isempty(rvalue)) {
3924 /* Empty assignment resets the list */
3925 *rt = strv_free(*rt);
3926 return 0;
3927 }
3928
3929 for (p = rvalue;;) {
3930 _cleanup_free_ char *word = NULL, *k = NULL;
3931
3932 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
3933 if (r == -ENOMEM)
3934 return log_oom();
3935 if (r < 0) {
3936 log_syntax(unit, LOG_WARNING, filename, line, r,
3937 "Invalid syntax, ignoring: %s", rvalue);
3938 return 0;
3939 }
3940 if (r == 0)
3941 return 0;
3942
3943 r = unit_full_printf(u, word, &k);
3944 if (r < 0) {
3945 log_syntax(unit, LOG_ERR, filename, line, r,
3946 "Failed to resolve unit specifiers in \"%s\", ignoring: %m", word);
3947 continue;
3948 }
3949
3950 r = path_simplify_and_warn(k, PATH_CHECK_RELATIVE, unit, filename, line, lvalue);
3951 if (r < 0)
3952 continue;
3953
3954 if (path_startswith(k, "private")) {
3955 log_syntax(unit, LOG_ERR, filename, line, 0,
3956 "%s= path can't be 'private', ignoring assignment: %s", lvalue, word);
3957 continue;
3958 }
3959
3960 r = strv_push(rt, k);
3961 if (r < 0)
3962 return log_oom();
3963 k = NULL;
3964 }
3965 }
3966
3967 int config_parse_set_status(
3968 const char *unit,
3969 const char *filename,
3970 unsigned line,
3971 const char *section,
3972 unsigned section_line,
3973 const char *lvalue,
3974 int ltype,
3975 const char *rvalue,
3976 void *data,
3977 void *userdata) {
3978
3979 size_t l;
3980 const char *word, *state;
3981 int r;
3982 ExitStatusSet *status_set = data;
3983
3984 assert(filename);
3985 assert(lvalue);
3986 assert(rvalue);
3987 assert(data);
3988
3989 /* Empty assignment resets the list */
3990 if (isempty(rvalue)) {
3991 exit_status_set_free(status_set);
3992 return 0;
3993 }
3994
3995 FOREACH_WORD(word, l, rvalue, state) {
3996 _cleanup_free_ char *temp;
3997 Bitmap *bitmap;
3998
3999 temp = strndup(word, l);
4000 if (!temp)
4001 return log_oom();
4002
4003 /* We need to call exit_status_from_string() first, because we want
4004 * to parse numbers as exit statuses, not signals. */
4005
4006 r = exit_status_from_string(temp);
4007 if (r >= 0) {
4008 assert(r >= 0 && r < 256);
4009 bitmap = &status_set->status;
4010 } else {
4011 r = signal_from_string(temp);
4012
4013 if (r <= 0) {
4014 log_syntax(unit, LOG_ERR, filename, line, 0,
4015 "Failed to parse value, ignoring: %s", word);
4016 continue;
4017 }
4018 bitmap = &status_set->signal;
4019 }
4020
4021 r = bitmap_set(bitmap, r);
4022 if (r < 0)
4023 return log_error_errno(r, "Failed to set signal or status %s: %m", word);
4024 }
4025 if (!isempty(state))
4026 log_syntax(unit, LOG_ERR, filename, line, 0, "Trailing garbage, ignoring.");
4027
4028 return 0;
4029 }
4030
4031 int config_parse_namespace_path_strv(
4032 const char *unit,
4033 const char *filename,
4034 unsigned line,
4035 const char *section,
4036 unsigned section_line,
4037 const char *lvalue,
4038 int ltype,
4039 const char *rvalue,
4040 void *data,
4041 void *userdata) {
4042
4043 const Unit *u = userdata;
4044 char*** sv = data;
4045 const char *p = rvalue;
4046 int r;
4047
4048 assert(filename);
4049 assert(lvalue);
4050 assert(rvalue);
4051 assert(data);
4052
4053 if (isempty(rvalue)) {
4054 /* Empty assignment resets the list */
4055 *sv = strv_free(*sv);
4056 return 0;
4057 }
4058
4059 for (;;) {
4060 _cleanup_free_ char *word = NULL, *resolved = NULL, *joined = NULL;
4061 const char *w;
4062 bool ignore_enoent = false, shall_prefix = false;
4063
4064 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
4065 if (r == 0)
4066 break;
4067 if (r == -ENOMEM)
4068 return log_oom();
4069 if (r < 0) {
4070 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to extract first word, ignoring: %s", rvalue);
4071 return 0;
4072 }
4073
4074 w = word;
4075 if (startswith(w, "-")) {
4076 ignore_enoent = true;
4077 w++;
4078 }
4079 if (startswith(w, "+")) {
4080 shall_prefix = true;
4081 w++;
4082 }
4083
4084 r = unit_full_printf(u, w, &resolved);
4085 if (r < 0) {
4086 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s: %m", w);
4087 continue;
4088 }
4089
4090 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
4091 if (r < 0)
4092 continue;
4093
4094 joined = strjoin(ignore_enoent ? "-" : "",
4095 shall_prefix ? "+" : "",
4096 resolved);
4097
4098 r = strv_push(sv, joined);
4099 if (r < 0)
4100 return log_oom();
4101
4102 joined = NULL;
4103 }
4104
4105 return 0;
4106 }
4107
4108 int config_parse_temporary_filesystems(
4109 const char *unit,
4110 const char *filename,
4111 unsigned line,
4112 const char *section,
4113 unsigned section_line,
4114 const char *lvalue,
4115 int ltype,
4116 const char *rvalue,
4117 void *data,
4118 void *userdata) {
4119
4120 const Unit *u = userdata;
4121 ExecContext *c = data;
4122 const char *p = rvalue;
4123 int r;
4124
4125 assert(filename);
4126 assert(lvalue);
4127 assert(rvalue);
4128 assert(data);
4129
4130 if (isempty(rvalue)) {
4131 /* Empty assignment resets the list */
4132 temporary_filesystem_free_many(c->temporary_filesystems, c->n_temporary_filesystems);
4133 c->temporary_filesystems = NULL;
4134 c->n_temporary_filesystems = 0;
4135 return 0;
4136 }
4137
4138 for (;;) {
4139 _cleanup_free_ char *word = NULL, *path = NULL, *resolved = NULL;
4140 const char *w;
4141
4142 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
4143 if (r == 0)
4144 return 0;
4145 if (r == -ENOMEM)
4146 return log_oom();
4147 if (r < 0) {
4148 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to extract first word, ignoring: %s", rvalue);
4149 return 0;
4150 }
4151
4152 w = word;
4153 r = extract_first_word(&w, &path, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
4154 if (r == -ENOMEM)
4155 return log_oom();
4156 if (r < 0) {
4157 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to extract first word, ignoring: %s", word);
4158 continue;
4159 }
4160 if (r == 0) {
4161 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid syntax, ignoring: %s", word);
4162 continue;
4163 }
4164
4165 r = unit_full_printf(u, path, &resolved);
4166 if (r < 0) {
4167 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", path);
4168 continue;
4169 }
4170
4171 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
4172 if (r < 0)
4173 continue;
4174
4175 r = temporary_filesystem_add(&c->temporary_filesystems, &c->n_temporary_filesystems, resolved, w);
4176 if (r < 0)
4177 return log_oom();
4178 }
4179 }
4180
4181 int config_parse_bind_paths(
4182 const char *unit,
4183 const char *filename,
4184 unsigned line,
4185 const char *section,
4186 unsigned section_line,
4187 const char *lvalue,
4188 int ltype,
4189 const char *rvalue,
4190 void *data,
4191 void *userdata) {
4192
4193 ExecContext *c = data;
4194 const Unit *u = userdata;
4195 const char *p;
4196 int r;
4197
4198 assert(filename);
4199 assert(lvalue);
4200 assert(rvalue);
4201 assert(data);
4202
4203 if (isempty(rvalue)) {
4204 /* Empty assignment resets the list */
4205 bind_mount_free_many(c->bind_mounts, c->n_bind_mounts);
4206 c->bind_mounts = NULL;
4207 c->n_bind_mounts = 0;
4208 return 0;
4209 }
4210
4211 p = rvalue;
4212 for (;;) {
4213 _cleanup_free_ char *source = NULL, *destination = NULL;
4214 _cleanup_free_ char *sresolved = NULL, *dresolved = NULL;
4215 char *s = NULL, *d = NULL;
4216 bool rbind = true, ignore_enoent = false;
4217
4218 r = extract_first_word(&p, &source, ":" WHITESPACE, EXTRACT_UNQUOTE|EXTRACT_DONT_COALESCE_SEPARATORS);
4219 if (r == 0)
4220 break;
4221 if (r == -ENOMEM)
4222 return log_oom();
4223 if (r < 0) {
4224 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse %s, ignoring: %s", lvalue, rvalue);
4225 return 0;
4226 }
4227
4228 r = unit_full_printf(u, source, &sresolved);
4229 if (r < 0) {
4230 log_syntax(unit, LOG_ERR, filename, line, r,
4231 "Failed to resolved unit specifiers in \"%s\", ignoring: %m", source);
4232 continue;
4233 }
4234
4235 s = sresolved;
4236 if (s[0] == '-') {
4237 ignore_enoent = true;
4238 s++;
4239 }
4240
4241 r = path_simplify_and_warn(s, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
4242 if (r < 0)
4243 continue;
4244
4245 /* Optionally, the destination is specified. */
4246 if (p && p[-1] == ':') {
4247 r = extract_first_word(&p, &destination, ":" WHITESPACE, EXTRACT_UNQUOTE|EXTRACT_DONT_COALESCE_SEPARATORS);
4248 if (r == -ENOMEM)
4249 return log_oom();
4250 if (r < 0) {
4251 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse %s, ignoring: %s", lvalue, rvalue);
4252 return 0;
4253 }
4254 if (r == 0) {
4255 log_syntax(unit, LOG_ERR, filename, line, 0, "Missing argument after ':', ignoring: %s", s);
4256 continue;
4257 }
4258
4259 r = unit_full_printf(u, destination, &dresolved);
4260 if (r < 0) {
4261 log_syntax(unit, LOG_ERR, filename, line, r,
4262 "Failed to resolved specifiers in \"%s\", ignoring: %m", destination);
4263 continue;
4264 }
4265
4266 r = path_simplify_and_warn(dresolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
4267 if (r < 0)
4268 continue;
4269
4270 d = dresolved;
4271
4272 /* Optionally, there's also a short option string specified */
4273 if (p && p[-1] == ':') {
4274 _cleanup_free_ char *options = NULL;
4275
4276 r = extract_first_word(&p, &options, NULL, EXTRACT_UNQUOTE);
4277 if (r == -ENOMEM)
4278 return log_oom();
4279 if (r < 0) {
4280 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse %s: %s", lvalue, rvalue);
4281 return 0;
4282 }
4283
4284 if (isempty(options) || streq(options, "rbind"))
4285 rbind = true;
4286 else if (streq(options, "norbind"))
4287 rbind = false;
4288 else {
4289 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid option string, ignoring setting: %s", options);
4290 continue;
4291 }
4292 }
4293 } else
4294 d = s;
4295
4296 r = bind_mount_add(&c->bind_mounts, &c->n_bind_mounts,
4297 &(BindMount) {
4298 .source = s,
4299 .destination = d,
4300 .read_only = !!strstr(lvalue, "ReadOnly"),
4301 .recursive = rbind,
4302 .ignore_enoent = ignore_enoent,
4303 });
4304 if (r < 0)
4305 return log_oom();
4306 }
4307
4308 return 0;
4309 }
4310
4311 int config_parse_job_timeout_sec(
4312 const char* unit,
4313 const char *filename,
4314 unsigned line,
4315 const char *section,
4316 unsigned section_line,
4317 const char *lvalue,
4318 int ltype,
4319 const char *rvalue,
4320 void *data,
4321 void *userdata) {
4322
4323 Unit *u = data;
4324 usec_t usec;
4325 int r;
4326
4327 assert(filename);
4328 assert(lvalue);
4329 assert(rvalue);
4330 assert(u);
4331
4332 r = parse_sec_fix_0(rvalue, &usec);
4333 if (r < 0) {
4334 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse JobTimeoutSec= parameter, ignoring: %s", rvalue);
4335 return 0;
4336 }
4337
4338 /* If the user explicitly changed JobTimeoutSec= also change JobRunningTimeoutSec=, for compatibility with old
4339 * versions. If JobRunningTimeoutSec= was explicitly set, avoid this however as whatever the user picked should
4340 * count. */
4341
4342 if (!u->job_running_timeout_set)
4343 u->job_running_timeout = usec;
4344
4345 u->job_timeout = usec;
4346
4347 return 0;
4348 }
4349
4350 int config_parse_job_running_timeout_sec(
4351 const char* unit,
4352 const char *filename,
4353 unsigned line,
4354 const char *section,
4355 unsigned section_line,
4356 const char *lvalue,
4357 int ltype,
4358 const char *rvalue,
4359 void *data,
4360 void *userdata) {
4361
4362 Unit *u = data;
4363 usec_t usec;
4364 int r;
4365
4366 assert(filename);
4367 assert(lvalue);
4368 assert(rvalue);
4369 assert(u);
4370
4371 r = parse_sec_fix_0(rvalue, &usec);
4372 if (r < 0) {
4373 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse JobRunningTimeoutSec= parameter, ignoring: %s", rvalue);
4374 return 0;
4375 }
4376
4377 u->job_running_timeout = usec;
4378 u->job_running_timeout_set = true;
4379
4380 return 0;
4381 }
4382
4383 int config_parse_emergency_action(
4384 const char* unit,
4385 const char *filename,
4386 unsigned line,
4387 const char *section,
4388 unsigned section_line,
4389 const char *lvalue,
4390 int ltype,
4391 const char *rvalue,
4392 void *data,
4393 void *userdata) {
4394
4395 Manager *m = NULL;
4396 EmergencyAction *x = data;
4397 int r;
4398
4399 assert(filename);
4400 assert(lvalue);
4401 assert(rvalue);
4402 assert(data);
4403
4404 if (unit)
4405 m = ((Unit*) userdata)->manager;
4406 else
4407 m = data;
4408
4409 r = parse_emergency_action(rvalue, MANAGER_IS_SYSTEM(m), x);
4410 if (r < 0) {
4411 if (r == -EOPNOTSUPP && MANAGER_IS_USER(m)) {
4412 /* Compat mode: remove for systemd 241. */
4413
4414 log_syntax(unit, LOG_INFO, filename, line, r,
4415 "%s= in user mode specified as \"%s\", using \"exit-force\" instead.",
4416 lvalue, rvalue);
4417 *x = EMERGENCY_ACTION_EXIT_FORCE;
4418 return 0;
4419 }
4420
4421 if (r == -EOPNOTSUPP)
4422 log_syntax(unit, LOG_ERR, filename, line, r,
4423 "%s= specified as %s mode action, ignoring: %s",
4424 lvalue, MANAGER_IS_SYSTEM(m) ? "user" : "system", rvalue);
4425 else
4426 log_syntax(unit, LOG_ERR, filename, line, r,
4427 "Failed to parse %s=, ignoring: %s", lvalue, rvalue);
4428 return 0;
4429 }
4430
4431 return 0;
4432 }
4433
4434 int config_parse_pid_file(
4435 const char *unit,
4436 const char *filename,
4437 unsigned line,
4438 const char *section,
4439 unsigned section_line,
4440 const char *lvalue,
4441 int ltype,
4442 const char *rvalue,
4443 void *data,
4444 void *userdata) {
4445
4446 _cleanup_free_ char *k = NULL, *n = NULL;
4447 const Unit *u = userdata;
4448 char **s = data;
4449 int r;
4450
4451 assert(filename);
4452 assert(lvalue);
4453 assert(rvalue);
4454 assert(u);
4455
4456 if (isempty(rvalue)) {
4457 /* An empty assignment removes already set value. */
4458 *s = mfree(*s);
4459 return 0;
4460 }
4461
4462 r = unit_full_printf(u, rvalue, &k);
4463 if (r < 0) {
4464 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
4465 return 0;
4466 }
4467
4468 /* If this is a relative path make it absolute by prefixing the /run */
4469 n = path_make_absolute(k, u->manager->prefix[EXEC_DIRECTORY_RUNTIME]);
4470 if (!n)
4471 return log_oom();
4472
4473 /* Check that the result is a sensible path */
4474 r = path_simplify_and_warn(n, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
4475 if (r < 0)
4476 return r;
4477
4478 r = patch_var_run(unit, filename, line, lvalue, &n);
4479 if (r < 0)
4480 return r;
4481
4482 free_and_replace(*s, n);
4483 return 0;
4484 }
4485
4486 int config_parse_exit_status(
4487 const char *unit,
4488 const char *filename,
4489 unsigned line,
4490 const char *section,
4491 unsigned section_line,
4492 const char *lvalue,
4493 int ltype,
4494 const char *rvalue,
4495 void *data,
4496 void *userdata) {
4497
4498 int *exit_status = data, r;
4499 uint8_t u;
4500
4501 assert(filename);
4502 assert(lvalue);
4503 assert(rvalue);
4504 assert(exit_status);
4505
4506 if (isempty(rvalue)) {
4507 *exit_status = -1;
4508 return 0;
4509 }
4510
4511 r = safe_atou8(rvalue, &u);
4512 if (r < 0) {
4513 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse exit status '%s', ignoring: %m", rvalue);
4514 return 0;
4515 }
4516
4517 *exit_status = u;
4518 return 0;
4519 }
4520
4521 int config_parse_disable_controllers(
4522 const char *unit,
4523 const char *filename,
4524 unsigned line,
4525 const char *section,
4526 unsigned section_line,
4527 const char *lvalue,
4528 int ltype,
4529 const char *rvalue,
4530 void *data,
4531 void *userdata) {
4532
4533 int r;
4534 CGroupContext *c = data;
4535 CGroupMask disabled_mask;
4536
4537 /* 1. If empty, make all controllers eligible for use again.
4538 * 2. If non-empty, merge all listed controllers, space separated. */
4539
4540 if (isempty(rvalue)) {
4541 c->disable_controllers = 0;
4542 return 0;
4543 }
4544
4545 r = cg_mask_from_string(rvalue, &disabled_mask);
4546 if (r < 0 || disabled_mask <= 0) {
4547 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid cgroup string: %s, ignoring", rvalue);
4548 return 0;
4549 }
4550
4551 c->disable_controllers |= disabled_mask;
4552
4553 return 0;
4554 }
4555
4556 int config_parse_ip_filter_bpf_progs(
4557 const char *unit,
4558 const char *filename,
4559 unsigned line,
4560 const char *section,
4561 unsigned section_line,
4562 const char *lvalue,
4563 int ltype,
4564 const char *rvalue,
4565 void *data,
4566 void *userdata) {
4567
4568 _cleanup_free_ char *resolved = NULL;
4569 const Unit *u = userdata;
4570 char ***paths = data;
4571 int r;
4572
4573 assert(filename);
4574 assert(lvalue);
4575 assert(rvalue);
4576 assert(paths);
4577
4578 if (isempty(rvalue)) {
4579 *paths = strv_free(*paths);
4580 return 0;
4581 }
4582
4583 r = unit_full_printf(u, rvalue, &resolved);
4584 if (r < 0) {
4585 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
4586 return 0;
4587 }
4588
4589 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
4590 if (r < 0)
4591 return 0;
4592
4593 if (strv_contains(*paths, resolved))
4594 return 0;
4595
4596 r = strv_extend(paths, resolved);
4597 if (r < 0)
4598 return log_oom();
4599
4600 r = bpf_firewall_supported();
4601 if (r < 0)
4602 return r;
4603 if (r != BPF_FIREWALL_SUPPORTED_WITH_MULTI) {
4604 static bool warned = false;
4605
4606 log_full(warned ? LOG_DEBUG : LOG_WARNING,
4607 "File %s:%u configures an IP firewall with BPF programs (%s=%s), but the local system does not support BPF/cgroup based firewalling with multiple filters.\n"
4608 "Starting this unit will fail! (This warning is only shown for the first loaded unit using IP firewalling.)", filename, line, lvalue, rvalue);
4609
4610 warned = true;
4611 }
4612
4613 return 0;
4614 }
4615
4616 static int merge_by_names(Unit **u, Set *names, const char *id) {
4617 char *k;
4618 int r;
4619
4620 assert(u);
4621 assert(*u);
4622
4623 /* Let's try to add in all names that are aliases of this unit */
4624 while ((k = set_steal_first(names))) {
4625 _cleanup_free_ _unused_ char *free_k = k;
4626
4627 /* First try to merge in the other name into our unit */
4628 r = unit_merge_by_name(*u, k);
4629 if (r < 0) {
4630 Unit *other;
4631
4632 /* Hmm, we couldn't merge the other unit into ours? Then let's try it the other way
4633 * round. */
4634
4635 other = manager_get_unit((*u)->manager, k);
4636 if (!other)
4637 return r; /* return previous failure */
4638
4639 r = unit_merge(other, *u);
4640 if (r < 0)
4641 return r;
4642
4643 *u = other;
4644 return merge_by_names(u, names, NULL);
4645 }
4646
4647 if (streq_ptr(id, k))
4648 unit_choose_id(*u, id);
4649 }
4650
4651 return 0;
4652 }
4653
4654 int unit_load_fragment(Unit *u) {
4655 const char *fragment;
4656 _cleanup_set_free_free_ Set *names = NULL;
4657 struct stat st;
4658 int r;
4659
4660 assert(u);
4661 assert(u->load_state == UNIT_STUB);
4662 assert(u->id);
4663
4664 if (u->transient) {
4665 u->load_state = UNIT_LOADED;
4666 return 0;
4667 }
4668
4669 /* Possibly rebuild the fragment map to catch new units */
4670 r = unit_file_build_name_map(&u->manager->lookup_paths,
4671 &u->manager->unit_cache_mtime,
4672 &u->manager->unit_id_map,
4673 &u->manager->unit_name_map,
4674 &u->manager->unit_path_cache);
4675 if (r < 0)
4676 log_error_errno(r, "Failed to rebuild name map: %m");
4677
4678 r = unit_file_find_fragment(u->manager->unit_id_map,
4679 u->manager->unit_name_map,
4680 u->id,
4681 &fragment,
4682 &names);
4683 if (r < 0 && r != -ENOENT)
4684 return r;
4685
4686 if (fragment) {
4687 /* Open the file, check if this is a mask, otherwise read. */
4688 _cleanup_fclose_ FILE *f = NULL;
4689
4690 /* Try to open the file name. A symlink is OK, for example for linked files or masks. We
4691 * expect that all symlinks within the lookup paths have been already resolved, but we don't
4692 * verify this here. */
4693 f = fopen(fragment, "re");
4694 if (!f)
4695 return log_unit_notice_errno(u, errno, "Failed to open %s: %m", fragment);
4696
4697 if (fstat(fileno(f), &st) < 0)
4698 return -errno;
4699
4700 r = free_and_strdup(&u->fragment_path, fragment);
4701 if (r < 0)
4702 return r;
4703
4704 if (null_or_empty(&st)) {
4705 u->load_state = UNIT_MASKED;
4706 u->fragment_mtime = 0;
4707 } else {
4708 u->load_state = UNIT_LOADED;
4709 u->fragment_mtime = timespec_load(&st.st_mtim);
4710
4711 /* Now, parse the file contents */
4712 r = config_parse(u->id, fragment, f,
4713 UNIT_VTABLE(u)->sections,
4714 config_item_perf_lookup, load_fragment_gperf_lookup,
4715 CONFIG_PARSE_ALLOW_INCLUDE, u);
4716 if (r == -ENOEXEC)
4717 log_unit_notice_errno(u, r, "Unit configuration has fatal error, unit will not be started.");
4718 if (r < 0)
4719 return r;
4720 }
4721 }
4722
4723 if (u->source_path) {
4724 if (stat(u->source_path, &st) >= 0)
4725 u->source_mtime = timespec_load(&st.st_mtim);
4726 else
4727 u->source_mtime = 0;
4728 }
4729
4730 /* We do the merge dance here because for some unit types, the unit might have aliases which are not
4731 * declared in the file system. In particular, this is true (and frequent) for device and swap units.
4732 */
4733 Unit *merged;
4734 const char *id = u->id;
4735 _cleanup_free_ char *free_id = NULL;
4736
4737 if (fragment) {
4738 id = basename(fragment);
4739 if (unit_name_is_valid(id, UNIT_NAME_TEMPLATE)) {
4740 assert(u->instance); /* If we're not trying to use a template for non-instanced unit,
4741 * this must be set. */
4742
4743 r = unit_name_replace_instance(id, u->instance, &free_id);
4744 if (r < 0)
4745 return log_debug_errno(r, "Failed to build id (%s + %s): %m", id, u->instance);
4746 id = free_id;
4747 }
4748 }
4749
4750 merged = u;
4751 r = merge_by_names(&merged, names, id);
4752 if (r < 0)
4753 return r;
4754
4755 if (merged != u)
4756 u->load_state = UNIT_MERGED;
4757
4758 return 0;
4759 }
4760
4761 void unit_dump_config_items(FILE *f) {
4762 static const struct {
4763 const ConfigParserCallback callback;
4764 const char *rvalue;
4765 } table[] = {
4766 { config_parse_warn_compat, "NOTSUPPORTED" },
4767 { config_parse_int, "INTEGER" },
4768 { config_parse_unsigned, "UNSIGNED" },
4769 { config_parse_iec_size, "SIZE" },
4770 { config_parse_iec_uint64, "SIZE" },
4771 { config_parse_si_size, "SIZE" },
4772 { config_parse_bool, "BOOLEAN" },
4773 { config_parse_string, "STRING" },
4774 { config_parse_path, "PATH" },
4775 { config_parse_unit_path_printf, "PATH" },
4776 { config_parse_strv, "STRING [...]" },
4777 { config_parse_exec_nice, "NICE" },
4778 { config_parse_exec_oom_score_adjust, "OOMSCOREADJUST" },
4779 { config_parse_exec_io_class, "IOCLASS" },
4780 { config_parse_exec_io_priority, "IOPRIORITY" },
4781 { config_parse_exec_cpu_sched_policy, "CPUSCHEDPOLICY" },
4782 { config_parse_exec_cpu_sched_prio, "CPUSCHEDPRIO" },
4783 { config_parse_exec_cpu_affinity, "CPUAFFINITY" },
4784 { config_parse_mode, "MODE" },
4785 { config_parse_unit_env_file, "FILE" },
4786 { config_parse_exec_output, "OUTPUT" },
4787 { config_parse_exec_input, "INPUT" },
4788 { config_parse_log_facility, "FACILITY" },
4789 { config_parse_log_level, "LEVEL" },
4790 { config_parse_exec_secure_bits, "SECUREBITS" },
4791 { config_parse_capability_set, "BOUNDINGSET" },
4792 { config_parse_rlimit, "LIMIT" },
4793 { config_parse_unit_deps, "UNIT [...]" },
4794 { config_parse_exec, "PATH [ARGUMENT [...]]" },
4795 { config_parse_service_type, "SERVICETYPE" },
4796 { config_parse_service_restart, "SERVICERESTART" },
4797 { config_parse_kill_mode, "KILLMODE" },
4798 { config_parse_signal, "SIGNAL" },
4799 { config_parse_socket_listen, "SOCKET [...]" },
4800 { config_parse_socket_bind, "SOCKETBIND" },
4801 { config_parse_socket_bindtodevice, "NETWORKINTERFACE" },
4802 { config_parse_sec, "SECONDS" },
4803 { config_parse_nsec, "NANOSECONDS" },
4804 { config_parse_namespace_path_strv, "PATH [...]" },
4805 { config_parse_bind_paths, "PATH[:PATH[:OPTIONS]] [...]" },
4806 { config_parse_unit_requires_mounts_for, "PATH [...]" },
4807 { config_parse_exec_mount_flags, "MOUNTFLAG [...]" },
4808 { config_parse_unit_string_printf, "STRING" },
4809 { config_parse_trigger_unit, "UNIT" },
4810 { config_parse_timer, "TIMER" },
4811 { config_parse_path_spec, "PATH" },
4812 { config_parse_notify_access, "ACCESS" },
4813 { config_parse_ip_tos, "TOS" },
4814 { config_parse_unit_condition_path, "CONDITION" },
4815 { config_parse_unit_condition_string, "CONDITION" },
4816 { config_parse_unit_condition_null, "CONDITION" },
4817 { config_parse_unit_slice, "SLICE" },
4818 { config_parse_documentation, "URL" },
4819 { config_parse_service_timeout, "SECONDS" },
4820 { config_parse_emergency_action, "ACTION" },
4821 { config_parse_set_status, "STATUS" },
4822 { config_parse_service_sockets, "SOCKETS" },
4823 { config_parse_environ, "ENVIRON" },
4824 #if HAVE_SECCOMP
4825 { config_parse_syscall_filter, "SYSCALLS" },
4826 { config_parse_syscall_archs, "ARCHS" },
4827 { config_parse_syscall_errno, "ERRNO" },
4828 { config_parse_address_families, "FAMILIES" },
4829 { config_parse_restrict_namespaces, "NAMESPACES" },
4830 #endif
4831 { config_parse_cpu_shares, "SHARES" },
4832 { config_parse_cg_weight, "WEIGHT" },
4833 { config_parse_memory_limit, "LIMIT" },
4834 { config_parse_device_allow, "DEVICE" },
4835 { config_parse_device_policy, "POLICY" },
4836 { config_parse_io_limit, "LIMIT" },
4837 { config_parse_io_device_weight, "DEVICEWEIGHT" },
4838 { config_parse_io_device_latency, "DEVICELATENCY" },
4839 { config_parse_blockio_bandwidth, "BANDWIDTH" },
4840 { config_parse_blockio_weight, "WEIGHT" },
4841 { config_parse_blockio_device_weight, "DEVICEWEIGHT" },
4842 { config_parse_long, "LONG" },
4843 { config_parse_socket_service, "SERVICE" },
4844 #if HAVE_SELINUX
4845 { config_parse_exec_selinux_context, "LABEL" },
4846 #endif
4847 { config_parse_job_mode, "MODE" },
4848 { config_parse_job_mode_isolate, "BOOLEAN" },
4849 { config_parse_personality, "PERSONALITY" },
4850 };
4851
4852 const char *prev = NULL;
4853 const char *i;
4854
4855 assert(f);
4856
4857 NULSTR_FOREACH(i, load_fragment_gperf_nulstr) {
4858 const char *rvalue = "OTHER", *lvalue;
4859 const ConfigPerfItem *p;
4860 size_t prefix_len;
4861 const char *dot;
4862 unsigned j;
4863
4864 assert_se(p = load_fragment_gperf_lookup(i, strlen(i)));
4865
4866 /* Hide legacy settings */
4867 if (p->parse == config_parse_warn_compat &&
4868 p->ltype == DISABLED_LEGACY)
4869 continue;
4870
4871 for (j = 0; j < ELEMENTSOF(table); j++)
4872 if (p->parse == table[j].callback) {
4873 rvalue = table[j].rvalue;
4874 break;
4875 }
4876
4877 dot = strchr(i, '.');
4878 lvalue = dot ? dot + 1 : i;
4879 prefix_len = dot-i;
4880
4881 if (dot)
4882 if (!prev || !strneq(prev, i, prefix_len+1)) {
4883 if (prev)
4884 fputc('\n', f);
4885
4886 fprintf(f, "[%.*s]\n", (int) prefix_len, i);
4887 }
4888
4889 fprintf(f, "%s=%s\n", lvalue, rvalue);
4890 prev = i;
4891 }
4892 }
4893
4894 int config_parse_cpu_affinity2(
4895 const char *unit,
4896 const char *filename,
4897 unsigned line,
4898 const char *section,
4899 unsigned section_line,
4900 const char *lvalue,
4901 int ltype,
4902 const char *rvalue,
4903 void *data,
4904 void *userdata) {
4905
4906 CPUSet *affinity = data;
4907
4908 assert(affinity);
4909
4910 (void) parse_cpu_set_extend(rvalue, affinity, true, unit, filename, line, lvalue);
4911
4912 return 0;
4913 }
4914
4915 int config_parse_show_status(
4916 const char* unit,
4917 const char *filename,
4918 unsigned line,
4919 const char *section,
4920 unsigned section_line,
4921 const char *lvalue,
4922 int ltype,
4923 const char *rvalue,
4924 void *data,
4925 void *userdata) {
4926
4927 int k;
4928 ShowStatus *b = data;
4929
4930 assert(filename);
4931 assert(lvalue);
4932 assert(rvalue);
4933 assert(data);
4934
4935 k = parse_show_status(rvalue, b);
4936 if (k < 0) {
4937 log_syntax(unit, LOG_ERR, filename, line, k, "Failed to parse show status setting, ignoring: %s", rvalue);
4938 return 0;
4939 }
4940
4941 return 0;
4942 }
4943
4944 int config_parse_output_restricted(
4945 const char* unit,
4946 const char *filename,
4947 unsigned line,
4948 const char *section,
4949 unsigned section_line,
4950 const char *lvalue,
4951 int ltype,
4952 const char *rvalue,
4953 void *data,
4954 void *userdata) {
4955
4956 ExecOutput t, *eo = data;
4957
4958 assert(filename);
4959 assert(lvalue);
4960 assert(rvalue);
4961 assert(data);
4962
4963 t = exec_output_from_string(rvalue);
4964 if (t < 0) {
4965 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse output type, ignoring: %s", rvalue);
4966 return 0;
4967 }
4968
4969 if (IN_SET(t, EXEC_OUTPUT_SOCKET, EXEC_OUTPUT_NAMED_FD, EXEC_OUTPUT_FILE, EXEC_OUTPUT_FILE_APPEND)) {
4970 log_syntax(unit, LOG_ERR, filename, line, 0, "Standard output types socket, fd:, file:, append: are not supported as defaults, ignoring: %s", rvalue);
4971 return 0;
4972 }
4973
4974 *eo = t;
4975 return 0;
4976 }
4977
4978 int config_parse_crash_chvt(
4979 const char* unit,
4980 const char *filename,
4981 unsigned line,
4982 const char *section,
4983 unsigned section_line,
4984 const char *lvalue,
4985 int ltype,
4986 const char *rvalue,
4987 void *data,
4988 void *userdata) {
4989
4990 int r;
4991
4992 assert(filename);
4993 assert(lvalue);
4994 assert(rvalue);
4995 assert(data);
4996
4997 r = parse_crash_chvt(rvalue, data);
4998 if (r < 0) {
4999 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse CrashChangeVT= setting, ignoring: %s", rvalue);
5000 return 0;
5001 }
5002
5003 return 0;
5004 }