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