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