]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/load-fragment.c
Merge pull request #15557 from poettering/journal-zero-fix
[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 bool obsolete = false;
1103 ExecOutput eo;
1104 int r;
1105
1106 assert(data);
1107 assert(filename);
1108 assert(line);
1109 assert(lvalue);
1110 assert(rvalue);
1111
1112 n = startswith(rvalue, "fd:");
1113 if (n) {
1114 r = unit_full_printf(u, n, &resolved);
1115 if (r < 0)
1116 return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s: %m", n);
1117
1118 if (isempty(resolved))
1119 resolved = mfree(resolved);
1120 else if (!fdname_is_valid(resolved)) {
1121 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid file descriptor name: %s", resolved);
1122 return -ENOEXEC;
1123 }
1124
1125 eo = EXEC_OUTPUT_NAMED_FD;
1126
1127 } else if (streq(rvalue, "syslog")) {
1128 eo = EXEC_OUTPUT_JOURNAL;
1129 obsolete = true;
1130
1131 } else if (streq(rvalue, "syslog+console")) {
1132 eo = EXEC_OUTPUT_JOURNAL_AND_CONSOLE;
1133 obsolete = true;
1134
1135 } else if ((n = startswith(rvalue, "file:"))) {
1136
1137 r = unit_full_printf(u, n, &resolved);
1138 if (r < 0)
1139 return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s: %m", n);
1140
1141 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE | PATH_CHECK_FATAL, unit, filename, line, lvalue);
1142 if (r < 0)
1143 return -ENOEXEC;
1144
1145 eo = EXEC_OUTPUT_FILE;
1146
1147 } else if ((n = startswith(rvalue, "append:"))) {
1148
1149 r = unit_full_printf(u, n, &resolved);
1150 if (r < 0)
1151 return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s: %m", n);
1152
1153 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE | PATH_CHECK_FATAL, unit, filename, line, lvalue);
1154 if (r < 0)
1155 return -ENOEXEC;
1156
1157 eo = EXEC_OUTPUT_FILE_APPEND;
1158 } else {
1159 eo = exec_output_from_string(rvalue);
1160 if (eo < 0) {
1161 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse output specifier, ignoring: %s", rvalue);
1162 return 0;
1163 }
1164 }
1165
1166 if (obsolete)
1167 log_syntax(unit, LOG_NOTICE, filename, line, 0,
1168 "Standard output type %s is obsolete, automatically updating to %s. Please update your unit file, and consider removing the setting altogether.",
1169 rvalue, exec_output_to_string(eo));
1170
1171 if (streq(lvalue, "StandardOutput")) {
1172 if (eo == EXEC_OUTPUT_NAMED_FD)
1173 free_and_replace(c->stdio_fdname[STDOUT_FILENO], resolved);
1174 else
1175 free_and_replace(c->stdio_file[STDOUT_FILENO], resolved);
1176
1177 c->std_output = eo;
1178
1179 } else {
1180 assert(streq(lvalue, "StandardError"));
1181
1182 if (eo == EXEC_OUTPUT_NAMED_FD)
1183 free_and_replace(c->stdio_fdname[STDERR_FILENO], resolved);
1184 else
1185 free_and_replace(c->stdio_file[STDERR_FILENO], resolved);
1186
1187 c->std_error = eo;
1188 }
1189
1190 return 0;
1191 }
1192
1193 int config_parse_exec_io_class(const char *unit,
1194 const char *filename,
1195 unsigned line,
1196 const char *section,
1197 unsigned section_line,
1198 const char *lvalue,
1199 int ltype,
1200 const char *rvalue,
1201 void *data,
1202 void *userdata) {
1203
1204 ExecContext *c = data;
1205 int x;
1206
1207 assert(filename);
1208 assert(lvalue);
1209 assert(rvalue);
1210 assert(data);
1211
1212 if (isempty(rvalue)) {
1213 c->ioprio_set = false;
1214 c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0);
1215 return 0;
1216 }
1217
1218 x = ioprio_class_from_string(rvalue);
1219 if (x < 0) {
1220 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse IO scheduling class, ignoring: %s", rvalue);
1221 return 0;
1222 }
1223
1224 c->ioprio = IOPRIO_PRIO_VALUE(x, IOPRIO_PRIO_DATA(c->ioprio));
1225 c->ioprio_set = true;
1226
1227 return 0;
1228 }
1229
1230 int config_parse_exec_io_priority(const char *unit,
1231 const char *filename,
1232 unsigned line,
1233 const char *section,
1234 unsigned section_line,
1235 const char *lvalue,
1236 int ltype,
1237 const char *rvalue,
1238 void *data,
1239 void *userdata) {
1240
1241 ExecContext *c = data;
1242 int i, r;
1243
1244 assert(filename);
1245 assert(lvalue);
1246 assert(rvalue);
1247 assert(data);
1248
1249 if (isempty(rvalue)) {
1250 c->ioprio_set = false;
1251 c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0);
1252 return 0;
1253 }
1254
1255 r = ioprio_parse_priority(rvalue, &i);
1256 if (r < 0) {
1257 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse IO priority, ignoring: %s", rvalue);
1258 return 0;
1259 }
1260
1261 c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_PRIO_CLASS(c->ioprio), i);
1262 c->ioprio_set = true;
1263
1264 return 0;
1265 }
1266
1267 int config_parse_exec_cpu_sched_policy(const char *unit,
1268 const char *filename,
1269 unsigned line,
1270 const char *section,
1271 unsigned section_line,
1272 const char *lvalue,
1273 int ltype,
1274 const char *rvalue,
1275 void *data,
1276 void *userdata) {
1277
1278 ExecContext *c = data;
1279 int x;
1280
1281 assert(filename);
1282 assert(lvalue);
1283 assert(rvalue);
1284 assert(data);
1285
1286 if (isempty(rvalue)) {
1287 c->cpu_sched_set = false;
1288 c->cpu_sched_policy = SCHED_OTHER;
1289 c->cpu_sched_priority = 0;
1290 return 0;
1291 }
1292
1293 x = sched_policy_from_string(rvalue);
1294 if (x < 0) {
1295 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse CPU scheduling policy, ignoring: %s", rvalue);
1296 return 0;
1297 }
1298
1299 c->cpu_sched_policy = x;
1300 /* Moving to or from real-time policy? We need to adjust the priority */
1301 c->cpu_sched_priority = CLAMP(c->cpu_sched_priority, sched_get_priority_min(x), sched_get_priority_max(x));
1302 c->cpu_sched_set = true;
1303
1304 return 0;
1305 }
1306
1307 int config_parse_numa_mask(const char *unit,
1308 const char *filename,
1309 unsigned line,
1310 const char *section,
1311 unsigned section_line,
1312 const char *lvalue,
1313 int ltype,
1314 const char *rvalue,
1315 void *data,
1316 void *userdata) {
1317 int r;
1318 NUMAPolicy *p = data;
1319
1320 assert(filename);
1321 assert(lvalue);
1322 assert(rvalue);
1323 assert(data);
1324
1325 r = parse_cpu_set_extend(rvalue, &p->nodes, true, unit, filename, line, lvalue);
1326 if (r < 0) {
1327 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse NUMA node mask, ignoring: %s", rvalue);
1328 return 0;
1329 }
1330
1331 return r;
1332 }
1333
1334 int config_parse_exec_cpu_sched_prio(const char *unit,
1335 const char *filename,
1336 unsigned line,
1337 const char *section,
1338 unsigned section_line,
1339 const char *lvalue,
1340 int ltype,
1341 const char *rvalue,
1342 void *data,
1343 void *userdata) {
1344
1345 ExecContext *c = data;
1346 int i, min, max, r;
1347
1348 assert(filename);
1349 assert(lvalue);
1350 assert(rvalue);
1351 assert(data);
1352
1353 r = safe_atoi(rvalue, &i);
1354 if (r < 0) {
1355 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse CPU scheduling priority, ignoring: %s", rvalue);
1356 return 0;
1357 }
1358
1359 /* On Linux RR/FIFO range from 1 to 99 and OTHER/BATCH may only be 0 */
1360 min = sched_get_priority_min(c->cpu_sched_policy);
1361 max = sched_get_priority_max(c->cpu_sched_policy);
1362
1363 if (i < min || i > max) {
1364 log_syntax(unit, LOG_ERR, filename, line, 0, "CPU scheduling priority is out of range, ignoring: %s", rvalue);
1365 return 0;
1366 }
1367
1368 c->cpu_sched_priority = i;
1369 c->cpu_sched_set = true;
1370
1371 return 0;
1372 }
1373
1374 int config_parse_exec_cpu_affinity(const char *unit,
1375 const char *filename,
1376 unsigned line,
1377 const char *section,
1378 unsigned section_line,
1379 const char *lvalue,
1380 int ltype,
1381 const char *rvalue,
1382 void *data,
1383 void *userdata) {
1384
1385 ExecContext *c = data;
1386 int r;
1387
1388 assert(filename);
1389 assert(lvalue);
1390 assert(rvalue);
1391 assert(data);
1392
1393 if (streq(rvalue, "numa")) {
1394 c->cpu_affinity_from_numa = true;
1395 cpu_set_reset(&c->cpu_set);
1396
1397 return 0;
1398 }
1399
1400 r = parse_cpu_set_extend(rvalue, &c->cpu_set, true, unit, filename, line, lvalue);
1401 if (r >= 0)
1402 c->cpu_affinity_from_numa = false;
1403
1404 return r;
1405 }
1406
1407 int config_parse_capability_set(
1408 const char *unit,
1409 const char *filename,
1410 unsigned line,
1411 const char *section,
1412 unsigned section_line,
1413 const char *lvalue,
1414 int ltype,
1415 const char *rvalue,
1416 void *data,
1417 void *userdata) {
1418
1419 uint64_t *capability_set = data;
1420 uint64_t sum = 0, initial = 0;
1421 bool invert = false;
1422 int r;
1423
1424 assert(filename);
1425 assert(lvalue);
1426 assert(rvalue);
1427 assert(data);
1428
1429 if (rvalue[0] == '~') {
1430 invert = true;
1431 rvalue++;
1432 }
1433
1434 if (streq(lvalue, "CapabilityBoundingSet"))
1435 initial = CAP_ALL; /* initialized to all bits on */
1436 /* else "AmbientCapabilities" initialized to all bits off */
1437
1438 r = capability_set_from_string(rvalue, &sum);
1439 if (r < 0) {
1440 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse %s= specifier '%s', ignoring: %m", lvalue, rvalue);
1441 return 0;
1442 }
1443
1444 if (sum == 0 || *capability_set == initial)
1445 /* "", "~" or uninitialized data -> replace */
1446 *capability_set = invert ? ~sum : sum;
1447 else {
1448 /* previous data -> merge */
1449 if (invert)
1450 *capability_set &= ~sum;
1451 else
1452 *capability_set |= sum;
1453 }
1454
1455 return 0;
1456 }
1457
1458 int config_parse_exec_selinux_context(
1459 const char *unit,
1460 const char *filename,
1461 unsigned line,
1462 const char *section,
1463 unsigned section_line,
1464 const char *lvalue,
1465 int ltype,
1466 const char *rvalue,
1467 void *data,
1468 void *userdata) {
1469
1470 ExecContext *c = data;
1471 const Unit *u = userdata;
1472 bool ignore;
1473 char *k;
1474 int r;
1475
1476 assert(filename);
1477 assert(lvalue);
1478 assert(rvalue);
1479 assert(data);
1480
1481 if (isempty(rvalue)) {
1482 c->selinux_context = mfree(c->selinux_context);
1483 c->selinux_context_ignore = false;
1484 return 0;
1485 }
1486
1487 if (rvalue[0] == '-') {
1488 ignore = true;
1489 rvalue++;
1490 } else
1491 ignore = false;
1492
1493 r = unit_full_printf(u, rvalue, &k);
1494 if (r < 0) {
1495 log_syntax(unit, LOG_ERR, filename, line, r,
1496 "Failed to resolve unit specifiers in '%s'%s: %m",
1497 rvalue, ignore ? ", ignoring" : "");
1498 return ignore ? 0 : -ENOEXEC;
1499 }
1500
1501 free_and_replace(c->selinux_context, k);
1502 c->selinux_context_ignore = ignore;
1503
1504 return 0;
1505 }
1506
1507 int config_parse_exec_apparmor_profile(
1508 const char *unit,
1509 const char *filename,
1510 unsigned line,
1511 const char *section,
1512 unsigned section_line,
1513 const char *lvalue,
1514 int ltype,
1515 const char *rvalue,
1516 void *data,
1517 void *userdata) {
1518
1519 ExecContext *c = data;
1520 const Unit *u = userdata;
1521 bool ignore;
1522 char *k;
1523 int r;
1524
1525 assert(filename);
1526 assert(lvalue);
1527 assert(rvalue);
1528 assert(data);
1529
1530 if (isempty(rvalue)) {
1531 c->apparmor_profile = mfree(c->apparmor_profile);
1532 c->apparmor_profile_ignore = false;
1533 return 0;
1534 }
1535
1536 if (rvalue[0] == '-') {
1537 ignore = true;
1538 rvalue++;
1539 } else
1540 ignore = false;
1541
1542 r = unit_full_printf(u, rvalue, &k);
1543 if (r < 0) {
1544 log_syntax(unit, LOG_ERR, filename, line, r,
1545 "Failed to resolve unit specifiers in '%s'%s: %m",
1546 rvalue, ignore ? ", ignoring" : "");
1547 return ignore ? 0 : -ENOEXEC;
1548 }
1549
1550 free_and_replace(c->apparmor_profile, k);
1551 c->apparmor_profile_ignore = ignore;
1552
1553 return 0;
1554 }
1555
1556 int config_parse_exec_smack_process_label(
1557 const char *unit,
1558 const char *filename,
1559 unsigned line,
1560 const char *section,
1561 unsigned section_line,
1562 const char *lvalue,
1563 int ltype,
1564 const char *rvalue,
1565 void *data,
1566 void *userdata) {
1567
1568 ExecContext *c = data;
1569 const Unit *u = userdata;
1570 bool ignore;
1571 char *k;
1572 int r;
1573
1574 assert(filename);
1575 assert(lvalue);
1576 assert(rvalue);
1577 assert(data);
1578
1579 if (isempty(rvalue)) {
1580 c->smack_process_label = mfree(c->smack_process_label);
1581 c->smack_process_label_ignore = false;
1582 return 0;
1583 }
1584
1585 if (rvalue[0] == '-') {
1586 ignore = true;
1587 rvalue++;
1588 } else
1589 ignore = false;
1590
1591 r = unit_full_printf(u, rvalue, &k);
1592 if (r < 0) {
1593 log_syntax(unit, LOG_ERR, filename, line, r,
1594 "Failed to resolve unit specifiers in '%s'%s: %m",
1595 rvalue, ignore ? ", ignoring" : "");
1596 return ignore ? 0 : -ENOEXEC;
1597 }
1598
1599 free_and_replace(c->smack_process_label, k);
1600 c->smack_process_label_ignore = ignore;
1601
1602 return 0;
1603 }
1604
1605 int config_parse_timer(
1606 const char *unit,
1607 const char *filename,
1608 unsigned line,
1609 const char *section,
1610 unsigned section_line,
1611 const char *lvalue,
1612 int ltype,
1613 const char *rvalue,
1614 void *data,
1615 void *userdata) {
1616
1617 _cleanup_(calendar_spec_freep) CalendarSpec *c = NULL;
1618 _cleanup_free_ char *k = NULL;
1619 const Unit *u = userdata;
1620 Timer *t = data;
1621 usec_t usec = 0;
1622 TimerValue *v;
1623 int r;
1624
1625 assert(filename);
1626 assert(lvalue);
1627 assert(rvalue);
1628 assert(data);
1629
1630 if (isempty(rvalue)) {
1631 /* Empty assignment resets list */
1632 timer_free_values(t);
1633 return 0;
1634 }
1635
1636 r = unit_full_printf(u, rvalue, &k);
1637 if (r < 0) {
1638 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
1639 return 0;
1640 }
1641
1642 if (ltype == TIMER_CALENDAR) {
1643 r = calendar_spec_from_string(k, &c);
1644 if (r < 0) {
1645 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse calendar specification, ignoring: %s", k);
1646 return 0;
1647 }
1648 } else {
1649 r = parse_sec(k, &usec);
1650 if (r < 0) {
1651 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse timer value, ignoring: %s", k);
1652 return 0;
1653 }
1654 }
1655
1656 v = new(TimerValue, 1);
1657 if (!v)
1658 return log_oom();
1659
1660 *v = (TimerValue) {
1661 .base = ltype,
1662 .value = usec,
1663 .calendar_spec = TAKE_PTR(c),
1664 };
1665
1666 LIST_PREPEND(value, t->values, v);
1667
1668 return 0;
1669 }
1670
1671 int config_parse_trigger_unit(
1672 const char *unit,
1673 const char *filename,
1674 unsigned line,
1675 const char *section,
1676 unsigned section_line,
1677 const char *lvalue,
1678 int ltype,
1679 const char *rvalue,
1680 void *data,
1681 void *userdata) {
1682
1683 _cleanup_free_ char *p = NULL;
1684 Unit *u = data;
1685 UnitType type;
1686 int r;
1687
1688 assert(filename);
1689 assert(lvalue);
1690 assert(rvalue);
1691 assert(data);
1692
1693 if (!hashmap_isempty(u->dependencies[UNIT_TRIGGERS])) {
1694 log_syntax(unit, LOG_ERR, filename, line, 0, "Multiple units to trigger specified, ignoring: %s", rvalue);
1695 return 0;
1696 }
1697
1698 r = unit_name_printf(u, rvalue, &p);
1699 if (r < 0) {
1700 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue);
1701 return 0;
1702 }
1703
1704 type = unit_name_to_type(p);
1705 if (type < 0) {
1706 log_syntax(unit, LOG_ERR, filename, line, 0, "Unit type not valid, ignoring: %s", rvalue);
1707 return 0;
1708 }
1709 if (unit_has_name(u, p)) {
1710 log_syntax(unit, LOG_ERR, filename, line, 0, "Units cannot trigger themselves, ignoring: %s", rvalue);
1711 return 0;
1712 }
1713
1714 r = unit_add_two_dependencies_by_name(u, UNIT_BEFORE, UNIT_TRIGGERS, p, true, UNIT_DEPENDENCY_FILE);
1715 if (r < 0) {
1716 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to add trigger on %s, ignoring: %m", p);
1717 return 0;
1718 }
1719
1720 return 0;
1721 }
1722
1723 int config_parse_path_spec(const char *unit,
1724 const char *filename,
1725 unsigned line,
1726 const char *section,
1727 unsigned section_line,
1728 const char *lvalue,
1729 int ltype,
1730 const char *rvalue,
1731 void *data,
1732 void *userdata) {
1733
1734 Path *p = data;
1735 PathSpec *s;
1736 PathType b;
1737 _cleanup_free_ char *k = NULL;
1738 int r;
1739
1740 assert(filename);
1741 assert(lvalue);
1742 assert(rvalue);
1743 assert(data);
1744
1745 if (isempty(rvalue)) {
1746 /* Empty assignment clears list */
1747 path_free_specs(p);
1748 return 0;
1749 }
1750
1751 b = path_type_from_string(lvalue);
1752 if (b < 0) {
1753 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse path type, ignoring: %s", lvalue);
1754 return 0;
1755 }
1756
1757 r = unit_full_printf(UNIT(p), rvalue, &k);
1758 if (r < 0) {
1759 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue);
1760 return 0;
1761 }
1762
1763 r = path_simplify_and_warn(k, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
1764 if (r < 0)
1765 return 0;
1766
1767 s = new0(PathSpec, 1);
1768 if (!s)
1769 return log_oom();
1770
1771 s->unit = UNIT(p);
1772 s->path = TAKE_PTR(k);
1773 s->type = b;
1774 s->inotify_fd = -1;
1775
1776 LIST_PREPEND(spec, p->specs, s);
1777
1778 return 0;
1779 }
1780
1781 int config_parse_socket_service(
1782 const char *unit,
1783 const char *filename,
1784 unsigned line,
1785 const char *section,
1786 unsigned section_line,
1787 const char *lvalue,
1788 int ltype,
1789 const char *rvalue,
1790 void *data,
1791 void *userdata) {
1792
1793 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1794 _cleanup_free_ char *p = NULL;
1795 Socket *s = data;
1796 Unit *x;
1797 int r;
1798
1799 assert(filename);
1800 assert(lvalue);
1801 assert(rvalue);
1802 assert(data);
1803
1804 r = unit_name_printf(UNIT(s), rvalue, &p);
1805 if (r < 0) {
1806 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s: %m", rvalue);
1807 return -ENOEXEC;
1808 }
1809
1810 if (!endswith(p, ".service")) {
1811 log_syntax(unit, LOG_ERR, filename, line, 0, "Unit must be of type service: %s", rvalue);
1812 return -ENOEXEC;
1813 }
1814
1815 r = manager_load_unit(UNIT(s)->manager, p, NULL, &error, &x);
1816 if (r < 0) {
1817 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to load unit %s: %s", rvalue, bus_error_message(&error, r));
1818 return -ENOEXEC;
1819 }
1820
1821 unit_ref_set(&s->service, UNIT(s), x);
1822
1823 return 0;
1824 }
1825
1826 int config_parse_fdname(
1827 const char *unit,
1828 const char *filename,
1829 unsigned line,
1830 const char *section,
1831 unsigned section_line,
1832 const char *lvalue,
1833 int ltype,
1834 const char *rvalue,
1835 void *data,
1836 void *userdata) {
1837
1838 _cleanup_free_ char *p = NULL;
1839 Socket *s = data;
1840 int r;
1841
1842 assert(filename);
1843 assert(lvalue);
1844 assert(rvalue);
1845 assert(data);
1846
1847 if (isempty(rvalue)) {
1848 s->fdname = mfree(s->fdname);
1849 return 0;
1850 }
1851
1852 r = unit_full_printf(UNIT(s), rvalue, &p);
1853 if (r < 0) {
1854 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
1855 return 0;
1856 }
1857
1858 if (!fdname_is_valid(p)) {
1859 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid file descriptor name, ignoring: %s", p);
1860 return 0;
1861 }
1862
1863 return free_and_replace(s->fdname, p);
1864 }
1865
1866 int config_parse_service_sockets(
1867 const char *unit,
1868 const char *filename,
1869 unsigned line,
1870 const char *section,
1871 unsigned section_line,
1872 const char *lvalue,
1873 int ltype,
1874 const char *rvalue,
1875 void *data,
1876 void *userdata) {
1877
1878 Service *s = data;
1879 const char *p;
1880 int r;
1881
1882 assert(filename);
1883 assert(lvalue);
1884 assert(rvalue);
1885 assert(data);
1886
1887 p = rvalue;
1888 for (;;) {
1889 _cleanup_free_ char *word = NULL, *k = NULL;
1890
1891 r = extract_first_word(&p, &word, NULL, 0);
1892 if (r == 0)
1893 break;
1894 if (r == -ENOMEM)
1895 return log_oom();
1896 if (r < 0) {
1897 log_syntax(unit, LOG_ERR, filename, line, r, "Trailing garbage in sockets, ignoring: %s", rvalue);
1898 break;
1899 }
1900
1901 r = unit_name_printf(UNIT(s), word, &k);
1902 if (r < 0) {
1903 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", word);
1904 continue;
1905 }
1906
1907 if (!endswith(k, ".socket")) {
1908 log_syntax(unit, LOG_ERR, filename, line, 0, "Unit must be of type socket, ignoring: %s", k);
1909 continue;
1910 }
1911
1912 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_WANTS, UNIT_AFTER, k, true, UNIT_DEPENDENCY_FILE);
1913 if (r < 0)
1914 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to add dependency on %s, ignoring: %m", k);
1915
1916 r = unit_add_dependency_by_name(UNIT(s), UNIT_TRIGGERED_BY, k, true, UNIT_DEPENDENCY_FILE);
1917 if (r < 0)
1918 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to add dependency on %s, ignoring: %m", k);
1919 }
1920
1921 return 0;
1922 }
1923
1924 int config_parse_bus_name(
1925 const char *unit,
1926 const char *filename,
1927 unsigned line,
1928 const char *section,
1929 unsigned section_line,
1930 const char *lvalue,
1931 int ltype,
1932 const char *rvalue,
1933 void *data,
1934 void *userdata) {
1935
1936 _cleanup_free_ char *k = NULL;
1937 const Unit *u = userdata;
1938 int r;
1939
1940 assert(filename);
1941 assert(lvalue);
1942 assert(rvalue);
1943 assert(u);
1944
1945 r = unit_full_printf(u, rvalue, &k);
1946 if (r < 0) {
1947 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue);
1948 return 0;
1949 }
1950
1951 if (!service_name_is_valid(k)) {
1952 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid bus name, ignoring: %s", k);
1953 return 0;
1954 }
1955
1956 return config_parse_string(unit, filename, line, section, section_line, lvalue, ltype, k, data, userdata);
1957 }
1958
1959 int config_parse_service_timeout(
1960 const char *unit,
1961 const char *filename,
1962 unsigned line,
1963 const char *section,
1964 unsigned section_line,
1965 const char *lvalue,
1966 int ltype,
1967 const char *rvalue,
1968 void *data,
1969 void *userdata) {
1970
1971 Service *s = userdata;
1972 usec_t usec;
1973 int r;
1974
1975 assert(filename);
1976 assert(lvalue);
1977 assert(rvalue);
1978 assert(s);
1979
1980 /* This is called for two cases: TimeoutSec= and TimeoutStartSec=. */
1981
1982 /* Traditionally, these options accepted 0 to disable the timeouts. However, a timeout of 0 suggests it happens
1983 * immediately, hence fix this to become USEC_INFINITY instead. This is in-line with how we internally handle
1984 * all other timeouts. */
1985 r = parse_sec_fix_0(rvalue, &usec);
1986 if (r < 0) {
1987 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse %s= parameter, ignoring: %s", lvalue, rvalue);
1988 return 0;
1989 }
1990
1991 s->start_timeout_defined = true;
1992 s->timeout_start_usec = usec;
1993
1994 if (streq(lvalue, "TimeoutSec"))
1995 s->timeout_stop_usec = usec;
1996
1997 return 0;
1998 }
1999
2000 int config_parse_timeout_abort(
2001 const char *unit,
2002 const char *filename,
2003 unsigned line,
2004 const char *section,
2005 unsigned section_line,
2006 const char *lvalue,
2007 int ltype,
2008 const char *rvalue,
2009 void *data,
2010 void *userdata) {
2011
2012 usec_t *ret = data;
2013 int r;
2014
2015 assert(filename);
2016 assert(lvalue);
2017 assert(rvalue);
2018 assert(ret);
2019
2020 /* Note: apart from setting the arg, this returns an extra bit of information in the return value. */
2021
2022 if (isempty(rvalue)) {
2023 *ret = 0;
2024 return 0; /* "not set" */
2025 }
2026
2027 r = parse_sec(rvalue, ret);
2028 if (r < 0)
2029 return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse %s= setting, ignoring: %s", lvalue, rvalue);
2030
2031 return 1; /* "set" */
2032 }
2033
2034 int config_parse_service_timeout_abort(
2035 const char *unit,
2036 const char *filename,
2037 unsigned line,
2038 const char *section,
2039 unsigned section_line,
2040 const char *lvalue,
2041 int ltype,
2042 const char *rvalue,
2043 void *data,
2044 void *userdata) {
2045
2046 Service *s = userdata;
2047 int r;
2048
2049 assert(s);
2050
2051 r = config_parse_timeout_abort(unit, filename, line, section, section_line, lvalue, ltype, rvalue,
2052 &s->timeout_abort_usec, s);
2053 if (r >= 0)
2054 s->timeout_abort_set = r;
2055 return 0;
2056 }
2057
2058 int config_parse_sec_fix_0(
2059 const char *unit,
2060 const char *filename,
2061 unsigned line,
2062 const char *section,
2063 unsigned section_line,
2064 const char *lvalue,
2065 int ltype,
2066 const char *rvalue,
2067 void *data,
2068 void *userdata) {
2069
2070 usec_t *usec = data;
2071 int r;
2072
2073 assert(filename);
2074 assert(lvalue);
2075 assert(rvalue);
2076 assert(usec);
2077
2078 /* This is pretty much like config_parse_sec(), except that this treats a time of 0 as infinity, for
2079 * compatibility with older versions of systemd where 0 instead of infinity was used as indicator to turn off a
2080 * timeout. */
2081
2082 r = parse_sec_fix_0(rvalue, usec);
2083 if (r < 0) {
2084 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse %s= parameter, ignoring: %s", lvalue, rvalue);
2085 return 0;
2086 }
2087
2088 return 0;
2089 }
2090
2091 int config_parse_user_group_compat(
2092 const char *unit,
2093 const char *filename,
2094 unsigned line,
2095 const char *section,
2096 unsigned section_line,
2097 const char *lvalue,
2098 int ltype,
2099 const char *rvalue,
2100 void *data,
2101 void *userdata) {
2102
2103 _cleanup_free_ char *k = NULL;
2104 char **user = data;
2105 const Unit *u = userdata;
2106 int r;
2107
2108 assert(filename);
2109 assert(lvalue);
2110 assert(rvalue);
2111 assert(u);
2112
2113 if (isempty(rvalue)) {
2114 *user = mfree(*user);
2115 return 0;
2116 }
2117
2118 r = unit_full_printf(u, rvalue, &k);
2119 if (r < 0) {
2120 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s: %m", rvalue);
2121 return -ENOEXEC;
2122 }
2123
2124 if (!valid_user_group_name(k, VALID_USER_ALLOW_NUMERIC|VALID_USER_RELAX|VALID_USER_WARN)) {
2125 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid user/group name or numeric ID: %s", k);
2126 return -ENOEXEC;
2127 }
2128
2129 return free_and_replace(*user, k);
2130 }
2131
2132 int config_parse_user_group_strv_compat(
2133 const char *unit,
2134 const char *filename,
2135 unsigned line,
2136 const char *section,
2137 unsigned section_line,
2138 const char *lvalue,
2139 int ltype,
2140 const char *rvalue,
2141 void *data,
2142 void *userdata) {
2143
2144 char ***users = data;
2145 const Unit *u = userdata;
2146 const char *p = rvalue;
2147 int r;
2148
2149 assert(filename);
2150 assert(lvalue);
2151 assert(rvalue);
2152 assert(u);
2153
2154 if (isempty(rvalue)) {
2155 *users = strv_free(*users);
2156 return 0;
2157 }
2158
2159 for (;;) {
2160 _cleanup_free_ char *word = NULL, *k = NULL;
2161
2162 r = extract_first_word(&p, &word, NULL, 0);
2163 if (r == 0)
2164 break;
2165 if (r == -ENOMEM)
2166 return log_oom();
2167 if (r < 0) {
2168 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid syntax: %s", rvalue);
2169 return -ENOEXEC;
2170 }
2171
2172 r = unit_full_printf(u, word, &k);
2173 if (r < 0) {
2174 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s: %m", word);
2175 return -ENOEXEC;
2176 }
2177
2178 if (!valid_user_group_name(k, VALID_USER_ALLOW_NUMERIC|VALID_USER_RELAX|VALID_USER_WARN)) {
2179 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid user/group name or numeric ID: %s", k);
2180 return -ENOEXEC;
2181 }
2182
2183 r = strv_push(users, k);
2184 if (r < 0)
2185 return log_oom();
2186
2187 k = NULL;
2188 }
2189
2190 return 0;
2191 }
2192
2193 int config_parse_working_directory(
2194 const char *unit,
2195 const char *filename,
2196 unsigned line,
2197 const char *section,
2198 unsigned section_line,
2199 const char *lvalue,
2200 int ltype,
2201 const char *rvalue,
2202 void *data,
2203 void *userdata) {
2204
2205 ExecContext *c = data;
2206 const Unit *u = userdata;
2207 bool missing_ok;
2208 int r;
2209
2210 assert(filename);
2211 assert(lvalue);
2212 assert(rvalue);
2213 assert(c);
2214 assert(u);
2215
2216 if (isempty(rvalue)) {
2217 c->working_directory_home = false;
2218 c->working_directory = mfree(c->working_directory);
2219 return 0;
2220 }
2221
2222 if (rvalue[0] == '-') {
2223 missing_ok = true;
2224 rvalue++;
2225 } else
2226 missing_ok = false;
2227
2228 if (streq(rvalue, "~")) {
2229 c->working_directory_home = true;
2230 c->working_directory = mfree(c->working_directory);
2231 } else {
2232 _cleanup_free_ char *k = NULL;
2233
2234 r = unit_full_printf(u, rvalue, &k);
2235 if (r < 0) {
2236 log_syntax(unit, LOG_ERR, filename, line, r,
2237 "Failed to resolve unit specifiers in working directory path '%s'%s: %m",
2238 rvalue, missing_ok ? ", ignoring" : "");
2239 return missing_ok ? 0 : -ENOEXEC;
2240 }
2241
2242 r = path_simplify_and_warn(k, PATH_CHECK_ABSOLUTE | (missing_ok ? 0 : PATH_CHECK_FATAL), unit, filename, line, lvalue);
2243 if (r < 0)
2244 return missing_ok ? 0 : -ENOEXEC;
2245
2246 c->working_directory_home = false;
2247 free_and_replace(c->working_directory, k);
2248 }
2249
2250 c->working_directory_missing_ok = missing_ok;
2251 return 0;
2252 }
2253
2254 int config_parse_unit_env_file(const char *unit,
2255 const char *filename,
2256 unsigned line,
2257 const char *section,
2258 unsigned section_line,
2259 const char *lvalue,
2260 int ltype,
2261 const char *rvalue,
2262 void *data,
2263 void *userdata) {
2264
2265 char ***env = data;
2266 const Unit *u = userdata;
2267 _cleanup_free_ char *n = NULL;
2268 int r;
2269
2270 assert(filename);
2271 assert(lvalue);
2272 assert(rvalue);
2273 assert(data);
2274
2275 if (isempty(rvalue)) {
2276 /* Empty assignment frees the list */
2277 *env = strv_free(*env);
2278 return 0;
2279 }
2280
2281 r = unit_full_printf(u, rvalue, &n);
2282 if (r < 0) {
2283 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
2284 return 0;
2285 }
2286
2287 r = path_simplify_and_warn(n[0] == '-' ? n + 1 : n, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
2288 if (r < 0)
2289 return 0;
2290
2291 r = strv_push(env, n);
2292 if (r < 0)
2293 return log_oom();
2294
2295 n = NULL;
2296
2297 return 0;
2298 }
2299
2300 int config_parse_environ(
2301 const char *unit,
2302 const char *filename,
2303 unsigned line,
2304 const char *section,
2305 unsigned section_line,
2306 const char *lvalue,
2307 int ltype,
2308 const char *rvalue,
2309 void *data,
2310 void *userdata) {
2311
2312 const Unit *u = userdata;
2313 char ***env = data;
2314 const char *p;
2315 int r;
2316
2317 assert(filename);
2318 assert(lvalue);
2319 assert(rvalue);
2320 assert(data);
2321
2322 if (isempty(rvalue)) {
2323 /* Empty assignment resets the list */
2324 *env = strv_free(*env);
2325 return 0;
2326 }
2327
2328 for (p = rvalue;; ) {
2329 _cleanup_free_ char *word = NULL, *k = NULL;
2330
2331 r = extract_first_word(&p, &word, NULL, EXTRACT_CUNESCAPE|EXTRACT_UNQUOTE);
2332 if (r == 0)
2333 return 0;
2334 if (r == -ENOMEM)
2335 return log_oom();
2336 if (r < 0) {
2337 log_syntax(unit, LOG_WARNING, filename, line, r,
2338 "Invalid syntax, ignoring: %s", rvalue);
2339 return 0;
2340 }
2341
2342 if (u) {
2343 r = unit_full_printf(u, word, &k);
2344 if (r < 0) {
2345 log_syntax(unit, LOG_ERR, filename, line, r,
2346 "Failed to resolve unit specifiers in %s, ignoring: %m", word);
2347 continue;
2348 }
2349 } else
2350 k = TAKE_PTR(word);
2351
2352 if (!env_assignment_is_valid(k)) {
2353 log_syntax(unit, LOG_ERR, filename, line, 0,
2354 "Invalid environment assignment, ignoring: %s", k);
2355 continue;
2356 }
2357
2358 r = strv_env_replace(env, k);
2359 if (r < 0)
2360 return log_oom();
2361
2362 k = NULL;
2363 }
2364 }
2365
2366 int config_parse_pass_environ(
2367 const char *unit,
2368 const char *filename,
2369 unsigned line,
2370 const char *section,
2371 unsigned section_line,
2372 const char *lvalue,
2373 int ltype,
2374 const char *rvalue,
2375 void *data,
2376 void *userdata) {
2377
2378 _cleanup_strv_free_ char **n = NULL;
2379 size_t nlen = 0, nbufsize = 0;
2380 char*** passenv = data;
2381 const char *p = rvalue;
2382 const Unit *u = userdata;
2383 int r;
2384
2385 assert(filename);
2386 assert(lvalue);
2387 assert(rvalue);
2388 assert(data);
2389
2390 if (isempty(rvalue)) {
2391 /* Empty assignment resets the list */
2392 *passenv = strv_free(*passenv);
2393 return 0;
2394 }
2395
2396 for (;;) {
2397 _cleanup_free_ char *word = NULL, *k = NULL;
2398
2399 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
2400 if (r == 0)
2401 break;
2402 if (r == -ENOMEM)
2403 return log_oom();
2404 if (r < 0) {
2405 log_syntax(unit, LOG_ERR, filename, line, r,
2406 "Trailing garbage in %s, ignoring: %s", lvalue, rvalue);
2407 break;
2408 }
2409
2410 if (u) {
2411 r = unit_full_printf(u, word, &k);
2412 if (r < 0) {
2413 log_syntax(unit, LOG_ERR, filename, line, r,
2414 "Failed to resolve specifiers in %s, ignoring: %m", word);
2415 continue;
2416 }
2417 } else
2418 k = TAKE_PTR(word);
2419
2420 if (!env_name_is_valid(k)) {
2421 log_syntax(unit, LOG_ERR, filename, line, 0,
2422 "Invalid environment name for %s, ignoring: %s", lvalue, k);
2423 continue;
2424 }
2425
2426 if (!GREEDY_REALLOC(n, nbufsize, nlen + 2))
2427 return log_oom();
2428
2429 n[nlen++] = TAKE_PTR(k);
2430 n[nlen] = NULL;
2431 }
2432
2433 if (n) {
2434 r = strv_extend_strv(passenv, n, true);
2435 if (r < 0)
2436 return r;
2437 }
2438
2439 return 0;
2440 }
2441
2442 int config_parse_unset_environ(
2443 const char *unit,
2444 const char *filename,
2445 unsigned line,
2446 const char *section,
2447 unsigned section_line,
2448 const char *lvalue,
2449 int ltype,
2450 const char *rvalue,
2451 void *data,
2452 void *userdata) {
2453
2454 _cleanup_strv_free_ char **n = NULL;
2455 size_t nlen = 0, nbufsize = 0;
2456 char*** unsetenv = data;
2457 const char *p = rvalue;
2458 const Unit *u = userdata;
2459 int r;
2460
2461 assert(filename);
2462 assert(lvalue);
2463 assert(rvalue);
2464 assert(data);
2465
2466 if (isempty(rvalue)) {
2467 /* Empty assignment resets the list */
2468 *unsetenv = strv_free(*unsetenv);
2469 return 0;
2470 }
2471
2472 for (;;) {
2473 _cleanup_free_ char *word = NULL, *k = NULL;
2474
2475 r = extract_first_word(&p, &word, NULL, EXTRACT_CUNESCAPE|EXTRACT_UNQUOTE);
2476 if (r == 0)
2477 break;
2478 if (r == -ENOMEM)
2479 return log_oom();
2480 if (r < 0) {
2481 log_syntax(unit, LOG_ERR, filename, line, r,
2482 "Trailing garbage in %s, ignoring: %s", lvalue, rvalue);
2483 break;
2484 }
2485
2486 if (u) {
2487 r = unit_full_printf(u, word, &k);
2488 if (r < 0) {
2489 log_syntax(unit, LOG_ERR, filename, line, r,
2490 "Failed to resolve unit specifiers in %s, ignoring: %m", word);
2491 continue;
2492 }
2493 } else
2494 k = TAKE_PTR(word);
2495
2496 if (!env_assignment_is_valid(k) && !env_name_is_valid(k)) {
2497 log_syntax(unit, LOG_ERR, filename, line, 0,
2498 "Invalid environment name or assignment %s, ignoring: %s", lvalue, k);
2499 continue;
2500 }
2501
2502 if (!GREEDY_REALLOC(n, nbufsize, nlen + 2))
2503 return log_oom();
2504
2505 n[nlen++] = TAKE_PTR(k);
2506 n[nlen] = NULL;
2507 }
2508
2509 if (n) {
2510 r = strv_extend_strv(unsetenv, n, true);
2511 if (r < 0)
2512 return r;
2513 }
2514
2515 return 0;
2516 }
2517
2518 int config_parse_log_extra_fields(
2519 const char *unit,
2520 const char *filename,
2521 unsigned line,
2522 const char *section,
2523 unsigned section_line,
2524 const char *lvalue,
2525 int ltype,
2526 const char *rvalue,
2527 void *data,
2528 void *userdata) {
2529
2530 ExecContext *c = data;
2531 const Unit *u = userdata;
2532 const char *p = rvalue;
2533 int r;
2534
2535 assert(filename);
2536 assert(lvalue);
2537 assert(rvalue);
2538 assert(c);
2539
2540 if (isempty(rvalue)) {
2541 exec_context_free_log_extra_fields(c);
2542 return 0;
2543 }
2544
2545 for (;;) {
2546 _cleanup_free_ char *word = NULL, *k = NULL;
2547 struct iovec *t;
2548 const char *eq;
2549
2550 r = extract_first_word(&p, &word, NULL, EXTRACT_CUNESCAPE|EXTRACT_UNQUOTE);
2551 if (r == 0)
2552 return 0;
2553 if (r == -ENOMEM)
2554 return log_oom();
2555 if (r < 0) {
2556 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
2557 return 0;
2558 }
2559
2560 r = unit_full_printf(u, word, &k);
2561 if (r < 0) {
2562 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", word);
2563 continue;
2564 }
2565
2566 eq = strchr(k, '=');
2567 if (!eq) {
2568 log_syntax(unit, LOG_ERR, filename, line, 0, "Log field lacks '=' character, ignoring: %s", k);
2569 continue;
2570 }
2571
2572 if (!journal_field_valid(k, eq-k, false)) {
2573 log_syntax(unit, LOG_ERR, filename, line, 0, "Log field name is invalid, ignoring: %s", k);
2574 continue;
2575 }
2576
2577 t = reallocarray(c->log_extra_fields, c->n_log_extra_fields+1, sizeof(struct iovec));
2578 if (!t)
2579 return log_oom();
2580
2581 c->log_extra_fields = t;
2582 c->log_extra_fields[c->n_log_extra_fields++] = IOVEC_MAKE_STRING(k);
2583
2584 k = NULL;
2585 }
2586 }
2587
2588 int config_parse_log_namespace(
2589 const char *unit,
2590 const char *filename,
2591 unsigned line,
2592 const char *section,
2593 unsigned section_line,
2594 const char *lvalue,
2595 int ltype,
2596 const char *rvalue,
2597 void *data,
2598 void *userdata) {
2599
2600 _cleanup_free_ char *k = NULL;
2601 ExecContext *c = data;
2602 const Unit *u = userdata;
2603 int r;
2604
2605 assert(filename);
2606 assert(lvalue);
2607 assert(rvalue);
2608 assert(c);
2609
2610 if (isempty(rvalue)) {
2611 c->log_namespace = mfree(c->log_namespace);
2612 return 0;
2613 }
2614
2615 r = unit_full_printf(u, rvalue, &k);
2616 if (r < 0) {
2617 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue);
2618 return 0;
2619 }
2620
2621 if (!log_namespace_name_valid(k)) {
2622 log_syntax(unit, LOG_ERR, filename, line, SYNTHETIC_ERRNO(EINVAL), "Specified log namespace name is not valid: %s", k);
2623 return 0;
2624 }
2625
2626 free_and_replace(c->log_namespace, k);
2627 return 0;
2628 }
2629
2630 int config_parse_unit_condition_path(
2631 const char *unit,
2632 const char *filename,
2633 unsigned line,
2634 const char *section,
2635 unsigned section_line,
2636 const char *lvalue,
2637 int ltype,
2638 const char *rvalue,
2639 void *data,
2640 void *userdata) {
2641
2642 _cleanup_free_ char *p = NULL;
2643 Condition **list = data, *c;
2644 ConditionType t = ltype;
2645 bool trigger, negate;
2646 const Unit *u = userdata;
2647 int r;
2648
2649 assert(filename);
2650 assert(lvalue);
2651 assert(rvalue);
2652 assert(data);
2653
2654 if (isempty(rvalue)) {
2655 /* Empty assignment resets the list */
2656 *list = condition_free_list(*list);
2657 return 0;
2658 }
2659
2660 trigger = rvalue[0] == '|';
2661 if (trigger)
2662 rvalue++;
2663
2664 negate = rvalue[0] == '!';
2665 if (negate)
2666 rvalue++;
2667
2668 r = unit_full_printf(u, rvalue, &p);
2669 if (r < 0) {
2670 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue);
2671 return 0;
2672 }
2673
2674 r = path_simplify_and_warn(p, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
2675 if (r < 0)
2676 return 0;
2677
2678 c = condition_new(t, p, trigger, negate);
2679 if (!c)
2680 return log_oom();
2681
2682 LIST_PREPEND(conditions, *list, c);
2683 return 0;
2684 }
2685
2686 int config_parse_unit_condition_string(
2687 const char *unit,
2688 const char *filename,
2689 unsigned line,
2690 const char *section,
2691 unsigned section_line,
2692 const char *lvalue,
2693 int ltype,
2694 const char *rvalue,
2695 void *data,
2696 void *userdata) {
2697
2698 _cleanup_free_ char *s = NULL;
2699 Condition **list = data, *c;
2700 ConditionType t = ltype;
2701 bool trigger, negate;
2702 const Unit *u = userdata;
2703 int r;
2704
2705 assert(filename);
2706 assert(lvalue);
2707 assert(rvalue);
2708 assert(data);
2709
2710 if (isempty(rvalue)) {
2711 /* Empty assignment resets the list */
2712 *list = condition_free_list(*list);
2713 return 0;
2714 }
2715
2716 trigger = *rvalue == '|';
2717 if (trigger)
2718 rvalue += 1 + strspn(rvalue + 1, WHITESPACE);
2719
2720 negate = *rvalue == '!';
2721 if (negate)
2722 rvalue += 1 + strspn(rvalue + 1, WHITESPACE);
2723
2724 r = unit_full_printf(u, rvalue, &s);
2725 if (r < 0) {
2726 log_syntax(unit, LOG_ERR, filename, line, r,
2727 "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
2728 return 0;
2729 }
2730
2731 c = condition_new(t, s, trigger, negate);
2732 if (!c)
2733 return log_oom();
2734
2735 LIST_PREPEND(conditions, *list, c);
2736 return 0;
2737 }
2738
2739 int config_parse_unit_condition_null(
2740 const char *unit,
2741 const char *filename,
2742 unsigned line,
2743 const char *section,
2744 unsigned section_line,
2745 const char *lvalue,
2746 int ltype,
2747 const char *rvalue,
2748 void *data,
2749 void *userdata) {
2750
2751 Condition **list = data, *c;
2752 bool trigger, negate;
2753 int b;
2754
2755 assert(filename);
2756 assert(lvalue);
2757 assert(rvalue);
2758 assert(data);
2759
2760 log_syntax(unit, LOG_WARNING, filename, line, 0, "%s= is deprecated, please do not use.", lvalue);
2761
2762 if (isempty(rvalue)) {
2763 /* Empty assignment resets the list */
2764 *list = condition_free_list(*list);
2765 return 0;
2766 }
2767
2768 trigger = rvalue[0] == '|';
2769 if (trigger)
2770 rvalue++;
2771
2772 negate = rvalue[0] == '!';
2773 if (negate)
2774 rvalue++;
2775
2776 b = parse_boolean(rvalue);
2777 if (b < 0) {
2778 log_syntax(unit, LOG_ERR, filename, line, b, "Failed to parse boolean value in condition, ignoring: %s", rvalue);
2779 return 0;
2780 }
2781
2782 if (!b)
2783 negate = !negate;
2784
2785 c = condition_new(CONDITION_NULL, NULL, trigger, negate);
2786 if (!c)
2787 return log_oom();
2788
2789 LIST_PREPEND(conditions, *list, c);
2790 return 0;
2791 }
2792
2793 int config_parse_unit_requires_mounts_for(
2794 const char *unit,
2795 const char *filename,
2796 unsigned line,
2797 const char *section,
2798 unsigned section_line,
2799 const char *lvalue,
2800 int ltype,
2801 const char *rvalue,
2802 void *data,
2803 void *userdata) {
2804
2805 const char *p = rvalue;
2806 Unit *u = userdata;
2807 int r;
2808
2809 assert(filename);
2810 assert(lvalue);
2811 assert(rvalue);
2812 assert(data);
2813
2814 for (;;) {
2815 _cleanup_free_ char *word = NULL, *resolved = NULL;
2816
2817 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
2818 if (r == 0)
2819 return 0;
2820 if (r == -ENOMEM)
2821 return log_oom();
2822 if (r < 0) {
2823 log_syntax(unit, LOG_WARNING, filename, line, r,
2824 "Invalid syntax, ignoring: %s", rvalue);
2825 return 0;
2826 }
2827
2828 r = unit_full_printf(u, word, &resolved);
2829 if (r < 0) {
2830 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", word);
2831 continue;
2832 }
2833
2834 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
2835 if (r < 0)
2836 continue;
2837
2838 r = unit_require_mounts_for(u, resolved, UNIT_DEPENDENCY_FILE);
2839 if (r < 0) {
2840 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to add required mount '%s', ignoring: %m", resolved);
2841 continue;
2842 }
2843 }
2844 }
2845
2846 int config_parse_documentation(const char *unit,
2847 const char *filename,
2848 unsigned line,
2849 const char *section,
2850 unsigned section_line,
2851 const char *lvalue,
2852 int ltype,
2853 const char *rvalue,
2854 void *data,
2855 void *userdata) {
2856
2857 Unit *u = userdata;
2858 int r;
2859 char **a, **b;
2860
2861 assert(filename);
2862 assert(lvalue);
2863 assert(rvalue);
2864 assert(u);
2865
2866 if (isempty(rvalue)) {
2867 /* Empty assignment resets the list */
2868 u->documentation = strv_free(u->documentation);
2869 return 0;
2870 }
2871
2872 r = config_parse_unit_strv_printf(unit, filename, line, section, section_line, lvalue, ltype,
2873 rvalue, data, userdata);
2874 if (r < 0)
2875 return r;
2876
2877 for (a = b = u->documentation; a && *a; a++) {
2878
2879 if (documentation_url_is_valid(*a))
2880 *(b++) = *a;
2881 else {
2882 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid URL, ignoring: %s", *a);
2883 free(*a);
2884 }
2885 }
2886 if (b)
2887 *b = NULL;
2888
2889 return r;
2890 }
2891
2892 #if HAVE_SECCOMP
2893 int config_parse_syscall_filter(
2894 const char *unit,
2895 const char *filename,
2896 unsigned line,
2897 const char *section,
2898 unsigned section_line,
2899 const char *lvalue,
2900 int ltype,
2901 const char *rvalue,
2902 void *data,
2903 void *userdata) {
2904
2905 ExecContext *c = data;
2906 _unused_ const Unit *u = userdata;
2907 bool invert = false;
2908 const char *p;
2909 int r;
2910
2911 assert(filename);
2912 assert(lvalue);
2913 assert(rvalue);
2914 assert(u);
2915
2916 if (isempty(rvalue)) {
2917 /* Empty assignment resets the list */
2918 c->syscall_filter = hashmap_free(c->syscall_filter);
2919 c->syscall_whitelist = false;
2920 return 0;
2921 }
2922
2923 if (rvalue[0] == '~') {
2924 invert = true;
2925 rvalue++;
2926 }
2927
2928 if (!c->syscall_filter) {
2929 c->syscall_filter = hashmap_new(NULL);
2930 if (!c->syscall_filter)
2931 return log_oom();
2932
2933 if (invert)
2934 /* Allow everything but the ones listed */
2935 c->syscall_whitelist = false;
2936 else {
2937 /* Allow nothing but the ones listed */
2938 c->syscall_whitelist = true;
2939
2940 /* Accept default syscalls if we are on a whitelist */
2941 r = seccomp_parse_syscall_filter(
2942 "@default", -1, c->syscall_filter,
2943 SECCOMP_PARSE_PERMISSIVE|SECCOMP_PARSE_WHITELIST,
2944 unit,
2945 NULL, 0);
2946 if (r < 0)
2947 return r;
2948 }
2949 }
2950
2951 p = rvalue;
2952 for (;;) {
2953 _cleanup_free_ char *word = NULL, *name = NULL;
2954 int num;
2955
2956 r = extract_first_word(&p, &word, NULL, 0);
2957 if (r == 0)
2958 return 0;
2959 if (r == -ENOMEM)
2960 return log_oom();
2961 if (r < 0) {
2962 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
2963 return 0;
2964 }
2965
2966 r = parse_syscall_and_errno(word, &name, &num);
2967 if (r < 0) {
2968 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse syscall:errno, ignoring: %s", word);
2969 continue;
2970 }
2971
2972 r = seccomp_parse_syscall_filter(
2973 name, num, c->syscall_filter,
2974 SECCOMP_PARSE_LOG|SECCOMP_PARSE_PERMISSIVE|
2975 (invert ? SECCOMP_PARSE_INVERT : 0)|
2976 (c->syscall_whitelist ? SECCOMP_PARSE_WHITELIST : 0),
2977 unit, filename, line);
2978 if (r < 0)
2979 return r;
2980 }
2981 }
2982
2983 int config_parse_syscall_archs(
2984 const char *unit,
2985 const char *filename,
2986 unsigned line,
2987 const char *section,
2988 unsigned section_line,
2989 const char *lvalue,
2990 int ltype,
2991 const char *rvalue,
2992 void *data,
2993 void *userdata) {
2994
2995 const char *p = rvalue;
2996 Set **archs = data;
2997 int r;
2998
2999 if (isempty(rvalue)) {
3000 *archs = set_free(*archs);
3001 return 0;
3002 }
3003
3004 r = set_ensure_allocated(archs, NULL);
3005 if (r < 0)
3006 return log_oom();
3007
3008 for (;;) {
3009 _cleanup_free_ char *word = NULL;
3010 uint32_t a;
3011
3012 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
3013 if (r == 0)
3014 return 0;
3015 if (r == -ENOMEM)
3016 return log_oom();
3017 if (r < 0) {
3018 log_syntax(unit, LOG_WARNING, filename, line, r,
3019 "Invalid syntax, ignoring: %s", rvalue);
3020 return 0;
3021 }
3022
3023 r = seccomp_arch_from_string(word, &a);
3024 if (r < 0) {
3025 log_syntax(unit, LOG_ERR, filename, line, r,
3026 "Failed to parse system call architecture \"%s\", ignoring: %m", word);
3027 continue;
3028 }
3029
3030 r = set_put(*archs, UINT32_TO_PTR(a + 1));
3031 if (r < 0)
3032 return log_oom();
3033 }
3034 }
3035
3036 int config_parse_syscall_errno(
3037 const char *unit,
3038 const char *filename,
3039 unsigned line,
3040 const char *section,
3041 unsigned section_line,
3042 const char *lvalue,
3043 int ltype,
3044 const char *rvalue,
3045 void *data,
3046 void *userdata) {
3047
3048 ExecContext *c = data;
3049 int e;
3050
3051 assert(filename);
3052 assert(lvalue);
3053 assert(rvalue);
3054
3055 if (isempty(rvalue)) {
3056 /* Empty assignment resets to KILL */
3057 c->syscall_errno = 0;
3058 return 0;
3059 }
3060
3061 e = parse_errno(rvalue);
3062 if (e <= 0) {
3063 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse error number, ignoring: %s", rvalue);
3064 return 0;
3065 }
3066
3067 c->syscall_errno = e;
3068 return 0;
3069 }
3070
3071 int config_parse_address_families(
3072 const char *unit,
3073 const char *filename,
3074 unsigned line,
3075 const char *section,
3076 unsigned section_line,
3077 const char *lvalue,
3078 int ltype,
3079 const char *rvalue,
3080 void *data,
3081 void *userdata) {
3082
3083 ExecContext *c = data;
3084 bool invert = false;
3085 const char *p;
3086 int r;
3087
3088 assert(filename);
3089 assert(lvalue);
3090 assert(rvalue);
3091
3092 if (isempty(rvalue)) {
3093 /* Empty assignment resets the list */
3094 c->address_families = set_free(c->address_families);
3095 c->address_families_whitelist = false;
3096 return 0;
3097 }
3098
3099 if (rvalue[0] == '~') {
3100 invert = true;
3101 rvalue++;
3102 }
3103
3104 if (!c->address_families) {
3105 c->address_families = set_new(NULL);
3106 if (!c->address_families)
3107 return log_oom();
3108
3109 c->address_families_whitelist = !invert;
3110 }
3111
3112 for (p = rvalue;;) {
3113 _cleanup_free_ char *word = NULL;
3114 int af;
3115
3116 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
3117 if (r == 0)
3118 return 0;
3119 if (r == -ENOMEM)
3120 return log_oom();
3121 if (r < 0) {
3122 log_syntax(unit, LOG_WARNING, filename, line, r,
3123 "Invalid syntax, ignoring: %s", rvalue);
3124 return 0;
3125 }
3126
3127 af = af_from_name(word);
3128 if (af < 0) {
3129 log_syntax(unit, LOG_ERR, filename, line, af,
3130 "Failed to parse address family, ignoring: %s", word);
3131 continue;
3132 }
3133
3134 /* If we previously wanted to forbid an address family and now
3135 * we want to allow it, then just remove it from the list.
3136 */
3137 if (!invert == c->address_families_whitelist) {
3138 r = set_put(c->address_families, INT_TO_PTR(af));
3139 if (r < 0)
3140 return log_oom();
3141 } else
3142 set_remove(c->address_families, INT_TO_PTR(af));
3143 }
3144 }
3145
3146 int config_parse_restrict_namespaces(
3147 const char *unit,
3148 const char *filename,
3149 unsigned line,
3150 const char *section,
3151 unsigned section_line,
3152 const char *lvalue,
3153 int ltype,
3154 const char *rvalue,
3155 void *data,
3156 void *userdata) {
3157
3158 ExecContext *c = data;
3159 unsigned long flags;
3160 bool invert = false;
3161 int r;
3162
3163 if (isempty(rvalue)) {
3164 /* Reset to the default. */
3165 c->restrict_namespaces = NAMESPACE_FLAGS_INITIAL;
3166 return 0;
3167 }
3168
3169 /* Boolean parameter ignores the previous settings */
3170 r = parse_boolean(rvalue);
3171 if (r > 0) {
3172 c->restrict_namespaces = 0;
3173 return 0;
3174 } else if (r == 0) {
3175 c->restrict_namespaces = NAMESPACE_FLAGS_ALL;
3176 return 0;
3177 }
3178
3179 if (rvalue[0] == '~') {
3180 invert = true;
3181 rvalue++;
3182 }
3183
3184 /* Not a boolean argument, in this case it's a list of namespace types. */
3185 r = namespace_flags_from_string(rvalue, &flags);
3186 if (r < 0) {
3187 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse namespace type string, ignoring: %s", rvalue);
3188 return 0;
3189 }
3190
3191 if (c->restrict_namespaces == NAMESPACE_FLAGS_INITIAL)
3192 /* Initial assignment. Just set the value. */
3193 c->restrict_namespaces = invert ? (~flags) & NAMESPACE_FLAGS_ALL : flags;
3194 else
3195 /* Merge the value with the previous one. */
3196 SET_FLAG(c->restrict_namespaces, flags, !invert);
3197
3198 return 0;
3199 }
3200 #endif
3201
3202 int config_parse_unit_slice(
3203 const char *unit,
3204 const char *filename,
3205 unsigned line,
3206 const char *section,
3207 unsigned section_line,
3208 const char *lvalue,
3209 int ltype,
3210 const char *rvalue,
3211 void *data,
3212 void *userdata) {
3213
3214 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
3215 _cleanup_free_ char *k = NULL;
3216 Unit *u = userdata, *slice;
3217 int r;
3218
3219 assert(filename);
3220 assert(lvalue);
3221 assert(rvalue);
3222 assert(u);
3223
3224 r = unit_name_printf(u, rvalue, &k);
3225 if (r < 0) {
3226 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue);
3227 return 0;
3228 }
3229
3230 r = manager_load_unit(u->manager, k, NULL, &error, &slice);
3231 if (r < 0) {
3232 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to load slice unit %s, ignoring: %s", k, bus_error_message(&error, r));
3233 return 0;
3234 }
3235
3236 r = unit_set_slice(u, slice);
3237 if (r < 0) {
3238 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to assign slice %s to unit %s, ignoring: %m", slice->id, u->id);
3239 return 0;
3240 }
3241
3242 return 0;
3243 }
3244
3245 int config_parse_cpu_quota(
3246 const char *unit,
3247 const char *filename,
3248 unsigned line,
3249 const char *section,
3250 unsigned section_line,
3251 const char *lvalue,
3252 int ltype,
3253 const char *rvalue,
3254 void *data,
3255 void *userdata) {
3256
3257 CGroupContext *c = data;
3258 int r;
3259
3260 assert(filename);
3261 assert(lvalue);
3262 assert(rvalue);
3263
3264 if (isempty(rvalue)) {
3265 c->cpu_quota_per_sec_usec = USEC_INFINITY;
3266 return 0;
3267 }
3268
3269 r = parse_permille_unbounded(rvalue);
3270 if (r <= 0) {
3271 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid CPU quota '%s', ignoring.", rvalue);
3272 return 0;
3273 }
3274
3275 c->cpu_quota_per_sec_usec = ((usec_t) r * USEC_PER_SEC) / 1000U;
3276 return 0;
3277 }
3278
3279 int config_parse_allowed_cpus(
3280 const char *unit,
3281 const char *filename,
3282 unsigned line,
3283 const char *section,
3284 unsigned section_line,
3285 const char *lvalue,
3286 int ltype,
3287 const char *rvalue,
3288 void *data,
3289 void *userdata) {
3290
3291 CGroupContext *c = data;
3292
3293 (void) parse_cpu_set_extend(rvalue, &c->cpuset_cpus, true, unit, filename, line, lvalue);
3294
3295 return 0;
3296 }
3297
3298 int config_parse_allowed_mems(
3299 const char *unit,
3300 const char *filename,
3301 unsigned line,
3302 const char *section,
3303 unsigned section_line,
3304 const char *lvalue,
3305 int ltype,
3306 const char *rvalue,
3307 void *data,
3308 void *userdata) {
3309
3310 CGroupContext *c = data;
3311
3312 (void) parse_cpu_set_extend(rvalue, &c->cpuset_mems, true, unit, filename, line, lvalue);
3313
3314 return 0;
3315 }
3316
3317 int config_parse_memory_limit(
3318 const char *unit,
3319 const char *filename,
3320 unsigned line,
3321 const char *section,
3322 unsigned section_line,
3323 const char *lvalue,
3324 int ltype,
3325 const char *rvalue,
3326 void *data,
3327 void *userdata) {
3328
3329 CGroupContext *c = data;
3330 uint64_t bytes = CGROUP_LIMIT_MAX;
3331 int r;
3332
3333 if (!isempty(rvalue) && !streq(rvalue, "infinity")) {
3334
3335 r = parse_permille(rvalue);
3336 if (r < 0) {
3337 r = parse_size(rvalue, 1024, &bytes);
3338 if (r < 0) {
3339 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid memory limit '%s', ignoring: %m", rvalue);
3340 return 0;
3341 }
3342 } else
3343 bytes = physical_memory_scale(r, 1000U);
3344
3345 if (bytes >= UINT64_MAX ||
3346 (bytes <= 0 && !STR_IN_SET(lvalue, "MemorySwapMax", "MemoryLow", "MemoryMin", "DefaultMemoryLow", "DefaultMemoryMin"))) {
3347 log_syntax(unit, LOG_ERR, filename, line, 0, "Memory limit '%s' out of range, ignoring.", rvalue);
3348 return 0;
3349 }
3350 }
3351
3352 if (streq(lvalue, "DefaultMemoryLow")) {
3353 c->default_memory_low_set = true;
3354 if (isempty(rvalue))
3355 c->default_memory_low = CGROUP_LIMIT_MIN;
3356 else
3357 c->default_memory_low = bytes;
3358 } else if (streq(lvalue, "DefaultMemoryMin")) {
3359 c->default_memory_min_set = true;
3360 if (isempty(rvalue))
3361 c->default_memory_min = CGROUP_LIMIT_MIN;
3362 else
3363 c->default_memory_min = bytes;
3364 } else if (streq(lvalue, "MemoryMin")) {
3365 c->memory_min = bytes;
3366 c->memory_min_set = true;
3367 } else if (streq(lvalue, "MemoryLow")) {
3368 c->memory_low = bytes;
3369 c->memory_low_set = true;
3370 } else if (streq(lvalue, "MemoryHigh"))
3371 c->memory_high = bytes;
3372 else if (streq(lvalue, "MemoryMax"))
3373 c->memory_max = bytes;
3374 else if (streq(lvalue, "MemorySwapMax"))
3375 c->memory_swap_max = bytes;
3376 else if (streq(lvalue, "MemoryLimit"))
3377 c->memory_limit = bytes;
3378 else
3379 return -EINVAL;
3380
3381 return 0;
3382 }
3383
3384 int config_parse_tasks_max(
3385 const char *unit,
3386 const char *filename,
3387 unsigned line,
3388 const char *section,
3389 unsigned section_line,
3390 const char *lvalue,
3391 int ltype,
3392 const char *rvalue,
3393 void *data,
3394 void *userdata) {
3395
3396 const Unit *u = userdata;
3397 TasksMax *tasks_max = data;
3398 uint64_t v;
3399 int r;
3400
3401 if (isempty(rvalue)) {
3402 *tasks_max = u ? u->manager->default_tasks_max : TASKS_MAX_UNSET;
3403 return 0;
3404 }
3405
3406 if (streq(rvalue, "infinity")) {
3407 *tasks_max = TASKS_MAX_UNSET;
3408 return 0;
3409 }
3410
3411 r = parse_permille(rvalue);
3412 if (r >= 0)
3413 *tasks_max = (TasksMax) { r, 1000U }; /* r‰ */
3414 else {
3415 r = safe_atou64(rvalue, &v);
3416 if (r < 0) {
3417 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid maximum tasks value '%s', ignoring: %m", rvalue);
3418 return 0;
3419 }
3420
3421 if (v <= 0 || v >= UINT64_MAX) {
3422 log_syntax(unit, LOG_ERR, filename, line, 0, "Maximum tasks value '%s' out of range, ignoring.", rvalue);
3423 return 0;
3424 }
3425
3426 *tasks_max = (TasksMax) { v };
3427 }
3428
3429 return 0;
3430 }
3431
3432 int config_parse_delegate(
3433 const char *unit,
3434 const char *filename,
3435 unsigned line,
3436 const char *section,
3437 unsigned section_line,
3438 const char *lvalue,
3439 int ltype,
3440 const char *rvalue,
3441 void *data,
3442 void *userdata) {
3443
3444 CGroupContext *c = data;
3445 UnitType t;
3446 int r;
3447
3448 t = unit_name_to_type(unit);
3449 assert(t != _UNIT_TYPE_INVALID);
3450
3451 if (!unit_vtable[t]->can_delegate) {
3452 log_syntax(unit, LOG_ERR, filename, line, 0, "Delegate= setting not supported for this unit type, ignoring.");
3453 return 0;
3454 }
3455
3456 /* We either accept a boolean value, which may be used to turn on delegation for all controllers, or turn it
3457 * off for all. Or it takes a list of controller names, in which case we add the specified controllers to the
3458 * mask to delegate. */
3459
3460 if (isempty(rvalue)) {
3461 /* An empty string resets controllers and set Delegate=yes. */
3462 c->delegate = true;
3463 c->delegate_controllers = 0;
3464 return 0;
3465 }
3466
3467 r = parse_boolean(rvalue);
3468 if (r < 0) {
3469 const char *p = rvalue;
3470 CGroupMask mask = 0;
3471
3472 for (;;) {
3473 _cleanup_free_ char *word = NULL;
3474 CGroupController cc;
3475
3476 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
3477 if (r == 0)
3478 break;
3479 if (r == -ENOMEM)
3480 return log_oom();
3481 if (r < 0) {
3482 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
3483 return 0;
3484 }
3485
3486 cc = cgroup_controller_from_string(word);
3487 if (cc < 0) {
3488 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid controller name '%s', ignoring", word);
3489 continue;
3490 }
3491
3492 mask |= CGROUP_CONTROLLER_TO_MASK(cc);
3493 }
3494
3495 c->delegate = true;
3496 c->delegate_controllers |= mask;
3497
3498 } else if (r > 0) {
3499 c->delegate = true;
3500 c->delegate_controllers = _CGROUP_MASK_ALL;
3501 } else {
3502 c->delegate = false;
3503 c->delegate_controllers = 0;
3504 }
3505
3506 return 0;
3507 }
3508
3509 int config_parse_device_allow(
3510 const char *unit,
3511 const char *filename,
3512 unsigned line,
3513 const char *section,
3514 unsigned section_line,
3515 const char *lvalue,
3516 int ltype,
3517 const char *rvalue,
3518 void *data,
3519 void *userdata) {
3520
3521 _cleanup_free_ char *path = NULL, *resolved = NULL;
3522 CGroupContext *c = data;
3523 const char *p = rvalue;
3524 int r;
3525
3526 if (isempty(rvalue)) {
3527 while (c->device_allow)
3528 cgroup_context_free_device_allow(c, c->device_allow);
3529
3530 return 0;
3531 }
3532
3533 r = extract_first_word(&p, &path, NULL, EXTRACT_UNQUOTE);
3534 if (r == -ENOMEM)
3535 return log_oom();
3536 if (r < 0) {
3537 log_syntax(unit, LOG_WARNING, filename, line, r,
3538 "Invalid syntax, ignoring: %s", rvalue);
3539 return 0;
3540 }
3541 if (r == 0) {
3542 log_syntax(unit, LOG_WARNING, filename, line, 0,
3543 "Failed to extract device path and rights from '%s', ignoring.", rvalue);
3544 return 0;
3545 }
3546
3547 r = unit_full_printf(userdata, path, &resolved);
3548 if (r < 0) {
3549 log_syntax(unit, LOG_WARNING, filename, line, r,
3550 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
3551 return 0;
3552 }
3553
3554 if (!STARTSWITH_SET(resolved, "block-", "char-")) {
3555
3556 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
3557 if (r < 0)
3558 return 0;
3559
3560 if (!valid_device_node_path(resolved)) {
3561 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid device node path '%s', ignoring.", resolved);
3562 return 0;
3563 }
3564 }
3565
3566 if (!isempty(p) && !in_charset(p, "rwm")) {
3567 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid device rights '%s', ignoring.", p);
3568 return 0;
3569 }
3570
3571 return cgroup_add_device_allow(c, resolved, p);
3572 }
3573
3574 int config_parse_io_device_weight(
3575 const char *unit,
3576 const char *filename,
3577 unsigned line,
3578 const char *section,
3579 unsigned section_line,
3580 const char *lvalue,
3581 int ltype,
3582 const char *rvalue,
3583 void *data,
3584 void *userdata) {
3585
3586 _cleanup_free_ char *path = NULL, *resolved = NULL;
3587 CGroupIODeviceWeight *w;
3588 CGroupContext *c = data;
3589 const char *p = rvalue;
3590 uint64_t u;
3591 int r;
3592
3593 assert(filename);
3594 assert(lvalue);
3595 assert(rvalue);
3596
3597 if (isempty(rvalue)) {
3598 while (c->io_device_weights)
3599 cgroup_context_free_io_device_weight(c, c->io_device_weights);
3600
3601 return 0;
3602 }
3603
3604 r = extract_first_word(&p, &path, NULL, EXTRACT_UNQUOTE);
3605 if (r == -ENOMEM)
3606 return log_oom();
3607 if (r < 0) {
3608 log_syntax(unit, LOG_WARNING, filename, line, r,
3609 "Invalid syntax, ignoring: %s", rvalue);
3610 return 0;
3611 }
3612 if (r == 0 || isempty(p)) {
3613 log_syntax(unit, LOG_WARNING, filename, line, 0,
3614 "Failed to extract device path and weight from '%s', ignoring.", rvalue);
3615 return 0;
3616 }
3617
3618 r = unit_full_printf(userdata, path, &resolved);
3619 if (r < 0) {
3620 log_syntax(unit, LOG_WARNING, filename, line, r,
3621 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
3622 return 0;
3623 }
3624
3625 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
3626 if (r < 0)
3627 return 0;
3628
3629 r = cg_weight_parse(p, &u);
3630 if (r < 0) {
3631 log_syntax(unit, LOG_ERR, filename, line, r, "IO weight '%s' invalid, ignoring: %m", p);
3632 return 0;
3633 }
3634
3635 assert(u != CGROUP_WEIGHT_INVALID);
3636
3637 w = new0(CGroupIODeviceWeight, 1);
3638 if (!w)
3639 return log_oom();
3640
3641 w->path = TAKE_PTR(resolved);
3642 w->weight = u;
3643
3644 LIST_PREPEND(device_weights, c->io_device_weights, w);
3645 return 0;
3646 }
3647
3648 int config_parse_io_device_latency(
3649 const char *unit,
3650 const char *filename,
3651 unsigned line,
3652 const char *section,
3653 unsigned section_line,
3654 const char *lvalue,
3655 int ltype,
3656 const char *rvalue,
3657 void *data,
3658 void *userdata) {
3659
3660 _cleanup_free_ char *path = NULL, *resolved = NULL;
3661 CGroupIODeviceLatency *l;
3662 CGroupContext *c = data;
3663 const char *p = rvalue;
3664 usec_t usec;
3665 int r;
3666
3667 assert(filename);
3668 assert(lvalue);
3669 assert(rvalue);
3670
3671 if (isempty(rvalue)) {
3672 while (c->io_device_latencies)
3673 cgroup_context_free_io_device_latency(c, c->io_device_latencies);
3674
3675 return 0;
3676 }
3677
3678 r = extract_first_word(&p, &path, NULL, EXTRACT_UNQUOTE);
3679 if (r == -ENOMEM)
3680 return log_oom();
3681 if (r < 0) {
3682 log_syntax(unit, LOG_WARNING, filename, line, r,
3683 "Invalid syntax, ignoring: %s", rvalue);
3684 return 0;
3685 }
3686 if (r == 0 || isempty(p)) {
3687 log_syntax(unit, LOG_WARNING, filename, line, 0,
3688 "Failed to extract device path and latency from '%s', ignoring.", rvalue);
3689 return 0;
3690 }
3691
3692 r = unit_full_printf(userdata, path, &resolved);
3693 if (r < 0) {
3694 log_syntax(unit, LOG_WARNING, filename, line, r,
3695 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
3696 return 0;
3697 }
3698
3699 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
3700 if (r < 0)
3701 return 0;
3702
3703 if (parse_sec(p, &usec) < 0) {
3704 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse timer value, ignoring: %s", p);
3705 return 0;
3706 }
3707
3708 l = new0(CGroupIODeviceLatency, 1);
3709 if (!l)
3710 return log_oom();
3711
3712 l->path = TAKE_PTR(resolved);
3713 l->target_usec = usec;
3714
3715 LIST_PREPEND(device_latencies, c->io_device_latencies, l);
3716 return 0;
3717 }
3718
3719 int config_parse_io_limit(
3720 const char *unit,
3721 const char *filename,
3722 unsigned line,
3723 const char *section,
3724 unsigned section_line,
3725 const char *lvalue,
3726 int ltype,
3727 const char *rvalue,
3728 void *data,
3729 void *userdata) {
3730
3731 _cleanup_free_ char *path = NULL, *resolved = NULL;
3732 CGroupIODeviceLimit *l = NULL, *t;
3733 CGroupContext *c = data;
3734 CGroupIOLimitType type;
3735 const char *p = rvalue;
3736 uint64_t num;
3737 int r;
3738
3739 assert(filename);
3740 assert(lvalue);
3741 assert(rvalue);
3742
3743 type = cgroup_io_limit_type_from_string(lvalue);
3744 assert(type >= 0);
3745
3746 if (isempty(rvalue)) {
3747 LIST_FOREACH(device_limits, l, c->io_device_limits)
3748 l->limits[type] = cgroup_io_limit_defaults[type];
3749 return 0;
3750 }
3751
3752 r = extract_first_word(&p, &path, NULL, EXTRACT_UNQUOTE);
3753 if (r == -ENOMEM)
3754 return log_oom();
3755 if (r < 0) {
3756 log_syntax(unit, LOG_WARNING, filename, line, r,
3757 "Invalid syntax, ignoring: %s", rvalue);
3758 return 0;
3759 }
3760 if (r == 0 || isempty(p)) {
3761 log_syntax(unit, LOG_WARNING, filename, line, 0,
3762 "Failed to extract device node and bandwidth from '%s', ignoring.", rvalue);
3763 return 0;
3764 }
3765
3766 r = unit_full_printf(userdata, path, &resolved);
3767 if (r < 0) {
3768 log_syntax(unit, LOG_WARNING, filename, line, r,
3769 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
3770 return 0;
3771 }
3772
3773 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
3774 if (r < 0)
3775 return 0;
3776
3777 if (streq("infinity", p))
3778 num = CGROUP_LIMIT_MAX;
3779 else {
3780 r = parse_size(p, 1000, &num);
3781 if (r < 0 || num <= 0) {
3782 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid IO limit '%s', ignoring.", p);
3783 return 0;
3784 }
3785 }
3786
3787 LIST_FOREACH(device_limits, t, c->io_device_limits) {
3788 if (path_equal(resolved, t->path)) {
3789 l = t;
3790 break;
3791 }
3792 }
3793
3794 if (!l) {
3795 CGroupIOLimitType ttype;
3796
3797 l = new0(CGroupIODeviceLimit, 1);
3798 if (!l)
3799 return log_oom();
3800
3801 l->path = TAKE_PTR(resolved);
3802 for (ttype = 0; ttype < _CGROUP_IO_LIMIT_TYPE_MAX; ttype++)
3803 l->limits[ttype] = cgroup_io_limit_defaults[ttype];
3804
3805 LIST_PREPEND(device_limits, c->io_device_limits, l);
3806 }
3807
3808 l->limits[type] = num;
3809
3810 return 0;
3811 }
3812
3813 int config_parse_blockio_device_weight(
3814 const char *unit,
3815 const char *filename,
3816 unsigned line,
3817 const char *section,
3818 unsigned section_line,
3819 const char *lvalue,
3820 int ltype,
3821 const char *rvalue,
3822 void *data,
3823 void *userdata) {
3824
3825 _cleanup_free_ char *path = NULL, *resolved = NULL;
3826 CGroupBlockIODeviceWeight *w;
3827 CGroupContext *c = data;
3828 const char *p = rvalue;
3829 uint64_t u;
3830 int r;
3831
3832 assert(filename);
3833 assert(lvalue);
3834 assert(rvalue);
3835
3836 if (isempty(rvalue)) {
3837 while (c->blockio_device_weights)
3838 cgroup_context_free_blockio_device_weight(c, c->blockio_device_weights);
3839
3840 return 0;
3841 }
3842
3843 r = extract_first_word(&p, &path, NULL, EXTRACT_UNQUOTE);
3844 if (r == -ENOMEM)
3845 return log_oom();
3846 if (r < 0) {
3847 log_syntax(unit, LOG_WARNING, filename, line, r,
3848 "Invalid syntax, ignoring: %s", rvalue);
3849 return 0;
3850 }
3851 if (r == 0 || isempty(p)) {
3852 log_syntax(unit, LOG_WARNING, filename, line, 0,
3853 "Failed to extract device node and weight from '%s', ignoring.", rvalue);
3854 return 0;
3855 }
3856
3857 r = unit_full_printf(userdata, path, &resolved);
3858 if (r < 0) {
3859 log_syntax(unit, LOG_WARNING, filename, line, r,
3860 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
3861 return 0;
3862 }
3863
3864 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
3865 if (r < 0)
3866 return 0;
3867
3868 r = cg_blkio_weight_parse(p, &u);
3869 if (r < 0) {
3870 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid block IO weight '%s', ignoring: %m", p);
3871 return 0;
3872 }
3873
3874 assert(u != CGROUP_BLKIO_WEIGHT_INVALID);
3875
3876 w = new0(CGroupBlockIODeviceWeight, 1);
3877 if (!w)
3878 return log_oom();
3879
3880 w->path = TAKE_PTR(resolved);
3881 w->weight = u;
3882
3883 LIST_PREPEND(device_weights, c->blockio_device_weights, w);
3884 return 0;
3885 }
3886
3887 int config_parse_blockio_bandwidth(
3888 const char *unit,
3889 const char *filename,
3890 unsigned line,
3891 const char *section,
3892 unsigned section_line,
3893 const char *lvalue,
3894 int ltype,
3895 const char *rvalue,
3896 void *data,
3897 void *userdata) {
3898
3899 _cleanup_free_ char *path = NULL, *resolved = NULL;
3900 CGroupBlockIODeviceBandwidth *b = NULL, *t;
3901 CGroupContext *c = data;
3902 const char *p = rvalue;
3903 uint64_t bytes;
3904 bool read;
3905 int r;
3906
3907 assert(filename);
3908 assert(lvalue);
3909 assert(rvalue);
3910
3911 read = streq("BlockIOReadBandwidth", lvalue);
3912
3913 if (isempty(rvalue)) {
3914 LIST_FOREACH(device_bandwidths, b, c->blockio_device_bandwidths) {
3915 b->rbps = CGROUP_LIMIT_MAX;
3916 b->wbps = CGROUP_LIMIT_MAX;
3917 }
3918 return 0;
3919 }
3920
3921 r = extract_first_word(&p, &path, NULL, EXTRACT_UNQUOTE);
3922 if (r == -ENOMEM)
3923 return log_oom();
3924 if (r < 0) {
3925 log_syntax(unit, LOG_WARNING, filename, line, r,
3926 "Invalid syntax, ignoring: %s", rvalue);
3927 return 0;
3928 }
3929 if (r == 0 || isempty(p)) {
3930 log_syntax(unit, LOG_WARNING, filename, line, 0,
3931 "Failed to extract device node and bandwidth from '%s', ignoring.", rvalue);
3932 return 0;
3933 }
3934
3935 r = unit_full_printf(userdata, path, &resolved);
3936 if (r < 0) {
3937 log_syntax(unit, LOG_WARNING, filename, line, r,
3938 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
3939 return 0;
3940 }
3941
3942 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
3943 if (r < 0)
3944 return 0;
3945
3946 r = parse_size(p, 1000, &bytes);
3947 if (r < 0 || bytes <= 0) {
3948 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid Block IO Bandwidth '%s', ignoring.", p);
3949 return 0;
3950 }
3951
3952 LIST_FOREACH(device_bandwidths, t, c->blockio_device_bandwidths) {
3953 if (path_equal(resolved, t->path)) {
3954 b = t;
3955 break;
3956 }
3957 }
3958
3959 if (!t) {
3960 b = new0(CGroupBlockIODeviceBandwidth, 1);
3961 if (!b)
3962 return log_oom();
3963
3964 b->path = TAKE_PTR(resolved);
3965 b->rbps = CGROUP_LIMIT_MAX;
3966 b->wbps = CGROUP_LIMIT_MAX;
3967
3968 LIST_PREPEND(device_bandwidths, c->blockio_device_bandwidths, b);
3969 }
3970
3971 if (read)
3972 b->rbps = bytes;
3973 else
3974 b->wbps = bytes;
3975
3976 return 0;
3977 }
3978
3979 int config_parse_job_mode_isolate(
3980 const char *unit,
3981 const char *filename,
3982 unsigned line,
3983 const char *section,
3984 unsigned section_line,
3985 const char *lvalue,
3986 int ltype,
3987 const char *rvalue,
3988 void *data,
3989 void *userdata) {
3990
3991 JobMode *m = data;
3992 int r;
3993
3994 assert(filename);
3995 assert(lvalue);
3996 assert(rvalue);
3997
3998 r = parse_boolean(rvalue);
3999 if (r < 0) {
4000 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse boolean, ignoring: %s", rvalue);
4001 return 0;
4002 }
4003
4004 log_notice("%s is deprecated. Please use OnFailureJobMode= instead", lvalue);
4005
4006 *m = r ? JOB_ISOLATE : JOB_REPLACE;
4007 return 0;
4008 }
4009
4010 int config_parse_exec_directories(
4011 const char *unit,
4012 const char *filename,
4013 unsigned line,
4014 const char *section,
4015 unsigned section_line,
4016 const char *lvalue,
4017 int ltype,
4018 const char *rvalue,
4019 void *data,
4020 void *userdata) {
4021
4022 char***rt = data;
4023 const Unit *u = userdata;
4024 const char *p;
4025 int r;
4026
4027 assert(filename);
4028 assert(lvalue);
4029 assert(rvalue);
4030 assert(data);
4031
4032 if (isempty(rvalue)) {
4033 /* Empty assignment resets the list */
4034 *rt = strv_free(*rt);
4035 return 0;
4036 }
4037
4038 for (p = rvalue;;) {
4039 _cleanup_free_ char *word = NULL, *k = NULL;
4040
4041 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
4042 if (r == -ENOMEM)
4043 return log_oom();
4044 if (r < 0) {
4045 log_syntax(unit, LOG_WARNING, filename, line, r,
4046 "Invalid syntax, ignoring: %s", rvalue);
4047 return 0;
4048 }
4049 if (r == 0)
4050 return 0;
4051
4052 r = unit_full_printf(u, word, &k);
4053 if (r < 0) {
4054 log_syntax(unit, LOG_ERR, filename, line, r,
4055 "Failed to resolve unit specifiers in \"%s\", ignoring: %m", word);
4056 continue;
4057 }
4058
4059 r = path_simplify_and_warn(k, PATH_CHECK_RELATIVE, unit, filename, line, lvalue);
4060 if (r < 0)
4061 continue;
4062
4063 if (path_startswith(k, "private")) {
4064 log_syntax(unit, LOG_ERR, filename, line, 0,
4065 "%s= path can't be 'private', ignoring assignment: %s", lvalue, word);
4066 continue;
4067 }
4068
4069 r = strv_push(rt, k);
4070 if (r < 0)
4071 return log_oom();
4072 k = NULL;
4073 }
4074 }
4075
4076 int config_parse_set_status(
4077 const char *unit,
4078 const char *filename,
4079 unsigned line,
4080 const char *section,
4081 unsigned section_line,
4082 const char *lvalue,
4083 int ltype,
4084 const char *rvalue,
4085 void *data,
4086 void *userdata) {
4087
4088 size_t l;
4089 const char *word, *state;
4090 int r;
4091 ExitStatusSet *status_set = data;
4092
4093 assert(filename);
4094 assert(lvalue);
4095 assert(rvalue);
4096 assert(data);
4097
4098 /* Empty assignment resets the list */
4099 if (isempty(rvalue)) {
4100 exit_status_set_free(status_set);
4101 return 0;
4102 }
4103
4104 FOREACH_WORD(word, l, rvalue, state) {
4105 _cleanup_free_ char *temp;
4106 Bitmap *bitmap;
4107
4108 temp = strndup(word, l);
4109 if (!temp)
4110 return log_oom();
4111
4112 /* We need to call exit_status_from_string() first, because we want
4113 * to parse numbers as exit statuses, not signals. */
4114
4115 r = exit_status_from_string(temp);
4116 if (r >= 0) {
4117 assert(r >= 0 && r < 256);
4118 bitmap = &status_set->status;
4119 } else {
4120 r = signal_from_string(temp);
4121
4122 if (r <= 0) {
4123 log_syntax(unit, LOG_ERR, filename, line, 0,
4124 "Failed to parse value, ignoring: %s", word);
4125 continue;
4126 }
4127 bitmap = &status_set->signal;
4128 }
4129
4130 r = bitmap_set(bitmap, r);
4131 if (r < 0)
4132 return log_error_errno(r, "Failed to set signal or status %s: %m", word);
4133 }
4134 if (!isempty(state))
4135 log_syntax(unit, LOG_ERR, filename, line, 0, "Trailing garbage, ignoring.");
4136
4137 return 0;
4138 }
4139
4140 int config_parse_namespace_path_strv(
4141 const char *unit,
4142 const char *filename,
4143 unsigned line,
4144 const char *section,
4145 unsigned section_line,
4146 const char *lvalue,
4147 int ltype,
4148 const char *rvalue,
4149 void *data,
4150 void *userdata) {
4151
4152 const Unit *u = userdata;
4153 char*** sv = data;
4154 const char *p = rvalue;
4155 int r;
4156
4157 assert(filename);
4158 assert(lvalue);
4159 assert(rvalue);
4160 assert(data);
4161
4162 if (isempty(rvalue)) {
4163 /* Empty assignment resets the list */
4164 *sv = strv_free(*sv);
4165 return 0;
4166 }
4167
4168 for (;;) {
4169 _cleanup_free_ char *word = NULL, *resolved = NULL, *joined = NULL;
4170 const char *w;
4171 bool ignore_enoent = false, shall_prefix = false;
4172
4173 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
4174 if (r == 0)
4175 break;
4176 if (r == -ENOMEM)
4177 return log_oom();
4178 if (r < 0) {
4179 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to extract first word, ignoring: %s", rvalue);
4180 return 0;
4181 }
4182
4183 w = word;
4184 if (startswith(w, "-")) {
4185 ignore_enoent = true;
4186 w++;
4187 }
4188 if (startswith(w, "+")) {
4189 shall_prefix = true;
4190 w++;
4191 }
4192
4193 r = unit_full_printf(u, w, &resolved);
4194 if (r < 0) {
4195 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s: %m", w);
4196 continue;
4197 }
4198
4199 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
4200 if (r < 0)
4201 continue;
4202
4203 joined = strjoin(ignore_enoent ? "-" : "",
4204 shall_prefix ? "+" : "",
4205 resolved);
4206
4207 r = strv_push(sv, joined);
4208 if (r < 0)
4209 return log_oom();
4210
4211 joined = NULL;
4212 }
4213
4214 return 0;
4215 }
4216
4217 int config_parse_temporary_filesystems(
4218 const char *unit,
4219 const char *filename,
4220 unsigned line,
4221 const char *section,
4222 unsigned section_line,
4223 const char *lvalue,
4224 int ltype,
4225 const char *rvalue,
4226 void *data,
4227 void *userdata) {
4228
4229 const Unit *u = userdata;
4230 ExecContext *c = data;
4231 const char *p = rvalue;
4232 int r;
4233
4234 assert(filename);
4235 assert(lvalue);
4236 assert(rvalue);
4237 assert(data);
4238
4239 if (isempty(rvalue)) {
4240 /* Empty assignment resets the list */
4241 temporary_filesystem_free_many(c->temporary_filesystems, c->n_temporary_filesystems);
4242 c->temporary_filesystems = NULL;
4243 c->n_temporary_filesystems = 0;
4244 return 0;
4245 }
4246
4247 for (;;) {
4248 _cleanup_free_ char *word = NULL, *path = NULL, *resolved = NULL;
4249 const char *w;
4250
4251 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
4252 if (r == 0)
4253 return 0;
4254 if (r == -ENOMEM)
4255 return log_oom();
4256 if (r < 0) {
4257 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to extract first word, ignoring: %s", rvalue);
4258 return 0;
4259 }
4260
4261 w = word;
4262 r = extract_first_word(&w, &path, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
4263 if (r == -ENOMEM)
4264 return log_oom();
4265 if (r < 0) {
4266 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to extract first word, ignoring: %s", word);
4267 continue;
4268 }
4269 if (r == 0) {
4270 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid syntax, ignoring: %s", word);
4271 continue;
4272 }
4273
4274 r = unit_full_printf(u, path, &resolved);
4275 if (r < 0) {
4276 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", path);
4277 continue;
4278 }
4279
4280 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
4281 if (r < 0)
4282 continue;
4283
4284 r = temporary_filesystem_add(&c->temporary_filesystems, &c->n_temporary_filesystems, resolved, w);
4285 if (r < 0)
4286 return log_oom();
4287 }
4288 }
4289
4290 int config_parse_bind_paths(
4291 const char *unit,
4292 const char *filename,
4293 unsigned line,
4294 const char *section,
4295 unsigned section_line,
4296 const char *lvalue,
4297 int ltype,
4298 const char *rvalue,
4299 void *data,
4300 void *userdata) {
4301
4302 ExecContext *c = data;
4303 const Unit *u = userdata;
4304 const char *p;
4305 int r;
4306
4307 assert(filename);
4308 assert(lvalue);
4309 assert(rvalue);
4310 assert(data);
4311
4312 if (isempty(rvalue)) {
4313 /* Empty assignment resets the list */
4314 bind_mount_free_many(c->bind_mounts, c->n_bind_mounts);
4315 c->bind_mounts = NULL;
4316 c->n_bind_mounts = 0;
4317 return 0;
4318 }
4319
4320 p = rvalue;
4321 for (;;) {
4322 _cleanup_free_ char *source = NULL, *destination = NULL;
4323 _cleanup_free_ char *sresolved = NULL, *dresolved = NULL;
4324 char *s = NULL, *d = NULL;
4325 bool rbind = true, ignore_enoent = false;
4326
4327 r = extract_first_word(&p, &source, ":" WHITESPACE, EXTRACT_UNQUOTE|EXTRACT_DONT_COALESCE_SEPARATORS);
4328 if (r == 0)
4329 break;
4330 if (r == -ENOMEM)
4331 return log_oom();
4332 if (r < 0) {
4333 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse %s, ignoring: %s", lvalue, rvalue);
4334 return 0;
4335 }
4336
4337 r = unit_full_printf(u, source, &sresolved);
4338 if (r < 0) {
4339 log_syntax(unit, LOG_ERR, filename, line, r,
4340 "Failed to resolved unit specifiers in \"%s\", ignoring: %m", source);
4341 continue;
4342 }
4343
4344 s = sresolved;
4345 if (s[0] == '-') {
4346 ignore_enoent = true;
4347 s++;
4348 }
4349
4350 r = path_simplify_and_warn(s, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
4351 if (r < 0)
4352 continue;
4353
4354 /* Optionally, the destination is specified. */
4355 if (p && p[-1] == ':') {
4356 r = extract_first_word(&p, &destination, ":" WHITESPACE, EXTRACT_UNQUOTE|EXTRACT_DONT_COALESCE_SEPARATORS);
4357 if (r == -ENOMEM)
4358 return log_oom();
4359 if (r < 0) {
4360 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse %s, ignoring: %s", lvalue, rvalue);
4361 return 0;
4362 }
4363 if (r == 0) {
4364 log_syntax(unit, LOG_ERR, filename, line, 0, "Missing argument after ':', ignoring: %s", s);
4365 continue;
4366 }
4367
4368 r = unit_full_printf(u, destination, &dresolved);
4369 if (r < 0) {
4370 log_syntax(unit, LOG_ERR, filename, line, r,
4371 "Failed to resolved specifiers in \"%s\", ignoring: %m", destination);
4372 continue;
4373 }
4374
4375 r = path_simplify_and_warn(dresolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
4376 if (r < 0)
4377 continue;
4378
4379 d = dresolved;
4380
4381 /* Optionally, there's also a short option string specified */
4382 if (p && p[-1] == ':') {
4383 _cleanup_free_ char *options = NULL;
4384
4385 r = extract_first_word(&p, &options, NULL, EXTRACT_UNQUOTE);
4386 if (r == -ENOMEM)
4387 return log_oom();
4388 if (r < 0) {
4389 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse %s: %s", lvalue, rvalue);
4390 return 0;
4391 }
4392
4393 if (isempty(options) || streq(options, "rbind"))
4394 rbind = true;
4395 else if (streq(options, "norbind"))
4396 rbind = false;
4397 else {
4398 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid option string, ignoring setting: %s", options);
4399 continue;
4400 }
4401 }
4402 } else
4403 d = s;
4404
4405 r = bind_mount_add(&c->bind_mounts, &c->n_bind_mounts,
4406 &(BindMount) {
4407 .source = s,
4408 .destination = d,
4409 .read_only = !!strstr(lvalue, "ReadOnly"),
4410 .recursive = rbind,
4411 .ignore_enoent = ignore_enoent,
4412 });
4413 if (r < 0)
4414 return log_oom();
4415 }
4416
4417 return 0;
4418 }
4419
4420 int config_parse_job_timeout_sec(
4421 const char* unit,
4422 const char *filename,
4423 unsigned line,
4424 const char *section,
4425 unsigned section_line,
4426 const char *lvalue,
4427 int ltype,
4428 const char *rvalue,
4429 void *data,
4430 void *userdata) {
4431
4432 Unit *u = data;
4433 usec_t usec;
4434 int r;
4435
4436 assert(filename);
4437 assert(lvalue);
4438 assert(rvalue);
4439 assert(u);
4440
4441 r = parse_sec_fix_0(rvalue, &usec);
4442 if (r < 0) {
4443 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse JobTimeoutSec= parameter, ignoring: %s", rvalue);
4444 return 0;
4445 }
4446
4447 /* If the user explicitly changed JobTimeoutSec= also change JobRunningTimeoutSec=, for compatibility with old
4448 * versions. If JobRunningTimeoutSec= was explicitly set, avoid this however as whatever the user picked should
4449 * count. */
4450
4451 if (!u->job_running_timeout_set)
4452 u->job_running_timeout = usec;
4453
4454 u->job_timeout = usec;
4455
4456 return 0;
4457 }
4458
4459 int config_parse_job_running_timeout_sec(
4460 const char* unit,
4461 const char *filename,
4462 unsigned line,
4463 const char *section,
4464 unsigned section_line,
4465 const char *lvalue,
4466 int ltype,
4467 const char *rvalue,
4468 void *data,
4469 void *userdata) {
4470
4471 Unit *u = data;
4472 usec_t usec;
4473 int r;
4474
4475 assert(filename);
4476 assert(lvalue);
4477 assert(rvalue);
4478 assert(u);
4479
4480 r = parse_sec_fix_0(rvalue, &usec);
4481 if (r < 0) {
4482 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse JobRunningTimeoutSec= parameter, ignoring: %s", rvalue);
4483 return 0;
4484 }
4485
4486 u->job_running_timeout = usec;
4487 u->job_running_timeout_set = true;
4488
4489 return 0;
4490 }
4491
4492 int config_parse_emergency_action(
4493 const char* unit,
4494 const char *filename,
4495 unsigned line,
4496 const char *section,
4497 unsigned section_line,
4498 const char *lvalue,
4499 int ltype,
4500 const char *rvalue,
4501 void *data,
4502 void *userdata) {
4503
4504 Manager *m = NULL;
4505 EmergencyAction *x = data;
4506 int r;
4507
4508 assert(filename);
4509 assert(lvalue);
4510 assert(rvalue);
4511 assert(data);
4512
4513 if (unit)
4514 m = ((Unit*) userdata)->manager;
4515 else
4516 m = data;
4517
4518 r = parse_emergency_action(rvalue, MANAGER_IS_SYSTEM(m), x);
4519 if (r < 0) {
4520 if (r == -EOPNOTSUPP && MANAGER_IS_USER(m)) {
4521 /* Compat mode: remove for systemd 241. */
4522
4523 log_syntax(unit, LOG_INFO, filename, line, r,
4524 "%s= in user mode specified as \"%s\", using \"exit-force\" instead.",
4525 lvalue, rvalue);
4526 *x = EMERGENCY_ACTION_EXIT_FORCE;
4527 return 0;
4528 }
4529
4530 if (r == -EOPNOTSUPP)
4531 log_syntax(unit, LOG_ERR, filename, line, r,
4532 "%s= specified as %s mode action, ignoring: %s",
4533 lvalue, MANAGER_IS_SYSTEM(m) ? "user" : "system", rvalue);
4534 else
4535 log_syntax(unit, LOG_ERR, filename, line, r,
4536 "Failed to parse %s=, ignoring: %s", lvalue, rvalue);
4537 return 0;
4538 }
4539
4540 return 0;
4541 }
4542
4543 int config_parse_pid_file(
4544 const char *unit,
4545 const char *filename,
4546 unsigned line,
4547 const char *section,
4548 unsigned section_line,
4549 const char *lvalue,
4550 int ltype,
4551 const char *rvalue,
4552 void *data,
4553 void *userdata) {
4554
4555 _cleanup_free_ char *k = NULL, *n = NULL;
4556 const Unit *u = userdata;
4557 char **s = data;
4558 int r;
4559
4560 assert(filename);
4561 assert(lvalue);
4562 assert(rvalue);
4563 assert(u);
4564
4565 if (isempty(rvalue)) {
4566 /* An empty assignment removes already set value. */
4567 *s = mfree(*s);
4568 return 0;
4569 }
4570
4571 r = unit_full_printf(u, rvalue, &k);
4572 if (r < 0) {
4573 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
4574 return 0;
4575 }
4576
4577 /* If this is a relative path make it absolute by prefixing the /run */
4578 n = path_make_absolute(k, u->manager->prefix[EXEC_DIRECTORY_RUNTIME]);
4579 if (!n)
4580 return log_oom();
4581
4582 /* Check that the result is a sensible path */
4583 r = path_simplify_and_warn(n, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
4584 if (r < 0)
4585 return r;
4586
4587 r = patch_var_run(unit, filename, line, lvalue, &n);
4588 if (r < 0)
4589 return r;
4590
4591 free_and_replace(*s, n);
4592 return 0;
4593 }
4594
4595 int config_parse_exit_status(
4596 const char *unit,
4597 const char *filename,
4598 unsigned line,
4599 const char *section,
4600 unsigned section_line,
4601 const char *lvalue,
4602 int ltype,
4603 const char *rvalue,
4604 void *data,
4605 void *userdata) {
4606
4607 int *exit_status = data, r;
4608 uint8_t u;
4609
4610 assert(filename);
4611 assert(lvalue);
4612 assert(rvalue);
4613 assert(exit_status);
4614
4615 if (isempty(rvalue)) {
4616 *exit_status = -1;
4617 return 0;
4618 }
4619
4620 r = safe_atou8(rvalue, &u);
4621 if (r < 0) {
4622 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse exit status '%s', ignoring: %m", rvalue);
4623 return 0;
4624 }
4625
4626 *exit_status = u;
4627 return 0;
4628 }
4629
4630 int config_parse_disable_controllers(
4631 const char *unit,
4632 const char *filename,
4633 unsigned line,
4634 const char *section,
4635 unsigned section_line,
4636 const char *lvalue,
4637 int ltype,
4638 const char *rvalue,
4639 void *data,
4640 void *userdata) {
4641
4642 int r;
4643 CGroupContext *c = data;
4644 CGroupMask disabled_mask;
4645
4646 /* 1. If empty, make all controllers eligible for use again.
4647 * 2. If non-empty, merge all listed controllers, space separated. */
4648
4649 if (isempty(rvalue)) {
4650 c->disable_controllers = 0;
4651 return 0;
4652 }
4653
4654 r = cg_mask_from_string(rvalue, &disabled_mask);
4655 if (r < 0 || disabled_mask <= 0) {
4656 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid cgroup string: %s, ignoring", rvalue);
4657 return 0;
4658 }
4659
4660 c->disable_controllers |= disabled_mask;
4661
4662 return 0;
4663 }
4664
4665 int config_parse_ip_filter_bpf_progs(
4666 const char *unit,
4667 const char *filename,
4668 unsigned line,
4669 const char *section,
4670 unsigned section_line,
4671 const char *lvalue,
4672 int ltype,
4673 const char *rvalue,
4674 void *data,
4675 void *userdata) {
4676
4677 _cleanup_free_ char *resolved = NULL;
4678 const Unit *u = userdata;
4679 char ***paths = data;
4680 int r;
4681
4682 assert(filename);
4683 assert(lvalue);
4684 assert(rvalue);
4685 assert(paths);
4686
4687 if (isempty(rvalue)) {
4688 *paths = strv_free(*paths);
4689 return 0;
4690 }
4691
4692 r = unit_full_printf(u, rvalue, &resolved);
4693 if (r < 0) {
4694 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
4695 return 0;
4696 }
4697
4698 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
4699 if (r < 0)
4700 return 0;
4701
4702 if (strv_contains(*paths, resolved))
4703 return 0;
4704
4705 r = strv_extend(paths, resolved);
4706 if (r < 0)
4707 return log_oom();
4708
4709 r = bpf_firewall_supported();
4710 if (r < 0)
4711 return r;
4712 if (r != BPF_FIREWALL_SUPPORTED_WITH_MULTI) {
4713 static bool warned = false;
4714
4715 log_full(warned ? LOG_DEBUG : LOG_WARNING,
4716 "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"
4717 "Starting this unit will fail! (This warning is only shown for the first loaded unit using IP firewalling.)", filename, line, lvalue, rvalue);
4718
4719 warned = true;
4720 }
4721
4722 return 0;
4723 }
4724
4725 static int merge_by_names(Unit **u, Set *names, const char *id) {
4726 char *k;
4727 int r;
4728
4729 assert(u);
4730 assert(*u);
4731
4732 /* Let's try to add in all names that are aliases of this unit */
4733 while ((k = set_steal_first(names))) {
4734 _cleanup_free_ _unused_ char *free_k = k;
4735
4736 /* First try to merge in the other name into our unit */
4737 r = unit_merge_by_name(*u, k);
4738 if (r < 0) {
4739 Unit *other;
4740
4741 /* Hmm, we couldn't merge the other unit into ours? Then let's try it the other way
4742 * round. */
4743
4744 other = manager_get_unit((*u)->manager, k);
4745 if (!other)
4746 return r; /* return previous failure */
4747
4748 r = unit_merge(other, *u);
4749 if (r < 0)
4750 return r;
4751
4752 *u = other;
4753 return merge_by_names(u, names, NULL);
4754 }
4755
4756 if (streq_ptr(id, k))
4757 unit_choose_id(*u, id);
4758 }
4759
4760 return 0;
4761 }
4762
4763 int unit_load_fragment(Unit *u) {
4764 const char *fragment;
4765 _cleanup_set_free_free_ Set *names = NULL;
4766 struct stat st;
4767 int r;
4768
4769 assert(u);
4770 assert(u->load_state == UNIT_STUB);
4771 assert(u->id);
4772
4773 if (u->transient) {
4774 u->load_state = UNIT_LOADED;
4775 return 0;
4776 }
4777
4778 /* Possibly rebuild the fragment map to catch new units */
4779 r = unit_file_build_name_map(&u->manager->lookup_paths,
4780 &u->manager->unit_cache_mtime,
4781 &u->manager->unit_id_map,
4782 &u->manager->unit_name_map,
4783 &u->manager->unit_path_cache);
4784 if (r < 0)
4785 log_error_errno(r, "Failed to rebuild name map: %m");
4786
4787 r = unit_file_find_fragment(u->manager->unit_id_map,
4788 u->manager->unit_name_map,
4789 u->id,
4790 &fragment,
4791 &names);
4792 if (r < 0 && r != -ENOENT)
4793 return r;
4794
4795 if (fragment) {
4796 /* Open the file, check if this is a mask, otherwise read. */
4797 _cleanup_fclose_ FILE *f = NULL;
4798
4799 /* Try to open the file name. A symlink is OK, for example for linked files or masks. We
4800 * expect that all symlinks within the lookup paths have been already resolved, but we don't
4801 * verify this here. */
4802 f = fopen(fragment, "re");
4803 if (!f)
4804 return log_unit_notice_errno(u, errno, "Failed to open %s: %m", fragment);
4805
4806 if (fstat(fileno(f), &st) < 0)
4807 return -errno;
4808
4809 r = free_and_strdup(&u->fragment_path, fragment);
4810 if (r < 0)
4811 return r;
4812
4813 if (null_or_empty(&st)) {
4814 /* Unit file is masked */
4815
4816 u->load_state = u->perpetual ? UNIT_LOADED : UNIT_MASKED; /* don't allow perpetual units to ever be masked */
4817 u->fragment_mtime = 0;
4818 } else {
4819 u->load_state = UNIT_LOADED;
4820 u->fragment_mtime = timespec_load(&st.st_mtim);
4821
4822 /* Now, parse the file contents */
4823 r = config_parse(u->id, fragment, f,
4824 UNIT_VTABLE(u)->sections,
4825 config_item_perf_lookup, load_fragment_gperf_lookup,
4826 CONFIG_PARSE_ALLOW_INCLUDE, u);
4827 if (r == -ENOEXEC)
4828 log_unit_notice_errno(u, r, "Unit configuration has fatal error, unit will not be started.");
4829 if (r < 0)
4830 return r;
4831 }
4832 }
4833
4834 if (u->source_path) {
4835 if (stat(u->source_path, &st) >= 0)
4836 u->source_mtime = timespec_load(&st.st_mtim);
4837 else
4838 u->source_mtime = 0;
4839 }
4840
4841 /* We do the merge dance here because for some unit types, the unit might have aliases which are not
4842 * declared in the file system. In particular, this is true (and frequent) for device and swap units.
4843 */
4844 Unit *merged;
4845 const char *id = u->id;
4846 _cleanup_free_ char *free_id = NULL;
4847
4848 if (fragment) {
4849 id = basename(fragment);
4850 if (unit_name_is_valid(id, UNIT_NAME_TEMPLATE)) {
4851 assert(u->instance); /* If we're not trying to use a template for non-instanced unit,
4852 * this must be set. */
4853
4854 r = unit_name_replace_instance(id, u->instance, &free_id);
4855 if (r < 0)
4856 return log_debug_errno(r, "Failed to build id (%s + %s): %m", id, u->instance);
4857 id = free_id;
4858 }
4859 }
4860
4861 merged = u;
4862 r = merge_by_names(&merged, names, id);
4863 if (r < 0)
4864 return r;
4865
4866 if (merged != u)
4867 u->load_state = UNIT_MERGED;
4868
4869 return 0;
4870 }
4871
4872 void unit_dump_config_items(FILE *f) {
4873 static const struct {
4874 const ConfigParserCallback callback;
4875 const char *rvalue;
4876 } table[] = {
4877 { config_parse_warn_compat, "NOTSUPPORTED" },
4878 { config_parse_int, "INTEGER" },
4879 { config_parse_unsigned, "UNSIGNED" },
4880 { config_parse_iec_size, "SIZE" },
4881 { config_parse_iec_uint64, "SIZE" },
4882 { config_parse_si_uint64, "SIZE" },
4883 { config_parse_bool, "BOOLEAN" },
4884 { config_parse_string, "STRING" },
4885 { config_parse_path, "PATH" },
4886 { config_parse_unit_path_printf, "PATH" },
4887 { config_parse_strv, "STRING [...]" },
4888 { config_parse_exec_nice, "NICE" },
4889 { config_parse_exec_oom_score_adjust, "OOMSCOREADJUST" },
4890 { config_parse_exec_io_class, "IOCLASS" },
4891 { config_parse_exec_io_priority, "IOPRIORITY" },
4892 { config_parse_exec_cpu_sched_policy, "CPUSCHEDPOLICY" },
4893 { config_parse_exec_cpu_sched_prio, "CPUSCHEDPRIO" },
4894 { config_parse_exec_cpu_affinity, "CPUAFFINITY" },
4895 { config_parse_mode, "MODE" },
4896 { config_parse_unit_env_file, "FILE" },
4897 { config_parse_exec_output, "OUTPUT" },
4898 { config_parse_exec_input, "INPUT" },
4899 { config_parse_log_facility, "FACILITY" },
4900 { config_parse_log_level, "LEVEL" },
4901 { config_parse_exec_secure_bits, "SECUREBITS" },
4902 { config_parse_capability_set, "BOUNDINGSET" },
4903 { config_parse_rlimit, "LIMIT" },
4904 { config_parse_unit_deps, "UNIT [...]" },
4905 { config_parse_exec, "PATH [ARGUMENT [...]]" },
4906 { config_parse_service_type, "SERVICETYPE" },
4907 { config_parse_service_restart, "SERVICERESTART" },
4908 { config_parse_kill_mode, "KILLMODE" },
4909 { config_parse_signal, "SIGNAL" },
4910 { config_parse_socket_listen, "SOCKET [...]" },
4911 { config_parse_socket_bind, "SOCKETBIND" },
4912 { config_parse_socket_bindtodevice, "NETWORKINTERFACE" },
4913 { config_parse_sec, "SECONDS" },
4914 { config_parse_nsec, "NANOSECONDS" },
4915 { config_parse_namespace_path_strv, "PATH [...]" },
4916 { config_parse_bind_paths, "PATH[:PATH[:OPTIONS]] [...]" },
4917 { config_parse_unit_requires_mounts_for, "PATH [...]" },
4918 { config_parse_exec_mount_flags, "MOUNTFLAG [...]" },
4919 { config_parse_unit_string_printf, "STRING" },
4920 { config_parse_trigger_unit, "UNIT" },
4921 { config_parse_timer, "TIMER" },
4922 { config_parse_path_spec, "PATH" },
4923 { config_parse_notify_access, "ACCESS" },
4924 { config_parse_ip_tos, "TOS" },
4925 { config_parse_unit_condition_path, "CONDITION" },
4926 { config_parse_unit_condition_string, "CONDITION" },
4927 { config_parse_unit_condition_null, "CONDITION" },
4928 { config_parse_unit_slice, "SLICE" },
4929 { config_parse_documentation, "URL" },
4930 { config_parse_service_timeout, "SECONDS" },
4931 { config_parse_emergency_action, "ACTION" },
4932 { config_parse_set_status, "STATUS" },
4933 { config_parse_service_sockets, "SOCKETS" },
4934 { config_parse_environ, "ENVIRON" },
4935 #if HAVE_SECCOMP
4936 { config_parse_syscall_filter, "SYSCALLS" },
4937 { config_parse_syscall_archs, "ARCHS" },
4938 { config_parse_syscall_errno, "ERRNO" },
4939 { config_parse_address_families, "FAMILIES" },
4940 { config_parse_restrict_namespaces, "NAMESPACES" },
4941 #endif
4942 { config_parse_cpu_shares, "SHARES" },
4943 { config_parse_cg_weight, "WEIGHT" },
4944 { config_parse_memory_limit, "LIMIT" },
4945 { config_parse_device_allow, "DEVICE" },
4946 { config_parse_device_policy, "POLICY" },
4947 { config_parse_io_limit, "LIMIT" },
4948 { config_parse_io_device_weight, "DEVICEWEIGHT" },
4949 { config_parse_io_device_latency, "DEVICELATENCY" },
4950 { config_parse_blockio_bandwidth, "BANDWIDTH" },
4951 { config_parse_blockio_weight, "WEIGHT" },
4952 { config_parse_blockio_device_weight, "DEVICEWEIGHT" },
4953 { config_parse_long, "LONG" },
4954 { config_parse_socket_service, "SERVICE" },
4955 #if HAVE_SELINUX
4956 { config_parse_exec_selinux_context, "LABEL" },
4957 #endif
4958 { config_parse_job_mode, "MODE" },
4959 { config_parse_job_mode_isolate, "BOOLEAN" },
4960 { config_parse_personality, "PERSONALITY" },
4961 };
4962
4963 const char *prev = NULL;
4964 const char *i;
4965
4966 assert(f);
4967
4968 NULSTR_FOREACH(i, load_fragment_gperf_nulstr) {
4969 const char *rvalue = "OTHER", *lvalue;
4970 const ConfigPerfItem *p;
4971 size_t prefix_len;
4972 const char *dot;
4973 unsigned j;
4974
4975 assert_se(p = load_fragment_gperf_lookup(i, strlen(i)));
4976
4977 /* Hide legacy settings */
4978 if (p->parse == config_parse_warn_compat &&
4979 p->ltype == DISABLED_LEGACY)
4980 continue;
4981
4982 for (j = 0; j < ELEMENTSOF(table); j++)
4983 if (p->parse == table[j].callback) {
4984 rvalue = table[j].rvalue;
4985 break;
4986 }
4987
4988 dot = strchr(i, '.');
4989 lvalue = dot ? dot + 1 : i;
4990 prefix_len = dot-i;
4991
4992 if (dot)
4993 if (!prev || !strneq(prev, i, prefix_len+1)) {
4994 if (prev)
4995 fputc('\n', f);
4996
4997 fprintf(f, "[%.*s]\n", (int) prefix_len, i);
4998 }
4999
5000 fprintf(f, "%s=%s\n", lvalue, rvalue);
5001 prev = i;
5002 }
5003 }
5004
5005 int config_parse_cpu_affinity2(
5006 const char *unit,
5007 const char *filename,
5008 unsigned line,
5009 const char *section,
5010 unsigned section_line,
5011 const char *lvalue,
5012 int ltype,
5013 const char *rvalue,
5014 void *data,
5015 void *userdata) {
5016
5017 CPUSet *affinity = data;
5018
5019 assert(affinity);
5020
5021 (void) parse_cpu_set_extend(rvalue, affinity, true, unit, filename, line, lvalue);
5022
5023 return 0;
5024 }
5025
5026 int config_parse_show_status(
5027 const char* unit,
5028 const char *filename,
5029 unsigned line,
5030 const char *section,
5031 unsigned section_line,
5032 const char *lvalue,
5033 int ltype,
5034 const char *rvalue,
5035 void *data,
5036 void *userdata) {
5037
5038 int k;
5039 ShowStatus *b = data;
5040
5041 assert(filename);
5042 assert(lvalue);
5043 assert(rvalue);
5044 assert(data);
5045
5046 k = parse_show_status(rvalue, b);
5047 if (k < 0) {
5048 log_syntax(unit, LOG_ERR, filename, line, k, "Failed to parse show status setting, ignoring: %s", rvalue);
5049 return 0;
5050 }
5051
5052 return 0;
5053 }
5054
5055 int config_parse_output_restricted(
5056 const char* unit,
5057 const char *filename,
5058 unsigned line,
5059 const char *section,
5060 unsigned section_line,
5061 const char *lvalue,
5062 int ltype,
5063 const char *rvalue,
5064 void *data,
5065 void *userdata) {
5066
5067 ExecOutput t, *eo = data;
5068 bool obsolete = false;
5069
5070 assert(filename);
5071 assert(lvalue);
5072 assert(rvalue);
5073 assert(data);
5074
5075 if (streq(rvalue, "syslog")) {
5076 t = EXEC_OUTPUT_JOURNAL;
5077 obsolete = true;
5078 } else if (streq(rvalue, "syslog+console")) {
5079 t = EXEC_OUTPUT_JOURNAL_AND_CONSOLE;
5080 obsolete = true;
5081 } else {
5082 t = exec_output_from_string(rvalue);
5083 if (t < 0) {
5084 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse output type, ignoring: %s", rvalue);
5085 return 0;
5086 }
5087
5088 if (IN_SET(t, EXEC_OUTPUT_SOCKET, EXEC_OUTPUT_NAMED_FD, EXEC_OUTPUT_FILE, EXEC_OUTPUT_FILE_APPEND)) {
5089 log_syntax(unit, LOG_ERR, filename, line, 0, "Standard output types socket, fd:, file:, append: are not supported as defaults, ignoring: %s", rvalue);
5090 return 0;
5091 }
5092 }
5093
5094 if (obsolete)
5095 log_syntax(unit, LOG_NOTICE, filename, line, 0,
5096 "Standard output type %s is obsolete, automatically updating to %s. Please update your configuration.",
5097 rvalue, exec_output_to_string(t));
5098
5099 *eo = t;
5100 return 0;
5101 }
5102
5103 int config_parse_crash_chvt(
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 int r;
5116
5117 assert(filename);
5118 assert(lvalue);
5119 assert(rvalue);
5120 assert(data);
5121
5122 r = parse_crash_chvt(rvalue, data);
5123 if (r < 0) {
5124 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse CrashChangeVT= setting, ignoring: %s", rvalue);
5125 return 0;
5126 }
5127
5128 return 0;
5129 }
5130
5131 int config_parse_swap_priority(
5132 const char *unit,
5133 const char *filename,
5134 unsigned line,
5135 const char *section,
5136 unsigned section_line,
5137 const char *lvalue,
5138 int ltype,
5139 const char *rvalue,
5140 void *data,
5141 void *userdata) {
5142
5143 Swap *s = userdata;
5144 int r, priority;
5145
5146 assert(s);
5147 assert(filename);
5148 assert(lvalue);
5149 assert(rvalue);
5150 assert(data);
5151
5152 if (isempty(rvalue)) {
5153 s->parameters_fragment.priority = -1;
5154 s->parameters_fragment.priority_set = false;
5155 return 0;
5156 }
5157
5158 r = safe_atoi(rvalue, &priority);
5159 if (r < 0) {
5160 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid swap priority '%s', ignoring.", rvalue);
5161 return 0;
5162 }
5163
5164 if (priority < -1) {
5165 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);
5166 return 0;
5167 }
5168
5169 if (priority > 32767) {
5170 log_syntax(unit, LOG_ERR, filename, line, 0, "Swap priority out of range, ignoring: %s", rvalue);
5171 return 0;
5172 }
5173
5174 s->parameters_fragment.priority = priority;
5175 s->parameters_fragment.priority_set = true;
5176 return 0;
5177 }