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