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