]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/dbus-execute.c
Merge pull request #17145 from poettering/kill-chmod-and-chown-unsafe
[thirdparty/systemd.git] / src / core / dbus-execute.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
4139c1b2 2
227b8a76 3#include <sys/mount.h>
82c121a4 4#include <sys/prctl.h>
4139c1b2 5
349cc4a5 6#if HAVE_SECCOMP
57183d11
LP
7#include <seccomp.h>
8#endif
9
3ffd4af2 10#include "af-list.h"
b5efdb8a 11#include "alloc-util.h"
40af3d02 12#include "bus-get-properties.h"
cffaed83 13#include "cap-list.h"
d3070fbd 14#include "capability-util.h"
501941aa 15#include "cpu-set-util.h"
3ffd4af2 16#include "dbus-execute.h"
7e2a3fcc 17#include "dbus-util.h"
c7040b5d 18#include "env-util.h"
cffaed83 19#include "errno-list.h"
2e59b241 20#include "escape.h"
3ffd4af2
LP
21#include "execute.h"
22#include "fd-util.h"
23#include "fileio.h"
08f3be7a 24#include "hexdecoct.h"
d3070fbd 25#include "io-util.h"
3ffd4af2 26#include "ioprio.h"
d3070fbd 27#include "journal-util.h"
049af8ad 28#include "mountpoint-util.h"
417116f2 29#include "namespace.h"
6bedfcbb 30#include "parse-util.h"
9b15b784 31#include "path-util.h"
7b3e062c 32#include "process-util.h"
78f22b97 33#include "rlimit-util.h"
349cc4a5 34#if HAVE_SECCOMP
57183d11
LP
35#include "seccomp-util.h"
36#endif
cffaed83 37#include "securebits-util.h"
2e59b241 38#include "specifier.h"
6bedfcbb 39#include "strv.h"
7ccbd1ae 40#include "syslog-util.h"
f900f582 41#include "unit-printf.h"
6f3e7985 42#include "user-util.h"
6bedfcbb 43#include "utf8.h"
57183d11 44
718db961 45BUS_DEFINE_PROPERTY_GET_ENUM(bus_property_get_exec_output, exec_output, ExecOutput);
718db961 46static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_exec_input, exec_input, ExecInput);
023a4f67 47static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_exec_utmp_mode, exec_utmp_mode, ExecUtmpMode);
53f47dfc 48static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_exec_preserve_mode, exec_preserve_mode, ExecPreserveMode);
b1edf445 49static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_exec_keyring_mode, exec_keyring_mode, ExecKeyringMode);
4e399953
LP
50static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_protect_proc, protect_proc, ProtectProc);
51static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_proc_subset, proc_subset, ProcSubset);
73b84e92
YW
52static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_protect_home, protect_home, ProtectHome);
53static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_protect_system, protect_system, ProtectSystem);
491eecb3 54static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_personality, personality, unsigned long);
019b34ca
YW
55static BUS_DEFINE_PROPERTY_GET(property_get_ioprio, "i", ExecContext, exec_context_get_effective_ioprio);
56static BUS_DEFINE_PROPERTY_GET2(property_get_ioprio_class, "i", ExecContext, exec_context_get_effective_ioprio, IOPRIO_PRIO_CLASS);
57static BUS_DEFINE_PROPERTY_GET2(property_get_ioprio_priority, "i", ExecContext, exec_context_get_effective_ioprio, IOPRIO_PRIO_DATA);
92c23c5a
YW
58static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_empty_string, "s", NULL);
59static BUS_DEFINE_PROPERTY_GET_REF(property_get_syslog_level, "i", int, LOG_PRI);
60static BUS_DEFINE_PROPERTY_GET_REF(property_get_syslog_facility, "i", int, LOG_FAC);
e2b2fb7f
MS
61static BUS_DEFINE_PROPERTY_GET(property_get_cpu_affinity_from_numa, "b", ExecContext, exec_context_get_cpu_affinity_from_numa);
62
718db961
LP
63static int property_get_environment_files(
64 sd_bus *bus,
65 const char *path,
66 const char *interface,
67 const char *property,
68 sd_bus_message *reply,
ebcf1f97
LP
69 void *userdata,
70 sd_bus_error *error) {
8c7be95e 71
718db961
LP
72 ExecContext *c = userdata;
73 char **j;
74 int r;
8c7be95e 75
718db961
LP
76 assert(bus);
77 assert(reply);
78 assert(c);
79
80 r = sd_bus_message_open_container(reply, 'a', "(sb)");
81 if (r < 0)
82 return r;
8c7be95e 83
718db961
LP
84 STRV_FOREACH(j, c->environment_files) {
85 const char *fn = *j;
8c7be95e 86
718db961
LP
87 r = sd_bus_message_append(reply, "(sb)", fn[0] == '-' ? fn + 1 : fn, fn[0] == '-');
88 if (r < 0)
89 return r;
8c7be95e
LP
90 }
91
718db961
LP
92 return sd_bus_message_close_container(reply);
93}
94
718db961
LP
95static int property_get_oom_score_adjust(
96 sd_bus *bus,
97 const char *path,
98 const char *interface,
99 const char *property,
100 sd_bus_message *reply,
ebcf1f97
LP
101 void *userdata,
102 sd_bus_error *error) {
718db961 103
718db961 104 ExecContext *c = userdata;
82c121a4 105 int32_t n;
b94fb74b 106 int r;
82c121a4 107
718db961
LP
108 assert(bus);
109 assert(reply);
82c121a4
LP
110 assert(c);
111
dd6c17b1
LP
112 if (c->oom_score_adjust_set)
113 n = c->oom_score_adjust;
82c121a4 114 else {
68eda4bd 115 _cleanup_free_ char *t = NULL;
82c121a4
LP
116
117 n = 0;
b94fb74b
ZJS
118 r = read_one_line_file("/proc/self/oom_score_adj", &t);
119 if (r < 0)
120 log_debug_errno(r, "Failed to read /proc/self/oom_score_adj, ignoring: %m");
121 else {
122 r = safe_atoi32(t, &n);
123 if (r < 0)
124 log_debug_errno(r, "Failed to parse \"%s\" from /proc/self/oom_score_adj, ignoring: %m", t);
125 }
82c121a4
LP
126 }
127
718db961 128 return sd_bus_message_append(reply, "i", n);
82c121a4
LP
129}
130
ad21e542
ZJS
131static int property_get_coredump_filter(
132 sd_bus *bus,
133 const char *path,
134 const char *interface,
135 const char *property,
136 sd_bus_message *reply,
137 void *userdata,
138 sd_bus_error *error) {
139
140 ExecContext *c = userdata;
141 uint64_t n;
142 int r;
143
144 assert(bus);
145 assert(reply);
146 assert(c);
147
148 if (c->coredump_filter_set)
149 n = c->coredump_filter;
150 else {
151 _cleanup_free_ char *t = NULL;
152
153 n = COREDUMP_FILTER_MASK_DEFAULT;
154 r = read_one_line_file("/proc/self/coredump_filter", &t);
155 if (r < 0)
156 log_debug_errno(r, "Failed to read /proc/self/coredump_filter, ignoring: %m");
157 else {
158 r = safe_atoux64(t, &n);
159 if (r < 0)
160 log_debug_errno(r, "Failed to parse \"%s\" from /proc/self/coredump_filter, ignoring: %m", t);
161 }
162 }
163
164 return sd_bus_message_append(reply, "t", n);
165}
166
718db961
LP
167static int property_get_nice(
168 sd_bus *bus,
169 const char *path,
170 const char *interface,
171 const char *property,
172 sd_bus_message *reply,
ebcf1f97
LP
173 void *userdata,
174 sd_bus_error *error) {
718db961 175
718db961 176 ExecContext *c = userdata;
82c121a4
LP
177 int32_t n;
178
718db961
LP
179 assert(bus);
180 assert(reply);
82c121a4
LP
181 assert(c);
182
183 if (c->nice_set)
184 n = c->nice;
718db961
LP
185 else {
186 errno = 0;
82c121a4 187 n = getpriority(PRIO_PROCESS, 0);
b3267152 188 if (errno > 0)
718db961
LP
189 n = 0;
190 }
82c121a4 191
718db961 192 return sd_bus_message_append(reply, "i", n);
82c121a4
LP
193}
194
718db961
LP
195static int property_get_cpu_sched_policy(
196 sd_bus *bus,
197 const char *path,
198 const char *interface,
199 const char *property,
200 sd_bus_message *reply,
ebcf1f97
LP
201 void *userdata,
202 sd_bus_error *error) {
718db961
LP
203
204 ExecContext *c = userdata;
82c121a4
LP
205 int32_t n;
206
718db961
LP
207 assert(bus);
208 assert(reply);
82c121a4
LP
209 assert(c);
210
211 if (c->cpu_sched_set)
212 n = c->cpu_sched_policy;
718db961 213 else {
82c121a4 214 n = sched_getscheduler(0);
718db961
LP
215 if (n < 0)
216 n = SCHED_OTHER;
217 }
82c121a4 218
718db961 219 return sd_bus_message_append(reply, "i", n);
82c121a4
LP
220}
221
718db961
LP
222static int property_get_cpu_sched_priority(
223 sd_bus *bus,
224 const char *path,
225 const char *interface,
226 const char *property,
227 sd_bus_message *reply,
ebcf1f97
LP
228 void *userdata,
229 sd_bus_error *error) {
718db961
LP
230
231 ExecContext *c = userdata;
82c121a4
LP
232 int32_t n;
233
718db961
LP
234 assert(bus);
235 assert(reply);
82c121a4
LP
236 assert(c);
237
238 if (c->cpu_sched_set)
239 n = c->cpu_sched_priority;
240 else {
b92bea5d 241 struct sched_param p = {};
82c121a4 242
82c121a4
LP
243 if (sched_getparam(0, &p) >= 0)
244 n = p.sched_priority;
e62d8c39
ZJS
245 else
246 n = 0;
82c121a4
LP
247 }
248
718db961 249 return sd_bus_message_append(reply, "i", n);
82c121a4
LP
250}
251
718db961
LP
252static int property_get_cpu_affinity(
253 sd_bus *bus,
254 const char *path,
255 const char *interface,
256 const char *property,
257 sd_bus_message *reply,
ebcf1f97
LP
258 void *userdata,
259 sd_bus_error *error) {
82c121a4 260
718db961 261 ExecContext *c = userdata;
e2b2fb7f 262 _cleanup_(cpu_set_reset) CPUSet s = {};
75e40119
MS
263 _cleanup_free_ uint8_t *array = NULL;
264 size_t allocated;
82c121a4 265
718db961
LP
266 assert(bus);
267 assert(reply);
268 assert(c);
82c121a4 269
e2b2fb7f
MS
270 if (c->cpu_affinity_from_numa) {
271 int r;
272
273 r = numa_to_cpu_set(&c->numa_policy, &s);
274 if (r < 0)
275 return r;
276 }
277
278 (void) cpu_set_to_dbus(c->cpu_affinity_from_numa ? &s : &c->cpu_set, &array, &allocated);
279
75e40119 280 return sd_bus_message_append_array(reply, 'y', array, allocated);
82c121a4
LP
281}
282
b070c7c0
MS
283static int property_get_numa_mask(
284 sd_bus *bus,
285 const char *path,
286 const char *interface,
287 const char *property,
288 sd_bus_message *reply,
289 void *userdata,
290 sd_bus_error *error) {
291
292 ExecContext *c = userdata;
293 _cleanup_free_ uint8_t *array = NULL;
294 size_t allocated;
295
296 assert(bus);
297 assert(reply);
298 assert(c);
299
300 (void) cpu_set_to_dbus(&c->numa_policy.nodes, &array, &allocated);
301
302 return sd_bus_message_append_array(reply, 'y', array, allocated);
303}
304
305static int property_get_numa_policy(
306 sd_bus *bus,
307 const char *path,
308 const char *interface,
309 const char *property,
310 sd_bus_message *reply,
311 void *userdata,
312 sd_bus_error *error) {
313 ExecContext *c = userdata;
314 int32_t policy;
315
316 assert(bus);
317 assert(reply);
318 assert(c);
319
320 policy = numa_policy_get_type(&c->numa_policy);
321
322 return sd_bus_message_append_basic(reply, 'i', &policy);
323}
324
718db961
LP
325static int property_get_timer_slack_nsec(
326 sd_bus *bus,
327 const char *path,
328 const char *interface,
329 const char *property,
330 sd_bus_message *reply,
ebcf1f97
LP
331 void *userdata,
332 sd_bus_error *error) {
718db961
LP
333
334 ExecContext *c = userdata;
82c121a4
LP
335 uint64_t u;
336
718db961
LP
337 assert(bus);
338 assert(reply);
82c121a4
LP
339 assert(c);
340
3a43da28 341 if (c->timer_slack_nsec != NSEC_INFINITY)
03fae018 342 u = (uint64_t) c->timer_slack_nsec;
82c121a4
LP
343 else
344 u = (uint64_t) prctl(PR_GET_TIMERSLACK);
345
718db961 346 return sd_bus_message_append(reply, "t", u);
82c121a4
LP
347}
348
718db961
LP
349static int property_get_syscall_filter(
350 sd_bus *bus,
351 const char *path,
352 const char *interface,
353 const char *property,
354 sd_bus_message *reply,
ebcf1f97
LP
355 void *userdata,
356 sd_bus_error *error) {
82c121a4 357
17df7223
LP
358 ExecContext *c = userdata;
359 _cleanup_strv_free_ char **l = NULL;
57183d11
LP
360 int r;
361
349cc4a5 362#if HAVE_SECCOMP
8cfa775f 363 void *id, *val;
351a19b1 364#endif
17df7223
LP
365
366 assert(bus);
367 assert(reply);
368 assert(c);
369
57183d11
LP
370 r = sd_bus_message_open_container(reply, 'r', "bas");
371 if (r < 0)
372 return r;
373
6b000af4 374 r = sd_bus_message_append(reply, "b", c->syscall_allow_list);
57183d11
LP
375 if (r < 0)
376 return r;
377
349cc4a5 378#if HAVE_SECCOMP
90e74a66 379 HASHMAP_FOREACH_KEY(val, id, c->syscall_filter) {
8cfa775f
YW
380 _cleanup_free_ char *name = NULL;
381 const char *e = NULL;
382 char *s;
383 int num = PTR_TO_INT(val);
17df7223
LP
384
385 name = seccomp_syscall_resolve_num_arch(SCMP_ARCH_NATIVE, PTR_TO_INT(id) - 1);
386 if (!name)
387 continue;
388
8cfa775f 389 if (num >= 0) {
005bfaf1 390 e = seccomp_errno_or_action_to_string(num);
8cfa775f
YW
391 if (e) {
392 s = strjoin(name, ":", e);
393 if (!s)
394 return -ENOMEM;
395 } else {
396 r = asprintf(&s, "%s:%d", name, num);
397 if (r < 0)
398 return -ENOMEM;
399 }
ae2a15bc
LP
400 } else
401 s = TAKE_PTR(name);
8cfa775f
YW
402
403 r = strv_consume(&l, s);
6e18964d
ZJS
404 if (r < 0)
405 return r;
17df7223 406 }
351a19b1 407#endif
17df7223
LP
408
409 strv_sort(l);
410
57183d11
LP
411 r = sd_bus_message_append_strv(reply, l);
412 if (r < 0)
413 return r;
17df7223 414
57183d11
LP
415 return sd_bus_message_close_container(reply);
416}
17df7223 417
9df2cdd8
TM
418static int property_get_syscall_log(
419 sd_bus *bus,
420 const char *path,
421 const char *interface,
422 const char *property,
423 sd_bus_message *reply,
424 void *userdata,
425 sd_bus_error *error) {
426
427 ExecContext *c = userdata;
428 _cleanup_strv_free_ char **l = NULL;
429 int r;
430
431#if HAVE_SECCOMP
432 void *id, *val;
433#endif
434
435 assert(bus);
436 assert(reply);
437 assert(c);
438
439 r = sd_bus_message_open_container(reply, 'r', "bas");
440 if (r < 0)
441 return r;
442
443 r = sd_bus_message_append(reply, "b", c->syscall_log_allow_list);
444 if (r < 0)
445 return r;
446
447#if HAVE_SECCOMP
448 HASHMAP_FOREACH_KEY(val, id, c->syscall_log) {
449 char *name = NULL;
450
451 name = seccomp_syscall_resolve_num_arch(SCMP_ARCH_NATIVE, PTR_TO_INT(id) - 1);
452 if (!name)
453 continue;
454
455 r = strv_consume(&l, name);
456 if (r < 0)
457 return r;
458 }
459#endif
460
461 strv_sort(l);
462
463 r = sd_bus_message_append_strv(reply, l);
464 if (r < 0)
465 return r;
466
467 return sd_bus_message_close_container(reply);
468}
469
57183d11
LP
470static int property_get_syscall_archs(
471 sd_bus *bus,
472 const char *path,
473 const char *interface,
474 const char *property,
475 sd_bus_message *reply,
476 void *userdata,
477 sd_bus_error *error) {
478
479 ExecContext *c = userdata;
480 _cleanup_strv_free_ char **l = NULL;
481 int r;
482
349cc4a5 483#if HAVE_SECCOMP
57183d11
LP
484 void *id;
485#endif
486
487 assert(bus);
488 assert(reply);
489 assert(c);
17df7223 490
349cc4a5 491#if HAVE_SECCOMP
90e74a66 492 SET_FOREACH(id, c->syscall_archs) {
57183d11
LP
493 const char *name;
494
495 name = seccomp_arch_to_string(PTR_TO_UINT32(id) - 1);
496 if (!name)
497 continue;
498
499 r = strv_extend(&l, name);
500 if (r < 0)
501 return -ENOMEM;
17df7223 502 }
57183d11
LP
503#endif
504
505 strv_sort(l);
506
507 r = sd_bus_message_append_strv(reply, l);
508 if (r < 0)
509 return r;
17df7223 510
57183d11 511 return 0;
17df7223
LP
512}
513
5f8640fb
LP
514static int property_get_selinux_context(
515 sd_bus *bus,
516 const char *path,
517 const char *interface,
518 const char *property,
519 sd_bus_message *reply,
520 void *userdata,
521 sd_bus_error *error) {
522
523 ExecContext *c = userdata;
524
525 assert(bus);
526 assert(reply);
527 assert(c);
528
529 return sd_bus_message_append(reply, "(bs)", c->selinux_context_ignore, c->selinux_context);
530}
531
eef65bf3
MS
532static int property_get_apparmor_profile(
533 sd_bus *bus,
534 const char *path,
535 const char *interface,
536 const char *property,
537 sd_bus_message *reply,
538 void *userdata,
539 sd_bus_error *error) {
540
541 ExecContext *c = userdata;
542
543 assert(bus);
544 assert(reply);
545 assert(c);
546
547 return sd_bus_message_append(reply, "(bs)", c->apparmor_profile_ignore, c->apparmor_profile);
548}
549
2ca620c4
WC
550static int property_get_smack_process_label(
551 sd_bus *bus,
552 const char *path,
553 const char *interface,
554 const char *property,
555 sd_bus_message *reply,
556 void *userdata,
557 sd_bus_error *error) {
558
559 ExecContext *c = userdata;
560
561 assert(bus);
562 assert(reply);
563 assert(c);
564
565 return sd_bus_message_append(reply, "(bs)", c->smack_process_label_ignore, c->smack_process_label);
566}
567
4298d0b5
LP
568static int property_get_address_families(
569 sd_bus *bus,
570 const char *path,
571 const char *interface,
572 const char *property,
573 sd_bus_message *reply,
574 void *userdata,
575 sd_bus_error *error) {
576
577 ExecContext *c = userdata;
578 _cleanup_strv_free_ char **l = NULL;
4298d0b5
LP
579 void *af;
580 int r;
581
582 assert(bus);
583 assert(reply);
584 assert(c);
585
586 r = sd_bus_message_open_container(reply, 'r', "bas");
587 if (r < 0)
588 return r;
589
6b000af4 590 r = sd_bus_message_append(reply, "b", c->address_families_allow_list);
4298d0b5
LP
591 if (r < 0)
592 return r;
593
90e74a66 594 SET_FOREACH(af, c->address_families) {
4298d0b5
LP
595 const char *name;
596
597 name = af_to_name(PTR_TO_INT(af));
598 if (!name)
599 continue;
600
601 r = strv_extend(&l, name);
602 if (r < 0)
603 return -ENOMEM;
604 }
605
606 strv_sort(l);
607
608 r = sd_bus_message_append_strv(reply, l);
609 if (r < 0)
610 return r;
611
612 return sd_bus_message_close_container(reply);
613}
614
5f5d8eab
LP
615static int property_get_working_directory(
616 sd_bus *bus,
617 const char *path,
618 const char *interface,
619 const char *property,
620 sd_bus_message *reply,
621 void *userdata,
622 sd_bus_error *error) {
623
624 ExecContext *c = userdata;
625 const char *wd;
626
627 assert(bus);
628 assert(reply);
629 assert(c);
630
631 if (c->working_directory_home)
632 wd = "~";
633 else
634 wd = c->working_directory;
635
636 if (c->working_directory_missing_ok)
637 wd = strjoina("!", wd);
638
639 return sd_bus_message_append(reply, "s", wd);
640}
641
5073ff6b 642static int property_get_stdio_fdname(
52c239d7
LB
643 sd_bus *bus,
644 const char *path,
645 const char *interface,
646 const char *property,
647 sd_bus_message *reply,
648 void *userdata,
649 sd_bus_error *error) {
650
651 ExecContext *c = userdata;
5073ff6b 652 int fileno;
52c239d7
LB
653
654 assert(bus);
655 assert(c);
656 assert(property);
657 assert(reply);
658
5073ff6b
LP
659 if (streq(property, "StandardInputFileDescriptorName"))
660 fileno = STDIN_FILENO;
661 else if (streq(property, "StandardOutputFileDescriptorName"))
662 fileno = STDOUT_FILENO;
663 else {
664 assert(streq(property, "StandardErrorFileDescriptorName"));
665 fileno = STDERR_FILENO;
666 }
52c239d7 667
5073ff6b 668 return sd_bus_message_append(reply, "s", exec_context_fdname(c, fileno));
52c239d7
LB
669}
670
08f3be7a 671static int property_get_input_data(
52c239d7
LB
672 sd_bus *bus,
673 const char *path,
674 const char *interface,
675 const char *property,
676 sd_bus_message *reply,
677 void *userdata,
678 sd_bus_error *error) {
679
680 ExecContext *c = userdata;
52c239d7
LB
681
682 assert(bus);
683 assert(c);
684 assert(property);
685 assert(reply);
686
08f3be7a 687 return sd_bus_message_append_array(reply, 'y', c->stdin_data, c->stdin_data_size);
52c239d7
LB
688}
689
d2d6c096
LP
690static int property_get_bind_paths(
691 sd_bus *bus,
692 const char *path,
693 const char *interface,
694 const char *property,
695 sd_bus_message *reply,
696 void *userdata,
697 sd_bus_error *error) {
698
699 ExecContext *c = userdata;
700 unsigned i;
701 bool ro;
702 int r;
703
704 assert(bus);
705 assert(c);
706 assert(property);
707 assert(reply);
708
5d904a6a 709 ro = strstr(property, "ReadOnly");
d2d6c096
LP
710
711 r = sd_bus_message_open_container(reply, 'a', "(ssbt)");
712 if (r < 0)
713 return r;
714
715 for (i = 0; i < c->n_bind_mounts; i++) {
716
717 if (ro != c->bind_mounts[i].read_only)
718 continue;
719
720 r = sd_bus_message_append(
721 reply, "(ssbt)",
722 c->bind_mounts[i].source,
723 c->bind_mounts[i].destination,
724 c->bind_mounts[i].ignore_enoent,
c9b06108 725 c->bind_mounts[i].recursive ? (uint64_t) MS_REC : (uint64_t) 0);
d2d6c096
LP
726 if (r < 0)
727 return r;
728 }
729
730 return sd_bus_message_close_container(reply);
731}
732
784ad252
YW
733static int property_get_temporary_filesystems(
734 sd_bus *bus,
735 const char *path,
736 const char *interface,
737 const char *property,
738 sd_bus_message *reply,
739 void *userdata,
740 sd_bus_error *error) {
741
742 ExecContext *c = userdata;
743 unsigned i;
744 int r;
745
746 assert(bus);
747 assert(c);
748 assert(property);
749 assert(reply);
750
751 r = sd_bus_message_open_container(reply, 'a', "(ss)");
752 if (r < 0)
753 return r;
754
755 for (i = 0; i < c->n_temporary_filesystems; i++) {
756 TemporaryFileSystem *t = c->temporary_filesystems + i;
757
758 r = sd_bus_message_append(
759 reply, "(ss)",
760 t->path,
761 t->options);
762 if (r < 0)
763 return r;
764 }
765
766 return sd_bus_message_close_container(reply);
767}
768
d3070fbd
LP
769static int property_get_log_extra_fields(
770 sd_bus *bus,
771 const char *path,
772 const char *interface,
773 const char *property,
774 sd_bus_message *reply,
775 void *userdata,
776 sd_bus_error *error) {
777
778 ExecContext *c = userdata;
779 size_t i;
780 int r;
781
782 assert(bus);
783 assert(c);
784 assert(property);
785 assert(reply);
786
787 r = sd_bus_message_open_container(reply, 'a', "ay");
788 if (r < 0)
789 return r;
790
791 for (i = 0; i < c->n_log_extra_fields; i++) {
792 r = sd_bus_message_append_array(reply, 'y', c->log_extra_fields[i].iov_base, c->log_extra_fields[i].iov_len);
793 if (r < 0)
794 return r;
795 }
796
797 return sd_bus_message_close_container(reply);
798}
799
bb0c0d6f
LP
800static int property_get_set_credential(
801 sd_bus *bus,
802 const char *path,
803 const char *interface,
804 const char *property,
805 sd_bus_message *reply,
806 void *userdata,
807 sd_bus_error *error) {
808
809 ExecContext *c = userdata;
810 ExecSetCredential *sc;
bb0c0d6f
LP
811 int r;
812
813 assert(bus);
814 assert(c);
815 assert(property);
816 assert(reply);
817
818 r = sd_bus_message_open_container(reply, 'a', "(say)");
819 if (r < 0)
820 return r;
821
90e74a66 822 HASHMAP_FOREACH(sc, c->set_credentials) {
bb0c0d6f
LP
823
824 r = sd_bus_message_open_container(reply, 'r', "say");
825 if (r < 0)
826 return r;
827
828 r = sd_bus_message_append(reply, "s", sc->id);
829 if (r < 0)
830 return r;
831
832 r = sd_bus_message_append_array(reply, 'y', sc->data, sc->size);
833 if (r < 0)
834 return r;
835
836 r = sd_bus_message_close_container(reply);
837 if (r < 0)
838 return r;
839 }
840
841 return sd_bus_message_close_container(reply);
842}
843
844static int property_get_load_credential(
845 sd_bus *bus,
846 const char *path,
847 const char *interface,
848 const char *property,
849 sd_bus_message *reply,
850 void *userdata,
851 sd_bus_error *error) {
852
853 ExecContext *c = userdata;
854 char **i, **j;
855 int r;
856
857 assert(bus);
858 assert(c);
859 assert(property);
860 assert(reply);
861
862 r = sd_bus_message_open_container(reply, 'a', "(ss)");
863 if (r < 0)
864 return r;
865
866 STRV_FOREACH_PAIR(i, j, c->load_credentials) {
867 r = sd_bus_message_append(reply, "(ss)", *i, *j);
868 if (r < 0)
869 return r;
870 }
871
872 return sd_bus_message_close_container(reply);
873}
874
0389f4fa
LB
875static int property_get_root_hash(
876 sd_bus *bus,
877 const char *path,
878 const char *interface,
879 const char *property,
880 sd_bus_message *reply,
881 void *userdata,
882 sd_bus_error *error) {
883
884 ExecContext *c = userdata;
885
886 assert(bus);
887 assert(c);
888 assert(property);
889 assert(reply);
890
891 return sd_bus_message_append_array(reply, 'y', c->root_hash, c->root_hash_size);
892}
893
d4d55b0d
LB
894static int property_get_root_hash_sig(
895 sd_bus *bus,
896 const char *path,
897 const char *interface,
898 const char *property,
899 sd_bus_message *reply,
900 void *userdata,
901 sd_bus_error *error) {
902
903 ExecContext *c = userdata;
904
905 assert(bus);
906 assert(c);
907 assert(property);
908 assert(reply);
909
910 return sd_bus_message_append_array(reply, 'y', c->root_hash_sig, c->root_hash_sig_size);
911}
912
18d73705
LB
913static int property_get_root_image_options(
914 sd_bus *bus,
915 const char *path,
916 const char *interface,
917 const char *property,
918 sd_bus_message *reply,
919 void *userdata,
920 sd_bus_error *error) {
921
922 ExecContext *c = userdata;
923 MountOptions *m;
924 int r;
925
926 assert(bus);
927 assert(c);
928 assert(property);
929 assert(reply);
930
9ece6444 931 r = sd_bus_message_open_container(reply, 'a', "(ss)");
18d73705
LB
932 if (r < 0)
933 return r;
934
935 LIST_FOREACH(mount_options, m, c->root_image_options) {
9ece6444
LB
936 r = sd_bus_message_append(reply, "(ss)",
937 partition_designator_to_string(m->partition_designator),
938 m->options);
18d73705
LB
939 if (r < 0)
940 return r;
941 }
942
943 return sd_bus_message_close_container(reply);
944}
945
b3d13314
LB
946static int property_get_mount_images(
947 sd_bus *bus,
948 const char *path,
949 const char *interface,
950 const char *property,
951 sd_bus_message *reply,
952 void *userdata,
953 sd_bus_error *error) {
954
955 ExecContext *c = userdata;
956 int r;
957
958 assert(bus);
959 assert(c);
960 assert(property);
961 assert(reply);
962
427353f6 963 r = sd_bus_message_open_container(reply, 'a', "(ssba(ss))");
b3d13314
LB
964 if (r < 0)
965 return r;
966
967 for (size_t i = 0; i < c->n_mount_images; i++) {
427353f6
LB
968 MountOptions *m;
969
970 r = sd_bus_message_open_container(reply, SD_BUS_TYPE_STRUCT, "ssba(ss)");
971 if (r < 0)
972 return r;
b3d13314 973 r = sd_bus_message_append(
427353f6 974 reply, "ssb",
b3d13314
LB
975 c->mount_images[i].source,
976 c->mount_images[i].destination,
977 c->mount_images[i].ignore_enoent);
978 if (r < 0)
979 return r;
427353f6
LB
980 r = sd_bus_message_open_container(reply, 'a', "(ss)");
981 if (r < 0)
982 return r;
983 LIST_FOREACH(mount_options, m, c->mount_images[i].mount_options) {
984 r = sd_bus_message_append(reply, "(ss)",
985 partition_designator_to_string(m->partition_designator),
986 m->options);
987 if (r < 0)
988 return r;
989 }
990 r = sd_bus_message_close_container(reply);
991 if (r < 0)
992 return r;
993 r = sd_bus_message_close_container(reply);
994 if (r < 0)
995 return r;
b3d13314
LB
996 }
997
998 return sd_bus_message_close_container(reply);
999}
1000
718db961
LP
1001const sd_bus_vtable bus_exec_vtable[] = {
1002 SD_BUS_VTABLE_START(0),
556089dc
LP
1003 SD_BUS_PROPERTY("Environment", "as", NULL, offsetof(ExecContext, environment), SD_BUS_VTABLE_PROPERTY_CONST),
1004 SD_BUS_PROPERTY("EnvironmentFiles", "a(sb)", property_get_environment_files, 0, SD_BUS_VTABLE_PROPERTY_CONST),
b4c14404 1005 SD_BUS_PROPERTY("PassEnvironment", "as", NULL, offsetof(ExecContext, pass_environment), SD_BUS_VTABLE_PROPERTY_CONST),
00819cc1 1006 SD_BUS_PROPERTY("UnsetEnvironment", "as", NULL, offsetof(ExecContext, unset_environment), SD_BUS_VTABLE_PROPERTY_CONST),
556089dc 1007 SD_BUS_PROPERTY("UMask", "u", bus_property_get_mode, offsetof(ExecContext, umask), SD_BUS_VTABLE_PROPERTY_CONST),
c9d031c3 1008 SD_BUS_PROPERTY("LimitCPU", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_CPU]), SD_BUS_VTABLE_PROPERTY_CONST),
147f6858 1009 SD_BUS_PROPERTY("LimitCPUSoft", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_CPU]), SD_BUS_VTABLE_PROPERTY_CONST),
c9d031c3 1010 SD_BUS_PROPERTY("LimitFSIZE", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_FSIZE]), SD_BUS_VTABLE_PROPERTY_CONST),
147f6858 1011 SD_BUS_PROPERTY("LimitFSIZESoft", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_FSIZE]), SD_BUS_VTABLE_PROPERTY_CONST),
c9d031c3 1012 SD_BUS_PROPERTY("LimitDATA", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_DATA]), SD_BUS_VTABLE_PROPERTY_CONST),
147f6858 1013 SD_BUS_PROPERTY("LimitDATASoft", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_DATA]), SD_BUS_VTABLE_PROPERTY_CONST),
c9d031c3 1014 SD_BUS_PROPERTY("LimitSTACK", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_STACK]), SD_BUS_VTABLE_PROPERTY_CONST),
147f6858 1015 SD_BUS_PROPERTY("LimitSTACKSoft", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_STACK]), SD_BUS_VTABLE_PROPERTY_CONST),
c9d031c3 1016 SD_BUS_PROPERTY("LimitCORE", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_CORE]), SD_BUS_VTABLE_PROPERTY_CONST),
147f6858 1017 SD_BUS_PROPERTY("LimitCORESoft", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_CORE]), SD_BUS_VTABLE_PROPERTY_CONST),
c9d031c3 1018 SD_BUS_PROPERTY("LimitRSS", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_RSS]), SD_BUS_VTABLE_PROPERTY_CONST),
147f6858 1019 SD_BUS_PROPERTY("LimitRSSSoft", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_RSS]), SD_BUS_VTABLE_PROPERTY_CONST),
c9d031c3 1020 SD_BUS_PROPERTY("LimitNOFILE", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_NOFILE]), SD_BUS_VTABLE_PROPERTY_CONST),
147f6858 1021 SD_BUS_PROPERTY("LimitNOFILESoft", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_NOFILE]), SD_BUS_VTABLE_PROPERTY_CONST),
c9d031c3 1022 SD_BUS_PROPERTY("LimitAS", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_AS]), SD_BUS_VTABLE_PROPERTY_CONST),
147f6858 1023 SD_BUS_PROPERTY("LimitASSoft", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_AS]), SD_BUS_VTABLE_PROPERTY_CONST),
c9d031c3 1024 SD_BUS_PROPERTY("LimitNPROC", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_NPROC]), SD_BUS_VTABLE_PROPERTY_CONST),
147f6858 1025 SD_BUS_PROPERTY("LimitNPROCSoft", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_NPROC]), SD_BUS_VTABLE_PROPERTY_CONST),
c9d031c3 1026 SD_BUS_PROPERTY("LimitMEMLOCK", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_MEMLOCK]), SD_BUS_VTABLE_PROPERTY_CONST),
147f6858 1027 SD_BUS_PROPERTY("LimitMEMLOCKSoft", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_MEMLOCK]), SD_BUS_VTABLE_PROPERTY_CONST),
c9d031c3 1028 SD_BUS_PROPERTY("LimitLOCKS", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_LOCKS]), SD_BUS_VTABLE_PROPERTY_CONST),
147f6858 1029 SD_BUS_PROPERTY("LimitLOCKSSoft", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_LOCKS]), SD_BUS_VTABLE_PROPERTY_CONST),
c9d031c3 1030 SD_BUS_PROPERTY("LimitSIGPENDING", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_SIGPENDING]), SD_BUS_VTABLE_PROPERTY_CONST),
147f6858 1031 SD_BUS_PROPERTY("LimitSIGPENDINGSoft", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_SIGPENDING]), SD_BUS_VTABLE_PROPERTY_CONST),
c9d031c3 1032 SD_BUS_PROPERTY("LimitMSGQUEUE", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_MSGQUEUE]), SD_BUS_VTABLE_PROPERTY_CONST),
147f6858 1033 SD_BUS_PROPERTY("LimitMSGQUEUESoft", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_MSGQUEUE]), SD_BUS_VTABLE_PROPERTY_CONST),
c9d031c3 1034 SD_BUS_PROPERTY("LimitNICE", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_NICE]), SD_BUS_VTABLE_PROPERTY_CONST),
147f6858 1035 SD_BUS_PROPERTY("LimitNICESoft", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_NICE]), SD_BUS_VTABLE_PROPERTY_CONST),
c9d031c3 1036 SD_BUS_PROPERTY("LimitRTPRIO", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_RTPRIO]), SD_BUS_VTABLE_PROPERTY_CONST),
147f6858 1037 SD_BUS_PROPERTY("LimitRTPRIOSoft", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_RTPRIO]), SD_BUS_VTABLE_PROPERTY_CONST),
c9d031c3 1038 SD_BUS_PROPERTY("LimitRTTIME", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_RTTIME]), SD_BUS_VTABLE_PROPERTY_CONST),
147f6858 1039 SD_BUS_PROPERTY("LimitRTTIMESoft", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_RTTIME]), SD_BUS_VTABLE_PROPERTY_CONST),
5f5d8eab 1040 SD_BUS_PROPERTY("WorkingDirectory", "s", property_get_working_directory, 0, SD_BUS_VTABLE_PROPERTY_CONST),
556089dc 1041 SD_BUS_PROPERTY("RootDirectory", "s", NULL, offsetof(ExecContext, root_directory), SD_BUS_VTABLE_PROPERTY_CONST),
915e6d16 1042 SD_BUS_PROPERTY("RootImage", "s", NULL, offsetof(ExecContext, root_image), SD_BUS_VTABLE_PROPERTY_CONST),
9ece6444 1043 SD_BUS_PROPERTY("RootImageOptions", "a(ss)", property_get_root_image_options, 0, SD_BUS_VTABLE_PROPERTY_CONST),
0389f4fa
LB
1044 SD_BUS_PROPERTY("RootHash", "ay", property_get_root_hash, 0, SD_BUS_VTABLE_PROPERTY_CONST),
1045 SD_BUS_PROPERTY("RootHashPath", "s", NULL, offsetof(ExecContext, root_hash_path), SD_BUS_VTABLE_PROPERTY_CONST),
d4d55b0d
LB
1046 SD_BUS_PROPERTY("RootHashSignature", "ay", property_get_root_hash_sig, 0, SD_BUS_VTABLE_PROPERTY_CONST),
1047 SD_BUS_PROPERTY("RootHashSignaturePath", "s", NULL, offsetof(ExecContext, root_hash_sig_path), SD_BUS_VTABLE_PROPERTY_CONST),
0389f4fa 1048 SD_BUS_PROPERTY("RootVerity", "s", NULL, offsetof(ExecContext, root_verity), SD_BUS_VTABLE_PROPERTY_CONST),
427353f6 1049 SD_BUS_PROPERTY("MountImages", "a(ssba(ss))", property_get_mount_images, 0, SD_BUS_VTABLE_PROPERTY_CONST),
556089dc 1050 SD_BUS_PROPERTY("OOMScoreAdjust", "i", property_get_oom_score_adjust, 0, SD_BUS_VTABLE_PROPERTY_CONST),
ad21e542 1051 SD_BUS_PROPERTY("CoredumpFilter", "t", property_get_coredump_filter, 0, SD_BUS_VTABLE_PROPERTY_CONST),
556089dc 1052 SD_BUS_PROPERTY("Nice", "i", property_get_nice, 0, SD_BUS_VTABLE_PROPERTY_CONST),
7f452159
LP
1053 SD_BUS_PROPERTY("IOSchedulingClass", "i", property_get_ioprio_class, 0, SD_BUS_VTABLE_PROPERTY_CONST),
1054 SD_BUS_PROPERTY("IOSchedulingPriority", "i", property_get_ioprio_priority, 0, SD_BUS_VTABLE_PROPERTY_CONST),
556089dc
LP
1055 SD_BUS_PROPERTY("CPUSchedulingPolicy", "i", property_get_cpu_sched_policy, 0, SD_BUS_VTABLE_PROPERTY_CONST),
1056 SD_BUS_PROPERTY("CPUSchedulingPriority", "i", property_get_cpu_sched_priority, 0, SD_BUS_VTABLE_PROPERTY_CONST),
1057 SD_BUS_PROPERTY("CPUAffinity", "ay", property_get_cpu_affinity, 0, SD_BUS_VTABLE_PROPERTY_CONST),
e2b2fb7f 1058 SD_BUS_PROPERTY("CPUAffinityFromNUMA", "b", property_get_cpu_affinity_from_numa, 0, SD_BUS_VTABLE_PROPERTY_CONST),
b070c7c0
MS
1059 SD_BUS_PROPERTY("NUMAPolicy", "i", property_get_numa_policy, 0, SD_BUS_VTABLE_PROPERTY_CONST),
1060 SD_BUS_PROPERTY("NUMAMask", "ay", property_get_numa_mask, 0, SD_BUS_VTABLE_PROPERTY_CONST),
556089dc
LP
1061 SD_BUS_PROPERTY("TimerSlackNSec", "t", property_get_timer_slack_nsec, 0, SD_BUS_VTABLE_PROPERTY_CONST),
1062 SD_BUS_PROPERTY("CPUSchedulingResetOnFork", "b", bus_property_get_bool, offsetof(ExecContext, cpu_sched_reset_on_fork), SD_BUS_VTABLE_PROPERTY_CONST),
1063 SD_BUS_PROPERTY("NonBlocking", "b", bus_property_get_bool, offsetof(ExecContext, non_blocking), SD_BUS_VTABLE_PROPERTY_CONST),
1064 SD_BUS_PROPERTY("StandardInput", "s", property_get_exec_input, offsetof(ExecContext, std_input), SD_BUS_VTABLE_PROPERTY_CONST),
5073ff6b 1065 SD_BUS_PROPERTY("StandardInputFileDescriptorName", "s", property_get_stdio_fdname, 0, SD_BUS_VTABLE_PROPERTY_CONST),
08f3be7a 1066 SD_BUS_PROPERTY("StandardInputData", "ay", property_get_input_data, 0, SD_BUS_VTABLE_PROPERTY_CONST),
556089dc 1067 SD_BUS_PROPERTY("StandardOutput", "s", bus_property_get_exec_output, offsetof(ExecContext, std_output), SD_BUS_VTABLE_PROPERTY_CONST),
5073ff6b 1068 SD_BUS_PROPERTY("StandardOutputFileDescriptorName", "s", property_get_stdio_fdname, 0, SD_BUS_VTABLE_PROPERTY_CONST),
556089dc 1069 SD_BUS_PROPERTY("StandardError", "s", bus_property_get_exec_output, offsetof(ExecContext, std_error), SD_BUS_VTABLE_PROPERTY_CONST),
5073ff6b 1070 SD_BUS_PROPERTY("StandardErrorFileDescriptorName", "s", property_get_stdio_fdname, 0, SD_BUS_VTABLE_PROPERTY_CONST),
556089dc
LP
1071 SD_BUS_PROPERTY("TTYPath", "s", NULL, offsetof(ExecContext, tty_path), SD_BUS_VTABLE_PROPERTY_CONST),
1072 SD_BUS_PROPERTY("TTYReset", "b", bus_property_get_bool, offsetof(ExecContext, tty_reset), SD_BUS_VTABLE_PROPERTY_CONST),
1073 SD_BUS_PROPERTY("TTYVHangup", "b", bus_property_get_bool, offsetof(ExecContext, tty_vhangup), SD_BUS_VTABLE_PROPERTY_CONST),
1074 SD_BUS_PROPERTY("TTYVTDisallocate", "b", bus_property_get_bool, offsetof(ExecContext, tty_vt_disallocate), SD_BUS_VTABLE_PROPERTY_CONST),
1075 SD_BUS_PROPERTY("SyslogPriority", "i", bus_property_get_int, offsetof(ExecContext, syslog_priority), SD_BUS_VTABLE_PROPERTY_CONST),
1076 SD_BUS_PROPERTY("SyslogIdentifier", "s", NULL, offsetof(ExecContext, syslog_identifier), SD_BUS_VTABLE_PROPERTY_CONST),
1077 SD_BUS_PROPERTY("SyslogLevelPrefix", "b", bus_property_get_bool, offsetof(ExecContext, syslog_level_prefix), SD_BUS_VTABLE_PROPERTY_CONST),
9d5527f2
YW
1078 SD_BUS_PROPERTY("SyslogLevel", "i", property_get_syslog_level, offsetof(ExecContext, syslog_priority), SD_BUS_VTABLE_PROPERTY_CONST),
1079 SD_BUS_PROPERTY("SyslogFacility", "i", property_get_syslog_facility, offsetof(ExecContext, syslog_priority), SD_BUS_VTABLE_PROPERTY_CONST),
d3070fbd 1080 SD_BUS_PROPERTY("LogLevelMax", "i", bus_property_get_int, offsetof(ExecContext, log_level_max), SD_BUS_VTABLE_PROPERTY_CONST),
5ac1530e
ZJS
1081 SD_BUS_PROPERTY("LogRateLimitIntervalUSec", "t", bus_property_get_usec, offsetof(ExecContext, log_ratelimit_interval_usec), SD_BUS_VTABLE_PROPERTY_CONST),
1082 SD_BUS_PROPERTY("LogRateLimitBurst", "u", bus_property_get_unsigned, offsetof(ExecContext, log_ratelimit_burst), SD_BUS_VTABLE_PROPERTY_CONST),
d3070fbd 1083 SD_BUS_PROPERTY("LogExtraFields", "aay", property_get_log_extra_fields, 0, SD_BUS_VTABLE_PROPERTY_CONST),
91dd5f7c 1084 SD_BUS_PROPERTY("LogNamespace", "s", NULL, offsetof(ExecContext, log_namespace), SD_BUS_VTABLE_PROPERTY_CONST),
556089dc 1085 SD_BUS_PROPERTY("SecureBits", "i", bus_property_get_int, offsetof(ExecContext, secure_bits), SD_BUS_VTABLE_PROPERTY_CONST),
c0159e20
YW
1086 SD_BUS_PROPERTY("CapabilityBoundingSet", "t", NULL, offsetof(ExecContext, capability_bounding_set), SD_BUS_VTABLE_PROPERTY_CONST),
1087 SD_BUS_PROPERTY("AmbientCapabilities", "t", NULL, offsetof(ExecContext, capability_ambient_set), SD_BUS_VTABLE_PROPERTY_CONST),
556089dc
LP
1088 SD_BUS_PROPERTY("User", "s", NULL, offsetof(ExecContext, user), SD_BUS_VTABLE_PROPERTY_CONST),
1089 SD_BUS_PROPERTY("Group", "s", NULL, offsetof(ExecContext, group), SD_BUS_VTABLE_PROPERTY_CONST),
29206d46 1090 SD_BUS_PROPERTY("DynamicUser", "b", bus_property_get_bool, offsetof(ExecContext, dynamic_user), SD_BUS_VTABLE_PROPERTY_CONST),
00d9ef85 1091 SD_BUS_PROPERTY("RemoveIPC", "b", bus_property_get_bool, offsetof(ExecContext, remove_ipc), SD_BUS_VTABLE_PROPERTY_CONST),
bb0c0d6f
LP
1092 SD_BUS_PROPERTY("SetCredential", "a(say)", property_get_set_credential, 0, SD_BUS_VTABLE_PROPERTY_CONST),
1093 SD_BUS_PROPERTY("LoadCredential", "a(ss)", property_get_load_credential, 0, SD_BUS_VTABLE_PROPERTY_CONST),
556089dc 1094 SD_BUS_PROPERTY("SupplementaryGroups", "as", NULL, offsetof(ExecContext, supplementary_groups), SD_BUS_VTABLE_PROPERTY_CONST),
556089dc 1095 SD_BUS_PROPERTY("PAMName", "s", NULL, offsetof(ExecContext, pam_name), SD_BUS_VTABLE_PROPERTY_CONST),
2a624c36
AP
1096 SD_BUS_PROPERTY("ReadWritePaths", "as", NULL, offsetof(ExecContext, read_write_paths), SD_BUS_VTABLE_PROPERTY_CONST),
1097 SD_BUS_PROPERTY("ReadOnlyPaths", "as", NULL, offsetof(ExecContext, read_only_paths), SD_BUS_VTABLE_PROPERTY_CONST),
1098 SD_BUS_PROPERTY("InaccessiblePaths", "as", NULL, offsetof(ExecContext, inaccessible_paths), SD_BUS_VTABLE_PROPERTY_CONST),
556089dc
LP
1099 SD_BUS_PROPERTY("MountFlags", "t", bus_property_get_ulong, offsetof(ExecContext, mount_flags), SD_BUS_VTABLE_PROPERTY_CONST),
1100 SD_BUS_PROPERTY("PrivateTmp", "b", bus_property_get_bool, offsetof(ExecContext, private_tmp), SD_BUS_VTABLE_PROPERTY_CONST),
7f112f50 1101 SD_BUS_PROPERTY("PrivateDevices", "b", bus_property_get_bool, offsetof(ExecContext, private_devices), SD_BUS_VTABLE_PROPERTY_CONST),
b8b7b838 1102 SD_BUS_PROPERTY("ProtectClock", "b", bus_property_get_bool, offsetof(ExecContext, protect_clock), SD_BUS_VTABLE_PROPERTY_CONST),
59eeb84b 1103 SD_BUS_PROPERTY("ProtectKernelTunables", "b", bus_property_get_bool, offsetof(ExecContext, protect_kernel_tunables), SD_BUS_VTABLE_PROPERTY_CONST),
502d704e 1104 SD_BUS_PROPERTY("ProtectKernelModules", "b", bus_property_get_bool, offsetof(ExecContext, protect_kernel_modules), SD_BUS_VTABLE_PROPERTY_CONST),
84703040 1105 SD_BUS_PROPERTY("ProtectKernelLogs", "b", bus_property_get_bool, offsetof(ExecContext, protect_kernel_logs), SD_BUS_VTABLE_PROPERTY_CONST),
59eeb84b 1106 SD_BUS_PROPERTY("ProtectControlGroups", "b", bus_property_get_bool, offsetof(ExecContext, protect_control_groups), SD_BUS_VTABLE_PROPERTY_CONST),
d251207d
LP
1107 SD_BUS_PROPERTY("PrivateNetwork", "b", bus_property_get_bool, offsetof(ExecContext, private_network), SD_BUS_VTABLE_PROPERTY_CONST),
1108 SD_BUS_PROPERTY("PrivateUsers", "b", bus_property_get_bool, offsetof(ExecContext, private_users), SD_BUS_VTABLE_PROPERTY_CONST),
228af36f 1109 SD_BUS_PROPERTY("PrivateMounts", "b", bus_property_get_bool, offsetof(ExecContext, private_mounts), SD_BUS_VTABLE_PROPERTY_CONST),
73b84e92
YW
1110 SD_BUS_PROPERTY("ProtectHome", "s", property_get_protect_home, offsetof(ExecContext, protect_home), SD_BUS_VTABLE_PROPERTY_CONST),
1111 SD_BUS_PROPERTY("ProtectSystem", "s", property_get_protect_system, offsetof(ExecContext, protect_system), SD_BUS_VTABLE_PROPERTY_CONST),
556089dc
LP
1112 SD_BUS_PROPERTY("SameProcessGroup", "b", bus_property_get_bool, offsetof(ExecContext, same_pgrp), SD_BUS_VTABLE_PROPERTY_CONST),
1113 SD_BUS_PROPERTY("UtmpIdentifier", "s", NULL, offsetof(ExecContext, utmp_id), SD_BUS_VTABLE_PROPERTY_CONST),
023a4f67 1114 SD_BUS_PROPERTY("UtmpMode", "s", property_get_exec_utmp_mode, offsetof(ExecContext, utmp_mode), SD_BUS_VTABLE_PROPERTY_CONST),
5f8640fb 1115 SD_BUS_PROPERTY("SELinuxContext", "(bs)", property_get_selinux_context, 0, SD_BUS_VTABLE_PROPERTY_CONST),
eef65bf3 1116 SD_BUS_PROPERTY("AppArmorProfile", "(bs)", property_get_apparmor_profile, 0, SD_BUS_VTABLE_PROPERTY_CONST),
2ca620c4 1117 SD_BUS_PROPERTY("SmackProcessLabel", "(bs)", property_get_smack_process_label, 0, SD_BUS_VTABLE_PROPERTY_CONST),
556089dc
LP
1118 SD_BUS_PROPERTY("IgnoreSIGPIPE", "b", bus_property_get_bool, offsetof(ExecContext, ignore_sigpipe), SD_BUS_VTABLE_PROPERTY_CONST),
1119 SD_BUS_PROPERTY("NoNewPrivileges", "b", bus_property_get_bool, offsetof(ExecContext, no_new_privileges), SD_BUS_VTABLE_PROPERTY_CONST),
57183d11
LP
1120 SD_BUS_PROPERTY("SystemCallFilter", "(bas)", property_get_syscall_filter, 0, SD_BUS_VTABLE_PROPERTY_CONST),
1121 SD_BUS_PROPERTY("SystemCallArchitectures", "as", property_get_syscall_archs, 0, SD_BUS_VTABLE_PROPERTY_CONST),
cf9d43a8 1122 SD_BUS_PROPERTY("SystemCallErrorNumber", "i", bus_property_get_int, offsetof(ExecContext, syscall_errno), SD_BUS_VTABLE_PROPERTY_CONST),
9df2cdd8 1123 SD_BUS_PROPERTY("SystemCallLog", "(bas)", property_get_syscall_log, 0, SD_BUS_VTABLE_PROPERTY_CONST),
491eecb3 1124 SD_BUS_PROPERTY("Personality", "s", property_get_personality, offsetof(ExecContext, personality), SD_BUS_VTABLE_PROPERTY_CONST),
78e864e5 1125 SD_BUS_PROPERTY("LockPersonality", "b", bus_property_get_bool, offsetof(ExecContext, lock_personality), SD_BUS_VTABLE_PROPERTY_CONST),
4298d0b5 1126 SD_BUS_PROPERTY("RestrictAddressFamilies", "(bas)", property_get_address_families, 0, SD_BUS_VTABLE_PROPERTY_CONST),
53f47dfc 1127 SD_BUS_PROPERTY("RuntimeDirectoryPreserve", "s", property_get_exec_preserve_mode, offsetof(ExecContext, runtime_directory_preserve_mode), SD_BUS_VTABLE_PROPERTY_CONST),
3536f49e
YW
1128 SD_BUS_PROPERTY("RuntimeDirectoryMode", "u", bus_property_get_mode, offsetof(ExecContext, directories[EXEC_DIRECTORY_RUNTIME].mode), SD_BUS_VTABLE_PROPERTY_CONST),
1129 SD_BUS_PROPERTY("RuntimeDirectory", "as", NULL, offsetof(ExecContext, directories[EXEC_DIRECTORY_RUNTIME].paths), SD_BUS_VTABLE_PROPERTY_CONST),
e940c1ef
YW
1130 SD_BUS_PROPERTY("StateDirectoryMode", "u", bus_property_get_mode, offsetof(ExecContext, directories[EXEC_DIRECTORY_STATE].mode), SD_BUS_VTABLE_PROPERTY_CONST),
1131 SD_BUS_PROPERTY("StateDirectory", "as", NULL, offsetof(ExecContext, directories[EXEC_DIRECTORY_STATE].paths), SD_BUS_VTABLE_PROPERTY_CONST),
3536f49e
YW
1132 SD_BUS_PROPERTY("CacheDirectoryMode", "u", bus_property_get_mode, offsetof(ExecContext, directories[EXEC_DIRECTORY_CACHE].mode), SD_BUS_VTABLE_PROPERTY_CONST),
1133 SD_BUS_PROPERTY("CacheDirectory", "as", NULL, offsetof(ExecContext, directories[EXEC_DIRECTORY_CACHE].paths), SD_BUS_VTABLE_PROPERTY_CONST),
1134 SD_BUS_PROPERTY("LogsDirectoryMode", "u", bus_property_get_mode, offsetof(ExecContext, directories[EXEC_DIRECTORY_LOGS].mode), SD_BUS_VTABLE_PROPERTY_CONST),
1135 SD_BUS_PROPERTY("LogsDirectory", "as", NULL, offsetof(ExecContext, directories[EXEC_DIRECTORY_LOGS].paths), SD_BUS_VTABLE_PROPERTY_CONST),
1136 SD_BUS_PROPERTY("ConfigurationDirectoryMode", "u", bus_property_get_mode, offsetof(ExecContext, directories[EXEC_DIRECTORY_CONFIGURATION].mode), SD_BUS_VTABLE_PROPERTY_CONST),
1137 SD_BUS_PROPERTY("ConfigurationDirectory", "as", NULL, offsetof(ExecContext, directories[EXEC_DIRECTORY_CONFIGURATION].paths), SD_BUS_VTABLE_PROPERTY_CONST),
12213aed 1138 SD_BUS_PROPERTY("TimeoutCleanUSec", "t", bus_property_get_usec, offsetof(ExecContext, timeout_clean_usec), SD_BUS_VTABLE_PROPERTY_CONST),
f3e43635 1139 SD_BUS_PROPERTY("MemoryDenyWriteExecute", "b", bus_property_get_bool, offsetof(ExecContext, memory_deny_write_execute), SD_BUS_VTABLE_PROPERTY_CONST),
f4170c67 1140 SD_BUS_PROPERTY("RestrictRealtime", "b", bus_property_get_bool, offsetof(ExecContext, restrict_realtime), SD_BUS_VTABLE_PROPERTY_CONST),
f69567cb 1141 SD_BUS_PROPERTY("RestrictSUIDSGID", "b", bus_property_get_bool, offsetof(ExecContext, restrict_suid_sgid), SD_BUS_VTABLE_PROPERTY_CONST),
6a8c2d59 1142 SD_BUS_PROPERTY("RestrictNamespaces", "t", bus_property_get_ulong, offsetof(ExecContext, restrict_namespaces), SD_BUS_VTABLE_PROPERTY_CONST),
d2d6c096
LP
1143 SD_BUS_PROPERTY("BindPaths", "a(ssbt)", property_get_bind_paths, 0, SD_BUS_VTABLE_PROPERTY_CONST),
1144 SD_BUS_PROPERTY("BindReadOnlyPaths", "a(ssbt)", property_get_bind_paths, 0, SD_BUS_VTABLE_PROPERTY_CONST),
784ad252 1145 SD_BUS_PROPERTY("TemporaryFileSystem", "a(ss)", property_get_temporary_filesystems, 0, SD_BUS_VTABLE_PROPERTY_CONST),
5d997827 1146 SD_BUS_PROPERTY("MountAPIVFS", "b", bus_property_get_bool, offsetof(ExecContext, mount_apivfs), SD_BUS_VTABLE_PROPERTY_CONST),
b1edf445 1147 SD_BUS_PROPERTY("KeyringMode", "s", property_get_exec_keyring_mode, offsetof(ExecContext, keyring_mode), SD_BUS_VTABLE_PROPERTY_CONST),
4e399953
LP
1148 SD_BUS_PROPERTY("ProtectProc", "s", property_get_protect_proc, offsetof(ExecContext, protect_proc), SD_BUS_VTABLE_PROPERTY_CONST),
1149 SD_BUS_PROPERTY("ProcSubset", "s", property_get_proc_subset, offsetof(ExecContext, proc_subset), SD_BUS_VTABLE_PROPERTY_CONST),
aecd5ac6 1150 SD_BUS_PROPERTY("ProtectHostname", "b", bus_property_get_bool, offsetof(ExecContext, protect_hostname), SD_BUS_VTABLE_PROPERTY_CONST),
a8d08f39 1151 SD_BUS_PROPERTY("NetworkNamespacePath", "s", NULL, offsetof(ExecContext, network_namespace_path), SD_BUS_VTABLE_PROPERTY_CONST),
7f452159
LP
1152
1153 /* Obsolete/redundant properties: */
1154 SD_BUS_PROPERTY("Capabilities", "s", property_get_empty_string, 0, SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_HIDDEN),
1155 SD_BUS_PROPERTY("ReadWriteDirectories", "as", NULL, offsetof(ExecContext, read_write_paths), SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_HIDDEN),
1156 SD_BUS_PROPERTY("ReadOnlyDirectories", "as", NULL, offsetof(ExecContext, read_only_paths), SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_HIDDEN),
1157 SD_BUS_PROPERTY("InaccessibleDirectories", "as", NULL, offsetof(ExecContext, inaccessible_paths), SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_HIDDEN),
1158 SD_BUS_PROPERTY("IOScheduling", "i", property_get_ioprio, 0, SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_HIDDEN),
1159
718db961
LP
1160 SD_BUS_VTABLE_END
1161};
82c121a4 1162
4d4c80d0
LP
1163static int append_exec_command(sd_bus_message *reply, ExecCommand *c) {
1164 int r;
1165
1166 assert(reply);
1167 assert(c);
1168
1169 if (!c->path)
1170 return 0;
1171
1172 r = sd_bus_message_open_container(reply, 'r', "sasbttttuii");
1173 if (r < 0)
1174 return r;
1175
1176 r = sd_bus_message_append(reply, "s", c->path);
1177 if (r < 0)
1178 return r;
1179
1180 r = sd_bus_message_append_strv(reply, c->argv);
1181 if (r < 0)
1182 return r;
1183
1184 r = sd_bus_message_append(reply, "bttttuii",
3ed0cd26 1185 !!(c->flags & EXEC_COMMAND_IGNORE_FAILURE),
4d4c80d0
LP
1186 c->exec_status.start_timestamp.realtime,
1187 c->exec_status.start_timestamp.monotonic,
1188 c->exec_status.exit_timestamp.realtime,
1189 c->exec_status.exit_timestamp.monotonic,
1190 (uint32_t) c->exec_status.pid,
1191 (int32_t) c->exec_status.code,
1192 (int32_t) c->exec_status.status);
1193 if (r < 0)
1194 return r;
1195
1196 return sd_bus_message_close_container(reply);
1197}
1198
b3d59367
AZ
1199static int append_exec_ex_command(sd_bus_message *reply, ExecCommand *c) {
1200 _cleanup_strv_free_ char **ex_opts = NULL;
1201 int r;
1202
1203 assert(reply);
1204 assert(c);
1205
1206 if (!c->path)
1207 return 0;
1208
1209 r = sd_bus_message_open_container(reply, 'r', "sasasttttuii");
1210 if (r < 0)
1211 return r;
1212
1213 r = sd_bus_message_append(reply, "s", c->path);
1214 if (r < 0)
1215 return r;
1216
1217 r = sd_bus_message_append_strv(reply, c->argv);
1218 if (r < 0)
1219 return r;
1220
1221 r = exec_command_flags_to_strv(c->flags, &ex_opts);
1222 if (r < 0)
1223 return r;
1224
1225 r = sd_bus_message_append_strv(reply, ex_opts);
1226 if (r < 0)
1227 return r;
1228
1229 r = sd_bus_message_append(reply, "ttttuii",
1230 c->exec_status.start_timestamp.realtime,
1231 c->exec_status.start_timestamp.monotonic,
1232 c->exec_status.exit_timestamp.realtime,
1233 c->exec_status.exit_timestamp.monotonic,
1234 (uint32_t) c->exec_status.pid,
1235 (int32_t) c->exec_status.code,
1236 (int32_t) c->exec_status.status);
1237 if (r < 0)
1238 return r;
1239
1240 return sd_bus_message_close_container(reply);
1241}
1242
718db961
LP
1243int bus_property_get_exec_command(
1244 sd_bus *bus,
1245 const char *path,
1246 const char *interface,
1247 const char *property,
1248 sd_bus_message *reply,
ebcf1f97
LP
1249 void *userdata,
1250 sd_bus_error *ret_error) {
fe68089d 1251
4d4c80d0 1252 ExecCommand *c = (ExecCommand*) userdata;
718db961 1253 int r;
fe68089d 1254
718db961
LP
1255 assert(bus);
1256 assert(reply);
fe68089d 1257
718db961
LP
1258 r = sd_bus_message_open_container(reply, 'a', "(sasbttttuii)");
1259 if (r < 0)
1260 return r;
fe68089d 1261
4d4c80d0
LP
1262 r = append_exec_command(reply, c);
1263 if (r < 0)
1264 return r;
fe68089d 1265
4d4c80d0
LP
1266 return sd_bus_message_close_container(reply);
1267}
718db961 1268
4d4c80d0
LP
1269int bus_property_get_exec_command_list(
1270 sd_bus *bus,
1271 const char *path,
1272 const char *interface,
1273 const char *property,
1274 sd_bus_message *reply,
1275 void *userdata,
1276 sd_bus_error *ret_error) {
718db961 1277
4d4c80d0
LP
1278 ExecCommand *c = *(ExecCommand**) userdata;
1279 int r;
718db961 1280
4d4c80d0
LP
1281 assert(bus);
1282 assert(reply);
1283
1284 r = sd_bus_message_open_container(reply, 'a', "(sasbttttuii)");
1285 if (r < 0)
1286 return r;
718db961 1287
4d4c80d0
LP
1288 LIST_FOREACH(command, c, c) {
1289 r = append_exec_command(reply, c);
718db961
LP
1290 if (r < 0)
1291 return r;
fe68089d
LP
1292 }
1293
718db961 1294 return sd_bus_message_close_container(reply);
8351ceae 1295}
c7040b5d 1296
b3d59367
AZ
1297int bus_property_get_exec_ex_command_list(
1298 sd_bus *bus,
1299 const char *path,
1300 const char *interface,
1301 const char *property,
1302 sd_bus_message *reply,
1303 void *userdata,
1304 sd_bus_error *ret_error) {
1305
1306 ExecCommand *c, *exec_command = *(ExecCommand**) userdata;
1307 int r;
1308
1309 assert(bus);
1310 assert(reply);
1311
1312 r = sd_bus_message_open_container(reply, 'a', "(sasasttttuii)");
1313 if (r < 0)
1314 return r;
1315
1316 LIST_FOREACH(command, c, exec_command) {
1317 r = append_exec_ex_command(reply, c);
1318 if (r < 0)
1319 return r;
1320 }
1321
1322 return sd_bus_message_close_container(reply);
1323}
1324
1325static char *exec_command_flags_to_exec_chars(ExecCommandFlags flags) {
a4fc96c8
ZJS
1326 return strjoin(FLAGS_SET(flags, EXEC_COMMAND_IGNORE_FAILURE) ? "-" : "",
1327 FLAGS_SET(flags, EXEC_COMMAND_NO_ENV_EXPAND) ? ":" : "",
1328 FLAGS_SET(flags, EXEC_COMMAND_FULLY_PRIVILEGED) ? "+" : "",
1329 FLAGS_SET(flags, EXEC_COMMAND_NO_SETUID) ? "!" : "",
1330 FLAGS_SET(flags, EXEC_COMMAND_AMBIENT_MAGIC) ? "!!" : "");
b3d59367
AZ
1331}
1332
237f7bcb 1333int bus_set_transient_exec_command(
9c0320e7
YW
1334 Unit *u,
1335 const char *name,
1336 ExecCommand **exec_command,
1337 sd_bus_message *message,
1338 UnitWriteFlags flags,
1339 sd_bus_error *error) {
b3d59367 1340 bool is_ex_prop = endswith(name, "Ex");
9c0320e7
YW
1341 unsigned n = 0;
1342 int r;
1343
b3d59367 1344 r = sd_bus_message_enter_container(message, 'a', is_ex_prop ? "(sasas)" : "(sasb)");
9c0320e7
YW
1345 if (r < 0)
1346 return r;
1347
b3d59367
AZ
1348 while ((r = sd_bus_message_enter_container(message, 'r', is_ex_prop ? "sasas" : "sasb")) > 0) {
1349 _cleanup_strv_free_ char **argv = NULL, **ex_opts = NULL;
9c0320e7
YW
1350 const char *path;
1351 int b;
1352
1353 r = sd_bus_message_read(message, "s", &path);
1354 if (r < 0)
1355 return r;
1356
8038b99d
ZJS
1357 if (!path_is_absolute(path) && !filename_is_valid(path))
1358 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
1359 "\"%s\" is neither a valid executable name nor an absolute path",
1360 path);
9c0320e7
YW
1361
1362 r = sd_bus_message_read_strv(message, &argv);
1363 if (r < 0)
1364 return r;
1365
b3d59367 1366 r = is_ex_prop ? sd_bus_message_read_strv(message, &ex_opts) : sd_bus_message_read(message, "b", &b);
9c0320e7
YW
1367 if (r < 0)
1368 return r;
1369
1370 r = sd_bus_message_exit_container(message);
1371 if (r < 0)
1372 return r;
1373
1374 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1375 ExecCommand *c;
1376
1377 c = new0(ExecCommand, 1);
1378 if (!c)
1379 return -ENOMEM;
1380
1381 c->path = strdup(path);
1382 if (!c->path) {
1383 free(c);
1384 return -ENOMEM;
1385 }
1386
ae2a15bc 1387 c->argv = TAKE_PTR(argv);
9c0320e7 1388
b3d59367
AZ
1389 if (is_ex_prop) {
1390 r = exec_command_flags_from_strv(ex_opts, &c->flags);
1391 if (r < 0)
1392 return r;
1393 } else
1394 c->flags = b ? EXEC_COMMAND_IGNORE_FAILURE : 0;
9c0320e7 1395
858d36c1 1396 path_simplify(c->path, false);
9c0320e7
YW
1397 exec_command_append_list(exec_command, c);
1398 }
1399
1400 n++;
1401 }
1402 if (r < 0)
1403 return r;
1404
1405 r = sd_bus_message_exit_container(message);
1406 if (r < 0)
1407 return r;
1408
1409 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1410 _cleanup_free_ char *buf = NULL;
1411 _cleanup_fclose_ FILE *f = NULL;
1412 ExecCommand *c;
1413 size_t size = 0;
1414
1415 if (n == 0)
1416 *exec_command = exec_command_free_list(*exec_command);
1417
2fe21124 1418 f = open_memstream_unlocked(&buf, &size);
9c0320e7
YW
1419 if (!f)
1420 return -ENOMEM;
1421
f14bf013 1422 fprintf(f, "%s=\n", name);
9c0320e7
YW
1423
1424 LIST_FOREACH(command, c, *exec_command) {
540ac933 1425 _cleanup_free_ char *a = NULL, *exec_chars = NULL;
9c0320e7 1426
540ac933
LP
1427 exec_chars = exec_command_flags_to_exec_chars(c->flags);
1428 if (!exec_chars)
9c0320e7
YW
1429 return -ENOMEM;
1430
1431 a = unit_concat_strv(c->argv, UNIT_ESCAPE_C|UNIT_ESCAPE_SPECIFIERS);
1432 if (!a)
1433 return -ENOMEM;
1434
540ac933
LP
1435 if (streq_ptr(c->path, c->argv ? c->argv[0] : NULL))
1436 fprintf(f, "%s=%s%s\n", name, exec_chars, a);
1437 else {
1438 _cleanup_free_ char *t = NULL;
1439 const char *p;
1440
1441 p = unit_escape_setting(c->path, UNIT_ESCAPE_C|UNIT_ESCAPE_SPECIFIERS, &t);
1442 if (!p)
1443 return -ENOMEM;
b3d59367 1444
540ac933
LP
1445 fprintf(f, "%s=%s@%s %s\n", name, exec_chars, p, a);
1446 }
9c0320e7
YW
1447 }
1448
1449 r = fflush_and_check(f);
1450 if (r < 0)
1451 return r;
1452
1453 unit_write_setting(u, flags, name, buf);
1454 }
1455
1456 return 1;
1457}
1458
7e2a3fcc
YW
1459static int parse_personality(const char *s, unsigned long *p) {
1460 unsigned long v;
1461
1462 assert(p);
1463
1464 v = personality_from_string(s);
1465 if (v == PERSONALITY_INVALID)
1466 return -EINVAL;
1467
1468 *p = v;
1469 return 0;
1470}
1471
1472static const char* mount_propagation_flags_to_string_with_check(unsigned long n) {
1473 if (!IN_SET(n, 0, MS_SHARED, MS_PRIVATE, MS_SLAVE))
1474 return NULL;
1475
1476 return mount_propagation_flags_to_string(n);
1477}
1478
1479static BUS_DEFINE_SET_TRANSIENT(nsec, "t", uint64_t, nsec_t, NSEC_FMT);
1480static BUS_DEFINE_SET_TRANSIENT_IS_VALID(log_level, "i", int32_t, int, "%" PRIi32, log_level_is_valid);
aad67b80 1481#if HAVE_SECCOMP
005bfaf1 1482static BUS_DEFINE_SET_TRANSIENT_IS_VALID(errno, "i", int32_t, int, "%" PRIi32, seccomp_errno_or_action_is_valid);
aad67b80 1483#endif
7e2a3fcc
YW
1484static BUS_DEFINE_SET_TRANSIENT_PARSE(std_input, ExecInput, exec_input_from_string);
1485static BUS_DEFINE_SET_TRANSIENT_PARSE(std_output, ExecOutput, exec_output_from_string);
1486static BUS_DEFINE_SET_TRANSIENT_PARSE(utmp_mode, ExecUtmpMode, exec_utmp_mode_from_string);
1e8c7bd5
YW
1487static BUS_DEFINE_SET_TRANSIENT_PARSE(protect_system, ProtectSystem, protect_system_from_string);
1488static BUS_DEFINE_SET_TRANSIENT_PARSE(protect_home, ProtectHome, protect_home_from_string);
7e2a3fcc 1489static BUS_DEFINE_SET_TRANSIENT_PARSE(keyring_mode, ExecKeyringMode, exec_keyring_mode_from_string);
4e399953
LP
1490static BUS_DEFINE_SET_TRANSIENT_PARSE(protect_proc, ProtectProc, protect_proc_from_string);
1491static BUS_DEFINE_SET_TRANSIENT_PARSE(proc_subset, ProcSubset, proc_subset_from_string);
7e2a3fcc
YW
1492static BUS_DEFINE_SET_TRANSIENT_PARSE(preserve_mode, ExecPreserveMode, exec_preserve_mode_from_string);
1493static BUS_DEFINE_SET_TRANSIENT_PARSE_PTR(personality, unsigned long, parse_personality);
1494static BUS_DEFINE_SET_TRANSIENT_TO_STRING_ALLOC(secure_bits, "i", int32_t, int, "%" PRIi32, secure_bits_to_string_alloc_with_check);
1495static BUS_DEFINE_SET_TRANSIENT_TO_STRING_ALLOC(capability, "t", uint64_t, uint64_t, "%" PRIu64, capability_set_to_string_alloc);
86c2a9f1 1496static BUS_DEFINE_SET_TRANSIENT_TO_STRING_ALLOC(namespace_flag, "t", uint64_t, unsigned long, "%" PRIu64, namespace_flags_to_string);
7e2a3fcc
YW
1497static BUS_DEFINE_SET_TRANSIENT_TO_STRING(mount_flags, "t", uint64_t, unsigned long, "%" PRIu64, mount_propagation_flags_to_string_with_check);
1498
427353f6
LB
1499/* ret_format_str is an accumulator, so if it has any pre-existing content, new options will be appended to it */
1500static int read_mount_options(sd_bus_message *message, sd_bus_error *error, MountOptions **ret_options, char **ret_format_str, const char *separator) {
1501 _cleanup_(mount_options_free_allp) MountOptions *options = NULL;
1502 _cleanup_free_ char *format_str = NULL;
1503 const char *mount_options, *partition;
1504 int r;
1505
1506 assert(message);
1507 assert(ret_options);
1508 assert(ret_format_str);
1509 assert(separator);
1510
1511 r = sd_bus_message_enter_container(message, 'a', "(ss)");
1512 if (r < 0)
1513 return r;
1514
1515 while ((r = sd_bus_message_read(message, "(ss)", &partition, &mount_options)) > 0) {
1516 _cleanup_free_ char *previous = NULL, *escaped = NULL;
1517 _cleanup_free_ MountOptions *o = NULL;
569a0e42 1518 PartitionDesignator partition_designator;
427353f6
LB
1519
1520 if (chars_intersect(mount_options, WHITESPACE))
1521 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
1522 "Invalid mount options string, contains whitespace character(s): %s", mount_options);
1523
1524 partition_designator = partition_designator_from_string(partition);
1525 if (partition_designator < 0)
1526 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid partition name %s", partition);
1527
1528 /* Need to store them in the unit with the escapes, so that they can be parsed again */
1529 escaped = shell_escape(mount_options, ":");
1530 if (!escaped)
1531 return -ENOMEM;
1532
1533 previous = TAKE_PTR(format_str);
1534 format_str = strjoin(previous, previous ? separator : "", partition, ":", escaped);
1535 if (!format_str)
1536 return -ENOMEM;
1537
1538 o = new(MountOptions, 1);
1539 if (!o)
1540 return -ENOMEM;
1541 *o = (MountOptions) {
1542 .partition_designator = partition_designator,
1543 .options = strdup(mount_options),
1544 };
1545 if (!o->options)
1546 return -ENOMEM;
1547 LIST_APPEND(mount_options, options, TAKE_PTR(o));
1548 }
1549 if (r < 0)
1550 return r;
1551
1552 r = sd_bus_message_exit_container(message);
1553 if (r < 0)
1554 return r;
1555
1556 if (!LIST_IS_EMPTY(options)) {
1557 char *final = strjoin(*ret_format_str, !isempty(*ret_format_str) ? separator : "", format_str);
1558 if (!final)
1559 return -ENOMEM;
1560 free_and_replace(*ret_format_str, final);
1561 LIST_JOIN(mount_options, *ret_options, options);
1562 }
1563
1564 return 0;
1565}
1566
c7040b5d
LP
1567int bus_exec_context_set_transient_property(
1568 Unit *u,
1569 ExecContext *c,
1570 const char *name,
1571 sd_bus_message *message,
2e59b241 1572 UnitWriteFlags flags,
c7040b5d
LP
1573 sd_bus_error *error) {
1574
6550c24c
LP
1575 const char *suffix;
1576 int r;
c7040b5d
LP
1577
1578 assert(u);
1579 assert(c);
1580 assert(name);
1581 assert(message);
1582
2e59b241
LP
1583 flags |= UNIT_PRIVATE;
1584
7e2a3fcc 1585 if (streq(name, "User"))
7a8867ab 1586 return bus_set_transient_user_relaxed(u, name, &c->user, message, flags, error);
c7040b5d 1587
7e2a3fcc 1588 if (streq(name, "Group"))
7a8867ab 1589 return bus_set_transient_user_relaxed(u, name, &c->group, message, flags, error);
c7040b5d 1590
7e2a3fcc
YW
1591 if (streq(name, "TTYPath"))
1592 return bus_set_transient_path(u, name, &c->tty_path, message, flags, error);
6f3e7985 1593
7e2a3fcc
YW
1594 if (streq(name, "RootImage"))
1595 return bus_set_transient_path(u, name, &c->root_image, message, flags, error);
76736280 1596
18d73705
LB
1597 if (streq(name, "RootImageOptions")) {
1598 _cleanup_(mount_options_free_allp) MountOptions *options = NULL;
1599 _cleanup_free_ char *format_str = NULL;
18d73705 1600
427353f6 1601 r = read_mount_options(message, error, &options, &format_str, " ");
18d73705
LB
1602 if (r < 0)
1603 return r;
1604
1605 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1606 if (LIST_IS_EMPTY(options)) {
1607 c->root_image_options = mount_options_free_all(c->root_image_options);
1608 unit_write_settingf(u, flags, name, "%s=", name);
1609 } else {
1610 LIST_JOIN(mount_options, c->root_image_options, options);
1611 unit_write_settingf(
1612 u, flags|UNIT_ESCAPE_SPECIFIERS, name,
1613 "%s=%s",
1614 name,
1615 format_str);
1616 }
1617 }
1618
1619 return 1;
1620 }
1621
0389f4fa
LB
1622 if (streq(name, "RootHash")) {
1623 const void *roothash_decoded;
1624 size_t roothash_decoded_size;
1625
1626 r = sd_bus_message_read_array(message, 'y', &roothash_decoded, &roothash_decoded_size);
1627 if (r < 0)
1628 return r;
1629
1630 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1631 _cleanup_free_ char *encoded = NULL;
1632
1633 if (roothash_decoded_size == 0) {
1634 c->root_hash_path = mfree(c->root_hash_path);
1635 c->root_hash = mfree(c->root_hash);
1636 c->root_hash_size = 0;
1637
1638 unit_write_settingf(u, flags, name, "RootHash=");
1639 } else {
1640 _cleanup_free_ void *p;
1641
1642 encoded = hexmem(roothash_decoded, roothash_decoded_size);
1643 if (!encoded)
1644 return -ENOMEM;
1645
1646 p = memdup(roothash_decoded, roothash_decoded_size);
1647 if (!p)
1648 return -ENOMEM;
1649
1650 free_and_replace(c->root_hash, p);
1651 c->root_hash_size = roothash_decoded_size;
1652 c->root_hash_path = mfree(c->root_hash_path);
1653
1654 unit_write_settingf(u, flags, name, "RootHash=%s", encoded);
1655 }
1656 }
1657
1658 return 1;
1659 }
1660
1661 if (streq(name, "RootHashPath")) {
1662 c->root_hash_size = 0;
1663 c->root_hash = mfree(c->root_hash);
1664
1665 return bus_set_transient_path(u, "RootHash", &c->root_hash_path, message, flags, error);
1666 }
1667
d4d55b0d
LB
1668 if (streq(name, "RootHashSignature")) {
1669 const void *roothash_sig_decoded;
1670 size_t roothash_sig_decoded_size;
1671
1672 r = sd_bus_message_read_array(message, 'y', &roothash_sig_decoded, &roothash_sig_decoded_size);
1673 if (r < 0)
1674 return r;
1675
1676 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1677 _cleanup_free_ char *encoded = NULL;
1678
1679 if (roothash_sig_decoded_size == 0) {
1680 c->root_hash_sig_path = mfree(c->root_hash_sig_path);
1681 c->root_hash_sig = mfree(c->root_hash_sig);
1682 c->root_hash_sig_size = 0;
1683
1684 unit_write_settingf(u, flags, name, "RootHashSignature=");
1685 } else {
1686 _cleanup_free_ void *p;
1687 ssize_t len;
1688
1689 len = base64mem(roothash_sig_decoded, roothash_sig_decoded_size, &encoded);
1690 if (len < 0)
1691 return -ENOMEM;
1692
1693 p = memdup(roothash_sig_decoded, roothash_sig_decoded_size);
1694 if (!p)
1695 return -ENOMEM;
1696
1697 free_and_replace(c->root_hash_sig, p);
1698 c->root_hash_sig_size = roothash_sig_decoded_size;
1699 c->root_hash_sig_path = mfree(c->root_hash_sig_path);
1700
1701 unit_write_settingf(u, flags, name, "RootHashSignature=base64:%s", encoded);
1702 }
1703 }
1704
1705 return 1;
1706 }
1707
1708 if (streq(name, "RootHashSignaturePath")) {
1709 c->root_hash_sig_size = 0;
1710 c->root_hash_sig = mfree(c->root_hash_sig);
1711
1712 return bus_set_transient_path(u, "RootHashSignature", &c->root_hash_sig_path, message, flags, error);
1713 }
1714
0389f4fa
LB
1715 if (streq(name, "RootVerity"))
1716 return bus_set_transient_path(u, name, &c->root_verity, message, flags, error);
1717
7e2a3fcc
YW
1718 if (streq(name, "RootDirectory"))
1719 return bus_set_transient_path(u, name, &c->root_directory, message, flags, error);
c7040b5d 1720
7e2a3fcc
YW
1721 if (streq(name, "SyslogIdentifier"))
1722 return bus_set_transient_string(u, name, &c->syslog_identifier, message, flags, error);
c7040b5d 1723
7e2a3fcc
YW
1724 if (streq(name, "LogLevelMax"))
1725 return bus_set_transient_log_level(u, name, &c->log_level_max, message, flags, error);
1726
90fc172e 1727 if (streq(name, "LogRateLimitIntervalUSec"))
5ac1530e 1728 return bus_set_transient_usec(u, name, &c->log_ratelimit_interval_usec, message, flags, error);
90fc172e
AZ
1729
1730 if (streq(name, "LogRateLimitBurst"))
5ac1530e 1731 return bus_set_transient_unsigned(u, name, &c->log_ratelimit_burst, message, flags, error);
90fc172e 1732
7e2a3fcc
YW
1733 if (streq(name, "Personality"))
1734 return bus_set_transient_personality(u, name, &c->personality, message, flags, error);
1735
7e2a3fcc
YW
1736 if (streq(name, "StandardInput"))
1737 return bus_set_transient_std_input(u, name, &c->std_input, message, flags, error);
1738
1739 if (streq(name, "StandardOutput"))
1740 return bus_set_transient_std_output(u, name, &c->std_output, message, flags, error);
1741
1742 if (streq(name, "StandardError"))
1743 return bus_set_transient_std_output(u, name, &c->std_error, message, flags, error);
1744
1745 if (streq(name, "IgnoreSIGPIPE"))
1746 return bus_set_transient_bool(u, name, &c->ignore_sigpipe, message, flags, error);
1747
1748 if (streq(name, "TTYVHangup"))
1749 return bus_set_transient_bool(u, name, &c->tty_vhangup, message, flags, error);
1750
1751 if (streq(name, "TTYReset"))
1752 return bus_set_transient_bool(u, name, &c->tty_reset, message, flags, error);
1753
1754 if (streq(name, "TTYVTDisallocate"))
1755 return bus_set_transient_bool(u, name, &c->tty_vt_disallocate, message, flags, error);
1756
1757 if (streq(name, "PrivateTmp"))
1758 return bus_set_transient_bool(u, name, &c->private_tmp, message, flags, error);
1759
1760 if (streq(name, "PrivateDevices"))
1761 return bus_set_transient_bool(u, name, &c->private_devices, message, flags, error);
1762
228af36f
LP
1763 if (streq(name, "PrivateMounts"))
1764 return bus_set_transient_bool(u, name, &c->private_mounts, message, flags, error);
1765
7e2a3fcc
YW
1766 if (streq(name, "PrivateNetwork"))
1767 return bus_set_transient_bool(u, name, &c->private_network, message, flags, error);
1768
1769 if (streq(name, "PrivateUsers"))
1770 return bus_set_transient_bool(u, name, &c->private_users, message, flags, error);
1771
1772 if (streq(name, "NoNewPrivileges"))
1773 return bus_set_transient_bool(u, name, &c->no_new_privileges, message, flags, error);
1774
1775 if (streq(name, "SyslogLevelPrefix"))
1776 return bus_set_transient_bool(u, name, &c->syslog_level_prefix, message, flags, error);
1777
1778 if (streq(name, "MemoryDenyWriteExecute"))
1779 return bus_set_transient_bool(u, name, &c->memory_deny_write_execute, message, flags, error);
1780
1781 if (streq(name, "RestrictRealtime"))
1782 return bus_set_transient_bool(u, name, &c->restrict_realtime, message, flags, error);
1783
f69567cb
LP
1784 if (streq(name, "RestrictSUIDSGID"))
1785 return bus_set_transient_bool(u, name, &c->restrict_suid_sgid, message, flags, error);
1786
7e2a3fcc
YW
1787 if (streq(name, "DynamicUser"))
1788 return bus_set_transient_bool(u, name, &c->dynamic_user, message, flags, error);
1789
1790 if (streq(name, "RemoveIPC"))
1791 return bus_set_transient_bool(u, name, &c->remove_ipc, message, flags, error);
1792
1793 if (streq(name, "ProtectKernelTunables"))
1794 return bus_set_transient_bool(u, name, &c->protect_kernel_tunables, message, flags, error);
1795
1796 if (streq(name, "ProtectKernelModules"))
1797 return bus_set_transient_bool(u, name, &c->protect_kernel_modules, message, flags, error);
1798
84703040
KK
1799 if (streq(name, "ProtectKernelLogs"))
1800 return bus_set_transient_bool(u, name, &c->protect_kernel_logs, message, flags, error);
1801
fc64760d
KK
1802 if (streq(name, "ProtectClock"))
1803 return bus_set_transient_bool(u, name, &c->protect_clock, message, flags, error);
1804
7e2a3fcc
YW
1805 if (streq(name, "ProtectControlGroups"))
1806 return bus_set_transient_bool(u, name, &c->protect_control_groups, message, flags, error);
1807
1808 if (streq(name, "MountAPIVFS"))
1809 return bus_set_transient_bool(u, name, &c->mount_apivfs, message, flags, error);
1810
1811 if (streq(name, "CPUSchedulingResetOnFork"))
1812 return bus_set_transient_bool(u, name, &c->cpu_sched_reset_on_fork, message, flags, error);
1813
1814 if (streq(name, "NonBlocking"))
1815 return bus_set_transient_bool(u, name, &c->non_blocking, message, flags, error);
1816
1817 if (streq(name, "LockPersonality"))
1818 return bus_set_transient_bool(u, name, &c->lock_personality, message, flags, error);
1819
aecd5ac6
TM
1820 if (streq(name, "ProtectHostname"))
1821 return bus_set_transient_bool(u, name, &c->protect_hostname, message, flags, error);
1822
7e2a3fcc
YW
1823 if (streq(name, "UtmpIdentifier"))
1824 return bus_set_transient_string(u, name, &c->utmp_id, message, flags, error);
1825
1826 if (streq(name, "UtmpMode"))
1827 return bus_set_transient_utmp_mode(u, name, &c->utmp_mode, message, flags, error);
1828
1829 if (streq(name, "PAMName"))
1830 return bus_set_transient_string(u, name, &c->pam_name, message, flags, error);
1831
1832 if (streq(name, "TimerSlackNSec"))
1833 return bus_set_transient_nsec(u, name, &c->timer_slack_nsec, message, flags, error);
1834
1835 if (streq(name, "ProtectSystem"))
1836 return bus_set_transient_protect_system(u, name, &c->protect_system, message, flags, error);
1837
1838 if (streq(name, "ProtectHome"))
1839 return bus_set_transient_protect_home(u, name, &c->protect_home, message, flags, error);
cffaed83 1840
7e2a3fcc
YW
1841 if (streq(name, "KeyringMode"))
1842 return bus_set_transient_keyring_mode(u, name, &c->keyring_mode, message, flags, error);
1843
4e399953
LP
1844 if (streq(name, "ProtectProc"))
1845 return bus_set_transient_protect_proc(u, name, &c->protect_proc, message, flags, error);
1846
1847 if (streq(name, "ProcSubset"))
1848 return bus_set_transient_proc_subset(u, name, &c->proc_subset, message, flags, error);
1849
7e2a3fcc
YW
1850 if (streq(name, "RuntimeDirectoryPreserve"))
1851 return bus_set_transient_preserve_mode(u, name, &c->runtime_directory_preserve_mode, message, flags, error);
1852
1853 if (streq(name, "UMask"))
1854 return bus_set_transient_mode_t(u, name, &c->umask, message, flags, error);
1855
1856 if (streq(name, "RuntimeDirectoryMode"))
1857 return bus_set_transient_mode_t(u, name, &c->directories[EXEC_DIRECTORY_RUNTIME].mode, message, flags, error);
1858
1859 if (streq(name, "StateDirectoryMode"))
1860 return bus_set_transient_mode_t(u, name, &c->directories[EXEC_DIRECTORY_STATE].mode, message, flags, error);
1861
1862 if (streq(name, "CacheDirectoryMode"))
1863 return bus_set_transient_mode_t(u, name, &c->directories[EXEC_DIRECTORY_CACHE].mode, message, flags, error);
1864
1865 if (streq(name, "LogsDirectoryMode"))
1866 return bus_set_transient_mode_t(u, name, &c->directories[EXEC_DIRECTORY_LOGS].mode, message, flags, error);
1867
1868 if (streq(name, "ConfigurationDirectoryMode"))
1869 return bus_set_transient_mode_t(u, name, &c->directories[EXEC_DIRECTORY_CONFIGURATION].mode, message, flags, error);
1870
1871 if (streq(name, "SELinuxContext"))
1872 return bus_set_transient_string(u, name, &c->selinux_context, message, flags, error);
1873
1874 if (streq(name, "SecureBits"))
1875 return bus_set_transient_secure_bits(u, name, &c->secure_bits, message, flags, error);
1876
1877 if (streq(name, "CapabilityBoundingSet"))
1878 return bus_set_transient_capability(u, name, &c->capability_bounding_set, message, flags, error);
1879
1880 if (streq(name, "AmbientCapabilities"))
1881 return bus_set_transient_capability(u, name, &c->capability_ambient_set, message, flags, error);
1882
7e2a3fcc
YW
1883 if (streq(name, "RestrictNamespaces"))
1884 return bus_set_transient_namespace_flag(u, name, &c->restrict_namespaces, message, flags, error);
1885
1886 if (streq(name, "MountFlags"))
1887 return bus_set_transient_mount_flags(u, name, &c->mount_flags, message, flags, error);
1888
a8d08f39
LP
1889 if (streq(name, "NetworkNamespacePath"))
1890 return bus_set_transient_path(u, name, &c->network_namespace_path, message, flags, error);
1891
7e2a3fcc 1892 if (streq(name, "SupplementaryGroups")) {
cffaed83
YW
1893 _cleanup_strv_free_ char **l = NULL;
1894 char **p;
1895
1896 r = sd_bus_message_read_strv(message, &l);
1897 if (r < 0)
1898 return r;
1899
d2a23692 1900 STRV_FOREACH(p, l)
7a8867ab 1901 if (!isempty(*p) && !valid_user_group_name(*p, VALID_USER_ALLOW_NUMERIC|VALID_USER_RELAX|VALID_USER_WARN))
d2a23692
ZJS
1902 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
1903 "Invalid supplementary group names");
cffaed83 1904
2e59b241 1905 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
7b943bb7 1906 if (strv_isempty(l)) {
cffaed83 1907 c->supplementary_groups = strv_free(c->supplementary_groups);
2e59b241 1908 unit_write_settingf(u, flags, name, "%s=", name);
cffaed83
YW
1909 } else {
1910 _cleanup_free_ char *joined = NULL;
1911
1912 r = strv_extend_strv(&c->supplementary_groups, l, true);
1913 if (r < 0)
1914 return -ENOMEM;
1915
1916 joined = strv_join(c->supplementary_groups, " ");
1917 if (!joined)
1918 return -ENOMEM;
1919
2e59b241 1920 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "%s=%s", name, joined);
cffaed83
YW
1921 }
1922 }
1923
1924 return 1;
1925
bb0c0d6f
LP
1926 } else if (streq(name, "SetCredential")) {
1927 bool isempty = true;
1928
1929 r = sd_bus_message_enter_container(message, 'a', "(say)");
1930 if (r < 0)
1931 return r;
1932
1933 for (;;) {
1934 const char *id;
1935 const void *p;
1936 size_t sz;
1937
1938 r = sd_bus_message_enter_container(message, 'r', "say");
1939 if (r < 0)
1940 return r;
1941 if (r == 0)
1942 break;
1943
1944 r = sd_bus_message_read(message, "s", &id);
1945 if (r < 0)
1946 return r;
1947
1948 r = sd_bus_message_read_array(message, 'y', &p, &sz);
1949 if (r < 0)
1950 return r;
1951
1952 r = sd_bus_message_exit_container(message);
1953 if (r < 0)
1954 return r;
1955
1956 if (!credential_name_valid(id))
1957 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Credential ID is invalid: %s", id);
1958
1959 isempty = false;
1960
1961 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1962 _cleanup_free_ char *a = NULL, *b = NULL;
1963 _cleanup_free_ void *copy = NULL;
1964 ExecSetCredential *old;
1965
1966 copy = memdup(p, sz);
1967 if (!copy)
1968 return -ENOMEM;
1969
1970 old = hashmap_get(c->set_credentials, id);
1971 if (old) {
1972 free_and_replace(old->data, copy);
1973 old->size = sz;
1974 } else {
1975 _cleanup_(exec_set_credential_freep) ExecSetCredential *sc = NULL;
1976
1977 sc = new0(ExecSetCredential, 1);
1978 if (!sc)
1979 return -ENOMEM;
1980
1981 sc->id = strdup(id);
1982 if (!sc->id)
1983 return -ENOMEM;
1984
1985 sc->data = TAKE_PTR(copy);
1986 sc->size = sz;
1987
1988 r = hashmap_ensure_allocated(&c->set_credentials, &exec_set_credential_hash_ops);
1989 if (r < 0)
1990 return r;
1991
1992 r = hashmap_put(c->set_credentials, sc->id, sc);
1993 if (r < 0)
1994 return r;
1995
1996 TAKE_PTR(sc);
1997 }
1998
1999 a = specifier_escape(id);
2000 if (!a)
2001 return -ENOMEM;
2002
2003 b = cescape_length(p, sz);
2004 if (!b)
2005 return -ENOMEM;
2006
2007 (void) unit_write_settingf(u, flags, name, "%s=%s:%s", name, a, b);
2008 }
2009 }
2010
2011 r = sd_bus_message_exit_container(message);
2012 if (r < 0)
2013 return r;
2014
2015 if (!UNIT_WRITE_FLAGS_NOOP(flags) && isempty) {
2016 c->set_credentials = hashmap_free(c->set_credentials);
2017 (void) unit_write_settingf(u, flags, name, "%s=", name);
2018 }
2019
2020 return 1;
2021
2022 } else if (streq(name, "LoadCredential")) {
2023 bool isempty = true;
2024
2025 r = sd_bus_message_enter_container(message, 'a', "(ss)");
2026 if (r < 0)
2027 return r;
2028
2029 for (;;) {
2030 const char *id, *source;
2031
2032 r = sd_bus_message_read(message, "(ss)", &id, &source);
2033 if (r < 0)
2034 return r;
2035 if (r == 0)
2036 break;
2037
2038 if (!credential_name_valid(id))
2039 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Credential ID is invalid: %s", id);
2040
2041 if (!(path_is_absolute(source) ? path_is_normalized(source) : credential_name_valid(source)))
2042 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Credential source is invalid: %s", source);
2043
2044 isempty = false;
2045
2046 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2047 r = strv_extend_strv(&c->load_credentials, STRV_MAKE(id, source), /* filter_duplicates = */ false);
2048 if (r < 0)
2049 return r;
2050
2051 (void) unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "%s=%s:%s", name, id, source);
2052 }
2053 }
2054
2055 r = sd_bus_message_exit_container(message);
2056 if (r < 0)
2057 return r;
2058
2059 if (!UNIT_WRITE_FLAGS_NOOP(flags) && isempty) {
2060 c->load_credentials = strv_free(c->load_credentials);
2061 (void) unit_write_settingf(u, flags, name, "%s=", name);
2062 }
2063
2064 return 1;
2065
a8a13575 2066 } else if (streq(name, "SyslogLevel")) {
8d1dd6ab 2067 int32_t level;
a8a13575
EV
2068
2069 r = sd_bus_message_read(message, "i", &level);
2070 if (r < 0)
2071 return r;
2072
e0d6e0fa
EV
2073 if (!log_level_is_valid(level))
2074 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Log level value out of range");
2075
2e59b241 2076 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
a8a13575 2077 c->syslog_priority = (c->syslog_priority & LOG_FACMASK) | level;
2e59b241 2078 unit_write_settingf(u, flags, name, "SyslogLevel=%i", level);
a8a13575
EV
2079 }
2080
460ed929 2081 return 1;
7e2a3fcc 2082
460ed929 2083 } else if (streq(name, "SyslogFacility")) {
8d1dd6ab 2084 int32_t facility;
460ed929
EV
2085
2086 r = sd_bus_message_read(message, "i", &facility);
2087 if (r < 0)
2088 return r;
2089
e0d6e0fa
EV
2090 if (!log_facility_unshifted_is_valid(facility))
2091 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Log facility value out of range");
2092
2e59b241 2093 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
460ed929 2094 c->syslog_priority = (facility << 3) | LOG_PRI(c->syslog_priority);
2e59b241 2095 unit_write_settingf(u, flags, name, "SyslogFacility=%i", facility);
460ed929
EV
2096 }
2097
cffaed83 2098 return 1;
d3070fbd 2099
91dd5f7c
LP
2100 } else if (streq(name, "LogNamespace")) {
2101 const char *n;
2102
2103 r = sd_bus_message_read(message, "s", &n);
2104 if (r < 0)
2105 return r;
2106
2107 if (!isempty(n) && !log_namespace_name_valid(n))
2108 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Log namespace name not valid");
2109
2110 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2111
2112 if (isempty(n)) {
2113 c->log_namespace = mfree(c->log_namespace);
2114 unit_write_settingf(u, flags, name, "%s=", name);
2115 } else {
2116 r = free_and_strdup(&c->log_namespace, n);
2117 if (r < 0)
2118 return r;
2119
2120 unit_write_settingf(u, flags, name, "%s=%s", name, n);
2121 }
2122 }
2123
2124 return 1;
2125
d3070fbd
LP
2126 } else if (streq(name, "LogExtraFields")) {
2127 size_t n = 0;
2128
2129 r = sd_bus_message_enter_container(message, 'a', "ay");
2130 if (r < 0)
2131 return r;
2132
2133 for (;;) {
2134 _cleanup_free_ void *copy = NULL;
2135 struct iovec *t;
2136 const char *eq;
2137 const void *p;
2138 size_t sz;
2139
2140 /* Note that we expect a byte array for each field, instead of a string. That's because on the
2141 * lower-level journal fields can actually contain binary data and are not restricted to text,
2142 * and we should not "lose precision" in our types on the way. That said, I am pretty sure
2143 * actually encoding binary data as unit metadata is not a good idea. Hence we actually refuse
2144 * any actual binary data, and only accept UTF-8. This allows us to eventually lift this
2145 * limitation, should a good, valid usecase arise. */
2146
2147 r = sd_bus_message_read_array(message, 'y', &p, &sz);
2148 if (r < 0)
2149 return r;
2150 if (r == 0)
2151 break;
2152
2153 if (memchr(p, 0, sz))
2154 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Journal field contains zero byte");
2155
2156 eq = memchr(p, '=', sz);
2157 if (!eq)
2158 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Journal field contains no '=' character");
2159 if (!journal_field_valid(p, eq - (const char*) p, false))
2160 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Journal field invalid");
2161
2e59b241 2162 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
aa484f35 2163 t = reallocarray(c->log_extra_fields, c->n_log_extra_fields+1, sizeof(struct iovec));
d3070fbd
LP
2164 if (!t)
2165 return -ENOMEM;
2166 c->log_extra_fields = t;
2167 }
2168
2169 copy = malloc(sz + 1);
2170 if (!copy)
2171 return -ENOMEM;
2172
2173 memcpy(copy, p, sz);
2174 ((uint8_t*) copy)[sz] = 0;
2175
2176 if (!utf8_is_valid(copy))
2177 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Journal field is not valid UTF-8");
2178
2e59b241 2179 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
d3070fbd 2180 c->log_extra_fields[c->n_log_extra_fields++] = IOVEC_MAKE(copy, sz);
2e59b241 2181 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS|UNIT_ESCAPE_C, name, "LogExtraFields=%s", (char*) copy);
d3070fbd
LP
2182
2183 copy = NULL;
2184 }
2185
2186 n++;
2187 }
2188
2189 r = sd_bus_message_exit_container(message);
2190 if (r < 0)
2191 return r;
2192
2e59b241 2193 if (!UNIT_WRITE_FLAGS_NOOP(flags) && n == 0) {
d3070fbd 2194 exec_context_free_log_extra_fields(c);
2e59b241 2195 unit_write_setting(u, flags, name, "LogExtraFields=");
d3070fbd
LP
2196 }
2197
2198 return 1;
7e2a3fcc 2199 }
cffaed83 2200
349cc4a5 2201#if HAVE_SECCOMP
cffaed83 2202
7e2a3fcc
YW
2203 if (streq(name, "SystemCallErrorNumber"))
2204 return bus_set_transient_errno(u, name, &c->syscall_errno, message, flags, error);
2205
2206 if (streq(name, "SystemCallFilter")) {
6b000af4 2207 int allow_list;
3f856a28 2208 _cleanup_strv_free_ char **l = NULL;
cffaed83
YW
2209
2210 r = sd_bus_message_enter_container(message, 'r', "bas");
2211 if (r < 0)
2212 return r;
2213
6b000af4 2214 r = sd_bus_message_read(message, "b", &allow_list);
cffaed83
YW
2215 if (r < 0)
2216 return r;
2217
2218 r = sd_bus_message_read_strv(message, &l);
2219 if (r < 0)
2220 return r;
2221
2222 r = sd_bus_message_exit_container(message);
2223 if (r < 0)
2224 return r;
2225
2e59b241 2226 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
cffaed83 2227 _cleanup_free_ char *joined = NULL;
6b000af4 2228 SeccompParseFlags invert_flag = allow_list ? 0 : SECCOMP_PARSE_INVERT;
898748d8 2229 char **s;
cffaed83 2230
7b943bb7 2231 if (strv_isempty(l)) {
6b000af4 2232 c->syscall_allow_list = false;
8cfa775f 2233 c->syscall_filter = hashmap_free(c->syscall_filter);
cffaed83 2234
898748d8
YW
2235 unit_write_settingf(u, flags, name, "SystemCallFilter=");
2236 return 1;
2237 }
cffaed83 2238
898748d8
YW
2239 if (!c->syscall_filter) {
2240 c->syscall_filter = hashmap_new(NULL);
2241 if (!c->syscall_filter)
2242 return log_oom();
cffaed83 2243
6b000af4 2244 c->syscall_allow_list = allow_list;
8cfa775f 2245
6b000af4 2246 if (c->syscall_allow_list) {
e7ccdfa8
ZJS
2247 r = seccomp_parse_syscall_filter("@default",
2248 -1,
2249 c->syscall_filter,
72545ae0 2250 SECCOMP_PARSE_PERMISSIVE |
6b000af4 2251 SECCOMP_PARSE_ALLOW_LIST | invert_flag,
58f6ab44
ZJS
2252 u->id,
2253 NULL, 0);
8cfa775f
YW
2254 if (r < 0)
2255 return r;
898748d8
YW
2256 }
2257 }
8cfa775f 2258
898748d8
YW
2259 STRV_FOREACH(s, l) {
2260 _cleanup_free_ char *n = NULL;
2261 int e;
cffaed83 2262
898748d8
YW
2263 r = parse_syscall_and_errno(*s, &n, &e);
2264 if (r < 0)
2265 return r;
cffaed83 2266
e7ccdfa8
ZJS
2267 r = seccomp_parse_syscall_filter(n,
2268 e,
2269 c->syscall_filter,
72545ae0
AZ
2270 SECCOMP_PARSE_LOG | SECCOMP_PARSE_PERMISSIVE |
2271 invert_flag |
6b000af4 2272 (c->syscall_allow_list ? SECCOMP_PARSE_ALLOW_LIST : 0),
58f6ab44
ZJS
2273 u->id,
2274 NULL, 0);
898748d8
YW
2275 if (r < 0)
2276 return r;
cffaed83
YW
2277 }
2278
2279 joined = strv_join(l, " ");
2280 if (!joined)
2281 return -ENOMEM;
2282
6b000af4 2283 unit_write_settingf(u, flags, name, "SystemCallFilter=%s%s", allow_list ? "" : "~", joined);
cffaed83
YW
2284 }
2285
2286 return 1;
2287
9df2cdd8
TM
2288 } else if (streq(name, "SystemCallLog")) {
2289 int allow_list;
2290 _cleanup_strv_free_ char **l = NULL;
2291
2292 r = sd_bus_message_enter_container(message, 'r', "bas");
2293 if (r < 0)
2294 return r;
2295
2296 r = sd_bus_message_read(message, "b", &allow_list);
2297 if (r < 0)
2298 return r;
2299
2300 r = sd_bus_message_read_strv(message, &l);
2301 if (r < 0)
2302 return r;
2303
2304 r = sd_bus_message_exit_container(message);
2305 if (r < 0)
2306 return r;
2307
2308 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2309 _cleanup_free_ char *joined = NULL;
2310 SeccompParseFlags invert_flag = allow_list ? 0 : SECCOMP_PARSE_INVERT;
2311 char **s;
2312
2313 if (strv_isempty(l)) {
2314 c->syscall_log_allow_list = false;
2315 c->syscall_log = hashmap_free(c->syscall_log);
2316
2317 unit_write_settingf(u, flags, name, "SystemCallLog=");
2318 return 1;
2319 }
2320
2321 if (!c->syscall_log) {
2322 c->syscall_log = hashmap_new(NULL);
2323 if (!c->syscall_log)
2324 return log_oom();
2325
2326 c->syscall_log_allow_list = allow_list;
2327 }
2328
2329 STRV_FOREACH(s, l) {
2330 _cleanup_free_ char *n = NULL;
2331 int e;
2332
2333 r = parse_syscall_and_errno(*s, &n, &e);
2334 if (r < 0)
2335 return r;
2336
2337 r = seccomp_parse_syscall_filter(n,
2338 0, /* errno not used */
2339 c->syscall_log,
2340 SECCOMP_PARSE_LOG | SECCOMP_PARSE_PERMISSIVE |
2341 invert_flag |
2342 (c->syscall_log_allow_list ? SECCOMP_PARSE_ALLOW_LIST : 0),
2343 u->id,
2344 NULL, 0);
2345 if (r < 0)
2346 return r;
2347 }
2348
2349 joined = strv_join(l, " ");
2350 if (!joined)
2351 return -ENOMEM;
2352
2353 unit_write_settingf(u, flags, name, "SystemCallLog=%s%s", allow_list ? "" : "~", joined);
2354 }
2355
2356 return 1;
2357
cffaed83
YW
2358 } else if (streq(name, "SystemCallArchitectures")) {
2359 _cleanup_strv_free_ char **l = NULL;
2360
2361 r = sd_bus_message_read_strv(message, &l);
2362 if (r < 0)
2363 return r;
2364
2e59b241 2365 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
cffaed83
YW
2366 _cleanup_free_ char *joined = NULL;
2367
7b943bb7 2368 if (strv_isempty(l))
cffaed83
YW
2369 c->syscall_archs = set_free(c->syscall_archs);
2370 else {
2371 char **s;
2372
cffaed83
YW
2373 STRV_FOREACH(s, l) {
2374 uint32_t a;
2375
2376 r = seccomp_arch_from_string(*s, &a);
2377 if (r < 0)
2378 return r;
2379
de7fef4b 2380 r = set_ensure_put(&c->syscall_archs, NULL, UINT32_TO_PTR(a + 1));
cffaed83
YW
2381 if (r < 0)
2382 return r;
2383 }
2384
2385 }
2386
2387 joined = strv_join(l, " ");
2388 if (!joined)
2389 return -ENOMEM;
2390
2e59b241 2391 unit_write_settingf(u, flags, name, "%s=%s", name, joined);
cffaed83
YW
2392 }
2393
2394 return 1;
2395
cffaed83 2396 } else if (streq(name, "RestrictAddressFamilies")) {
6b000af4 2397 int allow_list;
3f856a28 2398 _cleanup_strv_free_ char **l = NULL;
cffaed83
YW
2399
2400 r = sd_bus_message_enter_container(message, 'r', "bas");
2401 if (r < 0)
2402 return r;
2403
6b000af4 2404 r = sd_bus_message_read(message, "b", &allow_list);
cffaed83
YW
2405 if (r < 0)
2406 return r;
2407
2408 r = sd_bus_message_read_strv(message, &l);
2409 if (r < 0)
2410 return r;
2411
2412 r = sd_bus_message_exit_container(message);
2413 if (r < 0)
2414 return r;
2415
2e59b241 2416 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
cffaed83 2417 _cleanup_free_ char *joined = NULL;
9ee896d5 2418 char **s;
cffaed83 2419
7b943bb7 2420 if (strv_isempty(l)) {
6b000af4 2421 c->address_families_allow_list = false;
cffaed83 2422 c->address_families = set_free(c->address_families);
cffaed83 2423
9ee896d5
YW
2424 unit_write_settingf(u, flags, name, "RestrictAddressFamilies=");
2425 return 1;
2426 }
cffaed83 2427
9ee896d5
YW
2428 if (!c->address_families) {
2429 c->address_families = set_new(NULL);
2430 if (!c->address_families)
2431 return log_oom();
cffaed83 2432
6b000af4 2433 c->address_families_allow_list = allow_list;
9ee896d5
YW
2434 }
2435
2436 STRV_FOREACH(s, l) {
2437 int af;
cffaed83 2438
9ee896d5 2439 af = af_from_name(*s);
acf4d158
YW
2440 if (af < 0)
2441 return af;
cffaed83 2442
6b000af4 2443 if (allow_list == c->address_families_allow_list) {
cffaed83
YW
2444 r = set_put(c->address_families, INT_TO_PTR(af));
2445 if (r < 0)
2446 return r;
9ee896d5
YW
2447 } else
2448 (void) set_remove(c->address_families, INT_TO_PTR(af));
cffaed83
YW
2449 }
2450
2451 joined = strv_join(l, " ");
2452 if (!joined)
2453 return -ENOMEM;
2454
6b000af4 2455 unit_write_settingf(u, flags, name, "RestrictAddressFamilies=%s%s", allow_list ? "" : "~", joined);
cffaed83
YW
2456 }
2457
2458 return 1;
7e2a3fcc 2459 }
cffaed83 2460#endif
b070c7c0 2461 if (STR_IN_SET(name, "CPUAffinity", "NUMAMask")) {
cffaed83 2462 const void *a;
bd0abfae 2463 size_t n;
b070c7c0 2464 bool affinity = streq(name, "CPUAffinity");
c367f996 2465 _cleanup_(cpu_set_reset) CPUSet set = {};
cffaed83
YW
2466
2467 r = sd_bus_message_read_array(message, 'y', &a, &n);
2468 if (r < 0)
2469 return r;
2470
c367f996
MS
2471 r = cpu_set_from_dbus(a, n, &set);
2472 if (r < 0)
2473 return r;
2474
2e59b241 2475 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
cffaed83 2476 if (n == 0) {
b070c7c0 2477 cpu_set_reset(affinity ? &c->cpu_set : &c->numa_policy.nodes);
2e59b241 2478 unit_write_settingf(u, flags, name, "%s=", name);
cffaed83
YW
2479 } else {
2480 _cleanup_free_ char *str = NULL;
cffaed83 2481
0985c7c4 2482 str = cpu_set_to_string(&set);
a832893f
ZJS
2483 if (!str)
2484 return -ENOMEM;
cffaed83 2485
0985c7c4
ZJS
2486 /* We forego any optimizations here, and always create the structure using
2487 * cpu_set_add_all(), because we don't want to care if the existing size we
2488 * got over dbus is appropriate. */
b070c7c0 2489 r = cpu_set_add_all(affinity ? &c->cpu_set : &c->numa_policy.nodes, &set);
0985c7c4
ZJS
2490 if (r < 0)
2491 return r;
501941aa 2492
2e59b241 2493 unit_write_settingf(u, flags, name, "%s=%s", name, str);
cffaed83
YW
2494 }
2495 }
2496
c7040b5d
LP
2497 return 1;
2498
e2b2fb7f
MS
2499 } else if (streq(name, "CPUAffinityFromNUMA")) {
2500 int q;
2501
2502 r = sd_bus_message_read_basic(message, 'b', &q);
2503 if (r < 0)
2504 return r;
2505
2506 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2507 c->cpu_affinity_from_numa = q;
2508 unit_write_settingf(u, flags, name, "%s=%s", "CPUAffinity", "numa");
2509 }
2510
2511 return 1;
2512
b070c7c0
MS
2513 } else if (streq(name, "NUMAPolicy")) {
2514 int32_t type;
2515
2516 r = sd_bus_message_read(message, "i", &type);
2517 if (r < 0)
2518 return r;
2519
2520 if (!mpol_is_valid(type))
2521 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid NUMAPolicy value: %i", type);
2522
2523 if (!UNIT_WRITE_FLAGS_NOOP(flags))
2524 c->numa_policy.type = type;
2525
2526 return 1;
e2b2fb7f 2527
c250bf67
YW
2528 } else if (streq(name, "Nice")) {
2529 int32_t q;
2530
2531 r = sd_bus_message_read(message, "i", &q);
2532 if (r < 0)
2533 return r;
2534
2535 if (!nice_is_valid(q))
2536 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid Nice value: %i", q);
2537
2538 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2539 c->nice = q;
2540 c->nice_set = true;
2541
2542 unit_write_settingf(u, flags, name, "Nice=%i", q);
2543 }
2544
2545 return 1;
2546
2547 } else if (streq(name, "CPUSchedulingPolicy")) {
2548 int32_t q;
2549
2550 r = sd_bus_message_read(message, "i", &q);
2551 if (r < 0)
2552 return r;
2553
2554 if (!sched_policy_is_valid(q))
2555 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid CPU scheduling policy: %i", q);
2556
2557 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2558 _cleanup_free_ char *s = NULL;
2559
2560 r = sched_policy_to_string_alloc(q, &s);
2561 if (r < 0)
2562 return r;
2563
2564 c->cpu_sched_policy = q;
2565 c->cpu_sched_priority = CLAMP(c->cpu_sched_priority, sched_get_priority_min(q), sched_get_priority_max(q));
2566 c->cpu_sched_set = true;
2567
2568 unit_write_settingf(u, flags, name, "CPUSchedulingPolicy=%s", s);
2569 }
2570
2571 return 1;
2572
2573 } else if (streq(name, "CPUSchedulingPriority")) {
2574 int32_t p, min, max;
2575
2576 r = sd_bus_message_read(message, "i", &p);
2577 if (r < 0)
2578 return r;
2579
2580 min = sched_get_priority_min(c->cpu_sched_policy);
2581 max = sched_get_priority_max(c->cpu_sched_policy);
2582 if (p < min || p > max)
2583 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid CPU scheduling priority: %i", p);
2584
2585 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2586 c->cpu_sched_priority = p;
2587 c->cpu_sched_set = true;
2588
2589 unit_write_settingf(u, flags, name, "CPUSchedulingPriority=%i", p);
2590 }
2591
2592 return 1;
2593
7f452159
LP
2594 } else if (streq(name, "IOSchedulingClass")) {
2595 int32_t q;
2596
2597 r = sd_bus_message_read(message, "i", &q);
2598 if (r < 0)
2599 return r;
2600
2601 if (!ioprio_class_is_valid(q))
2602 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid IO scheduling class: %i", q);
2603
2e59b241 2604 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
7f452159
LP
2605 _cleanup_free_ char *s = NULL;
2606
2607 r = ioprio_class_to_string_alloc(q, &s);
2608 if (r < 0)
2609 return r;
2610
2611 c->ioprio = IOPRIO_PRIO_VALUE(q, IOPRIO_PRIO_DATA(c->ioprio));
2612 c->ioprio_set = true;
2613
2e59b241 2614 unit_write_settingf(u, flags, name, "IOSchedulingClass=%s", s);
7f452159
LP
2615 }
2616
2617 return 1;
2618
2619 } else if (streq(name, "IOSchedulingPriority")) {
2620 int32_t p;
2621
2622 r = sd_bus_message_read(message, "i", &p);
2623 if (r < 0)
2624 return r;
2625
2626 if (!ioprio_priority_is_valid(p))
2627 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid IO scheduling priority: %i", p);
2628
2e59b241 2629 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
7f452159
LP
2630 c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_PRIO_CLASS(c->ioprio), p);
2631 c->ioprio_set = true;
2632
2e59b241 2633 unit_write_settingf(u, flags, name, "IOSchedulingPriority=%i", p);
7f452159
LP
2634 }
2635
2636 return 1;
2637
5f5d8eab
LP
2638 } else if (streq(name, "WorkingDirectory")) {
2639 const char *s;
2640 bool missing_ok;
2641
2642 r = sd_bus_message_read(message, "s", &s);
2643 if (r < 0)
2644 return r;
2645
2646 if (s[0] == '-') {
2647 missing_ok = true;
2648 s++;
2649 } else
2650 missing_ok = false;
2651
d6ff82d3 2652 if (!isempty(s) && !streq(s, "~") && !path_is_absolute(s))
5f5d8eab
LP
2653 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "WorkingDirectory= expects an absolute path or '~'");
2654
2e59b241 2655 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
5f5d8eab
LP
2656 if (streq(s, "~")) {
2657 c->working_directory = mfree(c->working_directory);
2658 c->working_directory_home = true;
2659 } else {
d6ff82d3 2660 r = free_and_strdup(&c->working_directory, empty_to_null(s));
5f5d8eab
LP
2661 if (r < 0)
2662 return r;
2663
2664 c->working_directory_home = false;
2665 }
2666
2667 c->working_directory_missing_ok = missing_ok;
2e59b241 2668 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "WorkingDirectory=%s%s", missing_ok ? "-" : "", s);
5f5d8eab
LP
2669 }
2670
2671 return 1;
2672
52c239d7
LB
2673 } else if (STR_IN_SET(name,
2674 "StandardInputFileDescriptorName", "StandardOutputFileDescriptorName", "StandardErrorFileDescriptorName")) {
2675 const char *s;
2676
2677 r = sd_bus_message_read(message, "s", &s);
2678 if (r < 0)
2679 return r;
2680
d6ff82d3 2681 if (!isempty(s) && !fdname_is_valid(s))
52c239d7
LB
2682 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid file descriptor name");
2683
2e59b241 2684 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
5073ff6b 2685
52c239d7 2686 if (streq(name, "StandardInputFileDescriptorName")) {
d6ff82d3 2687 r = free_and_strdup(c->stdio_fdname + STDIN_FILENO, empty_to_null(s));
52c239d7
LB
2688 if (r < 0)
2689 return r;
5073ff6b 2690
0664775c 2691 c->std_input = EXEC_INPUT_NAMED_FD;
2e59b241 2692 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "StandardInput=fd:%s", exec_context_fdname(c, STDIN_FILENO));
5073ff6b 2693
52c239d7 2694 } else if (streq(name, "StandardOutputFileDescriptorName")) {
d6ff82d3 2695 r = free_and_strdup(c->stdio_fdname + STDOUT_FILENO, empty_to_null(s));
52c239d7
LB
2696 if (r < 0)
2697 return r;
5073ff6b 2698
52c239d7 2699 c->std_output = EXEC_OUTPUT_NAMED_FD;
2e59b241 2700 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "StandardOutput=fd:%s", exec_context_fdname(c, STDOUT_FILENO));
0664775c
LP
2701
2702 } else {
2703 assert(streq(name, "StandardErrorFileDescriptorName"));
5073ff6b 2704
d6ff82d3 2705 r = free_and_strdup(&c->stdio_fdname[STDERR_FILENO], empty_to_null(s));
52c239d7
LB
2706 if (r < 0)
2707 return r;
5073ff6b 2708
52c239d7 2709 c->std_error = EXEC_OUTPUT_NAMED_FD;
2e59b241 2710 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "StandardError=fd:%s", exec_context_fdname(c, STDERR_FILENO));
52c239d7
LB
2711 }
2712 }
2713
2714 return 1;
2715
566b7d23
ZD
2716 } else if (STR_IN_SET(name,
2717 "StandardInputFile",
922ce049
LP
2718 "StandardOutputFile", "StandardOutputFileToAppend",
2719 "StandardErrorFile", "StandardErrorFileToAppend")) {
2038c3f5
LP
2720 const char *s;
2721
2722 r = sd_bus_message_read(message, "s", &s);
2723 if (r < 0)
2724 return r;
2725
d6ff82d3
YW
2726 if (!isempty(s)) {
2727 if (!path_is_absolute(s))
2728 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path %s is not absolute", s);
2729 if (!path_is_normalized(s))
2730 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path %s is not normalized", s);
2731 }
2038c3f5 2732
2e59b241 2733 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2038c3f5
LP
2734
2735 if (streq(name, "StandardInputFile")) {
d6ff82d3 2736 r = free_and_strdup(&c->stdio_file[STDIN_FILENO], empty_to_null(s));
52c239d7
LB
2737 if (r < 0)
2738 return r;
2038c3f5
LP
2739
2740 c->std_input = EXEC_INPUT_FILE;
2e59b241 2741 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "StandardInput=file:%s", s);
2038c3f5 2742
566b7d23 2743 } else if (STR_IN_SET(name, "StandardOutputFile", "StandardOutputFileToAppend")) {
d6ff82d3 2744 r = free_and_strdup(&c->stdio_file[STDOUT_FILENO], empty_to_null(s));
2038c3f5
LP
2745 if (r < 0)
2746 return r;
2747
566b7d23
ZD
2748 if (streq(name, "StandardOutputFile")) {
2749 c->std_output = EXEC_OUTPUT_FILE;
2750 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "StandardOutput=file:%s", s);
2751 } else {
2752 assert(streq(name, "StandardOutputFileToAppend"));
2753 c->std_output = EXEC_OUTPUT_FILE_APPEND;
2754 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "StandardOutput=append:%s", s);
2755 }
2038c3f5 2756 } else {
566b7d23 2757 assert(STR_IN_SET(name, "StandardErrorFile", "StandardErrorFileToAppend"));
2038c3f5 2758
d6ff82d3 2759 r = free_and_strdup(&c->stdio_file[STDERR_FILENO], empty_to_null(s));
2038c3f5
LP
2760 if (r < 0)
2761 return r;
2762
566b7d23
ZD
2763 if (streq(name, "StandardErrorFile")) {
2764 c->std_error = EXEC_OUTPUT_FILE;
1704fba9 2765 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "StandardError=file:%s", s);
566b7d23 2766 } else {
dbe6c4b6
LP
2767 assert(streq(name, "StandardErrorFileToAppend"));
2768 c->std_error = EXEC_OUTPUT_FILE_APPEND;
1704fba9 2769 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "StandardError=append:%s", s);
566b7d23 2770 }
2038c3f5
LP
2771 }
2772 }
2773
2774 return 1;
2775
08f3be7a
LP
2776 } else if (streq(name, "StandardInputData")) {
2777 const void *p;
2778 size_t sz;
2779
2780 r = sd_bus_message_read_array(message, 'y', &p, &sz);
2781 if (r < 0)
2782 return r;
2783
2e59b241 2784 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
08f3be7a
LP
2785 _cleanup_free_ char *encoded = NULL;
2786
2787 if (sz == 0) {
2788 c->stdin_data = mfree(c->stdin_data);
2789 c->stdin_data_size = 0;
2790
2e59b241 2791 unit_write_settingf(u, flags, name, "StandardInputData=");
08f3be7a
LP
2792 } else {
2793 void *q;
2794 ssize_t n;
2795
2796 if (c->stdin_data_size + sz < c->stdin_data_size || /* check for overflow */
2797 c->stdin_data_size + sz > EXEC_STDIN_DATA_MAX)
2798 return -E2BIG;
2799
2800 n = base64mem(p, sz, &encoded);
2801 if (n < 0)
2802 return (int) n;
2803
2804 q = realloc(c->stdin_data, c->stdin_data_size + sz);
2805 if (!q)
2806 return -ENOMEM;
2807
2808 memcpy((uint8_t*) q + c->stdin_data_size, p, sz);
2809
2810 c->stdin_data = q;
2811 c->stdin_data_size += sz;
2812
2e59b241 2813 unit_write_settingf(u, flags, name, "StandardInputData=%s", encoded);
52c239d7
LB
2814 }
2815 }
2816
2817 return 1;
2818
c7040b5d
LP
2819 } else if (streq(name, "Environment")) {
2820
6171b822 2821 _cleanup_strv_free_ char **l = NULL;
c7040b5d
LP
2822
2823 r = sd_bus_message_read_strv(message, &l);
2824 if (r < 0)
2825 return r;
2826
6171b822 2827 if (!strv_env_is_valid(l))
1c68232e
LP
2828 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid environment block.");
2829
2e59b241 2830 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
7b943bb7 2831 if (strv_isempty(l)) {
e9876fc9 2832 c->environment = strv_free(c->environment);
2e59b241 2833 unit_write_setting(u, flags, name, "Environment=");
e9876fc9 2834 } else {
f900f582
ZJS
2835 _cleanup_free_ char *joined = NULL;
2836 char **e;
2837
2e59b241
LP
2838 joined = unit_concat_strv(l, UNIT_ESCAPE_SPECIFIERS|UNIT_ESCAPE_C);
2839 if (!joined)
2840 return -ENOMEM;
2841
6171b822 2842 e = strv_env_merge(2, c->environment, l);
e9876fc9
EV
2843 if (!e)
2844 return -ENOMEM;
c7040b5d 2845
130d3d22 2846 strv_free_and_replace(c->environment, e);
2e59b241 2847 unit_write_settingf(u, flags, name, "Environment=%s", joined);
e9876fc9 2848 }
c7040b5d
LP
2849 }
2850
d584f638
LP
2851 return 1;
2852
00819cc1
LP
2853 } else if (streq(name, "UnsetEnvironment")) {
2854
6171b822 2855 _cleanup_strv_free_ char **l = NULL;
00819cc1
LP
2856
2857 r = sd_bus_message_read_strv(message, &l);
2858 if (r < 0)
2859 return r;
2860
6171b822 2861 if (!strv_env_name_or_assignment_is_valid(l))
00819cc1
LP
2862 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid UnsetEnvironment= list.");
2863
2e59b241 2864 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
7b943bb7 2865 if (strv_isempty(l)) {
00819cc1 2866 c->unset_environment = strv_free(c->unset_environment);
2e59b241 2867 unit_write_setting(u, flags, name, "UnsetEnvironment=");
00819cc1
LP
2868 } else {
2869 _cleanup_free_ char *joined = NULL;
2870 char **e;
2871
2e59b241
LP
2872 joined = unit_concat_strv(l, UNIT_ESCAPE_SPECIFIERS|UNIT_ESCAPE_C);
2873 if (!joined)
2874 return -ENOMEM;
2875
6171b822 2876 e = strv_env_merge(2, c->unset_environment, l);
00819cc1
LP
2877 if (!e)
2878 return -ENOMEM;
2879
130d3d22 2880 strv_free_and_replace(c->unset_environment, e);
2e59b241 2881 unit_write_settingf(u, flags, name, "UnsetEnvironment=%s", joined);
00819cc1
LP
2882 }
2883 }
2884
2885 return 1;
2886
6b862936
EV
2887 } else if (streq(name, "OOMScoreAdjust")) {
2888 int oa;
2889
2890 r = sd_bus_message_read(message, "i", &oa);
2891 if (r < 0)
2892 return r;
2893
2894 if (!oom_score_adjust_is_valid(oa))
2895 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "OOM score adjust value out of range");
2896
2e59b241 2897 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
6b862936
EV
2898 c->oom_score_adjust = oa;
2899 c->oom_score_adjust_set = true;
2e59b241 2900 unit_write_settingf(u, flags, name, "OOMScoreAdjust=%i", oa);
6b862936
EV
2901 }
2902
2903 return 1;
2904
ad21e542
ZJS
2905 } else if (streq(name, "CoredumpFilter")) {
2906 uint64_t f;
2907
2908 r = sd_bus_message_read(message, "t", &f);
2909 if (r < 0)
2910 return r;
2911
2912 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2913 c->coredump_filter = f;
2914 c->coredump_filter_set = true;
2915 unit_write_settingf(u, flags, name, "CoredumpFilter=0x%"PRIx64, f);
2916 }
2917
2918 return 1;
2919
ceb728cf
NC
2920 } else if (streq(name, "EnvironmentFiles")) {
2921
2922 _cleanup_free_ char *joined = NULL;
2923 _cleanup_fclose_ FILE *f = NULL;
9b531f04 2924 _cleanup_strv_free_ char **l = NULL;
ceb728cf 2925 size_t size = 0;
2229f656 2926 char **i;
ceb728cf
NC
2927
2928 r = sd_bus_message_enter_container(message, 'a', "(sb)");
2929 if (r < 0)
2930 return r;
2931
2fe21124 2932 f = open_memstream_unlocked(&joined, &size);
ceb728cf
NC
2933 if (!f)
2934 return -ENOMEM;
2935
2e59b241
LP
2936 fputs("EnvironmentFile=\n", f);
2937
2938 STRV_FOREACH(i, c->environment_files) {
2939 _cleanup_free_ char *q = NULL;
2940
2941 q = specifier_escape(*i);
2942 if (!q)
2943 return -ENOMEM;
2944
2945 fprintf(f, "EnvironmentFile=%s\n", q);
2946 }
ceb728cf
NC
2947
2948 while ((r = sd_bus_message_enter_container(message, 'r', "sb")) > 0) {
2949 const char *path;
2950 int b;
2951
ceb728cf
NC
2952 r = sd_bus_message_read(message, "sb", &path, &b);
2953 if (r < 0)
2954 return r;
2955
2956 r = sd_bus_message_exit_container(message);
2957 if (r < 0)
2958 return r;
2959
d2d6c096
LP
2960 if (!path_is_absolute(path))
2961 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path %s is not absolute.", path);
ceb728cf 2962
2e59b241 2963 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
6abdec98 2964 _cleanup_free_ char *q = NULL, *buf = NULL;
ceb728cf 2965
605405c6 2966 buf = strjoin(b ? "-" : "", path);
2229f656 2967 if (!buf)
ceb728cf
NC
2968 return -ENOMEM;
2969
2e59b241 2970 q = specifier_escape(buf);
6abdec98 2971 if (!q)
2e59b241 2972 return -ENOMEM;
2e59b241
LP
2973
2974 fprintf(f, "EnvironmentFile=%s\n", q);
ceb728cf 2975
6abdec98 2976 r = strv_consume(&l, TAKE_PTR(buf));
ceb728cf
NC
2977 if (r < 0)
2978 return r;
2979 }
2980 }
2981 if (r < 0)
2982 return r;
2983
b0830e21
LP
2984 r = sd_bus_message_exit_container(message);
2985 if (r < 0)
2986 return r;
2987
2229f656
LP
2988 r = fflush_and_check(f);
2989 if (r < 0)
2990 return r;
ceb728cf 2991
2e59b241 2992 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2229f656
LP
2993 if (strv_isempty(l)) {
2994 c->environment_files = strv_free(c->environment_files);
2e59b241 2995 unit_write_setting(u, flags, name, "EnvironmentFile=");
2229f656
LP
2996 } else {
2997 r = strv_extend_strv(&c->environment_files, l, true);
2998 if (r < 0)
2999 return r;
3000
2e59b241 3001 unit_write_setting(u, flags, name, joined);
2229f656
LP
3002 }
3003 }
ceb728cf 3004
ceb728cf
NC
3005 return 1;
3006
b4c14404
FB
3007 } else if (streq(name, "PassEnvironment")) {
3008
6171b822 3009 _cleanup_strv_free_ char **l = NULL;
b4c14404
FB
3010
3011 r = sd_bus_message_read_strv(message, &l);
3012 if (r < 0)
3013 return r;
3014
6171b822 3015 if (!strv_env_name_is_valid(l))
00819cc1 3016 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid PassEnvironment= block.");
b4c14404 3017
2e59b241 3018 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
b4c14404
FB
3019 if (strv_isempty(l)) {
3020 c->pass_environment = strv_free(c->pass_environment);
2e59b241 3021 unit_write_setting(u, flags, name, "PassEnvironment=");
b4c14404
FB
3022 } else {
3023 _cleanup_free_ char *joined = NULL;
3024
9d4f242a
YW
3025 r = strv_extend_strv(&c->pass_environment, l, true);
3026 if (r < 0)
3027 return r;
3028
41de9cc2 3029 /* We write just the new settings out to file, with unresolved specifiers. */
2e59b241 3030 joined = unit_concat_strv(l, UNIT_ESCAPE_SPECIFIERS);
b4c14404
FB
3031 if (!joined)
3032 return -ENOMEM;
3033
2e59b241 3034 unit_write_settingf(u, flags, name, "PassEnvironment=%s", joined);
b4c14404
FB
3035 }
3036 }
3037
3038 return 1;
3039
2a624c36
AP
3040 } else if (STR_IN_SET(name, "ReadWriteDirectories", "ReadOnlyDirectories", "InaccessibleDirectories",
3041 "ReadWritePaths", "ReadOnlyPaths", "InaccessiblePaths")) {
08596068
EV
3042 _cleanup_strv_free_ char **l = NULL;
3043 char ***dirs;
3044 char **p;
3045
3046 r = sd_bus_message_read_strv(message, &l);
3047 if (r < 0)
3048 return r;
3049
3050 STRV_FOREACH(p, l) {
e7bcff4e 3051 char *i = *p;
20b7a007
LP
3052 size_t offset;
3053
20b7a007
LP
3054 offset = i[0] == '-';
3055 offset += i[offset] == '+';
3056 if (!path_is_absolute(i + offset))
08596068 3057 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid %s", name);
e7bcff4e 3058
858d36c1 3059 path_simplify(i + offset, false);
08596068
EV
3060 }
3061
2e59b241 3062 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
c4b41707
AP
3063 if (STR_IN_SET(name, "ReadWriteDirectories", "ReadWritePaths"))
3064 dirs = &c->read_write_paths;
3065 else if (STR_IN_SET(name, "ReadOnlyDirectories", "ReadOnlyPaths"))
3066 dirs = &c->read_only_paths;
3067 else /* "InaccessiblePaths" */
3068 dirs = &c->inaccessible_paths;
08596068 3069
7b943bb7 3070 if (strv_isempty(l)) {
08596068 3071 *dirs = strv_free(*dirs);
2e59b241 3072 unit_write_settingf(u, flags, name, "%s=", name);
08596068 3073 } else {
2e59b241 3074 _cleanup_free_ char *joined = NULL;
08596068 3075
2e59b241 3076 joined = unit_concat_strv(l, UNIT_ESCAPE_SPECIFIERS);
08596068
EV
3077 if (!joined)
3078 return -ENOMEM;
3079
2e59b241
LP
3080 r = strv_extend_strv(dirs, l, true);
3081 if (r < 0)
3082 return -ENOMEM;
08596068 3083
2e59b241
LP
3084 unit_write_settingf(u, flags, name, "%s=%s", name, joined);
3085 }
08596068
EV
3086 }
3087
3088 return 1;
3089
3536f49e 3090 } else if (STR_IN_SET(name, "RuntimeDirectory", "StateDirectory", "CacheDirectory", "LogsDirectory", "ConfigurationDirectory")) {
fa21b5e3
EV
3091 _cleanup_strv_free_ char **l = NULL;
3092 char **p;
3093
3094 r = sd_bus_message_read_strv(message, &l);
3095 if (r < 0)
3096 return r;
3097
3098 STRV_FOREACH(p, l) {
8994a117
YW
3099 if (!path_is_normalized(*p))
3100 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "%s= path is not normalized: %s", name, *p);
3101
3102 if (path_is_absolute(*p))
3103 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "%s= path is absolute: %s", name, *p);
3104
3105 if (path_startswith(*p, "private"))
3106 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "%s= path can't be 'private': %s", name, *p);
fa21b5e3
EV
3107 }
3108
2e59b241 3109 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
3536f49e 3110 ExecDirectoryType i;
b1ea85dc 3111 ExecDirectory *d;
3536f49e 3112
b1ea85dc
LP
3113 assert_se((i = exec_directory_type_from_string(name)) >= 0);
3114 d = c->directories + i;
fa21b5e3
EV
3115
3116 if (strv_isempty(l)) {
b1ea85dc 3117 d->paths = strv_free(d->paths);
2e59b241 3118 unit_write_settingf(u, flags, name, "%s=", name);
fa21b5e3 3119 } else {
2e59b241
LP
3120 _cleanup_free_ char *joined = NULL;
3121
b1ea85dc 3122 r = strv_extend_strv(&d->paths, l, true);
fa21b5e3 3123 if (r < 0)
c425c7c2 3124 return r;
fa21b5e3 3125
2e59b241 3126 joined = unit_concat_strv(l, UNIT_ESCAPE_SPECIFIERS);
fa21b5e3
EV
3127 if (!joined)
3128 return -ENOMEM;
3129
2e59b241 3130 unit_write_settingf(u, flags, name, "%s=%s", name, joined);
fa21b5e3
EV
3131 }
3132 }
3133
3134 return 1;
3135
cffaed83
YW
3136 } else if (STR_IN_SET(name, "AppArmorProfile", "SmackProcessLabel")) {
3137 int ignore;
3138 const char *s;
3139
280921f2 3140 r = sd_bus_message_read(message, "(bs)", &ignore, &s);
cffaed83
YW
3141 if (r < 0)
3142 return r;
3143
2e59b241 3144 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
cffaed83
YW
3145 char **p;
3146 bool *b;
3147
3148 if (streq(name, "AppArmorProfile")) {
3149 p = &c->apparmor_profile;
3150 b = &c->apparmor_profile_ignore;
3151 } else { /* "SmackProcessLabel" */
3152 p = &c->smack_process_label;
3153 b = &c->smack_process_label_ignore;
3154 }
3155
3156 if (isempty(s)) {
3157 *p = mfree(*p);
3158 *b = false;
3159 } else {
3160 if (free_and_strdup(p, s) < 0)
3161 return -ENOMEM;
3162 *b = ignore;
3163 }
3164
2e59b241 3165 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "%s=%s%s", name, ignore ? "-" : "", strempty(s));
cffaed83
YW
3166 }
3167
3168 return 1;
3169
d2d6c096 3170 } else if (STR_IN_SET(name, "BindPaths", "BindReadOnlyPaths")) {
ec04aef4 3171 char *source, *destination;
4ff4c98a
YW
3172 int ignore_enoent;
3173 uint64_t mount_flags;
3174 bool empty = true;
d2d6c096
LP
3175
3176 r = sd_bus_message_enter_container(message, 'a', "(ssbt)");
3177 if (r < 0)
3178 return r;
3179
4ff4c98a 3180 while ((r = sd_bus_message_read(message, "(ssbt)", &source, &destination, &ignore_enoent, &mount_flags)) > 0) {
d2d6c096
LP
3181
3182 if (!path_is_absolute(source))
3183 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Source path %s is not absolute.", source);
3184 if (!path_is_absolute(destination))
91d910e3 3185 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Destination path %s is not absolute.", destination);
d2d6c096
LP
3186 if (!IN_SET(mount_flags, 0, MS_REC))
3187 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown mount flags.");
3188
2e59b241 3189 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
d2d6c096
LP
3190 r = bind_mount_add(&c->bind_mounts, &c->n_bind_mounts,
3191 &(BindMount) {
ec04aef4
TM
3192 .source = source,
3193 .destination = destination,
d2d6c096
LP
3194 .read_only = !!strstr(name, "ReadOnly"),
3195 .recursive = !!(mount_flags & MS_REC),
3196 .ignore_enoent = ignore_enoent,
3197 });
3198 if (r < 0)
3199 return r;
3200
2e59b241
LP
3201 unit_write_settingf(
3202 u, flags|UNIT_ESCAPE_SPECIFIERS, name,
d2d6c096
LP
3203 "%s=%s%s:%s:%s",
3204 name,
3205 ignore_enoent ? "-" : "",
3206 source,
3207 destination,
3208 (mount_flags & MS_REC) ? "rbind" : "norbind");
3209 }
3210
3211 empty = false;
3212 }
3213 if (r < 0)
3214 return r;
3215
3216 r = sd_bus_message_exit_container(message);
3217 if (r < 0)
3218 return r;
3219
3220 if (empty) {
3221 bind_mount_free_many(c->bind_mounts, c->n_bind_mounts);
3222 c->bind_mounts = NULL;
3223 c->n_bind_mounts = 0;
2e59b241
LP
3224
3225 unit_write_settingf(u, flags, name, "%s=", name);
d2d6c096
LP
3226 }
3227
784ad252
YW
3228 return 1;
3229
3230 } else if (streq(name, "TemporaryFileSystem")) {
3231 const char *path, *options;
3232 bool empty = true;
3233
3234 r = sd_bus_message_enter_container(message, 'a', "(ss)");
3235 if (r < 0)
3236 return r;
3237
3238 while ((r = sd_bus_message_read(message, "(ss)", &path, &options)) > 0) {
3239
3240 if (!path_is_absolute(path))
3241 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Mount point %s is not absolute.", path);
3242
3243 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
3244 r = temporary_filesystem_add(&c->temporary_filesystems, &c->n_temporary_filesystems, path, options);
3245 if (r < 0)
3246 return r;
3247
3248 unit_write_settingf(
3249 u, flags|UNIT_ESCAPE_SPECIFIERS, name,
3250 "%s=%s:%s",
3251 name,
3252 path,
3253 options);
3254 }
3255
3256 empty = false;
3257 }
3258 if (r < 0)
3259 return r;
3260
3261 r = sd_bus_message_exit_container(message);
3262 if (r < 0)
3263 return r;
3264
3265 if (empty) {
3266 temporary_filesystem_free_many(c->temporary_filesystems, c->n_temporary_filesystems);
3267 c->temporary_filesystems = NULL;
3268 c->n_temporary_filesystems = 0;
3269
3270 unit_write_settingf(u, flags, name, "%s=", name);
3271 }
3272
83555251 3273 return 1;
d2d6c096 3274
6550c24c
LP
3275 } else if ((suffix = startswith(name, "Limit"))) {
3276 const char *soft = NULL;
3277 int ri;
cab2aca3 3278
6550c24c
LP
3279 ri = rlimit_from_string(suffix);
3280 if (ri < 0) {
3281 soft = endswith(suffix, "Soft");
3282 if (soft) {
3283 const char *n;
cab2aca3 3284
6550c24c
LP
3285 n = strndupa(suffix, soft - suffix);
3286 ri = rlimit_from_string(n);
3287 if (ri >= 0)
3288 name = strjoina("Limit", n);
3289 }
cab2aca3 3290 }
d584f638 3291
6550c24c
LP
3292 if (ri >= 0) {
3293 uint64_t rl;
3294 rlim_t x;
d584f638 3295
6550c24c 3296 r = sd_bus_message_read(message, "t", &rl);
cab2aca3
LP
3297 if (r < 0)
3298 return r;
d584f638 3299
6550c24c
LP
3300 if (rl == (uint64_t) -1)
3301 x = RLIM_INFINITY;
cab2aca3 3302 else {
6550c24c
LP
3303 x = (rlim_t) rl;
3304
3305 if ((uint64_t) x != rl)
3306 return -ERANGE;
d584f638
LP
3307 }
3308
6550c24c
LP
3309 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
3310 _cleanup_free_ char *f = NULL;
3311 struct rlimit nl;
3312
3313 if (c->rlimit[ri]) {
3314 nl = *c->rlimit[ri];
3315
3316 if (soft)
3317 nl.rlim_cur = x;
3318 else
3319 nl.rlim_max = x;
3320 } else
3321 /* When the resource limit is not initialized yet, then assign the value to both fields */
3322 nl = (struct rlimit) {
3323 .rlim_cur = x,
3324 .rlim_max = x,
3325 };
3326
3327 r = rlimit_format(&nl, &f);
3328 if (r < 0)
3329 return r;
3330
3331 if (c->rlimit[ri])
3332 *c->rlimit[ri] = nl;
3333 else {
3334 c->rlimit[ri] = newdup(struct rlimit, &nl, 1);
3335 if (!c->rlimit[ri])
3336 return -ENOMEM;
3337 }
3338
3339 unit_write_settingf(u, flags, name, "%s=%s", name, f);
3340 }
3341
3342 return 1;
d584f638
LP
3343 }
3344
b3d13314
LB
3345 } else if (streq(name, "MountImages")) {
3346 _cleanup_free_ char *format_str = NULL;
3347 MountImage *mount_images = NULL;
3348 size_t n_mount_images = 0;
3349 char *source, *destination;
3350 int permissive;
3351
427353f6 3352 r = sd_bus_message_enter_container(message, 'a', "(ssba(ss))");
b3d13314
LB
3353 if (r < 0)
3354 return r;
3355
427353f6
LB
3356 for (;;) {
3357 _cleanup_(mount_options_free_allp) MountOptions *options = NULL;
3358 _cleanup_free_ char *source_escaped = NULL, *destination_escaped = NULL;
b3d13314
LB
3359 char *tuple;
3360
427353f6
LB
3361 r = sd_bus_message_enter_container(message, 'r', "ssba(ss)");
3362 if (r < 0)
3363 return r;
3364
3365 r = sd_bus_message_read(message, "ssb", &source, &destination, &permissive);
3366 if (r <= 0)
3367 break;
3368
b3d13314
LB
3369 if (!path_is_absolute(source))
3370 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Source path %s is not absolute.", source);
3371 if (!path_is_normalized(source))
3372 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Source path %s is not normalized.", source);
3373 if (!path_is_absolute(destination))
3374 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Destination path %s is not absolute.", destination);
3375 if (!path_is_normalized(destination))
3376 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Destination path %s is not normalized.", destination);
3377
427353f6
LB
3378 /* Need to store them in the unit with the escapes, so that they can be parsed again */
3379 source_escaped = shell_escape(source, ":");
3380 if (!source_escaped)
3381 return -ENOMEM;
3382 destination_escaped = shell_escape(destination, ":");
3383 if (!destination_escaped)
3384 return -ENOMEM;
3385
3386 tuple = strjoin(format_str,
3387 format_str ? " " : "",
3388 permissive ? "-" : "",
3389 source_escaped,
3390 ":",
3391 destination_escaped);
b3d13314
LB
3392 if (!tuple)
3393 return -ENOMEM;
3394 free_and_replace(format_str, tuple);
3395
427353f6
LB
3396 r = read_mount_options(message, error, &options, &format_str, ":");
3397 if (r < 0)
3398 return r;
3399
3400 r = sd_bus_message_exit_container(message);
3401 if (r < 0)
3402 return r;
3403
b3d13314
LB
3404 r = mount_image_add(&mount_images, &n_mount_images,
3405 &(MountImage) {
3406 .source = source,
3407 .destination = destination,
427353f6 3408 .mount_options = options,
b3d13314
LB
3409 .ignore_enoent = permissive,
3410 });
3411 if (r < 0)
3412 return r;
3413 }
3414 if (r < 0)
3415 return r;
3416
3417 r = sd_bus_message_exit_container(message);
3418 if (r < 0)
3419 return r;
3420
3421 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
3422 if (n_mount_images == 0) {
3423 c->mount_images = mount_image_free_many(c->mount_images, &c->n_mount_images);
3424
3425 unit_write_settingf(u, flags, name, "%s=", name);
3426 } else {
3427 for (size_t i = 0; i < n_mount_images; ++i) {
3428 r = mount_image_add(&c->mount_images, &c->n_mount_images, &mount_images[i]);
3429 if (r < 0)
3430 return r;
3431 }
3432
3433 unit_write_settingf(u, flags|UNIT_ESCAPE_C|UNIT_ESCAPE_SPECIFIERS,
3434 name,
3435 "%s=%s",
3436 name,
3437 format_str);
3438 }
3439 }
3440
3441 mount_images = mount_image_free_many(mount_images, &n_mount_images);
3442
3443 return 1;
c7040b5d
LP
3444 }
3445
3446 return 0;
3447}