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