]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/load-fragment.c
shared/cpu-set-util: remove now-unused CPU_SIZE_TO_NUM()
[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
dc653bf4
JK
1897int config_parse_service_timeout_abort(
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 Service *s = userdata;
1910 int r;
1911
1912 assert(filename);
1913 assert(lvalue);
1914 assert(rvalue);
1915 assert(s);
1916
1917 rvalue += strspn(rvalue, WHITESPACE);
1918 if (isempty(rvalue)) {
1919 s->timeout_abort_set = false;
1920 return 0;
1921 }
1922
1923 r = parse_sec(rvalue, &s->timeout_abort_usec);
1924 if (r < 0) {
1925 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse TimeoutAbortSec= setting, ignoring: %s", rvalue);
1926 return 0;
1927 }
1928
1929 s->timeout_abort_set = true;
1930 return 0;
1931}
1932
89beff89
LP
1933int config_parse_sec_fix_0(
1934 const char *unit,
1935 const char *filename,
1936 unsigned line,
1937 const char *section,
1938 unsigned section_line,
1939 const char *lvalue,
1940 int ltype,
1941 const char *rvalue,
1942 void *data,
1943 void *userdata) {
1944
1945 usec_t *usec = data;
1946 int r;
1947
1948 assert(filename);
1949 assert(lvalue);
1950 assert(rvalue);
1951 assert(usec);
1952
1953 /* This is pretty much like config_parse_sec(), except that this treats a time of 0 as infinity, for
1954 * compatibility with older versions of systemd where 0 instead of infinity was used as indicator to turn off a
1955 * timeout. */
1956
0004f698 1957 r = parse_sec_fix_0(rvalue, usec);
89beff89
LP
1958 if (r < 0) {
1959 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse %s= parameter, ignoring: %s", lvalue, rvalue);
1960 return 0;
1961 }
1962
89beff89
LP
1963 return 0;
1964}
1965
66dccd8d
LP
1966int config_parse_user_group(
1967 const char *unit,
1968 const char *filename,
1969 unsigned line,
1970 const char *section,
1971 unsigned section_line,
1972 const char *lvalue,
1973 int ltype,
1974 const char *rvalue,
1975 void *data,
1976 void *userdata) {
1977
063c4b1a
YW
1978 _cleanup_free_ char *k = NULL;
1979 char **user = data;
66dccd8d
LP
1980 Unit *u = userdata;
1981 int r;
1982
1983 assert(filename);
1984 assert(lvalue);
1985 assert(rvalue);
1986 assert(u);
1987
063c4b1a
YW
1988 if (isempty(rvalue)) {
1989 *user = mfree(*user);
1990 return 0;
1991 }
66dccd8d 1992
063c4b1a
YW
1993 r = unit_full_printf(u, rvalue, &k);
1994 if (r < 0) {
1995 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s: %m", rvalue);
1996 return -ENOEXEC;
66dccd8d
LP
1997 }
1998
063c4b1a
YW
1999 if (!valid_user_group_name_or_id(k)) {
2000 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid user/group name or numeric ID: %s", k);
2001 return -ENOEXEC;
2002 }
66dccd8d 2003
063c4b1a 2004 return free_and_replace(*user, k);
66dccd8d
LP
2005}
2006
2007int config_parse_user_group_strv(
2008 const char *unit,
2009 const char *filename,
2010 unsigned line,
2011 const char *section,
2012 unsigned section_line,
2013 const char *lvalue,
2014 int ltype,
2015 const char *rvalue,
2016 void *data,
2017 void *userdata) {
2018
2019 char ***users = data;
2020 Unit *u = userdata;
063c4b1a 2021 const char *p = rvalue;
66dccd8d
LP
2022 int r;
2023
2024 assert(filename);
2025 assert(lvalue);
2026 assert(rvalue);
2027 assert(u);
2028
2029 if (isempty(rvalue)) {
9f2d41a6 2030 *users = strv_free(*users);
66dccd8d
LP
2031 return 0;
2032 }
2033
66dccd8d
LP
2034 for (;;) {
2035 _cleanup_free_ char *word = NULL, *k = NULL;
2036
9a82ab95 2037 r = extract_first_word(&p, &word, NULL, 0);
66dccd8d
LP
2038 if (r == 0)
2039 break;
2040 if (r == -ENOMEM)
2041 return log_oom();
2042 if (r < 0) {
bb28e684
ZJS
2043 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid syntax: %s", rvalue);
2044 return -ENOEXEC;
66dccd8d
LP
2045 }
2046
2047 r = unit_full_printf(u, word, &k);
2048 if (r < 0) {
bb28e684
ZJS
2049 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s: %m", word);
2050 return -ENOEXEC;
66dccd8d
LP
2051 }
2052
2053 if (!valid_user_group_name_or_id(k)) {
bb28e684
ZJS
2054 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid user/group name or numeric ID: %s", k);
2055 return -ENOEXEC;
66dccd8d
LP
2056 }
2057
2058 r = strv_push(users, k);
2059 if (r < 0)
2060 return log_oom();
2061
2062 k = NULL;
2063 }
2064
2065 return 0;
2066}
2067
5f5d8eab
LP
2068int config_parse_working_directory(
2069 const char *unit,
2070 const char *filename,
2071 unsigned line,
2072 const char *section,
2073 unsigned section_line,
2074 const char *lvalue,
2075 int ltype,
2076 const char *rvalue,
2077 void *data,
2078 void *userdata) {
2079
2080 ExecContext *c = data;
2081 Unit *u = userdata;
2082 bool missing_ok;
2083 int r;
2084
2085 assert(filename);
2086 assert(lvalue);
2087 assert(rvalue);
2088 assert(c);
2089 assert(u);
2090
862fcffd
YW
2091 if (isempty(rvalue)) {
2092 c->working_directory_home = false;
2093 c->working_directory = mfree(c->working_directory);
2094 return 0;
2095 }
2096
5f5d8eab
LP
2097 if (rvalue[0] == '-') {
2098 missing_ok = true;
2099 rvalue++;
2100 } else
2101 missing_ok = false;
2102
2103 if (streq(rvalue, "~")) {
2104 c->working_directory_home = true;
2105 c->working_directory = mfree(c->working_directory);
2106 } else {
2107 _cleanup_free_ char *k = NULL;
2108
2109 r = unit_full_printf(u, rvalue, &k);
2110 if (r < 0) {
bb28e684
ZJS
2111 log_syntax(unit, LOG_ERR, filename, line, r,
2112 "Failed to resolve unit specifiers in working directory path '%s'%s: %m",
2113 rvalue, missing_ok ? ", ignoring" : "");
2114 return missing_ok ? 0 : -ENOEXEC;
5f5d8eab
LP
2115 }
2116
2f4d31c1
YW
2117 r = path_simplify_and_warn(k, PATH_CHECK_ABSOLUTE | (missing_ok ? 0 : PATH_CHECK_FATAL), unit, filename, line, lvalue);
2118 if (r < 0)
bb28e684 2119 return missing_ok ? 0 : -ENOEXEC;
5f5d8eab 2120
5f5d8eab 2121 c->working_directory_home = false;
bb28e684 2122 free_and_replace(c->working_directory, k);
5f5d8eab
LP
2123 }
2124
2125 c->working_directory_missing_ok = missing_ok;
2126 return 0;
2127}
2128
e8e581bf
ZJS
2129int config_parse_unit_env_file(const char *unit,
2130 const char *filename,
2131 unsigned line,
2132 const char *section,
71a61510 2133 unsigned section_line,
e8e581bf
ZJS
2134 const char *lvalue,
2135 int ltype,
2136 const char *rvalue,
2137 void *data,
2138 void *userdata) {
ddb26e18 2139
853b8397 2140 char ***env = data;
8fef7659 2141 Unit *u = userdata;
19f6d710 2142 _cleanup_free_ char *n = NULL;
853b8397 2143 int r;
ddb26e18
LP
2144
2145 assert(filename);
2146 assert(lvalue);
2147 assert(rvalue);
2148 assert(data);
2149
74051b9b
LP
2150 if (isempty(rvalue)) {
2151 /* Empty assignment frees the list */
6796073e 2152 *env = strv_free(*env);
74051b9b
LP
2153 return 0;
2154 }
2155
19f6d710 2156 r = unit_full_printf(u, rvalue, &n);
12ca818f 2157 if (r < 0) {
063c4b1a 2158 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
12ca818f
LP
2159 return 0;
2160 }
8fef7659 2161
2f4d31c1
YW
2162 r = path_simplify_and_warn(n[0] == '-' ? n + 1 : n, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
2163 if (r < 0)
afe4bfe2 2164 return 0;
afe4bfe2 2165
2f4d31c1 2166 r = strv_push(env, n);
853b8397
LP
2167 if (r < 0)
2168 return log_oom();
2169
2f4d31c1
YW
2170 n = NULL;
2171
853b8397
LP
2172 return 0;
2173}
2174
f7f3f5c3
LP
2175int config_parse_environ(
2176 const char *unit,
2177 const char *filename,
2178 unsigned line,
2179 const char *section,
2180 unsigned section_line,
2181 const char *lvalue,
2182 int ltype,
2183 const char *rvalue,
2184 void *data,
2185 void *userdata) {
853b8397
LP
2186
2187 Unit *u = userdata;
035fe294
ZJS
2188 char ***env = data;
2189 const char *p;
19f6d710 2190 int r;
853b8397
LP
2191
2192 assert(filename);
2193 assert(lvalue);
2194 assert(rvalue);
97d0e5f8 2195 assert(data);
853b8397
LP
2196
2197 if (isempty(rvalue)) {
2198 /* Empty assignment resets the list */
6796073e 2199 *env = strv_free(*env);
853b8397
LP
2200 return 0;
2201 }
2202
035fe294
ZJS
2203 for (p = rvalue;; ) {
2204 _cleanup_free_ char *word = NULL, *k = NULL;
035fe294
ZJS
2205
2206 r = extract_first_word(&p, &word, NULL, EXTRACT_CUNESCAPE|EXTRACT_QUOTES);
2207 if (r == 0)
2208 return 0;
2209 if (r == -ENOMEM)
2210 return log_oom();
12ca818f 2211 if (r < 0) {
035fe294
ZJS
2212 log_syntax(unit, LOG_WARNING, filename, line, r,
2213 "Invalid syntax, ignoring: %s", rvalue);
12ca818f
LP
2214 return 0;
2215 }
97d0e5f8 2216
035fe294
ZJS
2217 if (u) {
2218 r = unit_full_printf(u, word, &k);
2219 if (r < 0) {
2220 log_syntax(unit, LOG_ERR, filename, line, r,
063c4b1a 2221 "Failed to resolve unit specifiers in %s, ignoring: %m", word);
035fe294
ZJS
2222 continue;
2223 }
ae2a15bc
LP
2224 } else
2225 k = TAKE_PTR(word);
853b8397 2226
035fe294
ZJS
2227 if (!env_assignment_is_valid(k)) {
2228 log_syntax(unit, LOG_ERR, filename, line, 0,
2229 "Invalid environment assignment, ignoring: %s", k);
853b8397
LP
2230 continue;
2231 }
2232
54ac3494
ZJS
2233 r = strv_env_replace(env, k);
2234 if (r < 0)
853b8397 2235 return log_oom();
f7f3f5c3 2236
54ac3494 2237 k = NULL;
853b8397 2238 }
ddb26e18
LP
2239}
2240
00819cc1
LP
2241int config_parse_pass_environ(
2242 const char *unit,
2243 const char *filename,
2244 unsigned line,
2245 const char *section,
2246 unsigned section_line,
2247 const char *lvalue,
2248 int ltype,
2249 const char *rvalue,
2250 void *data,
2251 void *userdata) {
b4c14404 2252
b4c14404
FB
2253 _cleanup_strv_free_ char **n = NULL;
2254 size_t nlen = 0, nbufsize = 0;
41de9cc2 2255 char*** passenv = data;
063c4b1a 2256 const char *p = rvalue;
41de9cc2 2257 Unit *u = userdata;
b4c14404
FB
2258 int r;
2259
2260 assert(filename);
2261 assert(lvalue);
2262 assert(rvalue);
2263 assert(data);
2264
2265 if (isempty(rvalue)) {
2266 /* Empty assignment resets the list */
2267 *passenv = strv_free(*passenv);
2268 return 0;
2269 }
2270
2271 for (;;) {
41de9cc2 2272 _cleanup_free_ char *word = NULL, *k = NULL;
b4c14404 2273
063c4b1a 2274 r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES);
b4c14404
FB
2275 if (r == 0)
2276 break;
2277 if (r == -ENOMEM)
2278 return log_oom();
2279 if (r < 0) {
2280 log_syntax(unit, LOG_ERR, filename, line, r,
063c4b1a 2281 "Trailing garbage in %s, ignoring: %s", lvalue, rvalue);
b4c14404
FB
2282 break;
2283 }
2284
41de9cc2
LP
2285 if (u) {
2286 r = unit_full_printf(u, word, &k);
2287 if (r < 0) {
2288 log_syntax(unit, LOG_ERR, filename, line, r,
063c4b1a 2289 "Failed to resolve specifiers in %s, ignoring: %m", word);
41de9cc2
LP
2290 continue;
2291 }
ae2a15bc
LP
2292 } else
2293 k = TAKE_PTR(word);
41de9cc2
LP
2294
2295 if (!env_name_is_valid(k)) {
2296 log_syntax(unit, LOG_ERR, filename, line, 0,
2297 "Invalid environment name for %s, ignoring: %s", lvalue, k);
b4c14404
FB
2298 continue;
2299 }
2300
2301 if (!GREEDY_REALLOC(n, nbufsize, nlen + 2))
2302 return log_oom();
41de9cc2 2303
1cc6c93a 2304 n[nlen++] = TAKE_PTR(k);
b4c14404 2305 n[nlen] = NULL;
b4c14404
FB
2306 }
2307
2308 if (n) {
2309 r = strv_extend_strv(passenv, n, true);
2310 if (r < 0)
2311 return r;
2312 }
2313
2314 return 0;
2315}
2316
00819cc1
LP
2317int config_parse_unset_environ(
2318 const char *unit,
2319 const char *filename,
2320 unsigned line,
2321 const char *section,
2322 unsigned section_line,
2323 const char *lvalue,
2324 int ltype,
2325 const char *rvalue,
2326 void *data,
2327 void *userdata) {
2328
2329 _cleanup_strv_free_ char **n = NULL;
00819cc1
LP
2330 size_t nlen = 0, nbufsize = 0;
2331 char*** unsetenv = data;
063c4b1a 2332 const char *p = rvalue;
00819cc1
LP
2333 Unit *u = userdata;
2334 int r;
2335
2336 assert(filename);
2337 assert(lvalue);
2338 assert(rvalue);
2339 assert(data);
2340
2341 if (isempty(rvalue)) {
2342 /* Empty assignment resets the list */
2343 *unsetenv = strv_free(*unsetenv);
2344 return 0;
2345 }
2346
2347 for (;;) {
2348 _cleanup_free_ char *word = NULL, *k = NULL;
2349
063c4b1a 2350 r = extract_first_word(&p, &word, NULL, EXTRACT_CUNESCAPE|EXTRACT_QUOTES);
00819cc1
LP
2351 if (r == 0)
2352 break;
2353 if (r == -ENOMEM)
2354 return log_oom();
2355 if (r < 0) {
2356 log_syntax(unit, LOG_ERR, filename, line, r,
063c4b1a 2357 "Trailing garbage in %s, ignoring: %s", lvalue, rvalue);
00819cc1
LP
2358 break;
2359 }
2360
2361 if (u) {
2362 r = unit_full_printf(u, word, &k);
2363 if (r < 0) {
2364 log_syntax(unit, LOG_ERR, filename, line, r,
063c4b1a 2365 "Failed to resolve unit specifiers in %s, ignoring: %m", word);
00819cc1
LP
2366 continue;
2367 }
ae2a15bc
LP
2368 } else
2369 k = TAKE_PTR(word);
00819cc1
LP
2370
2371 if (!env_assignment_is_valid(k) && !env_name_is_valid(k)) {
2372 log_syntax(unit, LOG_ERR, filename, line, 0,
2373 "Invalid environment name or assignment %s, ignoring: %s", lvalue, k);
2374 continue;
2375 }
2376
2377 if (!GREEDY_REALLOC(n, nbufsize, nlen + 2))
2378 return log_oom();
2379
1cc6c93a 2380 n[nlen++] = TAKE_PTR(k);
00819cc1 2381 n[nlen] = NULL;
00819cc1
LP
2382 }
2383
2384 if (n) {
2385 r = strv_extend_strv(unsetenv, n, true);
2386 if (r < 0)
2387 return r;
2388 }
2389
2390 return 0;
2391}
2392
d3070fbd
LP
2393int config_parse_log_extra_fields(
2394 const char *unit,
2395 const char *filename,
2396 unsigned line,
2397 const char *section,
2398 unsigned section_line,
2399 const char *lvalue,
2400 int ltype,
2401 const char *rvalue,
2402 void *data,
2403 void *userdata) {
2404
2405 ExecContext *c = data;
2406 Unit *u = userdata;
063c4b1a 2407 const char *p = rvalue;
d3070fbd
LP
2408 int r;
2409
2410 assert(filename);
2411 assert(lvalue);
2412 assert(rvalue);
2413 assert(c);
2414
2415 if (isempty(rvalue)) {
2416 exec_context_free_log_extra_fields(c);
2417 return 0;
2418 }
2419
063c4b1a 2420 for (;;) {
d3070fbd
LP
2421 _cleanup_free_ char *word = NULL, *k = NULL;
2422 struct iovec *t;
2423 const char *eq;
2424
2425 r = extract_first_word(&p, &word, NULL, EXTRACT_CUNESCAPE|EXTRACT_QUOTES);
2426 if (r == 0)
063c4b1a 2427 return 0;
d3070fbd
LP
2428 if (r == -ENOMEM)
2429 return log_oom();
2430 if (r < 0) {
2431 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
2432 return 0;
2433 }
2434
2435 r = unit_full_printf(u, word, &k);
2436 if (r < 0) {
063c4b1a 2437 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", word);
d3070fbd
LP
2438 continue;
2439 }
2440
2441 eq = strchr(k, '=');
2442 if (!eq) {
063c4b1a 2443 log_syntax(unit, LOG_ERR, filename, line, 0, "Log field lacks '=' character, ignoring: %s", k);
d3070fbd
LP
2444 continue;
2445 }
2446
2447 if (!journal_field_valid(k, eq-k, false)) {
063c4b1a 2448 log_syntax(unit, LOG_ERR, filename, line, 0, "Log field name is invalid, ignoring: %s", k);
d3070fbd
LP
2449 continue;
2450 }
2451
aa484f35 2452 t = reallocarray(c->log_extra_fields, c->n_log_extra_fields+1, sizeof(struct iovec));
d3070fbd
LP
2453 if (!t)
2454 return log_oom();
2455
2456 c->log_extra_fields = t;
2457 c->log_extra_fields[c->n_log_extra_fields++] = IOVEC_MAKE_STRING(k);
2458
2459 k = NULL;
2460 }
d3070fbd
LP
2461}
2462
59fccdc5
LP
2463int config_parse_unit_condition_path(
2464 const char *unit,
2465 const char *filename,
2466 unsigned line,
2467 const char *section,
2468 unsigned section_line,
2469 const char *lvalue,
2470 int ltype,
2471 const char *rvalue,
2472 void *data,
2473 void *userdata) {
52661efd 2474
2fbe635a 2475 _cleanup_free_ char *p = NULL;
59fccdc5
LP
2476 Condition **list = data, *c;
2477 ConditionType t = ltype;
2478 bool trigger, negate;
2479 Unit *u = userdata;
19f6d710 2480 int r;
52661efd
LP
2481
2482 assert(filename);
2483 assert(lvalue);
2484 assert(rvalue);
2485 assert(data);
2486
74051b9b
LP
2487 if (isempty(rvalue)) {
2488 /* Empty assignment resets the list */
447021aa 2489 *list = condition_free_list(*list);
74051b9b
LP
2490 return 0;
2491 }
2492
ab7f148f
LP
2493 trigger = rvalue[0] == '|';
2494 if (trigger)
267632f0
LP
2495 rvalue++;
2496
ab7f148f
LP
2497 negate = rvalue[0] == '!';
2498 if (negate)
52661efd
LP
2499 rvalue++;
2500
19f6d710 2501 r = unit_full_printf(u, rvalue, &p);
59fccdc5 2502 if (r < 0) {
063c4b1a 2503 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue);
59fccdc5 2504 return 0;
19f6d710 2505 }
095b2d7a 2506
2f4d31c1
YW
2507 r = path_simplify_and_warn(p, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
2508 if (r < 0)
52661efd 2509 return 0;
52661efd 2510
59fccdc5 2511 c = condition_new(t, p, trigger, negate);
ab7f148f 2512 if (!c)
74051b9b 2513 return log_oom();
52661efd 2514
59fccdc5 2515 LIST_PREPEND(conditions, *list, c);
52661efd
LP
2516 return 0;
2517}
2518
59fccdc5
LP
2519int config_parse_unit_condition_string(
2520 const char *unit,
2521 const char *filename,
2522 unsigned line,
2523 const char *section,
2524 unsigned section_line,
2525 const char *lvalue,
2526 int ltype,
2527 const char *rvalue,
2528 void *data,
2529 void *userdata) {
039655a4 2530
2fbe635a 2531 _cleanup_free_ char *s = NULL;
59fccdc5
LP
2532 Condition **list = data, *c;
2533 ConditionType t = ltype;
2534 bool trigger, negate;
2535 Unit *u = userdata;
19f6d710 2536 int r;
039655a4
LP
2537
2538 assert(filename);
2539 assert(lvalue);
2540 assert(rvalue);
2541 assert(data);
2542
74051b9b
LP
2543 if (isempty(rvalue)) {
2544 /* Empty assignment resets the list */
447021aa 2545 *list = condition_free_list(*list);
74051b9b
LP
2546 return 0;
2547 }
2548
c0d6e764
LP
2549 trigger = rvalue[0] == '|';
2550 if (trigger)
267632f0
LP
2551 rvalue++;
2552
c0d6e764
LP
2553 negate = rvalue[0] == '!';
2554 if (negate)
039655a4
LP
2555 rvalue++;
2556
19f6d710 2557 r = unit_full_printf(u, rvalue, &s);
59fccdc5 2558 if (r < 0) {
063c4b1a 2559 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
59fccdc5 2560 return 0;
19f6d710 2561 }
095b2d7a 2562
59fccdc5 2563 c = condition_new(t, s, trigger, negate);
c0d6e764
LP
2564 if (!c)
2565 return log_oom();
039655a4 2566
59fccdc5 2567 LIST_PREPEND(conditions, *list, c);
039655a4
LP
2568 return 0;
2569}
2570
59fccdc5
LP
2571int config_parse_unit_condition_null(
2572 const char *unit,
2573 const char *filename,
2574 unsigned line,
2575 const char *section,
2576 unsigned section_line,
2577 const char *lvalue,
2578 int ltype,
2579 const char *rvalue,
2580 void *data,
2581 void *userdata) {
d257ddef 2582
59fccdc5 2583 Condition **list = data, *c;
267632f0 2584 bool trigger, negate;
d257ddef
LP
2585 int b;
2586
2587 assert(filename);
2588 assert(lvalue);
2589 assert(rvalue);
2590 assert(data);
2591
55dadc5c
LP
2592 log_syntax(unit, LOG_WARNING, filename, line, 0, "%s= is deprecated, please do not use.", lvalue);
2593
74051b9b
LP
2594 if (isempty(rvalue)) {
2595 /* Empty assignment resets the list */
447021aa 2596 *list = condition_free_list(*list);
74051b9b
LP
2597 return 0;
2598 }
2599
2600 trigger = rvalue[0] == '|';
2601 if (trigger)
267632f0
LP
2602 rvalue++;
2603
74051b9b
LP
2604 negate = rvalue[0] == '!';
2605 if (negate)
d257ddef
LP
2606 rvalue++;
2607
74051b9b
LP
2608 b = parse_boolean(rvalue);
2609 if (b < 0) {
12ca818f 2610 log_syntax(unit, LOG_ERR, filename, line, b, "Failed to parse boolean value in condition, ignoring: %s", rvalue);
d257ddef
LP
2611 return 0;
2612 }
2613
2614 if (!b)
2615 negate = !negate;
2616
74051b9b
LP
2617 c = condition_new(CONDITION_NULL, NULL, trigger, negate);
2618 if (!c)
2619 return log_oom();
d257ddef 2620
59fccdc5 2621 LIST_PREPEND(conditions, *list, c);
d257ddef
LP
2622 return 0;
2623}
2624
a57f7e2c
LP
2625int config_parse_unit_requires_mounts_for(
2626 const char *unit,
2627 const char *filename,
2628 unsigned line,
2629 const char *section,
71a61510 2630 unsigned section_line,
a57f7e2c
LP
2631 const char *lvalue,
2632 int ltype,
2633 const char *rvalue,
2634 void *data,
2635 void *userdata) {
7c8fa05c 2636
063c4b1a 2637 const char *p = rvalue;
7c8fa05c 2638 Unit *u = userdata;
035fe294 2639 int r;
7c8fa05c
LP
2640
2641 assert(filename);
2642 assert(lvalue);
2643 assert(rvalue);
2644 assert(data);
2645
063c4b1a 2646 for (;;) {
744bb5b1 2647 _cleanup_free_ char *word = NULL, *resolved = NULL;
a57f7e2c 2648
035fe294
ZJS
2649 r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES);
2650 if (r == 0)
2651 return 0;
2652 if (r == -ENOMEM)
a57f7e2c 2653 return log_oom();
035fe294
ZJS
2654 if (r < 0) {
2655 log_syntax(unit, LOG_WARNING, filename, line, r,
2656 "Invalid syntax, ignoring: %s", rvalue);
2657 return 0;
2658 }
7c8fa05c 2659
744bb5b1
LP
2660 r = unit_full_printf(u, word, &resolved);
2661 if (r < 0) {
063c4b1a 2662 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", word);
744bb5b1
LP
2663 continue;
2664 }
2665
2f4d31c1
YW
2666 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
2667 if (r < 0)
2668 continue;
2669
eef85c4a 2670 r = unit_require_mounts_for(u, resolved, UNIT_DEPENDENCY_FILE);
a57f7e2c 2671 if (r < 0) {
063c4b1a 2672 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to add required mount '%s', ignoring: %m", resolved);
a57f7e2c
LP
2673 continue;
2674 }
2675 }
7c8fa05c 2676}
9e372868 2677
e8e581bf
ZJS
2678int config_parse_documentation(const char *unit,
2679 const char *filename,
2680 unsigned line,
2681 const char *section,
71a61510 2682 unsigned section_line,
e8e581bf
ZJS
2683 const char *lvalue,
2684 int ltype,
2685 const char *rvalue,
2686 void *data,
2687 void *userdata) {
49dbfa7b
LP
2688
2689 Unit *u = userdata;
2690 int r;
2691 char **a, **b;
2692
2693 assert(filename);
2694 assert(lvalue);
2695 assert(rvalue);
2696 assert(u);
2697
74051b9b
LP
2698 if (isempty(rvalue)) {
2699 /* Empty assignment resets the list */
6796073e 2700 u->documentation = strv_free(u->documentation);
74051b9b
LP
2701 return 0;
2702 }
2703
71a61510 2704 r = config_parse_unit_strv_printf(unit, filename, line, section, section_line, lvalue, ltype,
e8e581bf 2705 rvalue, data, userdata);
49dbfa7b
LP
2706 if (r < 0)
2707 return r;
2708
2709 for (a = b = u->documentation; a && *a; a++) {
2710
a2e03378 2711 if (documentation_url_is_valid(*a))
49dbfa7b
LP
2712 *(b++) = *a;
2713 else {
12ca818f 2714 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid URL, ignoring: %s", *a);
49dbfa7b
LP
2715 free(*a);
2716 }
2717 }
f6d2d421
ZJS
2718 if (b)
2719 *b = NULL;
49dbfa7b
LP
2720
2721 return r;
2722}
2723
349cc4a5 2724#if HAVE_SECCOMP
17df7223
LP
2725int config_parse_syscall_filter(
2726 const char *unit,
2727 const char *filename,
2728 unsigned line,
2729 const char *section,
2730 unsigned section_line,
2731 const char *lvalue,
2732 int ltype,
2733 const char *rvalue,
2734 void *data,
2735 void *userdata) {
2736
8351ceae
LP
2737 ExecContext *c = data;
2738 Unit *u = userdata;
b5fb3789 2739 bool invert = false;
8130926d 2740 const char *p;
17df7223 2741 int r;
8351ceae
LP
2742
2743 assert(filename);
2744 assert(lvalue);
2745 assert(rvalue);
2746 assert(u);
2747
74051b9b
LP
2748 if (isempty(rvalue)) {
2749 /* Empty assignment resets the list */
8cfa775f 2750 c->syscall_filter = hashmap_free(c->syscall_filter);
17df7223 2751 c->syscall_whitelist = false;
74051b9b
LP
2752 return 0;
2753 }
2754
8351ceae
LP
2755 if (rvalue[0] == '~') {
2756 invert = true;
2757 rvalue++;
2758 }
2759
17df7223 2760 if (!c->syscall_filter) {
8cfa775f 2761 c->syscall_filter = hashmap_new(NULL);
17df7223
LP
2762 if (!c->syscall_filter)
2763 return log_oom();
2764
c0467cf3 2765 if (invert)
17df7223
LP
2766 /* Allow everything but the ones listed */
2767 c->syscall_whitelist = false;
c0467cf3 2768 else {
17df7223
LP
2769 /* Allow nothing but the ones listed */
2770 c->syscall_whitelist = true;
8351ceae 2771
17df7223 2772 /* Accept default syscalls if we are on a whitelist */
2f6b9110
LP
2773 r = seccomp_parse_syscall_filter(
2774 "@default", -1, c->syscall_filter,
58f6ab44
ZJS
2775 SECCOMP_PARSE_PERMISSIVE|SECCOMP_PARSE_WHITELIST,
2776 unit,
2777 NULL, 0);
201c1cc2
TM
2778 if (r < 0)
2779 return r;
c0467cf3 2780 }
8351ceae
LP
2781 }
2782
8130926d
LP
2783 p = rvalue;
2784 for (;;) {
8cfa775f
YW
2785 _cleanup_free_ char *word = NULL, *name = NULL;
2786 int num;
8351ceae 2787
8130926d
LP
2788 r = extract_first_word(&p, &word, NULL, 0);
2789 if (r == 0)
063c4b1a 2790 return 0;
8130926d 2791 if (r == -ENOMEM)
74051b9b 2792 return log_oom();
8130926d
LP
2793 if (r < 0) {
2794 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
063c4b1a 2795 return 0;
8130926d 2796 }
8351ceae 2797
8cfa775f
YW
2798 r = parse_syscall_and_errno(word, &name, &num);
2799 if (r < 0) {
2800 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse syscall:errno, ignoring: %s", word);
2801 continue;
2802 }
2803
58f6ab44 2804 r = seccomp_parse_syscall_filter(
acd142af
LP
2805 name, num, c->syscall_filter,
2806 SECCOMP_PARSE_LOG|SECCOMP_PARSE_PERMISSIVE|
2807 (invert ? SECCOMP_PARSE_INVERT : 0)|
2808 (c->syscall_whitelist ? SECCOMP_PARSE_WHITELIST : 0),
2809 unit, filename, line);
201c1cc2
TM
2810 if (r < 0)
2811 return r;
c0467cf3 2812 }
17df7223
LP
2813}
2814
57183d11
LP
2815int config_parse_syscall_archs(
2816 const char *unit,
2817 const char *filename,
2818 unsigned line,
2819 const char *section,
2820 unsigned section_line,
2821 const char *lvalue,
2822 int ltype,
2823 const char *rvalue,
2824 void *data,
2825 void *userdata) {
2826
063c4b1a 2827 const char *p = rvalue;
d3b1c508 2828 Set **archs = data;
57183d11
LP
2829 int r;
2830
2831 if (isempty(rvalue)) {
525d3cc7 2832 *archs = set_free(*archs);
57183d11
LP
2833 return 0;
2834 }
2835
d5099efc 2836 r = set_ensure_allocated(archs, NULL);
57183d11
LP
2837 if (r < 0)
2838 return log_oom();
2839
063c4b1a 2840 for (;;) {
035fe294 2841 _cleanup_free_ char *word = NULL;
57183d11
LP
2842 uint32_t a;
2843
035fe294
ZJS
2844 r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES);
2845 if (r == 0)
2846 return 0;
2847 if (r == -ENOMEM)
57183d11 2848 return log_oom();
035fe294
ZJS
2849 if (r < 0) {
2850 log_syntax(unit, LOG_WARNING, filename, line, r,
2851 "Invalid syntax, ignoring: %s", rvalue);
2852 return 0;
2853 }
57183d11 2854
035fe294 2855 r = seccomp_arch_from_string(word, &a);
57183d11 2856 if (r < 0) {
035fe294
ZJS
2857 log_syntax(unit, LOG_ERR, filename, line, r,
2858 "Failed to parse system call architecture \"%s\", ignoring: %m", word);
57183d11
LP
2859 continue;
2860 }
2861
d3b1c508 2862 r = set_put(*archs, UINT32_TO_PTR(a + 1));
57183d11
LP
2863 if (r < 0)
2864 return log_oom();
2865 }
57183d11
LP
2866}
2867
17df7223
LP
2868int config_parse_syscall_errno(
2869 const char *unit,
2870 const char *filename,
2871 unsigned line,
2872 const char *section,
2873 unsigned section_line,
2874 const char *lvalue,
2875 int ltype,
2876 const char *rvalue,
2877 void *data,
2878 void *userdata) {
2879
2880 ExecContext *c = data;
2881 int e;
2882
2883 assert(filename);
2884 assert(lvalue);
2885 assert(rvalue);
2886
2887 if (isempty(rvalue)) {
2888 /* Empty assignment resets to KILL */
2889 c->syscall_errno = 0;
2890 return 0;
8351ceae
LP
2891 }
2892
3df90f24
YW
2893 e = parse_errno(rvalue);
2894 if (e <= 0) {
12ca818f 2895 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse error number, ignoring: %s", rvalue);
17df7223
LP
2896 return 0;
2897 }
8351ceae 2898
17df7223 2899 c->syscall_errno = e;
8351ceae
LP
2900 return 0;
2901}
4298d0b5
LP
2902
2903int config_parse_address_families(
2904 const char *unit,
2905 const char *filename,
2906 unsigned line,
2907 const char *section,
2908 unsigned section_line,
2909 const char *lvalue,
2910 int ltype,
2911 const char *rvalue,
2912 void *data,
2913 void *userdata) {
2914
2915 ExecContext *c = data;
4298d0b5 2916 bool invert = false;
035fe294 2917 const char *p;
4298d0b5
LP
2918 int r;
2919
2920 assert(filename);
2921 assert(lvalue);
2922 assert(rvalue);
4298d0b5
LP
2923
2924 if (isempty(rvalue)) {
2925 /* Empty assignment resets the list */
525d3cc7 2926 c->address_families = set_free(c->address_families);
4298d0b5
LP
2927 c->address_families_whitelist = false;
2928 return 0;
2929 }
2930
2931 if (rvalue[0] == '~') {
2932 invert = true;
2933 rvalue++;
2934 }
2935
2936 if (!c->address_families) {
d5099efc 2937 c->address_families = set_new(NULL);
4298d0b5
LP
2938 if (!c->address_families)
2939 return log_oom();
2940
2941 c->address_families_whitelist = !invert;
2942 }
2943
035fe294
ZJS
2944 for (p = rvalue;;) {
2945 _cleanup_free_ char *word = NULL;
4298d0b5
LP
2946 int af;
2947
035fe294
ZJS
2948 r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES);
2949 if (r == 0)
2950 return 0;
2951 if (r == -ENOMEM)
4298d0b5 2952 return log_oom();
035fe294
ZJS
2953 if (r < 0) {
2954 log_syntax(unit, LOG_WARNING, filename, line, r,
2955 "Invalid syntax, ignoring: %s", rvalue);
2956 return 0;
2957 }
4298d0b5 2958
035fe294 2959 af = af_from_name(word);
acf4d158
YW
2960 if (af < 0) {
2961 log_syntax(unit, LOG_ERR, filename, line, af,
063c4b1a 2962 "Failed to parse address family, ignoring: %s", word);
4298d0b5
LP
2963 continue;
2964 }
2965
2966 /* If we previously wanted to forbid an address family and now
035fe294 2967 * we want to allow it, then just remove it from the list.
4298d0b5
LP
2968 */
2969 if (!invert == c->address_families_whitelist) {
2970 r = set_put(c->address_families, INT_TO_PTR(af));
4298d0b5
LP
2971 if (r < 0)
2972 return log_oom();
2973 } else
2974 set_remove(c->address_families, INT_TO_PTR(af));
2975 }
4298d0b5 2976}
add00535
LP
2977
2978int config_parse_restrict_namespaces(
2979 const char *unit,
2980 const char *filename,
2981 unsigned line,
2982 const char *section,
2983 unsigned section_line,
2984 const char *lvalue,
2985 int ltype,
2986 const char *rvalue,
2987 void *data,
2988 void *userdata) {
2989
2990 ExecContext *c = data;
aa9d574d 2991 unsigned long flags;
add00535
LP
2992 bool invert = false;
2993 int r;
2994
2995 if (isempty(rvalue)) {
2996 /* Reset to the default. */
aa9d574d
YW
2997 c->restrict_namespaces = NAMESPACE_FLAGS_INITIAL;
2998 return 0;
2999 }
3000
3001 /* Boolean parameter ignores the previous settings */
3002 r = parse_boolean(rvalue);
3003 if (r > 0) {
3004 c->restrict_namespaces = 0;
3005 return 0;
3006 } else if (r == 0) {
add00535
LP
3007 c->restrict_namespaces = NAMESPACE_FLAGS_ALL;
3008 return 0;
3009 }
3010
3011 if (rvalue[0] == '~') {
3012 invert = true;
3013 rvalue++;
3014 }
3015
aa9d574d
YW
3016 /* Not a boolean argument, in this case it's a list of namespace types. */
3017 r = namespace_flags_from_string(rvalue, &flags);
3018 if (r < 0) {
3019 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse namespace type string, ignoring: %s", rvalue);
3020 return 0;
add00535
LP
3021 }
3022
aa9d574d
YW
3023 if (c->restrict_namespaces == NAMESPACE_FLAGS_INITIAL)
3024 /* Initial assignment. Just set the value. */
3025 c->restrict_namespaces = invert ? (~flags) & NAMESPACE_FLAGS_ALL : flags;
3026 else
3027 /* Merge the value with the previous one. */
3028 SET_FLAG(c->restrict_namespaces, flags, !invert);
add00535
LP
3029
3030 return 0;
3031}
c0467cf3 3032#endif
8351ceae 3033
a016b922
LP
3034int config_parse_unit_slice(
3035 const char *unit,
3036 const char *filename,
3037 unsigned line,
3038 const char *section,
71a61510 3039 unsigned section_line,
a016b922
LP
3040 const char *lvalue,
3041 int ltype,
3042 const char *rvalue,
3043 void *data,
3044 void *userdata) {
3045
063c4b1a 3046 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
a016b922 3047 _cleanup_free_ char *k = NULL;
d79200e2 3048 Unit *u = userdata, *slice = NULL;
a016b922
LP
3049 int r;
3050
3051 assert(filename);
3052 assert(lvalue);
3053 assert(rvalue);
3054 assert(u);
3055
19f6d710 3056 r = unit_name_printf(u, rvalue, &k);
d79200e2 3057 if (r < 0) {
063c4b1a 3058 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue);
d79200e2 3059 return 0;
19f6d710 3060 }
a016b922 3061
063c4b1a 3062 r = manager_load_unit(u->manager, k, NULL, &error, &slice);
a016b922 3063 if (r < 0) {
063c4b1a 3064 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to load slice unit %s, ignoring: %s", k, bus_error_message(&error, r));
a016b922
LP
3065 return 0;
3066 }
3067
d79200e2
LP
3068 r = unit_set_slice(u, slice);
3069 if (r < 0) {
063c4b1a 3070 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to assign slice %s to unit %s, ignoring: %m", slice->id, u->id);
a016b922
LP
3071 return 0;
3072 }
3073
a016b922
LP
3074 return 0;
3075}
3076
b2f8b02e
LP
3077int config_parse_cpu_quota(
3078 const char *unit,
3079 const char *filename,
3080 unsigned line,
3081 const char *section,
3082 unsigned section_line,
3083 const char *lvalue,
3084 int ltype,
3085 const char *rvalue,
3086 void *data,
3087 void *userdata) {
3088
3089 CGroupContext *c = data;
9184ca48 3090 int r;
b2f8b02e
LP
3091
3092 assert(filename);
3093 assert(lvalue);
3094 assert(rvalue);
3095
3096 if (isempty(rvalue)) {
3a43da28 3097 c->cpu_quota_per_sec_usec = USEC_INFINITY;
b2f8b02e
LP
3098 return 0;
3099 }
3100
f806dfd3 3101 r = parse_permille_unbounded(rvalue);
9184ca48 3102 if (r <= 0) {
063c4b1a 3103 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid CPU quota '%s', ignoring.", rvalue);
9a054909 3104 return 0;
b2f8b02e
LP
3105 }
3106
f806dfd3 3107 c->cpu_quota_per_sec_usec = ((usec_t) r * USEC_PER_SEC) / 1000U;
b2f8b02e
LP
3108 return 0;
3109}
3110
4ad49000
LP
3111int config_parse_memory_limit(
3112 const char *unit,
3113 const char *filename,
3114 unsigned line,
3115 const char *section,
71a61510 3116 unsigned section_line,
4ad49000
LP
3117 const char *lvalue,
3118 int ltype,
3119 const char *rvalue,
3120 void *data,
3121 void *userdata) {
3122
3123 CGroupContext *c = data;
da4d897e 3124 uint64_t bytes = CGROUP_LIMIT_MAX;
4ad49000
LP
3125 int r;
3126
e57c9ce1 3127 if (!isempty(rvalue) && !streq(rvalue, "infinity")) {
875ae566 3128
f806dfd3 3129 r = parse_permille(rvalue);
875ae566
LP
3130 if (r < 0) {
3131 r = parse_size(rvalue, 1024, &bytes);
3132 if (r < 0) {
063c4b1a 3133 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid memory limit '%s', ignoring: %m", rvalue);
875ae566
LP
3134 return 0;
3135 }
3136 } else
f806dfd3 3137 bytes = physical_memory_scale(r, 1000U);
875ae566 3138
906bdbf5 3139 if (bytes >= UINT64_MAX ||
22bf131b 3140 (bytes <= 0 && !STR_IN_SET(lvalue, "MemorySwapMax", "MemoryLow", "MemoryMin", "DefaultMemoryLow", "DefaultMemoryMin"))) {
063c4b1a 3141 log_syntax(unit, LOG_ERR, filename, line, 0, "Memory limit '%s' out of range, ignoring.", rvalue);
da4d897e
TH
3142 return 0;
3143 }
4ad49000
LP
3144 }
3145
c52db42b
CD
3146 if (streq(lvalue, "DefaultMemoryLow")) {
3147 c->default_memory_low_set = true;
3148 if (isempty(rvalue))
3149 c->default_memory_low = CGROUP_LIMIT_MIN;
3150 else
3151 c->default_memory_low = bytes;
7ad5439e
CD
3152 } else if (streq(lvalue, "DefaultMemoryMin")) {
3153 c->default_memory_min_set = true;
3154 if (isempty(rvalue))
3155 c->default_memory_min = CGROUP_LIMIT_MIN;
3156 else
3157 c->default_memory_min = bytes;
3158 } else if (streq(lvalue, "MemoryMin")) {
48422635 3159 c->memory_min = bytes;
7ad5439e
CD
3160 c->memory_min_set = true;
3161 } else if (streq(lvalue, "MemoryLow")) {
da4d897e 3162 c->memory_low = bytes;
c52db42b
CD
3163 c->memory_low_set = true;
3164 } else if (streq(lvalue, "MemoryHigh"))
da4d897e
TH
3165 c->memory_high = bytes;
3166 else if (streq(lvalue, "MemoryMax"))
3167 c->memory_max = bytes;
96e131ea
WC
3168 else if (streq(lvalue, "MemorySwapMax"))
3169 c->memory_swap_max = bytes;
3170 else if (streq(lvalue, "MemoryLimit"))
da4d897e 3171 c->memory_limit = bytes;
96e131ea
WC
3172 else
3173 return -EINVAL;
4ad49000 3174
4ad49000
LP
3175 return 0;
3176}
3177
03a7b521
LP
3178int config_parse_tasks_max(
3179 const char *unit,
3180 const char *filename,
3181 unsigned line,
3182 const char *section,
3183 unsigned section_line,
3184 const char *lvalue,
3185 int ltype,
3186 const char *rvalue,
3187 void *data,
3188 void *userdata) {
3189
f5058264
TH
3190 uint64_t *tasks_max = data, v;
3191 Unit *u = userdata;
03a7b521
LP
3192 int r;
3193
f5058264 3194 if (isempty(rvalue)) {
958b8c7b 3195 *tasks_max = u ? u->manager->default_tasks_max : UINT64_MAX;
f5058264
TH
3196 return 0;
3197 }
3198
3199 if (streq(rvalue, "infinity")) {
3200 *tasks_max = CGROUP_LIMIT_MAX;
03a7b521
LP
3201 return 0;
3202 }
3203
f806dfd3 3204 r = parse_permille(rvalue);
83f8e808 3205 if (r < 0) {
f5058264 3206 r = safe_atou64(rvalue, &v);
83f8e808 3207 if (r < 0) {
063c4b1a 3208 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid maximum tasks value '%s', ignoring: %m", rvalue);
83f8e808
LP
3209 return 0;
3210 }
3211 } else
f806dfd3 3212 v = system_tasks_max_scale(r, 1000U);
83f8e808 3213
f5058264 3214 if (v <= 0 || v >= UINT64_MAX) {
063c4b1a 3215 log_syntax(unit, LOG_ERR, filename, line, 0, "Maximum tasks value '%s' out of range, ignoring.", rvalue);
03a7b521
LP
3216 return 0;
3217 }
3218
f5058264 3219 *tasks_max = v;
03a7b521
LP
3220 return 0;
3221}
3222
02638280
LP
3223int config_parse_delegate(
3224 const char *unit,
3225 const char *filename,
3226 unsigned line,
3227 const char *section,
3228 unsigned section_line,
3229 const char *lvalue,
3230 int ltype,
3231 const char *rvalue,
3232 void *data,
3233 void *userdata) {
3234
3235 CGroupContext *c = data;
ecae73d7 3236 UnitType t;
02638280
LP
3237 int r;
3238
ecae73d7
ZJS
3239 t = unit_name_to_type(unit);
3240 assert(t != _UNIT_TYPE_INVALID);
3241
3242 if (!unit_vtable[t]->can_delegate) {
3243 log_syntax(unit, LOG_ERR, filename, line, 0, "Delegate= setting not supported for this unit type, ignoring.");
3244 return 0;
3245 }
3246
02638280
LP
3247 /* We either accept a boolean value, which may be used to turn on delegation for all controllers, or turn it
3248 * off for all. Or it takes a list of controller names, in which case we add the specified controllers to the
3249 * mask to delegate. */
3250
1bdfc7b9 3251 if (isempty(rvalue)) {
d48013f8
YW
3252 /* An empty string resets controllers and set Delegate=yes. */
3253 c->delegate = true;
1bdfc7b9
YW
3254 c->delegate_controllers = 0;
3255 return 0;
3256 }
3257
02638280
LP
3258 r = parse_boolean(rvalue);
3259 if (r < 0) {
3260 const char *p = rvalue;
3261 CGroupMask mask = 0;
3262
3263 for (;;) {
3264 _cleanup_free_ char *word = NULL;
3265 CGroupController cc;
3266
3267 r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES);
3268 if (r == 0)
3269 break;
3270 if (r == -ENOMEM)
3271 return log_oom();
3272 if (r < 0) {
3273 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
ff1b8455 3274 return 0;
02638280
LP
3275 }
3276
3277 cc = cgroup_controller_from_string(word);
3278 if (cc < 0) {
063c4b1a 3279 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid controller name '%s', ignoring", word);
02638280
LP
3280 continue;
3281 }
3282
3283 mask |= CGROUP_CONTROLLER_TO_MASK(cc);
3284 }
3285
3286 c->delegate = true;
3287 c->delegate_controllers |= mask;
3288
3289 } else if (r > 0) {
3290 c->delegate = true;
3291 c->delegate_controllers = _CGROUP_MASK_ALL;
3292 } else {
3293 c->delegate = false;
3294 c->delegate_controllers = 0;
3295 }
3296
3297 return 0;
3298}
3299
4ad49000
LP
3300int config_parse_device_allow(
3301 const char *unit,
3302 const char *filename,
3303 unsigned line,
3304 const char *section,
71a61510 3305 unsigned section_line,
4ad49000
LP
3306 const char *lvalue,
3307 int ltype,
3308 const char *rvalue,
3309 void *data,
3310 void *userdata) {
3311
c9f620bf 3312 _cleanup_free_ char *path = NULL, *resolved = NULL;
4ad49000 3313 CGroupContext *c = data;
c9f620bf 3314 const char *p = rvalue;
1116e14c 3315 int r;
4ad49000
LP
3316
3317 if (isempty(rvalue)) {
3318 while (c->device_allow)
3319 cgroup_context_free_device_allow(c, c->device_allow);
3320
3321 return 0;
3322 }
3323
c9f620bf
YW
3324 r = extract_first_word(&p, &path, NULL, EXTRACT_QUOTES);
3325 if (r == -ENOMEM)
3326 return log_oom();
508f63b4 3327 if (r < 0) {
1116e14c 3328 log_syntax(unit, LOG_WARNING, filename, line, r,
c9f620bf
YW
3329 "Invalid syntax, ignoring: %s", rvalue);
3330 return 0;
3331 }
3332 if (r == 0) {
3333 log_syntax(unit, LOG_WARNING, filename, line, 0,
3334 "Failed to extract device path and rights from '%s', ignoring.", rvalue);
20d52ab6 3335 return 0;
1116e14c
NBS
3336 }
3337
c9f620bf
YW
3338 r = unit_full_printf(userdata, path, &resolved);
3339 if (r < 0) {
3340 log_syntax(unit, LOG_WARNING, filename, line, r,
3341 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
4ad49000
LP
3342 return 0;
3343 }
3344
49fe5c09 3345 if (!STARTSWITH_SET(resolved, "block-", "char-")) {
2f4d31c1 3346
57e84e75
LP
3347 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
3348 if (r < 0)
3349 return 0;
3350
3351 if (!valid_device_node_path(resolved)) {
3352 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid device node path '%s', ignoring.", resolved);
3353 return 0;
3354 }
c9f620bf 3355 }
4ad49000 3356
c9f620bf
YW
3357 if (!isempty(p) && !in_charset(p, "rwm")) {
3358 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid device rights '%s', ignoring.", p);
4ad49000
LP
3359 return 0;
3360 }
3361
fd870bac 3362 return cgroup_add_device_allow(c, resolved, p);
4ad49000
LP
3363}
3364
13c31542
TH
3365int config_parse_io_device_weight(
3366 const char *unit,
3367 const char *filename,
3368 unsigned line,
3369 const char *section,
3370 unsigned section_line,
3371 const char *lvalue,
3372 int ltype,
3373 const char *rvalue,
3374 void *data,
3375 void *userdata) {
3376
c9f620bf 3377 _cleanup_free_ char *path = NULL, *resolved = NULL;
13c31542
TH
3378 CGroupIODeviceWeight *w;
3379 CGroupContext *c = data;
c9f620bf 3380 const char *p = rvalue;
13c31542 3381 uint64_t u;
13c31542
TH
3382 int r;
3383
3384 assert(filename);
3385 assert(lvalue);
3386 assert(rvalue);
3387
3388 if (isempty(rvalue)) {
3389 while (c->io_device_weights)
3390 cgroup_context_free_io_device_weight(c, c->io_device_weights);
3391
3392 return 0;
3393 }
3394
c9f620bf
YW
3395 r = extract_first_word(&p, &path, NULL, EXTRACT_QUOTES);
3396 if (r == -ENOMEM)
3397 return log_oom();
3398 if (r < 0) {
3399 log_syntax(unit, LOG_WARNING, filename, line, r,
3400 "Invalid syntax, ignoring: %s", rvalue);
3401 return 0;
3402 }
3403 if (r == 0 || isempty(p)) {
3404 log_syntax(unit, LOG_WARNING, filename, line, 0,
3405 "Failed to extract device path and weight from '%s', ignoring.", rvalue);
13c31542
TH
3406 return 0;
3407 }
3408
c9f620bf
YW
3409 r = unit_full_printf(userdata, path, &resolved);
3410 if (r < 0) {
3411 log_syntax(unit, LOG_WARNING, filename, line, r,
3412 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
3413 return 0;
3414 }
13c31542 3415
2f4d31c1
YW
3416 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
3417 if (r < 0)
3418 return 0;
3419
c9f620bf 3420 r = cg_weight_parse(p, &u);
13c31542 3421 if (r < 0) {
c9f620bf 3422 log_syntax(unit, LOG_ERR, filename, line, r, "IO weight '%s' invalid, ignoring: %m", p);
13c31542
TH
3423 return 0;
3424 }
3425
3426 assert(u != CGROUP_WEIGHT_INVALID);
3427
3428 w = new0(CGroupIODeviceWeight, 1);
3429 if (!w)
3430 return log_oom();
3431
c9f620bf 3432 w->path = TAKE_PTR(resolved);
13c31542
TH
3433 w->weight = u;
3434
3435 LIST_PREPEND(device_weights, c->io_device_weights, w);
3436 return 0;
3437}
3438
6ae4283c
TH
3439int config_parse_io_device_latency(
3440 const char *unit,
3441 const char *filename,
3442 unsigned line,
3443 const char *section,
3444 unsigned section_line,
3445 const char *lvalue,
3446 int ltype,
3447 const char *rvalue,
3448 void *data,
3449 void *userdata) {
3450
3451 _cleanup_free_ char *path = NULL, *resolved = NULL;
3452 CGroupIODeviceLatency *l;
3453 CGroupContext *c = data;
3454 const char *p = rvalue;
3455 usec_t usec;
3456 int r;
3457
3458 assert(filename);
3459 assert(lvalue);
3460 assert(rvalue);
3461
3462 if (isempty(rvalue)) {
3463 while (c->io_device_latencies)
3464 cgroup_context_free_io_device_latency(c, c->io_device_latencies);
3465
3466 return 0;
3467 }
3468
3469 r = extract_first_word(&p, &path, NULL, EXTRACT_QUOTES);
3470 if (r == -ENOMEM)
3471 return log_oom();
3472 if (r < 0) {
3473 log_syntax(unit, LOG_WARNING, filename, line, r,
3474 "Invalid syntax, ignoring: %s", rvalue);
3475 return 0;
3476 }
3477 if (r == 0 || isempty(p)) {
3478 log_syntax(unit, LOG_WARNING, filename, line, 0,
3479 "Failed to extract device path and latency from '%s', ignoring.", rvalue);
3480 return 0;
3481 }
3482
3483 r = unit_full_printf(userdata, path, &resolved);
3484 if (r < 0) {
3485 log_syntax(unit, LOG_WARNING, filename, line, r,
3486 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
3487 return 0;
3488 }
3489
3490 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
3491 if (r < 0)
3492 return 0;
3493
3494 if (parse_sec(p, &usec) < 0) {
3495 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse timer value, ignoring: %s", p);
3496 return 0;
3497 }
3498
3499 l = new0(CGroupIODeviceLatency, 1);
3500 if (!l)
3501 return log_oom();
3502
3503 l->path = TAKE_PTR(resolved);
3504 l->target_usec = usec;
3505
3506 LIST_PREPEND(device_latencies, c->io_device_latencies, l);
3507 return 0;
3508}
3509
13c31542
TH
3510int config_parse_io_limit(
3511 const char *unit,
3512 const char *filename,
3513 unsigned line,
3514 const char *section,
3515 unsigned section_line,
3516 const char *lvalue,
3517 int ltype,
3518 const char *rvalue,
3519 void *data,
3520 void *userdata) {
3521
c9f620bf 3522 _cleanup_free_ char *path = NULL, *resolved = NULL;
13c31542
TH
3523 CGroupIODeviceLimit *l = NULL, *t;
3524 CGroupContext *c = data;
9be57249 3525 CGroupIOLimitType type;
c9f620bf 3526 const char *p = rvalue;
13c31542 3527 uint64_t num;
13c31542
TH
3528 int r;
3529
3530 assert(filename);
3531 assert(lvalue);
3532 assert(rvalue);
3533
9be57249
TH
3534 type = cgroup_io_limit_type_from_string(lvalue);
3535 assert(type >= 0);
13c31542
TH
3536
3537 if (isempty(rvalue)) {
3538 LIST_FOREACH(device_limits, l, c->io_device_limits)
9be57249 3539 l->limits[type] = cgroup_io_limit_defaults[type];
13c31542
TH
3540 return 0;
3541 }
3542
c9f620bf
YW
3543 r = extract_first_word(&p, &path, NULL, EXTRACT_QUOTES);
3544 if (r == -ENOMEM)
3545 return log_oom();
3546 if (r < 0) {
3547 log_syntax(unit, LOG_WARNING, filename, line, r,
3548 "Invalid syntax, ignoring: %s", rvalue);
3549 return 0;
3550 }
3551 if (r == 0 || isempty(p)) {
3552 log_syntax(unit, LOG_WARNING, filename, line, 0,
3553 "Failed to extract device node and bandwidth from '%s', ignoring.", rvalue);
13c31542
TH
3554 return 0;
3555 }
3556
c9f620bf
YW
3557 r = unit_full_printf(userdata, path, &resolved);
3558 if (r < 0) {
3559 log_syntax(unit, LOG_WARNING, filename, line, r,
3560 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
3561 return 0;
3562 }
13c31542 3563
2f4d31c1
YW
3564 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
3565 if (r < 0)
3566 return 0;
3567
9d5e9b4a 3568 if (streq("infinity", p))
13c31542 3569 num = CGROUP_LIMIT_MAX;
9d5e9b4a 3570 else {
c9f620bf 3571 r = parse_size(p, 1000, &num);
13c31542 3572 if (r < 0 || num <= 0) {
c9f620bf 3573 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid IO limit '%s', ignoring.", p);
13c31542
TH
3574 return 0;
3575 }
3576 }
3577
3578 LIST_FOREACH(device_limits, t, c->io_device_limits) {
c9f620bf 3579 if (path_equal(resolved, t->path)) {
13c31542
TH
3580 l = t;
3581 break;
3582 }
3583 }
3584
3585 if (!l) {
9be57249
TH
3586 CGroupIOLimitType ttype;
3587
13c31542
TH
3588 l = new0(CGroupIODeviceLimit, 1);
3589 if (!l)
3590 return log_oom();
3591
c9f620bf 3592 l->path = TAKE_PTR(resolved);
9be57249
TH
3593 for (ttype = 0; ttype < _CGROUP_IO_LIMIT_TYPE_MAX; ttype++)
3594 l->limits[ttype] = cgroup_io_limit_defaults[ttype];
13c31542
TH
3595
3596 LIST_PREPEND(device_limits, c->io_device_limits, l);
3597 }
3598
9be57249 3599 l->limits[type] = num;
13c31542
TH
3600
3601 return 0;
3602}
3603
8e7076ca
LP
3604int config_parse_blockio_device_weight(
3605 const char *unit,
3606 const char *filename,
3607 unsigned line,
3608 const char *section,
71a61510 3609 unsigned section_line,
8e7076ca
LP
3610 const char *lvalue,
3611 int ltype,
3612 const char *rvalue,
3613 void *data,
3614 void *userdata) {
3615
c9f620bf 3616 _cleanup_free_ char *path = NULL, *resolved = NULL;
8e7076ca 3617 CGroupBlockIODeviceWeight *w;
4ad49000 3618 CGroupContext *c = data;
c9f620bf 3619 const char *p = rvalue;
d53d9474 3620 uint64_t u;
4ad49000
LP
3621 int r;
3622
3623 assert(filename);
3624 assert(lvalue);
3625 assert(rvalue);
3626
3627 if (isempty(rvalue)) {
4ad49000
LP
3628 while (c->blockio_device_weights)
3629 cgroup_context_free_blockio_device_weight(c, c->blockio_device_weights);
3630
3631 return 0;
3632 }
3633
c9f620bf
YW
3634 r = extract_first_word(&p, &path, NULL, EXTRACT_QUOTES);
3635 if (r == -ENOMEM)
3636 return log_oom();
3637 if (r < 0) {
3638 log_syntax(unit, LOG_WARNING, filename, line, r,
3639 "Invalid syntax, ignoring: %s", rvalue);
3640 return 0;
3641 }
3642 if (r == 0 || isempty(p)) {
3643 log_syntax(unit, LOG_WARNING, filename, line, 0,
3644 "Failed to extract device node and weight from '%s', ignoring.", rvalue);
8e7076ca
LP
3645 return 0;
3646 }
4ad49000 3647
c9f620bf
YW
3648 r = unit_full_printf(userdata, path, &resolved);
3649 if (r < 0) {
3650 log_syntax(unit, LOG_WARNING, filename, line, r,
3651 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
3652 return 0;
3653 }
4ad49000 3654
2f4d31c1
YW
3655 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
3656 if (r < 0)
3657 return 0;
3658
c9f620bf 3659 r = cg_blkio_weight_parse(p, &u);
d53d9474 3660 if (r < 0) {
c9f620bf 3661 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid block IO weight '%s', ignoring: %m", p);
4ad49000
LP
3662 return 0;
3663 }
3664
d53d9474
LP
3665 assert(u != CGROUP_BLKIO_WEIGHT_INVALID);
3666
8e7076ca
LP
3667 w = new0(CGroupBlockIODeviceWeight, 1);
3668 if (!w)
3669 return log_oom();
4ad49000 3670
c9f620bf 3671 w->path = TAKE_PTR(resolved);
d53d9474 3672 w->weight = u;
4ad49000 3673
71fda00f 3674 LIST_PREPEND(device_weights, c->blockio_device_weights, w);
4ad49000
LP
3675 return 0;
3676}
3677
3678int config_parse_blockio_bandwidth(
3679 const char *unit,
3680 const char *filename,
3681 unsigned line,
3682 const char *section,
71a61510 3683 unsigned section_line,
4ad49000
LP
3684 const char *lvalue,
3685 int ltype,
3686 const char *rvalue,
3687 void *data,
3688 void *userdata) {
3689
c9f620bf 3690 _cleanup_free_ char *path = NULL, *resolved = NULL;
979d0311 3691 CGroupBlockIODeviceBandwidth *b = NULL, *t;
4ad49000 3692 CGroupContext *c = data;
c9f620bf 3693 const char *p = rvalue;
59f448cf 3694 uint64_t bytes;
47c0980d 3695 bool read;
4ad49000
LP
3696 int r;
3697
3698 assert(filename);
3699 assert(lvalue);
3700 assert(rvalue);
3701
47c0980d
G
3702 read = streq("BlockIOReadBandwidth", lvalue);
3703
4ad49000 3704 if (isempty(rvalue)) {
979d0311
TH
3705 LIST_FOREACH(device_bandwidths, b, c->blockio_device_bandwidths) {
3706 b->rbps = CGROUP_LIMIT_MAX;
3707 b->wbps = CGROUP_LIMIT_MAX;
3708 }
4ad49000
LP
3709 return 0;
3710 }
3711
c9f620bf
YW
3712 r = extract_first_word(&p, &path, NULL, EXTRACT_QUOTES);
3713 if (r == -ENOMEM)
3714 return log_oom();
3715 if (r < 0) {
3716 log_syntax(unit, LOG_WARNING, filename, line, r,
3717 "Invalid syntax, ignoring: %s", rvalue);
3718 return 0;
3719 }
3720 if (r == 0 || isempty(p)) {
3721 log_syntax(unit, LOG_WARNING, filename, line, 0,
3722 "Failed to extract device node and bandwidth from '%s', ignoring.", rvalue);
4ad49000
LP
3723 return 0;
3724 }
3725
c9f620bf
YW
3726 r = unit_full_printf(userdata, path, &resolved);
3727 if (r < 0) {
3728 log_syntax(unit, LOG_WARNING, filename, line, r,
3729 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
3730 return 0;
3731 }
4ad49000 3732
2f4d31c1
YW
3733 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
3734 if (r < 0)
3735 return 0;
3736
c9f620bf 3737 r = parse_size(p, 1000, &bytes);
4ad49000 3738 if (r < 0 || bytes <= 0) {
c9f620bf 3739 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid Block IO Bandwidth '%s', ignoring.", p);
4ad49000
LP
3740 return 0;
3741 }
3742
979d0311 3743 LIST_FOREACH(device_bandwidths, t, c->blockio_device_bandwidths) {
c9f620bf 3744 if (path_equal(resolved, t->path)) {
979d0311
TH
3745 b = t;
3746 break;
3747 }
3748 }
4ad49000 3749
979d0311
TH
3750 if (!t) {
3751 b = new0(CGroupBlockIODeviceBandwidth, 1);
3752 if (!b)
3753 return log_oom();
3754
c9f620bf 3755 b->path = TAKE_PTR(resolved);
979d0311
TH
3756 b->rbps = CGROUP_LIMIT_MAX;
3757 b->wbps = CGROUP_LIMIT_MAX;
3758
3759 LIST_PREPEND(device_bandwidths, c->blockio_device_bandwidths, b);
3760 }
4ad49000 3761
979d0311
TH
3762 if (read)
3763 b->rbps = bytes;
3764 else
3765 b->wbps = bytes;
4ad49000
LP
3766
3767 return 0;
3768}
3769
d420282b
LP
3770int config_parse_job_mode_isolate(
3771 const char *unit,
3772 const char *filename,
3773 unsigned line,
3774 const char *section,
3775 unsigned section_line,
3776 const char *lvalue,
3777 int ltype,
3778 const char *rvalue,
3779 void *data,
3780 void *userdata) {
3781
3782 JobMode *m = data;
3783 int r;
3784
3785 assert(filename);
3786 assert(lvalue);
3787 assert(rvalue);
3788
3789 r = parse_boolean(rvalue);
3790 if (r < 0) {
12ca818f 3791 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse boolean, ignoring: %s", rvalue);
d420282b
LP
3792 return 0;
3793 }
3794
8ab39347
YW
3795 log_notice("%s is deprecated. Please use OnFailureJobMode= instead", lvalue);
3796
d420282b
LP
3797 *m = r ? JOB_ISOLATE : JOB_REPLACE;
3798 return 0;
3799}
3800
3536f49e 3801int config_parse_exec_directories(
e66cf1a3
LP
3802 const char *unit,
3803 const char *filename,
3804 unsigned line,
3805 const char *section,
3806 unsigned section_line,
3807 const char *lvalue,
3808 int ltype,
3809 const char *rvalue,
3810 void *data,
3811 void *userdata) {
3812
a2a5291b 3813 char***rt = data;
9b5864d9 3814 Unit *u = userdata;
035fe294 3815 const char *p;
e66cf1a3
LP
3816 int r;
3817
3818 assert(filename);
3819 assert(lvalue);
3820 assert(rvalue);
3821 assert(data);
3822
3823 if (isempty(rvalue)) {
3824 /* Empty assignment resets the list */
6796073e 3825 *rt = strv_free(*rt);
e66cf1a3
LP
3826 return 0;
3827 }
3828
035fe294
ZJS
3829 for (p = rvalue;;) {
3830 _cleanup_free_ char *word = NULL, *k = NULL;
e66cf1a3 3831
035fe294 3832 r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES);
035fe294 3833 if (r == -ENOMEM)
e66cf1a3 3834 return log_oom();
035fe294
ZJS
3835 if (r < 0) {
3836 log_syntax(unit, LOG_WARNING, filename, line, r,
3837 "Invalid syntax, ignoring: %s", rvalue);
3838 return 0;
3839 }
091e9efe
LP
3840 if (r == 0)
3841 return 0;
e66cf1a3 3842
18913df9 3843 r = unit_full_printf(u, word, &k);
9b5864d9 3844 if (r < 0) {
035fe294 3845 log_syntax(unit, LOG_ERR, filename, line, r,
063c4b1a 3846 "Failed to resolve unit specifiers in \"%s\", ignoring: %m", word);
9b5864d9
MG
3847 continue;
3848 }
3849
2f4d31c1
YW
3850 r = path_simplify_and_warn(k, PATH_CHECK_RELATIVE, unit, filename, line, lvalue);
3851 if (r < 0)
e8865688 3852 continue;
e8865688
LP
3853
3854 if (path_startswith(k, "private")) {
3855 log_syntax(unit, LOG_ERR, filename, line, 0,
4605de11 3856 "%s= path can't be 'private', ignoring assignment: %s", lvalue, word);
e66cf1a3
LP
3857 continue;
3858 }
3859
035fe294 3860 r = strv_push(rt, k);
e66cf1a3
LP
3861 if (r < 0)
3862 return log_oom();
035fe294 3863 k = NULL;
e66cf1a3 3864 }
e66cf1a3
LP
3865}
3866
3af00fb8
LP
3867int config_parse_set_status(
3868 const char *unit,
3869 const char *filename,
3870 unsigned line,
3871 const char *section,
3872 unsigned section_line,
3873 const char *lvalue,
3874 int ltype,
3875 const char *rvalue,
3876 void *data,
3877 void *userdata) {
3878
3af00fb8 3879 size_t l;
a2a5291b 3880 const char *word, *state;
3af00fb8
LP
3881 int r;
3882 ExitStatusSet *status_set = data;
3883
3884 assert(filename);
3885 assert(lvalue);
3886 assert(rvalue);
3887 assert(data);
3888
3e2d435b 3889 /* Empty assignment resets the list */
3af00fb8 3890 if (isempty(rvalue)) {
3e2d435b 3891 exit_status_set_free(status_set);
3af00fb8
LP
3892 return 0;
3893 }
3894
a2a5291b 3895 FOREACH_WORD(word, l, rvalue, state) {
3af00fb8
LP
3896 _cleanup_free_ char *temp;
3897 int val;
61593865 3898 Set **set;
3af00fb8 3899
a2a5291b 3900 temp = strndup(word, l);
3af00fb8
LP
3901 if (!temp)
3902 return log_oom();
3903
3904 r = safe_atoi(temp, &val);
3905 if (r < 0) {
29a3db75 3906 val = signal_from_string(temp);
3af00fb8 3907
1e2fd62d 3908 if (val <= 0) {
12ca818f 3909 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse value, ignoring: %s", word);
61593865 3910 continue;
3af00fb8 3911 }
61593865 3912 set = &status_set->signal;
3af00fb8 3913 } else {
1e2fd62d 3914 if (val < 0 || val > 255) {
12ca818f 3915 log_syntax(unit, LOG_ERR, filename, line, 0, "Value %d is outside range 0-255, ignoring", val);
1e2fd62d 3916 continue;
3af00fb8 3917 }
61593865 3918 set = &status_set->status;
3af00fb8 3919 }
1e2fd62d 3920
61593865 3921 r = set_ensure_allocated(set, NULL);
1e2fd62d
ZJS
3922 if (r < 0)
3923 return log_oom();
3924
61593865 3925 r = set_put(*set, INT_TO_PTR(val));
063c4b1a
YW
3926 if (r < 0)
3927 return log_oom();
3af00fb8 3928 }
b2fadec6 3929 if (!isempty(state))
12ca818f 3930 log_syntax(unit, LOG_ERR, filename, line, 0, "Trailing garbage, ignoring.");
3af00fb8
LP
3931
3932 return 0;
3933}
3934
94828d2d
LP
3935int config_parse_namespace_path_strv(
3936 const char *unit,
3937 const char *filename,
3938 unsigned line,
3939 const char *section,
3940 unsigned section_line,
3941 const char *lvalue,
3942 int ltype,
3943 const char *rvalue,
3944 void *data,
3945 void *userdata) {
3946
7b07e993 3947 Unit *u = userdata;
a2a5291b 3948 char*** sv = data;
063c4b1a 3949 const char *p = rvalue;
94828d2d
LP
3950 int r;
3951
3952 assert(filename);
3953 assert(lvalue);
3954 assert(rvalue);
3955 assert(data);
3956
3957 if (isempty(rvalue)) {
3958 /* Empty assignment resets the list */
6796073e 3959 *sv = strv_free(*sv);
94828d2d
LP
3960 return 0;
3961 }
3962
727f76d7 3963 for (;;) {
7b07e993 3964 _cleanup_free_ char *word = NULL, *resolved = NULL, *joined = NULL;
20b7a007
LP
3965 const char *w;
3966 bool ignore_enoent = false, shall_prefix = false;
94828d2d 3967
063c4b1a 3968 r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES);
0293a7a8
EV
3969 if (r == 0)
3970 break;
3971 if (r == -ENOMEM)
3972 return log_oom();
727f76d7 3973 if (r < 0) {
7b07e993 3974 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to extract first word, ignoring: %s", rvalue);
727f76d7
EV
3975 return 0;
3976 }
94828d2d 3977
20b7a007
LP
3978 w = word;
3979 if (startswith(w, "-")) {
3980 ignore_enoent = true;
3981 w++;
3982 }
3983 if (startswith(w, "+")) {
3984 shall_prefix = true;
3985 w++;
3986 }
7b07e993 3987
20b7a007 3988 r = unit_full_printf(u, w, &resolved);
7b07e993 3989 if (r < 0) {
063c4b1a 3990 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s: %m", w);
94828d2d
LP
3991 continue;
3992 }
3993
2f4d31c1
YW
3994 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
3995 if (r < 0)
7b07e993 3996 continue;
94828d2d 3997
20b7a007
LP
3998 joined = strjoin(ignore_enoent ? "-" : "",
3999 shall_prefix ? "+" : "",
4000 resolved);
7b07e993
LP
4001
4002 r = strv_push(sv, joined);
94828d2d
LP
4003 if (r < 0)
4004 return log_oom();
4005
7b07e993 4006 joined = NULL;
94828d2d
LP
4007 }
4008
4009 return 0;
4010}
4011
2abd4e38
YW
4012int config_parse_temporary_filesystems(
4013 const char *unit,
4014 const char *filename,
4015 unsigned line,
4016 const char *section,
4017 unsigned section_line,
4018 const char *lvalue,
4019 int ltype,
4020 const char *rvalue,
4021 void *data,
4022 void *userdata) {
4023
4024 Unit *u = userdata;
4025 ExecContext *c = data;
063c4b1a 4026 const char *p = rvalue;
2abd4e38
YW
4027 int r;
4028
4029 assert(filename);
4030 assert(lvalue);
4031 assert(rvalue);
4032 assert(data);
4033
4034 if (isempty(rvalue)) {
4035 /* Empty assignment resets the list */
4036 temporary_filesystem_free_many(c->temporary_filesystems, c->n_temporary_filesystems);
4037 c->temporary_filesystems = NULL;
4038 c->n_temporary_filesystems = 0;
4039 return 0;
4040 }
4041
2abd4e38
YW
4042 for (;;) {
4043 _cleanup_free_ char *word = NULL, *path = NULL, *resolved = NULL;
4044 const char *w;
4045
063c4b1a 4046 r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES);
2abd4e38 4047 if (r == 0)
063c4b1a 4048 return 0;
2abd4e38
YW
4049 if (r == -ENOMEM)
4050 return log_oom();
4051 if (r < 0) {
4052 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to extract first word, ignoring: %s", rvalue);
4053 return 0;
4054 }
4055
4056 w = word;
4057 r = extract_first_word(&w, &path, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
063c4b1a
YW
4058 if (r == -ENOMEM)
4059 return log_oom();
4060 if (r < 0) {
4061 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to extract first word, ignoring: %s", word);
4062 continue;
4063 }
4064 if (r == 0) {
4065 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid syntax, ignoring: %s", word);
4066 continue;
4067 }
2abd4e38
YW
4068
4069 r = unit_full_printf(u, path, &resolved);
4070 if (r < 0) {
063c4b1a 4071 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", path);
2abd4e38
YW
4072 continue;
4073 }
4074
2f4d31c1
YW
4075 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
4076 if (r < 0)
2abd4e38 4077 continue;
2abd4e38 4078
a26fec24 4079 r = temporary_filesystem_add(&c->temporary_filesystems, &c->n_temporary_filesystems, resolved, w);
6302d1ea 4080 if (r < 0)
2abd4e38 4081 return log_oom();
2abd4e38 4082 }
2abd4e38
YW
4083}
4084
d2d6c096
LP
4085int config_parse_bind_paths(
4086 const char *unit,
4087 const char *filename,
4088 unsigned line,
4089 const char *section,
4090 unsigned section_line,
4091 const char *lvalue,
4092 int ltype,
4093 const char *rvalue,
4094 void *data,
4095 void *userdata) {
4096
4097 ExecContext *c = data;
42d43f21 4098 Unit *u = userdata;
d2d6c096
LP
4099 const char *p;
4100 int r;
4101
4102 assert(filename);
4103 assert(lvalue);
4104 assert(rvalue);
4105 assert(data);
4106
4107 if (isempty(rvalue)) {
4108 /* Empty assignment resets the list */
4109 bind_mount_free_many(c->bind_mounts, c->n_bind_mounts);
4110 c->bind_mounts = NULL;
4111 c->n_bind_mounts = 0;
4112 return 0;
4113 }
4114
4115 p = rvalue;
4116 for (;;) {
4117 _cleanup_free_ char *source = NULL, *destination = NULL;
42d43f21 4118 _cleanup_free_ char *sresolved = NULL, *dresolved = NULL;
d2d6c096
LP
4119 char *s = NULL, *d = NULL;
4120 bool rbind = true, ignore_enoent = false;
4121
4122 r = extract_first_word(&p, &source, ":" WHITESPACE, EXTRACT_QUOTES|EXTRACT_DONT_COALESCE_SEPARATORS);
4123 if (r == 0)
4124 break;
4125 if (r == -ENOMEM)
4126 return log_oom();
4127 if (r < 0) {
2f4d31c1 4128 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse %s, ignoring: %s", lvalue, rvalue);
d2d6c096
LP
4129 return 0;
4130 }
4131
42d43f21
DC
4132 r = unit_full_printf(u, source, &sresolved);
4133 if (r < 0) {
4134 log_syntax(unit, LOG_ERR, filename, line, r,
063c4b1a 4135 "Failed to resolved unit specifiers in \"%s\", ignoring: %m", source);
2f4d31c1 4136 continue;
42d43f21
DC
4137 }
4138
4139 s = sresolved;
d2d6c096
LP
4140 if (s[0] == '-') {
4141 ignore_enoent = true;
4142 s++;
4143 }
4144
2f4d31c1
YW
4145 r = path_simplify_and_warn(s, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
4146 if (r < 0)
4147 continue;
d2d6c096
LP
4148
4149 /* Optionally, the destination is specified. */
4150 if (p && p[-1] == ':') {
4151 r = extract_first_word(&p, &destination, ":" WHITESPACE, EXTRACT_QUOTES|EXTRACT_DONT_COALESCE_SEPARATORS);
4152 if (r == -ENOMEM)
4153 return log_oom();
4154 if (r < 0) {
2f4d31c1 4155 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse %s, ignoring: %s", lvalue, rvalue);
d2d6c096
LP
4156 return 0;
4157 }
4158 if (r == 0) {
2f4d31c1
YW
4159 log_syntax(unit, LOG_ERR, filename, line, 0, "Missing argument after ':', ignoring: %s", s);
4160 continue;
d2d6c096
LP
4161 }
4162
42d43f21
DC
4163 r = unit_full_printf(u, destination, &dresolved);
4164 if (r < 0) {
4165 log_syntax(unit, LOG_ERR, filename, line, r,
4166 "Failed to resolved specifiers in \"%s\", ignoring: %m", destination);
2f4d31c1 4167 continue;
42d43f21
DC
4168 }
4169
2f4d31c1
YW
4170 r = path_simplify_and_warn(dresolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
4171 if (r < 0)
4172 continue;
d2d6c096 4173
2f4d31c1 4174 d = dresolved;
d2d6c096
LP
4175
4176 /* Optionally, there's also a short option string specified */
4177 if (p && p[-1] == ':') {
4178 _cleanup_free_ char *options = NULL;
4179
4180 r = extract_first_word(&p, &options, NULL, EXTRACT_QUOTES);
4181 if (r == -ENOMEM)
4182 return log_oom();
4183 if (r < 0) {
4184 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse %s: %s", lvalue, rvalue);
4185 return 0;
4186 }
4187
4188 if (isempty(options) || streq(options, "rbind"))
4189 rbind = true;
4190 else if (streq(options, "norbind"))
4191 rbind = false;
4192 else {
4193 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid option string, ignoring setting: %s", options);
2f4d31c1 4194 continue;
d2d6c096
LP
4195 }
4196 }
4197 } else
4198 d = s;
4199
4200 r = bind_mount_add(&c->bind_mounts, &c->n_bind_mounts,
4201 &(BindMount) {
4202 .source = s,
4203 .destination = d,
4204 .read_only = !!strstr(lvalue, "ReadOnly"),
4205 .recursive = rbind,
4206 .ignore_enoent = ignore_enoent,
4207 });
4208 if (r < 0)
4209 return log_oom();
4210 }
4211
4212 return 0;
4213}
4214
eae51da3
LP
4215int config_parse_job_timeout_sec(
4216 const char* unit,
4217 const char *filename,
4218 unsigned line,
4219 const char *section,
4220 unsigned section_line,
4221 const char *lvalue,
4222 int ltype,
4223 const char *rvalue,
4224 void *data,
4225 void *userdata) {
4226
4227 Unit *u = data;
4228 usec_t usec;
4229 int r;
4230
4231 assert(filename);
4232 assert(lvalue);
4233 assert(rvalue);
4234 assert(u);
4235
4236 r = parse_sec_fix_0(rvalue, &usec);
4237 if (r < 0) {
4238 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse JobTimeoutSec= parameter, ignoring: %s", rvalue);
4239 return 0;
4240 }
4241
4242 /* If the user explicitly changed JobTimeoutSec= also change JobRunningTimeoutSec=, for compatibility with old
c05f3c8f 4243 * versions. If JobRunningTimeoutSec= was explicitly set, avoid this however as whatever the user picked should
eae51da3
LP
4244 * count. */
4245
4246 if (!u->job_running_timeout_set)
4247 u->job_running_timeout = usec;
4248
4249 u->job_timeout = usec;
4250
4251 return 0;
4252}
4253
4254int config_parse_job_running_timeout_sec(
4255 const char* unit,
4256 const char *filename,
4257 unsigned line,
4258 const char *section,
4259 unsigned section_line,
4260 const char *lvalue,
4261 int ltype,
4262 const char *rvalue,
4263 void *data,
4264 void *userdata) {
4265
4266 Unit *u = data;
4267 usec_t usec;
4268 int r;
4269
4270 assert(filename);
4271 assert(lvalue);
4272 assert(rvalue);
4273 assert(u);
4274
4275 r = parse_sec_fix_0(rvalue, &usec);
4276 if (r < 0) {
4277 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse JobRunningTimeoutSec= parameter, ignoring: %s", rvalue);
4278 return 0;
4279 }
4280
4281 u->job_running_timeout = usec;
4282 u->job_running_timeout_set = true;
4283
4284 return 0;
4285}
4286
54fcb619
ZJS
4287int config_parse_emergency_action(
4288 const char* unit,
4289 const char *filename,
4290 unsigned line,
4291 const char *section,
4292 unsigned section_line,
4293 const char *lvalue,
4294 int ltype,
4295 const char *rvalue,
4296 void *data,
4297 void *userdata) {
4298
4299 Manager *m = NULL;
4300 EmergencyAction *x = data;
4301 int r;
4302
4303 assert(filename);
4304 assert(lvalue);
4305 assert(rvalue);
4306 assert(data);
4307
4308 if (unit)
4309 m = ((Unit*) userdata)->manager;
4310 else
4311 m = data;
4312
4313 r = parse_emergency_action(rvalue, MANAGER_IS_SYSTEM(m), x);
4314 if (r < 0) {
469f76f1
ZJS
4315 if (r == -EOPNOTSUPP && MANAGER_IS_USER(m)) {
4316 /* Compat mode: remove for systemd 241. */
4317
4318 log_syntax(unit, LOG_INFO, filename, line, r,
4319 "%s= in user mode specified as \"%s\", using \"exit-force\" instead.",
4320 lvalue, rvalue);
4321 *x = EMERGENCY_ACTION_EXIT_FORCE;
4322 return 0;
4323 }
4324
54fcb619
ZJS
4325 if (r == -EOPNOTSUPP)
4326 log_syntax(unit, LOG_ERR, filename, line, r,
4327 "%s= specified as %s mode action, ignoring: %s",
4328 lvalue, MANAGER_IS_SYSTEM(m) ? "user" : "system", rvalue);
4329 else
4330 log_syntax(unit, LOG_ERR, filename, line, r,
4331 "Failed to parse %s=, ignoring: %s", lvalue, rvalue);
4332 return 0;
4333 }
4334
4335 return 0;
4336}
4337
a9353a5c
LP
4338int config_parse_pid_file(
4339 const char *unit,
4340 const char *filename,
4341 unsigned line,
4342 const char *section,
4343 unsigned section_line,
4344 const char *lvalue,
4345 int ltype,
4346 const char *rvalue,
4347 void *data,
4348 void *userdata) {
4349
4350 _cleanup_free_ char *k = NULL, *n = NULL;
4351 Unit *u = userdata;
4352 char **s = data;
a9353a5c
LP
4353 int r;
4354
4355 assert(filename);
4356 assert(lvalue);
4357 assert(rvalue);
4358 assert(u);
4359
b8055c05
YW
4360 if (isempty(rvalue)) {
4361 /* An empty assignment removes already set value. */
4362 *s = mfree(*s);
4363 return 0;
4364 }
4365
a9353a5c
LP
4366 r = unit_full_printf(u, rvalue, &k);
4367 if (r < 0) {
4368 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
4369 return 0;
4370 }
4371
4372 /* If this is a relative path make it absolute by prefixing the /run */
4373 n = path_make_absolute(k, u->manager->prefix[EXEC_DIRECTORY_RUNTIME]);
4374 if (!n)
4375 return log_oom();
4376
4377 /* Check that the result is a sensible path */
4378 r = path_simplify_and_warn(n, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
4379 if (r < 0)
4380 return r;
4381
4a66b5c9
LP
4382 r = patch_var_run(unit, filename, line, lvalue, &n);
4383 if (r < 0)
4384 return r;
a9353a5c 4385
4a66b5c9 4386 free_and_replace(*s, n);
a9353a5c
LP
4387 return 0;
4388}
4389
7af67e9a
LP
4390int config_parse_exit_status(
4391 const char *unit,
4392 const char *filename,
4393 unsigned line,
4394 const char *section,
4395 unsigned section_line,
4396 const char *lvalue,
4397 int ltype,
4398 const char *rvalue,
4399 void *data,
4400 void *userdata) {
4401
4402 int *exit_status = data, r;
4403 uint8_t u;
4404
4405 assert(filename);
4406 assert(lvalue);
4407 assert(rvalue);
4408 assert(exit_status);
4409
4410 if (isempty(rvalue)) {
4411 *exit_status = -1;
4412 return 0;
4413 }
4414
4415 r = safe_atou8(rvalue, &u);
4416 if (r < 0) {
4417 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse exit status '%s', ignoring: %m", rvalue);
4418 return 0;
4419 }
4420
4421 *exit_status = u;
4422 return 0;
4423}
4424
c72703e2
CD
4425int config_parse_disable_controllers(
4426 const char *unit,
4427 const char *filename,
4428 unsigned line,
4429 const char *section,
4430 unsigned section_line,
4431 const char *lvalue,
4432 int ltype,
4433 const char *rvalue,
4434 void *data,
4435 void *userdata) {
4436
4437 int r;
4438 CGroupContext *c = data;
4439 CGroupMask disabled_mask;
4440
4441 /* 1. If empty, make all controllers eligible for use again.
4442 * 2. If non-empty, merge all listed controllers, space separated. */
4443
4444 if (isempty(rvalue)) {
4445 c->disable_controllers = 0;
4446 return 0;
4447 }
4448
4449 r = cg_mask_from_string(rvalue, &disabled_mask);
4450 if (r < 0 || disabled_mask <= 0) {
4451 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid cgroup string: %s, ignoring", rvalue);
4452 return 0;
4453 }
4454
4455 c->disable_controllers |= disabled_mask;
4456
4457 return 0;
4458}
4459
071830ff 4460#define FOLLOW_MAX 8
87f0e418 4461
9e2f7c11 4462static int open_follow(char **filename, FILE **_f, Set *names, char **_final) {
a837f088 4463 char *id = NULL;
0301abf4 4464 unsigned c = 0;
87f0e418
LP
4465 int fd, r;
4466 FILE *f;
87f0e418
LP
4467
4468 assert(filename);
4469 assert(*filename);
4470 assert(_f);
4471 assert(names);
4472
0301abf4
LP
4473 /* This will update the filename pointer if the loaded file is
4474 * reached by a symlink. The old string will be freed. */
87f0e418 4475
0301abf4 4476 for (;;) {
2c7108c4 4477 char *target, *name;
87f0e418 4478
0301abf4
LP
4479 if (c++ >= FOLLOW_MAX)
4480 return -ELOOP;
4481
858d36c1 4482 path_simplify(*filename, false);
b08d03ff 4483
87f0e418 4484 /* Add the file name we are currently looking at to
8f05424d
LP
4485 * the names of this unit, but only if it is a valid
4486 * unit name. */
2b6bf07d 4487 name = basename(*filename);
7410616c 4488 if (unit_name_is_valid(name, UNIT_NAME_ANY)) {
8f05424d 4489
15e11d81
LP
4490 id = set_get(names, name);
4491 if (!id) {
4492 id = strdup(name);
4493 if (!id)
8f05424d 4494 return -ENOMEM;
87f0e418 4495
ef42202a
ZJS
4496 r = set_consume(names, id);
4497 if (r < 0)
8f05424d 4498 return r;
87f0e418 4499 }
87f0e418
LP
4500 }
4501
0301abf4 4502 /* Try to open the file name, but don't if its a symlink */
9946996c
LP
4503 fd = open(*filename, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
4504 if (fd >= 0)
87f0e418
LP
4505 break;
4506
0301abf4
LP
4507 if (errno != ELOOP)
4508 return -errno;
4509
87f0e418 4510 /* Hmm, so this is a symlink. Let's read the name, and follow it manually */
9946996c
LP
4511 r = readlink_and_make_absolute(*filename, &target);
4512 if (r < 0)
0301abf4 4513 return r;
87f0e418 4514
17ee3d72 4515 free_and_replace(*filename, target);
87f0e418
LP
4516 }
4517
e92aaed3 4518 f = fdopen(fd, "r");
9946996c 4519 if (!f) {
03e334a1 4520 safe_close(fd);
d4ad27a1 4521 return -errno;
87f0e418
LP
4522 }
4523
4524 *_f = f;
9e2f7c11 4525 *_final = id;
a837f088 4526
0301abf4 4527 return 0;
87f0e418
LP
4528}
4529
23a177ef
LP
4530static int merge_by_names(Unit **u, Set *names, const char *id) {
4531 char *k;
4532 int r;
4533
4534 assert(u);
4535 assert(*u);
4536 assert(names);
4537
4538 /* Let's try to add in all symlink names we found */
4539 while ((k = set_steal_first(names))) {
4540
4541 /* First try to merge in the other name into our
4542 * unit */
9946996c
LP
4543 r = unit_merge_by_name(*u, k);
4544 if (r < 0) {
23a177ef
LP
4545 Unit *other;
4546
4547 /* Hmm, we couldn't merge the other unit into
4548 * ours? Then let's try it the other way
4549 * round */
4550
7aad67e7
MS
4551 /* If the symlink name we are looking at is unit template, then
4552 we must search for instance of this template */
9d3e3406 4553 if (unit_name_is_valid(k, UNIT_NAME_TEMPLATE) && (*u)->instance) {
7aad67e7
MS
4554 _cleanup_free_ char *instance = NULL;
4555
4556 r = unit_name_replace_instance(k, (*u)->instance, &instance);
4557 if (r < 0)
4558 return r;
4559
4560 other = manager_get_unit((*u)->manager, instance);
4561 } else
4562 other = manager_get_unit((*u)->manager, k);
4563
23a177ef
LP
4564 free(k);
4565
9946996c
LP
4566 if (other) {
4567 r = unit_merge(other, *u);
4568 if (r >= 0) {
23a177ef
LP
4569 *u = other;
4570 return merge_by_names(u, names, NULL);
4571 }
9946996c 4572 }
23a177ef
LP
4573
4574 return r;
4575 }
4576
4577 if (id == k)
4578 unit_choose_id(*u, id);
4579
4580 free(k);
4581 }
4582
4583 return 0;
4584}
4585
e537352b 4586static int load_from_path(Unit *u, const char *path) {
e48614c4
ZJS
4587 _cleanup_set_free_free_ Set *symlink_names = NULL;
4588 _cleanup_fclose_ FILE *f = NULL;
4589 _cleanup_free_ char *filename = NULL;
4590 char *id = NULL;
23a177ef 4591 Unit *merged;
45fb0699 4592 struct stat st;
a837f088 4593 int r;
23a177ef
LP
4594
4595 assert(u);
e537352b 4596 assert(path);
3efd4195 4597
d5099efc 4598 symlink_names = set_new(&string_hash_ops);
f975e971 4599 if (!symlink_names)
87f0e418 4600 return -ENOMEM;
3efd4195 4601
036643a2
LP
4602 if (path_is_absolute(path)) {
4603
9946996c 4604 filename = strdup(path);
e48614c4
ZJS
4605 if (!filename)
4606 return -ENOMEM;
036643a2 4607
9946996c
LP
4608 r = open_follow(&filename, &f, symlink_names, &id);
4609 if (r < 0) {
97b11eed 4610 filename = mfree(filename);
036643a2 4611 if (r != -ENOENT)
e48614c4 4612 return r;
036643a2
LP
4613 }
4614
4615 } else {
4616 char **p;
4617
a3c4eb07 4618 STRV_FOREACH(p, u->manager->lookup_paths.search_path) {
036643a2
LP
4619
4620 /* Instead of opening the path right away, we manually
4621 * follow all symlinks and add their name to our unit
4622 * name set while doing so */
9946996c 4623 filename = path_make_absolute(path, *p);
e48614c4
ZJS
4624 if (!filename)
4625 return -ENOMEM;
036643a2 4626
ac155bb8
MS
4627 if (u->manager->unit_path_cache &&
4628 !set_get(u->manager->unit_path_cache, filename))
fe51822e
LP
4629 r = -ENOENT;
4630 else
4631 r = open_follow(&filename, &f, symlink_names, &id);
a837f088
LP
4632 if (r >= 0)
4633 break;
a1feacf7
ZJS
4634
4635 /* ENOENT means that the file is missing or is a dangling symlink.
4636 * ENOTDIR means that one of paths we expect to be is a directory
4637 * is not a directory, we should just ignore that.
4638 * EACCES means that the directory or file permissions are wrong.
4639 */
4640 if (r == -EACCES)
4641 log_debug_errno(r, "Cannot access \"%s\": %m", filename);
4642 else if (!IN_SET(r, -ENOENT, -ENOTDIR))
a837f088 4643 return r;
fe51822e 4644
baa162ce 4645 filename = mfree(filename);
a837f088
LP
4646 /* Empty the symlink names for the next run */
4647 set_clear_free(symlink_names);
036643a2
LP
4648 }
4649 }
034c6ed7 4650
e48614c4 4651 if (!filename)
8f05424d 4652 /* Hmm, no suitable file found? */
e48614c4 4653 return 0;
87f0e418 4654
8a993b61 4655 if (!unit_type_may_alias(u->type) && set_size(symlink_names) > 1) {
a837f088
LP
4656 log_unit_warning(u, "Unit type of %s does not support alias names, refusing loading via symlink.", u->id);
4657 return -ELOOP;
4658 }
4659
23a177ef 4660 merged = u;
9946996c
LP
4661 r = merge_by_names(&merged, symlink_names, id);
4662 if (r < 0)
e48614c4 4663 return r;
87f0e418 4664
23a177ef 4665 if (merged != u) {
ac155bb8 4666 u->load_state = UNIT_MERGED;
e48614c4 4667 return 0;
034c6ed7
LP
4668 }
4669
e48614c4
ZJS
4670 if (fstat(fileno(f), &st) < 0)
4671 return -errno;
45fb0699 4672
3a8db9fe 4673 if (null_or_empty(&st)) {
ac155bb8 4674 u->load_state = UNIT_MASKED;
3a8db9fe
ZJS
4675 u->fragment_mtime = 0;
4676 } else {
c2756a68 4677 u->load_state = UNIT_LOADED;
3a8db9fe 4678 u->fragment_mtime = timespec_load(&st.st_mtim);
c2756a68 4679
00dc5d76 4680 /* Now, parse the file contents */
36f822c4
ZJS
4681 r = config_parse(u->id, filename, f,
4682 UNIT_VTABLE(u)->sections,
4683 config_item_perf_lookup, load_fragment_gperf_lookup,
bcde742e 4684 CONFIG_PARSE_ALLOW_INCLUDE, u);
f975e971 4685 if (r < 0)
e48614c4 4686 return r;
00dc5d76 4687 }
b08d03ff 4688
1cc6c93a 4689 free_and_replace(u->fragment_path, filename);
87f0e418 4690
1b64d026
LP
4691 if (u->source_path) {
4692 if (stat(u->source_path, &st) >= 0)
4693 u->source_mtime = timespec_load(&st.st_mtim);
4694 else
4695 u->source_mtime = 0;
4696 }
4697
e48614c4 4698 return 0;
0301abf4
LP
4699}
4700
e537352b 4701int unit_load_fragment(Unit *u) {
23a177ef 4702 int r;
294d81f1
LP
4703 Iterator i;
4704 const char *t;
0301abf4
LP
4705
4706 assert(u);
ac155bb8
MS
4707 assert(u->load_state == UNIT_STUB);
4708 assert(u->id);
23a177ef 4709
3f5e8115
LP
4710 if (u->transient) {
4711 u->load_state = UNIT_LOADED;
4712 return 0;
4713 }
4714
294d81f1
LP
4715 /* First, try to find the unit under its id. We always look
4716 * for unit files in the default directories, to make it easy
4717 * to override things by placing things in /etc/systemd/system */
9946996c
LP
4718 r = load_from_path(u, u->id);
4719 if (r < 0)
294d81f1
LP
4720 return r;
4721
4722 /* Try to find an alias we can load this with */
abc08d4d 4723 if (u->load_state == UNIT_STUB) {
ac155bb8 4724 SET_FOREACH(t, u->names, i) {
294d81f1 4725
ac155bb8 4726 if (t == u->id)
294d81f1
LP
4727 continue;
4728
9946996c
LP
4729 r = load_from_path(u, t);
4730 if (r < 0)
294d81f1
LP
4731 return r;
4732
ac155bb8 4733 if (u->load_state != UNIT_STUB)
294d81f1
LP
4734 break;
4735 }
abc08d4d 4736 }
23a177ef 4737
294d81f1 4738 /* And now, try looking for it under the suggested (originally linked) path */
ac155bb8 4739 if (u->load_state == UNIT_STUB && u->fragment_path) {
6ccb1b44 4740
9946996c
LP
4741 r = load_from_path(u, u->fragment_path);
4742 if (r < 0)
23a177ef 4743 return r;
0301abf4 4744
ece174c5 4745 if (u->load_state == UNIT_STUB)
6ccb1b44
LP
4746 /* Hmm, this didn't work? Then let's get rid
4747 * of the fragment path stored for us, so that
4748 * we don't point to an invalid location. */
a1e58e8e 4749 u->fragment_path = mfree(u->fragment_path);
6ccb1b44
LP
4750 }
4751
294d81f1 4752 /* Look for a template */
ac155bb8 4753 if (u->load_state == UNIT_STUB && u->instance) {
7410616c 4754 _cleanup_free_ char *k = NULL;
294d81f1 4755
7410616c
LP
4756 r = unit_name_template(u->id, &k);
4757 if (r < 0)
4758 return r;
294d81f1
LP
4759
4760 r = load_from_path(u, k);
bb28e684
ZJS
4761 if (r < 0) {
4762 if (r == -ENOEXEC)
4763 log_unit_notice(u, "Unit configuration has fatal error, unit will not be started.");
9e2f7c11 4764 return r;
bb28e684 4765 }
890f434c 4766
abc08d4d 4767 if (u->load_state == UNIT_STUB) {
ac155bb8 4768 SET_FOREACH(t, u->names, i) {
bc9fd78c 4769 _cleanup_free_ char *z = NULL;
87f0e418 4770
ac155bb8 4771 if (t == u->id)
23a177ef 4772 continue;
071830ff 4773
7410616c
LP
4774 r = unit_name_template(t, &z);
4775 if (r < 0)
4776 return r;
294d81f1 4777
bc9fd78c 4778 r = load_from_path(u, z);
294d81f1 4779 if (r < 0)
23a177ef 4780 return r;
890f434c 4781
ac155bb8 4782 if (u->load_state != UNIT_STUB)
23a177ef
LP
4783 break;
4784 }
abc08d4d 4785 }
071830ff
LP
4786 }
4787
23a177ef 4788 return 0;
3efd4195 4789}
e537352b
LP
4790
4791void unit_dump_config_items(FILE *f) {
f975e971
LP
4792 static const struct {
4793 const ConfigParserCallback callback;
4794 const char *rvalue;
4795 } table[] = {
17df7223 4796 { config_parse_warn_compat, "NOTSUPPORTED" },
f975e971
LP
4797 { config_parse_int, "INTEGER" },
4798 { config_parse_unsigned, "UNSIGNED" },
5556b5fe 4799 { config_parse_iec_size, "SIZE" },
59f448cf 4800 { config_parse_iec_uint64, "SIZE" },
5556b5fe 4801 { config_parse_si_size, "SIZE" },
f975e971
LP
4802 { config_parse_bool, "BOOLEAN" },
4803 { config_parse_string, "STRING" },
4804 { config_parse_path, "PATH" },
4805 { config_parse_unit_path_printf, "PATH" },
4806 { config_parse_strv, "STRING [...]" },
4807 { config_parse_exec_nice, "NICE" },
4808 { config_parse_exec_oom_score_adjust, "OOMSCOREADJUST" },
4809 { config_parse_exec_io_class, "IOCLASS" },
4810 { config_parse_exec_io_priority, "IOPRIORITY" },
4811 { config_parse_exec_cpu_sched_policy, "CPUSCHEDPOLICY" },
4812 { config_parse_exec_cpu_sched_prio, "CPUSCHEDPRIO" },
4813 { config_parse_exec_cpu_affinity, "CPUAFFINITY" },
4814 { config_parse_mode, "MODE" },
4815 { config_parse_unit_env_file, "FILE" },
52c239d7
LB
4816 { config_parse_exec_output, "OUTPUT" },
4817 { config_parse_exec_input, "INPUT" },
ca37242e
LP
4818 { config_parse_log_facility, "FACILITY" },
4819 { config_parse_log_level, "LEVEL" },
f975e971 4820 { config_parse_exec_secure_bits, "SECUREBITS" },
a103496c 4821 { config_parse_capability_set, "BOUNDINGSET" },
4f424df7 4822 { config_parse_rlimit, "LIMIT" },
f975e971 4823 { config_parse_unit_deps, "UNIT [...]" },
f975e971
LP
4824 { config_parse_exec, "PATH [ARGUMENT [...]]" },
4825 { config_parse_service_type, "SERVICETYPE" },
4826 { config_parse_service_restart, "SERVICERESTART" },
f975e971 4827 { config_parse_kill_mode, "KILLMODE" },
f757855e 4828 { config_parse_signal, "SIGNAL" },
f975e971
LP
4829 { config_parse_socket_listen, "SOCKET [...]" },
4830 { config_parse_socket_bind, "SOCKETBIND" },
4831 { config_parse_socket_bindtodevice, "NETWORKINTERFACE" },
7f602784 4832 { config_parse_sec, "SECONDS" },
d88a251b 4833 { config_parse_nsec, "NANOSECONDS" },
94828d2d 4834 { config_parse_namespace_path_strv, "PATH [...]" },
d2d6c096 4835 { config_parse_bind_paths, "PATH[:PATH[:OPTIONS]] [...]" },
7c8fa05c 4836 { config_parse_unit_requires_mounts_for, "PATH [...]" },
f975e971
LP
4837 { config_parse_exec_mount_flags, "MOUNTFLAG [...]" },
4838 { config_parse_unit_string_printf, "STRING" },
3ecaa09b 4839 { config_parse_trigger_unit, "UNIT" },
f975e971 4840 { config_parse_timer, "TIMER" },
f975e971 4841 { config_parse_path_spec, "PATH" },
f975e971
LP
4842 { config_parse_notify_access, "ACCESS" },
4843 { config_parse_ip_tos, "TOS" },
4844 { config_parse_unit_condition_path, "CONDITION" },
4845 { config_parse_unit_condition_string, "CONDITION" },
4846 { config_parse_unit_condition_null, "CONDITION" },
a016b922 4847 { config_parse_unit_slice, "SLICE" },
7f0386f6
LP
4848 { config_parse_documentation, "URL" },
4849 { config_parse_service_timeout, "SECONDS" },
87a47f99 4850 { config_parse_emergency_action, "ACTION" },
7f0386f6
LP
4851 { config_parse_set_status, "STATUS" },
4852 { config_parse_service_sockets, "SOCKETS" },
7f0386f6 4853 { config_parse_environ, "ENVIRON" },
349cc4a5 4854#if HAVE_SECCOMP
17df7223 4855 { config_parse_syscall_filter, "SYSCALLS" },
6a6751fe 4856 { config_parse_syscall_archs, "ARCHS" },
17df7223 4857 { config_parse_syscall_errno, "ERRNO" },
4298d0b5 4858 { config_parse_address_families, "FAMILIES" },
add00535 4859 { config_parse_restrict_namespaces, "NAMESPACES" },
c0467cf3 4860#endif
7f0386f6 4861 { config_parse_cpu_shares, "SHARES" },
984faf29 4862 { config_parse_cg_weight, "WEIGHT" },
7f0386f6
LP
4863 { config_parse_memory_limit, "LIMIT" },
4864 { config_parse_device_allow, "DEVICE" },
4865 { config_parse_device_policy, "POLICY" },
13c31542 4866 { config_parse_io_limit, "LIMIT" },
13c31542 4867 { config_parse_io_device_weight, "DEVICEWEIGHT" },
6ae4283c 4868 { config_parse_io_device_latency, "DEVICELATENCY" },
7f0386f6
LP
4869 { config_parse_blockio_bandwidth, "BANDWIDTH" },
4870 { config_parse_blockio_weight, "WEIGHT" },
4871 { config_parse_blockio_device_weight, "DEVICEWEIGHT" },
4872 { config_parse_long, "LONG" },
4873 { config_parse_socket_service, "SERVICE" },
349cc4a5 4874#if HAVE_SELINUX
6a6751fe
LP
4875 { config_parse_exec_selinux_context, "LABEL" },
4876#endif
4877 { config_parse_job_mode, "MODE" },
4878 { config_parse_job_mode_isolate, "BOOLEAN" },
4298d0b5 4879 { config_parse_personality, "PERSONALITY" },
f975e971
LP
4880 };
4881
4882 const char *prev = NULL;
4883 const char *i;
4884
4885 assert(f);
e537352b 4886
f975e971
LP
4887 NULSTR_FOREACH(i, load_fragment_gperf_nulstr) {
4888 const char *rvalue = "OTHER", *lvalue;
313b7856 4889 const ConfigPerfItem *p;
f975e971
LP
4890 size_t prefix_len;
4891 const char *dot;
313b7856 4892 unsigned j;
f975e971
LP
4893
4894 assert_se(p = load_fragment_gperf_lookup(i, strlen(i)));
4895
313b7856
LP
4896 /* Hide legacy settings */
4897 if (p->parse == config_parse_warn_compat &&
4898 p->ltype == DISABLED_LEGACY)
4899 continue;
4900
4901 for (j = 0; j < ELEMENTSOF(table); j++)
4902 if (p->parse == table[j].callback) {
4903 rvalue = table[j].rvalue;
4904 break;
4905 }
4906
f975e971
LP
4907 dot = strchr(i, '.');
4908 lvalue = dot ? dot + 1 : i;
4909 prefix_len = dot-i;
4910
4911 if (dot)
641906e9 4912 if (!prev || !strneq(prev, i, prefix_len+1)) {
f975e971
LP
4913 if (prev)
4914 fputc('\n', f);
4915
4916 fprintf(f, "[%.*s]\n", (int) prefix_len, i);
4917 }
4918
f975e971
LP
4919 fprintf(f, "%s=%s\n", lvalue, rvalue);
4920 prev = i;
4921 }
e537352b 4922}