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