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