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