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