]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/load-fragment.c
554180f1e3152360c11d1f1b6a2ce59a37866cec
[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_exec_preserve_mode, exec_preserve_mode, ExecPreserveMode, "Failed to parse resource 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(config_parse_memory_pressure_watch, cgroup_pressure_watch, CGroupPressureWatch, "Failed to parse memory pressure watch setting");
150 DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT(config_parse_ip_tos, ip_tos, int, -1, "Failed to parse IP TOS value");
151 DEFINE_CONFIG_PARSE_PTR(config_parse_blockio_weight, cg_blkio_weight_parse, uint64_t, "Invalid block IO weight");
152 DEFINE_CONFIG_PARSE_PTR(config_parse_cg_weight, cg_weight_parse, uint64_t, "Invalid weight");
153 DEFINE_CONFIG_PARSE_PTR(config_parse_cg_cpu_weight, cg_cpu_weight_parse, uint64_t, "Invalid CPU weight");
154 static DEFINE_CONFIG_PARSE_PTR(config_parse_cpu_shares_internal, cg_cpu_shares_parse, uint64_t, "Invalid CPU shares");
155 DEFINE_CONFIG_PARSE_PTR(config_parse_exec_mount_propagation_flag, mount_propagation_flag_from_string, unsigned long, "Failed to parse mount propagation flag");
156 DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT(config_parse_numa_policy, mpol, int, -1, "Invalid NUMA policy type");
157 DEFINE_CONFIG_PARSE_ENUM(config_parse_status_unit_format, status_unit_format, StatusUnitFormat, "Failed to parse status unit format");
158 DEFINE_CONFIG_PARSE_ENUM_FULL(config_parse_socket_timestamping, socket_timestamping_from_string_harder, SocketTimestamping, "Failed to parse timestamping precision");
159
160 int config_parse_cpu_shares(
161 const char *unit,
162 const char *filename,
163 unsigned line,
164 const char *section,
165 unsigned section_line,
166 const char *lvalue,
167 int ltype,
168 const char *rvalue,
169 void *data,
170 void *userdata) {
171
172 assert(filename);
173 assert(lvalue);
174 assert(rvalue);
175
176
177 log_syntax(unit, LOG_WARNING, filename, line, 0,
178 "Unit uses %s=; please use CPUWeight= instead. Support for %s= will be removed soon.",
179 lvalue, lvalue);
180
181 return config_parse_cpu_shares_internal(unit, filename, line, section, section_line, lvalue, ltype, rvalue, data, userdata);
182 }
183
184 bool contains_instance_specifier_superset(const char *s) {
185 const char *p, *q;
186 bool percent = false;
187
188 assert(s);
189
190 p = strchr(s, '@');
191 if (!p)
192 return false;
193
194 p++; /* Skip '@' */
195
196 q = strrchr(p, '.');
197 if (!q)
198 q = p + strlen(p);
199
200 /* If the string is just the instance specifier, it's not a superset of the instance. */
201 if (memcmp_nn(p, q - p, "%i", strlen("%i")) == 0)
202 return false;
203
204 /* %i, %n and %N all expand to the instance or a superset of it. */
205 for (; p < q; p++)
206 if (*p == '%')
207 percent = !percent;
208 else if (percent) {
209 if (IN_SET(*p, 'n', 'N', 'i'))
210 return true;
211 percent = false;
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->coredump_filter_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, def;
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_MASK_ALL; /* initialized to all bits on */
1904 def = CAP_MASK_UNSET; /* not set */
1905 } else
1906 def = initial = 0; /* All bits off */
1907
1908 r = capability_set_from_string(rvalue, &sum);
1909 if (r < 0) {
1910 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse %s= specifier '%s', ignoring: %m", lvalue, rvalue);
1911 return 0;
1912 }
1913
1914 if (sum == 0 || *capability_set == def)
1915 /* "", "~" or uninitialized data -> replace */
1916 *capability_set = invert ? ~sum : sum;
1917 else {
1918 /* previous data -> merge */
1919 if (invert)
1920 *capability_set &= ~sum;
1921 else
1922 *capability_set |= sum;
1923 }
1924
1925 return 0;
1926 }
1927
1928 int config_parse_exec_selinux_context(
1929 const char *unit,
1930 const char *filename,
1931 unsigned line,
1932 const char *section,
1933 unsigned section_line,
1934 const char *lvalue,
1935 int ltype,
1936 const char *rvalue,
1937 void *data,
1938 void *userdata) {
1939
1940 ExecContext *c = ASSERT_PTR(data);
1941 const Unit *u = userdata;
1942 bool ignore;
1943 char *k;
1944 int r;
1945
1946 assert(filename);
1947 assert(lvalue);
1948 assert(rvalue);
1949
1950 if (isempty(rvalue)) {
1951 c->selinux_context = mfree(c->selinux_context);
1952 c->selinux_context_ignore = false;
1953 return 0;
1954 }
1955
1956 if (rvalue[0] == '-') {
1957 ignore = true;
1958 rvalue++;
1959 } else
1960 ignore = false;
1961
1962 r = unit_full_printf(u, rvalue, &k);
1963 if (r < 0) {
1964 log_syntax(unit, ignore ? LOG_WARNING : LOG_ERR, filename, line, r,
1965 "Failed to resolve unit specifiers in '%s'%s: %m",
1966 rvalue, ignore ? ", ignoring" : "");
1967 return ignore ? 0 : -ENOEXEC;
1968 }
1969
1970 free_and_replace(c->selinux_context, k);
1971 c->selinux_context_ignore = ignore;
1972
1973 return 0;
1974 }
1975
1976 int config_parse_exec_apparmor_profile(
1977 const char *unit,
1978 const char *filename,
1979 unsigned line,
1980 const char *section,
1981 unsigned section_line,
1982 const char *lvalue,
1983 int ltype,
1984 const char *rvalue,
1985 void *data,
1986 void *userdata) {
1987
1988 ExecContext *c = ASSERT_PTR(data);
1989 const Unit *u = userdata;
1990 bool ignore;
1991 char *k;
1992 int r;
1993
1994 assert(filename);
1995 assert(lvalue);
1996 assert(rvalue);
1997
1998 if (isempty(rvalue)) {
1999 c->apparmor_profile = mfree(c->apparmor_profile);
2000 c->apparmor_profile_ignore = false;
2001 return 0;
2002 }
2003
2004 if (rvalue[0] == '-') {
2005 ignore = true;
2006 rvalue++;
2007 } else
2008 ignore = false;
2009
2010 r = unit_full_printf(u, rvalue, &k);
2011 if (r < 0) {
2012 log_syntax(unit, ignore ? LOG_WARNING : LOG_ERR, filename, line, r,
2013 "Failed to resolve unit specifiers in '%s'%s: %m",
2014 rvalue, ignore ? ", ignoring" : "");
2015 return ignore ? 0 : -ENOEXEC;
2016 }
2017
2018 free_and_replace(c->apparmor_profile, k);
2019 c->apparmor_profile_ignore = ignore;
2020
2021 return 0;
2022 }
2023
2024 int config_parse_exec_smack_process_label(
2025 const char *unit,
2026 const char *filename,
2027 unsigned line,
2028 const char *section,
2029 unsigned section_line,
2030 const char *lvalue,
2031 int ltype,
2032 const char *rvalue,
2033 void *data,
2034 void *userdata) {
2035
2036 ExecContext *c = ASSERT_PTR(data);
2037 const Unit *u = userdata;
2038 bool ignore;
2039 char *k;
2040 int r;
2041
2042 assert(filename);
2043 assert(lvalue);
2044 assert(rvalue);
2045
2046 if (isempty(rvalue)) {
2047 c->smack_process_label = mfree(c->smack_process_label);
2048 c->smack_process_label_ignore = false;
2049 return 0;
2050 }
2051
2052 if (rvalue[0] == '-') {
2053 ignore = true;
2054 rvalue++;
2055 } else
2056 ignore = false;
2057
2058 r = unit_full_printf(u, rvalue, &k);
2059 if (r < 0) {
2060 log_syntax(unit, ignore ? LOG_WARNING : LOG_ERR, filename, line, r,
2061 "Failed to resolve unit specifiers in '%s'%s: %m",
2062 rvalue, ignore ? ", ignoring" : "");
2063 return ignore ? 0 : -ENOEXEC;
2064 }
2065
2066 free_and_replace(c->smack_process_label, k);
2067 c->smack_process_label_ignore = ignore;
2068
2069 return 0;
2070 }
2071
2072 int config_parse_timer(
2073 const char *unit,
2074 const char *filename,
2075 unsigned line,
2076 const char *section,
2077 unsigned section_line,
2078 const char *lvalue,
2079 int ltype,
2080 const char *rvalue,
2081 void *data,
2082 void *userdata) {
2083
2084 _cleanup_(calendar_spec_freep) CalendarSpec *c = NULL;
2085 _cleanup_free_ char *k = NULL;
2086 const Unit *u = userdata;
2087 Timer *t = ASSERT_PTR(data);
2088 usec_t usec = 0;
2089 TimerValue *v;
2090 int r;
2091
2092 assert(filename);
2093 assert(lvalue);
2094 assert(rvalue);
2095
2096 if (isempty(rvalue)) {
2097 /* Empty assignment resets list */
2098 timer_free_values(t);
2099 return 0;
2100 }
2101
2102 r = unit_full_printf(u, rvalue, &k);
2103 if (r < 0) {
2104 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
2105 return 0;
2106 }
2107
2108 if (ltype == TIMER_CALENDAR) {
2109 r = calendar_spec_from_string(k, &c);
2110 if (r < 0) {
2111 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse calendar specification, ignoring: %s", k);
2112 return 0;
2113 }
2114 } else {
2115 r = parse_sec(k, &usec);
2116 if (r < 0) {
2117 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse timer value, ignoring: %s", k);
2118 return 0;
2119 }
2120 }
2121
2122 v = new(TimerValue, 1);
2123 if (!v)
2124 return log_oom();
2125
2126 *v = (TimerValue) {
2127 .base = ltype,
2128 .value = usec,
2129 .calendar_spec = TAKE_PTR(c),
2130 };
2131
2132 LIST_PREPEND(value, t->values, v);
2133
2134 return 0;
2135 }
2136
2137 int config_parse_trigger_unit(
2138 const char *unit,
2139 const char *filename,
2140 unsigned line,
2141 const char *section,
2142 unsigned section_line,
2143 const char *lvalue,
2144 int ltype,
2145 const char *rvalue,
2146 void *data,
2147 void *userdata) {
2148
2149 _cleanup_free_ char *p = NULL;
2150 Unit *u = ASSERT_PTR(data);
2151 UnitType type;
2152 int r;
2153
2154 assert(filename);
2155 assert(lvalue);
2156 assert(rvalue);
2157
2158 if (UNIT_TRIGGER(u)) {
2159 log_syntax(unit, LOG_WARNING, filename, line, 0, "Multiple units to trigger specified, ignoring: %s", rvalue);
2160 return 0;
2161 }
2162
2163 r = unit_name_printf(u, rvalue, &p);
2164 if (r < 0) {
2165 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue);
2166 return 0;
2167 }
2168
2169 type = unit_name_to_type(p);
2170 if (type < 0) {
2171 log_syntax(unit, LOG_WARNING, filename, line, type, "Unit type not valid, ignoring: %s", rvalue);
2172 return 0;
2173 }
2174 if (unit_has_name(u, p)) {
2175 log_syntax(unit, LOG_WARNING, filename, line, 0, "Units cannot trigger themselves, ignoring: %s", rvalue);
2176 return 0;
2177 }
2178
2179 r = unit_add_two_dependencies_by_name(u, UNIT_BEFORE, UNIT_TRIGGERS, p, true, UNIT_DEPENDENCY_FILE);
2180 if (r < 0) {
2181 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to add trigger on %s, ignoring: %m", p);
2182 return 0;
2183 }
2184
2185 return 0;
2186 }
2187
2188 int config_parse_path_spec(const char *unit,
2189 const char *filename,
2190 unsigned line,
2191 const char *section,
2192 unsigned section_line,
2193 const char *lvalue,
2194 int ltype,
2195 const char *rvalue,
2196 void *data,
2197 void *userdata) {
2198
2199 Path *p = ASSERT_PTR(data);
2200 PathSpec *s;
2201 PathType b;
2202 _cleanup_free_ char *k = NULL;
2203 int r;
2204
2205 assert(filename);
2206 assert(lvalue);
2207 assert(rvalue);
2208
2209 if (isempty(rvalue)) {
2210 /* Empty assignment clears list */
2211 path_free_specs(p);
2212 return 0;
2213 }
2214
2215 b = path_type_from_string(lvalue);
2216 if (b < 0) {
2217 log_syntax(unit, LOG_WARNING, filename, line, b, "Failed to parse path type, ignoring: %s", lvalue);
2218 return 0;
2219 }
2220
2221 r = unit_path_printf(UNIT(p), rvalue, &k);
2222 if (r < 0) {
2223 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue);
2224 return 0;
2225 }
2226
2227 r = path_simplify_and_warn(k, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
2228 if (r < 0)
2229 return 0;
2230
2231 s = new0(PathSpec, 1);
2232 if (!s)
2233 return log_oom();
2234
2235 s->unit = UNIT(p);
2236 s->path = TAKE_PTR(k);
2237 s->type = b;
2238 s->inotify_fd = -EBADF;
2239
2240 LIST_PREPEND(spec, p->specs, s);
2241
2242 return 0;
2243 }
2244
2245 int config_parse_socket_service(
2246 const char *unit,
2247 const char *filename,
2248 unsigned line,
2249 const char *section,
2250 unsigned section_line,
2251 const char *lvalue,
2252 int ltype,
2253 const char *rvalue,
2254 void *data,
2255 void *userdata) {
2256
2257 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2258 _cleanup_free_ char *p = NULL;
2259 Socket *s = ASSERT_PTR(data);
2260 Unit *x;
2261 int r;
2262
2263 assert(filename);
2264 assert(lvalue);
2265 assert(rvalue);
2266
2267 r = unit_name_printf(UNIT(s), rvalue, &p);
2268 if (r < 0) {
2269 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue);
2270 return 0;
2271 }
2272
2273 if (!endswith(p, ".service")) {
2274 log_syntax(unit, LOG_WARNING, filename, line, 0, "Unit must be of type service, ignoring: %s", rvalue);
2275 return 0;
2276 }
2277
2278 r = manager_load_unit(UNIT(s)->manager, p, NULL, &error, &x);
2279 if (r < 0) {
2280 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to load unit %s, ignoring: %s", rvalue, bus_error_message(&error, r));
2281 return 0;
2282 }
2283
2284 unit_ref_set(&s->service, UNIT(s), x);
2285
2286 return 0;
2287 }
2288
2289 int config_parse_fdname(
2290 const char *unit,
2291 const char *filename,
2292 unsigned line,
2293 const char *section,
2294 unsigned section_line,
2295 const char *lvalue,
2296 int ltype,
2297 const char *rvalue,
2298 void *data,
2299 void *userdata) {
2300
2301 _cleanup_free_ char *p = NULL;
2302 Socket *s = ASSERT_PTR(data);
2303 int r;
2304
2305 assert(filename);
2306 assert(lvalue);
2307 assert(rvalue);
2308
2309 if (isempty(rvalue)) {
2310 s->fdname = mfree(s->fdname);
2311 return 0;
2312 }
2313
2314 r = unit_fd_printf(UNIT(s), rvalue, &p);
2315 if (r < 0) {
2316 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
2317 return 0;
2318 }
2319
2320 if (!fdname_is_valid(p)) {
2321 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid file descriptor name, ignoring: %s", p);
2322 return 0;
2323 }
2324
2325 return free_and_replace(s->fdname, p);
2326 }
2327
2328 int config_parse_service_sockets(
2329 const char *unit,
2330 const char *filename,
2331 unsigned line,
2332 const char *section,
2333 unsigned section_line,
2334 const char *lvalue,
2335 int ltype,
2336 const char *rvalue,
2337 void *data,
2338 void *userdata) {
2339
2340 Service *s = ASSERT_PTR(data);
2341 int r;
2342
2343 assert(filename);
2344 assert(lvalue);
2345 assert(rvalue);
2346
2347 for (const char *p = rvalue;;) {
2348 _cleanup_free_ char *word = NULL, *k = NULL;
2349
2350 r = extract_first_word(&p, &word, NULL, 0);
2351 if (r == -ENOMEM)
2352 return log_oom();
2353 if (r < 0) {
2354 log_syntax(unit, LOG_WARNING, filename, line, r, "Trailing garbage in sockets, ignoring: %s", rvalue);
2355 return 0;
2356 }
2357 if (r == 0)
2358 return 0;
2359
2360 r = unit_name_printf(UNIT(s), word, &k);
2361 if (r < 0) {
2362 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", word);
2363 continue;
2364 }
2365
2366 if (!endswith(k, ".socket")) {
2367 log_syntax(unit, LOG_WARNING, filename, line, 0, "Unit must be of type socket, ignoring: %s", k);
2368 continue;
2369 }
2370
2371 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_WANTS, UNIT_AFTER, k, true, UNIT_DEPENDENCY_FILE);
2372 if (r < 0)
2373 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to add dependency on %s, ignoring: %m", k);
2374
2375 r = unit_add_dependency_by_name(UNIT(s), UNIT_TRIGGERED_BY, k, true, UNIT_DEPENDENCY_FILE);
2376 if (r < 0)
2377 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to add dependency on %s, ignoring: %m", k);
2378 }
2379 }
2380
2381 int config_parse_bus_name(
2382 const char *unit,
2383 const char *filename,
2384 unsigned line,
2385 const char *section,
2386 unsigned section_line,
2387 const char *lvalue,
2388 int ltype,
2389 const char *rvalue,
2390 void *data,
2391 void *userdata) {
2392
2393 _cleanup_free_ char *k = NULL;
2394 const Unit *u = ASSERT_PTR(userdata);
2395 int r;
2396
2397 assert(filename);
2398 assert(lvalue);
2399 assert(rvalue);
2400
2401 r = unit_full_printf_full(u, rvalue, SD_BUS_MAXIMUM_NAME_LENGTH, &k);
2402 if (r < 0) {
2403 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue);
2404 return 0;
2405 }
2406
2407 if (!sd_bus_service_name_is_valid(k)) {
2408 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid bus name, ignoring: %s", k);
2409 return 0;
2410 }
2411
2412 return config_parse_string(unit, filename, line, section, section_line, lvalue, ltype, k, data, userdata);
2413 }
2414
2415 int config_parse_service_timeout(
2416 const char *unit,
2417 const char *filename,
2418 unsigned line,
2419 const char *section,
2420 unsigned section_line,
2421 const char *lvalue,
2422 int ltype,
2423 const char *rvalue,
2424 void *data,
2425 void *userdata) {
2426
2427 Service *s = ASSERT_PTR(userdata);
2428 usec_t usec;
2429 int r;
2430
2431 assert(filename);
2432 assert(lvalue);
2433 assert(rvalue);
2434
2435 /* This is called for two cases: TimeoutSec= and TimeoutStartSec=. */
2436
2437 /* Traditionally, these options accepted 0 to disable the timeouts. However, a timeout of 0 suggests it happens
2438 * immediately, hence fix this to become USEC_INFINITY instead. This is in-line with how we internally handle
2439 * all other timeouts. */
2440 r = parse_sec_fix_0(rvalue, &usec);
2441 if (r < 0) {
2442 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse %s= parameter, ignoring: %s", lvalue, rvalue);
2443 return 0;
2444 }
2445
2446 s->start_timeout_defined = true;
2447 s->timeout_start_usec = usec;
2448
2449 if (streq(lvalue, "TimeoutSec"))
2450 s->timeout_stop_usec = usec;
2451
2452 return 0;
2453 }
2454
2455 int config_parse_timeout_abort(
2456 const char *unit,
2457 const char *filename,
2458 unsigned line,
2459 const char *section,
2460 unsigned section_line,
2461 const char *lvalue,
2462 int ltype,
2463 const char *rvalue,
2464 void *data,
2465 void *userdata) {
2466
2467 usec_t *ret = ASSERT_PTR(data);
2468 int r;
2469
2470 assert(filename);
2471 assert(lvalue);
2472 assert(rvalue);
2473
2474 /* Note: apart from setting the arg, this returns an extra bit of information in the return value. */
2475
2476 if (isempty(rvalue)) {
2477 *ret = 0;
2478 return 0; /* "not set" */
2479 }
2480
2481 r = parse_sec(rvalue, ret);
2482 if (r < 0)
2483 return log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse %s= setting, ignoring: %s", lvalue, rvalue);
2484
2485 return 1; /* "set" */
2486 }
2487
2488 int config_parse_service_timeout_abort(
2489 const char *unit,
2490 const char *filename,
2491 unsigned line,
2492 const char *section,
2493 unsigned section_line,
2494 const char *lvalue,
2495 int ltype,
2496 const char *rvalue,
2497 void *data,
2498 void *userdata) {
2499
2500 Service *s = ASSERT_PTR(userdata);
2501 int r;
2502
2503 r = config_parse_timeout_abort(unit, filename, line, section, section_line, lvalue, ltype, rvalue,
2504 &s->timeout_abort_usec, s);
2505 if (r >= 0)
2506 s->timeout_abort_set = r;
2507 return 0;
2508 }
2509
2510 int config_parse_user_group_compat(
2511 const char *unit,
2512 const char *filename,
2513 unsigned line,
2514 const char *section,
2515 unsigned section_line,
2516 const char *lvalue,
2517 int ltype,
2518 const char *rvalue,
2519 void *data,
2520 void *userdata) {
2521
2522 _cleanup_free_ char *k = NULL;
2523 char **user = data;
2524 const Unit *u = ASSERT_PTR(userdata);
2525 int r;
2526
2527 assert(filename);
2528 assert(lvalue);
2529 assert(rvalue);
2530
2531 if (isempty(rvalue)) {
2532 *user = mfree(*user);
2533 return 0;
2534 }
2535
2536 r = unit_full_printf(u, rvalue, &k);
2537 if (r < 0) {
2538 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s: %m", rvalue);
2539 return -ENOEXEC;
2540 }
2541
2542 if (!valid_user_group_name(k, VALID_USER_ALLOW_NUMERIC|VALID_USER_RELAX|VALID_USER_WARN)) {
2543 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid user/group name or numeric ID: %s", k);
2544 return -ENOEXEC;
2545 }
2546
2547 if (strstr(lvalue, "User") && streq(k, NOBODY_USER_NAME))
2548 log_struct(LOG_NOTICE,
2549 "MESSAGE=%s:%u: Special user %s configured, this is not safe!", filename, line, k,
2550 "UNIT=%s", unit,
2551 "MESSAGE_ID=" SD_MESSAGE_NOBODY_USER_UNSUITABLE_STR,
2552 "OFFENDING_USER=%s", k,
2553 "CONFIG_FILE=%s", filename,
2554 "CONFIG_LINE=%u", line);
2555
2556 return free_and_replace(*user, k);
2557 }
2558
2559 int config_parse_user_group_strv_compat(
2560 const char *unit,
2561 const char *filename,
2562 unsigned line,
2563 const char *section,
2564 unsigned section_line,
2565 const char *lvalue,
2566 int ltype,
2567 const char *rvalue,
2568 void *data,
2569 void *userdata) {
2570
2571 char ***users = data;
2572 const Unit *u = ASSERT_PTR(userdata);
2573 int r;
2574
2575 assert(filename);
2576 assert(lvalue);
2577 assert(rvalue);
2578
2579 if (isempty(rvalue)) {
2580 *users = strv_free(*users);
2581 return 0;
2582 }
2583
2584 for (const char *p = rvalue;;) {
2585 _cleanup_free_ char *word = NULL, *k = NULL;
2586
2587 r = extract_first_word(&p, &word, NULL, 0);
2588 if (r == -ENOMEM)
2589 return log_oom();
2590 if (r < 0) {
2591 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid syntax: %s", rvalue);
2592 return -ENOEXEC;
2593 }
2594 if (r == 0)
2595 return 0;
2596
2597 r = unit_full_printf(u, word, &k);
2598 if (r < 0) {
2599 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s: %m", word);
2600 return -ENOEXEC;
2601 }
2602
2603 if (!valid_user_group_name(k, VALID_USER_ALLOW_NUMERIC|VALID_USER_RELAX|VALID_USER_WARN)) {
2604 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid user/group name or numeric ID: %s", k);
2605 return -ENOEXEC;
2606 }
2607
2608 r = strv_push(users, k);
2609 if (r < 0)
2610 return log_oom();
2611
2612 k = NULL;
2613 }
2614 }
2615
2616 int config_parse_working_directory(
2617 const char *unit,
2618 const char *filename,
2619 unsigned line,
2620 const char *section,
2621 unsigned section_line,
2622 const char *lvalue,
2623 int ltype,
2624 const char *rvalue,
2625 void *data,
2626 void *userdata) {
2627
2628 ExecContext *c = ASSERT_PTR(data);
2629 const Unit *u = ASSERT_PTR(userdata);
2630 bool missing_ok;
2631 int r;
2632
2633 assert(filename);
2634 assert(lvalue);
2635 assert(rvalue);
2636
2637 if (isempty(rvalue)) {
2638 c->working_directory_home = false;
2639 c->working_directory = mfree(c->working_directory);
2640 return 0;
2641 }
2642
2643 if (rvalue[0] == '-') {
2644 missing_ok = true;
2645 rvalue++;
2646 } else
2647 missing_ok = false;
2648
2649 if (streq(rvalue, "~")) {
2650 c->working_directory_home = true;
2651 c->working_directory = mfree(c->working_directory);
2652 } else {
2653 _cleanup_free_ char *k = NULL;
2654
2655 r = unit_path_printf(u, rvalue, &k);
2656 if (r < 0) {
2657 log_syntax(unit, missing_ok ? LOG_WARNING : LOG_ERR, filename, line, r,
2658 "Failed to resolve unit specifiers in working directory path '%s'%s: %m",
2659 rvalue, missing_ok ? ", ignoring" : "");
2660 return missing_ok ? 0 : -ENOEXEC;
2661 }
2662
2663 r = path_simplify_and_warn(k, PATH_CHECK_ABSOLUTE | (missing_ok ? 0 : PATH_CHECK_FATAL), unit, filename, line, lvalue);
2664 if (r < 0)
2665 return missing_ok ? 0 : -ENOEXEC;
2666
2667 c->working_directory_home = false;
2668 free_and_replace(c->working_directory, k);
2669 }
2670
2671 c->working_directory_missing_ok = missing_ok;
2672 return 0;
2673 }
2674
2675 int config_parse_unit_env_file(const char *unit,
2676 const char *filename,
2677 unsigned line,
2678 const char *section,
2679 unsigned section_line,
2680 const char *lvalue,
2681 int ltype,
2682 const char *rvalue,
2683 void *data,
2684 void *userdata) {
2685
2686 char ***env = ASSERT_PTR(data);
2687 const Unit *u = userdata;
2688 _cleanup_free_ char *n = NULL;
2689 int r;
2690
2691 assert(filename);
2692 assert(lvalue);
2693 assert(rvalue);
2694
2695 if (isempty(rvalue)) {
2696 /* Empty assignment frees the list */
2697 *env = strv_free(*env);
2698 return 0;
2699 }
2700
2701 r = unit_full_printf_full(u, rvalue, PATH_MAX, &n);
2702 if (r < 0) {
2703 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
2704 return 0;
2705 }
2706
2707 r = path_simplify_and_warn(n[0] == '-' ? n + 1 : n, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
2708 if (r < 0)
2709 return 0;
2710
2711 r = strv_push(env, n);
2712 if (r < 0)
2713 return log_oom();
2714
2715 n = NULL;
2716
2717 return 0;
2718 }
2719
2720 int config_parse_environ(
2721 const char *unit,
2722 const char *filename,
2723 unsigned line,
2724 const char *section,
2725 unsigned section_line,
2726 const char *lvalue,
2727 int ltype,
2728 const char *rvalue,
2729 void *data,
2730 void *userdata) {
2731
2732 const Unit *u = userdata;
2733 char ***env = ASSERT_PTR(data);
2734 int r;
2735
2736 assert(filename);
2737 assert(lvalue);
2738 assert(rvalue);
2739
2740 if (isempty(rvalue)) {
2741 /* Empty assignment resets the list */
2742 *env = strv_free(*env);
2743 return 0;
2744 }
2745
2746 /* If 'u' is set, we operate on the regular unit specifier table. Otherwise we use a manager-specific
2747 * specifier table (in which case ltype must contain the runtime scope). */
2748 const Specifier *table = u ? NULL : (const Specifier[]) {
2749 COMMON_SYSTEM_SPECIFIERS,
2750 COMMON_TMP_SPECIFIERS,
2751 COMMON_CREDS_SPECIFIERS(ltype),
2752 { 'h', specifier_user_home, NULL },
2753 { 's', specifier_user_shell, NULL },
2754 };
2755
2756 for (const char *p = rvalue;; ) {
2757 _cleanup_free_ char *word = NULL, *resolved = NULL;
2758
2759 r = extract_first_word(&p, &word, NULL, EXTRACT_CUNESCAPE|EXTRACT_UNQUOTE);
2760 if (r == -ENOMEM)
2761 return log_oom();
2762 if (r < 0) {
2763 log_syntax(unit, LOG_WARNING, filename, line, r,
2764 "Invalid syntax, ignoring: %s", rvalue);
2765 return 0;
2766 }
2767 if (r == 0)
2768 return 0;
2769
2770 if (table)
2771 r = specifier_printf(word, sc_arg_max(), table, NULL, NULL, &resolved);
2772 else
2773 r = unit_env_printf(u, word, &resolved);
2774 if (r < 0) {
2775 log_syntax(unit, LOG_WARNING, filename, line, r,
2776 "Failed to resolve specifiers in %s, ignoring: %m", word);
2777 continue;
2778 }
2779
2780 if (!env_assignment_is_valid(resolved)) {
2781 log_syntax(unit, LOG_WARNING, filename, line, 0,
2782 "Invalid environment assignment, ignoring: %s", resolved);
2783 continue;
2784 }
2785
2786 r = strv_env_replace_consume(env, TAKE_PTR(resolved));
2787 if (r < 0)
2788 return log_error_errno(r, "Failed to update environment: %m");
2789 }
2790 }
2791
2792 int config_parse_pass_environ(
2793 const char *unit,
2794 const char *filename,
2795 unsigned line,
2796 const char *section,
2797 unsigned section_line,
2798 const char *lvalue,
2799 int ltype,
2800 const char *rvalue,
2801 void *data,
2802 void *userdata) {
2803
2804 _cleanup_strv_free_ char **n = NULL;
2805 const Unit *u = userdata;
2806 char*** passenv = ASSERT_PTR(data);
2807 size_t nlen = 0;
2808 int r;
2809
2810 assert(filename);
2811 assert(lvalue);
2812 assert(rvalue);
2813
2814 if (isempty(rvalue)) {
2815 /* Empty assignment resets the list */
2816 *passenv = strv_free(*passenv);
2817 return 0;
2818 }
2819
2820 for (const char *p = rvalue;;) {
2821 _cleanup_free_ char *word = NULL, *k = NULL;
2822
2823 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
2824 if (r == -ENOMEM)
2825 return log_oom();
2826 if (r < 0) {
2827 log_syntax(unit, LOG_WARNING, filename, line, r,
2828 "Trailing garbage in %s, ignoring: %s", lvalue, rvalue);
2829 break;
2830 }
2831 if (r == 0)
2832 break;
2833
2834 if (u) {
2835 r = unit_env_printf(u, word, &k);
2836 if (r < 0) {
2837 log_syntax(unit, LOG_WARNING, filename, line, r,
2838 "Failed to resolve specifiers in %s, ignoring: %m", word);
2839 continue;
2840 }
2841 } else
2842 k = TAKE_PTR(word);
2843
2844 if (!env_name_is_valid(k)) {
2845 log_syntax(unit, LOG_WARNING, filename, line, 0,
2846 "Invalid environment name for %s, ignoring: %s", lvalue, k);
2847 continue;
2848 }
2849
2850 if (!GREEDY_REALLOC(n, nlen + 2))
2851 return log_oom();
2852
2853 n[nlen++] = TAKE_PTR(k);
2854 n[nlen] = NULL;
2855 }
2856
2857 if (n) {
2858 r = strv_extend_strv(passenv, n, true);
2859 if (r < 0)
2860 return log_oom();
2861 }
2862
2863 return 0;
2864 }
2865
2866 int config_parse_unset_environ(
2867 const char *unit,
2868 const char *filename,
2869 unsigned line,
2870 const char *section,
2871 unsigned section_line,
2872 const char *lvalue,
2873 int ltype,
2874 const char *rvalue,
2875 void *data,
2876 void *userdata) {
2877
2878 _cleanup_strv_free_ char **n = NULL;
2879 char*** unsetenv = ASSERT_PTR(data);
2880 const Unit *u = userdata;
2881 size_t nlen = 0;
2882 int r;
2883
2884 assert(filename);
2885 assert(lvalue);
2886 assert(rvalue);
2887
2888 if (isempty(rvalue)) {
2889 /* Empty assignment resets the list */
2890 *unsetenv = strv_free(*unsetenv);
2891 return 0;
2892 }
2893
2894 for (const char *p = rvalue;;) {
2895 _cleanup_free_ char *word = NULL, *k = NULL;
2896
2897 r = extract_first_word(&p, &word, NULL, EXTRACT_CUNESCAPE|EXTRACT_UNQUOTE);
2898 if (r == -ENOMEM)
2899 return log_oom();
2900 if (r < 0) {
2901 log_syntax(unit, LOG_WARNING, filename, line, r,
2902 "Trailing garbage in %s, ignoring: %s", lvalue, rvalue);
2903 break;
2904 }
2905 if (r == 0)
2906 break;
2907
2908 if (u) {
2909 r = unit_env_printf(u, word, &k);
2910 if (r < 0) {
2911 log_syntax(unit, LOG_WARNING, filename, line, r,
2912 "Failed to resolve unit specifiers in %s, ignoring: %m", word);
2913 continue;
2914 }
2915 } else
2916 k = TAKE_PTR(word);
2917
2918 if (!env_assignment_is_valid(k) && !env_name_is_valid(k)) {
2919 log_syntax(unit, LOG_WARNING, filename, line, 0,
2920 "Invalid environment name or assignment %s, ignoring: %s", lvalue, k);
2921 continue;
2922 }
2923
2924 if (!GREEDY_REALLOC(n, nlen + 2))
2925 return log_oom();
2926
2927 n[nlen++] = TAKE_PTR(k);
2928 n[nlen] = NULL;
2929 }
2930
2931 if (n) {
2932 r = strv_extend_strv(unsetenv, n, true);
2933 if (r < 0)
2934 return log_oom();
2935 }
2936
2937 return 0;
2938 }
2939
2940 int config_parse_log_extra_fields(
2941 const char *unit,
2942 const char *filename,
2943 unsigned line,
2944 const char *section,
2945 unsigned section_line,
2946 const char *lvalue,
2947 int ltype,
2948 const char *rvalue,
2949 void *data,
2950 void *userdata) {
2951
2952 ExecContext *c = ASSERT_PTR(data);
2953 const Unit *u = userdata;
2954 int r;
2955
2956 assert(filename);
2957 assert(lvalue);
2958 assert(rvalue);
2959
2960 if (isempty(rvalue)) {
2961 exec_context_free_log_extra_fields(c);
2962 return 0;
2963 }
2964
2965 for (const char *p = rvalue;;) {
2966 _cleanup_free_ char *word = NULL, *k = NULL;
2967 struct iovec *t;
2968 const char *eq;
2969
2970 r = extract_first_word(&p, &word, NULL, EXTRACT_CUNESCAPE|EXTRACT_UNQUOTE);
2971 if (r == -ENOMEM)
2972 return log_oom();
2973 if (r < 0) {
2974 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
2975 return 0;
2976 }
2977 if (r == 0)
2978 return 0;
2979
2980 r = unit_full_printf(u, word, &k);
2981 if (r < 0) {
2982 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", word);
2983 continue;
2984 }
2985
2986 eq = strchr(k, '=');
2987 if (!eq) {
2988 log_syntax(unit, LOG_WARNING, filename, line, 0, "Log field lacks '=' character, ignoring: %s", k);
2989 continue;
2990 }
2991
2992 if (!journal_field_valid(k, eq-k, false)) {
2993 log_syntax(unit, LOG_WARNING, filename, line, 0, "Log field name is invalid, ignoring: %s", k);
2994 continue;
2995 }
2996
2997 t = reallocarray(c->log_extra_fields, c->n_log_extra_fields+1, sizeof(struct iovec));
2998 if (!t)
2999 return log_oom();
3000
3001 c->log_extra_fields = t;
3002 c->log_extra_fields[c->n_log_extra_fields++] = IOVEC_MAKE_STRING(k);
3003
3004 k = NULL;
3005 }
3006 }
3007
3008 int config_parse_log_namespace(
3009 const char *unit,
3010 const char *filename,
3011 unsigned line,
3012 const char *section,
3013 unsigned section_line,
3014 const char *lvalue,
3015 int ltype,
3016 const char *rvalue,
3017 void *data,
3018 void *userdata) {
3019
3020 _cleanup_free_ char *k = NULL;
3021 ExecContext *c = ASSERT_PTR(data);
3022 const Unit *u = userdata;
3023 int r;
3024
3025 assert(filename);
3026 assert(lvalue);
3027 assert(rvalue);
3028
3029 if (isempty(rvalue)) {
3030 c->log_namespace = mfree(c->log_namespace);
3031 return 0;
3032 }
3033
3034 r = unit_full_printf_full(u, rvalue, NAME_MAX, &k);
3035 if (r < 0) {
3036 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue);
3037 return 0;
3038 }
3039
3040 if (!log_namespace_name_valid(k)) {
3041 log_syntax(unit, LOG_WARNING, filename, line, 0, "Specified log namespace name is not valid, ignoring: %s", k);
3042 return 0;
3043 }
3044
3045 free_and_replace(c->log_namespace, k);
3046 return 0;
3047 }
3048
3049 int config_parse_unit_condition_path(
3050 const char *unit,
3051 const char *filename,
3052 unsigned line,
3053 const char *section,
3054 unsigned section_line,
3055 const char *lvalue,
3056 int ltype,
3057 const char *rvalue,
3058 void *data,
3059 void *userdata) {
3060
3061 _cleanup_free_ char *p = NULL;
3062 Condition **list = ASSERT_PTR(data), *c;
3063 ConditionType t = ltype;
3064 bool trigger, negate;
3065 const Unit *u = userdata;
3066 int r;
3067
3068 assert(filename);
3069 assert(lvalue);
3070 assert(rvalue);
3071
3072 if (isempty(rvalue)) {
3073 /* Empty assignment resets the list */
3074 *list = condition_free_list(*list);
3075 return 0;
3076 }
3077
3078 trigger = rvalue[0] == '|';
3079 if (trigger)
3080 rvalue++;
3081
3082 negate = rvalue[0] == '!';
3083 if (negate)
3084 rvalue++;
3085
3086 r = unit_path_printf(u, rvalue, &p);
3087 if (r < 0) {
3088 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue);
3089 return 0;
3090 }
3091
3092 r = path_simplify_and_warn(p, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
3093 if (r < 0)
3094 return 0;
3095
3096 c = condition_new(t, p, trigger, negate);
3097 if (!c)
3098 return log_oom();
3099
3100 LIST_PREPEND(conditions, *list, c);
3101 return 0;
3102 }
3103
3104 int config_parse_unit_condition_string(
3105 const char *unit,
3106 const char *filename,
3107 unsigned line,
3108 const char *section,
3109 unsigned section_line,
3110 const char *lvalue,
3111 int ltype,
3112 const char *rvalue,
3113 void *data,
3114 void *userdata) {
3115
3116 _cleanup_free_ char *s = NULL;
3117 Condition **list = ASSERT_PTR(data), *c;
3118 ConditionType t = ltype;
3119 bool trigger, negate;
3120 const Unit *u = userdata;
3121 int r;
3122
3123 assert(filename);
3124 assert(lvalue);
3125 assert(rvalue);
3126
3127 if (isempty(rvalue)) {
3128 /* Empty assignment resets the list */
3129 *list = condition_free_list(*list);
3130 return 0;
3131 }
3132
3133 trigger = *rvalue == '|';
3134 if (trigger)
3135 rvalue += 1 + strspn(rvalue + 1, WHITESPACE);
3136
3137 negate = *rvalue == '!';
3138 if (negate)
3139 rvalue += 1 + strspn(rvalue + 1, WHITESPACE);
3140
3141 r = unit_full_printf(u, rvalue, &s);
3142 if (r < 0) {
3143 log_syntax(unit, LOG_WARNING, filename, line, r,
3144 "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
3145 return 0;
3146 }
3147
3148 c = condition_new(t, s, trigger, negate);
3149 if (!c)
3150 return log_oom();
3151
3152 LIST_PREPEND(conditions, *list, c);
3153 return 0;
3154 }
3155
3156 int config_parse_unit_requires_mounts_for(
3157 const char *unit,
3158 const char *filename,
3159 unsigned line,
3160 const char *section,
3161 unsigned section_line,
3162 const char *lvalue,
3163 int ltype,
3164 const char *rvalue,
3165 void *data,
3166 void *userdata) {
3167
3168 Unit *u = userdata;
3169 int r;
3170
3171 assert(filename);
3172 assert(lvalue);
3173 assert(rvalue);
3174 assert(data);
3175
3176 for (const char *p = rvalue;;) {
3177 _cleanup_free_ char *word = NULL, *resolved = NULL;
3178
3179 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
3180 if (r == -ENOMEM)
3181 return log_oom();
3182 if (r < 0) {
3183 log_syntax(unit, LOG_WARNING, filename, line, r,
3184 "Invalid syntax, ignoring: %s", rvalue);
3185 return 0;
3186 }
3187 if (r == 0)
3188 return 0;
3189
3190 r = unit_path_printf(u, word, &resolved);
3191 if (r < 0) {
3192 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", word);
3193 continue;
3194 }
3195
3196 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
3197 if (r < 0)
3198 continue;
3199
3200 r = unit_require_mounts_for(u, resolved, UNIT_DEPENDENCY_FILE);
3201 if (r < 0) {
3202 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to add required mount '%s', ignoring: %m", resolved);
3203 continue;
3204 }
3205 }
3206 }
3207
3208 int config_parse_documentation(
3209 const char *unit,
3210 const char *filename,
3211 unsigned line,
3212 const char *section,
3213 unsigned section_line,
3214 const char *lvalue,
3215 int ltype,
3216 const char *rvalue,
3217 void *data,
3218 void *userdata) {
3219
3220 Unit *u = ASSERT_PTR(userdata);
3221 int r;
3222 char **a, **b;
3223
3224 assert(filename);
3225 assert(lvalue);
3226 assert(rvalue);
3227
3228 if (isempty(rvalue)) {
3229 /* Empty assignment resets the list */
3230 u->documentation = strv_free(u->documentation);
3231 return 0;
3232 }
3233
3234 r = config_parse_unit_strv_printf(unit, filename, line, section, section_line, lvalue, ltype,
3235 rvalue, data, userdata);
3236 if (r < 0)
3237 return r;
3238
3239 for (a = b = u->documentation; a && *a; a++) {
3240
3241 if (documentation_url_is_valid(*a))
3242 *(b++) = *a;
3243 else {
3244 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid URL, ignoring: %s", *a);
3245 free(*a);
3246 }
3247 }
3248 if (b)
3249 *b = NULL;
3250
3251 return 0;
3252 }
3253
3254 #if HAVE_SECCOMP
3255 int config_parse_syscall_filter(
3256 const char *unit,
3257 const char *filename,
3258 unsigned line,
3259 const char *section,
3260 unsigned section_line,
3261 const char *lvalue,
3262 int ltype,
3263 const char *rvalue,
3264 void *data,
3265 void *userdata) {
3266
3267 ExecContext *c = data;
3268 _unused_ const Unit *u = ASSERT_PTR(userdata);
3269 bool invert = false;
3270 int r;
3271
3272 assert(filename);
3273 assert(lvalue);
3274 assert(rvalue);
3275
3276 if (isempty(rvalue)) {
3277 /* Empty assignment resets the list */
3278 c->syscall_filter = hashmap_free(c->syscall_filter);
3279 c->syscall_allow_list = false;
3280 return 0;
3281 }
3282
3283 if (rvalue[0] == '~') {
3284 invert = true;
3285 rvalue++;
3286 }
3287
3288 if (!c->syscall_filter) {
3289 c->syscall_filter = hashmap_new(NULL);
3290 if (!c->syscall_filter)
3291 return log_oom();
3292
3293 if (invert)
3294 /* Allow everything but the ones listed */
3295 c->syscall_allow_list = false;
3296 else {
3297 /* Allow nothing but the ones listed */
3298 c->syscall_allow_list = true;
3299
3300 /* Accept default syscalls if we are on an allow_list */
3301 r = seccomp_parse_syscall_filter(
3302 "@default", -1, c->syscall_filter,
3303 SECCOMP_PARSE_PERMISSIVE|SECCOMP_PARSE_ALLOW_LIST,
3304 unit,
3305 NULL, 0);
3306 if (r < 0)
3307 return r;
3308 }
3309 }
3310
3311 for (const char *p = rvalue;;) {
3312 _cleanup_free_ char *word = NULL, *name = NULL;
3313 int num;
3314
3315 r = extract_first_word(&p, &word, NULL, 0);
3316 if (r == -ENOMEM)
3317 return log_oom();
3318 if (r < 0) {
3319 log_syntax(unit, LOG_WARNING, filename, line, r,
3320 "Invalid syntax, ignoring: %s", rvalue);
3321 return 0;
3322 }
3323 if (r == 0)
3324 return 0;
3325
3326 r = parse_syscall_and_errno(word, &name, &num);
3327 if (r < 0) {
3328 log_syntax(unit, LOG_WARNING, filename, line, r,
3329 "Failed to parse syscall:errno, ignoring: %s", word);
3330 continue;
3331 }
3332 if (!invert && num >= 0) {
3333 log_syntax(unit, LOG_WARNING, filename, line, 0,
3334 "Allow-listed system calls cannot take error number, ignoring: %s", word);
3335 continue;
3336 }
3337
3338 r = seccomp_parse_syscall_filter(
3339 name, num, c->syscall_filter,
3340 SECCOMP_PARSE_LOG|SECCOMP_PARSE_PERMISSIVE|
3341 (invert ? SECCOMP_PARSE_INVERT : 0)|
3342 (c->syscall_allow_list ? SECCOMP_PARSE_ALLOW_LIST : 0),
3343 unit, filename, line);
3344 if (r < 0)
3345 return r;
3346 }
3347 }
3348
3349 int config_parse_syscall_log(
3350 const char *unit,
3351 const char *filename,
3352 unsigned line,
3353 const char *section,
3354 unsigned section_line,
3355 const char *lvalue,
3356 int ltype,
3357 const char *rvalue,
3358 void *data,
3359 void *userdata) {
3360
3361 ExecContext *c = data;
3362 _unused_ const Unit *u = ASSERT_PTR(userdata);
3363 bool invert = false;
3364 const char *p;
3365 int r;
3366
3367 assert(filename);
3368 assert(lvalue);
3369 assert(rvalue);
3370
3371 if (isempty(rvalue)) {
3372 /* Empty assignment resets the list */
3373 c->syscall_log = hashmap_free(c->syscall_log);
3374 c->syscall_log_allow_list = false;
3375 return 0;
3376 }
3377
3378 if (rvalue[0] == '~') {
3379 invert = true;
3380 rvalue++;
3381 }
3382
3383 if (!c->syscall_log) {
3384 c->syscall_log = hashmap_new(NULL);
3385 if (!c->syscall_log)
3386 return log_oom();
3387
3388 if (invert)
3389 /* Log everything but the ones listed */
3390 c->syscall_log_allow_list = false;
3391 else
3392 /* Log nothing but the ones listed */
3393 c->syscall_log_allow_list = true;
3394 }
3395
3396 p = rvalue;
3397 for (;;) {
3398 _cleanup_free_ char *word = NULL;
3399
3400 r = extract_first_word(&p, &word, NULL, 0);
3401 if (r == -ENOMEM)
3402 return log_oom();
3403 if (r < 0) {
3404 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
3405 return 0;
3406 }
3407 if (r == 0)
3408 return 0;
3409
3410 r = seccomp_parse_syscall_filter(
3411 word, -1, c->syscall_log,
3412 SECCOMP_PARSE_LOG|SECCOMP_PARSE_PERMISSIVE|
3413 (invert ? SECCOMP_PARSE_INVERT : 0)|
3414 (c->syscall_log_allow_list ? SECCOMP_PARSE_ALLOW_LIST : 0),
3415 unit, filename, line);
3416 if (r < 0)
3417 return r;
3418 }
3419 }
3420
3421 int config_parse_syscall_archs(
3422 const char *unit,
3423 const char *filename,
3424 unsigned line,
3425 const char *section,
3426 unsigned section_line,
3427 const char *lvalue,
3428 int ltype,
3429 const char *rvalue,
3430 void *data,
3431 void *userdata) {
3432
3433 Set **archs = data;
3434 int r;
3435
3436 if (isempty(rvalue)) {
3437 *archs = set_free(*archs);
3438 return 0;
3439 }
3440
3441 for (const char *p = rvalue;;) {
3442 _cleanup_free_ char *word = NULL;
3443 uint32_t a;
3444
3445 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
3446 if (r == -ENOMEM)
3447 return log_oom();
3448 if (r < 0) {
3449 log_syntax(unit, LOG_WARNING, filename, line, r,
3450 "Invalid syntax, ignoring: %s", rvalue);
3451 return 0;
3452 }
3453 if (r == 0)
3454 return 0;
3455
3456 r = seccomp_arch_from_string(word, &a);
3457 if (r < 0) {
3458 log_syntax(unit, LOG_WARNING, filename, line, r,
3459 "Failed to parse system call architecture \"%s\", ignoring: %m", word);
3460 continue;
3461 }
3462
3463 r = set_ensure_put(archs, NULL, UINT32_TO_PTR(a + 1));
3464 if (r < 0)
3465 return log_oom();
3466 }
3467 }
3468
3469 int config_parse_syscall_errno(
3470 const char *unit,
3471 const char *filename,
3472 unsigned line,
3473 const char *section,
3474 unsigned section_line,
3475 const char *lvalue,
3476 int ltype,
3477 const char *rvalue,
3478 void *data,
3479 void *userdata) {
3480
3481 ExecContext *c = data;
3482 int e;
3483
3484 assert(filename);
3485 assert(lvalue);
3486 assert(rvalue);
3487
3488 if (isempty(rvalue) || streq(rvalue, "kill")) {
3489 /* Empty assignment resets to KILL */
3490 c->syscall_errno = SECCOMP_ERROR_NUMBER_KILL;
3491 return 0;
3492 }
3493
3494 e = parse_errno(rvalue);
3495 if (e < 0) {
3496 log_syntax(unit, LOG_WARNING, filename, line, e, "Failed to parse error number, ignoring: %s", rvalue);
3497 return 0;
3498 }
3499 if (e == 0) {
3500 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid error number, ignoring: %s", rvalue);
3501 return 0;
3502 }
3503
3504 c->syscall_errno = e;
3505 return 0;
3506 }
3507
3508 int config_parse_address_families(
3509 const char *unit,
3510 const char *filename,
3511 unsigned line,
3512 const char *section,
3513 unsigned section_line,
3514 const char *lvalue,
3515 int ltype,
3516 const char *rvalue,
3517 void *data,
3518 void *userdata) {
3519
3520 ExecContext *c = data;
3521 bool invert = false;
3522 int r;
3523
3524 assert(filename);
3525 assert(lvalue);
3526 assert(rvalue);
3527
3528 if (isempty(rvalue)) {
3529 /* Empty assignment resets the list */
3530 c->address_families = set_free(c->address_families);
3531 c->address_families_allow_list = false;
3532 return 0;
3533 }
3534
3535 if (streq(rvalue, "none")) {
3536 /* Forbid all address families. */
3537 c->address_families = set_free(c->address_families);
3538 c->address_families_allow_list = true;
3539 return 0;
3540 }
3541
3542 if (rvalue[0] == '~') {
3543 invert = true;
3544 rvalue++;
3545 }
3546
3547 if (!c->address_families) {
3548 c->address_families = set_new(NULL);
3549 if (!c->address_families)
3550 return log_oom();
3551
3552 c->address_families_allow_list = !invert;
3553 }
3554
3555 for (const char *p = rvalue;;) {
3556 _cleanup_free_ char *word = NULL;
3557 int af;
3558
3559 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
3560 if (r == -ENOMEM)
3561 return log_oom();
3562 if (r < 0) {
3563 log_syntax(unit, LOG_WARNING, filename, line, r,
3564 "Invalid syntax, ignoring: %s", rvalue);
3565 return 0;
3566 }
3567 if (r == 0)
3568 return 0;
3569
3570 af = af_from_name(word);
3571 if (af < 0) {
3572 log_syntax(unit, LOG_WARNING, filename, line, af,
3573 "Failed to parse address family, ignoring: %s", word);
3574 continue;
3575 }
3576
3577 /* If we previously wanted to forbid an address family and now
3578 * we want to allow it, then just remove it from the list.
3579 */
3580 if (!invert == c->address_families_allow_list) {
3581 r = set_put(c->address_families, INT_TO_PTR(af));
3582 if (r < 0)
3583 return log_oom();
3584 } else
3585 set_remove(c->address_families, INT_TO_PTR(af));
3586 }
3587 }
3588
3589 int config_parse_restrict_namespaces(
3590 const char *unit,
3591 const char *filename,
3592 unsigned line,
3593 const char *section,
3594 unsigned section_line,
3595 const char *lvalue,
3596 int ltype,
3597 const char *rvalue,
3598 void *data,
3599 void *userdata) {
3600
3601 ExecContext *c = data;
3602 unsigned long flags;
3603 bool invert = false;
3604 int r;
3605
3606 if (isempty(rvalue)) {
3607 /* Reset to the default. */
3608 c->restrict_namespaces = NAMESPACE_FLAGS_INITIAL;
3609 return 0;
3610 }
3611
3612 /* Boolean parameter ignores the previous settings */
3613 r = parse_boolean(rvalue);
3614 if (r > 0) {
3615 c->restrict_namespaces = 0;
3616 return 0;
3617 } else if (r == 0) {
3618 c->restrict_namespaces = NAMESPACE_FLAGS_ALL;
3619 return 0;
3620 }
3621
3622 if (rvalue[0] == '~') {
3623 invert = true;
3624 rvalue++;
3625 }
3626
3627 /* Not a boolean argument, in this case it's a list of namespace types. */
3628 r = namespace_flags_from_string(rvalue, &flags);
3629 if (r < 0) {
3630 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse namespace type string, ignoring: %s", rvalue);
3631 return 0;
3632 }
3633
3634 if (c->restrict_namespaces == NAMESPACE_FLAGS_INITIAL)
3635 /* Initial assignment. Just set the value. */
3636 c->restrict_namespaces = invert ? (~flags) & NAMESPACE_FLAGS_ALL : flags;
3637 else
3638 /* Merge the value with the previous one. */
3639 SET_FLAG(c->restrict_namespaces, flags, !invert);
3640
3641 return 0;
3642 }
3643 #endif
3644
3645 int config_parse_restrict_filesystems(
3646 const char *unit,
3647 const char *filename,
3648 unsigned line,
3649 const char *section,
3650 unsigned section_line,
3651 const char *lvalue,
3652 int ltype,
3653 const char *rvalue,
3654 void *data,
3655 void *userdata) {
3656 ExecContext *c = ASSERT_PTR(data);
3657 bool invert = false;
3658 int r;
3659
3660 assert(filename);
3661 assert(lvalue);
3662 assert(rvalue);
3663
3664 if (isempty(rvalue)) {
3665 /* Empty assignment resets the list */
3666 c->restrict_filesystems = set_free(c->restrict_filesystems);
3667 c->restrict_filesystems_allow_list = false;
3668 return 0;
3669 }
3670
3671 if (rvalue[0] == '~') {
3672 invert = true;
3673 rvalue++;
3674 }
3675
3676 if (!c->restrict_filesystems) {
3677 if (invert)
3678 /* Allow everything but the ones listed */
3679 c->restrict_filesystems_allow_list = false;
3680 else
3681 /* Allow nothing but the ones listed */
3682 c->restrict_filesystems_allow_list = true;
3683 }
3684
3685 for (const char *p = rvalue;;) {
3686 _cleanup_free_ char *word = NULL;
3687
3688 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
3689 if (r == 0)
3690 break;
3691 if (r == -ENOMEM)
3692 return log_oom();
3693 if (r < 0) {
3694 log_syntax(unit, LOG_WARNING, filename, line, r,
3695 "Trailing garbage in %s, ignoring: %s", lvalue, rvalue);
3696 break;
3697 }
3698
3699 r = lsm_bpf_parse_filesystem(
3700 word,
3701 &c->restrict_filesystems,
3702 FILESYSTEM_PARSE_LOG|
3703 (invert ? FILESYSTEM_PARSE_INVERT : 0)|
3704 (c->restrict_filesystems_allow_list ? FILESYSTEM_PARSE_ALLOW_LIST : 0),
3705 unit, filename, line);
3706
3707 if (r < 0)
3708 return r;
3709 }
3710
3711 return 0;
3712 }
3713
3714 int config_parse_unit_slice(
3715 const char *unit,
3716 const char *filename,
3717 unsigned line,
3718 const char *section,
3719 unsigned section_line,
3720 const char *lvalue,
3721 int ltype,
3722 const char *rvalue,
3723 void *data,
3724 void *userdata) {
3725
3726 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
3727 _cleanup_free_ char *k = NULL;
3728 Unit *u = userdata, *slice;
3729 int r;
3730
3731 assert(filename);
3732 assert(lvalue);
3733 assert(rvalue);
3734 assert(u);
3735
3736 r = unit_name_printf(u, rvalue, &k);
3737 if (r < 0) {
3738 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue);
3739 return 0;
3740 }
3741
3742 r = manager_load_unit(u->manager, k, NULL, &error, &slice);
3743 if (r < 0) {
3744 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to load slice unit %s, ignoring: %s", k, bus_error_message(&error, r));
3745 return 0;
3746 }
3747
3748 r = unit_set_slice(u, slice);
3749 if (r < 0) {
3750 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to assign slice %s to unit %s, ignoring: %m", slice->id, u->id);
3751 return 0;
3752 }
3753
3754 return 0;
3755 }
3756
3757 int config_parse_cpu_quota(
3758 const char *unit,
3759 const char *filename,
3760 unsigned line,
3761 const char *section,
3762 unsigned section_line,
3763 const char *lvalue,
3764 int ltype,
3765 const char *rvalue,
3766 void *data,
3767 void *userdata) {
3768
3769 CGroupContext *c = data;
3770 int r;
3771
3772 assert(filename);
3773 assert(lvalue);
3774 assert(rvalue);
3775
3776 if (isempty(rvalue)) {
3777 c->cpu_quota_per_sec_usec = USEC_INFINITY;
3778 return 0;
3779 }
3780
3781 r = parse_permyriad_unbounded(rvalue);
3782 if (r <= 0) {
3783 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid CPU quota '%s', ignoring.", rvalue);
3784 return 0;
3785 }
3786
3787 c->cpu_quota_per_sec_usec = ((usec_t) r * USEC_PER_SEC) / 10000U;
3788 return 0;
3789 }
3790
3791 int config_parse_allowed_cpuset(
3792 const char *unit,
3793 const char *filename,
3794 unsigned line,
3795 const char *section,
3796 unsigned section_line,
3797 const char *lvalue,
3798 int ltype,
3799 const char *rvalue,
3800 void *data,
3801 void *userdata) {
3802
3803 CPUSet *c = data;
3804
3805 (void) parse_cpu_set_extend(rvalue, c, true, unit, filename, line, lvalue);
3806 return 0;
3807 }
3808
3809 int config_parse_memory_limit(
3810 const char *unit,
3811 const char *filename,
3812 unsigned line,
3813 const char *section,
3814 unsigned section_line,
3815 const char *lvalue,
3816 int ltype,
3817 const char *rvalue,
3818 void *data,
3819 void *userdata) {
3820
3821 CGroupContext *c = data;
3822 uint64_t bytes = CGROUP_LIMIT_MAX;
3823 int r;
3824
3825 if (isempty(rvalue) && STR_IN_SET(lvalue, "DefaultMemoryLow",
3826 "DefaultMemoryMin",
3827 "MemoryLow",
3828 "StartupMemoryLow",
3829 "MemoryMin"))
3830 bytes = CGROUP_LIMIT_MIN;
3831 else if (!isempty(rvalue) && !streq(rvalue, "infinity")) {
3832
3833 r = parse_permyriad(rvalue);
3834 if (r < 0) {
3835 r = parse_size(rvalue, 1024, &bytes);
3836 if (r < 0) {
3837 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid memory limit '%s', ignoring: %m", rvalue);
3838 return 0;
3839 }
3840 } else
3841 bytes = physical_memory_scale(r, 10000U);
3842
3843 if (bytes >= UINT64_MAX ||
3844 (bytes <= 0 && !STR_IN_SET(lvalue,
3845 "MemorySwapMax",
3846 "StartupMemorySwapMax",
3847 "MemoryZSwapMax",
3848 "StartupMemoryZSwapMax",
3849 "MemoryLow",
3850 "StartupMemoryLow",
3851 "MemoryMin",
3852 "DefaultMemoryLow",
3853 "DefaultstartupMemoryLow",
3854 "DefaultMemoryMin"))) {
3855 log_syntax(unit, LOG_WARNING, filename, line, 0, "Memory limit '%s' out of range, ignoring.", rvalue);
3856 return 0;
3857 }
3858 }
3859
3860 if (streq(lvalue, "DefaultMemoryLow")) {
3861 c->default_memory_low = bytes;
3862 c->default_memory_low_set = true;
3863 } else if (streq(lvalue, "DefaultStartupMemoryLow")) {
3864 c->default_startup_memory_low = bytes;
3865 c->default_startup_memory_low_set = true;
3866 } else if (streq(lvalue, "DefaultMemoryMin")) {
3867 c->default_memory_min = bytes;
3868 c->default_memory_min_set = true;
3869 } else if (streq(lvalue, "MemoryMin")) {
3870 c->memory_min = bytes;
3871 c->memory_min_set = true;
3872 } else if (streq(lvalue, "MemoryLow")) {
3873 c->memory_low = bytes;
3874 c->memory_low_set = true;
3875 } else if (streq(lvalue, "StartupMemoryLow")) {
3876 c->startup_memory_low = bytes;
3877 c->startup_memory_low_set = true;
3878 } else if (streq(lvalue, "MemoryHigh"))
3879 c->memory_high = bytes;
3880 else if (streq(lvalue, "StartupMemoryHigh")) {
3881 c->startup_memory_high = bytes;
3882 c->startup_memory_high_set = true;
3883 } else if (streq(lvalue, "MemoryMax"))
3884 c->memory_max = bytes;
3885 else if (streq(lvalue, "StartupMemoryMax")) {
3886 c->startup_memory_max = bytes;
3887 c->startup_memory_max_set = true;
3888 } else if (streq(lvalue, "MemorySwapMax"))
3889 c->memory_swap_max = bytes;
3890 else if (streq(lvalue, "StartupMemorySwapMax")) {
3891 c->startup_memory_swap_max = bytes;
3892 c->startup_memory_swap_max_set = true;
3893 } else if (streq(lvalue, "MemoryZSwapMax"))
3894 c->memory_zswap_max = bytes;
3895 else if (streq(lvalue, "StartupMemoryZSwapMax")) {
3896 c->startup_memory_zswap_max = bytes;
3897 c->startup_memory_zswap_max_set = true;
3898 } else if (streq(lvalue, "MemoryLimit")) {
3899 log_syntax(unit, LOG_WARNING, filename, line, 0,
3900 "Unit uses MemoryLimit=; please use MemoryMax= instead. Support for MemoryLimit= will be removed soon.");
3901 c->memory_limit = bytes;
3902 } else
3903 return -EINVAL;
3904
3905 return 0;
3906 }
3907
3908 int config_parse_tasks_max(
3909 const char *unit,
3910 const char *filename,
3911 unsigned line,
3912 const char *section,
3913 unsigned section_line,
3914 const char *lvalue,
3915 int ltype,
3916 const char *rvalue,
3917 void *data,
3918 void *userdata) {
3919
3920 const Unit *u = userdata;
3921 TasksMax *tasks_max = data;
3922 uint64_t v;
3923 int r;
3924
3925 if (isempty(rvalue)) {
3926 *tasks_max = u ? u->manager->default_tasks_max : TASKS_MAX_UNSET;
3927 return 0;
3928 }
3929
3930 if (streq(rvalue, "infinity")) {
3931 *tasks_max = TASKS_MAX_UNSET;
3932 return 0;
3933 }
3934
3935 r = parse_permyriad(rvalue);
3936 if (r >= 0)
3937 *tasks_max = (TasksMax) { r, 10000U }; /* r‱ */
3938 else {
3939 r = safe_atou64(rvalue, &v);
3940 if (r < 0) {
3941 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid maximum tasks value '%s', ignoring: %m", rvalue);
3942 return 0;
3943 }
3944
3945 if (v <= 0 || v >= UINT64_MAX) {
3946 log_syntax(unit, LOG_WARNING, filename, line, 0, "Maximum tasks value '%s' out of range, ignoring.", rvalue);
3947 return 0;
3948 }
3949
3950 *tasks_max = (TasksMax) { v };
3951 }
3952
3953 return 0;
3954 }
3955
3956 int config_parse_delegate(
3957 const char *unit,
3958 const char *filename,
3959 unsigned line,
3960 const char *section,
3961 unsigned section_line,
3962 const char *lvalue,
3963 int ltype,
3964 const char *rvalue,
3965 void *data,
3966 void *userdata) {
3967
3968 CGroupContext *c = data;
3969 UnitType t;
3970 int r;
3971
3972 t = unit_name_to_type(unit);
3973 assert(t != _UNIT_TYPE_INVALID);
3974
3975 if (!unit_vtable[t]->can_delegate) {
3976 log_syntax(unit, LOG_WARNING, filename, line, 0, "Delegate= setting not supported for this unit type, ignoring.");
3977 return 0;
3978 }
3979
3980 /* We either accept a boolean value, which may be used to turn on delegation for all controllers, or
3981 * turn it off for all. Or it takes a list of controller names, in which case we add the specified
3982 * controllers to the mask to delegate. Delegate= enables delegation without any controllers. */
3983
3984 if (isempty(rvalue)) {
3985 /* An empty string resets controllers and sets Delegate=yes. */
3986 c->delegate = true;
3987 c->delegate_controllers = 0;
3988 return 0;
3989 }
3990
3991 r = parse_boolean(rvalue);
3992 if (r < 0) {
3993 CGroupMask mask = 0;
3994
3995 for (const char *p = rvalue;;) {
3996 _cleanup_free_ char *word = NULL;
3997 CGroupController cc;
3998
3999 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
4000 if (r == -ENOMEM)
4001 return log_oom();
4002 if (r < 0) {
4003 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
4004 return 0;
4005 }
4006 if (r == 0)
4007 break;
4008
4009 cc = cgroup_controller_from_string(word);
4010 if (cc < 0) {
4011 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid controller name '%s', ignoring", word);
4012 continue;
4013 }
4014
4015 mask |= CGROUP_CONTROLLER_TO_MASK(cc);
4016 }
4017
4018 c->delegate = true;
4019 c->delegate_controllers |= mask;
4020
4021 } else if (r > 0) {
4022 c->delegate = true;
4023 c->delegate_controllers = _CGROUP_MASK_ALL;
4024 } else {
4025 c->delegate = false;
4026 c->delegate_controllers = 0;
4027 }
4028
4029 return 0;
4030 }
4031
4032 int config_parse_delegate_subgroup(
4033 const char *unit,
4034 const char *filename,
4035 unsigned line,
4036 const char *section,
4037 unsigned section_line,
4038 const char *lvalue,
4039 int ltype,
4040 const char *rvalue,
4041 void *data,
4042 void *userdata) {
4043
4044 CGroupContext *c = ASSERT_PTR(data);
4045 UnitType t;
4046
4047 t = unit_name_to_type(unit);
4048 assert(t >= 0);
4049
4050 if (!unit_vtable[t]->can_delegate) {
4051 log_syntax(unit, LOG_WARNING, filename, line, 0, "DelegateSubgroup= setting not supported for this unit type, ignoring.");
4052 return 0;
4053 }
4054
4055 if (isempty(rvalue)) {
4056 c->delegate_subgroup = mfree(c->delegate_subgroup);
4057 return 0;
4058 }
4059
4060 if (cg_needs_escape(rvalue)) { /* Insist that specified names don't need escaping */
4061 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid control group name, ignoring: %s", rvalue);
4062 return 0;
4063 }
4064
4065 return free_and_strdup_warn(&c->delegate_subgroup, rvalue);
4066 }
4067
4068 int config_parse_managed_oom_mode(
4069 const char *unit,
4070 const char *filename,
4071 unsigned line,
4072 const char *section,
4073 unsigned section_line,
4074 const char *lvalue,
4075 int ltype,
4076 const char *rvalue,
4077 void *data,
4078 void *userdata) {
4079
4080 ManagedOOMMode *mode = data, m;
4081 UnitType t;
4082
4083 t = unit_name_to_type(unit);
4084 assert(t != _UNIT_TYPE_INVALID);
4085
4086 if (!unit_vtable[t]->can_set_managed_oom)
4087 return log_syntax(unit, LOG_WARNING, filename, line, 0, "%s= is not supported for this unit type, ignoring.", lvalue);
4088
4089 if (isempty(rvalue)) {
4090 *mode = MANAGED_OOM_AUTO;
4091 return 0;
4092 }
4093
4094 m = managed_oom_mode_from_string(rvalue);
4095 if (m < 0) {
4096 log_syntax(unit, LOG_WARNING, filename, line, m, "Invalid syntax, ignoring: %s", rvalue);
4097 return 0;
4098 }
4099
4100 *mode = m;
4101 return 0;
4102 }
4103
4104 int config_parse_managed_oom_mem_pressure_limit(
4105 const char *unit,
4106 const char *filename,
4107 unsigned line,
4108 const char *section,
4109 unsigned section_line,
4110 const char *lvalue,
4111 int ltype,
4112 const char *rvalue,
4113 void *data,
4114 void *userdata) {
4115
4116 uint32_t *limit = data;
4117 UnitType t;
4118 int r;
4119
4120 t = unit_name_to_type(unit);
4121 assert(t != _UNIT_TYPE_INVALID);
4122
4123 if (!unit_vtable[t]->can_set_managed_oom)
4124 return log_syntax(unit, LOG_WARNING, filename, line, 0, "%s= is not supported for this unit type, ignoring.", lvalue);
4125
4126 if (isempty(rvalue)) {
4127 *limit = 0;
4128 return 0;
4129 }
4130
4131 r = parse_permyriad(rvalue);
4132 if (r < 0) {
4133 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse memory pressure limit value, ignoring: %s", rvalue);
4134 return 0;
4135 }
4136
4137 /* Normalize to 2^32-1 == 100% */
4138 *limit = UINT32_SCALE_FROM_PERMYRIAD(r);
4139 return 0;
4140 }
4141
4142 int config_parse_device_allow(
4143 const char *unit,
4144 const char *filename,
4145 unsigned line,
4146 const char *section,
4147 unsigned section_line,
4148 const char *lvalue,
4149 int ltype,
4150 const char *rvalue,
4151 void *data,
4152 void *userdata) {
4153
4154 _cleanup_free_ char *path = NULL, *resolved = NULL;
4155 CGroupContext *c = data;
4156 const char *p = rvalue;
4157 int r;
4158
4159 if (isempty(rvalue)) {
4160 while (c->device_allow)
4161 cgroup_context_free_device_allow(c, c->device_allow);
4162
4163 return 0;
4164 }
4165
4166 r = extract_first_word(&p, &path, NULL, EXTRACT_UNQUOTE);
4167 if (r == -ENOMEM)
4168 return log_oom();
4169 if (r <= 0) {
4170 log_syntax(unit, LOG_WARNING, filename, line, r,
4171 "Failed to extract device path and rights from '%s', ignoring.", rvalue);
4172 return 0;
4173 }
4174
4175 r = unit_path_printf(userdata, path, &resolved);
4176 if (r < 0) {
4177 log_syntax(unit, LOG_WARNING, filename, line, r,
4178 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
4179 return 0;
4180 }
4181
4182 if (!STARTSWITH_SET(resolved, "block-", "char-")) {
4183
4184 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
4185 if (r < 0)
4186 return 0;
4187
4188 if (!valid_device_node_path(resolved)) {
4189 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid device node path '%s', ignoring.", resolved);
4190 return 0;
4191 }
4192 }
4193
4194 if (!isempty(p) && !in_charset(p, "rwm")) {
4195 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid device rights '%s', ignoring.", p);
4196 return 0;
4197 }
4198
4199 return cgroup_add_device_allow(c, resolved, p);
4200 }
4201
4202 int config_parse_io_device_weight(
4203 const char *unit,
4204 const char *filename,
4205 unsigned line,
4206 const char *section,
4207 unsigned section_line,
4208 const char *lvalue,
4209 int ltype,
4210 const char *rvalue,
4211 void *data,
4212 void *userdata) {
4213
4214 _cleanup_free_ char *path = NULL, *resolved = NULL;
4215 CGroupIODeviceWeight *w;
4216 CGroupContext *c = data;
4217 const char *p = ASSERT_PTR(rvalue);
4218 uint64_t u;
4219 int r;
4220
4221 assert(filename);
4222 assert(lvalue);
4223
4224 if (isempty(rvalue)) {
4225 while (c->io_device_weights)
4226 cgroup_context_free_io_device_weight(c, c->io_device_weights);
4227
4228 return 0;
4229 }
4230
4231 r = extract_first_word(&p, &path, NULL, EXTRACT_UNQUOTE);
4232 if (r == -ENOMEM)
4233 return log_oom();
4234 if (r < 0) {
4235 log_syntax(unit, LOG_WARNING, filename, line, r,
4236 "Failed to extract device path and weight from '%s', ignoring.", rvalue);
4237 return 0;
4238 }
4239 if (r == 0 || isempty(p)) {
4240 log_syntax(unit, LOG_WARNING, filename, line, 0,
4241 "Invalid device path or weight specified in '%s', ignoring.", rvalue);
4242 return 0;
4243 }
4244
4245 r = unit_path_printf(userdata, path, &resolved);
4246 if (r < 0) {
4247 log_syntax(unit, LOG_WARNING, filename, line, r,
4248 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
4249 return 0;
4250 }
4251
4252 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
4253 if (r < 0)
4254 return 0;
4255
4256 r = cg_weight_parse(p, &u);
4257 if (r < 0) {
4258 log_syntax(unit, LOG_WARNING, filename, line, r, "IO weight '%s' invalid, ignoring: %m", p);
4259 return 0;
4260 }
4261
4262 assert(u != CGROUP_WEIGHT_INVALID);
4263
4264 w = new0(CGroupIODeviceWeight, 1);
4265 if (!w)
4266 return log_oom();
4267
4268 w->path = TAKE_PTR(resolved);
4269 w->weight = u;
4270
4271 LIST_PREPEND(device_weights, c->io_device_weights, w);
4272 return 0;
4273 }
4274
4275 int config_parse_io_device_latency(
4276 const char *unit,
4277 const char *filename,
4278 unsigned line,
4279 const char *section,
4280 unsigned section_line,
4281 const char *lvalue,
4282 int ltype,
4283 const char *rvalue,
4284 void *data,
4285 void *userdata) {
4286
4287 _cleanup_free_ char *path = NULL, *resolved = NULL;
4288 CGroupIODeviceLatency *l;
4289 CGroupContext *c = data;
4290 const char *p = ASSERT_PTR(rvalue);
4291 usec_t usec;
4292 int r;
4293
4294 assert(filename);
4295 assert(lvalue);
4296
4297 if (isempty(rvalue)) {
4298 while (c->io_device_latencies)
4299 cgroup_context_free_io_device_latency(c, c->io_device_latencies);
4300
4301 return 0;
4302 }
4303
4304 r = extract_first_word(&p, &path, NULL, EXTRACT_UNQUOTE);
4305 if (r == -ENOMEM)
4306 return log_oom();
4307 if (r < 0) {
4308 log_syntax(unit, LOG_WARNING, filename, line, r,
4309 "Failed to extract device path and latency from '%s', ignoring.", rvalue);
4310 return 0;
4311 }
4312 if (r == 0 || isempty(p)) {
4313 log_syntax(unit, LOG_WARNING, filename, line, 0,
4314 "Invalid device path or latency specified in '%s', ignoring.", rvalue);
4315 return 0;
4316 }
4317
4318 r = unit_path_printf(userdata, path, &resolved);
4319 if (r < 0) {
4320 log_syntax(unit, LOG_WARNING, filename, line, r,
4321 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
4322 return 0;
4323 }
4324
4325 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
4326 if (r < 0)
4327 return 0;
4328
4329 r = parse_sec(p, &usec);
4330 if (r < 0) {
4331 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse timer value, ignoring: %s", p);
4332 return 0;
4333 }
4334
4335 l = new0(CGroupIODeviceLatency, 1);
4336 if (!l)
4337 return log_oom();
4338
4339 l->path = TAKE_PTR(resolved);
4340 l->target_usec = usec;
4341
4342 LIST_PREPEND(device_latencies, c->io_device_latencies, l);
4343 return 0;
4344 }
4345
4346 int config_parse_io_limit(
4347 const char *unit,
4348 const char *filename,
4349 unsigned line,
4350 const char *section,
4351 unsigned section_line,
4352 const char *lvalue,
4353 int ltype,
4354 const char *rvalue,
4355 void *data,
4356 void *userdata) {
4357
4358 _cleanup_free_ char *path = NULL, *resolved = NULL;
4359 CGroupIODeviceLimit *l = NULL;
4360 CGroupContext *c = data;
4361 CGroupIOLimitType type;
4362 const char *p = ASSERT_PTR(rvalue);
4363 uint64_t num;
4364 int r;
4365
4366 assert(filename);
4367 assert(lvalue);
4368
4369 type = cgroup_io_limit_type_from_string(lvalue);
4370 assert(type >= 0);
4371
4372 if (isempty(rvalue)) {
4373 LIST_FOREACH(device_limits, t, c->io_device_limits)
4374 t->limits[type] = cgroup_io_limit_defaults[type];
4375 return 0;
4376 }
4377
4378 r = extract_first_word(&p, &path, NULL, EXTRACT_UNQUOTE);
4379 if (r == -ENOMEM)
4380 return log_oom();
4381 if (r < 0) {
4382 log_syntax(unit, LOG_WARNING, filename, line, r,
4383 "Failed to extract device node and bandwidth from '%s', ignoring.", rvalue);
4384 return 0;
4385 }
4386 if (r == 0 || isempty(p)) {
4387 log_syntax(unit, LOG_WARNING, filename, line, 0,
4388 "Invalid device node or bandwidth specified in '%s', ignoring.", rvalue);
4389 return 0;
4390 }
4391
4392 r = unit_path_printf(userdata, path, &resolved);
4393 if (r < 0) {
4394 log_syntax(unit, LOG_WARNING, filename, line, r,
4395 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
4396 return 0;
4397 }
4398
4399 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
4400 if (r < 0)
4401 return 0;
4402
4403 if (streq("infinity", p))
4404 num = CGROUP_LIMIT_MAX;
4405 else {
4406 r = parse_size(p, 1000, &num);
4407 if (r < 0 || num <= 0) {
4408 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid IO limit '%s', ignoring.", p);
4409 return 0;
4410 }
4411 }
4412
4413 LIST_FOREACH(device_limits, t, c->io_device_limits)
4414 if (path_equal(resolved, t->path)) {
4415 l = t;
4416 break;
4417 }
4418
4419 if (!l) {
4420 l = new0(CGroupIODeviceLimit, 1);
4421 if (!l)
4422 return log_oom();
4423
4424 l->path = TAKE_PTR(resolved);
4425 for (CGroupIOLimitType i = 0; i < _CGROUP_IO_LIMIT_TYPE_MAX; i++)
4426 l->limits[i] = cgroup_io_limit_defaults[i];
4427
4428 LIST_PREPEND(device_limits, c->io_device_limits, l);
4429 }
4430
4431 l->limits[type] = num;
4432
4433 return 0;
4434 }
4435
4436 int config_parse_blockio_device_weight(
4437 const char *unit,
4438 const char *filename,
4439 unsigned line,
4440 const char *section,
4441 unsigned section_line,
4442 const char *lvalue,
4443 int ltype,
4444 const char *rvalue,
4445 void *data,
4446 void *userdata) {
4447
4448 _cleanup_free_ char *path = NULL, *resolved = NULL;
4449 CGroupBlockIODeviceWeight *w;
4450 CGroupContext *c = data;
4451 const char *p = ASSERT_PTR(rvalue);
4452 uint64_t u;
4453 int r;
4454
4455 assert(filename);
4456 assert(lvalue);
4457
4458 log_syntax(unit, LOG_WARNING, filename, line, 0,
4459 "Unit uses %s=; please use IO*= settings instead. Support for %s= will be removed soon.",
4460 lvalue, lvalue);
4461
4462 if (isempty(rvalue)) {
4463 while (c->blockio_device_weights)
4464 cgroup_context_free_blockio_device_weight(c, c->blockio_device_weights);
4465
4466 return 0;
4467 }
4468
4469 r = extract_first_word(&p, &path, NULL, EXTRACT_UNQUOTE);
4470 if (r == -ENOMEM)
4471 return log_oom();
4472 if (r < 0) {
4473 log_syntax(unit, LOG_WARNING, filename, line, r,
4474 "Failed to extract device node and weight from '%s', ignoring.", rvalue);
4475 return 0;
4476 }
4477 if (r == 0 || isempty(p)) {
4478 log_syntax(unit, LOG_WARNING, filename, line, 0,
4479 "Invalid device node or weight specified in '%s', ignoring.", rvalue);
4480 return 0;
4481 }
4482
4483 r = unit_path_printf(userdata, path, &resolved);
4484 if (r < 0) {
4485 log_syntax(unit, LOG_WARNING, filename, line, r,
4486 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
4487 return 0;
4488 }
4489
4490 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
4491 if (r < 0)
4492 return 0;
4493
4494 r = cg_blkio_weight_parse(p, &u);
4495 if (r < 0) {
4496 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid block IO weight '%s', ignoring: %m", p);
4497 return 0;
4498 }
4499
4500 assert(u != CGROUP_BLKIO_WEIGHT_INVALID);
4501
4502 w = new0(CGroupBlockIODeviceWeight, 1);
4503 if (!w)
4504 return log_oom();
4505
4506 w->path = TAKE_PTR(resolved);
4507 w->weight = u;
4508
4509 LIST_PREPEND(device_weights, c->blockio_device_weights, w);
4510 return 0;
4511 }
4512
4513 int config_parse_blockio_bandwidth(
4514 const char *unit,
4515 const char *filename,
4516 unsigned line,
4517 const char *section,
4518 unsigned section_line,
4519 const char *lvalue,
4520 int ltype,
4521 const char *rvalue,
4522 void *data,
4523 void *userdata) {
4524
4525 _cleanup_free_ char *path = NULL, *resolved = NULL;
4526 CGroupBlockIODeviceBandwidth *b = NULL;
4527 CGroupContext *c = data;
4528 const char *p = ASSERT_PTR(rvalue);
4529 uint64_t bytes;
4530 bool read;
4531 int r;
4532
4533 assert(filename);
4534 assert(lvalue);
4535
4536 log_syntax(unit, LOG_WARNING, filename, line, 0,
4537 "Unit uses %s=; please use IO*= settings instead. Support for %s= will be removed soon.",
4538 lvalue, lvalue);
4539
4540 read = streq("BlockIOReadBandwidth", lvalue);
4541
4542 if (isempty(rvalue)) {
4543 LIST_FOREACH(device_bandwidths, t, c->blockio_device_bandwidths) {
4544 t->rbps = CGROUP_LIMIT_MAX;
4545 t->wbps = CGROUP_LIMIT_MAX;
4546 }
4547 return 0;
4548 }
4549
4550 r = extract_first_word(&p, &path, NULL, EXTRACT_UNQUOTE);
4551 if (r == -ENOMEM)
4552 return log_oom();
4553 if (r < 0) {
4554 log_syntax(unit, LOG_WARNING, filename, line, r,
4555 "Failed to extract device node and bandwidth from '%s', ignoring.", rvalue);
4556 return 0;
4557 }
4558 if (r == 0 || isempty(p)) {
4559 log_syntax(unit, LOG_WARNING, filename, line, 0,
4560 "Invalid device node or bandwidth specified in '%s', ignoring.", rvalue);
4561 return 0;
4562 }
4563
4564 r = unit_path_printf(userdata, path, &resolved);
4565 if (r < 0) {
4566 log_syntax(unit, LOG_WARNING, filename, line, r,
4567 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
4568 return 0;
4569 }
4570
4571 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
4572 if (r < 0)
4573 return 0;
4574
4575 r = parse_size(p, 1000, &bytes);
4576 if (r < 0 || bytes <= 0) {
4577 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid Block IO Bandwidth '%s', ignoring.", p);
4578 return 0;
4579 }
4580
4581 LIST_FOREACH(device_bandwidths, t, c->blockio_device_bandwidths)
4582 if (path_equal(resolved, t->path)) {
4583 b = t;
4584 break;
4585 }
4586
4587 if (!b) {
4588 b = new0(CGroupBlockIODeviceBandwidth, 1);
4589 if (!b)
4590 return log_oom();
4591
4592 b->path = TAKE_PTR(resolved);
4593 b->rbps = CGROUP_LIMIT_MAX;
4594 b->wbps = CGROUP_LIMIT_MAX;
4595
4596 LIST_PREPEND(device_bandwidths, c->blockio_device_bandwidths, b);
4597 }
4598
4599 if (read)
4600 b->rbps = bytes;
4601 else
4602 b->wbps = bytes;
4603
4604 return 0;
4605 }
4606
4607 int config_parse_job_mode_isolate(
4608 const char *unit,
4609 const char *filename,
4610 unsigned line,
4611 const char *section,
4612 unsigned section_line,
4613 const char *lvalue,
4614 int ltype,
4615 const char *rvalue,
4616 void *data,
4617 void *userdata) {
4618
4619 JobMode *m = data;
4620 int r;
4621
4622 assert(filename);
4623 assert(lvalue);
4624 assert(rvalue);
4625
4626 r = parse_boolean(rvalue);
4627 if (r < 0) {
4628 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse boolean, ignoring: %s", rvalue);
4629 return 0;
4630 }
4631
4632 log_notice("%s is deprecated. Please use OnFailureJobMode= instead", lvalue);
4633
4634 *m = r ? JOB_ISOLATE : JOB_REPLACE;
4635 return 0;
4636 }
4637
4638 int config_parse_exec_directories(
4639 const char *unit,
4640 const char *filename,
4641 unsigned line,
4642 const char *section,
4643 unsigned section_line,
4644 const char *lvalue,
4645 int ltype,
4646 const char *rvalue,
4647 void *data,
4648 void *userdata) {
4649
4650 ExecDirectory *ed = ASSERT_PTR(data);
4651 const Unit *u = userdata;
4652 int r;
4653
4654 assert(filename);
4655 assert(lvalue);
4656 assert(rvalue);
4657
4658 if (isempty(rvalue)) {
4659 /* Empty assignment resets the list */
4660 exec_directory_done(ed);
4661 return 0;
4662 }
4663
4664 for (const char *p = rvalue;;) {
4665 _cleanup_free_ char *tuple = NULL;
4666
4667 r = extract_first_word(&p, &tuple, NULL, EXTRACT_UNQUOTE|EXTRACT_RETAIN_ESCAPE);
4668 if (r == -ENOMEM)
4669 return log_oom();
4670 if (r < 0) {
4671 log_syntax(unit, LOG_WARNING, filename, line, r,
4672 "Invalid syntax %s=%s, ignoring: %m", lvalue, rvalue);
4673 return 0;
4674 }
4675 if (r == 0)
4676 return 0;
4677
4678 _cleanup_free_ char *src = NULL, *dest = NULL;
4679 const char *q = tuple;
4680 r = extract_many_words(&q, ":", EXTRACT_CUNESCAPE|EXTRACT_UNESCAPE_SEPARATORS, &src, &dest, NULL);
4681 if (r == -ENOMEM)
4682 return log_oom();
4683 if (r <= 0) {
4684 log_syntax(unit, LOG_WARNING, filename, line, r,
4685 "Invalid syntax in %s=, ignoring: %s", lvalue, tuple);
4686 return 0;
4687 }
4688
4689 _cleanup_free_ char *sresolved = NULL;
4690 r = unit_path_printf(u, src, &sresolved);
4691 if (r < 0) {
4692 log_syntax(unit, LOG_WARNING, filename, line, r,
4693 "Failed to resolve unit specifiers in \"%s\", ignoring: %m", src);
4694 continue;
4695 }
4696
4697 r = path_simplify_and_warn(sresolved, PATH_CHECK_RELATIVE, unit, filename, line, lvalue);
4698 if (r < 0)
4699 continue;
4700
4701 if (path_startswith(sresolved, "private")) {
4702 log_syntax(unit, LOG_WARNING, filename, line, 0,
4703 "%s= path can't be 'private', ignoring assignment: %s", lvalue, tuple);
4704 continue;
4705 }
4706
4707 /* For State and Runtime directories we support an optional destination parameter, which
4708 * will be used to create a symlink to the source. */
4709 _cleanup_free_ char *dresolved = NULL;
4710 if (!isempty(dest)) {
4711 if (streq(lvalue, "ConfigurationDirectory")) {
4712 log_syntax(unit, LOG_WARNING, filename, line, 0,
4713 "Destination parameter is not supported for ConfigurationDirectory, ignoring: %s", tuple);
4714 continue;
4715 }
4716
4717 r = unit_path_printf(u, dest, &dresolved);
4718 if (r < 0) {
4719 log_syntax(unit, LOG_WARNING, filename, line, r,
4720 "Failed to resolve unit specifiers in \"%s\", ignoring: %m", dest);
4721 continue;
4722 }
4723
4724 r = path_simplify_and_warn(dresolved, PATH_CHECK_RELATIVE, unit, filename, line, lvalue);
4725 if (r < 0)
4726 continue;
4727 }
4728
4729 r = exec_directory_add(ed, sresolved, dresolved);
4730 if (r < 0)
4731 return log_oom();
4732 }
4733 }
4734
4735 int config_parse_set_credential(
4736 const char *unit,
4737 const char *filename,
4738 unsigned line,
4739 const char *section,
4740 unsigned section_line,
4741 const char *lvalue,
4742 int ltype,
4743 const char *rvalue,
4744 void *data,
4745 void *userdata) {
4746
4747 _cleanup_free_ char *word = NULL, *k = NULL;
4748 _cleanup_free_ void *d = NULL;
4749 ExecContext *context = ASSERT_PTR(data);
4750 ExecSetCredential *old;
4751 Unit *u = userdata;
4752 bool encrypted = ltype;
4753 const char *p = ASSERT_PTR(rvalue);
4754 size_t size;
4755 int r;
4756
4757 assert(filename);
4758 assert(lvalue);
4759
4760 if (isempty(rvalue)) {
4761 /* Empty assignment resets the list */
4762 context->set_credentials = hashmap_free(context->set_credentials);
4763 return 0;
4764 }
4765
4766 r = extract_first_word(&p, &word, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
4767 if (r == -ENOMEM)
4768 return log_oom();
4769 if (r < 0) {
4770 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to extract credential name, ignoring: %s", rvalue);
4771 return 0;
4772 }
4773 if (r == 0 || isempty(p)) {
4774 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid syntax, ignoring: %s", rvalue);
4775 return 0;
4776 }
4777
4778 r = unit_cred_printf(u, word, &k);
4779 if (r < 0) {
4780 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in \"%s\", ignoring: %m", word);
4781 return 0;
4782 }
4783 if (!credential_name_valid(k)) {
4784 log_syntax(unit, LOG_WARNING, filename, line, 0, "Credential name \"%s\" not valid, ignoring.", k);
4785 return 0;
4786 }
4787
4788 if (encrypted) {
4789 r = unbase64mem_full(p, SIZE_MAX, true, &d, &size);
4790 if (r < 0) {
4791 log_syntax(unit, LOG_WARNING, filename, line, r, "Encrypted credential data not valid Base64 data, ignoring.");
4792 return 0;
4793 }
4794 } else {
4795 char *unescaped;
4796 ssize_t l;
4797
4798 /* We support escape codes here, so that users can insert trailing \n if they like */
4799 l = cunescape(p, UNESCAPE_ACCEPT_NUL, &unescaped);
4800 if (l < 0) {
4801 log_syntax(unit, LOG_WARNING, filename, line, l, "Can't unescape \"%s\", ignoring: %m", p);
4802 return 0;
4803 }
4804
4805 d = unescaped;
4806 size = l;
4807 }
4808
4809 old = hashmap_get(context->set_credentials, k);
4810 if (old) {
4811 free_and_replace(old->data, d);
4812 old->size = size;
4813 old->encrypted = encrypted;
4814 } else {
4815 _cleanup_(exec_set_credential_freep) ExecSetCredential *sc = NULL;
4816
4817 sc = new(ExecSetCredential, 1);
4818 if (!sc)
4819 return log_oom();
4820
4821 *sc = (ExecSetCredential) {
4822 .id = TAKE_PTR(k),
4823 .data = TAKE_PTR(d),
4824 .size = size,
4825 .encrypted = encrypted,
4826 };
4827
4828 r = hashmap_ensure_put(&context->set_credentials, &exec_set_credential_hash_ops, sc->id, sc);
4829 if (r == -ENOMEM)
4830 return log_oom();
4831 if (r < 0) {
4832 log_syntax(unit, LOG_WARNING, filename, line, r,
4833 "Duplicated credential value '%s', ignoring assignment: %s", sc->id, rvalue);
4834 return 0;
4835 }
4836
4837 TAKE_PTR(sc);
4838 }
4839
4840 return 0;
4841 }
4842
4843 int config_parse_load_credential(
4844 const char *unit,
4845 const char *filename,
4846 unsigned line,
4847 const char *section,
4848 unsigned section_line,
4849 const char *lvalue,
4850 int ltype,
4851 const char *rvalue,
4852 void *data,
4853 void *userdata) {
4854
4855 _cleanup_free_ char *word = NULL, *k = NULL, *q = NULL;
4856 ExecContext *context = ASSERT_PTR(data);
4857 ExecLoadCredential *old;
4858 bool encrypted = ltype;
4859 Unit *u = userdata;
4860 const char *p;
4861 int r;
4862
4863 assert(filename);
4864 assert(lvalue);
4865 assert(rvalue);
4866
4867 if (isempty(rvalue)) {
4868 /* Empty assignment resets the list */
4869 context->load_credentials = hashmap_free(context->load_credentials);
4870 return 0;
4871 }
4872
4873 p = rvalue;
4874 r = extract_first_word(&p, &word, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
4875 if (r == -ENOMEM)
4876 return log_oom();
4877 if (r <= 0) {
4878 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
4879 return 0;
4880 }
4881
4882 r = unit_cred_printf(u, word, &k);
4883 if (r < 0) {
4884 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in \"%s\", ignoring: %m", word);
4885 return 0;
4886 }
4887 if (!credential_name_valid(k)) {
4888 log_syntax(unit, LOG_WARNING, filename, line, 0, "Credential name \"%s\" not valid, ignoring.", k);
4889 return 0;
4890 }
4891
4892 if (isempty(p)) {
4893 /* If only one field is specified take it as shortcut for inheriting a credential named
4894 * the same way from our parent */
4895 q = strdup(k);
4896 if (!q)
4897 return log_oom();
4898 } else {
4899 r = unit_path_printf(u, p, &q);
4900 if (r < 0) {
4901 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in \"%s\", ignoring: %m", p);
4902 return 0;
4903 }
4904 if (path_is_absolute(q) ? !path_is_normalized(q) : !credential_name_valid(q)) {
4905 log_syntax(unit, LOG_WARNING, filename, line, 0, "Credential source \"%s\" not valid, ignoring.", q);
4906 return 0;
4907 }
4908 }
4909
4910 old = hashmap_get(context->load_credentials, k);
4911 if (old) {
4912 free_and_replace(old->path, q);
4913 old->encrypted = encrypted;
4914 } else {
4915 _cleanup_(exec_load_credential_freep) ExecLoadCredential *lc = NULL;
4916
4917 lc = new(ExecLoadCredential, 1);
4918 if (!lc)
4919 return log_oom();
4920
4921 *lc = (ExecLoadCredential) {
4922 .id = TAKE_PTR(k),
4923 .path = TAKE_PTR(q),
4924 .encrypted = encrypted,
4925 };
4926
4927 r = hashmap_ensure_put(&context->load_credentials, &exec_load_credential_hash_ops, lc->id, lc);
4928 if (r == -ENOMEM)
4929 return log_oom();
4930 if (r < 0) {
4931 log_syntax(unit, LOG_WARNING, filename, line, r,
4932 "Duplicated credential value '%s', ignoring assignment: %s", lc->id, rvalue);
4933 return 0;
4934 }
4935
4936 TAKE_PTR(lc);
4937 }
4938
4939 return 0;
4940 }
4941
4942 int config_parse_set_status(
4943 const char *unit,
4944 const char *filename,
4945 unsigned line,
4946 const char *section,
4947 unsigned section_line,
4948 const char *lvalue,
4949 int ltype,
4950 const char *rvalue,
4951 void *data,
4952 void *userdata) {
4953
4954 ExitStatusSet *status_set = ASSERT_PTR(data);
4955 int r;
4956
4957 assert(filename);
4958 assert(lvalue);
4959 assert(rvalue);
4960
4961 /* Empty assignment resets the list */
4962 if (isempty(rvalue)) {
4963 exit_status_set_free(status_set);
4964 return 0;
4965 }
4966
4967 for (const char *p = rvalue;;) {
4968 _cleanup_free_ char *word = NULL;
4969 Bitmap *bitmap;
4970
4971 r = extract_first_word(&p, &word, NULL, 0);
4972 if (r == -ENOMEM)
4973 return log_oom();
4974 if (r < 0) {
4975 log_syntax(unit, LOG_WARNING, filename, line, r,
4976 "Failed to parse %s=%s, ignoring: %m", lvalue, rvalue);
4977 return 0;
4978 }
4979 if (r == 0)
4980 return 0;
4981
4982 /* We need to call exit_status_from_string() first, because we want
4983 * to parse numbers as exit statuses, not signals. */
4984
4985 r = exit_status_from_string(word);
4986 if (r >= 0) {
4987 assert(r >= 0 && r < 256);
4988 bitmap = &status_set->status;
4989 } else {
4990 r = signal_from_string(word);
4991 if (r < 0) {
4992 log_syntax(unit, LOG_WARNING, filename, line, r,
4993 "Failed to parse value, ignoring: %s", word);
4994 continue;
4995 }
4996 bitmap = &status_set->signal;
4997 }
4998
4999 r = bitmap_set(bitmap, r);
5000 if (r < 0)
5001 log_syntax(unit, LOG_WARNING, filename, line, r,
5002 "Failed to set signal or status %s, ignoring: %m", word);
5003 }
5004 }
5005
5006 int config_parse_namespace_path_strv(
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 char*** sv = 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 *sv = strv_free(*sv);
5029 return 0;
5030 }
5031
5032 for (const char *p = rvalue;;) {
5033 _cleanup_free_ char *word = NULL, *resolved = NULL, *joined = NULL;
5034 const char *w;
5035 bool ignore_enoent = false, shall_prefix = false;
5036
5037 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
5038 if (r == -ENOMEM)
5039 return log_oom();
5040 if (r < 0) {
5041 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to extract first word, ignoring: %s", rvalue);
5042 return 0;
5043 }
5044 if (r == 0)
5045 break;
5046
5047 w = word;
5048 if (startswith(w, "-")) {
5049 ignore_enoent = true;
5050 w++;
5051 }
5052 if (startswith(w, "+")) {
5053 shall_prefix = true;
5054 w++;
5055 }
5056
5057 r = unit_path_printf(u, w, &resolved);
5058 if (r < 0) {
5059 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in %s: %m", w);
5060 continue;
5061 }
5062
5063 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
5064 if (r < 0)
5065 continue;
5066
5067 joined = strjoin(ignore_enoent ? "-" : "",
5068 shall_prefix ? "+" : "",
5069 resolved);
5070
5071 r = strv_push(sv, joined);
5072 if (r < 0)
5073 return log_oom();
5074
5075 joined = NULL;
5076 }
5077
5078 return 0;
5079 }
5080
5081 int config_parse_temporary_filesystems(
5082 const char *unit,
5083 const char *filename,
5084 unsigned line,
5085 const char *section,
5086 unsigned section_line,
5087 const char *lvalue,
5088 int ltype,
5089 const char *rvalue,
5090 void *data,
5091 void *userdata) {
5092
5093 const Unit *u = userdata;
5094 ExecContext *c = ASSERT_PTR(data);
5095 int r;
5096
5097 assert(filename);
5098 assert(lvalue);
5099 assert(rvalue);
5100
5101 if (isempty(rvalue)) {
5102 /* Empty assignment resets the list */
5103 temporary_filesystem_free_many(c->temporary_filesystems, c->n_temporary_filesystems);
5104 c->temporary_filesystems = NULL;
5105 c->n_temporary_filesystems = 0;
5106 return 0;
5107 }
5108
5109 for (const char *p = rvalue;;) {
5110 _cleanup_free_ char *word = NULL, *path = NULL, *resolved = NULL;
5111 const char *w;
5112
5113 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
5114 if (r == -ENOMEM)
5115 return log_oom();
5116 if (r < 0) {
5117 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to extract first word, ignoring: %s", rvalue);
5118 return 0;
5119 }
5120 if (r == 0)
5121 return 0;
5122
5123 w = word;
5124 r = extract_first_word(&w, &path, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
5125 if (r == -ENOMEM)
5126 return log_oom();
5127 if (r < 0) {
5128 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to extract first word, ignoring: %s", word);
5129 continue;
5130 }
5131 if (r == 0) {
5132 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid syntax, ignoring: %s", word);
5133 continue;
5134 }
5135
5136 r = unit_path_printf(u, path, &resolved);
5137 if (r < 0) {
5138 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", path);
5139 continue;
5140 }
5141
5142 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
5143 if (r < 0)
5144 continue;
5145
5146 r = temporary_filesystem_add(&c->temporary_filesystems, &c->n_temporary_filesystems, resolved, w);
5147 if (r < 0)
5148 return log_oom();
5149 }
5150 }
5151
5152 int config_parse_bind_paths(
5153 const char *unit,
5154 const char *filename,
5155 unsigned line,
5156 const char *section,
5157 unsigned section_line,
5158 const char *lvalue,
5159 int ltype,
5160 const char *rvalue,
5161 void *data,
5162 void *userdata) {
5163
5164 ExecContext *c = ASSERT_PTR(data);
5165 const Unit *u = userdata;
5166 int r;
5167
5168 assert(filename);
5169 assert(lvalue);
5170 assert(rvalue);
5171
5172 if (isempty(rvalue)) {
5173 /* Empty assignment resets the list */
5174 bind_mount_free_many(c->bind_mounts, c->n_bind_mounts);
5175 c->bind_mounts = NULL;
5176 c->n_bind_mounts = 0;
5177 return 0;
5178 }
5179
5180 for (const char *p = rvalue;;) {
5181 _cleanup_free_ char *source = NULL, *destination = NULL;
5182 _cleanup_free_ char *sresolved = NULL, *dresolved = NULL;
5183 char *s = NULL, *d = NULL;
5184 bool rbind = true, ignore_enoent = false;
5185
5186 r = extract_first_word(&p, &source, ":" WHITESPACE, EXTRACT_UNQUOTE|EXTRACT_DONT_COALESCE_SEPARATORS);
5187 if (r == -ENOMEM)
5188 return log_oom();
5189 if (r < 0) {
5190 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse %s, ignoring: %s", lvalue, rvalue);
5191 return 0;
5192 }
5193 if (r == 0)
5194 break;
5195
5196 r = unit_full_printf_full(u, source, PATH_MAX, &sresolved);
5197 if (r < 0) {
5198 log_syntax(unit, LOG_WARNING, filename, line, r,
5199 "Failed to resolve unit specifiers in \"%s\", ignoring: %m", source);
5200 continue;
5201 }
5202
5203 s = sresolved;
5204 if (s[0] == '-') {
5205 ignore_enoent = true;
5206 s++;
5207 }
5208
5209 r = path_simplify_and_warn(s, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
5210 if (r < 0)
5211 continue;
5212
5213 /* Optionally, the destination is specified. */
5214 if (p && p[-1] == ':') {
5215 r = extract_first_word(&p, &destination, ":" WHITESPACE, EXTRACT_UNQUOTE|EXTRACT_DONT_COALESCE_SEPARATORS);
5216 if (r == -ENOMEM)
5217 return log_oom();
5218 if (r < 0) {
5219 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse %s, ignoring: %s", lvalue, rvalue);
5220 return 0;
5221 }
5222 if (r == 0) {
5223 log_syntax(unit, LOG_WARNING, filename, line, 0, "Missing argument after ':', ignoring: %s", s);
5224 continue;
5225 }
5226
5227 r = unit_path_printf(u, destination, &dresolved);
5228 if (r < 0) {
5229 log_syntax(unit, LOG_WARNING, filename, line, r,
5230 "Failed to resolve specifiers in \"%s\", ignoring: %m", destination);
5231 continue;
5232 }
5233
5234 r = path_simplify_and_warn(dresolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
5235 if (r < 0)
5236 continue;
5237
5238 d = dresolved;
5239
5240 /* Optionally, there's also a short option string specified */
5241 if (p && p[-1] == ':') {
5242 _cleanup_free_ char *options = NULL;
5243
5244 r = extract_first_word(&p, &options, NULL, EXTRACT_UNQUOTE);
5245 if (r == -ENOMEM)
5246 return log_oom();
5247 if (r < 0) {
5248 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse %s=, ignoring: %s", lvalue, rvalue);
5249 return 0;
5250 }
5251
5252 if (isempty(options) || streq(options, "rbind"))
5253 rbind = true;
5254 else if (streq(options, "norbind"))
5255 rbind = false;
5256 else {
5257 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid option string, ignoring setting: %s", options);
5258 continue;
5259 }
5260 }
5261 } else
5262 d = s;
5263
5264 r = bind_mount_add(&c->bind_mounts, &c->n_bind_mounts,
5265 &(BindMount) {
5266 .source = s,
5267 .destination = d,
5268 .read_only = !!strstr(lvalue, "ReadOnly"),
5269 .recursive = rbind,
5270 .ignore_enoent = ignore_enoent,
5271 });
5272 if (r < 0)
5273 return log_oom();
5274 }
5275
5276 return 0;
5277 }
5278
5279 int config_parse_mount_images(
5280 const char *unit,
5281 const char *filename,
5282 unsigned line,
5283 const char *section,
5284 unsigned section_line,
5285 const char *lvalue,
5286 int ltype,
5287 const char *rvalue,
5288 void *data,
5289 void *userdata) {
5290
5291 ExecContext *c = ASSERT_PTR(data);
5292 const Unit *u = userdata;
5293 int r;
5294
5295 assert(filename);
5296 assert(lvalue);
5297 assert(rvalue);
5298
5299 if (isempty(rvalue)) {
5300 /* Empty assignment resets the list */
5301 c->mount_images = mount_image_free_many(c->mount_images, &c->n_mount_images);
5302 return 0;
5303 }
5304
5305 for (const char *p = rvalue;;) {
5306 _cleanup_(mount_options_free_allp) MountOptions *options = NULL;
5307 _cleanup_free_ char *first = NULL, *second = NULL, *tuple = NULL;
5308 _cleanup_free_ char *sresolved = NULL, *dresolved = NULL;
5309 const char *q = NULL;
5310 char *s = NULL;
5311 bool permissive = false;
5312
5313 r = extract_first_word(&p, &tuple, NULL, EXTRACT_UNQUOTE|EXTRACT_RETAIN_ESCAPE);
5314 if (r == -ENOMEM)
5315 return log_oom();
5316 if (r < 0) {
5317 log_syntax(unit, LOG_WARNING, filename, line, r,
5318 "Invalid syntax %s=%s, ignoring: %m", lvalue, rvalue);
5319 return 0;
5320 }
5321 if (r == 0)
5322 return 0;
5323
5324 q = tuple;
5325 r = extract_many_words(&q, ":", EXTRACT_CUNESCAPE|EXTRACT_UNESCAPE_SEPARATORS, &first, &second, NULL);
5326 if (r == -ENOMEM)
5327 return log_oom();
5328 if (r < 0) {
5329 log_syntax(unit, LOG_WARNING, filename, line, r,
5330 "Invalid syntax in %s=, ignoring: %s", lvalue, tuple);
5331 return 0;
5332 }
5333 if (r == 0)
5334 continue;
5335
5336 s = first;
5337 if (s[0] == '-') {
5338 permissive = true;
5339 s++;
5340 }
5341
5342 r = unit_path_printf(u, s, &sresolved);
5343 if (r < 0) {
5344 log_syntax(unit, LOG_WARNING, filename, line, r,
5345 "Failed to resolve unit specifiers in \"%s\", ignoring: %m", s);
5346 continue;
5347 }
5348
5349 r = path_simplify_and_warn(sresolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
5350 if (r < 0)
5351 continue;
5352
5353 if (isempty(second)) {
5354 log_syntax(unit, LOG_WARNING, filename, line, 0, "Missing destination in %s, ignoring: %s", lvalue, rvalue);
5355 continue;
5356 }
5357
5358 r = unit_path_printf(u, second, &dresolved);
5359 if (r < 0) {
5360 log_syntax(unit, LOG_WARNING, filename, line, r,
5361 "Failed to resolve specifiers in \"%s\", ignoring: %m", second);
5362 continue;
5363 }
5364
5365 r = path_simplify_and_warn(dresolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
5366 if (r < 0)
5367 continue;
5368
5369 for (;;) {
5370 _cleanup_free_ char *partition = NULL, *mount_options = NULL, *mount_options_resolved = NULL;
5371 MountOptions *o = NULL;
5372 PartitionDesignator partition_designator;
5373
5374 r = extract_many_words(&q, ":", EXTRACT_CUNESCAPE|EXTRACT_UNESCAPE_SEPARATORS, &partition, &mount_options, NULL);
5375 if (r == -ENOMEM)
5376 return log_oom();
5377 if (r < 0) {
5378 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", q);
5379 return 0;
5380 }
5381 if (r == 0)
5382 break;
5383 /* Single set of options, applying to the root partition/single filesystem */
5384 if (r == 1) {
5385 r = unit_full_printf(u, partition, &mount_options_resolved);
5386 if (r < 0) {
5387 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", first);
5388 continue;
5389 }
5390
5391 o = new(MountOptions, 1);
5392 if (!o)
5393 return log_oom();
5394 *o = (MountOptions) {
5395 .partition_designator = PARTITION_ROOT,
5396 .options = TAKE_PTR(mount_options_resolved),
5397 };
5398 LIST_APPEND(mount_options, options, o);
5399
5400 break;
5401 }
5402
5403 partition_designator = partition_designator_from_string(partition);
5404 if (partition_designator < 0) {
5405 log_syntax(unit, LOG_WARNING, filename, line, partition_designator,
5406 "Invalid partition name %s, ignoring", partition);
5407 continue;
5408 }
5409 r = unit_full_printf(u, mount_options, &mount_options_resolved);
5410 if (r < 0) {
5411 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", mount_options);
5412 continue;
5413 }
5414
5415 o = new(MountOptions, 1);
5416 if (!o)
5417 return log_oom();
5418 *o = (MountOptions) {
5419 .partition_designator = partition_designator,
5420 .options = TAKE_PTR(mount_options_resolved),
5421 };
5422 LIST_APPEND(mount_options, options, o);
5423 }
5424
5425 r = mount_image_add(&c->mount_images, &c->n_mount_images,
5426 &(MountImage) {
5427 .source = sresolved,
5428 .destination = dresolved,
5429 .mount_options = options,
5430 .ignore_enoent = permissive,
5431 .type = MOUNT_IMAGE_DISCRETE,
5432 });
5433 if (r < 0)
5434 return log_oom();
5435 }
5436 }
5437
5438 int config_parse_extension_images(
5439 const char *unit,
5440 const char *filename,
5441 unsigned line,
5442 const char *section,
5443 unsigned section_line,
5444 const char *lvalue,
5445 int ltype,
5446 const char *rvalue,
5447 void *data,
5448 void *userdata) {
5449
5450 ExecContext *c = ASSERT_PTR(data);
5451 const Unit *u = userdata;
5452 int r;
5453
5454 assert(filename);
5455 assert(lvalue);
5456 assert(rvalue);
5457
5458 if (isempty(rvalue)) {
5459 /* Empty assignment resets the list */
5460 c->extension_images = mount_image_free_many(c->extension_images, &c->n_extension_images);
5461 return 0;
5462 }
5463
5464 for (const char *p = rvalue;;) {
5465 _cleanup_free_ char *source = NULL, *tuple = NULL, *sresolved = NULL;
5466 _cleanup_(mount_options_free_allp) MountOptions *options = NULL;
5467 bool permissive = false;
5468 const char *q = NULL;
5469 char *s = NULL;
5470
5471 r = extract_first_word(&p, &tuple, NULL, EXTRACT_UNQUOTE|EXTRACT_RETAIN_ESCAPE);
5472 if (r == -ENOMEM)
5473 return log_oom();
5474 if (r < 0) {
5475 log_syntax(unit, LOG_WARNING, filename, line, r,
5476 "Invalid syntax %s=%s, ignoring: %m", lvalue, rvalue);
5477 return 0;
5478 }
5479 if (r == 0)
5480 return 0;
5481
5482 q = tuple;
5483 r = extract_first_word(&q, &source, ":", EXTRACT_CUNESCAPE|EXTRACT_UNESCAPE_SEPARATORS);
5484 if (r == -ENOMEM)
5485 return log_oom();
5486 if (r < 0) {
5487 log_syntax(unit, LOG_WARNING, filename, line, r,
5488 "Invalid syntax in %s=, ignoring: %s", lvalue, tuple);
5489 return 0;
5490 }
5491 if (r == 0)
5492 continue;
5493
5494 s = source;
5495 if (s[0] == '-') {
5496 permissive = true;
5497 s++;
5498 }
5499
5500 r = unit_path_printf(u, s, &sresolved);
5501 if (r < 0) {
5502 log_syntax(unit, LOG_WARNING, filename, line, r,
5503 "Failed to resolve unit specifiers in \"%s\", ignoring: %m", s);
5504 continue;
5505 }
5506
5507 r = path_simplify_and_warn(sresolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
5508 if (r < 0)
5509 continue;
5510
5511 for (;;) {
5512 _cleanup_free_ char *partition = NULL, *mount_options = NULL, *mount_options_resolved = NULL;
5513 MountOptions *o = NULL;
5514 PartitionDesignator partition_designator;
5515
5516 r = extract_many_words(&q, ":", EXTRACT_CUNESCAPE|EXTRACT_UNESCAPE_SEPARATORS, &partition, &mount_options, NULL);
5517 if (r == -ENOMEM)
5518 return log_oom();
5519 if (r < 0) {
5520 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", q);
5521 return 0;
5522 }
5523 if (r == 0)
5524 break;
5525 /* Single set of options, applying to the root partition/single filesystem */
5526 if (r == 1) {
5527 r = unit_full_printf(u, partition, &mount_options_resolved);
5528 if (r < 0) {
5529 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", partition);
5530 continue;
5531 }
5532
5533 o = new(MountOptions, 1);
5534 if (!o)
5535 return log_oom();
5536 *o = (MountOptions) {
5537 .partition_designator = PARTITION_ROOT,
5538 .options = TAKE_PTR(mount_options_resolved),
5539 };
5540 LIST_APPEND(mount_options, options, o);
5541
5542 break;
5543 }
5544
5545 partition_designator = partition_designator_from_string(partition);
5546 if (partition_designator < 0) {
5547 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid partition name %s, ignoring", partition);
5548 continue;
5549 }
5550 r = unit_full_printf(u, mount_options, &mount_options_resolved);
5551 if (r < 0) {
5552 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", mount_options);
5553 continue;
5554 }
5555
5556 o = new(MountOptions, 1);
5557 if (!o)
5558 return log_oom();
5559 *o = (MountOptions) {
5560 .partition_designator = partition_designator,
5561 .options = TAKE_PTR(mount_options_resolved),
5562 };
5563 LIST_APPEND(mount_options, options, o);
5564 }
5565
5566 r = mount_image_add(&c->extension_images, &c->n_extension_images,
5567 &(MountImage) {
5568 .source = sresolved,
5569 .mount_options = options,
5570 .ignore_enoent = permissive,
5571 .type = MOUNT_IMAGE_EXTENSION,
5572 });
5573 if (r < 0)
5574 return log_oom();
5575 }
5576 }
5577
5578 int config_parse_job_timeout_sec(
5579 const char* unit,
5580 const char *filename,
5581 unsigned line,
5582 const char *section,
5583 unsigned section_line,
5584 const char *lvalue,
5585 int ltype,
5586 const char *rvalue,
5587 void *data,
5588 void *userdata) {
5589
5590 Unit *u = ASSERT_PTR(data);
5591 usec_t usec;
5592 int r;
5593
5594 assert(filename);
5595 assert(lvalue);
5596 assert(rvalue);
5597
5598 r = parse_sec_fix_0(rvalue, &usec);
5599 if (r < 0) {
5600 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse JobTimeoutSec= parameter, ignoring: %s", rvalue);
5601 return 0;
5602 }
5603
5604 /* If the user explicitly changed JobTimeoutSec= also change JobRunningTimeoutSec=, for compatibility with old
5605 * versions. If JobRunningTimeoutSec= was explicitly set, avoid this however as whatever the user picked should
5606 * count. */
5607
5608 if (!u->job_running_timeout_set)
5609 u->job_running_timeout = usec;
5610
5611 u->job_timeout = usec;
5612
5613 return 0;
5614 }
5615
5616 int config_parse_job_running_timeout_sec(
5617 const char* unit,
5618 const char *filename,
5619 unsigned line,
5620 const char *section,
5621 unsigned section_line,
5622 const char *lvalue,
5623 int ltype,
5624 const char *rvalue,
5625 void *data,
5626 void *userdata) {
5627
5628 Unit *u = ASSERT_PTR(data);
5629 usec_t usec;
5630 int r;
5631
5632 assert(filename);
5633 assert(lvalue);
5634 assert(rvalue);
5635
5636 r = parse_sec_fix_0(rvalue, &usec);
5637 if (r < 0) {
5638 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse JobRunningTimeoutSec= parameter, ignoring: %s", rvalue);
5639 return 0;
5640 }
5641
5642 u->job_running_timeout = usec;
5643 u->job_running_timeout_set = true;
5644
5645 return 0;
5646 }
5647
5648 int config_parse_emergency_action(
5649 const char* unit,
5650 const char *filename,
5651 unsigned line,
5652 const char *section,
5653 unsigned section_line,
5654 const char *lvalue,
5655 int ltype,
5656 const char *rvalue,
5657 void *data,
5658 void *userdata) {
5659
5660 EmergencyAction *x = ASSERT_PTR(data);
5661 RuntimeScope runtime_scope;
5662 int r;
5663
5664 assert(filename);
5665 assert(lvalue);
5666 assert(rvalue);
5667
5668 /* If we have a unit determine the scope based on it */
5669 if (unit)
5670 runtime_scope = ((Unit*) ASSERT_PTR(userdata))->manager->runtime_scope;
5671 else
5672 runtime_scope = ltype; /* otherwise, assume the scope is passed in via ltype */
5673
5674 r = parse_emergency_action(rvalue, runtime_scope, x);
5675 if (r < 0) {
5676 if (r == -EOPNOTSUPP)
5677 log_syntax(unit, LOG_WARNING, filename, line, r,
5678 "%s= specified as %s mode action, ignoring: %s",
5679 lvalue, runtime_scope_to_string(runtime_scope), rvalue);
5680 else
5681 log_syntax(unit, LOG_WARNING, filename, line, r,
5682 "Failed to parse %s=, ignoring: %s", lvalue, rvalue);
5683 return 0;
5684 }
5685
5686 return 0;
5687 }
5688
5689 int config_parse_pid_file(
5690 const char *unit,
5691 const char *filename,
5692 unsigned line,
5693 const char *section,
5694 unsigned section_line,
5695 const char *lvalue,
5696 int ltype,
5697 const char *rvalue,
5698 void *data,
5699 void *userdata) {
5700
5701 _cleanup_free_ char *k = NULL, *n = NULL;
5702 const Unit *u = ASSERT_PTR(userdata);
5703 char **s = data;
5704 int r;
5705
5706 assert(filename);
5707 assert(lvalue);
5708 assert(rvalue);
5709
5710 if (isempty(rvalue)) {
5711 /* An empty assignment removes already set value. */
5712 *s = mfree(*s);
5713 return 0;
5714 }
5715
5716 r = unit_path_printf(u, rvalue, &k);
5717 if (r < 0) {
5718 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
5719 return 0;
5720 }
5721
5722 /* If this is a relative path make it absolute by prefixing the /run */
5723 n = path_make_absolute(k, u->manager->prefix[EXEC_DIRECTORY_RUNTIME]);
5724 if (!n)
5725 return log_oom();
5726
5727 /* Check that the result is a sensible path */
5728 r = path_simplify_and_warn(n, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
5729 if (r < 0)
5730 return r;
5731
5732 r = patch_var_run(unit, filename, line, lvalue, &n);
5733 if (r < 0)
5734 return r;
5735
5736 free_and_replace(*s, n);
5737 return 0;
5738 }
5739
5740 int config_parse_exit_status(
5741 const char *unit,
5742 const char *filename,
5743 unsigned line,
5744 const char *section,
5745 unsigned section_line,
5746 const char *lvalue,
5747 int ltype,
5748 const char *rvalue,
5749 void *data,
5750 void *userdata) {
5751
5752 int *exit_status = data, r;
5753 uint8_t u;
5754
5755 assert(filename);
5756 assert(lvalue);
5757 assert(rvalue);
5758 assert(exit_status);
5759
5760 if (isempty(rvalue)) {
5761 *exit_status = -1;
5762 return 0;
5763 }
5764
5765 r = safe_atou8(rvalue, &u);
5766 if (r < 0) {
5767 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse exit status '%s', ignoring: %m", rvalue);
5768 return 0;
5769 }
5770
5771 *exit_status = u;
5772 return 0;
5773 }
5774
5775 int config_parse_disable_controllers(
5776 const char *unit,
5777 const char *filename,
5778 unsigned line,
5779 const char *section,
5780 unsigned section_line,
5781 const char *lvalue,
5782 int ltype,
5783 const char *rvalue,
5784 void *data,
5785 void *userdata) {
5786
5787 int r;
5788 CGroupContext *c = data;
5789 CGroupMask disabled_mask;
5790
5791 /* 1. If empty, make all controllers eligible for use again.
5792 * 2. If non-empty, merge all listed controllers, space separated. */
5793
5794 if (isempty(rvalue)) {
5795 c->disable_controllers = 0;
5796 return 0;
5797 }
5798
5799 r = cg_mask_from_string(rvalue, &disabled_mask);
5800 if (r < 0 || disabled_mask <= 0) {
5801 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid cgroup string: %s, ignoring", rvalue);
5802 return 0;
5803 }
5804
5805 c->disable_controllers |= disabled_mask;
5806
5807 return 0;
5808 }
5809
5810 int config_parse_ip_filter_bpf_progs(
5811 const char *unit,
5812 const char *filename,
5813 unsigned line,
5814 const char *section,
5815 unsigned section_line,
5816 const char *lvalue,
5817 int ltype,
5818 const char *rvalue,
5819 void *data,
5820 void *userdata) {
5821
5822 _cleanup_free_ char *resolved = NULL;
5823 const Unit *u = userdata;
5824 char ***paths = ASSERT_PTR(data);
5825 int r;
5826
5827 assert(filename);
5828 assert(lvalue);
5829 assert(rvalue);
5830
5831 if (isempty(rvalue)) {
5832 *paths = strv_free(*paths);
5833 return 0;
5834 }
5835
5836 r = unit_path_printf(u, rvalue, &resolved);
5837 if (r < 0) {
5838 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
5839 return 0;
5840 }
5841
5842 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
5843 if (r < 0)
5844 return 0;
5845
5846 if (strv_contains(*paths, resolved))
5847 return 0;
5848
5849 r = strv_extend(paths, resolved);
5850 if (r < 0)
5851 return log_oom();
5852
5853 r = bpf_firewall_supported();
5854 if (r < 0)
5855 return r;
5856 if (r != BPF_FIREWALL_SUPPORTED_WITH_MULTI) {
5857 static bool warned = false;
5858
5859 log_full(warned ? LOG_DEBUG : LOG_WARNING,
5860 "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"
5861 "Starting this unit will fail! (This warning is only shown for the first loaded unit using IP firewalling.)", filename, line, lvalue, rvalue);
5862
5863 warned = true;
5864 }
5865
5866 return 0;
5867 }
5868
5869 int config_parse_bpf_foreign_program(
5870 const char *unit,
5871 const char *filename,
5872 unsigned line,
5873 const char *section,
5874 unsigned section_line,
5875 const char *lvalue,
5876 int ltype,
5877 const char *rvalue,
5878 void *data,
5879 void *userdata) {
5880 _cleanup_free_ char *resolved = NULL, *word = NULL;
5881 CGroupContext *c = data;
5882 const char *p = ASSERT_PTR(rvalue);
5883 Unit *u = userdata;
5884 int attach_type, r;
5885
5886 assert(filename);
5887 assert(lvalue);
5888
5889 if (isempty(rvalue)) {
5890 while (c->bpf_foreign_programs)
5891 cgroup_context_remove_bpf_foreign_program(c, c->bpf_foreign_programs);
5892
5893 return 0;
5894 }
5895
5896 r = extract_first_word(&p, &word, ":", 0);
5897 if (r == -ENOMEM)
5898 return log_oom();
5899 if (r < 0) {
5900 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse foreign BPF program, ignoring: %s", rvalue);
5901 return 0;
5902 }
5903 if (r == 0 || isempty(p)) {
5904 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid syntax in %s=, ignoring: %s", lvalue, rvalue);
5905 return 0;
5906 }
5907
5908 attach_type = bpf_cgroup_attach_type_from_string(word);
5909 if (attach_type < 0) {
5910 log_syntax(unit, LOG_WARNING, filename, line, 0, "Unknown BPF attach type=%s, ignoring: %s", word, rvalue);
5911 return 0;
5912 }
5913
5914 r = unit_path_printf(u, p, &resolved);
5915 if (r < 0) {
5916 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %s", p, rvalue);
5917 return 0;
5918 }
5919
5920 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
5921 if (r < 0)
5922 return 0;
5923
5924 r = cgroup_add_bpf_foreign_program(c, attach_type, resolved);
5925 if (r < 0)
5926 return log_error_errno(r, "Failed to add foreign BPF program to cgroup context: %m");
5927
5928 return 0;
5929 }
5930
5931 int config_parse_cgroup_socket_bind(
5932 const char *unit,
5933 const char *filename,
5934 unsigned line,
5935 const char *section,
5936 unsigned section_line,
5937 const char *lvalue,
5938 int ltype,
5939 const char *rvalue,
5940 void *data,
5941 void *userdata) {
5942 _cleanup_free_ CGroupSocketBindItem *item = NULL;
5943 CGroupSocketBindItem **head = data;
5944 uint16_t nr_ports, port_min;
5945 int af, ip_protocol, r;
5946
5947 if (isempty(rvalue)) {
5948 cgroup_context_remove_socket_bind(head);
5949 return 0;
5950 }
5951
5952 r = parse_socket_bind_item(rvalue, &af, &ip_protocol, &nr_ports, &port_min);
5953 if (r == -ENOMEM)
5954 return log_oom();
5955 if (r < 0) {
5956 log_syntax(unit, LOG_WARNING, filename, line, r,
5957 "Unable to parse %s= assignment, ignoring: %s", lvalue, rvalue);
5958 return 0;
5959 }
5960
5961 item = new(CGroupSocketBindItem, 1);
5962 if (!item)
5963 return log_oom();
5964 *item = (CGroupSocketBindItem) {
5965 .address_family = af,
5966 .ip_protocol = ip_protocol,
5967 .nr_ports = nr_ports,
5968 .port_min = port_min,
5969 };
5970
5971 LIST_PREPEND(socket_bind_items, *head, TAKE_PTR(item));
5972
5973 return 0;
5974 }
5975
5976 int config_parse_restrict_network_interfaces(
5977 const char *unit,
5978 const char *filename,
5979 unsigned line,
5980 const char *section,
5981 unsigned section_line,
5982 const char *lvalue,
5983 int ltype,
5984 const char *rvalue,
5985 void *data,
5986 void *userdata) {
5987 CGroupContext *c = ASSERT_PTR(data);
5988 bool is_allow_rule = true;
5989 int r;
5990
5991 assert(filename);
5992 assert(lvalue);
5993 assert(rvalue);
5994
5995 if (isempty(rvalue)) {
5996 /* Empty assignment resets the list */
5997 c->restrict_network_interfaces = set_free(c->restrict_network_interfaces);
5998 return 0;
5999 }
6000
6001 if (rvalue[0] == '~') {
6002 is_allow_rule = false;
6003 rvalue++;
6004 }
6005
6006 if (set_isempty(c->restrict_network_interfaces))
6007 /* Only initialize this when creating the set */
6008 c->restrict_network_interfaces_is_allow_list = is_allow_rule;
6009
6010 for (const char *p = rvalue;;) {
6011 _cleanup_free_ char *word = NULL;
6012
6013 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
6014 if (r == 0)
6015 break;
6016 if (r == -ENOMEM)
6017 return log_oom();
6018 if (r < 0) {
6019 log_syntax(unit, LOG_WARNING, filename, line, r,
6020 "Trailing garbage in %s, ignoring: %s", lvalue, rvalue);
6021 break;
6022 }
6023
6024 if (!ifname_valid(word)) {
6025 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid interface name, ignoring: %s", word);
6026 continue;
6027 }
6028
6029 if (c->restrict_network_interfaces_is_allow_list != is_allow_rule)
6030 free(set_remove(c->restrict_network_interfaces, word));
6031 else {
6032 r = set_put_strdup(&c->restrict_network_interfaces, word);
6033 if (r < 0)
6034 return log_oom();
6035 }
6036 }
6037
6038 return 0;
6039 }
6040
6041 static int merge_by_names(Unit **u, Set *names, const char *id) {
6042 char *k;
6043 int r;
6044
6045 assert(u);
6046 assert(*u);
6047
6048 /* Let's try to add in all names that are aliases of this unit */
6049 while ((k = set_steal_first(names))) {
6050 _cleanup_free_ _unused_ char *free_k = k;
6051
6052 /* First try to merge in the other name into our unit */
6053 r = unit_merge_by_name(*u, k);
6054 if (r < 0) {
6055 Unit *other;
6056
6057 /* Hmm, we couldn't merge the other unit into ours? Then let's try it the other way
6058 * round. */
6059
6060 other = manager_get_unit((*u)->manager, k);
6061 if (!other)
6062 return r; /* return previous failure */
6063
6064 r = unit_merge(other, *u);
6065 if (r < 0)
6066 return r;
6067
6068 *u = other;
6069 return merge_by_names(u, names, NULL);
6070 }
6071
6072 if (streq_ptr(id, k))
6073 unit_choose_id(*u, id);
6074 }
6075
6076 return 0;
6077 }
6078
6079 int unit_load_fragment(Unit *u) {
6080 const char *fragment;
6081 _cleanup_set_free_free_ Set *names = NULL;
6082 int r;
6083
6084 assert(u);
6085 assert(u->load_state == UNIT_STUB);
6086 assert(u->id);
6087
6088 if (u->transient) {
6089 u->access_selinux_context = mfree(u->access_selinux_context);
6090 u->load_state = UNIT_LOADED;
6091 return 0;
6092 }
6093
6094 /* Possibly rebuild the fragment map to catch new units */
6095 r = unit_file_build_name_map(&u->manager->lookup_paths,
6096 &u->manager->unit_cache_timestamp_hash,
6097 &u->manager->unit_id_map,
6098 &u->manager->unit_name_map,
6099 &u->manager->unit_path_cache);
6100 if (r < 0)
6101 return log_error_errno(r, "Failed to rebuild name map: %m");
6102
6103 r = unit_file_find_fragment(u->manager->unit_id_map,
6104 u->manager->unit_name_map,
6105 u->id,
6106 &fragment,
6107 &names);
6108 if (r < 0 && r != -ENOENT)
6109 return r;
6110
6111 if (fragment) {
6112 /* Open the file, check if this is a mask, otherwise read. */
6113 _cleanup_fclose_ FILE *f = NULL;
6114 struct stat st;
6115
6116 /* Try to open the file name. A symlink is OK, for example for linked files or masks. We
6117 * expect that all symlinks within the lookup paths have been already resolved, but we don't
6118 * verify this here. */
6119 f = fopen(fragment, "re");
6120 if (!f)
6121 return log_unit_notice_errno(u, errno, "Failed to open %s: %m", fragment);
6122
6123 if (fstat(fileno(f), &st) < 0)
6124 return -errno;
6125
6126 r = free_and_strdup(&u->fragment_path, fragment);
6127 if (r < 0)
6128 return r;
6129
6130 if (null_or_empty(&st)) {
6131 /* Unit file is masked */
6132
6133 u->load_state = u->perpetual ? UNIT_LOADED : UNIT_MASKED; /* don't allow perpetual units to ever be masked */
6134 u->fragment_mtime = 0;
6135 u->access_selinux_context = mfree(u->access_selinux_context);
6136 } else {
6137 #if HAVE_SELINUX
6138 if (mac_selinux_use()) {
6139 _cleanup_freecon_ char *selcon = NULL;
6140
6141 /* Cache the SELinux context of the unit file here. We'll make use of when checking access permissions to loaded units */
6142 r = fgetfilecon_raw(fileno(f), &selcon);
6143 if (r < 0)
6144 log_unit_warning_errno(u, r, "Failed to read SELinux context of '%s', ignoring: %m", fragment);
6145
6146 r = free_and_strdup(&u->access_selinux_context, selcon);
6147 if (r < 0)
6148 return r;
6149 } else
6150 #endif
6151 u->access_selinux_context = mfree(u->access_selinux_context);
6152
6153 u->load_state = UNIT_LOADED;
6154 u->fragment_mtime = timespec_load(&st.st_mtim);
6155
6156 /* Now, parse the file contents */
6157 r = config_parse(u->id, fragment, f,
6158 UNIT_VTABLE(u)->sections,
6159 config_item_perf_lookup, load_fragment_gperf_lookup,
6160 0,
6161 u,
6162 NULL);
6163 if (r == -ENOEXEC)
6164 log_unit_notice_errno(u, r, "Unit configuration has fatal error, unit will not be started.");
6165 if (r < 0)
6166 return r;
6167 }
6168 }
6169
6170 /* Call merge_by_names with the name derived from the fragment path as the preferred name.
6171 *
6172 * We do the merge dance here because for some unit types, the unit might have aliases which are not
6173 * declared in the file system. In particular, this is true (and frequent) for device and swap units.
6174 */
6175 const char *id = u->id;
6176 _cleanup_free_ char *filename = NULL, *free_id = NULL;
6177
6178 if (fragment) {
6179 r = path_extract_filename(fragment, &filename);
6180 if (r < 0)
6181 return log_debug_errno(r, "Failed to extract filename from fragment '%s': %m", fragment);
6182 id = filename;
6183
6184 if (unit_name_is_valid(id, UNIT_NAME_TEMPLATE)) {
6185 assert(u->instance); /* If we're not trying to use a template for non-instanced unit,
6186 * this must be set. */
6187
6188 r = unit_name_replace_instance(id, u->instance, &free_id);
6189 if (r < 0)
6190 return log_debug_errno(r, "Failed to build id (%s + %s): %m", id, u->instance);
6191 id = free_id;
6192 }
6193 }
6194
6195 Unit *merged = u;
6196 r = merge_by_names(&merged, names, id);
6197 if (r < 0)
6198 return r;
6199
6200 if (merged != u)
6201 u->load_state = UNIT_MERGED;
6202
6203 return 0;
6204 }
6205
6206 void unit_dump_config_items(FILE *f) {
6207 static const struct {
6208 const ConfigParserCallback callback;
6209 const char *rvalue;
6210 } table[] = {
6211 { config_parse_warn_compat, "NOTSUPPORTED" },
6212 { config_parse_int, "INTEGER" },
6213 { config_parse_unsigned, "UNSIGNED" },
6214 { config_parse_iec_size, "SIZE" },
6215 { config_parse_iec_uint64, "SIZE" },
6216 { config_parse_si_uint64, "SIZE" },
6217 { config_parse_bool, "BOOLEAN" },
6218 { config_parse_string, "STRING" },
6219 { config_parse_path, "PATH" },
6220 { config_parse_unit_path_printf, "PATH" },
6221 { config_parse_colon_separated_paths, "PATH" },
6222 { config_parse_strv, "STRING [...]" },
6223 { config_parse_exec_nice, "NICE" },
6224 { config_parse_exec_oom_score_adjust, "OOMSCOREADJUST" },
6225 { config_parse_exec_io_class, "IOCLASS" },
6226 { config_parse_exec_io_priority, "IOPRIORITY" },
6227 { config_parse_exec_cpu_sched_policy, "CPUSCHEDPOLICY" },
6228 { config_parse_exec_cpu_sched_prio, "CPUSCHEDPRIO" },
6229 { config_parse_exec_cpu_affinity, "CPUAFFINITY" },
6230 { config_parse_mode, "MODE" },
6231 { config_parse_unit_env_file, "FILE" },
6232 { config_parse_exec_output, "OUTPUT" },
6233 { config_parse_exec_input, "INPUT" },
6234 { config_parse_log_facility, "FACILITY" },
6235 { config_parse_log_level, "LEVEL" },
6236 { config_parse_exec_secure_bits, "SECUREBITS" },
6237 { config_parse_capability_set, "BOUNDINGSET" },
6238 { config_parse_rlimit, "LIMIT" },
6239 { config_parse_unit_deps, "UNIT [...]" },
6240 { config_parse_exec, "PATH [ARGUMENT [...]]" },
6241 { config_parse_service_type, "SERVICETYPE" },
6242 { config_parse_service_exit_type, "SERVICEEXITTYPE" },
6243 { config_parse_service_restart, "SERVICERESTART" },
6244 { config_parse_service_timeout_failure_mode, "TIMEOUTMODE" },
6245 { config_parse_kill_mode, "KILLMODE" },
6246 { config_parse_signal, "SIGNAL" },
6247 { config_parse_socket_listen, "SOCKET [...]" },
6248 { config_parse_socket_bind, "SOCKETBIND" },
6249 { config_parse_socket_bindtodevice, "NETWORKINTERFACE" },
6250 { config_parse_sec, "SECONDS" },
6251 { config_parse_nsec, "NANOSECONDS" },
6252 { config_parse_namespace_path_strv, "PATH [...]" },
6253 { config_parse_bind_paths, "PATH[:PATH[:OPTIONS]] [...]" },
6254 { config_parse_unit_requires_mounts_for,
6255 "PATH [...]" },
6256 { config_parse_exec_mount_propagation_flag,
6257 "MOUNTFLAG" },
6258 { config_parse_unit_string_printf, "STRING" },
6259 { config_parse_trigger_unit, "UNIT" },
6260 { config_parse_timer, "TIMER" },
6261 { config_parse_path_spec, "PATH" },
6262 { config_parse_notify_access, "ACCESS" },
6263 { config_parse_ip_tos, "TOS" },
6264 { config_parse_unit_condition_path, "CONDITION" },
6265 { config_parse_unit_condition_string, "CONDITION" },
6266 { config_parse_unit_slice, "SLICE" },
6267 { config_parse_documentation, "URL" },
6268 { config_parse_service_timeout, "SECONDS" },
6269 { config_parse_emergency_action, "ACTION" },
6270 { config_parse_set_status, "STATUS" },
6271 { config_parse_service_sockets, "SOCKETS" },
6272 { config_parse_environ, "ENVIRON" },
6273 #if HAVE_SECCOMP
6274 { config_parse_syscall_filter, "SYSCALLS" },
6275 { config_parse_syscall_archs, "ARCHS" },
6276 { config_parse_syscall_errno, "ERRNO" },
6277 { config_parse_syscall_log, "SYSCALLS" },
6278 { config_parse_address_families, "FAMILIES" },
6279 { config_parse_restrict_namespaces, "NAMESPACES" },
6280 #endif
6281 { config_parse_restrict_filesystems, "FILESYSTEMS" },
6282 { config_parse_cpu_shares, "SHARES" },
6283 { config_parse_cg_weight, "WEIGHT" },
6284 { config_parse_cg_cpu_weight, "CPUWEIGHT" },
6285 { config_parse_memory_limit, "LIMIT" },
6286 { config_parse_device_allow, "DEVICE" },
6287 { config_parse_device_policy, "POLICY" },
6288 { config_parse_io_limit, "LIMIT" },
6289 { config_parse_io_device_weight, "DEVICEWEIGHT" },
6290 { config_parse_io_device_latency, "DEVICELATENCY" },
6291 { config_parse_blockio_bandwidth, "BANDWIDTH" },
6292 { config_parse_blockio_weight, "WEIGHT" },
6293 { config_parse_blockio_device_weight, "DEVICEWEIGHT" },
6294 { config_parse_long, "LONG" },
6295 { config_parse_socket_service, "SERVICE" },
6296 #if HAVE_SELINUX
6297 { config_parse_exec_selinux_context, "LABEL" },
6298 #endif
6299 { config_parse_job_mode, "MODE" },
6300 { config_parse_job_mode_isolate, "BOOLEAN" },
6301 { config_parse_personality, "PERSONALITY" },
6302 { config_parse_log_filter_patterns, "REGEX" },
6303 };
6304
6305 const char *prev = NULL;
6306
6307 assert(f);
6308
6309 NULSTR_FOREACH(i, load_fragment_gperf_nulstr) {
6310 const char *rvalue = "OTHER", *lvalue;
6311 const ConfigPerfItem *p;
6312 const char *dot;
6313
6314 assert_se(p = load_fragment_gperf_lookup(i, strlen(i)));
6315
6316 /* Hide legacy settings */
6317 if (p->parse == config_parse_warn_compat &&
6318 p->ltype == DISABLED_LEGACY)
6319 continue;
6320
6321 for (size_t j = 0; j < ELEMENTSOF(table); j++)
6322 if (p->parse == table[j].callback) {
6323 rvalue = table[j].rvalue;
6324 break;
6325 }
6326
6327 dot = strchr(i, '.');
6328 lvalue = dot ? dot + 1 : i;
6329
6330 if (dot) {
6331 size_t prefix_len = dot - i;
6332
6333 if (!prev || !strneq(prev, i, prefix_len+1)) {
6334 if (prev)
6335 fputc('\n', f);
6336
6337 fprintf(f, "[%.*s]\n", (int) prefix_len, i);
6338 }
6339 }
6340
6341 fprintf(f, "%s=%s\n", lvalue, rvalue);
6342 prev = i;
6343 }
6344 }
6345
6346 int config_parse_cpu_affinity2(
6347 const char *unit,
6348 const char *filename,
6349 unsigned line,
6350 const char *section,
6351 unsigned section_line,
6352 const char *lvalue,
6353 int ltype,
6354 const char *rvalue,
6355 void *data,
6356 void *userdata) {
6357
6358 CPUSet *affinity = ASSERT_PTR(data);
6359
6360 (void) parse_cpu_set_extend(rvalue, affinity, true, unit, filename, line, lvalue);
6361
6362 return 0;
6363 }
6364
6365 int config_parse_show_status(
6366 const char* unit,
6367 const char *filename,
6368 unsigned line,
6369 const char *section,
6370 unsigned section_line,
6371 const char *lvalue,
6372 int ltype,
6373 const char *rvalue,
6374 void *data,
6375 void *userdata) {
6376
6377 int k;
6378 ShowStatus *b = ASSERT_PTR(data);
6379
6380 assert(filename);
6381 assert(lvalue);
6382 assert(rvalue);
6383
6384 k = parse_show_status(rvalue, b);
6385 if (k < 0)
6386 log_syntax(unit, LOG_WARNING, filename, line, k, "Failed to parse show status setting, ignoring: %s", rvalue);
6387
6388 return 0;
6389 }
6390
6391 int config_parse_output_restricted(
6392 const char* unit,
6393 const char *filename,
6394 unsigned line,
6395 const char *section,
6396 unsigned section_line,
6397 const char *lvalue,
6398 int ltype,
6399 const char *rvalue,
6400 void *data,
6401 void *userdata) {
6402
6403 ExecOutput t, *eo = ASSERT_PTR(data);
6404 bool obsolete = false;
6405
6406 assert(filename);
6407 assert(lvalue);
6408 assert(rvalue);
6409
6410 if (streq(rvalue, "syslog")) {
6411 t = EXEC_OUTPUT_JOURNAL;
6412 obsolete = true;
6413 } else if (streq(rvalue, "syslog+console")) {
6414 t = EXEC_OUTPUT_JOURNAL_AND_CONSOLE;
6415 obsolete = true;
6416 } else {
6417 t = exec_output_from_string(rvalue);
6418 if (t < 0) {
6419 log_syntax(unit, LOG_WARNING, filename, line, t, "Failed to parse output type, ignoring: %s", rvalue);
6420 return 0;
6421 }
6422
6423 if (IN_SET(t, EXEC_OUTPUT_SOCKET, EXEC_OUTPUT_NAMED_FD, EXEC_OUTPUT_FILE, EXEC_OUTPUT_FILE_APPEND, EXEC_OUTPUT_FILE_TRUNCATE)) {
6424 log_syntax(unit, LOG_WARNING, filename, line, 0, "Standard output types socket, fd:, file:, append:, truncate: are not supported as defaults, ignoring: %s", rvalue);
6425 return 0;
6426 }
6427 }
6428
6429 if (obsolete)
6430 log_syntax(unit, LOG_NOTICE, filename, line, 0,
6431 "Standard output type %s is obsolete, automatically updating to %s. Please update your configuration.",
6432 rvalue, exec_output_to_string(t));
6433
6434 *eo = t;
6435 return 0;
6436 }
6437
6438 int config_parse_crash_chvt(
6439 const char* unit,
6440 const char *filename,
6441 unsigned line,
6442 const char *section,
6443 unsigned section_line,
6444 const char *lvalue,
6445 int ltype,
6446 const char *rvalue,
6447 void *data,
6448 void *userdata) {
6449
6450 int r;
6451
6452 assert(filename);
6453 assert(lvalue);
6454 assert(rvalue);
6455 assert(data);
6456
6457 r = parse_crash_chvt(rvalue, data);
6458 if (r < 0)
6459 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse CrashChangeVT= setting, ignoring: %s", rvalue);
6460
6461 return 0;
6462 }
6463
6464 int config_parse_swap_priority(
6465 const char *unit,
6466 const char *filename,
6467 unsigned line,
6468 const char *section,
6469 unsigned section_line,
6470 const char *lvalue,
6471 int ltype,
6472 const char *rvalue,
6473 void *data,
6474 void *userdata) {
6475
6476 Swap *s = ASSERT_PTR(userdata);
6477 int r, priority;
6478
6479 assert(filename);
6480 assert(lvalue);
6481 assert(rvalue);
6482 assert(data);
6483
6484 if (isempty(rvalue)) {
6485 s->parameters_fragment.priority = -1;
6486 s->parameters_fragment.priority_set = false;
6487 return 0;
6488 }
6489
6490 r = safe_atoi(rvalue, &priority);
6491 if (r < 0) {
6492 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid swap priority '%s', ignoring.", rvalue);
6493 return 0;
6494 }
6495
6496 if (priority < -1) {
6497 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);
6498 return 0;
6499 }
6500
6501 if (priority > 32767) {
6502 log_syntax(unit, LOG_WARNING, filename, line, 0, "Swap priority out of range, ignoring: %s", rvalue);
6503 return 0;
6504 }
6505
6506 s->parameters_fragment.priority = priority;
6507 s->parameters_fragment.priority_set = true;
6508 return 0;
6509 }
6510
6511 int config_parse_watchdog_sec(
6512 const char *unit,
6513 const char *filename,
6514 unsigned line,
6515 const char *section,
6516 unsigned section_line,
6517 const char *lvalue,
6518 int ltype,
6519 const char *rvalue,
6520 void *data,
6521 void *userdata) {
6522
6523 usec_t *usec = data;
6524
6525 assert(filename);
6526 assert(lvalue);
6527 assert(rvalue);
6528
6529 /* This is called for {Runtime,Reboot,KExec}WatchdogSec= where "default" maps to
6530 * USEC_INFINITY internally. */
6531
6532 if (streq(rvalue, "default"))
6533 *usec = USEC_INFINITY;
6534 else if (streq(rvalue, "off"))
6535 *usec = 0;
6536 else
6537 return config_parse_sec(unit, filename, line, section, section_line, lvalue, ltype, rvalue, data, userdata);
6538
6539 return 0;
6540 }
6541
6542 int config_parse_tty_size(
6543 const char *unit,
6544 const char *filename,
6545 unsigned line,
6546 const char *section,
6547 unsigned section_line,
6548 const char *lvalue,
6549 int ltype,
6550 const char *rvalue,
6551 void *data,
6552 void *userdata) {
6553
6554 unsigned *sz = data;
6555
6556 assert(filename);
6557 assert(lvalue);
6558 assert(rvalue);
6559
6560 if (isempty(rvalue)) {
6561 *sz = UINT_MAX;
6562 return 0;
6563 }
6564
6565 return config_parse_unsigned(unit, filename, line, section, section_line, lvalue, ltype, rvalue, data, userdata);
6566 }
6567
6568 int config_parse_log_filter_patterns(
6569 const char *unit,
6570 const char *filename,
6571 unsigned line,
6572 const char *section,
6573 unsigned section_line,
6574 const char *lvalue,
6575 int ltype,
6576 const char *rvalue,
6577 void *data,
6578 void *userdata) {
6579
6580 ExecContext *c = ASSERT_PTR(data);
6581 const char *pattern = ASSERT_PTR(rvalue);
6582 bool is_allowlist = true;
6583 int r;
6584
6585 assert(filename);
6586 assert(lvalue);
6587
6588 if (isempty(pattern)) {
6589 /* Empty assignment resets the lists. */
6590 c->log_filter_allowed_patterns = set_free(c->log_filter_allowed_patterns);
6591 c->log_filter_denied_patterns = set_free(c->log_filter_denied_patterns);
6592 return 0;
6593 }
6594
6595 if (pattern[0] == '~') {
6596 is_allowlist = false;
6597 pattern++;
6598 if (isempty(pattern))
6599 /* LogFilterPatterns=~ is not considered a valid pattern. */
6600 return log_syntax(unit, LOG_WARNING, filename, line, 0,
6601 "Regex pattern invalid, ignoring: %s=%s", lvalue, rvalue);
6602 }
6603
6604 if (pattern_compile_and_log(pattern, 0, NULL) < 0)
6605 return 0;
6606
6607 r = set_put_strdup(is_allowlist ? &c->log_filter_allowed_patterns : &c->log_filter_denied_patterns,
6608 pattern);
6609 if (r < 0) {
6610 log_syntax(unit, LOG_WARNING, filename, line, r,
6611 "Failed to store log filtering pattern, ignoring: %s=%s", lvalue, rvalue);
6612 return 0;
6613 }
6614
6615 return 0;
6616 }
6617
6618 int config_parse_open_file(
6619 const char *unit,
6620 const char *filename,
6621 unsigned line,
6622 const char *section,
6623 unsigned section_line,
6624 const char *lvalue,
6625 int ltype,
6626 const char *rvalue,
6627 void *data,
6628 void *userdata) {
6629
6630 _cleanup_(open_file_freep) OpenFile *of = NULL;
6631 OpenFile **head = ASSERT_PTR(data);
6632 int r;
6633
6634 assert(filename);
6635 assert(lvalue);
6636 assert(rvalue);
6637
6638 if (isempty(rvalue)) {
6639 open_file_free_many(head);
6640 return 0;
6641 }
6642
6643 r = open_file_parse(rvalue, &of);
6644 if (r < 0) {
6645 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse OpenFile= setting, ignoring: %s", rvalue);
6646 return 0;
6647 }
6648
6649 LIST_APPEND(open_files, *head, TAKE_PTR(of));
6650
6651 return 0;
6652 }