]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/load-fragment.c
core/load-fragment: move config_parse_sec_fix_0 to src/shared
[thirdparty/systemd.git] / src / core / load-fragment.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
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 "sd-messages.h"
17
18 #include "af-list.h"
19 #include "all-units.h"
20 #include "alloc-util.h"
21 #include "bpf-firewall.h"
22 #include "bpf-lsm.h"
23 #include "bpf-program.h"
24 #include "bpf-socket-bind.h"
25 #include "bus-error.h"
26 #include "bus-internal.h"
27 #include "bus-util.h"
28 #include "cap-list.h"
29 #include "capability-util.h"
30 #include "cgroup-setup.h"
31 #include "conf-parser.h"
32 #include "core-varlink.h"
33 #include "cpu-set-util.h"
34 #include "creds-util.h"
35 #include "env-util.h"
36 #include "errno-list.h"
37 #include "escape.h"
38 #include "fd-util.h"
39 #include "fileio.h"
40 #include "fs-util.h"
41 #include "hexdecoct.h"
42 #include "io-util.h"
43 #include "ioprio-util.h"
44 #include "ip-protocol-list.h"
45 #include "journal-file.h"
46 #include "limits-util.h"
47 #include "load-fragment.h"
48 #include "log.h"
49 #include "missing_ioprio.h"
50 #include "mountpoint-util.h"
51 #include "nulstr-util.h"
52 #include "parse-helpers.h"
53 #include "parse-util.h"
54 #include "path-util.h"
55 #include "percent-util.h"
56 #include "process-util.h"
57 #if HAVE_SECCOMP
58 #include "seccomp-util.h"
59 #endif
60 #include "securebits-util.h"
61 #include "selinux-util.h"
62 #include "signal-util.h"
63 #include "socket-netlink.h"
64 #include "specifier.h"
65 #include "stat-util.h"
66 #include "string-util.h"
67 #include "strv.h"
68 #include "syslog-util.h"
69 #include "time-util.h"
70 #include "unit-name.h"
71 #include "unit-printf.h"
72 #include "user-util.h"
73 #include "utf8.h"
74 #include "web-util.h"
75
76 static int parse_socket_protocol(const char *s) {
77 int r;
78
79 r = parse_ip_protocol(s);
80 if (r < 0)
81 return r;
82 if (!IN_SET(r, IPPROTO_UDPLITE, IPPROTO_SCTP))
83 return -EPROTONOSUPPORT;
84
85 return r;
86 }
87
88 int parse_crash_chvt(const char *value, int *data) {
89 int b;
90
91 if (safe_atoi(value, data) >= 0)
92 return 0;
93
94 b = parse_boolean(value);
95 if (b < 0)
96 return b;
97
98 if (b > 0)
99 *data = 0; /* switch to where kmsg goes */
100 else
101 *data = -1; /* turn off switching */
102
103 return 0;
104 }
105
106 int parse_confirm_spawn(const char *value, char **console) {
107 char *s;
108 int r;
109
110 r = value ? parse_boolean(value) : 1;
111 if (r == 0) {
112 *console = NULL;
113 return 0;
114 } else if (r > 0) /* on with default tty */
115 s = strdup("/dev/console");
116 else if (is_path(value)) /* on with fully qualified path */
117 s = strdup(value);
118 else /* on with only a tty file name, not a fully qualified path */
119 s = path_join("/dev/", value);
120 if (!s)
121 return -ENOMEM;
122
123 *console = s;
124 return 0;
125 }
126
127 DEFINE_CONFIG_PARSE(config_parse_socket_protocol, parse_socket_protocol, "Failed to parse socket protocol");
128 DEFINE_CONFIG_PARSE(config_parse_exec_secure_bits, secure_bits_from_string, "Failed to parse secure bits");
129 DEFINE_CONFIG_PARSE_ENUM(config_parse_collect_mode, collect_mode, CollectMode, "Failed to parse garbage collection mode");
130 DEFINE_CONFIG_PARSE_ENUM(config_parse_device_policy, cgroup_device_policy, CGroupDevicePolicy, "Failed to parse device policy");
131 DEFINE_CONFIG_PARSE_ENUM(config_parse_exec_keyring_mode, exec_keyring_mode, ExecKeyringMode, "Failed to parse keyring mode");
132 DEFINE_CONFIG_PARSE_ENUM(config_parse_protect_proc, protect_proc, ProtectProc, "Failed to parse /proc/ protection mode");
133 DEFINE_CONFIG_PARSE_ENUM(config_parse_proc_subset, proc_subset, ProcSubset, "Failed to parse /proc/ subset mode");
134 DEFINE_CONFIG_PARSE_ENUM(config_parse_exec_utmp_mode, exec_utmp_mode, ExecUtmpMode, "Failed to parse utmp mode");
135 DEFINE_CONFIG_PARSE_ENUM(config_parse_job_mode, job_mode, JobMode, "Failed to parse job mode");
136 DEFINE_CONFIG_PARSE_ENUM(config_parse_notify_access, notify_access, NotifyAccess, "Failed to parse notify access specifier");
137 DEFINE_CONFIG_PARSE_ENUM(config_parse_protect_home, protect_home, ProtectHome, "Failed to parse protect home value");
138 DEFINE_CONFIG_PARSE_ENUM(config_parse_protect_system, protect_system, ProtectSystem, "Failed to parse protect system value");
139 DEFINE_CONFIG_PARSE_ENUM(config_parse_runtime_preserve_mode, exec_preserve_mode, ExecPreserveMode, "Failed to parse runtime directory preserve mode");
140 DEFINE_CONFIG_PARSE_ENUM(config_parse_service_type, service_type, ServiceType, "Failed to parse service type");
141 DEFINE_CONFIG_PARSE_ENUM(config_parse_service_exit_type, service_exit_type, ServiceExitType, "Failed to parse service exit type");
142 DEFINE_CONFIG_PARSE_ENUM(config_parse_service_restart, service_restart, ServiceRestart, "Failed to parse service restart specifier");
143 DEFINE_CONFIG_PARSE_ENUM(config_parse_service_timeout_failure_mode, service_timeout_failure_mode, ServiceTimeoutFailureMode, "Failed to parse timeout failure mode");
144 DEFINE_CONFIG_PARSE_ENUM(config_parse_socket_bind, socket_address_bind_ipv6_only_or_bool, SocketAddressBindIPv6Only, "Failed to parse bind IPv6 only value");
145 DEFINE_CONFIG_PARSE_ENUM(config_parse_oom_policy, oom_policy, OOMPolicy, "Failed to parse OOM policy");
146 DEFINE_CONFIG_PARSE_ENUM(config_parse_managed_oom_preference, managed_oom_preference, ManagedOOMPreference, "Failed to parse ManagedOOMPreference=");
147 DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT(config_parse_ip_tos, ip_tos, int, -1, "Failed to parse IP TOS value");
148 DEFINE_CONFIG_PARSE_PTR(config_parse_blockio_weight, cg_blkio_weight_parse, uint64_t, "Invalid block IO weight");
149 DEFINE_CONFIG_PARSE_PTR(config_parse_cg_weight, cg_weight_parse, uint64_t, "Invalid weight");
150 DEFINE_CONFIG_PARSE_PTR(config_parse_cg_cpu_weight, cg_cpu_weight_parse, uint64_t, "Invalid CPU weight");
151 DEFINE_CONFIG_PARSE_PTR(config_parse_cpu_shares, cg_cpu_shares_parse, uint64_t, "Invalid CPU shares");
152 DEFINE_CONFIG_PARSE_PTR(config_parse_exec_mount_flags, mount_propagation_flags_from_string, unsigned long, "Failed to parse mount flag");
153 DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT(config_parse_numa_policy, mpol, int, -1, "Invalid NUMA policy type");
154 DEFINE_CONFIG_PARSE_ENUM(config_parse_status_unit_format, status_unit_format, StatusUnitFormat, "Failed to parse status unit format");
155 DEFINE_CONFIG_PARSE_ENUM_FULL(config_parse_socket_timestamping, socket_timestamping_from_string_harder, SocketTimestamping, "Failed to parse timestamping precision");
156
157 bool contains_instance_specifier_superset(const char *s) {
158 const char *p, *q;
159 bool percent = false;
160
161 assert(s);
162
163 p = strchr(s, '@');
164 if (!p)
165 return false;
166
167 p++; /* Skip '@' */
168
169 q = strrchr(p, '.');
170 if (!q)
171 q = p + strlen(p);
172
173 /* If the string is just the instance specifier, it's not a superset of the instance. */
174 if (memcmp_nn(p, q - p, "%i", strlen("%i")) == 0)
175 return false;
176
177 /* %i, %n and %N all expand to the instance or a superset of it. */
178 for (; p < q; p++) {
179 if (*p == '%')
180 percent = !percent;
181 else if (percent) {
182 if (IN_SET(*p, 'n', 'N', 'i'))
183 return true;
184 percent = false;
185 }
186 }
187
188 return false;
189 }
190
191 /* `name` is the rendered version of `format` via `unit_printf` or similar functions. */
192 int unit_is_likely_recursive_template_dependency(Unit *u, const char *name, const char *format) {
193 const char *fragment_path;
194 int r;
195
196 assert(u);
197 assert(name);
198
199 /* If a template unit has a direct dependency on itself that includes the unit instance as part of
200 * the template instance via a unit specifier (%i, %n or %N), this will almost certainly lead to
201 * infinite recursion as systemd will keep instantiating new instances of the template unit.
202 * https://github.com/systemd/systemd/issues/17602 shows a good example of how this can happen in
203 * practice. To guard against this, we check for templates that depend on themselves and have the
204 * instantiated unit instance included as part of the template instance of the dependency via a
205 * specifier.
206 *
207 * For example, if systemd-notify@.service depends on systemd-notify@%n.service, this will result in
208 * infinite recursion.
209 */
210
211 if (!unit_name_is_valid(name, UNIT_NAME_INSTANCE))
212 return false;
213
214 if (!unit_name_prefix_equal(u->id, name))
215 return false;
216
217 if (u->type != unit_name_to_type(name))
218 return false;
219
220 r = unit_file_find_fragment(u->manager->unit_id_map, u->manager->unit_name_map, name, &fragment_path, NULL);
221 if (r < 0)
222 return r;
223
224 /* Fragment paths should also be equal as a custom fragment for a specific template instance
225 * wouldn't necessarily lead to infinite recursion. */
226 if (!path_equal_ptr(u->fragment_path, fragment_path))
227 return false;
228
229 if (!contains_instance_specifier_superset(format))
230 return false;
231
232 return true;
233 }
234
235 int config_parse_unit_deps(
236 const char *unit,
237 const char *filename,
238 unsigned line,
239 const char *section,
240 unsigned section_line,
241 const char *lvalue,
242 int ltype,
243 const char *rvalue,
244 void *data,
245 void *userdata) {
246
247 UnitDependency d = ltype;
248 Unit *u = userdata;
249
250 assert(filename);
251 assert(lvalue);
252 assert(rvalue);
253
254 for (const char *p = rvalue;;) {
255 _cleanup_free_ char *word = NULL, *k = NULL;
256 int r;
257
258 r = extract_first_word(&p, &word, NULL, EXTRACT_RETAIN_ESCAPE);
259 if (r == 0)
260 return 0;
261 if (r == -ENOMEM)
262 return log_oom();
263 if (r < 0) {
264 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
265 return 0;
266 }
267
268 r = unit_name_printf(u, word, &k);
269 if (r < 0) {
270 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", word);
271 continue;
272 }
273
274 r = unit_is_likely_recursive_template_dependency(u, k, word);
275 if (r < 0) {
276 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to determine if '%s' is a recursive dependency, ignoring: %m", k);
277 continue;
278 }
279 if (r > 0) {
280 log_syntax(unit, LOG_DEBUG, filename, line, 0,
281 "Dropping dependency %s=%s that likely leads to infinite recursion.",
282 unit_dependency_to_string(d), word);
283 continue;
284 }
285
286 r = unit_add_dependency_by_name(u, d, k, true, UNIT_DEPENDENCY_FILE);
287 if (r < 0)
288 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to add dependency on %s, ignoring: %m", k);
289 }
290 }
291
292 int config_parse_obsolete_unit_deps(
293 const char *unit,
294 const char *filename,
295 unsigned line,
296 const char *section,
297 unsigned section_line,
298 const char *lvalue,
299 int ltype,
300 const char *rvalue,
301 void *data,
302 void *userdata) {
303
304 log_syntax(unit, LOG_WARNING, filename, line, 0,
305 "Unit dependency type %s= is obsolete, replacing by %s=, please update your unit file", lvalue, unit_dependency_to_string(ltype));
306
307 return config_parse_unit_deps(unit, filename, line, section, section_line, lvalue, ltype, rvalue, data, userdata);
308 }
309
310 int config_parse_unit_string_printf(
311 const char *unit,
312 const char *filename,
313 unsigned line,
314 const char *section,
315 unsigned section_line,
316 const char *lvalue,
317 int ltype,
318 const char *rvalue,
319 void *data,
320 void *userdata) {
321
322 _cleanup_free_ char *k = NULL;
323 const Unit *u = userdata;
324 int r;
325
326 assert(filename);
327 assert(lvalue);
328 assert(rvalue);
329 assert(u);
330
331 r = unit_full_printf(u, rvalue, &k);
332 if (r < 0) {
333 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
334 return 0;
335 }
336
337 return config_parse_string(unit, filename, line, section, section_line, lvalue, ltype, k, data, userdata);
338 }
339
340 int config_parse_unit_strv_printf(
341 const char *unit,
342 const char *filename,
343 unsigned line,
344 const char *section,
345 unsigned section_line,
346 const char *lvalue,
347 int ltype,
348 const char *rvalue,
349 void *data,
350 void *userdata) {
351
352 const Unit *u = userdata;
353 _cleanup_free_ char *k = NULL;
354 int r;
355
356 assert(filename);
357 assert(lvalue);
358 assert(rvalue);
359 assert(u);
360
361 r = unit_full_printf(u, rvalue, &k);
362 if (r < 0) {
363 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
364 return 0;
365 }
366
367 return config_parse_strv(unit, filename, line, section, section_line, lvalue, ltype, k, data, userdata);
368 }
369
370 int config_parse_unit_path_printf(
371 const char *unit,
372 const char *filename,
373 unsigned line,
374 const char *section,
375 unsigned section_line,
376 const char *lvalue,
377 int ltype,
378 const char *rvalue,
379 void *data,
380 void *userdata) {
381
382 _cleanup_free_ char *k = NULL;
383 const Unit *u = userdata;
384 int r;
385 bool fatal = ltype;
386
387 assert(filename);
388 assert(lvalue);
389 assert(rvalue);
390 assert(u);
391
392 r = unit_path_printf(u, rvalue, &k);
393 if (r < 0) {
394 log_syntax(unit, fatal ? LOG_ERR : LOG_WARNING, filename, line, r,
395 "Failed to resolve unit specifiers in '%s'%s: %m",
396 rvalue, fatal ? "" : ", ignoring");
397 return fatal ? -ENOEXEC : 0;
398 }
399
400 return config_parse_path(unit, filename, line, section, section_line, lvalue, ltype, k, data, userdata);
401 }
402
403 int config_parse_colon_separated_paths(
404 const char *unit,
405 const char *filename,
406 unsigned line,
407 const char *section,
408 unsigned section_line,
409 const char *lvalue,
410 int ltype,
411 const char *rvalue,
412 void *data,
413 void *userdata) {
414 char ***sv = data;
415 const Unit *u = userdata;
416 int r;
417
418 assert(filename);
419 assert(lvalue);
420 assert(rvalue);
421 assert(data);
422
423 if (isempty(rvalue)) {
424 /* Empty assignment resets the list */
425 *sv = strv_free(*sv);
426 return 0;
427 }
428
429 for (const char *p = rvalue;;) {
430 _cleanup_free_ char *word = NULL, *k = NULL;
431
432 r = extract_first_word(&p, &word, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
433 if (r == -ENOMEM)
434 return log_oom();
435 if (r < 0) {
436 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to extract first word, ignoring: %s", rvalue);
437 return 0;
438 }
439 if (r == 0)
440 break;
441
442 r = unit_path_printf(u, word, &k);
443 if (r < 0) {
444 log_syntax(unit, LOG_WARNING, filename, line, r,
445 "Failed to resolve unit specifiers in '%s', ignoring: %m", word);
446 return 0;
447 }
448
449 r = path_simplify_and_warn(k, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
450 if (r < 0)
451 return 0;
452
453 r = strv_consume(sv, TAKE_PTR(k));
454 if (r < 0)
455 return log_oom();
456 }
457
458 return 0;
459 }
460
461 int config_parse_unit_path_strv_printf(
462 const char *unit,
463 const char *filename,
464 unsigned line,
465 const char *section,
466 unsigned section_line,
467 const char *lvalue,
468 int ltype,
469 const char *rvalue,
470 void *data,
471 void *userdata) {
472
473 char ***x = data;
474 const Unit *u = userdata;
475 int r;
476
477 assert(filename);
478 assert(lvalue);
479 assert(rvalue);
480 assert(u);
481
482 if (isempty(rvalue)) {
483 *x = strv_free(*x);
484 return 0;
485 }
486
487 for (const char *p = rvalue;;) {
488 _cleanup_free_ char *word = NULL, *k = NULL;
489
490 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
491 if (r == 0)
492 return 0;
493 if (r == -ENOMEM)
494 return log_oom();
495 if (r < 0) {
496 log_syntax(unit, LOG_WARNING, filename, line, r,
497 "Invalid syntax, ignoring: %s", rvalue);
498 return 0;
499 }
500
501 r = unit_path_printf(u, word, &k);
502 if (r < 0) {
503 log_syntax(unit, LOG_WARNING, filename, line, r,
504 "Failed to resolve unit specifiers in '%s', ignoring: %m", word);
505 return 0;
506 }
507
508 r = path_simplify_and_warn(k, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
509 if (r < 0)
510 return 0;
511
512 r = strv_consume(x, TAKE_PTR(k));
513 if (r < 0)
514 return log_oom();
515 }
516 }
517
518 static int patch_var_run(
519 const char *unit,
520 const char *filename,
521 unsigned line,
522 const char *lvalue,
523 char **path) {
524
525 const char *e;
526 char *z;
527
528 e = path_startswith(*path, "/var/run/");
529 if (!e)
530 return 0;
531
532 z = path_join("/run/", e);
533 if (!z)
534 return log_oom();
535
536 log_syntax(unit, LOG_NOTICE, filename, line, 0,
537 "%s= references a path below legacy directory /var/run/, updating %s → %s; "
538 "please update the unit file accordingly.", lvalue, *path, z);
539
540 free_and_replace(*path, z);
541
542 return 1;
543 }
544
545 int config_parse_socket_listen(
546 const char *unit,
547 const char *filename,
548 unsigned line,
549 const char *section,
550 unsigned section_line,
551 const char *lvalue,
552 int ltype,
553 const char *rvalue,
554 void *data,
555 void *userdata) {
556
557 _cleanup_free_ SocketPort *p = NULL;
558 SocketPort *tail;
559 Socket *s;
560 int r;
561
562 assert(filename);
563 assert(lvalue);
564 assert(rvalue);
565 assert(data);
566
567 s = SOCKET(data);
568
569 if (isempty(rvalue)) {
570 /* An empty assignment removes all ports */
571 socket_free_ports(s);
572 return 0;
573 }
574
575 p = new0(SocketPort, 1);
576 if (!p)
577 return log_oom();
578
579 if (ltype != SOCKET_SOCKET) {
580 _cleanup_free_ char *k = NULL;
581
582 r = unit_path_printf(UNIT(s), rvalue, &k);
583 if (r < 0) {
584 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
585 return 0;
586 }
587
588 r = path_simplify_and_warn(k, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
589 if (r < 0)
590 return 0;
591
592 if (ltype == SOCKET_FIFO) {
593 r = patch_var_run(unit, filename, line, lvalue, &k);
594 if (r < 0)
595 return r;
596 }
597
598 free_and_replace(p->path, k);
599 p->type = ltype;
600
601 } else if (streq(lvalue, "ListenNetlink")) {
602 _cleanup_free_ char *k = NULL;
603
604 r = unit_path_printf(UNIT(s), rvalue, &k);
605 if (r < 0) {
606 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
607 return 0;
608 }
609
610 r = socket_address_parse_netlink(&p->address, k);
611 if (r < 0) {
612 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse address value in '%s', ignoring: %m", k);
613 return 0;
614 }
615
616 p->type = SOCKET_SOCKET;
617
618 } else {
619 _cleanup_free_ char *k = NULL;
620
621 r = unit_path_printf(UNIT(s), rvalue, &k);
622 if (r < 0) {
623 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
624 return 0;
625 }
626
627 if (k[0] == '/') { /* Only for AF_UNIX file system sockets… */
628 r = patch_var_run(unit, filename, line, lvalue, &k);
629 if (r < 0)
630 return r;
631 }
632
633 r = socket_address_parse_and_warn(&p->address, k);
634 if (r < 0) {
635 if (r != -EAFNOSUPPORT)
636 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse address value in '%s', ignoring: %m", k);
637 return 0;
638 }
639
640 if (streq(lvalue, "ListenStream"))
641 p->address.type = SOCK_STREAM;
642 else if (streq(lvalue, "ListenDatagram"))
643 p->address.type = SOCK_DGRAM;
644 else {
645 assert(streq(lvalue, "ListenSequentialPacket"));
646 p->address.type = SOCK_SEQPACKET;
647 }
648
649 if (socket_address_family(&p->address) != AF_UNIX && p->address.type == SOCK_SEQPACKET) {
650 log_syntax(unit, LOG_WARNING, filename, line, 0, "Address family not supported, ignoring: %s", rvalue);
651 return 0;
652 }
653
654 p->type = SOCKET_SOCKET;
655 }
656
657 p->fd = -1;
658 p->auxiliary_fds = NULL;
659 p->n_auxiliary_fds = 0;
660 p->socket = s;
661
662 LIST_FIND_TAIL(port, s->ports, tail);
663 LIST_INSERT_AFTER(port, s->ports, tail, p);
664
665 p = NULL;
666
667 return 0;
668 }
669
670 int config_parse_exec_nice(
671 const char *unit,
672 const char *filename,
673 unsigned line,
674 const char *section,
675 unsigned section_line,
676 const char *lvalue,
677 int ltype,
678 const char *rvalue,
679 void *data,
680 void *userdata) {
681
682 ExecContext *c = data;
683 int priority, r;
684
685 assert(filename);
686 assert(lvalue);
687 assert(rvalue);
688 assert(data);
689
690 if (isempty(rvalue)) {
691 c->nice_set = false;
692 return 0;
693 }
694
695 r = parse_nice(rvalue, &priority);
696 if (r < 0) {
697 if (r == -ERANGE)
698 log_syntax(unit, LOG_WARNING, filename, line, r, "Nice priority out of range, ignoring: %s", rvalue);
699 else
700 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse nice priority '%s', ignoring: %m", rvalue);
701 return 0;
702 }
703
704 c->nice = priority;
705 c->nice_set = true;
706
707 return 0;
708 }
709
710 int config_parse_exec_oom_score_adjust(
711 const char* unit,
712 const char *filename,
713 unsigned line,
714 const char *section,
715 unsigned section_line,
716 const char *lvalue,
717 int ltype,
718 const char *rvalue,
719 void *data,
720 void *userdata) {
721
722 ExecContext *c = data;
723 int oa, r;
724
725 assert(filename);
726 assert(lvalue);
727 assert(rvalue);
728 assert(data);
729
730 if (isempty(rvalue)) {
731 c->oom_score_adjust_set = false;
732 return 0;
733 }
734
735 r = parse_oom_score_adjust(rvalue, &oa);
736 if (r < 0) {
737 if (r == -ERANGE)
738 log_syntax(unit, LOG_WARNING, filename, line, r, "OOM score adjust value out of range, ignoring: %s", rvalue);
739 else
740 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse the OOM score adjust value '%s', ignoring: %m", rvalue);
741 return 0;
742 }
743
744 c->oom_score_adjust = oa;
745 c->oom_score_adjust_set = true;
746
747 return 0;
748 }
749
750 int config_parse_exec_coredump_filter(
751 const char* unit,
752 const char *filename,
753 unsigned line,
754 const char *section,
755 unsigned section_line,
756 const char *lvalue,
757 int ltype,
758 const char *rvalue,
759 void *data,
760 void *userdata) {
761
762 ExecContext *c = data;
763 int r;
764
765 assert(filename);
766 assert(lvalue);
767 assert(rvalue);
768 assert(data);
769
770 if (isempty(rvalue)) {
771 c->coredump_filter = 0;
772 c->coredump_filter_set = false;
773 return 0;
774 }
775
776 uint64_t f;
777 r = coredump_filter_mask_from_string(rvalue, &f);
778 if (r < 0) {
779 log_syntax(unit, LOG_WARNING, filename, line, r,
780 "Failed to parse the CoredumpFilter=%s, ignoring: %m", rvalue);
781 return 0;
782 }
783
784 c->coredump_filter |= f;
785 c->oom_score_adjust_set = true;
786 return 0;
787 }
788
789 int config_parse_kill_mode(
790 const char* unit,
791 const char *filename,
792 unsigned line,
793 const char *section,
794 unsigned section_line,
795 const char *lvalue,
796 int ltype,
797 const char *rvalue,
798 void *data,
799 void *userdata) {
800
801 KillMode *k = data, m;
802
803 assert(filename);
804 assert(lvalue);
805 assert(rvalue);
806 assert(data);
807
808 if (isempty(rvalue)) {
809 *k = KILL_CONTROL_GROUP;
810 return 0;
811 }
812
813 m = kill_mode_from_string(rvalue);
814 if (m < 0) {
815 log_syntax(unit, LOG_WARNING, filename, line, m,
816 "Failed to parse kill mode specification, ignoring: %s", rvalue);
817 return 0;
818 }
819
820 if (m == KILL_NONE)
821 log_syntax(unit, LOG_WARNING, filename, line, 0,
822 "Unit configured to use KillMode=none. "
823 "This is unsafe, as it disables systemd's process lifecycle management for the service. "
824 "Please update your service to use a safer KillMode=, such as 'mixed' or 'control-group'. "
825 "Support for KillMode=none is deprecated and will eventually be removed.");
826
827 *k = m;
828 return 0;
829 }
830
831 int config_parse_exec(
832 const char *unit,
833 const char *filename,
834 unsigned line,
835 const char *section,
836 unsigned section_line,
837 const char *lvalue,
838 int ltype,
839 const char *rvalue,
840 void *data,
841 void *userdata) {
842
843 ExecCommand **e = data;
844 const Unit *u = userdata;
845 const char *p;
846 bool semicolon;
847 int r;
848
849 assert(filename);
850 assert(lvalue);
851 assert(rvalue);
852 assert(e);
853
854 e += ltype;
855
856 if (isempty(rvalue)) {
857 /* An empty assignment resets the list */
858 *e = exec_command_free_list(*e);
859 return 0;
860 }
861
862 p = rvalue;
863 do {
864 _cleanup_free_ char *path = NULL, *firstword = NULL;
865 ExecCommandFlags flags = 0;
866 bool ignore = false, separate_argv0 = false;
867 _cleanup_free_ ExecCommand *nce = NULL;
868 _cleanup_strv_free_ char **n = NULL;
869 size_t nlen = 0;
870 const char *f;
871
872 semicolon = false;
873
874 r = extract_first_word_and_warn(&p, &firstword, NULL, EXTRACT_UNQUOTE|EXTRACT_CUNESCAPE, unit, filename, line, rvalue);
875 if (r <= 0)
876 return 0;
877
878 /* A lone ";" is a separator. Let's make sure we don't treat it as an executable name. */
879 if (streq(firstword, ";")) {
880 semicolon = true;
881 continue;
882 }
883
884 f = firstword;
885 for (;;) {
886 /* We accept an absolute path as first argument. If it's prefixed with - and the path doesn't
887 * exist, we ignore it instead of erroring out; if it's prefixed with @, we allow overriding of
888 * argv[0]; if it's prefixed with :, we will not do environment variable substitution;
889 * if it's prefixed with +, it will be run with full privileges and no sandboxing; if
890 * it's prefixed with '!' we apply sandboxing, but do not change user/group credentials; if
891 * it's prefixed with '!!', then we apply user/group credentials if the kernel supports ambient
892 * capabilities -- if it doesn't we don't apply the credentials themselves, but do apply most
893 * other sandboxing, with some special exceptions for changing UID.
894 *
895 * The idea is that '!!' may be used to write services that can take benefit of systemd's
896 * UID/GID dropping if the kernel supports ambient creds, but provide an automatic fallback to
897 * privilege dropping within the daemon if the kernel does not offer that. */
898
899 if (*f == '-' && !(flags & EXEC_COMMAND_IGNORE_FAILURE)) {
900 flags |= EXEC_COMMAND_IGNORE_FAILURE;
901 ignore = true;
902 } else if (*f == '@' && !separate_argv0)
903 separate_argv0 = true;
904 else if (*f == ':' && !(flags & EXEC_COMMAND_NO_ENV_EXPAND))
905 flags |= EXEC_COMMAND_NO_ENV_EXPAND;
906 else if (*f == '+' && !(flags & (EXEC_COMMAND_FULLY_PRIVILEGED|EXEC_COMMAND_NO_SETUID|EXEC_COMMAND_AMBIENT_MAGIC)))
907 flags |= EXEC_COMMAND_FULLY_PRIVILEGED;
908 else if (*f == '!' && !(flags & (EXEC_COMMAND_FULLY_PRIVILEGED|EXEC_COMMAND_NO_SETUID|EXEC_COMMAND_AMBIENT_MAGIC)))
909 flags |= EXEC_COMMAND_NO_SETUID;
910 else if (*f == '!' && !(flags & (EXEC_COMMAND_FULLY_PRIVILEGED|EXEC_COMMAND_AMBIENT_MAGIC))) {
911 flags &= ~EXEC_COMMAND_NO_SETUID;
912 flags |= EXEC_COMMAND_AMBIENT_MAGIC;
913 } else
914 break;
915 f++;
916 }
917
918 r = unit_path_printf(u, f, &path);
919 if (r < 0) {
920 log_syntax(unit, ignore ? LOG_WARNING : LOG_ERR, filename, line, r,
921 "Failed to resolve unit specifiers in '%s'%s: %m",
922 f, ignore ? ", ignoring" : "");
923 return ignore ? 0 : -ENOEXEC;
924 }
925
926 if (isempty(path)) {
927 /* First word is either "-" or "@" with no command. */
928 log_syntax(unit, ignore ? LOG_WARNING : LOG_ERR, filename, line, 0,
929 "Empty path in command line%s: '%s'",
930 ignore ? ", ignoring" : "", rvalue);
931 return ignore ? 0 : -ENOEXEC;
932 }
933 if (!string_is_safe(path)) {
934 log_syntax(unit, ignore ? LOG_WARNING : LOG_ERR, filename, line, 0,
935 "Executable name contains special characters%s: %s",
936 ignore ? ", ignoring" : "", path);
937 return ignore ? 0 : -ENOEXEC;
938 }
939 if (endswith(path, "/")) {
940 log_syntax(unit, ignore ? LOG_WARNING : LOG_ERR, filename, line, 0,
941 "Executable path specifies a directory%s: %s",
942 ignore ? ", ignoring" : "", path);
943 return ignore ? 0 : -ENOEXEC;
944 }
945
946 if (!(path_is_absolute(path) ? path_is_valid(path) : filename_is_valid(path))) {
947 log_syntax(unit, ignore ? LOG_WARNING : LOG_ERR, filename, line, 0,
948 "Neither a valid executable name nor an absolute path%s: %s",
949 ignore ? ", ignoring" : "", path);
950 return ignore ? 0 : -ENOEXEC;
951 }
952
953 if (!separate_argv0) {
954 char *w = NULL;
955
956 if (!GREEDY_REALLOC0(n, nlen + 2))
957 return log_oom();
958
959 w = strdup(path);
960 if (!w)
961 return log_oom();
962 n[nlen++] = w;
963 n[nlen] = NULL;
964 }
965
966 path_simplify(path);
967
968 while (!isempty(p)) {
969 _cleanup_free_ char *word = NULL, *resolved = NULL;
970
971 /* Check explicitly for an unquoted semicolon as
972 * command separator token. */
973 if (p[0] == ';' && (!p[1] || strchr(WHITESPACE, p[1]))) {
974 p++;
975 p += strspn(p, WHITESPACE);
976 semicolon = true;
977 break;
978 }
979
980 /* Check for \; explicitly, to not confuse it with \\; or "\;" or "\\;" etc.
981 * extract_first_word() would return the same for all of those. */
982 if (p[0] == '\\' && p[1] == ';' && (!p[2] || strchr(WHITESPACE, p[2]))) {
983 char *w;
984
985 p += 2;
986 p += strspn(p, WHITESPACE);
987
988 if (!GREEDY_REALLOC0(n, nlen + 2))
989 return log_oom();
990
991 w = strdup(";");
992 if (!w)
993 return log_oom();
994 n[nlen++] = w;
995 n[nlen] = NULL;
996 continue;
997 }
998
999 r = extract_first_word_and_warn(&p, &word, NULL, EXTRACT_UNQUOTE|EXTRACT_CUNESCAPE, unit, filename, line, rvalue);
1000 if (r == 0)
1001 break;
1002 if (r < 0)
1003 return ignore ? 0 : -ENOEXEC;
1004
1005 r = unit_full_printf(u, word, &resolved);
1006 if (r < 0) {
1007 log_syntax(unit, ignore ? LOG_WARNING : LOG_ERR, filename, line, r,
1008 "Failed to resolve unit specifiers in %s%s: %m",
1009 word, ignore ? ", ignoring" : "");
1010 return ignore ? 0 : -ENOEXEC;
1011 }
1012
1013 if (!GREEDY_REALLOC(n, nlen + 2))
1014 return log_oom();
1015
1016 n[nlen++] = TAKE_PTR(resolved);
1017 n[nlen] = NULL;
1018 }
1019
1020 if (!n || !n[0]) {
1021 log_syntax(unit, ignore ? LOG_WARNING : LOG_ERR, filename, line, 0,
1022 "Empty executable name or zeroeth argument%s: %s",
1023 ignore ? ", ignoring" : "", rvalue);
1024 return ignore ? 0 : -ENOEXEC;
1025 }
1026
1027 nce = new0(ExecCommand, 1);
1028 if (!nce)
1029 return log_oom();
1030
1031 nce->argv = TAKE_PTR(n);
1032 nce->path = TAKE_PTR(path);
1033 nce->flags = flags;
1034
1035 exec_command_append_list(e, nce);
1036
1037 /* Do not _cleanup_free_ these. */
1038 nce = NULL;
1039
1040 rvalue = p;
1041 } while (semicolon);
1042
1043 return 0;
1044 }
1045
1046 int config_parse_socket_bindtodevice(
1047 const char* unit,
1048 const char *filename,
1049 unsigned line,
1050 const char *section,
1051 unsigned section_line,
1052 const char *lvalue,
1053 int ltype,
1054 const char *rvalue,
1055 void *data,
1056 void *userdata) {
1057
1058 Socket *s = data;
1059
1060 assert(filename);
1061 assert(lvalue);
1062 assert(rvalue);
1063 assert(data);
1064
1065 if (isempty(rvalue) || streq(rvalue, "*")) {
1066 s->bind_to_device = mfree(s->bind_to_device);
1067 return 0;
1068 }
1069
1070 if (!ifname_valid(rvalue)) {
1071 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid interface name, ignoring: %s", rvalue);
1072 return 0;
1073 }
1074
1075 return free_and_strdup_warn(&s->bind_to_device, rvalue);
1076 }
1077
1078 int config_parse_exec_input(
1079 const char *unit,
1080 const char *filename,
1081 unsigned line,
1082 const char *section,
1083 unsigned section_line,
1084 const char *lvalue,
1085 int ltype,
1086 const char *rvalue,
1087 void *data,
1088 void *userdata) {
1089
1090 ExecContext *c = data;
1091 const Unit *u = userdata;
1092 const char *n;
1093 ExecInput ei;
1094 int r;
1095
1096 assert(data);
1097 assert(filename);
1098 assert(line);
1099 assert(rvalue);
1100
1101 n = startswith(rvalue, "fd:");
1102 if (n) {
1103 _cleanup_free_ char *resolved = NULL;
1104
1105 r = unit_fd_printf(u, n, &resolved);
1106 if (r < 0) {
1107 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", n);
1108 return 0;
1109 }
1110
1111 if (isempty(resolved))
1112 resolved = mfree(resolved);
1113 else if (!fdname_is_valid(resolved)) {
1114 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid file descriptor name, ignoring: %s", resolved);
1115 return 0;
1116 }
1117
1118 free_and_replace(c->stdio_fdname[STDIN_FILENO], resolved);
1119
1120 ei = EXEC_INPUT_NAMED_FD;
1121
1122 } else if ((n = startswith(rvalue, "file:"))) {
1123 _cleanup_free_ char *resolved = NULL;
1124
1125 r = unit_path_printf(u, n, &resolved);
1126 if (r < 0) {
1127 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", n);
1128 return 0;
1129 }
1130
1131 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE | PATH_CHECK_FATAL, unit, filename, line, lvalue);
1132 if (r < 0)
1133 return 0;
1134
1135 free_and_replace(c->stdio_file[STDIN_FILENO], resolved);
1136
1137 ei = EXEC_INPUT_FILE;
1138
1139 } else {
1140 ei = exec_input_from_string(rvalue);
1141 if (ei < 0) {
1142 log_syntax(unit, LOG_WARNING, filename, line, ei, "Failed to parse input specifier, ignoring: %s", rvalue);
1143 return 0;
1144 }
1145 }
1146
1147 c->std_input = ei;
1148 return 0;
1149 }
1150
1151 int config_parse_exec_input_text(
1152 const char *unit,
1153 const char *filename,
1154 unsigned line,
1155 const char *section,
1156 unsigned section_line,
1157 const char *lvalue,
1158 int ltype,
1159 const char *rvalue,
1160 void *data,
1161 void *userdata) {
1162
1163 _cleanup_free_ char *unescaped = NULL, *resolved = NULL;
1164 ExecContext *c = data;
1165 const Unit *u = userdata;
1166 int r;
1167
1168 assert(data);
1169 assert(filename);
1170 assert(line);
1171 assert(rvalue);
1172
1173 if (isempty(rvalue)) {
1174 /* Reset if the empty string is assigned */
1175 c->stdin_data = mfree(c->stdin_data);
1176 c->stdin_data_size = 0;
1177 return 0;
1178 }
1179
1180 ssize_t l = cunescape(rvalue, 0, &unescaped);
1181 if (l < 0) {
1182 log_syntax(unit, LOG_WARNING, filename, line, l,
1183 "Failed to decode C escaped text '%s', ignoring: %m", rvalue);
1184 return 0;
1185 }
1186
1187 r = unit_full_printf_full(u, unescaped, EXEC_STDIN_DATA_MAX, &resolved);
1188 if (r < 0) {
1189 log_syntax(unit, LOG_WARNING, filename, line, r,
1190 "Failed to resolve unit specifiers in '%s', ignoring: %m", unescaped);
1191 return 0;
1192 }
1193
1194 size_t sz = strlen(resolved);
1195 if (c->stdin_data_size + sz + 1 < c->stdin_data_size || /* check for overflow */
1196 c->stdin_data_size + sz + 1 > EXEC_STDIN_DATA_MAX) {
1197 log_syntax(unit, LOG_WARNING, filename, line, 0,
1198 "Standard input data too large (%zu), maximum of %zu permitted, ignoring.",
1199 c->stdin_data_size + sz, (size_t) EXEC_STDIN_DATA_MAX);
1200 return 0;
1201 }
1202
1203 void *p = realloc(c->stdin_data, c->stdin_data_size + sz + 1);
1204 if (!p)
1205 return log_oom();
1206
1207 *((char*) mempcpy((char*) p + c->stdin_data_size, resolved, sz)) = '\n';
1208
1209 c->stdin_data = p;
1210 c->stdin_data_size += sz + 1;
1211
1212 return 0;
1213 }
1214
1215 int config_parse_exec_input_data(
1216 const char *unit,
1217 const char *filename,
1218 unsigned line,
1219 const char *section,
1220 unsigned section_line,
1221 const char *lvalue,
1222 int ltype,
1223 const char *rvalue,
1224 void *data,
1225 void *userdata) {
1226
1227 _cleanup_free_ void *p = NULL;
1228 ExecContext *c = data;
1229 size_t sz;
1230 void *q;
1231 int r;
1232
1233 assert(data);
1234 assert(filename);
1235 assert(line);
1236 assert(rvalue);
1237
1238 if (isempty(rvalue)) {
1239 /* Reset if the empty string is assigned */
1240 c->stdin_data = mfree(c->stdin_data);
1241 c->stdin_data_size = 0;
1242 return 0;
1243 }
1244
1245 r = unbase64mem(rvalue, SIZE_MAX, &p, &sz);
1246 if (r < 0) {
1247 log_syntax(unit, LOG_WARNING, filename, line, r,
1248 "Failed to decode base64 data, ignoring: %s", rvalue);
1249 return 0;
1250 }
1251
1252 assert(sz > 0);
1253
1254 if (c->stdin_data_size + sz < c->stdin_data_size || /* check for overflow */
1255 c->stdin_data_size + sz > EXEC_STDIN_DATA_MAX) {
1256 log_syntax(unit, LOG_WARNING, filename, line, 0,
1257 "Standard input data too large (%zu), maximum of %zu permitted, ignoring.",
1258 c->stdin_data_size + sz, (size_t) EXEC_STDIN_DATA_MAX);
1259 return 0;
1260 }
1261
1262 q = realloc(c->stdin_data, c->stdin_data_size + sz);
1263 if (!q)
1264 return log_oom();
1265
1266 memcpy((uint8_t*) q + c->stdin_data_size, p, sz);
1267
1268 c->stdin_data = q;
1269 c->stdin_data_size += sz;
1270
1271 return 0;
1272 }
1273
1274 int config_parse_exec_output(
1275 const char *unit,
1276 const char *filename,
1277 unsigned line,
1278 const char *section,
1279 unsigned section_line,
1280 const char *lvalue,
1281 int ltype,
1282 const char *rvalue,
1283 void *data,
1284 void *userdata) {
1285
1286 _cleanup_free_ char *resolved = NULL;
1287 const char *n;
1288 ExecContext *c = data;
1289 const Unit *u = userdata;
1290 bool obsolete = false;
1291 ExecOutput eo;
1292 int r;
1293
1294 assert(data);
1295 assert(filename);
1296 assert(line);
1297 assert(lvalue);
1298 assert(rvalue);
1299
1300 n = startswith(rvalue, "fd:");
1301 if (n) {
1302 r = unit_fd_printf(u, n, &resolved);
1303 if (r < 0) {
1304 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in %s: %m", n);
1305 return 0;
1306 }
1307
1308 if (isempty(resolved))
1309 resolved = mfree(resolved);
1310 else if (!fdname_is_valid(resolved)) {
1311 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid file descriptor name, ignoring: %s", resolved);
1312 return 0;
1313 }
1314
1315 eo = EXEC_OUTPUT_NAMED_FD;
1316
1317 } else if (streq(rvalue, "syslog")) {
1318 eo = EXEC_OUTPUT_JOURNAL;
1319 obsolete = true;
1320
1321 } else if (streq(rvalue, "syslog+console")) {
1322 eo = EXEC_OUTPUT_JOURNAL_AND_CONSOLE;
1323 obsolete = true;
1324
1325 } else if ((n = startswith(rvalue, "file:"))) {
1326
1327 r = unit_path_printf(u, n, &resolved);
1328 if (r < 0) {
1329 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", n);
1330 return 0;
1331 }
1332
1333 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE | PATH_CHECK_FATAL, unit, filename, line, lvalue);
1334 if (r < 0)
1335 return 0;
1336
1337 eo = EXEC_OUTPUT_FILE;
1338
1339 } else if ((n = startswith(rvalue, "append:"))) {
1340
1341 r = unit_path_printf(u, n, &resolved);
1342 if (r < 0) {
1343 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", n);
1344 return 0;
1345 }
1346
1347 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE | PATH_CHECK_FATAL, unit, filename, line, lvalue);
1348 if (r < 0)
1349 return 0;
1350
1351 eo = EXEC_OUTPUT_FILE_APPEND;
1352
1353 } else if ((n = startswith(rvalue, "truncate:"))) {
1354
1355 r = unit_path_printf(u, n, &resolved);
1356 if (r < 0) {
1357 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", n);
1358 return 0;
1359 }
1360
1361 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE | PATH_CHECK_FATAL, unit, filename, line, lvalue);
1362 if (r < 0)
1363 return 0;
1364
1365 eo = EXEC_OUTPUT_FILE_TRUNCATE;
1366 } else {
1367 eo = exec_output_from_string(rvalue);
1368 if (eo < 0) {
1369 log_syntax(unit, LOG_WARNING, filename, line, eo, "Failed to parse output specifier, ignoring: %s", rvalue);
1370 return 0;
1371 }
1372 }
1373
1374 if (obsolete)
1375 log_syntax(unit, LOG_NOTICE, filename, line, 0,
1376 "Standard output type %s is obsolete, automatically updating to %s. Please update your unit file, and consider removing the setting altogether.",
1377 rvalue, exec_output_to_string(eo));
1378
1379 if (streq(lvalue, "StandardOutput")) {
1380 if (eo == EXEC_OUTPUT_NAMED_FD)
1381 free_and_replace(c->stdio_fdname[STDOUT_FILENO], resolved);
1382 else
1383 free_and_replace(c->stdio_file[STDOUT_FILENO], resolved);
1384
1385 c->std_output = eo;
1386
1387 } else {
1388 assert(streq(lvalue, "StandardError"));
1389
1390 if (eo == EXEC_OUTPUT_NAMED_FD)
1391 free_and_replace(c->stdio_fdname[STDERR_FILENO], resolved);
1392 else
1393 free_and_replace(c->stdio_file[STDERR_FILENO], resolved);
1394
1395 c->std_error = eo;
1396 }
1397
1398 return 0;
1399 }
1400
1401 int config_parse_exec_io_class(const char *unit,
1402 const char *filename,
1403 unsigned line,
1404 const char *section,
1405 unsigned section_line,
1406 const char *lvalue,
1407 int ltype,
1408 const char *rvalue,
1409 void *data,
1410 void *userdata) {
1411
1412 ExecContext *c = data;
1413 int x;
1414
1415 assert(filename);
1416 assert(lvalue);
1417 assert(rvalue);
1418 assert(data);
1419
1420 if (isempty(rvalue)) {
1421 c->ioprio_set = false;
1422 c->ioprio = IOPRIO_DEFAULT_CLASS_AND_PRIO;
1423 return 0;
1424 }
1425
1426 x = ioprio_class_from_string(rvalue);
1427 if (x < 0) {
1428 log_syntax(unit, LOG_WARNING, filename, line, x, "Failed to parse IO scheduling class, ignoring: %s", rvalue);
1429 return 0;
1430 }
1431
1432 c->ioprio = ioprio_normalize(ioprio_prio_value(x, ioprio_prio_data(c->ioprio)));
1433 c->ioprio_set = true;
1434
1435 return 0;
1436 }
1437
1438 int config_parse_exec_io_priority(const char *unit,
1439 const char *filename,
1440 unsigned line,
1441 const char *section,
1442 unsigned section_line,
1443 const char *lvalue,
1444 int ltype,
1445 const char *rvalue,
1446 void *data,
1447 void *userdata) {
1448
1449 ExecContext *c = data;
1450 int i, r;
1451
1452 assert(filename);
1453 assert(lvalue);
1454 assert(rvalue);
1455 assert(data);
1456
1457 if (isempty(rvalue)) {
1458 c->ioprio_set = false;
1459 c->ioprio = IOPRIO_DEFAULT_CLASS_AND_PRIO;
1460 return 0;
1461 }
1462
1463 r = ioprio_parse_priority(rvalue, &i);
1464 if (r < 0) {
1465 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse IO priority, ignoring: %s", rvalue);
1466 return 0;
1467 }
1468
1469 c->ioprio = ioprio_normalize(ioprio_prio_value(ioprio_prio_class(c->ioprio), i));
1470 c->ioprio_set = true;
1471
1472 return 0;
1473 }
1474
1475 int config_parse_exec_cpu_sched_policy(const char *unit,
1476 const char *filename,
1477 unsigned line,
1478 const char *section,
1479 unsigned section_line,
1480 const char *lvalue,
1481 int ltype,
1482 const char *rvalue,
1483 void *data,
1484 void *userdata) {
1485
1486 ExecContext *c = data;
1487 int x;
1488
1489 assert(filename);
1490 assert(lvalue);
1491 assert(rvalue);
1492 assert(data);
1493
1494 if (isempty(rvalue)) {
1495 c->cpu_sched_set = false;
1496 c->cpu_sched_policy = SCHED_OTHER;
1497 c->cpu_sched_priority = 0;
1498 return 0;
1499 }
1500
1501 x = sched_policy_from_string(rvalue);
1502 if (x < 0) {
1503 log_syntax(unit, LOG_WARNING, filename, line, x, "Failed to parse CPU scheduling policy, ignoring: %s", rvalue);
1504 return 0;
1505 }
1506
1507 c->cpu_sched_policy = x;
1508 /* Moving to or from real-time policy? We need to adjust the priority */
1509 c->cpu_sched_priority = CLAMP(c->cpu_sched_priority, sched_get_priority_min(x), sched_get_priority_max(x));
1510 c->cpu_sched_set = true;
1511
1512 return 0;
1513 }
1514
1515 int config_parse_exec_mount_apivfs(const char *unit,
1516 const char *filename,
1517 unsigned line,
1518 const char *section,
1519 unsigned section_line,
1520 const char *lvalue,
1521 int ltype,
1522 const char *rvalue,
1523 void *data,
1524 void *userdata) {
1525
1526 ExecContext *c = data;
1527 int k;
1528
1529 assert(filename);
1530 assert(lvalue);
1531 assert(rvalue);
1532 assert(data);
1533
1534 if (isempty(rvalue)) {
1535 c->mount_apivfs_set = false;
1536 c->mount_apivfs = false;
1537 return 0;
1538 }
1539
1540 k = parse_boolean(rvalue);
1541 if (k < 0) {
1542 log_syntax(unit, LOG_WARNING, filename, line, k,
1543 "Failed to parse boolean value, ignoring: %s",
1544 rvalue);
1545 return 0;
1546 }
1547
1548 c->mount_apivfs_set = true;
1549 c->mount_apivfs = k;
1550 return 0;
1551 }
1552
1553 int config_parse_numa_mask(const char *unit,
1554 const char *filename,
1555 unsigned line,
1556 const char *section,
1557 unsigned section_line,
1558 const char *lvalue,
1559 int ltype,
1560 const char *rvalue,
1561 void *data,
1562 void *userdata) {
1563 int r;
1564 NUMAPolicy *p = data;
1565
1566 assert(filename);
1567 assert(lvalue);
1568 assert(rvalue);
1569 assert(data);
1570
1571 if (streq(rvalue, "all")) {
1572 r = numa_mask_add_all(&p->nodes);
1573 if (r < 0)
1574 log_syntax(unit, LOG_WARNING, filename, line, r,
1575 "Failed to create NUMA mask representing \"all\" NUMA nodes, ignoring: %m");
1576 } else {
1577 r = parse_cpu_set_extend(rvalue, &p->nodes, true, unit, filename, line, lvalue);
1578 if (r < 0)
1579 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse NUMA node mask, ignoring: %s", rvalue);
1580 }
1581
1582 return 0;
1583 }
1584
1585 int config_parse_exec_cpu_sched_prio(const char *unit,
1586 const char *filename,
1587 unsigned line,
1588 const char *section,
1589 unsigned section_line,
1590 const char *lvalue,
1591 int ltype,
1592 const char *rvalue,
1593 void *data,
1594 void *userdata) {
1595
1596 ExecContext *c = data;
1597 int i, min, max, r;
1598
1599 assert(filename);
1600 assert(lvalue);
1601 assert(rvalue);
1602 assert(data);
1603
1604 r = safe_atoi(rvalue, &i);
1605 if (r < 0) {
1606 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse CPU scheduling priority, ignoring: %s", rvalue);
1607 return 0;
1608 }
1609
1610 /* On Linux RR/FIFO range from 1 to 99 and OTHER/BATCH may only be 0 */
1611 min = sched_get_priority_min(c->cpu_sched_policy);
1612 max = sched_get_priority_max(c->cpu_sched_policy);
1613
1614 if (i < min || i > max) {
1615 log_syntax(unit, LOG_WARNING, filename, line, 0, "CPU scheduling priority is out of range, ignoring: %s", rvalue);
1616 return 0;
1617 }
1618
1619 c->cpu_sched_priority = i;
1620 c->cpu_sched_set = true;
1621
1622 return 0;
1623 }
1624
1625 int config_parse_root_image_options(
1626 const char *unit,
1627 const char *filename,
1628 unsigned line,
1629 const char *section,
1630 unsigned section_line,
1631 const char *lvalue,
1632 int ltype,
1633 const char *rvalue,
1634 void *data,
1635 void *userdata) {
1636
1637 _cleanup_(mount_options_free_allp) MountOptions *options = NULL;
1638 _cleanup_strv_free_ char **l = NULL;
1639 ExecContext *c = data;
1640 const Unit *u = userdata;
1641 int r;
1642
1643 assert(filename);
1644 assert(lvalue);
1645 assert(rvalue);
1646 assert(data);
1647
1648 if (isempty(rvalue)) {
1649 c->root_image_options = mount_options_free_all(c->root_image_options);
1650 return 0;
1651 }
1652
1653 r = strv_split_colon_pairs(&l, rvalue);
1654 if (r == -ENOMEM)
1655 return log_oom();
1656 if (r < 0) {
1657 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse %s, ignoring: %s", lvalue, rvalue);
1658 return 0;
1659 }
1660
1661 STRV_FOREACH_PAIR(first, second, l) {
1662 MountOptions *o = NULL;
1663 _cleanup_free_ char *mount_options_resolved = NULL;
1664 const char *mount_options = NULL, *partition = "root";
1665 PartitionDesignator partition_designator;
1666
1667 /* Format is either 'root:foo' or 'foo' (root is implied) */
1668 if (!isempty(*second)) {
1669 partition = *first;
1670 mount_options = *second;
1671 } else
1672 mount_options = *first;
1673
1674 partition_designator = partition_designator_from_string(partition);
1675 if (partition_designator < 0) {
1676 log_syntax(unit, LOG_WARNING, filename, line, partition_designator,
1677 "Invalid partition name %s, ignoring", partition);
1678 continue;
1679 }
1680 r = unit_full_printf(u, mount_options, &mount_options_resolved);
1681 if (r < 0) {
1682 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", mount_options);
1683 continue;
1684 }
1685
1686 o = new(MountOptions, 1);
1687 if (!o)
1688 return log_oom();
1689 *o = (MountOptions) {
1690 .partition_designator = partition_designator,
1691 .options = TAKE_PTR(mount_options_resolved),
1692 };
1693 LIST_APPEND(mount_options, options, TAKE_PTR(o));
1694 }
1695
1696 if (options)
1697 LIST_JOIN(mount_options, c->root_image_options, options);
1698 else
1699 /* empty spaces/separators only */
1700 c->root_image_options = mount_options_free_all(c->root_image_options);
1701
1702 return 0;
1703 }
1704
1705 int config_parse_exec_root_hash(
1706 const char *unit,
1707 const char *filename,
1708 unsigned line,
1709 const char *section,
1710 unsigned section_line,
1711 const char *lvalue,
1712 int ltype,
1713 const char *rvalue,
1714 void *data,
1715 void *userdata) {
1716
1717 _cleanup_free_ void *roothash_decoded = NULL;
1718 ExecContext *c = data;
1719 size_t roothash_decoded_size = 0;
1720 int r;
1721
1722 assert(data);
1723 assert(filename);
1724 assert(line);
1725 assert(rvalue);
1726
1727 if (isempty(rvalue)) {
1728 /* Reset if the empty string is assigned */
1729 c->root_hash_path = mfree(c->root_hash_path);
1730 c->root_hash = mfree(c->root_hash);
1731 c->root_hash_size = 0;
1732 return 0;
1733 }
1734
1735 if (path_is_absolute(rvalue)) {
1736 /* We have the path to a roothash to load and decode, eg: RootHash=/foo/bar.roothash */
1737 _cleanup_free_ char *p = NULL;
1738
1739 p = strdup(rvalue);
1740 if (!p)
1741 return -ENOMEM;
1742
1743 free_and_replace(c->root_hash_path, p);
1744 c->root_hash = mfree(c->root_hash);
1745 c->root_hash_size = 0;
1746 return 0;
1747 }
1748
1749 /* We have a roothash to decode, eg: RootHash=012345789abcdef */
1750 r = unhexmem(rvalue, strlen(rvalue), &roothash_decoded, &roothash_decoded_size);
1751 if (r < 0) {
1752 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to decode RootHash=, ignoring: %s", rvalue);
1753 return 0;
1754 }
1755 if (roothash_decoded_size < sizeof(sd_id128_t)) {
1756 log_syntax(unit, LOG_WARNING, filename, line, 0, "RootHash= is too short, ignoring: %s", rvalue);
1757 return 0;
1758 }
1759
1760 free_and_replace(c->root_hash, roothash_decoded);
1761 c->root_hash_size = roothash_decoded_size;
1762 c->root_hash_path = mfree(c->root_hash_path);
1763
1764 return 0;
1765 }
1766
1767 int config_parse_exec_root_hash_sig(
1768 const char *unit,
1769 const char *filename,
1770 unsigned line,
1771 const char *section,
1772 unsigned section_line,
1773 const char *lvalue,
1774 int ltype,
1775 const char *rvalue,
1776 void *data,
1777 void *userdata) {
1778
1779 _cleanup_free_ void *roothash_sig_decoded = NULL;
1780 char *value;
1781 ExecContext *c = data;
1782 size_t roothash_sig_decoded_size = 0;
1783 int r;
1784
1785 assert(data);
1786 assert(filename);
1787 assert(line);
1788 assert(rvalue);
1789
1790 if (isempty(rvalue)) {
1791 /* Reset if the empty string is assigned */
1792 c->root_hash_sig_path = mfree(c->root_hash_sig_path);
1793 c->root_hash_sig = mfree(c->root_hash_sig);
1794 c->root_hash_sig_size = 0;
1795 return 0;
1796 }
1797
1798 if (path_is_absolute(rvalue)) {
1799 /* We have the path to a roothash signature to load and decode, eg: RootHashSignature=/foo/bar.roothash.p7s */
1800 _cleanup_free_ char *p = NULL;
1801
1802 p = strdup(rvalue);
1803 if (!p)
1804 return log_oom();
1805
1806 free_and_replace(c->root_hash_sig_path, p);
1807 c->root_hash_sig = mfree(c->root_hash_sig);
1808 c->root_hash_sig_size = 0;
1809 return 0;
1810 }
1811
1812 if (!(value = startswith(rvalue, "base64:"))) {
1813 log_syntax(unit, LOG_WARNING, filename, line, 0,
1814 "Failed to decode RootHashSignature=, not a path but doesn't start with 'base64:', ignoring: %s", rvalue);
1815 return 0;
1816 }
1817
1818 /* We have a roothash signature to decode, eg: RootHashSignature=base64:012345789abcdef */
1819 r = unbase64mem(value, strlen(value), &roothash_sig_decoded, &roothash_sig_decoded_size);
1820 if (r < 0) {
1821 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to decode RootHashSignature=, ignoring: %s", rvalue);
1822 return 0;
1823 }
1824
1825 free_and_replace(c->root_hash_sig, roothash_sig_decoded);
1826 c->root_hash_sig_size = roothash_sig_decoded_size;
1827 c->root_hash_sig_path = mfree(c->root_hash_sig_path);
1828
1829 return 0;
1830 }
1831
1832 int config_parse_exec_cpu_affinity(
1833 const char *unit,
1834 const char *filename,
1835 unsigned line,
1836 const char *section,
1837 unsigned section_line,
1838 const char *lvalue,
1839 int ltype,
1840 const char *rvalue,
1841 void *data,
1842 void *userdata) {
1843
1844 ExecContext *c = data;
1845 const Unit *u = userdata;
1846 _cleanup_free_ char *k = NULL;
1847 int r;
1848
1849 assert(filename);
1850 assert(lvalue);
1851 assert(rvalue);
1852 assert(data);
1853
1854 if (streq(rvalue, "numa")) {
1855 c->cpu_affinity_from_numa = true;
1856 cpu_set_reset(&c->cpu_set);
1857
1858 return 0;
1859 }
1860
1861 r = unit_full_printf(u, rvalue, &k);
1862 if (r < 0) {
1863 log_syntax(unit, LOG_WARNING, filename, line, r,
1864 "Failed to resolve unit specifiers in '%s', ignoring: %m",
1865 rvalue);
1866 return 0;
1867 }
1868
1869 r = parse_cpu_set_extend(k, &c->cpu_set, true, unit, filename, line, lvalue);
1870 if (r >= 0)
1871 c->cpu_affinity_from_numa = false;
1872
1873 return 0;
1874 }
1875
1876 int config_parse_capability_set(
1877 const char *unit,
1878 const char *filename,
1879 unsigned line,
1880 const char *section,
1881 unsigned section_line,
1882 const char *lvalue,
1883 int ltype,
1884 const char *rvalue,
1885 void *data,
1886 void *userdata) {
1887
1888 uint64_t *capability_set = data;
1889 uint64_t sum = 0, initial = 0;
1890 bool invert = false;
1891 int r;
1892
1893 assert(filename);
1894 assert(lvalue);
1895 assert(rvalue);
1896 assert(data);
1897
1898 if (rvalue[0] == '~') {
1899 invert = true;
1900 rvalue++;
1901 }
1902
1903 if (streq(lvalue, "CapabilityBoundingSet"))
1904 initial = CAP_ALL; /* initialized to all bits on */
1905 /* else "AmbientCapabilities" initialized to all bits off */
1906
1907 r = capability_set_from_string(rvalue, &sum);
1908 if (r < 0) {
1909 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse %s= specifier '%s', ignoring: %m", lvalue, rvalue);
1910 return 0;
1911 }
1912
1913 if (sum == 0 || *capability_set == initial)
1914 /* "", "~" or uninitialized data -> replace */
1915 *capability_set = invert ? ~sum : sum;
1916 else {
1917 /* previous data -> merge */
1918 if (invert)
1919 *capability_set &= ~sum;
1920 else
1921 *capability_set |= sum;
1922 }
1923
1924 return 0;
1925 }
1926
1927 int config_parse_exec_selinux_context(
1928 const char *unit,
1929 const char *filename,
1930 unsigned line,
1931 const char *section,
1932 unsigned section_line,
1933 const char *lvalue,
1934 int ltype,
1935 const char *rvalue,
1936 void *data,
1937 void *userdata) {
1938
1939 ExecContext *c = data;
1940 const Unit *u = userdata;
1941 bool ignore;
1942 char *k;
1943 int r;
1944
1945 assert(filename);
1946 assert(lvalue);
1947 assert(rvalue);
1948 assert(data);
1949
1950 if (isempty(rvalue)) {
1951 c->selinux_context = mfree(c->selinux_context);
1952 c->selinux_context_ignore = false;
1953 return 0;
1954 }
1955
1956 if (rvalue[0] == '-') {
1957 ignore = true;
1958 rvalue++;
1959 } else
1960 ignore = false;
1961
1962 r = unit_full_printf(u, rvalue, &k);
1963 if (r < 0) {
1964 log_syntax(unit, ignore ? LOG_WARNING : LOG_ERR, filename, line, r,
1965 "Failed to resolve unit specifiers in '%s'%s: %m",
1966 rvalue, ignore ? ", ignoring" : "");
1967 return ignore ? 0 : -ENOEXEC;
1968 }
1969
1970 free_and_replace(c->selinux_context, k);
1971 c->selinux_context_ignore = ignore;
1972
1973 return 0;
1974 }
1975
1976 int config_parse_exec_apparmor_profile(
1977 const char *unit,
1978 const char *filename,
1979 unsigned line,
1980 const char *section,
1981 unsigned section_line,
1982 const char *lvalue,
1983 int ltype,
1984 const char *rvalue,
1985 void *data,
1986 void *userdata) {
1987
1988 ExecContext *c = data;
1989 const Unit *u = userdata;
1990 bool ignore;
1991 char *k;
1992 int r;
1993
1994 assert(filename);
1995 assert(lvalue);
1996 assert(rvalue);
1997 assert(data);
1998
1999 if (isempty(rvalue)) {
2000 c->apparmor_profile = mfree(c->apparmor_profile);
2001 c->apparmor_profile_ignore = false;
2002 return 0;
2003 }
2004
2005 if (rvalue[0] == '-') {
2006 ignore = true;
2007 rvalue++;
2008 } else
2009 ignore = false;
2010
2011 r = unit_full_printf(u, rvalue, &k);
2012 if (r < 0) {
2013 log_syntax(unit, ignore ? LOG_WARNING : LOG_ERR, filename, line, r,
2014 "Failed to resolve unit specifiers in '%s'%s: %m",
2015 rvalue, ignore ? ", ignoring" : "");
2016 return ignore ? 0 : -ENOEXEC;
2017 }
2018
2019 free_and_replace(c->apparmor_profile, k);
2020 c->apparmor_profile_ignore = ignore;
2021
2022 return 0;
2023 }
2024
2025 int config_parse_exec_smack_process_label(
2026 const char *unit,
2027 const char *filename,
2028 unsigned line,
2029 const char *section,
2030 unsigned section_line,
2031 const char *lvalue,
2032 int ltype,
2033 const char *rvalue,
2034 void *data,
2035 void *userdata) {
2036
2037 ExecContext *c = data;
2038 const Unit *u = userdata;
2039 bool ignore;
2040 char *k;
2041 int r;
2042
2043 assert(filename);
2044 assert(lvalue);
2045 assert(rvalue);
2046 assert(data);
2047
2048 if (isempty(rvalue)) {
2049 c->smack_process_label = mfree(c->smack_process_label);
2050 c->smack_process_label_ignore = false;
2051 return 0;
2052 }
2053
2054 if (rvalue[0] == '-') {
2055 ignore = true;
2056 rvalue++;
2057 } else
2058 ignore = false;
2059
2060 r = unit_full_printf(u, rvalue, &k);
2061 if (r < 0) {
2062 log_syntax(unit, ignore ? LOG_WARNING : LOG_ERR, filename, line, r,
2063 "Failed to resolve unit specifiers in '%s'%s: %m",
2064 rvalue, ignore ? ", ignoring" : "");
2065 return ignore ? 0 : -ENOEXEC;
2066 }
2067
2068 free_and_replace(c->smack_process_label, k);
2069 c->smack_process_label_ignore = ignore;
2070
2071 return 0;
2072 }
2073
2074 int config_parse_timer(
2075 const char *unit,
2076 const char *filename,
2077 unsigned line,
2078 const char *section,
2079 unsigned section_line,
2080 const char *lvalue,
2081 int ltype,
2082 const char *rvalue,
2083 void *data,
2084 void *userdata) {
2085
2086 _cleanup_(calendar_spec_freep) CalendarSpec *c = NULL;
2087 _cleanup_free_ char *k = NULL;
2088 const Unit *u = userdata;
2089 Timer *t = data;
2090 usec_t usec = 0;
2091 TimerValue *v;
2092 int r;
2093
2094 assert(filename);
2095 assert(lvalue);
2096 assert(rvalue);
2097 assert(data);
2098
2099 if (isempty(rvalue)) {
2100 /* Empty assignment resets list */
2101 timer_free_values(t);
2102 return 0;
2103 }
2104
2105 r = unit_full_printf(u, rvalue, &k);
2106 if (r < 0) {
2107 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
2108 return 0;
2109 }
2110
2111 if (ltype == TIMER_CALENDAR) {
2112 r = calendar_spec_from_string(k, &c);
2113 if (r < 0) {
2114 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse calendar specification, ignoring: %s", k);
2115 return 0;
2116 }
2117 } else {
2118 r = parse_sec(k, &usec);
2119 if (r < 0) {
2120 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse timer value, ignoring: %s", k);
2121 return 0;
2122 }
2123 }
2124
2125 v = new(TimerValue, 1);
2126 if (!v)
2127 return log_oom();
2128
2129 *v = (TimerValue) {
2130 .base = ltype,
2131 .value = usec,
2132 .calendar_spec = TAKE_PTR(c),
2133 };
2134
2135 LIST_PREPEND(value, t->values, v);
2136
2137 return 0;
2138 }
2139
2140 int config_parse_trigger_unit(
2141 const char *unit,
2142 const char *filename,
2143 unsigned line,
2144 const char *section,
2145 unsigned section_line,
2146 const char *lvalue,
2147 int ltype,
2148 const char *rvalue,
2149 void *data,
2150 void *userdata) {
2151
2152 _cleanup_free_ char *p = NULL;
2153 Unit *u = data;
2154 UnitType type;
2155 int r;
2156
2157 assert(filename);
2158 assert(lvalue);
2159 assert(rvalue);
2160 assert(data);
2161
2162 if (UNIT_TRIGGER(u)) {
2163 log_syntax(unit, LOG_WARNING, filename, line, 0, "Multiple units to trigger specified, ignoring: %s", rvalue);
2164 return 0;
2165 }
2166
2167 r = unit_name_printf(u, rvalue, &p);
2168 if (r < 0) {
2169 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue);
2170 return 0;
2171 }
2172
2173 type = unit_name_to_type(p);
2174 if (type < 0) {
2175 log_syntax(unit, LOG_WARNING, filename, line, type, "Unit type not valid, ignoring: %s", rvalue);
2176 return 0;
2177 }
2178 if (unit_has_name(u, p)) {
2179 log_syntax(unit, LOG_WARNING, filename, line, 0, "Units cannot trigger themselves, ignoring: %s", rvalue);
2180 return 0;
2181 }
2182
2183 r = unit_add_two_dependencies_by_name(u, UNIT_BEFORE, UNIT_TRIGGERS, p, true, UNIT_DEPENDENCY_FILE);
2184 if (r < 0) {
2185 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to add trigger on %s, ignoring: %m", p);
2186 return 0;
2187 }
2188
2189 return 0;
2190 }
2191
2192 int config_parse_path_spec(const char *unit,
2193 const char *filename,
2194 unsigned line,
2195 const char *section,
2196 unsigned section_line,
2197 const char *lvalue,
2198 int ltype,
2199 const char *rvalue,
2200 void *data,
2201 void *userdata) {
2202
2203 Path *p = data;
2204 PathSpec *s;
2205 PathType b;
2206 _cleanup_free_ char *k = NULL;
2207 int r;
2208
2209 assert(filename);
2210 assert(lvalue);
2211 assert(rvalue);
2212 assert(data);
2213
2214 if (isempty(rvalue)) {
2215 /* Empty assignment clears list */
2216 path_free_specs(p);
2217 return 0;
2218 }
2219
2220 b = path_type_from_string(lvalue);
2221 if (b < 0) {
2222 log_syntax(unit, LOG_WARNING, filename, line, b, "Failed to parse path type, ignoring: %s", lvalue);
2223 return 0;
2224 }
2225
2226 r = unit_path_printf(UNIT(p), rvalue, &k);
2227 if (r < 0) {
2228 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue);
2229 return 0;
2230 }
2231
2232 r = path_simplify_and_warn(k, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
2233 if (r < 0)
2234 return 0;
2235
2236 s = new0(PathSpec, 1);
2237 if (!s)
2238 return log_oom();
2239
2240 s->unit = UNIT(p);
2241 s->path = TAKE_PTR(k);
2242 s->type = b;
2243 s->inotify_fd = -1;
2244
2245 LIST_PREPEND(spec, p->specs, s);
2246
2247 return 0;
2248 }
2249
2250 int config_parse_socket_service(
2251 const char *unit,
2252 const char *filename,
2253 unsigned line,
2254 const char *section,
2255 unsigned section_line,
2256 const char *lvalue,
2257 int ltype,
2258 const char *rvalue,
2259 void *data,
2260 void *userdata) {
2261
2262 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2263 _cleanup_free_ char *p = NULL;
2264 Socket *s = data;
2265 Unit *x;
2266 int r;
2267
2268 assert(filename);
2269 assert(lvalue);
2270 assert(rvalue);
2271 assert(data);
2272
2273 r = unit_name_printf(UNIT(s), rvalue, &p);
2274 if (r < 0) {
2275 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue);
2276 return 0;
2277 }
2278
2279 if (!endswith(p, ".service")) {
2280 log_syntax(unit, LOG_WARNING, filename, line, 0, "Unit must be of type service, ignoring: %s", rvalue);
2281 return 0;
2282 }
2283
2284 r = manager_load_unit(UNIT(s)->manager, p, NULL, &error, &x);
2285 if (r < 0) {
2286 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to load unit %s, ignoring: %s", rvalue, bus_error_message(&error, r));
2287 return 0;
2288 }
2289
2290 unit_ref_set(&s->service, UNIT(s), x);
2291
2292 return 0;
2293 }
2294
2295 int config_parse_fdname(
2296 const char *unit,
2297 const char *filename,
2298 unsigned line,
2299 const char *section,
2300 unsigned section_line,
2301 const char *lvalue,
2302 int ltype,
2303 const char *rvalue,
2304 void *data,
2305 void *userdata) {
2306
2307 _cleanup_free_ char *p = NULL;
2308 Socket *s = data;
2309 int r;
2310
2311 assert(filename);
2312 assert(lvalue);
2313 assert(rvalue);
2314 assert(data);
2315
2316 if (isempty(rvalue)) {
2317 s->fdname = mfree(s->fdname);
2318 return 0;
2319 }
2320
2321 r = unit_fd_printf(UNIT(s), rvalue, &p);
2322 if (r < 0) {
2323 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
2324 return 0;
2325 }
2326
2327 if (!fdname_is_valid(p)) {
2328 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid file descriptor name, ignoring: %s", p);
2329 return 0;
2330 }
2331
2332 return free_and_replace(s->fdname, p);
2333 }
2334
2335 int config_parse_service_sockets(
2336 const char *unit,
2337 const char *filename,
2338 unsigned line,
2339 const char *section,
2340 unsigned section_line,
2341 const char *lvalue,
2342 int ltype,
2343 const char *rvalue,
2344 void *data,
2345 void *userdata) {
2346
2347 Service *s = data;
2348 int r;
2349
2350 assert(filename);
2351 assert(lvalue);
2352 assert(rvalue);
2353 assert(data);
2354
2355 for (const char *p = rvalue;;) {
2356 _cleanup_free_ char *word = NULL, *k = NULL;
2357
2358 r = extract_first_word(&p, &word, NULL, 0);
2359 if (r == -ENOMEM)
2360 return log_oom();
2361 if (r < 0) {
2362 log_syntax(unit, LOG_WARNING, filename, line, r, "Trailing garbage in sockets, ignoring: %s", rvalue);
2363 return 0;
2364 }
2365 if (r == 0)
2366 return 0;
2367
2368 r = unit_name_printf(UNIT(s), word, &k);
2369 if (r < 0) {
2370 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", word);
2371 continue;
2372 }
2373
2374 if (!endswith(k, ".socket")) {
2375 log_syntax(unit, LOG_WARNING, filename, line, 0, "Unit must be of type socket, ignoring: %s", k);
2376 continue;
2377 }
2378
2379 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_WANTS, UNIT_AFTER, k, true, UNIT_DEPENDENCY_FILE);
2380 if (r < 0)
2381 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to add dependency on %s, ignoring: %m", k);
2382
2383 r = unit_add_dependency_by_name(UNIT(s), UNIT_TRIGGERED_BY, k, true, UNIT_DEPENDENCY_FILE);
2384 if (r < 0)
2385 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to add dependency on %s, ignoring: %m", k);
2386 }
2387 }
2388
2389 int config_parse_bus_name(
2390 const char *unit,
2391 const char *filename,
2392 unsigned line,
2393 const char *section,
2394 unsigned section_line,
2395 const char *lvalue,
2396 int ltype,
2397 const char *rvalue,
2398 void *data,
2399 void *userdata) {
2400
2401 _cleanup_free_ char *k = NULL;
2402 const Unit *u = userdata;
2403 int r;
2404
2405 assert(filename);
2406 assert(lvalue);
2407 assert(rvalue);
2408 assert(u);
2409
2410 r = unit_full_printf_full(u, rvalue, SD_BUS_MAXIMUM_NAME_LENGTH, &k);
2411 if (r < 0) {
2412 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue);
2413 return 0;
2414 }
2415
2416 if (!sd_bus_service_name_is_valid(k)) {
2417 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid bus name, ignoring: %s", k);
2418 return 0;
2419 }
2420
2421 return config_parse_string(unit, filename, line, section, section_line, lvalue, ltype, k, data, userdata);
2422 }
2423
2424 int config_parse_service_timeout(
2425 const char *unit,
2426 const char *filename,
2427 unsigned line,
2428 const char *section,
2429 unsigned section_line,
2430 const char *lvalue,
2431 int ltype,
2432 const char *rvalue,
2433 void *data,
2434 void *userdata) {
2435
2436 Service *s = userdata;
2437 usec_t usec;
2438 int r;
2439
2440 assert(filename);
2441 assert(lvalue);
2442 assert(rvalue);
2443 assert(s);
2444
2445 /* This is called for two cases: TimeoutSec= and TimeoutStartSec=. */
2446
2447 /* Traditionally, these options accepted 0 to disable the timeouts. However, a timeout of 0 suggests it happens
2448 * immediately, hence fix this to become USEC_INFINITY instead. This is in-line with how we internally handle
2449 * all other timeouts. */
2450 r = parse_sec_fix_0(rvalue, &usec);
2451 if (r < 0) {
2452 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse %s= parameter, ignoring: %s", lvalue, rvalue);
2453 return 0;
2454 }
2455
2456 s->start_timeout_defined = true;
2457 s->timeout_start_usec = usec;
2458
2459 if (streq(lvalue, "TimeoutSec"))
2460 s->timeout_stop_usec = usec;
2461
2462 return 0;
2463 }
2464
2465 int config_parse_timeout_abort(
2466 const char *unit,
2467 const char *filename,
2468 unsigned line,
2469 const char *section,
2470 unsigned section_line,
2471 const char *lvalue,
2472 int ltype,
2473 const char *rvalue,
2474 void *data,
2475 void *userdata) {
2476
2477 usec_t *ret = data;
2478 int r;
2479
2480 assert(filename);
2481 assert(lvalue);
2482 assert(rvalue);
2483 assert(ret);
2484
2485 /* Note: apart from setting the arg, this returns an extra bit of information in the return value. */
2486
2487 if (isempty(rvalue)) {
2488 *ret = 0;
2489 return 0; /* "not set" */
2490 }
2491
2492 r = parse_sec(rvalue, ret);
2493 if (r < 0)
2494 return log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse %s= setting, ignoring: %s", lvalue, rvalue);
2495
2496 return 1; /* "set" */
2497 }
2498
2499 int config_parse_service_timeout_abort(
2500 const char *unit,
2501 const char *filename,
2502 unsigned line,
2503 const char *section,
2504 unsigned section_line,
2505 const char *lvalue,
2506 int ltype,
2507 const char *rvalue,
2508 void *data,
2509 void *userdata) {
2510
2511 Service *s = userdata;
2512 int r;
2513
2514 assert(s);
2515
2516 r = config_parse_timeout_abort(unit, filename, line, section, section_line, lvalue, ltype, rvalue,
2517 &s->timeout_abort_usec, s);
2518 if (r >= 0)
2519 s->timeout_abort_set = r;
2520 return 0;
2521 }
2522
2523 int config_parse_user_group_compat(
2524 const char *unit,
2525 const char *filename,
2526 unsigned line,
2527 const char *section,
2528 unsigned section_line,
2529 const char *lvalue,
2530 int ltype,
2531 const char *rvalue,
2532 void *data,
2533 void *userdata) {
2534
2535 _cleanup_free_ char *k = NULL;
2536 char **user = data;
2537 const Unit *u = userdata;
2538 int r;
2539
2540 assert(filename);
2541 assert(lvalue);
2542 assert(rvalue);
2543 assert(u);
2544
2545 if (isempty(rvalue)) {
2546 *user = mfree(*user);
2547 return 0;
2548 }
2549
2550 r = unit_full_printf(u, rvalue, &k);
2551 if (r < 0) {
2552 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s: %m", rvalue);
2553 return -ENOEXEC;
2554 }
2555
2556 if (!valid_user_group_name(k, VALID_USER_ALLOW_NUMERIC|VALID_USER_RELAX|VALID_USER_WARN)) {
2557 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid user/group name or numeric ID: %s", k);
2558 return -ENOEXEC;
2559 }
2560
2561 if (strstr(lvalue, "User") && streq(k, NOBODY_USER_NAME))
2562 log_struct(LOG_NOTICE,
2563 "MESSAGE=%s:%u: Special user %s configured, this is not safe!", filename, line, k,
2564 "UNIT=%s", unit,
2565 "MESSAGE_ID=" SD_MESSAGE_NOBODY_USER_UNSUITABLE_STR,
2566 "OFFENDING_USER=%s", k,
2567 "CONFIG_FILE=%s", filename,
2568 "CONFIG_LINE=%u", line);
2569
2570 return free_and_replace(*user, k);
2571 }
2572
2573 int config_parse_user_group_strv_compat(
2574 const char *unit,
2575 const char *filename,
2576 unsigned line,
2577 const char *section,
2578 unsigned section_line,
2579 const char *lvalue,
2580 int ltype,
2581 const char *rvalue,
2582 void *data,
2583 void *userdata) {
2584
2585 char ***users = data;
2586 const Unit *u = userdata;
2587 int r;
2588
2589 assert(filename);
2590 assert(lvalue);
2591 assert(rvalue);
2592 assert(u);
2593
2594 if (isempty(rvalue)) {
2595 *users = strv_free(*users);
2596 return 0;
2597 }
2598
2599 for (const char *p = rvalue;;) {
2600 _cleanup_free_ char *word = NULL, *k = NULL;
2601
2602 r = extract_first_word(&p, &word, NULL, 0);
2603 if (r == -ENOMEM)
2604 return log_oom();
2605 if (r < 0) {
2606 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid syntax: %s", rvalue);
2607 return -ENOEXEC;
2608 }
2609 if (r == 0)
2610 return 0;
2611
2612 r = unit_full_printf(u, word, &k);
2613 if (r < 0) {
2614 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s: %m", word);
2615 return -ENOEXEC;
2616 }
2617
2618 if (!valid_user_group_name(k, VALID_USER_ALLOW_NUMERIC|VALID_USER_RELAX|VALID_USER_WARN)) {
2619 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid user/group name or numeric ID: %s", k);
2620 return -ENOEXEC;
2621 }
2622
2623 r = strv_push(users, k);
2624 if (r < 0)
2625 return log_oom();
2626
2627 k = NULL;
2628 }
2629 }
2630
2631 int config_parse_working_directory(
2632 const char *unit,
2633 const char *filename,
2634 unsigned line,
2635 const char *section,
2636 unsigned section_line,
2637 const char *lvalue,
2638 int ltype,
2639 const char *rvalue,
2640 void *data,
2641 void *userdata) {
2642
2643 ExecContext *c = data;
2644 const Unit *u = userdata;
2645 bool missing_ok;
2646 int r;
2647
2648 assert(filename);
2649 assert(lvalue);
2650 assert(rvalue);
2651 assert(c);
2652 assert(u);
2653
2654 if (isempty(rvalue)) {
2655 c->working_directory_home = false;
2656 c->working_directory = mfree(c->working_directory);
2657 return 0;
2658 }
2659
2660 if (rvalue[0] == '-') {
2661 missing_ok = true;
2662 rvalue++;
2663 } else
2664 missing_ok = false;
2665
2666 if (streq(rvalue, "~")) {
2667 c->working_directory_home = true;
2668 c->working_directory = mfree(c->working_directory);
2669 } else {
2670 _cleanup_free_ char *k = NULL;
2671
2672 r = unit_path_printf(u, rvalue, &k);
2673 if (r < 0) {
2674 log_syntax(unit, missing_ok ? LOG_WARNING : LOG_ERR, filename, line, r,
2675 "Failed to resolve unit specifiers in working directory path '%s'%s: %m",
2676 rvalue, missing_ok ? ", ignoring" : "");
2677 return missing_ok ? 0 : -ENOEXEC;
2678 }
2679
2680 r = path_simplify_and_warn(k, PATH_CHECK_ABSOLUTE | (missing_ok ? 0 : PATH_CHECK_FATAL), unit, filename, line, lvalue);
2681 if (r < 0)
2682 return missing_ok ? 0 : -ENOEXEC;
2683
2684 c->working_directory_home = false;
2685 free_and_replace(c->working_directory, k);
2686 }
2687
2688 c->working_directory_missing_ok = missing_ok;
2689 return 0;
2690 }
2691
2692 int config_parse_unit_env_file(const char *unit,
2693 const char *filename,
2694 unsigned line,
2695 const char *section,
2696 unsigned section_line,
2697 const char *lvalue,
2698 int ltype,
2699 const char *rvalue,
2700 void *data,
2701 void *userdata) {
2702
2703 char ***env = data;
2704 const Unit *u = userdata;
2705 _cleanup_free_ char *n = NULL;
2706 int r;
2707
2708 assert(filename);
2709 assert(lvalue);
2710 assert(rvalue);
2711 assert(data);
2712
2713 if (isempty(rvalue)) {
2714 /* Empty assignment frees the list */
2715 *env = strv_free(*env);
2716 return 0;
2717 }
2718
2719 r = unit_full_printf_full(u, rvalue, PATH_MAX, &n);
2720 if (r < 0) {
2721 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
2722 return 0;
2723 }
2724
2725 r = path_simplify_and_warn(n[0] == '-' ? n + 1 : n, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
2726 if (r < 0)
2727 return 0;
2728
2729 r = strv_push(env, n);
2730 if (r < 0)
2731 return log_oom();
2732
2733 n = NULL;
2734
2735 return 0;
2736 }
2737
2738 int config_parse_environ(
2739 const char *unit,
2740 const char *filename,
2741 unsigned line,
2742 const char *section,
2743 unsigned section_line,
2744 const char *lvalue,
2745 int ltype,
2746 const char *rvalue,
2747 void *data,
2748 void *userdata) {
2749
2750 const Unit *u = userdata;
2751 char ***env = data;
2752 int r;
2753
2754 assert(filename);
2755 assert(lvalue);
2756 assert(rvalue);
2757 assert(data);
2758
2759 if (isempty(rvalue)) {
2760 /* Empty assignment resets the list */
2761 *env = strv_free(*env);
2762 return 0;
2763 }
2764
2765 for (const char *p = rvalue;; ) {
2766 _cleanup_free_ char *word = NULL, *resolved = NULL;
2767
2768 r = extract_first_word(&p, &word, NULL, EXTRACT_CUNESCAPE|EXTRACT_UNQUOTE);
2769 if (r == -ENOMEM)
2770 return log_oom();
2771 if (r < 0) {
2772 log_syntax(unit, LOG_WARNING, filename, line, r,
2773 "Invalid syntax, ignoring: %s", rvalue);
2774 return 0;
2775 }
2776 if (r == 0)
2777 return 0;
2778
2779 if (u)
2780 r = unit_env_printf(u, word, &resolved);
2781 else
2782 r = specifier_printf(word, sc_arg_max(), system_and_tmp_specifier_table, NULL, NULL, &resolved);
2783 if (r < 0) {
2784 log_syntax(unit, LOG_WARNING, filename, line, r,
2785 "Failed to resolve specifiers in %s, ignoring: %m", word);
2786 continue;
2787 }
2788
2789 if (!env_assignment_is_valid(resolved)) {
2790 log_syntax(unit, LOG_WARNING, filename, line, 0,
2791 "Invalid environment assignment, ignoring: %s", resolved);
2792 continue;
2793 }
2794
2795 r = strv_env_replace_consume(env, TAKE_PTR(resolved));
2796 if (r < 0)
2797 return log_error_errno(r, "Failed to update environment: %m");
2798 }
2799 }
2800
2801 int config_parse_pass_environ(
2802 const char *unit,
2803 const char *filename,
2804 unsigned line,
2805 const char *section,
2806 unsigned section_line,
2807 const char *lvalue,
2808 int ltype,
2809 const char *rvalue,
2810 void *data,
2811 void *userdata) {
2812
2813 _cleanup_strv_free_ char **n = NULL;
2814 const Unit *u = userdata;
2815 char*** passenv = data;
2816 size_t nlen = 0;
2817 int r;
2818
2819 assert(filename);
2820 assert(lvalue);
2821 assert(rvalue);
2822 assert(data);
2823
2824 if (isempty(rvalue)) {
2825 /* Empty assignment resets the list */
2826 *passenv = strv_free(*passenv);
2827 return 0;
2828 }
2829
2830 for (const char *p = rvalue;;) {
2831 _cleanup_free_ char *word = NULL, *k = NULL;
2832
2833 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
2834 if (r == -ENOMEM)
2835 return log_oom();
2836 if (r < 0) {
2837 log_syntax(unit, LOG_WARNING, filename, line, r,
2838 "Trailing garbage in %s, ignoring: %s", lvalue, rvalue);
2839 break;
2840 }
2841 if (r == 0)
2842 break;
2843
2844 if (u) {
2845 r = unit_env_printf(u, word, &k);
2846 if (r < 0) {
2847 log_syntax(unit, LOG_WARNING, filename, line, r,
2848 "Failed to resolve specifiers in %s, ignoring: %m", word);
2849 continue;
2850 }
2851 } else
2852 k = TAKE_PTR(word);
2853
2854 if (!env_name_is_valid(k)) {
2855 log_syntax(unit, LOG_WARNING, filename, line, 0,
2856 "Invalid environment name for %s, ignoring: %s", lvalue, k);
2857 continue;
2858 }
2859
2860 if (!GREEDY_REALLOC(n, nlen + 2))
2861 return log_oom();
2862
2863 n[nlen++] = TAKE_PTR(k);
2864 n[nlen] = NULL;
2865 }
2866
2867 if (n) {
2868 r = strv_extend_strv(passenv, n, true);
2869 if (r < 0)
2870 return log_oom();
2871 }
2872
2873 return 0;
2874 }
2875
2876 int config_parse_unset_environ(
2877 const char *unit,
2878 const char *filename,
2879 unsigned line,
2880 const char *section,
2881 unsigned section_line,
2882 const char *lvalue,
2883 int ltype,
2884 const char *rvalue,
2885 void *data,
2886 void *userdata) {
2887
2888 _cleanup_strv_free_ char **n = NULL;
2889 char*** unsetenv = data;
2890 const Unit *u = userdata;
2891 size_t nlen = 0;
2892 int r;
2893
2894 assert(filename);
2895 assert(lvalue);
2896 assert(rvalue);
2897 assert(data);
2898
2899 if (isempty(rvalue)) {
2900 /* Empty assignment resets the list */
2901 *unsetenv = strv_free(*unsetenv);
2902 return 0;
2903 }
2904
2905 for (const char *p = rvalue;;) {
2906 _cleanup_free_ char *word = NULL, *k = NULL;
2907
2908 r = extract_first_word(&p, &word, NULL, EXTRACT_CUNESCAPE|EXTRACT_UNQUOTE);
2909 if (r == -ENOMEM)
2910 return log_oom();
2911 if (r < 0) {
2912 log_syntax(unit, LOG_WARNING, filename, line, r,
2913 "Trailing garbage in %s, ignoring: %s", lvalue, rvalue);
2914 break;
2915 }
2916 if (r == 0)
2917 break;
2918
2919 if (u) {
2920 r = unit_env_printf(u, word, &k);
2921 if (r < 0) {
2922 log_syntax(unit, LOG_WARNING, filename, line, r,
2923 "Failed to resolve unit specifiers in %s, ignoring: %m", word);
2924 continue;
2925 }
2926 } else
2927 k = TAKE_PTR(word);
2928
2929 if (!env_assignment_is_valid(k) && !env_name_is_valid(k)) {
2930 log_syntax(unit, LOG_WARNING, filename, line, 0,
2931 "Invalid environment name or assignment %s, ignoring: %s", lvalue, k);
2932 continue;
2933 }
2934
2935 if (!GREEDY_REALLOC(n, nlen + 2))
2936 return log_oom();
2937
2938 n[nlen++] = TAKE_PTR(k);
2939 n[nlen] = NULL;
2940 }
2941
2942 if (n) {
2943 r = strv_extend_strv(unsetenv, n, true);
2944 if (r < 0)
2945 return log_oom();
2946 }
2947
2948 return 0;
2949 }
2950
2951 int config_parse_log_extra_fields(
2952 const char *unit,
2953 const char *filename,
2954 unsigned line,
2955 const char *section,
2956 unsigned section_line,
2957 const char *lvalue,
2958 int ltype,
2959 const char *rvalue,
2960 void *data,
2961 void *userdata) {
2962
2963 ExecContext *c = data;
2964 const Unit *u = userdata;
2965 int r;
2966
2967 assert(filename);
2968 assert(lvalue);
2969 assert(rvalue);
2970 assert(c);
2971
2972 if (isempty(rvalue)) {
2973 exec_context_free_log_extra_fields(c);
2974 return 0;
2975 }
2976
2977 for (const char *p = rvalue;;) {
2978 _cleanup_free_ char *word = NULL, *k = NULL;
2979 struct iovec *t;
2980 const char *eq;
2981
2982 r = extract_first_word(&p, &word, NULL, EXTRACT_CUNESCAPE|EXTRACT_UNQUOTE);
2983 if (r == -ENOMEM)
2984 return log_oom();
2985 if (r < 0) {
2986 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
2987 return 0;
2988 }
2989 if (r == 0)
2990 return 0;
2991
2992 r = unit_full_printf(u, word, &k);
2993 if (r < 0) {
2994 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", word);
2995 continue;
2996 }
2997
2998 eq = strchr(k, '=');
2999 if (!eq) {
3000 log_syntax(unit, LOG_WARNING, filename, line, 0, "Log field lacks '=' character, ignoring: %s", k);
3001 continue;
3002 }
3003
3004 if (!journal_field_valid(k, eq-k, false)) {
3005 log_syntax(unit, LOG_WARNING, filename, line, 0, "Log field name is invalid, ignoring: %s", k);
3006 continue;
3007 }
3008
3009 t = reallocarray(c->log_extra_fields, c->n_log_extra_fields+1, sizeof(struct iovec));
3010 if (!t)
3011 return log_oom();
3012
3013 c->log_extra_fields = t;
3014 c->log_extra_fields[c->n_log_extra_fields++] = IOVEC_MAKE_STRING(k);
3015
3016 k = NULL;
3017 }
3018 }
3019
3020 int config_parse_log_namespace(
3021 const char *unit,
3022 const char *filename,
3023 unsigned line,
3024 const char *section,
3025 unsigned section_line,
3026 const char *lvalue,
3027 int ltype,
3028 const char *rvalue,
3029 void *data,
3030 void *userdata) {
3031
3032 _cleanup_free_ char *k = NULL;
3033 ExecContext *c = data;
3034 const Unit *u = userdata;
3035 int r;
3036
3037 assert(filename);
3038 assert(lvalue);
3039 assert(rvalue);
3040 assert(c);
3041
3042 if (isempty(rvalue)) {
3043 c->log_namespace = mfree(c->log_namespace);
3044 return 0;
3045 }
3046
3047 r = unit_full_printf_full(u, rvalue, NAME_MAX, &k);
3048 if (r < 0) {
3049 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue);
3050 return 0;
3051 }
3052
3053 if (!log_namespace_name_valid(k)) {
3054 log_syntax(unit, LOG_WARNING, filename, line, 0, "Specified log namespace name is not valid, ignoring: %s", k);
3055 return 0;
3056 }
3057
3058 free_and_replace(c->log_namespace, k);
3059 return 0;
3060 }
3061
3062 int config_parse_unit_condition_path(
3063 const char *unit,
3064 const char *filename,
3065 unsigned line,
3066 const char *section,
3067 unsigned section_line,
3068 const char *lvalue,
3069 int ltype,
3070 const char *rvalue,
3071 void *data,
3072 void *userdata) {
3073
3074 _cleanup_free_ char *p = NULL;
3075 Condition **list = data, *c;
3076 ConditionType t = ltype;
3077 bool trigger, negate;
3078 const Unit *u = userdata;
3079 int r;
3080
3081 assert(filename);
3082 assert(lvalue);
3083 assert(rvalue);
3084 assert(data);
3085
3086 if (isempty(rvalue)) {
3087 /* Empty assignment resets the list */
3088 *list = condition_free_list(*list);
3089 return 0;
3090 }
3091
3092 trigger = rvalue[0] == '|';
3093 if (trigger)
3094 rvalue++;
3095
3096 negate = rvalue[0] == '!';
3097 if (negate)
3098 rvalue++;
3099
3100 r = unit_path_printf(u, rvalue, &p);
3101 if (r < 0) {
3102 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue);
3103 return 0;
3104 }
3105
3106 r = path_simplify_and_warn(p, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
3107 if (r < 0)
3108 return 0;
3109
3110 c = condition_new(t, p, trigger, negate);
3111 if (!c)
3112 return log_oom();
3113
3114 LIST_PREPEND(conditions, *list, c);
3115 return 0;
3116 }
3117
3118 int config_parse_unit_condition_string(
3119 const char *unit,
3120 const char *filename,
3121 unsigned line,
3122 const char *section,
3123 unsigned section_line,
3124 const char *lvalue,
3125 int ltype,
3126 const char *rvalue,
3127 void *data,
3128 void *userdata) {
3129
3130 _cleanup_free_ char *s = NULL;
3131 Condition **list = data, *c;
3132 ConditionType t = ltype;
3133 bool trigger, negate;
3134 const Unit *u = userdata;
3135 int r;
3136
3137 assert(filename);
3138 assert(lvalue);
3139 assert(rvalue);
3140 assert(data);
3141
3142 if (isempty(rvalue)) {
3143 /* Empty assignment resets the list */
3144 *list = condition_free_list(*list);
3145 return 0;
3146 }
3147
3148 trigger = *rvalue == '|';
3149 if (trigger)
3150 rvalue += 1 + strspn(rvalue + 1, WHITESPACE);
3151
3152 negate = *rvalue == '!';
3153 if (negate)
3154 rvalue += 1 + strspn(rvalue + 1, WHITESPACE);
3155
3156 r = unit_full_printf(u, rvalue, &s);
3157 if (r < 0) {
3158 log_syntax(unit, LOG_WARNING, filename, line, r,
3159 "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
3160 return 0;
3161 }
3162
3163 c = condition_new(t, s, trigger, negate);
3164 if (!c)
3165 return log_oom();
3166
3167 LIST_PREPEND(conditions, *list, c);
3168 return 0;
3169 }
3170
3171 int config_parse_unit_requires_mounts_for(
3172 const char *unit,
3173 const char *filename,
3174 unsigned line,
3175 const char *section,
3176 unsigned section_line,
3177 const char *lvalue,
3178 int ltype,
3179 const char *rvalue,
3180 void *data,
3181 void *userdata) {
3182
3183 Unit *u = userdata;
3184 int r;
3185
3186 assert(filename);
3187 assert(lvalue);
3188 assert(rvalue);
3189 assert(data);
3190
3191 for (const char *p = rvalue;;) {
3192 _cleanup_free_ char *word = NULL, *resolved = NULL;
3193
3194 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
3195 if (r == -ENOMEM)
3196 return log_oom();
3197 if (r < 0) {
3198 log_syntax(unit, LOG_WARNING, filename, line, r,
3199 "Invalid syntax, ignoring: %s", rvalue);
3200 return 0;
3201 }
3202 if (r == 0)
3203 return 0;
3204
3205 r = unit_path_printf(u, word, &resolved);
3206 if (r < 0) {
3207 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", word);
3208 continue;
3209 }
3210
3211 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
3212 if (r < 0)
3213 continue;
3214
3215 r = unit_require_mounts_for(u, resolved, UNIT_DEPENDENCY_FILE);
3216 if (r < 0) {
3217 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to add required mount '%s', ignoring: %m", resolved);
3218 continue;
3219 }
3220 }
3221 }
3222
3223 int config_parse_documentation(
3224 const char *unit,
3225 const char *filename,
3226 unsigned line,
3227 const char *section,
3228 unsigned section_line,
3229 const char *lvalue,
3230 int ltype,
3231 const char *rvalue,
3232 void *data,
3233 void *userdata) {
3234
3235 Unit *u = userdata;
3236 int r;
3237 char **a, **b;
3238
3239 assert(filename);
3240 assert(lvalue);
3241 assert(rvalue);
3242 assert(u);
3243
3244 if (isempty(rvalue)) {
3245 /* Empty assignment resets the list */
3246 u->documentation = strv_free(u->documentation);
3247 return 0;
3248 }
3249
3250 r = config_parse_unit_strv_printf(unit, filename, line, section, section_line, lvalue, ltype,
3251 rvalue, data, userdata);
3252 if (r < 0)
3253 return r;
3254
3255 for (a = b = u->documentation; a && *a; a++) {
3256
3257 if (documentation_url_is_valid(*a))
3258 *(b++) = *a;
3259 else {
3260 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid URL, ignoring: %s", *a);
3261 free(*a);
3262 }
3263 }
3264 if (b)
3265 *b = NULL;
3266
3267 return 0;
3268 }
3269
3270 #if HAVE_SECCOMP
3271 int config_parse_syscall_filter(
3272 const char *unit,
3273 const char *filename,
3274 unsigned line,
3275 const char *section,
3276 unsigned section_line,
3277 const char *lvalue,
3278 int ltype,
3279 const char *rvalue,
3280 void *data,
3281 void *userdata) {
3282
3283 ExecContext *c = data;
3284 _unused_ const Unit *u = userdata;
3285 bool invert = false;
3286 int r;
3287
3288 assert(filename);
3289 assert(lvalue);
3290 assert(rvalue);
3291 assert(u);
3292
3293 if (isempty(rvalue)) {
3294 /* Empty assignment resets the list */
3295 c->syscall_filter = hashmap_free(c->syscall_filter);
3296 c->syscall_allow_list = false;
3297 return 0;
3298 }
3299
3300 if (rvalue[0] == '~') {
3301 invert = true;
3302 rvalue++;
3303 }
3304
3305 if (!c->syscall_filter) {
3306 c->syscall_filter = hashmap_new(NULL);
3307 if (!c->syscall_filter)
3308 return log_oom();
3309
3310 if (invert)
3311 /* Allow everything but the ones listed */
3312 c->syscall_allow_list = false;
3313 else {
3314 /* Allow nothing but the ones listed */
3315 c->syscall_allow_list = true;
3316
3317 /* Accept default syscalls if we are on an allow_list */
3318 r = seccomp_parse_syscall_filter(
3319 "@default", -1, c->syscall_filter,
3320 SECCOMP_PARSE_PERMISSIVE|SECCOMP_PARSE_ALLOW_LIST,
3321 unit,
3322 NULL, 0);
3323 if (r < 0)
3324 return r;
3325 }
3326 }
3327
3328 for (const char *p = rvalue;;) {
3329 _cleanup_free_ char *word = NULL, *name = NULL;
3330 int num;
3331
3332 r = extract_first_word(&p, &word, NULL, 0);
3333 if (r == -ENOMEM)
3334 return log_oom();
3335 if (r < 0) {
3336 log_syntax(unit, LOG_WARNING, filename, line, r,
3337 "Invalid syntax, ignoring: %s", rvalue);
3338 return 0;
3339 }
3340 if (r == 0)
3341 return 0;
3342
3343 r = parse_syscall_and_errno(word, &name, &num);
3344 if (r < 0) {
3345 log_syntax(unit, LOG_WARNING, filename, line, r,
3346 "Failed to parse syscall:errno, ignoring: %s", word);
3347 continue;
3348 }
3349 if (!invert && num >= 0) {
3350 log_syntax(unit, LOG_WARNING, filename, line, 0,
3351 "Allow-listed system calls cannot take error number, ignoring: %s", word);
3352 continue;
3353 }
3354
3355 r = seccomp_parse_syscall_filter(
3356 name, num, c->syscall_filter,
3357 SECCOMP_PARSE_LOG|SECCOMP_PARSE_PERMISSIVE|
3358 (invert ? SECCOMP_PARSE_INVERT : 0)|
3359 (c->syscall_allow_list ? SECCOMP_PARSE_ALLOW_LIST : 0),
3360 unit, filename, line);
3361 if (r < 0)
3362 return r;
3363 }
3364 }
3365
3366 int config_parse_syscall_log(
3367 const char *unit,
3368 const char *filename,
3369 unsigned line,
3370 const char *section,
3371 unsigned section_line,
3372 const char *lvalue,
3373 int ltype,
3374 const char *rvalue,
3375 void *data,
3376 void *userdata) {
3377
3378 ExecContext *c = data;
3379 _unused_ const Unit *u = userdata;
3380 bool invert = false;
3381 const char *p;
3382 int r;
3383
3384 assert(filename);
3385 assert(lvalue);
3386 assert(rvalue);
3387 assert(u);
3388
3389 if (isempty(rvalue)) {
3390 /* Empty assignment resets the list */
3391 c->syscall_log = hashmap_free(c->syscall_log);
3392 c->syscall_log_allow_list = false;
3393 return 0;
3394 }
3395
3396 if (rvalue[0] == '~') {
3397 invert = true;
3398 rvalue++;
3399 }
3400
3401 if (!c->syscall_log) {
3402 c->syscall_log = hashmap_new(NULL);
3403 if (!c->syscall_log)
3404 return log_oom();
3405
3406 if (invert)
3407 /* Log everything but the ones listed */
3408 c->syscall_log_allow_list = false;
3409 else
3410 /* Log nothing but the ones listed */
3411 c->syscall_log_allow_list = true;
3412 }
3413
3414 p = rvalue;
3415 for (;;) {
3416 _cleanup_free_ char *word = NULL;
3417
3418 r = extract_first_word(&p, &word, NULL, 0);
3419 if (r == -ENOMEM)
3420 return log_oom();
3421 if (r < 0) {
3422 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
3423 return 0;
3424 }
3425 if (r == 0)
3426 return 0;
3427
3428 r = seccomp_parse_syscall_filter(
3429 word, -1, c->syscall_log,
3430 SECCOMP_PARSE_LOG|SECCOMP_PARSE_PERMISSIVE|
3431 (invert ? SECCOMP_PARSE_INVERT : 0)|
3432 (c->syscall_log_allow_list ? SECCOMP_PARSE_ALLOW_LIST : 0),
3433 unit, filename, line);
3434 if (r < 0)
3435 return r;
3436 }
3437 }
3438
3439 int config_parse_syscall_archs(
3440 const char *unit,
3441 const char *filename,
3442 unsigned line,
3443 const char *section,
3444 unsigned section_line,
3445 const char *lvalue,
3446 int ltype,
3447 const char *rvalue,
3448 void *data,
3449 void *userdata) {
3450
3451 Set **archs = data;
3452 int r;
3453
3454 if (isempty(rvalue)) {
3455 *archs = set_free(*archs);
3456 return 0;
3457 }
3458
3459 for (const char *p = rvalue;;) {
3460 _cleanup_free_ char *word = NULL;
3461 uint32_t a;
3462
3463 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
3464 if (r == -ENOMEM)
3465 return log_oom();
3466 if (r < 0) {
3467 log_syntax(unit, LOG_WARNING, filename, line, r,
3468 "Invalid syntax, ignoring: %s", rvalue);
3469 return 0;
3470 }
3471 if (r == 0)
3472 return 0;
3473
3474 r = seccomp_arch_from_string(word, &a);
3475 if (r < 0) {
3476 log_syntax(unit, LOG_WARNING, filename, line, r,
3477 "Failed to parse system call architecture \"%s\", ignoring: %m", word);
3478 continue;
3479 }
3480
3481 r = set_ensure_put(archs, NULL, UINT32_TO_PTR(a + 1));
3482 if (r < 0)
3483 return log_oom();
3484 }
3485 }
3486
3487 int config_parse_syscall_errno(
3488 const char *unit,
3489 const char *filename,
3490 unsigned line,
3491 const char *section,
3492 unsigned section_line,
3493 const char *lvalue,
3494 int ltype,
3495 const char *rvalue,
3496 void *data,
3497 void *userdata) {
3498
3499 ExecContext *c = data;
3500 int e;
3501
3502 assert(filename);
3503 assert(lvalue);
3504 assert(rvalue);
3505
3506 if (isempty(rvalue) || streq(rvalue, "kill")) {
3507 /* Empty assignment resets to KILL */
3508 c->syscall_errno = SECCOMP_ERROR_NUMBER_KILL;
3509 return 0;
3510 }
3511
3512 e = parse_errno(rvalue);
3513 if (e < 0) {
3514 log_syntax(unit, LOG_WARNING, filename, line, e, "Failed to parse error number, ignoring: %s", rvalue);
3515 return 0;
3516 }
3517 if (e == 0) {
3518 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid error number, ignoring: %s", rvalue);
3519 return 0;
3520 }
3521
3522 c->syscall_errno = e;
3523 return 0;
3524 }
3525
3526 int config_parse_address_families(
3527 const char *unit,
3528 const char *filename,
3529 unsigned line,
3530 const char *section,
3531 unsigned section_line,
3532 const char *lvalue,
3533 int ltype,
3534 const char *rvalue,
3535 void *data,
3536 void *userdata) {
3537
3538 ExecContext *c = data;
3539 bool invert = false;
3540 int r;
3541
3542 assert(filename);
3543 assert(lvalue);
3544 assert(rvalue);
3545
3546 if (isempty(rvalue)) {
3547 /* Empty assignment resets the list */
3548 c->address_families = set_free(c->address_families);
3549 c->address_families_allow_list = false;
3550 return 0;
3551 }
3552
3553 if (streq(rvalue, "none")) {
3554 /* Forbid all address families. */
3555 c->address_families = set_free(c->address_families);
3556 c->address_families_allow_list = true;
3557 return 0;
3558 }
3559
3560 if (rvalue[0] == '~') {
3561 invert = true;
3562 rvalue++;
3563 }
3564
3565 if (!c->address_families) {
3566 c->address_families = set_new(NULL);
3567 if (!c->address_families)
3568 return log_oom();
3569
3570 c->address_families_allow_list = !invert;
3571 }
3572
3573 for (const char *p = rvalue;;) {
3574 _cleanup_free_ char *word = NULL;
3575 int af;
3576
3577 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
3578 if (r == -ENOMEM)
3579 return log_oom();
3580 if (r < 0) {
3581 log_syntax(unit, LOG_WARNING, filename, line, r,
3582 "Invalid syntax, ignoring: %s", rvalue);
3583 return 0;
3584 }
3585 if (r == 0)
3586 return 0;
3587
3588 af = af_from_name(word);
3589 if (af < 0) {
3590 log_syntax(unit, LOG_WARNING, filename, line, af,
3591 "Failed to parse address family, ignoring: %s", word);
3592 continue;
3593 }
3594
3595 /* If we previously wanted to forbid an address family and now
3596 * we want to allow it, then just remove it from the list.
3597 */
3598 if (!invert == c->address_families_allow_list) {
3599 r = set_put(c->address_families, INT_TO_PTR(af));
3600 if (r < 0)
3601 return log_oom();
3602 } else
3603 set_remove(c->address_families, INT_TO_PTR(af));
3604 }
3605 }
3606
3607 int config_parse_restrict_namespaces(
3608 const char *unit,
3609 const char *filename,
3610 unsigned line,
3611 const char *section,
3612 unsigned section_line,
3613 const char *lvalue,
3614 int ltype,
3615 const char *rvalue,
3616 void *data,
3617 void *userdata) {
3618
3619 ExecContext *c = data;
3620 unsigned long flags;
3621 bool invert = false;
3622 int r;
3623
3624 if (isempty(rvalue)) {
3625 /* Reset to the default. */
3626 c->restrict_namespaces = NAMESPACE_FLAGS_INITIAL;
3627 return 0;
3628 }
3629
3630 /* Boolean parameter ignores the previous settings */
3631 r = parse_boolean(rvalue);
3632 if (r > 0) {
3633 c->restrict_namespaces = 0;
3634 return 0;
3635 } else if (r == 0) {
3636 c->restrict_namespaces = NAMESPACE_FLAGS_ALL;
3637 return 0;
3638 }
3639
3640 if (rvalue[0] == '~') {
3641 invert = true;
3642 rvalue++;
3643 }
3644
3645 /* Not a boolean argument, in this case it's a list of namespace types. */
3646 r = namespace_flags_from_string(rvalue, &flags);
3647 if (r < 0) {
3648 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse namespace type string, ignoring: %s", rvalue);
3649 return 0;
3650 }
3651
3652 if (c->restrict_namespaces == NAMESPACE_FLAGS_INITIAL)
3653 /* Initial assignment. Just set the value. */
3654 c->restrict_namespaces = invert ? (~flags) & NAMESPACE_FLAGS_ALL : flags;
3655 else
3656 /* Merge the value with the previous one. */
3657 SET_FLAG(c->restrict_namespaces, flags, !invert);
3658
3659 return 0;
3660 }
3661 #endif
3662
3663 int config_parse_restrict_filesystems(
3664 const char *unit,
3665 const char *filename,
3666 unsigned line,
3667 const char *section,
3668 unsigned section_line,
3669 const char *lvalue,
3670 int ltype,
3671 const char *rvalue,
3672 void *data,
3673 void *userdata) {
3674 ExecContext *c = data;
3675 bool invert = false;
3676 int r;
3677
3678 assert(filename);
3679 assert(lvalue);
3680 assert(rvalue);
3681 assert(data);
3682
3683 if (isempty(rvalue)) {
3684 /* Empty assignment resets the list */
3685 c->restrict_filesystems = set_free(c->restrict_filesystems);
3686 c->restrict_filesystems_allow_list = false;
3687 return 0;
3688 }
3689
3690 if (rvalue[0] == '~') {
3691 invert = true;
3692 rvalue++;
3693 }
3694
3695 if (!c->restrict_filesystems) {
3696 if (invert)
3697 /* Allow everything but the ones listed */
3698 c->restrict_filesystems_allow_list = false;
3699 else
3700 /* Allow nothing but the ones listed */
3701 c->restrict_filesystems_allow_list = true;
3702 }
3703
3704 for (const char *p = rvalue;;) {
3705 _cleanup_free_ char *word = NULL;
3706
3707 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
3708 if (r == 0)
3709 break;
3710 if (r == -ENOMEM)
3711 return log_oom();
3712 if (r < 0) {
3713 log_syntax(unit, LOG_WARNING, filename, line, r,
3714 "Trailing garbage in %s, ignoring: %s", lvalue, rvalue);
3715 break;
3716 }
3717
3718 r = lsm_bpf_parse_filesystem(
3719 word,
3720 &c->restrict_filesystems,
3721 FILESYSTEM_PARSE_LOG|
3722 (invert ? FILESYSTEM_PARSE_INVERT : 0)|
3723 (c->restrict_filesystems_allow_list ? FILESYSTEM_PARSE_ALLOW_LIST : 0),
3724 unit, filename, line);
3725
3726 if (r < 0)
3727 return r;
3728 }
3729
3730 return 0;
3731 }
3732
3733 int config_parse_unit_slice(
3734 const char *unit,
3735 const char *filename,
3736 unsigned line,
3737 const char *section,
3738 unsigned section_line,
3739 const char *lvalue,
3740 int ltype,
3741 const char *rvalue,
3742 void *data,
3743 void *userdata) {
3744
3745 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
3746 _cleanup_free_ char *k = NULL;
3747 Unit *u = userdata, *slice;
3748 int r;
3749
3750 assert(filename);
3751 assert(lvalue);
3752 assert(rvalue);
3753 assert(u);
3754
3755 r = unit_name_printf(u, rvalue, &k);
3756 if (r < 0) {
3757 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue);
3758 return 0;
3759 }
3760
3761 r = manager_load_unit(u->manager, k, NULL, &error, &slice);
3762 if (r < 0) {
3763 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to load slice unit %s, ignoring: %s", k, bus_error_message(&error, r));
3764 return 0;
3765 }
3766
3767 r = unit_set_slice(u, slice);
3768 if (r < 0) {
3769 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to assign slice %s to unit %s, ignoring: %m", slice->id, u->id);
3770 return 0;
3771 }
3772
3773 return 0;
3774 }
3775
3776 int config_parse_cpu_quota(
3777 const char *unit,
3778 const char *filename,
3779 unsigned line,
3780 const char *section,
3781 unsigned section_line,
3782 const char *lvalue,
3783 int ltype,
3784 const char *rvalue,
3785 void *data,
3786 void *userdata) {
3787
3788 CGroupContext *c = data;
3789 int r;
3790
3791 assert(filename);
3792 assert(lvalue);
3793 assert(rvalue);
3794
3795 if (isempty(rvalue)) {
3796 c->cpu_quota_per_sec_usec = USEC_INFINITY;
3797 return 0;
3798 }
3799
3800 r = parse_permyriad_unbounded(rvalue);
3801 if (r <= 0) {
3802 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid CPU quota '%s', ignoring.", rvalue);
3803 return 0;
3804 }
3805
3806 c->cpu_quota_per_sec_usec = ((usec_t) r * USEC_PER_SEC) / 10000U;
3807 return 0;
3808 }
3809
3810 int config_parse_allowed_cpuset(
3811 const char *unit,
3812 const char *filename,
3813 unsigned line,
3814 const char *section,
3815 unsigned section_line,
3816 const char *lvalue,
3817 int ltype,
3818 const char *rvalue,
3819 void *data,
3820 void *userdata) {
3821
3822 CPUSet *c = data;
3823
3824 (void) parse_cpu_set_extend(rvalue, c, true, unit, filename, line, lvalue);
3825 return 0;
3826 }
3827
3828 int config_parse_memory_limit(
3829 const char *unit,
3830 const char *filename,
3831 unsigned line,
3832 const char *section,
3833 unsigned section_line,
3834 const char *lvalue,
3835 int ltype,
3836 const char *rvalue,
3837 void *data,
3838 void *userdata) {
3839
3840 CGroupContext *c = data;
3841 uint64_t bytes = CGROUP_LIMIT_MAX;
3842 int r;
3843
3844 if (isempty(rvalue) && STR_IN_SET(lvalue, "DefaultMemoryLow",
3845 "DefaultMemoryMin",
3846 "MemoryLow",
3847 "MemoryMin"))
3848 bytes = CGROUP_LIMIT_MIN;
3849 else if (!isempty(rvalue) && !streq(rvalue, "infinity")) {
3850
3851 r = parse_permyriad(rvalue);
3852 if (r < 0) {
3853 r = parse_size(rvalue, 1024, &bytes);
3854 if (r < 0) {
3855 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid memory limit '%s', ignoring: %m", rvalue);
3856 return 0;
3857 }
3858 } else
3859 bytes = physical_memory_scale(r, 10000U);
3860
3861 if (bytes >= UINT64_MAX ||
3862 (bytes <= 0 && !STR_IN_SET(lvalue, "MemorySwapMax", "MemoryLow", "MemoryMin", "DefaultMemoryLow", "DefaultMemoryMin"))) {
3863 log_syntax(unit, LOG_WARNING, filename, line, 0, "Memory limit '%s' out of range, ignoring.", rvalue);
3864 return 0;
3865 }
3866 }
3867
3868 if (streq(lvalue, "DefaultMemoryLow")) {
3869 c->default_memory_low = bytes;
3870 c->default_memory_low_set = true;
3871 } else if (streq(lvalue, "DefaultMemoryMin")) {
3872 c->default_memory_min = bytes;
3873 c->default_memory_min_set = true;
3874 } else if (streq(lvalue, "MemoryMin")) {
3875 c->memory_min = bytes;
3876 c->memory_min_set = true;
3877 } else if (streq(lvalue, "MemoryLow")) {
3878 c->memory_low = bytes;
3879 c->memory_low_set = true;
3880 } else if (streq(lvalue, "MemoryHigh"))
3881 c->memory_high = bytes;
3882 else if (streq(lvalue, "MemoryMax"))
3883 c->memory_max = bytes;
3884 else if (streq(lvalue, "MemorySwapMax"))
3885 c->memory_swap_max = bytes;
3886 else if (streq(lvalue, "MemoryLimit"))
3887 c->memory_limit = bytes;
3888 else
3889 return -EINVAL;
3890
3891 return 0;
3892 }
3893
3894 int config_parse_tasks_max(
3895 const char *unit,
3896 const char *filename,
3897 unsigned line,
3898 const char *section,
3899 unsigned section_line,
3900 const char *lvalue,
3901 int ltype,
3902 const char *rvalue,
3903 void *data,
3904 void *userdata) {
3905
3906 const Unit *u = userdata;
3907 TasksMax *tasks_max = data;
3908 uint64_t v;
3909 int r;
3910
3911 if (isempty(rvalue)) {
3912 *tasks_max = u ? u->manager->default_tasks_max : TASKS_MAX_UNSET;
3913 return 0;
3914 }
3915
3916 if (streq(rvalue, "infinity")) {
3917 *tasks_max = TASKS_MAX_UNSET;
3918 return 0;
3919 }
3920
3921 r = parse_permyriad(rvalue);
3922 if (r >= 0)
3923 *tasks_max = (TasksMax) { r, 10000U }; /* r‱ */
3924 else {
3925 r = safe_atou64(rvalue, &v);
3926 if (r < 0) {
3927 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid maximum tasks value '%s', ignoring: %m", rvalue);
3928 return 0;
3929 }
3930
3931 if (v <= 0 || v >= UINT64_MAX) {
3932 log_syntax(unit, LOG_WARNING, filename, line, 0, "Maximum tasks value '%s' out of range, ignoring.", rvalue);
3933 return 0;
3934 }
3935
3936 *tasks_max = (TasksMax) { v };
3937 }
3938
3939 return 0;
3940 }
3941
3942 int config_parse_delegate(
3943 const char *unit,
3944 const char *filename,
3945 unsigned line,
3946 const char *section,
3947 unsigned section_line,
3948 const char *lvalue,
3949 int ltype,
3950 const char *rvalue,
3951 void *data,
3952 void *userdata) {
3953
3954 CGroupContext *c = data;
3955 UnitType t;
3956 int r;
3957
3958 t = unit_name_to_type(unit);
3959 assert(t != _UNIT_TYPE_INVALID);
3960
3961 if (!unit_vtable[t]->can_delegate) {
3962 log_syntax(unit, LOG_WARNING, filename, line, 0, "Delegate= setting not supported for this unit type, ignoring.");
3963 return 0;
3964 }
3965
3966 /* We either accept a boolean value, which may be used to turn on delegation for all controllers, or turn it
3967 * off for all. Or it takes a list of controller names, in which case we add the specified controllers to the
3968 * mask to delegate. */
3969
3970 if (isempty(rvalue)) {
3971 /* An empty string resets controllers and set Delegate=yes. */
3972 c->delegate = true;
3973 c->delegate_controllers = 0;
3974 return 0;
3975 }
3976
3977 r = parse_boolean(rvalue);
3978 if (r < 0) {
3979 CGroupMask mask = 0;
3980
3981 for (const char *p = rvalue;;) {
3982 _cleanup_free_ char *word = NULL;
3983 CGroupController cc;
3984
3985 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
3986 if (r == -ENOMEM)
3987 return log_oom();
3988 if (r < 0) {
3989 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
3990 return 0;
3991 }
3992 if (r == 0)
3993 break;
3994
3995 cc = cgroup_controller_from_string(word);
3996 if (cc < 0) {
3997 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid controller name '%s', ignoring", word);
3998 continue;
3999 }
4000
4001 mask |= CGROUP_CONTROLLER_TO_MASK(cc);
4002 }
4003
4004 c->delegate = true;
4005 c->delegate_controllers |= mask;
4006
4007 } else if (r > 0) {
4008 c->delegate = true;
4009 c->delegate_controllers = _CGROUP_MASK_ALL;
4010 } else {
4011 c->delegate = false;
4012 c->delegate_controllers = 0;
4013 }
4014
4015 return 0;
4016 }
4017
4018 int config_parse_managed_oom_mode(
4019 const char *unit,
4020 const char *filename,
4021 unsigned line,
4022 const char *section,
4023 unsigned section_line,
4024 const char *lvalue,
4025 int ltype,
4026 const char *rvalue,
4027 void *data,
4028 void *userdata) {
4029
4030 ManagedOOMMode *mode = data, m;
4031 UnitType t;
4032
4033 t = unit_name_to_type(unit);
4034 assert(t != _UNIT_TYPE_INVALID);
4035
4036 if (!unit_vtable[t]->can_set_managed_oom)
4037 return log_syntax(unit, LOG_WARNING, filename, line, 0, "%s= is not supported for this unit type, ignoring.", lvalue);
4038
4039 if (isempty(rvalue)) {
4040 *mode = MANAGED_OOM_AUTO;
4041 return 0;
4042 }
4043
4044 m = managed_oom_mode_from_string(rvalue);
4045 if (m < 0) {
4046 log_syntax(unit, LOG_WARNING, filename, line, m, "Invalid syntax, ignoring: %s", rvalue);
4047 return 0;
4048 }
4049
4050 *mode = m;
4051 return 0;
4052 }
4053
4054 int config_parse_managed_oom_mem_pressure_limit(
4055 const char *unit,
4056 const char *filename,
4057 unsigned line,
4058 const char *section,
4059 unsigned section_line,
4060 const char *lvalue,
4061 int ltype,
4062 const char *rvalue,
4063 void *data,
4064 void *userdata) {
4065
4066 uint32_t *limit = data;
4067 UnitType t;
4068 int r;
4069
4070 t = unit_name_to_type(unit);
4071 assert(t != _UNIT_TYPE_INVALID);
4072
4073 if (!unit_vtable[t]->can_set_managed_oom)
4074 return log_syntax(unit, LOG_WARNING, filename, line, 0, "%s= is not supported for this unit type, ignoring.", lvalue);
4075
4076 if (isempty(rvalue)) {
4077 *limit = 0;
4078 return 0;
4079 }
4080
4081 r = parse_permyriad(rvalue);
4082 if (r < 0) {
4083 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse memory pressure limit value, ignoring: %s", rvalue);
4084 return 0;
4085 }
4086
4087 /* Normalize to 2^32-1 == 100% */
4088 *limit = UINT32_SCALE_FROM_PERMYRIAD(r);
4089 return 0;
4090 }
4091
4092 int config_parse_device_allow(
4093 const char *unit,
4094 const char *filename,
4095 unsigned line,
4096 const char *section,
4097 unsigned section_line,
4098 const char *lvalue,
4099 int ltype,
4100 const char *rvalue,
4101 void *data,
4102 void *userdata) {
4103
4104 _cleanup_free_ char *path = NULL, *resolved = NULL;
4105 CGroupContext *c = data;
4106 const char *p = rvalue;
4107 int r;
4108
4109 if (isempty(rvalue)) {
4110 while (c->device_allow)
4111 cgroup_context_free_device_allow(c, c->device_allow);
4112
4113 return 0;
4114 }
4115
4116 r = extract_first_word(&p, &path, NULL, EXTRACT_UNQUOTE);
4117 if (r == -ENOMEM)
4118 return log_oom();
4119 if (r <= 0) {
4120 log_syntax(unit, LOG_WARNING, filename, line, r,
4121 "Failed to extract device path and rights from '%s', ignoring.", rvalue);
4122 return 0;
4123 }
4124
4125 r = unit_path_printf(userdata, path, &resolved);
4126 if (r < 0) {
4127 log_syntax(unit, LOG_WARNING, filename, line, r,
4128 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
4129 return 0;
4130 }
4131
4132 if (!STARTSWITH_SET(resolved, "block-", "char-")) {
4133
4134 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
4135 if (r < 0)
4136 return 0;
4137
4138 if (!valid_device_node_path(resolved)) {
4139 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid device node path '%s', ignoring.", resolved);
4140 return 0;
4141 }
4142 }
4143
4144 if (!isempty(p) && !in_charset(p, "rwm")) {
4145 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid device rights '%s', ignoring.", p);
4146 return 0;
4147 }
4148
4149 return cgroup_add_device_allow(c, resolved, p);
4150 }
4151
4152 int config_parse_io_device_weight(
4153 const char *unit,
4154 const char *filename,
4155 unsigned line,
4156 const char *section,
4157 unsigned section_line,
4158 const char *lvalue,
4159 int ltype,
4160 const char *rvalue,
4161 void *data,
4162 void *userdata) {
4163
4164 _cleanup_free_ char *path = NULL, *resolved = NULL;
4165 CGroupIODeviceWeight *w;
4166 CGroupContext *c = data;
4167 const char *p = rvalue;
4168 uint64_t u;
4169 int r;
4170
4171 assert(filename);
4172 assert(lvalue);
4173 assert(rvalue);
4174
4175 if (isempty(rvalue)) {
4176 while (c->io_device_weights)
4177 cgroup_context_free_io_device_weight(c, c->io_device_weights);
4178
4179 return 0;
4180 }
4181
4182 r = extract_first_word(&p, &path, NULL, EXTRACT_UNQUOTE);
4183 if (r == -ENOMEM)
4184 return log_oom();
4185 if (r < 0) {
4186 log_syntax(unit, LOG_WARNING, filename, line, r,
4187 "Failed to extract device path and weight from '%s', ignoring.", rvalue);
4188 return 0;
4189 }
4190 if (r == 0 || isempty(p)) {
4191 log_syntax(unit, LOG_WARNING, filename, line, 0,
4192 "Invalid device path or weight specified in '%s', ignoring.", rvalue);
4193 return 0;
4194 }
4195
4196 r = unit_path_printf(userdata, path, &resolved);
4197 if (r < 0) {
4198 log_syntax(unit, LOG_WARNING, filename, line, r,
4199 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
4200 return 0;
4201 }
4202
4203 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
4204 if (r < 0)
4205 return 0;
4206
4207 r = cg_weight_parse(p, &u);
4208 if (r < 0) {
4209 log_syntax(unit, LOG_WARNING, filename, line, r, "IO weight '%s' invalid, ignoring: %m", p);
4210 return 0;
4211 }
4212
4213 assert(u != CGROUP_WEIGHT_INVALID);
4214
4215 w = new0(CGroupIODeviceWeight, 1);
4216 if (!w)
4217 return log_oom();
4218
4219 w->path = TAKE_PTR(resolved);
4220 w->weight = u;
4221
4222 LIST_PREPEND(device_weights, c->io_device_weights, w);
4223 return 0;
4224 }
4225
4226 int config_parse_io_device_latency(
4227 const char *unit,
4228 const char *filename,
4229 unsigned line,
4230 const char *section,
4231 unsigned section_line,
4232 const char *lvalue,
4233 int ltype,
4234 const char *rvalue,
4235 void *data,
4236 void *userdata) {
4237
4238 _cleanup_free_ char *path = NULL, *resolved = NULL;
4239 CGroupIODeviceLatency *l;
4240 CGroupContext *c = data;
4241 const char *p = rvalue;
4242 usec_t usec;
4243 int r;
4244
4245 assert(filename);
4246 assert(lvalue);
4247 assert(rvalue);
4248
4249 if (isempty(rvalue)) {
4250 while (c->io_device_latencies)
4251 cgroup_context_free_io_device_latency(c, c->io_device_latencies);
4252
4253 return 0;
4254 }
4255
4256 r = extract_first_word(&p, &path, NULL, EXTRACT_UNQUOTE);
4257 if (r == -ENOMEM)
4258 return log_oom();
4259 if (r < 0) {
4260 log_syntax(unit, LOG_WARNING, filename, line, r,
4261 "Failed to extract device path and latency from '%s', ignoring.", rvalue);
4262 return 0;
4263 }
4264 if (r == 0 || isempty(p)) {
4265 log_syntax(unit, LOG_WARNING, filename, line, 0,
4266 "Invalid device path or latency specified in '%s', ignoring.", rvalue);
4267 return 0;
4268 }
4269
4270 r = unit_path_printf(userdata, path, &resolved);
4271 if (r < 0) {
4272 log_syntax(unit, LOG_WARNING, filename, line, r,
4273 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
4274 return 0;
4275 }
4276
4277 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
4278 if (r < 0)
4279 return 0;
4280
4281 r = parse_sec(p, &usec);
4282 if (r < 0) {
4283 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse timer value, ignoring: %s", p);
4284 return 0;
4285 }
4286
4287 l = new0(CGroupIODeviceLatency, 1);
4288 if (!l)
4289 return log_oom();
4290
4291 l->path = TAKE_PTR(resolved);
4292 l->target_usec = usec;
4293
4294 LIST_PREPEND(device_latencies, c->io_device_latencies, l);
4295 return 0;
4296 }
4297
4298 int config_parse_io_limit(
4299 const char *unit,
4300 const char *filename,
4301 unsigned line,
4302 const char *section,
4303 unsigned section_line,
4304 const char *lvalue,
4305 int ltype,
4306 const char *rvalue,
4307 void *data,
4308 void *userdata) {
4309
4310 _cleanup_free_ char *path = NULL, *resolved = NULL;
4311 CGroupIODeviceLimit *l = NULL;
4312 CGroupContext *c = data;
4313 CGroupIOLimitType type;
4314 const char *p = rvalue;
4315 uint64_t num;
4316 int r;
4317
4318 assert(filename);
4319 assert(lvalue);
4320 assert(rvalue);
4321
4322 type = cgroup_io_limit_type_from_string(lvalue);
4323 assert(type >= 0);
4324
4325 if (isempty(rvalue)) {
4326 LIST_FOREACH(device_limits, t, c->io_device_limits)
4327 t->limits[type] = cgroup_io_limit_defaults[type];
4328 return 0;
4329 }
4330
4331 r = extract_first_word(&p, &path, NULL, EXTRACT_UNQUOTE);
4332 if (r == -ENOMEM)
4333 return log_oom();
4334 if (r < 0) {
4335 log_syntax(unit, LOG_WARNING, filename, line, r,
4336 "Failed to extract device node and bandwidth from '%s', ignoring.", rvalue);
4337 return 0;
4338 }
4339 if (r == 0 || isempty(p)) {
4340 log_syntax(unit, LOG_WARNING, filename, line, 0,
4341 "Invalid device node or bandwidth specified in '%s', ignoring.", rvalue);
4342 return 0;
4343 }
4344
4345 r = unit_path_printf(userdata, path, &resolved);
4346 if (r < 0) {
4347 log_syntax(unit, LOG_WARNING, filename, line, r,
4348 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
4349 return 0;
4350 }
4351
4352 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
4353 if (r < 0)
4354 return 0;
4355
4356 if (streq("infinity", p))
4357 num = CGROUP_LIMIT_MAX;
4358 else {
4359 r = parse_size(p, 1000, &num);
4360 if (r < 0 || num <= 0) {
4361 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid IO limit '%s', ignoring.", p);
4362 return 0;
4363 }
4364 }
4365
4366 LIST_FOREACH(device_limits, t, c->io_device_limits)
4367 if (path_equal(resolved, t->path)) {
4368 l = t;
4369 break;
4370 }
4371
4372 if (!l) {
4373 CGroupIOLimitType ttype;
4374
4375 l = new0(CGroupIODeviceLimit, 1);
4376 if (!l)
4377 return log_oom();
4378
4379 l->path = TAKE_PTR(resolved);
4380 for (ttype = 0; ttype < _CGROUP_IO_LIMIT_TYPE_MAX; ttype++)
4381 l->limits[ttype] = cgroup_io_limit_defaults[ttype];
4382
4383 LIST_PREPEND(device_limits, c->io_device_limits, l);
4384 }
4385
4386 l->limits[type] = num;
4387
4388 return 0;
4389 }
4390
4391 int config_parse_blockio_device_weight(
4392 const char *unit,
4393 const char *filename,
4394 unsigned line,
4395 const char *section,
4396 unsigned section_line,
4397 const char *lvalue,
4398 int ltype,
4399 const char *rvalue,
4400 void *data,
4401 void *userdata) {
4402
4403 _cleanup_free_ char *path = NULL, *resolved = NULL;
4404 CGroupBlockIODeviceWeight *w;
4405 CGroupContext *c = data;
4406 const char *p = rvalue;
4407 uint64_t u;
4408 int r;
4409
4410 assert(filename);
4411 assert(lvalue);
4412 assert(rvalue);
4413
4414 if (isempty(rvalue)) {
4415 while (c->blockio_device_weights)
4416 cgroup_context_free_blockio_device_weight(c, c->blockio_device_weights);
4417
4418 return 0;
4419 }
4420
4421 r = extract_first_word(&p, &path, NULL, EXTRACT_UNQUOTE);
4422 if (r == -ENOMEM)
4423 return log_oom();
4424 if (r < 0) {
4425 log_syntax(unit, LOG_WARNING, filename, line, r,
4426 "Failed to extract device node and weight from '%s', ignoring.", rvalue);
4427 return 0;
4428 }
4429 if (r == 0 || isempty(p)) {
4430 log_syntax(unit, LOG_WARNING, filename, line, 0,
4431 "Invalid device node or weight specified in '%s', ignoring.", rvalue);
4432 return 0;
4433 }
4434
4435 r = unit_path_printf(userdata, path, &resolved);
4436 if (r < 0) {
4437 log_syntax(unit, LOG_WARNING, filename, line, r,
4438 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
4439 return 0;
4440 }
4441
4442 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
4443 if (r < 0)
4444 return 0;
4445
4446 r = cg_blkio_weight_parse(p, &u);
4447 if (r < 0) {
4448 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid block IO weight '%s', ignoring: %m", p);
4449 return 0;
4450 }
4451
4452 assert(u != CGROUP_BLKIO_WEIGHT_INVALID);
4453
4454 w = new0(CGroupBlockIODeviceWeight, 1);
4455 if (!w)
4456 return log_oom();
4457
4458 w->path = TAKE_PTR(resolved);
4459 w->weight = u;
4460
4461 LIST_PREPEND(device_weights, c->blockio_device_weights, w);
4462 return 0;
4463 }
4464
4465 int config_parse_blockio_bandwidth(
4466 const char *unit,
4467 const char *filename,
4468 unsigned line,
4469 const char *section,
4470 unsigned section_line,
4471 const char *lvalue,
4472 int ltype,
4473 const char *rvalue,
4474 void *data,
4475 void *userdata) {
4476
4477 _cleanup_free_ char *path = NULL, *resolved = NULL;
4478 CGroupBlockIODeviceBandwidth *b = NULL;
4479 CGroupContext *c = data;
4480 const char *p = rvalue;
4481 uint64_t bytes;
4482 bool read;
4483 int r;
4484
4485 assert(filename);
4486 assert(lvalue);
4487 assert(rvalue);
4488
4489 read = streq("BlockIOReadBandwidth", lvalue);
4490
4491 if (isempty(rvalue)) {
4492 LIST_FOREACH(device_bandwidths, t, c->blockio_device_bandwidths) {
4493 t->rbps = CGROUP_LIMIT_MAX;
4494 t->wbps = CGROUP_LIMIT_MAX;
4495 }
4496 return 0;
4497 }
4498
4499 r = extract_first_word(&p, &path, NULL, EXTRACT_UNQUOTE);
4500 if (r == -ENOMEM)
4501 return log_oom();
4502 if (r < 0) {
4503 log_syntax(unit, LOG_WARNING, filename, line, r,
4504 "Failed to extract device node and bandwidth from '%s', ignoring.", rvalue);
4505 return 0;
4506 }
4507 if (r == 0 || isempty(p)) {
4508 log_syntax(unit, LOG_WARNING, filename, line, 0,
4509 "Invalid device node or bandwidth specified in '%s', ignoring.", rvalue);
4510 return 0;
4511 }
4512
4513 r = unit_path_printf(userdata, path, &resolved);
4514 if (r < 0) {
4515 log_syntax(unit, LOG_WARNING, filename, line, r,
4516 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
4517 return 0;
4518 }
4519
4520 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
4521 if (r < 0)
4522 return 0;
4523
4524 r = parse_size(p, 1000, &bytes);
4525 if (r < 0 || bytes <= 0) {
4526 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid Block IO Bandwidth '%s', ignoring.", p);
4527 return 0;
4528 }
4529
4530 LIST_FOREACH(device_bandwidths, t, c->blockio_device_bandwidths)
4531 if (path_equal(resolved, t->path)) {
4532 b = t;
4533 break;
4534 }
4535
4536 if (!b) {
4537 b = new0(CGroupBlockIODeviceBandwidth, 1);
4538 if (!b)
4539 return log_oom();
4540
4541 b->path = TAKE_PTR(resolved);
4542 b->rbps = CGROUP_LIMIT_MAX;
4543 b->wbps = CGROUP_LIMIT_MAX;
4544
4545 LIST_PREPEND(device_bandwidths, c->blockio_device_bandwidths, b);
4546 }
4547
4548 if (read)
4549 b->rbps = bytes;
4550 else
4551 b->wbps = bytes;
4552
4553 return 0;
4554 }
4555
4556 int config_parse_job_mode_isolate(
4557 const char *unit,
4558 const char *filename,
4559 unsigned line,
4560 const char *section,
4561 unsigned section_line,
4562 const char *lvalue,
4563 int ltype,
4564 const char *rvalue,
4565 void *data,
4566 void *userdata) {
4567
4568 JobMode *m = data;
4569 int r;
4570
4571 assert(filename);
4572 assert(lvalue);
4573 assert(rvalue);
4574
4575 r = parse_boolean(rvalue);
4576 if (r < 0) {
4577 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse boolean, ignoring: %s", rvalue);
4578 return 0;
4579 }
4580
4581 log_notice("%s is deprecated. Please use OnFailureJobMode= instead", lvalue);
4582
4583 *m = r ? JOB_ISOLATE : JOB_REPLACE;
4584 return 0;
4585 }
4586
4587 int config_parse_exec_directories(
4588 const char *unit,
4589 const char *filename,
4590 unsigned line,
4591 const char *section,
4592 unsigned section_line,
4593 const char *lvalue,
4594 int ltype,
4595 const char *rvalue,
4596 void *data,
4597 void *userdata) {
4598
4599 ExecDirectory *ed = data;
4600 const Unit *u = userdata;
4601 int r;
4602
4603 assert(filename);
4604 assert(lvalue);
4605 assert(rvalue);
4606 assert(data);
4607
4608 if (isempty(rvalue)) {
4609 /* Empty assignment resets the list */
4610 exec_directory_done(ed);
4611 return 0;
4612 }
4613
4614 for (const char *p = rvalue;;) {
4615 _cleanup_free_ char *tuple = NULL;
4616
4617 r = extract_first_word(&p, &tuple, NULL, EXTRACT_UNQUOTE|EXTRACT_RETAIN_ESCAPE);
4618 if (r == -ENOMEM)
4619 return log_oom();
4620 if (r < 0) {
4621 log_syntax(unit, LOG_WARNING, filename, line, r,
4622 "Invalid syntax %s=%s, ignoring: %m", lvalue, rvalue);
4623 return 0;
4624 }
4625 if (r == 0)
4626 return 0;
4627
4628 _cleanup_free_ char *src = NULL, *dest = NULL;
4629 const char *q = tuple;
4630 r = extract_many_words(&q, ":", EXTRACT_CUNESCAPE|EXTRACT_UNESCAPE_SEPARATORS, &src, &dest, NULL);
4631 if (r == -ENOMEM)
4632 return log_oom();
4633 if (r <= 0) {
4634 log_syntax(unit, LOG_WARNING, filename, line, r ?: SYNTHETIC_ERRNO(EINVAL),
4635 "Invalid syntax in %s=, ignoring: %s", lvalue, tuple);
4636 return 0;
4637 }
4638
4639 _cleanup_free_ char *sresolved = NULL;
4640 r = unit_path_printf(u, src, &sresolved);
4641 if (r < 0) {
4642 log_syntax(unit, LOG_WARNING, filename, line, r,
4643 "Failed to resolve unit specifiers in \"%s\", ignoring: %m", src);
4644 continue;
4645 }
4646
4647 r = path_simplify_and_warn(sresolved, PATH_CHECK_RELATIVE, unit, filename, line, lvalue);
4648 if (r < 0)
4649 continue;
4650
4651 if (path_startswith(sresolved, "private")) {
4652 log_syntax(unit, LOG_WARNING, filename, line, 0,
4653 "%s= path can't be 'private', ignoring assignment: %s", lvalue, tuple);
4654 continue;
4655 }
4656
4657 /* For State and Runtime directories we support an optional destination parameter, which
4658 * will be used to create a symlink to the source. */
4659 _cleanup_strv_free_ char **symlinks = NULL;
4660 if (!isempty(dest)) {
4661 _cleanup_free_ char *dresolved = NULL;
4662
4663 if (streq(lvalue, "ConfigurationDirectory")) {
4664 log_syntax(unit, LOG_WARNING, filename, line, 0,
4665 "Destination parameter is not supported for ConfigurationDirectory, ignoring: %s", tuple);
4666 continue;
4667 }
4668
4669 r = unit_path_printf(u, dest, &dresolved);
4670 if (r < 0) {
4671 log_syntax(unit, LOG_WARNING, filename, line, r,
4672 "Failed to resolve unit specifiers in \"%s\", ignoring: %m", dest);
4673 continue;
4674 }
4675
4676 r = path_simplify_and_warn(dresolved, PATH_CHECK_RELATIVE, unit, filename, line, lvalue);
4677 if (r < 0)
4678 continue;
4679
4680 r = strv_consume(&symlinks, TAKE_PTR(dresolved));
4681 if (r < 0)
4682 return log_oom();
4683 }
4684
4685 r = exec_directory_add(&ed->items, &ed->n_items, sresolved, symlinks);
4686 if (r < 0)
4687 return log_oom();
4688 }
4689 }
4690
4691 int config_parse_set_credential(
4692 const char *unit,
4693 const char *filename,
4694 unsigned line,
4695 const char *section,
4696 unsigned section_line,
4697 const char *lvalue,
4698 int ltype,
4699 const char *rvalue,
4700 void *data,
4701 void *userdata) {
4702
4703 _cleanup_free_ char *word = NULL, *k = NULL;
4704 _cleanup_free_ void *d = NULL;
4705 ExecContext *context = data;
4706 ExecSetCredential *old;
4707 Unit *u = userdata;
4708 bool encrypted = ltype;
4709 const char *p = rvalue;
4710 size_t size;
4711 int r;
4712
4713 assert(filename);
4714 assert(lvalue);
4715 assert(rvalue);
4716 assert(context);
4717
4718 if (isempty(rvalue)) {
4719 /* Empty assignment resets the list */
4720 context->set_credentials = hashmap_free(context->set_credentials);
4721 return 0;
4722 }
4723
4724 r = extract_first_word(&p, &word, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
4725 if (r == -ENOMEM)
4726 return log_oom();
4727 if (r < 0) {
4728 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to extract credential name, ignoring: %s", rvalue);
4729 return 0;
4730 }
4731 if (r == 0 || isempty(p)) {
4732 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid syntax, ignoring: %s", rvalue);
4733 return 0;
4734 }
4735
4736 r = unit_cred_printf(u, word, &k);
4737 if (r < 0) {
4738 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in \"%s\", ignoring: %m", word);
4739 return 0;
4740 }
4741 if (!credential_name_valid(k)) {
4742 log_syntax(unit, LOG_WARNING, filename, line, 0, "Credential name \"%s\" not valid, ignoring.", k);
4743 return 0;
4744 }
4745
4746 if (encrypted) {
4747 r = unbase64mem_full(p, SIZE_MAX, true, &d, &size);
4748 if (r < 0) {
4749 log_syntax(unit, LOG_WARNING, filename, line, r, "Encrypted credential data not valid Base64 data, ignoring.");
4750 return 0;
4751 }
4752 } else {
4753 char *unescaped;
4754 ssize_t l;
4755
4756 /* We support escape codes here, so that users can insert trailing \n if they like */
4757 l = cunescape(p, UNESCAPE_ACCEPT_NUL, &unescaped);
4758 if (l < 0) {
4759 log_syntax(unit, LOG_WARNING, filename, line, l, "Can't unescape \"%s\", ignoring: %m", p);
4760 return 0;
4761 }
4762
4763 d = unescaped;
4764 size = l;
4765 }
4766
4767 old = hashmap_get(context->set_credentials, k);
4768 if (old) {
4769 free_and_replace(old->data, d);
4770 old->size = size;
4771 old->encrypted = encrypted;
4772 } else {
4773 _cleanup_(exec_set_credential_freep) ExecSetCredential *sc = NULL;
4774
4775 sc = new(ExecSetCredential, 1);
4776 if (!sc)
4777 return log_oom();
4778
4779 *sc = (ExecSetCredential) {
4780 .id = TAKE_PTR(k),
4781 .data = TAKE_PTR(d),
4782 .size = size,
4783 .encrypted = encrypted,
4784 };
4785
4786 r = hashmap_ensure_put(&context->set_credentials, &exec_set_credential_hash_ops, sc->id, sc);
4787 if (r == -ENOMEM)
4788 return log_oom();
4789 if (r < 0) {
4790 log_syntax(unit, LOG_WARNING, filename, line, r,
4791 "Duplicated credential value '%s', ignoring assignment: %s", sc->id, rvalue);
4792 return 0;
4793 }
4794
4795 TAKE_PTR(sc);
4796 }
4797
4798 return 0;
4799 }
4800
4801 int config_parse_load_credential(
4802 const char *unit,
4803 const char *filename,
4804 unsigned line,
4805 const char *section,
4806 unsigned section_line,
4807 const char *lvalue,
4808 int ltype,
4809 const char *rvalue,
4810 void *data,
4811 void *userdata) {
4812
4813 _cleanup_free_ char *word = NULL, *k = NULL, *q = NULL;
4814 ExecContext *context = data;
4815 ExecLoadCredential *old;
4816 bool encrypted = ltype;
4817 Unit *u = userdata;
4818 const char *p;
4819 int r;
4820
4821 assert(filename);
4822 assert(lvalue);
4823 assert(rvalue);
4824 assert(context);
4825
4826 if (isempty(rvalue)) {
4827 /* Empty assignment resets the list */
4828 context->load_credentials = hashmap_free(context->load_credentials);
4829 return 0;
4830 }
4831
4832 p = rvalue;
4833 r = extract_first_word(&p, &word, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
4834 if (r == -ENOMEM)
4835 return log_oom();
4836 if (r <= 0) {
4837 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
4838 return 0;
4839 }
4840
4841 r = unit_cred_printf(u, word, &k);
4842 if (r < 0) {
4843 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in \"%s\", ignoring: %m", word);
4844 return 0;
4845 }
4846 if (!credential_name_valid(k)) {
4847 log_syntax(unit, LOG_WARNING, filename, line, 0, "Credential name \"%s\" not valid, ignoring.", k);
4848 return 0;
4849 }
4850
4851 if (isempty(p)) {
4852 /* If only one field is specified take it as shortcut for inheriting a credential named
4853 * the same way from our parent */
4854 q = strdup(k);
4855 if (!q)
4856 return log_oom();
4857 } else {
4858 r = unit_path_printf(u, p, &q);
4859 if (r < 0) {
4860 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in \"%s\", ignoring: %m", p);
4861 return 0;
4862 }
4863 if (path_is_absolute(q) ? !path_is_normalized(q) : !credential_name_valid(q)) {
4864 log_syntax(unit, LOG_WARNING, filename, line, 0, "Credential source \"%s\" not valid, ignoring.", q);
4865 return 0;
4866 }
4867 }
4868
4869 old = hashmap_get(context->load_credentials, k);
4870 if (old) {
4871 free_and_replace(old->path, q);
4872 old->encrypted = encrypted;
4873 } else {
4874 _cleanup_(exec_load_credential_freep) ExecLoadCredential *lc = NULL;
4875
4876 lc = new(ExecLoadCredential, 1);
4877 if (!lc)
4878 return log_oom();
4879
4880 *lc = (ExecLoadCredential) {
4881 .id = TAKE_PTR(k),
4882 .path = TAKE_PTR(q),
4883 .encrypted = encrypted,
4884 };
4885
4886 r = hashmap_ensure_put(&context->load_credentials, &exec_load_credential_hash_ops, lc->id, lc);
4887 if (r == -ENOMEM)
4888 return log_oom();
4889 if (r < 0) {
4890 log_syntax(unit, LOG_WARNING, filename, line, r,
4891 "Duplicated credential value '%s', ignoring assignment: %s", lc->id, rvalue);
4892 return 0;
4893 }
4894
4895 TAKE_PTR(lc);
4896 }
4897
4898 return 0;
4899 }
4900
4901 int config_parse_set_status(
4902 const char *unit,
4903 const char *filename,
4904 unsigned line,
4905 const char *section,
4906 unsigned section_line,
4907 const char *lvalue,
4908 int ltype,
4909 const char *rvalue,
4910 void *data,
4911 void *userdata) {
4912
4913 ExitStatusSet *status_set = data;
4914 int r;
4915
4916 assert(filename);
4917 assert(lvalue);
4918 assert(rvalue);
4919 assert(status_set);
4920
4921 /* Empty assignment resets the list */
4922 if (isempty(rvalue)) {
4923 exit_status_set_free(status_set);
4924 return 0;
4925 }
4926
4927 for (const char *p = rvalue;;) {
4928 _cleanup_free_ char *word = NULL;
4929 Bitmap *bitmap;
4930
4931 r = extract_first_word(&p, &word, NULL, 0);
4932 if (r == -ENOMEM)
4933 return log_oom();
4934 if (r < 0) {
4935 log_syntax(unit, LOG_WARNING, filename, line, r,
4936 "Failed to parse %s=%s, ignoring: %m", lvalue, rvalue);
4937 return 0;
4938 }
4939 if (r == 0)
4940 return 0;
4941
4942 /* We need to call exit_status_from_string() first, because we want
4943 * to parse numbers as exit statuses, not signals. */
4944
4945 r = exit_status_from_string(word);
4946 if (r >= 0) {
4947 assert(r >= 0 && r < 256);
4948 bitmap = &status_set->status;
4949 } else {
4950 r = signal_from_string(word);
4951 if (r < 0) {
4952 log_syntax(unit, LOG_WARNING, filename, line, r,
4953 "Failed to parse value, ignoring: %s", word);
4954 continue;
4955 }
4956 bitmap = &status_set->signal;
4957 }
4958
4959 r = bitmap_set(bitmap, r);
4960 if (r < 0)
4961 log_syntax(unit, LOG_WARNING, filename, line, r,
4962 "Failed to set signal or status %s, ignoring: %m", word);
4963 }
4964 }
4965
4966 int config_parse_namespace_path_strv(
4967 const char *unit,
4968 const char *filename,
4969 unsigned line,
4970 const char *section,
4971 unsigned section_line,
4972 const char *lvalue,
4973 int ltype,
4974 const char *rvalue,
4975 void *data,
4976 void *userdata) {
4977
4978 const Unit *u = userdata;
4979 char*** sv = data;
4980 int r;
4981
4982 assert(filename);
4983 assert(lvalue);
4984 assert(rvalue);
4985 assert(data);
4986
4987 if (isempty(rvalue)) {
4988 /* Empty assignment resets the list */
4989 *sv = strv_free(*sv);
4990 return 0;
4991 }
4992
4993 for (const char *p = rvalue;;) {
4994 _cleanup_free_ char *word = NULL, *resolved = NULL, *joined = NULL;
4995 const char *w;
4996 bool ignore_enoent = false, shall_prefix = false;
4997
4998 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
4999 if (r == -ENOMEM)
5000 return log_oom();
5001 if (r < 0) {
5002 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to extract first word, ignoring: %s", rvalue);
5003 return 0;
5004 }
5005 if (r == 0)
5006 break;
5007
5008 w = word;
5009 if (startswith(w, "-")) {
5010 ignore_enoent = true;
5011 w++;
5012 }
5013 if (startswith(w, "+")) {
5014 shall_prefix = true;
5015 w++;
5016 }
5017
5018 r = unit_path_printf(u, w, &resolved);
5019 if (r < 0) {
5020 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in %s: %m", w);
5021 continue;
5022 }
5023
5024 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
5025 if (r < 0)
5026 continue;
5027
5028 joined = strjoin(ignore_enoent ? "-" : "",
5029 shall_prefix ? "+" : "",
5030 resolved);
5031
5032 r = strv_push(sv, joined);
5033 if (r < 0)
5034 return log_oom();
5035
5036 joined = NULL;
5037 }
5038
5039 return 0;
5040 }
5041
5042 int config_parse_temporary_filesystems(
5043 const char *unit,
5044 const char *filename,
5045 unsigned line,
5046 const char *section,
5047 unsigned section_line,
5048 const char *lvalue,
5049 int ltype,
5050 const char *rvalue,
5051 void *data,
5052 void *userdata) {
5053
5054 const Unit *u = userdata;
5055 ExecContext *c = data;
5056 int r;
5057
5058 assert(filename);
5059 assert(lvalue);
5060 assert(rvalue);
5061 assert(data);
5062
5063 if (isempty(rvalue)) {
5064 /* Empty assignment resets the list */
5065 temporary_filesystem_free_many(c->temporary_filesystems, c->n_temporary_filesystems);
5066 c->temporary_filesystems = NULL;
5067 c->n_temporary_filesystems = 0;
5068 return 0;
5069 }
5070
5071 for (const char *p = rvalue;;) {
5072 _cleanup_free_ char *word = NULL, *path = NULL, *resolved = NULL;
5073 const char *w;
5074
5075 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
5076 if (r == -ENOMEM)
5077 return log_oom();
5078 if (r < 0) {
5079 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to extract first word, ignoring: %s", rvalue);
5080 return 0;
5081 }
5082 if (r == 0)
5083 return 0;
5084
5085 w = word;
5086 r = extract_first_word(&w, &path, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
5087 if (r == -ENOMEM)
5088 return log_oom();
5089 if (r < 0) {
5090 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to extract first word, ignoring: %s", word);
5091 continue;
5092 }
5093 if (r == 0) {
5094 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid syntax, ignoring: %s", word);
5095 continue;
5096 }
5097
5098 r = unit_path_printf(u, path, &resolved);
5099 if (r < 0) {
5100 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", path);
5101 continue;
5102 }
5103
5104 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
5105 if (r < 0)
5106 continue;
5107
5108 r = temporary_filesystem_add(&c->temporary_filesystems, &c->n_temporary_filesystems, resolved, w);
5109 if (r < 0)
5110 return log_oom();
5111 }
5112 }
5113
5114 int config_parse_bind_paths(
5115 const char *unit,
5116 const char *filename,
5117 unsigned line,
5118 const char *section,
5119 unsigned section_line,
5120 const char *lvalue,
5121 int ltype,
5122 const char *rvalue,
5123 void *data,
5124 void *userdata) {
5125
5126 ExecContext *c = data;
5127 const Unit *u = userdata;
5128 int r;
5129
5130 assert(filename);
5131 assert(lvalue);
5132 assert(rvalue);
5133 assert(data);
5134
5135 if (isempty(rvalue)) {
5136 /* Empty assignment resets the list */
5137 bind_mount_free_many(c->bind_mounts, c->n_bind_mounts);
5138 c->bind_mounts = NULL;
5139 c->n_bind_mounts = 0;
5140 return 0;
5141 }
5142
5143 for (const char *p = rvalue;;) {
5144 _cleanup_free_ char *source = NULL, *destination = NULL;
5145 _cleanup_free_ char *sresolved = NULL, *dresolved = NULL;
5146 char *s = NULL, *d = NULL;
5147 bool rbind = true, ignore_enoent = false;
5148
5149 r = extract_first_word(&p, &source, ":" WHITESPACE, EXTRACT_UNQUOTE|EXTRACT_DONT_COALESCE_SEPARATORS);
5150 if (r == -ENOMEM)
5151 return log_oom();
5152 if (r < 0) {
5153 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse %s, ignoring: %s", lvalue, rvalue);
5154 return 0;
5155 }
5156 if (r == 0)
5157 break;
5158
5159 r = unit_full_printf_full(u, source, PATH_MAX, &sresolved);
5160 if (r < 0) {
5161 log_syntax(unit, LOG_WARNING, filename, line, r,
5162 "Failed to resolve unit specifiers in \"%s\", ignoring: %m", source);
5163 continue;
5164 }
5165
5166 s = sresolved;
5167 if (s[0] == '-') {
5168 ignore_enoent = true;
5169 s++;
5170 }
5171
5172 r = path_simplify_and_warn(s, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
5173 if (r < 0)
5174 continue;
5175
5176 /* Optionally, the destination is specified. */
5177 if (p && p[-1] == ':') {
5178 r = extract_first_word(&p, &destination, ":" WHITESPACE, EXTRACT_UNQUOTE|EXTRACT_DONT_COALESCE_SEPARATORS);
5179 if (r == -ENOMEM)
5180 return log_oom();
5181 if (r < 0) {
5182 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse %s, ignoring: %s", lvalue, rvalue);
5183 return 0;
5184 }
5185 if (r == 0) {
5186 log_syntax(unit, LOG_WARNING, filename, line, 0, "Missing argument after ':', ignoring: %s", s);
5187 continue;
5188 }
5189
5190 r = unit_path_printf(u, destination, &dresolved);
5191 if (r < 0) {
5192 log_syntax(unit, LOG_WARNING, filename, line, r,
5193 "Failed to resolve specifiers in \"%s\", ignoring: %m", destination);
5194 continue;
5195 }
5196
5197 r = path_simplify_and_warn(dresolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
5198 if (r < 0)
5199 continue;
5200
5201 d = dresolved;
5202
5203 /* Optionally, there's also a short option string specified */
5204 if (p && p[-1] == ':') {
5205 _cleanup_free_ char *options = NULL;
5206
5207 r = extract_first_word(&p, &options, NULL, EXTRACT_UNQUOTE);
5208 if (r == -ENOMEM)
5209 return log_oom();
5210 if (r < 0) {
5211 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse %s=, ignoring: %s", lvalue, rvalue);
5212 return 0;
5213 }
5214
5215 if (isempty(options) || streq(options, "rbind"))
5216 rbind = true;
5217 else if (streq(options, "norbind"))
5218 rbind = false;
5219 else {
5220 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid option string, ignoring setting: %s", options);
5221 continue;
5222 }
5223 }
5224 } else
5225 d = s;
5226
5227 r = bind_mount_add(&c->bind_mounts, &c->n_bind_mounts,
5228 &(BindMount) {
5229 .source = s,
5230 .destination = d,
5231 .read_only = !!strstr(lvalue, "ReadOnly"),
5232 .recursive = rbind,
5233 .ignore_enoent = ignore_enoent,
5234 });
5235 if (r < 0)
5236 return log_oom();
5237 }
5238
5239 return 0;
5240 }
5241
5242 int config_parse_mount_images(
5243 const char *unit,
5244 const char *filename,
5245 unsigned line,
5246 const char *section,
5247 unsigned section_line,
5248 const char *lvalue,
5249 int ltype,
5250 const char *rvalue,
5251 void *data,
5252 void *userdata) {
5253
5254 ExecContext *c = data;
5255 const Unit *u = userdata;
5256 int r;
5257
5258 assert(filename);
5259 assert(lvalue);
5260 assert(rvalue);
5261 assert(data);
5262
5263 if (isempty(rvalue)) {
5264 /* Empty assignment resets the list */
5265 c->mount_images = mount_image_free_many(c->mount_images, &c->n_mount_images);
5266 return 0;
5267 }
5268
5269 for (const char *p = rvalue;;) {
5270 _cleanup_(mount_options_free_allp) MountOptions *options = NULL;
5271 _cleanup_free_ char *first = NULL, *second = NULL, *tuple = NULL;
5272 _cleanup_free_ char *sresolved = NULL, *dresolved = NULL;
5273 const char *q = NULL;
5274 char *s = NULL;
5275 bool permissive = false;
5276
5277 r = extract_first_word(&p, &tuple, NULL, EXTRACT_UNQUOTE|EXTRACT_RETAIN_ESCAPE);
5278 if (r == -ENOMEM)
5279 return log_oom();
5280 if (r < 0) {
5281 log_syntax(unit, LOG_WARNING, filename, line, r,
5282 "Invalid syntax %s=%s, ignoring: %m", lvalue, rvalue);
5283 return 0;
5284 }
5285 if (r == 0)
5286 return 0;
5287
5288 q = tuple;
5289 r = extract_many_words(&q, ":", EXTRACT_CUNESCAPE|EXTRACT_UNESCAPE_SEPARATORS, &first, &second, NULL);
5290 if (r == -ENOMEM)
5291 return log_oom();
5292 if (r < 0) {
5293 log_syntax(unit, LOG_WARNING, filename, line, r,
5294 "Invalid syntax in %s=, ignoring: %s", lvalue, tuple);
5295 return 0;
5296 }
5297 if (r == 0)
5298 continue;
5299
5300 s = first;
5301 if (s[0] == '-') {
5302 permissive = true;
5303 s++;
5304 }
5305
5306 r = unit_path_printf(u, s, &sresolved);
5307 if (r < 0) {
5308 log_syntax(unit, LOG_WARNING, filename, line, r,
5309 "Failed to resolve unit specifiers in \"%s\", ignoring: %m", s);
5310 continue;
5311 }
5312
5313 r = path_simplify_and_warn(sresolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
5314 if (r < 0)
5315 continue;
5316
5317 if (isempty(second)) {
5318 log_syntax(unit, LOG_WARNING, filename, line, 0, "Missing destination in %s, ignoring: %s", lvalue, rvalue);
5319 continue;
5320 }
5321
5322 r = unit_path_printf(u, second, &dresolved);
5323 if (r < 0) {
5324 log_syntax(unit, LOG_WARNING, filename, line, r,
5325 "Failed to resolve specifiers in \"%s\", ignoring: %m", second);
5326 continue;
5327 }
5328
5329 r = path_simplify_and_warn(dresolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
5330 if (r < 0)
5331 continue;
5332
5333 for (;;) {
5334 _cleanup_free_ char *partition = NULL, *mount_options = NULL, *mount_options_resolved = NULL;
5335 MountOptions *o = NULL;
5336 PartitionDesignator partition_designator;
5337
5338 r = extract_many_words(&q, ":", EXTRACT_CUNESCAPE|EXTRACT_UNESCAPE_SEPARATORS, &partition, &mount_options, NULL);
5339 if (r == -ENOMEM)
5340 return log_oom();
5341 if (r < 0) {
5342 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", q);
5343 return 0;
5344 }
5345 if (r == 0)
5346 break;
5347 /* Single set of options, applying to the root partition/single filesystem */
5348 if (r == 1) {
5349 r = unit_full_printf(u, partition, &mount_options_resolved);
5350 if (r < 0) {
5351 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", first);
5352 continue;
5353 }
5354
5355 o = new(MountOptions, 1);
5356 if (!o)
5357 return log_oom();
5358 *o = (MountOptions) {
5359 .partition_designator = PARTITION_ROOT,
5360 .options = TAKE_PTR(mount_options_resolved),
5361 };
5362 LIST_APPEND(mount_options, options, o);
5363
5364 break;
5365 }
5366
5367 partition_designator = partition_designator_from_string(partition);
5368 if (partition_designator < 0) {
5369 log_syntax(unit, LOG_WARNING, filename, line, partition_designator,
5370 "Invalid partition name %s, ignoring", partition);
5371 continue;
5372 }
5373 r = unit_full_printf(u, mount_options, &mount_options_resolved);
5374 if (r < 0) {
5375 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", mount_options);
5376 continue;
5377 }
5378
5379 o = new(MountOptions, 1);
5380 if (!o)
5381 return log_oom();
5382 *o = (MountOptions) {
5383 .partition_designator = partition_designator,
5384 .options = TAKE_PTR(mount_options_resolved),
5385 };
5386 LIST_APPEND(mount_options, options, o);
5387 }
5388
5389 r = mount_image_add(&c->mount_images, &c->n_mount_images,
5390 &(MountImage) {
5391 .source = sresolved,
5392 .destination = dresolved,
5393 .mount_options = options,
5394 .ignore_enoent = permissive,
5395 .type = MOUNT_IMAGE_DISCRETE,
5396 });
5397 if (r < 0)
5398 return log_oom();
5399 }
5400 }
5401
5402 int config_parse_extension_images(
5403 const char *unit,
5404 const char *filename,
5405 unsigned line,
5406 const char *section,
5407 unsigned section_line,
5408 const char *lvalue,
5409 int ltype,
5410 const char *rvalue,
5411 void *data,
5412 void *userdata) {
5413
5414 ExecContext *c = data;
5415 const Unit *u = userdata;
5416 int r;
5417
5418 assert(filename);
5419 assert(lvalue);
5420 assert(rvalue);
5421 assert(data);
5422
5423 if (isempty(rvalue)) {
5424 /* Empty assignment resets the list */
5425 c->extension_images = mount_image_free_many(c->extension_images, &c->n_extension_images);
5426 return 0;
5427 }
5428
5429 for (const char *p = rvalue;;) {
5430 _cleanup_free_ char *source = NULL, *tuple = NULL, *sresolved = NULL;
5431 _cleanup_(mount_options_free_allp) MountOptions *options = NULL;
5432 bool permissive = false;
5433 const char *q = NULL;
5434 char *s = NULL;
5435
5436 r = extract_first_word(&p, &tuple, NULL, EXTRACT_UNQUOTE|EXTRACT_RETAIN_ESCAPE);
5437 if (r == -ENOMEM)
5438 return log_oom();
5439 if (r < 0) {
5440 log_syntax(unit, LOG_WARNING, filename, line, r,
5441 "Invalid syntax %s=%s, ignoring: %m", lvalue, rvalue);
5442 return 0;
5443 }
5444 if (r == 0)
5445 return 0;
5446
5447 q = tuple;
5448 r = extract_first_word(&q, &source, ":", EXTRACT_CUNESCAPE|EXTRACT_UNESCAPE_SEPARATORS);
5449 if (r == -ENOMEM)
5450 return log_oom();
5451 if (r < 0) {
5452 log_syntax(unit, LOG_WARNING, filename, line, r,
5453 "Invalid syntax in %s=, ignoring: %s", lvalue, tuple);
5454 return 0;
5455 }
5456 if (r == 0)
5457 continue;
5458
5459 s = source;
5460 if (s[0] == '-') {
5461 permissive = true;
5462 s++;
5463 }
5464
5465 r = unit_path_printf(u, s, &sresolved);
5466 if (r < 0) {
5467 log_syntax(unit, LOG_WARNING, filename, line, r,
5468 "Failed to resolve unit specifiers in \"%s\", ignoring: %m", s);
5469 continue;
5470 }
5471
5472 r = path_simplify_and_warn(sresolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
5473 if (r < 0)
5474 continue;
5475
5476 for (;;) {
5477 _cleanup_free_ char *partition = NULL, *mount_options = NULL, *mount_options_resolved = NULL;
5478 MountOptions *o = NULL;
5479 PartitionDesignator partition_designator;
5480
5481 r = extract_many_words(&q, ":", EXTRACT_CUNESCAPE|EXTRACT_UNESCAPE_SEPARATORS, &partition, &mount_options, NULL);
5482 if (r == -ENOMEM)
5483 return log_oom();
5484 if (r < 0) {
5485 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", q);
5486 return 0;
5487 }
5488 if (r == 0)
5489 break;
5490 /* Single set of options, applying to the root partition/single filesystem */
5491 if (r == 1) {
5492 r = unit_full_printf(u, partition, &mount_options_resolved);
5493 if (r < 0) {
5494 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", partition);
5495 continue;
5496 }
5497
5498 o = new(MountOptions, 1);
5499 if (!o)
5500 return log_oom();
5501 *o = (MountOptions) {
5502 .partition_designator = PARTITION_ROOT,
5503 .options = TAKE_PTR(mount_options_resolved),
5504 };
5505 LIST_APPEND(mount_options, options, o);
5506
5507 break;
5508 }
5509
5510 partition_designator = partition_designator_from_string(partition);
5511 if (partition_designator < 0) {
5512 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid partition name %s, ignoring", partition);
5513 continue;
5514 }
5515 r = unit_full_printf(u, mount_options, &mount_options_resolved);
5516 if (r < 0) {
5517 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", mount_options);
5518 continue;
5519 }
5520
5521 o = new(MountOptions, 1);
5522 if (!o)
5523 return log_oom();
5524 *o = (MountOptions) {
5525 .partition_designator = partition_designator,
5526 .options = TAKE_PTR(mount_options_resolved),
5527 };
5528 LIST_APPEND(mount_options, options, o);
5529 }
5530
5531 r = mount_image_add(&c->extension_images, &c->n_extension_images,
5532 &(MountImage) {
5533 .source = sresolved,
5534 .mount_options = options,
5535 .ignore_enoent = permissive,
5536 .type = MOUNT_IMAGE_EXTENSION,
5537 });
5538 if (r < 0)
5539 return log_oom();
5540 }
5541 }
5542
5543 int config_parse_job_timeout_sec(
5544 const char* unit,
5545 const char *filename,
5546 unsigned line,
5547 const char *section,
5548 unsigned section_line,
5549 const char *lvalue,
5550 int ltype,
5551 const char *rvalue,
5552 void *data,
5553 void *userdata) {
5554
5555 Unit *u = data;
5556 usec_t usec;
5557 int r;
5558
5559 assert(filename);
5560 assert(lvalue);
5561 assert(rvalue);
5562 assert(u);
5563
5564 r = parse_sec_fix_0(rvalue, &usec);
5565 if (r < 0) {
5566 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse JobTimeoutSec= parameter, ignoring: %s", rvalue);
5567 return 0;
5568 }
5569
5570 /* If the user explicitly changed JobTimeoutSec= also change JobRunningTimeoutSec=, for compatibility with old
5571 * versions. If JobRunningTimeoutSec= was explicitly set, avoid this however as whatever the user picked should
5572 * count. */
5573
5574 if (!u->job_running_timeout_set)
5575 u->job_running_timeout = usec;
5576
5577 u->job_timeout = usec;
5578
5579 return 0;
5580 }
5581
5582 int config_parse_job_running_timeout_sec(
5583 const char* unit,
5584 const char *filename,
5585 unsigned line,
5586 const char *section,
5587 unsigned section_line,
5588 const char *lvalue,
5589 int ltype,
5590 const char *rvalue,
5591 void *data,
5592 void *userdata) {
5593
5594 Unit *u = data;
5595 usec_t usec;
5596 int r;
5597
5598 assert(filename);
5599 assert(lvalue);
5600 assert(rvalue);
5601 assert(u);
5602
5603 r = parse_sec_fix_0(rvalue, &usec);
5604 if (r < 0) {
5605 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse JobRunningTimeoutSec= parameter, ignoring: %s", rvalue);
5606 return 0;
5607 }
5608
5609 u->job_running_timeout = usec;
5610 u->job_running_timeout_set = true;
5611
5612 return 0;
5613 }
5614
5615 int config_parse_emergency_action(
5616 const char* unit,
5617 const char *filename,
5618 unsigned line,
5619 const char *section,
5620 unsigned section_line,
5621 const char *lvalue,
5622 int ltype,
5623 const char *rvalue,
5624 void *data,
5625 void *userdata) {
5626
5627 Manager *m = NULL;
5628 EmergencyAction *x = data;
5629 int r;
5630
5631 assert(filename);
5632 assert(lvalue);
5633 assert(rvalue);
5634 assert(data);
5635
5636 if (unit)
5637 m = ((Unit*) userdata)->manager;
5638 else
5639 m = data;
5640
5641 r = parse_emergency_action(rvalue, MANAGER_IS_SYSTEM(m), x);
5642 if (r < 0) {
5643 if (r == -EOPNOTSUPP && MANAGER_IS_USER(m)) {
5644 /* Compat mode: remove for systemd 241. */
5645
5646 log_syntax(unit, LOG_INFO, filename, line, r,
5647 "%s= in user mode specified as \"%s\", using \"exit-force\" instead.",
5648 lvalue, rvalue);
5649 *x = EMERGENCY_ACTION_EXIT_FORCE;
5650 return 0;
5651 }
5652
5653 if (r == -EOPNOTSUPP)
5654 log_syntax(unit, LOG_WARNING, filename, line, r,
5655 "%s= specified as %s mode action, ignoring: %s",
5656 lvalue, MANAGER_IS_SYSTEM(m) ? "user" : "system", rvalue);
5657 else
5658 log_syntax(unit, LOG_WARNING, filename, line, r,
5659 "Failed to parse %s=, ignoring: %s", lvalue, rvalue);
5660 return 0;
5661 }
5662
5663 return 0;
5664 }
5665
5666 int config_parse_pid_file(
5667 const char *unit,
5668 const char *filename,
5669 unsigned line,
5670 const char *section,
5671 unsigned section_line,
5672 const char *lvalue,
5673 int ltype,
5674 const char *rvalue,
5675 void *data,
5676 void *userdata) {
5677
5678 _cleanup_free_ char *k = NULL, *n = NULL;
5679 const Unit *u = userdata;
5680 char **s = data;
5681 int r;
5682
5683 assert(filename);
5684 assert(lvalue);
5685 assert(rvalue);
5686 assert(u);
5687
5688 if (isempty(rvalue)) {
5689 /* An empty assignment removes already set value. */
5690 *s = mfree(*s);
5691 return 0;
5692 }
5693
5694 r = unit_path_printf(u, rvalue, &k);
5695 if (r < 0) {
5696 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
5697 return 0;
5698 }
5699
5700 /* If this is a relative path make it absolute by prefixing the /run */
5701 n = path_make_absolute(k, u->manager->prefix[EXEC_DIRECTORY_RUNTIME]);
5702 if (!n)
5703 return log_oom();
5704
5705 /* Check that the result is a sensible path */
5706 r = path_simplify_and_warn(n, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
5707 if (r < 0)
5708 return r;
5709
5710 r = patch_var_run(unit, filename, line, lvalue, &n);
5711 if (r < 0)
5712 return r;
5713
5714 free_and_replace(*s, n);
5715 return 0;
5716 }
5717
5718 int config_parse_exit_status(
5719 const char *unit,
5720 const char *filename,
5721 unsigned line,
5722 const char *section,
5723 unsigned section_line,
5724 const char *lvalue,
5725 int ltype,
5726 const char *rvalue,
5727 void *data,
5728 void *userdata) {
5729
5730 int *exit_status = data, r;
5731 uint8_t u;
5732
5733 assert(filename);
5734 assert(lvalue);
5735 assert(rvalue);
5736 assert(exit_status);
5737
5738 if (isempty(rvalue)) {
5739 *exit_status = -1;
5740 return 0;
5741 }
5742
5743 r = safe_atou8(rvalue, &u);
5744 if (r < 0) {
5745 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse exit status '%s', ignoring: %m", rvalue);
5746 return 0;
5747 }
5748
5749 *exit_status = u;
5750 return 0;
5751 }
5752
5753 int config_parse_disable_controllers(
5754 const char *unit,
5755 const char *filename,
5756 unsigned line,
5757 const char *section,
5758 unsigned section_line,
5759 const char *lvalue,
5760 int ltype,
5761 const char *rvalue,
5762 void *data,
5763 void *userdata) {
5764
5765 int r;
5766 CGroupContext *c = data;
5767 CGroupMask disabled_mask;
5768
5769 /* 1. If empty, make all controllers eligible for use again.
5770 * 2. If non-empty, merge all listed controllers, space separated. */
5771
5772 if (isempty(rvalue)) {
5773 c->disable_controllers = 0;
5774 return 0;
5775 }
5776
5777 r = cg_mask_from_string(rvalue, &disabled_mask);
5778 if (r < 0 || disabled_mask <= 0) {
5779 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid cgroup string: %s, ignoring", rvalue);
5780 return 0;
5781 }
5782
5783 c->disable_controllers |= disabled_mask;
5784
5785 return 0;
5786 }
5787
5788 int config_parse_ip_filter_bpf_progs(
5789 const char *unit,
5790 const char *filename,
5791 unsigned line,
5792 const char *section,
5793 unsigned section_line,
5794 const char *lvalue,
5795 int ltype,
5796 const char *rvalue,
5797 void *data,
5798 void *userdata) {
5799
5800 _cleanup_free_ char *resolved = NULL;
5801 const Unit *u = userdata;
5802 char ***paths = data;
5803 int r;
5804
5805 assert(filename);
5806 assert(lvalue);
5807 assert(rvalue);
5808 assert(paths);
5809
5810 if (isempty(rvalue)) {
5811 *paths = strv_free(*paths);
5812 return 0;
5813 }
5814
5815 r = unit_path_printf(u, rvalue, &resolved);
5816 if (r < 0) {
5817 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
5818 return 0;
5819 }
5820
5821 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
5822 if (r < 0)
5823 return 0;
5824
5825 if (strv_contains(*paths, resolved))
5826 return 0;
5827
5828 r = strv_extend(paths, resolved);
5829 if (r < 0)
5830 return log_oom();
5831
5832 r = bpf_firewall_supported();
5833 if (r < 0)
5834 return r;
5835 if (r != BPF_FIREWALL_SUPPORTED_WITH_MULTI) {
5836 static bool warned = false;
5837
5838 log_full(warned ? LOG_DEBUG : LOG_WARNING,
5839 "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"
5840 "Starting this unit will fail! (This warning is only shown for the first loaded unit using IP firewalling.)", filename, line, lvalue, rvalue);
5841
5842 warned = true;
5843 }
5844
5845 return 0;
5846 }
5847
5848 int config_parse_bpf_foreign_program(
5849 const char *unit,
5850 const char *filename,
5851 unsigned line,
5852 const char *section,
5853 unsigned section_line,
5854 const char *lvalue,
5855 int ltype,
5856 const char *rvalue,
5857 void *data,
5858 void *userdata) {
5859 _cleanup_free_ char *resolved = NULL, *word = NULL;
5860 CGroupContext *c = data;
5861 const char *p = rvalue;
5862 Unit *u = userdata;
5863 int attach_type, r;
5864
5865 assert(filename);
5866 assert(lvalue);
5867 assert(rvalue);
5868
5869 if (isempty(rvalue)) {
5870 while (c->bpf_foreign_programs)
5871 cgroup_context_remove_bpf_foreign_program(c, c->bpf_foreign_programs);
5872
5873 return 0;
5874 }
5875
5876 r = extract_first_word(&p, &word, ":", 0);
5877 if (r == -ENOMEM)
5878 return log_oom();
5879 if (r < 0) {
5880 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse foreign BPF program, ignoring: %s", rvalue);
5881 return 0;
5882 }
5883 if (r == 0 || isempty(p)) {
5884 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid syntax in %s=, ignoring: %s", lvalue, rvalue);
5885 return 0;
5886 }
5887
5888 attach_type = bpf_cgroup_attach_type_from_string(word);
5889 if (attach_type < 0) {
5890 log_syntax(unit, LOG_WARNING, filename, line, 0, "Unknown BPF attach type=%s, ignoring: %s", word, rvalue);
5891 return 0;
5892 }
5893
5894 r = unit_path_printf(u, p, &resolved);
5895 if (r < 0) {
5896 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %s", p, rvalue);
5897 return 0;
5898 }
5899
5900 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
5901 if (r < 0)
5902 return 0;
5903
5904 r = cgroup_add_bpf_foreign_program(c, attach_type, resolved);
5905 if (r < 0)
5906 return log_error_errno(r, "Failed to add foreign BPF program to cgroup context: %m");
5907
5908 return 0;
5909 }
5910
5911 int config_parse_cgroup_socket_bind(
5912 const char *unit,
5913 const char *filename,
5914 unsigned line,
5915 const char *section,
5916 unsigned section_line,
5917 const char *lvalue,
5918 int ltype,
5919 const char *rvalue,
5920 void *data,
5921 void *userdata) {
5922 _cleanup_free_ CGroupSocketBindItem *item = NULL;
5923 CGroupSocketBindItem **head = data;
5924 uint16_t nr_ports, port_min;
5925 int af, ip_protocol, r;
5926
5927 if (isempty(rvalue)) {
5928 cgroup_context_remove_socket_bind(head);
5929 return 0;
5930 }
5931
5932 r = parse_socket_bind_item(rvalue, &af, &ip_protocol, &nr_ports, &port_min);
5933 if (r == -ENOMEM)
5934 return log_oom();
5935 if (r < 0) {
5936 log_syntax(unit, LOG_WARNING, filename, line, r,
5937 "Unable to parse %s= assignment, ignoring: %s", lvalue, rvalue);
5938 return 0;
5939 }
5940
5941 item = new(CGroupSocketBindItem, 1);
5942 if (!item)
5943 return log_oom();
5944 *item = (CGroupSocketBindItem) {
5945 .address_family = af,
5946 .ip_protocol = ip_protocol,
5947 .nr_ports = nr_ports,
5948 .port_min = port_min,
5949 };
5950
5951 LIST_PREPEND(socket_bind_items, *head, TAKE_PTR(item));
5952
5953 return 0;
5954 }
5955
5956 int config_parse_restrict_network_interfaces(
5957 const char *unit,
5958 const char *filename,
5959 unsigned line,
5960 const char *section,
5961 unsigned section_line,
5962 const char *lvalue,
5963 int ltype,
5964 const char *rvalue,
5965 void *data,
5966 void *userdata) {
5967 CGroupContext *c = data;
5968 bool is_allow_rule = true;
5969 int r;
5970
5971 assert(filename);
5972 assert(lvalue);
5973 assert(rvalue);
5974 assert(data);
5975
5976 if (isempty(rvalue)) {
5977 /* Empty assignment resets the list */
5978 c->restrict_network_interfaces = set_free(c->restrict_network_interfaces);
5979 return 0;
5980 }
5981
5982 if (rvalue[0] == '~') {
5983 is_allow_rule = false;
5984 rvalue++;
5985 }
5986
5987 if (set_isempty(c->restrict_network_interfaces))
5988 /* Only initialize this when creating the set */
5989 c->restrict_network_interfaces_is_allow_list = is_allow_rule;
5990
5991 for (const char *p = rvalue;;) {
5992 _cleanup_free_ char *word = NULL;
5993
5994 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
5995 if (r == 0)
5996 break;
5997 if (r == -ENOMEM)
5998 return log_oom();
5999 if (r < 0) {
6000 log_syntax(unit, LOG_WARNING, filename, line, r,
6001 "Trailing garbage in %s, ignoring: %s", lvalue, rvalue);
6002 break;
6003 }
6004
6005 if (!ifname_valid(word)) {
6006 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid interface name, ignoring: %s", word);
6007 continue;
6008 }
6009
6010 if (c->restrict_network_interfaces_is_allow_list != is_allow_rule)
6011 free(set_remove(c->restrict_network_interfaces, word));
6012 else {
6013 r = set_put_strdup(&c->restrict_network_interfaces, word);
6014 if (r < 0)
6015 return log_oom();
6016 }
6017 }
6018
6019 return 0;
6020 }
6021
6022 static int merge_by_names(Unit **u, Set *names, const char *id) {
6023 char *k;
6024 int r;
6025
6026 assert(u);
6027 assert(*u);
6028
6029 /* Let's try to add in all names that are aliases of this unit */
6030 while ((k = set_steal_first(names))) {
6031 _cleanup_free_ _unused_ char *free_k = k;
6032
6033 /* First try to merge in the other name into our unit */
6034 r = unit_merge_by_name(*u, k);
6035 if (r < 0) {
6036 Unit *other;
6037
6038 /* Hmm, we couldn't merge the other unit into ours? Then let's try it the other way
6039 * round. */
6040
6041 other = manager_get_unit((*u)->manager, k);
6042 if (!other)
6043 return r; /* return previous failure */
6044
6045 r = unit_merge(other, *u);
6046 if (r < 0)
6047 return r;
6048
6049 *u = other;
6050 return merge_by_names(u, names, NULL);
6051 }
6052
6053 if (streq_ptr(id, k))
6054 unit_choose_id(*u, id);
6055 }
6056
6057 return 0;
6058 }
6059
6060 int unit_load_fragment(Unit *u) {
6061 const char *fragment;
6062 _cleanup_set_free_free_ Set *names = NULL;
6063 int r;
6064
6065 assert(u);
6066 assert(u->load_state == UNIT_STUB);
6067 assert(u->id);
6068
6069 if (u->transient) {
6070 u->access_selinux_context = mfree(u->access_selinux_context);
6071 u->load_state = UNIT_LOADED;
6072 return 0;
6073 }
6074
6075 /* Possibly rebuild the fragment map to catch new units */
6076 r = unit_file_build_name_map(&u->manager->lookup_paths,
6077 &u->manager->unit_cache_timestamp_hash,
6078 &u->manager->unit_id_map,
6079 &u->manager->unit_name_map,
6080 &u->manager->unit_path_cache);
6081 if (r < 0)
6082 return log_error_errno(r, "Failed to rebuild name map: %m");
6083
6084 r = unit_file_find_fragment(u->manager->unit_id_map,
6085 u->manager->unit_name_map,
6086 u->id,
6087 &fragment,
6088 &names);
6089 if (r < 0 && r != -ENOENT)
6090 return r;
6091
6092 if (fragment) {
6093 /* Open the file, check if this is a mask, otherwise read. */
6094 _cleanup_fclose_ FILE *f = NULL;
6095 struct stat st;
6096
6097 /* Try to open the file name. A symlink is OK, for example for linked files or masks. We
6098 * expect that all symlinks within the lookup paths have been already resolved, but we don't
6099 * verify this here. */
6100 f = fopen(fragment, "re");
6101 if (!f)
6102 return log_unit_notice_errno(u, errno, "Failed to open %s: %m", fragment);
6103
6104 if (fstat(fileno(f), &st) < 0)
6105 return -errno;
6106
6107 r = free_and_strdup(&u->fragment_path, fragment);
6108 if (r < 0)
6109 return r;
6110
6111 if (null_or_empty(&st)) {
6112 /* Unit file is masked */
6113
6114 u->load_state = u->perpetual ? UNIT_LOADED : UNIT_MASKED; /* don't allow perpetual units to ever be masked */
6115 u->fragment_mtime = 0;
6116 u->access_selinux_context = mfree(u->access_selinux_context);
6117 } else {
6118 #if HAVE_SELINUX
6119 if (mac_selinux_use()) {
6120 _cleanup_freecon_ char *selcon = NULL;
6121
6122 /* Cache the SELinux context of the unit file here. We'll make use of when checking access permissions to loaded units */
6123 r = fgetfilecon_raw(fileno(f), &selcon);
6124 if (r < 0)
6125 log_unit_warning_errno(u, r, "Failed to read SELinux context of '%s', ignoring: %m", fragment);
6126
6127 r = free_and_strdup(&u->access_selinux_context, selcon);
6128 if (r < 0)
6129 return r;
6130 } else
6131 #endif
6132 u->access_selinux_context = mfree(u->access_selinux_context);
6133
6134 u->load_state = UNIT_LOADED;
6135 u->fragment_mtime = timespec_load(&st.st_mtim);
6136
6137 /* Now, parse the file contents */
6138 r = config_parse(u->id, fragment, f,
6139 UNIT_VTABLE(u)->sections,
6140 config_item_perf_lookup, load_fragment_gperf_lookup,
6141 0,
6142 u,
6143 NULL);
6144 if (r == -ENOEXEC)
6145 log_unit_notice_errno(u, r, "Unit configuration has fatal error, unit will not be started.");
6146 if (r < 0)
6147 return r;
6148 }
6149 }
6150
6151 /* Call merge_by_names with the name derived from the fragment path as the preferred name.
6152 *
6153 * We do the merge dance here because for some unit types, the unit might have aliases which are not
6154 * declared in the file system. In particular, this is true (and frequent) for device and swap units.
6155 */
6156 const char *id = u->id;
6157 _cleanup_free_ char *free_id = NULL;
6158
6159 if (fragment) {
6160 id = basename(fragment);
6161 if (unit_name_is_valid(id, UNIT_NAME_TEMPLATE)) {
6162 assert(u->instance); /* If we're not trying to use a template for non-instanced unit,
6163 * this must be set. */
6164
6165 r = unit_name_replace_instance(id, u->instance, &free_id);
6166 if (r < 0)
6167 return log_debug_errno(r, "Failed to build id (%s + %s): %m", id, u->instance);
6168 id = free_id;
6169 }
6170 }
6171
6172 Unit *merged = u;
6173 r = merge_by_names(&merged, names, id);
6174 if (r < 0)
6175 return r;
6176
6177 if (merged != u)
6178 u->load_state = UNIT_MERGED;
6179
6180 return 0;
6181 }
6182
6183 void unit_dump_config_items(FILE *f) {
6184 static const struct {
6185 const ConfigParserCallback callback;
6186 const char *rvalue;
6187 } table[] = {
6188 { config_parse_warn_compat, "NOTSUPPORTED" },
6189 { config_parse_int, "INTEGER" },
6190 { config_parse_unsigned, "UNSIGNED" },
6191 { config_parse_iec_size, "SIZE" },
6192 { config_parse_iec_uint64, "SIZE" },
6193 { config_parse_si_uint64, "SIZE" },
6194 { config_parse_bool, "BOOLEAN" },
6195 { config_parse_string, "STRING" },
6196 { config_parse_path, "PATH" },
6197 { config_parse_unit_path_printf, "PATH" },
6198 { config_parse_colon_separated_paths, "PATH" },
6199 { config_parse_strv, "STRING [...]" },
6200 { config_parse_exec_nice, "NICE" },
6201 { config_parse_exec_oom_score_adjust, "OOMSCOREADJUST" },
6202 { config_parse_exec_io_class, "IOCLASS" },
6203 { config_parse_exec_io_priority, "IOPRIORITY" },
6204 { config_parse_exec_cpu_sched_policy, "CPUSCHEDPOLICY" },
6205 { config_parse_exec_cpu_sched_prio, "CPUSCHEDPRIO" },
6206 { config_parse_exec_cpu_affinity, "CPUAFFINITY" },
6207 { config_parse_mode, "MODE" },
6208 { config_parse_unit_env_file, "FILE" },
6209 { config_parse_exec_output, "OUTPUT" },
6210 { config_parse_exec_input, "INPUT" },
6211 { config_parse_log_facility, "FACILITY" },
6212 { config_parse_log_level, "LEVEL" },
6213 { config_parse_exec_secure_bits, "SECUREBITS" },
6214 { config_parse_capability_set, "BOUNDINGSET" },
6215 { config_parse_rlimit, "LIMIT" },
6216 { config_parse_unit_deps, "UNIT [...]" },
6217 { config_parse_exec, "PATH [ARGUMENT [...]]" },
6218 { config_parse_service_type, "SERVICETYPE" },
6219 { config_parse_service_exit_type, "SERVICEEXITTYPE" },
6220 { config_parse_service_restart, "SERVICERESTART" },
6221 { config_parse_service_timeout_failure_mode, "TIMEOUTMODE" },
6222 { config_parse_kill_mode, "KILLMODE" },
6223 { config_parse_signal, "SIGNAL" },
6224 { config_parse_socket_listen, "SOCKET [...]" },
6225 { config_parse_socket_bind, "SOCKETBIND" },
6226 { config_parse_socket_bindtodevice, "NETWORKINTERFACE" },
6227 { config_parse_sec, "SECONDS" },
6228 { config_parse_nsec, "NANOSECONDS" },
6229 { config_parse_namespace_path_strv, "PATH [...]" },
6230 { config_parse_bind_paths, "PATH[:PATH[:OPTIONS]] [...]" },
6231 { config_parse_unit_requires_mounts_for, "PATH [...]" },
6232 { config_parse_exec_mount_flags, "MOUNTFLAG [...]" },
6233 { config_parse_unit_string_printf, "STRING" },
6234 { config_parse_trigger_unit, "UNIT" },
6235 { config_parse_timer, "TIMER" },
6236 { config_parse_path_spec, "PATH" },
6237 { config_parse_notify_access, "ACCESS" },
6238 { config_parse_ip_tos, "TOS" },
6239 { config_parse_unit_condition_path, "CONDITION" },
6240 { config_parse_unit_condition_string, "CONDITION" },
6241 { config_parse_unit_slice, "SLICE" },
6242 { config_parse_documentation, "URL" },
6243 { config_parse_service_timeout, "SECONDS" },
6244 { config_parse_emergency_action, "ACTION" },
6245 { config_parse_set_status, "STATUS" },
6246 { config_parse_service_sockets, "SOCKETS" },
6247 { config_parse_environ, "ENVIRON" },
6248 #if HAVE_SECCOMP
6249 { config_parse_syscall_filter, "SYSCALLS" },
6250 { config_parse_syscall_archs, "ARCHS" },
6251 { config_parse_syscall_errno, "ERRNO" },
6252 { config_parse_syscall_log, "SYSCALLS" },
6253 { config_parse_address_families, "FAMILIES" },
6254 { config_parse_restrict_namespaces, "NAMESPACES" },
6255 #endif
6256 { config_parse_restrict_filesystems, "FILESYSTEMS" },
6257 { config_parse_cpu_shares, "SHARES" },
6258 { config_parse_cg_weight, "WEIGHT" },
6259 { config_parse_cg_cpu_weight, "CPUWEIGHT" },
6260 { config_parse_memory_limit, "LIMIT" },
6261 { config_parse_device_allow, "DEVICE" },
6262 { config_parse_device_policy, "POLICY" },
6263 { config_parse_io_limit, "LIMIT" },
6264 { config_parse_io_device_weight, "DEVICEWEIGHT" },
6265 { config_parse_io_device_latency, "DEVICELATENCY" },
6266 { config_parse_blockio_bandwidth, "BANDWIDTH" },
6267 { config_parse_blockio_weight, "WEIGHT" },
6268 { config_parse_blockio_device_weight, "DEVICEWEIGHT" },
6269 { config_parse_long, "LONG" },
6270 { config_parse_socket_service, "SERVICE" },
6271 #if HAVE_SELINUX
6272 { config_parse_exec_selinux_context, "LABEL" },
6273 #endif
6274 { config_parse_job_mode, "MODE" },
6275 { config_parse_job_mode_isolate, "BOOLEAN" },
6276 { config_parse_personality, "PERSONALITY" },
6277 };
6278
6279 const char *prev = NULL;
6280 const char *i;
6281
6282 assert(f);
6283
6284 NULSTR_FOREACH(i, load_fragment_gperf_nulstr) {
6285 const char *rvalue = "OTHER", *lvalue;
6286 const ConfigPerfItem *p;
6287 const char *dot;
6288
6289 assert_se(p = load_fragment_gperf_lookup(i, strlen(i)));
6290
6291 /* Hide legacy settings */
6292 if (p->parse == config_parse_warn_compat &&
6293 p->ltype == DISABLED_LEGACY)
6294 continue;
6295
6296 for (size_t j = 0; j < ELEMENTSOF(table); j++)
6297 if (p->parse == table[j].callback) {
6298 rvalue = table[j].rvalue;
6299 break;
6300 }
6301
6302 dot = strchr(i, '.');
6303 lvalue = dot ? dot + 1 : i;
6304
6305 if (dot) {
6306 size_t prefix_len = dot - i;
6307
6308 if (!prev || !strneq(prev, i, prefix_len+1)) {
6309 if (prev)
6310 fputc('\n', f);
6311
6312 fprintf(f, "[%.*s]\n", (int) prefix_len, i);
6313 }
6314 }
6315
6316 fprintf(f, "%s=%s\n", lvalue, rvalue);
6317 prev = i;
6318 }
6319 }
6320
6321 int config_parse_cpu_affinity2(
6322 const char *unit,
6323 const char *filename,
6324 unsigned line,
6325 const char *section,
6326 unsigned section_line,
6327 const char *lvalue,
6328 int ltype,
6329 const char *rvalue,
6330 void *data,
6331 void *userdata) {
6332
6333 CPUSet *affinity = data;
6334
6335 assert(affinity);
6336
6337 (void) parse_cpu_set_extend(rvalue, affinity, true, unit, filename, line, lvalue);
6338
6339 return 0;
6340 }
6341
6342 int config_parse_show_status(
6343 const char* unit,
6344 const char *filename,
6345 unsigned line,
6346 const char *section,
6347 unsigned section_line,
6348 const char *lvalue,
6349 int ltype,
6350 const char *rvalue,
6351 void *data,
6352 void *userdata) {
6353
6354 int k;
6355 ShowStatus *b = data;
6356
6357 assert(filename);
6358 assert(lvalue);
6359 assert(rvalue);
6360 assert(data);
6361
6362 k = parse_show_status(rvalue, b);
6363 if (k < 0)
6364 log_syntax(unit, LOG_WARNING, filename, line, k, "Failed to parse show status setting, ignoring: %s", rvalue);
6365
6366 return 0;
6367 }
6368
6369 int config_parse_output_restricted(
6370 const char* unit,
6371 const char *filename,
6372 unsigned line,
6373 const char *section,
6374 unsigned section_line,
6375 const char *lvalue,
6376 int ltype,
6377 const char *rvalue,
6378 void *data,
6379 void *userdata) {
6380
6381 ExecOutput t, *eo = data;
6382 bool obsolete = false;
6383
6384 assert(filename);
6385 assert(lvalue);
6386 assert(rvalue);
6387 assert(data);
6388
6389 if (streq(rvalue, "syslog")) {
6390 t = EXEC_OUTPUT_JOURNAL;
6391 obsolete = true;
6392 } else if (streq(rvalue, "syslog+console")) {
6393 t = EXEC_OUTPUT_JOURNAL_AND_CONSOLE;
6394 obsolete = true;
6395 } else {
6396 t = exec_output_from_string(rvalue);
6397 if (t < 0) {
6398 log_syntax(unit, LOG_WARNING, filename, line, t, "Failed to parse output type, ignoring: %s", rvalue);
6399 return 0;
6400 }
6401
6402 if (IN_SET(t, EXEC_OUTPUT_SOCKET, EXEC_OUTPUT_NAMED_FD, EXEC_OUTPUT_FILE, EXEC_OUTPUT_FILE_APPEND, EXEC_OUTPUT_FILE_TRUNCATE)) {
6403 log_syntax(unit, LOG_WARNING, filename, line, 0, "Standard output types socket, fd:, file:, append:, truncate: are not supported as defaults, ignoring: %s", rvalue);
6404 return 0;
6405 }
6406 }
6407
6408 if (obsolete)
6409 log_syntax(unit, LOG_NOTICE, filename, line, 0,
6410 "Standard output type %s is obsolete, automatically updating to %s. Please update your configuration.",
6411 rvalue, exec_output_to_string(t));
6412
6413 *eo = t;
6414 return 0;
6415 }
6416
6417 int config_parse_crash_chvt(
6418 const char* unit,
6419 const char *filename,
6420 unsigned line,
6421 const char *section,
6422 unsigned section_line,
6423 const char *lvalue,
6424 int ltype,
6425 const char *rvalue,
6426 void *data,
6427 void *userdata) {
6428
6429 int r;
6430
6431 assert(filename);
6432 assert(lvalue);
6433 assert(rvalue);
6434 assert(data);
6435
6436 r = parse_crash_chvt(rvalue, data);
6437 if (r < 0)
6438 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse CrashChangeVT= setting, ignoring: %s", rvalue);
6439
6440 return 0;
6441 }
6442
6443 int config_parse_swap_priority(
6444 const char *unit,
6445 const char *filename,
6446 unsigned line,
6447 const char *section,
6448 unsigned section_line,
6449 const char *lvalue,
6450 int ltype,
6451 const char *rvalue,
6452 void *data,
6453 void *userdata) {
6454
6455 Swap *s = userdata;
6456 int r, priority;
6457
6458 assert(s);
6459 assert(filename);
6460 assert(lvalue);
6461 assert(rvalue);
6462 assert(data);
6463
6464 if (isempty(rvalue)) {
6465 s->parameters_fragment.priority = -1;
6466 s->parameters_fragment.priority_set = false;
6467 return 0;
6468 }
6469
6470 r = safe_atoi(rvalue, &priority);
6471 if (r < 0) {
6472 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid swap priority '%s', ignoring.", rvalue);
6473 return 0;
6474 }
6475
6476 if (priority < -1) {
6477 log_syntax(unit, LOG_WARNING, filename, line, 0, "Sorry, swap priorities smaller than -1 may only be assigned by the kernel itself, ignoring: %s", rvalue);
6478 return 0;
6479 }
6480
6481 if (priority > 32767) {
6482 log_syntax(unit, LOG_WARNING, filename, line, 0, "Swap priority out of range, ignoring: %s", rvalue);
6483 return 0;
6484 }
6485
6486 s->parameters_fragment.priority = priority;
6487 s->parameters_fragment.priority_set = true;
6488 return 0;
6489 }
6490
6491 int config_parse_watchdog_sec(
6492 const char *unit,
6493 const char *filename,
6494 unsigned line,
6495 const char *section,
6496 unsigned section_line,
6497 const char *lvalue,
6498 int ltype,
6499 const char *rvalue,
6500 void *data,
6501 void *userdata) {
6502
6503 usec_t *usec = data;
6504
6505 assert(filename);
6506 assert(lvalue);
6507 assert(rvalue);
6508
6509 /* This is called for {Runtime,Reboot,KExec}WatchdogSec= where "default" maps to
6510 * USEC_INFINITY internally. */
6511
6512 if (streq(rvalue, "default"))
6513 *usec = USEC_INFINITY;
6514 else if (streq(rvalue, "off"))
6515 *usec = 0;
6516 else
6517 return config_parse_sec(unit, filename, line, section, section_line, lvalue, ltype, rvalue, data, userdata);
6518
6519 return 0;
6520 }
6521
6522 int config_parse_tty_size(
6523 const char *unit,
6524 const char *filename,
6525 unsigned line,
6526 const char *section,
6527 unsigned section_line,
6528 const char *lvalue,
6529 int ltype,
6530 const char *rvalue,
6531 void *data,
6532 void *userdata) {
6533
6534 unsigned *sz = data;
6535
6536 assert(filename);
6537 assert(lvalue);
6538 assert(rvalue);
6539
6540 if (isempty(rvalue)) {
6541 *sz = UINT_MAX;
6542 return 0;
6543 }
6544
6545 return config_parse_unsigned(unit, filename, line, section, section_line, lvalue, ltype, rvalue, data, userdata);
6546 }