Debian Codegraph @

src/shared/bus-unit-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <paths.h>
4 #include <sys/mount.h>
5
6 #include "sd-bus.h"
7
8 #include "alloc-util.h"
9 #include "bus-common-errors.h"
10 #include "bus-error.h"
11 #include "bus-locator.h"
12 #include "bus-unit-util.h"
13 #include "bus-util.h"
14 #include "capability-list.h"
15 #include "cgroup-setup.h"
16 #include "cgroup-util.h"
17 #include "condition.h"
18 #include "constants.h"
19 #include "coredump-util.h"
20 #include "cpu-set-util.h"
21 #include "escape.h"
22 #include "exec-util.h"
23 #include "exit-status.h"
24 #include "extract-word.h"
25 #include "firewall-util.h"
26 #include "hexdecoct.h"
27 #include "hostname-util.h"
28 #include "in-addr-util.h"
29 #include "install.h"
30 #include "ioprio-util.h"
31 #include "ip-protocol-list.h"
32 #include "log.h"
33 #include "mountpoint-util.h"
34 #include "nsflags.h"
35 #include "numa-util.h"
36 #include "open-file.h"
37 #include "parse-helpers.h"
38 #include "parse-util.h"
39 #include "path-util.h"
40 #include "percent-util.h"
41 #include "pidref.h"
42 #include "process-util.h"
43 #include "rlimit-util.h"
44 #include "seccomp-util.h"
45 #include "securebits-util.h"
46 #include "signal-util.h"
47 #include "socket-util.h"
48 #include "string-util.h"
49 #include "syslog-util.h"
50 #include "time-util.h"
51 #include "unit-def.h"
52
53 int bus_parse_unit_info(sd_bus_message *message, UnitInfo *u) {
54 assert(message);
55 assert(u);
56
57 u->machine = NULL;
58
59 return sd_bus_message_read(
60 message,
61 "(ssssssouso)",
62 &u->id,
63 &u->description,
64 &u->load_state,
65 &u->active_state,
66 &u->sub_state,
67 &u->following,
68 &u->unit_path,
69 &u->job_id,
70 &u->job_type,
71 &u->job_path);
72 }
73
74 static int warn_deprecated(_unused_ sd_bus_message *m, const char *field, const char *eq) {
75 log_warning("D-Bus property %s is deprecated, ignoring assignment: %s=%s", field, field, eq);
76 return 1;
77 }
78
79 static int parse_log_error(int error, const char *field, const char *eq) {
80 if (error == -ENOMEM)
81 return log_oom();
82 if (error != 0) /* Allow SYNTHETIC_ERRNO to be used, i.e. positive values. */
83 return log_error_errno(error, "Failed to parse %s= value '%s': %m", field, eq);
84
85 /* We don't log the error value for cases where we have a general "syntax error". */
86 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
87 "Invalid syntax for %s= value: '%s'", field, eq);
88 }
89
90 #define DEFINE_BUS_APPEND_PARSE_PTR(bus_type, cast_type, type, parse_func) \
91 static int bus_append_##parse_func( \
92 sd_bus_message *m, \
93 const char *field, \
94 const char *eq) { \
95 type val; \
96 int r; \
97 \
98 r = parse_func(eq, &val); \
99 if (r < 0) \
100 return parse_log_error(r, field, eq); \
101 \
102 r = sd_bus_message_append(m, "(sv)", field, \
103 bus_type, (cast_type) val); \
104 if (r < 0) \
105 return bus_log_create_error(r); \
106 \
107 return 1; \
108 }
109
110 #define DEFINE_BUS_APPEND_PARSE(bus_type, parse_func) \
111 static int bus_append_##parse_func( \
112 sd_bus_message *m, \
113 const char *field, \
114 const char *eq) { \
115 int r; \
116 \
117 r = parse_func(eq); \
118 if (r < 0) \
119 return parse_log_error(r, field, eq); \
120 \
121 r = sd_bus_message_append(m, "(sv)", field, \
122 bus_type, (int32_t) r); \
123 if (r < 0) \
124 return bus_log_create_error(r); \
125 \
126 return 1; \
127 }
128
129 DEFINE_BUS_APPEND_PARSE("b", parse_boolean);
130 DEFINE_BUS_APPEND_PARSE("i", ioprio_class_from_string);
131 DEFINE_BUS_APPEND_PARSE("i", ip_tos_from_string);
132 DEFINE_BUS_APPEND_PARSE("i", log_facility_unshifted_from_string);
133 DEFINE_BUS_APPEND_PARSE("i", log_level_from_string);
134 DEFINE_BUS_APPEND_PARSE("i", seccomp_parse_errno_or_action);
135 DEFINE_BUS_APPEND_PARSE("i", sched_policy_from_string);
136 DEFINE_BUS_APPEND_PARSE("i", secure_bits_from_string);
137 DEFINE_BUS_APPEND_PARSE("i", signal_from_string);
138 DEFINE_BUS_APPEND_PARSE("i", parse_ip_protocol);
139 DEFINE_BUS_APPEND_PARSE("i", mpol_from_string);
140 DEFINE_BUS_APPEND_PARSE_PTR("i", int32_t, int, ioprio_parse_priority);
141 DEFINE_BUS_APPEND_PARSE_PTR("i", int32_t, int, parse_nice);
142 DEFINE_BUS_APPEND_PARSE_PTR("i", int32_t, int, safe_atoi);
143 DEFINE_BUS_APPEND_PARSE_PTR("t", uint64_t, nsec_t, parse_nsec);
144 DEFINE_BUS_APPEND_PARSE_PTR("t", uint64_t, uint64_t, cg_weight_parse);
145 DEFINE_BUS_APPEND_PARSE_PTR("t", uint64_t, uint64_t, cg_cpu_weight_parse);
146 DEFINE_BUS_APPEND_PARSE_PTR("t", uint64_t, unsigned long, mount_propagation_flag_from_string);
147 DEFINE_BUS_APPEND_PARSE_PTR("t", uint64_t, uint64_t, safe_atou64);
148 DEFINE_BUS_APPEND_PARSE_PTR("u", uint32_t, mode_t, parse_mode);
149 DEFINE_BUS_APPEND_PARSE_PTR("u", uint32_t, unsigned, safe_atou);
150 DEFINE_BUS_APPEND_PARSE_PTR("x", int64_t, int64_t, safe_atoi64);
151 DEFINE_BUS_APPEND_PARSE_PTR("t", uint64_t, uint64_t, coredump_filter_mask_from_string);
152
153 static int bus_append_string(sd_bus_message *m, const char *field, const char *eq) {
154 int r;
155
156 r = sd_bus_message_append(m, "(sv)", field, "s", eq);
157 if (r < 0)
158 return bus_log_create_error(r);
159
160 return 1;
161 }
162
163 static int bus_append_strv_full(sd_bus_message *m, const char *field, const char *eq, const char *separators, ExtractFlags flags) {
164 int r;
165
166 assert(m);
167 assert(field);
168
169 r = sd_bus_message_open_container(m, 'r', "sv");
170 if (r < 0)
171 return bus_log_create_error(r);
172
173 r = sd_bus_message_append_basic(m, 's', field);
174 if (r < 0)
175 return bus_log_create_error(r);
176
177 r = sd_bus_message_open_container(m, 'v', "as");
178 if (r < 0)
179 return bus_log_create_error(r);
180
181 r = sd_bus_message_open_container(m, 'a', "s");
182 if (r < 0)
183 return bus_log_create_error(r);
184
185 for (const char *p = eq;;) {
186 _cleanup_free_ char *word = NULL;
187
188 r = extract_first_word(&p, &word, separators, flags);
189 if (r < 0)
190 return parse_log_error(r, field, eq);
191 if (r == 0)
192 break;
193
194 r = sd_bus_message_append_basic(m, 's', word);
195 if (r < 0)
196 return bus_log_create_error(r);
197 }
198
199 r = sd_bus_message_close_container(m);
200 if (r < 0)
201 return bus_log_create_error(r);
202
203 r = sd_bus_message_close_container(m);
204 if (r < 0)
205 return bus_log_create_error(r);
206
207 r = sd_bus_message_close_container(m);
208 if (r < 0)
209 return bus_log_create_error(r);
210
211 return 1;
212 }
213
214 static int bus_append_strv(sd_bus_message *m, const char *field, const char *eq) {
215 return bus_append_strv_full(m, field, eq, /* separators= */ NULL, EXTRACT_UNQUOTE);
216 }
217
218 static int bus_append_strv_cunescape(sd_bus_message *m, const char *field, const char *eq) {
219 return bus_append_strv_full(m, field, eq, /* separators= */ NULL, EXTRACT_UNQUOTE | EXTRACT_CUNESCAPE);
220 }
221
222 static int bus_append_strv_colon(sd_bus_message *m, const char *field, const char *eq) {
223 /* This also accepts colon as the separator. */
224 return bus_append_strv_full(m, field, eq, ":" WHITESPACE, EXTRACT_UNQUOTE);
225 }
226
227 static int bus_append_byte_array(sd_bus_message *m, const char *field, const void *buf, size_t n) {
228 int r;
229
230 r = sd_bus_message_open_container(m, SD_BUS_TYPE_STRUCT, "sv");
231 if (r < 0)
232 return bus_log_create_error(r);
233
234 r = sd_bus_message_append_basic(m, SD_BUS_TYPE_STRING, field);
235 if (r < 0)
236 return bus_log_create_error(r);
237
238 r = sd_bus_message_open_container(m, 'v', "ay");
239 if (r < 0)
240 return bus_log_create_error(r);
241
242 r = sd_bus_message_append_array(m, 'y', buf, n);
243 if (r < 0)
244 return bus_log_create_error(r);
245
246 r = sd_bus_message_close_container(m);
247 if (r < 0)
248 return bus_log_create_error(r);
249
250 r = sd_bus_message_close_container(m);
251 if (r < 0)
252 return bus_log_create_error(r);
253
254 return 1;
255 }
256
257 static int bus_append_parse_sec_rename(sd_bus_message *m, const char *field, const char *eq) {
258 char *n;
259 usec_t t;
260 size_t l;
261 int r;
262
263 r = parse_sec(eq, &t);
264 if (r < 0)
265 return parse_log_error(r, field, eq);
266
267 l = strlen(field);
268 n = newa(char, l + 2);
269 /* Change suffix Sec → USec */
270 strcpy(mempcpy(n, field, l - 3), "USec");
271
272 r = sd_bus_message_append(m, "(sv)", n, "t", t);
273 if (r < 0)
274 return bus_log_create_error(r);
275
276 return 1;
277 }
278
279 static int bus_append_parse_sec_rename_infinity(sd_bus_message *m, const char *field, const char *eq) {
280 return bus_append_parse_sec_rename(m, field, isempty(eq) ? "infinity" : eq);
281 }
282
283 static int bus_append_parse_size(sd_bus_message *m, const char *field, const char *eq) {
284 uint64_t v;
285 int r;
286
287 r = parse_size(eq, /* base= */ 1024, &v);
288 if (r < 0)
289 return parse_log_error(r, field, eq);
290
291 r = sd_bus_message_append(m, "(sv)", field, "t", v);
292 if (r < 0)
293 return bus_log_create_error(r);
294
295 return 1;
296 }
297
298 static int bus_append_parse_permyriad(sd_bus_message *m, const char *field, const char *eq) {
299 int r;
300
301 r = parse_permyriad(eq);
302 if (r < 0)
303 return parse_log_error(r, field, eq);
304
305 /* Pass around scaled to 2^32-1 == 100% */
306 r = sd_bus_message_append(m, "(sv)", field, "u", UINT32_SCALE_FROM_PERMYRIAD(r));
307 if (r < 0)
308 return bus_log_create_error(r);
309
310 return 1;
311 }
312
313 static int bus_append_parse_cpu_set(sd_bus_message *m, const char *field, const char *eq) {
314 _cleanup_(cpu_set_done) CPUSet cpuset = {};
315 _cleanup_free_ uint8_t *array = NULL;
316 size_t allocated;
317 int r;
318
319 r = parse_cpu_set(eq, &cpuset);
320 if (r < 0)
321 return parse_log_error(r, field, eq);
322
323 r = cpu_set_to_dbus(&cpuset, &array, &allocated);
324 if (r < 0)
325 return log_error_errno(r, "Failed to serialize %s: %m", field);
326
327 return bus_append_byte_array(m, field, array, allocated);
328 }
329
330 static int bus_append_parse_delegate(sd_bus_message *m, const char *field, const char *eq) {
331 int r;
332
333 r = parse_boolean(eq);
334 if (r < 0)
335 return bus_append_strv(m, "DelegateControllers", eq);
336
337 r = sd_bus_message_append(m, "(sv)", "Delegate", "b", r);
338 if (r < 0)
339 return bus_log_create_error(r);
340
341 return 1;
342 }
343
344 static int bus_append_parse_resource_limit(sd_bus_message *m, const char *field, const char *eq) {
345 int r;
346
347 if (isempty(eq) || streq(eq, "infinity")) {
348 uint64_t x = streq(eq, "infinity") ? CGROUP_LIMIT_MAX :
349 STR_IN_SET(field,
350 "MemoryLow",
351 "MemoryMin") ? CGROUP_LIMIT_MIN : CGROUP_LIMIT_MAX;
352
353 r = sd_bus_message_append(m, "(sv)", field, "t", x);
354 if (r < 0)
355 return bus_log_create_error(r);
356
357 return 1;
358 }
359
360 r = parse_permyriad(eq);
361 if (r >= 0) {
362 char *n;
363
364 /* When this is a percentage we'll convert this into a relative value in the range
365 * 0…UINT32_MAX and pass it in the MemoryLowScale property (and related ones). This
366 * way the physical memory size can be determined server-side. */
367
368 n = strjoina(field, "Scale");
369 r = sd_bus_message_append(m, "(sv)", n, "u", UINT32_SCALE_FROM_PERMYRIAD(r));
370 if (r < 0)
371 return bus_log_create_error(r);
372
373 return 1;
374 }
375
376 if (streq(field, "TasksMax"))
377 return bus_append_safe_atou64(m, field, eq);
378
379 return bus_append_parse_size(m, field, eq);
380 }
381
382 static int bus_append_parse_cpu_quota(sd_bus_message *m, const char *field, const char *eq) {
383 uint64_t x;
384 int r;
385
386 if (isempty(eq))
387 x = USEC_INFINITY;
388 else {
389 r = parse_permyriad_unbounded(eq);
390 if (r < 0)
391 return parse_log_error(r, field, eq);
392 if (r == 0)
393 return parse_log_error(SYNTHETIC_ERRNO(ERANGE), field, eq);
394 x = r * USEC_PER_SEC / 10000U;
395 }
396
397 r = sd_bus_message_append(m, "(sv)", "CPUQuotaPerSecUSec", "t", x);
398 if (r < 0)
399 return bus_log_create_error(r);
400
401 return 1;
402 }
403
404 static int bus_append_parse_device_allow(sd_bus_message *m, const char *field, const char *eq) {
405 int r;
406
407 if (isempty(eq))
408 r = sd_bus_message_append(m, "(sv)", field, "a(ss)", 0);
409 else {
410 _cleanup_free_ char *_path = NULL;
411 const char *path = eq, *rwm = NULL, *e;
412
413 e = strchr(eq, ' ');
414 if (e) {
415 path = _path = strndup(eq, e - eq);
416 if (!path)
417 return log_oom();
418
419 rwm = e + 1;
420 }
421
422 r = sd_bus_message_append(m, "(sv)", field, "a(ss)", 1, path, strempty(rwm));
423 }
424 if (r < 0)
425 return bus_log_create_error(r);
426
427 return 1;
428 }
429
430 static int bus_try_append_parse_cgroup_io_limit(sd_bus_message *m, const char *field, const char *eq) {
431 int r;
432
433 if (cgroup_io_limit_type_from_string(field) < 0)
434 return 0;
435
436 if (isempty(eq))
437 r = sd_bus_message_append(m, "(sv)", field, "a(st)", 0);
438 else {
439 const char *e = strchr(eq, ' ');
440 if (!e)
441 return parse_log_error(0, field, eq);
442
443 const char *bandwidth = e + 1;
444 _cleanup_free_ char *path = strndup(eq, e - eq);
445 if (!path)
446 return log_oom();
447
448 uint64_t bytes;
449 if (streq(bandwidth, "infinity"))
450 bytes = CGROUP_LIMIT_MAX;
451 else {
452 r = parse_size(bandwidth, 1000, &bytes);
453 if (r < 0)
454 return parse_log_error(r, field, eq);
455 }
456
457 r = sd_bus_message_append(m, "(sv)", field, "a(st)", 1, path, bytes);
458 }
459 if (r < 0)
460 return bus_log_create_error(r);
461
462 return 1;
463 }
464
465 static int bus_append_parse_io_device_weight(sd_bus_message *m, const char *field, const char *eq) {
466 int r;
467
468 if (isempty(eq))
469 r = sd_bus_message_append(m, "(sv)", field, "a(st)", 0);
470 else {
471 const char *e = strchr(eq, ' ');
472 if (!e)
473 return parse_log_error(0, field, eq);
474
475 const char *weight = e + 1;
476 _cleanup_free_ char *path = strndup(eq, e - eq);
477 if (!path)
478 return log_oom();
479
480 uint64_t u;
481 r = safe_atou64(weight, &u);
482 if (r < 0)
483 return parse_log_error(r, field, weight);
484
485 r = sd_bus_message_append(m, "(sv)", field, "a(st)", 1, path, u);
486 }
487 if (r < 0)
488 return bus_log_create_error(r);
489
490 return 1;
491 }
492
493 static int bus_append_parse_io_device_latency(sd_bus_message *m, const char *field, const char *eq) {
494 const char *field_usec = "IODeviceLatencyTargetUSec";
495 int r;
496
497 if (isempty(eq))
498 r = sd_bus_message_append(m, "(sv)", field_usec, "a(st)", 0);
499 else {
500 const char *e = strchr(eq, ' ');
501 if (!e)
502 return parse_log_error(0, field, eq);
503
504 const char *target = e + 1;
505 _cleanup_free_ char *path = strndup(eq, e - eq);
506 if (!path)
507 return log_oom();
508
509 usec_t usec;
510 r = parse_sec(target, &usec);
511 if (r < 0)
512 return parse_log_error(r, field, target);
513
514 r = sd_bus_message_append(m, "(sv)", field_usec, "a(st)", 1, path, usec);
515 }
516 if (r < 0)
517 return bus_log_create_error(r);
518
519 return 1;
520 }
521
522 static int bus_append_bpf_program(sd_bus_message *m, const char *field, const char *eq) {
523 int r;
524
525 if (isempty(eq))
526 r = sd_bus_message_append(m, "(sv)", field, "a(ss)", 0);
527 else {
528 _cleanup_free_ char *word = NULL;
529
530 r = extract_first_word(&eq, &word, ":", 0);
531 if (r < 0)
532 return parse_log_error(r, field, eq);
533
534 r = sd_bus_message_append(m, "(sv)", field, "a(ss)", 1, word, eq);
535 }
536 if (r < 0)
537 return bus_log_create_error(r);
538
539 return 1;
540 }
541
542 static int bus_append_socket_filter(sd_bus_message *m, const char *field, const char *eq) {
543 int r;
544
545 if (isempty(eq))
546 r = sd_bus_message_append(m, "(sv)", field, "a(iiqq)", 0);
547 else {
548 int32_t family, ip_protocol;
549 uint16_t nr_ports, port_min;
550
551 r = parse_socket_bind_item(eq, &family, &ip_protocol, &nr_ports, &port_min);
552 if (r < 0)
553 return parse_log_error(r, field, eq);
554
555 r = sd_bus_message_append(
556 m, "(sv)", field, "a(iiqq)", 1, family, ip_protocol, nr_ports, port_min);
557 }
558 if (r < 0)
559 return bus_log_create_error(r);
560
561 return 1;
562 }
563
564 static int bus_append_exec_command(sd_bus_message *m, const char *field, const char *eq) {
565 bool explicit_path = false, done = false, ambient_hack = false;
566 _cleanup_strv_free_ char **cmdline = NULL, **ex_opts = NULL;
567 _cleanup_free_ char *_path = NULL;
568 ExecCommandFlags flags = 0;
569 int r;
570
571 do {
572 switch (*eq) {
573
574 case '-':
575 if (FLAGS_SET(flags, EXEC_COMMAND_IGNORE_FAILURE))
576 done = true;
577 else {
578 flags |= EXEC_COMMAND_IGNORE_FAILURE;
579 eq++;
580 }
581 break;
582
583 case '@':
584 if (explicit_path)
585 done = true;
586 else {
587 explicit_path = true;
588 eq++;
589 }
590 break;
591
592 case ':':
593 if (FLAGS_SET(flags, EXEC_COMMAND_NO_ENV_EXPAND))
594 done = true;
595 else {
596 flags |= EXEC_COMMAND_NO_ENV_EXPAND;
597 eq++;
598 }
599 break;
600
601 case '+':
602 if ((flags & (EXEC_COMMAND_FULLY_PRIVILEGED|EXEC_COMMAND_NO_SETUID)) != 0 || ambient_hack)
603 done = true;
604 else {
605 flags |= EXEC_COMMAND_FULLY_PRIVILEGED;
606 eq++;
607 }
608 break;
609
610 case '!':
611 if (FLAGS_SET(flags, EXEC_COMMAND_FULLY_PRIVILEGED) || ambient_hack)
612 done = true;
613 else if (FLAGS_SET(flags, EXEC_COMMAND_NO_SETUID)) {
614 /* Compatibility with the old !! ambient caps hack (removed in v258). Since
615 * we don't support that anymore and !! was a noop on non-supporting systems,
616 * we'll just turn off the EXEC_COMMAND_NO_SETUID flag again and be done with
617 * it. */
618 flags &= ~EXEC_COMMAND_NO_SETUID;
619 eq++;
620 ambient_hack = true;
621
622 log_notice("!! modifier for %s= fields is no longer supported and is now ignored.", field);
623 } else {
624 flags |= EXEC_COMMAND_NO_SETUID;
625 eq++;
626 }
627 break;
628
629 case '|':
630 if (FLAGS_SET(flags, EXEC_COMMAND_VIA_SHELL))
631 done = true;
632 else {
633 flags |= EXEC_COMMAND_VIA_SHELL;
634 eq++;
635 }
636 break;
637
638 default:
639 done = true;
640 }
641 } while (!done);
642
643 bool ex_prop = flags & (EXEC_COMMAND_NO_ENV_EXPAND|EXEC_COMMAND_FULLY_PRIVILEGED|EXEC_COMMAND_NO_SETUID|EXEC_COMMAND_VIA_SHELL);
644 if (ex_prop) {
645 /* We need to use ExecXYZEx=. */
646 if (!endswith(field, "Ex"))
647 field = strjoina(field, "Ex");
648
649 r = exec_command_flags_to_strv(flags, &ex_opts);
650 if (r < 0)
651 return log_error_errno(r, "Failed to serialize ExecCommand flags: %m");
652 } else {
653 if (endswith(field, "Ex"))
654 field = strndupa_safe(field, strlen(field) - 2);
655 }
656
657 const char *path = NULL;
658 if (FLAGS_SET(flags, EXEC_COMMAND_VIA_SHELL))
659 path = _PATH_BSHELL;
660 else if (explicit_path) {
661 r = extract_first_word(&eq, &_path, NULL, EXTRACT_UNQUOTE|EXTRACT_CUNESCAPE);
662 if (r < 0)
663 return parse_log_error(r, field, eq);
664 if (r == 0)
665 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No executable path specified for %s=, refusing.", field);
666 if (isempty(eq))
667 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Got empty command line for %s=, refusing.", field);
668 path = _path;
669 }
670
671 r = strv_split_full(&cmdline, eq, NULL, EXTRACT_UNQUOTE|EXTRACT_CUNESCAPE);
672 if (r < 0)
673 return parse_log_error(r, field, eq);
674
675 if (FLAGS_SET(flags, EXEC_COMMAND_VIA_SHELL)) {
676 r = strv_prepend(&cmdline, explicit_path ? "-sh" : "sh");
677 if (r < 0)
678 return log_oom();
679 }
680
681 r = sd_bus_message_open_container(m, SD_BUS_TYPE_STRUCT, "sv");
682 if (r < 0)
683 return bus_log_create_error(r);
684
685 r = sd_bus_message_append_basic(m, SD_BUS_TYPE_STRING, field);
686 if (r < 0)
687 return bus_log_create_error(r);
688
689 r = sd_bus_message_open_container(m, 'v', ex_prop ? "a(sasas)" : "a(sasb)");
690 if (r < 0)
691 return bus_log_create_error(r);
692
693 r = sd_bus_message_open_container(m, 'a', ex_prop ? "(sasas)" : "(sasb)");
694 if (r < 0)
695 return bus_log_create_error(r);
696
697 if (!strv_isempty(cmdline)) {
698 r = sd_bus_message_open_container(m, 'r', ex_prop ? "sasas" : "sasb");
699 if (r < 0)
700 return bus_log_create_error(r);
701
702 r = sd_bus_message_append(m, "s", path ?: cmdline[0]);
703 if (r < 0)
704 return bus_log_create_error(r);
705
706 r = sd_bus_message_append_strv(m, cmdline);
707 if (r < 0)
708 return bus_log_create_error(r);
709
710 r = ex_prop ? sd_bus_message_append_strv(m, ex_opts) :
711 sd_bus_message_append(m, "b", FLAGS_SET(flags, EXEC_COMMAND_IGNORE_FAILURE));
712 if (r < 0)
713 return bus_log_create_error(r);
714
715 r = sd_bus_message_close_container(m);
716 if (r < 0)
717 return bus_log_create_error(r);
718 }
719
720 r = sd_bus_message_close_container(m);
721 if (r < 0)
722 return bus_log_create_error(r);
723
724 r = sd_bus_message_close_container(m);
725 if (r < 0)
726 return bus_log_create_error(r);
727
728 r = sd_bus_message_close_container(m);
729 if (r < 0)
730 return bus_log_create_error(r);
731
732 return 1;
733 }
734
735 static int bus_append_open_file(sd_bus_message *m, const char *field, const char *eq) {
736 _cleanup_(open_file_freep) OpenFile *of = NULL;
737 int r;
738
739 assert(m);
740
741 r = open_file_parse(eq, &of);
742 if (r < 0)
743 return parse_log_error(r, field, eq);
744
745 r = sd_bus_message_append(m, "(sv)", field, "a(sst)", (size_t) 1, of->path, of->fdname, of->flags);
746 if (r < 0)
747 return bus_log_create_error(r);
748
749 return 1;
750 }
751
752 static int bus_append_ip_address_access(sd_bus_message *m, int family, const union in_addr_union *prefix, unsigned char prefixlen) {
753 int r;
754
755 assert(m);
756 assert(prefix);
757
758 r = sd_bus_message_open_container(m, 'r', "iayu");
759 if (r < 0)
760 return r;
761
762 r = sd_bus_message_append(m, "i", family);
763 if (r < 0)
764 return r;
765
766 r = sd_bus_message_append_array(m, 'y', prefix, FAMILY_ADDRESS_SIZE(family));
767 if (r < 0)
768 return r;
769
770 r = sd_bus_message_append(m, "u", prefixlen);
771 if (r < 0)
772 return r;
773
774 return sd_bus_message_close_container(m);
775 }
776
777 static int bus_append_parse_ip_address_filter(sd_bus_message *m, const char *field, const char *eq) {
778 union in_addr_union prefix = {};
779 unsigned char prefixlen;
780 int family, r;
781
782 if (isempty(eq)) {
783 r = sd_bus_message_append(m, "(sv)", field, "a(iayu)", 0);
784 if (r < 0)
785 return bus_log_create_error(r);
786
787 return 1;
788 }
789
790 r = sd_bus_message_open_container(m, SD_BUS_TYPE_STRUCT, "sv");
791 if (r < 0)
792 return bus_log_create_error(r);
793
794 r = sd_bus_message_append_basic(m, SD_BUS_TYPE_STRING, field);
795 if (r < 0)
796 return bus_log_create_error(r);
797
798 r = sd_bus_message_open_container(m, 'v', "a(iayu)");
799 if (r < 0)
800 return bus_log_create_error(r);
801
802 r = sd_bus_message_open_container(m, 'a', "(iayu)");
803 if (r < 0)
804 return bus_log_create_error(r);
805
806 if (streq(eq, "any")) {
807 /* "any" is a shortcut for 0.0.0.0/0 and ::/0 */
808
809 r = bus_append_ip_address_access(m, AF_INET, &prefix, 0);
810 if (r < 0)
811 return bus_log_create_error(r);
812
813 r = bus_append_ip_address_access(m, AF_INET6, &prefix, 0);
814 if (r < 0)
815 return bus_log_create_error(r);
816
817 } else if (is_localhost(eq)) {
818 /* "localhost" is a shortcut for 127.0.0.0/8 and ::1/128 */
819
820 prefix.in.s_addr = htobe32(0x7f000000);
821 r = bus_append_ip_address_access(m, AF_INET, &prefix, 8);
822 if (r < 0)
823 return bus_log_create_error(r);
824
825 prefix.in6 = (struct in6_addr) IN6ADDR_LOOPBACK_INIT;
826 r = bus_append_ip_address_access(m, AF_INET6, &prefix, 128);
827 if (r < 0)
828 return r;
829
830 } else if (streq(eq, "link-local")) {
831 /* "link-local" is a shortcut for 169.254.0.0/16 and fe80::/64 */
832
833 prefix.in.s_addr = htobe32((UINT32_C(169) << 24 | UINT32_C(254) << 16));
834 r = bus_append_ip_address_access(m, AF_INET, &prefix, 16);
835 if (r < 0)
836 return bus_log_create_error(r);
837
838 prefix.in6 = (struct in6_addr) {
839 .s6_addr32[0] = htobe32(0xfe800000)
840 };
841 r = bus_append_ip_address_access(m, AF_INET6, &prefix, 64);
842 if (r < 0)
843 return bus_log_create_error(r);
844
845 } else if (streq(eq, "multicast")) {
846 /* "multicast" is a shortcut for 224.0.0.0/4 and ff00::/8 */
847
848 prefix.in.s_addr = htobe32((UINT32_C(224) << 24));
849 r = bus_append_ip_address_access(m, AF_INET, &prefix, 4);
850 if (r < 0)
851 return bus_log_create_error(r);
852
853 prefix.in6 = (struct in6_addr) {
854 .s6_addr32[0] = htobe32(0xff000000)
855 };
856 r = bus_append_ip_address_access(m, AF_INET6, &prefix, 8);
857 if (r < 0)
858 return bus_log_create_error(r);
859
860 } else
861 for (;;) {
862 _cleanup_free_ char *word = NULL;
863
864 r = extract_first_word(&eq, &word, NULL, 0);
865 if (r < 0)
866 return parse_log_error(r, field, eq);
867 if (r == 0)
868 break;
869
870 r = in_addr_prefix_from_string_auto(word, &family, &prefix, &prefixlen);
871 if (r < 0)
872 return log_error_errno(r, "Failed to parse IP address prefix '%s': %m", word);
873
874 r = bus_append_ip_address_access(m, family, &prefix, prefixlen);
875 if (r < 0)
876 return bus_log_create_error(r);
877 }
878
879 r = sd_bus_message_close_container(m);
880 if (r < 0)
881 return bus_log_create_error(r);
882
883 r = sd_bus_message_close_container(m);
884 if (r < 0)
885 return bus_log_create_error(r);
886
887 r = sd_bus_message_close_container(m);
888 if (r < 0)
889 return bus_log_create_error(r);
890
891 return 1;
892 }
893
894 #define bus_append_trivial_array(m, field, eq, types, ...) \
895 ({ \
896 int r; \
897 \
898 if (isempty(eq)) \
899 r = sd_bus_message_append(m, "(sv)", field, types, 0); \
900 else \
901 r = sd_bus_message_append(m, "(sv)", field, types, 1, __VA_ARGS__); \
902 r < 0 ? bus_log_create_error(r) : 1; \
903 })
904
905 static int bus_append_ip_filter_path(sd_bus_message *m, const char *field, const char *eq) {
906 return bus_append_trivial_array(m, field, eq,
907 "as", eq);
908 }
909
910 static int bus_append_nft_set(sd_bus_message *m, const char *field, const char *eq) {
911 int r;
912
913 assert(m);
914 assert(field);
915 assert(eq);
916
917 if (isempty(eq)) {
918 r = sd_bus_message_append(m, "(sv)", field, "a(iiss)", 0);
919 if (r < 0)
920 return bus_log_create_error(r);
921
922 return 1;
923 }
924
925 r = sd_bus_message_open_container(m, SD_BUS_TYPE_STRUCT, "sv");
926 if (r < 0)
927 return bus_log_create_error(r);
928
929 r = sd_bus_message_append_basic(m, SD_BUS_TYPE_STRING, field);
930 if (r < 0)
931 return bus_log_create_error(r);
932
933 r = sd_bus_message_open_container(m, 'v', "a(iiss)");
934 if (r < 0)
935 return bus_log_create_error(r);
936
937 r = sd_bus_message_open_container(m, 'a', "(iiss)");
938 if (r < 0)
939 return bus_log_create_error(r);
940
941 for (const char *p = eq;;) {
942 _cleanup_free_ char *tuple = NULL, *source_str = NULL, *nfproto_str = NULL, *table = NULL, *set = NULL;
943 const char *q = NULL;
944 int source, nfproto;
945
946 r = extract_first_word(&p, &tuple, NULL, EXTRACT_UNQUOTE|EXTRACT_RETAIN_ESCAPE);
947 if (r < 0)
948 return parse_log_error(r, field, eq);
949 if (r == 0)
950 break;
951 if (isempty(tuple))
952 return parse_log_error(0, field, eq);
953
954 q = tuple;
955 r = extract_many_words(&q, ":", EXTRACT_CUNESCAPE, &source_str, &nfproto_str, &table, &set);
956 if (r != 4 || !isempty(q))
957 return parse_log_error(0, field, tuple);
958
959 assert(source_str);
960 assert(nfproto_str);
961 assert(table);
962 assert(set);
963
964 source = r = nft_set_source_from_string(source_str);
965 if (r < 0)
966 return log_error_errno(r, "Failed to parse NFT set source '%s': %m", source_str);
967 if (!IN_SET(source, NFT_SET_SOURCE_CGROUP, NFT_SET_SOURCE_USER, NFT_SET_SOURCE_GROUP))
968 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Bad NFT set source value '%s'.",
969 nft_set_source_to_string(source));
970
971 nfproto = r = nfproto_from_string(nfproto_str);
972 if (r < 0)
973 return log_error_errno(r, "Failed to parse nft protocol '%s': %m", nfproto_str);
974
975 if (!nft_identifier_valid(table))
976 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Bad NFT identifier name '%s'.", table);
977 if (!nft_identifier_valid(set))
978 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Bad NFT identifier name '%s'.", set);
979
980 r = sd_bus_message_append(m, "(iiss)", source, nfproto, table, set);
981 if (r < 0)
982 return bus_log_create_error(r);
983 }
984 r = sd_bus_message_close_container(m);
985 if (r < 0)
986 return bus_log_create_error(r);
987
988 r = sd_bus_message_close_container(m);
989 if (r < 0)
990 return bus_log_create_error(r);
991
992 r = sd_bus_message_close_container(m);
993 if (r < 0)
994 return bus_log_create_error(r);
995
996 return 1;
997 }
998
999 static int bus_append_environment_files(sd_bus_message *m, const char *field, const char *eq) {
1000 return bus_append_trivial_array(m, "EnvironmentFiles", eq,
1001 "a(sb)",
1002 eq[0] == '-' ? eq + 1 : eq,
1003 eq[0] == '-');
1004 }
1005
1006 static int bus_append_set_credential(sd_bus_message *m, const char *field, const char *eq) {
1007 int r;
1008
1009 r = sd_bus_message_open_container(m, 'r', "sv");
1010 if (r < 0)
1011 return bus_log_create_error(r);
1012
1013 r = sd_bus_message_append_basic(m, 's', field);
1014 if (r < 0)
1015 return bus_log_create_error(r);
1016
1017 r = sd_bus_message_open_container(m, 'v', "a(say)");
1018 if (r < 0)
1019 return bus_log_create_error(r);
1020
1021 if (isempty(eq))
1022 r = sd_bus_message_append(m, "a(say)", 0);
1023 else {
1024 _cleanup_free_ char *word = NULL;
1025 const char *p = eq;
1026
1027 r = extract_first_word(&p, &word, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
1028 if (r <= 0 || !p)
1029 return parse_log_error(r < 0 ? r : 0, field, eq);
1030
1031 r = sd_bus_message_open_container(m, 'a', "(say)");
1032 if (r < 0)
1033 return bus_log_create_error(r);
1034
1035 r = sd_bus_message_open_container(m, 'r', "say");
1036 if (r < 0)
1037 return bus_log_create_error(r);
1038
1039 r = sd_bus_message_append(m, "s", word);
1040 if (r < 0)
1041 return bus_log_create_error(r);
1042
1043 if (endswith(field, "Encrypted")) {
1044 _cleanup_free_ void *decoded = NULL;
1045 size_t decoded_size;
1046
1047 r = unbase64mem(p, &decoded, &decoded_size);
1048 if (r < 0)
1049 return log_error_errno(r, "Failed to decode base64 data for %s=: %m", field);
1050
1051 r = sd_bus_message_append_array(m, 'y', decoded, decoded_size);
1052 } else {
1053 _cleanup_free_ char *unescaped = NULL;
1054 ssize_t l;
1055
1056 l = cunescape(p, UNESCAPE_ACCEPT_NUL, &unescaped);
1057 if (l < 0)
1058 return log_error_errno(l, "Failed to unescape value for %s=: %s", field, p);
1059
1060 r = sd_bus_message_append_array(m, 'y', unescaped, l);
1061 }
1062 if (r < 0)
1063 return bus_log_create_error(r);
1064
1065 r = sd_bus_message_close_container(m);
1066 if (r < 0)
1067 return bus_log_create_error(r);
1068
1069 r = sd_bus_message_close_container(m);
1070 }
1071 if (r < 0)
1072 return bus_log_create_error(r);
1073
1074 r = sd_bus_message_close_container(m);
1075 if (r < 0)
1076 return bus_log_create_error(r);
1077
1078 r = sd_bus_message_close_container(m);
1079 if (r < 0)
1080 return bus_log_create_error(r);
1081
1082 return 1;
1083 }
1084
1085 static int bus_append_load_credential(sd_bus_message *m, const char *field, const char *eq) {
1086 int r;
1087
1088 r = sd_bus_message_open_container(m, 'r', "sv");
1089 if (r < 0)
1090 return bus_log_create_error(r);
1091
1092 r = sd_bus_message_append_basic(m, 's', field);
1093 if (r < 0)
1094 return bus_log_create_error(r);
1095
1096 r = sd_bus_message_open_container(m, 'v', "a(ss)");
1097 if (r < 0)
1098 return bus_log_create_error(r);
1099
1100 if (isempty(eq))
1101 r = sd_bus_message_append(m, "a(ss)", 0);
1102 else {
1103 _cleanup_free_ char *word = NULL;
1104 const char *p = eq;
1105
1106 r = extract_first_word(&p, &word, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
1107 if (r <= 0)
1108 return parse_log_error(r, field, eq);
1109
1110 if (isempty(p)) /* If only one field is specified, then this means "inherit from above" */
1111 p = eq;
1112
1113 r = sd_bus_message_append(m, "a(ss)", 1, word, p);
1114 }
1115 if (r < 0)
1116 return bus_log_create_error(r);
1117
1118 r = sd_bus_message_close_container(m);
1119 if (r < 0)
1120 return bus_log_create_error(r);
1121
1122 r = sd_bus_message_close_container(m);
1123 if (r < 0)
1124 return bus_log_create_error(r);
1125
1126 return 1;
1127 }
1128
1129 static int bus_append_import_credential(sd_bus_message *m, const char *field, const char *eq) {
1130 int r;
1131
1132 if (isempty(eq))
1133 r = sd_bus_message_append(m, "(sv)", "ImportCredential", "as", 0);
1134 else {
1135 _cleanup_free_ char *word = NULL;
1136 const char *p = eq;
1137
1138 r = extract_first_word(&p, &word, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
1139 if (r <= 0)
1140 return parse_log_error(r, field, eq);
1141
1142 if (!p)
1143 r = sd_bus_message_append(m, "(sv)", "ImportCredential", "as", 1, eq);
1144 else {
1145 /* We need to send ImportCredentialEx */
1146 r = sd_bus_message_open_container(m, 'r', "sv");
1147 if (r < 0)
1148 return bus_log_create_error(r);
1149
1150 r = sd_bus_message_append_basic(m, 's', "ImportCredentialEx");
1151 if (r < 0)
1152 return bus_log_create_error(r);
1153
1154 r = sd_bus_message_open_container(m, 'v', "a(ss)");
1155 if (r < 0)
1156 return bus_log_create_error(r);
1157
1158 r = sd_bus_message_append(m, "a(ss)", 1, word, p);
1159 if (r < 0)
1160 return bus_log_create_error(r);
1161
1162 r = sd_bus_message_close_container(m);
1163 if (r < 0)
1164 return bus_log_create_error(r);
1165
1166 r = sd_bus_message_close_container(m);
1167 }
1168 }
1169 if (r < 0)
1170 return bus_log_create_error(r);
1171
1172 return 1;
1173 }
1174
1175 static int bus_append_refresh_on_reload(sd_bus_message *m, const char *field, const char *eq) {
1176 int r;
1177
1178 assert(eq);
1179
1180 r = sd_bus_message_open_container(m, 'r', "sv");
1181 if (r < 0)
1182 return bus_log_create_error(r);
1183
1184 r = sd_bus_message_append_basic(m, 's', field);
1185 if (r < 0)
1186 return bus_log_create_error(r);
1187
1188 r = sd_bus_message_open_container(m, 'v', "a(bs)");
1189 if (r < 0)
1190 return bus_log_create_error(r);
1191
1192 r = sd_bus_message_open_container(m, 'a', "(bs)");
1193 if (r < 0)
1194 return bus_log_create_error(r);
1195
1196 bool invert = *eq == '~';
1197
1198 for (const char *p = eq + invert;;) {
1199 _cleanup_free_ char *word = NULL;
1200
1201 r = extract_first_word(&p, &word, NULL, 0);
1202 if (r < 0)
1203 return parse_log_error(r, field, eq);
1204 if (r == 0)
1205 break;
1206
1207 r = sd_bus_message_append(m, "(bs)", invert, word);
1208 if (r < 0)
1209 return bus_log_create_error(r);
1210 }
1211
1212 r = sd_bus_message_close_container(m);
1213 if (r < 0)
1214 return bus_log_create_error(r);
1215
1216 r = sd_bus_message_close_container(m);
1217 if (r < 0)
1218 return bus_log_create_error(r);
1219
1220 r = sd_bus_message_close_container(m);
1221 if (r < 0)
1222 return bus_log_create_error(r);
1223
1224 return 1;
1225 }
1226
1227 static int bus_append_log_extra_fields(sd_bus_message *m, const char *field, const char *eq) {
1228 int r;
1229
1230 r = sd_bus_message_open_container(m, 'r', "sv");
1231 if (r < 0)
1232 return bus_log_create_error(r);
1233
1234 r = sd_bus_message_append_basic(m, 's', field);
1235 if (r < 0)
1236 return bus_log_create_error(r);
1237
1238 r = sd_bus_message_open_container(m, 'v', "aay");
1239 if (r < 0)
1240 return bus_log_create_error(r);
1241
1242 r = sd_bus_message_open_container(m, 'a', "ay");
1243 if (r < 0)
1244 return bus_log_create_error(r);
1245
1246 r = sd_bus_message_append_array(m, 'y', eq, strlen(eq));
1247 if (r < 0)
1248 return bus_log_create_error(r);
1249
1250 r = sd_bus_message_close_container(m);
1251 if (r < 0)
1252 return bus_log_create_error(r);
1253
1254 r = sd_bus_message_close_container(m);
1255 if (r < 0)
1256 return bus_log_create_error(r);
1257
1258 r = sd_bus_message_close_container(m);
1259 if (r < 0)
1260 return bus_log_create_error(r);
1261
1262 return 1;
1263 }
1264
1265 static int bus_append_log_filter_patterns(sd_bus_message *m, const char *field, const char *eq) {
1266 return bus_append_trivial_array(m, field, eq,
1267 "a(bs)",
1268 eq[0] != '~',
1269 eq[0] != '~' ? eq : eq + 1);
1270 }
1271
1272 static int bus_append_standard_inputs(sd_bus_message *m, const char *field, const char *eq) {
1273 const char *n, *appended;
1274 int r;
1275
1276 if ((n = startswith(eq, "fd:"))) {
1277 appended = strjoina(field, "FileDescriptorName");
1278 r = sd_bus_message_append(m, "(sv)", appended, "s", n);
1279 } else if ((n = startswith(eq, "file:"))) {
1280 appended = strjoina(field, "File");
1281 r = sd_bus_message_append(m, "(sv)", appended, "s", n);
1282 } else if ((n = startswith(eq, "append:"))) {
1283 appended = strjoina(field, "FileToAppend");
1284 r = sd_bus_message_append(m, "(sv)", appended, "s", n);
1285 } else if ((n = startswith(eq, "truncate:"))) {
1286 appended = strjoina(field, "FileToTruncate");
1287 r = sd_bus_message_append(m, "(sv)", appended, "s", n);
1288 } else
1289 r = sd_bus_message_append(m, "(sv)", field, "s", eq);
1290 if (r < 0)
1291 return bus_log_create_error(r);
1292
1293 return 1;
1294 }
1295
1296 static int bus_append_standard_input_text(sd_bus_message *m, const char *field, const char *eq) {
1297 _cleanup_free_ char *unescaped = NULL;
1298 ssize_t l;
1299
1300 l = cunescape(eq, 0, &unescaped);
1301 if (l < 0)
1302 return log_error_errno(l, "Failed to unescape value for %s=: %s", field, eq);
1303
1304 if (!strextend(&unescaped, "\n"))
1305 return log_oom();
1306
1307 /* Note that we don't expand specifiers here, but that should be OK, as this is a
1308 * programmatic interface anyway */
1309
1310 /* The server side does not have StandardInputText, using StandardInputData instead. */
1311 return bus_append_byte_array(m, "StandardInputData", unescaped, l + 1);
1312 }
1313
1314 static int bus_append_standard_input_data(sd_bus_message *m, const char *field, const char *eq) {
1315 _cleanup_free_ void *decoded = NULL;
1316 size_t sz;
1317 int r;
1318
1319 r = unbase64mem(eq, &decoded, &sz);
1320 if (r < 0)
1321 return log_error_errno(r, "Failed to decode base64 data for %s=: %m", field);
1322
1323 return bus_append_byte_array(m, field, decoded, sz);
1324 }
1325
1326 static int bus_try_append_resource_limit(sd_bus_message *m, const char *field, const char *eq) {
1327 int r;
1328
1329 const char *suffix = startswith(field, "Limit");
1330 if (!suffix)
1331 return 0;
1332
1333 int rl = rlimit_from_string(suffix);
1334 if (rl < 0)
1335 return 0; /* We let the generic error machinery handle this. */
1336
1337 struct rlimit l;
1338 r = rlimit_parse(rl, eq, &l);
1339 if (r < 0)
1340 return parse_log_error(r, field, eq);
1341
1342 r = sd_bus_message_append(m, "(sv)", field, "t", (uint64_t) l.rlim_max);
1343 if (r < 0)
1344 return bus_log_create_error(r);
1345
1346 const char *sn = strjoina(field, "Soft");
1347 r = sd_bus_message_append(m, "(sv)", sn, "t", (uint64_t) l.rlim_cur);
1348 if (r < 0)
1349 return bus_log_create_error(r);
1350
1351 return 1;
1352 }
1353
1354 static void dump_resource_limits(void) {
1355 rlimits_list("Limit");
1356 }
1357
1358 static int bus_append_string_with_ignore(sd_bus_message *m, const char *field, const char *eq) {
1359 int ignore = 0;
1360 const char *s = eq;
1361 int r;
1362
1363 if (eq[0] == '-') {
1364 ignore = 1;
1365 s = eq + 1;
1366 }
1367
1368 r = sd_bus_message_append(m, "(sv)", field, "(bs)", ignore, s);
1369 if (r < 0)
1370 return bus_log_create_error(r);
1371
1372 return 1;
1373 }
1374
1375 static int bus_append_capabilities(sd_bus_message *m, const char *field, const char *eq) {
1376 uint64_t sum = 0;
1377 bool invert = false;
1378 const char *p = eq;
1379 int r;
1380
1381 if (*p == '~') {
1382 invert = true;
1383 p++;
1384 }
1385
1386 r = capability_set_from_string(p, &sum);
1387 if (r < 0)
1388 return parse_log_error(r, field, eq);
1389
1390 sum = invert ? ~sum : sum;
1391
1392 r = sd_bus_message_append(m, "(sv)", field, "t", sum);
1393 if (r < 0)
1394 return bus_log_create_error(r);
1395
1396 return 1;
1397 }
1398
1399 static int bus_append_cpu_affinity(sd_bus_message *m, const char *field, const char *eq) {
1400 int r;
1401
1402 if (streq_ptr(eq, "numa")) {
1403 r = sd_bus_message_append(m, "(sv)", "CPUAffinityFromNUMA", "b", true);
1404 if (r < 0)
1405 return bus_log_create_error(r);
1406 return r;
1407 }
1408
1409 return bus_append_parse_cpu_set(m, field, eq);
1410 }
1411
1412 static int bus_append_numa_mask(sd_bus_message *m, const char *field, const char *eq) {
1413 _cleanup_(cpu_set_done) CPUSet nodes = {};
1414 _cleanup_free_ uint8_t *array = NULL;
1415 size_t allocated;
1416 int r;
1417
1418 if (eq && streq(eq, "all")) {
1419 r = numa_mask_add_all(&nodes);
1420 if (r < 0)
1421 return log_error_errno(r, "Failed to create NUMA mask representing \"all\" NUMA nodes: %m");
1422 } else {
1423 r = parse_cpu_set(eq, &nodes);
1424 if (r < 0)
1425 return parse_log_error(r, field, eq);
1426 }
1427
1428 r = cpu_set_to_dbus(&nodes, &array, &allocated);
1429 if (r < 0)
1430 return log_error_errno(r, "Failed to serialize %s: %m", field);
1431
1432 return bus_append_byte_array(m, field, array, allocated);
1433 }
1434
1435 static int bus_append_filter_list(sd_bus_message *m, const char *field, const char *eq) {
1436 int allow_list = 1;
1437 const char *p = eq;
1438 int r;
1439
1440 if (*p == '~') {
1441 allow_list = 0;
1442 p++;
1443 }
1444
1445 r = sd_bus_message_open_container(m, SD_BUS_TYPE_STRUCT, "sv");
1446 if (r < 0)
1447 return bus_log_create_error(r);
1448
1449 r = sd_bus_message_append_basic(m, SD_BUS_TYPE_STRING, field);
1450 if (r < 0)
1451 return bus_log_create_error(r);
1452
1453 r = sd_bus_message_open_container(m, 'v', "(bas)");
1454 if (r < 0)
1455 return bus_log_create_error(r);
1456
1457 r = sd_bus_message_open_container(m, 'r', "bas");
1458 if (r < 0)
1459 return bus_log_create_error(r);
1460
1461 r = sd_bus_message_append_basic(m, 'b', &allow_list);
1462 if (r < 0)
1463 return bus_log_create_error(r);
1464
1465 r = sd_bus_message_open_container(m, 'a', "s");
1466 if (r < 0)
1467 return bus_log_create_error(r);
1468
1469 for (;;) {
1470 _cleanup_free_ char *word = NULL;
1471
1472 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
1473 if (r < 0)
1474 return parse_log_error(r, field, eq);
1475 if (r == 0)
1476 break;
1477
1478 r = sd_bus_message_append_basic(m, 's', word);
1479 if (r < 0)
1480 return bus_log_create_error(r);
1481 }
1482
1483 r = sd_bus_message_close_container(m);
1484 if (r < 0)
1485 return bus_log_create_error(r);
1486
1487 r = sd_bus_message_close_container(m);
1488 if (r < 0)
1489 return bus_log_create_error(r);
1490
1491 r = sd_bus_message_close_container(m);
1492 if (r < 0)
1493 return bus_log_create_error(r);
1494
1495 r = sd_bus_message_close_container(m);
1496 if (r < 0)
1497 return bus_log_create_error(r);
1498
1499 return 1;
1500 }
1501
1502 static int bus_append_namespace_list(sd_bus_message *m, const char *field, const char *eq) {
1503 bool invert = false;
1504 unsigned long all = UPDATE_FLAG(NAMESPACE_FLAGS_ALL, CLONE_NEWUSER, !streq(field, "DelegateNamespaces"));
1505 unsigned long flags;
1506 int r;
1507
1508 r = parse_boolean(eq);
1509 if (r > 0)
1510 /* RestrictNamespaces= value gets stored into a field with reverse semantics (the
1511 * namespaces which are retained), so RestrictNamespaces=true means we retain no
1512 * access to any namespaces and vice-versa. */
1513 flags = streq(field, "RestrictNamespaces") ? 0 : all;
1514 else if (r == 0)
1515 flags = streq(field, "RestrictNamespaces") ? all : 0;
1516 else {
1517 if (eq[0] == '~') {
1518 invert = true;
1519 eq++;
1520 }
1521
1522 r = namespace_flags_from_string(eq, &flags);
1523 if (r < 0)
1524 return parse_log_error(r, field, eq);
1525 }
1526
1527 if (invert)
1528 flags = (~flags) & all;
1529
1530 r = sd_bus_message_append(m, "(sv)", field, "t", (uint64_t) flags);
1531 if (r < 0)
1532 return bus_log_create_error(r);
1533
1534 return 1;
1535 }
1536
1537 static int bus_append_bind_paths(sd_bus_message *m, const char *field, const char *eq) {
1538 const char *p = eq;
1539 int r;
1540
1541 r = sd_bus_message_open_container(m, SD_BUS_TYPE_STRUCT, "sv");
1542 if (r < 0)
1543 return bus_log_create_error(r);
1544
1545 r = sd_bus_message_append_basic(m, SD_BUS_TYPE_STRING, field);
1546 if (r < 0)
1547 return bus_log_create_error(r);
1548
1549 r = sd_bus_message_open_container(m, 'v', "a(ssbt)");
1550 if (r < 0)
1551 return bus_log_create_error(r);
1552
1553 r = sd_bus_message_open_container(m, 'a', "(ssbt)");
1554 if (r < 0)
1555 return bus_log_create_error(r);
1556
1557 for (;;) {
1558 _cleanup_free_ char *source = NULL, *destination = NULL;
1559 char *s = NULL, *d = NULL;
1560 bool ignore_enoent = false;
1561 uint64_t flags = MS_REC;
1562
1563 r = extract_first_word(&p, &source, ":" WHITESPACE, EXTRACT_UNQUOTE|EXTRACT_DONT_COALESCE_SEPARATORS);
1564 if (r < 0)
1565 return parse_log_error(r, field, eq);
1566 if (r == 0)
1567 break;
1568
1569 s = source;
1570 if (s[0] == '-') {
1571 ignore_enoent = true;
1572 s++;
1573 }
1574
1575 if (p && p[-1] == ':') {
1576 r = extract_first_word(&p, &destination, ":" WHITESPACE, EXTRACT_UNQUOTE|EXTRACT_DONT_COALESCE_SEPARATORS);
1577 if (r < 0)
1578 return parse_log_error(r, field, p);
1579 if (r == 0)
1580 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1581 "Missing argument after ':': %s", eq);
1582
1583 d = destination;
1584
1585 if (p && p[-1] == ':') {
1586 _cleanup_free_ char *options = NULL;
1587
1588 r = extract_first_word(&p, &options, NULL, EXTRACT_UNQUOTE);
1589 if (r < 0)
1590 return parse_log_error(r, field, p);
1591
1592 if (isempty(options) || streq(options, "rbind"))
1593 flags = MS_REC;
1594 else if (streq(options, "norbind"))
1595 flags = 0;
1596 else
1597 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1598 "Unknown options: %s", eq);
1599 }
1600 } else
1601 d = s;
1602
1603 r = sd_bus_message_append(m, "(ssbt)", s, d, ignore_enoent, flags);
1604 if (r < 0)
1605 return bus_log_create_error(r);
1606 }
1607
1608 r = sd_bus_message_close_container(m);
1609 if (r < 0)
1610 return bus_log_create_error(r);
1611
1612 r = sd_bus_message_close_container(m);
1613 if (r < 0)
1614 return bus_log_create_error(r);
1615
1616 r = sd_bus_message_close_container(m);
1617 if (r < 0)
1618 return bus_log_create_error(r);
1619
1620 return 1;
1621 }
1622
1623 static int bus_append_temporary_file_system(sd_bus_message *m, const char *field, const char *eq) {
1624 const char *p = eq;
1625 int r;
1626
1627 r = sd_bus_message_open_container(m, SD_BUS_TYPE_STRUCT, "sv");
1628 if (r < 0)
1629 return bus_log_create_error(r);
1630
1631 r = sd_bus_message_append_basic(m, SD_BUS_TYPE_STRING, field);
1632 if (r < 0)
1633 return bus_log_create_error(r);
1634
1635 r = sd_bus_message_open_container(m, 'v', "a(ss)");
1636 if (r < 0)
1637 return bus_log_create_error(r);
1638
1639 r = sd_bus_message_open_container(m, 'a', "(ss)");
1640 if (r < 0)
1641 return bus_log_create_error(r);
1642
1643 for (;;) {
1644 _cleanup_free_ char *word = NULL, *path = NULL;
1645 const char *w;
1646
1647 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
1648 if (r < 0)
1649 return parse_log_error(r, field, eq);
1650 if (r == 0)
1651 break;
1652
1653 w = word;
1654 r = extract_first_word(&w, &path, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
1655 if (r <= 0)
1656 return parse_log_error(r, field, eq);
1657
1658 r = sd_bus_message_append(m, "(ss)", path, w);
1659 if (r < 0)
1660 return bus_log_create_error(r);
1661 }
1662
1663 r = sd_bus_message_close_container(m);
1664 if (r < 0)
1665 return bus_log_create_error(r);
1666
1667 r = sd_bus_message_close_container(m);
1668 if (r < 0)
1669 return bus_log_create_error(r);
1670
1671 r = sd_bus_message_close_container(m);
1672 if (r < 0)
1673 return bus_log_create_error(r);
1674
1675 return 1;
1676 }
1677
1678 static int bus_append_root_hash(sd_bus_message *m, const char *field, const char *eq) {
1679 _cleanup_free_ void *roothash_decoded = NULL;
1680 size_t roothash_decoded_size = 0;
1681 int r;
1682
1683 /* We have the path to a roothash to load and decode, eg: RootHash=/foo/bar.roothash */
1684 if (path_is_absolute(eq))
1685 return bus_append_string(m, "RootHashPath", eq);
1686
1687 /* We have a roothash to decode, eg: RootHash=012345789abcdef */
1688 r = unhexmem(eq, &roothash_decoded, &roothash_decoded_size);
1689 if (r < 0)
1690 return log_error_errno(r, "Failed to decode base64 data for %s=: %m", field);
1691 if (roothash_decoded_size < sizeof(sd_id128_t))
1692 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "%s value '%s' is too short.", field, eq);
1693
1694 return bus_append_byte_array(m, field, roothash_decoded, roothash_decoded_size);
1695 }
1696
1697 static int bus_append_root_hash_signature(sd_bus_message *m, const char *field, const char *eq) {
1698 _cleanup_free_ void *roothash_sig_decoded = NULL;
1699 size_t roothash_sig_decoded_size = 0;
1700 int r;
1701
1702 /* We have the path to a roothash signature to load and decode, eg: RootHash=/foo/bar.roothash.p7s */
1703 if (path_is_absolute(eq))
1704 return bus_append_string(m, "RootHashSignaturePath", eq);
1705
1706 const char *value = startswith(eq, "base64:");
1707 if (!value)
1708 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1709 "Failed to decode %s value '%s': neither a path nor starts with 'base64:'.",
1710 field, eq);
1711
1712 /* We have a roothash signature to decode, eg: RootHashSignature=base64:012345789abcdef */
1713 r = unbase64mem(value, &roothash_sig_decoded, &roothash_sig_decoded_size);
1714 if (r < 0)
1715 return log_error_errno(r, "Failed to decode base64 data for %s=: %m", field);
1716
1717 return bus_append_byte_array(m, field, roothash_sig_decoded, roothash_sig_decoded_size);
1718 }
1719
1720 static int bus_append_root_image_options(sd_bus_message *m, const char *field, const char *eq) {
1721 _cleanup_strv_free_ char **l = NULL;
1722 const char *p = eq;
1723 int r;
1724
1725 r = sd_bus_message_open_container(m, SD_BUS_TYPE_STRUCT, "sv");
1726 if (r < 0)
1727 return bus_log_create_error(r);
1728
1729 r = sd_bus_message_append_basic(m, SD_BUS_TYPE_STRING, field);
1730 if (r < 0)
1731 return bus_log_create_error(r);
1732
1733 r = sd_bus_message_open_container(m, 'v', "a(ss)");
1734 if (r < 0)
1735 return bus_log_create_error(r);
1736
1737 r = sd_bus_message_open_container(m, 'a', "(ss)");
1738 if (r < 0)
1739 return bus_log_create_error(r);
1740
1741 r = strv_split_colon_pairs(&l, p);
1742 if (r < 0)
1743 return parse_log_error(r, field, eq);
1744
1745 STRV_FOREACH_PAIR(first, second, l) {
1746 r = sd_bus_message_append(m, "(ss)",
1747 !isempty(*second) ? *first : "root",
1748 !isempty(*second) ? *second : *first);
1749 if (r < 0)
1750 return bus_log_create_error(r);
1751 }
1752
1753 r = sd_bus_message_close_container(m);
1754 if (r < 0)
1755 return bus_log_create_error(r);
1756
1757 r = sd_bus_message_close_container(m);
1758 if (r < 0)
1759 return bus_log_create_error(r);
1760
1761 r = sd_bus_message_close_container(m);
1762 if (r < 0)
1763 return bus_log_create_error(r);
1764
1765 return 1;
1766 }
1767
1768 static int bus_append_mount_images(sd_bus_message *m, const char *field, const char *eq) {
1769 const char *p = eq;
1770 int r;
1771
1772 r = sd_bus_message_open_container(m, SD_BUS_TYPE_STRUCT, "sv");
1773 if (r < 0)
1774 return bus_log_create_error(r);
1775
1776 r = sd_bus_message_append_basic(m, SD_BUS_TYPE_STRING, field);
1777 if (r < 0)
1778 return bus_log_create_error(r);
1779
1780 r = sd_bus_message_open_container(m, 'v', "a(ssba(ss))");
1781 if (r < 0)
1782 return bus_log_create_error(r);
1783
1784 r = sd_bus_message_open_container(m, 'a', "(ssba(ss))");
1785 if (r < 0)
1786 return bus_log_create_error(r);
1787
1788 for (;;) {
1789 _cleanup_free_ char *first = NULL, *second = NULL, *tuple = NULL;
1790 const char *q = NULL, *source = NULL;
1791 bool permissive = false;
1792
1793 r = extract_first_word(&p, &tuple, NULL, EXTRACT_UNQUOTE|EXTRACT_RETAIN_ESCAPE);
1794 if (r < 0)
1795 return parse_log_error(r, field, eq);
1796 if (r == 0)
1797 break;
1798
1799 q = tuple;
1800 r = extract_many_words(&q, ":", EXTRACT_CUNESCAPE|EXTRACT_UNESCAPE_SEPARATORS, &first, &second);
1801 if (r < 0)
1802 return parse_log_error(r, field, eq);
1803 if (r == 0)
1804 continue;
1805
1806 source = first;
1807 if (source[0] == '-') {
1808 permissive = true;
1809 source++;
1810 }
1811
1812 if (isempty(second))
1813 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1814 "Missing argument after ':' for %s=: '%s'", field, eq);
1815
1816 r = sd_bus_message_open_container(m, 'r', "ssba(ss)");
1817 if (r < 0)
1818 return bus_log_create_error(r);
1819
1820 r = sd_bus_message_append(m, "ssb", source, second, permissive);
1821 if (r < 0)
1822 return bus_log_create_error(r);
1823
1824 r = sd_bus_message_open_container(m, 'a', "(ss)");
1825 if (r < 0)
1826 return bus_log_create_error(r);
1827
1828 for (;;) {
1829 _cleanup_free_ char *partition = NULL, *mount_options = NULL;
1830
1831 r = extract_many_words(&q, ":", EXTRACT_CUNESCAPE|EXTRACT_UNESCAPE_SEPARATORS, &partition, &mount_options);
1832 if (r < 0)
1833 return parse_log_error(r, field, eq);
1834 if (r == 0)
1835 break;
1836 /* Single set of options, applying to the root partition/single filesystem */
1837 if (r == 1) {
1838 r = sd_bus_message_append(m, "(ss)", "root", partition);
1839 if (r < 0)
1840 return bus_log_create_error(r);
1841
1842 break;
1843 }
1844
1845 r = sd_bus_message_append(m, "(ss)", partition, mount_options);
1846 if (r < 0)
1847 return bus_log_create_error(r);
1848 }
1849
1850 r = sd_bus_message_close_container(m);
1851 if (r < 0)
1852 return bus_log_create_error(r);
1853
1854 r = sd_bus_message_close_container(m);
1855 if (r < 0)
1856 return bus_log_create_error(r);
1857 }
1858
1859 r = sd_bus_message_close_container(m);
1860 if (r < 0)
1861 return bus_log_create_error(r);
1862
1863 r = sd_bus_message_close_container(m);
1864 if (r < 0)
1865 return bus_log_create_error(r);
1866
1867 r = sd_bus_message_close_container(m);
1868 if (r < 0)
1869 return bus_log_create_error(r);
1870
1871 return 1;
1872 }
1873
1874 static int bus_append_extension_images(sd_bus_message *m, const char *field, const char *eq) {
1875 const char *p = eq;
1876 int r;
1877
1878 r = sd_bus_message_open_container(m, SD_BUS_TYPE_STRUCT, "sv");
1879 if (r < 0)
1880 return bus_log_create_error(r);
1881
1882 r = sd_bus_message_append_basic(m, SD_BUS_TYPE_STRING, field);
1883 if (r < 0)
1884 return bus_log_create_error(r);
1885
1886 r = sd_bus_message_open_container(m, 'v', "a(sba(ss))");
1887 if (r < 0)
1888 return bus_log_create_error(r);
1889
1890 r = sd_bus_message_open_container(m, 'a', "(sba(ss))");
1891 if (r < 0)
1892 return bus_log_create_error(r);
1893
1894 for (;;) {
1895 _cleanup_free_ char *source = NULL, *tuple = NULL;
1896 const char *q = NULL, *s = NULL;
1897 bool permissive = false;
1898
1899 r = extract_first_word(&p, &tuple, NULL, EXTRACT_UNQUOTE|EXTRACT_RETAIN_ESCAPE);
1900 if (r < 0)
1901 return parse_log_error(r, field, eq);
1902 if (r == 0)
1903 break;
1904
1905 q = tuple;
1906 r = extract_first_word(&q, &source, ":", EXTRACT_CUNESCAPE|EXTRACT_UNESCAPE_SEPARATORS);
1907 if (r < 0)
1908 return parse_log_error(r, field, eq);
1909 if (r == 0)
1910 continue;
1911
1912 s = source;
1913 if (s[0] == '-') {
1914 permissive = true;
1915 s++;
1916 }
1917
1918 r = sd_bus_message_open_container(m, 'r', "sba(ss)");
1919 if (r < 0)
1920 return bus_log_create_error(r);
1921
1922 r = sd_bus_message_append(m, "sb", s, permissive);
1923 if (r < 0)
1924 return bus_log_create_error(r);
1925
1926 r = sd_bus_message_open_container(m, 'a', "(ss)");
1927 if (r < 0)
1928 return bus_log_create_error(r);
1929
1930 for (;;) {
1931 _cleanup_free_ char *partition = NULL, *mount_options = NULL;
1932
1933 r = extract_many_words(&q, ":", EXTRACT_CUNESCAPE|EXTRACT_UNESCAPE_SEPARATORS, &partition, &mount_options);
1934 if (r < 0)
1935 return parse_log_error(r, field, eq);
1936 if (r == 0)
1937 break;
1938 /* Single set of options, applying to the root partition/single filesystem */
1939 if (r == 1) {
1940 r = sd_bus_message_append(m, "(ss)", "root", partition);
1941 if (r < 0)
1942 return bus_log_create_error(r);
1943
1944 break;
1945 }
1946
1947 r = sd_bus_message_append(m, "(ss)", partition, mount_options);
1948 if (r < 0)
1949 return bus_log_create_error(r);
1950 }
1951
1952 r = sd_bus_message_close_container(m);
1953 if (r < 0)
1954 return bus_log_create_error(r);
1955
1956 r = sd_bus_message_close_container(m);
1957 if (r < 0)
1958 return bus_log_create_error(r);
1959 }
1960
1961 r = sd_bus_message_close_container(m);
1962 if (r < 0)
1963 return bus_log_create_error(r);
1964
1965 r = sd_bus_message_close_container(m);
1966 if (r < 0)
1967 return bus_log_create_error(r);
1968
1969 r = sd_bus_message_close_container(m);
1970 if (r < 0)
1971 return bus_log_create_error(r);
1972
1973 return 1;
1974 }
1975
1976 static int bus_append_directory(sd_bus_message *m, const char *field, const char *eq) {
1977 _cleanup_strv_free_ char **symlinks = NULL, **symlinks_ro = NULL, **sources = NULL, **sources_ro = NULL;
1978 const char *p = eq;
1979 int r;
1980
1981 /* Adding new directories is supported from both *DirectorySymlink methods and the
1982 * older ones, so first parse the input, and if we are given a new-style src:dst
1983 * tuple use the new method, else use the old one. */
1984
1985 for (;;) {
1986 _cleanup_free_ char *tuple = NULL, *source = NULL, *dest = NULL, *flags = NULL;
1987
1988 r = extract_first_word(&p, &tuple, NULL, EXTRACT_UNQUOTE);
1989 if (r < 0)
1990 return parse_log_error(r, field, eq);
1991 if (r == 0)
1992 break;
1993
1994 const char *t = tuple;
1995 r = extract_many_words(&t, ":", EXTRACT_UNQUOTE|EXTRACT_DONT_COALESCE_SEPARATORS, &source, &dest, &flags);
1996 if (r <= 0)
1997 return parse_log_error(r, field, eq);
1998
1999 path_simplify(source);
2000
2001 if (isempty(dest) && isempty(flags)) {
2002 r = strv_consume(&sources, TAKE_PTR(source));
2003 if (r < 0)
2004 return bus_log_create_error(r);
2005 } else if (isempty(flags)) {
2006 path_simplify(dest);
2007 r = strv_consume_pair(&symlinks, TAKE_PTR(source), TAKE_PTR(dest));
2008 if (r < 0)
2009 return log_oom();
2010 } else {
2011 ExecDirectoryFlags exec_directory_flags = exec_directory_flags_from_string(flags);
2012 if (exec_directory_flags < 0 || (exec_directory_flags & ~_EXEC_DIRECTORY_FLAGS_PUBLIC) != 0)
2013 return log_error_errno(r, "Failed to parse flags for %s=: '%s'", field, flags);
2014
2015 if (!isempty(dest)) {
2016 path_simplify(dest);
2017 r = strv_consume_pair(&symlinks_ro, TAKE_PTR(source), TAKE_PTR(dest));
2018 } else
2019 r = strv_consume(&sources_ro, TAKE_PTR(source));
2020 if (r < 0)
2021 return log_oom();
2022 }
2023 }
2024
2025 if (!strv_isempty(sources)) {
2026 r = sd_bus_message_open_container(m, SD_BUS_TYPE_STRUCT, "sv");
2027 if (r < 0)
2028 return bus_log_create_error(r);
2029
2030 r = sd_bus_message_append_basic(m, SD_BUS_TYPE_STRING, field);
2031 if (r < 0)
2032 return bus_log_create_error(r);
2033
2034 r = sd_bus_message_open_container(m, 'v', "as");
2035 if (r < 0)
2036 return bus_log_create_error(r);
2037
2038 r = sd_bus_message_append_strv(m, sources);
2039 if (r < 0)
2040 return bus_log_create_error(r);
2041
2042 r = sd_bus_message_close_container(m);
2043 if (r < 0)
2044 return bus_log_create_error(r);
2045
2046 r = sd_bus_message_close_container(m);
2047 if (r < 0)
2048 return bus_log_create_error(r);
2049 }
2050
2051 /* For State and Runtime directories we support an optional destination parameter, which
2052 * will be used to create a symlink to the source. But it is new so we cannot change the
2053 * old DBUS signatures, so append a new message type. */
2054 if (!strv_isempty(symlinks) || !strv_isempty(symlinks_ro) || !strv_isempty(sources_ro)) {
2055 const char *symlink_field;
2056
2057 r = sd_bus_message_open_container(m, SD_BUS_TYPE_STRUCT, "sv");
2058 if (r < 0)
2059 return bus_log_create_error(r);
2060
2061 if (streq(field, "StateDirectory"))
2062 symlink_field = "StateDirectorySymlink";
2063 else if (streq(field, "RuntimeDirectory"))
2064 symlink_field = "RuntimeDirectorySymlink";
2065 else if (streq(field, "CacheDirectory"))
2066 symlink_field = "CacheDirectorySymlink";
2067 else if (streq(field, "LogsDirectory"))
2068 symlink_field = "LogsDirectorySymlink";
2069 else
2070 assert_not_reached();
2071
2072 r = sd_bus_message_append_basic(m, SD_BUS_TYPE_STRING, symlink_field);
2073 if (r < 0)
2074 return bus_log_create_error(r);
2075
2076 r = sd_bus_message_open_container(m, 'v', "a(sst)");
2077 if (r < 0)
2078 return bus_log_create_error(r);
2079
2080 r = sd_bus_message_open_container(m, 'a', "(sst)");
2081 if (r < 0)
2082 return bus_log_create_error(r);
2083
2084 STRV_FOREACH_PAIR(source, destination, symlinks) {
2085 r = sd_bus_message_append(m, "(sst)", *source, *destination, UINT64_C(0));
2086 if (r < 0)
2087 return bus_log_create_error(r);
2088 }
2089
2090 STRV_FOREACH_PAIR(source, destination, symlinks_ro) {
2091 r = sd_bus_message_append(m, "(sst)", *source, *destination, (uint64_t) EXEC_DIRECTORY_READ_ONLY);
2092 if (r < 0)
2093 return bus_log_create_error(r);
2094 }
2095
2096 STRV_FOREACH(source, sources_ro) {
2097 r = sd_bus_message_append(m, "(sst)", *source, "", (uint64_t) EXEC_DIRECTORY_READ_ONLY);
2098 if (r < 0)
2099 return bus_log_create_error(r);
2100 }
2101
2102 r = sd_bus_message_close_container(m);
2103 if (r < 0)
2104 return bus_log_create_error(r);
2105
2106 r = sd_bus_message_close_container(m);
2107 if (r < 0)
2108 return bus_log_create_error(r);
2109
2110 r = sd_bus_message_close_container(m);
2111 if (r < 0)
2112 return bus_log_create_error(r);
2113 }
2114
2115 return 1;
2116 }
2117
2118 static int bus_append_quota_directory(sd_bus_message *m, const char *field, const char *eq) {
2119 uint64_t quota_absolute = UINT64_MAX;
2120 uint32_t quota_scale = UINT32_MAX;
2121 int quota_enforce = false;
2122 int r;
2123
2124 if (!isempty(eq) && !streq(eq, "off")) {
2125 r = parse_permyriad(eq);
2126 if (r < 0) {
2127 r = parse_size(eq, 1024, &quota_absolute);
2128 if (r < 0)
2129 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to parse argument: %s=%s", field, eq);
2130 } else
2131 quota_scale = UINT32_SCALE_FROM_PERMYRIAD(r);
2132
2133 quota_enforce = true;
2134 }
2135
2136 r = sd_bus_message_append(m, "(sv)", field, "(tus)", quota_absolute, quota_scale, yes_no(quota_enforce));
2137 if (r < 0)
2138 return bus_log_create_error(r);
2139
2140 return 1;
2141 }
2142
2143 static int bus_append_protect_hostname(sd_bus_message *m, const char *field, const char *eq) {
2144 int r;
2145
2146 /* The command-line field is called "ProtectHostname". We also accept "ProtectHostnameEx" as the
2147 * field name for backward compatibility. We set ProtectHostname or ProtectHostnameEx. */
2148
2149 r = parse_boolean(eq);
2150 if (r >= 0)
2151 r = sd_bus_message_append(m, "(sv)", "ProtectHostname", "b", r);
2152 else {
2153 const char *colon = strchr(eq, ':');
2154 if (colon) {
2155 if (isempty(colon + 1))
2156 return parse_log_error(0, field, eq);
2157
2158 _cleanup_free_ char *p = strndup(eq, colon - eq);
2159 if (!p)
2160 return -ENOMEM;
2161
2162 r = sd_bus_message_append(m, "(sv)", "ProtectHostnameEx", "(ss)", p, colon + 1);
2163 } else
2164 r = sd_bus_message_append(m, "(sv)", "ProtectHostnameEx", "(ss)", eq, NULL);
2165 }
2166 if (r < 0)
2167 return bus_log_create_error(r);
2168
2169 return 1;
2170 }
2171
2172 static int bus_append_boolean_or_ex_string(sd_bus_message *m, const char *field, const char *eq) {
2173 int r;
2174
2175 r = parse_boolean(eq);
2176 if (r >= 0) {
2177 if (endswith(field, "Ex"))
2178 field = strndupa_safe(field, strlen(field) - 2);
2179
2180 r = sd_bus_message_append(m, "(sv)", field, "b", r);
2181 } else {
2182 if (!endswith(field, "Ex"))
2183 field = strjoina(field, "Ex");
2184
2185 /* We allow any string through and let the server perform the verification. */
2186 r = sd_bus_message_append(m, "(sv)", field, "s", eq);
2187 }
2188 if (r < 0)
2189 return bus_log_create_error(r);
2190
2191 return 1;
2192 }
2193
2194 static int bus_append_paths(sd_bus_message *m, const char *field, const char *eq) {
2195 return bus_append_trivial_array(m, "Paths", eq,
2196 "a(ss)", field, eq);
2197 }
2198
2199 static int bus_append_exit_status(sd_bus_message *m, const char *field, const char *eq) {
2200 _cleanup_free_ int *status = NULL, *signal = NULL;
2201 size_t n_status = 0, n_signal = 0;
2202 int r;
2203
2204 for (const char *p = eq;;) {
2205 _cleanup_free_ char *word = NULL;
2206
2207 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
2208 if (r < 0)
2209 return parse_log_error(r, field, eq);
2210 if (r == 0)
2211 break;
2212
2213 /* We need to call exit_status_from_string() first, because we want
2214 * to parse numbers as exit statuses, not signals. */
2215
2216 r = exit_status_from_string(word);
2217 if (r >= 0) {
2218 assert(r >= 0 && r < 256);
2219
2220 if (!GREEDY_REALLOC(status, n_status + 1))
2221 return log_oom();
2222
2223 status[n_status++] = r;
2224
2225 } else if ((r = signal_from_string(word)) >= 0) {
2226 if (!GREEDY_REALLOC(signal, n_signal + 1))
2227 return log_oom();
2228
2229 signal[n_signal++] = r;
2230
2231 } else
2232 /* original r from exit_status_to_string() */
2233 return parse_log_error(r, field, word);
2234 }
2235
2236 r = sd_bus_message_open_container(m, SD_BUS_TYPE_STRUCT, "sv");
2237 if (r < 0)
2238 return bus_log_create_error(r);
2239
2240 r = sd_bus_message_append_basic(m, SD_BUS_TYPE_STRING, field);
2241 if (r < 0)
2242 return bus_log_create_error(r);
2243
2244 r = sd_bus_message_open_container(m, 'v', "(aiai)");
2245 if (r < 0)
2246 return bus_log_create_error(r);
2247
2248 r = sd_bus_message_open_container(m, 'r', "aiai");
2249 if (r < 0)
2250 return bus_log_create_error(r);
2251
2252 r = sd_bus_message_append_array(m, 'i', status, n_status * sizeof(int));
2253 if (r < 0)
2254 return bus_log_create_error(r);
2255
2256 r = sd_bus_message_append_array(m, 'i', signal, n_signal * sizeof(int));
2257 if (r < 0)
2258 return bus_log_create_error(r);
2259
2260 r = sd_bus_message_close_container(m);
2261 if (r < 0)
2262 return bus_log_create_error(r);
2263
2264 r = sd_bus_message_close_container(m);
2265 if (r < 0)
2266 return bus_log_create_error(r);
2267
2268 r = sd_bus_message_close_container(m);
2269 if (r < 0)
2270 return bus_log_create_error(r);
2271
2272 return 1;
2273 }
2274
2275 static int bus_append_action_exit_status(sd_bus_message *m, const char *field, const char *eq) {
2276 int r;
2277
2278 if (isempty(eq))
2279 r = sd_bus_message_append(m, "(sv)", field, "i", -1);
2280 else {
2281 uint8_t u;
2282
2283 r = safe_atou8(eq, &u);
2284 if (r < 0)
2285 return parse_log_error(r, field, eq);
2286
2287 r = sd_bus_message_append(m, "(sv)", field, "i", (int) u);
2288 }
2289 if (r < 0)
2290 return bus_log_create_error(r);
2291
2292 return 1;
2293 }
2294
2295 static int bus_append_listen(sd_bus_message *m, const char *field, const char *eq) {
2296 const char *p = ASSERT_PTR(startswith(field, "Listen"));
2297
2298 return bus_append_trivial_array(m, "Listen", eq,
2299 "a(ss)", p, eq);
2300 }
2301
2302 static int bus_append_timers_monotonic(sd_bus_message *m, const char *field, const char *eq) {
2303 int r;
2304
2305 if (isempty(eq))
2306 r = sd_bus_message_append(m, "(sv)", "TimersMonotonic", "a(st)", 0);
2307 else {
2308 usec_t t;
2309 r = parse_sec(eq, &t);
2310 if (r < 0)
2311 return parse_log_error(r, field, eq);
2312
2313 r = sd_bus_message_append(m, "(sv)", "TimersMonotonic", "a(st)", 1, field, t);
2314 }
2315 if (r < 0)
2316 return bus_log_create_error(r);
2317
2318 return 1;
2319 }
2320
2321 static int bus_append_timers_calendar(sd_bus_message *m, const char *field, const char *eq) {
2322 return bus_append_trivial_array(m, "TimersCalendar", eq,
2323 "a(ss)", field, eq);
2324 }
2325
2326 static int bus_append_timeout_sec(sd_bus_message *m, const char *field, const char *eq) {
2327 int r;
2328
2329 r = bus_append_parse_sec_rename(m, "TimeoutStartSec", eq);
2330 if (r < 0)
2331 return r;
2332
2333 return bus_append_parse_sec_rename(m, "TimeoutStopSec", eq);
2334 }
2335
2336 static int bus_try_append_condition(sd_bus_message *m, const char *field, const char *eq) {
2337 ConditionType t = condition_type_from_string(field);
2338 bool is_condition = t >= 0;
2339
2340 if (!is_condition) {
2341 t = assert_type_from_string(field);
2342 if (t < 0)
2343 return 0;
2344 }
2345
2346 const char *p = eq;
2347
2348 int trigger = p && *p == '|';
2349 if (trigger)
2350 p++;
2351
2352 int negate = p && *p == '!';
2353 if (negate)
2354 p++;
2355
2356 return bus_append_trivial_array(m,
2357 is_condition ? "Conditions" : "Asserts",
2358 eq,
2359 "a(sbbs)",
2360 field, trigger, negate, p);
2361 }
2362
2363 static void dump_conditions(void) {
2364 condition_types_list();
2365 assert_types_list();
2366 }
2367
2368 static int bus_try_append_unit_dependency(sd_bus_message *m, const char *field, const char *eq) {
2369 if (unit_dependency_from_string(field) < 0)
2370 return 0;
2371
2372 return bus_append_strv(m, field, eq);
2373 }
2374
2375 typedef struct BusProperty {
2376 const char *name;
2377 int (*convert)(sd_bus_message *m, const char *field, const char *eq);
2378 void (*dump)(void);
2379 } BusProperty;
2380
2381 static const BusProperty cgroup_properties[] = {
2382 { "DevicePolicy", bus_append_string },
2383 { "Slice", bus_append_string },
2384 { "ManagedOOMSwap", bus_append_string },
2385 { "ManagedOOMMemoryPressure", bus_append_string },
2386 { "ManagedOOMPreference", bus_append_string },
2387 { "OOMRules", bus_append_strv },
2388 { "MemoryPressureWatch", bus_append_string },
2389 { "CPUPressureWatch", bus_append_string },
2390 { "IOPressureWatch", bus_append_string },
2391 { "DelegateSubgroup", bus_append_string },
2392 { "ManagedOOMMemoryPressureLimit", bus_append_parse_permyriad },
2393 { "MemoryAccounting", bus_append_parse_boolean },
2394 { "MemoryZSwapWriteback", bus_append_parse_boolean },
2395 { "IOAccounting", bus_append_parse_boolean },
2396 { "TasksAccounting", bus_append_parse_boolean },
2397 { "IPAccounting", bus_append_parse_boolean },
2398 { "CoredumpReceive", bus_append_parse_boolean },
2399 { "CPUWeight", bus_append_cg_cpu_weight_parse },
2400 { "StartupCPUWeight", bus_append_cg_cpu_weight_parse },
2401 { "IOWeight", bus_append_cg_weight_parse },
2402 { "StartupIOWeight", bus_append_cg_weight_parse },
2403 { "AllowedCPUs", bus_append_parse_cpu_set },
2404 { "StartupAllowedCPUs", bus_append_parse_cpu_set },
2405 { "AllowedMemoryNodes", bus_append_parse_cpu_set },
2406 { "StartupAllowedMemoryNodes", bus_append_parse_cpu_set },
2407 { "CPUSetPartition", bus_append_string },
2408 { "DisableControllers", bus_append_strv },
2409 { "Delegate", bus_append_parse_delegate },
2410 { "MemoryMin", bus_append_parse_resource_limit },
2411 { "MemoryLow", bus_append_parse_resource_limit },
2412 { "MemoryHigh", bus_append_parse_resource_limit },
2413 { "MemoryMax", bus_append_parse_resource_limit },
2414 { "MemorySwapMax", bus_append_parse_resource_limit },
2415 { "MemoryZSwapMax", bus_append_parse_resource_limit },
2416 { "TasksMax", bus_append_parse_resource_limit },
2417 { "CPUQuota", bus_append_parse_cpu_quota },
2418 { "CPUQuotaPeriodSec", bus_append_parse_sec_rename_infinity },
2419 { "DeviceAllow", bus_append_parse_device_allow },
2420 { "IODeviceWeight", bus_append_parse_io_device_weight },
2421 { "IODeviceLatencyTargetSec", bus_append_parse_io_device_latency },
2422 { "IPAddressAllow", bus_append_parse_ip_address_filter },
2423 { "IPAddressDeny", bus_append_parse_ip_address_filter },
2424 { "IPIngressFilterPath", bus_append_ip_filter_path },
2425 { "IPEgressFilterPath", bus_append_ip_filter_path },
2426 { "BPFProgram", bus_append_bpf_program },
2427 { "SocketBindAllow", bus_append_socket_filter },
2428 { "SocketBindDeny", bus_append_socket_filter },
2429 { "MemoryPressureThresholdSec", bus_append_parse_sec_rename },
2430 { "CPUPressureThresholdSec", bus_append_parse_sec_rename },
2431 { "IOPressureThresholdSec", bus_append_parse_sec_rename },
2432 { "NFTSet", bus_append_nft_set },
2433 { "BindNetworkInterface", bus_append_string },
2434
2435 /* While infinity is disallowed in unit file, infinity is allowed in D-Bus API which
2436 * means use the default memory pressure duration from oomd.conf. */
2437 { "ManagedOOMMemoryPressureDurationSec", bus_append_parse_sec_rename_infinity },
2438
2439 { "MemoryLimit", warn_deprecated },
2440 { "CPUShares", warn_deprecated },
2441 { "StartupCPUShares", warn_deprecated },
2442 { "BlockIOAccounting", warn_deprecated },
2443 { "BlockIOWeight", warn_deprecated },
2444 { "StartupBlockIOWeight", warn_deprecated },
2445 { "BlockIODeviceWeight", warn_deprecated },
2446 { "BlockIOReadBandwidth", warn_deprecated },
2447 { "BlockIOWriteBandwidth", warn_deprecated },
2448 { "CPUAccounting", warn_deprecated },
2449 { "DefaultMemoryMin", warn_deprecated },
2450 { "DefaultMemoryLow", warn_deprecated },
2451
2452 { NULL, bus_try_append_parse_cgroup_io_limit, cgroup_io_limits_list },
2453 {}
2454 };
2455
2456 static const BusProperty automount_properties[] = {
2457 { "Where", bus_append_string },
2458 { "ExtraOptions", bus_append_string },
2459 { "DirectoryMode", bus_append_parse_mode },
2460 { "TimeoutIdleSec", bus_append_parse_sec_rename },
2461 {}
2462 };
2463
2464 static const BusProperty execute_properties[] = {
2465 { "User", bus_append_string },
2466 { "Group", bus_append_string },
2467 { "UtmpIdentifier", bus_append_string },
2468 { "UtmpMode", bus_append_string },
2469 { "PAMName", bus_append_string },
2470 { "TTYPath", bus_append_string },
2471 { "WorkingDirectory", bus_append_string },
2472 { "RootDirectory", bus_append_string },
2473 { "SyslogIdentifier", bus_append_string },
2474 { "ProtectSystem", bus_append_string },
2475 { "ProtectHome", bus_append_string },
2476 { "SELinuxContext", bus_append_string },
2477 { "RootImage", bus_append_string },
2478 { "RootVerity", bus_append_string },
2479 { "RootMStack", bus_append_string },
2480 { "RuntimeDirectoryPreserve", bus_append_string },
2481 { "Personality", bus_append_string },
2482 { "KeyringMode", bus_append_string },
2483 { "ProtectProc", bus_append_string },
2484 { "ProcSubset", bus_append_string },
2485 { "NetworkNamespacePath", bus_append_string },
2486 { "UserNamespacePath", bus_append_string },
2487 { "IPCNamespacePath", bus_append_string },
2488 { "LogNamespace", bus_append_string },
2489 { "RootImagePolicy", bus_append_string },
2490 { "MountImagePolicy", bus_append_string },
2491 { "ExtensionImagePolicy", bus_append_string },
2492 { "PrivatePIDs", bus_append_string },
2493 { "PrivateBPF", bus_append_string },
2494 { "BPFDelegateCommands", bus_append_string },
2495 { "BPFDelegateMaps", bus_append_string },
2496 { "BPFDelegatePrograms", bus_append_string },
2497 { "BPFDelegateAttachments", bus_append_string },
2498 { "IgnoreSIGPIPE", bus_append_parse_boolean },
2499 { "TTYVHangup", bus_append_parse_boolean },
2500 { "TTYReset", bus_append_parse_boolean },
2501 { "TTYVTDisallocate", bus_append_parse_boolean },
2502 { "PrivateDevices", bus_append_parse_boolean },
2503 { "PrivateNetwork", bus_append_parse_boolean },
2504 { "PrivateMounts", bus_append_parse_boolean },
2505 { "PrivateIPC", bus_append_parse_boolean },
2506 { "NoNewPrivileges", bus_append_parse_boolean },
2507 { "SyslogLevelPrefix", bus_append_parse_boolean },
2508 { "MemoryDenyWriteExecute", bus_append_parse_boolean },
2509 { "RestrictRealtime", bus_append_parse_boolean },
2510 { "DynamicUser", bus_append_parse_boolean },
2511 { "RemoveIPC", bus_append_parse_boolean },
2512 { "ProtectKernelTunables", bus_append_parse_boolean },
2513 { "ProtectKernelModules", bus_append_parse_boolean },
2514 { "ProtectKernelLogs", bus_append_parse_boolean },
2515 { "ProtectClock", bus_append_parse_boolean },
2516 { "MountAPIVFS", bus_append_parse_boolean },
2517 { "BindLogSockets", bus_append_parse_boolean },
2518 { "CPUSchedulingResetOnFork", bus_append_parse_boolean },
2519 { "LockPersonality", bus_append_parse_boolean },
2520 { "MemoryKSM", bus_append_parse_boolean },
2521 { "MemoryTHP", bus_append_string },
2522 { "RestrictSUIDSGID", bus_append_parse_boolean },
2523 { "RootEphemeral", bus_append_parse_boolean },
2524 { "SetLoginEnvironment", bus_append_parse_boolean },
2525 { "ReadWriteDirectories", bus_append_strv },
2526 { "ReadOnlyDirectories", bus_append_strv },
2527 { "InaccessibleDirectories", bus_append_strv },
2528 { "ReadWritePaths", bus_append_strv },
2529 { "ReadOnlyPaths", bus_append_strv },
2530 { "InaccessiblePaths", bus_append_strv },
2531 { "ExecPaths", bus_append_strv },
2532 { "NoExecPaths", bus_append_strv },
2533 { "ExecSearchPath", bus_append_strv_colon },
2534 { "ExtensionDirectories", bus_append_strv },
2535 { "ConfigurationDirectory", bus_append_strv },
2536 { "SupplementaryGroups", bus_append_strv },
2537 { "SystemCallArchitectures", bus_append_strv },
2538 { "SyslogLevel", bus_append_log_level_from_string },
2539 { "LogLevelMax", bus_append_log_level_from_string },
2540 { "SyslogFacility", bus_append_log_facility_unshifted_from_string },
2541 { "SecureBits", bus_append_secure_bits_from_string },
2542 { "CPUSchedulingPolicy", bus_append_sched_policy_from_string },
2543 { "CPUSchedulingPriority", bus_append_safe_atoi },
2544 { "OOMScoreAdjust", bus_append_safe_atoi },
2545 { "CoredumpFilter", bus_append_coredump_filter_mask_from_string },
2546 { "Nice", bus_append_parse_nice },
2547 { "SystemCallErrorNumber", bus_append_seccomp_parse_errno_or_action },
2548 { "IOSchedulingClass", bus_append_ioprio_class_from_string },
2549 { "IOSchedulingPriority", bus_append_ioprio_parse_priority },
2550 { "RuntimeDirectoryMode", bus_append_parse_mode },
2551 { "StateDirectoryMode", bus_append_parse_mode },
2552 { "CacheDirectoryMode", bus_append_parse_mode },
2553 { "LogsDirectoryMode", bus_append_parse_mode },
2554 { "ConfigurationDirectoryMode", bus_append_parse_mode },
2555 { "UMask", bus_append_parse_mode },
2556 { "TimerSlackNSec", bus_append_parse_nsec },
2557 { "LogRateLimitIntervalSec", bus_append_parse_sec_rename },
2558 { "LogRateLimitBurst", bus_append_safe_atou },
2559 { "TTYRows", bus_append_safe_atou },
2560 { "TTYColumns", bus_append_safe_atou },
2561 { "MountFlags", bus_append_mount_propagation_flag_from_string },
2562 { "Environment", bus_append_strv_cunescape },
2563 { "UnsetEnvironment", bus_append_strv_cunescape },
2564 { "PassEnvironment", bus_append_strv_cunescape },
2565 { "EnvironmentFile", bus_append_environment_files },
2566 { "SetCredential", bus_append_set_credential },
2567 { "SetCredentialEncrypted", bus_append_set_credential },
2568 { "LoadCredential", bus_append_load_credential },
2569 { "LoadCredentialEncrypted", bus_append_load_credential },
2570 { "ImportCredential", bus_append_import_credential },
2571 { "ImportCredentialEx", bus_append_import_credential }, /* compat */
2572 { "LogExtraFields", bus_append_log_extra_fields },
2573 { "LogFilterPatterns", bus_append_log_filter_patterns },
2574 { "StandardInput", bus_append_standard_inputs },
2575 { "StandardOutput", bus_append_standard_inputs },
2576 { "StandardError", bus_append_standard_inputs },
2577 { "StandardInputText", bus_append_standard_input_text },
2578 { "StandardInputData", bus_append_standard_input_data },
2579 { "AppArmorProfile", bus_append_string_with_ignore },
2580 { "SmackProcessLabel", bus_append_string_with_ignore },
2581 { "CapabilityBoundingSet", bus_append_capabilities },
2582 { "AmbientCapabilities", bus_append_capabilities },
2583 { "CPUAffinity", bus_append_cpu_affinity },
2584 { "NUMAPolicy", bus_append_mpol_from_string },
2585 { "NUMAMask", bus_append_numa_mask },
2586 { "RestrictAddressFamilies", bus_append_filter_list },
2587 { "RestrictFileSystems", bus_append_filter_list },
2588 { "SystemCallFilter", bus_append_filter_list },
2589 { "SystemCallLog", bus_append_filter_list },
2590 { "RestrictNetworkInterfaces", bus_append_filter_list },
2591 { "RestrictNamespaces", bus_append_namespace_list },
2592 { "DelegateNamespaces", bus_append_namespace_list },
2593 { "BindPaths", bus_append_bind_paths },
2594 { "BindReadOnlyPaths", bus_append_bind_paths },
2595 { "TemporaryFileSystem", bus_append_temporary_file_system },
2596 { "RootHash", bus_append_root_hash },
2597 { "RootHashSignature", bus_append_root_hash_signature },
2598 { "RootImageOptions", bus_append_root_image_options },
2599 { "MountImages", bus_append_mount_images },
2600 { "ExtensionImages", bus_append_extension_images },
2601 { "StateDirectory", bus_append_directory },
2602 { "RuntimeDirectory", bus_append_directory },
2603 { "CacheDirectory", bus_append_directory },
2604 { "LogsDirectory", bus_append_directory },
2605 { "ProtectHostname", bus_append_protect_hostname },
2606 { "ProtectHostnameEx", bus_append_protect_hostname }, /* compat */
2607 { "PrivateTmp", bus_append_boolean_or_ex_string },
2608 { "PrivateTmpEx", bus_append_boolean_or_ex_string }, /* compat */
2609 { "ProtectControlGroups", bus_append_boolean_or_ex_string },
2610 { "ProtectControlGroupsEx", bus_append_boolean_or_ex_string }, /* compat */
2611 { "PrivateUsers", bus_append_boolean_or_ex_string },
2612 { "PrivateUsersEx", bus_append_boolean_or_ex_string }, /* compat */
2613 { "StateDirectoryQuota", bus_append_quota_directory },
2614 { "CacheDirectoryQuota", bus_append_quota_directory },
2615 { "LogsDirectoryQuota", bus_append_quota_directory },
2616 { "StateDirectoryAccounting", bus_append_parse_boolean },
2617 { "CacheDirectoryAccounting", bus_append_parse_boolean },
2618 { "LogsDirectoryAccounting", bus_append_parse_boolean },
2619
2620 { NULL, bus_try_append_resource_limit, dump_resource_limits },
2621 {}
2622 };
2623
2624 static const BusProperty kill_properties[] = {
2625 { "KillMode", bus_append_string },
2626 { "SendSIGHUP", bus_append_parse_boolean },
2627 { "SendSIGKILL", bus_append_parse_boolean },
2628 { "KillSignal", bus_append_signal_from_string },
2629 { "RestartKillSignal", bus_append_signal_from_string },
2630 { "FinalKillSignal", bus_append_signal_from_string },
2631 { "WatchdogSignal", bus_append_signal_from_string },
2632 {}
2633 };
2634
2635 static const BusProperty mount_properties[] = {
2636 { "What", bus_append_string },
2637 { "Where", bus_append_string },
2638 { "Options", bus_append_string },
2639 { "Type", bus_append_string },
2640 { "TimeoutSec", bus_append_parse_sec_rename },
2641 { "DirectoryMode", bus_append_parse_mode },
2642 { "SloppyOptions", bus_append_parse_boolean },
2643 { "LazyUnmount", bus_append_parse_boolean },
2644 { "ForceUnmount", bus_append_parse_boolean },
2645 { "ReadwriteOnly", bus_append_parse_boolean },
2646 {}
2647 };
2648
2649 static const BusProperty path_properties[] = {
2650 { "MakeDirectory", bus_append_parse_boolean },
2651 { "DirectoryMode", bus_append_parse_mode },
2652 { "PathExists", bus_append_paths },
2653 { "PathExistsGlob", bus_append_paths },
2654 { "PathChanged", bus_append_paths },
2655 { "PathModified", bus_append_paths },
2656 { "DirectoryNotEmpty", bus_append_paths },
2657 { "TriggerLimitBurst", bus_append_safe_atou },
2658 { "PollLimitBurst", bus_append_safe_atou },
2659 { "TriggerLimitIntervalSec", bus_append_parse_sec_rename },
2660 { "PollLimitIntervalSec", bus_append_parse_sec_rename },
2661 {}
2662 };
2663
2664 static const BusProperty scope_properties[] = {
2665 { "RuntimeMaxSec", bus_append_parse_sec_rename },
2666 { "RuntimeRandomizedExtraSec", bus_append_parse_sec_rename },
2667 { "TimeoutStopSec", bus_append_parse_sec_rename },
2668 { "OOMPolicy", bus_append_string },
2669
2670 /* Scope units don't have execution context but we still want to allow setting these two,
2671 * so let's handle them separately. */
2672 { "User", bus_append_string },
2673 { "Group", bus_append_string },
2674 {}
2675 };
2676
2677 static const BusProperty service_properties[] = {
2678 { "PIDFile", bus_append_string },
2679 { "Type", bus_append_string },
2680 { "ExitType", bus_append_string },
2681 { "Restart", bus_append_string },
2682 { "RestartMode", bus_append_string },
2683 { "BusName", bus_append_string },
2684 { "NotifyAccess", bus_append_string },
2685 { "USBFunctionDescriptors", bus_append_string },
2686 { "USBFunctionStrings", bus_append_string },
2687 { "OOMPolicy", bus_append_string },
2688 { "TimeoutStartFailureMode", bus_append_string },
2689 { "TimeoutStopFailureMode", bus_append_string },
2690 { "FileDescriptorStorePreserve", bus_append_string },
2691 { "PermissionsStartOnly", bus_append_parse_boolean },
2692 { "RootDirectoryStartOnly", bus_append_parse_boolean },
2693 { "RemainAfterExit", bus_append_parse_boolean },
2694 { "GuessMainPID", bus_append_parse_boolean },
2695 { "RestartSec", bus_append_parse_sec_rename },
2696 { "RestartMaxDelaySec", bus_append_parse_sec_rename },
2697 { "TimeoutStartSec", bus_append_parse_sec_rename },
2698 { "TimeoutStopSec", bus_append_parse_sec_rename },
2699 { "TimeoutAbortSec", bus_append_parse_sec_rename },
2700 { "RuntimeMaxSec", bus_append_parse_sec_rename },
2701 { "RuntimeRandomizedExtraSec", bus_append_parse_sec_rename },
2702 { "WatchdogSec", bus_append_parse_sec_rename },
2703 { "TimeoutSec", bus_append_timeout_sec },
2704 { "FileDescriptorStoreMax", bus_append_safe_atou },
2705 { "RestartSteps", bus_append_safe_atou },
2706 { "ExecCondition", bus_append_exec_command },
2707 { "ExecConditionEx", bus_append_exec_command }, /* compat */
2708 { "ExecStartPre", bus_append_exec_command },
2709 { "ExecStartPreEx", bus_append_exec_command }, /* compat */
2710 { "ExecStart", bus_append_exec_command },
2711 { "ExecStartEx", bus_append_exec_command }, /* compat */
2712 { "ExecStartPost", bus_append_exec_command },
2713 { "ExecStartPostEx", bus_append_exec_command }, /* compat */
2714 { "ExecReload", bus_append_exec_command },
2715 { "ExecReloadEx", bus_append_exec_command }, /* compat */
2716 { "ExecReloadPost", bus_append_exec_command },
2717 { "ExecReloadPostEx", bus_append_exec_command }, /* compat */
2718 { "ExecStop", bus_append_exec_command },
2719 { "ExecStopEx", bus_append_exec_command }, /* compat */
2720 { "ExecStopPost", bus_append_exec_command },
2721 { "ExecStopPostEx", bus_append_exec_command }, /* compat */
2722 { "RestartPreventExitStatus", bus_append_exit_status },
2723 { "RestartForceExitStatus", bus_append_exit_status },
2724 { "SuccessExitStatus", bus_append_exit_status },
2725 { "OpenFile", bus_append_open_file },
2726 { "ReloadSignal", bus_append_signal_from_string },
2727 { "RefreshOnReload", bus_append_refresh_on_reload },
2728 {}
2729 };
2730
2731 static const BusProperty socket_properties[] = {
2732 { "Accept", bus_append_parse_boolean },
2733 { "FlushPending", bus_append_parse_boolean },
2734 { "Writable", bus_append_parse_boolean },
2735 { "KeepAlive", bus_append_parse_boolean },
2736 { "NoDelay", bus_append_parse_boolean },
2737 { "FreeBind", bus_append_parse_boolean },
2738 { "Transparent", bus_append_parse_boolean },
2739 { "Broadcast", bus_append_parse_boolean },
2740 { "PassCredentials", bus_append_parse_boolean },
2741 { "PassFileDescriptorsToExec", bus_append_parse_boolean },
2742 { "PassSecurity", bus_append_parse_boolean },
2743 { "PassPacketInfo", bus_append_parse_boolean },
2744 { "ReusePort", bus_append_parse_boolean },
2745 { "RemoveOnStop", bus_append_parse_boolean },
2746 { "SELinuxContextFromNet", bus_append_parse_boolean },
2747 { "Priority", bus_append_safe_atoi },
2748 { "IPTTL", bus_append_safe_atoi },
2749 { "Mark", bus_append_safe_atoi },
2750 { "IPTOS", bus_append_ip_tos_from_string },
2751 { "Backlog", bus_append_safe_atou },
2752 { "MaxConnections", bus_append_safe_atou },
2753 { "MaxConnectionsPerSource", bus_append_safe_atou },
2754 { "KeepAliveProbes", bus_append_safe_atou },
2755 { "TriggerLimitBurst", bus_append_safe_atou },
2756 { "PollLimitBurst", bus_append_safe_atou },
2757 { "SocketMode", bus_append_parse_mode },
2758 { "DirectoryMode", bus_append_parse_mode },
2759 { "MessageQueueMaxMessages", bus_append_safe_atoi64 },
2760 { "MessageQueueMessageSize", bus_append_safe_atoi64 },
2761 { "TimeoutSec", bus_append_parse_sec_rename },
2762 { "KeepAliveTimeSec", bus_append_parse_sec_rename },
2763 { "KeepAliveIntervalSec", bus_append_parse_sec_rename },
2764 { "DeferAcceptSec", bus_append_parse_sec_rename },
2765 { "TriggerLimitIntervalSec", bus_append_parse_sec_rename },
2766 { "PollLimitIntervalSec", bus_append_parse_sec_rename },
2767 { "DeferTriggerMaxSec", bus_append_parse_sec_rename },
2768 { "ReceiveBuffer", bus_append_parse_size },
2769 { "SendBuffer", bus_append_parse_size },
2770 { "PipeSize", bus_append_parse_size },
2771 { "ExecStartPre", bus_append_exec_command },
2772 { "ExecStartPost", bus_append_exec_command },
2773 { "ExecReload", bus_append_exec_command },
2774 { "ExecStopPost", bus_append_exec_command },
2775 { "SmackLabel", bus_append_string },
2776 { "SmackLabelIPIn", bus_append_string },
2777 { "SmackLabelIPOut", bus_append_string },
2778 { "TCPCongestion", bus_append_string },
2779 { "BindToDevice", bus_append_string },
2780 { "BindIPv6Only", bus_append_string },
2781 { "FileDescriptorName", bus_append_string },
2782 { "SocketUser", bus_append_string },
2783 { "SocketGroup", bus_append_string },
2784 { "Timestamping", bus_append_string },
2785 { "DeferTrigger", bus_append_string },
2786 { "Symlinks", bus_append_strv },
2787 { "SocketProtocol", bus_append_parse_ip_protocol },
2788 { "ListenStream", bus_append_listen },
2789 { "ListenDatagram", bus_append_listen },
2790 { "ListenSequentialPacket", bus_append_listen },
2791 { "ListenNetlink", bus_append_listen },
2792 { "ListenSpecial", bus_append_listen },
2793 { "ListenMessageQueue", bus_append_listen },
2794 { "ListenFIFO", bus_append_listen },
2795 { "ListenUSBFunction", bus_append_listen },
2796 {}
2797 };
2798
2799 static const BusProperty timer_properties[] = {
2800 { "WakeSystem", bus_append_parse_boolean },
2801 { "RemainAfterElapse", bus_append_parse_boolean },
2802 { "Persistent", bus_append_parse_boolean },
2803 { "OnTimezoneChange", bus_append_parse_boolean },
2804 { "OnClockChange", bus_append_parse_boolean },
2805 { "FixedRandomDelay", bus_append_parse_boolean },
2806 { "DeferReactivation", bus_append_parse_boolean },
2807 { "AccuracySec", bus_append_parse_sec_rename },
2808 { "RandomizedDelaySec", bus_append_parse_sec_rename },
2809 { "RandomizedOffsetSec", bus_append_parse_sec_rename },
2810 { "OnActiveSec", bus_append_timers_monotonic },
2811 { "OnBootSec", bus_append_timers_monotonic },
2812 { "OnStartupSec", bus_append_timers_monotonic },
2813 { "OnUnitActiveSec", bus_append_timers_monotonic },
2814 { "OnUnitInactiveSec", bus_append_timers_monotonic },
2815 { "OnCalendar", bus_append_timers_calendar },
2816 {}
2817 };
2818
2819 static const BusProperty unit_properties[] = {
2820 { "Description", bus_append_string },
2821 { "SourcePath", bus_append_string },
2822 { "OnFailureJobMode", bus_append_string },
2823 { "JobTimeoutAction", bus_append_string },
2824 { "JobTimeoutRebootArgument", bus_append_string },
2825 { "StartLimitAction", bus_append_string },
2826 { "FailureAction", bus_append_string },
2827 { "SuccessAction", bus_append_string },
2828 { "RebootArgument", bus_append_string },
2829 { "CollectMode", bus_append_string },
2830 { "StopWhenUnneeded", bus_append_parse_boolean },
2831 { "RefuseManualStart", bus_append_parse_boolean },
2832 { "RefuseManualStop", bus_append_parse_boolean },
2833 { "AllowIsolate", bus_append_parse_boolean },
2834 { "IgnoreOnIsolate", bus_append_parse_boolean },
2835 { "SurviveFinalKillSignal", bus_append_parse_boolean },
2836 { "DefaultDependencies", bus_append_parse_boolean },
2837 { "JobTimeoutSec", bus_append_parse_sec_rename },
2838 { "JobRunningTimeoutSec", bus_append_parse_sec_rename },
2839 { "StartLimitIntervalSec", bus_append_parse_sec_rename },
2840 { "StartLimitBurst", bus_append_safe_atou },
2841 { "SuccessActionExitStatus", bus_append_action_exit_status },
2842 { "FailureActionExitStatus", bus_append_action_exit_status },
2843 { "Documentation", bus_append_strv },
2844 { "RequiresMountsFor", bus_append_strv },
2845 { "WantsMountsFor", bus_append_strv },
2846 { "Markers", bus_append_strv },
2847
2848 { NULL, bus_try_append_unit_dependency, unit_types_list },
2849 { NULL, bus_try_append_condition, dump_conditions },
2850 {}
2851 };
2852
2853 static const BusProperty* service_unit_properties[] = {
2854 cgroup_properties,
2855 execute_properties,
2856 kill_properties,
2857 service_properties,
2858 unit_properties,
2859 NULL,
2860 };
2861
2862 static const BusProperty* socket_unit_properties[] = {
2863 cgroup_properties,
2864 execute_properties,
2865 kill_properties,
2866 socket_properties,
2867 unit_properties,
2868 NULL,
2869 };
2870
2871 static const BusProperty* timer_unit_properties[] = {
2872 timer_properties,
2873 unit_properties,
2874 NULL,
2875 };
2876
2877 static const BusProperty* path_unit_properties[] = {
2878 path_properties,
2879 unit_properties,
2880 NULL,
2881 };
2882
2883 static const BusProperty* slice_unit_properties[] = {
2884 cgroup_properties,
2885 unit_properties,
2886 NULL,
2887 };
2888
2889 static const BusProperty* scope_unit_properties[] = {
2890 cgroup_properties,
2891 kill_properties,
2892 scope_properties,
2893 unit_properties,
2894 NULL,
2895 };
2896
2897 static const BusProperty* mount_unit_properties[] = {
2898 cgroup_properties,
2899 execute_properties,
2900 kill_properties,
2901 mount_properties,
2902 unit_properties,
2903 NULL,
2904 };
2905
2906 static const BusProperty* automount_unit_properties[] = {
2907 automount_properties,
2908 unit_properties,
2909 NULL,
2910 };
2911
2912 static const BusProperty* other_unit_properties[] = {
2913 unit_properties,
2914 NULL,
2915 };
2916
2917 static const BusProperty** unit_type_properties[_UNIT_TYPE_MAX] = {
2918 [UNIT_SERVICE] = service_unit_properties,
2919 [UNIT_SOCKET] = socket_unit_properties,
2920 [UNIT_TIMER] = timer_unit_properties,
2921 [UNIT_PATH] = path_unit_properties,
2922 [UNIT_SLICE] = slice_unit_properties,
2923 [UNIT_SCOPE] = scope_unit_properties,
2924 [UNIT_MOUNT] = mount_unit_properties,
2925 [UNIT_AUTOMOUNT] = automount_unit_properties,
2926 [UNIT_TARGET] = other_unit_properties,
2927 [UNIT_DEVICE] = other_unit_properties,
2928 [UNIT_SWAP] = other_unit_properties,
2929 };
2930
2931 int bus_append_unit_property_assignment(sd_bus_message *m, UnitType t, const char *assignment) {
2932 _cleanup_free_ char *field = NULL;
2933 const char *eq;
2934 int r;
2935
2936 assert(m);
2937 assert(assignment);
2938 assert(t >= 0 && t < _UNIT_TYPE_MAX);
2939
2940 eq = strchr(assignment, '=');
2941 if (!eq)
2942 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
2943 "Not an assignment: %s", assignment);
2944
2945
2946 field = strndup(assignment, eq - assignment);
2947 if (!field)
2948 return log_oom();
2949 eq++;
2950
2951 for (const BusProperty** tables = ASSERT_PTR(unit_type_properties[t]); *tables; tables++)
2952 for (const BusProperty *item = *tables; item->convert; item++)
2953 if (item->name) {
2954 if (streq(item->name, field))
2955 return item->convert(m, field, eq);
2956 } else {
2957 /* If .name is not set, the function must be a "try" helper */
2958 r = item->convert(m, field, eq);
2959 if (r != 0)
2960 return r;
2961 }
2962
2963 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
2964 "Unknown assignment: %s", assignment);
2965 }
2966
2967 int bus_append_unit_property_assignment_many(sd_bus_message *m, UnitType t, char * const *l) {
2968 int r;
2969
2970 assert(m);
2971
2972 STRV_FOREACH(i, l) {
2973 r = bus_append_unit_property_assignment(m, t, *i);
2974 if (r < 0)
2975 return r;
2976 }
2977
2978 return 0;
2979 }
2980
2981 void bus_dump_transient_settings(UnitType t) {
2982 assert(t >= 0 && t < _UNIT_TYPE_MAX);
2983
2984 for (const BusProperty** tables = ASSERT_PTR(unit_type_properties[t]); *tables; tables++)
2985 for (const BusProperty *item = *tables; item->convert; item++) {
2986 assert(item->name || item->dump);
2987
2988 /* Do not print deprecated names. All "Ex" variants are deprecated. */
2989 if (item->convert == warn_deprecated)
2990 continue;
2991 if (item->name && endswith(item->name, "Ex"))
2992 continue;
2993
2994 if (item->name)
2995 puts(item->name);
2996 else
2997 item->dump();
2998 }
2999 }
3000
3001 int bus_append_scope_pidref(sd_bus_message *m, const PidRef *pidref, bool allow_pidfd) {
3002 assert(m);
3003
3004 if (!pidref_is_set(pidref))
3005 return -ESRCH;
3006
3007 if (pidref->fd >= 0 && allow_pidfd)
3008 return sd_bus_message_append(
3009 m, "(sv)",
3010 "PIDFDs", "ah", 1, pidref->fd);
3011
3012 return sd_bus_message_append(
3013 m, "(sv)",
3014 "PIDs", "au", 1, pidref->pid);
3015 }
3016
3017 int bus_deserialize_and_dump_unit_file_changes(sd_bus_message *m, bool quiet) {
3018 const char *type, *path, *source;
3019 InstallChange *changes = NULL;
3020 size_t n_changes = 0;
3021 int r;
3022
3023 CLEANUP_ARRAY(changes, n_changes, install_changes_free);
3024
3025 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sss)");
3026 if (r < 0)
3027 return bus_log_parse_error(r);
3028
3029 while ((r = sd_bus_message_read(m, "(sss)", &type, &path, &source)) > 0) {
3030 InstallChangeType t;
3031
3032 /* We expect only "success" changes to be sent over the bus. Hence, reject anything
3033 * negative. */
3034 t = install_change_type_from_string(type);
3035 if (t < 0) {
3036 log_notice_errno(t, "Manager reported unknown change type \"%s\" for path \"%s\", ignoring.",
3037 type, path);
3038 continue;
3039 }
3040
3041 r = install_changes_add(&changes, &n_changes, t, path, source);
3042 if (r < 0)
3043 return r;
3044 }
3045 if (r < 0)
3046 return bus_log_parse_error(r);
3047
3048 r = sd_bus_message_exit_container(m);
3049 if (r < 0)
3050 return bus_log_parse_error(r);
3051
3052 install_changes_dump(0, NULL, changes, n_changes, quiet);
3053
3054 return 0;
3055 }
3056
3057 int unit_load_state(sd_bus *bus, const char *name, char **ret) {
3058 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
3059 _cleanup_free_ char *path = NULL;
3060 int r;
3061
3062 path = unit_dbus_path_from_name(name);
3063 if (!path)
3064 return log_oom();
3065
3066 /* This function warns on its own, because otherwise it'd be awkward to pass
3067 * the dbus error message around. */
3068
3069 r = sd_bus_get_property_string(
3070 bus,
3071 "org.freedesktop.systemd1",
3072 path,
3073 "org.freedesktop.systemd1.Unit",
3074 "LoadState",
3075 &error,
3076 ret);
3077 if (r < 0)
3078 return log_error_errno(r, "Failed to get load state of %s: %s", name, bus_error_message(&error, r));
3079
3080 return 0;
3081 }
3082
3083 int unit_info_compare(const UnitInfo *a, const UnitInfo *b) {
3084 int r;
3085
3086 /* First, order by machine */
3087 r = strcasecmp_ptr(a->machine, b->machine);
3088 if (r != 0)
3089 return r;
3090
3091 /* Second, order by unit type */
3092 r = strcasecmp_ptr(strrchr(a->id, '.'), strrchr(b->id, '.'));
3093 if (r != 0)
3094 return r;
3095
3096 /* Third, order by name */
3097 return strcasecmp(a->id, b->id);
3098 }
3099
3100 int bus_service_manager_reload(sd_bus *bus) {
3101 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
3102 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
3103 int r;
3104
3105 assert(bus);
3106
3107 r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "Reload");
3108 if (r < 0)
3109 return bus_log_create_error(r);
3110
3111 /* Reloading the daemon may take long, hence set a longer timeout here */
3112 r = sd_bus_call(bus, m, DAEMON_RELOAD_TIMEOUT_SEC, &error, NULL);
3113 if (r < 0)
3114 return log_error_errno(r, "Failed to reload service manager: %s", bus_error_message(&error, r));
3115
3116 return 0;
3117 }
3118
3119 typedef struct UnitFreezer {
3120 char *name;
3121 sd_bus *bus;
3122 } UnitFreezer;
3123
3124 /* Wait for 60 seconds at maximum for freezer operation */
3125 #define FREEZE_BUS_CALL_TIMEOUT (60 * USEC_PER_SEC)
3126
3127 UnitFreezer* unit_freezer_free(UnitFreezer *f) {
3128 if (!f)
3129 return NULL;
3130
3131 free(f->name);
3132 sd_bus_flush_close_unref(f->bus);
3133
3134 return mfree(f);
3135 }
3136
3137 int unit_freezer_new(const char *name, UnitFreezer **ret) {
3138 _cleanup_(unit_freezer_freep) UnitFreezer *f = NULL;
3139 int r;
3140
3141 assert(name);
3142 assert(ret);
3143
3144 f = new(UnitFreezer, 1);
3145 if (!f)
3146 return log_oom();
3147
3148 *f = (UnitFreezer) {
3149 .name = strdup(name),
3150 };
3151 if (!f->name)
3152 return log_oom();
3153
3154 r = bus_connect_system_systemd(&f->bus);
3155 if (r < 0)
3156 return log_error_errno(r, "Failed to open connection to systemd: %m");
3157
3158 (void) sd_bus_set_method_call_timeout(f->bus, FREEZE_BUS_CALL_TIMEOUT);
3159
3160 *ret = TAKE_PTR(f);
3161 return 0;
3162 }
3163
3164 static int unit_freezer_action(UnitFreezer *f, bool freeze) {
3165 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
3166 int r;
3167
3168 assert(f);
3169 assert(f->name);
3170 assert(f->bus);
3171
3172 r = bus_call_method(f->bus, bus_systemd_mgr,
3173 freeze ? "FreezeUnit" : "ThawUnit",
3174 &error,
3175 /* ret_reply= */ NULL,
3176 "s",
3177 f->name);
3178 if (r < 0) {
3179 if (sd_bus_error_has_names(&error,
3180 BUS_ERROR_NO_SUCH_UNIT,
3181 BUS_ERROR_UNIT_INACTIVE,
3182 SD_BUS_ERROR_NOT_SUPPORTED)) {
3183
3184 log_debug_errno(r, "Skipping freezer for '%s': %s", f->name, bus_error_message(&error, r));
3185 return 0;
3186 }
3187
3188 return log_error_errno(r, "Failed to %s unit '%s': %s",
3189 freeze ? "freeze" : "thaw", f->name, bus_error_message(&error, r));
3190 }
3191
3192 log_info("Successfully %s unit '%s'.", freeze ? "froze" : "thawed", f->name);
3193 return 1;
3194 }
3195
3196 int unit_freezer_freeze(UnitFreezer *f) {
3197 return unit_freezer_action(f, true);
3198 }
3199
3200 int unit_freezer_thaw(UnitFreezer *f) {
3201 return unit_freezer_action(f, false);
3202 }
3203
3204 ExecDirectoryFlags exec_directory_flags_from_string(const char *s) {
3205 if (isempty(s))
3206 return 0;
3207
3208 if (streq(s, "ro"))
3209 return EXEC_DIRECTORY_READ_ONLY;
3210
3211 return _EXEC_DIRECTORY_FLAGS_INVALID;
3212 }