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