]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/load-fragment.c
Merge pull request #10357 from poettering/import-fs
[thirdparty/systemd.git] / src / core / load-fragment.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
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 <string.h>
15 #include <sys/resource.h>
16 #include <sys/stat.h>
17
18 #include "af-list.h"
19 #include "alloc-util.h"
20 #include "all-units.h"
21 #include "bus-error.h"
22 #include "bus-internal.h"
23 #include "bus-util.h"
24 #include "cap-list.h"
25 #include "capability-util.h"
26 #include "cgroup.h"
27 #include "conf-parser.h"
28 #include "cpu-set-util.h"
29 #include "env-util.h"
30 #include "errno-list.h"
31 #include "escape.h"
32 #include "fd-util.h"
33 #include "fs-util.h"
34 #include "hexdecoct.h"
35 #include "io-util.h"
36 #include "ioprio.h"
37 #include "journal-util.h"
38 #include "load-fragment.h"
39 #include "log.h"
40 #include "missing.h"
41 #include "mount-util.h"
42 #include "parse-util.h"
43 #include "path-util.h"
44 #include "process-util.h"
45 #if HAVE_SECCOMP
46 #include "seccomp-util.h"
47 #endif
48 #include "securebits.h"
49 #include "securebits-util.h"
50 #include "signal-util.h"
51 #include "socket-protocol-list.h"
52 #include "stat-util.h"
53 #include "string-util.h"
54 #include "strv.h"
55 #include "unit-name.h"
56 #include "unit-printf.h"
57 #include "user-util.h"
58 #include "web-util.h"
59
60 static int supported_socket_protocol_from_string(const char *s) {
61 int r;
62
63 if (isempty(s))
64 return IPPROTO_IP;
65
66 r = socket_protocol_from_name(s);
67 if (r < 0)
68 return r;
69 if (!IN_SET(r, IPPROTO_UDPLITE, IPPROTO_SCTP))
70 return -EPROTONOSUPPORT;
71
72 return r;
73 }
74
75 DEFINE_CONFIG_PARSE(config_parse_socket_protocol, supported_socket_protocol_from_string, "Failed to parse socket protocol");
76 DEFINE_CONFIG_PARSE(config_parse_exec_secure_bits, secure_bits_from_string, "Failed to parse secure bits");
77 DEFINE_CONFIG_PARSE_ENUM(config_parse_collect_mode, collect_mode, CollectMode, "Failed to parse garbage collection mode");
78 DEFINE_CONFIG_PARSE_ENUM(config_parse_device_policy, cgroup_device_policy, CGroupDevicePolicy, "Failed to parse device policy");
79 DEFINE_CONFIG_PARSE_ENUM(config_parse_exec_keyring_mode, exec_keyring_mode, ExecKeyringMode, "Failed to parse keyring mode");
80 DEFINE_CONFIG_PARSE_ENUM(config_parse_exec_utmp_mode, exec_utmp_mode, ExecUtmpMode, "Failed to parse utmp mode");
81 DEFINE_CONFIG_PARSE_ENUM(config_parse_job_mode, job_mode, JobMode, "Failed to parse job mode");
82 DEFINE_CONFIG_PARSE_ENUM(config_parse_kill_mode, kill_mode, KillMode, "Failed to parse kill mode");
83 DEFINE_CONFIG_PARSE_ENUM(config_parse_notify_access, notify_access, NotifyAccess, "Failed to parse notify access specifier");
84 DEFINE_CONFIG_PARSE_ENUM(config_parse_protect_home, protect_home, ProtectHome, "Failed to parse protect home value");
85 DEFINE_CONFIG_PARSE_ENUM(config_parse_protect_system, protect_system, ProtectSystem, "Failed to parse protect system value");
86 DEFINE_CONFIG_PARSE_ENUM(config_parse_runtime_preserve_mode, exec_preserve_mode, ExecPreserveMode, "Failed to parse runtime directory preserve mode");
87 DEFINE_CONFIG_PARSE_ENUM(config_parse_service_type, service_type, ServiceType, "Failed to parse service type");
88 DEFINE_CONFIG_PARSE_ENUM(config_parse_service_restart, service_restart, ServiceRestart, "Failed to parse service restart specifier");
89 DEFINE_CONFIG_PARSE_ENUM(config_parse_socket_bind, socket_address_bind_ipv6_only_or_bool, SocketAddressBindIPv6Only, "Failed to parse bind IPv6 only value");
90 DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT(config_parse_ip_tos, ip_tos, int, -1, "Failed to parse IP TOS value");
91 DEFINE_CONFIG_PARSE_PTR(config_parse_blockio_weight, cg_blkio_weight_parse, uint64_t, "Invalid block IO weight");
92 DEFINE_CONFIG_PARSE_PTR(config_parse_cg_weight, cg_weight_parse, uint64_t, "Invalid weight");
93 DEFINE_CONFIG_PARSE_PTR(config_parse_cpu_shares, cg_cpu_shares_parse, uint64_t, "Invalid CPU shares");
94 DEFINE_CONFIG_PARSE_PTR(config_parse_exec_mount_flags, mount_propagation_flags_from_string, unsigned long, "Failed to parse mount flag");
95
96 int config_parse_unit_deps(
97 const char *unit,
98 const char *filename,
99 unsigned line,
100 const char *section,
101 unsigned section_line,
102 const char *lvalue,
103 int ltype,
104 const char *rvalue,
105 void *data,
106 void *userdata) {
107
108 UnitDependency d = ltype;
109 Unit *u = userdata;
110 const char *p;
111
112 assert(filename);
113 assert(lvalue);
114 assert(rvalue);
115
116 p = rvalue;
117 for (;;) {
118 _cleanup_free_ char *word = NULL, *k = NULL;
119 int r;
120
121 r = extract_first_word(&p, &word, NULL, EXTRACT_RETAIN_ESCAPE);
122 if (r == 0)
123 break;
124 if (r == -ENOMEM)
125 return log_oom();
126 if (r < 0) {
127 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
128 break;
129 }
130
131 r = unit_name_printf(u, word, &k);
132 if (r < 0) {
133 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", word);
134 continue;
135 }
136
137 r = unit_add_dependency_by_name(u, d, k, true, UNIT_DEPENDENCY_FILE);
138 if (r < 0)
139 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to add dependency on %s, ignoring: %m", k);
140 }
141
142 return 0;
143 }
144
145 int config_parse_obsolete_unit_deps(
146 const char *unit,
147 const char *filename,
148 unsigned line,
149 const char *section,
150 unsigned section_line,
151 const char *lvalue,
152 int ltype,
153 const char *rvalue,
154 void *data,
155 void *userdata) {
156
157 log_syntax(unit, LOG_WARNING, filename, line, 0,
158 "Unit dependency type %s= is obsolete, replacing by %s=, please update your unit file", lvalue, unit_dependency_to_string(ltype));
159
160 return config_parse_unit_deps(unit, filename, line, section, section_line, lvalue, ltype, rvalue, data, userdata);
161 }
162
163 int config_parse_unit_string_printf(
164 const char *unit,
165 const char *filename,
166 unsigned line,
167 const char *section,
168 unsigned section_line,
169 const char *lvalue,
170 int ltype,
171 const char *rvalue,
172 void *data,
173 void *userdata) {
174
175 _cleanup_free_ char *k = NULL;
176 Unit *u = userdata;
177 int r;
178
179 assert(filename);
180 assert(lvalue);
181 assert(rvalue);
182 assert(u);
183
184 r = unit_full_printf(u, rvalue, &k);
185 if (r < 0) {
186 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
187 return 0;
188 }
189
190 return config_parse_string(unit, filename, line, section, section_line, lvalue, ltype, k, data, userdata);
191 }
192
193 int config_parse_unit_strv_printf(
194 const char *unit,
195 const char *filename,
196 unsigned line,
197 const char *section,
198 unsigned section_line,
199 const char *lvalue,
200 int ltype,
201 const char *rvalue,
202 void *data,
203 void *userdata) {
204
205 Unit *u = userdata;
206 _cleanup_free_ char *k = NULL;
207 int r;
208
209 assert(filename);
210 assert(lvalue);
211 assert(rvalue);
212 assert(u);
213
214 r = unit_full_printf(u, rvalue, &k);
215 if (r < 0) {
216 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
217 return 0;
218 }
219
220 return config_parse_strv(unit, filename, line, section, section_line, lvalue, ltype, k, data, userdata);
221 }
222
223 int config_parse_unit_path_printf(
224 const char *unit,
225 const char *filename,
226 unsigned line,
227 const char *section,
228 unsigned section_line,
229 const char *lvalue,
230 int ltype,
231 const char *rvalue,
232 void *data,
233 void *userdata) {
234
235 _cleanup_free_ char *k = NULL;
236 Unit *u = userdata;
237 int r;
238 bool fatal = ltype;
239
240 assert(filename);
241 assert(lvalue);
242 assert(rvalue);
243 assert(u);
244
245 /* Let's not bother with anything that is too long */
246 if (strlen(rvalue) >= PATH_MAX) {
247 log_syntax(unit, LOG_ERR, filename, line, 0,
248 "%s value too long%s.",
249 lvalue, fatal ? "" : ", ignoring");
250 return fatal ? -ENAMETOOLONG : 0;
251 }
252
253 r = unit_full_printf(u, rvalue, &k);
254 if (r < 0) {
255 log_syntax(unit, LOG_ERR, filename, line, r,
256 "Failed to resolve unit specifiers in '%s'%s: %m",
257 rvalue, fatal ? "" : ", ignoring");
258 return fatal ? -ENOEXEC : 0;
259 }
260
261 return config_parse_path(unit, filename, line, section, section_line, lvalue, ltype, k, data, userdata);
262 }
263
264 int config_parse_unit_path_strv_printf(
265 const char *unit,
266 const char *filename,
267 unsigned line,
268 const char *section,
269 unsigned section_line,
270 const char *lvalue,
271 int ltype,
272 const char *rvalue,
273 void *data,
274 void *userdata) {
275
276 char ***x = data;
277 Unit *u = userdata;
278 int r;
279 const char *p;
280
281 assert(filename);
282 assert(lvalue);
283 assert(rvalue);
284 assert(u);
285
286 if (isempty(rvalue)) {
287 *x = strv_free(*x);
288 return 0;
289 }
290
291 for (p = rvalue;;) {
292 _cleanup_free_ char *word = NULL, *k = NULL;
293
294 r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES);
295 if (r == 0)
296 return 0;
297 if (r == -ENOMEM)
298 return log_oom();
299 if (r < 0) {
300 log_syntax(unit, LOG_WARNING, filename, line, r,
301 "Invalid syntax, ignoring: %s", rvalue);
302 return 0;
303 }
304
305 r = unit_full_printf(u, word, &k);
306 if (r < 0) {
307 log_syntax(unit, LOG_ERR, filename, line, r,
308 "Failed to resolve unit specifiers in '%s', ignoring: %m", word);
309 return 0;
310 }
311
312 r = path_simplify_and_warn(k, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
313 if (r < 0)
314 return 0;
315
316 r = strv_push(x, k);
317 if (r < 0)
318 return log_oom();
319 k = NULL;
320 }
321 }
322
323 int config_parse_socket_listen(const char *unit,
324 const char *filename,
325 unsigned line,
326 const char *section,
327 unsigned section_line,
328 const char *lvalue,
329 int ltype,
330 const char *rvalue,
331 void *data,
332 void *userdata) {
333
334 _cleanup_free_ SocketPort *p = NULL;
335 SocketPort *tail;
336 Socket *s;
337 int r;
338
339 assert(filename);
340 assert(lvalue);
341 assert(rvalue);
342 assert(data);
343
344 s = SOCKET(data);
345
346 if (isempty(rvalue)) {
347 /* An empty assignment removes all ports */
348 socket_free_ports(s);
349 return 0;
350 }
351
352 p = new0(SocketPort, 1);
353 if (!p)
354 return log_oom();
355
356 if (ltype != SOCKET_SOCKET) {
357 _cleanup_free_ char *k = NULL;
358
359 r = unit_full_printf(UNIT(s), rvalue, &k);
360 if (r < 0) {
361 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
362 return 0;
363 }
364
365 r = path_simplify_and_warn(k, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
366 if (r < 0)
367 return 0;
368
369 free_and_replace(p->path, k);
370 p->type = ltype;
371
372 } else if (streq(lvalue, "ListenNetlink")) {
373 _cleanup_free_ char *k = NULL;
374
375 r = unit_full_printf(UNIT(s), rvalue, &k);
376 if (r < 0) {
377 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
378 return 0;
379 }
380
381 r = socket_address_parse_netlink(&p->address, k);
382 if (r < 0) {
383 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse address value in '%s', ignoring: %m", k);
384 return 0;
385 }
386
387 p->type = SOCKET_SOCKET;
388
389 } else {
390 _cleanup_free_ char *k = NULL;
391
392 r = unit_full_printf(UNIT(s), rvalue, &k);
393 if (r < 0) {
394 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
395 return 0;
396 }
397
398 r = socket_address_parse_and_warn(&p->address, k);
399 if (r < 0) {
400 if (r != -EAFNOSUPPORT)
401 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse address value in '%s', ignoring: %m", k);
402 return 0;
403 }
404
405 if (streq(lvalue, "ListenStream"))
406 p->address.type = SOCK_STREAM;
407 else if (streq(lvalue, "ListenDatagram"))
408 p->address.type = SOCK_DGRAM;
409 else {
410 assert(streq(lvalue, "ListenSequentialPacket"));
411 p->address.type = SOCK_SEQPACKET;
412 }
413
414 if (socket_address_family(&p->address) != AF_LOCAL && p->address.type == SOCK_SEQPACKET) {
415 log_syntax(unit, LOG_ERR, filename, line, 0, "Address family not supported, ignoring: %s", rvalue);
416 return 0;
417 }
418
419 p->type = SOCKET_SOCKET;
420 }
421
422 p->fd = -1;
423 p->auxiliary_fds = NULL;
424 p->n_auxiliary_fds = 0;
425 p->socket = s;
426
427 LIST_FIND_TAIL(port, s->ports, tail);
428 LIST_INSERT_AFTER(port, s->ports, tail, p);
429
430 p = NULL;
431
432 return 0;
433 }
434
435 int config_parse_exec_nice(
436 const char *unit,
437 const char *filename,
438 unsigned line,
439 const char *section,
440 unsigned section_line,
441 const char *lvalue,
442 int ltype,
443 const char *rvalue,
444 void *data,
445 void *userdata) {
446
447 ExecContext *c = data;
448 int priority, r;
449
450 assert(filename);
451 assert(lvalue);
452 assert(rvalue);
453 assert(data);
454
455 if (isempty(rvalue)) {
456 c->nice_set = false;
457 return 0;
458 }
459
460 r = parse_nice(rvalue, &priority);
461 if (r < 0) {
462 if (r == -ERANGE)
463 log_syntax(unit, LOG_ERR, filename, line, r, "Nice priority out of range, ignoring: %s", rvalue);
464 else
465 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse nice priority '%s', ignoring: %m", rvalue);
466 return 0;
467 }
468
469 c->nice = priority;
470 c->nice_set = true;
471
472 return 0;
473 }
474
475 int config_parse_exec_oom_score_adjust(
476 const char* unit,
477 const char *filename,
478 unsigned line,
479 const char *section,
480 unsigned section_line,
481 const char *lvalue,
482 int ltype,
483 const char *rvalue,
484 void *data,
485 void *userdata) {
486
487 ExecContext *c = data;
488 int oa, r;
489
490 assert(filename);
491 assert(lvalue);
492 assert(rvalue);
493 assert(data);
494
495 if (isempty(rvalue)) {
496 c->oom_score_adjust_set = false;
497 return 0;
498 }
499
500 r = parse_oom_score_adjust(rvalue, &oa);
501 if (r < 0) {
502 if (r == -ERANGE)
503 log_syntax(unit, LOG_ERR, filename, line, r, "OOM score adjust value out of range, ignoring: %s", rvalue);
504 else
505 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse the OOM score adjust value '%s', ignoring: %m", rvalue);
506 return 0;
507 }
508
509 c->oom_score_adjust = oa;
510 c->oom_score_adjust_set = true;
511
512 return 0;
513 }
514
515 int config_parse_exec(
516 const char *unit,
517 const char *filename,
518 unsigned line,
519 const char *section,
520 unsigned section_line,
521 const char *lvalue,
522 int ltype,
523 const char *rvalue,
524 void *data,
525 void *userdata) {
526
527 ExecCommand **e = data;
528 Unit *u = userdata;
529 const char *p;
530 bool semicolon;
531 int r;
532
533 assert(filename);
534 assert(lvalue);
535 assert(rvalue);
536 assert(e);
537
538 e += ltype;
539 rvalue += strspn(rvalue, WHITESPACE);
540
541 if (isempty(rvalue)) {
542 /* An empty assignment resets the list */
543 *e = exec_command_free_list(*e);
544 return 0;
545 }
546
547 p = rvalue;
548 do {
549 _cleanup_free_ char *path = NULL, *firstword = NULL;
550 ExecCommandFlags flags = 0;
551 bool ignore = false, separate_argv0 = false;
552 _cleanup_free_ ExecCommand *nce = NULL;
553 _cleanup_strv_free_ char **n = NULL;
554 size_t nlen = 0, nbufsize = 0;
555 const char *f;
556
557 semicolon = false;
558
559 r = extract_first_word_and_warn(&p, &firstword, NULL, EXTRACT_QUOTES|EXTRACT_CUNESCAPE, unit, filename, line, rvalue);
560 if (r <= 0)
561 return 0;
562
563 f = firstword;
564 for (;;) {
565 /* We accept an absolute path as first argument. If it's prefixed with - and the path doesn't
566 * exist, we ignore it instead of erroring out; if it's prefixed with @, we allow overriding of
567 * argv[0]; if it's prefixed with +, it will be run with full privileges and no sandboxing; if
568 * it's prefixed with '!' we apply sandboxing, but do not change user/group credentials; if
569 * it's prefixed with '!!', then we apply user/group credentials if the kernel supports ambient
570 * capabilities -- if it doesn't we don't apply the credentials themselves, but do apply most
571 * other sandboxing, with some special exceptions for changing UID.
572 *
573 * The idea is that '!!' may be used to write services that can take benefit of systemd's
574 * UID/GID dropping if the kernel supports ambient creds, but provide an automatic fallback to
575 * privilege dropping within the daemon if the kernel does not offer that. */
576
577 if (*f == '-' && !(flags & EXEC_COMMAND_IGNORE_FAILURE)) {
578 flags |= EXEC_COMMAND_IGNORE_FAILURE;
579 ignore = true;
580 } else if (*f == '@' && !separate_argv0)
581 separate_argv0 = true;
582 else if (*f == '+' && !(flags & (EXEC_COMMAND_FULLY_PRIVILEGED|EXEC_COMMAND_NO_SETUID|EXEC_COMMAND_AMBIENT_MAGIC)))
583 flags |= EXEC_COMMAND_FULLY_PRIVILEGED;
584 else if (*f == '!' && !(flags & (EXEC_COMMAND_FULLY_PRIVILEGED|EXEC_COMMAND_NO_SETUID|EXEC_COMMAND_AMBIENT_MAGIC)))
585 flags |= EXEC_COMMAND_NO_SETUID;
586 else if (*f == '!' && !(flags & (EXEC_COMMAND_FULLY_PRIVILEGED|EXEC_COMMAND_AMBIENT_MAGIC))) {
587 flags &= ~EXEC_COMMAND_NO_SETUID;
588 flags |= EXEC_COMMAND_AMBIENT_MAGIC;
589 } else
590 break;
591 f++;
592 }
593
594 r = unit_full_printf(u, f, &path);
595 if (r < 0) {
596 log_syntax(unit, LOG_ERR, filename, line, r,
597 "Failed to resolve unit specifiers in '%s'%s: %m",
598 f, ignore ? ", ignoring" : "");
599 return ignore ? 0 : -ENOEXEC;
600 }
601
602 if (isempty(path)) {
603 /* First word is either "-" or "@" with no command. */
604 log_syntax(unit, LOG_ERR, filename, line, 0,
605 "Empty path in command line%s: '%s'",
606 ignore ? ", ignoring" : "", rvalue);
607 return ignore ? 0 : -ENOEXEC;
608 }
609 if (!string_is_safe(path)) {
610 log_syntax(unit, LOG_ERR, filename, line, 0,
611 "Executable name contains special characters%s: %s",
612 ignore ? ", ignoring" : "", path);
613 return ignore ? 0 : -ENOEXEC;
614 }
615 if (endswith(path, "/")) {
616 log_syntax(unit, LOG_ERR, filename, line, 0,
617 "Executable path specifies a directory%s: %s",
618 ignore ? ", ignoring" : "", path);
619 return ignore ? 0 : -ENOEXEC;
620 }
621
622 if (!path_is_absolute(path)) {
623 const char *prefix;
624 bool found = false;
625
626 if (!filename_is_valid(path)) {
627 log_syntax(unit, LOG_ERR, filename, line, 0,
628 "Neither a valid executable name nor an absolute path%s: %s",
629 ignore ? ", ignoring" : "", path);
630 return ignore ? 0 : -ENOEXEC;
631 }
632
633 /* Resolve a single-component name to a full path */
634 NULSTR_FOREACH(prefix, DEFAULT_PATH_NULSTR) {
635 _cleanup_free_ char *fullpath = NULL;
636
637 fullpath = strjoin(prefix, "/", path);
638 if (!fullpath)
639 return log_oom();
640
641 if (access(fullpath, F_OK) >= 0) {
642 free_and_replace(path, fullpath);
643 found = true;
644 break;
645 }
646 }
647
648 if (!found) {
649 log_syntax(unit, LOG_ERR, filename, line, 0,
650 "Executable \"%s\" not found in path \"%s\"%s",
651 path, DEFAULT_PATH, ignore ? ", ignoring" : "");
652 return ignore ? 0 : -ENOEXEC;
653 }
654 }
655
656 if (!separate_argv0) {
657 char *w = NULL;
658
659 if (!GREEDY_REALLOC(n, nbufsize, nlen + 2))
660 return log_oom();
661
662 w = strdup(path);
663 if (!w)
664 return log_oom();
665 n[nlen++] = w;
666 n[nlen] = NULL;
667 }
668
669 path_simplify(path, false);
670
671 while (!isempty(p)) {
672 _cleanup_free_ char *word = NULL, *resolved = NULL;
673
674 /* Check explicitly for an unquoted semicolon as
675 * command separator token. */
676 if (p[0] == ';' && (!p[1] || strchr(WHITESPACE, p[1]))) {
677 p++;
678 p += strspn(p, WHITESPACE);
679 semicolon = true;
680 break;
681 }
682
683 /* Check for \; explicitly, to not confuse it with \\; or "\;" or "\\;" etc.
684 * extract_first_word() would return the same for all of those. */
685 if (p[0] == '\\' && p[1] == ';' && (!p[2] || strchr(WHITESPACE, p[2]))) {
686 char *w;
687
688 p += 2;
689 p += strspn(p, WHITESPACE);
690
691 if (!GREEDY_REALLOC(n, nbufsize, nlen + 2))
692 return log_oom();
693
694 w = strdup(";");
695 if (!w)
696 return log_oom();
697 n[nlen++] = w;
698 n[nlen] = NULL;
699 continue;
700 }
701
702 r = extract_first_word_and_warn(&p, &word, NULL, EXTRACT_QUOTES|EXTRACT_CUNESCAPE, unit, filename, line, rvalue);
703 if (r == 0)
704 break;
705 if (r < 0)
706 return ignore ? 0 : -ENOEXEC;
707
708 r = unit_full_printf(u, word, &resolved);
709 if (r < 0) {
710 log_syntax(unit, LOG_ERR, filename, line, r,
711 "Failed to resolve unit specifiers in %s%s: %m",
712 word, ignore ? ", ignoring" : "");
713 return ignore ? 0 : -ENOEXEC;
714 }
715
716 if (!GREEDY_REALLOC(n, nbufsize, nlen + 2))
717 return log_oom();
718
719 n[nlen++] = TAKE_PTR(resolved);
720 n[nlen] = NULL;
721 }
722
723 if (!n || !n[0]) {
724 log_syntax(unit, LOG_ERR, filename, line, 0,
725 "Empty executable name or zeroeth argument%s: %s",
726 ignore ? ", ignoring" : "", rvalue);
727 return ignore ? 0 : -ENOEXEC;
728 }
729
730 nce = new0(ExecCommand, 1);
731 if (!nce)
732 return log_oom();
733
734 nce->argv = TAKE_PTR(n);
735 nce->path = TAKE_PTR(path);
736 nce->flags = flags;
737
738 exec_command_append_list(e, nce);
739
740 /* Do not _cleanup_free_ these. */
741 nce = NULL;
742
743 rvalue = p;
744 } while (semicolon);
745
746 return 0;
747 }
748
749 int config_parse_socket_bindtodevice(
750 const char* unit,
751 const char *filename,
752 unsigned line,
753 const char *section,
754 unsigned section_line,
755 const char *lvalue,
756 int ltype,
757 const char *rvalue,
758 void *data,
759 void *userdata) {
760
761 Socket *s = data;
762
763 assert(filename);
764 assert(lvalue);
765 assert(rvalue);
766 assert(data);
767
768 if (isempty(rvalue) || streq(rvalue, "*")) {
769 s->bind_to_device = mfree(s->bind_to_device);
770 return 0;
771 }
772
773 if (!ifname_valid(rvalue)) {
774 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid interface name, ignoring: %s", rvalue);
775 return 0;
776 }
777
778 if (free_and_strdup(&s->bind_to_device, rvalue) < 0)
779 return log_oom();
780
781 return 0;
782 }
783
784 int config_parse_exec_input(
785 const char *unit,
786 const char *filename,
787 unsigned line,
788 const char *section,
789 unsigned section_line,
790 const char *lvalue,
791 int ltype,
792 const char *rvalue,
793 void *data,
794 void *userdata) {
795
796 ExecContext *c = data;
797 Unit *u = userdata;
798 const char *n;
799 ExecInput ei;
800 int r;
801
802 assert(data);
803 assert(filename);
804 assert(line);
805 assert(rvalue);
806
807 n = startswith(rvalue, "fd:");
808 if (n) {
809 _cleanup_free_ char *resolved = NULL;
810
811 r = unit_full_printf(u, n, &resolved);
812 if (r < 0)
813 return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s': %m", n);
814
815 if (isempty(resolved))
816 resolved = mfree(resolved);
817 else if (!fdname_is_valid(resolved)) {
818 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid file descriptor name: %s", resolved);
819 return -ENOEXEC;
820 }
821
822 free_and_replace(c->stdio_fdname[STDIN_FILENO], resolved);
823
824 ei = EXEC_INPUT_NAMED_FD;
825
826 } else if ((n = startswith(rvalue, "file:"))) {
827 _cleanup_free_ char *resolved = NULL;
828
829 r = unit_full_printf(u, n, &resolved);
830 if (r < 0)
831 return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s': %m", n);
832
833 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE | PATH_CHECK_FATAL, unit, filename, line, lvalue);
834 if (r < 0)
835 return -ENOEXEC;
836
837 free_and_replace(c->stdio_file[STDIN_FILENO], resolved);
838
839 ei = EXEC_INPUT_FILE;
840
841 } else {
842 ei = exec_input_from_string(rvalue);
843 if (ei < 0) {
844 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse input specifier, ignoring: %s", rvalue);
845 return 0;
846 }
847 }
848
849 c->std_input = ei;
850 return 0;
851 }
852
853 int config_parse_exec_input_text(
854 const char *unit,
855 const char *filename,
856 unsigned line,
857 const char *section,
858 unsigned section_line,
859 const char *lvalue,
860 int ltype,
861 const char *rvalue,
862 void *data,
863 void *userdata) {
864
865 _cleanup_free_ char *unescaped = NULL, *resolved = NULL;
866 ExecContext *c = data;
867 Unit *u = userdata;
868 size_t sz;
869 void *p;
870 int r;
871
872 assert(data);
873 assert(filename);
874 assert(line);
875 assert(rvalue);
876
877 if (isempty(rvalue)) {
878 /* Reset if the empty string is assigned */
879 c->stdin_data = mfree(c->stdin_data);
880 c->stdin_data_size = 0;
881 return 0;
882 }
883
884 r = cunescape(rvalue, 0, &unescaped);
885 if (r < 0)
886 return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to decode C escaped text '%s': %m", rvalue);
887
888 r = unit_full_printf(u, unescaped, &resolved);
889 if (r < 0)
890 return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s': %m", unescaped);
891
892 sz = strlen(resolved);
893 if (c->stdin_data_size + sz + 1 < c->stdin_data_size || /* check for overflow */
894 c->stdin_data_size + sz + 1 > EXEC_STDIN_DATA_MAX) {
895 log_syntax(unit, LOG_ERR, filename, line, 0, "Standard input data too large (%zu), maximum of %zu permitted, ignoring.", c->stdin_data_size + sz, (size_t) EXEC_STDIN_DATA_MAX);
896 return -E2BIG;
897 }
898
899 p = realloc(c->stdin_data, c->stdin_data_size + sz + 1);
900 if (!p)
901 return log_oom();
902
903 *((char*) mempcpy((char*) p + c->stdin_data_size, resolved, sz)) = '\n';
904
905 c->stdin_data = p;
906 c->stdin_data_size += sz + 1;
907
908 return 0;
909 }
910
911 int config_parse_exec_input_data(
912 const char *unit,
913 const char *filename,
914 unsigned line,
915 const char *section,
916 unsigned section_line,
917 const char *lvalue,
918 int ltype,
919 const char *rvalue,
920 void *data,
921 void *userdata) {
922
923 _cleanup_free_ void *p = NULL;
924 ExecContext *c = data;
925 size_t sz;
926 void *q;
927 int r;
928
929 assert(data);
930 assert(filename);
931 assert(line);
932 assert(rvalue);
933
934 if (isempty(rvalue)) {
935 /* Reset if the empty string is assigned */
936 c->stdin_data = mfree(c->stdin_data);
937 c->stdin_data_size = 0;
938 return 0;
939 }
940
941 r = unbase64mem(rvalue, (size_t) -1, &p, &sz);
942 if (r < 0)
943 return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to decode base64 data, ignoring: %s", rvalue);
944
945 assert(sz > 0);
946
947 if (c->stdin_data_size + sz < c->stdin_data_size || /* check for overflow */
948 c->stdin_data_size + sz > EXEC_STDIN_DATA_MAX) {
949 log_syntax(unit, LOG_ERR, filename, line, 0, "Standard input data too large (%zu), maximum of %zu permitted, ignoring.", c->stdin_data_size + sz, (size_t) EXEC_STDIN_DATA_MAX);
950 return -E2BIG;
951 }
952
953 q = realloc(c->stdin_data, c->stdin_data_size + sz);
954 if (!q)
955 return log_oom();
956
957 memcpy((uint8_t*) q + c->stdin_data_size, p, sz);
958
959 c->stdin_data = q;
960 c->stdin_data_size += sz;
961
962 return 0;
963 }
964
965 int config_parse_exec_output(
966 const char *unit,
967 const char *filename,
968 unsigned line,
969 const char *section,
970 unsigned section_line,
971 const char *lvalue,
972 int ltype,
973 const char *rvalue,
974 void *data,
975 void *userdata) {
976
977 _cleanup_free_ char *resolved = NULL;
978 const char *n;
979 ExecContext *c = data;
980 Unit *u = userdata;
981 ExecOutput eo;
982 int r;
983
984 assert(data);
985 assert(filename);
986 assert(line);
987 assert(lvalue);
988 assert(rvalue);
989
990 n = startswith(rvalue, "fd:");
991 if (n) {
992 r = unit_full_printf(u, n, &resolved);
993 if (r < 0)
994 return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s: %m", n);
995
996 if (isempty(resolved))
997 resolved = mfree(resolved);
998 else if (!fdname_is_valid(resolved)) {
999 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid file descriptor name: %s", resolved);
1000 return -ENOEXEC;
1001 }
1002
1003 eo = EXEC_OUTPUT_NAMED_FD;
1004
1005 } else if ((n = startswith(rvalue, "file:"))) {
1006
1007 r = unit_full_printf(u, n, &resolved);
1008 if (r < 0)
1009 return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s: %m", n);
1010
1011 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE | PATH_CHECK_FATAL, unit, filename, line, lvalue);
1012 if (r < 0)
1013 return -ENOEXEC;
1014
1015 eo = EXEC_OUTPUT_FILE;
1016
1017 } else if ((n = startswith(rvalue, "append:"))) {
1018
1019 r = unit_full_printf(u, n, &resolved);
1020 if (r < 0)
1021 return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s: %m", n);
1022
1023 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE | PATH_CHECK_FATAL, unit, filename, line, lvalue);
1024 if (r < 0)
1025 return -ENOEXEC;
1026
1027 eo = EXEC_OUTPUT_FILE_APPEND;
1028 } else {
1029 eo = exec_output_from_string(rvalue);
1030 if (eo < 0) {
1031 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse output specifier, ignoring: %s", rvalue);
1032 return 0;
1033 }
1034 }
1035
1036 if (streq(lvalue, "StandardOutput")) {
1037 if (eo == EXEC_OUTPUT_NAMED_FD)
1038 free_and_replace(c->stdio_fdname[STDOUT_FILENO], resolved);
1039 else
1040 free_and_replace(c->stdio_file[STDOUT_FILENO], resolved);
1041
1042 c->std_output = eo;
1043
1044 } else {
1045 assert(streq(lvalue, "StandardError"));
1046
1047 if (eo == EXEC_OUTPUT_NAMED_FD)
1048 free_and_replace(c->stdio_fdname[STDERR_FILENO], resolved);
1049 else
1050 free_and_replace(c->stdio_file[STDERR_FILENO], resolved);
1051
1052 c->std_error = eo;
1053 }
1054
1055 return 0;
1056 }
1057
1058 int config_parse_exec_io_class(const char *unit,
1059 const char *filename,
1060 unsigned line,
1061 const char *section,
1062 unsigned section_line,
1063 const char *lvalue,
1064 int ltype,
1065 const char *rvalue,
1066 void *data,
1067 void *userdata) {
1068
1069 ExecContext *c = data;
1070 int x;
1071
1072 assert(filename);
1073 assert(lvalue);
1074 assert(rvalue);
1075 assert(data);
1076
1077 if (isempty(rvalue)) {
1078 c->ioprio_set = false;
1079 c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0);
1080 return 0;
1081 }
1082
1083 x = ioprio_class_from_string(rvalue);
1084 if (x < 0) {
1085 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse IO scheduling class, ignoring: %s", rvalue);
1086 return 0;
1087 }
1088
1089 c->ioprio = IOPRIO_PRIO_VALUE(x, IOPRIO_PRIO_DATA(c->ioprio));
1090 c->ioprio_set = true;
1091
1092 return 0;
1093 }
1094
1095 int config_parse_exec_io_priority(const char *unit,
1096 const char *filename,
1097 unsigned line,
1098 const char *section,
1099 unsigned section_line,
1100 const char *lvalue,
1101 int ltype,
1102 const char *rvalue,
1103 void *data,
1104 void *userdata) {
1105
1106 ExecContext *c = data;
1107 int i, r;
1108
1109 assert(filename);
1110 assert(lvalue);
1111 assert(rvalue);
1112 assert(data);
1113
1114 if (isempty(rvalue)) {
1115 c->ioprio_set = false;
1116 c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0);
1117 return 0;
1118 }
1119
1120 r = ioprio_parse_priority(rvalue, &i);
1121 if (r < 0) {
1122 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse IO priority, ignoring: %s", rvalue);
1123 return 0;
1124 }
1125
1126 c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_PRIO_CLASS(c->ioprio), i);
1127 c->ioprio_set = true;
1128
1129 return 0;
1130 }
1131
1132 int config_parse_exec_cpu_sched_policy(const char *unit,
1133 const char *filename,
1134 unsigned line,
1135 const char *section,
1136 unsigned section_line,
1137 const char *lvalue,
1138 int ltype,
1139 const char *rvalue,
1140 void *data,
1141 void *userdata) {
1142
1143 ExecContext *c = data;
1144 int x;
1145
1146 assert(filename);
1147 assert(lvalue);
1148 assert(rvalue);
1149 assert(data);
1150
1151 if (isempty(rvalue)) {
1152 c->cpu_sched_set = false;
1153 c->cpu_sched_policy = SCHED_OTHER;
1154 c->cpu_sched_priority = 0;
1155 return 0;
1156 }
1157
1158 x = sched_policy_from_string(rvalue);
1159 if (x < 0) {
1160 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse CPU scheduling policy, ignoring: %s", rvalue);
1161 return 0;
1162 }
1163
1164 c->cpu_sched_policy = x;
1165 /* Moving to or from real-time policy? We need to adjust the priority */
1166 c->cpu_sched_priority = CLAMP(c->cpu_sched_priority, sched_get_priority_min(x), sched_get_priority_max(x));
1167 c->cpu_sched_set = true;
1168
1169 return 0;
1170 }
1171
1172 int config_parse_exec_cpu_sched_prio(const char *unit,
1173 const char *filename,
1174 unsigned line,
1175 const char *section,
1176 unsigned section_line,
1177 const char *lvalue,
1178 int ltype,
1179 const char *rvalue,
1180 void *data,
1181 void *userdata) {
1182
1183 ExecContext *c = data;
1184 int i, min, max, r;
1185
1186 assert(filename);
1187 assert(lvalue);
1188 assert(rvalue);
1189 assert(data);
1190
1191 r = safe_atoi(rvalue, &i);
1192 if (r < 0) {
1193 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse CPU scheduling priority, ignoring: %s", rvalue);
1194 return 0;
1195 }
1196
1197 /* On Linux RR/FIFO range from 1 to 99 and OTHER/BATCH may only be 0 */
1198 min = sched_get_priority_min(c->cpu_sched_policy);
1199 max = sched_get_priority_max(c->cpu_sched_policy);
1200
1201 if (i < min || i > max) {
1202 log_syntax(unit, LOG_ERR, filename, line, 0, "CPU scheduling priority is out of range, ignoring: %s", rvalue);
1203 return 0;
1204 }
1205
1206 c->cpu_sched_priority = i;
1207 c->cpu_sched_set = true;
1208
1209 return 0;
1210 }
1211
1212 int config_parse_exec_cpu_affinity(const char *unit,
1213 const char *filename,
1214 unsigned line,
1215 const char *section,
1216 unsigned section_line,
1217 const char *lvalue,
1218 int ltype,
1219 const char *rvalue,
1220 void *data,
1221 void *userdata) {
1222
1223 ExecContext *c = data;
1224 _cleanup_cpu_free_ cpu_set_t *cpuset = NULL;
1225 int ncpus;
1226
1227 assert(filename);
1228 assert(lvalue);
1229 assert(rvalue);
1230 assert(data);
1231
1232 ncpus = parse_cpu_set_and_warn(rvalue, &cpuset, unit, filename, line, lvalue);
1233 if (ncpus < 0)
1234 return ncpus;
1235
1236 if (ncpus == 0) {
1237 /* An empty assignment resets the CPU list */
1238 c->cpuset = cpu_set_mfree(c->cpuset);
1239 c->cpuset_ncpus = 0;
1240 return 0;
1241 }
1242
1243 if (!c->cpuset) {
1244 c->cpuset = TAKE_PTR(cpuset);
1245 c->cpuset_ncpus = (unsigned) ncpus;
1246 return 0;
1247 }
1248
1249 if (c->cpuset_ncpus < (unsigned) ncpus) {
1250 CPU_OR_S(CPU_ALLOC_SIZE(c->cpuset_ncpus), cpuset, c->cpuset, cpuset);
1251 CPU_FREE(c->cpuset);
1252 c->cpuset = TAKE_PTR(cpuset);
1253 c->cpuset_ncpus = (unsigned) ncpus;
1254 return 0;
1255 }
1256
1257 CPU_OR_S(CPU_ALLOC_SIZE((unsigned) ncpus), c->cpuset, c->cpuset, cpuset);
1258
1259 return 0;
1260 }
1261
1262 int config_parse_capability_set(
1263 const char *unit,
1264 const char *filename,
1265 unsigned line,
1266 const char *section,
1267 unsigned section_line,
1268 const char *lvalue,
1269 int ltype,
1270 const char *rvalue,
1271 void *data,
1272 void *userdata) {
1273
1274 uint64_t *capability_set = data;
1275 uint64_t sum = 0, initial = 0;
1276 bool invert = false;
1277 int r;
1278
1279 assert(filename);
1280 assert(lvalue);
1281 assert(rvalue);
1282 assert(data);
1283
1284 if (rvalue[0] == '~') {
1285 invert = true;
1286 rvalue++;
1287 }
1288
1289 if (streq(lvalue, "CapabilityBoundingSet"))
1290 initial = CAP_ALL; /* initialized to all bits on */
1291 /* else "AmbientCapabilities" initialized to all bits off */
1292
1293 r = capability_set_from_string(rvalue, &sum);
1294 if (r < 0) {
1295 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse %s= specifier '%s', ignoring: %m", lvalue, rvalue);
1296 return 0;
1297 }
1298
1299 if (sum == 0 || *capability_set == initial)
1300 /* "", "~" or uninitialized data -> replace */
1301 *capability_set = invert ? ~sum : sum;
1302 else {
1303 /* previous data -> merge */
1304 if (invert)
1305 *capability_set &= ~sum;
1306 else
1307 *capability_set |= sum;
1308 }
1309
1310 return 0;
1311 }
1312
1313 int config_parse_exec_selinux_context(
1314 const char *unit,
1315 const char *filename,
1316 unsigned line,
1317 const char *section,
1318 unsigned section_line,
1319 const char *lvalue,
1320 int ltype,
1321 const char *rvalue,
1322 void *data,
1323 void *userdata) {
1324
1325 ExecContext *c = data;
1326 Unit *u = userdata;
1327 bool ignore;
1328 char *k;
1329 int r;
1330
1331 assert(filename);
1332 assert(lvalue);
1333 assert(rvalue);
1334 assert(data);
1335
1336 if (isempty(rvalue)) {
1337 c->selinux_context = mfree(c->selinux_context);
1338 c->selinux_context_ignore = false;
1339 return 0;
1340 }
1341
1342 if (rvalue[0] == '-') {
1343 ignore = true;
1344 rvalue++;
1345 } else
1346 ignore = false;
1347
1348 r = unit_full_printf(u, rvalue, &k);
1349 if (r < 0) {
1350 log_syntax(unit, LOG_ERR, filename, line, r,
1351 "Failed to resolve unit specifiers in '%s'%s: %m",
1352 rvalue, ignore ? ", ignoring" : "");
1353 return ignore ? 0 : -ENOEXEC;
1354 }
1355
1356 free_and_replace(c->selinux_context, k);
1357 c->selinux_context_ignore = ignore;
1358
1359 return 0;
1360 }
1361
1362 int config_parse_exec_apparmor_profile(
1363 const char *unit,
1364 const char *filename,
1365 unsigned line,
1366 const char *section,
1367 unsigned section_line,
1368 const char *lvalue,
1369 int ltype,
1370 const char *rvalue,
1371 void *data,
1372 void *userdata) {
1373
1374 ExecContext *c = data;
1375 Unit *u = userdata;
1376 bool ignore;
1377 char *k;
1378 int r;
1379
1380 assert(filename);
1381 assert(lvalue);
1382 assert(rvalue);
1383 assert(data);
1384
1385 if (isempty(rvalue)) {
1386 c->apparmor_profile = mfree(c->apparmor_profile);
1387 c->apparmor_profile_ignore = false;
1388 return 0;
1389 }
1390
1391 if (rvalue[0] == '-') {
1392 ignore = true;
1393 rvalue++;
1394 } else
1395 ignore = false;
1396
1397 r = unit_full_printf(u, rvalue, &k);
1398 if (r < 0) {
1399 log_syntax(unit, LOG_ERR, filename, line, r,
1400 "Failed to resolve unit specifiers in '%s'%s: %m",
1401 rvalue, ignore ? ", ignoring" : "");
1402 return ignore ? 0 : -ENOEXEC;
1403 }
1404
1405 free_and_replace(c->apparmor_profile, k);
1406 c->apparmor_profile_ignore = ignore;
1407
1408 return 0;
1409 }
1410
1411 int config_parse_exec_smack_process_label(
1412 const char *unit,
1413 const char *filename,
1414 unsigned line,
1415 const char *section,
1416 unsigned section_line,
1417 const char *lvalue,
1418 int ltype,
1419 const char *rvalue,
1420 void *data,
1421 void *userdata) {
1422
1423 ExecContext *c = data;
1424 Unit *u = userdata;
1425 bool ignore;
1426 char *k;
1427 int r;
1428
1429 assert(filename);
1430 assert(lvalue);
1431 assert(rvalue);
1432 assert(data);
1433
1434 if (isempty(rvalue)) {
1435 c->smack_process_label = mfree(c->smack_process_label);
1436 c->smack_process_label_ignore = false;
1437 return 0;
1438 }
1439
1440 if (rvalue[0] == '-') {
1441 ignore = true;
1442 rvalue++;
1443 } else
1444 ignore = false;
1445
1446 r = unit_full_printf(u, rvalue, &k);
1447 if (r < 0) {
1448 log_syntax(unit, LOG_ERR, filename, line, r,
1449 "Failed to resolve unit specifiers in '%s'%s: %m",
1450 rvalue, ignore ? ", ignoring" : "");
1451 return ignore ? 0 : -ENOEXEC;
1452 }
1453
1454 free_and_replace(c->smack_process_label, k);
1455 c->smack_process_label_ignore = ignore;
1456
1457 return 0;
1458 }
1459
1460 int config_parse_timer(const char *unit,
1461 const char *filename,
1462 unsigned line,
1463 const char *section,
1464 unsigned section_line,
1465 const char *lvalue,
1466 int ltype,
1467 const char *rvalue,
1468 void *data,
1469 void *userdata) {
1470
1471 Timer *t = data;
1472 usec_t usec = 0;
1473 TimerValue *v;
1474 TimerBase b;
1475 _cleanup_(calendar_spec_freep) CalendarSpec *c = NULL;
1476 Unit *u = userdata;
1477 _cleanup_free_ char *k = NULL;
1478 int r;
1479
1480 assert(filename);
1481 assert(lvalue);
1482 assert(rvalue);
1483 assert(data);
1484
1485 if (isempty(rvalue)) {
1486 /* Empty assignment resets list */
1487 timer_free_values(t);
1488 return 0;
1489 }
1490
1491 b = timer_base_from_string(lvalue);
1492 if (b < 0) {
1493 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse timer base, ignoring: %s", lvalue);
1494 return 0;
1495 }
1496
1497 r = unit_full_printf(u, rvalue, &k);
1498 if (r < 0) {
1499 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
1500 return 0;
1501 }
1502
1503 if (b == TIMER_CALENDAR) {
1504 if (calendar_spec_from_string(k, &c) < 0) {
1505 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse calendar specification, ignoring: %s", k);
1506 return 0;
1507 }
1508 } else
1509 if (parse_sec(k, &usec) < 0) {
1510 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse timer value, ignoring: %s", k);
1511 return 0;
1512 }
1513
1514 v = new0(TimerValue, 1);
1515 if (!v)
1516 return log_oom();
1517
1518 v->base = b;
1519 v->value = usec;
1520 v->calendar_spec = TAKE_PTR(c);
1521
1522 LIST_PREPEND(value, t->values, v);
1523
1524 return 0;
1525 }
1526
1527 int config_parse_trigger_unit(
1528 const char *unit,
1529 const char *filename,
1530 unsigned line,
1531 const char *section,
1532 unsigned section_line,
1533 const char *lvalue,
1534 int ltype,
1535 const char *rvalue,
1536 void *data,
1537 void *userdata) {
1538
1539 _cleanup_free_ char *p = NULL;
1540 Unit *u = data;
1541 UnitType type;
1542 int r;
1543
1544 assert(filename);
1545 assert(lvalue);
1546 assert(rvalue);
1547 assert(data);
1548
1549 if (!hashmap_isempty(u->dependencies[UNIT_TRIGGERS])) {
1550 log_syntax(unit, LOG_ERR, filename, line, 0, "Multiple units to trigger specified, ignoring: %s", rvalue);
1551 return 0;
1552 }
1553
1554 r = unit_name_printf(u, rvalue, &p);
1555 if (r < 0) {
1556 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue);
1557 return 0;
1558 }
1559
1560 type = unit_name_to_type(p);
1561 if (type < 0) {
1562 log_syntax(unit, LOG_ERR, filename, line, 0, "Unit type not valid, ignoring: %s", rvalue);
1563 return 0;
1564 }
1565 if (unit_has_name(u, p)) {
1566 log_syntax(unit, LOG_ERR, filename, line, 0, "Units cannot trigger themselves, ignoring: %s", rvalue);
1567 return 0;
1568 }
1569
1570 r = unit_add_two_dependencies_by_name(u, UNIT_BEFORE, UNIT_TRIGGERS, p, true, UNIT_DEPENDENCY_FILE);
1571 if (r < 0) {
1572 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to add trigger on %s, ignoring: %m", p);
1573 return 0;
1574 }
1575
1576 return 0;
1577 }
1578
1579 int config_parse_path_spec(const char *unit,
1580 const char *filename,
1581 unsigned line,
1582 const char *section,
1583 unsigned section_line,
1584 const char *lvalue,
1585 int ltype,
1586 const char *rvalue,
1587 void *data,
1588 void *userdata) {
1589
1590 Path *p = data;
1591 PathSpec *s;
1592 PathType b;
1593 _cleanup_free_ char *k = NULL;
1594 int r;
1595
1596 assert(filename);
1597 assert(lvalue);
1598 assert(rvalue);
1599 assert(data);
1600
1601 if (isempty(rvalue)) {
1602 /* Empty assignment clears list */
1603 path_free_specs(p);
1604 return 0;
1605 }
1606
1607 b = path_type_from_string(lvalue);
1608 if (b < 0) {
1609 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse path type, ignoring: %s", lvalue);
1610 return 0;
1611 }
1612
1613 r = unit_full_printf(UNIT(p), rvalue, &k);
1614 if (r < 0) {
1615 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue);
1616 return 0;
1617 }
1618
1619 r = path_simplify_and_warn(k, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
1620 if (r < 0)
1621 return 0;
1622
1623 s = new0(PathSpec, 1);
1624 if (!s)
1625 return log_oom();
1626
1627 s->unit = UNIT(p);
1628 s->path = TAKE_PTR(k);
1629 s->type = b;
1630 s->inotify_fd = -1;
1631
1632 LIST_PREPEND(spec, p->specs, s);
1633
1634 return 0;
1635 }
1636
1637 int config_parse_socket_service(
1638 const char *unit,
1639 const char *filename,
1640 unsigned line,
1641 const char *section,
1642 unsigned section_line,
1643 const char *lvalue,
1644 int ltype,
1645 const char *rvalue,
1646 void *data,
1647 void *userdata) {
1648
1649 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1650 _cleanup_free_ char *p = NULL;
1651 Socket *s = data;
1652 Unit *x;
1653 int r;
1654
1655 assert(filename);
1656 assert(lvalue);
1657 assert(rvalue);
1658 assert(data);
1659
1660 r = unit_name_printf(UNIT(s), rvalue, &p);
1661 if (r < 0) {
1662 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s: %m", rvalue);
1663 return -ENOEXEC;
1664 }
1665
1666 if (!endswith(p, ".service")) {
1667 log_syntax(unit, LOG_ERR, filename, line, 0, "Unit must be of type service: %s", rvalue);
1668 return -ENOEXEC;
1669 }
1670
1671 r = manager_load_unit(UNIT(s)->manager, p, NULL, &error, &x);
1672 if (r < 0) {
1673 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to load unit %s: %s", rvalue, bus_error_message(&error, r));
1674 return -ENOEXEC;
1675 }
1676
1677 unit_ref_set(&s->service, UNIT(s), x);
1678
1679 return 0;
1680 }
1681
1682 int config_parse_fdname(
1683 const char *unit,
1684 const char *filename,
1685 unsigned line,
1686 const char *section,
1687 unsigned section_line,
1688 const char *lvalue,
1689 int ltype,
1690 const char *rvalue,
1691 void *data,
1692 void *userdata) {
1693
1694 _cleanup_free_ char *p = NULL;
1695 Socket *s = data;
1696 int r;
1697
1698 assert(filename);
1699 assert(lvalue);
1700 assert(rvalue);
1701 assert(data);
1702
1703 if (isempty(rvalue)) {
1704 s->fdname = mfree(s->fdname);
1705 return 0;
1706 }
1707
1708 r = unit_full_printf(UNIT(s), rvalue, &p);
1709 if (r < 0) {
1710 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
1711 return 0;
1712 }
1713
1714 if (!fdname_is_valid(p)) {
1715 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid file descriptor name, ignoring: %s", p);
1716 return 0;
1717 }
1718
1719 return free_and_replace(s->fdname, p);
1720 }
1721
1722 int config_parse_service_sockets(
1723 const char *unit,
1724 const char *filename,
1725 unsigned line,
1726 const char *section,
1727 unsigned section_line,
1728 const char *lvalue,
1729 int ltype,
1730 const char *rvalue,
1731 void *data,
1732 void *userdata) {
1733
1734 Service *s = data;
1735 const char *p;
1736 int r;
1737
1738 assert(filename);
1739 assert(lvalue);
1740 assert(rvalue);
1741 assert(data);
1742
1743 p = rvalue;
1744 for (;;) {
1745 _cleanup_free_ char *word = NULL, *k = NULL;
1746
1747 r = extract_first_word(&p, &word, NULL, 0);
1748 if (r == 0)
1749 break;
1750 if (r == -ENOMEM)
1751 return log_oom();
1752 if (r < 0) {
1753 log_syntax(unit, LOG_ERR, filename, line, r, "Trailing garbage in sockets, ignoring: %s", rvalue);
1754 break;
1755 }
1756
1757 r = unit_name_printf(UNIT(s), word, &k);
1758 if (r < 0) {
1759 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", word);
1760 continue;
1761 }
1762
1763 if (!endswith(k, ".socket")) {
1764 log_syntax(unit, LOG_ERR, filename, line, 0, "Unit must be of type socket, ignoring: %s", k);
1765 continue;
1766 }
1767
1768 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_WANTS, UNIT_AFTER, k, true, UNIT_DEPENDENCY_FILE);
1769 if (r < 0)
1770 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to add dependency on %s, ignoring: %m", k);
1771
1772 r = unit_add_dependency_by_name(UNIT(s), UNIT_TRIGGERED_BY, k, true, UNIT_DEPENDENCY_FILE);
1773 if (r < 0)
1774 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to add dependency on %s, ignoring: %m", k);
1775 }
1776
1777 return 0;
1778 }
1779
1780 int config_parse_bus_name(
1781 const char *unit,
1782 const char *filename,
1783 unsigned line,
1784 const char *section,
1785 unsigned section_line,
1786 const char *lvalue,
1787 int ltype,
1788 const char *rvalue,
1789 void *data,
1790 void *userdata) {
1791
1792 _cleanup_free_ char *k = NULL;
1793 Unit *u = userdata;
1794 int r;
1795
1796 assert(filename);
1797 assert(lvalue);
1798 assert(rvalue);
1799 assert(u);
1800
1801 r = unit_full_printf(u, rvalue, &k);
1802 if (r < 0) {
1803 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue);
1804 return 0;
1805 }
1806
1807 if (!service_name_is_valid(k)) {
1808 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid bus name, ignoring: %s", k);
1809 return 0;
1810 }
1811
1812 return config_parse_string(unit, filename, line, section, section_line, lvalue, ltype, k, data, userdata);
1813 }
1814
1815 int config_parse_service_timeout(
1816 const char *unit,
1817 const char *filename,
1818 unsigned line,
1819 const char *section,
1820 unsigned section_line,
1821 const char *lvalue,
1822 int ltype,
1823 const char *rvalue,
1824 void *data,
1825 void *userdata) {
1826
1827 Service *s = userdata;
1828 usec_t usec;
1829 int r;
1830
1831 assert(filename);
1832 assert(lvalue);
1833 assert(rvalue);
1834 assert(s);
1835
1836 /* This is called for two cases: TimeoutSec= and TimeoutStartSec=. */
1837
1838 /* Traditionally, these options accepted 0 to disable the timeouts. However, a timeout of 0 suggests it happens
1839 * immediately, hence fix this to become USEC_INFINITY instead. This is in-line with how we internally handle
1840 * all other timeouts. */
1841 r = parse_sec_fix_0(rvalue, &usec);
1842 if (r < 0) {
1843 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse %s= parameter, ignoring: %s", lvalue, rvalue);
1844 return 0;
1845 }
1846
1847 s->start_timeout_defined = true;
1848 s->timeout_start_usec = usec;
1849
1850 if (streq(lvalue, "TimeoutSec"))
1851 s->timeout_stop_usec = usec;
1852
1853 return 0;
1854 }
1855
1856 int config_parse_sec_fix_0(
1857 const char *unit,
1858 const char *filename,
1859 unsigned line,
1860 const char *section,
1861 unsigned section_line,
1862 const char *lvalue,
1863 int ltype,
1864 const char *rvalue,
1865 void *data,
1866 void *userdata) {
1867
1868 usec_t *usec = data;
1869 int r;
1870
1871 assert(filename);
1872 assert(lvalue);
1873 assert(rvalue);
1874 assert(usec);
1875
1876 /* This is pretty much like config_parse_sec(), except that this treats a time of 0 as infinity, for
1877 * compatibility with older versions of systemd where 0 instead of infinity was used as indicator to turn off a
1878 * timeout. */
1879
1880 r = parse_sec_fix_0(rvalue, usec);
1881 if (r < 0) {
1882 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse %s= parameter, ignoring: %s", lvalue, rvalue);
1883 return 0;
1884 }
1885
1886 return 0;
1887 }
1888
1889 int config_parse_user_group(
1890 const char *unit,
1891 const char *filename,
1892 unsigned line,
1893 const char *section,
1894 unsigned section_line,
1895 const char *lvalue,
1896 int ltype,
1897 const char *rvalue,
1898 void *data,
1899 void *userdata) {
1900
1901 _cleanup_free_ char *k = NULL;
1902 char **user = data;
1903 Unit *u = userdata;
1904 int r;
1905
1906 assert(filename);
1907 assert(lvalue);
1908 assert(rvalue);
1909 assert(u);
1910
1911 if (isempty(rvalue)) {
1912 *user = mfree(*user);
1913 return 0;
1914 }
1915
1916 r = unit_full_printf(u, rvalue, &k);
1917 if (r < 0) {
1918 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s: %m", rvalue);
1919 return -ENOEXEC;
1920 }
1921
1922 if (!valid_user_group_name_or_id(k)) {
1923 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid user/group name or numeric ID: %s", k);
1924 return -ENOEXEC;
1925 }
1926
1927 return free_and_replace(*user, k);
1928 }
1929
1930 int config_parse_user_group_strv(
1931 const char *unit,
1932 const char *filename,
1933 unsigned line,
1934 const char *section,
1935 unsigned section_line,
1936 const char *lvalue,
1937 int ltype,
1938 const char *rvalue,
1939 void *data,
1940 void *userdata) {
1941
1942 char ***users = data;
1943 Unit *u = userdata;
1944 const char *p = rvalue;
1945 int r;
1946
1947 assert(filename);
1948 assert(lvalue);
1949 assert(rvalue);
1950 assert(u);
1951
1952 if (isempty(rvalue)) {
1953 *users = strv_free(*users);
1954 return 0;
1955 }
1956
1957 for (;;) {
1958 _cleanup_free_ char *word = NULL, *k = NULL;
1959
1960 r = extract_first_word(&p, &word, NULL, 0);
1961 if (r == 0)
1962 break;
1963 if (r == -ENOMEM)
1964 return log_oom();
1965 if (r < 0) {
1966 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid syntax: %s", rvalue);
1967 return -ENOEXEC;
1968 }
1969
1970 r = unit_full_printf(u, word, &k);
1971 if (r < 0) {
1972 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s: %m", word);
1973 return -ENOEXEC;
1974 }
1975
1976 if (!valid_user_group_name_or_id(k)) {
1977 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid user/group name or numeric ID: %s", k);
1978 return -ENOEXEC;
1979 }
1980
1981 r = strv_push(users, k);
1982 if (r < 0)
1983 return log_oom();
1984
1985 k = NULL;
1986 }
1987
1988 return 0;
1989 }
1990
1991 int config_parse_working_directory(
1992 const char *unit,
1993 const char *filename,
1994 unsigned line,
1995 const char *section,
1996 unsigned section_line,
1997 const char *lvalue,
1998 int ltype,
1999 const char *rvalue,
2000 void *data,
2001 void *userdata) {
2002
2003 ExecContext *c = data;
2004 Unit *u = userdata;
2005 bool missing_ok;
2006 int r;
2007
2008 assert(filename);
2009 assert(lvalue);
2010 assert(rvalue);
2011 assert(c);
2012 assert(u);
2013
2014 if (isempty(rvalue)) {
2015 c->working_directory_home = false;
2016 c->working_directory = mfree(c->working_directory);
2017 return 0;
2018 }
2019
2020 if (rvalue[0] == '-') {
2021 missing_ok = true;
2022 rvalue++;
2023 } else
2024 missing_ok = false;
2025
2026 if (streq(rvalue, "~")) {
2027 c->working_directory_home = true;
2028 c->working_directory = mfree(c->working_directory);
2029 } else {
2030 _cleanup_free_ char *k = NULL;
2031
2032 r = unit_full_printf(u, rvalue, &k);
2033 if (r < 0) {
2034 log_syntax(unit, LOG_ERR, filename, line, r,
2035 "Failed to resolve unit specifiers in working directory path '%s'%s: %m",
2036 rvalue, missing_ok ? ", ignoring" : "");
2037 return missing_ok ? 0 : -ENOEXEC;
2038 }
2039
2040 r = path_simplify_and_warn(k, PATH_CHECK_ABSOLUTE | (missing_ok ? 0 : PATH_CHECK_FATAL), unit, filename, line, lvalue);
2041 if (r < 0)
2042 return missing_ok ? 0 : -ENOEXEC;
2043
2044 c->working_directory_home = false;
2045 free_and_replace(c->working_directory, k);
2046 }
2047
2048 c->working_directory_missing_ok = missing_ok;
2049 return 0;
2050 }
2051
2052 int config_parse_unit_env_file(const char *unit,
2053 const char *filename,
2054 unsigned line,
2055 const char *section,
2056 unsigned section_line,
2057 const char *lvalue,
2058 int ltype,
2059 const char *rvalue,
2060 void *data,
2061 void *userdata) {
2062
2063 char ***env = data;
2064 Unit *u = userdata;
2065 _cleanup_free_ char *n = NULL;
2066 int r;
2067
2068 assert(filename);
2069 assert(lvalue);
2070 assert(rvalue);
2071 assert(data);
2072
2073 if (isempty(rvalue)) {
2074 /* Empty assignment frees the list */
2075 *env = strv_free(*env);
2076 return 0;
2077 }
2078
2079 r = unit_full_printf(u, rvalue, &n);
2080 if (r < 0) {
2081 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
2082 return 0;
2083 }
2084
2085 r = path_simplify_and_warn(n[0] == '-' ? n + 1 : n, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
2086 if (r < 0)
2087 return 0;
2088
2089 r = strv_push(env, n);
2090 if (r < 0)
2091 return log_oom();
2092
2093 n = NULL;
2094
2095 return 0;
2096 }
2097
2098 int config_parse_environ(
2099 const char *unit,
2100 const char *filename,
2101 unsigned line,
2102 const char *section,
2103 unsigned section_line,
2104 const char *lvalue,
2105 int ltype,
2106 const char *rvalue,
2107 void *data,
2108 void *userdata) {
2109
2110 Unit *u = userdata;
2111 char ***env = data;
2112 const char *p;
2113 int r;
2114
2115 assert(filename);
2116 assert(lvalue);
2117 assert(rvalue);
2118 assert(data);
2119
2120 if (isempty(rvalue)) {
2121 /* Empty assignment resets the list */
2122 *env = strv_free(*env);
2123 return 0;
2124 }
2125
2126 for (p = rvalue;; ) {
2127 _cleanup_free_ char *word = NULL, *k = NULL;
2128
2129 r = extract_first_word(&p, &word, NULL, EXTRACT_CUNESCAPE|EXTRACT_QUOTES);
2130 if (r == 0)
2131 return 0;
2132 if (r == -ENOMEM)
2133 return log_oom();
2134 if (r < 0) {
2135 log_syntax(unit, LOG_WARNING, filename, line, r,
2136 "Invalid syntax, ignoring: %s", rvalue);
2137 return 0;
2138 }
2139
2140 if (u) {
2141 r = unit_full_printf(u, word, &k);
2142 if (r < 0) {
2143 log_syntax(unit, LOG_ERR, filename, line, r,
2144 "Failed to resolve unit specifiers in %s, ignoring: %m", word);
2145 continue;
2146 }
2147 } else
2148 k = TAKE_PTR(word);
2149
2150 if (!env_assignment_is_valid(k)) {
2151 log_syntax(unit, LOG_ERR, filename, line, 0,
2152 "Invalid environment assignment, ignoring: %s", k);
2153 continue;
2154 }
2155
2156 r = strv_env_replace(env, k);
2157 if (r < 0)
2158 return log_oom();
2159
2160 k = NULL;
2161 }
2162 }
2163
2164 int config_parse_pass_environ(
2165 const char *unit,
2166 const char *filename,
2167 unsigned line,
2168 const char *section,
2169 unsigned section_line,
2170 const char *lvalue,
2171 int ltype,
2172 const char *rvalue,
2173 void *data,
2174 void *userdata) {
2175
2176 _cleanup_strv_free_ char **n = NULL;
2177 size_t nlen = 0, nbufsize = 0;
2178 char*** passenv = data;
2179 const char *p = rvalue;
2180 Unit *u = userdata;
2181 int r;
2182
2183 assert(filename);
2184 assert(lvalue);
2185 assert(rvalue);
2186 assert(data);
2187
2188 if (isempty(rvalue)) {
2189 /* Empty assignment resets the list */
2190 *passenv = strv_free(*passenv);
2191 return 0;
2192 }
2193
2194 for (;;) {
2195 _cleanup_free_ char *word = NULL, *k = NULL;
2196
2197 r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES);
2198 if (r == 0)
2199 break;
2200 if (r == -ENOMEM)
2201 return log_oom();
2202 if (r < 0) {
2203 log_syntax(unit, LOG_ERR, filename, line, r,
2204 "Trailing garbage in %s, ignoring: %s", lvalue, rvalue);
2205 break;
2206 }
2207
2208 if (u) {
2209 r = unit_full_printf(u, word, &k);
2210 if (r < 0) {
2211 log_syntax(unit, LOG_ERR, filename, line, r,
2212 "Failed to resolve specifiers in %s, ignoring: %m", word);
2213 continue;
2214 }
2215 } else
2216 k = TAKE_PTR(word);
2217
2218 if (!env_name_is_valid(k)) {
2219 log_syntax(unit, LOG_ERR, filename, line, 0,
2220 "Invalid environment name for %s, ignoring: %s", lvalue, k);
2221 continue;
2222 }
2223
2224 if (!GREEDY_REALLOC(n, nbufsize, nlen + 2))
2225 return log_oom();
2226
2227 n[nlen++] = TAKE_PTR(k);
2228 n[nlen] = NULL;
2229 }
2230
2231 if (n) {
2232 r = strv_extend_strv(passenv, n, true);
2233 if (r < 0)
2234 return r;
2235 }
2236
2237 return 0;
2238 }
2239
2240 int config_parse_unset_environ(
2241 const char *unit,
2242 const char *filename,
2243 unsigned line,
2244 const char *section,
2245 unsigned section_line,
2246 const char *lvalue,
2247 int ltype,
2248 const char *rvalue,
2249 void *data,
2250 void *userdata) {
2251
2252 _cleanup_strv_free_ char **n = NULL;
2253 size_t nlen = 0, nbufsize = 0;
2254 char*** unsetenv = data;
2255 const char *p = rvalue;
2256 Unit *u = userdata;
2257 int r;
2258
2259 assert(filename);
2260 assert(lvalue);
2261 assert(rvalue);
2262 assert(data);
2263
2264 if (isempty(rvalue)) {
2265 /* Empty assignment resets the list */
2266 *unsetenv = strv_free(*unsetenv);
2267 return 0;
2268 }
2269
2270 for (;;) {
2271 _cleanup_free_ char *word = NULL, *k = NULL;
2272
2273 r = extract_first_word(&p, &word, NULL, EXTRACT_CUNESCAPE|EXTRACT_QUOTES);
2274 if (r == 0)
2275 break;
2276 if (r == -ENOMEM)
2277 return log_oom();
2278 if (r < 0) {
2279 log_syntax(unit, LOG_ERR, filename, line, r,
2280 "Trailing garbage in %s, ignoring: %s", lvalue, rvalue);
2281 break;
2282 }
2283
2284 if (u) {
2285 r = unit_full_printf(u, word, &k);
2286 if (r < 0) {
2287 log_syntax(unit, LOG_ERR, filename, line, r,
2288 "Failed to resolve unit specifiers in %s, ignoring: %m", word);
2289 continue;
2290 }
2291 } else
2292 k = TAKE_PTR(word);
2293
2294 if (!env_assignment_is_valid(k) && !env_name_is_valid(k)) {
2295 log_syntax(unit, LOG_ERR, filename, line, 0,
2296 "Invalid environment name or assignment %s, ignoring: %s", lvalue, k);
2297 continue;
2298 }
2299
2300 if (!GREEDY_REALLOC(n, nbufsize, nlen + 2))
2301 return log_oom();
2302
2303 n[nlen++] = TAKE_PTR(k);
2304 n[nlen] = NULL;
2305 }
2306
2307 if (n) {
2308 r = strv_extend_strv(unsetenv, n, true);
2309 if (r < 0)
2310 return r;
2311 }
2312
2313 return 0;
2314 }
2315
2316 int config_parse_log_extra_fields(
2317 const char *unit,
2318 const char *filename,
2319 unsigned line,
2320 const char *section,
2321 unsigned section_line,
2322 const char *lvalue,
2323 int ltype,
2324 const char *rvalue,
2325 void *data,
2326 void *userdata) {
2327
2328 ExecContext *c = data;
2329 Unit *u = userdata;
2330 const char *p = rvalue;
2331 int r;
2332
2333 assert(filename);
2334 assert(lvalue);
2335 assert(rvalue);
2336 assert(c);
2337
2338 if (isempty(rvalue)) {
2339 exec_context_free_log_extra_fields(c);
2340 return 0;
2341 }
2342
2343 for (;;) {
2344 _cleanup_free_ char *word = NULL, *k = NULL;
2345 struct iovec *t;
2346 const char *eq;
2347
2348 r = extract_first_word(&p, &word, NULL, EXTRACT_CUNESCAPE|EXTRACT_QUOTES);
2349 if (r == 0)
2350 return 0;
2351 if (r == -ENOMEM)
2352 return log_oom();
2353 if (r < 0) {
2354 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
2355 return 0;
2356 }
2357
2358 r = unit_full_printf(u, word, &k);
2359 if (r < 0) {
2360 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", word);
2361 continue;
2362 }
2363
2364 eq = strchr(k, '=');
2365 if (!eq) {
2366 log_syntax(unit, LOG_ERR, filename, line, 0, "Log field lacks '=' character, ignoring: %s", k);
2367 continue;
2368 }
2369
2370 if (!journal_field_valid(k, eq-k, false)) {
2371 log_syntax(unit, LOG_ERR, filename, line, 0, "Log field name is invalid, ignoring: %s", k);
2372 continue;
2373 }
2374
2375 t = reallocarray(c->log_extra_fields, c->n_log_extra_fields+1, sizeof(struct iovec));
2376 if (!t)
2377 return log_oom();
2378
2379 c->log_extra_fields = t;
2380 c->log_extra_fields[c->n_log_extra_fields++] = IOVEC_MAKE_STRING(k);
2381
2382 k = NULL;
2383 }
2384 }
2385
2386 int config_parse_unit_condition_path(
2387 const char *unit,
2388 const char *filename,
2389 unsigned line,
2390 const char *section,
2391 unsigned section_line,
2392 const char *lvalue,
2393 int ltype,
2394 const char *rvalue,
2395 void *data,
2396 void *userdata) {
2397
2398 _cleanup_free_ char *p = NULL;
2399 Condition **list = data, *c;
2400 ConditionType t = ltype;
2401 bool trigger, negate;
2402 Unit *u = userdata;
2403 int r;
2404
2405 assert(filename);
2406 assert(lvalue);
2407 assert(rvalue);
2408 assert(data);
2409
2410 if (isempty(rvalue)) {
2411 /* Empty assignment resets the list */
2412 *list = condition_free_list(*list);
2413 return 0;
2414 }
2415
2416 trigger = rvalue[0] == '|';
2417 if (trigger)
2418 rvalue++;
2419
2420 negate = rvalue[0] == '!';
2421 if (negate)
2422 rvalue++;
2423
2424 r = unit_full_printf(u, rvalue, &p);
2425 if (r < 0) {
2426 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue);
2427 return 0;
2428 }
2429
2430 r = path_simplify_and_warn(p, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
2431 if (r < 0)
2432 return 0;
2433
2434 c = condition_new(t, p, trigger, negate);
2435 if (!c)
2436 return log_oom();
2437
2438 LIST_PREPEND(conditions, *list, c);
2439 return 0;
2440 }
2441
2442 int config_parse_unit_condition_string(
2443 const char *unit,
2444 const char *filename,
2445 unsigned line,
2446 const char *section,
2447 unsigned section_line,
2448 const char *lvalue,
2449 int ltype,
2450 const char *rvalue,
2451 void *data,
2452 void *userdata) {
2453
2454 _cleanup_free_ char *s = NULL;
2455 Condition **list = data, *c;
2456 ConditionType t = ltype;
2457 bool trigger, negate;
2458 Unit *u = userdata;
2459 int r;
2460
2461 assert(filename);
2462 assert(lvalue);
2463 assert(rvalue);
2464 assert(data);
2465
2466 if (isempty(rvalue)) {
2467 /* Empty assignment resets the list */
2468 *list = condition_free_list(*list);
2469 return 0;
2470 }
2471
2472 trigger = rvalue[0] == '|';
2473 if (trigger)
2474 rvalue++;
2475
2476 negate = rvalue[0] == '!';
2477 if (negate)
2478 rvalue++;
2479
2480 r = unit_full_printf(u, rvalue, &s);
2481 if (r < 0) {
2482 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
2483 return 0;
2484 }
2485
2486 c = condition_new(t, s, trigger, negate);
2487 if (!c)
2488 return log_oom();
2489
2490 LIST_PREPEND(conditions, *list, c);
2491 return 0;
2492 }
2493
2494 int config_parse_unit_condition_null(
2495 const char *unit,
2496 const char *filename,
2497 unsigned line,
2498 const char *section,
2499 unsigned section_line,
2500 const char *lvalue,
2501 int ltype,
2502 const char *rvalue,
2503 void *data,
2504 void *userdata) {
2505
2506 Condition **list = data, *c;
2507 bool trigger, negate;
2508 int b;
2509
2510 assert(filename);
2511 assert(lvalue);
2512 assert(rvalue);
2513 assert(data);
2514
2515 if (isempty(rvalue)) {
2516 /* Empty assignment resets the list */
2517 *list = condition_free_list(*list);
2518 return 0;
2519 }
2520
2521 trigger = rvalue[0] == '|';
2522 if (trigger)
2523 rvalue++;
2524
2525 negate = rvalue[0] == '!';
2526 if (negate)
2527 rvalue++;
2528
2529 b = parse_boolean(rvalue);
2530 if (b < 0) {
2531 log_syntax(unit, LOG_ERR, filename, line, b, "Failed to parse boolean value in condition, ignoring: %s", rvalue);
2532 return 0;
2533 }
2534
2535 if (!b)
2536 negate = !negate;
2537
2538 c = condition_new(CONDITION_NULL, NULL, trigger, negate);
2539 if (!c)
2540 return log_oom();
2541
2542 LIST_PREPEND(conditions, *list, c);
2543 return 0;
2544 }
2545
2546 int config_parse_unit_requires_mounts_for(
2547 const char *unit,
2548 const char *filename,
2549 unsigned line,
2550 const char *section,
2551 unsigned section_line,
2552 const char *lvalue,
2553 int ltype,
2554 const char *rvalue,
2555 void *data,
2556 void *userdata) {
2557
2558 const char *p = rvalue;
2559 Unit *u = userdata;
2560 int r;
2561
2562 assert(filename);
2563 assert(lvalue);
2564 assert(rvalue);
2565 assert(data);
2566
2567 for (;;) {
2568 _cleanup_free_ char *word = NULL, *resolved = NULL;
2569
2570 r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES);
2571 if (r == 0)
2572 return 0;
2573 if (r == -ENOMEM)
2574 return log_oom();
2575 if (r < 0) {
2576 log_syntax(unit, LOG_WARNING, filename, line, r,
2577 "Invalid syntax, ignoring: %s", rvalue);
2578 return 0;
2579 }
2580
2581 r = unit_full_printf(u, word, &resolved);
2582 if (r < 0) {
2583 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", word);
2584 continue;
2585 }
2586
2587 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
2588 if (r < 0)
2589 continue;
2590
2591 r = unit_require_mounts_for(u, resolved, UNIT_DEPENDENCY_FILE);
2592 if (r < 0) {
2593 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to add required mount '%s', ignoring: %m", resolved);
2594 continue;
2595 }
2596 }
2597 }
2598
2599 int config_parse_documentation(const char *unit,
2600 const char *filename,
2601 unsigned line,
2602 const char *section,
2603 unsigned section_line,
2604 const char *lvalue,
2605 int ltype,
2606 const char *rvalue,
2607 void *data,
2608 void *userdata) {
2609
2610 Unit *u = userdata;
2611 int r;
2612 char **a, **b;
2613
2614 assert(filename);
2615 assert(lvalue);
2616 assert(rvalue);
2617 assert(u);
2618
2619 if (isempty(rvalue)) {
2620 /* Empty assignment resets the list */
2621 u->documentation = strv_free(u->documentation);
2622 return 0;
2623 }
2624
2625 r = config_parse_unit_strv_printf(unit, filename, line, section, section_line, lvalue, ltype,
2626 rvalue, data, userdata);
2627 if (r < 0)
2628 return r;
2629
2630 for (a = b = u->documentation; a && *a; a++) {
2631
2632 if (documentation_url_is_valid(*a))
2633 *(b++) = *a;
2634 else {
2635 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid URL, ignoring: %s", *a);
2636 free(*a);
2637 }
2638 }
2639 if (b)
2640 *b = NULL;
2641
2642 return r;
2643 }
2644
2645 #if HAVE_SECCOMP
2646 int config_parse_syscall_filter(
2647 const char *unit,
2648 const char *filename,
2649 unsigned line,
2650 const char *section,
2651 unsigned section_line,
2652 const char *lvalue,
2653 int ltype,
2654 const char *rvalue,
2655 void *data,
2656 void *userdata) {
2657
2658 ExecContext *c = data;
2659 Unit *u = userdata;
2660 bool invert = false;
2661 const char *p;
2662 int r;
2663
2664 assert(filename);
2665 assert(lvalue);
2666 assert(rvalue);
2667 assert(u);
2668
2669 if (isempty(rvalue)) {
2670 /* Empty assignment resets the list */
2671 c->syscall_filter = hashmap_free(c->syscall_filter);
2672 c->syscall_whitelist = false;
2673 return 0;
2674 }
2675
2676 if (rvalue[0] == '~') {
2677 invert = true;
2678 rvalue++;
2679 }
2680
2681 if (!c->syscall_filter) {
2682 c->syscall_filter = hashmap_new(NULL);
2683 if (!c->syscall_filter)
2684 return log_oom();
2685
2686 if (invert)
2687 /* Allow everything but the ones listed */
2688 c->syscall_whitelist = false;
2689 else {
2690 /* Allow nothing but the ones listed */
2691 c->syscall_whitelist = true;
2692
2693 /* Accept default syscalls if we are on a whitelist */
2694 r = seccomp_parse_syscall_filter("@default", -1, c->syscall_filter, SECCOMP_PARSE_WHITELIST);
2695 if (r < 0)
2696 return r;
2697 }
2698 }
2699
2700 p = rvalue;
2701 for (;;) {
2702 _cleanup_free_ char *word = NULL, *name = NULL;
2703 int num;
2704
2705 r = extract_first_word(&p, &word, NULL, 0);
2706 if (r == 0)
2707 return 0;
2708 if (r == -ENOMEM)
2709 return log_oom();
2710 if (r < 0) {
2711 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
2712 return 0;
2713 }
2714
2715 r = parse_syscall_and_errno(word, &name, &num);
2716 if (r < 0) {
2717 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse syscall:errno, ignoring: %s", word);
2718 continue;
2719 }
2720
2721 r = seccomp_parse_syscall_filter_full(name, num, c->syscall_filter,
2722 SECCOMP_PARSE_LOG|SECCOMP_PARSE_PERMISSIVE|(invert ? SECCOMP_PARSE_INVERT : 0)|(c->syscall_whitelist ? SECCOMP_PARSE_WHITELIST : 0),
2723 unit, filename, line);
2724 if (r < 0)
2725 return r;
2726 }
2727 }
2728
2729 int config_parse_syscall_archs(
2730 const char *unit,
2731 const char *filename,
2732 unsigned line,
2733 const char *section,
2734 unsigned section_line,
2735 const char *lvalue,
2736 int ltype,
2737 const char *rvalue,
2738 void *data,
2739 void *userdata) {
2740
2741 const char *p = rvalue;
2742 Set **archs = data;
2743 int r;
2744
2745 if (isempty(rvalue)) {
2746 *archs = set_free(*archs);
2747 return 0;
2748 }
2749
2750 r = set_ensure_allocated(archs, NULL);
2751 if (r < 0)
2752 return log_oom();
2753
2754 for (;;) {
2755 _cleanup_free_ char *word = NULL;
2756 uint32_t a;
2757
2758 r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES);
2759 if (r == 0)
2760 return 0;
2761 if (r == -ENOMEM)
2762 return log_oom();
2763 if (r < 0) {
2764 log_syntax(unit, LOG_WARNING, filename, line, r,
2765 "Invalid syntax, ignoring: %s", rvalue);
2766 return 0;
2767 }
2768
2769 r = seccomp_arch_from_string(word, &a);
2770 if (r < 0) {
2771 log_syntax(unit, LOG_ERR, filename, line, r,
2772 "Failed to parse system call architecture \"%s\", ignoring: %m", word);
2773 continue;
2774 }
2775
2776 r = set_put(*archs, UINT32_TO_PTR(a + 1));
2777 if (r < 0)
2778 return log_oom();
2779 }
2780 }
2781
2782 int config_parse_syscall_errno(
2783 const char *unit,
2784 const char *filename,
2785 unsigned line,
2786 const char *section,
2787 unsigned section_line,
2788 const char *lvalue,
2789 int ltype,
2790 const char *rvalue,
2791 void *data,
2792 void *userdata) {
2793
2794 ExecContext *c = data;
2795 int e;
2796
2797 assert(filename);
2798 assert(lvalue);
2799 assert(rvalue);
2800
2801 if (isempty(rvalue)) {
2802 /* Empty assignment resets to KILL */
2803 c->syscall_errno = 0;
2804 return 0;
2805 }
2806
2807 e = parse_errno(rvalue);
2808 if (e <= 0) {
2809 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse error number, ignoring: %s", rvalue);
2810 return 0;
2811 }
2812
2813 c->syscall_errno = e;
2814 return 0;
2815 }
2816
2817 int config_parse_address_families(
2818 const char *unit,
2819 const char *filename,
2820 unsigned line,
2821 const char *section,
2822 unsigned section_line,
2823 const char *lvalue,
2824 int ltype,
2825 const char *rvalue,
2826 void *data,
2827 void *userdata) {
2828
2829 ExecContext *c = data;
2830 bool invert = false;
2831 const char *p;
2832 int r;
2833
2834 assert(filename);
2835 assert(lvalue);
2836 assert(rvalue);
2837
2838 if (isempty(rvalue)) {
2839 /* Empty assignment resets the list */
2840 c->address_families = set_free(c->address_families);
2841 c->address_families_whitelist = false;
2842 return 0;
2843 }
2844
2845 if (rvalue[0] == '~') {
2846 invert = true;
2847 rvalue++;
2848 }
2849
2850 if (!c->address_families) {
2851 c->address_families = set_new(NULL);
2852 if (!c->address_families)
2853 return log_oom();
2854
2855 c->address_families_whitelist = !invert;
2856 }
2857
2858 for (p = rvalue;;) {
2859 _cleanup_free_ char *word = NULL;
2860 int af;
2861
2862 r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES);
2863 if (r == 0)
2864 return 0;
2865 if (r == -ENOMEM)
2866 return log_oom();
2867 if (r < 0) {
2868 log_syntax(unit, LOG_WARNING, filename, line, r,
2869 "Invalid syntax, ignoring: %s", rvalue);
2870 return 0;
2871 }
2872
2873 af = af_from_name(word);
2874 if (af < 0) {
2875 log_syntax(unit, LOG_ERR, filename, line, af,
2876 "Failed to parse address family, ignoring: %s", word);
2877 continue;
2878 }
2879
2880 /* If we previously wanted to forbid an address family and now
2881 * we want to allow it, then just remove it from the list.
2882 */
2883 if (!invert == c->address_families_whitelist) {
2884 r = set_put(c->address_families, INT_TO_PTR(af));
2885 if (r < 0)
2886 return log_oom();
2887 } else
2888 set_remove(c->address_families, INT_TO_PTR(af));
2889 }
2890 }
2891
2892 int config_parse_restrict_namespaces(
2893 const char *unit,
2894 const char *filename,
2895 unsigned line,
2896 const char *section,
2897 unsigned section_line,
2898 const char *lvalue,
2899 int ltype,
2900 const char *rvalue,
2901 void *data,
2902 void *userdata) {
2903
2904 ExecContext *c = data;
2905 unsigned long flags;
2906 bool invert = false;
2907 int r;
2908
2909 if (isempty(rvalue)) {
2910 /* Reset to the default. */
2911 c->restrict_namespaces = NAMESPACE_FLAGS_INITIAL;
2912 return 0;
2913 }
2914
2915 /* Boolean parameter ignores the previous settings */
2916 r = parse_boolean(rvalue);
2917 if (r > 0) {
2918 c->restrict_namespaces = 0;
2919 return 0;
2920 } else if (r == 0) {
2921 c->restrict_namespaces = NAMESPACE_FLAGS_ALL;
2922 return 0;
2923 }
2924
2925 if (rvalue[0] == '~') {
2926 invert = true;
2927 rvalue++;
2928 }
2929
2930 /* Not a boolean argument, in this case it's a list of namespace types. */
2931 r = namespace_flags_from_string(rvalue, &flags);
2932 if (r < 0) {
2933 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse namespace type string, ignoring: %s", rvalue);
2934 return 0;
2935 }
2936
2937 if (c->restrict_namespaces == NAMESPACE_FLAGS_INITIAL)
2938 /* Initial assignment. Just set the value. */
2939 c->restrict_namespaces = invert ? (~flags) & NAMESPACE_FLAGS_ALL : flags;
2940 else
2941 /* Merge the value with the previous one. */
2942 SET_FLAG(c->restrict_namespaces, flags, !invert);
2943
2944 return 0;
2945 }
2946 #endif
2947
2948 int config_parse_unit_slice(
2949 const char *unit,
2950 const char *filename,
2951 unsigned line,
2952 const char *section,
2953 unsigned section_line,
2954 const char *lvalue,
2955 int ltype,
2956 const char *rvalue,
2957 void *data,
2958 void *userdata) {
2959
2960 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2961 _cleanup_free_ char *k = NULL;
2962 Unit *u = userdata, *slice = NULL;
2963 int r;
2964
2965 assert(filename);
2966 assert(lvalue);
2967 assert(rvalue);
2968 assert(u);
2969
2970 r = unit_name_printf(u, rvalue, &k);
2971 if (r < 0) {
2972 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue);
2973 return 0;
2974 }
2975
2976 r = manager_load_unit(u->manager, k, NULL, &error, &slice);
2977 if (r < 0) {
2978 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to load slice unit %s, ignoring: %s", k, bus_error_message(&error, r));
2979 return 0;
2980 }
2981
2982 r = unit_set_slice(u, slice);
2983 if (r < 0) {
2984 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to assign slice %s to unit %s, ignoring: %m", slice->id, u->id);
2985 return 0;
2986 }
2987
2988 return 0;
2989 }
2990
2991 int config_parse_cpu_quota(
2992 const char *unit,
2993 const char *filename,
2994 unsigned line,
2995 const char *section,
2996 unsigned section_line,
2997 const char *lvalue,
2998 int ltype,
2999 const char *rvalue,
3000 void *data,
3001 void *userdata) {
3002
3003 CGroupContext *c = data;
3004 int r;
3005
3006 assert(filename);
3007 assert(lvalue);
3008 assert(rvalue);
3009
3010 if (isempty(rvalue)) {
3011 c->cpu_quota_per_sec_usec = USEC_INFINITY;
3012 return 0;
3013 }
3014
3015 r = parse_permille_unbounded(rvalue);
3016 if (r <= 0) {
3017 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid CPU quota '%s', ignoring.", rvalue);
3018 return 0;
3019 }
3020
3021 c->cpu_quota_per_sec_usec = ((usec_t) r * USEC_PER_SEC) / 1000U;
3022 return 0;
3023 }
3024
3025 int config_parse_memory_limit(
3026 const char *unit,
3027 const char *filename,
3028 unsigned line,
3029 const char *section,
3030 unsigned section_line,
3031 const char *lvalue,
3032 int ltype,
3033 const char *rvalue,
3034 void *data,
3035 void *userdata) {
3036
3037 CGroupContext *c = data;
3038 uint64_t bytes = CGROUP_LIMIT_MAX;
3039 int r;
3040
3041 if (!isempty(rvalue) && !streq(rvalue, "infinity")) {
3042
3043 r = parse_permille(rvalue);
3044 if (r < 0) {
3045 r = parse_size(rvalue, 1024, &bytes);
3046 if (r < 0) {
3047 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid memory limit '%s', ignoring: %m", rvalue);
3048 return 0;
3049 }
3050 } else
3051 bytes = physical_memory_scale(r, 1000U);
3052
3053 if (bytes >= UINT64_MAX ||
3054 (bytes <= 0 && !streq(lvalue, "MemorySwapMax"))) {
3055 log_syntax(unit, LOG_ERR, filename, line, 0, "Memory limit '%s' out of range, ignoring.", rvalue);
3056 return 0;
3057 }
3058 }
3059
3060 if (streq(lvalue, "MemoryMin"))
3061 c->memory_min = bytes;
3062 else if (streq(lvalue, "MemoryLow"))
3063 c->memory_low = bytes;
3064 else if (streq(lvalue, "MemoryHigh"))
3065 c->memory_high = bytes;
3066 else if (streq(lvalue, "MemoryMax"))
3067 c->memory_max = bytes;
3068 else if (streq(lvalue, "MemorySwapMax"))
3069 c->memory_swap_max = bytes;
3070 else if (streq(lvalue, "MemoryLimit"))
3071 c->memory_limit = bytes;
3072 else
3073 return -EINVAL;
3074
3075 return 0;
3076 }
3077
3078 int config_parse_tasks_max(
3079 const char *unit,
3080 const char *filename,
3081 unsigned line,
3082 const char *section,
3083 unsigned section_line,
3084 const char *lvalue,
3085 int ltype,
3086 const char *rvalue,
3087 void *data,
3088 void *userdata) {
3089
3090 uint64_t *tasks_max = data, v;
3091 Unit *u = userdata;
3092 int r;
3093
3094 if (isempty(rvalue)) {
3095 *tasks_max = u ? u->manager->default_tasks_max : UINT64_MAX;
3096 return 0;
3097 }
3098
3099 if (streq(rvalue, "infinity")) {
3100 *tasks_max = CGROUP_LIMIT_MAX;
3101 return 0;
3102 }
3103
3104 r = parse_permille(rvalue);
3105 if (r < 0) {
3106 r = safe_atou64(rvalue, &v);
3107 if (r < 0) {
3108 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid maximum tasks value '%s', ignoring: %m", rvalue);
3109 return 0;
3110 }
3111 } else
3112 v = system_tasks_max_scale(r, 1000U);
3113
3114 if (v <= 0 || v >= UINT64_MAX) {
3115 log_syntax(unit, LOG_ERR, filename, line, 0, "Maximum tasks value '%s' out of range, ignoring.", rvalue);
3116 return 0;
3117 }
3118
3119 *tasks_max = v;
3120 return 0;
3121 }
3122
3123 int config_parse_delegate(
3124 const char *unit,
3125 const char *filename,
3126 unsigned line,
3127 const char *section,
3128 unsigned section_line,
3129 const char *lvalue,
3130 int ltype,
3131 const char *rvalue,
3132 void *data,
3133 void *userdata) {
3134
3135 CGroupContext *c = data;
3136 UnitType t;
3137 int r;
3138
3139 t = unit_name_to_type(unit);
3140 assert(t != _UNIT_TYPE_INVALID);
3141
3142 if (!unit_vtable[t]->can_delegate) {
3143 log_syntax(unit, LOG_ERR, filename, line, 0, "Delegate= setting not supported for this unit type, ignoring.");
3144 return 0;
3145 }
3146
3147 /* We either accept a boolean value, which may be used to turn on delegation for all controllers, or turn it
3148 * off for all. Or it takes a list of controller names, in which case we add the specified controllers to the
3149 * mask to delegate. */
3150
3151 if (isempty(rvalue)) {
3152 /* An empty string resets controllers and set Delegate=yes. */
3153 c->delegate = true;
3154 c->delegate_controllers = 0;
3155 return 0;
3156 }
3157
3158 r = parse_boolean(rvalue);
3159 if (r < 0) {
3160 const char *p = rvalue;
3161 CGroupMask mask = 0;
3162
3163 for (;;) {
3164 _cleanup_free_ char *word = NULL;
3165 CGroupController cc;
3166
3167 r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES);
3168 if (r == 0)
3169 break;
3170 if (r == -ENOMEM)
3171 return log_oom();
3172 if (r < 0) {
3173 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
3174 return 0;
3175 }
3176
3177 cc = cgroup_controller_from_string(word);
3178 if (cc < 0) {
3179 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid controller name '%s', ignoring", word);
3180 continue;
3181 }
3182
3183 mask |= CGROUP_CONTROLLER_TO_MASK(cc);
3184 }
3185
3186 c->delegate = true;
3187 c->delegate_controllers |= mask;
3188
3189 } else if (r > 0) {
3190 c->delegate = true;
3191 c->delegate_controllers = _CGROUP_MASK_ALL;
3192 } else {
3193 c->delegate = false;
3194 c->delegate_controllers = 0;
3195 }
3196
3197 return 0;
3198 }
3199
3200 int config_parse_device_allow(
3201 const char *unit,
3202 const char *filename,
3203 unsigned line,
3204 const char *section,
3205 unsigned section_line,
3206 const char *lvalue,
3207 int ltype,
3208 const char *rvalue,
3209 void *data,
3210 void *userdata) {
3211
3212 _cleanup_free_ char *path = NULL, *resolved = NULL;
3213 CGroupContext *c = data;
3214 const char *p = rvalue;
3215 int r;
3216
3217 if (isempty(rvalue)) {
3218 while (c->device_allow)
3219 cgroup_context_free_device_allow(c, c->device_allow);
3220
3221 return 0;
3222 }
3223
3224 r = extract_first_word(&p, &path, NULL, EXTRACT_QUOTES);
3225 if (r == -ENOMEM)
3226 return log_oom();
3227 if (r < 0) {
3228 log_syntax(unit, LOG_WARNING, filename, line, r,
3229 "Invalid syntax, ignoring: %s", rvalue);
3230 return 0;
3231 }
3232 if (r == 0) {
3233 log_syntax(unit, LOG_WARNING, filename, line, 0,
3234 "Failed to extract device path and rights from '%s', ignoring.", rvalue);
3235 return 0;
3236 }
3237
3238 r = unit_full_printf(userdata, path, &resolved);
3239 if (r < 0) {
3240 log_syntax(unit, LOG_WARNING, filename, line, r,
3241 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
3242 return 0;
3243 }
3244
3245 if (!STARTSWITH_SET(resolved, "block-", "char-")) {
3246
3247 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
3248 if (r < 0)
3249 return 0;
3250
3251 if (!valid_device_node_path(resolved)) {
3252 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid device node path '%s', ignoring.", resolved);
3253 return 0;
3254 }
3255 }
3256
3257 if (!isempty(p) && !in_charset(p, "rwm")) {
3258 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid device rights '%s', ignoring.", p);
3259 return 0;
3260 }
3261
3262 return cgroup_add_device_allow(c, resolved, p);
3263 }
3264
3265 int config_parse_io_device_weight(
3266 const char *unit,
3267 const char *filename,
3268 unsigned line,
3269 const char *section,
3270 unsigned section_line,
3271 const char *lvalue,
3272 int ltype,
3273 const char *rvalue,
3274 void *data,
3275 void *userdata) {
3276
3277 _cleanup_free_ char *path = NULL, *resolved = NULL;
3278 CGroupIODeviceWeight *w;
3279 CGroupContext *c = data;
3280 const char *p = rvalue;
3281 uint64_t u;
3282 int r;
3283
3284 assert(filename);
3285 assert(lvalue);
3286 assert(rvalue);
3287
3288 if (isempty(rvalue)) {
3289 while (c->io_device_weights)
3290 cgroup_context_free_io_device_weight(c, c->io_device_weights);
3291
3292 return 0;
3293 }
3294
3295 r = extract_first_word(&p, &path, NULL, EXTRACT_QUOTES);
3296 if (r == -ENOMEM)
3297 return log_oom();
3298 if (r < 0) {
3299 log_syntax(unit, LOG_WARNING, filename, line, r,
3300 "Invalid syntax, ignoring: %s", rvalue);
3301 return 0;
3302 }
3303 if (r == 0 || isempty(p)) {
3304 log_syntax(unit, LOG_WARNING, filename, line, 0,
3305 "Failed to extract device path and weight from '%s', ignoring.", rvalue);
3306 return 0;
3307 }
3308
3309 r = unit_full_printf(userdata, path, &resolved);
3310 if (r < 0) {
3311 log_syntax(unit, LOG_WARNING, filename, line, r,
3312 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
3313 return 0;
3314 }
3315
3316 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
3317 if (r < 0)
3318 return 0;
3319
3320 r = cg_weight_parse(p, &u);
3321 if (r < 0) {
3322 log_syntax(unit, LOG_ERR, filename, line, r, "IO weight '%s' invalid, ignoring: %m", p);
3323 return 0;
3324 }
3325
3326 assert(u != CGROUP_WEIGHT_INVALID);
3327
3328 w = new0(CGroupIODeviceWeight, 1);
3329 if (!w)
3330 return log_oom();
3331
3332 w->path = TAKE_PTR(resolved);
3333 w->weight = u;
3334
3335 LIST_PREPEND(device_weights, c->io_device_weights, w);
3336 return 0;
3337 }
3338
3339 int config_parse_io_device_latency(
3340 const char *unit,
3341 const char *filename,
3342 unsigned line,
3343 const char *section,
3344 unsigned section_line,
3345 const char *lvalue,
3346 int ltype,
3347 const char *rvalue,
3348 void *data,
3349 void *userdata) {
3350
3351 _cleanup_free_ char *path = NULL, *resolved = NULL;
3352 CGroupIODeviceLatency *l;
3353 CGroupContext *c = data;
3354 const char *p = rvalue;
3355 usec_t usec;
3356 int r;
3357
3358 assert(filename);
3359 assert(lvalue);
3360 assert(rvalue);
3361
3362 if (isempty(rvalue)) {
3363 while (c->io_device_latencies)
3364 cgroup_context_free_io_device_latency(c, c->io_device_latencies);
3365
3366 return 0;
3367 }
3368
3369 r = extract_first_word(&p, &path, NULL, EXTRACT_QUOTES);
3370 if (r == -ENOMEM)
3371 return log_oom();
3372 if (r < 0) {
3373 log_syntax(unit, LOG_WARNING, filename, line, r,
3374 "Invalid syntax, ignoring: %s", rvalue);
3375 return 0;
3376 }
3377 if (r == 0 || isempty(p)) {
3378 log_syntax(unit, LOG_WARNING, filename, line, 0,
3379 "Failed to extract device path and latency from '%s', ignoring.", rvalue);
3380 return 0;
3381 }
3382
3383 r = unit_full_printf(userdata, path, &resolved);
3384 if (r < 0) {
3385 log_syntax(unit, LOG_WARNING, filename, line, r,
3386 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
3387 return 0;
3388 }
3389
3390 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
3391 if (r < 0)
3392 return 0;
3393
3394 if (parse_sec(p, &usec) < 0) {
3395 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse timer value, ignoring: %s", p);
3396 return 0;
3397 }
3398
3399 l = new0(CGroupIODeviceLatency, 1);
3400 if (!l)
3401 return log_oom();
3402
3403 l->path = TAKE_PTR(resolved);
3404 l->target_usec = usec;
3405
3406 LIST_PREPEND(device_latencies, c->io_device_latencies, l);
3407 return 0;
3408 }
3409
3410 int config_parse_io_limit(
3411 const char *unit,
3412 const char *filename,
3413 unsigned line,
3414 const char *section,
3415 unsigned section_line,
3416 const char *lvalue,
3417 int ltype,
3418 const char *rvalue,
3419 void *data,
3420 void *userdata) {
3421
3422 _cleanup_free_ char *path = NULL, *resolved = NULL;
3423 CGroupIODeviceLimit *l = NULL, *t;
3424 CGroupContext *c = data;
3425 CGroupIOLimitType type;
3426 const char *p = rvalue;
3427 uint64_t num;
3428 int r;
3429
3430 assert(filename);
3431 assert(lvalue);
3432 assert(rvalue);
3433
3434 type = cgroup_io_limit_type_from_string(lvalue);
3435 assert(type >= 0);
3436
3437 if (isempty(rvalue)) {
3438 LIST_FOREACH(device_limits, l, c->io_device_limits)
3439 l->limits[type] = cgroup_io_limit_defaults[type];
3440 return 0;
3441 }
3442
3443 r = extract_first_word(&p, &path, NULL, EXTRACT_QUOTES);
3444 if (r == -ENOMEM)
3445 return log_oom();
3446 if (r < 0) {
3447 log_syntax(unit, LOG_WARNING, filename, line, r,
3448 "Invalid syntax, ignoring: %s", rvalue);
3449 return 0;
3450 }
3451 if (r == 0 || isempty(p)) {
3452 log_syntax(unit, LOG_WARNING, filename, line, 0,
3453 "Failed to extract device node and bandwidth from '%s', ignoring.", rvalue);
3454 return 0;
3455 }
3456
3457 r = unit_full_printf(userdata, path, &resolved);
3458 if (r < 0) {
3459 log_syntax(unit, LOG_WARNING, filename, line, r,
3460 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
3461 return 0;
3462 }
3463
3464 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
3465 if (r < 0)
3466 return 0;
3467
3468 if (streq("infinity", p))
3469 num = CGROUP_LIMIT_MAX;
3470 else {
3471 r = parse_size(p, 1000, &num);
3472 if (r < 0 || num <= 0) {
3473 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid IO limit '%s', ignoring.", p);
3474 return 0;
3475 }
3476 }
3477
3478 LIST_FOREACH(device_limits, t, c->io_device_limits) {
3479 if (path_equal(resolved, t->path)) {
3480 l = t;
3481 break;
3482 }
3483 }
3484
3485 if (!l) {
3486 CGroupIOLimitType ttype;
3487
3488 l = new0(CGroupIODeviceLimit, 1);
3489 if (!l)
3490 return log_oom();
3491
3492 l->path = TAKE_PTR(resolved);
3493 for (ttype = 0; ttype < _CGROUP_IO_LIMIT_TYPE_MAX; ttype++)
3494 l->limits[ttype] = cgroup_io_limit_defaults[ttype];
3495
3496 LIST_PREPEND(device_limits, c->io_device_limits, l);
3497 }
3498
3499 l->limits[type] = num;
3500
3501 return 0;
3502 }
3503
3504 int config_parse_blockio_device_weight(
3505 const char *unit,
3506 const char *filename,
3507 unsigned line,
3508 const char *section,
3509 unsigned section_line,
3510 const char *lvalue,
3511 int ltype,
3512 const char *rvalue,
3513 void *data,
3514 void *userdata) {
3515
3516 _cleanup_free_ char *path = NULL, *resolved = NULL;
3517 CGroupBlockIODeviceWeight *w;
3518 CGroupContext *c = data;
3519 const char *p = rvalue;
3520 uint64_t u;
3521 int r;
3522
3523 assert(filename);
3524 assert(lvalue);
3525 assert(rvalue);
3526
3527 if (isempty(rvalue)) {
3528 while (c->blockio_device_weights)
3529 cgroup_context_free_blockio_device_weight(c, c->blockio_device_weights);
3530
3531 return 0;
3532 }
3533
3534 r = extract_first_word(&p, &path, NULL, EXTRACT_QUOTES);
3535 if (r == -ENOMEM)
3536 return log_oom();
3537 if (r < 0) {
3538 log_syntax(unit, LOG_WARNING, filename, line, r,
3539 "Invalid syntax, ignoring: %s", rvalue);
3540 return 0;
3541 }
3542 if (r == 0 || isempty(p)) {
3543 log_syntax(unit, LOG_WARNING, filename, line, 0,
3544 "Failed to extract device node and weight from '%s', ignoring.", rvalue);
3545 return 0;
3546 }
3547
3548 r = unit_full_printf(userdata, path, &resolved);
3549 if (r < 0) {
3550 log_syntax(unit, LOG_WARNING, filename, line, r,
3551 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
3552 return 0;
3553 }
3554
3555 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
3556 if (r < 0)
3557 return 0;
3558
3559 r = cg_blkio_weight_parse(p, &u);
3560 if (r < 0) {
3561 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid block IO weight '%s', ignoring: %m", p);
3562 return 0;
3563 }
3564
3565 assert(u != CGROUP_BLKIO_WEIGHT_INVALID);
3566
3567 w = new0(CGroupBlockIODeviceWeight, 1);
3568 if (!w)
3569 return log_oom();
3570
3571 w->path = TAKE_PTR(resolved);
3572 w->weight = u;
3573
3574 LIST_PREPEND(device_weights, c->blockio_device_weights, w);
3575 return 0;
3576 }
3577
3578 int config_parse_blockio_bandwidth(
3579 const char *unit,
3580 const char *filename,
3581 unsigned line,
3582 const char *section,
3583 unsigned section_line,
3584 const char *lvalue,
3585 int ltype,
3586 const char *rvalue,
3587 void *data,
3588 void *userdata) {
3589
3590 _cleanup_free_ char *path = NULL, *resolved = NULL;
3591 CGroupBlockIODeviceBandwidth *b = NULL, *t;
3592 CGroupContext *c = data;
3593 const char *p = rvalue;
3594 uint64_t bytes;
3595 bool read;
3596 int r;
3597
3598 assert(filename);
3599 assert(lvalue);
3600 assert(rvalue);
3601
3602 read = streq("BlockIOReadBandwidth", lvalue);
3603
3604 if (isempty(rvalue)) {
3605 LIST_FOREACH(device_bandwidths, b, c->blockio_device_bandwidths) {
3606 b->rbps = CGROUP_LIMIT_MAX;
3607 b->wbps = CGROUP_LIMIT_MAX;
3608 }
3609 return 0;
3610 }
3611
3612 r = extract_first_word(&p, &path, NULL, EXTRACT_QUOTES);
3613 if (r == -ENOMEM)
3614 return log_oom();
3615 if (r < 0) {
3616 log_syntax(unit, LOG_WARNING, filename, line, r,
3617 "Invalid syntax, ignoring: %s", rvalue);
3618 return 0;
3619 }
3620 if (r == 0 || isempty(p)) {
3621 log_syntax(unit, LOG_WARNING, filename, line, 0,
3622 "Failed to extract device node and bandwidth from '%s', ignoring.", rvalue);
3623 return 0;
3624 }
3625
3626 r = unit_full_printf(userdata, path, &resolved);
3627 if (r < 0) {
3628 log_syntax(unit, LOG_WARNING, filename, line, r,
3629 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
3630 return 0;
3631 }
3632
3633 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
3634 if (r < 0)
3635 return 0;
3636
3637 r = parse_size(p, 1000, &bytes);
3638 if (r < 0 || bytes <= 0) {
3639 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid Block IO Bandwidth '%s', ignoring.", p);
3640 return 0;
3641 }
3642
3643 LIST_FOREACH(device_bandwidths, t, c->blockio_device_bandwidths) {
3644 if (path_equal(resolved, t->path)) {
3645 b = t;
3646 break;
3647 }
3648 }
3649
3650 if (!t) {
3651 b = new0(CGroupBlockIODeviceBandwidth, 1);
3652 if (!b)
3653 return log_oom();
3654
3655 b->path = TAKE_PTR(resolved);
3656 b->rbps = CGROUP_LIMIT_MAX;
3657 b->wbps = CGROUP_LIMIT_MAX;
3658
3659 LIST_PREPEND(device_bandwidths, c->blockio_device_bandwidths, b);
3660 }
3661
3662 if (read)
3663 b->rbps = bytes;
3664 else
3665 b->wbps = bytes;
3666
3667 return 0;
3668 }
3669
3670 int config_parse_job_mode_isolate(
3671 const char *unit,
3672 const char *filename,
3673 unsigned line,
3674 const char *section,
3675 unsigned section_line,
3676 const char *lvalue,
3677 int ltype,
3678 const char *rvalue,
3679 void *data,
3680 void *userdata) {
3681
3682 JobMode *m = data;
3683 int r;
3684
3685 assert(filename);
3686 assert(lvalue);
3687 assert(rvalue);
3688
3689 r = parse_boolean(rvalue);
3690 if (r < 0) {
3691 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse boolean, ignoring: %s", rvalue);
3692 return 0;
3693 }
3694
3695 log_notice("%s is deprecated. Please use OnFailureJobMode= instead", lvalue);
3696
3697 *m = r ? JOB_ISOLATE : JOB_REPLACE;
3698 return 0;
3699 }
3700
3701 int config_parse_exec_directories(
3702 const char *unit,
3703 const char *filename,
3704 unsigned line,
3705 const char *section,
3706 unsigned section_line,
3707 const char *lvalue,
3708 int ltype,
3709 const char *rvalue,
3710 void *data,
3711 void *userdata) {
3712
3713 char***rt = data;
3714 Unit *u = userdata;
3715 const char *p;
3716 int r;
3717
3718 assert(filename);
3719 assert(lvalue);
3720 assert(rvalue);
3721 assert(data);
3722
3723 if (isempty(rvalue)) {
3724 /* Empty assignment resets the list */
3725 *rt = strv_free(*rt);
3726 return 0;
3727 }
3728
3729 for (p = rvalue;;) {
3730 _cleanup_free_ char *word = NULL, *k = NULL;
3731
3732 r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES);
3733 if (r == -ENOMEM)
3734 return log_oom();
3735 if (r < 0) {
3736 log_syntax(unit, LOG_WARNING, filename, line, r,
3737 "Invalid syntax, ignoring: %s", rvalue);
3738 return 0;
3739 }
3740 if (r == 0)
3741 return 0;
3742
3743 r = unit_full_printf(u, word, &k);
3744 if (r < 0) {
3745 log_syntax(unit, LOG_ERR, filename, line, r,
3746 "Failed to resolve unit specifiers in \"%s\", ignoring: %m", word);
3747 continue;
3748 }
3749
3750 r = path_simplify_and_warn(k, PATH_CHECK_RELATIVE, unit, filename, line, lvalue);
3751 if (r < 0)
3752 continue;
3753
3754 if (path_startswith(k, "private")) {
3755 log_syntax(unit, LOG_ERR, filename, line, 0,
3756 "%s= path can't be 'private', ingoring assignment: %s", lvalue, word);
3757 continue;
3758 }
3759
3760 r = strv_push(rt, k);
3761 if (r < 0)
3762 return log_oom();
3763 k = NULL;
3764 }
3765 }
3766
3767 int config_parse_set_status(
3768 const char *unit,
3769 const char *filename,
3770 unsigned line,
3771 const char *section,
3772 unsigned section_line,
3773 const char *lvalue,
3774 int ltype,
3775 const char *rvalue,
3776 void *data,
3777 void *userdata) {
3778
3779 size_t l;
3780 const char *word, *state;
3781 int r;
3782 ExitStatusSet *status_set = data;
3783
3784 assert(filename);
3785 assert(lvalue);
3786 assert(rvalue);
3787 assert(data);
3788
3789 /* Empty assignment resets the list */
3790 if (isempty(rvalue)) {
3791 exit_status_set_free(status_set);
3792 return 0;
3793 }
3794
3795 FOREACH_WORD(word, l, rvalue, state) {
3796 _cleanup_free_ char *temp;
3797 int val;
3798 Set **set;
3799
3800 temp = strndup(word, l);
3801 if (!temp)
3802 return log_oom();
3803
3804 r = safe_atoi(temp, &val);
3805 if (r < 0) {
3806 val = signal_from_string(temp);
3807
3808 if (val <= 0) {
3809 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse value, ignoring: %s", word);
3810 continue;
3811 }
3812 set = &status_set->signal;
3813 } else {
3814 if (val < 0 || val > 255) {
3815 log_syntax(unit, LOG_ERR, filename, line, 0, "Value %d is outside range 0-255, ignoring", val);
3816 continue;
3817 }
3818 set = &status_set->status;
3819 }
3820
3821 r = set_ensure_allocated(set, NULL);
3822 if (r < 0)
3823 return log_oom();
3824
3825 r = set_put(*set, INT_TO_PTR(val));
3826 if (r < 0)
3827 return log_oom();
3828 }
3829 if (!isempty(state))
3830 log_syntax(unit, LOG_ERR, filename, line, 0, "Trailing garbage, ignoring.");
3831
3832 return 0;
3833 }
3834
3835 int config_parse_namespace_path_strv(
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 Unit *u = userdata;
3848 char*** sv = data;
3849 const char *p = rvalue;
3850 int r;
3851
3852 assert(filename);
3853 assert(lvalue);
3854 assert(rvalue);
3855 assert(data);
3856
3857 if (isempty(rvalue)) {
3858 /* Empty assignment resets the list */
3859 *sv = strv_free(*sv);
3860 return 0;
3861 }
3862
3863 for (;;) {
3864 _cleanup_free_ char *word = NULL, *resolved = NULL, *joined = NULL;
3865 const char *w;
3866 bool ignore_enoent = false, shall_prefix = false;
3867
3868 r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES);
3869 if (r == 0)
3870 break;
3871 if (r == -ENOMEM)
3872 return log_oom();
3873 if (r < 0) {
3874 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to extract first word, ignoring: %s", rvalue);
3875 return 0;
3876 }
3877
3878 w = word;
3879 if (startswith(w, "-")) {
3880 ignore_enoent = true;
3881 w++;
3882 }
3883 if (startswith(w, "+")) {
3884 shall_prefix = true;
3885 w++;
3886 }
3887
3888 r = unit_full_printf(u, w, &resolved);
3889 if (r < 0) {
3890 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s: %m", w);
3891 continue;
3892 }
3893
3894 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
3895 if (r < 0)
3896 continue;
3897
3898 joined = strjoin(ignore_enoent ? "-" : "",
3899 shall_prefix ? "+" : "",
3900 resolved);
3901
3902 r = strv_push(sv, joined);
3903 if (r < 0)
3904 return log_oom();
3905
3906 joined = NULL;
3907 }
3908
3909 return 0;
3910 }
3911
3912 int config_parse_temporary_filesystems(
3913 const char *unit,
3914 const char *filename,
3915 unsigned line,
3916 const char *section,
3917 unsigned section_line,
3918 const char *lvalue,
3919 int ltype,
3920 const char *rvalue,
3921 void *data,
3922 void *userdata) {
3923
3924 Unit *u = userdata;
3925 ExecContext *c = data;
3926 const char *p = rvalue;
3927 int r;
3928
3929 assert(filename);
3930 assert(lvalue);
3931 assert(rvalue);
3932 assert(data);
3933
3934 if (isempty(rvalue)) {
3935 /* Empty assignment resets the list */
3936 temporary_filesystem_free_many(c->temporary_filesystems, c->n_temporary_filesystems);
3937 c->temporary_filesystems = NULL;
3938 c->n_temporary_filesystems = 0;
3939 return 0;
3940 }
3941
3942 for (;;) {
3943 _cleanup_free_ char *word = NULL, *path = NULL, *resolved = NULL;
3944 const char *w;
3945
3946 r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES);
3947 if (r == 0)
3948 return 0;
3949 if (r == -ENOMEM)
3950 return log_oom();
3951 if (r < 0) {
3952 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to extract first word, ignoring: %s", rvalue);
3953 return 0;
3954 }
3955
3956 w = word;
3957 r = extract_first_word(&w, &path, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
3958 if (r == -ENOMEM)
3959 return log_oom();
3960 if (r < 0) {
3961 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to extract first word, ignoring: %s", word);
3962 continue;
3963 }
3964 if (r == 0) {
3965 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid syntax, ignoring: %s", word);
3966 continue;
3967 }
3968
3969 r = unit_full_printf(u, path, &resolved);
3970 if (r < 0) {
3971 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", path);
3972 continue;
3973 }
3974
3975 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
3976 if (r < 0)
3977 continue;
3978
3979 r = temporary_filesystem_add(&c->temporary_filesystems, &c->n_temporary_filesystems, resolved, w);
3980 if (r < 0)
3981 return log_oom();
3982 }
3983 }
3984
3985 int config_parse_bind_paths(
3986 const char *unit,
3987 const char *filename,
3988 unsigned line,
3989 const char *section,
3990 unsigned section_line,
3991 const char *lvalue,
3992 int ltype,
3993 const char *rvalue,
3994 void *data,
3995 void *userdata) {
3996
3997 ExecContext *c = data;
3998 Unit *u = userdata;
3999 const char *p;
4000 int r;
4001
4002 assert(filename);
4003 assert(lvalue);
4004 assert(rvalue);
4005 assert(data);
4006
4007 if (isempty(rvalue)) {
4008 /* Empty assignment resets the list */
4009 bind_mount_free_many(c->bind_mounts, c->n_bind_mounts);
4010 c->bind_mounts = NULL;
4011 c->n_bind_mounts = 0;
4012 return 0;
4013 }
4014
4015 p = rvalue;
4016 for (;;) {
4017 _cleanup_free_ char *source = NULL, *destination = NULL;
4018 _cleanup_free_ char *sresolved = NULL, *dresolved = NULL;
4019 char *s = NULL, *d = NULL;
4020 bool rbind = true, ignore_enoent = false;
4021
4022 r = extract_first_word(&p, &source, ":" WHITESPACE, EXTRACT_QUOTES|EXTRACT_DONT_COALESCE_SEPARATORS);
4023 if (r == 0)
4024 break;
4025 if (r == -ENOMEM)
4026 return log_oom();
4027 if (r < 0) {
4028 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse %s, ignoring: %s", lvalue, rvalue);
4029 return 0;
4030 }
4031
4032 r = unit_full_printf(u, source, &sresolved);
4033 if (r < 0) {
4034 log_syntax(unit, LOG_ERR, filename, line, r,
4035 "Failed to resolved unit specifiers in \"%s\", ignoring: %m", source);
4036 continue;
4037 }
4038
4039 s = sresolved;
4040 if (s[0] == '-') {
4041 ignore_enoent = true;
4042 s++;
4043 }
4044
4045 r = path_simplify_and_warn(s, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
4046 if (r < 0)
4047 continue;
4048
4049 /* Optionally, the destination is specified. */
4050 if (p && p[-1] == ':') {
4051 r = extract_first_word(&p, &destination, ":" WHITESPACE, EXTRACT_QUOTES|EXTRACT_DONT_COALESCE_SEPARATORS);
4052 if (r == -ENOMEM)
4053 return log_oom();
4054 if (r < 0) {
4055 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse %s, ignoring: %s", lvalue, rvalue);
4056 return 0;
4057 }
4058 if (r == 0) {
4059 log_syntax(unit, LOG_ERR, filename, line, 0, "Missing argument after ':', ignoring: %s", s);
4060 continue;
4061 }
4062
4063 r = unit_full_printf(u, destination, &dresolved);
4064 if (r < 0) {
4065 log_syntax(unit, LOG_ERR, filename, line, r,
4066 "Failed to resolved specifiers in \"%s\", ignoring: %m", destination);
4067 continue;
4068 }
4069
4070 r = path_simplify_and_warn(dresolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
4071 if (r < 0)
4072 continue;
4073
4074 d = dresolved;
4075
4076 /* Optionally, there's also a short option string specified */
4077 if (p && p[-1] == ':') {
4078 _cleanup_free_ char *options = NULL;
4079
4080 r = extract_first_word(&p, &options, NULL, EXTRACT_QUOTES);
4081 if (r == -ENOMEM)
4082 return log_oom();
4083 if (r < 0) {
4084 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse %s: %s", lvalue, rvalue);
4085 return 0;
4086 }
4087
4088 if (isempty(options) || streq(options, "rbind"))
4089 rbind = true;
4090 else if (streq(options, "norbind"))
4091 rbind = false;
4092 else {
4093 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid option string, ignoring setting: %s", options);
4094 continue;
4095 }
4096 }
4097 } else
4098 d = s;
4099
4100 r = bind_mount_add(&c->bind_mounts, &c->n_bind_mounts,
4101 &(BindMount) {
4102 .source = s,
4103 .destination = d,
4104 .read_only = !!strstr(lvalue, "ReadOnly"),
4105 .recursive = rbind,
4106 .ignore_enoent = ignore_enoent,
4107 });
4108 if (r < 0)
4109 return log_oom();
4110 }
4111
4112 return 0;
4113 }
4114
4115 int config_parse_job_timeout_sec(
4116 const char* unit,
4117 const char *filename,
4118 unsigned line,
4119 const char *section,
4120 unsigned section_line,
4121 const char *lvalue,
4122 int ltype,
4123 const char *rvalue,
4124 void *data,
4125 void *userdata) {
4126
4127 Unit *u = data;
4128 usec_t usec;
4129 int r;
4130
4131 assert(filename);
4132 assert(lvalue);
4133 assert(rvalue);
4134 assert(u);
4135
4136 r = parse_sec_fix_0(rvalue, &usec);
4137 if (r < 0) {
4138 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse JobTimeoutSec= parameter, ignoring: %s", rvalue);
4139 return 0;
4140 }
4141
4142 /* If the user explicitly changed JobTimeoutSec= also change JobRunningTimeoutSec=, for compatibility with old
4143 * versions. If JobRunningTimeoutSec= was explicitly set, avoid this however as whatever the user picked should
4144 * count. */
4145
4146 if (!u->job_running_timeout_set)
4147 u->job_running_timeout = usec;
4148
4149 u->job_timeout = usec;
4150
4151 return 0;
4152 }
4153
4154 int config_parse_job_running_timeout_sec(
4155 const char* unit,
4156 const char *filename,
4157 unsigned line,
4158 const char *section,
4159 unsigned section_line,
4160 const char *lvalue,
4161 int ltype,
4162 const char *rvalue,
4163 void *data,
4164 void *userdata) {
4165
4166 Unit *u = data;
4167 usec_t usec;
4168 int r;
4169
4170 assert(filename);
4171 assert(lvalue);
4172 assert(rvalue);
4173 assert(u);
4174
4175 r = parse_sec_fix_0(rvalue, &usec);
4176 if (r < 0) {
4177 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse JobRunningTimeoutSec= parameter, ignoring: %s", rvalue);
4178 return 0;
4179 }
4180
4181 u->job_running_timeout = usec;
4182 u->job_running_timeout_set = true;
4183
4184 return 0;
4185 }
4186
4187 int config_parse_emergency_action(
4188 const char* unit,
4189 const char *filename,
4190 unsigned line,
4191 const char *section,
4192 unsigned section_line,
4193 const char *lvalue,
4194 int ltype,
4195 const char *rvalue,
4196 void *data,
4197 void *userdata) {
4198
4199 Manager *m = NULL;
4200 EmergencyAction *x = data;
4201 int r;
4202
4203 assert(filename);
4204 assert(lvalue);
4205 assert(rvalue);
4206 assert(data);
4207
4208 if (unit)
4209 m = ((Unit*) userdata)->manager;
4210 else
4211 m = data;
4212
4213 r = parse_emergency_action(rvalue, MANAGER_IS_SYSTEM(m), x);
4214 if (r < 0) {
4215 if (r == -EOPNOTSUPP && MANAGER_IS_USER(m)) {
4216 /* Compat mode: remove for systemd 241. */
4217
4218 log_syntax(unit, LOG_INFO, filename, line, r,
4219 "%s= in user mode specified as \"%s\", using \"exit-force\" instead.",
4220 lvalue, rvalue);
4221 *x = EMERGENCY_ACTION_EXIT_FORCE;
4222 return 0;
4223 }
4224
4225 if (r == -EOPNOTSUPP)
4226 log_syntax(unit, LOG_ERR, filename, line, r,
4227 "%s= specified as %s mode action, ignoring: %s",
4228 lvalue, MANAGER_IS_SYSTEM(m) ? "user" : "system", rvalue);
4229 else
4230 log_syntax(unit, LOG_ERR, filename, line, r,
4231 "Failed to parse %s=, ignoring: %s", lvalue, rvalue);
4232 return 0;
4233 }
4234
4235 return 0;
4236 }
4237
4238 int config_parse_pid_file(
4239 const char *unit,
4240 const char *filename,
4241 unsigned line,
4242 const char *section,
4243 unsigned section_line,
4244 const char *lvalue,
4245 int ltype,
4246 const char *rvalue,
4247 void *data,
4248 void *userdata) {
4249
4250 _cleanup_free_ char *k = NULL, *n = NULL;
4251 Unit *u = userdata;
4252 char **s = data;
4253 const char *e;
4254 int r;
4255
4256 assert(filename);
4257 assert(lvalue);
4258 assert(rvalue);
4259 assert(u);
4260
4261 r = unit_full_printf(u, rvalue, &k);
4262 if (r < 0) {
4263 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
4264 return 0;
4265 }
4266
4267 /* If this is a relative path make it absolute by prefixing the /run */
4268 n = path_make_absolute(k, u->manager->prefix[EXEC_DIRECTORY_RUNTIME]);
4269 if (!n)
4270 return log_oom();
4271
4272 /* Check that the result is a sensible path */
4273 r = path_simplify_and_warn(n, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
4274 if (r < 0)
4275 return r;
4276
4277 e = path_startswith(n, "/var/run/");
4278 if (e) {
4279 char *z;
4280
4281 z = strjoin("/run/", e);
4282 if (!z)
4283 return log_oom();
4284
4285 log_syntax(unit, LOG_NOTICE, filename, line, 0, "PIDFile= references path below legacy directory /var/run/, updating %s → %s; please update the unit file accordingly.", n, z);
4286
4287 free_and_replace(*s, z);
4288 } else
4289 free_and_replace(*s, n);
4290
4291 return 0;
4292 }
4293
4294 int config_parse_exit_status(
4295 const char *unit,
4296 const char *filename,
4297 unsigned line,
4298 const char *section,
4299 unsigned section_line,
4300 const char *lvalue,
4301 int ltype,
4302 const char *rvalue,
4303 void *data,
4304 void *userdata) {
4305
4306 int *exit_status = data, r;
4307 uint8_t u;
4308
4309 assert(filename);
4310 assert(lvalue);
4311 assert(rvalue);
4312 assert(exit_status);
4313
4314 if (isempty(rvalue)) {
4315 *exit_status = -1;
4316 return 0;
4317 }
4318
4319 r = safe_atou8(rvalue, &u);
4320 if (r < 0) {
4321 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse exit status '%s', ignoring: %m", rvalue);
4322 return 0;
4323 }
4324
4325 *exit_status = u;
4326 return 0;
4327 }
4328
4329 #define FOLLOW_MAX 8
4330
4331 static int open_follow(char **filename, FILE **_f, Set *names, char **_final) {
4332 char *id = NULL;
4333 unsigned c = 0;
4334 int fd, r;
4335 FILE *f;
4336
4337 assert(filename);
4338 assert(*filename);
4339 assert(_f);
4340 assert(names);
4341
4342 /* This will update the filename pointer if the loaded file is
4343 * reached by a symlink. The old string will be freed. */
4344
4345 for (;;) {
4346 char *target, *name;
4347
4348 if (c++ >= FOLLOW_MAX)
4349 return -ELOOP;
4350
4351 path_simplify(*filename, false);
4352
4353 /* Add the file name we are currently looking at to
4354 * the names of this unit, but only if it is a valid
4355 * unit name. */
4356 name = basename(*filename);
4357 if (unit_name_is_valid(name, UNIT_NAME_ANY)) {
4358
4359 id = set_get(names, name);
4360 if (!id) {
4361 id = strdup(name);
4362 if (!id)
4363 return -ENOMEM;
4364
4365 r = set_consume(names, id);
4366 if (r < 0)
4367 return r;
4368 }
4369 }
4370
4371 /* Try to open the file name, but don't if its a symlink */
4372 fd = open(*filename, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
4373 if (fd >= 0)
4374 break;
4375
4376 if (errno != ELOOP)
4377 return -errno;
4378
4379 /* Hmm, so this is a symlink. Let's read the name, and follow it manually */
4380 r = readlink_and_make_absolute(*filename, &target);
4381 if (r < 0)
4382 return r;
4383
4384 free_and_replace(*filename, target);
4385 }
4386
4387 f = fdopen(fd, "re");
4388 if (!f) {
4389 safe_close(fd);
4390 return -errno;
4391 }
4392
4393 *_f = f;
4394 *_final = id;
4395
4396 return 0;
4397 }
4398
4399 static int merge_by_names(Unit **u, Set *names, const char *id) {
4400 char *k;
4401 int r;
4402
4403 assert(u);
4404 assert(*u);
4405 assert(names);
4406
4407 /* Let's try to add in all symlink names we found */
4408 while ((k = set_steal_first(names))) {
4409
4410 /* First try to merge in the other name into our
4411 * unit */
4412 r = unit_merge_by_name(*u, k);
4413 if (r < 0) {
4414 Unit *other;
4415
4416 /* Hmm, we couldn't merge the other unit into
4417 * ours? Then let's try it the other way
4418 * round */
4419
4420 /* If the symlink name we are looking at is unit template, then
4421 we must search for instance of this template */
4422 if (unit_name_is_valid(k, UNIT_NAME_TEMPLATE) && (*u)->instance) {
4423 _cleanup_free_ char *instance = NULL;
4424
4425 r = unit_name_replace_instance(k, (*u)->instance, &instance);
4426 if (r < 0)
4427 return r;
4428
4429 other = manager_get_unit((*u)->manager, instance);
4430 } else
4431 other = manager_get_unit((*u)->manager, k);
4432
4433 free(k);
4434
4435 if (other) {
4436 r = unit_merge(other, *u);
4437 if (r >= 0) {
4438 *u = other;
4439 return merge_by_names(u, names, NULL);
4440 }
4441 }
4442
4443 return r;
4444 }
4445
4446 if (id == k)
4447 unit_choose_id(*u, id);
4448
4449 free(k);
4450 }
4451
4452 return 0;
4453 }
4454
4455 static int load_from_path(Unit *u, const char *path) {
4456 _cleanup_set_free_free_ Set *symlink_names = NULL;
4457 _cleanup_fclose_ FILE *f = NULL;
4458 _cleanup_free_ char *filename = NULL;
4459 char *id = NULL;
4460 Unit *merged;
4461 struct stat st;
4462 int r;
4463
4464 assert(u);
4465 assert(path);
4466
4467 symlink_names = set_new(&string_hash_ops);
4468 if (!symlink_names)
4469 return -ENOMEM;
4470
4471 if (path_is_absolute(path)) {
4472
4473 filename = strdup(path);
4474 if (!filename)
4475 return -ENOMEM;
4476
4477 r = open_follow(&filename, &f, symlink_names, &id);
4478 if (r < 0) {
4479 filename = mfree(filename);
4480 if (r != -ENOENT)
4481 return r;
4482 }
4483
4484 } else {
4485 char **p;
4486
4487 STRV_FOREACH(p, u->manager->lookup_paths.search_path) {
4488
4489 /* Instead of opening the path right away, we manually
4490 * follow all symlinks and add their name to our unit
4491 * name set while doing so */
4492 filename = path_make_absolute(path, *p);
4493 if (!filename)
4494 return -ENOMEM;
4495
4496 if (u->manager->unit_path_cache &&
4497 !set_get(u->manager->unit_path_cache, filename))
4498 r = -ENOENT;
4499 else
4500 r = open_follow(&filename, &f, symlink_names, &id);
4501 if (r >= 0)
4502 break;
4503 filename = mfree(filename);
4504
4505 /* ENOENT means that the file is missing or is a dangling symlink.
4506 * ENOTDIR means that one of paths we expect to be is a directory
4507 * is not a directory, we should just ignore that.
4508 * EACCES means that the directory or file permissions are wrong.
4509 */
4510 if (r == -EACCES)
4511 log_debug_errno(r, "Cannot access \"%s\": %m", filename);
4512 else if (!IN_SET(r, -ENOENT, -ENOTDIR))
4513 return r;
4514
4515 /* Empty the symlink names for the next run */
4516 set_clear_free(symlink_names);
4517 }
4518 }
4519
4520 if (!filename)
4521 /* Hmm, no suitable file found? */
4522 return 0;
4523
4524 if (!unit_type_may_alias(u->type) && set_size(symlink_names) > 1) {
4525 log_unit_warning(u, "Unit type of %s does not support alias names, refusing loading via symlink.", u->id);
4526 return -ELOOP;
4527 }
4528
4529 merged = u;
4530 r = merge_by_names(&merged, symlink_names, id);
4531 if (r < 0)
4532 return r;
4533
4534 if (merged != u) {
4535 u->load_state = UNIT_MERGED;
4536 return 0;
4537 }
4538
4539 if (fstat(fileno(f), &st) < 0)
4540 return -errno;
4541
4542 if (null_or_empty(&st)) {
4543 u->load_state = UNIT_MASKED;
4544 u->fragment_mtime = 0;
4545 } else {
4546 u->load_state = UNIT_LOADED;
4547 u->fragment_mtime = timespec_load(&st.st_mtim);
4548
4549 /* Now, parse the file contents */
4550 r = config_parse(u->id, filename, f,
4551 UNIT_VTABLE(u)->sections,
4552 config_item_perf_lookup, load_fragment_gperf_lookup,
4553 CONFIG_PARSE_ALLOW_INCLUDE, u);
4554 if (r < 0)
4555 return r;
4556 }
4557
4558 free_and_replace(u->fragment_path, filename);
4559
4560 if (u->source_path) {
4561 if (stat(u->source_path, &st) >= 0)
4562 u->source_mtime = timespec_load(&st.st_mtim);
4563 else
4564 u->source_mtime = 0;
4565 }
4566
4567 return 0;
4568 }
4569
4570 int unit_load_fragment(Unit *u) {
4571 int r;
4572 Iterator i;
4573 const char *t;
4574
4575 assert(u);
4576 assert(u->load_state == UNIT_STUB);
4577 assert(u->id);
4578
4579 if (u->transient) {
4580 u->load_state = UNIT_LOADED;
4581 return 0;
4582 }
4583
4584 /* First, try to find the unit under its id. We always look
4585 * for unit files in the default directories, to make it easy
4586 * to override things by placing things in /etc/systemd/system */
4587 r = load_from_path(u, u->id);
4588 if (r < 0)
4589 return r;
4590
4591 /* Try to find an alias we can load this with */
4592 if (u->load_state == UNIT_STUB) {
4593 SET_FOREACH(t, u->names, i) {
4594
4595 if (t == u->id)
4596 continue;
4597
4598 r = load_from_path(u, t);
4599 if (r < 0)
4600 return r;
4601
4602 if (u->load_state != UNIT_STUB)
4603 break;
4604 }
4605 }
4606
4607 /* And now, try looking for it under the suggested (originally linked) path */
4608 if (u->load_state == UNIT_STUB && u->fragment_path) {
4609
4610 r = load_from_path(u, u->fragment_path);
4611 if (r < 0)
4612 return r;
4613
4614 if (u->load_state == UNIT_STUB)
4615 /* Hmm, this didn't work? Then let's get rid
4616 * of the fragment path stored for us, so that
4617 * we don't point to an invalid location. */
4618 u->fragment_path = mfree(u->fragment_path);
4619 }
4620
4621 /* Look for a template */
4622 if (u->load_state == UNIT_STUB && u->instance) {
4623 _cleanup_free_ char *k = NULL;
4624
4625 r = unit_name_template(u->id, &k);
4626 if (r < 0)
4627 return r;
4628
4629 r = load_from_path(u, k);
4630 if (r < 0) {
4631 if (r == -ENOEXEC)
4632 log_unit_notice(u, "Unit configuration has fatal error, unit will not be started.");
4633 return r;
4634 }
4635
4636 if (u->load_state == UNIT_STUB) {
4637 SET_FOREACH(t, u->names, i) {
4638 _cleanup_free_ char *z = NULL;
4639
4640 if (t == u->id)
4641 continue;
4642
4643 r = unit_name_template(t, &z);
4644 if (r < 0)
4645 return r;
4646
4647 r = load_from_path(u, z);
4648 if (r < 0)
4649 return r;
4650
4651 if (u->load_state != UNIT_STUB)
4652 break;
4653 }
4654 }
4655 }
4656
4657 return 0;
4658 }
4659
4660 void unit_dump_config_items(FILE *f) {
4661 static const struct {
4662 const ConfigParserCallback callback;
4663 const char *rvalue;
4664 } table[] = {
4665 { config_parse_warn_compat, "NOTSUPPORTED" },
4666 { config_parse_int, "INTEGER" },
4667 { config_parse_unsigned, "UNSIGNED" },
4668 { config_parse_iec_size, "SIZE" },
4669 { config_parse_iec_uint64, "SIZE" },
4670 { config_parse_si_size, "SIZE" },
4671 { config_parse_bool, "BOOLEAN" },
4672 { config_parse_string, "STRING" },
4673 { config_parse_path, "PATH" },
4674 { config_parse_unit_path_printf, "PATH" },
4675 { config_parse_strv, "STRING [...]" },
4676 { config_parse_exec_nice, "NICE" },
4677 { config_parse_exec_oom_score_adjust, "OOMSCOREADJUST" },
4678 { config_parse_exec_io_class, "IOCLASS" },
4679 { config_parse_exec_io_priority, "IOPRIORITY" },
4680 { config_parse_exec_cpu_sched_policy, "CPUSCHEDPOLICY" },
4681 { config_parse_exec_cpu_sched_prio, "CPUSCHEDPRIO" },
4682 { config_parse_exec_cpu_affinity, "CPUAFFINITY" },
4683 { config_parse_mode, "MODE" },
4684 { config_parse_unit_env_file, "FILE" },
4685 { config_parse_exec_output, "OUTPUT" },
4686 { config_parse_exec_input, "INPUT" },
4687 { config_parse_log_facility, "FACILITY" },
4688 { config_parse_log_level, "LEVEL" },
4689 { config_parse_exec_secure_bits, "SECUREBITS" },
4690 { config_parse_capability_set, "BOUNDINGSET" },
4691 { config_parse_rlimit, "LIMIT" },
4692 { config_parse_unit_deps, "UNIT [...]" },
4693 { config_parse_exec, "PATH [ARGUMENT [...]]" },
4694 { config_parse_service_type, "SERVICETYPE" },
4695 { config_parse_service_restart, "SERVICERESTART" },
4696 { config_parse_kill_mode, "KILLMODE" },
4697 { config_parse_signal, "SIGNAL" },
4698 { config_parse_socket_listen, "SOCKET [...]" },
4699 { config_parse_socket_bind, "SOCKETBIND" },
4700 { config_parse_socket_bindtodevice, "NETWORKINTERFACE" },
4701 { config_parse_sec, "SECONDS" },
4702 { config_parse_nsec, "NANOSECONDS" },
4703 { config_parse_namespace_path_strv, "PATH [...]" },
4704 { config_parse_bind_paths, "PATH[:PATH[:OPTIONS]] [...]" },
4705 { config_parse_unit_requires_mounts_for, "PATH [...]" },
4706 { config_parse_exec_mount_flags, "MOUNTFLAG [...]" },
4707 { config_parse_unit_string_printf, "STRING" },
4708 { config_parse_trigger_unit, "UNIT" },
4709 { config_parse_timer, "TIMER" },
4710 { config_parse_path_spec, "PATH" },
4711 { config_parse_notify_access, "ACCESS" },
4712 { config_parse_ip_tos, "TOS" },
4713 { config_parse_unit_condition_path, "CONDITION" },
4714 { config_parse_unit_condition_string, "CONDITION" },
4715 { config_parse_unit_condition_null, "CONDITION" },
4716 { config_parse_unit_slice, "SLICE" },
4717 { config_parse_documentation, "URL" },
4718 { config_parse_service_timeout, "SECONDS" },
4719 { config_parse_emergency_action, "ACTION" },
4720 { config_parse_set_status, "STATUS" },
4721 { config_parse_service_sockets, "SOCKETS" },
4722 { config_parse_environ, "ENVIRON" },
4723 #if HAVE_SECCOMP
4724 { config_parse_syscall_filter, "SYSCALLS" },
4725 { config_parse_syscall_archs, "ARCHS" },
4726 { config_parse_syscall_errno, "ERRNO" },
4727 { config_parse_address_families, "FAMILIES" },
4728 { config_parse_restrict_namespaces, "NAMESPACES" },
4729 #endif
4730 { config_parse_cpu_shares, "SHARES" },
4731 { config_parse_cg_weight, "WEIGHT" },
4732 { config_parse_memory_limit, "LIMIT" },
4733 { config_parse_device_allow, "DEVICE" },
4734 { config_parse_device_policy, "POLICY" },
4735 { config_parse_io_limit, "LIMIT" },
4736 { config_parse_io_device_weight, "DEVICEWEIGHT" },
4737 { config_parse_io_device_latency, "DEVICELATENCY" },
4738 { config_parse_blockio_bandwidth, "BANDWIDTH" },
4739 { config_parse_blockio_weight, "WEIGHT" },
4740 { config_parse_blockio_device_weight, "DEVICEWEIGHT" },
4741 { config_parse_long, "LONG" },
4742 { config_parse_socket_service, "SERVICE" },
4743 #if HAVE_SELINUX
4744 { config_parse_exec_selinux_context, "LABEL" },
4745 #endif
4746 { config_parse_job_mode, "MODE" },
4747 { config_parse_job_mode_isolate, "BOOLEAN" },
4748 { config_parse_personality, "PERSONALITY" },
4749 };
4750
4751 const char *prev = NULL;
4752 const char *i;
4753
4754 assert(f);
4755
4756 NULSTR_FOREACH(i, load_fragment_gperf_nulstr) {
4757 const char *rvalue = "OTHER", *lvalue;
4758 const ConfigPerfItem *p;
4759 size_t prefix_len;
4760 const char *dot;
4761 unsigned j;
4762
4763 assert_se(p = load_fragment_gperf_lookup(i, strlen(i)));
4764
4765 /* Hide legacy settings */
4766 if (p->parse == config_parse_warn_compat &&
4767 p->ltype == DISABLED_LEGACY)
4768 continue;
4769
4770 for (j = 0; j < ELEMENTSOF(table); j++)
4771 if (p->parse == table[j].callback) {
4772 rvalue = table[j].rvalue;
4773 break;
4774 }
4775
4776 dot = strchr(i, '.');
4777 lvalue = dot ? dot + 1 : i;
4778 prefix_len = dot-i;
4779
4780 if (dot)
4781 if (!prev || !strneq(prev, i, prefix_len+1)) {
4782 if (prev)
4783 fputc('\n', f);
4784
4785 fprintf(f, "[%.*s]\n", (int) prefix_len, i);
4786 }
4787
4788 fprintf(f, "%s=%s\n", lvalue, rvalue);
4789 prev = i;
4790 }
4791 }