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