]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/load-fragment.c
service: handle abort stops with dedicated timeout
[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
YW
3139 if (bytes >= UINT64_MAX ||
3140 (bytes <= 0 && !streq(lvalue, "MemorySwapMax"))) {
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;
3152 } else if (streq(lvalue, "MemoryMin"))
48422635 3153 c->memory_min = bytes;
c52db42b 3154 else if (streq(lvalue, "MemoryLow")) {
da4d897e 3155 c->memory_low = bytes;
c52db42b
CD
3156 c->memory_low_set = true;
3157 } else if (streq(lvalue, "MemoryHigh"))
da4d897e
TH
3158 c->memory_high = bytes;
3159 else if (streq(lvalue, "MemoryMax"))
3160 c->memory_max = bytes;
96e131ea
WC
3161 else if (streq(lvalue, "MemorySwapMax"))
3162 c->memory_swap_max = bytes;
3163 else if (streq(lvalue, "MemoryLimit"))
da4d897e 3164 c->memory_limit = bytes;
96e131ea
WC
3165 else
3166 return -EINVAL;
4ad49000 3167
4ad49000
LP
3168 return 0;
3169}
3170
03a7b521
LP
3171int config_parse_tasks_max(
3172 const char *unit,
3173 const char *filename,
3174 unsigned line,
3175 const char *section,
3176 unsigned section_line,
3177 const char *lvalue,
3178 int ltype,
3179 const char *rvalue,
3180 void *data,
3181 void *userdata) {
3182
f5058264
TH
3183 uint64_t *tasks_max = data, v;
3184 Unit *u = userdata;
03a7b521
LP
3185 int r;
3186
f5058264 3187 if (isempty(rvalue)) {
958b8c7b 3188 *tasks_max = u ? u->manager->default_tasks_max : UINT64_MAX;
f5058264
TH
3189 return 0;
3190 }
3191
3192 if (streq(rvalue, "infinity")) {
3193 *tasks_max = CGROUP_LIMIT_MAX;
03a7b521
LP
3194 return 0;
3195 }
3196
f806dfd3 3197 r = parse_permille(rvalue);
83f8e808 3198 if (r < 0) {
f5058264 3199 r = safe_atou64(rvalue, &v);
83f8e808 3200 if (r < 0) {
063c4b1a 3201 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid maximum tasks value '%s', ignoring: %m", rvalue);
83f8e808
LP
3202 return 0;
3203 }
3204 } else
f806dfd3 3205 v = system_tasks_max_scale(r, 1000U);
83f8e808 3206
f5058264 3207 if (v <= 0 || v >= UINT64_MAX) {
063c4b1a 3208 log_syntax(unit, LOG_ERR, filename, line, 0, "Maximum tasks value '%s' out of range, ignoring.", rvalue);
03a7b521
LP
3209 return 0;
3210 }
3211
f5058264 3212 *tasks_max = v;
03a7b521
LP
3213 return 0;
3214}
3215
02638280
LP
3216int config_parse_delegate(
3217 const char *unit,
3218 const char *filename,
3219 unsigned line,
3220 const char *section,
3221 unsigned section_line,
3222 const char *lvalue,
3223 int ltype,
3224 const char *rvalue,
3225 void *data,
3226 void *userdata) {
3227
3228 CGroupContext *c = data;
ecae73d7 3229 UnitType t;
02638280
LP
3230 int r;
3231
ecae73d7
ZJS
3232 t = unit_name_to_type(unit);
3233 assert(t != _UNIT_TYPE_INVALID);
3234
3235 if (!unit_vtable[t]->can_delegate) {
3236 log_syntax(unit, LOG_ERR, filename, line, 0, "Delegate= setting not supported for this unit type, ignoring.");
3237 return 0;
3238 }
3239
02638280
LP
3240 /* We either accept a boolean value, which may be used to turn on delegation for all controllers, or turn it
3241 * off for all. Or it takes a list of controller names, in which case we add the specified controllers to the
3242 * mask to delegate. */
3243
1bdfc7b9 3244 if (isempty(rvalue)) {
d48013f8
YW
3245 /* An empty string resets controllers and set Delegate=yes. */
3246 c->delegate = true;
1bdfc7b9
YW
3247 c->delegate_controllers = 0;
3248 return 0;
3249 }
3250
02638280
LP
3251 r = parse_boolean(rvalue);
3252 if (r < 0) {
3253 const char *p = rvalue;
3254 CGroupMask mask = 0;
3255
3256 for (;;) {
3257 _cleanup_free_ char *word = NULL;
3258 CGroupController cc;
3259
3260 r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES);
3261 if (r == 0)
3262 break;
3263 if (r == -ENOMEM)
3264 return log_oom();
3265 if (r < 0) {
3266 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
ff1b8455 3267 return 0;
02638280
LP
3268 }
3269
3270 cc = cgroup_controller_from_string(word);
3271 if (cc < 0) {
063c4b1a 3272 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid controller name '%s', ignoring", word);
02638280
LP
3273 continue;
3274 }
3275
3276 mask |= CGROUP_CONTROLLER_TO_MASK(cc);
3277 }
3278
3279 c->delegate = true;
3280 c->delegate_controllers |= mask;
3281
3282 } else if (r > 0) {
3283 c->delegate = true;
3284 c->delegate_controllers = _CGROUP_MASK_ALL;
3285 } else {
3286 c->delegate = false;
3287 c->delegate_controllers = 0;
3288 }
3289
3290 return 0;
3291}
3292
4ad49000
LP
3293int config_parse_device_allow(
3294 const char *unit,
3295 const char *filename,
3296 unsigned line,
3297 const char *section,
71a61510 3298 unsigned section_line,
4ad49000
LP
3299 const char *lvalue,
3300 int ltype,
3301 const char *rvalue,
3302 void *data,
3303 void *userdata) {
3304
c9f620bf 3305 _cleanup_free_ char *path = NULL, *resolved = NULL;
4ad49000 3306 CGroupContext *c = data;
c9f620bf 3307 const char *p = rvalue;
1116e14c 3308 int r;
4ad49000
LP
3309
3310 if (isempty(rvalue)) {
3311 while (c->device_allow)
3312 cgroup_context_free_device_allow(c, c->device_allow);
3313
3314 return 0;
3315 }
3316
c9f620bf
YW
3317 r = extract_first_word(&p, &path, NULL, EXTRACT_QUOTES);
3318 if (r == -ENOMEM)
3319 return log_oom();
508f63b4 3320 if (r < 0) {
1116e14c 3321 log_syntax(unit, LOG_WARNING, filename, line, r,
c9f620bf
YW
3322 "Invalid syntax, ignoring: %s", rvalue);
3323 return 0;
3324 }
3325 if (r == 0) {
3326 log_syntax(unit, LOG_WARNING, filename, line, 0,
3327 "Failed to extract device path and rights from '%s', ignoring.", rvalue);
20d52ab6 3328 return 0;
1116e14c
NBS
3329 }
3330
c9f620bf
YW
3331 r = unit_full_printf(userdata, path, &resolved);
3332 if (r < 0) {
3333 log_syntax(unit, LOG_WARNING, filename, line, r,
3334 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
4ad49000
LP
3335 return 0;
3336 }
3337
49fe5c09 3338 if (!STARTSWITH_SET(resolved, "block-", "char-")) {
2f4d31c1 3339
57e84e75
LP
3340 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
3341 if (r < 0)
3342 return 0;
3343
3344 if (!valid_device_node_path(resolved)) {
3345 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid device node path '%s', ignoring.", resolved);
3346 return 0;
3347 }
c9f620bf 3348 }
4ad49000 3349
c9f620bf
YW
3350 if (!isempty(p) && !in_charset(p, "rwm")) {
3351 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid device rights '%s', ignoring.", p);
4ad49000
LP
3352 return 0;
3353 }
3354
fd870bac 3355 return cgroup_add_device_allow(c, resolved, p);
4ad49000
LP
3356}
3357
13c31542
TH
3358int config_parse_io_device_weight(
3359 const char *unit,
3360 const char *filename,
3361 unsigned line,
3362 const char *section,
3363 unsigned section_line,
3364 const char *lvalue,
3365 int ltype,
3366 const char *rvalue,
3367 void *data,
3368 void *userdata) {
3369
c9f620bf 3370 _cleanup_free_ char *path = NULL, *resolved = NULL;
13c31542
TH
3371 CGroupIODeviceWeight *w;
3372 CGroupContext *c = data;
c9f620bf 3373 const char *p = rvalue;
13c31542 3374 uint64_t u;
13c31542
TH
3375 int r;
3376
3377 assert(filename);
3378 assert(lvalue);
3379 assert(rvalue);
3380
3381 if (isempty(rvalue)) {
3382 while (c->io_device_weights)
3383 cgroup_context_free_io_device_weight(c, c->io_device_weights);
3384
3385 return 0;
3386 }
3387
c9f620bf
YW
3388 r = extract_first_word(&p, &path, NULL, EXTRACT_QUOTES);
3389 if (r == -ENOMEM)
3390 return log_oom();
3391 if (r < 0) {
3392 log_syntax(unit, LOG_WARNING, filename, line, r,
3393 "Invalid syntax, ignoring: %s", rvalue);
3394 return 0;
3395 }
3396 if (r == 0 || isempty(p)) {
3397 log_syntax(unit, LOG_WARNING, filename, line, 0,
3398 "Failed to extract device path and weight from '%s', ignoring.", rvalue);
13c31542
TH
3399 return 0;
3400 }
3401
c9f620bf
YW
3402 r = unit_full_printf(userdata, path, &resolved);
3403 if (r < 0) {
3404 log_syntax(unit, LOG_WARNING, filename, line, r,
3405 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
3406 return 0;
3407 }
13c31542 3408
2f4d31c1
YW
3409 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
3410 if (r < 0)
3411 return 0;
3412
c9f620bf 3413 r = cg_weight_parse(p, &u);
13c31542 3414 if (r < 0) {
c9f620bf 3415 log_syntax(unit, LOG_ERR, filename, line, r, "IO weight '%s' invalid, ignoring: %m", p);
13c31542
TH
3416 return 0;
3417 }
3418
3419 assert(u != CGROUP_WEIGHT_INVALID);
3420
3421 w = new0(CGroupIODeviceWeight, 1);
3422 if (!w)
3423 return log_oom();
3424
c9f620bf 3425 w->path = TAKE_PTR(resolved);
13c31542
TH
3426 w->weight = u;
3427
3428 LIST_PREPEND(device_weights, c->io_device_weights, w);
3429 return 0;
3430}
3431
6ae4283c
TH
3432int config_parse_io_device_latency(
3433 const char *unit,
3434 const char *filename,
3435 unsigned line,
3436 const char *section,
3437 unsigned section_line,
3438 const char *lvalue,
3439 int ltype,
3440 const char *rvalue,
3441 void *data,
3442 void *userdata) {
3443
3444 _cleanup_free_ char *path = NULL, *resolved = NULL;
3445 CGroupIODeviceLatency *l;
3446 CGroupContext *c = data;
3447 const char *p = rvalue;
3448 usec_t usec;
3449 int r;
3450
3451 assert(filename);
3452 assert(lvalue);
3453 assert(rvalue);
3454
3455 if (isempty(rvalue)) {
3456 while (c->io_device_latencies)
3457 cgroup_context_free_io_device_latency(c, c->io_device_latencies);
3458
3459 return 0;
3460 }
3461
3462 r = extract_first_word(&p, &path, NULL, EXTRACT_QUOTES);
3463 if (r == -ENOMEM)
3464 return log_oom();
3465 if (r < 0) {
3466 log_syntax(unit, LOG_WARNING, filename, line, r,
3467 "Invalid syntax, ignoring: %s", rvalue);
3468 return 0;
3469 }
3470 if (r == 0 || isempty(p)) {
3471 log_syntax(unit, LOG_WARNING, filename, line, 0,
3472 "Failed to extract device path and latency from '%s', ignoring.", rvalue);
3473 return 0;
3474 }
3475
3476 r = unit_full_printf(userdata, path, &resolved);
3477 if (r < 0) {
3478 log_syntax(unit, LOG_WARNING, filename, line, r,
3479 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
3480 return 0;
3481 }
3482
3483 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
3484 if (r < 0)
3485 return 0;
3486
3487 if (parse_sec(p, &usec) < 0) {
3488 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse timer value, ignoring: %s", p);
3489 return 0;
3490 }
3491
3492 l = new0(CGroupIODeviceLatency, 1);
3493 if (!l)
3494 return log_oom();
3495
3496 l->path = TAKE_PTR(resolved);
3497 l->target_usec = usec;
3498
3499 LIST_PREPEND(device_latencies, c->io_device_latencies, l);
3500 return 0;
3501}
3502
13c31542
TH
3503int config_parse_io_limit(
3504 const char *unit,
3505 const char *filename,
3506 unsigned line,
3507 const char *section,
3508 unsigned section_line,
3509 const char *lvalue,
3510 int ltype,
3511 const char *rvalue,
3512 void *data,
3513 void *userdata) {
3514
c9f620bf 3515 _cleanup_free_ char *path = NULL, *resolved = NULL;
13c31542
TH
3516 CGroupIODeviceLimit *l = NULL, *t;
3517 CGroupContext *c = data;
9be57249 3518 CGroupIOLimitType type;
c9f620bf 3519 const char *p = rvalue;
13c31542 3520 uint64_t num;
13c31542
TH
3521 int r;
3522
3523 assert(filename);
3524 assert(lvalue);
3525 assert(rvalue);
3526
9be57249
TH
3527 type = cgroup_io_limit_type_from_string(lvalue);
3528 assert(type >= 0);
13c31542
TH
3529
3530 if (isempty(rvalue)) {
3531 LIST_FOREACH(device_limits, l, c->io_device_limits)
9be57249 3532 l->limits[type] = cgroup_io_limit_defaults[type];
13c31542
TH
3533 return 0;
3534 }
3535
c9f620bf
YW
3536 r = extract_first_word(&p, &path, NULL, EXTRACT_QUOTES);
3537 if (r == -ENOMEM)
3538 return log_oom();
3539 if (r < 0) {
3540 log_syntax(unit, LOG_WARNING, filename, line, r,
3541 "Invalid syntax, ignoring: %s", rvalue);
3542 return 0;
3543 }
3544 if (r == 0 || isempty(p)) {
3545 log_syntax(unit, LOG_WARNING, filename, line, 0,
3546 "Failed to extract device node and bandwidth from '%s', ignoring.", rvalue);
13c31542
TH
3547 return 0;
3548 }
3549
c9f620bf
YW
3550 r = unit_full_printf(userdata, path, &resolved);
3551 if (r < 0) {
3552 log_syntax(unit, LOG_WARNING, filename, line, r,
3553 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
3554 return 0;
3555 }
13c31542 3556
2f4d31c1
YW
3557 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
3558 if (r < 0)
3559 return 0;
3560
9d5e9b4a 3561 if (streq("infinity", p))
13c31542 3562 num = CGROUP_LIMIT_MAX;
9d5e9b4a 3563 else {
c9f620bf 3564 r = parse_size(p, 1000, &num);
13c31542 3565 if (r < 0 || num <= 0) {
c9f620bf 3566 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid IO limit '%s', ignoring.", p);
13c31542
TH
3567 return 0;
3568 }
3569 }
3570
3571 LIST_FOREACH(device_limits, t, c->io_device_limits) {
c9f620bf 3572 if (path_equal(resolved, t->path)) {
13c31542
TH
3573 l = t;
3574 break;
3575 }
3576 }
3577
3578 if (!l) {
9be57249
TH
3579 CGroupIOLimitType ttype;
3580
13c31542
TH
3581 l = new0(CGroupIODeviceLimit, 1);
3582 if (!l)
3583 return log_oom();
3584
c9f620bf 3585 l->path = TAKE_PTR(resolved);
9be57249
TH
3586 for (ttype = 0; ttype < _CGROUP_IO_LIMIT_TYPE_MAX; ttype++)
3587 l->limits[ttype] = cgroup_io_limit_defaults[ttype];
13c31542
TH
3588
3589 LIST_PREPEND(device_limits, c->io_device_limits, l);
3590 }
3591
9be57249 3592 l->limits[type] = num;
13c31542
TH
3593
3594 return 0;
3595}
3596
8e7076ca
LP
3597int config_parse_blockio_device_weight(
3598 const char *unit,
3599 const char *filename,
3600 unsigned line,
3601 const char *section,
71a61510 3602 unsigned section_line,
8e7076ca
LP
3603 const char *lvalue,
3604 int ltype,
3605 const char *rvalue,
3606 void *data,
3607 void *userdata) {
3608
c9f620bf 3609 _cleanup_free_ char *path = NULL, *resolved = NULL;
8e7076ca 3610 CGroupBlockIODeviceWeight *w;
4ad49000 3611 CGroupContext *c = data;
c9f620bf 3612 const char *p = rvalue;
d53d9474 3613 uint64_t u;
4ad49000
LP
3614 int r;
3615
3616 assert(filename);
3617 assert(lvalue);
3618 assert(rvalue);
3619
3620 if (isempty(rvalue)) {
4ad49000
LP
3621 while (c->blockio_device_weights)
3622 cgroup_context_free_blockio_device_weight(c, c->blockio_device_weights);
3623
3624 return 0;
3625 }
3626
c9f620bf
YW
3627 r = extract_first_word(&p, &path, NULL, EXTRACT_QUOTES);
3628 if (r == -ENOMEM)
3629 return log_oom();
3630 if (r < 0) {
3631 log_syntax(unit, LOG_WARNING, filename, line, r,
3632 "Invalid syntax, ignoring: %s", rvalue);
3633 return 0;
3634 }
3635 if (r == 0 || isempty(p)) {
3636 log_syntax(unit, LOG_WARNING, filename, line, 0,
3637 "Failed to extract device node and weight from '%s', ignoring.", rvalue);
8e7076ca
LP
3638 return 0;
3639 }
4ad49000 3640
c9f620bf
YW
3641 r = unit_full_printf(userdata, path, &resolved);
3642 if (r < 0) {
3643 log_syntax(unit, LOG_WARNING, filename, line, r,
3644 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
3645 return 0;
3646 }
4ad49000 3647
2f4d31c1
YW
3648 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
3649 if (r < 0)
3650 return 0;
3651
c9f620bf 3652 r = cg_blkio_weight_parse(p, &u);
d53d9474 3653 if (r < 0) {
c9f620bf 3654 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid block IO weight '%s', ignoring: %m", p);
4ad49000
LP
3655 return 0;
3656 }
3657
d53d9474
LP
3658 assert(u != CGROUP_BLKIO_WEIGHT_INVALID);
3659
8e7076ca
LP
3660 w = new0(CGroupBlockIODeviceWeight, 1);
3661 if (!w)
3662 return log_oom();
4ad49000 3663
c9f620bf 3664 w->path = TAKE_PTR(resolved);
d53d9474 3665 w->weight = u;
4ad49000 3666
71fda00f 3667 LIST_PREPEND(device_weights, c->blockio_device_weights, w);
4ad49000
LP
3668 return 0;
3669}
3670
3671int config_parse_blockio_bandwidth(
3672 const char *unit,
3673 const char *filename,
3674 unsigned line,
3675 const char *section,
71a61510 3676 unsigned section_line,
4ad49000
LP
3677 const char *lvalue,
3678 int ltype,
3679 const char *rvalue,
3680 void *data,
3681 void *userdata) {
3682
c9f620bf 3683 _cleanup_free_ char *path = NULL, *resolved = NULL;
979d0311 3684 CGroupBlockIODeviceBandwidth *b = NULL, *t;
4ad49000 3685 CGroupContext *c = data;
c9f620bf 3686 const char *p = rvalue;
59f448cf 3687 uint64_t bytes;
47c0980d 3688 bool read;
4ad49000
LP
3689 int r;
3690
3691 assert(filename);
3692 assert(lvalue);
3693 assert(rvalue);
3694
47c0980d
G
3695 read = streq("BlockIOReadBandwidth", lvalue);
3696
4ad49000 3697 if (isempty(rvalue)) {
979d0311
TH
3698 LIST_FOREACH(device_bandwidths, b, c->blockio_device_bandwidths) {
3699 b->rbps = CGROUP_LIMIT_MAX;
3700 b->wbps = CGROUP_LIMIT_MAX;
3701 }
4ad49000
LP
3702 return 0;
3703 }
3704
c9f620bf
YW
3705 r = extract_first_word(&p, &path, NULL, EXTRACT_QUOTES);
3706 if (r == -ENOMEM)
3707 return log_oom();
3708 if (r < 0) {
3709 log_syntax(unit, LOG_WARNING, filename, line, r,
3710 "Invalid syntax, ignoring: %s", rvalue);
3711 return 0;
3712 }
3713 if (r == 0 || isempty(p)) {
3714 log_syntax(unit, LOG_WARNING, filename, line, 0,
3715 "Failed to extract device node and bandwidth from '%s', ignoring.", rvalue);
4ad49000
LP
3716 return 0;
3717 }
3718
c9f620bf
YW
3719 r = unit_full_printf(userdata, path, &resolved);
3720 if (r < 0) {
3721 log_syntax(unit, LOG_WARNING, filename, line, r,
3722 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
3723 return 0;
3724 }
4ad49000 3725
2f4d31c1
YW
3726 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
3727 if (r < 0)
3728 return 0;
3729
c9f620bf 3730 r = parse_size(p, 1000, &bytes);
4ad49000 3731 if (r < 0 || bytes <= 0) {
c9f620bf 3732 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid Block IO Bandwidth '%s', ignoring.", p);
4ad49000
LP
3733 return 0;
3734 }
3735
979d0311 3736 LIST_FOREACH(device_bandwidths, t, c->blockio_device_bandwidths) {
c9f620bf 3737 if (path_equal(resolved, t->path)) {
979d0311
TH
3738 b = t;
3739 break;
3740 }
3741 }
4ad49000 3742
979d0311
TH
3743 if (!t) {
3744 b = new0(CGroupBlockIODeviceBandwidth, 1);
3745 if (!b)
3746 return log_oom();
3747
c9f620bf 3748 b->path = TAKE_PTR(resolved);
979d0311
TH
3749 b->rbps = CGROUP_LIMIT_MAX;
3750 b->wbps = CGROUP_LIMIT_MAX;
3751
3752 LIST_PREPEND(device_bandwidths, c->blockio_device_bandwidths, b);
3753 }
4ad49000 3754
979d0311
TH
3755 if (read)
3756 b->rbps = bytes;
3757 else
3758 b->wbps = bytes;
4ad49000
LP
3759
3760 return 0;
3761}
3762
d420282b
LP
3763int config_parse_job_mode_isolate(
3764 const char *unit,
3765 const char *filename,
3766 unsigned line,
3767 const char *section,
3768 unsigned section_line,
3769 const char *lvalue,
3770 int ltype,
3771 const char *rvalue,
3772 void *data,
3773 void *userdata) {
3774
3775 JobMode *m = data;
3776 int r;
3777
3778 assert(filename);
3779 assert(lvalue);
3780 assert(rvalue);
3781
3782 r = parse_boolean(rvalue);
3783 if (r < 0) {
12ca818f 3784 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse boolean, ignoring: %s", rvalue);
d420282b
LP
3785 return 0;
3786 }
3787
8ab39347
YW
3788 log_notice("%s is deprecated. Please use OnFailureJobMode= instead", lvalue);
3789
d420282b
LP
3790 *m = r ? JOB_ISOLATE : JOB_REPLACE;
3791 return 0;
3792}
3793
3536f49e 3794int config_parse_exec_directories(
e66cf1a3
LP
3795 const char *unit,
3796 const char *filename,
3797 unsigned line,
3798 const char *section,
3799 unsigned section_line,
3800 const char *lvalue,
3801 int ltype,
3802 const char *rvalue,
3803 void *data,
3804 void *userdata) {
3805
a2a5291b 3806 char***rt = data;
9b5864d9 3807 Unit *u = userdata;
035fe294 3808 const char *p;
e66cf1a3
LP
3809 int r;
3810
3811 assert(filename);
3812 assert(lvalue);
3813 assert(rvalue);
3814 assert(data);
3815
3816 if (isempty(rvalue)) {
3817 /* Empty assignment resets the list */
6796073e 3818 *rt = strv_free(*rt);
e66cf1a3
LP
3819 return 0;
3820 }
3821
035fe294
ZJS
3822 for (p = rvalue;;) {
3823 _cleanup_free_ char *word = NULL, *k = NULL;
e66cf1a3 3824
035fe294 3825 r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES);
035fe294 3826 if (r == -ENOMEM)
e66cf1a3 3827 return log_oom();
035fe294
ZJS
3828 if (r < 0) {
3829 log_syntax(unit, LOG_WARNING, filename, line, r,
3830 "Invalid syntax, ignoring: %s", rvalue);
3831 return 0;
3832 }
091e9efe
LP
3833 if (r == 0)
3834 return 0;
e66cf1a3 3835
18913df9 3836 r = unit_full_printf(u, word, &k);
9b5864d9 3837 if (r < 0) {
035fe294 3838 log_syntax(unit, LOG_ERR, filename, line, r,
063c4b1a 3839 "Failed to resolve unit specifiers in \"%s\", ignoring: %m", word);
9b5864d9
MG
3840 continue;
3841 }
3842
2f4d31c1
YW
3843 r = path_simplify_and_warn(k, PATH_CHECK_RELATIVE, unit, filename, line, lvalue);
3844 if (r < 0)
e8865688 3845 continue;
e8865688
LP
3846
3847 if (path_startswith(k, "private")) {
3848 log_syntax(unit, LOG_ERR, filename, line, 0,
4605de11 3849 "%s= path can't be 'private', ignoring assignment: %s", lvalue, word);
e66cf1a3
LP
3850 continue;
3851 }
3852
035fe294 3853 r = strv_push(rt, k);
e66cf1a3
LP
3854 if (r < 0)
3855 return log_oom();
035fe294 3856 k = NULL;
e66cf1a3 3857 }
e66cf1a3
LP
3858}
3859
3af00fb8
LP
3860int config_parse_set_status(
3861 const char *unit,
3862 const char *filename,
3863 unsigned line,
3864 const char *section,
3865 unsigned section_line,
3866 const char *lvalue,
3867 int ltype,
3868 const char *rvalue,
3869 void *data,
3870 void *userdata) {
3871
3af00fb8 3872 size_t l;
a2a5291b 3873 const char *word, *state;
3af00fb8
LP
3874 int r;
3875 ExitStatusSet *status_set = data;
3876
3877 assert(filename);
3878 assert(lvalue);
3879 assert(rvalue);
3880 assert(data);
3881
3e2d435b 3882 /* Empty assignment resets the list */
3af00fb8 3883 if (isempty(rvalue)) {
3e2d435b 3884 exit_status_set_free(status_set);
3af00fb8
LP
3885 return 0;
3886 }
3887
a2a5291b 3888 FOREACH_WORD(word, l, rvalue, state) {
3af00fb8
LP
3889 _cleanup_free_ char *temp;
3890 int val;
61593865 3891 Set **set;
3af00fb8 3892
a2a5291b 3893 temp = strndup(word, l);
3af00fb8
LP
3894 if (!temp)
3895 return log_oom();
3896
3897 r = safe_atoi(temp, &val);
3898 if (r < 0) {
29a3db75 3899 val = signal_from_string(temp);
3af00fb8 3900
1e2fd62d 3901 if (val <= 0) {
12ca818f 3902 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse value, ignoring: %s", word);
61593865 3903 continue;
3af00fb8 3904 }
61593865 3905 set = &status_set->signal;
3af00fb8 3906 } else {
1e2fd62d 3907 if (val < 0 || val > 255) {
12ca818f 3908 log_syntax(unit, LOG_ERR, filename, line, 0, "Value %d is outside range 0-255, ignoring", val);
1e2fd62d 3909 continue;
3af00fb8 3910 }
61593865 3911 set = &status_set->status;
3af00fb8 3912 }
1e2fd62d 3913
61593865 3914 r = set_ensure_allocated(set, NULL);
1e2fd62d
ZJS
3915 if (r < 0)
3916 return log_oom();
3917
61593865 3918 r = set_put(*set, INT_TO_PTR(val));
063c4b1a
YW
3919 if (r < 0)
3920 return log_oom();
3af00fb8 3921 }
b2fadec6 3922 if (!isempty(state))
12ca818f 3923 log_syntax(unit, LOG_ERR, filename, line, 0, "Trailing garbage, ignoring.");
3af00fb8
LP
3924
3925 return 0;
3926}
3927
94828d2d
LP
3928int config_parse_namespace_path_strv(
3929 const char *unit,
3930 const char *filename,
3931 unsigned line,
3932 const char *section,
3933 unsigned section_line,
3934 const char *lvalue,
3935 int ltype,
3936 const char *rvalue,
3937 void *data,
3938 void *userdata) {
3939
7b07e993 3940 Unit *u = userdata;
a2a5291b 3941 char*** sv = data;
063c4b1a 3942 const char *p = rvalue;
94828d2d
LP
3943 int r;
3944
3945 assert(filename);
3946 assert(lvalue);
3947 assert(rvalue);
3948 assert(data);
3949
3950 if (isempty(rvalue)) {
3951 /* Empty assignment resets the list */
6796073e 3952 *sv = strv_free(*sv);
94828d2d
LP
3953 return 0;
3954 }
3955
727f76d7 3956 for (;;) {
7b07e993 3957 _cleanup_free_ char *word = NULL, *resolved = NULL, *joined = NULL;
20b7a007
LP
3958 const char *w;
3959 bool ignore_enoent = false, shall_prefix = false;
94828d2d 3960
063c4b1a 3961 r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES);
0293a7a8
EV
3962 if (r == 0)
3963 break;
3964 if (r == -ENOMEM)
3965 return log_oom();
727f76d7 3966 if (r < 0) {
7b07e993 3967 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to extract first word, ignoring: %s", rvalue);
727f76d7
EV
3968 return 0;
3969 }
94828d2d 3970
20b7a007
LP
3971 w = word;
3972 if (startswith(w, "-")) {
3973 ignore_enoent = true;
3974 w++;
3975 }
3976 if (startswith(w, "+")) {
3977 shall_prefix = true;
3978 w++;
3979 }
7b07e993 3980
20b7a007 3981 r = unit_full_printf(u, w, &resolved);
7b07e993 3982 if (r < 0) {
063c4b1a 3983 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s: %m", w);
94828d2d
LP
3984 continue;
3985 }
3986
2f4d31c1
YW
3987 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
3988 if (r < 0)
7b07e993 3989 continue;
94828d2d 3990
20b7a007
LP
3991 joined = strjoin(ignore_enoent ? "-" : "",
3992 shall_prefix ? "+" : "",
3993 resolved);
7b07e993
LP
3994
3995 r = strv_push(sv, joined);
94828d2d
LP
3996 if (r < 0)
3997 return log_oom();
3998
7b07e993 3999 joined = NULL;
94828d2d
LP
4000 }
4001
4002 return 0;
4003}
4004
2abd4e38
YW
4005int config_parse_temporary_filesystems(
4006 const char *unit,
4007 const char *filename,
4008 unsigned line,
4009 const char *section,
4010 unsigned section_line,
4011 const char *lvalue,
4012 int ltype,
4013 const char *rvalue,
4014 void *data,
4015 void *userdata) {
4016
4017 Unit *u = userdata;
4018 ExecContext *c = data;
063c4b1a 4019 const char *p = rvalue;
2abd4e38
YW
4020 int r;
4021
4022 assert(filename);
4023 assert(lvalue);
4024 assert(rvalue);
4025 assert(data);
4026
4027 if (isempty(rvalue)) {
4028 /* Empty assignment resets the list */
4029 temporary_filesystem_free_many(c->temporary_filesystems, c->n_temporary_filesystems);
4030 c->temporary_filesystems = NULL;
4031 c->n_temporary_filesystems = 0;
4032 return 0;
4033 }
4034
2abd4e38
YW
4035 for (;;) {
4036 _cleanup_free_ char *word = NULL, *path = NULL, *resolved = NULL;
4037 const char *w;
4038
063c4b1a 4039 r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES);
2abd4e38 4040 if (r == 0)
063c4b1a 4041 return 0;
2abd4e38
YW
4042 if (r == -ENOMEM)
4043 return log_oom();
4044 if (r < 0) {
4045 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to extract first word, ignoring: %s", rvalue);
4046 return 0;
4047 }
4048
4049 w = word;
4050 r = extract_first_word(&w, &path, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
063c4b1a
YW
4051 if (r == -ENOMEM)
4052 return log_oom();
4053 if (r < 0) {
4054 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to extract first word, ignoring: %s", word);
4055 continue;
4056 }
4057 if (r == 0) {
4058 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid syntax, ignoring: %s", word);
4059 continue;
4060 }
2abd4e38
YW
4061
4062 r = unit_full_printf(u, path, &resolved);
4063 if (r < 0) {
063c4b1a 4064 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", path);
2abd4e38
YW
4065 continue;
4066 }
4067
2f4d31c1
YW
4068 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
4069 if (r < 0)
2abd4e38 4070 continue;
2abd4e38 4071
a26fec24 4072 r = temporary_filesystem_add(&c->temporary_filesystems, &c->n_temporary_filesystems, resolved, w);
6302d1ea 4073 if (r < 0)
2abd4e38 4074 return log_oom();
2abd4e38 4075 }
2abd4e38
YW
4076}
4077
d2d6c096
LP
4078int config_parse_bind_paths(
4079 const char *unit,
4080 const char *filename,
4081 unsigned line,
4082 const char *section,
4083 unsigned section_line,
4084 const char *lvalue,
4085 int ltype,
4086 const char *rvalue,
4087 void *data,
4088 void *userdata) {
4089
4090 ExecContext *c = data;
42d43f21 4091 Unit *u = userdata;
d2d6c096
LP
4092 const char *p;
4093 int r;
4094
4095 assert(filename);
4096 assert(lvalue);
4097 assert(rvalue);
4098 assert(data);
4099
4100 if (isempty(rvalue)) {
4101 /* Empty assignment resets the list */
4102 bind_mount_free_many(c->bind_mounts, c->n_bind_mounts);
4103 c->bind_mounts = NULL;
4104 c->n_bind_mounts = 0;
4105 return 0;
4106 }
4107
4108 p = rvalue;
4109 for (;;) {
4110 _cleanup_free_ char *source = NULL, *destination = NULL;
42d43f21 4111 _cleanup_free_ char *sresolved = NULL, *dresolved = NULL;
d2d6c096
LP
4112 char *s = NULL, *d = NULL;
4113 bool rbind = true, ignore_enoent = false;
4114
4115 r = extract_first_word(&p, &source, ":" WHITESPACE, EXTRACT_QUOTES|EXTRACT_DONT_COALESCE_SEPARATORS);
4116 if (r == 0)
4117 break;
4118 if (r == -ENOMEM)
4119 return log_oom();
4120 if (r < 0) {
2f4d31c1 4121 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse %s, ignoring: %s", lvalue, rvalue);
d2d6c096
LP
4122 return 0;
4123 }
4124
42d43f21
DC
4125 r = unit_full_printf(u, source, &sresolved);
4126 if (r < 0) {
4127 log_syntax(unit, LOG_ERR, filename, line, r,
063c4b1a 4128 "Failed to resolved unit specifiers in \"%s\", ignoring: %m", source);
2f4d31c1 4129 continue;
42d43f21
DC
4130 }
4131
4132 s = sresolved;
d2d6c096
LP
4133 if (s[0] == '-') {
4134 ignore_enoent = true;
4135 s++;
4136 }
4137
2f4d31c1
YW
4138 r = path_simplify_and_warn(s, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
4139 if (r < 0)
4140 continue;
d2d6c096
LP
4141
4142 /* Optionally, the destination is specified. */
4143 if (p && p[-1] == ':') {
4144 r = extract_first_word(&p, &destination, ":" WHITESPACE, EXTRACT_QUOTES|EXTRACT_DONT_COALESCE_SEPARATORS);
4145 if (r == -ENOMEM)
4146 return log_oom();
4147 if (r < 0) {
2f4d31c1 4148 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse %s, ignoring: %s", lvalue, rvalue);
d2d6c096
LP
4149 return 0;
4150 }
4151 if (r == 0) {
2f4d31c1
YW
4152 log_syntax(unit, LOG_ERR, filename, line, 0, "Missing argument after ':', ignoring: %s", s);
4153 continue;
d2d6c096
LP
4154 }
4155
42d43f21
DC
4156 r = unit_full_printf(u, destination, &dresolved);
4157 if (r < 0) {
4158 log_syntax(unit, LOG_ERR, filename, line, r,
4159 "Failed to resolved specifiers in \"%s\", ignoring: %m", destination);
2f4d31c1 4160 continue;
42d43f21
DC
4161 }
4162
2f4d31c1
YW
4163 r = path_simplify_and_warn(dresolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
4164 if (r < 0)
4165 continue;
d2d6c096 4166
2f4d31c1 4167 d = dresolved;
d2d6c096
LP
4168
4169 /* Optionally, there's also a short option string specified */
4170 if (p && p[-1] == ':') {
4171 _cleanup_free_ char *options = NULL;
4172
4173 r = extract_first_word(&p, &options, NULL, EXTRACT_QUOTES);
4174 if (r == -ENOMEM)
4175 return log_oom();
4176 if (r < 0) {
4177 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse %s: %s", lvalue, rvalue);
4178 return 0;
4179 }
4180
4181 if (isempty(options) || streq(options, "rbind"))
4182 rbind = true;
4183 else if (streq(options, "norbind"))
4184 rbind = false;
4185 else {
4186 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid option string, ignoring setting: %s", options);
2f4d31c1 4187 continue;
d2d6c096
LP
4188 }
4189 }
4190 } else
4191 d = s;
4192
4193 r = bind_mount_add(&c->bind_mounts, &c->n_bind_mounts,
4194 &(BindMount) {
4195 .source = s,
4196 .destination = d,
4197 .read_only = !!strstr(lvalue, "ReadOnly"),
4198 .recursive = rbind,
4199 .ignore_enoent = ignore_enoent,
4200 });
4201 if (r < 0)
4202 return log_oom();
4203 }
4204
4205 return 0;
4206}
4207
eae51da3
LP
4208int config_parse_job_timeout_sec(
4209 const char* unit,
4210 const char *filename,
4211 unsigned line,
4212 const char *section,
4213 unsigned section_line,
4214 const char *lvalue,
4215 int ltype,
4216 const char *rvalue,
4217 void *data,
4218 void *userdata) {
4219
4220 Unit *u = data;
4221 usec_t usec;
4222 int r;
4223
4224 assert(filename);
4225 assert(lvalue);
4226 assert(rvalue);
4227 assert(u);
4228
4229 r = parse_sec_fix_0(rvalue, &usec);
4230 if (r < 0) {
4231 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse JobTimeoutSec= parameter, ignoring: %s", rvalue);
4232 return 0;
4233 }
4234
4235 /* If the user explicitly changed JobTimeoutSec= also change JobRunningTimeoutSec=, for compatibility with old
c05f3c8f 4236 * versions. If JobRunningTimeoutSec= was explicitly set, avoid this however as whatever the user picked should
eae51da3
LP
4237 * count. */
4238
4239 if (!u->job_running_timeout_set)
4240 u->job_running_timeout = usec;
4241
4242 u->job_timeout = usec;
4243
4244 return 0;
4245}
4246
4247int config_parse_job_running_timeout_sec(
4248 const char* unit,
4249 const char *filename,
4250 unsigned line,
4251 const char *section,
4252 unsigned section_line,
4253 const char *lvalue,
4254 int ltype,
4255 const char *rvalue,
4256 void *data,
4257 void *userdata) {
4258
4259 Unit *u = data;
4260 usec_t usec;
4261 int r;
4262
4263 assert(filename);
4264 assert(lvalue);
4265 assert(rvalue);
4266 assert(u);
4267
4268 r = parse_sec_fix_0(rvalue, &usec);
4269 if (r < 0) {
4270 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse JobRunningTimeoutSec= parameter, ignoring: %s", rvalue);
4271 return 0;
4272 }
4273
4274 u->job_running_timeout = usec;
4275 u->job_running_timeout_set = true;
4276
4277 return 0;
4278}
4279
54fcb619
ZJS
4280int config_parse_emergency_action(
4281 const char* unit,
4282 const char *filename,
4283 unsigned line,
4284 const char *section,
4285 unsigned section_line,
4286 const char *lvalue,
4287 int ltype,
4288 const char *rvalue,
4289 void *data,
4290 void *userdata) {
4291
4292 Manager *m = NULL;
4293 EmergencyAction *x = data;
4294 int r;
4295
4296 assert(filename);
4297 assert(lvalue);
4298 assert(rvalue);
4299 assert(data);
4300
4301 if (unit)
4302 m = ((Unit*) userdata)->manager;
4303 else
4304 m = data;
4305
4306 r = parse_emergency_action(rvalue, MANAGER_IS_SYSTEM(m), x);
4307 if (r < 0) {
469f76f1
ZJS
4308 if (r == -EOPNOTSUPP && MANAGER_IS_USER(m)) {
4309 /* Compat mode: remove for systemd 241. */
4310
4311 log_syntax(unit, LOG_INFO, filename, line, r,
4312 "%s= in user mode specified as \"%s\", using \"exit-force\" instead.",
4313 lvalue, rvalue);
4314 *x = EMERGENCY_ACTION_EXIT_FORCE;
4315 return 0;
4316 }
4317
54fcb619
ZJS
4318 if (r == -EOPNOTSUPP)
4319 log_syntax(unit, LOG_ERR, filename, line, r,
4320 "%s= specified as %s mode action, ignoring: %s",
4321 lvalue, MANAGER_IS_SYSTEM(m) ? "user" : "system", rvalue);
4322 else
4323 log_syntax(unit, LOG_ERR, filename, line, r,
4324 "Failed to parse %s=, ignoring: %s", lvalue, rvalue);
4325 return 0;
4326 }
4327
4328 return 0;
4329}
4330
a9353a5c
LP
4331int config_parse_pid_file(
4332 const char *unit,
4333 const char *filename,
4334 unsigned line,
4335 const char *section,
4336 unsigned section_line,
4337 const char *lvalue,
4338 int ltype,
4339 const char *rvalue,
4340 void *data,
4341 void *userdata) {
4342
4343 _cleanup_free_ char *k = NULL, *n = NULL;
4344 Unit *u = userdata;
4345 char **s = data;
a9353a5c
LP
4346 int r;
4347
4348 assert(filename);
4349 assert(lvalue);
4350 assert(rvalue);
4351 assert(u);
4352
b8055c05
YW
4353 if (isempty(rvalue)) {
4354 /* An empty assignment removes already set value. */
4355 *s = mfree(*s);
4356 return 0;
4357 }
4358
a9353a5c
LP
4359 r = unit_full_printf(u, rvalue, &k);
4360 if (r < 0) {
4361 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
4362 return 0;
4363 }
4364
4365 /* If this is a relative path make it absolute by prefixing the /run */
4366 n = path_make_absolute(k, u->manager->prefix[EXEC_DIRECTORY_RUNTIME]);
4367 if (!n)
4368 return log_oom();
4369
4370 /* Check that the result is a sensible path */
4371 r = path_simplify_and_warn(n, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
4372 if (r < 0)
4373 return r;
4374
4a66b5c9
LP
4375 r = patch_var_run(unit, filename, line, lvalue, &n);
4376 if (r < 0)
4377 return r;
a9353a5c 4378
4a66b5c9 4379 free_and_replace(*s, n);
a9353a5c
LP
4380 return 0;
4381}
4382
7af67e9a
LP
4383int config_parse_exit_status(
4384 const char *unit,
4385 const char *filename,
4386 unsigned line,
4387 const char *section,
4388 unsigned section_line,
4389 const char *lvalue,
4390 int ltype,
4391 const char *rvalue,
4392 void *data,
4393 void *userdata) {
4394
4395 int *exit_status = data, r;
4396 uint8_t u;
4397
4398 assert(filename);
4399 assert(lvalue);
4400 assert(rvalue);
4401 assert(exit_status);
4402
4403 if (isempty(rvalue)) {
4404 *exit_status = -1;
4405 return 0;
4406 }
4407
4408 r = safe_atou8(rvalue, &u);
4409 if (r < 0) {
4410 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse exit status '%s', ignoring: %m", rvalue);
4411 return 0;
4412 }
4413
4414 *exit_status = u;
4415 return 0;
4416}
4417
c72703e2
CD
4418int config_parse_disable_controllers(
4419 const char *unit,
4420 const char *filename,
4421 unsigned line,
4422 const char *section,
4423 unsigned section_line,
4424 const char *lvalue,
4425 int ltype,
4426 const char *rvalue,
4427 void *data,
4428 void *userdata) {
4429
4430 int r;
4431 CGroupContext *c = data;
4432 CGroupMask disabled_mask;
4433
4434 /* 1. If empty, make all controllers eligible for use again.
4435 * 2. If non-empty, merge all listed controllers, space separated. */
4436
4437 if (isempty(rvalue)) {
4438 c->disable_controllers = 0;
4439 return 0;
4440 }
4441
4442 r = cg_mask_from_string(rvalue, &disabled_mask);
4443 if (r < 0 || disabled_mask <= 0) {
4444 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid cgroup string: %s, ignoring", rvalue);
4445 return 0;
4446 }
4447
4448 c->disable_controllers |= disabled_mask;
4449
4450 return 0;
4451}
4452
071830ff 4453#define FOLLOW_MAX 8
87f0e418 4454
9e2f7c11 4455static int open_follow(char **filename, FILE **_f, Set *names, char **_final) {
a837f088 4456 char *id = NULL;
0301abf4 4457 unsigned c = 0;
87f0e418
LP
4458 int fd, r;
4459 FILE *f;
87f0e418
LP
4460
4461 assert(filename);
4462 assert(*filename);
4463 assert(_f);
4464 assert(names);
4465
0301abf4
LP
4466 /* This will update the filename pointer if the loaded file is
4467 * reached by a symlink. The old string will be freed. */
87f0e418 4468
0301abf4 4469 for (;;) {
2c7108c4 4470 char *target, *name;
87f0e418 4471
0301abf4
LP
4472 if (c++ >= FOLLOW_MAX)
4473 return -ELOOP;
4474
858d36c1 4475 path_simplify(*filename, false);
b08d03ff 4476
87f0e418 4477 /* Add the file name we are currently looking at to
8f05424d
LP
4478 * the names of this unit, but only if it is a valid
4479 * unit name. */
2b6bf07d 4480 name = basename(*filename);
7410616c 4481 if (unit_name_is_valid(name, UNIT_NAME_ANY)) {
8f05424d 4482
15e11d81
LP
4483 id = set_get(names, name);
4484 if (!id) {
4485 id = strdup(name);
4486 if (!id)
8f05424d 4487 return -ENOMEM;
87f0e418 4488
ef42202a
ZJS
4489 r = set_consume(names, id);
4490 if (r < 0)
8f05424d 4491 return r;
87f0e418 4492 }
87f0e418
LP
4493 }
4494
0301abf4 4495 /* Try to open the file name, but don't if its a symlink */
9946996c
LP
4496 fd = open(*filename, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
4497 if (fd >= 0)
87f0e418
LP
4498 break;
4499
0301abf4
LP
4500 if (errno != ELOOP)
4501 return -errno;
4502
87f0e418 4503 /* Hmm, so this is a symlink. Let's read the name, and follow it manually */
9946996c
LP
4504 r = readlink_and_make_absolute(*filename, &target);
4505 if (r < 0)
0301abf4 4506 return r;
87f0e418 4507
17ee3d72 4508 free_and_replace(*filename, target);
87f0e418
LP
4509 }
4510
e92aaed3 4511 f = fdopen(fd, "r");
9946996c 4512 if (!f) {
03e334a1 4513 safe_close(fd);
d4ad27a1 4514 return -errno;
87f0e418
LP
4515 }
4516
4517 *_f = f;
9e2f7c11 4518 *_final = id;
a837f088 4519
0301abf4 4520 return 0;
87f0e418
LP
4521}
4522
23a177ef
LP
4523static int merge_by_names(Unit **u, Set *names, const char *id) {
4524 char *k;
4525 int r;
4526
4527 assert(u);
4528 assert(*u);
4529 assert(names);
4530
4531 /* Let's try to add in all symlink names we found */
4532 while ((k = set_steal_first(names))) {
4533
4534 /* First try to merge in the other name into our
4535 * unit */
9946996c
LP
4536 r = unit_merge_by_name(*u, k);
4537 if (r < 0) {
23a177ef
LP
4538 Unit *other;
4539
4540 /* Hmm, we couldn't merge the other unit into
4541 * ours? Then let's try it the other way
4542 * round */
4543
7aad67e7
MS
4544 /* If the symlink name we are looking at is unit template, then
4545 we must search for instance of this template */
9d3e3406 4546 if (unit_name_is_valid(k, UNIT_NAME_TEMPLATE) && (*u)->instance) {
7aad67e7
MS
4547 _cleanup_free_ char *instance = NULL;
4548
4549 r = unit_name_replace_instance(k, (*u)->instance, &instance);
4550 if (r < 0)
4551 return r;
4552
4553 other = manager_get_unit((*u)->manager, instance);
4554 } else
4555 other = manager_get_unit((*u)->manager, k);
4556
23a177ef
LP
4557 free(k);
4558
9946996c
LP
4559 if (other) {
4560 r = unit_merge(other, *u);
4561 if (r >= 0) {
23a177ef
LP
4562 *u = other;
4563 return merge_by_names(u, names, NULL);
4564 }
9946996c 4565 }
23a177ef
LP
4566
4567 return r;
4568 }
4569
4570 if (id == k)
4571 unit_choose_id(*u, id);
4572
4573 free(k);
4574 }
4575
4576 return 0;
4577}
4578
e537352b 4579static int load_from_path(Unit *u, const char *path) {
e48614c4
ZJS
4580 _cleanup_set_free_free_ Set *symlink_names = NULL;
4581 _cleanup_fclose_ FILE *f = NULL;
4582 _cleanup_free_ char *filename = NULL;
4583 char *id = NULL;
23a177ef 4584 Unit *merged;
45fb0699 4585 struct stat st;
a837f088 4586 int r;
23a177ef
LP
4587
4588 assert(u);
e537352b 4589 assert(path);
3efd4195 4590
d5099efc 4591 symlink_names = set_new(&string_hash_ops);
f975e971 4592 if (!symlink_names)
87f0e418 4593 return -ENOMEM;
3efd4195 4594
036643a2
LP
4595 if (path_is_absolute(path)) {
4596
9946996c 4597 filename = strdup(path);
e48614c4
ZJS
4598 if (!filename)
4599 return -ENOMEM;
036643a2 4600
9946996c
LP
4601 r = open_follow(&filename, &f, symlink_names, &id);
4602 if (r < 0) {
97b11eed 4603 filename = mfree(filename);
036643a2 4604 if (r != -ENOENT)
e48614c4 4605 return r;
036643a2
LP
4606 }
4607
4608 } else {
4609 char **p;
4610
a3c4eb07 4611 STRV_FOREACH(p, u->manager->lookup_paths.search_path) {
036643a2
LP
4612
4613 /* Instead of opening the path right away, we manually
4614 * follow all symlinks and add their name to our unit
4615 * name set while doing so */
9946996c 4616 filename = path_make_absolute(path, *p);
e48614c4
ZJS
4617 if (!filename)
4618 return -ENOMEM;
036643a2 4619
ac155bb8
MS
4620 if (u->manager->unit_path_cache &&
4621 !set_get(u->manager->unit_path_cache, filename))
fe51822e
LP
4622 r = -ENOENT;
4623 else
4624 r = open_follow(&filename, &f, symlink_names, &id);
a837f088
LP
4625 if (r >= 0)
4626 break;
a1feacf7
ZJS
4627
4628 /* ENOENT means that the file is missing or is a dangling symlink.
4629 * ENOTDIR means that one of paths we expect to be is a directory
4630 * is not a directory, we should just ignore that.
4631 * EACCES means that the directory or file permissions are wrong.
4632 */
4633 if (r == -EACCES)
4634 log_debug_errno(r, "Cannot access \"%s\": %m", filename);
4635 else if (!IN_SET(r, -ENOENT, -ENOTDIR))
a837f088 4636 return r;
fe51822e 4637
baa162ce 4638 filename = mfree(filename);
a837f088
LP
4639 /* Empty the symlink names for the next run */
4640 set_clear_free(symlink_names);
036643a2
LP
4641 }
4642 }
034c6ed7 4643
e48614c4 4644 if (!filename)
8f05424d 4645 /* Hmm, no suitable file found? */
e48614c4 4646 return 0;
87f0e418 4647
8a993b61 4648 if (!unit_type_may_alias(u->type) && set_size(symlink_names) > 1) {
a837f088
LP
4649 log_unit_warning(u, "Unit type of %s does not support alias names, refusing loading via symlink.", u->id);
4650 return -ELOOP;
4651 }
4652
23a177ef 4653 merged = u;
9946996c
LP
4654 r = merge_by_names(&merged, symlink_names, id);
4655 if (r < 0)
e48614c4 4656 return r;
87f0e418 4657
23a177ef 4658 if (merged != u) {
ac155bb8 4659 u->load_state = UNIT_MERGED;
e48614c4 4660 return 0;
034c6ed7
LP
4661 }
4662
e48614c4
ZJS
4663 if (fstat(fileno(f), &st) < 0)
4664 return -errno;
45fb0699 4665
3a8db9fe 4666 if (null_or_empty(&st)) {
ac155bb8 4667 u->load_state = UNIT_MASKED;
3a8db9fe
ZJS
4668 u->fragment_mtime = 0;
4669 } else {
c2756a68 4670 u->load_state = UNIT_LOADED;
3a8db9fe 4671 u->fragment_mtime = timespec_load(&st.st_mtim);
c2756a68 4672
00dc5d76 4673 /* Now, parse the file contents */
36f822c4
ZJS
4674 r = config_parse(u->id, filename, f,
4675 UNIT_VTABLE(u)->sections,
4676 config_item_perf_lookup, load_fragment_gperf_lookup,
bcde742e 4677 CONFIG_PARSE_ALLOW_INCLUDE, u);
f975e971 4678 if (r < 0)
e48614c4 4679 return r;
00dc5d76 4680 }
b08d03ff 4681
1cc6c93a 4682 free_and_replace(u->fragment_path, filename);
87f0e418 4683
1b64d026
LP
4684 if (u->source_path) {
4685 if (stat(u->source_path, &st) >= 0)
4686 u->source_mtime = timespec_load(&st.st_mtim);
4687 else
4688 u->source_mtime = 0;
4689 }
4690
e48614c4 4691 return 0;
0301abf4
LP
4692}
4693
e537352b 4694int unit_load_fragment(Unit *u) {
23a177ef 4695 int r;
294d81f1
LP
4696 Iterator i;
4697 const char *t;
0301abf4
LP
4698
4699 assert(u);
ac155bb8
MS
4700 assert(u->load_state == UNIT_STUB);
4701 assert(u->id);
23a177ef 4702
3f5e8115
LP
4703 if (u->transient) {
4704 u->load_state = UNIT_LOADED;
4705 return 0;
4706 }
4707
294d81f1
LP
4708 /* First, try to find the unit under its id. We always look
4709 * for unit files in the default directories, to make it easy
4710 * to override things by placing things in /etc/systemd/system */
9946996c
LP
4711 r = load_from_path(u, u->id);
4712 if (r < 0)
294d81f1
LP
4713 return r;
4714
4715 /* Try to find an alias we can load this with */
abc08d4d 4716 if (u->load_state == UNIT_STUB) {
ac155bb8 4717 SET_FOREACH(t, u->names, i) {
294d81f1 4718
ac155bb8 4719 if (t == u->id)
294d81f1
LP
4720 continue;
4721
9946996c
LP
4722 r = load_from_path(u, t);
4723 if (r < 0)
294d81f1
LP
4724 return r;
4725
ac155bb8 4726 if (u->load_state != UNIT_STUB)
294d81f1
LP
4727 break;
4728 }
abc08d4d 4729 }
23a177ef 4730
294d81f1 4731 /* And now, try looking for it under the suggested (originally linked) path */
ac155bb8 4732 if (u->load_state == UNIT_STUB && u->fragment_path) {
6ccb1b44 4733
9946996c
LP
4734 r = load_from_path(u, u->fragment_path);
4735 if (r < 0)
23a177ef 4736 return r;
0301abf4 4737
ece174c5 4738 if (u->load_state == UNIT_STUB)
6ccb1b44
LP
4739 /* Hmm, this didn't work? Then let's get rid
4740 * of the fragment path stored for us, so that
4741 * we don't point to an invalid location. */
a1e58e8e 4742 u->fragment_path = mfree(u->fragment_path);
6ccb1b44
LP
4743 }
4744
294d81f1 4745 /* Look for a template */
ac155bb8 4746 if (u->load_state == UNIT_STUB && u->instance) {
7410616c 4747 _cleanup_free_ char *k = NULL;
294d81f1 4748
7410616c
LP
4749 r = unit_name_template(u->id, &k);
4750 if (r < 0)
4751 return r;
294d81f1
LP
4752
4753 r = load_from_path(u, k);
bb28e684
ZJS
4754 if (r < 0) {
4755 if (r == -ENOEXEC)
4756 log_unit_notice(u, "Unit configuration has fatal error, unit will not be started.");
9e2f7c11 4757 return r;
bb28e684 4758 }
890f434c 4759
abc08d4d 4760 if (u->load_state == UNIT_STUB) {
ac155bb8 4761 SET_FOREACH(t, u->names, i) {
bc9fd78c 4762 _cleanup_free_ char *z = NULL;
87f0e418 4763
ac155bb8 4764 if (t == u->id)
23a177ef 4765 continue;
071830ff 4766
7410616c
LP
4767 r = unit_name_template(t, &z);
4768 if (r < 0)
4769 return r;
294d81f1 4770
bc9fd78c 4771 r = load_from_path(u, z);
294d81f1 4772 if (r < 0)
23a177ef 4773 return r;
890f434c 4774
ac155bb8 4775 if (u->load_state != UNIT_STUB)
23a177ef
LP
4776 break;
4777 }
abc08d4d 4778 }
071830ff
LP
4779 }
4780
23a177ef 4781 return 0;
3efd4195 4782}
e537352b
LP
4783
4784void unit_dump_config_items(FILE *f) {
f975e971
LP
4785 static const struct {
4786 const ConfigParserCallback callback;
4787 const char *rvalue;
4788 } table[] = {
17df7223 4789 { config_parse_warn_compat, "NOTSUPPORTED" },
f975e971
LP
4790 { config_parse_int, "INTEGER" },
4791 { config_parse_unsigned, "UNSIGNED" },
5556b5fe 4792 { config_parse_iec_size, "SIZE" },
59f448cf 4793 { config_parse_iec_uint64, "SIZE" },
5556b5fe 4794 { config_parse_si_size, "SIZE" },
f975e971
LP
4795 { config_parse_bool, "BOOLEAN" },
4796 { config_parse_string, "STRING" },
4797 { config_parse_path, "PATH" },
4798 { config_parse_unit_path_printf, "PATH" },
4799 { config_parse_strv, "STRING [...]" },
4800 { config_parse_exec_nice, "NICE" },
4801 { config_parse_exec_oom_score_adjust, "OOMSCOREADJUST" },
4802 { config_parse_exec_io_class, "IOCLASS" },
4803 { config_parse_exec_io_priority, "IOPRIORITY" },
4804 { config_parse_exec_cpu_sched_policy, "CPUSCHEDPOLICY" },
4805 { config_parse_exec_cpu_sched_prio, "CPUSCHEDPRIO" },
4806 { config_parse_exec_cpu_affinity, "CPUAFFINITY" },
4807 { config_parse_mode, "MODE" },
4808 { config_parse_unit_env_file, "FILE" },
52c239d7
LB
4809 { config_parse_exec_output, "OUTPUT" },
4810 { config_parse_exec_input, "INPUT" },
ca37242e
LP
4811 { config_parse_log_facility, "FACILITY" },
4812 { config_parse_log_level, "LEVEL" },
f975e971 4813 { config_parse_exec_secure_bits, "SECUREBITS" },
a103496c 4814 { config_parse_capability_set, "BOUNDINGSET" },
4f424df7 4815 { config_parse_rlimit, "LIMIT" },
f975e971 4816 { config_parse_unit_deps, "UNIT [...]" },
f975e971
LP
4817 { config_parse_exec, "PATH [ARGUMENT [...]]" },
4818 { config_parse_service_type, "SERVICETYPE" },
4819 { config_parse_service_restart, "SERVICERESTART" },
f975e971 4820 { config_parse_kill_mode, "KILLMODE" },
f757855e 4821 { config_parse_signal, "SIGNAL" },
f975e971
LP
4822 { config_parse_socket_listen, "SOCKET [...]" },
4823 { config_parse_socket_bind, "SOCKETBIND" },
4824 { config_parse_socket_bindtodevice, "NETWORKINTERFACE" },
7f602784 4825 { config_parse_sec, "SECONDS" },
d88a251b 4826 { config_parse_nsec, "NANOSECONDS" },
94828d2d 4827 { config_parse_namespace_path_strv, "PATH [...]" },
d2d6c096 4828 { config_parse_bind_paths, "PATH[:PATH[:OPTIONS]] [...]" },
7c8fa05c 4829 { config_parse_unit_requires_mounts_for, "PATH [...]" },
f975e971
LP
4830 { config_parse_exec_mount_flags, "MOUNTFLAG [...]" },
4831 { config_parse_unit_string_printf, "STRING" },
3ecaa09b 4832 { config_parse_trigger_unit, "UNIT" },
f975e971 4833 { config_parse_timer, "TIMER" },
f975e971 4834 { config_parse_path_spec, "PATH" },
f975e971
LP
4835 { config_parse_notify_access, "ACCESS" },
4836 { config_parse_ip_tos, "TOS" },
4837 { config_parse_unit_condition_path, "CONDITION" },
4838 { config_parse_unit_condition_string, "CONDITION" },
4839 { config_parse_unit_condition_null, "CONDITION" },
a016b922 4840 { config_parse_unit_slice, "SLICE" },
7f0386f6
LP
4841 { config_parse_documentation, "URL" },
4842 { config_parse_service_timeout, "SECONDS" },
87a47f99 4843 { config_parse_emergency_action, "ACTION" },
7f0386f6
LP
4844 { config_parse_set_status, "STATUS" },
4845 { config_parse_service_sockets, "SOCKETS" },
7f0386f6 4846 { config_parse_environ, "ENVIRON" },
349cc4a5 4847#if HAVE_SECCOMP
17df7223 4848 { config_parse_syscall_filter, "SYSCALLS" },
6a6751fe 4849 { config_parse_syscall_archs, "ARCHS" },
17df7223 4850 { config_parse_syscall_errno, "ERRNO" },
4298d0b5 4851 { config_parse_address_families, "FAMILIES" },
add00535 4852 { config_parse_restrict_namespaces, "NAMESPACES" },
c0467cf3 4853#endif
7f0386f6 4854 { config_parse_cpu_shares, "SHARES" },
984faf29 4855 { config_parse_cg_weight, "WEIGHT" },
7f0386f6
LP
4856 { config_parse_memory_limit, "LIMIT" },
4857 { config_parse_device_allow, "DEVICE" },
4858 { config_parse_device_policy, "POLICY" },
13c31542 4859 { config_parse_io_limit, "LIMIT" },
13c31542 4860 { config_parse_io_device_weight, "DEVICEWEIGHT" },
6ae4283c 4861 { config_parse_io_device_latency, "DEVICELATENCY" },
7f0386f6
LP
4862 { config_parse_blockio_bandwidth, "BANDWIDTH" },
4863 { config_parse_blockio_weight, "WEIGHT" },
4864 { config_parse_blockio_device_weight, "DEVICEWEIGHT" },
4865 { config_parse_long, "LONG" },
4866 { config_parse_socket_service, "SERVICE" },
349cc4a5 4867#if HAVE_SELINUX
6a6751fe
LP
4868 { config_parse_exec_selinux_context, "LABEL" },
4869#endif
4870 { config_parse_job_mode, "MODE" },
4871 { config_parse_job_mode_isolate, "BOOLEAN" },
4298d0b5 4872 { config_parse_personality, "PERSONALITY" },
f975e971
LP
4873 };
4874
4875 const char *prev = NULL;
4876 const char *i;
4877
4878 assert(f);
e537352b 4879
f975e971
LP
4880 NULSTR_FOREACH(i, load_fragment_gperf_nulstr) {
4881 const char *rvalue = "OTHER", *lvalue;
313b7856 4882 const ConfigPerfItem *p;
f975e971
LP
4883 size_t prefix_len;
4884 const char *dot;
313b7856 4885 unsigned j;
f975e971
LP
4886
4887 assert_se(p = load_fragment_gperf_lookup(i, strlen(i)));
4888
313b7856
LP
4889 /* Hide legacy settings */
4890 if (p->parse == config_parse_warn_compat &&
4891 p->ltype == DISABLED_LEGACY)
4892 continue;
4893
4894 for (j = 0; j < ELEMENTSOF(table); j++)
4895 if (p->parse == table[j].callback) {
4896 rvalue = table[j].rvalue;
4897 break;
4898 }
4899
f975e971
LP
4900 dot = strchr(i, '.');
4901 lvalue = dot ? dot + 1 : i;
4902 prefix_len = dot-i;
4903
4904 if (dot)
641906e9 4905 if (!prev || !strneq(prev, i, prefix_len+1)) {
f975e971
LP
4906 if (prev)
4907 fputc('\n', f);
4908
4909 fprintf(f, "[%.*s]\n", (int) prefix_len, i);
4910 }
4911
f975e971
LP
4912 fprintf(f, "%s=%s\n", lvalue, rvalue);
4913 prev = i;
4914 }
e537352b 4915}