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