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