]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/load-fragment.c
core: introduce cgroup_add_device_allow()
[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 const char *p = rvalue;
3216 int r;
3217
3218 if (isempty(rvalue)) {
3219 while (c->device_allow)
3220 cgroup_context_free_device_allow(c, c->device_allow);
3221
3222 return 0;
3223 }
3224
3225 r = extract_first_word(&p, &path, NULL, EXTRACT_QUOTES);
3226 if (r == -ENOMEM)
3227 return log_oom();
3228 if (r < 0) {
3229 log_syntax(unit, LOG_WARNING, filename, line, r,
3230 "Invalid syntax, ignoring: %s", rvalue);
3231 return 0;
3232 }
3233 if (r == 0) {
3234 log_syntax(unit, LOG_WARNING, filename, line, 0,
3235 "Failed to extract device path and rights from '%s', ignoring.", rvalue);
3236 return 0;
3237 }
3238
3239 r = unit_full_printf(userdata, path, &resolved);
3240 if (r < 0) {
3241 log_syntax(unit, LOG_WARNING, filename, line, r,
3242 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
3243 return 0;
3244 }
3245
3246 if (!startswith(resolved, "block-") && !startswith(resolved, "char-")) {
3247
3248 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
3249 if (r < 0)
3250 return 0;
3251
3252 if (!valid_device_node_path(resolved)) {
3253 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid device node path '%s', ignoring.", resolved);
3254 return 0;
3255 }
3256 }
3257
3258 if (!isempty(p) && !in_charset(p, "rwm")) {
3259 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid device rights '%s', ignoring.", p);
3260 return 0;
3261 }
3262
3263 return cgroup_add_device_allow(c, resolved, p);
3264 }
3265
3266 int config_parse_io_device_weight(
3267 const char *unit,
3268 const char *filename,
3269 unsigned line,
3270 const char *section,
3271 unsigned section_line,
3272 const char *lvalue,
3273 int ltype,
3274 const char *rvalue,
3275 void *data,
3276 void *userdata) {
3277
3278 _cleanup_free_ char *path = NULL, *resolved = NULL;
3279 CGroupIODeviceWeight *w;
3280 CGroupContext *c = data;
3281 const char *p = rvalue;
3282 uint64_t u;
3283 int r;
3284
3285 assert(filename);
3286 assert(lvalue);
3287 assert(rvalue);
3288
3289 if (isempty(rvalue)) {
3290 while (c->io_device_weights)
3291 cgroup_context_free_io_device_weight(c, c->io_device_weights);
3292
3293 return 0;
3294 }
3295
3296 r = extract_first_word(&p, &path, NULL, EXTRACT_QUOTES);
3297 if (r == -ENOMEM)
3298 return log_oom();
3299 if (r < 0) {
3300 log_syntax(unit, LOG_WARNING, filename, line, r,
3301 "Invalid syntax, ignoring: %s", rvalue);
3302 return 0;
3303 }
3304 if (r == 0 || isempty(p)) {
3305 log_syntax(unit, LOG_WARNING, filename, line, 0,
3306 "Failed to extract device path and weight from '%s', ignoring.", rvalue);
3307 return 0;
3308 }
3309
3310 r = unit_full_printf(userdata, path, &resolved);
3311 if (r < 0) {
3312 log_syntax(unit, LOG_WARNING, filename, line, r,
3313 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
3314 return 0;
3315 }
3316
3317 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
3318 if (r < 0)
3319 return 0;
3320
3321 r = cg_weight_parse(p, &u);
3322 if (r < 0) {
3323 log_syntax(unit, LOG_ERR, filename, line, r, "IO weight '%s' invalid, ignoring: %m", p);
3324 return 0;
3325 }
3326
3327 assert(u != CGROUP_WEIGHT_INVALID);
3328
3329 w = new0(CGroupIODeviceWeight, 1);
3330 if (!w)
3331 return log_oom();
3332
3333 w->path = TAKE_PTR(resolved);
3334 w->weight = u;
3335
3336 LIST_PREPEND(device_weights, c->io_device_weights, w);
3337 return 0;
3338 }
3339
3340 int config_parse_io_limit(
3341 const char *unit,
3342 const char *filename,
3343 unsigned line,
3344 const char *section,
3345 unsigned section_line,
3346 const char *lvalue,
3347 int ltype,
3348 const char *rvalue,
3349 void *data,
3350 void *userdata) {
3351
3352 _cleanup_free_ char *path = NULL, *resolved = NULL;
3353 CGroupIODeviceLimit *l = NULL, *t;
3354 CGroupContext *c = data;
3355 CGroupIOLimitType type;
3356 const char *p = rvalue;
3357 uint64_t num;
3358 int r;
3359
3360 assert(filename);
3361 assert(lvalue);
3362 assert(rvalue);
3363
3364 type = cgroup_io_limit_type_from_string(lvalue);
3365 assert(type >= 0);
3366
3367 if (isempty(rvalue)) {
3368 LIST_FOREACH(device_limits, l, c->io_device_limits)
3369 l->limits[type] = cgroup_io_limit_defaults[type];
3370 return 0;
3371 }
3372
3373 r = extract_first_word(&p, &path, NULL, EXTRACT_QUOTES);
3374 if (r == -ENOMEM)
3375 return log_oom();
3376 if (r < 0) {
3377 log_syntax(unit, LOG_WARNING, filename, line, r,
3378 "Invalid syntax, ignoring: %s", rvalue);
3379 return 0;
3380 }
3381 if (r == 0 || isempty(p)) {
3382 log_syntax(unit, LOG_WARNING, filename, line, 0,
3383 "Failed to extract device node and bandwidth from '%s', ignoring.", rvalue);
3384 return 0;
3385 }
3386
3387 r = unit_full_printf(userdata, path, &resolved);
3388 if (r < 0) {
3389 log_syntax(unit, LOG_WARNING, filename, line, r,
3390 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
3391 return 0;
3392 }
3393
3394 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
3395 if (r < 0)
3396 return 0;
3397
3398 if (streq("infinity", p))
3399 num = CGROUP_LIMIT_MAX;
3400 else {
3401 r = parse_size(p, 1000, &num);
3402 if (r < 0 || num <= 0) {
3403 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid IO limit '%s', ignoring.", p);
3404 return 0;
3405 }
3406 }
3407
3408 LIST_FOREACH(device_limits, t, c->io_device_limits) {
3409 if (path_equal(resolved, t->path)) {
3410 l = t;
3411 break;
3412 }
3413 }
3414
3415 if (!l) {
3416 CGroupIOLimitType ttype;
3417
3418 l = new0(CGroupIODeviceLimit, 1);
3419 if (!l)
3420 return log_oom();
3421
3422 l->path = TAKE_PTR(resolved);
3423 for (ttype = 0; ttype < _CGROUP_IO_LIMIT_TYPE_MAX; ttype++)
3424 l->limits[ttype] = cgroup_io_limit_defaults[ttype];
3425
3426 LIST_PREPEND(device_limits, c->io_device_limits, l);
3427 }
3428
3429 l->limits[type] = num;
3430
3431 return 0;
3432 }
3433
3434 int config_parse_blockio_device_weight(
3435 const char *unit,
3436 const char *filename,
3437 unsigned line,
3438 const char *section,
3439 unsigned section_line,
3440 const char *lvalue,
3441 int ltype,
3442 const char *rvalue,
3443 void *data,
3444 void *userdata) {
3445
3446 _cleanup_free_ char *path = NULL, *resolved = NULL;
3447 CGroupBlockIODeviceWeight *w;
3448 CGroupContext *c = data;
3449 const char *p = rvalue;
3450 uint64_t u;
3451 int r;
3452
3453 assert(filename);
3454 assert(lvalue);
3455 assert(rvalue);
3456
3457 if (isempty(rvalue)) {
3458 while (c->blockio_device_weights)
3459 cgroup_context_free_blockio_device_weight(c, c->blockio_device_weights);
3460
3461 return 0;
3462 }
3463
3464 r = extract_first_word(&p, &path, NULL, EXTRACT_QUOTES);
3465 if (r == -ENOMEM)
3466 return log_oom();
3467 if (r < 0) {
3468 log_syntax(unit, LOG_WARNING, filename, line, r,
3469 "Invalid syntax, ignoring: %s", rvalue);
3470 return 0;
3471 }
3472 if (r == 0 || isempty(p)) {
3473 log_syntax(unit, LOG_WARNING, filename, line, 0,
3474 "Failed to extract device node and weight from '%s', ignoring.", rvalue);
3475 return 0;
3476 }
3477
3478 r = unit_full_printf(userdata, path, &resolved);
3479 if (r < 0) {
3480 log_syntax(unit, LOG_WARNING, filename, line, r,
3481 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
3482 return 0;
3483 }
3484
3485 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
3486 if (r < 0)
3487 return 0;
3488
3489 r = cg_blkio_weight_parse(p, &u);
3490 if (r < 0) {
3491 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid block IO weight '%s', ignoring: %m", p);
3492 return 0;
3493 }
3494
3495 assert(u != CGROUP_BLKIO_WEIGHT_INVALID);
3496
3497 w = new0(CGroupBlockIODeviceWeight, 1);
3498 if (!w)
3499 return log_oom();
3500
3501 w->path = TAKE_PTR(resolved);
3502 w->weight = u;
3503
3504 LIST_PREPEND(device_weights, c->blockio_device_weights, w);
3505 return 0;
3506 }
3507
3508 int config_parse_blockio_bandwidth(
3509 const char *unit,
3510 const char *filename,
3511 unsigned line,
3512 const char *section,
3513 unsigned section_line,
3514 const char *lvalue,
3515 int ltype,
3516 const char *rvalue,
3517 void *data,
3518 void *userdata) {
3519
3520 _cleanup_free_ char *path = NULL, *resolved = NULL;
3521 CGroupBlockIODeviceBandwidth *b = NULL, *t;
3522 CGroupContext *c = data;
3523 const char *p = rvalue;
3524 uint64_t bytes;
3525 bool read;
3526 int r;
3527
3528 assert(filename);
3529 assert(lvalue);
3530 assert(rvalue);
3531
3532 read = streq("BlockIOReadBandwidth", lvalue);
3533
3534 if (isempty(rvalue)) {
3535 LIST_FOREACH(device_bandwidths, b, c->blockio_device_bandwidths) {
3536 b->rbps = CGROUP_LIMIT_MAX;
3537 b->wbps = CGROUP_LIMIT_MAX;
3538 }
3539 return 0;
3540 }
3541
3542 r = extract_first_word(&p, &path, NULL, EXTRACT_QUOTES);
3543 if (r == -ENOMEM)
3544 return log_oom();
3545 if (r < 0) {
3546 log_syntax(unit, LOG_WARNING, filename, line, r,
3547 "Invalid syntax, ignoring: %s", rvalue);
3548 return 0;
3549 }
3550 if (r == 0 || isempty(p)) {
3551 log_syntax(unit, LOG_WARNING, filename, line, 0,
3552 "Failed to extract device node and bandwidth from '%s', ignoring.", rvalue);
3553 return 0;
3554 }
3555
3556 r = unit_full_printf(userdata, path, &resolved);
3557 if (r < 0) {
3558 log_syntax(unit, LOG_WARNING, filename, line, r,
3559 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
3560 return 0;
3561 }
3562
3563 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
3564 if (r < 0)
3565 return 0;
3566
3567 r = parse_size(p, 1000, &bytes);
3568 if (r < 0 || bytes <= 0) {
3569 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid Block IO Bandwidth '%s', ignoring.", p);
3570 return 0;
3571 }
3572
3573 LIST_FOREACH(device_bandwidths, t, c->blockio_device_bandwidths) {
3574 if (path_equal(resolved, t->path)) {
3575 b = t;
3576 break;
3577 }
3578 }
3579
3580 if (!t) {
3581 b = new0(CGroupBlockIODeviceBandwidth, 1);
3582 if (!b)
3583 return log_oom();
3584
3585 b->path = TAKE_PTR(resolved);
3586 b->rbps = CGROUP_LIMIT_MAX;
3587 b->wbps = CGROUP_LIMIT_MAX;
3588
3589 LIST_PREPEND(device_bandwidths, c->blockio_device_bandwidths, b);
3590 }
3591
3592 if (read)
3593 b->rbps = bytes;
3594 else
3595 b->wbps = bytes;
3596
3597 return 0;
3598 }
3599
3600 int config_parse_job_mode_isolate(
3601 const char *unit,
3602 const char *filename,
3603 unsigned line,
3604 const char *section,
3605 unsigned section_line,
3606 const char *lvalue,
3607 int ltype,
3608 const char *rvalue,
3609 void *data,
3610 void *userdata) {
3611
3612 JobMode *m = data;
3613 int r;
3614
3615 assert(filename);
3616 assert(lvalue);
3617 assert(rvalue);
3618
3619 r = parse_boolean(rvalue);
3620 if (r < 0) {
3621 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse boolean, ignoring: %s", rvalue);
3622 return 0;
3623 }
3624
3625 log_notice("%s is deprecated. Please use OnFailureJobMode= instead", lvalue);
3626
3627 *m = r ? JOB_ISOLATE : JOB_REPLACE;
3628 return 0;
3629 }
3630
3631 int config_parse_exec_directories(
3632 const char *unit,
3633 const char *filename,
3634 unsigned line,
3635 const char *section,
3636 unsigned section_line,
3637 const char *lvalue,
3638 int ltype,
3639 const char *rvalue,
3640 void *data,
3641 void *userdata) {
3642
3643 char***rt = data;
3644 Unit *u = userdata;
3645 const char *p;
3646 int r;
3647
3648 assert(filename);
3649 assert(lvalue);
3650 assert(rvalue);
3651 assert(data);
3652
3653 if (isempty(rvalue)) {
3654 /* Empty assignment resets the list */
3655 *rt = strv_free(*rt);
3656 return 0;
3657 }
3658
3659 for (p = rvalue;;) {
3660 _cleanup_free_ char *word = NULL, *k = NULL;
3661
3662 r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES);
3663 if (r == -ENOMEM)
3664 return log_oom();
3665 if (r < 0) {
3666 log_syntax(unit, LOG_WARNING, filename, line, r,
3667 "Invalid syntax, ignoring: %s", rvalue);
3668 return 0;
3669 }
3670 if (r == 0)
3671 return 0;
3672
3673 r = unit_full_printf(u, word, &k);
3674 if (r < 0) {
3675 log_syntax(unit, LOG_ERR, filename, line, r,
3676 "Failed to resolve unit specifiers in \"%s\", ignoring: %m", word);
3677 continue;
3678 }
3679
3680 r = path_simplify_and_warn(k, PATH_CHECK_RELATIVE, unit, filename, line, lvalue);
3681 if (r < 0)
3682 continue;
3683
3684 if (path_startswith(k, "private")) {
3685 log_syntax(unit, LOG_ERR, filename, line, 0,
3686 "%s= path can't be 'private', ingoring assignment: %s", lvalue, word);
3687 continue;
3688 }
3689
3690 r = strv_push(rt, k);
3691 if (r < 0)
3692 return log_oom();
3693 k = NULL;
3694 }
3695 }
3696
3697 int config_parse_set_status(
3698 const char *unit,
3699 const char *filename,
3700 unsigned line,
3701 const char *section,
3702 unsigned section_line,
3703 const char *lvalue,
3704 int ltype,
3705 const char *rvalue,
3706 void *data,
3707 void *userdata) {
3708
3709 size_t l;
3710 const char *word, *state;
3711 int r;
3712 ExitStatusSet *status_set = data;
3713
3714 assert(filename);
3715 assert(lvalue);
3716 assert(rvalue);
3717 assert(data);
3718
3719 /* Empty assignment resets the list */
3720 if (isempty(rvalue)) {
3721 exit_status_set_free(status_set);
3722 return 0;
3723 }
3724
3725 FOREACH_WORD(word, l, rvalue, state) {
3726 _cleanup_free_ char *temp;
3727 int val;
3728 Set **set;
3729
3730 temp = strndup(word, l);
3731 if (!temp)
3732 return log_oom();
3733
3734 r = safe_atoi(temp, &val);
3735 if (r < 0) {
3736 val = signal_from_string(temp);
3737
3738 if (val <= 0) {
3739 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse value, ignoring: %s", word);
3740 continue;
3741 }
3742 set = &status_set->signal;
3743 } else {
3744 if (val < 0 || val > 255) {
3745 log_syntax(unit, LOG_ERR, filename, line, 0, "Value %d is outside range 0-255, ignoring", val);
3746 continue;
3747 }
3748 set = &status_set->status;
3749 }
3750
3751 r = set_ensure_allocated(set, NULL);
3752 if (r < 0)
3753 return log_oom();
3754
3755 r = set_put(*set, INT_TO_PTR(val));
3756 if (r < 0)
3757 return log_oom();
3758 }
3759 if (!isempty(state))
3760 log_syntax(unit, LOG_ERR, filename, line, 0, "Trailing garbage, ignoring.");
3761
3762 return 0;
3763 }
3764
3765 int config_parse_namespace_path_strv(
3766 const char *unit,
3767 const char *filename,
3768 unsigned line,
3769 const char *section,
3770 unsigned section_line,
3771 const char *lvalue,
3772 int ltype,
3773 const char *rvalue,
3774 void *data,
3775 void *userdata) {
3776
3777 Unit *u = userdata;
3778 char*** sv = data;
3779 const char *p = rvalue;
3780 int r;
3781
3782 assert(filename);
3783 assert(lvalue);
3784 assert(rvalue);
3785 assert(data);
3786
3787 if (isempty(rvalue)) {
3788 /* Empty assignment resets the list */
3789 *sv = strv_free(*sv);
3790 return 0;
3791 }
3792
3793 for (;;) {
3794 _cleanup_free_ char *word = NULL, *resolved = NULL, *joined = NULL;
3795 const char *w;
3796 bool ignore_enoent = false, shall_prefix = false;
3797
3798 r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES);
3799 if (r == 0)
3800 break;
3801 if (r == -ENOMEM)
3802 return log_oom();
3803 if (r < 0) {
3804 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to extract first word, ignoring: %s", rvalue);
3805 return 0;
3806 }
3807
3808 w = word;
3809 if (startswith(w, "-")) {
3810 ignore_enoent = true;
3811 w++;
3812 }
3813 if (startswith(w, "+")) {
3814 shall_prefix = true;
3815 w++;
3816 }
3817
3818 r = unit_full_printf(u, w, &resolved);
3819 if (r < 0) {
3820 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s: %m", w);
3821 continue;
3822 }
3823
3824 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
3825 if (r < 0)
3826 continue;
3827
3828 joined = strjoin(ignore_enoent ? "-" : "",
3829 shall_prefix ? "+" : "",
3830 resolved);
3831
3832 r = strv_push(sv, joined);
3833 if (r < 0)
3834 return log_oom();
3835
3836 joined = NULL;
3837 }
3838
3839 return 0;
3840 }
3841
3842 int config_parse_temporary_filesystems(
3843 const char *unit,
3844 const char *filename,
3845 unsigned line,
3846 const char *section,
3847 unsigned section_line,
3848 const char *lvalue,
3849 int ltype,
3850 const char *rvalue,
3851 void *data,
3852 void *userdata) {
3853
3854 Unit *u = userdata;
3855 ExecContext *c = data;
3856 const char *p = rvalue;
3857 int r;
3858
3859 assert(filename);
3860 assert(lvalue);
3861 assert(rvalue);
3862 assert(data);
3863
3864 if (isempty(rvalue)) {
3865 /* Empty assignment resets the list */
3866 temporary_filesystem_free_many(c->temporary_filesystems, c->n_temporary_filesystems);
3867 c->temporary_filesystems = NULL;
3868 c->n_temporary_filesystems = 0;
3869 return 0;
3870 }
3871
3872 for (;;) {
3873 _cleanup_free_ char *word = NULL, *path = NULL, *resolved = NULL;
3874 const char *w;
3875
3876 r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES);
3877 if (r == 0)
3878 return 0;
3879 if (r == -ENOMEM)
3880 return log_oom();
3881 if (r < 0) {
3882 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to extract first word, ignoring: %s", rvalue);
3883 return 0;
3884 }
3885
3886 w = word;
3887 r = extract_first_word(&w, &path, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
3888 if (r == -ENOMEM)
3889 return log_oom();
3890 if (r < 0) {
3891 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to extract first word, ignoring: %s", word);
3892 continue;
3893 }
3894 if (r == 0) {
3895 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid syntax, ignoring: %s", word);
3896 continue;
3897 }
3898
3899 r = unit_full_printf(u, path, &resolved);
3900 if (r < 0) {
3901 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", path);
3902 continue;
3903 }
3904
3905 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
3906 if (r < 0)
3907 continue;
3908
3909 r = temporary_filesystem_add(&c->temporary_filesystems, &c->n_temporary_filesystems, resolved, w);
3910 if (r < 0)
3911 return log_oom();
3912 }
3913 }
3914
3915 int config_parse_bind_paths(
3916 const char *unit,
3917 const char *filename,
3918 unsigned line,
3919 const char *section,
3920 unsigned section_line,
3921 const char *lvalue,
3922 int ltype,
3923 const char *rvalue,
3924 void *data,
3925 void *userdata) {
3926
3927 ExecContext *c = data;
3928 Unit *u = userdata;
3929 const char *p;
3930 int r;
3931
3932 assert(filename);
3933 assert(lvalue);
3934 assert(rvalue);
3935 assert(data);
3936
3937 if (isempty(rvalue)) {
3938 /* Empty assignment resets the list */
3939 bind_mount_free_many(c->bind_mounts, c->n_bind_mounts);
3940 c->bind_mounts = NULL;
3941 c->n_bind_mounts = 0;
3942 return 0;
3943 }
3944
3945 p = rvalue;
3946 for (;;) {
3947 _cleanup_free_ char *source = NULL, *destination = NULL;
3948 _cleanup_free_ char *sresolved = NULL, *dresolved = NULL;
3949 char *s = NULL, *d = NULL;
3950 bool rbind = true, ignore_enoent = false;
3951
3952 r = extract_first_word(&p, &source, ":" WHITESPACE, EXTRACT_QUOTES|EXTRACT_DONT_COALESCE_SEPARATORS);
3953 if (r == 0)
3954 break;
3955 if (r == -ENOMEM)
3956 return log_oom();
3957 if (r < 0) {
3958 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse %s, ignoring: %s", lvalue, rvalue);
3959 return 0;
3960 }
3961
3962 r = unit_full_printf(u, source, &sresolved);
3963 if (r < 0) {
3964 log_syntax(unit, LOG_ERR, filename, line, r,
3965 "Failed to resolved unit specifiers in \"%s\", ignoring: %m", source);
3966 continue;
3967 }
3968
3969 s = sresolved;
3970 if (s[0] == '-') {
3971 ignore_enoent = true;
3972 s++;
3973 }
3974
3975 r = path_simplify_and_warn(s, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
3976 if (r < 0)
3977 continue;
3978
3979 /* Optionally, the destination is specified. */
3980 if (p && p[-1] == ':') {
3981 r = extract_first_word(&p, &destination, ":" WHITESPACE, EXTRACT_QUOTES|EXTRACT_DONT_COALESCE_SEPARATORS);
3982 if (r == -ENOMEM)
3983 return log_oom();
3984 if (r < 0) {
3985 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse %s, ignoring: %s", lvalue, rvalue);
3986 return 0;
3987 }
3988 if (r == 0) {
3989 log_syntax(unit, LOG_ERR, filename, line, 0, "Missing argument after ':', ignoring: %s", s);
3990 continue;
3991 }
3992
3993 r = unit_full_printf(u, destination, &dresolved);
3994 if (r < 0) {
3995 log_syntax(unit, LOG_ERR, filename, line, r,
3996 "Failed to resolved specifiers in \"%s\", ignoring: %m", destination);
3997 continue;
3998 }
3999
4000 r = path_simplify_and_warn(dresolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
4001 if (r < 0)
4002 continue;
4003
4004 d = dresolved;
4005
4006 /* Optionally, there's also a short option string specified */
4007 if (p && p[-1] == ':') {
4008 _cleanup_free_ char *options = NULL;
4009
4010 r = extract_first_word(&p, &options, NULL, EXTRACT_QUOTES);
4011 if (r == -ENOMEM)
4012 return log_oom();
4013 if (r < 0) {
4014 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse %s: %s", lvalue, rvalue);
4015 return 0;
4016 }
4017
4018 if (isempty(options) || streq(options, "rbind"))
4019 rbind = true;
4020 else if (streq(options, "norbind"))
4021 rbind = false;
4022 else {
4023 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid option string, ignoring setting: %s", options);
4024 continue;
4025 }
4026 }
4027 } else
4028 d = s;
4029
4030 r = bind_mount_add(&c->bind_mounts, &c->n_bind_mounts,
4031 &(BindMount) {
4032 .source = s,
4033 .destination = d,
4034 .read_only = !!strstr(lvalue, "ReadOnly"),
4035 .recursive = rbind,
4036 .ignore_enoent = ignore_enoent,
4037 });
4038 if (r < 0)
4039 return log_oom();
4040 }
4041
4042 return 0;
4043 }
4044
4045 int config_parse_job_timeout_sec(
4046 const char* unit,
4047 const char *filename,
4048 unsigned line,
4049 const char *section,
4050 unsigned section_line,
4051 const char *lvalue,
4052 int ltype,
4053 const char *rvalue,
4054 void *data,
4055 void *userdata) {
4056
4057 Unit *u = data;
4058 usec_t usec;
4059 int r;
4060
4061 assert(filename);
4062 assert(lvalue);
4063 assert(rvalue);
4064 assert(u);
4065
4066 r = parse_sec_fix_0(rvalue, &usec);
4067 if (r < 0) {
4068 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse JobTimeoutSec= parameter, ignoring: %s", rvalue);
4069 return 0;
4070 }
4071
4072 /* If the user explicitly changed JobTimeoutSec= also change JobRunningTimeoutSec=, for compatibility with old
4073 * versions. If JobRunningTimeoutSec= was explicitly set, avoid this however as whatever the user picked should
4074 * count. */
4075
4076 if (!u->job_running_timeout_set)
4077 u->job_running_timeout = usec;
4078
4079 u->job_timeout = usec;
4080
4081 return 0;
4082 }
4083
4084 int config_parse_job_running_timeout_sec(
4085 const char* unit,
4086 const char *filename,
4087 unsigned line,
4088 const char *section,
4089 unsigned section_line,
4090 const char *lvalue,
4091 int ltype,
4092 const char *rvalue,
4093 void *data,
4094 void *userdata) {
4095
4096 Unit *u = data;
4097 usec_t usec;
4098 int r;
4099
4100 assert(filename);
4101 assert(lvalue);
4102 assert(rvalue);
4103 assert(u);
4104
4105 r = parse_sec_fix_0(rvalue, &usec);
4106 if (r < 0) {
4107 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse JobRunningTimeoutSec= parameter, ignoring: %s", rvalue);
4108 return 0;
4109 }
4110
4111 u->job_running_timeout = usec;
4112 u->job_running_timeout_set = true;
4113
4114 return 0;
4115 }
4116
4117 #define FOLLOW_MAX 8
4118
4119 static int open_follow(char **filename, FILE **_f, Set *names, char **_final) {
4120 char *id = NULL;
4121 unsigned c = 0;
4122 int fd, r;
4123 FILE *f;
4124
4125 assert(filename);
4126 assert(*filename);
4127 assert(_f);
4128 assert(names);
4129
4130 /* This will update the filename pointer if the loaded file is
4131 * reached by a symlink. The old string will be freed. */
4132
4133 for (;;) {
4134 char *target, *name;
4135
4136 if (c++ >= FOLLOW_MAX)
4137 return -ELOOP;
4138
4139 path_simplify(*filename, false);
4140
4141 /* Add the file name we are currently looking at to
4142 * the names of this unit, but only if it is a valid
4143 * unit name. */
4144 name = basename(*filename);
4145 if (unit_name_is_valid(name, UNIT_NAME_ANY)) {
4146
4147 id = set_get(names, name);
4148 if (!id) {
4149 id = strdup(name);
4150 if (!id)
4151 return -ENOMEM;
4152
4153 r = set_consume(names, id);
4154 if (r < 0)
4155 return r;
4156 }
4157 }
4158
4159 /* Try to open the file name, but don't if its a symlink */
4160 fd = open(*filename, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
4161 if (fd >= 0)
4162 break;
4163
4164 if (errno != ELOOP)
4165 return -errno;
4166
4167 /* Hmm, so this is a symlink. Let's read the name, and follow it manually */
4168 r = readlink_and_make_absolute(*filename, &target);
4169 if (r < 0)
4170 return r;
4171
4172 free_and_replace(*filename, target);
4173 }
4174
4175 f = fdopen(fd, "re");
4176 if (!f) {
4177 safe_close(fd);
4178 return -errno;
4179 }
4180
4181 *_f = f;
4182 *_final = id;
4183
4184 return 0;
4185 }
4186
4187 static int merge_by_names(Unit **u, Set *names, const char *id) {
4188 char *k;
4189 int r;
4190
4191 assert(u);
4192 assert(*u);
4193 assert(names);
4194
4195 /* Let's try to add in all symlink names we found */
4196 while ((k = set_steal_first(names))) {
4197
4198 /* First try to merge in the other name into our
4199 * unit */
4200 r = unit_merge_by_name(*u, k);
4201 if (r < 0) {
4202 Unit *other;
4203
4204 /* Hmm, we couldn't merge the other unit into
4205 * ours? Then let's try it the other way
4206 * round */
4207
4208 /* If the symlink name we are looking at is unit template, then
4209 we must search for instance of this template */
4210 if (unit_name_is_valid(k, UNIT_NAME_TEMPLATE) && (*u)->instance) {
4211 _cleanup_free_ char *instance = NULL;
4212
4213 r = unit_name_replace_instance(k, (*u)->instance, &instance);
4214 if (r < 0)
4215 return r;
4216
4217 other = manager_get_unit((*u)->manager, instance);
4218 } else
4219 other = manager_get_unit((*u)->manager, k);
4220
4221 free(k);
4222
4223 if (other) {
4224 r = unit_merge(other, *u);
4225 if (r >= 0) {
4226 *u = other;
4227 return merge_by_names(u, names, NULL);
4228 }
4229 }
4230
4231 return r;
4232 }
4233
4234 if (id == k)
4235 unit_choose_id(*u, id);
4236
4237 free(k);
4238 }
4239
4240 return 0;
4241 }
4242
4243 static int load_from_path(Unit *u, const char *path) {
4244 _cleanup_set_free_free_ Set *symlink_names = NULL;
4245 _cleanup_fclose_ FILE *f = NULL;
4246 _cleanup_free_ char *filename = NULL;
4247 char *id = NULL;
4248 Unit *merged;
4249 struct stat st;
4250 int r;
4251
4252 assert(u);
4253 assert(path);
4254
4255 symlink_names = set_new(&string_hash_ops);
4256 if (!symlink_names)
4257 return -ENOMEM;
4258
4259 if (path_is_absolute(path)) {
4260
4261 filename = strdup(path);
4262 if (!filename)
4263 return -ENOMEM;
4264
4265 r = open_follow(&filename, &f, symlink_names, &id);
4266 if (r < 0) {
4267 filename = mfree(filename);
4268 if (r != -ENOENT)
4269 return r;
4270 }
4271
4272 } else {
4273 char **p;
4274
4275 STRV_FOREACH(p, u->manager->lookup_paths.search_path) {
4276
4277 /* Instead of opening the path right away, we manually
4278 * follow all symlinks and add their name to our unit
4279 * name set while doing so */
4280 filename = path_make_absolute(path, *p);
4281 if (!filename)
4282 return -ENOMEM;
4283
4284 if (u->manager->unit_path_cache &&
4285 !set_get(u->manager->unit_path_cache, filename))
4286 r = -ENOENT;
4287 else
4288 r = open_follow(&filename, &f, symlink_names, &id);
4289 if (r >= 0)
4290 break;
4291 filename = mfree(filename);
4292
4293 /* ENOENT means that the file is missing or is a dangling symlink.
4294 * ENOTDIR means that one of paths we expect to be is a directory
4295 * is not a directory, we should just ignore that.
4296 * EACCES means that the directory or file permissions are wrong.
4297 */
4298 if (r == -EACCES)
4299 log_debug_errno(r, "Cannot access \"%s\": %m", filename);
4300 else if (!IN_SET(r, -ENOENT, -ENOTDIR))
4301 return r;
4302
4303 /* Empty the symlink names for the next run */
4304 set_clear_free(symlink_names);
4305 }
4306 }
4307
4308 if (!filename)
4309 /* Hmm, no suitable file found? */
4310 return 0;
4311
4312 if (!unit_type_may_alias(u->type) && set_size(symlink_names) > 1) {
4313 log_unit_warning(u, "Unit type of %s does not support alias names, refusing loading via symlink.", u->id);
4314 return -ELOOP;
4315 }
4316
4317 merged = u;
4318 r = merge_by_names(&merged, symlink_names, id);
4319 if (r < 0)
4320 return r;
4321
4322 if (merged != u) {
4323 u->load_state = UNIT_MERGED;
4324 return 0;
4325 }
4326
4327 if (fstat(fileno(f), &st) < 0)
4328 return -errno;
4329
4330 if (null_or_empty(&st)) {
4331 u->load_state = UNIT_MASKED;
4332 u->fragment_mtime = 0;
4333 } else {
4334 u->load_state = UNIT_LOADED;
4335 u->fragment_mtime = timespec_load(&st.st_mtim);
4336
4337 /* Now, parse the file contents */
4338 r = config_parse(u->id, filename, f,
4339 UNIT_VTABLE(u)->sections,
4340 config_item_perf_lookup, load_fragment_gperf_lookup,
4341 CONFIG_PARSE_ALLOW_INCLUDE, u);
4342 if (r < 0)
4343 return r;
4344 }
4345
4346 free_and_replace(u->fragment_path, filename);
4347
4348 if (u->source_path) {
4349 if (stat(u->source_path, &st) >= 0)
4350 u->source_mtime = timespec_load(&st.st_mtim);
4351 else
4352 u->source_mtime = 0;
4353 }
4354
4355 return 0;
4356 }
4357
4358 int unit_load_fragment(Unit *u) {
4359 int r;
4360 Iterator i;
4361 const char *t;
4362
4363 assert(u);
4364 assert(u->load_state == UNIT_STUB);
4365 assert(u->id);
4366
4367 if (u->transient) {
4368 u->load_state = UNIT_LOADED;
4369 return 0;
4370 }
4371
4372 /* First, try to find the unit under its id. We always look
4373 * for unit files in the default directories, to make it easy
4374 * to override things by placing things in /etc/systemd/system */
4375 r = load_from_path(u, u->id);
4376 if (r < 0)
4377 return r;
4378
4379 /* Try to find an alias we can load this with */
4380 if (u->load_state == UNIT_STUB) {
4381 SET_FOREACH(t, u->names, i) {
4382
4383 if (t == u->id)
4384 continue;
4385
4386 r = load_from_path(u, t);
4387 if (r < 0)
4388 return r;
4389
4390 if (u->load_state != UNIT_STUB)
4391 break;
4392 }
4393 }
4394
4395 /* And now, try looking for it under the suggested (originally linked) path */
4396 if (u->load_state == UNIT_STUB && u->fragment_path) {
4397
4398 r = load_from_path(u, u->fragment_path);
4399 if (r < 0)
4400 return r;
4401
4402 if (u->load_state == UNIT_STUB)
4403 /* Hmm, this didn't work? Then let's get rid
4404 * of the fragment path stored for us, so that
4405 * we don't point to an invalid location. */
4406 u->fragment_path = mfree(u->fragment_path);
4407 }
4408
4409 /* Look for a template */
4410 if (u->load_state == UNIT_STUB && u->instance) {
4411 _cleanup_free_ char *k = NULL;
4412
4413 r = unit_name_template(u->id, &k);
4414 if (r < 0)
4415 return r;
4416
4417 r = load_from_path(u, k);
4418 if (r < 0) {
4419 if (r == -ENOEXEC)
4420 log_unit_notice(u, "Unit configuration has fatal error, unit will not be started.");
4421 return r;
4422 }
4423
4424 if (u->load_state == UNIT_STUB) {
4425 SET_FOREACH(t, u->names, i) {
4426 _cleanup_free_ char *z = NULL;
4427
4428 if (t == u->id)
4429 continue;
4430
4431 r = unit_name_template(t, &z);
4432 if (r < 0)
4433 return r;
4434
4435 r = load_from_path(u, z);
4436 if (r < 0)
4437 return r;
4438
4439 if (u->load_state != UNIT_STUB)
4440 break;
4441 }
4442 }
4443 }
4444
4445 return 0;
4446 }
4447
4448 void unit_dump_config_items(FILE *f) {
4449 static const struct {
4450 const ConfigParserCallback callback;
4451 const char *rvalue;
4452 } table[] = {
4453 { config_parse_warn_compat, "NOTSUPPORTED" },
4454 { config_parse_int, "INTEGER" },
4455 { config_parse_unsigned, "UNSIGNED" },
4456 { config_parse_iec_size, "SIZE" },
4457 { config_parse_iec_uint64, "SIZE" },
4458 { config_parse_si_size, "SIZE" },
4459 { config_parse_bool, "BOOLEAN" },
4460 { config_parse_string, "STRING" },
4461 { config_parse_path, "PATH" },
4462 { config_parse_unit_path_printf, "PATH" },
4463 { config_parse_strv, "STRING [...]" },
4464 { config_parse_exec_nice, "NICE" },
4465 { config_parse_exec_oom_score_adjust, "OOMSCOREADJUST" },
4466 { config_parse_exec_io_class, "IOCLASS" },
4467 { config_parse_exec_io_priority, "IOPRIORITY" },
4468 { config_parse_exec_cpu_sched_policy, "CPUSCHEDPOLICY" },
4469 { config_parse_exec_cpu_sched_prio, "CPUSCHEDPRIO" },
4470 { config_parse_exec_cpu_affinity, "CPUAFFINITY" },
4471 { config_parse_mode, "MODE" },
4472 { config_parse_unit_env_file, "FILE" },
4473 { config_parse_exec_output, "OUTPUT" },
4474 { config_parse_exec_input, "INPUT" },
4475 { config_parse_log_facility, "FACILITY" },
4476 { config_parse_log_level, "LEVEL" },
4477 { config_parse_exec_secure_bits, "SECUREBITS" },
4478 { config_parse_capability_set, "BOUNDINGSET" },
4479 { config_parse_rlimit, "LIMIT" },
4480 { config_parse_unit_deps, "UNIT [...]" },
4481 { config_parse_exec, "PATH [ARGUMENT [...]]" },
4482 { config_parse_service_type, "SERVICETYPE" },
4483 { config_parse_service_restart, "SERVICERESTART" },
4484 { config_parse_kill_mode, "KILLMODE" },
4485 { config_parse_signal, "SIGNAL" },
4486 { config_parse_socket_listen, "SOCKET [...]" },
4487 { config_parse_socket_bind, "SOCKETBIND" },
4488 { config_parse_socket_bindtodevice, "NETWORKINTERFACE" },
4489 { config_parse_sec, "SECONDS" },
4490 { config_parse_nsec, "NANOSECONDS" },
4491 { config_parse_namespace_path_strv, "PATH [...]" },
4492 { config_parse_bind_paths, "PATH[:PATH[:OPTIONS]] [...]" },
4493 { config_parse_unit_requires_mounts_for, "PATH [...]" },
4494 { config_parse_exec_mount_flags, "MOUNTFLAG [...]" },
4495 { config_parse_unit_string_printf, "STRING" },
4496 { config_parse_trigger_unit, "UNIT" },
4497 { config_parse_timer, "TIMER" },
4498 { config_parse_path_spec, "PATH" },
4499 { config_parse_notify_access, "ACCESS" },
4500 { config_parse_ip_tos, "TOS" },
4501 { config_parse_unit_condition_path, "CONDITION" },
4502 { config_parse_unit_condition_string, "CONDITION" },
4503 { config_parse_unit_condition_null, "CONDITION" },
4504 { config_parse_unit_slice, "SLICE" },
4505 { config_parse_documentation, "URL" },
4506 { config_parse_service_timeout, "SECONDS" },
4507 { config_parse_emergency_action, "ACTION" },
4508 { config_parse_set_status, "STATUS" },
4509 { config_parse_service_sockets, "SOCKETS" },
4510 { config_parse_environ, "ENVIRON" },
4511 #if HAVE_SECCOMP
4512 { config_parse_syscall_filter, "SYSCALLS" },
4513 { config_parse_syscall_archs, "ARCHS" },
4514 { config_parse_syscall_errno, "ERRNO" },
4515 { config_parse_address_families, "FAMILIES" },
4516 { config_parse_restrict_namespaces, "NAMESPACES" },
4517 #endif
4518 { config_parse_cpu_shares, "SHARES" },
4519 { config_parse_cg_weight, "WEIGHT" },
4520 { config_parse_memory_limit, "LIMIT" },
4521 { config_parse_device_allow, "DEVICE" },
4522 { config_parse_device_policy, "POLICY" },
4523 { config_parse_io_limit, "LIMIT" },
4524 { config_parse_io_device_weight, "DEVICEWEIGHT" },
4525 { config_parse_blockio_bandwidth, "BANDWIDTH" },
4526 { config_parse_blockio_weight, "WEIGHT" },
4527 { config_parse_blockio_device_weight, "DEVICEWEIGHT" },
4528 { config_parse_long, "LONG" },
4529 { config_parse_socket_service, "SERVICE" },
4530 #if HAVE_SELINUX
4531 { config_parse_exec_selinux_context, "LABEL" },
4532 #endif
4533 { config_parse_job_mode, "MODE" },
4534 { config_parse_job_mode_isolate, "BOOLEAN" },
4535 { config_parse_personality, "PERSONALITY" },
4536 };
4537
4538 const char *prev = NULL;
4539 const char *i;
4540
4541 assert(f);
4542
4543 NULSTR_FOREACH(i, load_fragment_gperf_nulstr) {
4544 const char *rvalue = "OTHER", *lvalue;
4545 const ConfigPerfItem *p;
4546 size_t prefix_len;
4547 const char *dot;
4548 unsigned j;
4549
4550 assert_se(p = load_fragment_gperf_lookup(i, strlen(i)));
4551
4552 /* Hide legacy settings */
4553 if (p->parse == config_parse_warn_compat &&
4554 p->ltype == DISABLED_LEGACY)
4555 continue;
4556
4557 for (j = 0; j < ELEMENTSOF(table); j++)
4558 if (p->parse == table[j].callback) {
4559 rvalue = table[j].rvalue;
4560 break;
4561 }
4562
4563 dot = strchr(i, '.');
4564 lvalue = dot ? dot + 1 : i;
4565 prefix_len = dot-i;
4566
4567 if (dot)
4568 if (!prev || !strneq(prev, i, prefix_len+1)) {
4569 if (prev)
4570 fputc('\n', f);
4571
4572 fprintf(f, "[%.*s]\n", (int) prefix_len, i);
4573 }
4574
4575 fprintf(f, "%s=%s\n", lvalue, rvalue);
4576 prev = i;
4577 }
4578 }