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