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