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