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