]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/load-fragment.c
core/load-fragment: use unit_path_printf where appropriate
[thirdparty/systemd.git] / src / core / load-fragment.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
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
LP
9#include <linux/oom.h>
10#include <sched.h>
3d57c6ab 11#include <sys/resource.h>
3efd4195 12
bed0b7df
LP
13#include "sd-messages.h"
14
5f5d8eab 15#include "af-list.h"
57b7a260 16#include "all-units.h"
786d19fd 17#include "alloc-util.h"
fab34748 18#include "bpf-firewall.h"
e59ccd03 19#include "bpf-lsm.h"
0879da98 20#include "bpf-program.h"
cd09a5f3 21#include "bpf-socket-bind.h"
5f5d8eab
LP
22#include "bus-error.h"
23#include "bus-internal.h"
24#include "bus-util.h"
25#include "cap-list.h"
a103496c 26#include "capability-util.h"
fdb3deca 27#include "cgroup-setup.h"
3efd4195 28#include "conf-parser.h"
e30bbc90 29#include "core-varlink.h"
618234a5 30#include "cpu-set-util.h"
786d19fd 31#include "creds-util.h"
5f5d8eab
LP
32#include "env-util.h"
33#include "errno-list.h"
4f5dd394 34#include "escape.h"
43962c30 35#include "exec-credential.h"
dc7d69b3 36#include "execute.h"
3ffd4af2 37#include "fd-util.h"
0389f4fa 38#include "fileio.h"
dc7d69b3 39#include "firewall-util.h"
f4f15635 40#include "fs-util.h"
08f3be7a 41#include "hexdecoct.h"
bd1ae178 42#include "iovec-util.h"
032b3afb 43#include "ioprio-util.h"
da96ad5a 44#include "ip-protocol-list.h"
adce225a 45#include "journal-file.h"
eefc66aa 46#include "limits-util.h"
3ffd4af2 47#include "load-fragment.h"
5f5d8eab 48#include "log.h"
5bead76e 49#include "missing_ioprio.h"
049af8ad 50#include "mountpoint-util.h"
d8b4d14d 51#include "nulstr-util.h"
cd48e23f 52#include "open-file.h"
c3eaba2d 53#include "parse-helpers.h"
6bedfcbb 54#include "parse-util.h"
9eb977db 55#include "path-util.h"
523ea123 56#include "pcre2-util.h"
ed5033fd 57#include "percent-util.h"
7b3e062c 58#include "process-util.h"
57183d11 59#include "seccomp-util.h"
07d46372 60#include "securebits-util.h"
23e9a7dd 61#include "selinux-util.h"
5f5d8eab 62#include "signal-util.h"
5c3fa98d 63#include "socket-netlink.h"
46a9ee5d 64#include "specifier.h"
8fcde012 65#include "stat-util.h"
07630cea 66#include "string-util.h"
5f5d8eab 67#include "strv.h"
91dd5f7c
LP
68#include "syslog-util.h"
69#include "time-util.h"
5f5d8eab
LP
70#include "unit-name.h"
71#include "unit-printf.h"
66dccd8d 72#include "user-util.h"
bb0c0d6f 73#include "utf8.h"
49cf4170 74#include "web-util.h"
57183d11 75
d2b42d63 76static int parse_socket_protocol(const char *s) {
53577580
YW
77 int r;
78
d2b42d63 79 r = parse_ip_protocol(s);
53577580 80 if (r < 0)
acf4d158 81 return r;
53577580
YW
82 if (!IN_SET(r, IPPROTO_UDPLITE, IPPROTO_SCTP))
83 return -EPROTONOSUPPORT;
84
85 return r;
86}
87
a07a7324
FS
88int parse_crash_chvt(const char *value, int *data) {
89 int b;
90
91 if (safe_atoi(value, data) >= 0)
92 return 0;
93
94 b = parse_boolean(value);
95 if (b < 0)
96 return b;
97
98 if (b > 0)
99 *data = 0; /* switch to where kmsg goes */
100 else
101 *data = -1; /* turn off switching */
102
103 return 0;
104}
105
106int parse_confirm_spawn(const char *value, char **console) {
107 char *s;
108 int r;
109
110 r = value ? parse_boolean(value) : 1;
111 if (r == 0) {
112 *console = NULL;
113 return 0;
4a8daee7 114 } else if (r > 0) /* on with default tty */
a07a7324
FS
115 s = strdup("/dev/console");
116 else if (is_path(value)) /* on with fully qualified path */
117 s = strdup(value);
118 else /* on with only a tty file name, not a fully qualified path */
4a8daee7 119 s = path_join("/dev/", value);
a07a7324
FS
120 if (!s)
121 return -ENOMEM;
122
123 *console = s;
124 return 0;
125}
126
d2b42d63 127DEFINE_CONFIG_PARSE(config_parse_socket_protocol, parse_socket_protocol, "Failed to parse socket protocol");
53577580 128DEFINE_CONFIG_PARSE(config_parse_exec_secure_bits, secure_bits_from_string, "Failed to parse secure bits");
5afe510c 129DEFINE_CONFIG_PARSE_ENUM(config_parse_collect_mode, collect_mode, CollectMode, "Failed to parse garbage collection mode");
53577580 130DEFINE_CONFIG_PARSE_ENUM(config_parse_device_policy, cgroup_device_policy, CGroupDevicePolicy, "Failed to parse device policy");
53577580 131DEFINE_CONFIG_PARSE_ENUM(config_parse_exec_keyring_mode, exec_keyring_mode, ExecKeyringMode, "Failed to parse keyring mode");
4e399953
LP
132DEFINE_CONFIG_PARSE_ENUM(config_parse_protect_proc, protect_proc, ProtectProc, "Failed to parse /proc/ protection mode");
133DEFINE_CONFIG_PARSE_ENUM(config_parse_proc_subset, proc_subset, ProcSubset, "Failed to parse /proc/ subset mode");
53577580
YW
134DEFINE_CONFIG_PARSE_ENUM(config_parse_exec_utmp_mode, exec_utmp_mode, ExecUtmpMode, "Failed to parse utmp mode");
135DEFINE_CONFIG_PARSE_ENUM(config_parse_job_mode, job_mode, JobMode, "Failed to parse job mode");
53577580 136DEFINE_CONFIG_PARSE_ENUM(config_parse_notify_access, notify_access, NotifyAccess, "Failed to parse notify access specifier");
1e8c7bd5
YW
137DEFINE_CONFIG_PARSE_ENUM(config_parse_protect_home, protect_home, ProtectHome, "Failed to parse protect home value");
138DEFINE_CONFIG_PARSE_ENUM(config_parse_protect_system, protect_system, ProtectSystem, "Failed to parse protect system value");
b9c1883a 139DEFINE_CONFIG_PARSE_ENUM(config_parse_exec_preserve_mode, exec_preserve_mode, ExecPreserveMode, "Failed to parse resource preserve mode");
53577580 140DEFINE_CONFIG_PARSE_ENUM(config_parse_service_type, service_type, ServiceType, "Failed to parse service type");
596e4470 141DEFINE_CONFIG_PARSE_ENUM(config_parse_service_exit_type, service_exit_type, ServiceExitType, "Failed to parse service exit type");
53577580 142DEFINE_CONFIG_PARSE_ENUM(config_parse_service_restart, service_restart, ServiceRestart, "Failed to parse service restart specifier");
e568fea9 143DEFINE_CONFIG_PARSE_ENUM(config_parse_service_restart_mode, service_restart_mode, ServiceRestartMode, "Failed to parse service restart mode");
bf760801 144DEFINE_CONFIG_PARSE_ENUM(config_parse_service_timeout_failure_mode, service_timeout_failure_mode, ServiceTimeoutFailureMode, "Failed to parse timeout failure mode");
53577580 145DEFINE_CONFIG_PARSE_ENUM(config_parse_socket_bind, socket_address_bind_ipv6_only_or_bool, SocketAddressBindIPv6Only, "Failed to parse bind IPv6 only value");
afcfaa69 146DEFINE_CONFIG_PARSE_ENUM(config_parse_oom_policy, oom_policy, OOMPolicy, "Failed to parse OOM policy");
4e806bfa 147DEFINE_CONFIG_PARSE_ENUM(config_parse_managed_oom_preference, managed_oom_preference, ManagedOOMPreference, "Failed to parse ManagedOOMPreference=");
054749e4 148DEFINE_CONFIG_PARSE_ENUM(config_parse_memory_pressure_watch, cgroup_pressure_watch, CGroupPressureWatch, "Failed to parse memory pressure watch setting");
53577580
YW
149DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT(config_parse_ip_tos, ip_tos, int, -1, "Failed to parse IP TOS value");
150DEFINE_CONFIG_PARSE_PTR(config_parse_blockio_weight, cg_blkio_weight_parse, uint64_t, "Invalid block IO weight");
151DEFINE_CONFIG_PARSE_PTR(config_parse_cg_weight, cg_weight_parse, uint64_t, "Invalid weight");
c8340822 152DEFINE_CONFIG_PARSE_PTR(config_parse_cg_cpu_weight, cg_cpu_weight_parse, uint64_t, "Invalid CPU weight");
c1e701e2 153static DEFINE_CONFIG_PARSE_PTR(config_parse_cpu_shares_internal, cg_cpu_shares_parse, uint64_t, "Invalid CPU shares");
874cdcbc 154DEFINE_CONFIG_PARSE_PTR(config_parse_exec_mount_propagation_flag, mount_propagation_flag_from_string, unsigned long, "Failed to parse mount propagation flag");
b070c7c0 155DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT(config_parse_numa_policy, mpol, int, -1, "Invalid NUMA policy type");
6327aa9f 156DEFINE_CONFIG_PARSE_ENUM(config_parse_status_unit_format, status_unit_format, StatusUnitFormat, "Failed to parse status unit format");
9b191525 157DEFINE_CONFIG_PARSE_ENUM_FULL(config_parse_socket_timestamping, socket_timestamping_from_string_harder, SocketTimestamping, "Failed to parse timestamping precision");
5afe510c 158
c1e701e2
LP
159int config_parse_cpu_shares(
160 const char *unit,
161 const char *filename,
162 unsigned line,
163 const char *section,
164 unsigned section_line,
165 const char *lvalue,
166 int ltype,
167 const char *rvalue,
168 void *data,
169 void *userdata) {
170
171 assert(filename);
172 assert(lvalue);
173 assert(rvalue);
174
175
176 log_syntax(unit, LOG_WARNING, filename, line, 0,
177 "Unit uses %s=; please use CPUWeight= instead. Support for %s= will be removed soon.",
178 lvalue, lvalue);
179
180 return config_parse_cpu_shares_internal(unit, filename, line, section, section_line, lvalue, ltype, rvalue, data, userdata);
181}
182
88022148
DDM
183bool contains_instance_specifier_superset(const char *s) {
184 const char *p, *q;
185 bool percent = false;
186
187 assert(s);
188
189 p = strchr(s, '@');
190 if (!p)
191 return false;
192
193 p++; /* Skip '@' */
194
195 q = strrchr(p, '.');
196 if (!q)
197 q = p + strlen(p);
198
199 /* If the string is just the instance specifier, it's not a superset of the instance. */
200 if (memcmp_nn(p, q - p, "%i", strlen("%i")) == 0)
201 return false;
202
203 /* %i, %n and %N all expand to the instance or a superset of it. */
24aaf6c6 204 for (; p < q; p++)
88022148
DDM
205 if (*p == '%')
206 percent = !percent;
207 else if (percent) {
208 if (IN_SET(*p, 'n', 'N', 'i'))
209 return true;
210 percent = false;
211 }
88022148
DDM
212
213 return false;
214}
215
216/* `name` is the rendered version of `format` via `unit_printf` or similar functions. */
217int unit_is_likely_recursive_template_dependency(Unit *u, const char *name, const char *format) {
218 const char *fragment_path;
219 int r;
220
221 assert(u);
222 assert(name);
223
224 /* If a template unit has a direct dependency on itself that includes the unit instance as part of
225 * the template instance via a unit specifier (%i, %n or %N), this will almost certainly lead to
226 * infinite recursion as systemd will keep instantiating new instances of the template unit.
227 * https://github.com/systemd/systemd/issues/17602 shows a good example of how this can happen in
228 * practice. To guard against this, we check for templates that depend on themselves and have the
229 * instantiated unit instance included as part of the template instance of the dependency via a
230 * specifier.
231 *
232 * For example, if systemd-notify@.service depends on systemd-notify@%n.service, this will result in
233 * infinite recursion.
234 */
235
236 if (!unit_name_is_valid(name, UNIT_NAME_INSTANCE))
237 return false;
238
239 if (!unit_name_prefix_equal(u->id, name))
240 return false;
241
242 if (u->type != unit_name_to_type(name))
243 return false;
244
245 r = unit_file_find_fragment(u->manager->unit_id_map, u->manager->unit_name_map, name, &fragment_path, NULL);
246 if (r < 0)
247 return r;
248
249 /* Fragment paths should also be equal as a custom fragment for a specific template instance
250 * wouldn't necessarily lead to infinite recursion. */
251 if (!path_equal_ptr(u->fragment_path, fragment_path))
252 return false;
253
254 if (!contains_instance_specifier_superset(format))
255 return false;
256
257 return true;
258}
259
f32b43bd
LP
260int config_parse_unit_deps(
261 const char *unit,
262 const char *filename,
263 unsigned line,
264 const char *section,
265 unsigned section_line,
266 const char *lvalue,
267 int ltype,
268 const char *rvalue,
269 void *data,
270 void *userdata) {
3efd4195 271
f975e971 272 UnitDependency d = ltype;
87f0e418 273 Unit *u = userdata;
3efd4195
LP
274
275 assert(filename);
276 assert(lvalue);
277 assert(rvalue);
3efd4195 278
323dda78 279 for (const char *p = rvalue;;) {
3d793d29 280 _cleanup_free_ char *word = NULL, *k = NULL;
3efd4195 281 int r;
3efd4195 282
c89f52ac 283 r = extract_first_word(&p, &word, NULL, EXTRACT_RETAIN_ESCAPE);
3d793d29 284 if (r == 0)
323dda78 285 return 0;
3d793d29 286 if (r == -ENOMEM)
74051b9b 287 return log_oom();
3d793d29 288 if (r < 0) {
323dda78
YW
289 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
290 return 0;
3d793d29 291 }
3efd4195 292
3d793d29 293 r = unit_name_printf(u, word, &k);
19f6d710 294 if (r < 0) {
323dda78 295 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", word);
19f6d710
LP
296 continue;
297 }
9e2f7c11 298
88022148
DDM
299 r = unit_is_likely_recursive_template_dependency(u, k, word);
300 if (r < 0) {
301 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to determine if '%s' is a recursive dependency, ignoring: %m", k);
302 continue;
303 }
304 if (r > 0) {
305 log_syntax(unit, LOG_DEBUG, filename, line, 0,
306 "Dropping dependency %s=%s that likely leads to infinite recursion.",
307 unit_dependency_to_string(d), word);
308 continue;
309 }
310
35d8c19a 311 r = unit_add_dependency_by_name(u, d, k, true, UNIT_DEPENDENCY_FILE);
57020a3a 312 if (r < 0)
323dda78 313 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to add dependency on %s, ignoring: %m", k);
3efd4195 314 }
3efd4195
LP
315}
316
f32b43bd
LP
317int config_parse_obsolete_unit_deps(
318 const char *unit,
319 const char *filename,
320 unsigned line,
321 const char *section,
322 unsigned section_line,
323 const char *lvalue,
324 int ltype,
325 const char *rvalue,
326 void *data,
327 void *userdata) {
328
329 log_syntax(unit, LOG_WARNING, filename, line, 0,
330 "Unit dependency type %s= is obsolete, replacing by %s=, please update your unit file", lvalue, unit_dependency_to_string(ltype));
331
332 return config_parse_unit_deps(unit, filename, line, section, section_line, lvalue, ltype, rvalue, data, userdata);
333}
334
b02cb41c
LP
335int config_parse_unit_string_printf(
336 const char *unit,
337 const char *filename,
338 unsigned line,
339 const char *section,
340 unsigned section_line,
341 const char *lvalue,
342 int ltype,
343 const char *rvalue,
344 void *data,
345 void *userdata) {
932921b5 346
74051b9b 347 _cleanup_free_ char *k = NULL;
99534007 348 const Unit *u = ASSERT_PTR(userdata);
19f6d710 349 int r;
932921b5
LP
350
351 assert(filename);
352 assert(lvalue);
353 assert(rvalue);
932921b5 354
19f6d710 355 r = unit_full_printf(u, rvalue, &k);
b02cb41c 356 if (r < 0) {
323dda78 357 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
b02cb41c
LP
358 return 0;
359 }
932921b5 360
b02cb41c 361 return config_parse_string(unit, filename, line, section, section_line, lvalue, ltype, k, data, userdata);
932921b5
LP
362}
363
12ca818f
LP
364int config_parse_unit_strv_printf(
365 const char *unit,
366 const char *filename,
367 unsigned line,
368 const char *section,
369 unsigned section_line,
370 const char *lvalue,
371 int ltype,
372 const char *rvalue,
373 void *data,
374 void *userdata) {
8fef7659 375
99534007 376 const Unit *u = ASSERT_PTR(userdata);
74051b9b 377 _cleanup_free_ char *k = NULL;
19f6d710 378 int r;
8fef7659
LP
379
380 assert(filename);
381 assert(lvalue);
382 assert(rvalue);
8fef7659 383
19f6d710 384 r = unit_full_printf(u, rvalue, &k);
12ca818f 385 if (r < 0) {
323dda78 386 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
12ca818f
LP
387 return 0;
388 }
8fef7659 389
12ca818f 390 return config_parse_strv(unit, filename, line, section, section_line, lvalue, ltype, k, data, userdata);
8fef7659
LP
391}
392
5f5d8eab
LP
393int config_parse_unit_path_printf(
394 const char *unit,
395 const char *filename,
396 unsigned line,
397 const char *section,
398 unsigned section_line,
399 const char *lvalue,
400 int ltype,
401 const char *rvalue,
402 void *data,
403 void *userdata) {
6ea832a2 404
74051b9b 405 _cleanup_free_ char *k = NULL;
99534007 406 const Unit *u = ASSERT_PTR(userdata);
19f6d710 407 int r;
2c75fb73 408 bool fatal = ltype;
6ea832a2
LP
409
410 assert(filename);
411 assert(lvalue);
412 assert(rvalue);
6ea832a2 413
06536492 414 r = unit_path_printf(u, rvalue, &k);
811ba7a0 415 if (r < 0) {
323dda78 416 log_syntax(unit, fatal ? LOG_ERR : LOG_WARNING, filename, line, r,
063c4b1a 417 "Failed to resolve unit specifiers in '%s'%s: %m",
e3c3d676 418 rvalue, fatal ? "" : ", ignoring");
2c75fb73 419 return fatal ? -ENOEXEC : 0;
811ba7a0 420 }
6ea832a2 421
811ba7a0
LP
422 return config_parse_path(unit, filename, line, section, section_line, lvalue, ltype, k, data, userdata);
423}
424
8c35c10d 425int config_parse_colon_separated_paths(
426 const char *unit,
427 const char *filename,
428 unsigned line,
429 const char *section,
430 unsigned section_line,
431 const char *lvalue,
432 int ltype,
433 const char *rvalue,
434 void *data,
435 void *userdata) {
99534007 436 char ***sv = ASSERT_PTR(data);
8c35c10d 437 const Unit *u = userdata;
438 int r;
439
440 assert(filename);
441 assert(lvalue);
442 assert(rvalue);
8c35c10d 443
444 if (isempty(rvalue)) {
445 /* Empty assignment resets the list */
446 *sv = strv_free(*sv);
447 return 0;
448 }
449
450 for (const char *p = rvalue;;) {
451 _cleanup_free_ char *word = NULL, *k = NULL;
452
453 r = extract_first_word(&p, &word, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
454 if (r == -ENOMEM)
455 return log_oom();
456 if (r < 0) {
457 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to extract first word, ignoring: %s", rvalue);
458 return 0;
459 }
460 if (r == 0)
461 break;
462
463 r = unit_path_printf(u, word, &k);
464 if (r < 0) {
465 log_syntax(unit, LOG_WARNING, filename, line, r,
466 "Failed to resolve unit specifiers in '%s', ignoring: %m", word);
467 return 0;
468 }
469
470 r = path_simplify_and_warn(k, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
471 if (r < 0)
472 return 0;
473
474 r = strv_consume(sv, TAKE_PTR(k));
475 if (r < 0)
476 return log_oom();
477 }
478
479 return 0;
480}
481
811ba7a0
LP
482int config_parse_unit_path_strv_printf(
483 const char *unit,
484 const char *filename,
485 unsigned line,
486 const char *section,
487 unsigned section_line,
488 const char *lvalue,
489 int ltype,
490 const char *rvalue,
491 void *data,
492 void *userdata) {
493
a2a5291b 494 char ***x = data;
99534007 495 const Unit *u = ASSERT_PTR(userdata);
811ba7a0
LP
496 int r;
497
498 assert(filename);
499 assert(lvalue);
500 assert(rvalue);
811ba7a0 501
499295fb 502 if (isempty(rvalue)) {
9f2d41a6 503 *x = strv_free(*x);
499295fb
YW
504 return 0;
505 }
506
323dda78 507 for (const char *p = rvalue;;) {
035fe294 508 _cleanup_free_ char *word = NULL, *k = NULL;
811ba7a0 509
4ec85141 510 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
035fe294
ZJS
511 if (r == 0)
512 return 0;
513 if (r == -ENOMEM)
514 return log_oom();
515 if (r < 0) {
516 log_syntax(unit, LOG_WARNING, filename, line, r,
517 "Invalid syntax, ignoring: %s", rvalue);
518 return 0;
519 }
811ba7a0 520
06536492 521 r = unit_path_printf(u, word, &k);
811ba7a0 522 if (r < 0) {
323dda78 523 log_syntax(unit, LOG_WARNING, filename, line, r,
063c4b1a 524 "Failed to resolve unit specifiers in '%s', ignoring: %m", word);
811ba7a0
LP
525 return 0;
526 }
527
2f4d31c1
YW
528 r = path_simplify_and_warn(k, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
529 if (r < 0)
811ba7a0 530 return 0;
811ba7a0 531
7d2c9c6b 532 r = strv_consume(x, TAKE_PTR(k));
811ba7a0
LP
533 if (r < 0)
534 return log_oom();
811ba7a0 535 }
6ea832a2
LP
536}
537
4a66b5c9
LP
538static int patch_var_run(
539 const char *unit,
540 const char *filename,
541 unsigned line,
542 const char *lvalue,
543 char **path) {
544
545 const char *e;
546 char *z;
547
548 e = path_startswith(*path, "/var/run/");
549 if (!e)
550 return 0;
551
552 z = path_join("/run/", e);
553 if (!z)
554 return log_oom();
555
556 log_syntax(unit, LOG_NOTICE, filename, line, 0,
557 "%s= references a path below legacy directory /var/run/, updating %s → %s; "
558 "please update the unit file accordingly.", lvalue, *path, z);
559
560 free_and_replace(*path, z);
561
562 return 1;
563}
564
565int config_parse_socket_listen(
566 const char *unit,
567 const char *filename,
568 unsigned line,
569 const char *section,
570 unsigned section_line,
571 const char *lvalue,
572 int ltype,
573 const char *rvalue,
574 void *data,
575 void *userdata) {
42f4e3c4 576
b1389b0d
ZJS
577 _cleanup_free_ SocketPort *p = NULL;
578 SocketPort *tail;
542563ba 579 Socket *s;
19f6d710 580 int r;
16354eff 581
42f4e3c4
LP
582 assert(filename);
583 assert(lvalue);
584 assert(rvalue);
585 assert(data);
586
595ed347 587 s = SOCKET(data);
542563ba 588
74051b9b
LP
589 if (isempty(rvalue)) {
590 /* An empty assignment removes all ports */
591 socket_free_ports(s);
592 return 0;
593 }
594
7f110ff9
LP
595 p = new0(SocketPort, 1);
596 if (!p)
74051b9b 597 return log_oom();
916abb21 598
74051b9b 599 if (ltype != SOCKET_SOCKET) {
2f4d31c1 600 _cleanup_free_ char *k = NULL;
916abb21 601
06536492 602 r = unit_path_printf(UNIT(s), rvalue, &k);
19f6d710 603 if (r < 0) {
323dda78 604 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
12ca818f 605 return 0;
916abb21
LP
606 }
607
2f4d31c1
YW
608 r = path_simplify_and_warn(k, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
609 if (r < 0)
610 return 0;
611
4a66b5c9
LP
612 if (ltype == SOCKET_FIFO) {
613 r = patch_var_run(unit, filename, line, lvalue, &k);
614 if (r < 0)
615 return r;
616 }
617
2f4d31c1
YW
618 free_and_replace(p->path, k);
619 p->type = ltype;
916abb21 620
7a22745a 621 } else if (streq(lvalue, "ListenNetlink")) {
74051b9b 622 _cleanup_free_ char *k = NULL;
1fd45a90 623
06536492 624 r = unit_path_printf(UNIT(s), rvalue, &k);
12ca818f 625 if (r < 0) {
323dda78 626 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
12ca818f
LP
627 return 0;
628 }
7a22745a 629
12ca818f 630 r = socket_address_parse_netlink(&p->address, k);
1fd45a90 631 if (r < 0) {
323dda78 632 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse address value in '%s', ignoring: %m", k);
7a22745a
LP
633 return 0;
634 }
635
2f4d31c1
YW
636 p->type = SOCKET_SOCKET;
637
542563ba 638 } else {
74051b9b 639 _cleanup_free_ char *k = NULL;
1fd45a90 640
06536492 641 r = unit_path_printf(UNIT(s), rvalue, &k);
12ca818f 642 if (r < 0) {
323dda78 643 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
12ca818f
LP
644 return 0;
645 }
542563ba 646
4a66b5c9
LP
647 if (k[0] == '/') { /* Only for AF_UNIX file system sockets… */
648 r = patch_var_run(unit, filename, line, lvalue, &k);
649 if (r < 0)
650 return r;
651 }
652
12ca818f 653 r = socket_address_parse_and_warn(&p->address, k);
1fd45a90 654 if (r < 0) {
f847b8b7 655 if (r != -EAFNOSUPPORT)
323dda78 656 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse address value in '%s', ignoring: %m", k);
c0b34696 657 return 0;
542563ba
LP
658 }
659
660 if (streq(lvalue, "ListenStream"))
661 p->address.type = SOCK_STREAM;
662 else if (streq(lvalue, "ListenDatagram"))
663 p->address.type = SOCK_DGRAM;
664 else {
665 assert(streq(lvalue, "ListenSequentialPacket"));
666 p->address.type = SOCK_SEQPACKET;
667 }
668
618b3642 669 if (socket_address_family(&p->address) != AF_UNIX && p->address.type == SOCK_SEQPACKET) {
323dda78 670 log_syntax(unit, LOG_WARNING, filename, line, 0, "Address family not supported, ignoring: %s", rvalue);
c0b34696 671 return 0;
542563ba 672 }
2f4d31c1
YW
673
674 p->type = SOCKET_SOCKET;
16354eff
LP
675 }
676
254d1313 677 p->fd = -EBADF;
15087cdb
PS
678 p->auxiliary_fds = NULL;
679 p->n_auxiliary_fds = 0;
2e41a51e 680 p->socket = s;
49f91047 681
cc232fa0 682 tail = LIST_FIND_TAIL(port, s->ports);
533f8a67
YW
683 LIST_INSERT_AFTER(port, s->ports, tail, p);
684
b1389b0d 685 p = NULL;
542563ba 686
16354eff 687 return 0;
42f4e3c4
LP
688}
689
41bf0590
LP
690int config_parse_exec_nice(
691 const char *unit,
692 const char *filename,
693 unsigned line,
694 const char *section,
695 unsigned section_line,
696 const char *lvalue,
697 int ltype,
698 const char *rvalue,
699 void *data,
700 void *userdata) {
034c6ed7 701
99534007 702 ExecContext *c = ASSERT_PTR(data);
e8e581bf 703 int priority, r;
034c6ed7
LP
704
705 assert(filename);
706 assert(lvalue);
707 assert(rvalue);
034c6ed7 708
de5e6038
YW
709 if (isempty(rvalue)) {
710 c->nice_set = false;
711 return 0;
712 }
713
41bf0590 714 r = parse_nice(rvalue, &priority);
e8e581bf 715 if (r < 0) {
41bf0590 716 if (r == -ERANGE)
323dda78 717 log_syntax(unit, LOG_WARNING, filename, line, r, "Nice priority out of range, ignoring: %s", rvalue);
41bf0590 718 else
323dda78 719 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse nice priority '%s', ignoring: %m", rvalue);
c0b34696 720 return 0;
034c6ed7
LP
721 }
722
fb33a393 723 c->nice = priority;
71155933 724 c->nice_set = true;
fb33a393 725
034c6ed7
LP
726 return 0;
727}
728
e9eb2c02
LP
729int config_parse_exec_oom_score_adjust(
730 const char* unit,
731 const char *filename,
732 unsigned line,
733 const char *section,
734 unsigned section_line,
735 const char *lvalue,
736 int ltype,
737 const char *rvalue,
738 void *data,
739 void *userdata) {
034c6ed7 740
99534007 741 ExecContext *c = ASSERT_PTR(data);
e8e581bf 742 int oa, r;
034c6ed7
LP
743
744 assert(filename);
745 assert(lvalue);
746 assert(rvalue);
034c6ed7 747
e9eb2c02
LP
748 if (isempty(rvalue)) {
749 c->oom_score_adjust_set = false;
c0b34696 750 return 0;
034c6ed7
LP
751 }
752
e9eb2c02 753 r = parse_oom_score_adjust(rvalue, &oa);
e9eb2c02 754 if (r < 0) {
063c4b1a 755 if (r == -ERANGE)
323dda78 756 log_syntax(unit, LOG_WARNING, filename, line, r, "OOM score adjust value out of range, ignoring: %s", rvalue);
063c4b1a 757 else
323dda78 758 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse the OOM score adjust value '%s', ignoring: %m", rvalue);
c0b34696 759 return 0;
034c6ed7
LP
760 }
761
dd6c17b1
LP
762 c->oom_score_adjust = oa;
763 c->oom_score_adjust_set = true;
fb33a393 764
034c6ed7
LP
765 return 0;
766}
767
ad21e542
ZJS
768int config_parse_exec_coredump_filter(
769 const char* unit,
770 const char *filename,
771 unsigned line,
772 const char *section,
773 unsigned section_line,
774 const char *lvalue,
775 int ltype,
776 const char *rvalue,
777 void *data,
778 void *userdata) {
779
99534007 780 ExecContext *c = ASSERT_PTR(data);
ad21e542
ZJS
781 int r;
782
783 assert(filename);
784 assert(lvalue);
785 assert(rvalue);
ad21e542
ZJS
786
787 if (isempty(rvalue)) {
788 c->coredump_filter = 0;
789 c->coredump_filter_set = false;
790 return 0;
791 }
792
793 uint64_t f;
794 r = coredump_filter_mask_from_string(rvalue, &f);
795 if (r < 0) {
796 log_syntax(unit, LOG_WARNING, filename, line, r,
797 "Failed to parse the CoredumpFilter=%s, ignoring: %m", rvalue);
798 return 0;
799 }
800
801 c->coredump_filter |= f;
9c669abb 802 c->coredump_filter_set = true;
ad21e542
ZJS
803 return 0;
804}
805
d068765b
LP
806int config_parse_kill_mode(
807 const char* unit,
808 const char *filename,
809 unsigned line,
810 const char *section,
811 unsigned section_line,
812 const char *lvalue,
813 int ltype,
814 const char *rvalue,
815 void *data,
816 void *userdata) {
817
818 KillMode *k = data, m;
819
820 assert(filename);
821 assert(lvalue);
822 assert(rvalue);
823 assert(data);
824
825 if (isempty(rvalue)) {
826 *k = KILL_CONTROL_GROUP;
827 return 0;
828 }
829
830 m = kill_mode_from_string(rvalue);
831 if (m < 0) {
b98680b2 832 log_syntax(unit, LOG_WARNING, filename, line, m,
d068765b
LP
833 "Failed to parse kill mode specification, ignoring: %s", rvalue);
834 return 0;
835 }
836
837 if (m == KILL_NONE)
838 log_syntax(unit, LOG_WARNING, filename, line, 0,
af9d5d50 839 "Unit uses KillMode=none. "
15e6a6e8 840 "This is unsafe, as it disables systemd's process lifecycle management for the service. "
af9d5d50 841 "Please update the service to use a safer KillMode=, such as 'mixed' or 'control-group'. "
d068765b
LP
842 "Support for KillMode=none is deprecated and will eventually be removed.");
843
844 *k = m;
845 return 0;
846}
847
527b7a42
LP
848int config_parse_exec(
849 const char *unit,
850 const char *filename,
851 unsigned line,
852 const char *section,
853 unsigned section_line,
854 const char *lvalue,
855 int ltype,
856 const char *rvalue,
857 void *data,
858 void *userdata) {
034c6ed7 859
99534007 860 ExecCommand **e = ASSERT_PTR(data);
47538b76 861 const Unit *u = userdata;
46a0d98a
FB
862 const char *p;
863 bool semicolon;
7f110ff9 864 int r;
034c6ed7
LP
865
866 assert(filename);
867 assert(lvalue);
868 assert(rvalue);
034c6ed7 869
74051b9b 870 e += ltype;
c83f1f30 871
74051b9b
LP
872 if (isempty(rvalue)) {
873 /* An empty assignment resets the list */
f1acf85a 874 *e = exec_command_free_list(*e);
74051b9b
LP
875 return 0;
876 }
877
bd1b973f 878 p = rvalue;
46a0d98a 879 do {
dea7b6b0 880 _cleanup_free_ char *path = NULL, *firstword = NULL;
165a31c0
LP
881 ExecCommandFlags flags = 0;
882 bool ignore = false, separate_argv0 = false;
dea7b6b0 883 _cleanup_free_ ExecCommand *nce = NULL;
46a0d98a 884 _cleanup_strv_free_ char **n = NULL;
319a4f4b 885 size_t nlen = 0;
5125e762 886 const char *f;
6c666e26 887
46a0d98a
FB
888 semicolon = false;
889
4ec85141 890 r = extract_first_word_and_warn(&p, &firstword, NULL, EXTRACT_UNQUOTE|EXTRACT_CUNESCAPE, unit, filename, line, rvalue);
46a0d98a
FB
891 if (r <= 0)
892 return 0;
6c666e26 893
598c47c8
ZJS
894 /* A lone ";" is a separator. Let's make sure we don't treat it as an executable name. */
895 if (streq(firstword, ";")) {
896 semicolon = true;
897 continue;
898 }
899
46a0d98a 900 f = firstword;
007f48bb 901 for (;;) {
165a31c0
LP
902 /* We accept an absolute path as first argument. If it's prefixed with - and the path doesn't
903 * exist, we ignore it instead of erroring out; if it's prefixed with @, we allow overriding of
7ca69792
AZ
904 * argv[0]; if it's prefixed with :, we will not do environment variable substitution;
905 * if it's prefixed with +, it will be run with full privileges and no sandboxing; if
165a31c0
LP
906 * it's prefixed with '!' we apply sandboxing, but do not change user/group credentials; if
907 * it's prefixed with '!!', then we apply user/group credentials if the kernel supports ambient
908 * capabilities -- if it doesn't we don't apply the credentials themselves, but do apply most
909 * other sandboxing, with some special exceptions for changing UID.
910 *
911 * The idea is that '!!' may be used to write services that can take benefit of systemd's
912 * UID/GID dropping if the kernel supports ambient creds, but provide an automatic fallback to
913 * privilege dropping within the daemon if the kernel does not offer that. */
914
915 if (*f == '-' && !(flags & EXEC_COMMAND_IGNORE_FAILURE)) {
916 flags |= EXEC_COMMAND_IGNORE_FAILURE;
46a0d98a 917 ignore = true;
165a31c0 918 } else if (*f == '@' && !separate_argv0)
46a0d98a 919 separate_argv0 = true;
7ca69792
AZ
920 else if (*f == ':' && !(flags & EXEC_COMMAND_NO_ENV_EXPAND))
921 flags |= EXEC_COMMAND_NO_ENV_EXPAND;
165a31c0
LP
922 else if (*f == '+' && !(flags & (EXEC_COMMAND_FULLY_PRIVILEGED|EXEC_COMMAND_NO_SETUID|EXEC_COMMAND_AMBIENT_MAGIC)))
923 flags |= EXEC_COMMAND_FULLY_PRIVILEGED;
924 else if (*f == '!' && !(flags & (EXEC_COMMAND_FULLY_PRIVILEGED|EXEC_COMMAND_NO_SETUID|EXEC_COMMAND_AMBIENT_MAGIC)))
925 flags |= EXEC_COMMAND_NO_SETUID;
926 else if (*f == '!' && !(flags & (EXEC_COMMAND_FULLY_PRIVILEGED|EXEC_COMMAND_AMBIENT_MAGIC))) {
927 flags &= ~EXEC_COMMAND_NO_SETUID;
928 flags |= EXEC_COMMAND_AMBIENT_MAGIC;
929 } else
46a0d98a 930 break;
313cefa1 931 f++;
61e5d8ed 932 }
46a0d98a 933
06536492 934 r = unit_path_printf(u, f, &path);
5125e762 935 if (r < 0) {
323dda78 936 log_syntax(unit, ignore ? LOG_WARNING : LOG_ERR, filename, line, r,
063c4b1a 937 "Failed to resolve unit specifiers in '%s'%s: %m",
bb28e684
ZJS
938 f, ignore ? ", ignoring" : "");
939 return ignore ? 0 : -ENOEXEC;
5125e762
LP
940 }
941
942 if (isempty(path)) {
46a0d98a 943 /* First word is either "-" or "@" with no command. */
323dda78 944 log_syntax(unit, ignore ? LOG_WARNING : LOG_ERR, filename, line, 0,
063c4b1a 945 "Empty path in command line%s: '%s'",
bb28e684
ZJS
946 ignore ? ", ignoring" : "", rvalue);
947 return ignore ? 0 : -ENOEXEC;
b2fadec6 948 }
5125e762 949 if (!string_is_safe(path)) {
323dda78 950 log_syntax(unit, ignore ? LOG_WARNING : LOG_ERR, filename, line, 0,
5008da1e
ZJS
951 "Executable name contains special characters%s: %s",
952 ignore ? ", ignoring" : "", path);
bb28e684 953 return ignore ? 0 : -ENOEXEC;
46a0d98a 954 }
5125e762 955 if (endswith(path, "/")) {
323dda78 956 log_syntax(unit, ignore ? LOG_WARNING : LOG_ERR, filename, line, 0,
bb28e684 957 "Executable path specifies a directory%s: %s",
5008da1e 958 ignore ? ", ignoring" : "", path);
bb28e684 959 return ignore ? 0 : -ENOEXEC;
46a0d98a 960 }
61e5d8ed 961
108144ad 962 if (!(path_is_absolute(path) ? path_is_valid(path) : filename_is_valid(path))) {
9f71ba8d
ZJS
963 log_syntax(unit, ignore ? LOG_WARNING : LOG_ERR, filename, line, 0,
964 "Neither a valid executable name nor an absolute path%s: %s",
965 ignore ? ", ignoring" : "", path);
966 return ignore ? 0 : -ENOEXEC;
5008da1e
ZJS
967 }
968
46a0d98a 969 if (!separate_argv0) {
5125e762
LP
970 char *w = NULL;
971
15092743 972 if (!GREEDY_REALLOC0(n, nlen + 2))
46a0d98a 973 return log_oom();
5125e762
LP
974
975 w = strdup(path);
976 if (!w)
46a0d98a 977 return log_oom();
5125e762 978 n[nlen++] = w;
46a0d98a
FB
979 n[nlen] = NULL;
980 }
7f110ff9 981
4ff361cc 982 path_simplify(path);
46a0d98a 983
4b1c1753 984 while (!isempty(p)) {
5125e762 985 _cleanup_free_ char *word = NULL, *resolved = NULL;
46a0d98a
FB
986
987 /* Check explicitly for an unquoted semicolon as
988 * command separator token. */
989 if (p[0] == ';' && (!p[1] || strchr(WHITESPACE, p[1]))) {
313cefa1 990 p++;
46a0d98a
FB
991 p += strspn(p, WHITESPACE);
992 semicolon = true;
993 break;
c8539536 994 }
7f110ff9 995
5125e762
LP
996 /* Check for \; explicitly, to not confuse it with \\; or "\;" or "\\;" etc.
997 * extract_first_word() would return the same for all of those. */
46a0d98a 998 if (p[0] == '\\' && p[1] == ';' && (!p[2] || strchr(WHITESPACE, p[2]))) {
5125e762
LP
999 char *w;
1000
46a0d98a
FB
1001 p += 2;
1002 p += strspn(p, WHITESPACE);
5125e762 1003
15092743 1004 if (!GREEDY_REALLOC0(n, nlen + 2))
46a0d98a 1005 return log_oom();
5125e762
LP
1006
1007 w = strdup(";");
1008 if (!w)
46a0d98a 1009 return log_oom();
5125e762 1010 n[nlen++] = w;
46a0d98a
FB
1011 n[nlen] = NULL;
1012 continue;
61e5d8ed 1013 }
c8539536 1014
4ec85141 1015 r = extract_first_word_and_warn(&p, &word, NULL, EXTRACT_UNQUOTE|EXTRACT_CUNESCAPE, unit, filename, line, rvalue);
46a0d98a
FB
1016 if (r == 0)
1017 break;
5125e762 1018 if (r < 0)
bb28e684 1019 return ignore ? 0 : -ENOEXEC;
5125e762 1020
58dd4999 1021 r = unit_full_printf(u, word, &resolved);
5125e762 1022 if (r < 0) {
323dda78 1023 log_syntax(unit, ignore ? LOG_WARNING : LOG_ERR, filename, line, r,
063c4b1a 1024 "Failed to resolve unit specifiers in %s%s: %m",
bb28e684
ZJS
1025 word, ignore ? ", ignoring" : "");
1026 return ignore ? 0 : -ENOEXEC;
5125e762 1027 }
46a0d98a 1028
319a4f4b 1029 if (!GREEDY_REALLOC(n, nlen + 2))
46a0d98a 1030 return log_oom();
1cc6c93a
YW
1031
1032 n[nlen++] = TAKE_PTR(resolved);
46a0d98a 1033 n[nlen] = NULL;
61e5d8ed
LP
1034 }
1035
46a0d98a 1036 if (!n || !n[0]) {
323dda78 1037 log_syntax(unit, ignore ? LOG_WARNING : LOG_ERR, filename, line, 0,
bb28e684
ZJS
1038 "Empty executable name or zeroeth argument%s: %s",
1039 ignore ? ", ignoring" : "", rvalue);
1040 return ignore ? 0 : -ENOEXEC;
7f110ff9 1041 }
6c666e26 1042
7f110ff9 1043 nce = new0(ExecCommand, 1);
46a0d98a
FB
1044 if (!nce)
1045 return log_oom();
61e5d8ed 1046
1cc6c93a
YW
1047 nce->argv = TAKE_PTR(n);
1048 nce->path = TAKE_PTR(path);
165a31c0 1049 nce->flags = flags;
034c6ed7 1050
61e5d8ed 1051 exec_command_append_list(e, nce);
01f78473 1052
46a0d98a 1053 /* Do not _cleanup_free_ these. */
46a0d98a 1054 nce = NULL;
034c6ed7 1055
46a0d98a
FB
1056 rvalue = p;
1057 } while (semicolon);
034c6ed7 1058
46a0d98a 1059 return 0;
034c6ed7
LP
1060}
1061
d31645ad
LP
1062int config_parse_socket_bindtodevice(
1063 const char* unit,
1064 const char *filename,
1065 unsigned line,
1066 const char *section,
1067 unsigned section_line,
1068 const char *lvalue,
1069 int ltype,
1070 const char *rvalue,
1071 void *data,
1072 void *userdata) {
acbb0225 1073
99534007 1074 Socket *s = ASSERT_PTR(data);
acbb0225
LP
1075
1076 assert(filename);
1077 assert(lvalue);
1078 assert(rvalue);
acbb0225 1079
063c4b1a
YW
1080 if (isempty(rvalue) || streq(rvalue, "*")) {
1081 s->bind_to_device = mfree(s->bind_to_device);
1082 return 0;
1083 }
d31645ad 1084
063c4b1a 1085 if (!ifname_valid(rvalue)) {
323dda78 1086 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid interface name, ignoring: %s", rvalue);
063c4b1a
YW
1087 return 0;
1088 }
acbb0225 1089
b3f9c17a 1090 return free_and_strdup_warn(&s->bind_to_device, rvalue);
acbb0225
LP
1091}
1092
9bd6a50e
LP
1093int config_parse_exec_input(
1094 const char *unit,
1095 const char *filename,
1096 unsigned line,
1097 const char *section,
1098 unsigned section_line,
1099 const char *lvalue,
1100 int ltype,
1101 const char *rvalue,
1102 void *data,
1103 void *userdata) {
52c239d7 1104
99534007 1105 ExecContext *c = ASSERT_PTR(data);
47538b76 1106 const Unit *u = userdata;
2038c3f5
LP
1107 const char *n;
1108 ExecInput ei;
52c239d7
LB
1109 int r;
1110
52c239d7
LB
1111 assert(filename);
1112 assert(line);
1113 assert(rvalue);
1114
2038c3f5
LP
1115 n = startswith(rvalue, "fd:");
1116 if (n) {
1117 _cleanup_free_ char *resolved = NULL;
1118
06536492 1119 r = unit_fd_printf(u, n, &resolved);
323dda78
YW
1120 if (r < 0) {
1121 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", n);
1122 return 0;
1123 }
2038c3f5
LP
1124
1125 if (isempty(resolved))
1126 resolved = mfree(resolved);
1127 else if (!fdname_is_valid(resolved)) {
323dda78
YW
1128 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid file descriptor name, ignoring: %s", resolved);
1129 return 0;
52c239d7 1130 }
9bd6a50e 1131
2038c3f5
LP
1132 free_and_replace(c->stdio_fdname[STDIN_FILENO], resolved);
1133
1134 ei = EXEC_INPUT_NAMED_FD;
1135
1136 } else if ((n = startswith(rvalue, "file:"))) {
1137 _cleanup_free_ char *resolved = NULL;
1138
06536492 1139 r = unit_path_printf(u, n, &resolved);
323dda78
YW
1140 if (r < 0) {
1141 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", n);
1142 return 0;
1143 }
9bd6a50e 1144
2f4d31c1
YW
1145 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE | PATH_CHECK_FATAL, unit, filename, line, lvalue);
1146 if (r < 0)
323dda78 1147 return 0;
2038c3f5
LP
1148
1149 free_and_replace(c->stdio_file[STDIN_FILENO], resolved);
1150
1151 ei = EXEC_INPUT_FILE;
9bd6a50e 1152
52c239d7 1153 } else {
9bd6a50e
LP
1154 ei = exec_input_from_string(rvalue);
1155 if (ei < 0) {
b98680b2 1156 log_syntax(unit, LOG_WARNING, filename, line, ei, "Failed to parse input specifier, ignoring: %s", rvalue);
9bd6a50e
LP
1157 return 0;
1158 }
52c239d7 1159 }
9bd6a50e 1160
2038c3f5 1161 c->std_input = ei;
9bd6a50e 1162 return 0;
52c239d7
LB
1163}
1164
08f3be7a
LP
1165int config_parse_exec_input_text(
1166 const char *unit,
1167 const char *filename,
1168 unsigned line,
1169 const char *section,
1170 unsigned section_line,
1171 const char *lvalue,
1172 int ltype,
1173 const char *rvalue,
1174 void *data,
1175 void *userdata) {
1176
1177 _cleanup_free_ char *unescaped = NULL, *resolved = NULL;
99534007 1178 ExecContext *c = ASSERT_PTR(data);
47538b76 1179 const Unit *u = userdata;
08f3be7a
LP
1180 int r;
1181
08f3be7a
LP
1182 assert(filename);
1183 assert(line);
1184 assert(rvalue);
1185
1186 if (isempty(rvalue)) {
1187 /* Reset if the empty string is assigned */
1188 c->stdin_data = mfree(c->stdin_data);
1189 c->stdin_data_size = 0;
52c239d7
LB
1190 return 0;
1191 }
08f3be7a 1192
e437538f
ZJS
1193 ssize_t l = cunescape(rvalue, 0, &unescaped);
1194 if (l < 0) {
1195 log_syntax(unit, LOG_WARNING, filename, line, l,
323dda78
YW
1196 "Failed to decode C escaped text '%s', ignoring: %m", rvalue);
1197 return 0;
1198 }
08f3be7a 1199
06536492 1200 r = unit_full_printf_full(u, unescaped, EXEC_STDIN_DATA_MAX, &resolved);
323dda78
YW
1201 if (r < 0) {
1202 log_syntax(unit, LOG_WARNING, filename, line, r,
1203 "Failed to resolve unit specifiers in '%s', ignoring: %m", unescaped);
1204 return 0;
1205 }
08f3be7a 1206
e437538f 1207 size_t sz = strlen(resolved);
08f3be7a
LP
1208 if (c->stdin_data_size + sz + 1 < c->stdin_data_size || /* check for overflow */
1209 c->stdin_data_size + sz + 1 > EXEC_STDIN_DATA_MAX) {
323dda78
YW
1210 log_syntax(unit, LOG_WARNING, filename, line, 0,
1211 "Standard input data too large (%zu), maximum of %zu permitted, ignoring.",
1212 c->stdin_data_size + sz, (size_t) EXEC_STDIN_DATA_MAX);
1213 return 0;
08f3be7a
LP
1214 }
1215
e437538f 1216 void *p = realloc(c->stdin_data, c->stdin_data_size + sz + 1);
08f3be7a
LP
1217 if (!p)
1218 return log_oom();
1219
1220 *((char*) mempcpy((char*) p + c->stdin_data_size, resolved, sz)) = '\n';
1221
1222 c->stdin_data = p;
1223 c->stdin_data_size += sz + 1;
1224
1225 return 0;
52c239d7
LB
1226}
1227
08f3be7a
LP
1228int config_parse_exec_input_data(
1229 const char *unit,
1230 const char *filename,
1231 unsigned line,
1232 const char *section,
1233 unsigned section_line,
1234 const char *lvalue,
1235 int ltype,
1236 const char *rvalue,
1237 void *data,
1238 void *userdata) {
1239
08f3be7a 1240 _cleanup_free_ void *p = NULL;
99534007 1241 ExecContext *c = ASSERT_PTR(data);
08f3be7a
LP
1242 size_t sz;
1243 void *q;
1244 int r;
1245
08f3be7a
LP
1246 assert(filename);
1247 assert(line);
1248 assert(rvalue);
1249
1250 if (isempty(rvalue)) {
1251 /* Reset if the empty string is assigned */
1252 c->stdin_data = mfree(c->stdin_data);
1253 c->stdin_data_size = 0;
1254 return 0;
1255 }
1256
bdd2036e 1257 r = unbase64mem(rvalue, &p, &sz);
323dda78
YW
1258 if (r < 0) {
1259 log_syntax(unit, LOG_WARNING, filename, line, r,
1260 "Failed to decode base64 data, ignoring: %s", rvalue);
1261 return 0;
1262 }
08f3be7a
LP
1263
1264 assert(sz > 0);
1265
1266 if (c->stdin_data_size + sz < c->stdin_data_size || /* check for overflow */
1267 c->stdin_data_size + sz > EXEC_STDIN_DATA_MAX) {
323dda78
YW
1268 log_syntax(unit, LOG_WARNING, filename, line, 0,
1269 "Standard input data too large (%zu), maximum of %zu permitted, ignoring.",
1270 c->stdin_data_size + sz, (size_t) EXEC_STDIN_DATA_MAX);
1271 return 0;
08f3be7a
LP
1272 }
1273
1274 q = realloc(c->stdin_data, c->stdin_data_size + sz);
1275 if (!q)
1276 return log_oom();
1277
1278 memcpy((uint8_t*) q + c->stdin_data_size, p, sz);
1279
1280 c->stdin_data = q;
1281 c->stdin_data_size += sz;
1282
1283 return 0;
1284}
1285
2038c3f5
LP
1286int config_parse_exec_output(
1287 const char *unit,
1288 const char *filename,
1289 unsigned line,
1290 const char *section,
1291 unsigned section_line,
1292 const char *lvalue,
1293 int ltype,
1294 const char *rvalue,
1295 void *data,
1296 void *userdata) {
1297
1298 _cleanup_free_ char *resolved = NULL;
1299 const char *n;
99534007 1300 ExecContext *c = ASSERT_PTR(data);
47538b76 1301 const Unit *u = userdata;
f3dc6af2 1302 bool obsolete = false;
52c239d7 1303 ExecOutput eo;
52c239d7
LB
1304 int r;
1305
52c239d7
LB
1306 assert(filename);
1307 assert(line);
1308 assert(lvalue);
1309 assert(rvalue);
1310
2038c3f5
LP
1311 n = startswith(rvalue, "fd:");
1312 if (n) {
06536492 1313 r = unit_fd_printf(u, n, &resolved);
323dda78
YW
1314 if (r < 0) {
1315 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in %s: %m", n);
1316 return 0;
1317 }
2038c3f5
LP
1318
1319 if (isempty(resolved))
1320 resolved = mfree(resolved);
1321 else if (!fdname_is_valid(resolved)) {
323dda78
YW
1322 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid file descriptor name, ignoring: %s", resolved);
1323 return 0;
52c239d7 1324 }
2038c3f5 1325
52c239d7 1326 eo = EXEC_OUTPUT_NAMED_FD;
2038c3f5 1327
f3dc6af2
LP
1328 } else if (streq(rvalue, "syslog")) {
1329 eo = EXEC_OUTPUT_JOURNAL;
1330 obsolete = true;
1331
1332 } else if (streq(rvalue, "syslog+console")) {
1333 eo = EXEC_OUTPUT_JOURNAL_AND_CONSOLE;
1334 obsolete = true;
1335
2038c3f5
LP
1336 } else if ((n = startswith(rvalue, "file:"))) {
1337
06536492 1338 r = unit_path_printf(u, n, &resolved);
323dda78
YW
1339 if (r < 0) {
1340 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", n);
1341 return 0;
1342 }
2038c3f5 1343
2f4d31c1
YW
1344 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE | PATH_CHECK_FATAL, unit, filename, line, lvalue);
1345 if (r < 0)
323dda78 1346 return 0;
2038c3f5
LP
1347
1348 eo = EXEC_OUTPUT_FILE;
1349
566b7d23
ZD
1350 } else if ((n = startswith(rvalue, "append:"))) {
1351
06536492 1352 r = unit_path_printf(u, n, &resolved);
323dda78
YW
1353 if (r < 0) {
1354 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", n);
1355 return 0;
1356 }
566b7d23
ZD
1357
1358 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE | PATH_CHECK_FATAL, unit, filename, line, lvalue);
1359 if (r < 0)
323dda78 1360 return 0;
566b7d23
ZD
1361
1362 eo = EXEC_OUTPUT_FILE_APPEND;
8d7dab1f
LW
1363
1364 } else if ((n = startswith(rvalue, "truncate:"))) {
1365
06536492 1366 r = unit_path_printf(u, n, &resolved);
8d7dab1f
LW
1367 if (r < 0) {
1368 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", n);
1369 return 0;
1370 }
1371
1372 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE | PATH_CHECK_FATAL, unit, filename, line, lvalue);
1373 if (r < 0)
1374 return 0;
1375
1376 eo = EXEC_OUTPUT_FILE_TRUNCATE;
52c239d7
LB
1377 } else {
1378 eo = exec_output_from_string(rvalue);
2038c3f5 1379 if (eo < 0) {
b98680b2 1380 log_syntax(unit, LOG_WARNING, filename, line, eo, "Failed to parse output specifier, ignoring: %s", rvalue);
52c239d7
LB
1381 return 0;
1382 }
1383 }
1384
f3dc6af2
LP
1385 if (obsolete)
1386 log_syntax(unit, LOG_NOTICE, filename, line, 0,
1387 "Standard output type %s is obsolete, automatically updating to %s. Please update your unit file, and consider removing the setting altogether.",
1388 rvalue, exec_output_to_string(eo));
1389
52c239d7 1390 if (streq(lvalue, "StandardOutput")) {
2038c3f5
LP
1391 if (eo == EXEC_OUTPUT_NAMED_FD)
1392 free_and_replace(c->stdio_fdname[STDOUT_FILENO], resolved);
1393 else
1394 free_and_replace(c->stdio_file[STDOUT_FILENO], resolved);
1395
52c239d7 1396 c->std_output = eo;
2038c3f5 1397
52c239d7 1398 } else {
2038c3f5
LP
1399 assert(streq(lvalue, "StandardError"));
1400
1401 if (eo == EXEC_OUTPUT_NAMED_FD)
1402 free_and_replace(c->stdio_fdname[STDERR_FILENO], resolved);
1403 else
1404 free_and_replace(c->stdio_file[STDERR_FILENO], resolved);
1405
1406 c->std_error = eo;
52c239d7 1407 }
2038c3f5
LP
1408
1409 return 0;
52c239d7 1410}
87f0e418 1411
e8e581bf
ZJS
1412int config_parse_exec_io_class(const char *unit,
1413 const char *filename,
1414 unsigned line,
1415 const char *section,
71a61510 1416 unsigned section_line,
e8e581bf
ZJS
1417 const char *lvalue,
1418 int ltype,
1419 const char *rvalue,
1420 void *data,
1421 void *userdata) {
94f04347 1422
99534007 1423 ExecContext *c = ASSERT_PTR(data);
94f04347
LP
1424 int x;
1425
1426 assert(filename);
1427 assert(lvalue);
1428 assert(rvalue);
94f04347 1429
617d253a
YW
1430 if (isempty(rvalue)) {
1431 c->ioprio_set = false;
0692548c 1432 c->ioprio = IOPRIO_DEFAULT_CLASS_AND_PRIO;
617d253a
YW
1433 return 0;
1434 }
1435
f8b69d1d
MS
1436 x = ioprio_class_from_string(rvalue);
1437 if (x < 0) {
b98680b2 1438 log_syntax(unit, LOG_WARNING, filename, line, x, "Failed to parse IO scheduling class, ignoring: %s", rvalue);
c0b34696 1439 return 0;
0d87eb42 1440 }
94f04347 1441
ba7772fe 1442 c->ioprio = ioprio_normalize(ioprio_prio_value(x, ioprio_prio_data(c->ioprio)));
94f04347
LP
1443 c->ioprio_set = true;
1444
1445 return 0;
1446}
1447
e8e581bf
ZJS
1448int config_parse_exec_io_priority(const char *unit,
1449 const char *filename,
1450 unsigned line,
1451 const char *section,
71a61510 1452 unsigned section_line,
e8e581bf
ZJS
1453 const char *lvalue,
1454 int ltype,
1455 const char *rvalue,
1456 void *data,
1457 void *userdata) {
94f04347 1458
99534007 1459 ExecContext *c = ASSERT_PTR(data);
e8e581bf 1460 int i, r;
94f04347
LP
1461
1462 assert(filename);
1463 assert(lvalue);
1464 assert(rvalue);
94f04347 1465
617d253a
YW
1466 if (isempty(rvalue)) {
1467 c->ioprio_set = false;
0692548c 1468 c->ioprio = IOPRIO_DEFAULT_CLASS_AND_PRIO;
617d253a
YW
1469 return 0;
1470 }
1471
7f452159
LP
1472 r = ioprio_parse_priority(rvalue, &i);
1473 if (r < 0) {
323dda78 1474 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse IO priority, ignoring: %s", rvalue);
c0b34696 1475 return 0;
071830ff
LP
1476 }
1477
ba7772fe 1478 c->ioprio = ioprio_normalize(ioprio_prio_value(ioprio_prio_class(c->ioprio), i));
94f04347
LP
1479 c->ioprio_set = true;
1480
071830ff
LP
1481 return 0;
1482}
1483
e8e581bf
ZJS
1484int config_parse_exec_cpu_sched_policy(const char *unit,
1485 const char *filename,
1486 unsigned line,
1487 const char *section,
71a61510 1488 unsigned section_line,
e8e581bf
ZJS
1489 const char *lvalue,
1490 int ltype,
1491 const char *rvalue,
1492 void *data,
1493 void *userdata) {
9eba9da4 1494
99534007 1495 ExecContext *c = ASSERT_PTR(data);
94f04347
LP
1496 int x;
1497
1498 assert(filename);
1499 assert(lvalue);
1500 assert(rvalue);
94f04347 1501
b00e1a9e
YW
1502 if (isempty(rvalue)) {
1503 c->cpu_sched_set = false;
1504 c->cpu_sched_policy = SCHED_OTHER;
1505 c->cpu_sched_priority = 0;
1506 return 0;
1507 }
1508
f8b69d1d
MS
1509 x = sched_policy_from_string(rvalue);
1510 if (x < 0) {
b98680b2 1511 log_syntax(unit, LOG_WARNING, filename, line, x, "Failed to parse CPU scheduling policy, ignoring: %s", rvalue);
c0b34696 1512 return 0;
0d87eb42 1513 }
94f04347
LP
1514
1515 c->cpu_sched_policy = x;
bb112710
HHPF
1516 /* Moving to or from real-time policy? We need to adjust the priority */
1517 c->cpu_sched_priority = CLAMP(c->cpu_sched_priority, sched_get_priority_min(x), sched_get_priority_max(x));
94f04347
LP
1518 c->cpu_sched_set = true;
1519
1520 return 0;
1521}
1522
5e98086d
ZJS
1523int config_parse_exec_mount_apivfs(const char *unit,
1524 const char *filename,
1525 unsigned line,
1526 const char *section,
1527 unsigned section_line,
1528 const char *lvalue,
1529 int ltype,
1530 const char *rvalue,
1531 void *data,
1532 void *userdata) {
1533
99534007 1534 ExecContext *c = ASSERT_PTR(data);
5e98086d
ZJS
1535 int k;
1536
1537 assert(filename);
1538 assert(lvalue);
1539 assert(rvalue);
5e98086d
ZJS
1540
1541 if (isempty(rvalue)) {
1542 c->mount_apivfs_set = false;
1543 c->mount_apivfs = false;
1544 return 0;
1545 }
1546
1547 k = parse_boolean(rvalue);
1548 if (k < 0) {
1549 log_syntax(unit, LOG_WARNING, filename, line, k,
1550 "Failed to parse boolean value, ignoring: %s",
1551 rvalue);
1552 return 0;
1553 }
1554
1555 c->mount_apivfs_set = true;
1556 c->mount_apivfs = k;
1557 return 0;
1558}
1559
b070c7c0
MS
1560int config_parse_numa_mask(const char *unit,
1561 const char *filename,
1562 unsigned line,
1563 const char *section,
1564 unsigned section_line,
1565 const char *lvalue,
1566 int ltype,
1567 const char *rvalue,
1568 void *data,
1569 void *userdata) {
1570 int r;
99534007 1571 NUMAPolicy *p = ASSERT_PTR(data);
b070c7c0
MS
1572
1573 assert(filename);
1574 assert(lvalue);
1575 assert(rvalue);
b070c7c0 1576
332d387f
MS
1577 if (streq(rvalue, "all")) {
1578 r = numa_mask_add_all(&p->nodes);
323dda78
YW
1579 if (r < 0)
1580 log_syntax(unit, LOG_WARNING, filename, line, r,
1581 "Failed to create NUMA mask representing \"all\" NUMA nodes, ignoring: %m");
332d387f
MS
1582 } else {
1583 r = parse_cpu_set_extend(rvalue, &p->nodes, true, unit, filename, line, lvalue);
323dda78
YW
1584 if (r < 0)
1585 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse NUMA node mask, ignoring: %s", rvalue);
b070c7c0
MS
1586 }
1587
323dda78 1588 return 0;
b070c7c0
MS
1589}
1590
e8e581bf
ZJS
1591int config_parse_exec_cpu_sched_prio(const char *unit,
1592 const char *filename,
1593 unsigned line,
1594 const char *section,
71a61510 1595 unsigned section_line,
e8e581bf
ZJS
1596 const char *lvalue,
1597 int ltype,
1598 const char *rvalue,
1599 void *data,
1600 void *userdata) {
9eba9da4 1601
99534007 1602 ExecContext *c = ASSERT_PTR(data);
40c05a34 1603 int i, r;
9eba9da4
LP
1604
1605 assert(filename);
1606 assert(lvalue);
1607 assert(rvalue);
9eba9da4 1608
e8e581bf
ZJS
1609 r = safe_atoi(rvalue, &i);
1610 if (r < 0) {
323dda78 1611 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse CPU scheduling priority, ignoring: %s", rvalue);
c0b34696 1612 return 0;
94f04347 1613 }
9eba9da4 1614
40c05a34
LB
1615 /* On Linux RR/FIFO range from 1 to 99 and OTHER/BATCH may only be 0. Policy might be set later so
1616 * we do not check the precise range, but only the generic outer bounds. */
1617 if (i < 0 || i > 99) {
323dda78 1618 log_syntax(unit, LOG_WARNING, filename, line, 0, "CPU scheduling priority is out of range, ignoring: %s", rvalue);
bb112710
HHPF
1619 return 0;
1620 }
1621
94f04347
LP
1622 c->cpu_sched_priority = i;
1623 c->cpu_sched_set = true;
1624
1625 return 0;
1626}
1627
18d73705
LB
1628int config_parse_root_image_options(
1629 const char *unit,
1630 const char *filename,
1631 unsigned line,
1632 const char *section,
1633 unsigned section_line,
1634 const char *lvalue,
1635 int ltype,
1636 const char *rvalue,
1637 void *data,
1638 void *userdata) {
1639
1640 _cleanup_(mount_options_free_allp) MountOptions *options = NULL;
bc8d56d3 1641 _cleanup_strv_free_ char **l = NULL;
99534007 1642 ExecContext *c = ASSERT_PTR(data);
18d73705 1643 const Unit *u = userdata;
18d73705
LB
1644 int r;
1645
1646 assert(filename);
1647 assert(lvalue);
1648 assert(rvalue);
18d73705
LB
1649
1650 if (isempty(rvalue)) {
1651 c->root_image_options = mount_options_free_all(c->root_image_options);
1652 return 0;
1653 }
1654
bc8d56d3
LB
1655 r = strv_split_colon_pairs(&l, rvalue);
1656 if (r == -ENOMEM)
1657 return log_oom();
1658 if (r < 0) {
323dda78 1659 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse %s, ignoring: %s", lvalue, rvalue);
bc8d56d3
LB
1660 return 0;
1661 }
18d73705 1662
bc8d56d3 1663 STRV_FOREACH_PAIR(first, second, l) {
18d73705 1664 MountOptions *o = NULL;
9ece6444
LB
1665 _cleanup_free_ char *mount_options_resolved = NULL;
1666 const char *mount_options = NULL, *partition = "root";
569a0e42 1667 PartitionDesignator partition_designator;
18d73705 1668
9ece6444 1669 /* Format is either 'root:foo' or 'foo' (root is implied) */
bc8d56d3 1670 if (!isempty(*second)) {
9ece6444 1671 partition = *first;
bc8d56d3 1672 mount_options = *second;
18d73705 1673 } else
bc8d56d3 1674 mount_options = *first;
18d73705 1675
9ece6444
LB
1676 partition_designator = partition_designator_from_string(partition);
1677 if (partition_designator < 0) {
b98680b2
YW
1678 log_syntax(unit, LOG_WARNING, filename, line, partition_designator,
1679 "Invalid partition name %s, ignoring", partition);
18d73705
LB
1680 continue;
1681 }
18d73705
LB
1682 r = unit_full_printf(u, mount_options, &mount_options_resolved);
1683 if (r < 0) {
323dda78 1684 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", mount_options);
18d73705
LB
1685 continue;
1686 }
1687
1688 o = new(MountOptions, 1);
1689 if (!o)
1690 return log_oom();
1691 *o = (MountOptions) {
9ece6444 1692 .partition_designator = partition_designator,
18d73705
LB
1693 .options = TAKE_PTR(mount_options_resolved),
1694 };
9ece6444 1695 LIST_APPEND(mount_options, options, TAKE_PTR(o));
18d73705
LB
1696 }
1697
64903d18 1698 if (options)
18d73705 1699 LIST_JOIN(mount_options, c->root_image_options, options);
64903d18
ZJS
1700 else
1701 /* empty spaces/separators only */
1702 c->root_image_options = mount_options_free_all(c->root_image_options);
18d73705
LB
1703
1704 return 0;
1705}
1706
0389f4fa
LB
1707int config_parse_exec_root_hash(
1708 const char *unit,
1709 const char *filename,
1710 unsigned line,
1711 const char *section,
1712 unsigned section_line,
1713 const char *lvalue,
1714 int ltype,
1715 const char *rvalue,
1716 void *data,
1717 void *userdata) {
1718
1719 _cleanup_free_ void *roothash_decoded = NULL;
99534007 1720 ExecContext *c = ASSERT_PTR(data);
0389f4fa
LB
1721 size_t roothash_decoded_size = 0;
1722 int r;
1723
0389f4fa
LB
1724 assert(filename);
1725 assert(line);
1726 assert(rvalue);
1727
1728 if (isempty(rvalue)) {
1729 /* Reset if the empty string is assigned */
1730 c->root_hash_path = mfree(c->root_hash_path);
1731 c->root_hash = mfree(c->root_hash);
1732 c->root_hash_size = 0;
1733 return 0;
1734 }
1735
1736 if (path_is_absolute(rvalue)) {
1737 /* We have the path to a roothash to load and decode, eg: RootHash=/foo/bar.roothash */
1738 _cleanup_free_ char *p = NULL;
1739
1740 p = strdup(rvalue);
1741 if (!p)
1742 return -ENOMEM;
1743
1744 free_and_replace(c->root_hash_path, p);
1745 c->root_hash = mfree(c->root_hash);
1746 c->root_hash_size = 0;
1747 return 0;
1748 }
1749
1750 /* We have a roothash to decode, eg: RootHash=012345789abcdef */
bdd2036e 1751 r = unhexmem(rvalue, &roothash_decoded, &roothash_decoded_size);
323dda78
YW
1752 if (r < 0) {
1753 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to decode RootHash=, ignoring: %s", rvalue);
1754 return 0;
1755 }
1756 if (roothash_decoded_size < sizeof(sd_id128_t)) {
1757 log_syntax(unit, LOG_WARNING, filename, line, 0, "RootHash= is too short, ignoring: %s", rvalue);
1758 return 0;
1759 }
0389f4fa
LB
1760
1761 free_and_replace(c->root_hash, roothash_decoded);
1762 c->root_hash_size = roothash_decoded_size;
1763 c->root_hash_path = mfree(c->root_hash_path);
1764
1765 return 0;
1766}
1767
d4d55b0d
LB
1768int config_parse_exec_root_hash_sig(
1769 const char *unit,
1770 const char *filename,
1771 unsigned line,
1772 const char *section,
1773 unsigned section_line,
1774 const char *lvalue,
1775 int ltype,
1776 const char *rvalue,
1777 void *data,
1778 void *userdata) {
1779
1780 _cleanup_free_ void *roothash_sig_decoded = NULL;
1781 char *value;
99534007 1782 ExecContext *c = ASSERT_PTR(data);
d4d55b0d
LB
1783 size_t roothash_sig_decoded_size = 0;
1784 int r;
1785
d4d55b0d
LB
1786 assert(filename);
1787 assert(line);
1788 assert(rvalue);
1789
1790 if (isempty(rvalue)) {
1791 /* Reset if the empty string is assigned */
1792 c->root_hash_sig_path = mfree(c->root_hash_sig_path);
1793 c->root_hash_sig = mfree(c->root_hash_sig);
1794 c->root_hash_sig_size = 0;
1795 return 0;
1796 }
1797
1798 if (path_is_absolute(rvalue)) {
1799 /* We have the path to a roothash signature to load and decode, eg: RootHashSignature=/foo/bar.roothash.p7s */
1800 _cleanup_free_ char *p = NULL;
1801
1802 p = strdup(rvalue);
1803 if (!p)
323dda78 1804 return log_oom();
d4d55b0d
LB
1805
1806 free_and_replace(c->root_hash_sig_path, p);
1807 c->root_hash_sig = mfree(c->root_hash_sig);
1808 c->root_hash_sig_size = 0;
1809 return 0;
1810 }
1811
323dda78
YW
1812 if (!(value = startswith(rvalue, "base64:"))) {
1813 log_syntax(unit, LOG_WARNING, filename, line, 0,
1814 "Failed to decode RootHashSignature=, not a path but doesn't start with 'base64:', ignoring: %s", rvalue);
1815 return 0;
1816 }
d4d55b0d
LB
1817
1818 /* We have a roothash signature to decode, eg: RootHashSignature=base64:012345789abcdef */
bdd2036e 1819 r = unbase64mem(value, &roothash_sig_decoded, &roothash_sig_decoded_size);
323dda78
YW
1820 if (r < 0) {
1821 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to decode RootHashSignature=, ignoring: %s", rvalue);
1822 return 0;
1823 }
d4d55b0d
LB
1824
1825 free_and_replace(c->root_hash_sig, roothash_sig_decoded);
1826 c->root_hash_sig_size = roothash_sig_decoded_size;
1827 c->root_hash_sig_path = mfree(c->root_hash_sig_path);
1828
1829 return 0;
1830}
1831
ca9169f4
YW
1832int config_parse_exec_cpu_affinity(
1833 const char *unit,
1834 const char *filename,
1835 unsigned line,
1836 const char *section,
1837 unsigned section_line,
1838 const char *lvalue,
1839 int ltype,
1840 const char *rvalue,
1841 void *data,
1842 void *userdata) {
94f04347 1843
99534007 1844 ExecContext *c = ASSERT_PTR(data);
54cfe9a7
FG
1845 const Unit *u = userdata;
1846 _cleanup_free_ char *k = NULL;
e2b2fb7f 1847 int r;
94f04347
LP
1848
1849 assert(filename);
1850 assert(lvalue);
1851 assert(rvalue);
94f04347 1852
e2b2fb7f
MS
1853 if (streq(rvalue, "numa")) {
1854 c->cpu_affinity_from_numa = true;
1855 cpu_set_reset(&c->cpu_set);
1856
1857 return 0;
1858 }
1859
54cfe9a7
FG
1860 r = unit_full_printf(u, rvalue, &k);
1861 if (r < 0) {
1862 log_syntax(unit, LOG_WARNING, filename, line, r,
1863 "Failed to resolve unit specifiers in '%s', ignoring: %m",
1864 rvalue);
1865 return 0;
1866 }
1867
1868 r = parse_cpu_set_extend(k, &c->cpu_set, true, unit, filename, line, lvalue);
e2b2fb7f
MS
1869 if (r >= 0)
1870 c->cpu_affinity_from_numa = false;
1871
ca9169f4 1872 return 0;
94f04347
LP
1873}
1874
a103496c 1875int config_parse_capability_set(
65dce264
LP
1876 const char *unit,
1877 const char *filename,
1878 unsigned line,
1879 const char *section,
1880 unsigned section_line,
1881 const char *lvalue,
1882 int ltype,
1883 const char *rvalue,
1884 void *data,
1885 void *userdata) {
94f04347 1886
99534007 1887 uint64_t *capability_set = ASSERT_PTR(data);
3fd5190b 1888 uint64_t sum = 0, initial, def;
260abb78 1889 bool invert = false;
dd1f5bd0 1890 int r;
94f04347
LP
1891
1892 assert(filename);
1893 assert(lvalue);
1894 assert(rvalue);
94f04347 1895
260abb78
LP
1896 if (rvalue[0] == '~') {
1897 invert = true;
1898 rvalue++;
1899 }
1900
3fd5190b
LP
1901 if (streq(lvalue, "CapabilityBoundingSet")) {
1902 initial = CAP_MASK_ALL; /* initialized to all bits on */
1903 def = CAP_MASK_UNSET; /* not set */
1904 } else
1905 def = initial = 0; /* All bits off */
260abb78 1906
dd1f5bd0 1907 r = capability_set_from_string(rvalue, &sum);
dd1f5bd0 1908 if (r < 0) {
323dda78 1909 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse %s= specifier '%s', ignoring: %m", lvalue, rvalue);
dd1f5bd0 1910 return 0;
94f04347 1911 }
9eba9da4 1912
3fd5190b 1913 if (sum == 0 || *capability_set == def)
c792ec2e
IP
1914 /* "", "~" or uninitialized data -> replace */
1915 *capability_set = invert ? ~sum : sum;
1916 else {
a103496c 1917 /* previous data -> merge */
c792ec2e
IP
1918 if (invert)
1919 *capability_set &= ~sum;
1920 else
1921 *capability_set |= sum;
1922 }
260abb78 1923
9eba9da4
LP
1924 return 0;
1925}
1926
5f8640fb
LP
1927int config_parse_exec_selinux_context(
1928 const char *unit,
1929 const char *filename,
1930 unsigned line,
1931 const char *section,
1932 unsigned section_line,
1933 const char *lvalue,
1934 int ltype,
1935 const char *rvalue,
1936 void *data,
1937 void *userdata) {
1938
99534007 1939 ExecContext *c = ASSERT_PTR(data);
47538b76 1940 const Unit *u = userdata;
5f8640fb
LP
1941 bool ignore;
1942 char *k;
1943 int r;
1944
1945 assert(filename);
1946 assert(lvalue);
1947 assert(rvalue);
5f8640fb
LP
1948
1949 if (isempty(rvalue)) {
a1e58e8e 1950 c->selinux_context = mfree(c->selinux_context);
5f8640fb
LP
1951 c->selinux_context_ignore = false;
1952 return 0;
1953 }
1954
1955 if (rvalue[0] == '-') {
1956 ignore = true;
1957 rvalue++;
1958 } else
1959 ignore = false;
1960
18913df9 1961 r = unit_full_printf(u, rvalue, &k);
5f8640fb 1962 if (r < 0) {
323dda78 1963 log_syntax(unit, ignore ? LOG_WARNING : LOG_ERR, filename, line, r,
063c4b1a
YW
1964 "Failed to resolve unit specifiers in '%s'%s: %m",
1965 rvalue, ignore ? ", ignoring" : "");
bb28e684 1966 return ignore ? 0 : -ENOEXEC;
5f8640fb
LP
1967 }
1968
063c4b1a 1969 free_and_replace(c->selinux_context, k);
5f8640fb
LP
1970 c->selinux_context_ignore = ignore;
1971
1972 return 0;
1973}
1974
eef65bf3
MS
1975int config_parse_exec_apparmor_profile(
1976 const char *unit,
1977 const char *filename,
1978 unsigned line,
1979 const char *section,
1980 unsigned section_line,
1981 const char *lvalue,
1982 int ltype,
1983 const char *rvalue,
1984 void *data,
1985 void *userdata) {
1986
99534007 1987 ExecContext *c = ASSERT_PTR(data);
47538b76 1988 const Unit *u = userdata;
eef65bf3
MS
1989 bool ignore;
1990 char *k;
1991 int r;
1992
1993 assert(filename);
1994 assert(lvalue);
1995 assert(rvalue);
eef65bf3
MS
1996
1997 if (isempty(rvalue)) {
a1e58e8e 1998 c->apparmor_profile = mfree(c->apparmor_profile);
eef65bf3
MS
1999 c->apparmor_profile_ignore = false;
2000 return 0;
2001 }
2002
2003 if (rvalue[0] == '-') {
2004 ignore = true;
2005 rvalue++;
2006 } else
2007 ignore = false;
2008
18913df9 2009 r = unit_full_printf(u, rvalue, &k);
eef65bf3 2010 if (r < 0) {
323dda78 2011 log_syntax(unit, ignore ? LOG_WARNING : LOG_ERR, filename, line, r,
063c4b1a
YW
2012 "Failed to resolve unit specifiers in '%s'%s: %m",
2013 rvalue, ignore ? ", ignoring" : "");
bb28e684 2014 return ignore ? 0 : -ENOEXEC;
eef65bf3
MS
2015 }
2016
063c4b1a 2017 free_and_replace(c->apparmor_profile, k);
eef65bf3
MS
2018 c->apparmor_profile_ignore = ignore;
2019
2020 return 0;
2021}
2022
2ca620c4
WC
2023int config_parse_exec_smack_process_label(
2024 const char *unit,
2025 const char *filename,
2026 unsigned line,
2027 const char *section,
2028 unsigned section_line,
2029 const char *lvalue,
2030 int ltype,
2031 const char *rvalue,
2032 void *data,
2033 void *userdata) {
2034
99534007 2035 ExecContext *c = ASSERT_PTR(data);
47538b76 2036 const Unit *u = userdata;
2ca620c4
WC
2037 bool ignore;
2038 char *k;
2039 int r;
2040
2041 assert(filename);
2042 assert(lvalue);
2043 assert(rvalue);
2ca620c4
WC
2044
2045 if (isempty(rvalue)) {
a1e58e8e 2046 c->smack_process_label = mfree(c->smack_process_label);
2ca620c4
WC
2047 c->smack_process_label_ignore = false;
2048 return 0;
2049 }
2050
2051 if (rvalue[0] == '-') {
2052 ignore = true;
2053 rvalue++;
2054 } else
2055 ignore = false;
2056
18913df9 2057 r = unit_full_printf(u, rvalue, &k);
2ca620c4 2058 if (r < 0) {
323dda78 2059 log_syntax(unit, ignore ? LOG_WARNING : LOG_ERR, filename, line, r,
063c4b1a
YW
2060 "Failed to resolve unit specifiers in '%s'%s: %m",
2061 rvalue, ignore ? ", ignoring" : "");
bb28e684 2062 return ignore ? 0 : -ENOEXEC;
2ca620c4
WC
2063 }
2064
063c4b1a 2065 free_and_replace(c->smack_process_label, k);
2ca620c4
WC
2066 c->smack_process_label_ignore = ignore;
2067
2068 return 0;
2069}
2070
25a04ae5
LP
2071int config_parse_timer(
2072 const char *unit,
2073 const char *filename,
2074 unsigned line,
2075 const char *section,
2076 unsigned section_line,
2077 const char *lvalue,
2078 int ltype,
2079 const char *rvalue,
2080 void *data,
2081 void *userdata) {
871d7de4 2082
25a04ae5
LP
2083 _cleanup_(calendar_spec_freep) CalendarSpec *c = NULL;
2084 _cleanup_free_ char *k = NULL;
47538b76 2085 const Unit *u = userdata;
99534007 2086 Timer *t = ASSERT_PTR(data);
2507992f 2087 usec_t usec = 0;
871d7de4 2088 TimerValue *v;
2507992f 2089 int r;
871d7de4
LP
2090
2091 assert(filename);
2092 assert(lvalue);
2093 assert(rvalue);
871d7de4 2094
74051b9b
LP
2095 if (isempty(rvalue)) {
2096 /* Empty assignment resets list */
2097 timer_free_values(t);
2098 return 0;
2099 }
2100
2507992f
DC
2101 r = unit_full_printf(u, rvalue, &k);
2102 if (r < 0) {
323dda78 2103 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
2507992f
DC
2104 return 0;
2105 }
2106
25a04ae5 2107 if (ltype == TIMER_CALENDAR) {
dc44c96d
LP
2108 r = calendar_spec_from_string(k, &c);
2109 if (r < 0) {
323dda78 2110 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse calendar specification, ignoring: %s", k);
36697dc0
LP
2111 return 0;
2112 }
dc44c96d
LP
2113 } else {
2114 r = parse_sec(k, &usec);
2115 if (r < 0) {
323dda78 2116 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse timer value, ignoring: %s", k);
36697dc0
LP
2117 return 0;
2118 }
dc44c96d 2119 }
871d7de4 2120
25a04ae5 2121 v = new(TimerValue, 1);
921b5987 2122 if (!v)
74051b9b 2123 return log_oom();
871d7de4 2124
25a04ae5
LP
2125 *v = (TimerValue) {
2126 .base = ltype,
2127 .value = usec,
2128 .calendar_spec = TAKE_PTR(c),
2129 };
871d7de4 2130
71fda00f 2131 LIST_PREPEND(value, t->values, v);
871d7de4
LP
2132
2133 return 0;
2134}
2135
3ecaa09b
LP
2136int config_parse_trigger_unit(
2137 const char *unit,
2138 const char *filename,
2139 unsigned line,
2140 const char *section,
71a61510 2141 unsigned section_line,
3ecaa09b
LP
2142 const char *lvalue,
2143 int ltype,
2144 const char *rvalue,
2145 void *data,
2146 void *userdata) {
871d7de4 2147
74051b9b 2148 _cleanup_free_ char *p = NULL;
99534007 2149 Unit *u = ASSERT_PTR(data);
3ecaa09b
LP
2150 UnitType type;
2151 int r;
398ef8ba
LP
2152
2153 assert(filename);
2154 assert(lvalue);
2155 assert(rvalue);
398ef8ba 2156
bc32241e 2157 if (UNIT_TRIGGER(u)) {
323dda78 2158 log_syntax(unit, LOG_WARNING, filename, line, 0, "Multiple units to trigger specified, ignoring: %s", rvalue);
3ecaa09b
LP
2159 return 0;
2160 }
871d7de4 2161
19f6d710 2162 r = unit_name_printf(u, rvalue, &p);
12ca818f 2163 if (r < 0) {
323dda78 2164 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue);
12ca818f
LP
2165 return 0;
2166 }
74051b9b 2167
12ca818f 2168 type = unit_name_to_type(p);
3ecaa09b 2169 if (type < 0) {
b98680b2 2170 log_syntax(unit, LOG_WARNING, filename, line, type, "Unit type not valid, ignoring: %s", rvalue);
c0b34696 2171 return 0;
871d7de4 2172 }
49219a1c 2173 if (unit_has_name(u, p)) {
323dda78 2174 log_syntax(unit, LOG_WARNING, filename, line, 0, "Units cannot trigger themselves, ignoring: %s", rvalue);
3ecaa09b
LP
2175 return 0;
2176 }
2177
5a724170 2178 r = unit_add_two_dependencies_by_name(u, UNIT_BEFORE, UNIT_TRIGGERS, p, true, UNIT_DEPENDENCY_FILE);
57020a3a 2179 if (r < 0) {
323dda78 2180 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to add trigger on %s, ignoring: %m", p);
c0b34696 2181 return 0;
871d7de4
LP
2182 }
2183
2184 return 0;
2185}
2186
e8e581bf
ZJS
2187int config_parse_path_spec(const char *unit,
2188 const char *filename,
2189 unsigned line,
2190 const char *section,
71a61510 2191 unsigned section_line,
e8e581bf
ZJS
2192 const char *lvalue,
2193 int ltype,
2194 const char *rvalue,
2195 void *data,
2196 void *userdata) {
01f78473 2197
99534007 2198 Path *p = ASSERT_PTR(data);
01f78473
LP
2199 PathSpec *s;
2200 PathType b;
7fd1b19b 2201 _cleanup_free_ char *k = NULL;
19f6d710 2202 int r;
01f78473
LP
2203
2204 assert(filename);
2205 assert(lvalue);
2206 assert(rvalue);
01f78473 2207
74051b9b
LP
2208 if (isempty(rvalue)) {
2209 /* Empty assignment clears list */
2210 path_free_specs(p);
2211 return 0;
2212 }
2213
93e4c84b
LP
2214 b = path_type_from_string(lvalue);
2215 if (b < 0) {
b98680b2 2216 log_syntax(unit, LOG_WARNING, filename, line, b, "Failed to parse path type, ignoring: %s", lvalue);
c0b34696 2217 return 0;
01f78473
LP
2218 }
2219
06536492 2220 r = unit_path_printf(UNIT(p), rvalue, &k);
19f6d710 2221 if (r < 0) {
323dda78 2222 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue);
12ca818f 2223 return 0;
487060c2 2224 }
93e4c84b 2225
2f4d31c1
YW
2226 r = path_simplify_and_warn(k, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
2227 if (r < 0)
c0b34696 2228 return 0;
01f78473 2229
93e4c84b 2230 s = new0(PathSpec, 1);
543295ad 2231 if (!s)
93e4c84b 2232 return log_oom();
01f78473 2233
718db961 2234 s->unit = UNIT(p);
063c4b1a 2235 s->path = TAKE_PTR(k);
01f78473 2236 s->type = b;
254d1313 2237 s->inotify_fd = -EBADF;
01f78473 2238
71fda00f 2239 LIST_PREPEND(spec, p->specs, s);
01f78473
LP
2240
2241 return 0;
2242}
2243
b02cb41c
LP
2244int config_parse_socket_service(
2245 const char *unit,
2246 const char *filename,
2247 unsigned line,
2248 const char *section,
2249 unsigned section_line,
2250 const char *lvalue,
2251 int ltype,
2252 const char *rvalue,
2253 void *data,
2254 void *userdata) {
d9ff321a 2255
4afd3348 2256 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
8dd4c05b 2257 _cleanup_free_ char *p = NULL;
99534007 2258 Socket *s = ASSERT_PTR(data);
4ff77f66 2259 Unit *x;
8dd4c05b 2260 int r;
d9ff321a
LP
2261
2262 assert(filename);
2263 assert(lvalue);
2264 assert(rvalue);
d9ff321a 2265
19f6d710 2266 r = unit_name_printf(UNIT(s), rvalue, &p);
613b411c 2267 if (r < 0) {
323dda78
YW
2268 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue);
2269 return 0;
613b411c 2270 }
74051b9b 2271
613b411c 2272 if (!endswith(p, ".service")) {
323dda78
YW
2273 log_syntax(unit, LOG_WARNING, filename, line, 0, "Unit must be of type service, ignoring: %s", rvalue);
2274 return 0;
d9ff321a
LP
2275 }
2276
613b411c 2277 r = manager_load_unit(UNIT(s)->manager, p, NULL, &error, &x);
4ff77f66 2278 if (r < 0) {
323dda78
YW
2279 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to load unit %s, ignoring: %s", rvalue, bus_error_message(&error, r));
2280 return 0;
d9ff321a
LP
2281 }
2282
7f7d01ed 2283 unit_ref_set(&s->service, UNIT(s), x);
4ff77f66 2284
d9ff321a
LP
2285 return 0;
2286}
2287
8dd4c05b
LP
2288int config_parse_fdname(
2289 const char *unit,
2290 const char *filename,
2291 unsigned line,
2292 const char *section,
2293 unsigned section_line,
2294 const char *lvalue,
2295 int ltype,
2296 const char *rvalue,
2297 void *data,
2298 void *userdata) {
2299
2300 _cleanup_free_ char *p = NULL;
99534007 2301 Socket *s = ASSERT_PTR(data);
8dd4c05b
LP
2302 int r;
2303
2304 assert(filename);
2305 assert(lvalue);
2306 assert(rvalue);
8dd4c05b
LP
2307
2308 if (isempty(rvalue)) {
2309 s->fdname = mfree(s->fdname);
2310 return 0;
2311 }
2312
06536492 2313 r = unit_fd_printf(UNIT(s), rvalue, &p);
8dd4c05b 2314 if (r < 0) {
323dda78 2315 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
8dd4c05b
LP
2316 return 0;
2317 }
2318
2319 if (!fdname_is_valid(p)) {
323dda78 2320 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid file descriptor name, ignoring: %s", p);
8dd4c05b
LP
2321 return 0;
2322 }
2323
3b319885 2324 return free_and_replace(s->fdname, p);
8dd4c05b
LP
2325}
2326
b02cb41c
LP
2327int config_parse_service_sockets(
2328 const char *unit,
2329 const char *filename,
2330 unsigned line,
2331 const char *section,
2332 unsigned section_line,
2333 const char *lvalue,
2334 int ltype,
2335 const char *rvalue,
2336 void *data,
2337 void *userdata) {
f976f3f6 2338
99534007 2339 Service *s = ASSERT_PTR(data);
b02cb41c 2340 int r;
f976f3f6
LP
2341
2342 assert(filename);
2343 assert(lvalue);
2344 assert(rvalue);
f976f3f6 2345
323dda78 2346 for (const char *p = rvalue;;) {
6a0f3175 2347 _cleanup_free_ char *word = NULL, *k = NULL;
f976f3f6 2348
7b2313f5 2349 r = extract_first_word(&p, &word, NULL, 0);
7b2313f5 2350 if (r == -ENOMEM)
74051b9b 2351 return log_oom();
7b2313f5 2352 if (r < 0) {
323dda78
YW
2353 log_syntax(unit, LOG_WARNING, filename, line, r, "Trailing garbage in sockets, ignoring: %s", rvalue);
2354 return 0;
7b2313f5 2355 }
a687f500
ZJS
2356 if (r == 0)
2357 return 0;
f976f3f6 2358
7b2313f5 2359 r = unit_name_printf(UNIT(s), word, &k);
b02cb41c 2360 if (r < 0) {
323dda78 2361 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", word);
b02cb41c
LP
2362 continue;
2363 }
57020a3a 2364
b02cb41c 2365 if (!endswith(k, ".socket")) {
323dda78 2366 log_syntax(unit, LOG_WARNING, filename, line, 0, "Unit must be of type socket, ignoring: %s", k);
f976f3f6
LP
2367 continue;
2368 }
2369
5a724170 2370 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_WANTS, UNIT_AFTER, k, true, UNIT_DEPENDENCY_FILE);
57020a3a 2371 if (r < 0)
323dda78 2372 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to add dependency on %s, ignoring: %m", k);
f976f3f6 2373
35d8c19a 2374 r = unit_add_dependency_by_name(UNIT(s), UNIT_TRIGGERED_BY, k, true, UNIT_DEPENDENCY_FILE);
57020a3a 2375 if (r < 0)
323dda78 2376 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to add dependency on %s, ignoring: %m", k);
f976f3f6 2377 }
f976f3f6
LP
2378}
2379
b02cb41c
LP
2380int config_parse_bus_name(
2381 const char *unit,
2382 const char *filename,
2383 unsigned line,
2384 const char *section,
2385 unsigned section_line,
2386 const char *lvalue,
2387 int ltype,
2388 const char *rvalue,
2389 void *data,
2390 void *userdata) {
2391
2392 _cleanup_free_ char *k = NULL;
99534007 2393 const Unit *u = ASSERT_PTR(userdata);
b02cb41c
LP
2394 int r;
2395
2396 assert(filename);
2397 assert(lvalue);
2398 assert(rvalue);
b02cb41c 2399
06536492 2400 r = unit_full_printf_full(u, rvalue, SD_BUS_MAXIMUM_NAME_LENGTH, &k);
b02cb41c 2401 if (r < 0) {
323dda78 2402 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue);
b02cb41c
LP
2403 return 0;
2404 }
2405
5453a4b1 2406 if (!sd_bus_service_name_is_valid(k)) {
323dda78 2407 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid bus name, ignoring: %s", k);
b02cb41c
LP
2408 return 0;
2409 }
2410
2411 return config_parse_string(unit, filename, line, section, section_line, lvalue, ltype, k, data, userdata);
2412}
2413
aad41f08
LP
2414int config_parse_service_timeout(
2415 const char *unit,
2416 const char *filename,
2417 unsigned line,
2418 const char *section,
2419 unsigned section_line,
2420 const char *lvalue,
2421 int ltype,
2422 const char *rvalue,
2423 void *data,
2424 void *userdata) {
98709151 2425
99534007 2426 Service *s = ASSERT_PTR(userdata);
aad41f08 2427 usec_t usec;
98709151
LN
2428 int r;
2429
2430 assert(filename);
2431 assert(lvalue);
2432 assert(rvalue);
98709151 2433
6c58305a 2434 /* This is called for two cases: TimeoutSec= and TimeoutStartSec=. */
98709151 2435
fb27be3f
YW
2436 /* Traditionally, these options accepted 0 to disable the timeouts. However, a timeout of 0 suggests it happens
2437 * immediately, hence fix this to become USEC_INFINITY instead. This is in-line with how we internally handle
2438 * all other timeouts. */
2439 r = parse_sec_fix_0(rvalue, &usec);
aad41f08 2440 if (r < 0) {
323dda78 2441 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse %s= parameter, ignoring: %s", lvalue, rvalue);
aad41f08
LP
2442 return 0;
2443 }
d568a335 2444
6c58305a
YW
2445 s->start_timeout_defined = true;
2446 s->timeout_start_usec = usec;
36c16a7c 2447
6c58305a 2448 if (streq(lvalue, "TimeoutSec"))
aad41f08 2449 s->timeout_stop_usec = usec;
36c16a7c 2450
d568a335 2451 return 0;
98709151
LN
2452}
2453
a61d6874 2454int config_parse_timeout_abort(
dc653bf4
JK
2455 const char *unit,
2456 const char *filename,
2457 unsigned line,
2458 const char *section,
2459 unsigned section_line,
2460 const char *lvalue,
2461 int ltype,
2462 const char *rvalue,
2463 void *data,
2464 void *userdata) {
2465
99534007 2466 usec_t *ret = ASSERT_PTR(data);
dc653bf4
JK
2467 int r;
2468
2469 assert(filename);
2470 assert(lvalue);
2471 assert(rvalue);
a61d6874
ZJS
2472
2473 /* Note: apart from setting the arg, this returns an extra bit of information in the return value. */
dc653bf4 2474
dc653bf4 2475 if (isempty(rvalue)) {
a61d6874
ZJS
2476 *ret = 0;
2477 return 0; /* "not set" */
dc653bf4
JK
2478 }
2479
a61d6874
ZJS
2480 r = parse_sec(rvalue, ret);
2481 if (r < 0)
323dda78 2482 return log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse %s= setting, ignoring: %s", lvalue, rvalue);
a61d6874
ZJS
2483
2484 return 1; /* "set" */
2485}
2486
2487int config_parse_service_timeout_abort(
2488 const char *unit,
2489 const char *filename,
2490 unsigned line,
2491 const char *section,
2492 unsigned section_line,
2493 const char *lvalue,
2494 int ltype,
2495 const char *rvalue,
2496 void *data,
2497 void *userdata) {
2498
99534007 2499 Service *s = ASSERT_PTR(userdata);
a61d6874 2500 int r;
dc653bf4 2501
a61d6874
ZJS
2502 r = config_parse_timeout_abort(unit, filename, line, section, section_line, lvalue, ltype, rvalue,
2503 &s->timeout_abort_usec, s);
2504 if (r >= 0)
2505 s->timeout_abort_set = r;
dc653bf4
JK
2506 return 0;
2507}
2508
ae480f0b 2509int config_parse_user_group_compat(
66dccd8d
LP
2510 const char *unit,
2511 const char *filename,
2512 unsigned line,
2513 const char *section,
2514 unsigned section_line,
2515 const char *lvalue,
2516 int ltype,
2517 const char *rvalue,
2518 void *data,
2519 void *userdata) {
2520
063c4b1a
YW
2521 _cleanup_free_ char *k = NULL;
2522 char **user = data;
99534007 2523 const Unit *u = ASSERT_PTR(userdata);
66dccd8d
LP
2524 int r;
2525
2526 assert(filename);
2527 assert(lvalue);
2528 assert(rvalue);
66dccd8d 2529
063c4b1a
YW
2530 if (isempty(rvalue)) {
2531 *user = mfree(*user);
2532 return 0;
2533 }
66dccd8d 2534
063c4b1a
YW
2535 r = unit_full_printf(u, rvalue, &k);
2536 if (r < 0) {
2537 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s: %m", rvalue);
2538 return -ENOEXEC;
66dccd8d
LP
2539 }
2540
7a8867ab 2541 if (!valid_user_group_name(k, VALID_USER_ALLOW_NUMERIC|VALID_USER_RELAX|VALID_USER_WARN)) {
063c4b1a
YW
2542 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid user/group name or numeric ID: %s", k);
2543 return -ENOEXEC;
2544 }
66dccd8d 2545
bed0b7df
LP
2546 if (strstr(lvalue, "User") && streq(k, NOBODY_USER_NAME))
2547 log_struct(LOG_NOTICE,
2548 "MESSAGE=%s:%u: Special user %s configured, this is not safe!", filename, line, k,
2549 "UNIT=%s", unit,
2550 "MESSAGE_ID=" SD_MESSAGE_NOBODY_USER_UNSUITABLE_STR,
2551 "OFFENDING_USER=%s", k,
2552 "CONFIG_FILE=%s", filename,
2553 "CONFIG_LINE=%u", line);
2554
063c4b1a 2555 return free_and_replace(*user, k);
66dccd8d
LP
2556}
2557
ae480f0b 2558int config_parse_user_group_strv_compat(
66dccd8d
LP
2559 const char *unit,
2560 const char *filename,
2561 unsigned line,
2562 const char *section,
2563 unsigned section_line,
2564 const char *lvalue,
2565 int ltype,
2566 const char *rvalue,
2567 void *data,
2568 void *userdata) {
2569
2570 char ***users = data;
99534007 2571 const Unit *u = ASSERT_PTR(userdata);
66dccd8d
LP
2572 int r;
2573
2574 assert(filename);
2575 assert(lvalue);
2576 assert(rvalue);
66dccd8d
LP
2577
2578 if (isempty(rvalue)) {
9f2d41a6 2579 *users = strv_free(*users);
66dccd8d
LP
2580 return 0;
2581 }
2582
323dda78 2583 for (const char *p = rvalue;;) {
66dccd8d
LP
2584 _cleanup_free_ char *word = NULL, *k = NULL;
2585
9a82ab95 2586 r = extract_first_word(&p, &word, NULL, 0);
66dccd8d
LP
2587 if (r == -ENOMEM)
2588 return log_oom();
2589 if (r < 0) {
bb28e684
ZJS
2590 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid syntax: %s", rvalue);
2591 return -ENOEXEC;
66dccd8d 2592 }
a687f500
ZJS
2593 if (r == 0)
2594 return 0;
66dccd8d
LP
2595
2596 r = unit_full_printf(u, word, &k);
2597 if (r < 0) {
bb28e684
ZJS
2598 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s: %m", word);
2599 return -ENOEXEC;
66dccd8d
LP
2600 }
2601
7a8867ab 2602 if (!valid_user_group_name(k, VALID_USER_ALLOW_NUMERIC|VALID_USER_RELAX|VALID_USER_WARN)) {
bb28e684
ZJS
2603 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid user/group name or numeric ID: %s", k);
2604 return -ENOEXEC;
66dccd8d
LP
2605 }
2606
2607 r = strv_push(users, k);
2608 if (r < 0)
2609 return log_oom();
2610
2611 k = NULL;
2612 }
66dccd8d
LP
2613}
2614
5f5d8eab
LP
2615int config_parse_working_directory(
2616 const char *unit,
2617 const char *filename,
2618 unsigned line,
2619 const char *section,
2620 unsigned section_line,
2621 const char *lvalue,
2622 int ltype,
2623 const char *rvalue,
2624 void *data,
2625 void *userdata) {
2626
99534007
DT
2627 ExecContext *c = ASSERT_PTR(data);
2628 const Unit *u = ASSERT_PTR(userdata);
5f5d8eab
LP
2629 bool missing_ok;
2630 int r;
2631
2632 assert(filename);
2633 assert(lvalue);
2634 assert(rvalue);
5f5d8eab 2635
862fcffd
YW
2636 if (isempty(rvalue)) {
2637 c->working_directory_home = false;
2638 c->working_directory = mfree(c->working_directory);
2639 return 0;
2640 }
2641
5f5d8eab
LP
2642 if (rvalue[0] == '-') {
2643 missing_ok = true;
2644 rvalue++;
2645 } else
2646 missing_ok = false;
2647
2648 if (streq(rvalue, "~")) {
2649 c->working_directory_home = true;
2650 c->working_directory = mfree(c->working_directory);
2651 } else {
2652 _cleanup_free_ char *k = NULL;
2653
06536492 2654 r = unit_path_printf(u, rvalue, &k);
5f5d8eab 2655 if (r < 0) {
323dda78 2656 log_syntax(unit, missing_ok ? LOG_WARNING : LOG_ERR, filename, line, r,
bb28e684
ZJS
2657 "Failed to resolve unit specifiers in working directory path '%s'%s: %m",
2658 rvalue, missing_ok ? ", ignoring" : "");
2659 return missing_ok ? 0 : -ENOEXEC;
5f5d8eab
LP
2660 }
2661
2f4d31c1
YW
2662 r = path_simplify_and_warn(k, PATH_CHECK_ABSOLUTE | (missing_ok ? 0 : PATH_CHECK_FATAL), unit, filename, line, lvalue);
2663 if (r < 0)
bb28e684 2664 return missing_ok ? 0 : -ENOEXEC;
5f5d8eab 2665
5f5d8eab 2666 c->working_directory_home = false;
bb28e684 2667 free_and_replace(c->working_directory, k);
5f5d8eab
LP
2668 }
2669
2670 c->working_directory_missing_ok = missing_ok;
2671 return 0;
2672}
2673
e8e581bf
ZJS
2674int config_parse_unit_env_file(const char *unit,
2675 const char *filename,
2676 unsigned line,
2677 const char *section,
71a61510 2678 unsigned section_line,
e8e581bf
ZJS
2679 const char *lvalue,
2680 int ltype,
2681 const char *rvalue,
2682 void *data,
2683 void *userdata) {
ddb26e18 2684
99534007 2685 char ***env = ASSERT_PTR(data);
47538b76 2686 const Unit *u = userdata;
19f6d710 2687 _cleanup_free_ char *n = NULL;
853b8397 2688 int r;
ddb26e18
LP
2689
2690 assert(filename);
2691 assert(lvalue);
2692 assert(rvalue);
ddb26e18 2693
74051b9b
LP
2694 if (isempty(rvalue)) {
2695 /* Empty assignment frees the list */
6796073e 2696 *env = strv_free(*env);
74051b9b
LP
2697 return 0;
2698 }
2699
e195a5c1 2700 r = unit_path_printf(u, rvalue, &n);
12ca818f 2701 if (r < 0) {
323dda78 2702 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
12ca818f
LP
2703 return 0;
2704 }
8fef7659 2705
2f4d31c1
YW
2706 r = path_simplify_and_warn(n[0] == '-' ? n + 1 : n, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
2707 if (r < 0)
afe4bfe2 2708 return 0;
afe4bfe2 2709
2f4d31c1 2710 r = strv_push(env, n);
853b8397
LP
2711 if (r < 0)
2712 return log_oom();
2713
2f4d31c1
YW
2714 n = NULL;
2715
853b8397
LP
2716 return 0;
2717}
2718
f7f3f5c3
LP
2719int config_parse_environ(
2720 const char *unit,
2721 const char *filename,
2722 unsigned line,
2723 const char *section,
2724 unsigned section_line,
2725 const char *lvalue,
2726 int ltype,
2727 const char *rvalue,
2728 void *data,
2729 void *userdata) {
853b8397 2730
47538b76 2731 const Unit *u = userdata;
99534007 2732 char ***env = ASSERT_PTR(data);
19f6d710 2733 int r;
853b8397
LP
2734
2735 assert(filename);
2736 assert(lvalue);
2737 assert(rvalue);
853b8397
LP
2738
2739 if (isempty(rvalue)) {
2740 /* Empty assignment resets the list */
6796073e 2741 *env = strv_free(*env);
853b8397
LP
2742 return 0;
2743 }
2744
4870133b
LP
2745 /* If 'u' is set, we operate on the regular unit specifier table. Otherwise we use a manager-specific
2746 * specifier table (in which case ltype must contain the runtime scope). */
2747 const Specifier *table = u ? NULL : (const Specifier[]) {
0b40688d
RP
2748 COMMON_SYSTEM_SPECIFIERS,
2749 COMMON_TMP_SPECIFIERS,
4870133b 2750 COMMON_CREDS_SPECIFIERS(ltype),
0b40688d
RP
2751 { 'h', specifier_user_home, NULL },
2752 { 's', specifier_user_shell, NULL },
2753 };
2754
323dda78 2755 for (const char *p = rvalue;; ) {
13734c75 2756 _cleanup_free_ char *word = NULL, *resolved = NULL;
035fe294 2757
4ec85141 2758 r = extract_first_word(&p, &word, NULL, EXTRACT_CUNESCAPE|EXTRACT_UNQUOTE);
035fe294
ZJS
2759 if (r == -ENOMEM)
2760 return log_oom();
12ca818f 2761 if (r < 0) {
035fe294
ZJS
2762 log_syntax(unit, LOG_WARNING, filename, line, r,
2763 "Invalid syntax, ignoring: %s", rvalue);
12ca818f
LP
2764 return 0;
2765 }
a687f500
ZJS
2766 if (r == 0)
2767 return 0;
97d0e5f8 2768
4870133b 2769 if (table)
0b40688d 2770 r = specifier_printf(word, sc_arg_max(), table, NULL, NULL, &resolved);
4870133b
LP
2771 else
2772 r = unit_env_printf(u, word, &resolved);
46a9ee5d
LP
2773 if (r < 0) {
2774 log_syntax(unit, LOG_WARNING, filename, line, r,
2775 "Failed to resolve specifiers in %s, ignoring: %m", word);
2776 continue;
2777 }
853b8397 2778
13734c75 2779 if (!env_assignment_is_valid(resolved)) {
323dda78 2780 log_syntax(unit, LOG_WARNING, filename, line, 0,
13734c75 2781 "Invalid environment assignment, ignoring: %s", resolved);
853b8397
LP
2782 continue;
2783 }
2784
13734c75 2785 r = strv_env_replace_consume(env, TAKE_PTR(resolved));
54ac3494 2786 if (r < 0)
13734c75 2787 return log_error_errno(r, "Failed to update environment: %m");
853b8397 2788 }
ddb26e18
LP
2789}
2790
00819cc1
LP
2791int config_parse_pass_environ(
2792 const char *unit,
2793 const char *filename,
2794 unsigned line,
2795 const char *section,
2796 unsigned section_line,
2797 const char *lvalue,
2798 int ltype,
2799 const char *rvalue,
2800 void *data,
2801 void *userdata) {
b4c14404 2802
b4c14404 2803 _cleanup_strv_free_ char **n = NULL;
47538b76 2804 const Unit *u = userdata;
99534007 2805 char*** passenv = ASSERT_PTR(data);
319a4f4b 2806 size_t nlen = 0;
b4c14404
FB
2807 int r;
2808
2809 assert(filename);
2810 assert(lvalue);
2811 assert(rvalue);
b4c14404
FB
2812
2813 if (isempty(rvalue)) {
2814 /* Empty assignment resets the list */
2815 *passenv = strv_free(*passenv);
2816 return 0;
2817 }
2818
323dda78 2819 for (const char *p = rvalue;;) {
41de9cc2 2820 _cleanup_free_ char *word = NULL, *k = NULL;
b4c14404 2821
4ec85141 2822 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
b4c14404
FB
2823 if (r == -ENOMEM)
2824 return log_oom();
2825 if (r < 0) {
323dda78 2826 log_syntax(unit, LOG_WARNING, filename, line, r,
063c4b1a 2827 "Trailing garbage in %s, ignoring: %s", lvalue, rvalue);
b4c14404
FB
2828 break;
2829 }
a687f500
ZJS
2830 if (r == 0)
2831 break;
b4c14404 2832
41de9cc2 2833 if (u) {
06536492 2834 r = unit_env_printf(u, word, &k);
41de9cc2 2835 if (r < 0) {
323dda78 2836 log_syntax(unit, LOG_WARNING, filename, line, r,
063c4b1a 2837 "Failed to resolve specifiers in %s, ignoring: %m", word);
41de9cc2
LP
2838 continue;
2839 }
ae2a15bc
LP
2840 } else
2841 k = TAKE_PTR(word);
41de9cc2
LP
2842
2843 if (!env_name_is_valid(k)) {
323dda78 2844 log_syntax(unit, LOG_WARNING, filename, line, 0,
41de9cc2 2845 "Invalid environment name for %s, ignoring: %s", lvalue, k);
b4c14404
FB
2846 continue;
2847 }
2848
319a4f4b 2849 if (!GREEDY_REALLOC(n, nlen + 2))
b4c14404 2850 return log_oom();
41de9cc2 2851
1cc6c93a 2852 n[nlen++] = TAKE_PTR(k);
b4c14404 2853 n[nlen] = NULL;
b4c14404
FB
2854 }
2855
2856 if (n) {
2857 r = strv_extend_strv(passenv, n, true);
2858 if (r < 0)
16eb0c4a 2859 return log_oom();
b4c14404
FB
2860 }
2861
2862 return 0;
2863}
2864
00819cc1
LP
2865int config_parse_unset_environ(
2866 const char *unit,
2867 const char *filename,
2868 unsigned line,
2869 const char *section,
2870 unsigned section_line,
2871 const char *lvalue,
2872 int ltype,
2873 const char *rvalue,
2874 void *data,
2875 void *userdata) {
2876
2877 _cleanup_strv_free_ char **n = NULL;
99534007 2878 char*** unsetenv = ASSERT_PTR(data);
47538b76 2879 const Unit *u = userdata;
319a4f4b 2880 size_t nlen = 0;
00819cc1
LP
2881 int r;
2882
2883 assert(filename);
2884 assert(lvalue);
2885 assert(rvalue);
00819cc1
LP
2886
2887 if (isempty(rvalue)) {
2888 /* Empty assignment resets the list */
2889 *unsetenv = strv_free(*unsetenv);
2890 return 0;
2891 }
2892
323dda78 2893 for (const char *p = rvalue;;) {
00819cc1
LP
2894 _cleanup_free_ char *word = NULL, *k = NULL;
2895
4ec85141 2896 r = extract_first_word(&p, &word, NULL, EXTRACT_CUNESCAPE|EXTRACT_UNQUOTE);
00819cc1
LP
2897 if (r == -ENOMEM)
2898 return log_oom();
2899 if (r < 0) {
323dda78 2900 log_syntax(unit, LOG_WARNING, filename, line, r,
063c4b1a 2901 "Trailing garbage in %s, ignoring: %s", lvalue, rvalue);
00819cc1
LP
2902 break;
2903 }
a687f500
ZJS
2904 if (r == 0)
2905 break;
00819cc1
LP
2906
2907 if (u) {
06536492 2908 r = unit_env_printf(u, word, &k);
00819cc1 2909 if (r < 0) {
323dda78 2910 log_syntax(unit, LOG_WARNING, filename, line, r,
063c4b1a 2911 "Failed to resolve unit specifiers in %s, ignoring: %m", word);
00819cc1
LP
2912 continue;
2913 }
ae2a15bc
LP
2914 } else
2915 k = TAKE_PTR(word);
00819cc1
LP
2916
2917 if (!env_assignment_is_valid(k) && !env_name_is_valid(k)) {
323dda78 2918 log_syntax(unit, LOG_WARNING, filename, line, 0,
00819cc1
LP
2919 "Invalid environment name or assignment %s, ignoring: %s", lvalue, k);
2920 continue;
2921 }
2922
319a4f4b 2923 if (!GREEDY_REALLOC(n, nlen + 2))
00819cc1
LP
2924 return log_oom();
2925
1cc6c93a 2926 n[nlen++] = TAKE_PTR(k);
00819cc1 2927 n[nlen] = NULL;
00819cc1
LP
2928 }
2929
2930 if (n) {
2931 r = strv_extend_strv(unsetenv, n, true);
2932 if (r < 0)
16eb0c4a 2933 return log_oom();
00819cc1
LP
2934 }
2935
2936 return 0;
2937}
2938
d3070fbd
LP
2939int config_parse_log_extra_fields(
2940 const char *unit,
2941 const char *filename,
2942 unsigned line,
2943 const char *section,
2944 unsigned section_line,
2945 const char *lvalue,
2946 int ltype,
2947 const char *rvalue,
2948 void *data,
2949 void *userdata) {
2950
99534007 2951 ExecContext *c = ASSERT_PTR(data);
47538b76 2952 const Unit *u = userdata;
d3070fbd
LP
2953 int r;
2954
2955 assert(filename);
2956 assert(lvalue);
2957 assert(rvalue);
d3070fbd
LP
2958
2959 if (isempty(rvalue)) {
2960 exec_context_free_log_extra_fields(c);
2961 return 0;
2962 }
2963
323dda78 2964 for (const char *p = rvalue;;) {
d3070fbd
LP
2965 _cleanup_free_ char *word = NULL, *k = NULL;
2966 struct iovec *t;
2967 const char *eq;
2968
4ec85141 2969 r = extract_first_word(&p, &word, NULL, EXTRACT_CUNESCAPE|EXTRACT_UNQUOTE);
d3070fbd
LP
2970 if (r == -ENOMEM)
2971 return log_oom();
2972 if (r < 0) {
2973 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
2974 return 0;
2975 }
a687f500
ZJS
2976 if (r == 0)
2977 return 0;
d3070fbd
LP
2978
2979 r = unit_full_printf(u, word, &k);
2980 if (r < 0) {
323dda78 2981 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", word);
d3070fbd
LP
2982 continue;
2983 }
2984
2985 eq = strchr(k, '=');
2986 if (!eq) {
323dda78 2987 log_syntax(unit, LOG_WARNING, filename, line, 0, "Log field lacks '=' character, ignoring: %s", k);
d3070fbd
LP
2988 continue;
2989 }
2990
2991 if (!journal_field_valid(k, eq-k, false)) {
323dda78 2992 log_syntax(unit, LOG_WARNING, filename, line, 0, "Log field name is invalid, ignoring: %s", k);
d3070fbd
LP
2993 continue;
2994 }
2995
aa484f35 2996 t = reallocarray(c->log_extra_fields, c->n_log_extra_fields+1, sizeof(struct iovec));
d3070fbd
LP
2997 if (!t)
2998 return log_oom();
2999
3000 c->log_extra_fields = t;
3001 c->log_extra_fields[c->n_log_extra_fields++] = IOVEC_MAKE_STRING(k);
3002
3003 k = NULL;
3004 }
d3070fbd
LP
3005}
3006
91dd5f7c
LP
3007int config_parse_log_namespace(
3008 const char *unit,
3009 const char *filename,
3010 unsigned line,
3011 const char *section,
3012 unsigned section_line,
3013 const char *lvalue,
3014 int ltype,
3015 const char *rvalue,
3016 void *data,
3017 void *userdata) {
3018
3019 _cleanup_free_ char *k = NULL;
99534007 3020 ExecContext *c = ASSERT_PTR(data);
91dd5f7c
LP
3021 const Unit *u = userdata;
3022 int r;
3023
3024 assert(filename);
3025 assert(lvalue);
3026 assert(rvalue);
91dd5f7c
LP
3027
3028 if (isempty(rvalue)) {
3029 c->log_namespace = mfree(c->log_namespace);
3030 return 0;
3031 }
3032
06536492 3033 r = unit_full_printf_full(u, rvalue, NAME_MAX, &k);
91dd5f7c 3034 if (r < 0) {
323dda78 3035 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue);
91dd5f7c
LP
3036 return 0;
3037 }
3038
3039 if (!log_namespace_name_valid(k)) {
323dda78 3040 log_syntax(unit, LOG_WARNING, filename, line, 0, "Specified log namespace name is not valid, ignoring: %s", k);
91dd5f7c
LP
3041 return 0;
3042 }
3043
3044 free_and_replace(c->log_namespace, k);
3045 return 0;
3046}
3047
59fccdc5
LP
3048int config_parse_unit_condition_path(
3049 const char *unit,
3050 const char *filename,
3051 unsigned line,
3052 const char *section,
3053 unsigned section_line,
3054 const char *lvalue,
3055 int ltype,
3056 const char *rvalue,
3057 void *data,
3058 void *userdata) {
52661efd 3059
2fbe635a 3060 _cleanup_free_ char *p = NULL;
99534007 3061 Condition **list = ASSERT_PTR(data), *c;
59fccdc5
LP
3062 ConditionType t = ltype;
3063 bool trigger, negate;
47538b76 3064 const Unit *u = userdata;
19f6d710 3065 int r;
52661efd
LP
3066
3067 assert(filename);
3068 assert(lvalue);
3069 assert(rvalue);
52661efd 3070
74051b9b
LP
3071 if (isempty(rvalue)) {
3072 /* Empty assignment resets the list */
447021aa 3073 *list = condition_free_list(*list);
74051b9b
LP
3074 return 0;
3075 }
3076
ab7f148f
LP
3077 trigger = rvalue[0] == '|';
3078 if (trigger)
267632f0
LP
3079 rvalue++;
3080
ab7f148f
LP
3081 negate = rvalue[0] == '!';
3082 if (negate)
52661efd
LP
3083 rvalue++;
3084
06536492 3085 r = unit_path_printf(u, rvalue, &p);
59fccdc5 3086 if (r < 0) {
323dda78 3087 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue);
59fccdc5 3088 return 0;
19f6d710 3089 }
095b2d7a 3090
2f4d31c1
YW
3091 r = path_simplify_and_warn(p, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
3092 if (r < 0)
52661efd 3093 return 0;
52661efd 3094
59fccdc5 3095 c = condition_new(t, p, trigger, negate);
ab7f148f 3096 if (!c)
74051b9b 3097 return log_oom();
52661efd 3098
59fccdc5 3099 LIST_PREPEND(conditions, *list, c);
52661efd
LP
3100 return 0;
3101}
3102
59fccdc5
LP
3103int config_parse_unit_condition_string(
3104 const char *unit,
3105 const char *filename,
3106 unsigned line,
3107 const char *section,
3108 unsigned section_line,
3109 const char *lvalue,
3110 int ltype,
3111 const char *rvalue,
3112 void *data,
3113 void *userdata) {
039655a4 3114
2fbe635a 3115 _cleanup_free_ char *s = NULL;
99534007 3116 Condition **list = ASSERT_PTR(data), *c;
59fccdc5
LP
3117 ConditionType t = ltype;
3118 bool trigger, negate;
47538b76 3119 const Unit *u = userdata;
19f6d710 3120 int r;
039655a4
LP
3121
3122 assert(filename);
3123 assert(lvalue);
3124 assert(rvalue);
039655a4 3125
74051b9b
LP
3126 if (isempty(rvalue)) {
3127 /* Empty assignment resets the list */
447021aa 3128 *list = condition_free_list(*list);
74051b9b
LP
3129 return 0;
3130 }
3131
9266f31e 3132 trigger = *rvalue == '|';
c0d6e764 3133 if (trigger)
9266f31e 3134 rvalue += 1 + strspn(rvalue + 1, WHITESPACE);
267632f0 3135
9266f31e 3136 negate = *rvalue == '!';
c0d6e764 3137 if (negate)
9266f31e 3138 rvalue += 1 + strspn(rvalue + 1, WHITESPACE);
039655a4 3139
19f6d710 3140 r = unit_full_printf(u, rvalue, &s);
59fccdc5 3141 if (r < 0) {
323dda78 3142 log_syntax(unit, LOG_WARNING, filename, line, r,
cae90de3 3143 "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
59fccdc5 3144 return 0;
19f6d710 3145 }
095b2d7a 3146
59fccdc5 3147 c = condition_new(t, s, trigger, negate);
c0d6e764
LP
3148 if (!c)
3149 return log_oom();
039655a4 3150
59fccdc5 3151 LIST_PREPEND(conditions, *list, c);
039655a4
LP
3152 return 0;
3153}
3154
9e615fa3 3155int config_parse_unit_mounts_for(
a57f7e2c
LP
3156 const char *unit,
3157 const char *filename,
3158 unsigned line,
3159 const char *section,
71a61510 3160 unsigned section_line,
a57f7e2c
LP
3161 const char *lvalue,
3162 int ltype,
3163 const char *rvalue,
3164 void *data,
3165 void *userdata) {
7c8fa05c
LP
3166
3167 Unit *u = userdata;
035fe294 3168 int r;
7c8fa05c
LP
3169
3170 assert(filename);
3171 assert(lvalue);
3172 assert(rvalue);
3173 assert(data);
9e615fa3 3174 assert(STR_IN_SET(lvalue, "RequiresMountsFor", "WantsMountsFor"));
7c8fa05c 3175
323dda78 3176 for (const char *p = rvalue;;) {
744bb5b1 3177 _cleanup_free_ char *word = NULL, *resolved = NULL;
a57f7e2c 3178
4ec85141 3179 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
035fe294 3180 if (r == -ENOMEM)
a57f7e2c 3181 return log_oom();
035fe294
ZJS
3182 if (r < 0) {
3183 log_syntax(unit, LOG_WARNING, filename, line, r,
3184 "Invalid syntax, ignoring: %s", rvalue);
3185 return 0;
3186 }
a687f500
ZJS
3187 if (r == 0)
3188 return 0;
7c8fa05c 3189
06536492 3190 r = unit_path_printf(u, word, &resolved);
744bb5b1 3191 if (r < 0) {
323dda78 3192 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", word);
744bb5b1
LP
3193 continue;
3194 }
3195
2f4d31c1
YW
3196 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
3197 if (r < 0)
3198 continue;
3199
9e615fa3 3200 r = unit_add_mounts_for(u, resolved, UNIT_DEPENDENCY_FILE, unit_mount_dependency_type_from_string(lvalue));
a57f7e2c 3201 if (r < 0) {
9e615fa3 3202 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to add requested mount '%s', ignoring: %m", resolved);
a57f7e2c
LP
3203 continue;
3204 }
3205 }
7c8fa05c 3206}
9e372868 3207
8c6493e5
YW
3208int config_parse_documentation(
3209 const char *unit,
3210 const char *filename,
3211 unsigned line,
3212 const char *section,
3213 unsigned section_line,
3214 const char *lvalue,
3215 int ltype,
3216 const char *rvalue,
3217 void *data,
3218 void *userdata) {
49dbfa7b 3219
99534007 3220 Unit *u = ASSERT_PTR(userdata);
49dbfa7b
LP
3221 int r;
3222 char **a, **b;
3223
3224 assert(filename);
3225 assert(lvalue);
3226 assert(rvalue);
49dbfa7b 3227
74051b9b
LP
3228 if (isempty(rvalue)) {
3229 /* Empty assignment resets the list */
6796073e 3230 u->documentation = strv_free(u->documentation);
74051b9b
LP
3231 return 0;
3232 }
3233
71a61510 3234 r = config_parse_unit_strv_printf(unit, filename, line, section, section_line, lvalue, ltype,
e8e581bf 3235 rvalue, data, userdata);
49dbfa7b
LP
3236 if (r < 0)
3237 return r;
3238
3239 for (a = b = u->documentation; a && *a; a++) {
3240
a2e03378 3241 if (documentation_url_is_valid(*a))
49dbfa7b
LP
3242 *(b++) = *a;
3243 else {
323dda78 3244 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid URL, ignoring: %s", *a);
49dbfa7b
LP
3245 free(*a);
3246 }
3247 }
f6d2d421
ZJS
3248 if (b)
3249 *b = NULL;
49dbfa7b 3250
8c6493e5 3251 return 0;
49dbfa7b
LP
3252}
3253
349cc4a5 3254#if HAVE_SECCOMP
17df7223
LP
3255int config_parse_syscall_filter(
3256 const char *unit,
3257 const char *filename,
3258 unsigned line,
3259 const char *section,
3260 unsigned section_line,
3261 const char *lvalue,
3262 int ltype,
3263 const char *rvalue,
3264 void *data,
3265 void *userdata) {
3266
8351ceae 3267 ExecContext *c = data;
99534007 3268 _unused_ const Unit *u = ASSERT_PTR(userdata);
b5fb3789 3269 bool invert = false;
17df7223 3270 int r;
8351ceae
LP
3271
3272 assert(filename);
3273 assert(lvalue);
3274 assert(rvalue);
8351ceae 3275
74051b9b
LP
3276 if (isempty(rvalue)) {
3277 /* Empty assignment resets the list */
8cfa775f 3278 c->syscall_filter = hashmap_free(c->syscall_filter);
6b000af4 3279 c->syscall_allow_list = false;
74051b9b
LP
3280 return 0;
3281 }
3282
8351ceae
LP
3283 if (rvalue[0] == '~') {
3284 invert = true;
3285 rvalue++;
3286 }
3287
17df7223 3288 if (!c->syscall_filter) {
8cfa775f 3289 c->syscall_filter = hashmap_new(NULL);
17df7223
LP
3290 if (!c->syscall_filter)
3291 return log_oom();
3292
c0467cf3 3293 if (invert)
17df7223 3294 /* Allow everything but the ones listed */
6b000af4 3295 c->syscall_allow_list = false;
c0467cf3 3296 else {
17df7223 3297 /* Allow nothing but the ones listed */
6b000af4 3298 c->syscall_allow_list = true;
8351ceae 3299
387f6955 3300 /* Accept default syscalls if we are on an allow_list */
2f6b9110
LP
3301 r = seccomp_parse_syscall_filter(
3302 "@default", -1, c->syscall_filter,
6b000af4 3303 SECCOMP_PARSE_PERMISSIVE|SECCOMP_PARSE_ALLOW_LIST,
58f6ab44
ZJS
3304 unit,
3305 NULL, 0);
201c1cc2
TM
3306 if (r < 0)
3307 return r;
c0467cf3 3308 }
8351ceae
LP
3309 }
3310
323dda78 3311 for (const char *p = rvalue;;) {
8cfa775f
YW
3312 _cleanup_free_ char *word = NULL, *name = NULL;
3313 int num;
8351ceae 3314
8130926d 3315 r = extract_first_word(&p, &word, NULL, 0);
8130926d 3316 if (r == -ENOMEM)
74051b9b 3317 return log_oom();
8130926d 3318 if (r < 0) {
084a46d7
YW
3319 log_syntax(unit, LOG_WARNING, filename, line, r,
3320 "Invalid syntax, ignoring: %s", rvalue);
063c4b1a 3321 return 0;
8130926d 3322 }
a687f500
ZJS
3323 if (r == 0)
3324 return 0;
8351ceae 3325
8cfa775f
YW
3326 r = parse_syscall_and_errno(word, &name, &num);
3327 if (r < 0) {
084a46d7
YW
3328 log_syntax(unit, LOG_WARNING, filename, line, r,
3329 "Failed to parse syscall:errno, ignoring: %s", word);
3330 continue;
3331 }
3332 if (!invert && num >= 0) {
3333 log_syntax(unit, LOG_WARNING, filename, line, 0,
3334 "Allow-listed system calls cannot take error number, ignoring: %s", word);
8cfa775f
YW
3335 continue;
3336 }
3337
58f6ab44 3338 r = seccomp_parse_syscall_filter(
acd142af
LP
3339 name, num, c->syscall_filter,
3340 SECCOMP_PARSE_LOG|SECCOMP_PARSE_PERMISSIVE|
3341 (invert ? SECCOMP_PARSE_INVERT : 0)|
6b000af4 3342 (c->syscall_allow_list ? SECCOMP_PARSE_ALLOW_LIST : 0),
acd142af 3343 unit, filename, line);
201c1cc2
TM
3344 if (r < 0)
3345 return r;
c0467cf3 3346 }
17df7223
LP
3347}
3348
9df2cdd8
TM
3349int config_parse_syscall_log(
3350 const char *unit,
3351 const char *filename,
3352 unsigned line,
3353 const char *section,
3354 unsigned section_line,
3355 const char *lvalue,
3356 int ltype,
3357 const char *rvalue,
3358 void *data,
3359 void *userdata) {
3360
3361 ExecContext *c = data;
99534007 3362 _unused_ const Unit *u = ASSERT_PTR(userdata);
9df2cdd8
TM
3363 bool invert = false;
3364 const char *p;
3365 int r;
3366
3367 assert(filename);
3368 assert(lvalue);
3369 assert(rvalue);
9df2cdd8
TM
3370
3371 if (isempty(rvalue)) {
3372 /* Empty assignment resets the list */
3373 c->syscall_log = hashmap_free(c->syscall_log);
3374 c->syscall_log_allow_list = false;
3375 return 0;
3376 }
3377
3378 if (rvalue[0] == '~') {
3379 invert = true;
3380 rvalue++;
3381 }
3382
3383 if (!c->syscall_log) {
3384 c->syscall_log = hashmap_new(NULL);
3385 if (!c->syscall_log)
3386 return log_oom();
3387
3388 if (invert)
3389 /* Log everything but the ones listed */
3390 c->syscall_log_allow_list = false;
3391 else
3392 /* Log nothing but the ones listed */
3393 c->syscall_log_allow_list = true;
3394 }
3395
3396 p = rvalue;
3397 for (;;) {
696a13ba 3398 _cleanup_free_ char *word = NULL;
9df2cdd8
TM
3399
3400 r = extract_first_word(&p, &word, NULL, 0);
9df2cdd8
TM
3401 if (r == -ENOMEM)
3402 return log_oom();
3403 if (r < 0) {
3404 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
3405 return 0;
3406 }
a687f500
ZJS
3407 if (r == 0)
3408 return 0;
9df2cdd8 3409
9df2cdd8 3410 r = seccomp_parse_syscall_filter(
696a13ba 3411 word, -1, c->syscall_log,
9df2cdd8
TM
3412 SECCOMP_PARSE_LOG|SECCOMP_PARSE_PERMISSIVE|
3413 (invert ? SECCOMP_PARSE_INVERT : 0)|
3414 (c->syscall_log_allow_list ? SECCOMP_PARSE_ALLOW_LIST : 0),
3415 unit, filename, line);
3416 if (r < 0)
3417 return r;
3418 }
3419}
3420
57183d11
LP
3421int config_parse_syscall_archs(
3422 const char *unit,
3423 const char *filename,
3424 unsigned line,
3425 const char *section,
3426 unsigned section_line,
3427 const char *lvalue,
3428 int ltype,
3429 const char *rvalue,
3430 void *data,
3431 void *userdata) {
3432
d3b1c508 3433 Set **archs = data;
57183d11
LP
3434 int r;
3435
3436 if (isempty(rvalue)) {
525d3cc7 3437 *archs = set_free(*archs);
57183d11
LP
3438 return 0;
3439 }
3440
323dda78 3441 for (const char *p = rvalue;;) {
035fe294 3442 _cleanup_free_ char *word = NULL;
57183d11
LP
3443 uint32_t a;
3444
4ec85141 3445 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
035fe294 3446 if (r == -ENOMEM)
57183d11 3447 return log_oom();
035fe294
ZJS
3448 if (r < 0) {
3449 log_syntax(unit, LOG_WARNING, filename, line, r,
3450 "Invalid syntax, ignoring: %s", rvalue);
3451 return 0;
3452 }
a687f500
ZJS
3453 if (r == 0)
3454 return 0;
57183d11 3455
035fe294 3456 r = seccomp_arch_from_string(word, &a);
57183d11 3457 if (r < 0) {
323dda78 3458 log_syntax(unit, LOG_WARNING, filename, line, r,
035fe294 3459 "Failed to parse system call architecture \"%s\", ignoring: %m", word);
57183d11
LP
3460 continue;
3461 }
3462
de7fef4b 3463 r = set_ensure_put(archs, NULL, UINT32_TO_PTR(a + 1));
57183d11
LP
3464 if (r < 0)
3465 return log_oom();
3466 }
57183d11
LP
3467}
3468
17df7223
LP
3469int config_parse_syscall_errno(
3470 const char *unit,
3471 const char *filename,
3472 unsigned line,
3473 const char *section,
3474 unsigned section_line,
3475 const char *lvalue,
3476 int ltype,
3477 const char *rvalue,
3478 void *data,
3479 void *userdata) {
3480
3481 ExecContext *c = data;
3482 int e;
3483
3484 assert(filename);
3485 assert(lvalue);
3486 assert(rvalue);
3487
005bfaf1 3488 if (isempty(rvalue) || streq(rvalue, "kill")) {
17df7223 3489 /* Empty assignment resets to KILL */
005bfaf1 3490 c->syscall_errno = SECCOMP_ERROR_NUMBER_KILL;
17df7223 3491 return 0;
8351ceae
LP
3492 }
3493
3df90f24 3494 e = parse_errno(rvalue);
b98680b2
YW
3495 if (e < 0) {
3496 log_syntax(unit, LOG_WARNING, filename, line, e, "Failed to parse error number, ignoring: %s", rvalue);
3497 return 0;
3498 }
3499 if (e == 0) {
3500 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid error number, ignoring: %s", rvalue);
17df7223
LP
3501 return 0;
3502 }
8351ceae 3503
17df7223 3504 c->syscall_errno = e;
8351ceae
LP
3505 return 0;
3506}
4298d0b5
LP
3507
3508int config_parse_address_families(
3509 const char *unit,
3510 const char *filename,
3511 unsigned line,
3512 const char *section,
3513 unsigned section_line,
3514 const char *lvalue,
3515 int ltype,
3516 const char *rvalue,
3517 void *data,
3518 void *userdata) {
3519
3520 ExecContext *c = data;
4298d0b5 3521 bool invert = false;
4298d0b5
LP
3522 int r;
3523
3524 assert(filename);
3525 assert(lvalue);
3526 assert(rvalue);
4298d0b5
LP
3527
3528 if (isempty(rvalue)) {
3529 /* Empty assignment resets the list */
525d3cc7 3530 c->address_families = set_free(c->address_families);
6b000af4 3531 c->address_families_allow_list = false;
4298d0b5
LP
3532 return 0;
3533 }
3534
4e6c50a5
YW
3535 if (streq(rvalue, "none")) {
3536 /* Forbid all address families. */
3537 c->address_families = set_free(c->address_families);
3538 c->address_families_allow_list = true;
3539 return 0;
3540 }
3541
4298d0b5
LP
3542 if (rvalue[0] == '~') {
3543 invert = true;
3544 rvalue++;
3545 }
3546
3547 if (!c->address_families) {
d5099efc 3548 c->address_families = set_new(NULL);
4298d0b5
LP
3549 if (!c->address_families)
3550 return log_oom();
3551
6b000af4 3552 c->address_families_allow_list = !invert;
4298d0b5
LP
3553 }
3554
323dda78 3555 for (const char *p = rvalue;;) {
035fe294 3556 _cleanup_free_ char *word = NULL;
4298d0b5
LP
3557 int af;
3558
4ec85141 3559 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
035fe294 3560 if (r == -ENOMEM)
4298d0b5 3561 return log_oom();
035fe294
ZJS
3562 if (r < 0) {
3563 log_syntax(unit, LOG_WARNING, filename, line, r,
3564 "Invalid syntax, ignoring: %s", rvalue);
3565 return 0;
3566 }
a687f500
ZJS
3567 if (r == 0)
3568 return 0;
4298d0b5 3569
035fe294 3570 af = af_from_name(word);
acf4d158 3571 if (af < 0) {
323dda78 3572 log_syntax(unit, LOG_WARNING, filename, line, af,
063c4b1a 3573 "Failed to parse address family, ignoring: %s", word);
4298d0b5
LP
3574 continue;
3575 }
3576
3577 /* If we previously wanted to forbid an address family and now
035fe294 3578 * we want to allow it, then just remove it from the list.
4298d0b5 3579 */
6b000af4 3580 if (!invert == c->address_families_allow_list) {
4298d0b5 3581 r = set_put(c->address_families, INT_TO_PTR(af));
4298d0b5
LP
3582 if (r < 0)
3583 return log_oom();
3584 } else
3585 set_remove(c->address_families, INT_TO_PTR(af));
3586 }
4298d0b5 3587}
add00535
LP
3588
3589int config_parse_restrict_namespaces(
3590 const char *unit,
3591 const char *filename,
3592 unsigned line,
3593 const char *section,
3594 unsigned section_line,
3595 const char *lvalue,
3596 int ltype,
3597 const char *rvalue,
3598 void *data,
3599 void *userdata) {
3600
3601 ExecContext *c = data;
aa9d574d 3602 unsigned long flags;
add00535
LP
3603 bool invert = false;
3604 int r;
3605
3606 if (isempty(rvalue)) {
3607 /* Reset to the default. */
aa9d574d
YW
3608 c->restrict_namespaces = NAMESPACE_FLAGS_INITIAL;
3609 return 0;
3610 }
3611
3612 /* Boolean parameter ignores the previous settings */
3613 r = parse_boolean(rvalue);
3614 if (r > 0) {
3615 c->restrict_namespaces = 0;
3616 return 0;
3617 } else if (r == 0) {
add00535
LP
3618 c->restrict_namespaces = NAMESPACE_FLAGS_ALL;
3619 return 0;
3620 }
3621
3622 if (rvalue[0] == '~') {
3623 invert = true;
3624 rvalue++;
3625 }
3626
aa9d574d
YW
3627 /* Not a boolean argument, in this case it's a list of namespace types. */
3628 r = namespace_flags_from_string(rvalue, &flags);
3629 if (r < 0) {
323dda78 3630 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse namespace type string, ignoring: %s", rvalue);
aa9d574d 3631 return 0;
add00535
LP
3632 }
3633
aa9d574d
YW
3634 if (c->restrict_namespaces == NAMESPACE_FLAGS_INITIAL)
3635 /* Initial assignment. Just set the value. */
3636 c->restrict_namespaces = invert ? (~flags) & NAMESPACE_FLAGS_ALL : flags;
3637 else
3638 /* Merge the value with the previous one. */
3639 SET_FLAG(c->restrict_namespaces, flags, !invert);
add00535
LP
3640
3641 return 0;
3642}
c0467cf3 3643#endif
8351ceae 3644
e59ccd03
ILG
3645int config_parse_restrict_filesystems(
3646 const char *unit,
3647 const char *filename,
3648 unsigned line,
3649 const char *section,
3650 unsigned section_line,
3651 const char *lvalue,
3652 int ltype,
3653 const char *rvalue,
3654 void *data,
3655 void *userdata) {
99534007 3656 ExecContext *c = ASSERT_PTR(data);
e59ccd03
ILG
3657 bool invert = false;
3658 int r;
3659
3660 assert(filename);
3661 assert(lvalue);
3662 assert(rvalue);
e59ccd03
ILG
3663
3664 if (isempty(rvalue)) {
3665 /* Empty assignment resets the list */
1a572fd0 3666 c->restrict_filesystems = set_free_free(c->restrict_filesystems);
e59ccd03
ILG
3667 c->restrict_filesystems_allow_list = false;
3668 return 0;
3669 }
3670
3671 if (rvalue[0] == '~') {
3672 invert = true;
3673 rvalue++;
3674 }
3675
3676 if (!c->restrict_filesystems) {
3677 if (invert)
3678 /* Allow everything but the ones listed */
3679 c->restrict_filesystems_allow_list = false;
3680 else
3681 /* Allow nothing but the ones listed */
3682 c->restrict_filesystems_allow_list = true;
3683 }
3684
3685 for (const char *p = rvalue;;) {
3686 _cleanup_free_ char *word = NULL;
3687
3688 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
3689 if (r == 0)
3690 break;
3691 if (r == -ENOMEM)
3692 return log_oom();
3693 if (r < 0) {
3694 log_syntax(unit, LOG_WARNING, filename, line, r,
3695 "Trailing garbage in %s, ignoring: %s", lvalue, rvalue);
3696 break;
3697 }
3698
3699 r = lsm_bpf_parse_filesystem(
3700 word,
3701 &c->restrict_filesystems,
3702 FILESYSTEM_PARSE_LOG|
3703 (invert ? FILESYSTEM_PARSE_INVERT : 0)|
3704 (c->restrict_filesystems_allow_list ? FILESYSTEM_PARSE_ALLOW_LIST : 0),
3705 unit, filename, line);
3706
3707 if (r < 0)
3708 return r;
3709 }
3710
3711 return 0;
3712}
3713
a016b922
LP
3714int config_parse_unit_slice(
3715 const char *unit,
3716 const char *filename,
3717 unsigned line,
3718 const char *section,
71a61510 3719 unsigned section_line,
a016b922
LP
3720 const char *lvalue,
3721 int ltype,
3722 const char *rvalue,
3723 void *data,
3724 void *userdata) {
3725
063c4b1a 3726 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
a016b922 3727 _cleanup_free_ char *k = NULL;
abc9fa1c 3728 Unit *u = userdata, *slice;
a016b922
LP
3729 int r;
3730
3731 assert(filename);
3732 assert(lvalue);
3733 assert(rvalue);
3734 assert(u);
3735
19f6d710 3736 r = unit_name_printf(u, rvalue, &k);
d79200e2 3737 if (r < 0) {
323dda78 3738 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue);
d79200e2 3739 return 0;
19f6d710 3740 }
a016b922 3741
063c4b1a 3742 r = manager_load_unit(u->manager, k, NULL, &error, &slice);
a016b922 3743 if (r < 0) {
323dda78 3744 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to load slice unit %s, ignoring: %s", k, bus_error_message(&error, r));
a016b922
LP
3745 return 0;
3746 }
3747
899acf5c 3748 r = unit_set_slice(u, slice);
d79200e2 3749 if (r < 0) {
323dda78 3750 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to assign slice %s to unit %s, ignoring: %m", slice->id, u->id);
a016b922
LP
3751 return 0;
3752 }
3753
a016b922
LP
3754 return 0;
3755}
3756
b2f8b02e
LP
3757int config_parse_cpu_quota(
3758 const char *unit,
3759 const char *filename,
3760 unsigned line,
3761 const char *section,
3762 unsigned section_line,
3763 const char *lvalue,
3764 int ltype,
3765 const char *rvalue,
3766 void *data,
3767 void *userdata) {
3768
3769 CGroupContext *c = data;
9184ca48 3770 int r;
b2f8b02e
LP
3771
3772 assert(filename);
3773 assert(lvalue);
3774 assert(rvalue);
3775
3776 if (isempty(rvalue)) {
3a43da28 3777 c->cpu_quota_per_sec_usec = USEC_INFINITY;
b2f8b02e
LP
3778 return 0;
3779 }
3780
fe845b5e 3781 r = parse_permyriad_unbounded(rvalue);
9184ca48 3782 if (r <= 0) {
323dda78 3783 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid CPU quota '%s', ignoring.", rvalue);
9a054909 3784 return 0;
b2f8b02e
LP
3785 }
3786
fe845b5e 3787 c->cpu_quota_per_sec_usec = ((usec_t) r * USEC_PER_SEC) / 10000U;
b2f8b02e
LP
3788 return 0;
3789}
3790
31d3a520 3791int config_parse_allowed_cpuset(
047f5d63
PH
3792 const char *unit,
3793 const char *filename,
3794 unsigned line,
3795 const char *section,
3796 unsigned section_line,
3797 const char *lvalue,
3798 int ltype,
3799 const char *rvalue,
3800 void *data,
3801 void *userdata) {
3802
31d3a520 3803 CPUSet *c = data;
67001c25
AL
3804 const Unit *u = userdata;
3805 _cleanup_free_ char *k = NULL;
3806 int r;
3807
3808 assert(filename);
3809 assert(lvalue);
3810 assert(rvalue);
3811
3812 r = unit_full_printf(u, rvalue, &k);
3813 if (r < 0) {
3814 log_syntax(unit, LOG_WARNING, filename, line, r,
3815 "Failed to resolve unit specifiers in '%s', ignoring: %m",
3816 rvalue);
3817 return 0;
3818 }
047f5d63 3819
67001c25 3820 (void) parse_cpu_set_extend(k, c, true, unit, filename, line, lvalue);
047f5d63
PH
3821 return 0;
3822}
3823
4ad49000
LP
3824int config_parse_memory_limit(
3825 const char *unit,
3826 const char *filename,
3827 unsigned line,
3828 const char *section,
71a61510 3829 unsigned section_line,
4ad49000
LP
3830 const char *lvalue,
3831 int ltype,
3832 const char *rvalue,
3833 void *data,
3834 void *userdata) {
3835
3836 CGroupContext *c = data;
da4d897e 3837 uint64_t bytes = CGROUP_LIMIT_MAX;
4ad49000
LP
3838 int r;
3839
67e2baff
MK
3840 if (isempty(rvalue) && STR_IN_SET(lvalue, "DefaultMemoryLow",
3841 "DefaultMemoryMin",
3842 "MemoryLow",
53fda560 3843 "StartupMemoryLow",
67e2baff 3844 "MemoryMin"))
db2b8d2e 3845 bytes = CGROUP_LIMIT_MIN;
67e2baff 3846 else if (!isempty(rvalue) && !streq(rvalue, "infinity")) {
875ae566 3847
fe845b5e 3848 r = parse_permyriad(rvalue);
875ae566
LP
3849 if (r < 0) {
3850 r = parse_size(rvalue, 1024, &bytes);
3851 if (r < 0) {
323dda78 3852 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid memory limit '%s', ignoring: %m", rvalue);
875ae566
LP
3853 return 0;
3854 }
3855 } else
fe845b5e 3856 bytes = physical_memory_scale(r, 10000U);
875ae566 3857
906bdbf5 3858 if (bytes >= UINT64_MAX ||
53fda560
LB
3859 (bytes <= 0 && !STR_IN_SET(lvalue,
3860 "MemorySwapMax",
3861 "StartupMemorySwapMax",
3862 "MemoryZSwapMax",
3863 "StartupMemoryZSwapMax",
3864 "MemoryLow",
3865 "StartupMemoryLow",
3866 "MemoryMin",
3867 "DefaultMemoryLow",
3868 "DefaultstartupMemoryLow",
3869 "DefaultMemoryMin"))) {
323dda78 3870 log_syntax(unit, LOG_WARNING, filename, line, 0, "Memory limit '%s' out of range, ignoring.", rvalue);
da4d897e
TH
3871 return 0;
3872 }
4ad49000
LP
3873 }
3874
c52db42b 3875 if (streq(lvalue, "DefaultMemoryLow")) {
db2b8d2e 3876 c->default_memory_low = bytes;
c52db42b 3877 c->default_memory_low_set = true;
53fda560
LB
3878 } else if (streq(lvalue, "DefaultStartupMemoryLow")) {
3879 c->default_startup_memory_low = bytes;
3880 c->default_startup_memory_low_set = true;
7ad5439e 3881 } else if (streq(lvalue, "DefaultMemoryMin")) {
db2b8d2e 3882 c->default_memory_min = bytes;
7ad5439e 3883 c->default_memory_min_set = true;
7ad5439e 3884 } else if (streq(lvalue, "MemoryMin")) {
48422635 3885 c->memory_min = bytes;
311a0e2e 3886 c->memory_min_set = true;
7ad5439e 3887 } else if (streq(lvalue, "MemoryLow")) {
da4d897e 3888 c->memory_low = bytes;
311a0e2e 3889 c->memory_low_set = true;
53fda560
LB
3890 } else if (streq(lvalue, "StartupMemoryLow")) {
3891 c->startup_memory_low = bytes;
3892 c->startup_memory_low_set = true;
c52db42b 3893 } else if (streq(lvalue, "MemoryHigh"))
da4d897e 3894 c->memory_high = bytes;
53fda560
LB
3895 else if (streq(lvalue, "StartupMemoryHigh")) {
3896 c->startup_memory_high = bytes;
3897 c->startup_memory_high_set = true;
3898 } else if (streq(lvalue, "MemoryMax"))
da4d897e 3899 c->memory_max = bytes;
53fda560
LB
3900 else if (streq(lvalue, "StartupMemoryMax")) {
3901 c->startup_memory_max = bytes;
3902 c->startup_memory_max_set = true;
3903 } else if (streq(lvalue, "MemorySwapMax"))
96e131ea 3904 c->memory_swap_max = bytes;
53fda560
LB
3905 else if (streq(lvalue, "StartupMemorySwapMax")) {
3906 c->startup_memory_swap_max = bytes;
3907 c->startup_memory_swap_max_set = true;
3908 } else if (streq(lvalue, "MemoryZSwapMax"))
d7fe0a67 3909 c->memory_zswap_max = bytes;
53fda560
LB
3910 else if (streq(lvalue, "StartupMemoryZSwapMax")) {
3911 c->startup_memory_zswap_max = bytes;
3912 c->startup_memory_zswap_max_set = true;
3913 } else if (streq(lvalue, "MemoryLimit")) {
c1e701e2
LP
3914 log_syntax(unit, LOG_WARNING, filename, line, 0,
3915 "Unit uses MemoryLimit=; please use MemoryMax= instead. Support for MemoryLimit= will be removed soon.");
da4d897e 3916 c->memory_limit = bytes;
c1e701e2 3917 } else
96e131ea 3918 return -EINVAL;
4ad49000 3919
4ad49000
LP
3920 return 0;
3921}
3922
03a7b521
LP
3923int config_parse_tasks_max(
3924 const char *unit,
3925 const char *filename,
3926 unsigned line,
3927 const char *section,
3928 unsigned section_line,
3929 const char *lvalue,
3930 int ltype,
3931 const char *rvalue,
3932 void *data,
3933 void *userdata) {
3934
47538b76 3935 const Unit *u = userdata;
94f0b13b 3936 CGroupTasksMax *tasks_max = data;
3a0f06c4 3937 uint64_t v;
03a7b521
LP
3938 int r;
3939
f5058264 3940 if (isempty(rvalue)) {
94f0b13b 3941 *tasks_max = u ? u->manager->defaults.tasks_max : CGROUP_TASKS_MAX_UNSET;
f5058264
TH
3942 return 0;
3943 }
3944
3945 if (streq(rvalue, "infinity")) {
94f0b13b 3946 *tasks_max = CGROUP_TASKS_MAX_UNSET;
03a7b521
LP
3947 return 0;
3948 }
3949
fe845b5e 3950 r = parse_permyriad(rvalue);
3a0f06c4 3951 if (r >= 0)
94f0b13b 3952 *tasks_max = (CGroupTasksMax) { r, 10000U }; /* r‱ */
3a0f06c4 3953 else {
f5058264 3954 r = safe_atou64(rvalue, &v);
83f8e808 3955 if (r < 0) {
323dda78 3956 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid maximum tasks value '%s', ignoring: %m", rvalue);
83f8e808
LP
3957 return 0;
3958 }
83f8e808 3959
3a0f06c4 3960 if (v <= 0 || v >= UINT64_MAX) {
323dda78 3961 log_syntax(unit, LOG_WARNING, filename, line, 0, "Maximum tasks value '%s' out of range, ignoring.", rvalue);
3a0f06c4
ZJS
3962 return 0;
3963 }
3964
94f0b13b 3965 *tasks_max = (CGroupTasksMax) { v };
03a7b521
LP
3966 }
3967
3968 return 0;
3969}
3970
02638280
LP
3971int config_parse_delegate(
3972 const char *unit,
3973 const char *filename,
3974 unsigned line,
3975 const char *section,
3976 unsigned section_line,
3977 const char *lvalue,
3978 int ltype,
3979 const char *rvalue,
3980 void *data,
3981 void *userdata) {
3982
3983 CGroupContext *c = data;
ecae73d7 3984 UnitType t;
02638280
LP
3985 int r;
3986
ecae73d7
ZJS
3987 t = unit_name_to_type(unit);
3988 assert(t != _UNIT_TYPE_INVALID);
3989
3990 if (!unit_vtable[t]->can_delegate) {
323dda78 3991 log_syntax(unit, LOG_WARNING, filename, line, 0, "Delegate= setting not supported for this unit type, ignoring.");
ecae73d7
ZJS
3992 return 0;
3993 }
3994
449172f9
ZJS
3995 /* We either accept a boolean value, which may be used to turn on delegation for all controllers, or
3996 * turn it off for all. Or it takes a list of controller names, in which case we add the specified
3997 * controllers to the mask to delegate. Delegate= enables delegation without any controllers. */
02638280 3998
1bdfc7b9 3999 if (isempty(rvalue)) {
449172f9 4000 /* An empty string resets controllers and sets Delegate=yes. */
d48013f8 4001 c->delegate = true;
1bdfc7b9
YW
4002 c->delegate_controllers = 0;
4003 return 0;
4004 }
4005
02638280
LP
4006 r = parse_boolean(rvalue);
4007 if (r < 0) {
02638280
LP
4008 CGroupMask mask = 0;
4009
323dda78 4010 for (const char *p = rvalue;;) {
02638280
LP
4011 _cleanup_free_ char *word = NULL;
4012 CGroupController cc;
4013
4ec85141 4014 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
02638280
LP
4015 if (r == -ENOMEM)
4016 return log_oom();
4017 if (r < 0) {
323dda78 4018 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
ff1b8455 4019 return 0;
02638280 4020 }
a687f500
ZJS
4021 if (r == 0)
4022 break;
02638280
LP
4023
4024 cc = cgroup_controller_from_string(word);
4025 if (cc < 0) {
323dda78 4026 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid controller name '%s', ignoring", word);
02638280
LP
4027 continue;
4028 }
4029
4030 mask |= CGROUP_CONTROLLER_TO_MASK(cc);
4031 }
4032
4033 c->delegate = true;
4034 c->delegate_controllers |= mask;
4035
4036 } else if (r > 0) {
4037 c->delegate = true;
4038 c->delegate_controllers = _CGROUP_MASK_ALL;
4039 } else {
4040 c->delegate = false;
4041 c->delegate_controllers = 0;
4042 }
4043
4044 return 0;
4045}
4046
a8b993dc
LP
4047int config_parse_delegate_subgroup(
4048 const char *unit,
4049 const char *filename,
4050 unsigned line,
4051 const char *section,
4052 unsigned section_line,
4053 const char *lvalue,
4054 int ltype,
4055 const char *rvalue,
4056 void *data,
4057 void *userdata) {
4058
4059 CGroupContext *c = ASSERT_PTR(data);
4060 UnitType t;
4061
4062 t = unit_name_to_type(unit);
4063 assert(t >= 0);
4064
4065 if (!unit_vtable[t]->can_delegate) {
4066 log_syntax(unit, LOG_WARNING, filename, line, 0, "DelegateSubgroup= setting not supported for this unit type, ignoring.");
4067 return 0;
4068 }
4069
4070 if (isempty(rvalue)) {
4071 c->delegate_subgroup = mfree(c->delegate_subgroup);
4072 return 0;
4073 }
4074
4075 if (cg_needs_escape(rvalue)) { /* Insist that specified names don't need escaping */
4076 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid control group name, ignoring: %s", rvalue);
4077 return 0;
4078 }
4079
4080 return free_and_strdup_warn(&c->delegate_subgroup, rvalue);
4081}
4082
4d824a4e
AZ
4083int config_parse_managed_oom_mode(
4084 const char *unit,
4085 const char *filename,
4086 unsigned line,
4087 const char *section,
4088 unsigned section_line,
4089 const char *lvalue,
4090 int ltype,
4091 const char *rvalue,
4092 void *data,
4093 void *userdata) {
ed5033fd 4094
4d824a4e
AZ
4095 ManagedOOMMode *mode = data, m;
4096 UnitType t;
4097
4098 t = unit_name_to_type(unit);
4099 assert(t != _UNIT_TYPE_INVALID);
4100
4101 if (!unit_vtable[t]->can_set_managed_oom)
4102 return log_syntax(unit, LOG_WARNING, filename, line, 0, "%s= is not supported for this unit type, ignoring.", lvalue);
4103
4104 if (isempty(rvalue)) {
4105 *mode = MANAGED_OOM_AUTO;
f561e8c6 4106 return 0;
4d824a4e
AZ
4107 }
4108
4109 m = managed_oom_mode_from_string(rvalue);
4110 if (m < 0) {
b98680b2 4111 log_syntax(unit, LOG_WARNING, filename, line, m, "Invalid syntax, ignoring: %s", rvalue);
4d824a4e
AZ
4112 return 0;
4113 }
ed5033fd 4114
4d824a4e
AZ
4115 *mode = m;
4116 return 0;
4117}
4118
4119int config_parse_managed_oom_mem_pressure_limit(
4120 const char *unit,
4121 const char *filename,
4122 unsigned line,
4123 const char *section,
4124 unsigned section_line,
4125 const char *lvalue,
4126 int ltype,
4127 const char *rvalue,
4128 void *data,
4129 void *userdata) {
ed5033fd 4130
0a9f9344 4131 uint32_t *limit = data;
4d824a4e
AZ
4132 UnitType t;
4133 int r;
4134
4135 t = unit_name_to_type(unit);
4136 assert(t != _UNIT_TYPE_INVALID);
4137
4138 if (!unit_vtable[t]->can_set_managed_oom)
4139 return log_syntax(unit, LOG_WARNING, filename, line, 0, "%s= is not supported for this unit type, ignoring.", lvalue);
4140
4141 if (isempty(rvalue)) {
f561e8c6
AZ
4142 *limit = 0;
4143 return 0;
4d824a4e
AZ
4144 }
4145
0a9f9344 4146 r = parse_permyriad(rvalue);
4d824a4e 4147 if (r < 0) {
0a9f9344 4148 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse memory pressure limit value, ignoring: %s", rvalue);
4d824a4e
AZ
4149 return 0;
4150 }
4151
d9d3f05d
LP
4152 /* Normalize to 2^32-1 == 100% */
4153 *limit = UINT32_SCALE_FROM_PERMYRIAD(r);
4d824a4e
AZ
4154 return 0;
4155}
4156
4ad49000
LP
4157int config_parse_device_allow(
4158 const char *unit,
4159 const char *filename,
4160 unsigned line,
4161 const char *section,
71a61510 4162 unsigned section_line,
4ad49000
LP
4163 const char *lvalue,
4164 int ltype,
4165 const char *rvalue,
4166 void *data,
4167 void *userdata) {
4168
c9f620bf 4169 _cleanup_free_ char *path = NULL, *resolved = NULL;
a1044811 4170 CGroupDevicePermissions permissions;
4ad49000 4171 CGroupContext *c = data;
c9f620bf 4172 const char *p = rvalue;
1116e14c 4173 int r;
4ad49000
LP
4174
4175 if (isempty(rvalue)) {
4176 while (c->device_allow)
4177 cgroup_context_free_device_allow(c, c->device_allow);
4178
4179 return 0;
4180 }
4181
4ec85141 4182 r = extract_first_word(&p, &path, NULL, EXTRACT_UNQUOTE);
c9f620bf
YW
4183 if (r == -ENOMEM)
4184 return log_oom();
a687f500 4185 if (r <= 0) {
1116e14c 4186 log_syntax(unit, LOG_WARNING, filename, line, r,
c9f620bf 4187 "Failed to extract device path and rights from '%s', ignoring.", rvalue);
20d52ab6 4188 return 0;
1116e14c
NBS
4189 }
4190
06536492 4191 r = unit_path_printf(userdata, path, &resolved);
c9f620bf
YW
4192 if (r < 0) {
4193 log_syntax(unit, LOG_WARNING, filename, line, r,
4194 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
4ad49000
LP
4195 return 0;
4196 }
4197
49fe5c09 4198 if (!STARTSWITH_SET(resolved, "block-", "char-")) {
2f4d31c1 4199
57e84e75
LP
4200 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
4201 if (r < 0)
4202 return 0;
4203
4204 if (!valid_device_node_path(resolved)) {
323dda78 4205 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid device node path '%s', ignoring.", resolved);
57e84e75
LP
4206 return 0;
4207 }
c9f620bf 4208 }
4ad49000 4209
a1044811
LP
4210 permissions = isempty(p) ? 0 : cgroup_device_permissions_from_string(p);
4211 if (permissions < 0) {
4212 log_syntax(unit, LOG_WARNING, filename, line, permissions, "Invalid device rights '%s', ignoring.", p);
4ad49000
LP
4213 return 0;
4214 }
4215
a1044811 4216 return cgroup_context_add_device_allow(c, resolved, permissions);
4ad49000
LP
4217}
4218
13c31542
TH
4219int config_parse_io_device_weight(
4220 const char *unit,
4221 const char *filename,
4222 unsigned line,
4223 const char *section,
4224 unsigned section_line,
4225 const char *lvalue,
4226 int ltype,
4227 const char *rvalue,
4228 void *data,
4229 void *userdata) {
4230
c9f620bf 4231 _cleanup_free_ char *path = NULL, *resolved = NULL;
13c31542
TH
4232 CGroupIODeviceWeight *w;
4233 CGroupContext *c = data;
99534007 4234 const char *p = ASSERT_PTR(rvalue);
13c31542 4235 uint64_t u;
13c31542
TH
4236 int r;
4237
4238 assert(filename);
4239 assert(lvalue);
13c31542
TH
4240
4241 if (isempty(rvalue)) {
4242 while (c->io_device_weights)
4243 cgroup_context_free_io_device_weight(c, c->io_device_weights);
4244
4245 return 0;
4246 }
4247
4ec85141 4248 r = extract_first_word(&p, &path, NULL, EXTRACT_UNQUOTE);
c9f620bf
YW
4249 if (r == -ENOMEM)
4250 return log_oom();
6a35d52d 4251 if (r < 0) {
c9f620bf 4252 log_syntax(unit, LOG_WARNING, filename, line, r,
c9f620bf 4253 "Failed to extract device path and weight from '%s', ignoring.", rvalue);
13c31542
TH
4254 return 0;
4255 }
6a35d52d
YW
4256 if (r == 0 || isempty(p)) {
4257 log_syntax(unit, LOG_WARNING, filename, line, 0,
4258 "Invalid device path or weight specified in '%s', ignoring.", rvalue);
4259 return 0;
4260 }
13c31542 4261
06536492 4262 r = unit_path_printf(userdata, path, &resolved);
c9f620bf
YW
4263 if (r < 0) {
4264 log_syntax(unit, LOG_WARNING, filename, line, r,
4265 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
4266 return 0;
4267 }
13c31542 4268
2f4d31c1
YW
4269 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
4270 if (r < 0)
4271 return 0;
4272
c9f620bf 4273 r = cg_weight_parse(p, &u);
13c31542 4274 if (r < 0) {
323dda78 4275 log_syntax(unit, LOG_WARNING, filename, line, r, "IO weight '%s' invalid, ignoring: %m", p);
13c31542
TH
4276 return 0;
4277 }
4278
4279 assert(u != CGROUP_WEIGHT_INVALID);
4280
4281 w = new0(CGroupIODeviceWeight, 1);
4282 if (!w)
4283 return log_oom();
4284
c9f620bf 4285 w->path = TAKE_PTR(resolved);
13c31542
TH
4286 w->weight = u;
4287
4288 LIST_PREPEND(device_weights, c->io_device_weights, w);
4289 return 0;
4290}
4291
6ae4283c
TH
4292int config_parse_io_device_latency(
4293 const char *unit,
4294 const char *filename,
4295 unsigned line,
4296 const char *section,
4297 unsigned section_line,
4298 const char *lvalue,
4299 int ltype,
4300 const char *rvalue,
4301 void *data,
4302 void *userdata) {
4303
4304 _cleanup_free_ char *path = NULL, *resolved = NULL;
4305 CGroupIODeviceLatency *l;
4306 CGroupContext *c = data;
99534007 4307 const char *p = ASSERT_PTR(rvalue);
6ae4283c
TH
4308 usec_t usec;
4309 int r;
4310
4311 assert(filename);
4312 assert(lvalue);
6ae4283c
TH
4313
4314 if (isempty(rvalue)) {
4315 while (c->io_device_latencies)
4316 cgroup_context_free_io_device_latency(c, c->io_device_latencies);
4317
4318 return 0;
4319 }
4320
4ec85141 4321 r = extract_first_word(&p, &path, NULL, EXTRACT_UNQUOTE);
6ae4283c
TH
4322 if (r == -ENOMEM)
4323 return log_oom();
6a35d52d 4324 if (r < 0) {
6ae4283c 4325 log_syntax(unit, LOG_WARNING, filename, line, r,
6ae4283c
TH
4326 "Failed to extract device path and latency from '%s', ignoring.", rvalue);
4327 return 0;
4328 }
6a35d52d
YW
4329 if (r == 0 || isempty(p)) {
4330 log_syntax(unit, LOG_WARNING, filename, line, 0,
4331 "Invalid device path or latency specified in '%s', ignoring.", rvalue);
4332 return 0;
4333 }
6ae4283c 4334
06536492 4335 r = unit_path_printf(userdata, path, &resolved);
6ae4283c
TH
4336 if (r < 0) {
4337 log_syntax(unit, LOG_WARNING, filename, line, r,
4338 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
4339 return 0;
4340 }
4341
4342 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
4343 if (r < 0)
4344 return 0;
4345
323dda78
YW
4346 r = parse_sec(p, &usec);
4347 if (r < 0) {
4348 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse timer value, ignoring: %s", p);
6ae4283c
TH
4349 return 0;
4350 }
4351
4352 l = new0(CGroupIODeviceLatency, 1);
4353 if (!l)
4354 return log_oom();
4355
4356 l->path = TAKE_PTR(resolved);
4357 l->target_usec = usec;
4358
4359 LIST_PREPEND(device_latencies, c->io_device_latencies, l);
4360 return 0;
4361}
4362
13c31542
TH
4363int config_parse_io_limit(
4364 const char *unit,
4365 const char *filename,
4366 unsigned line,
4367 const char *section,
4368 unsigned section_line,
4369 const char *lvalue,
4370 int ltype,
4371 const char *rvalue,
4372 void *data,
4373 void *userdata) {
4374
c9f620bf 4375 _cleanup_free_ char *path = NULL, *resolved = NULL;
03677889 4376 CGroupIODeviceLimit *l = NULL;
13c31542 4377 CGroupContext *c = data;
9be57249 4378 CGroupIOLimitType type;
99534007 4379 const char *p = ASSERT_PTR(rvalue);
13c31542 4380 uint64_t num;
13c31542
TH
4381 int r;
4382
4383 assert(filename);
4384 assert(lvalue);
13c31542 4385
9be57249
TH
4386 type = cgroup_io_limit_type_from_string(lvalue);
4387 assert(type >= 0);
13c31542
TH
4388
4389 if (isempty(rvalue)) {
03677889
YW
4390 LIST_FOREACH(device_limits, t, c->io_device_limits)
4391 t->limits[type] = cgroup_io_limit_defaults[type];
13c31542
TH
4392 return 0;
4393 }
4394
4ec85141 4395 r = extract_first_word(&p, &path, NULL, EXTRACT_UNQUOTE);
c9f620bf
YW
4396 if (r == -ENOMEM)
4397 return log_oom();
6a35d52d 4398 if (r < 0) {
c9f620bf 4399 log_syntax(unit, LOG_WARNING, filename, line, r,
c9f620bf 4400 "Failed to extract device node and bandwidth from '%s', ignoring.", rvalue);
13c31542
TH
4401 return 0;
4402 }
6a35d52d
YW
4403 if (r == 0 || isempty(p)) {
4404 log_syntax(unit, LOG_WARNING, filename, line, 0,
4405 "Invalid device node or bandwidth specified in '%s', ignoring.", rvalue);
4406 return 0;
4407 }
13c31542 4408
06536492 4409 r = unit_path_printf(userdata, path, &resolved);
c9f620bf
YW
4410 if (r < 0) {
4411 log_syntax(unit, LOG_WARNING, filename, line, r,
4412 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
4413 return 0;
4414 }
13c31542 4415
2f4d31c1
YW
4416 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
4417 if (r < 0)
4418 return 0;
4419
9d5e9b4a 4420 if (streq("infinity", p))
13c31542 4421 num = CGROUP_LIMIT_MAX;
9d5e9b4a 4422 else {
c9f620bf 4423 r = parse_size(p, 1000, &num);
13c31542 4424 if (r < 0 || num <= 0) {
323dda78 4425 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid IO limit '%s', ignoring.", p);
13c31542
TH
4426 return 0;
4427 }
4428 }
4429
03677889 4430 LIST_FOREACH(device_limits, t, c->io_device_limits)
c9f620bf 4431 if (path_equal(resolved, t->path)) {
13c31542
TH
4432 l = t;
4433 break;
4434 }
13c31542
TH
4435
4436 if (!l) {
4437 l = new0(CGroupIODeviceLimit, 1);
4438 if (!l)
4439 return log_oom();
4440
c9f620bf 4441 l->path = TAKE_PTR(resolved);
24aaf6c6
ZJS
4442 for (CGroupIOLimitType i = 0; i < _CGROUP_IO_LIMIT_TYPE_MAX; i++)
4443 l->limits[i] = cgroup_io_limit_defaults[i];
13c31542
TH
4444
4445 LIST_PREPEND(device_limits, c->io_device_limits, l);
4446 }
4447
9be57249 4448 l->limits[type] = num;
13c31542
TH
4449
4450 return 0;
4451}
4452
8e7076ca
LP
4453int config_parse_blockio_device_weight(
4454 const char *unit,
4455 const char *filename,
4456 unsigned line,
4457 const char *section,
71a61510 4458 unsigned section_line,
8e7076ca
LP
4459 const char *lvalue,
4460 int ltype,
4461 const char *rvalue,
4462 void *data,
4463 void *userdata) {
4464
c9f620bf 4465 _cleanup_free_ char *path = NULL, *resolved = NULL;
8e7076ca 4466 CGroupBlockIODeviceWeight *w;
4ad49000 4467 CGroupContext *c = data;
99534007 4468 const char *p = ASSERT_PTR(rvalue);
d53d9474 4469 uint64_t u;
4ad49000
LP
4470 int r;
4471
4472 assert(filename);
4473 assert(lvalue);
4ad49000 4474
c1e701e2
LP
4475 log_syntax(unit, LOG_WARNING, filename, line, 0,
4476 "Unit uses %s=; please use IO*= settings instead. Support for %s= will be removed soon.",
4477 lvalue, lvalue);
4478
4ad49000 4479 if (isempty(rvalue)) {
4ad49000
LP
4480 while (c->blockio_device_weights)
4481 cgroup_context_free_blockio_device_weight(c, c->blockio_device_weights);
4482
4483 return 0;
4484 }
4485
4ec85141 4486 r = extract_first_word(&p, &path, NULL, EXTRACT_UNQUOTE);
c9f620bf
YW
4487 if (r == -ENOMEM)
4488 return log_oom();
6a35d52d 4489 if (r < 0) {
c9f620bf 4490 log_syntax(unit, LOG_WARNING, filename, line, r,
c9f620bf 4491 "Failed to extract device node and weight from '%s', ignoring.", rvalue);
8e7076ca
LP
4492 return 0;
4493 }
6a35d52d
YW
4494 if (r == 0 || isempty(p)) {
4495 log_syntax(unit, LOG_WARNING, filename, line, 0,
4496 "Invalid device node or weight specified in '%s', ignoring.", rvalue);
4497 return 0;
4498 }
4ad49000 4499
06536492 4500 r = unit_path_printf(userdata, path, &resolved);
c9f620bf
YW
4501 if (r < 0) {
4502 log_syntax(unit, LOG_WARNING, filename, line, r,
4503 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
4504 return 0;
4505 }
4ad49000 4506
2f4d31c1
YW
4507 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
4508 if (r < 0)
4509 return 0;
4510
c9f620bf 4511 r = cg_blkio_weight_parse(p, &u);
d53d9474 4512 if (r < 0) {
323dda78 4513 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid block IO weight '%s', ignoring: %m", p);
4ad49000
LP
4514 return 0;
4515 }
4516
d53d9474
LP
4517 assert(u != CGROUP_BLKIO_WEIGHT_INVALID);
4518
8e7076ca
LP
4519 w = new0(CGroupBlockIODeviceWeight, 1);
4520 if (!w)
4521 return log_oom();
4ad49000 4522
c9f620bf 4523 w->path = TAKE_PTR(resolved);
d53d9474 4524 w->weight = u;
4ad49000 4525
71fda00f 4526 LIST_PREPEND(device_weights, c->blockio_device_weights, w);
4ad49000
LP
4527 return 0;
4528}
4529
4530int config_parse_blockio_bandwidth(
4531 const char *unit,
4532 const char *filename,
4533 unsigned line,
4534 const char *section,
71a61510 4535 unsigned section_line,
4ad49000
LP
4536 const char *lvalue,
4537 int ltype,
4538 const char *rvalue,
4539 void *data,
4540 void *userdata) {
4541
c9f620bf 4542 _cleanup_free_ char *path = NULL, *resolved = NULL;
03677889 4543 CGroupBlockIODeviceBandwidth *b = NULL;
4ad49000 4544 CGroupContext *c = data;
99534007 4545 const char *p = ASSERT_PTR(rvalue);
59f448cf 4546 uint64_t bytes;
47c0980d 4547 bool read;
4ad49000
LP
4548 int r;
4549
4550 assert(filename);
4551 assert(lvalue);
4ad49000 4552
c1e701e2
LP
4553 log_syntax(unit, LOG_WARNING, filename, line, 0,
4554 "Unit uses %s=; please use IO*= settings instead. Support for %s= will be removed soon.",
4555 lvalue, lvalue);
4556
47c0980d
G
4557 read = streq("BlockIOReadBandwidth", lvalue);
4558
4ad49000 4559 if (isempty(rvalue)) {
03677889
YW
4560 LIST_FOREACH(device_bandwidths, t, c->blockio_device_bandwidths) {
4561 t->rbps = CGROUP_LIMIT_MAX;
4562 t->wbps = CGROUP_LIMIT_MAX;
979d0311 4563 }
4ad49000
LP
4564 return 0;
4565 }
4566
4ec85141 4567 r = extract_first_word(&p, &path, NULL, EXTRACT_UNQUOTE);
c9f620bf
YW
4568 if (r == -ENOMEM)
4569 return log_oom();
6a35d52d 4570 if (r < 0) {
c9f620bf 4571 log_syntax(unit, LOG_WARNING, filename, line, r,
c9f620bf 4572 "Failed to extract device node and bandwidth from '%s', ignoring.", rvalue);
4ad49000
LP
4573 return 0;
4574 }
6a35d52d
YW
4575 if (r == 0 || isempty(p)) {
4576 log_syntax(unit, LOG_WARNING, filename, line, 0,
4577 "Invalid device node or bandwidth specified in '%s', ignoring.", rvalue);
4578 return 0;
4579 }
4ad49000 4580
06536492 4581 r = unit_path_printf(userdata, path, &resolved);
c9f620bf
YW
4582 if (r < 0) {
4583 log_syntax(unit, LOG_WARNING, filename, line, r,
4584 "Failed to resolve unit specifiers in '%s', ignoring: %m", path);
4585 return 0;
4586 }
4ad49000 4587
2f4d31c1
YW
4588 r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
4589 if (r < 0)
4590 return 0;
4591
c9f620bf 4592 r = parse_size(p, 1000, &bytes);
4ad49000 4593 if (r < 0 || bytes <= 0) {
323dda78 4594 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid Block IO Bandwidth '%s', ignoring.", p);
4ad49000
LP
4595 return 0;
4596 }
4597
03677889 4598 LIST_FOREACH(device_bandwidths, t, c->blockio_device_bandwidths)
c9f620bf 4599 if (path_equal(resolved, t->path)) {
979d0311
TH
4600 b = t;
4601 break;
4602 }
4ad49000 4603
03677889 4604 if (!b) {
979d0311
TH
4605 b = new0(CGroupBlockIODeviceBandwidth, 1);
4606 if (!b)
4607 return log_oom();
4608
c9f620bf 4609 b->path = TAKE_PTR(resolved);
979d0311
TH
4610 b->rbps = CGROUP_LIMIT_MAX;
4611 b->wbps = CGROUP_LIMIT_MAX;
4612
4613 LIST_PREPEND(device_bandwidths, c->blockio_device_bandwidths, b);
4614 }
4ad49000 4615
979d0311
TH
4616 if (read)
4617 b->rbps = bytes;
4618 else
4619 b->wbps = bytes;
4ad49000
LP
4620
4621 return 0;
4622}
4623
d420282b
LP
4624int config_parse_job_mode_isolate(
4625 const char *unit,
4626 const char *filename,
4627 unsigned line,
4628 const char *section,
4629 unsigned section_line,
4630 const char *lvalue,
4631 int ltype,
4632 const char *rvalue,
4633 void *data,
4634 void *userdata) {
4635
4636 JobMode *m = data;
4637 int r;
4638
4639 assert(filename);
4640 assert(lvalue);
4641 assert(rvalue);
4642
4643 r = parse_boolean(rvalue);
4644 if (r < 0) {
323dda78 4645 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse boolean, ignoring: %s", rvalue);
d420282b
LP
4646 return 0;
4647 }
4648
8ab39347
YW
4649 log_notice("%s is deprecated. Please use OnFailureJobMode= instead", lvalue);
4650
d420282b
LP
4651 *m = r ? JOB_ISOLATE : JOB_REPLACE;
4652 return 0;
4653}
4654
3536f49e 4655int config_parse_exec_directories(
e66cf1a3
LP
4656 const char *unit,
4657 const char *filename,
4658 unsigned line,
4659 const char *section,
4660 unsigned section_line,
4661 const char *lvalue,
4662 int ltype,
4663 const char *rvalue,
4664 void *data,
4665 void *userdata) {
4666
99534007 4667 ExecDirectory *ed = ASSERT_PTR(data);
47538b76 4668 const Unit *u = userdata;
e66cf1a3
LP
4669 int r;
4670
4671 assert(filename);
4672 assert(lvalue);
4673 assert(rvalue);
e66cf1a3
LP
4674
4675 if (isempty(rvalue)) {
4676 /* Empty assignment resets the list */
211a3d87 4677 exec_directory_done(ed);
e66cf1a3
LP
4678 return 0;
4679 }
4680
323dda78 4681 for (const char *p = rvalue;;) {
211a3d87 4682 _cleanup_free_ char *tuple = NULL;
e66cf1a3 4683
211a3d87 4684 r = extract_first_word(&p, &tuple, NULL, EXTRACT_UNQUOTE|EXTRACT_RETAIN_ESCAPE);
035fe294 4685 if (r == -ENOMEM)
e66cf1a3 4686 return log_oom();
035fe294
ZJS
4687 if (r < 0) {
4688 log_syntax(unit, LOG_WARNING, filename, line, r,
211a3d87 4689 "Invalid syntax %s=%s, ignoring: %m", lvalue, rvalue);
035fe294
ZJS
4690 return 0;
4691 }
091e9efe
LP
4692 if (r == 0)
4693 return 0;
e66cf1a3 4694
211a3d87
LB
4695 _cleanup_free_ char *src = NULL, *dest = NULL;
4696 const char *q = tuple;
4697 r = extract_many_words(&q, ":", EXTRACT_CUNESCAPE|EXTRACT_UNESCAPE_SEPARATORS, &src, &dest, NULL);
4698 if (r == -ENOMEM)
4699 return log_oom();
4700 if (r <= 0) {
5afdb462 4701 log_syntax(unit, LOG_WARNING, filename, line, r,
211a3d87
LB
4702 "Invalid syntax in %s=, ignoring: %s", lvalue, tuple);
4703 return 0;
4704 }
4705
4706 _cleanup_free_ char *sresolved = NULL;
4707 r = unit_path_printf(u, src, &sresolved);
9b5864d9 4708 if (r < 0) {
330f8990 4709 log_syntax(unit, LOG_WARNING, filename, line, r,
211a3d87 4710 "Failed to resolve unit specifiers in \"%s\", ignoring: %m", src);
9b5864d9
MG
4711 continue;
4712 }
4713
211a3d87 4714 r = path_simplify_and_warn(sresolved, PATH_CHECK_RELATIVE, unit, filename, line, lvalue);
2f4d31c1 4715 if (r < 0)
e8865688 4716 continue;
e8865688 4717
211a3d87 4718 if (path_startswith(sresolved, "private")) {
330f8990 4719 log_syntax(unit, LOG_WARNING, filename, line, 0,
211a3d87 4720 "%s= path can't be 'private', ignoring assignment: %s", lvalue, tuple);
e66cf1a3
LP
4721 continue;
4722 }
4723
211a3d87
LB
4724 /* For State and Runtime directories we support an optional destination parameter, which
4725 * will be used to create a symlink to the source. */
564e5c98 4726 _cleanup_free_ char *dresolved = NULL;
211a3d87 4727 if (!isempty(dest)) {
211a3d87
LB
4728 if (streq(lvalue, "ConfigurationDirectory")) {
4729 log_syntax(unit, LOG_WARNING, filename, line, 0,
4730 "Destination parameter is not supported for ConfigurationDirectory, ignoring: %s", tuple);
4731 continue;
4732 }
4733
4734 r = unit_path_printf(u, dest, &dresolved);
4735 if (r < 0) {
4736 log_syntax(unit, LOG_WARNING, filename, line, r,
4737 "Failed to resolve unit specifiers in \"%s\", ignoring: %m", dest);
4738 continue;
4739 }
4740
4741 r = path_simplify_and_warn(dresolved, PATH_CHECK_RELATIVE, unit, filename, line, lvalue);
4742 if (r < 0)
4743 continue;
211a3d87
LB
4744 }
4745
564e5c98 4746 r = exec_directory_add(ed, sresolved, dresolved);
e66cf1a3
LP
4747 if (r < 0)
4748 return log_oom();
e66cf1a3 4749 }
e66cf1a3
LP
4750}
4751
bb0c0d6f
LP
4752int config_parse_set_credential(
4753 const char *unit,
4754 const char *filename,
4755 unsigned line,
4756 const char *section,
4757 unsigned section_line,
4758 const char *lvalue,
4759 int ltype,
4760 const char *rvalue,
4761 void *data,
4762 void *userdata) {
4763
43144be4
LP
4764 _cleanup_free_ char *word = NULL, *k = NULL;
4765 _cleanup_free_ void *d = NULL;
99534007 4766 ExecContext *context = ASSERT_PTR(data);
bb0c0d6f
LP
4767 ExecSetCredential *old;
4768 Unit *u = userdata;
43144be4 4769 bool encrypted = ltype;
99534007 4770 const char *p = ASSERT_PTR(rvalue);
43144be4
LP
4771 size_t size;
4772 int r;
bb0c0d6f
LP
4773
4774 assert(filename);
4775 assert(lvalue);
bb0c0d6f
LP
4776
4777 if (isempty(rvalue)) {
4778 /* Empty assignment resets the list */
4779 context->set_credentials = hashmap_free(context->set_credentials);
4780 return 0;
4781 }
4782
bb0c0d6f
LP
4783 r = extract_first_word(&p, &word, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
4784 if (r == -ENOMEM)
4785 return log_oom();
6a35d52d
YW
4786 if (r < 0) {
4787 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to extract credential name, ignoring: %s", rvalue);
4788 return 0;
4789 }
4790 if (r == 0 || isempty(p)) {
4791 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid syntax, ignoring: %s", rvalue);
bb0c0d6f
LP
4792 return 0;
4793 }
4794
06536492 4795 r = unit_cred_printf(u, word, &k);
bb0c0d6f
LP
4796 if (r < 0) {
4797 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in \"%s\", ignoring: %m", word);
4798 return 0;
4799 }
4800 if (!credential_name_valid(k)) {
4801 log_syntax(unit, LOG_WARNING, filename, line, 0, "Credential name \"%s\" not valid, ignoring.", k);
4802 return 0;
4803 }
4804
43144be4
LP
4805 if (encrypted) {
4806 r = unbase64mem_full(p, SIZE_MAX, true, &d, &size);
4807 if (r < 0) {
4808 log_syntax(unit, LOG_WARNING, filename, line, r, "Encrypted credential data not valid Base64 data, ignoring.");
4809 return 0;
4810 }
4811 } else {
1421705d 4812 char *unescaped;
e437538f 4813 ssize_t l;
43144be4
LP
4814
4815 /* We support escape codes here, so that users can insert trailing \n if they like */
4816 l = cunescape(p, UNESCAPE_ACCEPT_NUL, &unescaped);
4817 if (l < 0) {
4818 log_syntax(unit, LOG_WARNING, filename, line, l, "Can't unescape \"%s\", ignoring: %m", p);
4819 return 0;
4820 }
4821
4822 d = unescaped;
4823 size = l;
bb0c0d6f
LP
4824 }
4825
4826 old = hashmap_get(context->set_credentials, k);
4827 if (old) {
43144be4
LP
4828 free_and_replace(old->data, d);
4829 old->size = size;
4830 old->encrypted = encrypted;
bb0c0d6f
LP
4831 } else {
4832 _cleanup_(exec_set_credential_freep) ExecSetCredential *sc = NULL;
4833
43144be4 4834 sc = new(ExecSetCredential, 1);
bb0c0d6f
LP
4835 if (!sc)
4836 return log_oom();
4837
43144be4
LP
4838 *sc = (ExecSetCredential) {
4839 .id = TAKE_PTR(k),
4840 .data = TAKE_PTR(d),
4841 .size = size,
4842 .encrypted = encrypted,
4843 };
bb0c0d6f 4844
f85f5f0d
SS
4845 r = hashmap_ensure_put(&context->set_credentials, &exec_set_credential_hash_ops, sc->id, sc);
4846 if (r == -ENOMEM)
4847 return log_oom();
2400743e 4848 if (r < 0) {
43144be4 4849 log_syntax(unit, LOG_WARNING, filename, line, r,
2400743e
YW
4850 "Duplicated credential value '%s', ignoring assignment: %s", sc->id, rvalue);
4851 return 0;
4852 }
bb0c0d6f 4853
bb0c0d6f
LP
4854 TAKE_PTR(sc);
4855 }
4856
4857 return 0;
4858}
4859
bbfb25f4
DDM
4860int hashmap_put_credential(Hashmap **h, const char *id, const char *path, bool encrypted) {
4861 ExecLoadCredential *old;
4862 int r;
4863
4864 assert(h);
4865 assert(id);
4866 assert(path);
4867
4868 old = hashmap_get(*h, id);
4869 if (old) {
4870 r = free_and_strdup(&old->path, path);
4871 if (r < 0)
4872 return r;
4873
4874 old->encrypted = encrypted;
4875 } else {
4876 _cleanup_(exec_load_credential_freep) ExecLoadCredential *lc = NULL;
4877
4878 lc = new(ExecLoadCredential, 1);
4879 if (!lc)
4880 return log_oom();
4881
4882 *lc = (ExecLoadCredential) {
4883 .id = strdup(id),
4884 .path = strdup(path),
4885 .encrypted = encrypted,
4886 };
4887 if (!lc->id || !lc->path)
4888 return -ENOMEM;
4889
4890 r = hashmap_ensure_put(h, &exec_load_credential_hash_ops, lc->id, lc);
4891 if (r < 0)
4892 return r;
4893
4894 TAKE_PTR(lc);
4895 }
4896
4897 return 0;
4898}
4899
bb0c0d6f
LP
4900int config_parse_load_credential(
4901 const char *unit,
4902 const char *filename,
4903 unsigned line,
4904 const char *section,
4905 unsigned section_line,
4906 const char *lvalue,
4907 int ltype,
4908 const char *rvalue,
4909 void *data,
4910 void *userdata) {
4911
4912 _cleanup_free_ char *word = NULL, *k = NULL, *q = NULL;
99534007 4913 ExecContext *context = ASSERT_PTR(data);
43144be4 4914 bool encrypted = ltype;
bb0c0d6f
LP
4915 Unit *u = userdata;
4916 const char *p;
4917 int r;
4918
4919 assert(filename);
4920 assert(lvalue);
4921 assert(rvalue);
bb0c0d6f
LP
4922
4923 if (isempty(rvalue)) {
4924 /* Empty assignment resets the list */
43144be4 4925 context->load_credentials = hashmap_free(context->load_credentials);
bb0c0d6f
LP
4926 return 0;
4927 }
4928
4929 p = rvalue;
4930 r = extract_first_word(&p, &word, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
4931 if (r == -ENOMEM)
4932 return log_oom();
4933 if (r <= 0) {
4934 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
4935 return 0;
4936 }
4937
06536492 4938 r = unit_cred_printf(u, word, &k);
bb0c0d6f
LP
4939 if (r < 0) {
4940 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in \"%s\", ignoring: %m", word);
4941 return 0;
4942 }
4943 if (!credential_name_valid(k)) {
4944 log_syntax(unit, LOG_WARNING, filename, line, 0, "Credential name \"%s\" not valid, ignoring.", k);
4945 return 0;
4946 }
8a29862e
LP
4947
4948 if (isempty(p)) {
08a7e545 4949 /* If only one field is specified take it as shortcut for inheriting a credential named
8a29862e
LP
4950 * the same way from our parent */
4951 q = strdup(k);
4952 if (!q)
4953 return log_oom();
4954 } else {
06536492 4955 r = unit_path_printf(u, p, &q);
8a29862e
LP
4956 if (r < 0) {
4957 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in \"%s\", ignoring: %m", p);
4958 return 0;
4959 }
4960 if (path_is_absolute(q) ? !path_is_normalized(q) : !credential_name_valid(q)) {
43144be4 4961 log_syntax(unit, LOG_WARNING, filename, line, 0, "Credential source \"%s\" not valid, ignoring.", q);
8a29862e
LP
4962 return 0;
4963 }
bb0c0d6f
LP
4964 }
4965
bbfb25f4
DDM
4966 r = hashmap_put_credential(&context->load_credentials, k, q, encrypted);
4967 if (r < 0)
4968 return log_error_errno(r, "Failed to store load credential '%s': %m", rvalue);
43144be4 4969
bbfb25f4
DDM
4970 return 0;
4971}
43144be4 4972
bbfb25f4
DDM
4973int config_parse_import_credential(
4974 const char *unit,
4975 const char *filename,
4976 unsigned line,
4977 const char *section,
4978 unsigned section_line,
4979 const char *lvalue,
4980 int ltype,
4981 const char *rvalue,
4982 void *data,
4983 void *userdata) {
43144be4 4984
bbfb25f4
DDM
4985 _cleanup_free_ char *s = NULL;
4986 Set** import_credentials = ASSERT_PTR(data);
4987 Unit *u = userdata;
4988 int r;
43144be4 4989
bbfb25f4
DDM
4990 assert(filename);
4991 assert(lvalue);
4992 assert(rvalue);
4993
4994 if (isempty(rvalue)) {
4995 /* Empty assignment resets the list */
1a572fd0 4996 *import_credentials = set_free_free(*import_credentials);
bbfb25f4
DDM
4997 return 0;
4998 }
4999
5000 r = unit_cred_printf(u, rvalue, &s);
5001 if (r < 0) {
5002 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in \"%s\", ignoring: %m", s);
5003 return 0;
5004 }
947c4d39
LP
5005 if (!credential_glob_valid(s)) {
5006 log_syntax(unit, LOG_WARNING, filename, line, 0, "Credential name or glob \"%s\" not valid, ignoring.", s);
bbfb25f4 5007 return 0;
43144be4 5008 }
bb0c0d6f 5009
bbfb25f4
DDM
5010 r = set_put_strdup(import_credentials, s);
5011 if (r < 0)
5012 return log_error_errno(r, "Failed to store credential name '%s': %m", rvalue);
5013
bb0c0d6f
LP
5014 return 0;
5015}
5016
3af00fb8
LP
5017int config_parse_set_status(
5018 const char *unit,
5019 const char *filename,
5020 unsigned line,
5021 const char *section,
5022 unsigned section_line,
5023 const char *lvalue,
5024 int ltype,
5025 const char *rvalue,
5026 void *data,
5027 void *userdata) {
5028
99534007 5029 ExitStatusSet *status_set = ASSERT_PTR(data);
7896ad8f 5030 int r;
3af00fb8
LP
5031
5032 assert(filename);
5033 assert(lvalue);
5034 assert(rvalue);
3af00fb8 5035
3e2d435b 5036 /* Empty assignment resets the list */
3af00fb8 5037 if (isempty(rvalue)) {
3e2d435b 5038 exit_status_set_free(status_set);
3af00fb8
LP
5039 return 0;
5040 }
5041
7896ad8f
ZJS
5042 for (const char *p = rvalue;;) {
5043 _cleanup_free_ char *word = NULL;
23d5dd16 5044 Bitmap *bitmap;
3af00fb8 5045
7896ad8f 5046 r = extract_first_word(&p, &word, NULL, 0);
323dda78
YW
5047 if (r == -ENOMEM)
5048 return log_oom();
5049 if (r < 0) {
5050 log_syntax(unit, LOG_WARNING, filename, line, r,
5051 "Failed to parse %s=%s, ignoring: %m", lvalue, rvalue);
5052 return 0;
5053 }
7896ad8f
ZJS
5054 if (r == 0)
5055 return 0;
3af00fb8 5056
2e2ed880
ZJS
5057 /* We need to call exit_status_from_string() first, because we want
5058 * to parse numbers as exit statuses, not signals. */
3af00fb8 5059
7896ad8f 5060 r = exit_status_from_string(word);
2e2ed880
ZJS
5061 if (r >= 0) {
5062 assert(r >= 0 && r < 256);
5063 bitmap = &status_set->status;
3af00fb8 5064 } else {
7896ad8f
ZJS
5065 r = signal_from_string(word);
5066 if (r < 0) {
b98680b2 5067 log_syntax(unit, LOG_WARNING, filename, line, r,
2e2ed880 5068 "Failed to parse value, ignoring: %s", word);
1e2fd62d 5069 continue;
3af00fb8 5070 }
2e2ed880 5071 bitmap = &status_set->signal;
3af00fb8 5072 }
1e2fd62d 5073
2e2ed880 5074 r = bitmap_set(bitmap, r);
063c4b1a 5075 if (r < 0)
323dda78
YW
5076 log_syntax(unit, LOG_WARNING, filename, line, r,
5077 "Failed to set signal or status %s, ignoring: %m", word);
3af00fb8 5078 }
3af00fb8
LP
5079}
5080
94828d2d
LP
5081int config_parse_namespace_path_strv(
5082 const char *unit,
5083 const char *filename,
5084 unsigned line,
5085 const char *section,
5086 unsigned section_line,
5087 const char *lvalue,
5088 int ltype,
5089 const char *rvalue,
5090 void *data,
5091 void *userdata) {
5092
47538b76 5093 const Unit *u = userdata;
99534007 5094 char*** sv = ASSERT_PTR(data);
94828d2d
LP
5095 int r;
5096
5097 assert(filename);
5098 assert(lvalue);
5099 assert(rvalue);
94828d2d
LP
5100
5101 if (isempty(rvalue)) {
5102 /* Empty assignment resets the list */
6796073e 5103 *sv = strv_free(*sv);
94828d2d
LP
5104 return 0;
5105 }
5106
323dda78 5107 for (const char *p = rvalue;;) {
7b07e993 5108 _cleanup_free_ char *word = NULL, *resolved = NULL, *joined = NULL;
20b7a007
LP
5109 const char *w;
5110 bool ignore_enoent = false, shall_prefix = false;
94828d2d 5111
4ec85141 5112 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
0293a7a8
EV
5113 if (r == -ENOMEM)
5114 return log_oom();
727f76d7 5115 if (r < 0) {
323dda78 5116 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to extract first word, ignoring: %s", rvalue);
727f76d7
EV
5117 return 0;
5118 }
a687f500
ZJS
5119 if (r == 0)
5120 break;
94828d2d 5121
20b7a007
LP
5122 w = word;
5123 if (startswith(w, "-")) {
5124 ignore_enoent = true;
5125 w++;
5126 }
5127 if (startswith(w, "+")) {
5128 shall_prefix = true;
5129 w++;
5130 }
7b07e993 5131
06536492 5132 r = unit_path_printf(u, w, &resolved);
7b07e993 5133 if (r < 0) {
323dda78 5134 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in %s: %m", w);
94828d2d
LP
5135 continue;
5136 }
5137
2f4d31c1
YW
5138 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
5139 if (r < 0)
7b07e993 5140 continue;
94828d2d 5141
20b7a007
LP
5142 joined = strjoin(ignore_enoent ? "-" : "",
5143 shall_prefix ? "+" : "",
5144 resolved);
7b07e993
LP
5145
5146 r = strv_push(sv, joined);
94828d2d
LP
5147 if (r < 0)
5148 return log_oom();
5149
7b07e993 5150 joined = NULL;
94828d2d
LP
5151 }
5152
5153 return 0;
5154}
5155
2abd4e38
YW
5156int config_parse_temporary_filesystems(
5157 const char *unit,
5158 const char *filename,
5159 unsigned line,
5160 const char *section,
5161 unsigned section_line,
5162 const char *lvalue,
5163 int ltype,
5164 const char *rvalue,
5165 void *data,
5166 void *userdata) {
5167
47538b76 5168 const Unit *u = userdata;
99534007 5169 ExecContext *c = ASSERT_PTR(data);
2abd4e38
YW
5170 int r;
5171
5172 assert(filename);
5173 assert(lvalue);
5174 assert(rvalue);
2abd4e38
YW
5175
5176 if (isempty(rvalue)) {
5177 /* Empty assignment resets the list */
5178 temporary_filesystem_free_many(c->temporary_filesystems, c->n_temporary_filesystems);
5179 c->temporary_filesystems = NULL;
5180 c->n_temporary_filesystems = 0;
5181 return 0;
5182 }
5183
323dda78 5184 for (const char *p = rvalue;;) {
2abd4e38
YW
5185 _cleanup_free_ char *word = NULL, *path = NULL, *resolved = NULL;
5186 const char *w;
5187
4ec85141 5188 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
2abd4e38
YW
5189 if (r == -ENOMEM)
5190 return log_oom();
5191 if (r < 0) {
323dda78 5192 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to extract first word, ignoring: %s", rvalue);
2abd4e38
YW
5193 return 0;
5194 }
a687f500
ZJS
5195 if (r == 0)
5196 return 0;
2abd4e38
YW
5197
5198 w = word;
5199 r = extract_first_word(&w, &path, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
063c4b1a
YW
5200 if (r == -ENOMEM)
5201 return log_oom();
5202 if (r < 0) {
323dda78 5203 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to extract first word, ignoring: %s", word);
063c4b1a
YW
5204 continue;
5205 }
5206 if (r == 0) {
323dda78 5207 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid syntax, ignoring: %s", word);
063c4b1a
YW
5208 continue;
5209 }
2abd4e38 5210
06536492 5211 r = unit_path_printf(u, path, &resolved);
2abd4e38 5212 if (r < 0) {
323dda78 5213 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", path);
2abd4e38
YW
5214 continue;
5215 }
5216
2f4d31c1
YW
5217 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
5218 if (r < 0)
2abd4e38 5219 continue;
2abd4e38 5220
a26fec24 5221 r = temporary_filesystem_add(&c->temporary_filesystems, &c->n_temporary_filesystems, resolved, w);
6302d1ea 5222 if (r < 0)
2abd4e38 5223 return log_oom();
2abd4e38 5224 }
2abd4e38
YW
5225}
5226
d2d6c096
LP
5227int config_parse_bind_paths(
5228 const char *unit,
5229 const char *filename,
5230 unsigned line,
5231 const char *section,
5232 unsigned section_line,
5233 const char *lvalue,
5234 int ltype,
5235 const char *rvalue,
5236 void *data,
5237 void *userdata) {
5238
99534007 5239 ExecContext *c = ASSERT_PTR(data);
47538b76 5240 const Unit *u = userdata;
d2d6c096
LP
5241 int r;
5242
5243 assert(filename);
5244 assert(lvalue);
5245 assert(rvalue);
d2d6c096
LP
5246
5247 if (isempty(rvalue)) {
5248 /* Empty assignment resets the list */
5249 bind_mount_free_many(c->bind_mounts, c->n_bind_mounts);
5250 c->bind_mounts = NULL;
5251 c->n_bind_mounts = 0;
5252 return 0;
5253 }
5254
323dda78 5255 for (const char *p = rvalue;;) {
d2d6c096 5256 _cleanup_free_ char *source = NULL, *destination = NULL;
42d43f21 5257 _cleanup_free_ char *sresolved = NULL, *dresolved = NULL;
d2d6c096
LP
5258 char *s = NULL, *d = NULL;
5259 bool rbind = true, ignore_enoent = false;
5260
4ec85141 5261 r = extract_first_word(&p, &source, ":" WHITESPACE, EXTRACT_UNQUOTE|EXTRACT_DONT_COALESCE_SEPARATORS);
d2d6c096
LP
5262 if (r == -ENOMEM)
5263 return log_oom();
5264 if (r < 0) {
323dda78 5265 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse %s, ignoring: %s", lvalue, rvalue);
d2d6c096
LP
5266 return 0;
5267 }
a687f500
ZJS
5268 if (r == 0)
5269 break;
d2d6c096 5270
e195a5c1 5271 r = unit_path_printf(u, source, &sresolved);
42d43f21 5272 if (r < 0) {
323dda78 5273 log_syntax(unit, LOG_WARNING, filename, line, r,
556a7bbe 5274 "Failed to resolve unit specifiers in \"%s\", ignoring: %m", source);
2f4d31c1 5275 continue;
42d43f21
DC
5276 }
5277
5278 s = sresolved;
d2d6c096
LP
5279 if (s[0] == '-') {
5280 ignore_enoent = true;
5281 s++;
5282 }
5283
2f4d31c1
YW
5284 r = path_simplify_and_warn(s, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
5285 if (r < 0)
5286 continue;
d2d6c096
LP
5287
5288 /* Optionally, the destination is specified. */
5289 if (p && p[-1] == ':') {
4ec85141 5290 r = extract_first_word(&p, &destination, ":" WHITESPACE, EXTRACT_UNQUOTE|EXTRACT_DONT_COALESCE_SEPARATORS);
d2d6c096
LP
5291 if (r == -ENOMEM)
5292 return log_oom();
5293 if (r < 0) {
323dda78 5294 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse %s, ignoring: %s", lvalue, rvalue);
d2d6c096
LP
5295 return 0;
5296 }
5297 if (r == 0) {
323dda78 5298 log_syntax(unit, LOG_WARNING, filename, line, 0, "Missing argument after ':', ignoring: %s", s);
2f4d31c1 5299 continue;
d2d6c096
LP
5300 }
5301
06536492 5302 r = unit_path_printf(u, destination, &dresolved);
42d43f21 5303 if (r < 0) {
323dda78 5304 log_syntax(unit, LOG_WARNING, filename, line, r,
556a7bbe 5305 "Failed to resolve specifiers in \"%s\", ignoring: %m", destination);
2f4d31c1 5306 continue;
42d43f21
DC
5307 }
5308
2f4d31c1
YW
5309 r = path_simplify_and_warn(dresolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
5310 if (r < 0)
5311 continue;
d2d6c096 5312
2f4d31c1 5313 d = dresolved;
d2d6c096
LP
5314
5315 /* Optionally, there's also a short option string specified */
5316 if (p && p[-1] == ':') {
5317 _cleanup_free_ char *options = NULL;
5318
4ec85141 5319 r = extract_first_word(&p, &options, NULL, EXTRACT_UNQUOTE);
d2d6c096
LP
5320 if (r == -ENOMEM)
5321 return log_oom();
5322 if (r < 0) {
6a35d52d 5323 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse %s=, ignoring: %s", lvalue, rvalue);
d2d6c096
LP
5324 return 0;
5325 }
5326
5327 if (isempty(options) || streq(options, "rbind"))
5328 rbind = true;
5329 else if (streq(options, "norbind"))
5330 rbind = false;
5331 else {
323dda78 5332 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid option string, ignoring setting: %s", options);
2f4d31c1 5333 continue;
d2d6c096
LP
5334 }
5335 }
5336 } else
5337 d = s;
5338
5339 r = bind_mount_add(&c->bind_mounts, &c->n_bind_mounts,
5340 &(BindMount) {
5341 .source = s,
5342 .destination = d,
5343 .read_only = !!strstr(lvalue, "ReadOnly"),
5344 .recursive = rbind,
5345 .ignore_enoent = ignore_enoent,
5346 });
5347 if (r < 0)
5348 return log_oom();
5349 }
5350
5351 return 0;
5352}
5353
b3d13314
LB
5354int config_parse_mount_images(
5355 const char *unit,
5356 const char *filename,
5357 unsigned line,
5358 const char *section,
5359 unsigned section_line,
5360 const char *lvalue,
5361 int ltype,
5362 const char *rvalue,
5363 void *data,
5364 void *userdata) {
5365
99534007 5366 ExecContext *c = ASSERT_PTR(data);
b3d13314 5367 const Unit *u = userdata;
b3d13314
LB
5368 int r;
5369
5370 assert(filename);
5371 assert(lvalue);
5372 assert(rvalue);
b3d13314
LB
5373
5374 if (isempty(rvalue)) {
5375 /* Empty assignment resets the list */
5376 c->mount_images = mount_image_free_many(c->mount_images, &c->n_mount_images);
5377 return 0;
5378 }
5379
323dda78 5380 for (const char *p = rvalue;;) {
427353f6
LB
5381 _cleanup_(mount_options_free_allp) MountOptions *options = NULL;
5382 _cleanup_free_ char *first = NULL, *second = NULL, *tuple = NULL;
b3d13314 5383 _cleanup_free_ char *sresolved = NULL, *dresolved = NULL;
427353f6 5384 const char *q = NULL;
b3d13314
LB
5385 char *s = NULL;
5386 bool permissive = false;
5387
427353f6 5388 r = extract_first_word(&p, &tuple, NULL, EXTRACT_UNQUOTE|EXTRACT_RETAIN_ESCAPE);
323dda78
YW
5389 if (r == -ENOMEM)
5390 return log_oom();
5391 if (r < 0) {
5392 log_syntax(unit, LOG_WARNING, filename, line, r,
5393 "Invalid syntax %s=%s, ignoring: %m", lvalue, rvalue);
5394 return 0;
5395 }
427353f6 5396 if (r == 0)
323dda78 5397 return 0;
427353f6
LB
5398
5399 q = tuple;
5400 r = extract_many_words(&q, ":", EXTRACT_CUNESCAPE|EXTRACT_UNESCAPE_SEPARATORS, &first, &second, NULL);
323dda78
YW
5401 if (r == -ENOMEM)
5402 return log_oom();
5403 if (r < 0) {
5404 log_syntax(unit, LOG_WARNING, filename, line, r,
5405 "Invalid syntax in %s=, ignoring: %s", lvalue, tuple);
5406 return 0;
5407 }
427353f6
LB
5408 if (r == 0)
5409 continue;
5410
6c3f7ca0 5411 s = first;
b3d13314
LB
5412 if (s[0] == '-') {
5413 permissive = true;
5414 s++;
5415 }
5416
06536492 5417 r = unit_path_printf(u, s, &sresolved);
6c3f7ca0
LB
5418 if (r < 0) {
5419 log_syntax(unit, LOG_WARNING, filename, line, r,
5420 "Failed to resolve unit specifiers in \"%s\", ignoring: %m", s);
5421 continue;
5422 }
5423
5424 r = path_simplify_and_warn(sresolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
b3d13314
LB
5425 if (r < 0)
5426 continue;
5427
427353f6 5428 if (isempty(second)) {
323dda78 5429 log_syntax(unit, LOG_WARNING, filename, line, 0, "Missing destination in %s, ignoring: %s", lvalue, rvalue);
b3d13314
LB
5430 continue;
5431 }
5432
06536492 5433 r = unit_path_printf(u, second, &dresolved);
b3d13314 5434 if (r < 0) {
323dda78 5435 log_syntax(unit, LOG_WARNING, filename, line, r,
427353f6 5436 "Failed to resolve specifiers in \"%s\", ignoring: %m", second);
b3d13314
LB
5437 continue;
5438 }
5439
5440 r = path_simplify_and_warn(dresolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
5441 if (r < 0)
5442 continue;
5443
427353f6
LB
5444 for (;;) {
5445 _cleanup_free_ char *partition = NULL, *mount_options = NULL, *mount_options_resolved = NULL;
5446 MountOptions *o = NULL;
569a0e42 5447 PartitionDesignator partition_designator;
427353f6
LB
5448
5449 r = extract_many_words(&q, ":", EXTRACT_CUNESCAPE|EXTRACT_UNESCAPE_SEPARATORS, &partition, &mount_options, NULL);
323dda78
YW
5450 if (r == -ENOMEM)
5451 return log_oom();
5452 if (r < 0) {
5453 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", q);
5454 return 0;
5455 }
427353f6
LB
5456 if (r == 0)
5457 break;
5458 /* Single set of options, applying to the root partition/single filesystem */
5459 if (r == 1) {
5460 r = unit_full_printf(u, partition, &mount_options_resolved);
5461 if (r < 0) {
323dda78 5462 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", first);
427353f6
LB
5463 continue;
5464 }
5465
5466 o = new(MountOptions, 1);
5467 if (!o)
5468 return log_oom();
5469 *o = (MountOptions) {
5470 .partition_designator = PARTITION_ROOT,
5471 .options = TAKE_PTR(mount_options_resolved),
5472 };
5473 LIST_APPEND(mount_options, options, o);
5474
5475 break;
5476 }
5477
5478 partition_designator = partition_designator_from_string(partition);
5479 if (partition_designator < 0) {
b98680b2
YW
5480 log_syntax(unit, LOG_WARNING, filename, line, partition_designator,
5481 "Invalid partition name %s, ignoring", partition);
427353f6
LB
5482 continue;
5483 }
5484 r = unit_full_printf(u, mount_options, &mount_options_resolved);
5485 if (r < 0) {
323dda78 5486 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", mount_options);
427353f6
LB
5487 continue;
5488 }
5489
5490 o = new(MountOptions, 1);
5491 if (!o)
5492 return log_oom();
5493 *o = (MountOptions) {
5494 .partition_designator = partition_designator,
5495 .options = TAKE_PTR(mount_options_resolved),
5496 };
5497 LIST_APPEND(mount_options, options, o);
5498 }
5499
b3d13314
LB
5500 r = mount_image_add(&c->mount_images, &c->n_mount_images,
5501 &(MountImage) {
6c3f7ca0 5502 .source = sresolved,
b3d13314 5503 .destination = dresolved,
427353f6 5504 .mount_options = options,
b3d13314 5505 .ignore_enoent = permissive,
93f59701
LB
5506 .type = MOUNT_IMAGE_DISCRETE,
5507 });
5508 if (r < 0)
5509 return log_oom();
5510 }
5511}
5512
5513int config_parse_extension_images(
5514 const char *unit,
5515 const char *filename,
5516 unsigned line,
5517 const char *section,
5518 unsigned section_line,
5519 const char *lvalue,
5520 int ltype,
5521 const char *rvalue,
5522 void *data,
5523 void *userdata) {
5524
99534007 5525 ExecContext *c = ASSERT_PTR(data);
93f59701
LB
5526 const Unit *u = userdata;
5527 int r;
5528
5529 assert(filename);
5530 assert(lvalue);
5531 assert(rvalue);
93f59701
LB
5532
5533 if (isempty(rvalue)) {
5534 /* Empty assignment resets the list */
5535 c->extension_images = mount_image_free_many(c->extension_images, &c->n_extension_images);
5536 return 0;
5537 }
5538
5539 for (const char *p = rvalue;;) {
5540 _cleanup_free_ char *source = NULL, *tuple = NULL, *sresolved = NULL;
5541 _cleanup_(mount_options_free_allp) MountOptions *options = NULL;
5542 bool permissive = false;
5543 const char *q = NULL;
5544 char *s = NULL;
5545
5546 r = extract_first_word(&p, &tuple, NULL, EXTRACT_UNQUOTE|EXTRACT_RETAIN_ESCAPE);
5547 if (r == -ENOMEM)
5548 return log_oom();
5549 if (r < 0) {
5550 log_syntax(unit, LOG_WARNING, filename, line, r,
5551 "Invalid syntax %s=%s, ignoring: %m", lvalue, rvalue);
5552 return 0;
5553 }
5554 if (r == 0)
5555 return 0;
5556
5557 q = tuple;
5558 r = extract_first_word(&q, &source, ":", EXTRACT_CUNESCAPE|EXTRACT_UNESCAPE_SEPARATORS);
5559 if (r == -ENOMEM)
5560 return log_oom();
5561 if (r < 0) {
5562 log_syntax(unit, LOG_WARNING, filename, line, r,
5563 "Invalid syntax in %s=, ignoring: %s", lvalue, tuple);
5564 return 0;
5565 }
5566 if (r == 0)
5567 continue;
5568
5569 s = source;
5570 if (s[0] == '-') {
5571 permissive = true;
5572 s++;
5573 }
5574
06536492 5575 r = unit_path_printf(u, s, &sresolved);
93f59701
LB
5576 if (r < 0) {
5577 log_syntax(unit, LOG_WARNING, filename, line, r,
5578 "Failed to resolve unit specifiers in \"%s\", ignoring: %m", s);
5579 continue;
5580 }
5581
5582 r = path_simplify_and_warn(sresolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
5583 if (r < 0)
5584 continue;
5585
5586 for (;;) {
5587 _cleanup_free_ char *partition = NULL, *mount_options = NULL, *mount_options_resolved = NULL;
5588 MountOptions *o = NULL;
5589 PartitionDesignator partition_designator;
5590
5591 r = extract_many_words(&q, ":", EXTRACT_CUNESCAPE|EXTRACT_UNESCAPE_SEPARATORS, &partition, &mount_options, NULL);
5592 if (r == -ENOMEM)
5593 return log_oom();
5594 if (r < 0) {
5595 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", q);
5596 return 0;
5597 }
5598 if (r == 0)
5599 break;
5600 /* Single set of options, applying to the root partition/single filesystem */
5601 if (r == 1) {
5602 r = unit_full_printf(u, partition, &mount_options_resolved);
5603 if (r < 0) {
5604 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", partition);
5605 continue;
5606 }
5607
5608 o = new(MountOptions, 1);
5609 if (!o)
5610 return log_oom();
5611 *o = (MountOptions) {
5612 .partition_designator = PARTITION_ROOT,
5613 .options = TAKE_PTR(mount_options_resolved),
5614 };
5615 LIST_APPEND(mount_options, options, o);
5616
5617 break;
5618 }
5619
5620 partition_designator = partition_designator_from_string(partition);
5621 if (partition_designator < 0) {
5622 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid partition name %s, ignoring", partition);
5623 continue;
5624 }
5625 r = unit_full_printf(u, mount_options, &mount_options_resolved);
5626 if (r < 0) {
5627 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", mount_options);
5628 continue;
5629 }
5630
5631 o = new(MountOptions, 1);
5632 if (!o)
5633 return log_oom();
5634 *o = (MountOptions) {
5635 .partition_designator = partition_designator,
5636 .options = TAKE_PTR(mount_options_resolved),
5637 };
5638 LIST_APPEND(mount_options, options, o);
5639 }
5640
5641 r = mount_image_add(&c->extension_images, &c->n_extension_images,
5642 &(MountImage) {
5643 .source = sresolved,
5644 .mount_options = options,
5645 .ignore_enoent = permissive,
5646 .type = MOUNT_IMAGE_EXTENSION,
b3d13314
LB
5647 });
5648 if (r < 0)
5649 return log_oom();
5650 }
b3d13314
LB
5651}
5652
eae51da3
LP
5653int config_parse_job_timeout_sec(
5654 const char* unit,
5655 const char *filename,
5656 unsigned line,
5657 const char *section,
5658 unsigned section_line,
5659 const char *lvalue,
5660 int ltype,
5661 const char *rvalue,
5662 void *data,
5663 void *userdata) {
5664
99534007 5665 Unit *u = ASSERT_PTR(data);
eae51da3
LP
5666 usec_t usec;
5667 int r;
5668
5669 assert(filename);
5670 assert(lvalue);
5671 assert(rvalue);
eae51da3
LP
5672
5673 r = parse_sec_fix_0(rvalue, &usec);
5674 if (r < 0) {
323dda78 5675 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse JobTimeoutSec= parameter, ignoring: %s", rvalue);
eae51da3
LP
5676 return 0;
5677 }
5678
5679 /* If the user explicitly changed JobTimeoutSec= also change JobRunningTimeoutSec=, for compatibility with old
c05f3c8f 5680 * versions. If JobRunningTimeoutSec= was explicitly set, avoid this however as whatever the user picked should
eae51da3
LP
5681 * count. */
5682
5683 if (!u->job_running_timeout_set)
5684 u->job_running_timeout = usec;
5685
5686 u->job_timeout = usec;
5687
5688 return 0;
5689}
5690
5691int config_parse_job_running_timeout_sec(
5692 const char* unit,
5693 const char *filename,
5694 unsigned line,
5695 const char *section,
5696 unsigned section_line,
5697 const char *lvalue,
5698 int ltype,
5699 const char *rvalue,
5700 void *data,
5701 void *userdata) {
5702
99534007 5703 Unit *u = ASSERT_PTR(data);
eae51da3
LP
5704 usec_t usec;
5705 int r;
5706
5707 assert(filename);
5708 assert(lvalue);
5709 assert(rvalue);
eae51da3
LP
5710
5711 r = parse_sec_fix_0(rvalue, &usec);
5712 if (r < 0) {
323dda78 5713 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse JobRunningTimeoutSec= parameter, ignoring: %s", rvalue);
eae51da3
LP
5714 return 0;
5715 }
5716
5717 u->job_running_timeout = usec;
5718 u->job_running_timeout_set = true;
5719
5720 return 0;
5721}
5722
54fcb619
ZJS
5723int config_parse_emergency_action(
5724 const char* unit,
5725 const char *filename,
5726 unsigned line,
5727 const char *section,
5728 unsigned section_line,
5729 const char *lvalue,
5730 int ltype,
5731 const char *rvalue,
5732 void *data,
5733 void *userdata) {
5734
99534007 5735 EmergencyAction *x = ASSERT_PTR(data);
4870133b 5736 RuntimeScope runtime_scope;
54fcb619
ZJS
5737 int r;
5738
5739 assert(filename);
5740 assert(lvalue);
5741 assert(rvalue);
54fcb619 5742
724f061d 5743 /* If we have a unit determine the scope based on it */
54fcb619 5744 if (unit)
4870133b 5745 runtime_scope = ((Unit*) ASSERT_PTR(userdata))->manager->runtime_scope;
54fcb619 5746 else
4870133b 5747 runtime_scope = ltype; /* otherwise, assume the scope is passed in via ltype */
54fcb619 5748
4870133b 5749 r = parse_emergency_action(rvalue, runtime_scope, x);
54fcb619
ZJS
5750 if (r < 0) {
5751 if (r == -EOPNOTSUPP)
323dda78 5752 log_syntax(unit, LOG_WARNING, filename, line, r,
54fcb619 5753 "%s= specified as %s mode action, ignoring: %s",
4870133b 5754 lvalue, runtime_scope_to_string(runtime_scope), rvalue);
54fcb619 5755 else
323dda78 5756 log_syntax(unit, LOG_WARNING, filename, line, r,
54fcb619
ZJS
5757 "Failed to parse %s=, ignoring: %s", lvalue, rvalue);
5758 return 0;
5759 }
5760
5761 return 0;
5762}
5763
a9353a5c
LP
5764int config_parse_pid_file(
5765 const char *unit,
5766 const char *filename,
5767 unsigned line,
5768 const char *section,
5769 unsigned section_line,
5770 const char *lvalue,
5771 int ltype,
5772 const char *rvalue,
5773 void *data,
5774 void *userdata) {
5775
5776 _cleanup_free_ char *k = NULL, *n = NULL;
99534007 5777 const Unit *u = ASSERT_PTR(userdata);
a9353a5c 5778 char **s = data;
a9353a5c
LP
5779 int r;
5780
5781 assert(filename);
5782 assert(lvalue);
5783 assert(rvalue);
a9353a5c 5784
b8055c05
YW
5785 if (isempty(rvalue)) {
5786 /* An empty assignment removes already set value. */
5787 *s = mfree(*s);
5788 return 0;
5789 }
5790
06536492 5791 r = unit_path_printf(u, rvalue, &k);
a9353a5c 5792 if (r < 0) {
323dda78 5793 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
a9353a5c
LP
5794 return 0;
5795 }
5796
5797 /* If this is a relative path make it absolute by prefixing the /run */
5798 n = path_make_absolute(k, u->manager->prefix[EXEC_DIRECTORY_RUNTIME]);
5799 if (!n)
5800 return log_oom();
5801
5802 /* Check that the result is a sensible path */
5803 r = path_simplify_and_warn(n, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
5804 if (r < 0)
5805 return r;
5806
4a66b5c9
LP
5807 r = patch_var_run(unit, filename, line, lvalue, &n);
5808 if (r < 0)
5809 return r;
a9353a5c 5810
4a66b5c9 5811 free_and_replace(*s, n);
a9353a5c
LP
5812 return 0;
5813}
5814
7af67e9a
LP
5815int config_parse_exit_status(
5816 const char *unit,
5817 const char *filename,
5818 unsigned line,
5819 const char *section,
5820 unsigned section_line,
5821 const char *lvalue,
5822 int ltype,
5823 const char *rvalue,
5824 void *data,
5825 void *userdata) {
5826
5827 int *exit_status = data, r;
5828 uint8_t u;
5829
5830 assert(filename);
5831 assert(lvalue);
5832 assert(rvalue);
5833 assert(exit_status);
5834
5835 if (isempty(rvalue)) {
5836 *exit_status = -1;
5837 return 0;
5838 }
5839
5840 r = safe_atou8(rvalue, &u);
5841 if (r < 0) {
323dda78 5842 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse exit status '%s', ignoring: %m", rvalue);
7af67e9a
LP
5843 return 0;
5844 }
5845
5846 *exit_status = u;
5847 return 0;
5848}
5849
c72703e2
CD
5850int config_parse_disable_controllers(
5851 const char *unit,
5852 const char *filename,
5853 unsigned line,
5854 const char *section,
5855 unsigned section_line,
5856 const char *lvalue,
5857 int ltype,
5858 const char *rvalue,
5859 void *data,
5860 void *userdata) {
5861
5862 int r;
5863 CGroupContext *c = data;
5864 CGroupMask disabled_mask;
5865
5866 /* 1. If empty, make all controllers eligible for use again.
5867 * 2. If non-empty, merge all listed controllers, space separated. */
5868
5869 if (isempty(rvalue)) {
5870 c->disable_controllers = 0;
5871 return 0;
5872 }
5873
5874 r = cg_mask_from_string(rvalue, &disabled_mask);
5875 if (r < 0 || disabled_mask <= 0) {
323dda78 5876 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid cgroup string: %s, ignoring", rvalue);
c72703e2
CD
5877 return 0;
5878 }
5879
5880 c->disable_controllers |= disabled_mask;
5881
5882 return 0;
5883}
5884
fab34748
KL
5885int config_parse_ip_filter_bpf_progs(
5886 const char *unit,
5887 const char *filename,
5888 unsigned line,
5889 const char *section,
5890 unsigned section_line,
5891 const char *lvalue,
5892 int ltype,
5893 const char *rvalue,
5894 void *data,
5895 void *userdata) {
5896
5897 _cleanup_free_ char *resolved = NULL;
47538b76 5898 const Unit *u = userdata;
99534007 5899 char ***paths = ASSERT_PTR(data);
fab34748
KL
5900 int r;
5901
5902 assert(filename);
5903 assert(lvalue);
5904 assert(rvalue);
fab34748
KL
5905
5906 if (isempty(rvalue)) {
5907 *paths = strv_free(*paths);
5908 return 0;
5909 }
5910
06536492 5911 r = unit_path_printf(u, rvalue, &resolved);
fab34748 5912 if (r < 0) {
323dda78 5913 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue);
fab34748
KL
5914 return 0;
5915 }
5916
5917 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
5918 if (r < 0)
5919 return 0;
5920
5921 if (strv_contains(*paths, resolved))
5922 return 0;
5923
5924 r = strv_extend(paths, resolved);
5925 if (r < 0)
5926 return log_oom();
5927
5928 r = bpf_firewall_supported();
5929 if (r < 0)
5930 return r;
5931 if (r != BPF_FIREWALL_SUPPORTED_WITH_MULTI) {
5932 static bool warned = false;
5933
5934 log_full(warned ? LOG_DEBUG : LOG_WARNING,
5935 "File %s:%u configures an IP firewall with BPF programs (%s=%s), but the local system does not support BPF/cgroup based firewalling with multiple filters.\n"
5936 "Starting this unit will fail! (This warning is only shown for the first loaded unit using IP firewalling.)", filename, line, lvalue, rvalue);
5937
5938 warned = true;
5939 }
5940
5941 return 0;
5942}
5943
0879da98
JK
5944int config_parse_bpf_foreign_program(
5945 const char *unit,
5946 const char *filename,
5947 unsigned line,
5948 const char *section,
5949 unsigned section_line,
5950 const char *lvalue,
5951 int ltype,
5952 const char *rvalue,
5953 void *data,
5954 void *userdata) {
5955 _cleanup_free_ char *resolved = NULL, *word = NULL;
5956 CGroupContext *c = data;
99534007 5957 const char *p = ASSERT_PTR(rvalue);
0879da98
JK
5958 Unit *u = userdata;
5959 int attach_type, r;
5960
5961 assert(filename);
5962 assert(lvalue);
0879da98
JK
5963
5964 if (isempty(rvalue)) {
5965 while (c->bpf_foreign_programs)
5966 cgroup_context_remove_bpf_foreign_program(c, c->bpf_foreign_programs);
5967
5968 return 0;
5969 }
5970
6a35d52d 5971 r = extract_first_word(&p, &word, ":", 0);
0879da98
JK
5972 if (r == -ENOMEM)
5973 return log_oom();
6a35d52d 5974 if (r < 0) {
0879da98
JK
5975 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse foreign BPF program, ignoring: %s", rvalue);
5976 return 0;
5977 }
6a35d52d
YW
5978 if (r == 0 || isempty(p)) {
5979 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid syntax in %s=, ignoring: %s", lvalue, rvalue);
5980 return 0;
5981 }
0879da98
JK
5982
5983 attach_type = bpf_cgroup_attach_type_from_string(word);
5984 if (attach_type < 0) {
5985 log_syntax(unit, LOG_WARNING, filename, line, 0, "Unknown BPF attach type=%s, ignoring: %s", word, rvalue);
5986 return 0;
5987 }
5988
6a35d52d 5989 r = unit_path_printf(u, p, &resolved);
0879da98 5990 if (r < 0) {
6a35d52d 5991 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %s", p, rvalue);
0879da98
JK
5992 return 0;
5993 }
5994
5995 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
5996 if (r < 0)
5997 return 0;
5998
c6f2dca6 5999 r = cgroup_context_add_bpf_foreign_program(c, attach_type, resolved);
0879da98
JK
6000 if (r < 0)
6001 return log_error_errno(r, "Failed to add foreign BPF program to cgroup context: %m");
6002
6003 return 0;
6004}
6005
8dd210ab
JK
6006int config_parse_cgroup_socket_bind(
6007 const char *unit,
6008 const char *filename,
6009 unsigned line,
6010 const char *section,
6011 unsigned section_line,
6012 const char *lvalue,
6013 int ltype,
6014 const char *rvalue,
6015 void *data,
6016 void *userdata) {
6017 _cleanup_free_ CGroupSocketBindItem *item = NULL;
8dd210ab 6018 CGroupSocketBindItem **head = data;
5587ce7f
JK
6019 uint16_t nr_ports, port_min;
6020 int af, ip_protocol, r;
8dd210ab
JK
6021
6022 if (isempty(rvalue)) {
6023 cgroup_context_remove_socket_bind(head);
6024 return 0;
6025 }
6026
5587ce7f 6027 r = parse_socket_bind_item(rvalue, &af, &ip_protocol, &nr_ports, &port_min);
8dd210ab
JK
6028 if (r == -ENOMEM)
6029 return log_oom();
5587ce7f 6030 if (r < 0) {
cc87b3f6
ZJS
6031 log_syntax(unit, LOG_WARNING, filename, line, r,
6032 "Unable to parse %s= assignment, ignoring: %s", lvalue, rvalue);
6033 return 0;
6034 }
8dd210ab 6035
8dd210ab
JK
6036 item = new(CGroupSocketBindItem, 1);
6037 if (!item)
6038 return log_oom();
6039 *item = (CGroupSocketBindItem) {
6040 .address_family = af,
5587ce7f 6041 .ip_protocol = ip_protocol,
8dd210ab
JK
6042 .nr_ports = nr_ports,
6043 .port_min = port_min,
6044 };
6045
6046 LIST_PREPEND(socket_bind_items, *head, TAKE_PTR(item));
6047
6048 return 0;
6049}
6050
4f0c25c7
MV
6051int config_parse_restrict_network_interfaces(
6052 const char *unit,
6053 const char *filename,
6054 unsigned line,
6055 const char *section,
6056 unsigned section_line,
6057 const char *lvalue,
6058 int ltype,
6059 const char *rvalue,
6060 void *data,
6061 void *userdata) {
99534007 6062 CGroupContext *c = ASSERT_PTR(data);
4f0c25c7
MV
6063 bool is_allow_rule = true;
6064 int r;
6065
6066 assert(filename);
6067 assert(lvalue);
6068 assert(rvalue);
4f0c25c7
MV
6069
6070 if (isempty(rvalue)) {
6071 /* Empty assignment resets the list */
1a572fd0 6072 c->restrict_network_interfaces = set_free_free(c->restrict_network_interfaces);
4f0c25c7
MV
6073 return 0;
6074 }
6075
6076 if (rvalue[0] == '~') {
6077 is_allow_rule = false;
6078 rvalue++;
6079 }
6080
6081 if (set_isempty(c->restrict_network_interfaces))
6082 /* Only initialize this when creating the set */
6083 c->restrict_network_interfaces_is_allow_list = is_allow_rule;
6084
6085 for (const char *p = rvalue;;) {
6086 _cleanup_free_ char *word = NULL;
6087
6088 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
6089 if (r == 0)
6090 break;
6091 if (r == -ENOMEM)
6092 return log_oom();
6093 if (r < 0) {
6094 log_syntax(unit, LOG_WARNING, filename, line, r,
6095 "Trailing garbage in %s, ignoring: %s", lvalue, rvalue);
6096 break;
6097 }
6098
4e0db87e 6099 if (!ifname_valid_full(word, IFNAME_VALID_ALTERNATIVE)) {
4f0c25c7
MV
6100 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid interface name, ignoring: %s", word);
6101 continue;
6102 }
6103
6104 if (c->restrict_network_interfaces_is_allow_list != is_allow_rule)
6105 free(set_remove(c->restrict_network_interfaces, word));
6106 else {
6107 r = set_put_strdup(&c->restrict_network_interfaces, word);
6108 if (r < 0)
6109 return log_oom();
6110 }
6111 }
6112
6113 return 0;
6114}
6115
57ffa99d 6116static int merge_by_names(Unit *u, Set *names, const char *id) {
23a177ef
LP
6117 char *k;
6118 int r;
6119
6120 assert(u);
23a177ef 6121
e8630e69 6122 /* Let's try to add in all names that are aliases of this unit */
23a177ef 6123 while ((k = set_steal_first(names))) {
e8630e69 6124 _cleanup_free_ _unused_ char *free_k = k;
23a177ef 6125
e8630e69 6126 /* First try to merge in the other name into our unit */
57ffa99d 6127 r = unit_merge_by_name(u, k);
9946996c 6128 if (r < 0) {
23a177ef
LP
6129 Unit *other;
6130
e8630e69
ZJS
6131 /* Hmm, we couldn't merge the other unit into ours? Then let's try it the other way
6132 * round. */
036643a2 6133
57ffa99d 6134 other = manager_get_unit(u->manager, k);
e8630e69
ZJS
6135 if (!other)
6136 return r; /* return previous failure */
036643a2 6137
57ffa99d 6138 r = unit_merge(other, u);
e8630e69 6139 if (r < 0)
a837f088 6140 return r;
fe51822e 6141
57ffa99d 6142 return merge_by_names(other, names, NULL);
036643a2 6143 }
034c6ed7 6144
e8630e69 6145 if (streq_ptr(id, k))
57ffa99d 6146 unit_choose_id(u, id);
1b64d026
LP
6147 }
6148
e48614c4 6149 return 0;
0301abf4
LP
6150}
6151
e537352b 6152int unit_load_fragment(Unit *u) {
e8630e69
ZJS
6153 const char *fragment;
6154 _cleanup_set_free_free_ Set *names = NULL;
23a177ef 6155 int r;
0301abf4
LP
6156
6157 assert(u);
ac155bb8
MS
6158 assert(u->load_state == UNIT_STUB);
6159 assert(u->id);
23a177ef 6160
3f5e8115 6161 if (u->transient) {
23e9a7dd 6162 u->access_selinux_context = mfree(u->access_selinux_context);
3f5e8115
LP
6163 u->load_state = UNIT_LOADED;
6164 return 0;
6165 }
6166
91e0ee5f
ZJS
6167 /* Possibly rebuild the fragment map to catch new units */
6168 r = unit_file_build_name_map(&u->manager->lookup_paths,
c2911d48 6169 &u->manager->unit_cache_timestamp_hash,
91e0ee5f
ZJS
6170 &u->manager->unit_id_map,
6171 &u->manager->unit_name_map,
6172 &u->manager->unit_path_cache);
9946996c 6173 if (r < 0)
14140908 6174 return log_error_errno(r, "Failed to rebuild name map: %m");
91e0ee5f 6175
e8630e69
ZJS
6176 r = unit_file_find_fragment(u->manager->unit_id_map,
6177 u->manager->unit_name_map,
6178 u->id,
6179 &fragment,
6180 &names);
6181 if (r < 0 && r != -ENOENT)
294d81f1
LP
6182 return r;
6183
e8630e69
ZJS
6184 if (fragment) {
6185 /* Open the file, check if this is a mask, otherwise read. */
6186 _cleanup_fclose_ FILE *f = NULL;
c9e06956 6187 struct stat st;
0301abf4 6188
e8630e69
ZJS
6189 /* Try to open the file name. A symlink is OK, for example for linked files or masks. We
6190 * expect that all symlinks within the lookup paths have been already resolved, but we don't
6191 * verify this here. */
6192 f = fopen(fragment, "re");
6193 if (!f)
6194 return log_unit_notice_errno(u, errno, "Failed to open %s: %m", fragment);
6ccb1b44 6195
e8630e69
ZJS
6196 if (fstat(fileno(f), &st) < 0)
6197 return -errno;
294d81f1 6198
e8630e69 6199 r = free_and_strdup(&u->fragment_path, fragment);
7410616c
LP
6200 if (r < 0)
6201 return r;
294d81f1 6202
e8630e69 6203 if (null_or_empty(&st)) {
88414eed
LP
6204 /* Unit file is masked */
6205
6206 u->load_state = u->perpetual ? UNIT_LOADED : UNIT_MASKED; /* don't allow perpetual units to ever be masked */
e8630e69 6207 u->fragment_mtime = 0;
23e9a7dd 6208 u->access_selinux_context = mfree(u->access_selinux_context);
e8630e69 6209 } else {
23e9a7dd
LP
6210#if HAVE_SELINUX
6211 if (mac_selinux_use()) {
6212 _cleanup_freecon_ char *selcon = NULL;
6213
6214 /* Cache the SELinux context of the unit file here. We'll make use of when checking access permissions to loaded units */
6215 r = fgetfilecon_raw(fileno(f), &selcon);
6216 if (r < 0)
6217 log_unit_warning_errno(u, r, "Failed to read SELinux context of '%s', ignoring: %m", fragment);
6218
6219 r = free_and_strdup(&u->access_selinux_context, selcon);
6220 if (r < 0)
6221 return r;
6222 } else
6223#endif
6224 u->access_selinux_context = mfree(u->access_selinux_context);
6225
e8630e69
ZJS
6226 u->load_state = UNIT_LOADED;
6227 u->fragment_mtime = timespec_load(&st.st_mtim);
6228
6229 /* Now, parse the file contents */
6230 r = config_parse(u->id, fragment, f,
6231 UNIT_VTABLE(u)->sections,
6232 config_item_perf_lookup, load_fragment_gperf_lookup,
7ade8982 6233 0,
4f9ff96a
LP
6234 u,
6235 NULL);
bb28e684 6236 if (r == -ENOEXEC)
e8630e69
ZJS
6237 log_unit_notice_errno(u, r, "Unit configuration has fatal error, unit will not be started.");
6238 if (r < 0)
6239 return r;
bb28e684 6240 }
e8630e69 6241 }
890f434c 6242
3aa57658
ZJS
6243 /* Call merge_by_names with the name derived from the fragment path as the preferred name.
6244 *
6245 * We do the merge dance here because for some unit types, the unit might have aliases which are not
e8630e69
ZJS
6246 * declared in the file system. In particular, this is true (and frequent) for device and swap units.
6247 */
e8630e69 6248 const char *id = u->id;
b58feca6 6249 _cleanup_free_ char *filename = NULL, *free_id = NULL;
294d81f1 6250
e8630e69 6251 if (fragment) {
b58feca6
JR
6252 r = path_extract_filename(fragment, &filename);
6253 if (r < 0)
6254 return log_debug_errno(r, "Failed to extract filename from fragment '%s': %m", fragment);
6255 id = filename;
6256
e8630e69
ZJS
6257 if (unit_name_is_valid(id, UNIT_NAME_TEMPLATE)) {
6258 assert(u->instance); /* If we're not trying to use a template for non-instanced unit,
6259 * this must be set. */
890f434c 6260
e8630e69
ZJS
6261 r = unit_name_replace_instance(id, u->instance, &free_id);
6262 if (r < 0)
6263 return log_debug_errno(r, "Failed to build id (%s + %s): %m", id, u->instance);
6264 id = free_id;
abc08d4d 6265 }
071830ff
LP
6266 }
6267
57ffa99d 6268 return merge_by_names(u, names, id);
3efd4195 6269}
e537352b
LP
6270
6271void unit_dump_config_items(FILE *f) {
f975e971
LP
6272 static const struct {
6273 const ConfigParserCallback callback;
6274 const char *rvalue;
6275 } table[] = {
17df7223 6276 { config_parse_warn_compat, "NOTSUPPORTED" },
f975e971
LP
6277 { config_parse_int, "INTEGER" },
6278 { config_parse_unsigned, "UNSIGNED" },
5556b5fe 6279 { config_parse_iec_size, "SIZE" },
59f448cf 6280 { config_parse_iec_uint64, "SIZE" },
50299121 6281 { config_parse_si_uint64, "SIZE" },
f975e971
LP
6282 { config_parse_bool, "BOOLEAN" },
6283 { config_parse_string, "STRING" },
6284 { config_parse_path, "PATH" },
6285 { config_parse_unit_path_printf, "PATH" },
8c35c10d 6286 { config_parse_colon_separated_paths, "PATH" },
f975e971
LP
6287 { config_parse_strv, "STRING [...]" },
6288 { config_parse_exec_nice, "NICE" },
6289 { config_parse_exec_oom_score_adjust, "OOMSCOREADJUST" },
6290 { config_parse_exec_io_class, "IOCLASS" },
6291 { config_parse_exec_io_priority, "IOPRIORITY" },
6292 { config_parse_exec_cpu_sched_policy, "CPUSCHEDPOLICY" },
6293 { config_parse_exec_cpu_sched_prio, "CPUSCHEDPRIO" },
6294 { config_parse_exec_cpu_affinity, "CPUAFFINITY" },
6295 { config_parse_mode, "MODE" },
6296 { config_parse_unit_env_file, "FILE" },
52c239d7
LB
6297 { config_parse_exec_output, "OUTPUT" },
6298 { config_parse_exec_input, "INPUT" },
ca37242e
LP
6299 { config_parse_log_facility, "FACILITY" },
6300 { config_parse_log_level, "LEVEL" },
f975e971 6301 { config_parse_exec_secure_bits, "SECUREBITS" },
a103496c 6302 { config_parse_capability_set, "BOUNDINGSET" },
4f424df7 6303 { config_parse_rlimit, "LIMIT" },
f975e971 6304 { config_parse_unit_deps, "UNIT [...]" },
f975e971
LP
6305 { config_parse_exec, "PATH [ARGUMENT [...]]" },
6306 { config_parse_service_type, "SERVICETYPE" },
596e4470 6307 { config_parse_service_exit_type, "SERVICEEXITTYPE" },
f975e971 6308 { config_parse_service_restart, "SERVICERESTART" },
e568fea9 6309 { config_parse_service_restart_mode, "SERVICERESTARTMODE" },
bf760801 6310 { config_parse_service_timeout_failure_mode, "TIMEOUTMODE" },
f975e971 6311 { config_parse_kill_mode, "KILLMODE" },
f757855e 6312 { config_parse_signal, "SIGNAL" },
f975e971
LP
6313 { config_parse_socket_listen, "SOCKET [...]" },
6314 { config_parse_socket_bind, "SOCKETBIND" },
6315 { config_parse_socket_bindtodevice, "NETWORKINTERFACE" },
7f602784 6316 { config_parse_sec, "SECONDS" },
d88a251b 6317 { config_parse_nsec, "NANOSECONDS" },
94828d2d 6318 { config_parse_namespace_path_strv, "PATH [...]" },
d2d6c096 6319 { config_parse_bind_paths, "PATH[:PATH[:OPTIONS]] [...]" },
9e615fa3 6320 { config_parse_unit_mounts_for, "PATH [...]" },
874cdcbc 6321 { config_parse_exec_mount_propagation_flag,
f0a96d19 6322 "MOUNTFLAG" },
f975e971 6323 { config_parse_unit_string_printf, "STRING" },
3ecaa09b 6324 { config_parse_trigger_unit, "UNIT" },
f975e971 6325 { config_parse_timer, "TIMER" },
f975e971 6326 { config_parse_path_spec, "PATH" },
f975e971
LP
6327 { config_parse_notify_access, "ACCESS" },
6328 { config_parse_ip_tos, "TOS" },
6329 { config_parse_unit_condition_path, "CONDITION" },
6330 { config_parse_unit_condition_string, "CONDITION" },
a016b922 6331 { config_parse_unit_slice, "SLICE" },
7f0386f6
LP
6332 { config_parse_documentation, "URL" },
6333 { config_parse_service_timeout, "SECONDS" },
87a47f99 6334 { config_parse_emergency_action, "ACTION" },
7f0386f6
LP
6335 { config_parse_set_status, "STATUS" },
6336 { config_parse_service_sockets, "SOCKETS" },
7f0386f6 6337 { config_parse_environ, "ENVIRON" },
349cc4a5 6338#if HAVE_SECCOMP
17df7223 6339 { config_parse_syscall_filter, "SYSCALLS" },
6a6751fe 6340 { config_parse_syscall_archs, "ARCHS" },
17df7223 6341 { config_parse_syscall_errno, "ERRNO" },
9df2cdd8 6342 { config_parse_syscall_log, "SYSCALLS" },
4298d0b5 6343 { config_parse_address_families, "FAMILIES" },
add00535 6344 { config_parse_restrict_namespaces, "NAMESPACES" },
c0467cf3 6345#endif
e59ccd03 6346 { config_parse_restrict_filesystems, "FILESYSTEMS" },
7f0386f6 6347 { config_parse_cpu_shares, "SHARES" },
984faf29 6348 { config_parse_cg_weight, "WEIGHT" },
c8340822 6349 { config_parse_cg_cpu_weight, "CPUWEIGHT" },
7f0386f6
LP
6350 { config_parse_memory_limit, "LIMIT" },
6351 { config_parse_device_allow, "DEVICE" },
6352 { config_parse_device_policy, "POLICY" },
13c31542 6353 { config_parse_io_limit, "LIMIT" },
13c31542 6354 { config_parse_io_device_weight, "DEVICEWEIGHT" },
6ae4283c 6355 { config_parse_io_device_latency, "DEVICELATENCY" },
7f0386f6
LP
6356 { config_parse_blockio_bandwidth, "BANDWIDTH" },
6357 { config_parse_blockio_weight, "WEIGHT" },
6358 { config_parse_blockio_device_weight, "DEVICEWEIGHT" },
6359 { config_parse_long, "LONG" },
6360 { config_parse_socket_service, "SERVICE" },
349cc4a5 6361#if HAVE_SELINUX
6a6751fe
LP
6362 { config_parse_exec_selinux_context, "LABEL" },
6363#endif
6364 { config_parse_job_mode, "MODE" },
6365 { config_parse_job_mode_isolate, "BOOLEAN" },
4298d0b5 6366 { config_parse_personality, "PERSONALITY" },
523ea123 6367 { config_parse_log_filter_patterns, "REGEX" },
f975e971
LP
6368 };
6369
6370 const char *prev = NULL;
f975e971
LP
6371
6372 assert(f);
e537352b 6373
f975e971
LP
6374 NULSTR_FOREACH(i, load_fragment_gperf_nulstr) {
6375 const char *rvalue = "OTHER", *lvalue;
313b7856 6376 const ConfigPerfItem *p;
f975e971 6377 const char *dot;
f975e971
LP
6378
6379 assert_se(p = load_fragment_gperf_lookup(i, strlen(i)));
6380
313b7856
LP
6381 /* Hide legacy settings */
6382 if (p->parse == config_parse_warn_compat &&
6383 p->ltype == DISABLED_LEGACY)
6384 continue;
6385
601844b7 6386 for (size_t j = 0; j < ELEMENTSOF(table); j++)
313b7856
LP
6387 if (p->parse == table[j].callback) {
6388 rvalue = table[j].rvalue;
6389 break;
6390 }
6391
f975e971
LP
6392 dot = strchr(i, '.');
6393 lvalue = dot ? dot + 1 : i;
f975e971 6394
601844b7
YW
6395 if (dot) {
6396 size_t prefix_len = dot - i;
6397
641906e9 6398 if (!prev || !strneq(prev, i, prefix_len+1)) {
f975e971
LP
6399 if (prev)
6400 fputc('\n', f);
6401
6402 fprintf(f, "[%.*s]\n", (int) prefix_len, i);
6403 }
601844b7 6404 }
f975e971 6405
f975e971
LP
6406 fprintf(f, "%s=%s\n", lvalue, rvalue);
6407 prev = i;
6408 }
e537352b 6409}
a07a7324
FS
6410
6411int config_parse_cpu_affinity2(
6412 const char *unit,
6413 const char *filename,
6414 unsigned line,
6415 const char *section,
6416 unsigned section_line,
6417 const char *lvalue,
6418 int ltype,
6419 const char *rvalue,
6420 void *data,
6421 void *userdata) {
6422
99534007 6423 CPUSet *affinity = ASSERT_PTR(data);
a07a7324
FS
6424
6425 (void) parse_cpu_set_extend(rvalue, affinity, true, unit, filename, line, lvalue);
6426
6427 return 0;
6428}
6429
6430int config_parse_show_status(
6431 const char* unit,
6432 const char *filename,
6433 unsigned line,
6434 const char *section,
6435 unsigned section_line,
6436 const char *lvalue,
6437 int ltype,
6438 const char *rvalue,
6439 void *data,
6440 void *userdata) {
6441
6442 int k;
99534007 6443 ShowStatus *b = ASSERT_PTR(data);
a07a7324
FS
6444
6445 assert(filename);
6446 assert(lvalue);
6447 assert(rvalue);
a07a7324
FS
6448
6449 k = parse_show_status(rvalue, b);
323dda78
YW
6450 if (k < 0)
6451 log_syntax(unit, LOG_WARNING, filename, line, k, "Failed to parse show status setting, ignoring: %s", rvalue);
a07a7324
FS
6452
6453 return 0;
6454}
6455
6456int config_parse_output_restricted(
6457 const char* unit,
6458 const char *filename,
6459 unsigned line,
6460 const char *section,
6461 unsigned section_line,
6462 const char *lvalue,
6463 int ltype,
6464 const char *rvalue,
6465 void *data,
6466 void *userdata) {
6467
99534007 6468 ExecOutput t, *eo = ASSERT_PTR(data);
f3dc6af2 6469 bool obsolete = false;
a07a7324
FS
6470
6471 assert(filename);
6472 assert(lvalue);
6473 assert(rvalue);
a07a7324 6474
f3dc6af2
LP
6475 if (streq(rvalue, "syslog")) {
6476 t = EXEC_OUTPUT_JOURNAL;
6477 obsolete = true;
6478 } else if (streq(rvalue, "syslog+console")) {
6479 t = EXEC_OUTPUT_JOURNAL_AND_CONSOLE;
6480 obsolete = true;
6481 } else {
6482 t = exec_output_from_string(rvalue);
6483 if (t < 0) {
b98680b2 6484 log_syntax(unit, LOG_WARNING, filename, line, t, "Failed to parse output type, ignoring: %s", rvalue);
f3dc6af2
LP
6485 return 0;
6486 }
a07a7324 6487
8d7dab1f
LW
6488 if (IN_SET(t, EXEC_OUTPUT_SOCKET, EXEC_OUTPUT_NAMED_FD, EXEC_OUTPUT_FILE, EXEC_OUTPUT_FILE_APPEND, EXEC_OUTPUT_FILE_TRUNCATE)) {
6489 log_syntax(unit, LOG_WARNING, filename, line, 0, "Standard output types socket, fd:, file:, append:, truncate: are not supported as defaults, ignoring: %s", rvalue);
f3dc6af2
LP
6490 return 0;
6491 }
a07a7324
FS
6492 }
6493
f3dc6af2
LP
6494 if (obsolete)
6495 log_syntax(unit, LOG_NOTICE, filename, line, 0,
6496 "Standard output type %s is obsolete, automatically updating to %s. Please update your configuration.",
6497 rvalue, exec_output_to_string(t));
6498
a07a7324
FS
6499 *eo = t;
6500 return 0;
6501}
6502
6503int config_parse_crash_chvt(
6504 const char* unit,
6505 const char *filename,
6506 unsigned line,
6507 const char *section,
6508 unsigned section_line,
6509 const char *lvalue,
6510 int ltype,
6511 const char *rvalue,
6512 void *data,
6513 void *userdata) {
6514
6515 int r;
6516
6517 assert(filename);
6518 assert(lvalue);
6519 assert(rvalue);
6520 assert(data);
6521
6522 r = parse_crash_chvt(rvalue, data);
323dda78
YW
6523 if (r < 0)
6524 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse CrashChangeVT= setting, ignoring: %s", rvalue);
a07a7324
FS
6525
6526 return 0;
6527}
eb34a981
LP
6528
6529int config_parse_swap_priority(
6530 const char *unit,
6531 const char *filename,
6532 unsigned line,
6533 const char *section,
6534 unsigned section_line,
6535 const char *lvalue,
6536 int ltype,
6537 const char *rvalue,
6538 void *data,
6539 void *userdata) {
6540
99534007 6541 Swap *s = ASSERT_PTR(userdata);
eb34a981
LP
6542 int r, priority;
6543
eb34a981
LP
6544 assert(filename);
6545 assert(lvalue);
6546 assert(rvalue);
6547 assert(data);
6548
6549 if (isempty(rvalue)) {
6550 s->parameters_fragment.priority = -1;
6551 s->parameters_fragment.priority_set = false;
6552 return 0;
6553 }
6554
6555 r = safe_atoi(rvalue, &priority);
6556 if (r < 0) {
323dda78 6557 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid swap priority '%s', ignoring.", rvalue);
eb34a981
LP
6558 return 0;
6559 }
6560
6561 if (priority < -1) {
323dda78 6562 log_syntax(unit, LOG_WARNING, filename, line, 0, "Sorry, swap priorities smaller than -1 may only be assigned by the kernel itself, ignoring: %s", rvalue);
eb34a981
LP
6563 return 0;
6564 }
6565
6566 if (priority > 32767) {
323dda78 6567 log_syntax(unit, LOG_WARNING, filename, line, 0, "Swap priority out of range, ignoring: %s", rvalue);
eb34a981
LP
6568 return 0;
6569 }
6570
6571 s->parameters_fragment.priority = priority;
6572 s->parameters_fragment.priority_set = true;
6573 return 0;
6574}
8a85c5b6
FB
6575
6576int config_parse_watchdog_sec(
6577 const char *unit,
6578 const char *filename,
6579 unsigned line,
6580 const char *section,
6581 unsigned section_line,
6582 const char *lvalue,
6583 int ltype,
6584 const char *rvalue,
6585 void *data,
6586 void *userdata) {
6587
c91c95e6
LP
6588 usec_t *usec = data;
6589
8a85c5b6
FB
6590 assert(filename);
6591 assert(lvalue);
6592 assert(rvalue);
6593
6594 /* This is called for {Runtime,Reboot,KExec}WatchdogSec= where "default" maps to
6595 * USEC_INFINITY internally. */
6596
c91c95e6 6597 if (streq(rvalue, "default"))
8a85c5b6 6598 *usec = USEC_INFINITY;
c91c95e6
LP
6599 else if (streq(rvalue, "off"))
6600 *usec = 0;
6601 else
6602 return config_parse_sec(unit, filename, line, section, section_line, lvalue, ltype, rvalue, data, userdata);
8a85c5b6 6603
c91c95e6 6604 return 0;
8a85c5b6 6605}
51462135
DDM
6606
6607int config_parse_tty_size(
6608 const char *unit,
6609 const char *filename,
6610 unsigned line,
6611 const char *section,
6612 unsigned section_line,
6613 const char *lvalue,
6614 int ltype,
6615 const char *rvalue,
6616 void *data,
6617 void *userdata) {
6618
6619 unsigned *sz = data;
6620
6621 assert(filename);
6622 assert(lvalue);
6623 assert(rvalue);
6624
6625 if (isempty(rvalue)) {
6626 *sz = UINT_MAX;
6627 return 0;
6628 }
6629
6630 return config_parse_unsigned(unit, filename, line, section, section_line, lvalue, ltype, rvalue, data, userdata);
6631}
523ea123
QD
6632
6633int config_parse_log_filter_patterns(
6634 const char *unit,
6635 const char *filename,
6636 unsigned line,
6637 const char *section,
6638 unsigned section_line,
6639 const char *lvalue,
6640 int ltype,
6641 const char *rvalue,
6642 void *data,
6643 void *userdata) {
6644
6645 ExecContext *c = ASSERT_PTR(data);
523ea123
QD
6646 const char *pattern = ASSERT_PTR(rvalue);
6647 bool is_allowlist = true;
6648 int r;
6649
6650 assert(filename);
6651 assert(lvalue);
6652
6653 if (isempty(pattern)) {
6654 /* Empty assignment resets the lists. */
1a572fd0
DT
6655 c->log_filter_allowed_patterns = set_free_free(c->log_filter_allowed_patterns);
6656 c->log_filter_denied_patterns = set_free_free(c->log_filter_denied_patterns);
523ea123
QD
6657 return 0;
6658 }
6659
6660 if (pattern[0] == '~') {
6661 is_allowlist = false;
6662 pattern++;
6663 if (isempty(pattern))
6664 /* LogFilterPatterns=~ is not considered a valid pattern. */
6665 return log_syntax(unit, LOG_WARNING, filename, line, 0,
6666 "Regex pattern invalid, ignoring: %s=%s", lvalue, rvalue);
6667 }
6668
48d85160 6669 if (pattern_compile_and_log(pattern, 0, NULL) < 0)
523ea123
QD
6670 return 0;
6671
6672 r = set_put_strdup(is_allowlist ? &c->log_filter_allowed_patterns : &c->log_filter_denied_patterns,
6673 pattern);
6674 if (r < 0) {
6675 log_syntax(unit, LOG_WARNING, filename, line, r,
6676 "Failed to store log filtering pattern, ignoring: %s=%s", lvalue, rvalue);
6677 return 0;
6678 }
6679
6680 return 0;
6681}
cd48e23f
RP
6682
6683int config_parse_open_file(
6684 const char *unit,
6685 const char *filename,
6686 unsigned line,
6687 const char *section,
6688 unsigned section_line,
6689 const char *lvalue,
6690 int ltype,
6691 const char *rvalue,
6692 void *data,
6693 void *userdata) {
6694
6695 _cleanup_(open_file_freep) OpenFile *of = NULL;
6696 OpenFile **head = ASSERT_PTR(data);
6697 int r;
6698
6699 assert(filename);
6700 assert(lvalue);
6701 assert(rvalue);
6702
6703 if (isempty(rvalue)) {
6704 open_file_free_many(head);
6705 return 0;
6706 }
6707
6708 r = open_file_parse(rvalue, &of);
6709 if (r < 0) {
6710 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse OpenFile= setting, ignoring: %s", rvalue);
6711 return 0;
6712 }
6713
6714 LIST_APPEND(open_files, *head, TAKE_PTR(of));
6715
6716 return 0;
6717}
dc7d69b3
TM
6718
6719int config_parse_cgroup_nft_set(
6720 const char *unit,
6721 const char *filename,
6722 unsigned line,
6723 const char *section,
6724 unsigned section_line,
6725 const char *lvalue,
6726 int ltype,
6727 const char *rvalue,
6728 void *data,
6729 void *userdata) {
6730
6731 CGroupContext *c = ASSERT_PTR(data);
6732 Unit *u = ASSERT_PTR(userdata);
6733
6734 return config_parse_nft_set(unit, filename, line, section, section_line, lvalue, ltype, rvalue, &c->nft_set_context, u);
6735}