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