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