]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/load-fragment.c
tree-wide: drop stat.h or statfs.h when stat-util.h is included
[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_cpuset_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_cpuset_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 uint64_t *tasks_max = data, v;
3267 const Unit *u = userdata;
3268 int r;
3269
3270 if (isempty(rvalue)) {
3271 *tasks_max = u ? u->manager->default_tasks_max : UINT64_MAX;
3272 return 0;
3273 }
3274
3275 if (streq(rvalue, "infinity")) {
3276 *tasks_max = CGROUP_LIMIT_MAX;
3277 return 0;
3278 }
3279
3280 r = parse_permille(rvalue);
3281 if (r < 0) {
3282 r = safe_atou64(rvalue, &v);
3283 if (r < 0) {
3284 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid maximum tasks value '%s', ignoring: %m", rvalue);
3285 return 0;
3286 }
3287 } else
3288 v = system_tasks_max_scale(r, 1000U);
3289
3290 if (v <= 0 || v >= UINT64_MAX) {
3291 log_syntax(unit, LOG_ERR, filename, line, 0, "Maximum tasks value '%s' out of range, ignoring.", rvalue);
3292 return 0;
3293 }
3294
3295 *tasks_max = v;
3296 return 0;
3297 }
3298
3299 int config_parse_delegate(
3300 const char *unit,
3301 const char *filename,
3302 unsigned line,
3303 const char *section,
3304 unsigned section_line,
3305 const char *lvalue,
3306 int ltype,
3307 const char *rvalue,
3308 void *data,
3309 void *userdata) {
3310
3311 CGroupContext *c = data;
3312 UnitType t;
3313 int r;
3314
3315 t = unit_name_to_type(unit);
3316 assert(t != _UNIT_TYPE_INVALID);
3317
3318 if (!unit_vtable[t]->can_delegate) {
3319 log_syntax(unit, LOG_ERR, filename, line, 0, "Delegate= setting not supported for this unit type, ignoring.");
3320 return 0;
3321 }
3322
3323 /* We either accept a boolean value, which may be used to turn on delegation for all controllers, or turn it
3324 * off for all. Or it takes a list of controller names, in which case we add the specified controllers to the
3325 * mask to delegate. */
3326
3327 if (isempty(rvalue)) {
3328 /* An empty string resets controllers and set Delegate=yes. */
3329 c->delegate = true;
3330 c->delegate_controllers = 0;
3331 return 0;
3332 }
3333
3334 r = parse_boolean(rvalue);
3335 if (r < 0) {
3336 const char *p = rvalue;
3337 CGroupMask mask = 0;
3338
3339 for (;;) {
3340 _cleanup_free_ char *word = NULL;
3341 CGroupController cc;
3342
3343 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
3344 if (r == 0)
3345 break;
3346 if (r == -ENOMEM)
3347 return log_oom();
3348 if (r < 0) {
3349 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
3350 return 0;
3351 }
3352
3353 cc = cgroup_controller_from_string(word);
3354 if (cc < 0) {
3355 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid controller name '%s', ignoring", word);
3356 continue;
3357 }
3358
3359 mask |= CGROUP_CONTROLLER_TO_MASK(cc);
3360 }
3361
3362 c->delegate = true;
3363 c->delegate_controllers |= mask;
3364
3365 } else if (r > 0) {
3366 c->delegate = true;
3367 c->delegate_controllers = _CGROUP_MASK_ALL;
3368 } else {
3369 c->delegate = false;
3370 c->delegate_controllers = 0;
3371 }
3372
3373 return 0;
3374 }
3375
3376 int config_parse_device_allow(
3377 const char *unit,
3378 const char *filename,
3379 unsigned line,
3380 const char *section,
3381 unsigned section_line,
3382 const char *lvalue,
3383 int ltype,
3384 const char *rvalue,
3385 void *data,
3386 void *userdata) {
3387
3388 _cleanup_free_ char *path = NULL, *resolved = NULL;
3389 CGroupContext *c = data;
3390 const char *p = rvalue;
3391 int r;
3392
3393 if (isempty(rvalue)) {
3394 while (c->device_allow)
3395 cgroup_context_free_device_allow(c, c->device_allow);
3396
3397 return 0;
3398 }
3399
3400 r = extract_first_word(&p, &path, NULL, EXTRACT_UNQUOTE);
3401 if (r == -ENOMEM)
3402 return log_oom();
3403 if (r < 0) {
3404 log_syntax(unit, LOG_WARNING, filename, line, r,
3405 "Invalid syntax, ignoring: %s", rvalue);
3406 return 0;
3407 }
3408 if (r == 0) {
3409 log_syntax(unit, LOG_WARNING, filename, line, 0,
3410 "Failed to extract device path and rights from '%s', ignoring.", rvalue);
3411 return 0;
3412 }
3413
3414 r = unit_full_printf(userdata, path, &resolved);
3415 if (r < 0) {
3416 log_syntax(unit, LOG_WARNING, filename, line, r,
3417 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
3418 return 0;
3419 }
3420
3421 if (!STARTSWITH_SET(resolved, "block-", "char-")) {
3422
3423 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
3424 if (r < 0)
3425 return 0;
3426
3427 if (!valid_device_node_path(resolved)) {
3428 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid device node path '%s', ignoring.", resolved);
3429 return 0;
3430 }
3431 }
3432
3433 if (!isempty(p) && !in_charset(p, "rwm")) {
3434 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid device rights '%s', ignoring.", p);
3435 return 0;
3436 }
3437
3438 return cgroup_add_device_allow(c, resolved, p);
3439 }
3440
3441 int config_parse_io_device_weight(
3442 const char *unit,
3443 const char *filename,
3444 unsigned line,
3445 const char *section,
3446 unsigned section_line,
3447 const char *lvalue,
3448 int ltype,
3449 const char *rvalue,
3450 void *data,
3451 void *userdata) {
3452
3453 _cleanup_free_ char *path = NULL, *resolved = NULL;
3454 CGroupIODeviceWeight *w;
3455 CGroupContext *c = data;
3456 const char *p = rvalue;
3457 uint64_t u;
3458 int r;
3459
3460 assert(filename);
3461 assert(lvalue);
3462 assert(rvalue);
3463
3464 if (isempty(rvalue)) {
3465 while (c->io_device_weights)
3466 cgroup_context_free_io_device_weight(c, c->io_device_weights);
3467
3468 return 0;
3469 }
3470
3471 r = extract_first_word(&p, &path, NULL, EXTRACT_UNQUOTE);
3472 if (r == -ENOMEM)
3473 return log_oom();
3474 if (r < 0) {
3475 log_syntax(unit, LOG_WARNING, filename, line, r,
3476 "Invalid syntax, ignoring: %s", rvalue);
3477 return 0;
3478 }
3479 if (r == 0 || isempty(p)) {
3480 log_syntax(unit, LOG_WARNING, filename, line, 0,
3481 "Failed to extract device path and weight from '%s', ignoring.", rvalue);
3482 return 0;
3483 }
3484
3485 r = unit_full_printf(userdata, path, &resolved);
3486 if (r < 0) {
3487 log_syntax(unit, LOG_WARNING, filename, line, r,
3488 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
3489 return 0;
3490 }
3491
3492 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
3493 if (r < 0)
3494 return 0;
3495
3496 r = cg_weight_parse(p, &u);
3497 if (r < 0) {
3498 log_syntax(unit, LOG_ERR, filename, line, r, "IO weight '%s' invalid, ignoring: %m", p);
3499 return 0;
3500 }
3501
3502 assert(u != CGROUP_WEIGHT_INVALID);
3503
3504 w = new0(CGroupIODeviceWeight, 1);
3505 if (!w)
3506 return log_oom();
3507
3508 w->path = TAKE_PTR(resolved);
3509 w->weight = u;
3510
3511 LIST_PREPEND(device_weights, c->io_device_weights, w);
3512 return 0;
3513 }
3514
3515 int config_parse_io_device_latency(
3516 const char *unit,
3517 const char *filename,
3518 unsigned line,
3519 const char *section,
3520 unsigned section_line,
3521 const char *lvalue,
3522 int ltype,
3523 const char *rvalue,
3524 void *data,
3525 void *userdata) {
3526
3527 _cleanup_free_ char *path = NULL, *resolved = NULL;
3528 CGroupIODeviceLatency *l;
3529 CGroupContext *c = data;
3530 const char *p = rvalue;
3531 usec_t usec;
3532 int r;
3533
3534 assert(filename);
3535 assert(lvalue);
3536 assert(rvalue);
3537
3538 if (isempty(rvalue)) {
3539 while (c->io_device_latencies)
3540 cgroup_context_free_io_device_latency(c, c->io_device_latencies);
3541
3542 return 0;
3543 }
3544
3545 r = extract_first_word(&p, &path, NULL, EXTRACT_UNQUOTE);
3546 if (r == -ENOMEM)
3547 return log_oom();
3548 if (r < 0) {
3549 log_syntax(unit, LOG_WARNING, filename, line, r,
3550 "Invalid syntax, ignoring: %s", rvalue);
3551 return 0;
3552 }
3553 if (r == 0 || isempty(p)) {
3554 log_syntax(unit, LOG_WARNING, filename, line, 0,
3555 "Failed to extract device path and latency from '%s', ignoring.", rvalue);
3556 return 0;
3557 }
3558
3559 r = unit_full_printf(userdata, path, &resolved);
3560 if (r < 0) {
3561 log_syntax(unit, LOG_WARNING, filename, line, r,
3562 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
3563 return 0;
3564 }
3565
3566 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
3567 if (r < 0)
3568 return 0;
3569
3570 if (parse_sec(p, &usec) < 0) {
3571 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse timer value, ignoring: %s", p);
3572 return 0;
3573 }
3574
3575 l = new0(CGroupIODeviceLatency, 1);
3576 if (!l)
3577 return log_oom();
3578
3579 l->path = TAKE_PTR(resolved);
3580 l->target_usec = usec;
3581
3582 LIST_PREPEND(device_latencies, c->io_device_latencies, l);
3583 return 0;
3584 }
3585
3586 int config_parse_io_limit(
3587 const char *unit,
3588 const char *filename,
3589 unsigned line,
3590 const char *section,
3591 unsigned section_line,
3592 const char *lvalue,
3593 int ltype,
3594 const char *rvalue,
3595 void *data,
3596 void *userdata) {
3597
3598 _cleanup_free_ char *path = NULL, *resolved = NULL;
3599 CGroupIODeviceLimit *l = NULL, *t;
3600 CGroupContext *c = data;
3601 CGroupIOLimitType type;
3602 const char *p = rvalue;
3603 uint64_t num;
3604 int r;
3605
3606 assert(filename);
3607 assert(lvalue);
3608 assert(rvalue);
3609
3610 type = cgroup_io_limit_type_from_string(lvalue);
3611 assert(type >= 0);
3612
3613 if (isempty(rvalue)) {
3614 LIST_FOREACH(device_limits, l, c->io_device_limits)
3615 l->limits[type] = cgroup_io_limit_defaults[type];
3616 return 0;
3617 }
3618
3619 r = extract_first_word(&p, &path, NULL, EXTRACT_UNQUOTE);
3620 if (r == -ENOMEM)
3621 return log_oom();
3622 if (r < 0) {
3623 log_syntax(unit, LOG_WARNING, filename, line, r,
3624 "Invalid syntax, ignoring: %s", rvalue);
3625 return 0;
3626 }
3627 if (r == 0 || isempty(p)) {
3628 log_syntax(unit, LOG_WARNING, filename, line, 0,
3629 "Failed to extract device node and bandwidth from '%s', ignoring.", rvalue);
3630 return 0;
3631 }
3632
3633 r = unit_full_printf(userdata, path, &resolved);
3634 if (r < 0) {
3635 log_syntax(unit, LOG_WARNING, filename, line, r,
3636 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
3637 return 0;
3638 }
3639
3640 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
3641 if (r < 0)
3642 return 0;
3643
3644 if (streq("infinity", p))
3645 num = CGROUP_LIMIT_MAX;
3646 else {
3647 r = parse_size(p, 1000, &num);
3648 if (r < 0 || num <= 0) {
3649 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid IO limit '%s', ignoring.", p);
3650 return 0;
3651 }
3652 }
3653
3654 LIST_FOREACH(device_limits, t, c->io_device_limits) {
3655 if (path_equal(resolved, t->path)) {
3656 l = t;
3657 break;
3658 }
3659 }
3660
3661 if (!l) {
3662 CGroupIOLimitType ttype;
3663
3664 l = new0(CGroupIODeviceLimit, 1);
3665 if (!l)
3666 return log_oom();
3667
3668 l->path = TAKE_PTR(resolved);
3669 for (ttype = 0; ttype < _CGROUP_IO_LIMIT_TYPE_MAX; ttype++)
3670 l->limits[ttype] = cgroup_io_limit_defaults[ttype];
3671
3672 LIST_PREPEND(device_limits, c->io_device_limits, l);
3673 }
3674
3675 l->limits[type] = num;
3676
3677 return 0;
3678 }
3679
3680 int config_parse_blockio_device_weight(
3681 const char *unit,
3682 const char *filename,
3683 unsigned line,
3684 const char *section,
3685 unsigned section_line,
3686 const char *lvalue,
3687 int ltype,
3688 const char *rvalue,
3689 void *data,
3690 void *userdata) {
3691
3692 _cleanup_free_ char *path = NULL, *resolved = NULL;
3693 CGroupBlockIODeviceWeight *w;
3694 CGroupContext *c = data;
3695 const char *p = rvalue;
3696 uint64_t u;
3697 int r;
3698
3699 assert(filename);
3700 assert(lvalue);
3701 assert(rvalue);
3702
3703 if (isempty(rvalue)) {
3704 while (c->blockio_device_weights)
3705 cgroup_context_free_blockio_device_weight(c, c->blockio_device_weights);
3706
3707 return 0;
3708 }
3709
3710 r = extract_first_word(&p, &path, NULL, EXTRACT_UNQUOTE);
3711 if (r == -ENOMEM)
3712 return log_oom();
3713 if (r < 0) {
3714 log_syntax(unit, LOG_WARNING, filename, line, r,
3715 "Invalid syntax, ignoring: %s", rvalue);
3716 return 0;
3717 }
3718 if (r == 0 || isempty(p)) {
3719 log_syntax(unit, LOG_WARNING, filename, line, 0,
3720 "Failed to extract device node and weight from '%s', ignoring.", rvalue);
3721 return 0;
3722 }
3723
3724 r = unit_full_printf(userdata, path, &resolved);
3725 if (r < 0) {
3726 log_syntax(unit, LOG_WARNING, filename, line, r,
3727 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
3728 return 0;
3729 }
3730
3731 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
3732 if (r < 0)
3733 return 0;
3734
3735 r = cg_blkio_weight_parse(p, &u);
3736 if (r < 0) {
3737 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid block IO weight '%s', ignoring: %m", p);
3738 return 0;
3739 }
3740
3741 assert(u != CGROUP_BLKIO_WEIGHT_INVALID);
3742
3743 w = new0(CGroupBlockIODeviceWeight, 1);
3744 if (!w)
3745 return log_oom();
3746
3747 w->path = TAKE_PTR(resolved);
3748 w->weight = u;
3749
3750 LIST_PREPEND(device_weights, c->blockio_device_weights, w);
3751 return 0;
3752 }
3753
3754 int config_parse_blockio_bandwidth(
3755 const char *unit,
3756 const char *filename,
3757 unsigned line,
3758 const char *section,
3759 unsigned section_line,
3760 const char *lvalue,
3761 int ltype,
3762 const char *rvalue,
3763 void *data,
3764 void *userdata) {
3765
3766 _cleanup_free_ char *path = NULL, *resolved = NULL;
3767 CGroupBlockIODeviceBandwidth *b = NULL, *t;
3768 CGroupContext *c = data;
3769 const char *p = rvalue;
3770 uint64_t bytes;
3771 bool read;
3772 int r;
3773
3774 assert(filename);
3775 assert(lvalue);
3776 assert(rvalue);
3777
3778 read = streq("BlockIOReadBandwidth", lvalue);
3779
3780 if (isempty(rvalue)) {
3781 LIST_FOREACH(device_bandwidths, b, c->blockio_device_bandwidths) {
3782 b->rbps = CGROUP_LIMIT_MAX;
3783 b->wbps = CGROUP_LIMIT_MAX;
3784 }
3785 return 0;
3786 }
3787
3788 r = extract_first_word(&p, &path, NULL, EXTRACT_UNQUOTE);
3789 if (r == -ENOMEM)
3790 return log_oom();
3791 if (r < 0) {
3792 log_syntax(unit, LOG_WARNING, filename, line, r,
3793 "Invalid syntax, ignoring: %s", rvalue);
3794 return 0;
3795 }
3796 if (r == 0 || isempty(p)) {
3797 log_syntax(unit, LOG_WARNING, filename, line, 0,
3798 "Failed to extract device node and bandwidth from '%s', ignoring.", rvalue);
3799 return 0;
3800 }
3801
3802 r = unit_full_printf(userdata, path, &resolved);
3803 if (r < 0) {
3804 log_syntax(unit, LOG_WARNING, filename, line, r,
3805 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
3806 return 0;
3807 }
3808
3809 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
3810 if (r < 0)
3811 return 0;
3812
3813 r = parse_size(p, 1000, &bytes);
3814 if (r < 0 || bytes <= 0) {
3815 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid Block IO Bandwidth '%s', ignoring.", p);
3816 return 0;
3817 }
3818
3819 LIST_FOREACH(device_bandwidths, t, c->blockio_device_bandwidths) {
3820 if (path_equal(resolved, t->path)) {
3821 b = t;
3822 break;
3823 }
3824 }
3825
3826 if (!t) {
3827 b = new0(CGroupBlockIODeviceBandwidth, 1);
3828 if (!b)
3829 return log_oom();
3830
3831 b->path = TAKE_PTR(resolved);
3832 b->rbps = CGROUP_LIMIT_MAX;
3833 b->wbps = CGROUP_LIMIT_MAX;
3834
3835 LIST_PREPEND(device_bandwidths, c->blockio_device_bandwidths, b);
3836 }
3837
3838 if (read)
3839 b->rbps = bytes;
3840 else
3841 b->wbps = bytes;
3842
3843 return 0;
3844 }
3845
3846 int config_parse_job_mode_isolate(
3847 const char *unit,
3848 const char *filename,
3849 unsigned line,
3850 const char *section,
3851 unsigned section_line,
3852 const char *lvalue,
3853 int ltype,
3854 const char *rvalue,
3855 void *data,
3856 void *userdata) {
3857
3858 JobMode *m = data;
3859 int r;
3860
3861 assert(filename);
3862 assert(lvalue);
3863 assert(rvalue);
3864
3865 r = parse_boolean(rvalue);
3866 if (r < 0) {
3867 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse boolean, ignoring: %s", rvalue);
3868 return 0;
3869 }
3870
3871 log_notice("%s is deprecated. Please use OnFailureJobMode= instead", lvalue);
3872
3873 *m = r ? JOB_ISOLATE : JOB_REPLACE;
3874 return 0;
3875 }
3876
3877 int config_parse_exec_directories(
3878 const char *unit,
3879 const char *filename,
3880 unsigned line,
3881 const char *section,
3882 unsigned section_line,
3883 const char *lvalue,
3884 int ltype,
3885 const char *rvalue,
3886 void *data,
3887 void *userdata) {
3888
3889 char***rt = data;
3890 const Unit *u = userdata;
3891 const char *p;
3892 int r;
3893
3894 assert(filename);
3895 assert(lvalue);
3896 assert(rvalue);
3897 assert(data);
3898
3899 if (isempty(rvalue)) {
3900 /* Empty assignment resets the list */
3901 *rt = strv_free(*rt);
3902 return 0;
3903 }
3904
3905 for (p = rvalue;;) {
3906 _cleanup_free_ char *word = NULL, *k = NULL;
3907
3908 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
3909 if (r == -ENOMEM)
3910 return log_oom();
3911 if (r < 0) {
3912 log_syntax(unit, LOG_WARNING, filename, line, r,
3913 "Invalid syntax, ignoring: %s", rvalue);
3914 return 0;
3915 }
3916 if (r == 0)
3917 return 0;
3918
3919 r = unit_full_printf(u, word, &k);
3920 if (r < 0) {
3921 log_syntax(unit, LOG_ERR, filename, line, r,
3922 "Failed to resolve unit specifiers in \"%s\", ignoring: %m", word);
3923 continue;
3924 }
3925
3926 r = path_simplify_and_warn(k, PATH_CHECK_RELATIVE, unit, filename, line, lvalue);
3927 if (r < 0)
3928 continue;
3929
3930 if (path_startswith(k, "private")) {
3931 log_syntax(unit, LOG_ERR, filename, line, 0,
3932 "%s= path can't be 'private', ignoring assignment: %s", lvalue, word);
3933 continue;
3934 }
3935
3936 r = strv_push(rt, k);
3937 if (r < 0)
3938 return log_oom();
3939 k = NULL;
3940 }
3941 }
3942
3943 int config_parse_set_status(
3944 const char *unit,
3945 const char *filename,
3946 unsigned line,
3947 const char *section,
3948 unsigned section_line,
3949 const char *lvalue,
3950 int ltype,
3951 const char *rvalue,
3952 void *data,
3953 void *userdata) {
3954
3955 size_t l;
3956 const char *word, *state;
3957 int r;
3958 ExitStatusSet *status_set = data;
3959
3960 assert(filename);
3961 assert(lvalue);
3962 assert(rvalue);
3963 assert(data);
3964
3965 /* Empty assignment resets the list */
3966 if (isempty(rvalue)) {
3967 exit_status_set_free(status_set);
3968 return 0;
3969 }
3970
3971 FOREACH_WORD(word, l, rvalue, state) {
3972 _cleanup_free_ char *temp;
3973 Bitmap *bitmap;
3974
3975 temp = strndup(word, l);
3976 if (!temp)
3977 return log_oom();
3978
3979 /* We need to call exit_status_from_string() first, because we want
3980 * to parse numbers as exit statuses, not signals. */
3981
3982 r = exit_status_from_string(temp);
3983 if (r >= 0) {
3984 assert(r >= 0 && r < 256);
3985 bitmap = &status_set->status;
3986 } else {
3987 r = signal_from_string(temp);
3988
3989 if (r <= 0) {
3990 log_syntax(unit, LOG_ERR, filename, line, 0,
3991 "Failed to parse value, ignoring: %s", word);
3992 continue;
3993 }
3994 bitmap = &status_set->signal;
3995 }
3996
3997 r = bitmap_set(bitmap, r);
3998 if (r < 0)
3999 return log_error_errno(r, "Failed to set signal or status %s: %m", word);
4000 }
4001 if (!isempty(state))
4002 log_syntax(unit, LOG_ERR, filename, line, 0, "Trailing garbage, ignoring.");
4003
4004 return 0;
4005 }
4006
4007 int config_parse_namespace_path_strv(
4008 const char *unit,
4009 const char *filename,
4010 unsigned line,
4011 const char *section,
4012 unsigned section_line,
4013 const char *lvalue,
4014 int ltype,
4015 const char *rvalue,
4016 void *data,
4017 void *userdata) {
4018
4019 const Unit *u = userdata;
4020 char*** sv = data;
4021 const char *p = rvalue;
4022 int r;
4023
4024 assert(filename);
4025 assert(lvalue);
4026 assert(rvalue);
4027 assert(data);
4028
4029 if (isempty(rvalue)) {
4030 /* Empty assignment resets the list */
4031 *sv = strv_free(*sv);
4032 return 0;
4033 }
4034
4035 for (;;) {
4036 _cleanup_free_ char *word = NULL, *resolved = NULL, *joined = NULL;
4037 const char *w;
4038 bool ignore_enoent = false, shall_prefix = false;
4039
4040 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
4041 if (r == 0)
4042 break;
4043 if (r == -ENOMEM)
4044 return log_oom();
4045 if (r < 0) {
4046 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to extract first word, ignoring: %s", rvalue);
4047 return 0;
4048 }
4049
4050 w = word;
4051 if (startswith(w, "-")) {
4052 ignore_enoent = true;
4053 w++;
4054 }
4055 if (startswith(w, "+")) {
4056 shall_prefix = true;
4057 w++;
4058 }
4059
4060 r = unit_full_printf(u, w, &resolved);
4061 if (r < 0) {
4062 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s: %m", w);
4063 continue;
4064 }
4065
4066 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
4067 if (r < 0)
4068 continue;
4069
4070 joined = strjoin(ignore_enoent ? "-" : "",
4071 shall_prefix ? "+" : "",
4072 resolved);
4073
4074 r = strv_push(sv, joined);
4075 if (r < 0)
4076 return log_oom();
4077
4078 joined = NULL;
4079 }
4080
4081 return 0;
4082 }
4083
4084 int config_parse_temporary_filesystems(
4085 const char *unit,
4086 const char *filename,
4087 unsigned line,
4088 const char *section,
4089 unsigned section_line,
4090 const char *lvalue,
4091 int ltype,
4092 const char *rvalue,
4093 void *data,
4094 void *userdata) {
4095
4096 const Unit *u = userdata;
4097 ExecContext *c = data;
4098 const char *p = rvalue;
4099 int r;
4100
4101 assert(filename);
4102 assert(lvalue);
4103 assert(rvalue);
4104 assert(data);
4105
4106 if (isempty(rvalue)) {
4107 /* Empty assignment resets the list */
4108 temporary_filesystem_free_many(c->temporary_filesystems, c->n_temporary_filesystems);
4109 c->temporary_filesystems = NULL;
4110 c->n_temporary_filesystems = 0;
4111 return 0;
4112 }
4113
4114 for (;;) {
4115 _cleanup_free_ char *word = NULL, *path = NULL, *resolved = NULL;
4116 const char *w;
4117
4118 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
4119 if (r == 0)
4120 return 0;
4121 if (r == -ENOMEM)
4122 return log_oom();
4123 if (r < 0) {
4124 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to extract first word, ignoring: %s", rvalue);
4125 return 0;
4126 }
4127
4128 w = word;
4129 r = extract_first_word(&w, &path, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
4130 if (r == -ENOMEM)
4131 return log_oom();
4132 if (r < 0) {
4133 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to extract first word, ignoring: %s", word);
4134 continue;
4135 }
4136 if (r == 0) {
4137 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid syntax, ignoring: %s", word);
4138 continue;
4139 }
4140
4141 r = unit_full_printf(u, path, &resolved);
4142 if (r < 0) {
4143 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", path);
4144 continue;
4145 }
4146
4147 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
4148 if (r < 0)
4149 continue;
4150
4151 r = temporary_filesystem_add(&c->temporary_filesystems, &c->n_temporary_filesystems, resolved, w);
4152 if (r < 0)
4153 return log_oom();
4154 }
4155 }
4156
4157 int config_parse_bind_paths(
4158 const char *unit,
4159 const char *filename,
4160 unsigned line,
4161 const char *section,
4162 unsigned section_line,
4163 const char *lvalue,
4164 int ltype,
4165 const char *rvalue,
4166 void *data,
4167 void *userdata) {
4168
4169 ExecContext *c = data;
4170 const Unit *u = userdata;
4171 const char *p;
4172 int r;
4173
4174 assert(filename);
4175 assert(lvalue);
4176 assert(rvalue);
4177 assert(data);
4178
4179 if (isempty(rvalue)) {
4180 /* Empty assignment resets the list */
4181 bind_mount_free_many(c->bind_mounts, c->n_bind_mounts);
4182 c->bind_mounts = NULL;
4183 c->n_bind_mounts = 0;
4184 return 0;
4185 }
4186
4187 p = rvalue;
4188 for (;;) {
4189 _cleanup_free_ char *source = NULL, *destination = NULL;
4190 _cleanup_free_ char *sresolved = NULL, *dresolved = NULL;
4191 char *s = NULL, *d = NULL;
4192 bool rbind = true, ignore_enoent = false;
4193
4194 r = extract_first_word(&p, &source, ":" WHITESPACE, EXTRACT_UNQUOTE|EXTRACT_DONT_COALESCE_SEPARATORS);
4195 if (r == 0)
4196 break;
4197 if (r == -ENOMEM)
4198 return log_oom();
4199 if (r < 0) {
4200 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse %s, ignoring: %s", lvalue, rvalue);
4201 return 0;
4202 }
4203
4204 r = unit_full_printf(u, source, &sresolved);
4205 if (r < 0) {
4206 log_syntax(unit, LOG_ERR, filename, line, r,
4207 "Failed to resolved unit specifiers in \"%s\", ignoring: %m", source);
4208 continue;
4209 }
4210
4211 s = sresolved;
4212 if (s[0] == '-') {
4213 ignore_enoent = true;
4214 s++;
4215 }
4216
4217 r = path_simplify_and_warn(s, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
4218 if (r < 0)
4219 continue;
4220
4221 /* Optionally, the destination is specified. */
4222 if (p && p[-1] == ':') {
4223 r = extract_first_word(&p, &destination, ":" WHITESPACE, EXTRACT_UNQUOTE|EXTRACT_DONT_COALESCE_SEPARATORS);
4224 if (r == -ENOMEM)
4225 return log_oom();
4226 if (r < 0) {
4227 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse %s, ignoring: %s", lvalue, rvalue);
4228 return 0;
4229 }
4230 if (r == 0) {
4231 log_syntax(unit, LOG_ERR, filename, line, 0, "Missing argument after ':', ignoring: %s", s);
4232 continue;
4233 }
4234
4235 r = unit_full_printf(u, destination, &dresolved);
4236 if (r < 0) {
4237 log_syntax(unit, LOG_ERR, filename, line, r,
4238 "Failed to resolved specifiers in \"%s\", ignoring: %m", destination);
4239 continue;
4240 }
4241
4242 r = path_simplify_and_warn(dresolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
4243 if (r < 0)
4244 continue;
4245
4246 d = dresolved;
4247
4248 /* Optionally, there's also a short option string specified */
4249 if (p && p[-1] == ':') {
4250 _cleanup_free_ char *options = NULL;
4251
4252 r = extract_first_word(&p, &options, NULL, EXTRACT_UNQUOTE);
4253 if (r == -ENOMEM)
4254 return log_oom();
4255 if (r < 0) {
4256 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse %s: %s", lvalue, rvalue);
4257 return 0;
4258 }
4259
4260 if (isempty(options) || streq(options, "rbind"))
4261 rbind = true;
4262 else if (streq(options, "norbind"))
4263 rbind = false;
4264 else {
4265 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid option string, ignoring setting: %s", options);
4266 continue;
4267 }
4268 }
4269 } else
4270 d = s;
4271
4272 r = bind_mount_add(&c->bind_mounts, &c->n_bind_mounts,
4273 &(BindMount) {
4274 .source = s,
4275 .destination = d,
4276 .read_only = !!strstr(lvalue, "ReadOnly"),
4277 .recursive = rbind,
4278 .ignore_enoent = ignore_enoent,
4279 });
4280 if (r < 0)
4281 return log_oom();
4282 }
4283
4284 return 0;
4285 }
4286
4287 int config_parse_job_timeout_sec(
4288 const char* unit,
4289 const char *filename,
4290 unsigned line,
4291 const char *section,
4292 unsigned section_line,
4293 const char *lvalue,
4294 int ltype,
4295 const char *rvalue,
4296 void *data,
4297 void *userdata) {
4298
4299 Unit *u = data;
4300 usec_t usec;
4301 int r;
4302
4303 assert(filename);
4304 assert(lvalue);
4305 assert(rvalue);
4306 assert(u);
4307
4308 r = parse_sec_fix_0(rvalue, &usec);
4309 if (r < 0) {
4310 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse JobTimeoutSec= parameter, ignoring: %s", rvalue);
4311 return 0;
4312 }
4313
4314 /* If the user explicitly changed JobTimeoutSec= also change JobRunningTimeoutSec=, for compatibility with old
4315 * versions. If JobRunningTimeoutSec= was explicitly set, avoid this however as whatever the user picked should
4316 * count. */
4317
4318 if (!u->job_running_timeout_set)
4319 u->job_running_timeout = usec;
4320
4321 u->job_timeout = usec;
4322
4323 return 0;
4324 }
4325
4326 int config_parse_job_running_timeout_sec(
4327 const char* unit,
4328 const char *filename,
4329 unsigned line,
4330 const char *section,
4331 unsigned section_line,
4332 const char *lvalue,
4333 int ltype,
4334 const char *rvalue,
4335 void *data,
4336 void *userdata) {
4337
4338 Unit *u = data;
4339 usec_t usec;
4340 int r;
4341
4342 assert(filename);
4343 assert(lvalue);
4344 assert(rvalue);
4345 assert(u);
4346
4347 r = parse_sec_fix_0(rvalue, &usec);
4348 if (r < 0) {
4349 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse JobRunningTimeoutSec= parameter, ignoring: %s", rvalue);
4350 return 0;
4351 }
4352
4353 u->job_running_timeout = usec;
4354 u->job_running_timeout_set = true;
4355
4356 return 0;
4357 }
4358
4359 int config_parse_emergency_action(
4360 const char* unit,
4361 const char *filename,
4362 unsigned line,
4363 const char *section,
4364 unsigned section_line,
4365 const char *lvalue,
4366 int ltype,
4367 const char *rvalue,
4368 void *data,
4369 void *userdata) {
4370
4371 Manager *m = NULL;
4372 EmergencyAction *x = data;
4373 int r;
4374
4375 assert(filename);
4376 assert(lvalue);
4377 assert(rvalue);
4378 assert(data);
4379
4380 if (unit)
4381 m = ((Unit*) userdata)->manager;
4382 else
4383 m = data;
4384
4385 r = parse_emergency_action(rvalue, MANAGER_IS_SYSTEM(m), x);
4386 if (r < 0) {
4387 if (r == -EOPNOTSUPP && MANAGER_IS_USER(m)) {
4388 /* Compat mode: remove for systemd 241. */
4389
4390 log_syntax(unit, LOG_INFO, filename, line, r,
4391 "%s= in user mode specified as \"%s\", using \"exit-force\" instead.",
4392 lvalue, rvalue);
4393 *x = EMERGENCY_ACTION_EXIT_FORCE;
4394 return 0;
4395 }
4396
4397 if (r == -EOPNOTSUPP)
4398 log_syntax(unit, LOG_ERR, filename, line, r,
4399 "%s= specified as %s mode action, ignoring: %s",
4400 lvalue, MANAGER_IS_SYSTEM(m) ? "user" : "system", rvalue);
4401 else
4402 log_syntax(unit, LOG_ERR, filename, line, r,
4403 "Failed to parse %s=, ignoring: %s", lvalue, rvalue);
4404 return 0;
4405 }
4406
4407 return 0;
4408 }
4409
4410 int config_parse_pid_file(
4411 const char *unit,
4412 const char *filename,
4413 unsigned line,
4414 const char *section,
4415 unsigned section_line,
4416 const char *lvalue,
4417 int ltype,
4418 const char *rvalue,
4419 void *data,
4420 void *userdata) {
4421
4422 _cleanup_free_ char *k = NULL, *n = NULL;
4423 const Unit *u = userdata;
4424 char **s = data;
4425 int r;
4426
4427 assert(filename);
4428 assert(lvalue);
4429 assert(rvalue);
4430 assert(u);
4431
4432 if (isempty(rvalue)) {
4433 /* An empty assignment removes already set value. */
4434 *s = mfree(*s);
4435 return 0;
4436 }
4437
4438 r = unit_full_printf(u, rvalue, &k);
4439 if (r < 0) {
4440 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
4441 return 0;
4442 }
4443
4444 /* If this is a relative path make it absolute by prefixing the /run */
4445 n = path_make_absolute(k, u->manager->prefix[EXEC_DIRECTORY_RUNTIME]);
4446 if (!n)
4447 return log_oom();
4448
4449 /* Check that the result is a sensible path */
4450 r = path_simplify_and_warn(n, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
4451 if (r < 0)
4452 return r;
4453
4454 r = patch_var_run(unit, filename, line, lvalue, &n);
4455 if (r < 0)
4456 return r;
4457
4458 free_and_replace(*s, n);
4459 return 0;
4460 }
4461
4462 int config_parse_exit_status(
4463 const char *unit,
4464 const char *filename,
4465 unsigned line,
4466 const char *section,
4467 unsigned section_line,
4468 const char *lvalue,
4469 int ltype,
4470 const char *rvalue,
4471 void *data,
4472 void *userdata) {
4473
4474 int *exit_status = data, r;
4475 uint8_t u;
4476
4477 assert(filename);
4478 assert(lvalue);
4479 assert(rvalue);
4480 assert(exit_status);
4481
4482 if (isempty(rvalue)) {
4483 *exit_status = -1;
4484 return 0;
4485 }
4486
4487 r = safe_atou8(rvalue, &u);
4488 if (r < 0) {
4489 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse exit status '%s', ignoring: %m", rvalue);
4490 return 0;
4491 }
4492
4493 *exit_status = u;
4494 return 0;
4495 }
4496
4497 int config_parse_disable_controllers(
4498 const char *unit,
4499 const char *filename,
4500 unsigned line,
4501 const char *section,
4502 unsigned section_line,
4503 const char *lvalue,
4504 int ltype,
4505 const char *rvalue,
4506 void *data,
4507 void *userdata) {
4508
4509 int r;
4510 CGroupContext *c = data;
4511 CGroupMask disabled_mask;
4512
4513 /* 1. If empty, make all controllers eligible for use again.
4514 * 2. If non-empty, merge all listed controllers, space separated. */
4515
4516 if (isempty(rvalue)) {
4517 c->disable_controllers = 0;
4518 return 0;
4519 }
4520
4521 r = cg_mask_from_string(rvalue, &disabled_mask);
4522 if (r < 0 || disabled_mask <= 0) {
4523 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid cgroup string: %s, ignoring", rvalue);
4524 return 0;
4525 }
4526
4527 c->disable_controllers |= disabled_mask;
4528
4529 return 0;
4530 }
4531
4532 int config_parse_ip_filter_bpf_progs(
4533 const char *unit,
4534 const char *filename,
4535 unsigned line,
4536 const char *section,
4537 unsigned section_line,
4538 const char *lvalue,
4539 int ltype,
4540 const char *rvalue,
4541 void *data,
4542 void *userdata) {
4543
4544 _cleanup_free_ char *resolved = NULL;
4545 const Unit *u = userdata;
4546 char ***paths = data;
4547 int r;
4548
4549 assert(filename);
4550 assert(lvalue);
4551 assert(rvalue);
4552 assert(paths);
4553
4554 if (isempty(rvalue)) {
4555 *paths = strv_free(*paths);
4556 return 0;
4557 }
4558
4559 r = unit_full_printf(u, rvalue, &resolved);
4560 if (r < 0) {
4561 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
4562 return 0;
4563 }
4564
4565 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
4566 if (r < 0)
4567 return 0;
4568
4569 if (strv_contains(*paths, resolved))
4570 return 0;
4571
4572 r = strv_extend(paths, resolved);
4573 if (r < 0)
4574 return log_oom();
4575
4576 r = bpf_firewall_supported();
4577 if (r < 0)
4578 return r;
4579 if (r != BPF_FIREWALL_SUPPORTED_WITH_MULTI) {
4580 static bool warned = false;
4581
4582 log_full(warned ? LOG_DEBUG : LOG_WARNING,
4583 "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"
4584 "Starting this unit will fail! (This warning is only shown for the first loaded unit using IP firewalling.)", filename, line, lvalue, rvalue);
4585
4586 warned = true;
4587 }
4588
4589 return 0;
4590 }
4591
4592 static int merge_by_names(Unit **u, Set *names, const char *id) {
4593 char *k;
4594 int r;
4595
4596 assert(u);
4597 assert(*u);
4598
4599 /* Let's try to add in all names that are aliases of this unit */
4600 while ((k = set_steal_first(names))) {
4601 _cleanup_free_ _unused_ char *free_k = k;
4602
4603 /* First try to merge in the other name into our unit */
4604 r = unit_merge_by_name(*u, k);
4605 if (r < 0) {
4606 Unit *other;
4607
4608 /* Hmm, we couldn't merge the other unit into ours? Then let's try it the other way
4609 * round. */
4610
4611 other = manager_get_unit((*u)->manager, k);
4612 if (!other)
4613 return r; /* return previous failure */
4614
4615 r = unit_merge(other, *u);
4616 if (r < 0)
4617 return r;
4618
4619 *u = other;
4620 return merge_by_names(u, names, NULL);
4621 }
4622
4623 if (streq_ptr(id, k))
4624 unit_choose_id(*u, id);
4625 }
4626
4627 return 0;
4628 }
4629
4630 int unit_load_fragment(Unit *u) {
4631 const char *fragment;
4632 _cleanup_set_free_free_ Set *names = NULL;
4633 struct stat st;
4634 int r;
4635
4636 assert(u);
4637 assert(u->load_state == UNIT_STUB);
4638 assert(u->id);
4639
4640 if (u->transient) {
4641 u->load_state = UNIT_LOADED;
4642 return 0;
4643 }
4644
4645 /* Possibly rebuild the fragment map to catch new units */
4646 r = unit_file_build_name_map(&u->manager->lookup_paths,
4647 &u->manager->unit_cache_mtime,
4648 &u->manager->unit_id_map,
4649 &u->manager->unit_name_map,
4650 &u->manager->unit_path_cache);
4651 if (r < 0)
4652 log_error_errno(r, "Failed to rebuild name map: %m");
4653
4654 r = unit_file_find_fragment(u->manager->unit_id_map,
4655 u->manager->unit_name_map,
4656 u->id,
4657 &fragment,
4658 &names);
4659 if (r < 0 && r != -ENOENT)
4660 return r;
4661
4662 if (fragment) {
4663 /* Open the file, check if this is a mask, otherwise read. */
4664 _cleanup_fclose_ FILE *f = NULL;
4665
4666 /* Try to open the file name. A symlink is OK, for example for linked files or masks. We
4667 * expect that all symlinks within the lookup paths have been already resolved, but we don't
4668 * verify this here. */
4669 f = fopen(fragment, "re");
4670 if (!f)
4671 return log_unit_notice_errno(u, errno, "Failed to open %s: %m", fragment);
4672
4673 if (fstat(fileno(f), &st) < 0)
4674 return -errno;
4675
4676 r = free_and_strdup(&u->fragment_path, fragment);
4677 if (r < 0)
4678 return r;
4679
4680 if (null_or_empty(&st)) {
4681 u->load_state = UNIT_MASKED;
4682 u->fragment_mtime = 0;
4683 } else {
4684 u->load_state = UNIT_LOADED;
4685 u->fragment_mtime = timespec_load(&st.st_mtim);
4686
4687 /* Now, parse the file contents */
4688 r = config_parse(u->id, fragment, f,
4689 UNIT_VTABLE(u)->sections,
4690 config_item_perf_lookup, load_fragment_gperf_lookup,
4691 CONFIG_PARSE_ALLOW_INCLUDE, u);
4692 if (r == -ENOEXEC)
4693 log_unit_notice_errno(u, r, "Unit configuration has fatal error, unit will not be started.");
4694 if (r < 0)
4695 return r;
4696 }
4697 }
4698
4699 if (u->source_path) {
4700 if (stat(u->source_path, &st) >= 0)
4701 u->source_mtime = timespec_load(&st.st_mtim);
4702 else
4703 u->source_mtime = 0;
4704 }
4705
4706 /* We do the merge dance here because for some unit types, the unit might have aliases which are not
4707 * declared in the file system. In particular, this is true (and frequent) for device and swap units.
4708 */
4709 Unit *merged;
4710 const char *id = u->id;
4711 _cleanup_free_ char *free_id = NULL;
4712
4713 if (fragment) {
4714 id = basename(fragment);
4715 if (unit_name_is_valid(id, UNIT_NAME_TEMPLATE)) {
4716 assert(u->instance); /* If we're not trying to use a template for non-instanced unit,
4717 * this must be set. */
4718
4719 r = unit_name_replace_instance(id, u->instance, &free_id);
4720 if (r < 0)
4721 return log_debug_errno(r, "Failed to build id (%s + %s): %m", id, u->instance);
4722 id = free_id;
4723 }
4724 }
4725
4726 merged = u;
4727 r = merge_by_names(&merged, names, id);
4728 if (r < 0)
4729 return r;
4730
4731 if (merged != u)
4732 u->load_state = UNIT_MERGED;
4733
4734 return 0;
4735 }
4736
4737 void unit_dump_config_items(FILE *f) {
4738 static const struct {
4739 const ConfigParserCallback callback;
4740 const char *rvalue;
4741 } table[] = {
4742 { config_parse_warn_compat, "NOTSUPPORTED" },
4743 { config_parse_int, "INTEGER" },
4744 { config_parse_unsigned, "UNSIGNED" },
4745 { config_parse_iec_size, "SIZE" },
4746 { config_parse_iec_uint64, "SIZE" },
4747 { config_parse_si_size, "SIZE" },
4748 { config_parse_bool, "BOOLEAN" },
4749 { config_parse_string, "STRING" },
4750 { config_parse_path, "PATH" },
4751 { config_parse_unit_path_printf, "PATH" },
4752 { config_parse_strv, "STRING [...]" },
4753 { config_parse_exec_nice, "NICE" },
4754 { config_parse_exec_oom_score_adjust, "OOMSCOREADJUST" },
4755 { config_parse_exec_io_class, "IOCLASS" },
4756 { config_parse_exec_io_priority, "IOPRIORITY" },
4757 { config_parse_exec_cpu_sched_policy, "CPUSCHEDPOLICY" },
4758 { config_parse_exec_cpu_sched_prio, "CPUSCHEDPRIO" },
4759 { config_parse_exec_cpu_affinity, "CPUAFFINITY" },
4760 { config_parse_mode, "MODE" },
4761 { config_parse_unit_env_file, "FILE" },
4762 { config_parse_exec_output, "OUTPUT" },
4763 { config_parse_exec_input, "INPUT" },
4764 { config_parse_log_facility, "FACILITY" },
4765 { config_parse_log_level, "LEVEL" },
4766 { config_parse_exec_secure_bits, "SECUREBITS" },
4767 { config_parse_capability_set, "BOUNDINGSET" },
4768 { config_parse_rlimit, "LIMIT" },
4769 { config_parse_unit_deps, "UNIT [...]" },
4770 { config_parse_exec, "PATH [ARGUMENT [...]]" },
4771 { config_parse_service_type, "SERVICETYPE" },
4772 { config_parse_service_restart, "SERVICERESTART" },
4773 { config_parse_kill_mode, "KILLMODE" },
4774 { config_parse_signal, "SIGNAL" },
4775 { config_parse_socket_listen, "SOCKET [...]" },
4776 { config_parse_socket_bind, "SOCKETBIND" },
4777 { config_parse_socket_bindtodevice, "NETWORKINTERFACE" },
4778 { config_parse_sec, "SECONDS" },
4779 { config_parse_nsec, "NANOSECONDS" },
4780 { config_parse_namespace_path_strv, "PATH [...]" },
4781 { config_parse_bind_paths, "PATH[:PATH[:OPTIONS]] [...]" },
4782 { config_parse_unit_requires_mounts_for, "PATH [...]" },
4783 { config_parse_exec_mount_flags, "MOUNTFLAG [...]" },
4784 { config_parse_unit_string_printf, "STRING" },
4785 { config_parse_trigger_unit, "UNIT" },
4786 { config_parse_timer, "TIMER" },
4787 { config_parse_path_spec, "PATH" },
4788 { config_parse_notify_access, "ACCESS" },
4789 { config_parse_ip_tos, "TOS" },
4790 { config_parse_unit_condition_path, "CONDITION" },
4791 { config_parse_unit_condition_string, "CONDITION" },
4792 { config_parse_unit_condition_null, "CONDITION" },
4793 { config_parse_unit_slice, "SLICE" },
4794 { config_parse_documentation, "URL" },
4795 { config_parse_service_timeout, "SECONDS" },
4796 { config_parse_emergency_action, "ACTION" },
4797 { config_parse_set_status, "STATUS" },
4798 { config_parse_service_sockets, "SOCKETS" },
4799 { config_parse_environ, "ENVIRON" },
4800 #if HAVE_SECCOMP
4801 { config_parse_syscall_filter, "SYSCALLS" },
4802 { config_parse_syscall_archs, "ARCHS" },
4803 { config_parse_syscall_errno, "ERRNO" },
4804 { config_parse_address_families, "FAMILIES" },
4805 { config_parse_restrict_namespaces, "NAMESPACES" },
4806 #endif
4807 { config_parse_cpu_shares, "SHARES" },
4808 { config_parse_cg_weight, "WEIGHT" },
4809 { config_parse_memory_limit, "LIMIT" },
4810 { config_parse_device_allow, "DEVICE" },
4811 { config_parse_device_policy, "POLICY" },
4812 { config_parse_io_limit, "LIMIT" },
4813 { config_parse_io_device_weight, "DEVICEWEIGHT" },
4814 { config_parse_io_device_latency, "DEVICELATENCY" },
4815 { config_parse_blockio_bandwidth, "BANDWIDTH" },
4816 { config_parse_blockio_weight, "WEIGHT" },
4817 { config_parse_blockio_device_weight, "DEVICEWEIGHT" },
4818 { config_parse_long, "LONG" },
4819 { config_parse_socket_service, "SERVICE" },
4820 #if HAVE_SELINUX
4821 { config_parse_exec_selinux_context, "LABEL" },
4822 #endif
4823 { config_parse_job_mode, "MODE" },
4824 { config_parse_job_mode_isolate, "BOOLEAN" },
4825 { config_parse_personality, "PERSONALITY" },
4826 };
4827
4828 const char *prev = NULL;
4829 const char *i;
4830
4831 assert(f);
4832
4833 NULSTR_FOREACH(i, load_fragment_gperf_nulstr) {
4834 const char *rvalue = "OTHER", *lvalue;
4835 const ConfigPerfItem *p;
4836 size_t prefix_len;
4837 const char *dot;
4838 unsigned j;
4839
4840 assert_se(p = load_fragment_gperf_lookup(i, strlen(i)));
4841
4842 /* Hide legacy settings */
4843 if (p->parse == config_parse_warn_compat &&
4844 p->ltype == DISABLED_LEGACY)
4845 continue;
4846
4847 for (j = 0; j < ELEMENTSOF(table); j++)
4848 if (p->parse == table[j].callback) {
4849 rvalue = table[j].rvalue;
4850 break;
4851 }
4852
4853 dot = strchr(i, '.');
4854 lvalue = dot ? dot + 1 : i;
4855 prefix_len = dot-i;
4856
4857 if (dot)
4858 if (!prev || !strneq(prev, i, prefix_len+1)) {
4859 if (prev)
4860 fputc('\n', f);
4861
4862 fprintf(f, "[%.*s]\n", (int) prefix_len, i);
4863 }
4864
4865 fprintf(f, "%s=%s\n", lvalue, rvalue);
4866 prev = i;
4867 }
4868 }
4869
4870 int config_parse_cpu_affinity2(
4871 const char *unit,
4872 const char *filename,
4873 unsigned line,
4874 const char *section,
4875 unsigned section_line,
4876 const char *lvalue,
4877 int ltype,
4878 const char *rvalue,
4879 void *data,
4880 void *userdata) {
4881
4882 CPUSet *affinity = data;
4883
4884 assert(affinity);
4885
4886 (void) parse_cpu_set_extend(rvalue, affinity, true, unit, filename, line, lvalue);
4887
4888 return 0;
4889 }
4890
4891 int config_parse_show_status(
4892 const char* unit,
4893 const char *filename,
4894 unsigned line,
4895 const char *section,
4896 unsigned section_line,
4897 const char *lvalue,
4898 int ltype,
4899 const char *rvalue,
4900 void *data,
4901 void *userdata) {
4902
4903 int k;
4904 ShowStatus *b = data;
4905
4906 assert(filename);
4907 assert(lvalue);
4908 assert(rvalue);
4909 assert(data);
4910
4911 k = parse_show_status(rvalue, b);
4912 if (k < 0) {
4913 log_syntax(unit, LOG_ERR, filename, line, k, "Failed to parse show status setting, ignoring: %s", rvalue);
4914 return 0;
4915 }
4916
4917 return 0;
4918 }
4919
4920 int config_parse_output_restricted(
4921 const char* unit,
4922 const char *filename,
4923 unsigned line,
4924 const char *section,
4925 unsigned section_line,
4926 const char *lvalue,
4927 int ltype,
4928 const char *rvalue,
4929 void *data,
4930 void *userdata) {
4931
4932 ExecOutput t, *eo = data;
4933
4934 assert(filename);
4935 assert(lvalue);
4936 assert(rvalue);
4937 assert(data);
4938
4939 t = exec_output_from_string(rvalue);
4940 if (t < 0) {
4941 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse output type, ignoring: %s", rvalue);
4942 return 0;
4943 }
4944
4945 if (IN_SET(t, EXEC_OUTPUT_SOCKET, EXEC_OUTPUT_NAMED_FD, EXEC_OUTPUT_FILE, EXEC_OUTPUT_FILE_APPEND)) {
4946 log_syntax(unit, LOG_ERR, filename, line, 0, "Standard output types socket, fd:, file:, append: are not supported as defaults, ignoring: %s", rvalue);
4947 return 0;
4948 }
4949
4950 *eo = t;
4951 return 0;
4952 }
4953
4954 int config_parse_crash_chvt(
4955 const char* unit,
4956 const char *filename,
4957 unsigned line,
4958 const char *section,
4959 unsigned section_line,
4960 const char *lvalue,
4961 int ltype,
4962 const char *rvalue,
4963 void *data,
4964 void *userdata) {
4965
4966 int r;
4967
4968 assert(filename);
4969 assert(lvalue);
4970 assert(rvalue);
4971 assert(data);
4972
4973 r = parse_crash_chvt(rvalue, data);
4974 if (r < 0) {
4975 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse CrashChangeVT= setting, ignoring: %s", rvalue);
4976 return 0;
4977 }
4978
4979 return 0;
4980 }
4981
4982 int config_parse_timeout_abort(
4983 const char* unit,
4984 const char *filename,
4985 unsigned line,
4986 const char *section,
4987 unsigned section_line,
4988 const char *lvalue,
4989 int ltype,
4990 const char *rvalue,
4991 void *data,
4992 void *userdata) {
4993
4994 usec_t *timeout_usec = data;
4995 int r;
4996
4997 assert(filename);
4998 assert(lvalue);
4999 assert(rvalue);
5000 assert(timeout_usec);
5001
5002 rvalue += strspn(rvalue, WHITESPACE);
5003 if (isempty(rvalue)) {
5004 *timeout_usec = false;
5005 return 0;
5006 }
5007
5008 r = parse_sec(rvalue, timeout_usec);
5009 if (r < 0) {
5010 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse DefaultTimeoutAbortSec= setting, ignoring: %s", rvalue);
5011 return 0;
5012 }
5013
5014 *timeout_usec = true;
5015 return 0;
5016 }