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