Debian Codegraph @

src/core/load-fragment.c

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