]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/load-fragment.c
pid1: fix the names of AllowedCPUs= and AllowedMemoryNodes=
[thirdparty/systemd.git] / src / core / load-fragment.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright © 2012 Holger Hans Peter Freyther
4 ***/
5
6 #include <errno.h>
7 #include <fcntl.h>
8 #include <linux/fs.h>
9 #include <linux/oom.h>
10 #if HAVE_SECCOMP
11 #include <seccomp.h>
12 #endif
13 #include <sched.h>
14 #include <sys/resource.h>
15
16 #include "af-list.h"
17 #include "alloc-util.h"
18 #include "all-units.h"
19 #include "bpf-firewall.h"
20 #include "bus-error.h"
21 #include "bus-internal.h"
22 #include "bus-util.h"
23 #include "cap-list.h"
24 #include "capability-util.h"
25 #include "cgroup-setup.h"
26 #include "conf-parser.h"
27 #include "cpu-set-util.h"
28 #include "env-util.h"
29 #include "errno-list.h"
30 #include "escape.h"
31 #include "fd-util.h"
32 #include "fs-util.h"
33 #include "hexdecoct.h"
34 #include "io-util.h"
35 #include "ioprio.h"
36 #include "ip-protocol-list.h"
37 #include "journal-util.h"
38 #include "limits-util.h"
39 #include "load-fragment.h"
40 #include "log.h"
41 #include "mountpoint-util.h"
42 #include "nulstr-util.h"
43 #include "parse-util.h"
44 #include "path-util.h"
45 #include "process-util.h"
46 #if HAVE_SECCOMP
47 #include "seccomp-util.h"
48 #endif
49 #include "securebits-util.h"
50 #include "signal-util.h"
51 #include "stat-util.h"
52 #include "string-util.h"
53 #include "strv.h"
54 #include "unit-name.h"
55 #include "unit-printf.h"
56 #include "user-util.h"
57 #include "time-util.h"
58 #include "web-util.h"
59
60 static int parse_socket_protocol(const char *s) {
61 int r;
62
63 r = parse_ip_protocol(s);
64 if (r < 0)
65 return r;
66 if (!IN_SET(r, IPPROTO_UDPLITE, IPPROTO_SCTP))
67 return -EPROTONOSUPPORT;
68
69 return r;
70 }
71
72 int parse_crash_chvt(const char *value, int *data) {
73 int b;
74
75 if (safe_atoi(value, data) >= 0)
76 return 0;
77
78 b = parse_boolean(value);
79 if (b < 0)
80 return b;
81
82 if (b > 0)
83 *data = 0; /* switch to where kmsg goes */
84 else
85 *data = -1; /* turn off switching */
86
87 return 0;
88 }
89
90 int parse_confirm_spawn(const char *value, char **console) {
91 char *s;
92 int r;
93
94 r = value ? parse_boolean(value) : 1;
95 if (r == 0) {
96 *console = NULL;
97 return 0;
98 } else if (r > 0) /* on with default tty */
99 s = strdup("/dev/console");
100 else if (is_path(value)) /* on with fully qualified path */
101 s = strdup(value);
102 else /* on with only a tty file name, not a fully qualified path */
103 s = path_join("/dev/", value);
104 if (!s)
105 return -ENOMEM;
106
107 *console = s;
108 return 0;
109 }
110
111 DEFINE_CONFIG_PARSE(config_parse_socket_protocol, parse_socket_protocol, "Failed to parse socket protocol");
112 DEFINE_CONFIG_PARSE(config_parse_exec_secure_bits, secure_bits_from_string, "Failed to parse secure bits");
113 DEFINE_CONFIG_PARSE_ENUM(config_parse_collect_mode, collect_mode, CollectMode, "Failed to parse garbage collection mode");
114 DEFINE_CONFIG_PARSE_ENUM(config_parse_device_policy, cgroup_device_policy, CGroupDevicePolicy, "Failed to parse device policy");
115 DEFINE_CONFIG_PARSE_ENUM(config_parse_exec_keyring_mode, exec_keyring_mode, ExecKeyringMode, "Failed to parse keyring mode");
116 DEFINE_CONFIG_PARSE_ENUM(config_parse_exec_utmp_mode, exec_utmp_mode, ExecUtmpMode, "Failed to parse utmp mode");
117 DEFINE_CONFIG_PARSE_ENUM(config_parse_job_mode, job_mode, JobMode, "Failed to parse job mode");
118 DEFINE_CONFIG_PARSE_ENUM(config_parse_kill_mode, kill_mode, KillMode, "Failed to parse kill mode");
119 DEFINE_CONFIG_PARSE_ENUM(config_parse_notify_access, notify_access, NotifyAccess, "Failed to parse notify access specifier");
120 DEFINE_CONFIG_PARSE_ENUM(config_parse_protect_home, protect_home, ProtectHome, "Failed to parse protect home value");
121 DEFINE_CONFIG_PARSE_ENUM(config_parse_protect_system, protect_system, ProtectSystem, "Failed to parse protect system value");
122 DEFINE_CONFIG_PARSE_ENUM(config_parse_runtime_preserve_mode, exec_preserve_mode, ExecPreserveMode, "Failed to parse runtime directory preserve mode");
123 DEFINE_CONFIG_PARSE_ENUM(config_parse_service_type, service_type, ServiceType, "Failed to parse service type");
124 DEFINE_CONFIG_PARSE_ENUM(config_parse_service_restart, service_restart, ServiceRestart, "Failed to parse service restart specifier");
125 DEFINE_CONFIG_PARSE_ENUM(config_parse_socket_bind, socket_address_bind_ipv6_only_or_bool, SocketAddressBindIPv6Only, "Failed to parse bind IPv6 only value");
126 DEFINE_CONFIG_PARSE_ENUM(config_parse_oom_policy, oom_policy, OOMPolicy, "Failed to parse OOM policy");
127 DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT(config_parse_ip_tos, ip_tos, int, -1, "Failed to parse IP TOS value");
128 DEFINE_CONFIG_PARSE_PTR(config_parse_blockio_weight, cg_blkio_weight_parse, uint64_t, "Invalid block IO weight");
129 DEFINE_CONFIG_PARSE_PTR(config_parse_cg_weight, cg_weight_parse, uint64_t, "Invalid weight");
130 DEFINE_CONFIG_PARSE_PTR(config_parse_cpu_shares, cg_cpu_shares_parse, uint64_t, "Invalid CPU shares");
131 DEFINE_CONFIG_PARSE_PTR(config_parse_exec_mount_flags, mount_propagation_flags_from_string, unsigned long, "Failed to parse mount flag");
132 DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT(config_parse_numa_policy, mpol, int, -1, "Invalid NUMA policy type");
133 DEFINE_CONFIG_PARSE_ENUM(config_parse_status_unit_format, status_unit_format, StatusUnitFormat, "Failed to parse status unit format");
134
135 int config_parse_unit_deps(
136 const char *unit,
137 const char *filename,
138 unsigned line,
139 const char *section,
140 unsigned section_line,
141 const char *lvalue,
142 int ltype,
143 const char *rvalue,
144 void *data,
145 void *userdata) {
146
147 UnitDependency d = ltype;
148 Unit *u = userdata;
149 const char *p;
150
151 assert(filename);
152 assert(lvalue);
153 assert(rvalue);
154
155 p = rvalue;
156 for (;;) {
157 _cleanup_free_ char *word = NULL, *k = NULL;
158 int r;
159
160 r = extract_first_word(&p, &word, NULL, EXTRACT_RETAIN_ESCAPE);
161 if (r == 0)
162 break;
163 if (r == -ENOMEM)
164 return log_oom();
165 if (r < 0) {
166 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
167 break;
168 }
169
170 r = unit_name_printf(u, word, &k);
171 if (r < 0) {
172 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", word);
173 continue;
174 }
175
176 r = unit_add_dependency_by_name(u, d, k, true, UNIT_DEPENDENCY_FILE);
177 if (r < 0)
178 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to add dependency on %s, ignoring: %m", k);
179 }
180
181 return 0;
182 }
183
184 int config_parse_obsolete_unit_deps(
185 const char *unit,
186 const char *filename,
187 unsigned line,
188 const char *section,
189 unsigned section_line,
190 const char *lvalue,
191 int ltype,
192 const char *rvalue,
193 void *data,
194 void *userdata) {
195
196 log_syntax(unit, LOG_WARNING, filename, line, 0,
197 "Unit dependency type %s= is obsolete, replacing by %s=, please update your unit file", lvalue, unit_dependency_to_string(ltype));
198
199 return config_parse_unit_deps(unit, filename, line, section, section_line, lvalue, ltype, rvalue, data, userdata);
200 }
201
202 int config_parse_unit_string_printf(
203 const char *unit,
204 const char *filename,
205 unsigned line,
206 const char *section,
207 unsigned section_line,
208 const char *lvalue,
209 int ltype,
210 const char *rvalue,
211 void *data,
212 void *userdata) {
213
214 _cleanup_free_ char *k = NULL;
215 const Unit *u = userdata;
216 int r;
217
218 assert(filename);
219 assert(lvalue);
220 assert(rvalue);
221 assert(u);
222
223 r = unit_full_printf(u, rvalue, &k);
224 if (r < 0) {
225 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
226 return 0;
227 }
228
229 return config_parse_string(unit, filename, line, section, section_line, lvalue, ltype, k, data, userdata);
230 }
231
232 int config_parse_unit_strv_printf(
233 const char *unit,
234 const char *filename,
235 unsigned line,
236 const char *section,
237 unsigned section_line,
238 const char *lvalue,
239 int ltype,
240 const char *rvalue,
241 void *data,
242 void *userdata) {
243
244 const Unit *u = userdata;
245 _cleanup_free_ char *k = NULL;
246 int r;
247
248 assert(filename);
249 assert(lvalue);
250 assert(rvalue);
251 assert(u);
252
253 r = unit_full_printf(u, rvalue, &k);
254 if (r < 0) {
255 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
256 return 0;
257 }
258
259 return config_parse_strv(unit, filename, line, section, section_line, lvalue, ltype, k, data, userdata);
260 }
261
262 int config_parse_unit_path_printf(
263 const char *unit,
264 const char *filename,
265 unsigned line,
266 const char *section,
267 unsigned section_line,
268 const char *lvalue,
269 int ltype,
270 const char *rvalue,
271 void *data,
272 void *userdata) {
273
274 _cleanup_free_ char *k = NULL;
275 const Unit *u = userdata;
276 int r;
277 bool fatal = ltype;
278
279 assert(filename);
280 assert(lvalue);
281 assert(rvalue);
282 assert(u);
283
284 /* Let's not bother with anything that is too long */
285 if (strlen(rvalue) >= PATH_MAX) {
286 log_syntax(unit, LOG_ERR, filename, line, 0,
287 "%s value too long%s.",
288 lvalue, fatal ? "" : ", ignoring");
289 return fatal ? -ENAMETOOLONG : 0;
290 }
291
292 r = unit_full_printf(u, rvalue, &k);
293 if (r < 0) {
294 log_syntax(unit, LOG_ERR, filename, line, r,
295 "Failed to resolve unit specifiers in '%s'%s: %m",
296 rvalue, fatal ? "" : ", ignoring");
297 return fatal ? -ENOEXEC : 0;
298 }
299
300 return config_parse_path(unit, filename, line, section, section_line, lvalue, ltype, k, data, userdata);
301 }
302
303 int config_parse_unit_path_strv_printf(
304 const char *unit,
305 const char *filename,
306 unsigned line,
307 const char *section,
308 unsigned section_line,
309 const char *lvalue,
310 int ltype,
311 const char *rvalue,
312 void *data,
313 void *userdata) {
314
315 char ***x = data;
316 const Unit *u = userdata;
317 int r;
318 const char *p;
319
320 assert(filename);
321 assert(lvalue);
322 assert(rvalue);
323 assert(u);
324
325 if (isempty(rvalue)) {
326 *x = strv_free(*x);
327 return 0;
328 }
329
330 for (p = rvalue;;) {
331 _cleanup_free_ char *word = NULL, *k = NULL;
332
333 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
334 if (r == 0)
335 return 0;
336 if (r == -ENOMEM)
337 return log_oom();
338 if (r < 0) {
339 log_syntax(unit, LOG_WARNING, filename, line, r,
340 "Invalid syntax, ignoring: %s", rvalue);
341 return 0;
342 }
343
344 r = unit_full_printf(u, word, &k);
345 if (r < 0) {
346 log_syntax(unit, LOG_ERR, filename, line, r,
347 "Failed to resolve unit specifiers in '%s', ignoring: %m", word);
348 return 0;
349 }
350
351 r = path_simplify_and_warn(k, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
352 if (r < 0)
353 return 0;
354
355 r = strv_consume(x, TAKE_PTR(k));
356 if (r < 0)
357 return log_oom();
358 }
359 }
360
361 static int patch_var_run(
362 const char *unit,
363 const char *filename,
364 unsigned line,
365 const char *lvalue,
366 char **path) {
367
368 const char *e;
369 char *z;
370
371 e = path_startswith(*path, "/var/run/");
372 if (!e)
373 return 0;
374
375 z = path_join("/run/", e);
376 if (!z)
377 return log_oom();
378
379 log_syntax(unit, LOG_NOTICE, filename, line, 0,
380 "%s= references a path below legacy directory /var/run/, updating %s → %s; "
381 "please update the unit file accordingly.", lvalue, *path, z);
382
383 free_and_replace(*path, z);
384
385 return 1;
386 }
387
388 int config_parse_socket_listen(
389 const char *unit,
390 const char *filename,
391 unsigned line,
392 const char *section,
393 unsigned section_line,
394 const char *lvalue,
395 int ltype,
396 const char *rvalue,
397 void *data,
398 void *userdata) {
399
400 _cleanup_free_ SocketPort *p = NULL;
401 SocketPort *tail;
402 Socket *s;
403 int r;
404
405 assert(filename);
406 assert(lvalue);
407 assert(rvalue);
408 assert(data);
409
410 s = SOCKET(data);
411
412 if (isempty(rvalue)) {
413 /* An empty assignment removes all ports */
414 socket_free_ports(s);
415 return 0;
416 }
417
418 p = new0(SocketPort, 1);
419 if (!p)
420 return log_oom();
421
422 if (ltype != SOCKET_SOCKET) {
423 _cleanup_free_ char *k = NULL;
424
425 r = unit_full_printf(UNIT(s), rvalue, &k);
426 if (r < 0) {
427 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
428 return 0;
429 }
430
431 r = path_simplify_and_warn(k, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
432 if (r < 0)
433 return 0;
434
435 if (ltype == SOCKET_FIFO) {
436 r = patch_var_run(unit, filename, line, lvalue, &k);
437 if (r < 0)
438 return r;
439 }
440
441 free_and_replace(p->path, k);
442 p->type = ltype;
443
444 } else if (streq(lvalue, "ListenNetlink")) {
445 _cleanup_free_ char *k = NULL;
446
447 r = unit_full_printf(UNIT(s), rvalue, &k);
448 if (r < 0) {
449 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
450 return 0;
451 }
452
453 r = socket_address_parse_netlink(&p->address, k);
454 if (r < 0) {
455 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse address value in '%s', ignoring: %m", k);
456 return 0;
457 }
458
459 p->type = SOCKET_SOCKET;
460
461 } else {
462 _cleanup_free_ char *k = NULL;
463
464 r = unit_full_printf(UNIT(s), rvalue, &k);
465 if (r < 0) {
466 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
467 return 0;
468 }
469
470 if (k[0] == '/') { /* Only for AF_UNIX file system sockets… */
471 r = patch_var_run(unit, filename, line, lvalue, &k);
472 if (r < 0)
473 return r;
474 }
475
476 r = socket_address_parse_and_warn(&p->address, k);
477 if (r < 0) {
478 if (r != -EAFNOSUPPORT)
479 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse address value in '%s', ignoring: %m", k);
480 return 0;
481 }
482
483 if (streq(lvalue, "ListenStream"))
484 p->address.type = SOCK_STREAM;
485 else if (streq(lvalue, "ListenDatagram"))
486 p->address.type = SOCK_DGRAM;
487 else {
488 assert(streq(lvalue, "ListenSequentialPacket"));
489 p->address.type = SOCK_SEQPACKET;
490 }
491
492 if (socket_address_family(&p->address) != AF_LOCAL && p->address.type == SOCK_SEQPACKET) {
493 log_syntax(unit, LOG_ERR, filename, line, 0, "Address family not supported, ignoring: %s", rvalue);
494 return 0;
495 }
496
497 p->type = SOCKET_SOCKET;
498 }
499
500 p->fd = -1;
501 p->auxiliary_fds = NULL;
502 p->n_auxiliary_fds = 0;
503 p->socket = s;
504
505 LIST_FIND_TAIL(port, s->ports, tail);
506 LIST_INSERT_AFTER(port, s->ports, tail, p);
507
508 p = NULL;
509
510 return 0;
511 }
512
513 int config_parse_exec_nice(
514 const char *unit,
515 const char *filename,
516 unsigned line,
517 const char *section,
518 unsigned section_line,
519 const char *lvalue,
520 int ltype,
521 const char *rvalue,
522 void *data,
523 void *userdata) {
524
525 ExecContext *c = data;
526 int priority, r;
527
528 assert(filename);
529 assert(lvalue);
530 assert(rvalue);
531 assert(data);
532
533 if (isempty(rvalue)) {
534 c->nice_set = false;
535 return 0;
536 }
537
538 r = parse_nice(rvalue, &priority);
539 if (r < 0) {
540 if (r == -ERANGE)
541 log_syntax(unit, LOG_ERR, filename, line, r, "Nice priority out of range, ignoring: %s", rvalue);
542 else
543 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse nice priority '%s', ignoring: %m", rvalue);
544 return 0;
545 }
546
547 c->nice = priority;
548 c->nice_set = true;
549
550 return 0;
551 }
552
553 int config_parse_exec_oom_score_adjust(
554 const char* unit,
555 const char *filename,
556 unsigned line,
557 const char *section,
558 unsigned section_line,
559 const char *lvalue,
560 int ltype,
561 const char *rvalue,
562 void *data,
563 void *userdata) {
564
565 ExecContext *c = data;
566 int oa, r;
567
568 assert(filename);
569 assert(lvalue);
570 assert(rvalue);
571 assert(data);
572
573 if (isempty(rvalue)) {
574 c->oom_score_adjust_set = false;
575 return 0;
576 }
577
578 r = parse_oom_score_adjust(rvalue, &oa);
579 if (r < 0) {
580 if (r == -ERANGE)
581 log_syntax(unit, LOG_ERR, filename, line, r, "OOM score adjust value out of range, ignoring: %s", rvalue);
582 else
583 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse the OOM score adjust value '%s', ignoring: %m", rvalue);
584 return 0;
585 }
586
587 c->oom_score_adjust = oa;
588 c->oom_score_adjust_set = true;
589
590 return 0;
591 }
592
593 int config_parse_exec(
594 const char *unit,
595 const char *filename,
596 unsigned line,
597 const char *section,
598 unsigned section_line,
599 const char *lvalue,
600 int ltype,
601 const char *rvalue,
602 void *data,
603 void *userdata) {
604
605 ExecCommand **e = data;
606 const Unit *u = userdata;
607 const char *p;
608 bool semicolon;
609 int r;
610
611 assert(filename);
612 assert(lvalue);
613 assert(rvalue);
614 assert(e);
615
616 e += ltype;
617 rvalue += strspn(rvalue, WHITESPACE);
618
619 if (isempty(rvalue)) {
620 /* An empty assignment resets the list */
621 *e = exec_command_free_list(*e);
622 return 0;
623 }
624
625 p = rvalue;
626 do {
627 _cleanup_free_ char *path = NULL, *firstword = NULL;
628 ExecCommandFlags flags = 0;
629 bool ignore = false, separate_argv0 = false;
630 _cleanup_free_ ExecCommand *nce = NULL;
631 _cleanup_strv_free_ char **n = NULL;
632 size_t nlen = 0, nbufsize = 0;
633 const char *f;
634
635 semicolon = false;
636
637 r = extract_first_word_and_warn(&p, &firstword, NULL, EXTRACT_UNQUOTE|EXTRACT_CUNESCAPE, unit, filename, line, rvalue);
638 if (r <= 0)
639 return 0;
640
641 f = firstword;
642 for (;;) {
643 /* We accept an absolute path as first argument. If it's prefixed with - and the path doesn't
644 * exist, we ignore it instead of erroring out; if it's prefixed with @, we allow overriding of
645 * argv[0]; if it's prefixed with :, we will not do environment variable substitution;
646 * if it's prefixed with +, it will be run with full privileges and no sandboxing; if
647 * it's prefixed with '!' we apply sandboxing, but do not change user/group credentials; if
648 * it's prefixed with '!!', then we apply user/group credentials if the kernel supports ambient
649 * capabilities -- if it doesn't we don't apply the credentials themselves, but do apply most
650 * other sandboxing, with some special exceptions for changing UID.
651 *
652 * The idea is that '!!' may be used to write services that can take benefit of systemd's
653 * UID/GID dropping if the kernel supports ambient creds, but provide an automatic fallback to
654 * privilege dropping within the daemon if the kernel does not offer that. */
655
656 if (*f == '-' && !(flags & EXEC_COMMAND_IGNORE_FAILURE)) {
657 flags |= EXEC_COMMAND_IGNORE_FAILURE;
658 ignore = true;
659 } else if (*f == '@' && !separate_argv0)
660 separate_argv0 = true;
661 else if (*f == ':' && !(flags & EXEC_COMMAND_NO_ENV_EXPAND))
662 flags |= EXEC_COMMAND_NO_ENV_EXPAND;
663 else if (*f == '+' && !(flags & (EXEC_COMMAND_FULLY_PRIVILEGED|EXEC_COMMAND_NO_SETUID|EXEC_COMMAND_AMBIENT_MAGIC)))
664 flags |= EXEC_COMMAND_FULLY_PRIVILEGED;
665 else if (*f == '!' && !(flags & (EXEC_COMMAND_FULLY_PRIVILEGED|EXEC_COMMAND_NO_SETUID|EXEC_COMMAND_AMBIENT_MAGIC)))
666 flags |= EXEC_COMMAND_NO_SETUID;
667 else if (*f == '!' && !(flags & (EXEC_COMMAND_FULLY_PRIVILEGED|EXEC_COMMAND_AMBIENT_MAGIC))) {
668 flags &= ~EXEC_COMMAND_NO_SETUID;
669 flags |= EXEC_COMMAND_AMBIENT_MAGIC;
670 } else
671 break;
672 f++;
673 }
674
675 r = unit_full_printf(u, f, &path);
676 if (r < 0) {
677 log_syntax(unit, LOG_ERR, filename, line, r,
678 "Failed to resolve unit specifiers in '%s'%s: %m",
679 f, ignore ? ", ignoring" : "");
680 return ignore ? 0 : -ENOEXEC;
681 }
682
683 if (isempty(path)) {
684 /* First word is either "-" or "@" with no command. */
685 log_syntax(unit, LOG_ERR, filename, line, 0,
686 "Empty path in command line%s: '%s'",
687 ignore ? ", ignoring" : "", rvalue);
688 return ignore ? 0 : -ENOEXEC;
689 }
690 if (!string_is_safe(path)) {
691 log_syntax(unit, LOG_ERR, filename, line, 0,
692 "Executable name contains special characters%s: %s",
693 ignore ? ", ignoring" : "", path);
694 return ignore ? 0 : -ENOEXEC;
695 }
696 if (endswith(path, "/")) {
697 log_syntax(unit, LOG_ERR, filename, line, 0,
698 "Executable path specifies a directory%s: %s",
699 ignore ? ", ignoring" : "", path);
700 return ignore ? 0 : -ENOEXEC;
701 }
702
703 if (!path_is_absolute(path)) {
704 const char *prefix;
705 bool found = false;
706
707 if (!filename_is_valid(path)) {
708 log_syntax(unit, LOG_ERR, filename, line, 0,
709 "Neither a valid executable name nor an absolute path%s: %s",
710 ignore ? ", ignoring" : "", path);
711 return ignore ? 0 : -ENOEXEC;
712 }
713
714 /* Resolve a single-component name to a full path */
715 NULSTR_FOREACH(prefix, DEFAULT_PATH_NULSTR) {
716 _cleanup_free_ char *fullpath = NULL;
717
718 fullpath = path_join(prefix, path);
719 if (!fullpath)
720 return log_oom();
721
722 if (access(fullpath, F_OK) >= 0) {
723 free_and_replace(path, fullpath);
724 found = true;
725 break;
726 }
727 }
728
729 if (!found) {
730 log_syntax(unit, LOG_ERR, filename, line, 0,
731 "Executable \"%s\" not found in path \"%s\"%s",
732 path, DEFAULT_PATH, ignore ? ", ignoring" : "");
733 return ignore ? 0 : -ENOEXEC;
734 }
735 }
736
737 if (!separate_argv0) {
738 char *w = NULL;
739
740 if (!GREEDY_REALLOC(n, nbufsize, nlen + 2))
741 return log_oom();
742
743 w = strdup(path);
744 if (!w)
745 return log_oom();
746 n[nlen++] = w;
747 n[nlen] = NULL;
748 }
749
750 path_simplify(path, false);
751
752 while (!isempty(p)) {
753 _cleanup_free_ char *word = NULL, *resolved = NULL;
754
755 /* Check explicitly for an unquoted semicolon as
756 * command separator token. */
757 if (p[0] == ';' && (!p[1] || strchr(WHITESPACE, p[1]))) {
758 p++;
759 p += strspn(p, WHITESPACE);
760 semicolon = true;
761 break;
762 }
763
764 /* Check for \; explicitly, to not confuse it with \\; or "\;" or "\\;" etc.
765 * extract_first_word() would return the same for all of those. */
766 if (p[0] == '\\' && p[1] == ';' && (!p[2] || strchr(WHITESPACE, p[2]))) {
767 char *w;
768
769 p += 2;
770 p += strspn(p, WHITESPACE);
771
772 if (!GREEDY_REALLOC(n, nbufsize, nlen + 2))
773 return log_oom();
774
775 w = strdup(";");
776 if (!w)
777 return log_oom();
778 n[nlen++] = w;
779 n[nlen] = NULL;
780 continue;
781 }
782
783 r = extract_first_word_and_warn(&p, &word, NULL, EXTRACT_UNQUOTE|EXTRACT_CUNESCAPE, unit, filename, line, rvalue);
784 if (r == 0)
785 break;
786 if (r < 0)
787 return ignore ? 0 : -ENOEXEC;
788
789 r = unit_full_printf(u, word, &resolved);
790 if (r < 0) {
791 log_syntax(unit, LOG_ERR, filename, line, r,
792 "Failed to resolve unit specifiers in %s%s: %m",
793 word, ignore ? ", ignoring" : "");
794 return ignore ? 0 : -ENOEXEC;
795 }
796
797 if (!GREEDY_REALLOC(n, nbufsize, nlen + 2))
798 return log_oom();
799
800 n[nlen++] = TAKE_PTR(resolved);
801 n[nlen] = NULL;
802 }
803
804 if (!n || !n[0]) {
805 log_syntax(unit, LOG_ERR, filename, line, 0,
806 "Empty executable name or zeroeth argument%s: %s",
807 ignore ? ", ignoring" : "", rvalue);
808 return ignore ? 0 : -ENOEXEC;
809 }
810
811 nce = new0(ExecCommand, 1);
812 if (!nce)
813 return log_oom();
814
815 nce->argv = TAKE_PTR(n);
816 nce->path = TAKE_PTR(path);
817 nce->flags = flags;
818
819 exec_command_append_list(e, nce);
820
821 /* Do not _cleanup_free_ these. */
822 nce = NULL;
823
824 rvalue = p;
825 } while (semicolon);
826
827 return 0;
828 }
829
830 int config_parse_socket_bindtodevice(
831 const char* unit,
832 const char *filename,
833 unsigned line,
834 const char *section,
835 unsigned section_line,
836 const char *lvalue,
837 int ltype,
838 const char *rvalue,
839 void *data,
840 void *userdata) {
841
842 Socket *s = data;
843
844 assert(filename);
845 assert(lvalue);
846 assert(rvalue);
847 assert(data);
848
849 if (isempty(rvalue) || streq(rvalue, "*")) {
850 s->bind_to_device = mfree(s->bind_to_device);
851 return 0;
852 }
853
854 if (!ifname_valid(rvalue)) {
855 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid interface name, ignoring: %s", rvalue);
856 return 0;
857 }
858
859 if (free_and_strdup(&s->bind_to_device, rvalue) < 0)
860 return log_oom();
861
862 return 0;
863 }
864
865 int config_parse_exec_input(
866 const char *unit,
867 const char *filename,
868 unsigned line,
869 const char *section,
870 unsigned section_line,
871 const char *lvalue,
872 int ltype,
873 const char *rvalue,
874 void *data,
875 void *userdata) {
876
877 ExecContext *c = data;
878 const Unit *u = userdata;
879 const char *n;
880 ExecInput ei;
881 int r;
882
883 assert(data);
884 assert(filename);
885 assert(line);
886 assert(rvalue);
887
888 n = startswith(rvalue, "fd:");
889 if (n) {
890 _cleanup_free_ char *resolved = NULL;
891
892 r = unit_full_printf(u, n, &resolved);
893 if (r < 0)
894 return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s': %m", n);
895
896 if (isempty(resolved))
897 resolved = mfree(resolved);
898 else if (!fdname_is_valid(resolved)) {
899 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid file descriptor name: %s", resolved);
900 return -ENOEXEC;
901 }
902
903 free_and_replace(c->stdio_fdname[STDIN_FILENO], resolved);
904
905 ei = EXEC_INPUT_NAMED_FD;
906
907 } else if ((n = startswith(rvalue, "file:"))) {
908 _cleanup_free_ char *resolved = NULL;
909
910 r = unit_full_printf(u, n, &resolved);
911 if (r < 0)
912 return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s': %m", n);
913
914 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE | PATH_CHECK_FATAL, unit, filename, line, lvalue);
915 if (r < 0)
916 return -ENOEXEC;
917
918 free_and_replace(c->stdio_file[STDIN_FILENO], resolved);
919
920 ei = EXEC_INPUT_FILE;
921
922 } else {
923 ei = exec_input_from_string(rvalue);
924 if (ei < 0) {
925 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse input specifier, ignoring: %s", rvalue);
926 return 0;
927 }
928 }
929
930 c->std_input = ei;
931 return 0;
932 }
933
934 int config_parse_exec_input_text(
935 const char *unit,
936 const char *filename,
937 unsigned line,
938 const char *section,
939 unsigned section_line,
940 const char *lvalue,
941 int ltype,
942 const char *rvalue,
943 void *data,
944 void *userdata) {
945
946 _cleanup_free_ char *unescaped = NULL, *resolved = NULL;
947 ExecContext *c = data;
948 const Unit *u = userdata;
949 size_t sz;
950 void *p;
951 int r;
952
953 assert(data);
954 assert(filename);
955 assert(line);
956 assert(rvalue);
957
958 if (isempty(rvalue)) {
959 /* Reset if the empty string is assigned */
960 c->stdin_data = mfree(c->stdin_data);
961 c->stdin_data_size = 0;
962 return 0;
963 }
964
965 r = cunescape(rvalue, 0, &unescaped);
966 if (r < 0)
967 return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to decode C escaped text '%s': %m", rvalue);
968
969 r = unit_full_printf(u, unescaped, &resolved);
970 if (r < 0)
971 return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s': %m", unescaped);
972
973 sz = strlen(resolved);
974 if (c->stdin_data_size + sz + 1 < c->stdin_data_size || /* check for overflow */
975 c->stdin_data_size + sz + 1 > EXEC_STDIN_DATA_MAX) {
976 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);
977 return -E2BIG;
978 }
979
980 p = realloc(c->stdin_data, c->stdin_data_size + sz + 1);
981 if (!p)
982 return log_oom();
983
984 *((char*) mempcpy((char*) p + c->stdin_data_size, resolved, sz)) = '\n';
985
986 c->stdin_data = p;
987 c->stdin_data_size += sz + 1;
988
989 return 0;
990 }
991
992 int config_parse_exec_input_data(
993 const char *unit,
994 const char *filename,
995 unsigned line,
996 const char *section,
997 unsigned section_line,
998 const char *lvalue,
999 int ltype,
1000 const char *rvalue,
1001 void *data,
1002 void *userdata) {
1003
1004 _cleanup_free_ void *p = NULL;
1005 ExecContext *c = data;
1006 size_t sz;
1007 void *q;
1008 int r;
1009
1010 assert(data);
1011 assert(filename);
1012 assert(line);
1013 assert(rvalue);
1014
1015 if (isempty(rvalue)) {
1016 /* Reset if the empty string is assigned */
1017 c->stdin_data = mfree(c->stdin_data);
1018 c->stdin_data_size = 0;
1019 return 0;
1020 }
1021
1022 r = unbase64mem(rvalue, (size_t) -1, &p, &sz);
1023 if (r < 0)
1024 return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to decode base64 data, ignoring: %s", rvalue);
1025
1026 assert(sz > 0);
1027
1028 if (c->stdin_data_size + sz < c->stdin_data_size || /* check for overflow */
1029 c->stdin_data_size + sz > EXEC_STDIN_DATA_MAX) {
1030 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);
1031 return -E2BIG;
1032 }
1033
1034 q = realloc(c->stdin_data, c->stdin_data_size + sz);
1035 if (!q)
1036 return log_oom();
1037
1038 memcpy((uint8_t*) q + c->stdin_data_size, p, sz);
1039
1040 c->stdin_data = q;
1041 c->stdin_data_size += sz;
1042
1043 return 0;
1044 }
1045
1046 int config_parse_exec_output(
1047 const char *unit,
1048 const char *filename,
1049 unsigned line,
1050 const char *section,
1051 unsigned section_line,
1052 const char *lvalue,
1053 int ltype,
1054 const char *rvalue,
1055 void *data,
1056 void *userdata) {
1057
1058 _cleanup_free_ char *resolved = NULL;
1059 const char *n;
1060 ExecContext *c = data;
1061 const Unit *u = userdata;
1062 ExecOutput eo;
1063 int r;
1064
1065 assert(data);
1066 assert(filename);
1067 assert(line);
1068 assert(lvalue);
1069 assert(rvalue);
1070
1071 n = startswith(rvalue, "fd:");
1072 if (n) {
1073 r = unit_full_printf(u, n, &resolved);
1074 if (r < 0)
1075 return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s: %m", n);
1076
1077 if (isempty(resolved))
1078 resolved = mfree(resolved);
1079 else if (!fdname_is_valid(resolved)) {
1080 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid file descriptor name: %s", resolved);
1081 return -ENOEXEC;
1082 }
1083
1084 eo = EXEC_OUTPUT_NAMED_FD;
1085
1086 } else if ((n = startswith(rvalue, "file:"))) {
1087
1088 r = unit_full_printf(u, n, &resolved);
1089 if (r < 0)
1090 return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s: %m", n);
1091
1092 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE | PATH_CHECK_FATAL, unit, filename, line, lvalue);
1093 if (r < 0)
1094 return -ENOEXEC;
1095
1096 eo = EXEC_OUTPUT_FILE;
1097
1098 } else if ((n = startswith(rvalue, "append:"))) {
1099
1100 r = unit_full_printf(u, n, &resolved);
1101 if (r < 0)
1102 return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s: %m", n);
1103
1104 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE | PATH_CHECK_FATAL, unit, filename, line, lvalue);
1105 if (r < 0)
1106 return -ENOEXEC;
1107
1108 eo = EXEC_OUTPUT_FILE_APPEND;
1109 } else {
1110 eo = exec_output_from_string(rvalue);
1111 if (eo < 0) {
1112 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse output specifier, ignoring: %s", rvalue);
1113 return 0;
1114 }
1115 }
1116
1117 if (streq(lvalue, "StandardOutput")) {
1118 if (eo == EXEC_OUTPUT_NAMED_FD)
1119 free_and_replace(c->stdio_fdname[STDOUT_FILENO], resolved);
1120 else
1121 free_and_replace(c->stdio_file[STDOUT_FILENO], resolved);
1122
1123 c->std_output = eo;
1124
1125 } else {
1126 assert(streq(lvalue, "StandardError"));
1127
1128 if (eo == EXEC_OUTPUT_NAMED_FD)
1129 free_and_replace(c->stdio_fdname[STDERR_FILENO], resolved);
1130 else
1131 free_and_replace(c->stdio_file[STDERR_FILENO], resolved);
1132
1133 c->std_error = eo;
1134 }
1135
1136 return 0;
1137 }
1138
1139 int config_parse_exec_io_class(const char *unit,
1140 const char *filename,
1141 unsigned line,
1142 const char *section,
1143 unsigned section_line,
1144 const char *lvalue,
1145 int ltype,
1146 const char *rvalue,
1147 void *data,
1148 void *userdata) {
1149
1150 ExecContext *c = data;
1151 int x;
1152
1153 assert(filename);
1154 assert(lvalue);
1155 assert(rvalue);
1156 assert(data);
1157
1158 if (isempty(rvalue)) {
1159 c->ioprio_set = false;
1160 c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0);
1161 return 0;
1162 }
1163
1164 x = ioprio_class_from_string(rvalue);
1165 if (x < 0) {
1166 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse IO scheduling class, ignoring: %s", rvalue);
1167 return 0;
1168 }
1169
1170 c->ioprio = IOPRIO_PRIO_VALUE(x, IOPRIO_PRIO_DATA(c->ioprio));
1171 c->ioprio_set = true;
1172
1173 return 0;
1174 }
1175
1176 int config_parse_exec_io_priority(const char *unit,
1177 const char *filename,
1178 unsigned line,
1179 const char *section,
1180 unsigned section_line,
1181 const char *lvalue,
1182 int ltype,
1183 const char *rvalue,
1184 void *data,
1185 void *userdata) {
1186
1187 ExecContext *c = data;
1188 int i, r;
1189
1190 assert(filename);
1191 assert(lvalue);
1192 assert(rvalue);
1193 assert(data);
1194
1195 if (isempty(rvalue)) {
1196 c->ioprio_set = false;
1197 c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0);
1198 return 0;
1199 }
1200
1201 r = ioprio_parse_priority(rvalue, &i);
1202 if (r < 0) {
1203 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse IO priority, ignoring: %s", rvalue);
1204 return 0;
1205 }
1206
1207 c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_PRIO_CLASS(c->ioprio), i);
1208 c->ioprio_set = true;
1209
1210 return 0;
1211 }
1212
1213 int config_parse_exec_cpu_sched_policy(const char *unit,
1214 const char *filename,
1215 unsigned line,
1216 const char *section,
1217 unsigned section_line,
1218 const char *lvalue,
1219 int ltype,
1220 const char *rvalue,
1221 void *data,
1222 void *userdata) {
1223
1224 ExecContext *c = data;
1225 int x;
1226
1227 assert(filename);
1228 assert(lvalue);
1229 assert(rvalue);
1230 assert(data);
1231
1232 if (isempty(rvalue)) {
1233 c->cpu_sched_set = false;
1234 c->cpu_sched_policy = SCHED_OTHER;
1235 c->cpu_sched_priority = 0;
1236 return 0;
1237 }
1238
1239 x = sched_policy_from_string(rvalue);
1240 if (x < 0) {
1241 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse CPU scheduling policy, ignoring: %s", rvalue);
1242 return 0;
1243 }
1244
1245 c->cpu_sched_policy = x;
1246 /* Moving to or from real-time policy? We need to adjust the priority */
1247 c->cpu_sched_priority = CLAMP(c->cpu_sched_priority, sched_get_priority_min(x), sched_get_priority_max(x));
1248 c->cpu_sched_set = true;
1249
1250 return 0;
1251 }
1252
1253 int config_parse_numa_mask(const char *unit,
1254 const char *filename,
1255 unsigned line,
1256 const char *section,
1257 unsigned section_line,
1258 const char *lvalue,
1259 int ltype,
1260 const char *rvalue,
1261 void *data,
1262 void *userdata) {
1263 int r;
1264 NUMAPolicy *p = data;
1265
1266 assert(filename);
1267 assert(lvalue);
1268 assert(rvalue);
1269 assert(data);
1270
1271 r = parse_cpu_set_extend(rvalue, &p->nodes, true, unit, filename, line, lvalue);
1272 if (r < 0) {
1273 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse NUMA node mask, ignoring: %s", rvalue);
1274 return 0;
1275 }
1276
1277 return r;
1278 }
1279
1280 int config_parse_exec_cpu_sched_prio(const char *unit,
1281 const char *filename,
1282 unsigned line,
1283 const char *section,
1284 unsigned section_line,
1285 const char *lvalue,
1286 int ltype,
1287 const char *rvalue,
1288 void *data,
1289 void *userdata) {
1290
1291 ExecContext *c = data;
1292 int i, min, max, r;
1293
1294 assert(filename);
1295 assert(lvalue);
1296 assert(rvalue);
1297 assert(data);
1298
1299 r = safe_atoi(rvalue, &i);
1300 if (r < 0) {
1301 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse CPU scheduling priority, ignoring: %s", rvalue);
1302 return 0;
1303 }
1304
1305 /* On Linux RR/FIFO range from 1 to 99 and OTHER/BATCH may only be 0 */
1306 min = sched_get_priority_min(c->cpu_sched_policy);
1307 max = sched_get_priority_max(c->cpu_sched_policy);
1308
1309 if (i < min || i > max) {
1310 log_syntax(unit, LOG_ERR, filename, line, 0, "CPU scheduling priority is out of range, ignoring: %s", rvalue);
1311 return 0;
1312 }
1313
1314 c->cpu_sched_priority = i;
1315 c->cpu_sched_set = true;
1316
1317 return 0;
1318 }
1319
1320 int config_parse_exec_cpu_affinity(const char *unit,
1321 const char *filename,
1322 unsigned line,
1323 const char *section,
1324 unsigned section_line,
1325 const char *lvalue,
1326 int ltype,
1327 const char *rvalue,
1328 void *data,
1329 void *userdata) {
1330
1331 ExecContext *c = data;
1332
1333 assert(filename);
1334 assert(lvalue);
1335 assert(rvalue);
1336 assert(data);
1337
1338 return parse_cpu_set_extend(rvalue, &c->cpu_set, true, unit, filename, line, lvalue);
1339 }
1340
1341 int config_parse_capability_set(
1342 const char *unit,
1343 const char *filename,
1344 unsigned line,
1345 const char *section,
1346 unsigned section_line,
1347 const char *lvalue,
1348 int ltype,
1349 const char *rvalue,
1350 void *data,
1351 void *userdata) {
1352
1353 uint64_t *capability_set = data;
1354 uint64_t sum = 0, initial = 0;
1355 bool invert = false;
1356 int r;
1357
1358 assert(filename);
1359 assert(lvalue);
1360 assert(rvalue);
1361 assert(data);
1362
1363 if (rvalue[0] == '~') {
1364 invert = true;
1365 rvalue++;
1366 }
1367
1368 if (streq(lvalue, "CapabilityBoundingSet"))
1369 initial = CAP_ALL; /* initialized to all bits on */
1370 /* else "AmbientCapabilities" initialized to all bits off */
1371
1372 r = capability_set_from_string(rvalue, &sum);
1373 if (r < 0) {
1374 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse %s= specifier '%s', ignoring: %m", lvalue, rvalue);
1375 return 0;
1376 }
1377
1378 if (sum == 0 || *capability_set == initial)
1379 /* "", "~" or uninitialized data -> replace */
1380 *capability_set = invert ? ~sum : sum;
1381 else {
1382 /* previous data -> merge */
1383 if (invert)
1384 *capability_set &= ~sum;
1385 else
1386 *capability_set |= sum;
1387 }
1388
1389 return 0;
1390 }
1391
1392 int config_parse_exec_selinux_context(
1393 const char *unit,
1394 const char *filename,
1395 unsigned line,
1396 const char *section,
1397 unsigned section_line,
1398 const char *lvalue,
1399 int ltype,
1400 const char *rvalue,
1401 void *data,
1402 void *userdata) {
1403
1404 ExecContext *c = data;
1405 const Unit *u = userdata;
1406 bool ignore;
1407 char *k;
1408 int r;
1409
1410 assert(filename);
1411 assert(lvalue);
1412 assert(rvalue);
1413 assert(data);
1414
1415 if (isempty(rvalue)) {
1416 c->selinux_context = mfree(c->selinux_context);
1417 c->selinux_context_ignore = false;
1418 return 0;
1419 }
1420
1421 if (rvalue[0] == '-') {
1422 ignore = true;
1423 rvalue++;
1424 } else
1425 ignore = false;
1426
1427 r = unit_full_printf(u, rvalue, &k);
1428 if (r < 0) {
1429 log_syntax(unit, LOG_ERR, filename, line, r,
1430 "Failed to resolve unit specifiers in '%s'%s: %m",
1431 rvalue, ignore ? ", ignoring" : "");
1432 return ignore ? 0 : -ENOEXEC;
1433 }
1434
1435 free_and_replace(c->selinux_context, k);
1436 c->selinux_context_ignore = ignore;
1437
1438 return 0;
1439 }
1440
1441 int config_parse_exec_apparmor_profile(
1442 const char *unit,
1443 const char *filename,
1444 unsigned line,
1445 const char *section,
1446 unsigned section_line,
1447 const char *lvalue,
1448 int ltype,
1449 const char *rvalue,
1450 void *data,
1451 void *userdata) {
1452
1453 ExecContext *c = data;
1454 const Unit *u = userdata;
1455 bool ignore;
1456 char *k;
1457 int r;
1458
1459 assert(filename);
1460 assert(lvalue);
1461 assert(rvalue);
1462 assert(data);
1463
1464 if (isempty(rvalue)) {
1465 c->apparmor_profile = mfree(c->apparmor_profile);
1466 c->apparmor_profile_ignore = false;
1467 return 0;
1468 }
1469
1470 if (rvalue[0] == '-') {
1471 ignore = true;
1472 rvalue++;
1473 } else
1474 ignore = false;
1475
1476 r = unit_full_printf(u, rvalue, &k);
1477 if (r < 0) {
1478 log_syntax(unit, LOG_ERR, filename, line, r,
1479 "Failed to resolve unit specifiers in '%s'%s: %m",
1480 rvalue, ignore ? ", ignoring" : "");
1481 return ignore ? 0 : -ENOEXEC;
1482 }
1483
1484 free_and_replace(c->apparmor_profile, k);
1485 c->apparmor_profile_ignore = ignore;
1486
1487 return 0;
1488 }
1489
1490 int config_parse_exec_smack_process_label(
1491 const char *unit,
1492 const char *filename,
1493 unsigned line,
1494 const char *section,
1495 unsigned section_line,
1496 const char *lvalue,
1497 int ltype,
1498 const char *rvalue,
1499 void *data,
1500 void *userdata) {
1501
1502 ExecContext *c = data;
1503 const Unit *u = userdata;
1504 bool ignore;
1505 char *k;
1506 int r;
1507
1508 assert(filename);
1509 assert(lvalue);
1510 assert(rvalue);
1511 assert(data);
1512
1513 if (isempty(rvalue)) {
1514 c->smack_process_label = mfree(c->smack_process_label);
1515 c->smack_process_label_ignore = false;
1516 return 0;
1517 }
1518
1519 if (rvalue[0] == '-') {
1520 ignore = true;
1521 rvalue++;
1522 } else
1523 ignore = false;
1524
1525 r = unit_full_printf(u, rvalue, &k);
1526 if (r < 0) {
1527 log_syntax(unit, LOG_ERR, filename, line, r,
1528 "Failed to resolve unit specifiers in '%s'%s: %m",
1529 rvalue, ignore ? ", ignoring" : "");
1530 return ignore ? 0 : -ENOEXEC;
1531 }
1532
1533 free_and_replace(c->smack_process_label, k);
1534 c->smack_process_label_ignore = ignore;
1535
1536 return 0;
1537 }
1538
1539 int config_parse_timer(
1540 const char *unit,
1541 const char *filename,
1542 unsigned line,
1543 const char *section,
1544 unsigned section_line,
1545 const char *lvalue,
1546 int ltype,
1547 const char *rvalue,
1548 void *data,
1549 void *userdata) {
1550
1551 _cleanup_(calendar_spec_freep) CalendarSpec *c = NULL;
1552 _cleanup_free_ char *k = NULL;
1553 const Unit *u = userdata;
1554 Timer *t = data;
1555 usec_t usec = 0;
1556 TimerValue *v;
1557 int r;
1558
1559 assert(filename);
1560 assert(lvalue);
1561 assert(rvalue);
1562 assert(data);
1563
1564 if (isempty(rvalue)) {
1565 /* Empty assignment resets list */
1566 timer_free_values(t);
1567 return 0;
1568 }
1569
1570 r = unit_full_printf(u, rvalue, &k);
1571 if (r < 0) {
1572 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
1573 return 0;
1574 }
1575
1576 if (ltype == TIMER_CALENDAR) {
1577 r = calendar_spec_from_string(k, &c);
1578 if (r < 0) {
1579 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse calendar specification, ignoring: %s", k);
1580 return 0;
1581 }
1582 } else {
1583 r = parse_sec(k, &usec);
1584 if (r < 0) {
1585 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse timer value, ignoring: %s", k);
1586 return 0;
1587 }
1588 }
1589
1590 v = new(TimerValue, 1);
1591 if (!v)
1592 return log_oom();
1593
1594 *v = (TimerValue) {
1595 .base = ltype,
1596 .value = usec,
1597 .calendar_spec = TAKE_PTR(c),
1598 };
1599
1600 LIST_PREPEND(value, t->values, v);
1601
1602 return 0;
1603 }
1604
1605 int config_parse_trigger_unit(
1606 const char *unit,
1607 const char *filename,
1608 unsigned line,
1609 const char *section,
1610 unsigned section_line,
1611 const char *lvalue,
1612 int ltype,
1613 const char *rvalue,
1614 void *data,
1615 void *userdata) {
1616
1617 _cleanup_free_ char *p = NULL;
1618 Unit *u = data;
1619 UnitType type;
1620 int r;
1621
1622 assert(filename);
1623 assert(lvalue);
1624 assert(rvalue);
1625 assert(data);
1626
1627 if (!hashmap_isempty(u->dependencies[UNIT_TRIGGERS])) {
1628 log_syntax(unit, LOG_ERR, filename, line, 0, "Multiple units to trigger specified, ignoring: %s", rvalue);
1629 return 0;
1630 }
1631
1632 r = unit_name_printf(u, rvalue, &p);
1633 if (r < 0) {
1634 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue);
1635 return 0;
1636 }
1637
1638 type = unit_name_to_type(p);
1639 if (type < 0) {
1640 log_syntax(unit, LOG_ERR, filename, line, 0, "Unit type not valid, ignoring: %s", rvalue);
1641 return 0;
1642 }
1643 if (unit_has_name(u, p)) {
1644 log_syntax(unit, LOG_ERR, filename, line, 0, "Units cannot trigger themselves, ignoring: %s", rvalue);
1645 return 0;
1646 }
1647
1648 r = unit_add_two_dependencies_by_name(u, UNIT_BEFORE, UNIT_TRIGGERS, p, true, UNIT_DEPENDENCY_FILE);
1649 if (r < 0) {
1650 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to add trigger on %s, ignoring: %m", p);
1651 return 0;
1652 }
1653
1654 return 0;
1655 }
1656
1657 int config_parse_path_spec(const char *unit,
1658 const char *filename,
1659 unsigned line,
1660 const char *section,
1661 unsigned section_line,
1662 const char *lvalue,
1663 int ltype,
1664 const char *rvalue,
1665 void *data,
1666 void *userdata) {
1667
1668 Path *p = data;
1669 PathSpec *s;
1670 PathType b;
1671 _cleanup_free_ char *k = NULL;
1672 int r;
1673
1674 assert(filename);
1675 assert(lvalue);
1676 assert(rvalue);
1677 assert(data);
1678
1679 if (isempty(rvalue)) {
1680 /* Empty assignment clears list */
1681 path_free_specs(p);
1682 return 0;
1683 }
1684
1685 b = path_type_from_string(lvalue);
1686 if (b < 0) {
1687 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse path type, ignoring: %s", lvalue);
1688 return 0;
1689 }
1690
1691 r = unit_full_printf(UNIT(p), rvalue, &k);
1692 if (r < 0) {
1693 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue);
1694 return 0;
1695 }
1696
1697 r = path_simplify_and_warn(k, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
1698 if (r < 0)
1699 return 0;
1700
1701 s = new0(PathSpec, 1);
1702 if (!s)
1703 return log_oom();
1704
1705 s->unit = UNIT(p);
1706 s->path = TAKE_PTR(k);
1707 s->type = b;
1708 s->inotify_fd = -1;
1709
1710 LIST_PREPEND(spec, p->specs, s);
1711
1712 return 0;
1713 }
1714
1715 int config_parse_socket_service(
1716 const char *unit,
1717 const char *filename,
1718 unsigned line,
1719 const char *section,
1720 unsigned section_line,
1721 const char *lvalue,
1722 int ltype,
1723 const char *rvalue,
1724 void *data,
1725 void *userdata) {
1726
1727 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1728 _cleanup_free_ char *p = NULL;
1729 Socket *s = data;
1730 Unit *x;
1731 int r;
1732
1733 assert(filename);
1734 assert(lvalue);
1735 assert(rvalue);
1736 assert(data);
1737
1738 r = unit_name_printf(UNIT(s), rvalue, &p);
1739 if (r < 0) {
1740 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s: %m", rvalue);
1741 return -ENOEXEC;
1742 }
1743
1744 if (!endswith(p, ".service")) {
1745 log_syntax(unit, LOG_ERR, filename, line, 0, "Unit must be of type service: %s", rvalue);
1746 return -ENOEXEC;
1747 }
1748
1749 r = manager_load_unit(UNIT(s)->manager, p, NULL, &error, &x);
1750 if (r < 0) {
1751 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to load unit %s: %s", rvalue, bus_error_message(&error, r));
1752 return -ENOEXEC;
1753 }
1754
1755 unit_ref_set(&s->service, UNIT(s), x);
1756
1757 return 0;
1758 }
1759
1760 int config_parse_fdname(
1761 const char *unit,
1762 const char *filename,
1763 unsigned line,
1764 const char *section,
1765 unsigned section_line,
1766 const char *lvalue,
1767 int ltype,
1768 const char *rvalue,
1769 void *data,
1770 void *userdata) {
1771
1772 _cleanup_free_ char *p = NULL;
1773 Socket *s = data;
1774 int r;
1775
1776 assert(filename);
1777 assert(lvalue);
1778 assert(rvalue);
1779 assert(data);
1780
1781 if (isempty(rvalue)) {
1782 s->fdname = mfree(s->fdname);
1783 return 0;
1784 }
1785
1786 r = unit_full_printf(UNIT(s), rvalue, &p);
1787 if (r < 0) {
1788 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
1789 return 0;
1790 }
1791
1792 if (!fdname_is_valid(p)) {
1793 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid file descriptor name, ignoring: %s", p);
1794 return 0;
1795 }
1796
1797 return free_and_replace(s->fdname, p);
1798 }
1799
1800 int config_parse_service_sockets(
1801 const char *unit,
1802 const char *filename,
1803 unsigned line,
1804 const char *section,
1805 unsigned section_line,
1806 const char *lvalue,
1807 int ltype,
1808 const char *rvalue,
1809 void *data,
1810 void *userdata) {
1811
1812 Service *s = data;
1813 const char *p;
1814 int r;
1815
1816 assert(filename);
1817 assert(lvalue);
1818 assert(rvalue);
1819 assert(data);
1820
1821 p = rvalue;
1822 for (;;) {
1823 _cleanup_free_ char *word = NULL, *k = NULL;
1824
1825 r = extract_first_word(&p, &word, NULL, 0);
1826 if (r == 0)
1827 break;
1828 if (r == -ENOMEM)
1829 return log_oom();
1830 if (r < 0) {
1831 log_syntax(unit, LOG_ERR, filename, line, r, "Trailing garbage in sockets, ignoring: %s", rvalue);
1832 break;
1833 }
1834
1835 r = unit_name_printf(UNIT(s), word, &k);
1836 if (r < 0) {
1837 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", word);
1838 continue;
1839 }
1840
1841 if (!endswith(k, ".socket")) {
1842 log_syntax(unit, LOG_ERR, filename, line, 0, "Unit must be of type socket, ignoring: %s", k);
1843 continue;
1844 }
1845
1846 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_WANTS, UNIT_AFTER, k, true, UNIT_DEPENDENCY_FILE);
1847 if (r < 0)
1848 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to add dependency on %s, ignoring: %m", k);
1849
1850 r = unit_add_dependency_by_name(UNIT(s), UNIT_TRIGGERED_BY, k, true, UNIT_DEPENDENCY_FILE);
1851 if (r < 0)
1852 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to add dependency on %s, ignoring: %m", k);
1853 }
1854
1855 return 0;
1856 }
1857
1858 int config_parse_bus_name(
1859 const char *unit,
1860 const char *filename,
1861 unsigned line,
1862 const char *section,
1863 unsigned section_line,
1864 const char *lvalue,
1865 int ltype,
1866 const char *rvalue,
1867 void *data,
1868 void *userdata) {
1869
1870 _cleanup_free_ char *k = NULL;
1871 const Unit *u = userdata;
1872 int r;
1873
1874 assert(filename);
1875 assert(lvalue);
1876 assert(rvalue);
1877 assert(u);
1878
1879 r = unit_full_printf(u, rvalue, &k);
1880 if (r < 0) {
1881 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue);
1882 return 0;
1883 }
1884
1885 if (!service_name_is_valid(k)) {
1886 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid bus name, ignoring: %s", k);
1887 return 0;
1888 }
1889
1890 return config_parse_string(unit, filename, line, section, section_line, lvalue, ltype, k, data, userdata);
1891 }
1892
1893 int config_parse_service_timeout(
1894 const char *unit,
1895 const char *filename,
1896 unsigned line,
1897 const char *section,
1898 unsigned section_line,
1899 const char *lvalue,
1900 int ltype,
1901 const char *rvalue,
1902 void *data,
1903 void *userdata) {
1904
1905 Service *s = userdata;
1906 usec_t usec;
1907 int r;
1908
1909 assert(filename);
1910 assert(lvalue);
1911 assert(rvalue);
1912 assert(s);
1913
1914 /* This is called for two cases: TimeoutSec= and TimeoutStartSec=. */
1915
1916 /* Traditionally, these options accepted 0 to disable the timeouts. However, a timeout of 0 suggests it happens
1917 * immediately, hence fix this to become USEC_INFINITY instead. This is in-line with how we internally handle
1918 * all other timeouts. */
1919 r = parse_sec_fix_0(rvalue, &usec);
1920 if (r < 0) {
1921 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse %s= parameter, ignoring: %s", lvalue, rvalue);
1922 return 0;
1923 }
1924
1925 s->start_timeout_defined = true;
1926 s->timeout_start_usec = usec;
1927
1928 if (streq(lvalue, "TimeoutSec"))
1929 s->timeout_stop_usec = usec;
1930
1931 return 0;
1932 }
1933
1934 int config_parse_service_timeout_abort(
1935 const char *unit,
1936 const char *filename,
1937 unsigned line,
1938 const char *section,
1939 unsigned section_line,
1940 const char *lvalue,
1941 int ltype,
1942 const char *rvalue,
1943 void *data,
1944 void *userdata) {
1945
1946 Service *s = userdata;
1947 int r;
1948
1949 assert(filename);
1950 assert(lvalue);
1951 assert(rvalue);
1952 assert(s);
1953
1954 rvalue += strspn(rvalue, WHITESPACE);
1955 if (isempty(rvalue)) {
1956 s->timeout_abort_set = false;
1957 return 0;
1958 }
1959
1960 r = parse_sec(rvalue, &s->timeout_abort_usec);
1961 if (r < 0) {
1962 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse TimeoutAbortSec= setting, ignoring: %s", rvalue);
1963 return 0;
1964 }
1965
1966 s->timeout_abort_set = true;
1967 return 0;
1968 }
1969
1970 int config_parse_sec_fix_0(
1971 const char *unit,
1972 const char *filename,
1973 unsigned line,
1974 const char *section,
1975 unsigned section_line,
1976 const char *lvalue,
1977 int ltype,
1978 const char *rvalue,
1979 void *data,
1980 void *userdata) {
1981
1982 usec_t *usec = data;
1983 int r;
1984
1985 assert(filename);
1986 assert(lvalue);
1987 assert(rvalue);
1988 assert(usec);
1989
1990 /* This is pretty much like config_parse_sec(), except that this treats a time of 0 as infinity, for
1991 * compatibility with older versions of systemd where 0 instead of infinity was used as indicator to turn off a
1992 * timeout. */
1993
1994 r = parse_sec_fix_0(rvalue, usec);
1995 if (r < 0) {
1996 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse %s= parameter, ignoring: %s", lvalue, rvalue);
1997 return 0;
1998 }
1999
2000 return 0;
2001 }
2002
2003 int config_parse_user_group_compat(
2004 const char *unit,
2005 const char *filename,
2006 unsigned line,
2007 const char *section,
2008 unsigned section_line,
2009 const char *lvalue,
2010 int ltype,
2011 const char *rvalue,
2012 void *data,
2013 void *userdata) {
2014
2015 _cleanup_free_ char *k = NULL;
2016 char **user = data;
2017 const Unit *u = userdata;
2018 int r;
2019
2020 assert(filename);
2021 assert(lvalue);
2022 assert(rvalue);
2023 assert(u);
2024
2025 if (isempty(rvalue)) {
2026 *user = mfree(*user);
2027 return 0;
2028 }
2029
2030 r = unit_full_printf(u, rvalue, &k);
2031 if (r < 0) {
2032 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s: %m", rvalue);
2033 return -ENOEXEC;
2034 }
2035
2036 if (!valid_user_group_name_or_id_compat(k)) {
2037 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid user/group name or numeric ID: %s", k);
2038 return -ENOEXEC;
2039 }
2040
2041 return free_and_replace(*user, k);
2042 }
2043
2044 int config_parse_user_group_strv_compat(
2045 const char *unit,
2046 const char *filename,
2047 unsigned line,
2048 const char *section,
2049 unsigned section_line,
2050 const char *lvalue,
2051 int ltype,
2052 const char *rvalue,
2053 void *data,
2054 void *userdata) {
2055
2056 char ***users = data;
2057 const Unit *u = userdata;
2058 const char *p = rvalue;
2059 int r;
2060
2061 assert(filename);
2062 assert(lvalue);
2063 assert(rvalue);
2064 assert(u);
2065
2066 if (isempty(rvalue)) {
2067 *users = strv_free(*users);
2068 return 0;
2069 }
2070
2071 for (;;) {
2072 _cleanup_free_ char *word = NULL, *k = NULL;
2073
2074 r = extract_first_word(&p, &word, NULL, 0);
2075 if (r == 0)
2076 break;
2077 if (r == -ENOMEM)
2078 return log_oom();
2079 if (r < 0) {
2080 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid syntax: %s", rvalue);
2081 return -ENOEXEC;
2082 }
2083
2084 r = unit_full_printf(u, word, &k);
2085 if (r < 0) {
2086 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s: %m", word);
2087 return -ENOEXEC;
2088 }
2089
2090 if (!valid_user_group_name_or_id_compat(k)) {
2091 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid user/group name or numeric ID: %s", k);
2092 return -ENOEXEC;
2093 }
2094
2095 r = strv_push(users, k);
2096 if (r < 0)
2097 return log_oom();
2098
2099 k = NULL;
2100 }
2101
2102 return 0;
2103 }
2104
2105 int config_parse_working_directory(
2106 const char *unit,
2107 const char *filename,
2108 unsigned line,
2109 const char *section,
2110 unsigned section_line,
2111 const char *lvalue,
2112 int ltype,
2113 const char *rvalue,
2114 void *data,
2115 void *userdata) {
2116
2117 ExecContext *c = data;
2118 const Unit *u = userdata;
2119 bool missing_ok;
2120 int r;
2121
2122 assert(filename);
2123 assert(lvalue);
2124 assert(rvalue);
2125 assert(c);
2126 assert(u);
2127
2128 if (isempty(rvalue)) {
2129 c->working_directory_home = false;
2130 c->working_directory = mfree(c->working_directory);
2131 return 0;
2132 }
2133
2134 if (rvalue[0] == '-') {
2135 missing_ok = true;
2136 rvalue++;
2137 } else
2138 missing_ok = false;
2139
2140 if (streq(rvalue, "~")) {
2141 c->working_directory_home = true;
2142 c->working_directory = mfree(c->working_directory);
2143 } else {
2144 _cleanup_free_ char *k = NULL;
2145
2146 r = unit_full_printf(u, rvalue, &k);
2147 if (r < 0) {
2148 log_syntax(unit, LOG_ERR, filename, line, r,
2149 "Failed to resolve unit specifiers in working directory path '%s'%s: %m",
2150 rvalue, missing_ok ? ", ignoring" : "");
2151 return missing_ok ? 0 : -ENOEXEC;
2152 }
2153
2154 r = path_simplify_and_warn(k, PATH_CHECK_ABSOLUTE | (missing_ok ? 0 : PATH_CHECK_FATAL), unit, filename, line, lvalue);
2155 if (r < 0)
2156 return missing_ok ? 0 : -ENOEXEC;
2157
2158 c->working_directory_home = false;
2159 free_and_replace(c->working_directory, k);
2160 }
2161
2162 c->working_directory_missing_ok = missing_ok;
2163 return 0;
2164 }
2165
2166 int config_parse_unit_env_file(const char *unit,
2167 const char *filename,
2168 unsigned line,
2169 const char *section,
2170 unsigned section_line,
2171 const char *lvalue,
2172 int ltype,
2173 const char *rvalue,
2174 void *data,
2175 void *userdata) {
2176
2177 char ***env = data;
2178 const Unit *u = userdata;
2179 _cleanup_free_ char *n = NULL;
2180 int r;
2181
2182 assert(filename);
2183 assert(lvalue);
2184 assert(rvalue);
2185 assert(data);
2186
2187 if (isempty(rvalue)) {
2188 /* Empty assignment frees the list */
2189 *env = strv_free(*env);
2190 return 0;
2191 }
2192
2193 r = unit_full_printf(u, rvalue, &n);
2194 if (r < 0) {
2195 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
2196 return 0;
2197 }
2198
2199 r = path_simplify_and_warn(n[0] == '-' ? n + 1 : n, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
2200 if (r < 0)
2201 return 0;
2202
2203 r = strv_push(env, n);
2204 if (r < 0)
2205 return log_oom();
2206
2207 n = NULL;
2208
2209 return 0;
2210 }
2211
2212 int config_parse_environ(
2213 const char *unit,
2214 const char *filename,
2215 unsigned line,
2216 const char *section,
2217 unsigned section_line,
2218 const char *lvalue,
2219 int ltype,
2220 const char *rvalue,
2221 void *data,
2222 void *userdata) {
2223
2224 const Unit *u = userdata;
2225 char ***env = data;
2226 const char *p;
2227 int r;
2228
2229 assert(filename);
2230 assert(lvalue);
2231 assert(rvalue);
2232 assert(data);
2233
2234 if (isempty(rvalue)) {
2235 /* Empty assignment resets the list */
2236 *env = strv_free(*env);
2237 return 0;
2238 }
2239
2240 for (p = rvalue;; ) {
2241 _cleanup_free_ char *word = NULL, *k = NULL;
2242
2243 r = extract_first_word(&p, &word, NULL, EXTRACT_CUNESCAPE|EXTRACT_UNQUOTE);
2244 if (r == 0)
2245 return 0;
2246 if (r == -ENOMEM)
2247 return log_oom();
2248 if (r < 0) {
2249 log_syntax(unit, LOG_WARNING, filename, line, r,
2250 "Invalid syntax, ignoring: %s", rvalue);
2251 return 0;
2252 }
2253
2254 if (u) {
2255 r = unit_full_printf(u, word, &k);
2256 if (r < 0) {
2257 log_syntax(unit, LOG_ERR, filename, line, r,
2258 "Failed to resolve unit specifiers in %s, ignoring: %m", word);
2259 continue;
2260 }
2261 } else
2262 k = TAKE_PTR(word);
2263
2264 if (!env_assignment_is_valid(k)) {
2265 log_syntax(unit, LOG_ERR, filename, line, 0,
2266 "Invalid environment assignment, ignoring: %s", k);
2267 continue;
2268 }
2269
2270 r = strv_env_replace(env, k);
2271 if (r < 0)
2272 return log_oom();
2273
2274 k = NULL;
2275 }
2276 }
2277
2278 int config_parse_pass_environ(
2279 const char *unit,
2280 const char *filename,
2281 unsigned line,
2282 const char *section,
2283 unsigned section_line,
2284 const char *lvalue,
2285 int ltype,
2286 const char *rvalue,
2287 void *data,
2288 void *userdata) {
2289
2290 _cleanup_strv_free_ char **n = NULL;
2291 size_t nlen = 0, nbufsize = 0;
2292 char*** passenv = data;
2293 const char *p = rvalue;
2294 const Unit *u = userdata;
2295 int r;
2296
2297 assert(filename);
2298 assert(lvalue);
2299 assert(rvalue);
2300 assert(data);
2301
2302 if (isempty(rvalue)) {
2303 /* Empty assignment resets the list */
2304 *passenv = strv_free(*passenv);
2305 return 0;
2306 }
2307
2308 for (;;) {
2309 _cleanup_free_ char *word = NULL, *k = NULL;
2310
2311 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
2312 if (r == 0)
2313 break;
2314 if (r == -ENOMEM)
2315 return log_oom();
2316 if (r < 0) {
2317 log_syntax(unit, LOG_ERR, filename, line, r,
2318 "Trailing garbage in %s, ignoring: %s", lvalue, rvalue);
2319 break;
2320 }
2321
2322 if (u) {
2323 r = unit_full_printf(u, word, &k);
2324 if (r < 0) {
2325 log_syntax(unit, LOG_ERR, filename, line, r,
2326 "Failed to resolve specifiers in %s, ignoring: %m", word);
2327 continue;
2328 }
2329 } else
2330 k = TAKE_PTR(word);
2331
2332 if (!env_name_is_valid(k)) {
2333 log_syntax(unit, LOG_ERR, filename, line, 0,
2334 "Invalid environment name for %s, ignoring: %s", lvalue, k);
2335 continue;
2336 }
2337
2338 if (!GREEDY_REALLOC(n, nbufsize, nlen + 2))
2339 return log_oom();
2340
2341 n[nlen++] = TAKE_PTR(k);
2342 n[nlen] = NULL;
2343 }
2344
2345 if (n) {
2346 r = strv_extend_strv(passenv, n, true);
2347 if (r < 0)
2348 return r;
2349 }
2350
2351 return 0;
2352 }
2353
2354 int config_parse_unset_environ(
2355 const char *unit,
2356 const char *filename,
2357 unsigned line,
2358 const char *section,
2359 unsigned section_line,
2360 const char *lvalue,
2361 int ltype,
2362 const char *rvalue,
2363 void *data,
2364 void *userdata) {
2365
2366 _cleanup_strv_free_ char **n = NULL;
2367 size_t nlen = 0, nbufsize = 0;
2368 char*** unsetenv = data;
2369 const char *p = rvalue;
2370 const Unit *u = userdata;
2371 int r;
2372
2373 assert(filename);
2374 assert(lvalue);
2375 assert(rvalue);
2376 assert(data);
2377
2378 if (isempty(rvalue)) {
2379 /* Empty assignment resets the list */
2380 *unsetenv = strv_free(*unsetenv);
2381 return 0;
2382 }
2383
2384 for (;;) {
2385 _cleanup_free_ char *word = NULL, *k = NULL;
2386
2387 r = extract_first_word(&p, &word, NULL, EXTRACT_CUNESCAPE|EXTRACT_UNQUOTE);
2388 if (r == 0)
2389 break;
2390 if (r == -ENOMEM)
2391 return log_oom();
2392 if (r < 0) {
2393 log_syntax(unit, LOG_ERR, filename, line, r,
2394 "Trailing garbage in %s, ignoring: %s", lvalue, rvalue);
2395 break;
2396 }
2397
2398 if (u) {
2399 r = unit_full_printf(u, word, &k);
2400 if (r < 0) {
2401 log_syntax(unit, LOG_ERR, filename, line, r,
2402 "Failed to resolve unit specifiers in %s, ignoring: %m", word);
2403 continue;
2404 }
2405 } else
2406 k = TAKE_PTR(word);
2407
2408 if (!env_assignment_is_valid(k) && !env_name_is_valid(k)) {
2409 log_syntax(unit, LOG_ERR, filename, line, 0,
2410 "Invalid environment name or assignment %s, ignoring: %s", lvalue, k);
2411 continue;
2412 }
2413
2414 if (!GREEDY_REALLOC(n, nbufsize, nlen + 2))
2415 return log_oom();
2416
2417 n[nlen++] = TAKE_PTR(k);
2418 n[nlen] = NULL;
2419 }
2420
2421 if (n) {
2422 r = strv_extend_strv(unsetenv, n, true);
2423 if (r < 0)
2424 return r;
2425 }
2426
2427 return 0;
2428 }
2429
2430 int config_parse_log_extra_fields(
2431 const char *unit,
2432 const char *filename,
2433 unsigned line,
2434 const char *section,
2435 unsigned section_line,
2436 const char *lvalue,
2437 int ltype,
2438 const char *rvalue,
2439 void *data,
2440 void *userdata) {
2441
2442 ExecContext *c = data;
2443 const Unit *u = userdata;
2444 const char *p = rvalue;
2445 int r;
2446
2447 assert(filename);
2448 assert(lvalue);
2449 assert(rvalue);
2450 assert(c);
2451
2452 if (isempty(rvalue)) {
2453 exec_context_free_log_extra_fields(c);
2454 return 0;
2455 }
2456
2457 for (;;) {
2458 _cleanup_free_ char *word = NULL, *k = NULL;
2459 struct iovec *t;
2460 const char *eq;
2461
2462 r = extract_first_word(&p, &word, NULL, EXTRACT_CUNESCAPE|EXTRACT_UNQUOTE);
2463 if (r == 0)
2464 return 0;
2465 if (r == -ENOMEM)
2466 return log_oom();
2467 if (r < 0) {
2468 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
2469 return 0;
2470 }
2471
2472 r = unit_full_printf(u, word, &k);
2473 if (r < 0) {
2474 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", word);
2475 continue;
2476 }
2477
2478 eq = strchr(k, '=');
2479 if (!eq) {
2480 log_syntax(unit, LOG_ERR, filename, line, 0, "Log field lacks '=' character, ignoring: %s", k);
2481 continue;
2482 }
2483
2484 if (!journal_field_valid(k, eq-k, false)) {
2485 log_syntax(unit, LOG_ERR, filename, line, 0, "Log field name is invalid, ignoring: %s", k);
2486 continue;
2487 }
2488
2489 t = reallocarray(c->log_extra_fields, c->n_log_extra_fields+1, sizeof(struct iovec));
2490 if (!t)
2491 return log_oom();
2492
2493 c->log_extra_fields = t;
2494 c->log_extra_fields[c->n_log_extra_fields++] = IOVEC_MAKE_STRING(k);
2495
2496 k = NULL;
2497 }
2498 }
2499
2500 int config_parse_unit_condition_path(
2501 const char *unit,
2502 const char *filename,
2503 unsigned line,
2504 const char *section,
2505 unsigned section_line,
2506 const char *lvalue,
2507 int ltype,
2508 const char *rvalue,
2509 void *data,
2510 void *userdata) {
2511
2512 _cleanup_free_ char *p = NULL;
2513 Condition **list = data, *c;
2514 ConditionType t = ltype;
2515 bool trigger, negate;
2516 const Unit *u = userdata;
2517 int r;
2518
2519 assert(filename);
2520 assert(lvalue);
2521 assert(rvalue);
2522 assert(data);
2523
2524 if (isempty(rvalue)) {
2525 /* Empty assignment resets the list */
2526 *list = condition_free_list(*list);
2527 return 0;
2528 }
2529
2530 trigger = rvalue[0] == '|';
2531 if (trigger)
2532 rvalue++;
2533
2534 negate = rvalue[0] == '!';
2535 if (negate)
2536 rvalue++;
2537
2538 r = unit_full_printf(u, rvalue, &p);
2539 if (r < 0) {
2540 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue);
2541 return 0;
2542 }
2543
2544 r = path_simplify_and_warn(p, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
2545 if (r < 0)
2546 return 0;
2547
2548 c = condition_new(t, p, trigger, negate);
2549 if (!c)
2550 return log_oom();
2551
2552 LIST_PREPEND(conditions, *list, c);
2553 return 0;
2554 }
2555
2556 int config_parse_unit_condition_string(
2557 const char *unit,
2558 const char *filename,
2559 unsigned line,
2560 const char *section,
2561 unsigned section_line,
2562 const char *lvalue,
2563 int ltype,
2564 const char *rvalue,
2565 void *data,
2566 void *userdata) {
2567
2568 _cleanup_free_ char *s = NULL;
2569 Condition **list = data, *c;
2570 ConditionType t = ltype;
2571 bool trigger, negate;
2572 const Unit *u = userdata;
2573 int r;
2574
2575 assert(filename);
2576 assert(lvalue);
2577 assert(rvalue);
2578 assert(data);
2579
2580 if (isempty(rvalue)) {
2581 /* Empty assignment resets the list */
2582 *list = condition_free_list(*list);
2583 return 0;
2584 }
2585
2586 trigger = *rvalue == '|';
2587 if (trigger)
2588 rvalue += 1 + strspn(rvalue + 1, WHITESPACE);
2589
2590 negate = *rvalue == '!';
2591 if (negate)
2592 rvalue += 1 + strspn(rvalue + 1, WHITESPACE);
2593
2594 r = unit_full_printf(u, rvalue, &s);
2595 if (r < 0) {
2596 log_syntax(unit, LOG_ERR, filename, line, r,
2597 "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
2598 return 0;
2599 }
2600
2601 c = condition_new(t, s, trigger, negate);
2602 if (!c)
2603 return log_oom();
2604
2605 LIST_PREPEND(conditions, *list, c);
2606 return 0;
2607 }
2608
2609 int config_parse_unit_condition_null(
2610 const char *unit,
2611 const char *filename,
2612 unsigned line,
2613 const char *section,
2614 unsigned section_line,
2615 const char *lvalue,
2616 int ltype,
2617 const char *rvalue,
2618 void *data,
2619 void *userdata) {
2620
2621 Condition **list = data, *c;
2622 bool trigger, negate;
2623 int b;
2624
2625 assert(filename);
2626 assert(lvalue);
2627 assert(rvalue);
2628 assert(data);
2629
2630 log_syntax(unit, LOG_WARNING, filename, line, 0, "%s= is deprecated, please do not use.", lvalue);
2631
2632 if (isempty(rvalue)) {
2633 /* Empty assignment resets the list */
2634 *list = condition_free_list(*list);
2635 return 0;
2636 }
2637
2638 trigger = rvalue[0] == '|';
2639 if (trigger)
2640 rvalue++;
2641
2642 negate = rvalue[0] == '!';
2643 if (negate)
2644 rvalue++;
2645
2646 b = parse_boolean(rvalue);
2647 if (b < 0) {
2648 log_syntax(unit, LOG_ERR, filename, line, b, "Failed to parse boolean value in condition, ignoring: %s", rvalue);
2649 return 0;
2650 }
2651
2652 if (!b)
2653 negate = !negate;
2654
2655 c = condition_new(CONDITION_NULL, NULL, trigger, negate);
2656 if (!c)
2657 return log_oom();
2658
2659 LIST_PREPEND(conditions, *list, c);
2660 return 0;
2661 }
2662
2663 int config_parse_unit_requires_mounts_for(
2664 const char *unit,
2665 const char *filename,
2666 unsigned line,
2667 const char *section,
2668 unsigned section_line,
2669 const char *lvalue,
2670 int ltype,
2671 const char *rvalue,
2672 void *data,
2673 void *userdata) {
2674
2675 const char *p = rvalue;
2676 Unit *u = userdata;
2677 int r;
2678
2679 assert(filename);
2680 assert(lvalue);
2681 assert(rvalue);
2682 assert(data);
2683
2684 for (;;) {
2685 _cleanup_free_ char *word = NULL, *resolved = NULL;
2686
2687 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
2688 if (r == 0)
2689 return 0;
2690 if (r == -ENOMEM)
2691 return log_oom();
2692 if (r < 0) {
2693 log_syntax(unit, LOG_WARNING, filename, line, r,
2694 "Invalid syntax, ignoring: %s", rvalue);
2695 return 0;
2696 }
2697
2698 r = unit_full_printf(u, word, &resolved);
2699 if (r < 0) {
2700 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", word);
2701 continue;
2702 }
2703
2704 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
2705 if (r < 0)
2706 continue;
2707
2708 r = unit_require_mounts_for(u, resolved, UNIT_DEPENDENCY_FILE);
2709 if (r < 0) {
2710 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to add required mount '%s', ignoring: %m", resolved);
2711 continue;
2712 }
2713 }
2714 }
2715
2716 int config_parse_documentation(const char *unit,
2717 const char *filename,
2718 unsigned line,
2719 const char *section,
2720 unsigned section_line,
2721 const char *lvalue,
2722 int ltype,
2723 const char *rvalue,
2724 void *data,
2725 void *userdata) {
2726
2727 Unit *u = userdata;
2728 int r;
2729 char **a, **b;
2730
2731 assert(filename);
2732 assert(lvalue);
2733 assert(rvalue);
2734 assert(u);
2735
2736 if (isempty(rvalue)) {
2737 /* Empty assignment resets the list */
2738 u->documentation = strv_free(u->documentation);
2739 return 0;
2740 }
2741
2742 r = config_parse_unit_strv_printf(unit, filename, line, section, section_line, lvalue, ltype,
2743 rvalue, data, userdata);
2744 if (r < 0)
2745 return r;
2746
2747 for (a = b = u->documentation; a && *a; a++) {
2748
2749 if (documentation_url_is_valid(*a))
2750 *(b++) = *a;
2751 else {
2752 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid URL, ignoring: %s", *a);
2753 free(*a);
2754 }
2755 }
2756 if (b)
2757 *b = NULL;
2758
2759 return r;
2760 }
2761
2762 #if HAVE_SECCOMP
2763 int config_parse_syscall_filter(
2764 const char *unit,
2765 const char *filename,
2766 unsigned line,
2767 const char *section,
2768 unsigned section_line,
2769 const char *lvalue,
2770 int ltype,
2771 const char *rvalue,
2772 void *data,
2773 void *userdata) {
2774
2775 ExecContext *c = data;
2776 const Unit *u = userdata;
2777 bool invert = false;
2778 const char *p;
2779 int r;
2780
2781 assert(filename);
2782 assert(lvalue);
2783 assert(rvalue);
2784 assert(u);
2785
2786 if (isempty(rvalue)) {
2787 /* Empty assignment resets the list */
2788 c->syscall_filter = hashmap_free(c->syscall_filter);
2789 c->syscall_whitelist = false;
2790 return 0;
2791 }
2792
2793 if (rvalue[0] == '~') {
2794 invert = true;
2795 rvalue++;
2796 }
2797
2798 if (!c->syscall_filter) {
2799 c->syscall_filter = hashmap_new(NULL);
2800 if (!c->syscall_filter)
2801 return log_oom();
2802
2803 if (invert)
2804 /* Allow everything but the ones listed */
2805 c->syscall_whitelist = false;
2806 else {
2807 /* Allow nothing but the ones listed */
2808 c->syscall_whitelist = true;
2809
2810 /* Accept default syscalls if we are on a whitelist */
2811 r = seccomp_parse_syscall_filter(
2812 "@default", -1, c->syscall_filter,
2813 SECCOMP_PARSE_PERMISSIVE|SECCOMP_PARSE_WHITELIST,
2814 unit,
2815 NULL, 0);
2816 if (r < 0)
2817 return r;
2818 }
2819 }
2820
2821 p = rvalue;
2822 for (;;) {
2823 _cleanup_free_ char *word = NULL, *name = NULL;
2824 int num;
2825
2826 r = extract_first_word(&p, &word, NULL, 0);
2827 if (r == 0)
2828 return 0;
2829 if (r == -ENOMEM)
2830 return log_oom();
2831 if (r < 0) {
2832 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
2833 return 0;
2834 }
2835
2836 r = parse_syscall_and_errno(word, &name, &num);
2837 if (r < 0) {
2838 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse syscall:errno, ignoring: %s", word);
2839 continue;
2840 }
2841
2842 r = seccomp_parse_syscall_filter(
2843 name, num, c->syscall_filter,
2844 SECCOMP_PARSE_LOG|SECCOMP_PARSE_PERMISSIVE|
2845 (invert ? SECCOMP_PARSE_INVERT : 0)|
2846 (c->syscall_whitelist ? SECCOMP_PARSE_WHITELIST : 0),
2847 unit, filename, line);
2848 if (r < 0)
2849 return r;
2850 }
2851 }
2852
2853 int config_parse_syscall_archs(
2854 const char *unit,
2855 const char *filename,
2856 unsigned line,
2857 const char *section,
2858 unsigned section_line,
2859 const char *lvalue,
2860 int ltype,
2861 const char *rvalue,
2862 void *data,
2863 void *userdata) {
2864
2865 const char *p = rvalue;
2866 Set **archs = data;
2867 int r;
2868
2869 if (isempty(rvalue)) {
2870 *archs = set_free(*archs);
2871 return 0;
2872 }
2873
2874 r = set_ensure_allocated(archs, NULL);
2875 if (r < 0)
2876 return log_oom();
2877
2878 for (;;) {
2879 _cleanup_free_ char *word = NULL;
2880 uint32_t a;
2881
2882 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
2883 if (r == 0)
2884 return 0;
2885 if (r == -ENOMEM)
2886 return log_oom();
2887 if (r < 0) {
2888 log_syntax(unit, LOG_WARNING, filename, line, r,
2889 "Invalid syntax, ignoring: %s", rvalue);
2890 return 0;
2891 }
2892
2893 r = seccomp_arch_from_string(word, &a);
2894 if (r < 0) {
2895 log_syntax(unit, LOG_ERR, filename, line, r,
2896 "Failed to parse system call architecture \"%s\", ignoring: %m", word);
2897 continue;
2898 }
2899
2900 r = set_put(*archs, UINT32_TO_PTR(a + 1));
2901 if (r < 0)
2902 return log_oom();
2903 }
2904 }
2905
2906 int config_parse_syscall_errno(
2907 const char *unit,
2908 const char *filename,
2909 unsigned line,
2910 const char *section,
2911 unsigned section_line,
2912 const char *lvalue,
2913 int ltype,
2914 const char *rvalue,
2915 void *data,
2916 void *userdata) {
2917
2918 ExecContext *c = data;
2919 int e;
2920
2921 assert(filename);
2922 assert(lvalue);
2923 assert(rvalue);
2924
2925 if (isempty(rvalue)) {
2926 /* Empty assignment resets to KILL */
2927 c->syscall_errno = 0;
2928 return 0;
2929 }
2930
2931 e = parse_errno(rvalue);
2932 if (e <= 0) {
2933 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse error number, ignoring: %s", rvalue);
2934 return 0;
2935 }
2936
2937 c->syscall_errno = e;
2938 return 0;
2939 }
2940
2941 int config_parse_address_families(
2942 const char *unit,
2943 const char *filename,
2944 unsigned line,
2945 const char *section,
2946 unsigned section_line,
2947 const char *lvalue,
2948 int ltype,
2949 const char *rvalue,
2950 void *data,
2951 void *userdata) {
2952
2953 ExecContext *c = data;
2954 bool invert = false;
2955 const char *p;
2956 int r;
2957
2958 assert(filename);
2959 assert(lvalue);
2960 assert(rvalue);
2961
2962 if (isempty(rvalue)) {
2963 /* Empty assignment resets the list */
2964 c->address_families = set_free(c->address_families);
2965 c->address_families_whitelist = false;
2966 return 0;
2967 }
2968
2969 if (rvalue[0] == '~') {
2970 invert = true;
2971 rvalue++;
2972 }
2973
2974 if (!c->address_families) {
2975 c->address_families = set_new(NULL);
2976 if (!c->address_families)
2977 return log_oom();
2978
2979 c->address_families_whitelist = !invert;
2980 }
2981
2982 for (p = rvalue;;) {
2983 _cleanup_free_ char *word = NULL;
2984 int af;
2985
2986 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
2987 if (r == 0)
2988 return 0;
2989 if (r == -ENOMEM)
2990 return log_oom();
2991 if (r < 0) {
2992 log_syntax(unit, LOG_WARNING, filename, line, r,
2993 "Invalid syntax, ignoring: %s", rvalue);
2994 return 0;
2995 }
2996
2997 af = af_from_name(word);
2998 if (af < 0) {
2999 log_syntax(unit, LOG_ERR, filename, line, af,
3000 "Failed to parse address family, ignoring: %s", word);
3001 continue;
3002 }
3003
3004 /* If we previously wanted to forbid an address family and now
3005 * we want to allow it, then just remove it from the list.
3006 */
3007 if (!invert == c->address_families_whitelist) {
3008 r = set_put(c->address_families, INT_TO_PTR(af));
3009 if (r < 0)
3010 return log_oom();
3011 } else
3012 set_remove(c->address_families, INT_TO_PTR(af));
3013 }
3014 }
3015
3016 int config_parse_restrict_namespaces(
3017 const char *unit,
3018 const char *filename,
3019 unsigned line,
3020 const char *section,
3021 unsigned section_line,
3022 const char *lvalue,
3023 int ltype,
3024 const char *rvalue,
3025 void *data,
3026 void *userdata) {
3027
3028 ExecContext *c = data;
3029 unsigned long flags;
3030 bool invert = false;
3031 int r;
3032
3033 if (isempty(rvalue)) {
3034 /* Reset to the default. */
3035 c->restrict_namespaces = NAMESPACE_FLAGS_INITIAL;
3036 return 0;
3037 }
3038
3039 /* Boolean parameter ignores the previous settings */
3040 r = parse_boolean(rvalue);
3041 if (r > 0) {
3042 c->restrict_namespaces = 0;
3043 return 0;
3044 } else if (r == 0) {
3045 c->restrict_namespaces = NAMESPACE_FLAGS_ALL;
3046 return 0;
3047 }
3048
3049 if (rvalue[0] == '~') {
3050 invert = true;
3051 rvalue++;
3052 }
3053
3054 /* Not a boolean argument, in this case it's a list of namespace types. */
3055 r = namespace_flags_from_string(rvalue, &flags);
3056 if (r < 0) {
3057 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse namespace type string, ignoring: %s", rvalue);
3058 return 0;
3059 }
3060
3061 if (c->restrict_namespaces == NAMESPACE_FLAGS_INITIAL)
3062 /* Initial assignment. Just set the value. */
3063 c->restrict_namespaces = invert ? (~flags) & NAMESPACE_FLAGS_ALL : flags;
3064 else
3065 /* Merge the value with the previous one. */
3066 SET_FLAG(c->restrict_namespaces, flags, !invert);
3067
3068 return 0;
3069 }
3070 #endif
3071
3072 int config_parse_unit_slice(
3073 const char *unit,
3074 const char *filename,
3075 unsigned line,
3076 const char *section,
3077 unsigned section_line,
3078 const char *lvalue,
3079 int ltype,
3080 const char *rvalue,
3081 void *data,
3082 void *userdata) {
3083
3084 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
3085 _cleanup_free_ char *k = NULL;
3086 Unit *u = userdata, *slice;
3087 int r;
3088
3089 assert(filename);
3090 assert(lvalue);
3091 assert(rvalue);
3092 assert(u);
3093
3094 r = unit_name_printf(u, rvalue, &k);
3095 if (r < 0) {
3096 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue);
3097 return 0;
3098 }
3099
3100 r = manager_load_unit(u->manager, k, NULL, &error, &slice);
3101 if (r < 0) {
3102 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to load slice unit %s, ignoring: %s", k, bus_error_message(&error, r));
3103 return 0;
3104 }
3105
3106 r = unit_set_slice(u, slice);
3107 if (r < 0) {
3108 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to assign slice %s to unit %s, ignoring: %m", slice->id, u->id);
3109 return 0;
3110 }
3111
3112 return 0;
3113 }
3114
3115 int config_parse_cpu_quota(
3116 const char *unit,
3117 const char *filename,
3118 unsigned line,
3119 const char *section,
3120 unsigned section_line,
3121 const char *lvalue,
3122 int ltype,
3123 const char *rvalue,
3124 void *data,
3125 void *userdata) {
3126
3127 CGroupContext *c = data;
3128 int r;
3129
3130 assert(filename);
3131 assert(lvalue);
3132 assert(rvalue);
3133
3134 if (isempty(rvalue)) {
3135 c->cpu_quota_per_sec_usec = USEC_INFINITY;
3136 return 0;
3137 }
3138
3139 r = parse_permille_unbounded(rvalue);
3140 if (r <= 0) {
3141 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid CPU quota '%s', ignoring.", rvalue);
3142 return 0;
3143 }
3144
3145 c->cpu_quota_per_sec_usec = ((usec_t) r * USEC_PER_SEC) / 1000U;
3146 return 0;
3147 }
3148
3149 int config_parse_allowed_cpus(
3150 const char *unit,
3151 const char *filename,
3152 unsigned line,
3153 const char *section,
3154 unsigned section_line,
3155 const char *lvalue,
3156 int ltype,
3157 const char *rvalue,
3158 void *data,
3159 void *userdata) {
3160
3161 CGroupContext *c = data;
3162
3163 (void) parse_cpu_set_extend(rvalue, &c->cpuset_cpus, true, unit, filename, line, lvalue);
3164
3165 return 0;
3166 }
3167
3168 int config_parse_allowed_mems(
3169 const char *unit,
3170 const char *filename,
3171 unsigned line,
3172 const char *section,
3173 unsigned section_line,
3174 const char *lvalue,
3175 int ltype,
3176 const char *rvalue,
3177 void *data,
3178 void *userdata) {
3179
3180 CGroupContext *c = data;
3181
3182 (void) parse_cpu_set_extend(rvalue, &c->cpuset_mems, true, unit, filename, line, lvalue);
3183
3184 return 0;
3185 }
3186
3187 int config_parse_memory_limit(
3188 const char *unit,
3189 const char *filename,
3190 unsigned line,
3191 const char *section,
3192 unsigned section_line,
3193 const char *lvalue,
3194 int ltype,
3195 const char *rvalue,
3196 void *data,
3197 void *userdata) {
3198
3199 CGroupContext *c = data;
3200 uint64_t bytes = CGROUP_LIMIT_MAX;
3201 int r;
3202
3203 if (!isempty(rvalue) && !streq(rvalue, "infinity")) {
3204
3205 r = parse_permille(rvalue);
3206 if (r < 0) {
3207 r = parse_size(rvalue, 1024, &bytes);
3208 if (r < 0) {
3209 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid memory limit '%s', ignoring: %m", rvalue);
3210 return 0;
3211 }
3212 } else
3213 bytes = physical_memory_scale(r, 1000U);
3214
3215 if (bytes >= UINT64_MAX ||
3216 (bytes <= 0 && !STR_IN_SET(lvalue, "MemorySwapMax", "MemoryLow", "MemoryMin", "DefaultMemoryLow", "DefaultMemoryMin"))) {
3217 log_syntax(unit, LOG_ERR, filename, line, 0, "Memory limit '%s' out of range, ignoring.", rvalue);
3218 return 0;
3219 }
3220 }
3221
3222 if (streq(lvalue, "DefaultMemoryLow")) {
3223 c->default_memory_low_set = true;
3224 if (isempty(rvalue))
3225 c->default_memory_low = CGROUP_LIMIT_MIN;
3226 else
3227 c->default_memory_low = bytes;
3228 } else if (streq(lvalue, "DefaultMemoryMin")) {
3229 c->default_memory_min_set = true;
3230 if (isempty(rvalue))
3231 c->default_memory_min = CGROUP_LIMIT_MIN;
3232 else
3233 c->default_memory_min = bytes;
3234 } else if (streq(lvalue, "MemoryMin")) {
3235 c->memory_min = bytes;
3236 c->memory_min_set = true;
3237 } else if (streq(lvalue, "MemoryLow")) {
3238 c->memory_low = bytes;
3239 c->memory_low_set = true;
3240 } else if (streq(lvalue, "MemoryHigh"))
3241 c->memory_high = bytes;
3242 else if (streq(lvalue, "MemoryMax"))
3243 c->memory_max = bytes;
3244 else if (streq(lvalue, "MemorySwapMax"))
3245 c->memory_swap_max = bytes;
3246 else if (streq(lvalue, "MemoryLimit"))
3247 c->memory_limit = bytes;
3248 else
3249 return -EINVAL;
3250
3251 return 0;
3252 }
3253
3254 int config_parse_tasks_max(
3255 const char *unit,
3256 const char *filename,
3257 unsigned line,
3258 const char *section,
3259 unsigned section_line,
3260 const char *lvalue,
3261 int ltype,
3262 const char *rvalue,
3263 void *data,
3264 void *userdata) {
3265
3266 const Unit *u = userdata;
3267 TasksMax *tasks_max = data;
3268 uint64_t v;
3269 int r;
3270
3271 if (isempty(rvalue)) {
3272 *tasks_max = u ? u->manager->default_tasks_max : TASKS_MAX_UNSET;
3273 return 0;
3274 }
3275
3276 if (streq(rvalue, "infinity")) {
3277 *tasks_max = TASKS_MAX_UNSET;
3278 return 0;
3279 }
3280
3281 r = parse_permille(rvalue);
3282 if (r >= 0)
3283 *tasks_max = (TasksMax) { r, 1000U }; /* r‰ */
3284 else {
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
3291 if (v <= 0 || v >= UINT64_MAX) {
3292 log_syntax(unit, LOG_ERR, filename, line, 0, "Maximum tasks value '%s' out of range, ignoring.", rvalue);
3293 return 0;
3294 }
3295
3296 *tasks_max = (TasksMax) { v };
3297 }
3298
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 const 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 const 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 const 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 const 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 const 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 const 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 }