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