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