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