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