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