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