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