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