]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/dbus-execute.c
core: don't try to load units from non-absolute paths
[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
1357 if (!path_is_absolute(path))
1358 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path %s is not absolute.", path);
1359
1360 r = sd_bus_message_read_strv(message, &argv);
1361 if (r < 0)
1362 return r;
1363
b3d59367 1364 r = is_ex_prop ? sd_bus_message_read_strv(message, &ex_opts) : sd_bus_message_read(message, "b", &b);
9c0320e7
YW
1365 if (r < 0)
1366 return r;
1367
1368 r = sd_bus_message_exit_container(message);
1369 if (r < 0)
1370 return r;
1371
1372 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1373 ExecCommand *c;
1374
1375 c = new0(ExecCommand, 1);
1376 if (!c)
1377 return -ENOMEM;
1378
1379 c->path = strdup(path);
1380 if (!c->path) {
1381 free(c);
1382 return -ENOMEM;
1383 }
1384
ae2a15bc 1385 c->argv = TAKE_PTR(argv);
9c0320e7 1386
b3d59367
AZ
1387 if (is_ex_prop) {
1388 r = exec_command_flags_from_strv(ex_opts, &c->flags);
1389 if (r < 0)
1390 return r;
1391 } else
1392 c->flags = b ? EXEC_COMMAND_IGNORE_FAILURE : 0;
9c0320e7 1393
858d36c1 1394 path_simplify(c->path, false);
9c0320e7
YW
1395 exec_command_append_list(exec_command, c);
1396 }
1397
1398 n++;
1399 }
1400 if (r < 0)
1401 return r;
1402
1403 r = sd_bus_message_exit_container(message);
1404 if (r < 0)
1405 return r;
1406
1407 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1408 _cleanup_free_ char *buf = NULL;
1409 _cleanup_fclose_ FILE *f = NULL;
1410 ExecCommand *c;
1411 size_t size = 0;
1412
1413 if (n == 0)
1414 *exec_command = exec_command_free_list(*exec_command);
1415
2fe21124 1416 f = open_memstream_unlocked(&buf, &size);
9c0320e7
YW
1417 if (!f)
1418 return -ENOMEM;
1419
f14bf013 1420 fprintf(f, "%s=\n", name);
9c0320e7
YW
1421
1422 LIST_FOREACH(command, c, *exec_command) {
540ac933 1423 _cleanup_free_ char *a = NULL, *exec_chars = NULL;
9c0320e7 1424
540ac933
LP
1425 exec_chars = exec_command_flags_to_exec_chars(c->flags);
1426 if (!exec_chars)
9c0320e7
YW
1427 return -ENOMEM;
1428
1429 a = unit_concat_strv(c->argv, UNIT_ESCAPE_C|UNIT_ESCAPE_SPECIFIERS);
1430 if (!a)
1431 return -ENOMEM;
1432
540ac933
LP
1433 if (streq_ptr(c->path, c->argv ? c->argv[0] : NULL))
1434 fprintf(f, "%s=%s%s\n", name, exec_chars, a);
1435 else {
1436 _cleanup_free_ char *t = NULL;
1437 const char *p;
1438
1439 p = unit_escape_setting(c->path, UNIT_ESCAPE_C|UNIT_ESCAPE_SPECIFIERS, &t);
1440 if (!p)
1441 return -ENOMEM;
b3d59367 1442
540ac933
LP
1443 fprintf(f, "%s=%s@%s %s\n", name, exec_chars, p, a);
1444 }
9c0320e7
YW
1445 }
1446
1447 r = fflush_and_check(f);
1448 if (r < 0)
1449 return r;
1450
1451 unit_write_setting(u, flags, name, buf);
1452 }
1453
1454 return 1;
1455}
1456
7e2a3fcc
YW
1457static int parse_personality(const char *s, unsigned long *p) {
1458 unsigned long v;
1459
1460 assert(p);
1461
1462 v = personality_from_string(s);
1463 if (v == PERSONALITY_INVALID)
1464 return -EINVAL;
1465
1466 *p = v;
1467 return 0;
1468}
1469
1470static const char* mount_propagation_flags_to_string_with_check(unsigned long n) {
1471 if (!IN_SET(n, 0, MS_SHARED, MS_PRIVATE, MS_SLAVE))
1472 return NULL;
1473
1474 return mount_propagation_flags_to_string(n);
1475}
1476
1477static BUS_DEFINE_SET_TRANSIENT(nsec, "t", uint64_t, nsec_t, NSEC_FMT);
1478static BUS_DEFINE_SET_TRANSIENT_IS_VALID(log_level, "i", int32_t, int, "%" PRIi32, log_level_is_valid);
aad67b80 1479#if HAVE_SECCOMP
005bfaf1 1480static BUS_DEFINE_SET_TRANSIENT_IS_VALID(errno, "i", int32_t, int, "%" PRIi32, seccomp_errno_or_action_is_valid);
aad67b80 1481#endif
7e2a3fcc
YW
1482static BUS_DEFINE_SET_TRANSIENT_PARSE(std_input, ExecInput, exec_input_from_string);
1483static BUS_DEFINE_SET_TRANSIENT_PARSE(std_output, ExecOutput, exec_output_from_string);
1484static BUS_DEFINE_SET_TRANSIENT_PARSE(utmp_mode, ExecUtmpMode, exec_utmp_mode_from_string);
1e8c7bd5
YW
1485static BUS_DEFINE_SET_TRANSIENT_PARSE(protect_system, ProtectSystem, protect_system_from_string);
1486static BUS_DEFINE_SET_TRANSIENT_PARSE(protect_home, ProtectHome, protect_home_from_string);
7e2a3fcc 1487static BUS_DEFINE_SET_TRANSIENT_PARSE(keyring_mode, ExecKeyringMode, exec_keyring_mode_from_string);
4e399953
LP
1488static BUS_DEFINE_SET_TRANSIENT_PARSE(protect_proc, ProtectProc, protect_proc_from_string);
1489static BUS_DEFINE_SET_TRANSIENT_PARSE(proc_subset, ProcSubset, proc_subset_from_string);
7e2a3fcc
YW
1490static BUS_DEFINE_SET_TRANSIENT_PARSE(preserve_mode, ExecPreserveMode, exec_preserve_mode_from_string);
1491static BUS_DEFINE_SET_TRANSIENT_PARSE_PTR(personality, unsigned long, parse_personality);
1492static BUS_DEFINE_SET_TRANSIENT_TO_STRING_ALLOC(secure_bits, "i", int32_t, int, "%" PRIi32, secure_bits_to_string_alloc_with_check);
1493static BUS_DEFINE_SET_TRANSIENT_TO_STRING_ALLOC(capability, "t", uint64_t, uint64_t, "%" PRIu64, capability_set_to_string_alloc);
86c2a9f1 1494static BUS_DEFINE_SET_TRANSIENT_TO_STRING_ALLOC(namespace_flag, "t", uint64_t, unsigned long, "%" PRIu64, namespace_flags_to_string);
7e2a3fcc
YW
1495static BUS_DEFINE_SET_TRANSIENT_TO_STRING(mount_flags, "t", uint64_t, unsigned long, "%" PRIu64, mount_propagation_flags_to_string_with_check);
1496
427353f6
LB
1497/* ret_format_str is an accumulator, so if it has any pre-existing content, new options will be appended to it */
1498static int read_mount_options(sd_bus_message *message, sd_bus_error *error, MountOptions **ret_options, char **ret_format_str, const char *separator) {
1499 _cleanup_(mount_options_free_allp) MountOptions *options = NULL;
1500 _cleanup_free_ char *format_str = NULL;
1501 const char *mount_options, *partition;
1502 int r;
1503
1504 assert(message);
1505 assert(ret_options);
1506 assert(ret_format_str);
1507 assert(separator);
1508
1509 r = sd_bus_message_enter_container(message, 'a', "(ss)");
1510 if (r < 0)
1511 return r;
1512
1513 while ((r = sd_bus_message_read(message, "(ss)", &partition, &mount_options)) > 0) {
1514 _cleanup_free_ char *previous = NULL, *escaped = NULL;
1515 _cleanup_free_ MountOptions *o = NULL;
569a0e42 1516 PartitionDesignator partition_designator;
427353f6
LB
1517
1518 if (chars_intersect(mount_options, WHITESPACE))
1519 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
1520 "Invalid mount options string, contains whitespace character(s): %s", mount_options);
1521
1522 partition_designator = partition_designator_from_string(partition);
1523 if (partition_designator < 0)
1524 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid partition name %s", partition);
1525
1526 /* Need to store them in the unit with the escapes, so that they can be parsed again */
1527 escaped = shell_escape(mount_options, ":");
1528 if (!escaped)
1529 return -ENOMEM;
1530
1531 previous = TAKE_PTR(format_str);
1532 format_str = strjoin(previous, previous ? separator : "", partition, ":", escaped);
1533 if (!format_str)
1534 return -ENOMEM;
1535
1536 o = new(MountOptions, 1);
1537 if (!o)
1538 return -ENOMEM;
1539 *o = (MountOptions) {
1540 .partition_designator = partition_designator,
1541 .options = strdup(mount_options),
1542 };
1543 if (!o->options)
1544 return -ENOMEM;
1545 LIST_APPEND(mount_options, options, TAKE_PTR(o));
1546 }
1547 if (r < 0)
1548 return r;
1549
1550 r = sd_bus_message_exit_container(message);
1551 if (r < 0)
1552 return r;
1553
1554 if (!LIST_IS_EMPTY(options)) {
1555 char *final = strjoin(*ret_format_str, !isempty(*ret_format_str) ? separator : "", format_str);
1556 if (!final)
1557 return -ENOMEM;
1558 free_and_replace(*ret_format_str, final);
1559 LIST_JOIN(mount_options, *ret_options, options);
1560 }
1561
1562 return 0;
1563}
1564
c7040b5d
LP
1565int bus_exec_context_set_transient_property(
1566 Unit *u,
1567 ExecContext *c,
1568 const char *name,
1569 sd_bus_message *message,
2e59b241 1570 UnitWriteFlags flags,
c7040b5d
LP
1571 sd_bus_error *error) {
1572
6550c24c
LP
1573 const char *suffix;
1574 int r;
c7040b5d
LP
1575
1576 assert(u);
1577 assert(c);
1578 assert(name);
1579 assert(message);
1580
2e59b241
LP
1581 flags |= UNIT_PRIVATE;
1582
7e2a3fcc 1583 if (streq(name, "User"))
7a8867ab 1584 return bus_set_transient_user_relaxed(u, name, &c->user, message, flags, error);
c7040b5d 1585
7e2a3fcc 1586 if (streq(name, "Group"))
7a8867ab 1587 return bus_set_transient_user_relaxed(u, name, &c->group, message, flags, error);
c7040b5d 1588
7e2a3fcc
YW
1589 if (streq(name, "TTYPath"))
1590 return bus_set_transient_path(u, name, &c->tty_path, message, flags, error);
6f3e7985 1591
7e2a3fcc
YW
1592 if (streq(name, "RootImage"))
1593 return bus_set_transient_path(u, name, &c->root_image, message, flags, error);
76736280 1594
18d73705
LB
1595 if (streq(name, "RootImageOptions")) {
1596 _cleanup_(mount_options_free_allp) MountOptions *options = NULL;
1597 _cleanup_free_ char *format_str = NULL;
18d73705 1598
427353f6 1599 r = read_mount_options(message, error, &options, &format_str, " ");
18d73705
LB
1600 if (r < 0)
1601 return r;
1602
1603 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1604 if (LIST_IS_EMPTY(options)) {
1605 c->root_image_options = mount_options_free_all(c->root_image_options);
1606 unit_write_settingf(u, flags, name, "%s=", name);
1607 } else {
1608 LIST_JOIN(mount_options, c->root_image_options, options);
1609 unit_write_settingf(
1610 u, flags|UNIT_ESCAPE_SPECIFIERS, name,
1611 "%s=%s",
1612 name,
1613 format_str);
1614 }
1615 }
1616
1617 return 1;
1618 }
1619
0389f4fa
LB
1620 if (streq(name, "RootHash")) {
1621 const void *roothash_decoded;
1622 size_t roothash_decoded_size;
1623
1624 r = sd_bus_message_read_array(message, 'y', &roothash_decoded, &roothash_decoded_size);
1625 if (r < 0)
1626 return r;
1627
1628 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1629 _cleanup_free_ char *encoded = NULL;
1630
1631 if (roothash_decoded_size == 0) {
1632 c->root_hash_path = mfree(c->root_hash_path);
1633 c->root_hash = mfree(c->root_hash);
1634 c->root_hash_size = 0;
1635
1636 unit_write_settingf(u, flags, name, "RootHash=");
1637 } else {
1638 _cleanup_free_ void *p;
1639
1640 encoded = hexmem(roothash_decoded, roothash_decoded_size);
1641 if (!encoded)
1642 return -ENOMEM;
1643
1644 p = memdup(roothash_decoded, roothash_decoded_size);
1645 if (!p)
1646 return -ENOMEM;
1647
1648 free_and_replace(c->root_hash, p);
1649 c->root_hash_size = roothash_decoded_size;
1650 c->root_hash_path = mfree(c->root_hash_path);
1651
1652 unit_write_settingf(u, flags, name, "RootHash=%s", encoded);
1653 }
1654 }
1655
1656 return 1;
1657 }
1658
1659 if (streq(name, "RootHashPath")) {
1660 c->root_hash_size = 0;
1661 c->root_hash = mfree(c->root_hash);
1662
1663 return bus_set_transient_path(u, "RootHash", &c->root_hash_path, message, flags, error);
1664 }
1665
d4d55b0d
LB
1666 if (streq(name, "RootHashSignature")) {
1667 const void *roothash_sig_decoded;
1668 size_t roothash_sig_decoded_size;
1669
1670 r = sd_bus_message_read_array(message, 'y', &roothash_sig_decoded, &roothash_sig_decoded_size);
1671 if (r < 0)
1672 return r;
1673
1674 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1675 _cleanup_free_ char *encoded = NULL;
1676
1677 if (roothash_sig_decoded_size == 0) {
1678 c->root_hash_sig_path = mfree(c->root_hash_sig_path);
1679 c->root_hash_sig = mfree(c->root_hash_sig);
1680 c->root_hash_sig_size = 0;
1681
1682 unit_write_settingf(u, flags, name, "RootHashSignature=");
1683 } else {
1684 _cleanup_free_ void *p;
1685 ssize_t len;
1686
1687 len = base64mem(roothash_sig_decoded, roothash_sig_decoded_size, &encoded);
1688 if (len < 0)
1689 return -ENOMEM;
1690
1691 p = memdup(roothash_sig_decoded, roothash_sig_decoded_size);
1692 if (!p)
1693 return -ENOMEM;
1694
1695 free_and_replace(c->root_hash_sig, p);
1696 c->root_hash_sig_size = roothash_sig_decoded_size;
1697 c->root_hash_sig_path = mfree(c->root_hash_sig_path);
1698
1699 unit_write_settingf(u, flags, name, "RootHashSignature=base64:%s", encoded);
1700 }
1701 }
1702
1703 return 1;
1704 }
1705
1706 if (streq(name, "RootHashSignaturePath")) {
1707 c->root_hash_sig_size = 0;
1708 c->root_hash_sig = mfree(c->root_hash_sig);
1709
1710 return bus_set_transient_path(u, "RootHashSignature", &c->root_hash_sig_path, message, flags, error);
1711 }
1712
0389f4fa
LB
1713 if (streq(name, "RootVerity"))
1714 return bus_set_transient_path(u, name, &c->root_verity, message, flags, error);
1715
7e2a3fcc
YW
1716 if (streq(name, "RootDirectory"))
1717 return bus_set_transient_path(u, name, &c->root_directory, message, flags, error);
c7040b5d 1718
7e2a3fcc
YW
1719 if (streq(name, "SyslogIdentifier"))
1720 return bus_set_transient_string(u, name, &c->syslog_identifier, message, flags, error);
c7040b5d 1721
7e2a3fcc
YW
1722 if (streq(name, "LogLevelMax"))
1723 return bus_set_transient_log_level(u, name, &c->log_level_max, message, flags, error);
1724
90fc172e 1725 if (streq(name, "LogRateLimitIntervalUSec"))
5ac1530e 1726 return bus_set_transient_usec(u, name, &c->log_ratelimit_interval_usec, message, flags, error);
90fc172e
AZ
1727
1728 if (streq(name, "LogRateLimitBurst"))
5ac1530e 1729 return bus_set_transient_unsigned(u, name, &c->log_ratelimit_burst, message, flags, error);
90fc172e 1730
7e2a3fcc
YW
1731 if (streq(name, "Personality"))
1732 return bus_set_transient_personality(u, name, &c->personality, message, flags, error);
1733
7e2a3fcc
YW
1734 if (streq(name, "StandardInput"))
1735 return bus_set_transient_std_input(u, name, &c->std_input, message, flags, error);
1736
1737 if (streq(name, "StandardOutput"))
1738 return bus_set_transient_std_output(u, name, &c->std_output, message, flags, error);
1739
1740 if (streq(name, "StandardError"))
1741 return bus_set_transient_std_output(u, name, &c->std_error, message, flags, error);
1742
1743 if (streq(name, "IgnoreSIGPIPE"))
1744 return bus_set_transient_bool(u, name, &c->ignore_sigpipe, message, flags, error);
1745
1746 if (streq(name, "TTYVHangup"))
1747 return bus_set_transient_bool(u, name, &c->tty_vhangup, message, flags, error);
1748
1749 if (streq(name, "TTYReset"))
1750 return bus_set_transient_bool(u, name, &c->tty_reset, message, flags, error);
1751
1752 if (streq(name, "TTYVTDisallocate"))
1753 return bus_set_transient_bool(u, name, &c->tty_vt_disallocate, message, flags, error);
1754
1755 if (streq(name, "PrivateTmp"))
1756 return bus_set_transient_bool(u, name, &c->private_tmp, message, flags, error);
1757
1758 if (streq(name, "PrivateDevices"))
1759 return bus_set_transient_bool(u, name, &c->private_devices, message, flags, error);
1760
228af36f
LP
1761 if (streq(name, "PrivateMounts"))
1762 return bus_set_transient_bool(u, name, &c->private_mounts, message, flags, error);
1763
7e2a3fcc
YW
1764 if (streq(name, "PrivateNetwork"))
1765 return bus_set_transient_bool(u, name, &c->private_network, message, flags, error);
1766
1767 if (streq(name, "PrivateUsers"))
1768 return bus_set_transient_bool(u, name, &c->private_users, message, flags, error);
1769
1770 if (streq(name, "NoNewPrivileges"))
1771 return bus_set_transient_bool(u, name, &c->no_new_privileges, message, flags, error);
1772
1773 if (streq(name, "SyslogLevelPrefix"))
1774 return bus_set_transient_bool(u, name, &c->syslog_level_prefix, message, flags, error);
1775
1776 if (streq(name, "MemoryDenyWriteExecute"))
1777 return bus_set_transient_bool(u, name, &c->memory_deny_write_execute, message, flags, error);
1778
1779 if (streq(name, "RestrictRealtime"))
1780 return bus_set_transient_bool(u, name, &c->restrict_realtime, message, flags, error);
1781
f69567cb
LP
1782 if (streq(name, "RestrictSUIDSGID"))
1783 return bus_set_transient_bool(u, name, &c->restrict_suid_sgid, message, flags, error);
1784
7e2a3fcc
YW
1785 if (streq(name, "DynamicUser"))
1786 return bus_set_transient_bool(u, name, &c->dynamic_user, message, flags, error);
1787
1788 if (streq(name, "RemoveIPC"))
1789 return bus_set_transient_bool(u, name, &c->remove_ipc, message, flags, error);
1790
1791 if (streq(name, "ProtectKernelTunables"))
1792 return bus_set_transient_bool(u, name, &c->protect_kernel_tunables, message, flags, error);
1793
1794 if (streq(name, "ProtectKernelModules"))
1795 return bus_set_transient_bool(u, name, &c->protect_kernel_modules, message, flags, error);
1796
84703040
KK
1797 if (streq(name, "ProtectKernelLogs"))
1798 return bus_set_transient_bool(u, name, &c->protect_kernel_logs, message, flags, error);
1799
fc64760d
KK
1800 if (streq(name, "ProtectClock"))
1801 return bus_set_transient_bool(u, name, &c->protect_clock, message, flags, error);
1802
7e2a3fcc
YW
1803 if (streq(name, "ProtectControlGroups"))
1804 return bus_set_transient_bool(u, name, &c->protect_control_groups, message, flags, error);
1805
1806 if (streq(name, "MountAPIVFS"))
1807 return bus_set_transient_bool(u, name, &c->mount_apivfs, message, flags, error);
1808
1809 if (streq(name, "CPUSchedulingResetOnFork"))
1810 return bus_set_transient_bool(u, name, &c->cpu_sched_reset_on_fork, message, flags, error);
1811
1812 if (streq(name, "NonBlocking"))
1813 return bus_set_transient_bool(u, name, &c->non_blocking, message, flags, error);
1814
1815 if (streq(name, "LockPersonality"))
1816 return bus_set_transient_bool(u, name, &c->lock_personality, message, flags, error);
1817
aecd5ac6
TM
1818 if (streq(name, "ProtectHostname"))
1819 return bus_set_transient_bool(u, name, &c->protect_hostname, message, flags, error);
1820
7e2a3fcc
YW
1821 if (streq(name, "UtmpIdentifier"))
1822 return bus_set_transient_string(u, name, &c->utmp_id, message, flags, error);
1823
1824 if (streq(name, "UtmpMode"))
1825 return bus_set_transient_utmp_mode(u, name, &c->utmp_mode, message, flags, error);
1826
1827 if (streq(name, "PAMName"))
1828 return bus_set_transient_string(u, name, &c->pam_name, message, flags, error);
1829
1830 if (streq(name, "TimerSlackNSec"))
1831 return bus_set_transient_nsec(u, name, &c->timer_slack_nsec, message, flags, error);
1832
1833 if (streq(name, "ProtectSystem"))
1834 return bus_set_transient_protect_system(u, name, &c->protect_system, message, flags, error);
1835
1836 if (streq(name, "ProtectHome"))
1837 return bus_set_transient_protect_home(u, name, &c->protect_home, message, flags, error);
cffaed83 1838
7e2a3fcc
YW
1839 if (streq(name, "KeyringMode"))
1840 return bus_set_transient_keyring_mode(u, name, &c->keyring_mode, message, flags, error);
1841
4e399953
LP
1842 if (streq(name, "ProtectProc"))
1843 return bus_set_transient_protect_proc(u, name, &c->protect_proc, message, flags, error);
1844
1845 if (streq(name, "ProcSubset"))
1846 return bus_set_transient_proc_subset(u, name, &c->proc_subset, message, flags, error);
1847
7e2a3fcc
YW
1848 if (streq(name, "RuntimeDirectoryPreserve"))
1849 return bus_set_transient_preserve_mode(u, name, &c->runtime_directory_preserve_mode, message, flags, error);
1850
1851 if (streq(name, "UMask"))
1852 return bus_set_transient_mode_t(u, name, &c->umask, message, flags, error);
1853
1854 if (streq(name, "RuntimeDirectoryMode"))
1855 return bus_set_transient_mode_t(u, name, &c->directories[EXEC_DIRECTORY_RUNTIME].mode, message, flags, error);
1856
1857 if (streq(name, "StateDirectoryMode"))
1858 return bus_set_transient_mode_t(u, name, &c->directories[EXEC_DIRECTORY_STATE].mode, message, flags, error);
1859
1860 if (streq(name, "CacheDirectoryMode"))
1861 return bus_set_transient_mode_t(u, name, &c->directories[EXEC_DIRECTORY_CACHE].mode, message, flags, error);
1862
1863 if (streq(name, "LogsDirectoryMode"))
1864 return bus_set_transient_mode_t(u, name, &c->directories[EXEC_DIRECTORY_LOGS].mode, message, flags, error);
1865
1866 if (streq(name, "ConfigurationDirectoryMode"))
1867 return bus_set_transient_mode_t(u, name, &c->directories[EXEC_DIRECTORY_CONFIGURATION].mode, message, flags, error);
1868
1869 if (streq(name, "SELinuxContext"))
1870 return bus_set_transient_string(u, name, &c->selinux_context, message, flags, error);
1871
1872 if (streq(name, "SecureBits"))
1873 return bus_set_transient_secure_bits(u, name, &c->secure_bits, message, flags, error);
1874
1875 if (streq(name, "CapabilityBoundingSet"))
1876 return bus_set_transient_capability(u, name, &c->capability_bounding_set, message, flags, error);
1877
1878 if (streq(name, "AmbientCapabilities"))
1879 return bus_set_transient_capability(u, name, &c->capability_ambient_set, message, flags, error);
1880
7e2a3fcc
YW
1881 if (streq(name, "RestrictNamespaces"))
1882 return bus_set_transient_namespace_flag(u, name, &c->restrict_namespaces, message, flags, error);
1883
1884 if (streq(name, "MountFlags"))
1885 return bus_set_transient_mount_flags(u, name, &c->mount_flags, message, flags, error);
1886
a8d08f39
LP
1887 if (streq(name, "NetworkNamespacePath"))
1888 return bus_set_transient_path(u, name, &c->network_namespace_path, message, flags, error);
1889
7e2a3fcc 1890 if (streq(name, "SupplementaryGroups")) {
cffaed83
YW
1891 _cleanup_strv_free_ char **l = NULL;
1892 char **p;
1893
1894 r = sd_bus_message_read_strv(message, &l);
1895 if (r < 0)
1896 return r;
1897
d2a23692 1898 STRV_FOREACH(p, l)
7a8867ab 1899 if (!isempty(*p) && !valid_user_group_name(*p, VALID_USER_ALLOW_NUMERIC|VALID_USER_RELAX|VALID_USER_WARN))
d2a23692
ZJS
1900 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
1901 "Invalid supplementary group names");
cffaed83 1902
2e59b241 1903 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
7b943bb7 1904 if (strv_isempty(l)) {
cffaed83 1905 c->supplementary_groups = strv_free(c->supplementary_groups);
2e59b241 1906 unit_write_settingf(u, flags, name, "%s=", name);
cffaed83
YW
1907 } else {
1908 _cleanup_free_ char *joined = NULL;
1909
1910 r = strv_extend_strv(&c->supplementary_groups, l, true);
1911 if (r < 0)
1912 return -ENOMEM;
1913
1914 joined = strv_join(c->supplementary_groups, " ");
1915 if (!joined)
1916 return -ENOMEM;
1917
2e59b241 1918 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "%s=%s", name, joined);
cffaed83
YW
1919 }
1920 }
1921
1922 return 1;
1923
bb0c0d6f
LP
1924 } else if (streq(name, "SetCredential")) {
1925 bool isempty = true;
1926
1927 r = sd_bus_message_enter_container(message, 'a', "(say)");
1928 if (r < 0)
1929 return r;
1930
1931 for (;;) {
1932 const char *id;
1933 const void *p;
1934 size_t sz;
1935
1936 r = sd_bus_message_enter_container(message, 'r', "say");
1937 if (r < 0)
1938 return r;
1939 if (r == 0)
1940 break;
1941
1942 r = sd_bus_message_read(message, "s", &id);
1943 if (r < 0)
1944 return r;
1945
1946 r = sd_bus_message_read_array(message, 'y', &p, &sz);
1947 if (r < 0)
1948 return r;
1949
1950 r = sd_bus_message_exit_container(message);
1951 if (r < 0)
1952 return r;
1953
1954 if (!credential_name_valid(id))
1955 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Credential ID is invalid: %s", id);
1956
1957 isempty = false;
1958
1959 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1960 _cleanup_free_ char *a = NULL, *b = NULL;
1961 _cleanup_free_ void *copy = NULL;
1962 ExecSetCredential *old;
1963
1964 copy = memdup(p, sz);
1965 if (!copy)
1966 return -ENOMEM;
1967
1968 old = hashmap_get(c->set_credentials, id);
1969 if (old) {
1970 free_and_replace(old->data, copy);
1971 old->size = sz;
1972 } else {
1973 _cleanup_(exec_set_credential_freep) ExecSetCredential *sc = NULL;
1974
1975 sc = new0(ExecSetCredential, 1);
1976 if (!sc)
1977 return -ENOMEM;
1978
1979 sc->id = strdup(id);
1980 if (!sc->id)
1981 return -ENOMEM;
1982
1983 sc->data = TAKE_PTR(copy);
1984 sc->size = sz;
1985
1986 r = hashmap_ensure_allocated(&c->set_credentials, &exec_set_credential_hash_ops);
1987 if (r < 0)
1988 return r;
1989
1990 r = hashmap_put(c->set_credentials, sc->id, sc);
1991 if (r < 0)
1992 return r;
1993
1994 TAKE_PTR(sc);
1995 }
1996
1997 a = specifier_escape(id);
1998 if (!a)
1999 return -ENOMEM;
2000
2001 b = cescape_length(p, sz);
2002 if (!b)
2003 return -ENOMEM;
2004
2005 (void) unit_write_settingf(u, flags, name, "%s=%s:%s", name, a, b);
2006 }
2007 }
2008
2009 r = sd_bus_message_exit_container(message);
2010 if (r < 0)
2011 return r;
2012
2013 if (!UNIT_WRITE_FLAGS_NOOP(flags) && isempty) {
2014 c->set_credentials = hashmap_free(c->set_credentials);
2015 (void) unit_write_settingf(u, flags, name, "%s=", name);
2016 }
2017
2018 return 1;
2019
2020 } else if (streq(name, "LoadCredential")) {
2021 bool isempty = true;
2022
2023 r = sd_bus_message_enter_container(message, 'a', "(ss)");
2024 if (r < 0)
2025 return r;
2026
2027 for (;;) {
2028 const char *id, *source;
2029
2030 r = sd_bus_message_read(message, "(ss)", &id, &source);
2031 if (r < 0)
2032 return r;
2033 if (r == 0)
2034 break;
2035
2036 if (!credential_name_valid(id))
2037 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Credential ID is invalid: %s", id);
2038
2039 if (!(path_is_absolute(source) ? path_is_normalized(source) : credential_name_valid(source)))
2040 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Credential source is invalid: %s", source);
2041
2042 isempty = false;
2043
2044 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2045 r = strv_extend_strv(&c->load_credentials, STRV_MAKE(id, source), /* filter_duplicates = */ false);
2046 if (r < 0)
2047 return r;
2048
2049 (void) unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "%s=%s:%s", name, id, source);
2050 }
2051 }
2052
2053 r = sd_bus_message_exit_container(message);
2054 if (r < 0)
2055 return r;
2056
2057 if (!UNIT_WRITE_FLAGS_NOOP(flags) && isempty) {
2058 c->load_credentials = strv_free(c->load_credentials);
2059 (void) unit_write_settingf(u, flags, name, "%s=", name);
2060 }
2061
2062 return 1;
2063
a8a13575 2064 } else if (streq(name, "SyslogLevel")) {
8d1dd6ab 2065 int32_t level;
a8a13575
EV
2066
2067 r = sd_bus_message_read(message, "i", &level);
2068 if (r < 0)
2069 return r;
2070
e0d6e0fa
EV
2071 if (!log_level_is_valid(level))
2072 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Log level value out of range");
2073
2e59b241 2074 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
a8a13575 2075 c->syslog_priority = (c->syslog_priority & LOG_FACMASK) | level;
2e59b241 2076 unit_write_settingf(u, flags, name, "SyslogLevel=%i", level);
a8a13575
EV
2077 }
2078
460ed929 2079 return 1;
7e2a3fcc 2080
460ed929 2081 } else if (streq(name, "SyslogFacility")) {
8d1dd6ab 2082 int32_t facility;
460ed929
EV
2083
2084 r = sd_bus_message_read(message, "i", &facility);
2085 if (r < 0)
2086 return r;
2087
e0d6e0fa
EV
2088 if (!log_facility_unshifted_is_valid(facility))
2089 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Log facility value out of range");
2090
2e59b241 2091 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
460ed929 2092 c->syslog_priority = (facility << 3) | LOG_PRI(c->syslog_priority);
2e59b241 2093 unit_write_settingf(u, flags, name, "SyslogFacility=%i", facility);
460ed929
EV
2094 }
2095
cffaed83 2096 return 1;
d3070fbd 2097
91dd5f7c
LP
2098 } else if (streq(name, "LogNamespace")) {
2099 const char *n;
2100
2101 r = sd_bus_message_read(message, "s", &n);
2102 if (r < 0)
2103 return r;
2104
2105 if (!isempty(n) && !log_namespace_name_valid(n))
2106 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Log namespace name not valid");
2107
2108 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2109
2110 if (isempty(n)) {
2111 c->log_namespace = mfree(c->log_namespace);
2112 unit_write_settingf(u, flags, name, "%s=", name);
2113 } else {
2114 r = free_and_strdup(&c->log_namespace, n);
2115 if (r < 0)
2116 return r;
2117
2118 unit_write_settingf(u, flags, name, "%s=%s", name, n);
2119 }
2120 }
2121
2122 return 1;
2123
d3070fbd
LP
2124 } else if (streq(name, "LogExtraFields")) {
2125 size_t n = 0;
2126
2127 r = sd_bus_message_enter_container(message, 'a', "ay");
2128 if (r < 0)
2129 return r;
2130
2131 for (;;) {
2132 _cleanup_free_ void *copy = NULL;
2133 struct iovec *t;
2134 const char *eq;
2135 const void *p;
2136 size_t sz;
2137
2138 /* Note that we expect a byte array for each field, instead of a string. That's because on the
2139 * lower-level journal fields can actually contain binary data and are not restricted to text,
2140 * and we should not "lose precision" in our types on the way. That said, I am pretty sure
2141 * actually encoding binary data as unit metadata is not a good idea. Hence we actually refuse
2142 * any actual binary data, and only accept UTF-8. This allows us to eventually lift this
2143 * limitation, should a good, valid usecase arise. */
2144
2145 r = sd_bus_message_read_array(message, 'y', &p, &sz);
2146 if (r < 0)
2147 return r;
2148 if (r == 0)
2149 break;
2150
2151 if (memchr(p, 0, sz))
2152 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Journal field contains zero byte");
2153
2154 eq = memchr(p, '=', sz);
2155 if (!eq)
2156 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Journal field contains no '=' character");
2157 if (!journal_field_valid(p, eq - (const char*) p, false))
2158 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Journal field invalid");
2159
2e59b241 2160 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
aa484f35 2161 t = reallocarray(c->log_extra_fields, c->n_log_extra_fields+1, sizeof(struct iovec));
d3070fbd
LP
2162 if (!t)
2163 return -ENOMEM;
2164 c->log_extra_fields = t;
2165 }
2166
2167 copy = malloc(sz + 1);
2168 if (!copy)
2169 return -ENOMEM;
2170
2171 memcpy(copy, p, sz);
2172 ((uint8_t*) copy)[sz] = 0;
2173
2174 if (!utf8_is_valid(copy))
2175 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Journal field is not valid UTF-8");
2176
2e59b241 2177 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
d3070fbd 2178 c->log_extra_fields[c->n_log_extra_fields++] = IOVEC_MAKE(copy, sz);
2e59b241 2179 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS|UNIT_ESCAPE_C, name, "LogExtraFields=%s", (char*) copy);
d3070fbd
LP
2180
2181 copy = NULL;
2182 }
2183
2184 n++;
2185 }
2186
2187 r = sd_bus_message_exit_container(message);
2188 if (r < 0)
2189 return r;
2190
2e59b241 2191 if (!UNIT_WRITE_FLAGS_NOOP(flags) && n == 0) {
d3070fbd 2192 exec_context_free_log_extra_fields(c);
2e59b241 2193 unit_write_setting(u, flags, name, "LogExtraFields=");
d3070fbd
LP
2194 }
2195
2196 return 1;
7e2a3fcc 2197 }
cffaed83 2198
349cc4a5 2199#if HAVE_SECCOMP
cffaed83 2200
7e2a3fcc
YW
2201 if (streq(name, "SystemCallErrorNumber"))
2202 return bus_set_transient_errno(u, name, &c->syscall_errno, message, flags, error);
2203
2204 if (streq(name, "SystemCallFilter")) {
6b000af4 2205 int allow_list;
3f856a28 2206 _cleanup_strv_free_ char **l = NULL;
cffaed83
YW
2207
2208 r = sd_bus_message_enter_container(message, 'r', "bas");
2209 if (r < 0)
2210 return r;
2211
6b000af4 2212 r = sd_bus_message_read(message, "b", &allow_list);
cffaed83
YW
2213 if (r < 0)
2214 return r;
2215
2216 r = sd_bus_message_read_strv(message, &l);
2217 if (r < 0)
2218 return r;
2219
2220 r = sd_bus_message_exit_container(message);
2221 if (r < 0)
2222 return r;
2223
2e59b241 2224 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
cffaed83 2225 _cleanup_free_ char *joined = NULL;
6b000af4 2226 SeccompParseFlags invert_flag = allow_list ? 0 : SECCOMP_PARSE_INVERT;
898748d8 2227 char **s;
cffaed83 2228
7b943bb7 2229 if (strv_isempty(l)) {
6b000af4 2230 c->syscall_allow_list = false;
8cfa775f 2231 c->syscall_filter = hashmap_free(c->syscall_filter);
cffaed83 2232
898748d8
YW
2233 unit_write_settingf(u, flags, name, "SystemCallFilter=");
2234 return 1;
2235 }
cffaed83 2236
898748d8
YW
2237 if (!c->syscall_filter) {
2238 c->syscall_filter = hashmap_new(NULL);
2239 if (!c->syscall_filter)
2240 return log_oom();
cffaed83 2241
6b000af4 2242 c->syscall_allow_list = allow_list;
8cfa775f 2243
6b000af4 2244 if (c->syscall_allow_list) {
e7ccdfa8
ZJS
2245 r = seccomp_parse_syscall_filter("@default",
2246 -1,
2247 c->syscall_filter,
72545ae0 2248 SECCOMP_PARSE_PERMISSIVE |
6b000af4 2249 SECCOMP_PARSE_ALLOW_LIST | invert_flag,
58f6ab44
ZJS
2250 u->id,
2251 NULL, 0);
8cfa775f
YW
2252 if (r < 0)
2253 return r;
898748d8
YW
2254 }
2255 }
8cfa775f 2256
898748d8
YW
2257 STRV_FOREACH(s, l) {
2258 _cleanup_free_ char *n = NULL;
2259 int e;
cffaed83 2260
898748d8
YW
2261 r = parse_syscall_and_errno(*s, &n, &e);
2262 if (r < 0)
2263 return r;
cffaed83 2264
e7ccdfa8
ZJS
2265 r = seccomp_parse_syscall_filter(n,
2266 e,
2267 c->syscall_filter,
72545ae0
AZ
2268 SECCOMP_PARSE_LOG | SECCOMP_PARSE_PERMISSIVE |
2269 invert_flag |
6b000af4 2270 (c->syscall_allow_list ? SECCOMP_PARSE_ALLOW_LIST : 0),
58f6ab44
ZJS
2271 u->id,
2272 NULL, 0);
898748d8
YW
2273 if (r < 0)
2274 return r;
cffaed83
YW
2275 }
2276
2277 joined = strv_join(l, " ");
2278 if (!joined)
2279 return -ENOMEM;
2280
6b000af4 2281 unit_write_settingf(u, flags, name, "SystemCallFilter=%s%s", allow_list ? "" : "~", joined);
cffaed83
YW
2282 }
2283
2284 return 1;
2285
9df2cdd8
TM
2286 } else if (streq(name, "SystemCallLog")) {
2287 int allow_list;
2288 _cleanup_strv_free_ char **l = NULL;
2289
2290 r = sd_bus_message_enter_container(message, 'r', "bas");
2291 if (r < 0)
2292 return r;
2293
2294 r = sd_bus_message_read(message, "b", &allow_list);
2295 if (r < 0)
2296 return r;
2297
2298 r = sd_bus_message_read_strv(message, &l);
2299 if (r < 0)
2300 return r;
2301
2302 r = sd_bus_message_exit_container(message);
2303 if (r < 0)
2304 return r;
2305
2306 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2307 _cleanup_free_ char *joined = NULL;
2308 SeccompParseFlags invert_flag = allow_list ? 0 : SECCOMP_PARSE_INVERT;
2309 char **s;
2310
2311 if (strv_isempty(l)) {
2312 c->syscall_log_allow_list = false;
2313 c->syscall_log = hashmap_free(c->syscall_log);
2314
2315 unit_write_settingf(u, flags, name, "SystemCallLog=");
2316 return 1;
2317 }
2318
2319 if (!c->syscall_log) {
2320 c->syscall_log = hashmap_new(NULL);
2321 if (!c->syscall_log)
2322 return log_oom();
2323
2324 c->syscall_log_allow_list = allow_list;
2325 }
2326
2327 STRV_FOREACH(s, l) {
2328 _cleanup_free_ char *n = NULL;
2329 int e;
2330
2331 r = parse_syscall_and_errno(*s, &n, &e);
2332 if (r < 0)
2333 return r;
2334
2335 r = seccomp_parse_syscall_filter(n,
2336 0, /* errno not used */
2337 c->syscall_log,
2338 SECCOMP_PARSE_LOG | SECCOMP_PARSE_PERMISSIVE |
2339 invert_flag |
2340 (c->syscall_log_allow_list ? SECCOMP_PARSE_ALLOW_LIST : 0),
2341 u->id,
2342 NULL, 0);
2343 if (r < 0)
2344 return r;
2345 }
2346
2347 joined = strv_join(l, " ");
2348 if (!joined)
2349 return -ENOMEM;
2350
2351 unit_write_settingf(u, flags, name, "SystemCallLog=%s%s", allow_list ? "" : "~", joined);
2352 }
2353
2354 return 1;
2355
cffaed83
YW
2356 } else if (streq(name, "SystemCallArchitectures")) {
2357 _cleanup_strv_free_ char **l = NULL;
2358
2359 r = sd_bus_message_read_strv(message, &l);
2360 if (r < 0)
2361 return r;
2362
2e59b241 2363 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
cffaed83
YW
2364 _cleanup_free_ char *joined = NULL;
2365
7b943bb7 2366 if (strv_isempty(l))
cffaed83
YW
2367 c->syscall_archs = set_free(c->syscall_archs);
2368 else {
2369 char **s;
2370
cffaed83
YW
2371 STRV_FOREACH(s, l) {
2372 uint32_t a;
2373
2374 r = seccomp_arch_from_string(*s, &a);
2375 if (r < 0)
2376 return r;
2377
de7fef4b 2378 r = set_ensure_put(&c->syscall_archs, NULL, UINT32_TO_PTR(a + 1));
cffaed83
YW
2379 if (r < 0)
2380 return r;
2381 }
2382
2383 }
2384
2385 joined = strv_join(l, " ");
2386 if (!joined)
2387 return -ENOMEM;
2388
2e59b241 2389 unit_write_settingf(u, flags, name, "%s=%s", name, joined);
cffaed83
YW
2390 }
2391
2392 return 1;
2393
cffaed83 2394 } else if (streq(name, "RestrictAddressFamilies")) {
6b000af4 2395 int allow_list;
3f856a28 2396 _cleanup_strv_free_ char **l = NULL;
cffaed83
YW
2397
2398 r = sd_bus_message_enter_container(message, 'r', "bas");
2399 if (r < 0)
2400 return r;
2401
6b000af4 2402 r = sd_bus_message_read(message, "b", &allow_list);
cffaed83
YW
2403 if (r < 0)
2404 return r;
2405
2406 r = sd_bus_message_read_strv(message, &l);
2407 if (r < 0)
2408 return r;
2409
2410 r = sd_bus_message_exit_container(message);
2411 if (r < 0)
2412 return r;
2413
2e59b241 2414 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
cffaed83 2415 _cleanup_free_ char *joined = NULL;
9ee896d5 2416 char **s;
cffaed83 2417
7b943bb7 2418 if (strv_isempty(l)) {
6b000af4 2419 c->address_families_allow_list = false;
cffaed83 2420 c->address_families = set_free(c->address_families);
cffaed83 2421
9ee896d5
YW
2422 unit_write_settingf(u, flags, name, "RestrictAddressFamilies=");
2423 return 1;
2424 }
cffaed83 2425
9ee896d5
YW
2426 if (!c->address_families) {
2427 c->address_families = set_new(NULL);
2428 if (!c->address_families)
2429 return log_oom();
cffaed83 2430
6b000af4 2431 c->address_families_allow_list = allow_list;
9ee896d5
YW
2432 }
2433
2434 STRV_FOREACH(s, l) {
2435 int af;
cffaed83 2436
9ee896d5 2437 af = af_from_name(*s);
acf4d158
YW
2438 if (af < 0)
2439 return af;
cffaed83 2440
6b000af4 2441 if (allow_list == c->address_families_allow_list) {
cffaed83
YW
2442 r = set_put(c->address_families, INT_TO_PTR(af));
2443 if (r < 0)
2444 return r;
9ee896d5
YW
2445 } else
2446 (void) set_remove(c->address_families, INT_TO_PTR(af));
cffaed83
YW
2447 }
2448
2449 joined = strv_join(l, " ");
2450 if (!joined)
2451 return -ENOMEM;
2452
6b000af4 2453 unit_write_settingf(u, flags, name, "RestrictAddressFamilies=%s%s", allow_list ? "" : "~", joined);
cffaed83
YW
2454 }
2455
2456 return 1;
7e2a3fcc 2457 }
cffaed83 2458#endif
b070c7c0 2459 if (STR_IN_SET(name, "CPUAffinity", "NUMAMask")) {
cffaed83 2460 const void *a;
bd0abfae 2461 size_t n;
b070c7c0 2462 bool affinity = streq(name, "CPUAffinity");
c367f996 2463 _cleanup_(cpu_set_reset) CPUSet set = {};
cffaed83
YW
2464
2465 r = sd_bus_message_read_array(message, 'y', &a, &n);
2466 if (r < 0)
2467 return r;
2468
c367f996
MS
2469 r = cpu_set_from_dbus(a, n, &set);
2470 if (r < 0)
2471 return r;
2472
2e59b241 2473 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
cffaed83 2474 if (n == 0) {
b070c7c0 2475 cpu_set_reset(affinity ? &c->cpu_set : &c->numa_policy.nodes);
2e59b241 2476 unit_write_settingf(u, flags, name, "%s=", name);
cffaed83
YW
2477 } else {
2478 _cleanup_free_ char *str = NULL;
cffaed83 2479
0985c7c4 2480 str = cpu_set_to_string(&set);
a832893f
ZJS
2481 if (!str)
2482 return -ENOMEM;
cffaed83 2483
0985c7c4
ZJS
2484 /* We forego any optimizations here, and always create the structure using
2485 * cpu_set_add_all(), because we don't want to care if the existing size we
2486 * got over dbus is appropriate. */
b070c7c0 2487 r = cpu_set_add_all(affinity ? &c->cpu_set : &c->numa_policy.nodes, &set);
0985c7c4
ZJS
2488 if (r < 0)
2489 return r;
501941aa 2490
2e59b241 2491 unit_write_settingf(u, flags, name, "%s=%s", name, str);
cffaed83
YW
2492 }
2493 }
2494
c7040b5d
LP
2495 return 1;
2496
e2b2fb7f
MS
2497 } else if (streq(name, "CPUAffinityFromNUMA")) {
2498 int q;
2499
2500 r = sd_bus_message_read_basic(message, 'b', &q);
2501 if (r < 0)
2502 return r;
2503
2504 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2505 c->cpu_affinity_from_numa = q;
2506 unit_write_settingf(u, flags, name, "%s=%s", "CPUAffinity", "numa");
2507 }
2508
2509 return 1;
2510
b070c7c0
MS
2511 } else if (streq(name, "NUMAPolicy")) {
2512 int32_t type;
2513
2514 r = sd_bus_message_read(message, "i", &type);
2515 if (r < 0)
2516 return r;
2517
2518 if (!mpol_is_valid(type))
2519 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid NUMAPolicy value: %i", type);
2520
2521 if (!UNIT_WRITE_FLAGS_NOOP(flags))
2522 c->numa_policy.type = type;
2523
2524 return 1;
e2b2fb7f 2525
c250bf67
YW
2526 } else if (streq(name, "Nice")) {
2527 int32_t q;
2528
2529 r = sd_bus_message_read(message, "i", &q);
2530 if (r < 0)
2531 return r;
2532
2533 if (!nice_is_valid(q))
2534 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid Nice value: %i", q);
2535
2536 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2537 c->nice = q;
2538 c->nice_set = true;
2539
2540 unit_write_settingf(u, flags, name, "Nice=%i", q);
2541 }
2542
2543 return 1;
2544
2545 } else if (streq(name, "CPUSchedulingPolicy")) {
2546 int32_t q;
2547
2548 r = sd_bus_message_read(message, "i", &q);
2549 if (r < 0)
2550 return r;
2551
2552 if (!sched_policy_is_valid(q))
2553 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid CPU scheduling policy: %i", q);
2554
2555 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2556 _cleanup_free_ char *s = NULL;
2557
2558 r = sched_policy_to_string_alloc(q, &s);
2559 if (r < 0)
2560 return r;
2561
2562 c->cpu_sched_policy = q;
2563 c->cpu_sched_priority = CLAMP(c->cpu_sched_priority, sched_get_priority_min(q), sched_get_priority_max(q));
2564 c->cpu_sched_set = true;
2565
2566 unit_write_settingf(u, flags, name, "CPUSchedulingPolicy=%s", s);
2567 }
2568
2569 return 1;
2570
2571 } else if (streq(name, "CPUSchedulingPriority")) {
2572 int32_t p, min, max;
2573
2574 r = sd_bus_message_read(message, "i", &p);
2575 if (r < 0)
2576 return r;
2577
2578 min = sched_get_priority_min(c->cpu_sched_policy);
2579 max = sched_get_priority_max(c->cpu_sched_policy);
2580 if (p < min || p > max)
2581 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid CPU scheduling priority: %i", p);
2582
2583 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2584 c->cpu_sched_priority = p;
2585 c->cpu_sched_set = true;
2586
2587 unit_write_settingf(u, flags, name, "CPUSchedulingPriority=%i", p);
2588 }
2589
2590 return 1;
2591
7f452159
LP
2592 } else if (streq(name, "IOSchedulingClass")) {
2593 int32_t q;
2594
2595 r = sd_bus_message_read(message, "i", &q);
2596 if (r < 0)
2597 return r;
2598
2599 if (!ioprio_class_is_valid(q))
2600 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid IO scheduling class: %i", q);
2601
2e59b241 2602 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
7f452159
LP
2603 _cleanup_free_ char *s = NULL;
2604
2605 r = ioprio_class_to_string_alloc(q, &s);
2606 if (r < 0)
2607 return r;
2608
2609 c->ioprio = IOPRIO_PRIO_VALUE(q, IOPRIO_PRIO_DATA(c->ioprio));
2610 c->ioprio_set = true;
2611
2e59b241 2612 unit_write_settingf(u, flags, name, "IOSchedulingClass=%s", s);
7f452159
LP
2613 }
2614
2615 return 1;
2616
2617 } else if (streq(name, "IOSchedulingPriority")) {
2618 int32_t p;
2619
2620 r = sd_bus_message_read(message, "i", &p);
2621 if (r < 0)
2622 return r;
2623
2624 if (!ioprio_priority_is_valid(p))
2625 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid IO scheduling priority: %i", p);
2626
2e59b241 2627 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
7f452159
LP
2628 c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_PRIO_CLASS(c->ioprio), p);
2629 c->ioprio_set = true;
2630
2e59b241 2631 unit_write_settingf(u, flags, name, "IOSchedulingPriority=%i", p);
7f452159
LP
2632 }
2633
2634 return 1;
2635
5f5d8eab
LP
2636 } else if (streq(name, "WorkingDirectory")) {
2637 const char *s;
2638 bool missing_ok;
2639
2640 r = sd_bus_message_read(message, "s", &s);
2641 if (r < 0)
2642 return r;
2643
2644 if (s[0] == '-') {
2645 missing_ok = true;
2646 s++;
2647 } else
2648 missing_ok = false;
2649
d6ff82d3 2650 if (!isempty(s) && !streq(s, "~") && !path_is_absolute(s))
5f5d8eab
LP
2651 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "WorkingDirectory= expects an absolute path or '~'");
2652
2e59b241 2653 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
5f5d8eab
LP
2654 if (streq(s, "~")) {
2655 c->working_directory = mfree(c->working_directory);
2656 c->working_directory_home = true;
2657 } else {
d6ff82d3 2658 r = free_and_strdup(&c->working_directory, empty_to_null(s));
5f5d8eab
LP
2659 if (r < 0)
2660 return r;
2661
2662 c->working_directory_home = false;
2663 }
2664
2665 c->working_directory_missing_ok = missing_ok;
2e59b241 2666 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "WorkingDirectory=%s%s", missing_ok ? "-" : "", s);
5f5d8eab
LP
2667 }
2668
2669 return 1;
2670
52c239d7
LB
2671 } else if (STR_IN_SET(name,
2672 "StandardInputFileDescriptorName", "StandardOutputFileDescriptorName", "StandardErrorFileDescriptorName")) {
2673 const char *s;
2674
2675 r = sd_bus_message_read(message, "s", &s);
2676 if (r < 0)
2677 return r;
2678
d6ff82d3 2679 if (!isempty(s) && !fdname_is_valid(s))
52c239d7
LB
2680 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid file descriptor name");
2681
2e59b241 2682 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
5073ff6b 2683
52c239d7 2684 if (streq(name, "StandardInputFileDescriptorName")) {
d6ff82d3 2685 r = free_and_strdup(c->stdio_fdname + STDIN_FILENO, empty_to_null(s));
52c239d7
LB
2686 if (r < 0)
2687 return r;
5073ff6b 2688
0664775c 2689 c->std_input = EXEC_INPUT_NAMED_FD;
2e59b241 2690 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "StandardInput=fd:%s", exec_context_fdname(c, STDIN_FILENO));
5073ff6b 2691
52c239d7 2692 } else if (streq(name, "StandardOutputFileDescriptorName")) {
d6ff82d3 2693 r = free_and_strdup(c->stdio_fdname + STDOUT_FILENO, empty_to_null(s));
52c239d7
LB
2694 if (r < 0)
2695 return r;
5073ff6b 2696
52c239d7 2697 c->std_output = EXEC_OUTPUT_NAMED_FD;
2e59b241 2698 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "StandardOutput=fd:%s", exec_context_fdname(c, STDOUT_FILENO));
0664775c
LP
2699
2700 } else {
2701 assert(streq(name, "StandardErrorFileDescriptorName"));
5073ff6b 2702
d6ff82d3 2703 r = free_and_strdup(&c->stdio_fdname[STDERR_FILENO], empty_to_null(s));
52c239d7
LB
2704 if (r < 0)
2705 return r;
5073ff6b 2706
52c239d7 2707 c->std_error = EXEC_OUTPUT_NAMED_FD;
2e59b241 2708 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "StandardError=fd:%s", exec_context_fdname(c, STDERR_FILENO));
52c239d7
LB
2709 }
2710 }
2711
2712 return 1;
2713
566b7d23
ZD
2714 } else if (STR_IN_SET(name,
2715 "StandardInputFile",
922ce049
LP
2716 "StandardOutputFile", "StandardOutputFileToAppend",
2717 "StandardErrorFile", "StandardErrorFileToAppend")) {
2038c3f5
LP
2718 const char *s;
2719
2720 r = sd_bus_message_read(message, "s", &s);
2721 if (r < 0)
2722 return r;
2723
d6ff82d3
YW
2724 if (!isempty(s)) {
2725 if (!path_is_absolute(s))
2726 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path %s is not absolute", s);
2727 if (!path_is_normalized(s))
2728 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path %s is not normalized", s);
2729 }
2038c3f5 2730
2e59b241 2731 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2038c3f5
LP
2732
2733 if (streq(name, "StandardInputFile")) {
d6ff82d3 2734 r = free_and_strdup(&c->stdio_file[STDIN_FILENO], empty_to_null(s));
52c239d7
LB
2735 if (r < 0)
2736 return r;
2038c3f5
LP
2737
2738 c->std_input = EXEC_INPUT_FILE;
2e59b241 2739 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "StandardInput=file:%s", s);
2038c3f5 2740
566b7d23 2741 } else if (STR_IN_SET(name, "StandardOutputFile", "StandardOutputFileToAppend")) {
d6ff82d3 2742 r = free_and_strdup(&c->stdio_file[STDOUT_FILENO], empty_to_null(s));
2038c3f5
LP
2743 if (r < 0)
2744 return r;
2745
566b7d23
ZD
2746 if (streq(name, "StandardOutputFile")) {
2747 c->std_output = EXEC_OUTPUT_FILE;
2748 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "StandardOutput=file:%s", s);
2749 } else {
2750 assert(streq(name, "StandardOutputFileToAppend"));
2751 c->std_output = EXEC_OUTPUT_FILE_APPEND;
2752 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "StandardOutput=append:%s", s);
2753 }
2038c3f5 2754 } else {
566b7d23 2755 assert(STR_IN_SET(name, "StandardErrorFile", "StandardErrorFileToAppend"));
2038c3f5 2756
d6ff82d3 2757 r = free_and_strdup(&c->stdio_file[STDERR_FILENO], empty_to_null(s));
2038c3f5
LP
2758 if (r < 0)
2759 return r;
2760
566b7d23
ZD
2761 if (streq(name, "StandardErrorFile")) {
2762 c->std_error = EXEC_OUTPUT_FILE;
1704fba9 2763 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "StandardError=file:%s", s);
566b7d23 2764 } else {
dbe6c4b6
LP
2765 assert(streq(name, "StandardErrorFileToAppend"));
2766 c->std_error = EXEC_OUTPUT_FILE_APPEND;
1704fba9 2767 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "StandardError=append:%s", s);
566b7d23 2768 }
2038c3f5
LP
2769 }
2770 }
2771
2772 return 1;
2773
08f3be7a
LP
2774 } else if (streq(name, "StandardInputData")) {
2775 const void *p;
2776 size_t sz;
2777
2778 r = sd_bus_message_read_array(message, 'y', &p, &sz);
2779 if (r < 0)
2780 return r;
2781
2e59b241 2782 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
08f3be7a
LP
2783 _cleanup_free_ char *encoded = NULL;
2784
2785 if (sz == 0) {
2786 c->stdin_data = mfree(c->stdin_data);
2787 c->stdin_data_size = 0;
2788
2e59b241 2789 unit_write_settingf(u, flags, name, "StandardInputData=");
08f3be7a
LP
2790 } else {
2791 void *q;
2792 ssize_t n;
2793
2794 if (c->stdin_data_size + sz < c->stdin_data_size || /* check for overflow */
2795 c->stdin_data_size + sz > EXEC_STDIN_DATA_MAX)
2796 return -E2BIG;
2797
2798 n = base64mem(p, sz, &encoded);
2799 if (n < 0)
2800 return (int) n;
2801
2802 q = realloc(c->stdin_data, c->stdin_data_size + sz);
2803 if (!q)
2804 return -ENOMEM;
2805
2806 memcpy((uint8_t*) q + c->stdin_data_size, p, sz);
2807
2808 c->stdin_data = q;
2809 c->stdin_data_size += sz;
2810
2e59b241 2811 unit_write_settingf(u, flags, name, "StandardInputData=%s", encoded);
52c239d7
LB
2812 }
2813 }
2814
2815 return 1;
2816
c7040b5d
LP
2817 } else if (streq(name, "Environment")) {
2818
6171b822 2819 _cleanup_strv_free_ char **l = NULL;
c7040b5d
LP
2820
2821 r = sd_bus_message_read_strv(message, &l);
2822 if (r < 0)
2823 return r;
2824
6171b822 2825 if (!strv_env_is_valid(l))
1c68232e
LP
2826 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid environment block.");
2827
2e59b241 2828 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
7b943bb7 2829 if (strv_isempty(l)) {
e9876fc9 2830 c->environment = strv_free(c->environment);
2e59b241 2831 unit_write_setting(u, flags, name, "Environment=");
e9876fc9 2832 } else {
f900f582
ZJS
2833 _cleanup_free_ char *joined = NULL;
2834 char **e;
2835
2e59b241
LP
2836 joined = unit_concat_strv(l, UNIT_ESCAPE_SPECIFIERS|UNIT_ESCAPE_C);
2837 if (!joined)
2838 return -ENOMEM;
2839
6171b822 2840 e = strv_env_merge(2, c->environment, l);
e9876fc9
EV
2841 if (!e)
2842 return -ENOMEM;
c7040b5d 2843
130d3d22 2844 strv_free_and_replace(c->environment, e);
2e59b241 2845 unit_write_settingf(u, flags, name, "Environment=%s", joined);
e9876fc9 2846 }
c7040b5d
LP
2847 }
2848
d584f638
LP
2849 return 1;
2850
00819cc1
LP
2851 } else if (streq(name, "UnsetEnvironment")) {
2852
6171b822 2853 _cleanup_strv_free_ char **l = NULL;
00819cc1
LP
2854
2855 r = sd_bus_message_read_strv(message, &l);
2856 if (r < 0)
2857 return r;
2858
6171b822 2859 if (!strv_env_name_or_assignment_is_valid(l))
00819cc1
LP
2860 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid UnsetEnvironment= list.");
2861
2e59b241 2862 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
7b943bb7 2863 if (strv_isempty(l)) {
00819cc1 2864 c->unset_environment = strv_free(c->unset_environment);
2e59b241 2865 unit_write_setting(u, flags, name, "UnsetEnvironment=");
00819cc1
LP
2866 } else {
2867 _cleanup_free_ char *joined = NULL;
2868 char **e;
2869
2e59b241
LP
2870 joined = unit_concat_strv(l, UNIT_ESCAPE_SPECIFIERS|UNIT_ESCAPE_C);
2871 if (!joined)
2872 return -ENOMEM;
2873
6171b822 2874 e = strv_env_merge(2, c->unset_environment, l);
00819cc1
LP
2875 if (!e)
2876 return -ENOMEM;
2877
130d3d22 2878 strv_free_and_replace(c->unset_environment, e);
2e59b241 2879 unit_write_settingf(u, flags, name, "UnsetEnvironment=%s", joined);
00819cc1
LP
2880 }
2881 }
2882
2883 return 1;
2884
6b862936
EV
2885 } else if (streq(name, "OOMScoreAdjust")) {
2886 int oa;
2887
2888 r = sd_bus_message_read(message, "i", &oa);
2889 if (r < 0)
2890 return r;
2891
2892 if (!oom_score_adjust_is_valid(oa))
2893 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "OOM score adjust value out of range");
2894
2e59b241 2895 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
6b862936
EV
2896 c->oom_score_adjust = oa;
2897 c->oom_score_adjust_set = true;
2e59b241 2898 unit_write_settingf(u, flags, name, "OOMScoreAdjust=%i", oa);
6b862936
EV
2899 }
2900
2901 return 1;
2902
ad21e542
ZJS
2903 } else if (streq(name, "CoredumpFilter")) {
2904 uint64_t f;
2905
2906 r = sd_bus_message_read(message, "t", &f);
2907 if (r < 0)
2908 return r;
2909
2910 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2911 c->coredump_filter = f;
2912 c->coredump_filter_set = true;
2913 unit_write_settingf(u, flags, name, "CoredumpFilter=0x%"PRIx64, f);
2914 }
2915
2916 return 1;
2917
ceb728cf
NC
2918 } else if (streq(name, "EnvironmentFiles")) {
2919
2920 _cleanup_free_ char *joined = NULL;
2921 _cleanup_fclose_ FILE *f = NULL;
9b531f04 2922 _cleanup_strv_free_ char **l = NULL;
ceb728cf 2923 size_t size = 0;
2229f656 2924 char **i;
ceb728cf
NC
2925
2926 r = sd_bus_message_enter_container(message, 'a', "(sb)");
2927 if (r < 0)
2928 return r;
2929
2fe21124 2930 f = open_memstream_unlocked(&joined, &size);
ceb728cf
NC
2931 if (!f)
2932 return -ENOMEM;
2933
2e59b241
LP
2934 fputs("EnvironmentFile=\n", f);
2935
2936 STRV_FOREACH(i, c->environment_files) {
2937 _cleanup_free_ char *q = NULL;
2938
2939 q = specifier_escape(*i);
2940 if (!q)
2941 return -ENOMEM;
2942
2943 fprintf(f, "EnvironmentFile=%s\n", q);
2944 }
ceb728cf
NC
2945
2946 while ((r = sd_bus_message_enter_container(message, 'r', "sb")) > 0) {
2947 const char *path;
2948 int b;
2949
ceb728cf
NC
2950 r = sd_bus_message_read(message, "sb", &path, &b);
2951 if (r < 0)
2952 return r;
2953
2954 r = sd_bus_message_exit_container(message);
2955 if (r < 0)
2956 return r;
2957
d2d6c096
LP
2958 if (!path_is_absolute(path))
2959 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path %s is not absolute.", path);
ceb728cf 2960
2e59b241 2961 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
6abdec98 2962 _cleanup_free_ char *q = NULL, *buf = NULL;
ceb728cf 2963
605405c6 2964 buf = strjoin(b ? "-" : "", path);
2229f656 2965 if (!buf)
ceb728cf
NC
2966 return -ENOMEM;
2967
2e59b241 2968 q = specifier_escape(buf);
6abdec98 2969 if (!q)
2e59b241 2970 return -ENOMEM;
2e59b241
LP
2971
2972 fprintf(f, "EnvironmentFile=%s\n", q);
ceb728cf 2973
6abdec98 2974 r = strv_consume(&l, TAKE_PTR(buf));
ceb728cf
NC
2975 if (r < 0)
2976 return r;
2977 }
2978 }
2979 if (r < 0)
2980 return r;
2981
b0830e21
LP
2982 r = sd_bus_message_exit_container(message);
2983 if (r < 0)
2984 return r;
2985
2229f656
LP
2986 r = fflush_and_check(f);
2987 if (r < 0)
2988 return r;
ceb728cf 2989
2e59b241 2990 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2229f656
LP
2991 if (strv_isempty(l)) {
2992 c->environment_files = strv_free(c->environment_files);
2e59b241 2993 unit_write_setting(u, flags, name, "EnvironmentFile=");
2229f656
LP
2994 } else {
2995 r = strv_extend_strv(&c->environment_files, l, true);
2996 if (r < 0)
2997 return r;
2998
2e59b241 2999 unit_write_setting(u, flags, name, joined);
2229f656
LP
3000 }
3001 }
ceb728cf 3002
ceb728cf
NC
3003 return 1;
3004
b4c14404
FB
3005 } else if (streq(name, "PassEnvironment")) {
3006
6171b822 3007 _cleanup_strv_free_ char **l = NULL;
b4c14404
FB
3008
3009 r = sd_bus_message_read_strv(message, &l);
3010 if (r < 0)
3011 return r;
3012
6171b822 3013 if (!strv_env_name_is_valid(l))
00819cc1 3014 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid PassEnvironment= block.");
b4c14404 3015
2e59b241 3016 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
b4c14404
FB
3017 if (strv_isempty(l)) {
3018 c->pass_environment = strv_free(c->pass_environment);
2e59b241 3019 unit_write_setting(u, flags, name, "PassEnvironment=");
b4c14404
FB
3020 } else {
3021 _cleanup_free_ char *joined = NULL;
3022
9d4f242a
YW
3023 r = strv_extend_strv(&c->pass_environment, l, true);
3024 if (r < 0)
3025 return r;
3026
41de9cc2 3027 /* We write just the new settings out to file, with unresolved specifiers. */
2e59b241 3028 joined = unit_concat_strv(l, UNIT_ESCAPE_SPECIFIERS);
b4c14404
FB
3029 if (!joined)
3030 return -ENOMEM;
3031
2e59b241 3032 unit_write_settingf(u, flags, name, "PassEnvironment=%s", joined);
b4c14404
FB
3033 }
3034 }
3035
3036 return 1;
3037
2a624c36
AP
3038 } else if (STR_IN_SET(name, "ReadWriteDirectories", "ReadOnlyDirectories", "InaccessibleDirectories",
3039 "ReadWritePaths", "ReadOnlyPaths", "InaccessiblePaths")) {
08596068
EV
3040 _cleanup_strv_free_ char **l = NULL;
3041 char ***dirs;
3042 char **p;
3043
3044 r = sd_bus_message_read_strv(message, &l);
3045 if (r < 0)
3046 return r;
3047
3048 STRV_FOREACH(p, l) {
e7bcff4e 3049 char *i = *p;
20b7a007
LP
3050 size_t offset;
3051
20b7a007
LP
3052 offset = i[0] == '-';
3053 offset += i[offset] == '+';
3054 if (!path_is_absolute(i + offset))
08596068 3055 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid %s", name);
e7bcff4e 3056
858d36c1 3057 path_simplify(i + offset, false);
08596068
EV
3058 }
3059
2e59b241 3060 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
c4b41707
AP
3061 if (STR_IN_SET(name, "ReadWriteDirectories", "ReadWritePaths"))
3062 dirs = &c->read_write_paths;
3063 else if (STR_IN_SET(name, "ReadOnlyDirectories", "ReadOnlyPaths"))
3064 dirs = &c->read_only_paths;
3065 else /* "InaccessiblePaths" */
3066 dirs = &c->inaccessible_paths;
08596068 3067
7b943bb7 3068 if (strv_isempty(l)) {
08596068 3069 *dirs = strv_free(*dirs);
2e59b241 3070 unit_write_settingf(u, flags, name, "%s=", name);
08596068 3071 } else {
2e59b241 3072 _cleanup_free_ char *joined = NULL;
08596068 3073
2e59b241 3074 joined = unit_concat_strv(l, UNIT_ESCAPE_SPECIFIERS);
08596068
EV
3075 if (!joined)
3076 return -ENOMEM;
3077
2e59b241
LP
3078 r = strv_extend_strv(dirs, l, true);
3079 if (r < 0)
3080 return -ENOMEM;
08596068 3081
2e59b241
LP
3082 unit_write_settingf(u, flags, name, "%s=%s", name, joined);
3083 }
08596068
EV
3084 }
3085
3086 return 1;
3087
3536f49e 3088 } else if (STR_IN_SET(name, "RuntimeDirectory", "StateDirectory", "CacheDirectory", "LogsDirectory", "ConfigurationDirectory")) {
fa21b5e3
EV
3089 _cleanup_strv_free_ char **l = NULL;
3090 char **p;
3091
3092 r = sd_bus_message_read_strv(message, &l);
3093 if (r < 0)
3094 return r;
3095
3096 STRV_FOREACH(p, l) {
8994a117
YW
3097 if (!path_is_normalized(*p))
3098 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "%s= path is not normalized: %s", name, *p);
3099
3100 if (path_is_absolute(*p))
3101 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "%s= path is absolute: %s", name, *p);
3102
3103 if (path_startswith(*p, "private"))
3104 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "%s= path can't be 'private': %s", name, *p);
fa21b5e3
EV
3105 }
3106
2e59b241 3107 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
3536f49e 3108 ExecDirectoryType i;
b1ea85dc 3109 ExecDirectory *d;
3536f49e 3110
b1ea85dc
LP
3111 assert_se((i = exec_directory_type_from_string(name)) >= 0);
3112 d = c->directories + i;
fa21b5e3
EV
3113
3114 if (strv_isempty(l)) {
b1ea85dc 3115 d->paths = strv_free(d->paths);
2e59b241 3116 unit_write_settingf(u, flags, name, "%s=", name);
fa21b5e3 3117 } else {
2e59b241
LP
3118 _cleanup_free_ char *joined = NULL;
3119
b1ea85dc 3120 r = strv_extend_strv(&d->paths, l, true);
fa21b5e3 3121 if (r < 0)
c425c7c2 3122 return r;
fa21b5e3 3123
2e59b241 3124 joined = unit_concat_strv(l, UNIT_ESCAPE_SPECIFIERS);
fa21b5e3
EV
3125 if (!joined)
3126 return -ENOMEM;
3127
2e59b241 3128 unit_write_settingf(u, flags, name, "%s=%s", name, joined);
fa21b5e3
EV
3129 }
3130 }
3131
3132 return 1;
3133
cffaed83
YW
3134 } else if (STR_IN_SET(name, "AppArmorProfile", "SmackProcessLabel")) {
3135 int ignore;
3136 const char *s;
3137
280921f2 3138 r = sd_bus_message_read(message, "(bs)", &ignore, &s);
cffaed83
YW
3139 if (r < 0)
3140 return r;
3141
2e59b241 3142 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
cffaed83
YW
3143 char **p;
3144 bool *b;
3145
3146 if (streq(name, "AppArmorProfile")) {
3147 p = &c->apparmor_profile;
3148 b = &c->apparmor_profile_ignore;
3149 } else { /* "SmackProcessLabel" */
3150 p = &c->smack_process_label;
3151 b = &c->smack_process_label_ignore;
3152 }
3153
3154 if (isempty(s)) {
3155 *p = mfree(*p);
3156 *b = false;
3157 } else {
3158 if (free_and_strdup(p, s) < 0)
3159 return -ENOMEM;
3160 *b = ignore;
3161 }
3162
2e59b241 3163 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "%s=%s%s", name, ignore ? "-" : "", strempty(s));
cffaed83
YW
3164 }
3165
3166 return 1;
3167
d2d6c096 3168 } else if (STR_IN_SET(name, "BindPaths", "BindReadOnlyPaths")) {
ec04aef4 3169 char *source, *destination;
4ff4c98a
YW
3170 int ignore_enoent;
3171 uint64_t mount_flags;
3172 bool empty = true;
d2d6c096
LP
3173
3174 r = sd_bus_message_enter_container(message, 'a', "(ssbt)");
3175 if (r < 0)
3176 return r;
3177
4ff4c98a 3178 while ((r = sd_bus_message_read(message, "(ssbt)", &source, &destination, &ignore_enoent, &mount_flags)) > 0) {
d2d6c096
LP
3179
3180 if (!path_is_absolute(source))
3181 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Source path %s is not absolute.", source);
3182 if (!path_is_absolute(destination))
91d910e3 3183 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Destination path %s is not absolute.", destination);
d2d6c096
LP
3184 if (!IN_SET(mount_flags, 0, MS_REC))
3185 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown mount flags.");
3186
2e59b241 3187 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
d2d6c096
LP
3188 r = bind_mount_add(&c->bind_mounts, &c->n_bind_mounts,
3189 &(BindMount) {
ec04aef4
TM
3190 .source = source,
3191 .destination = destination,
d2d6c096
LP
3192 .read_only = !!strstr(name, "ReadOnly"),
3193 .recursive = !!(mount_flags & MS_REC),
3194 .ignore_enoent = ignore_enoent,
3195 });
3196 if (r < 0)
3197 return r;
3198
2e59b241
LP
3199 unit_write_settingf(
3200 u, flags|UNIT_ESCAPE_SPECIFIERS, name,
d2d6c096
LP
3201 "%s=%s%s:%s:%s",
3202 name,
3203 ignore_enoent ? "-" : "",
3204 source,
3205 destination,
3206 (mount_flags & MS_REC) ? "rbind" : "norbind");
3207 }
3208
3209 empty = false;
3210 }
3211 if (r < 0)
3212 return r;
3213
3214 r = sd_bus_message_exit_container(message);
3215 if (r < 0)
3216 return r;
3217
3218 if (empty) {
3219 bind_mount_free_many(c->bind_mounts, c->n_bind_mounts);
3220 c->bind_mounts = NULL;
3221 c->n_bind_mounts = 0;
2e59b241
LP
3222
3223 unit_write_settingf(u, flags, name, "%s=", name);
d2d6c096
LP
3224 }
3225
784ad252
YW
3226 return 1;
3227
3228 } else if (streq(name, "TemporaryFileSystem")) {
3229 const char *path, *options;
3230 bool empty = true;
3231
3232 r = sd_bus_message_enter_container(message, 'a', "(ss)");
3233 if (r < 0)
3234 return r;
3235
3236 while ((r = sd_bus_message_read(message, "(ss)", &path, &options)) > 0) {
3237
3238 if (!path_is_absolute(path))
3239 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Mount point %s is not absolute.", path);
3240
3241 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
3242 r = temporary_filesystem_add(&c->temporary_filesystems, &c->n_temporary_filesystems, path, options);
3243 if (r < 0)
3244 return r;
3245
3246 unit_write_settingf(
3247 u, flags|UNIT_ESCAPE_SPECIFIERS, name,
3248 "%s=%s:%s",
3249 name,
3250 path,
3251 options);
3252 }
3253
3254 empty = false;
3255 }
3256 if (r < 0)
3257 return r;
3258
3259 r = sd_bus_message_exit_container(message);
3260 if (r < 0)
3261 return r;
3262
3263 if (empty) {
3264 temporary_filesystem_free_many(c->temporary_filesystems, c->n_temporary_filesystems);
3265 c->temporary_filesystems = NULL;
3266 c->n_temporary_filesystems = 0;
3267
3268 unit_write_settingf(u, flags, name, "%s=", name);
3269 }
3270
83555251 3271 return 1;
d2d6c096 3272
6550c24c
LP
3273 } else if ((suffix = startswith(name, "Limit"))) {
3274 const char *soft = NULL;
3275 int ri;
cab2aca3 3276
6550c24c
LP
3277 ri = rlimit_from_string(suffix);
3278 if (ri < 0) {
3279 soft = endswith(suffix, "Soft");
3280 if (soft) {
3281 const char *n;
cab2aca3 3282
6550c24c
LP
3283 n = strndupa(suffix, soft - suffix);
3284 ri = rlimit_from_string(n);
3285 if (ri >= 0)
3286 name = strjoina("Limit", n);
3287 }
cab2aca3 3288 }
d584f638 3289
6550c24c
LP
3290 if (ri >= 0) {
3291 uint64_t rl;
3292 rlim_t x;
d584f638 3293
6550c24c 3294 r = sd_bus_message_read(message, "t", &rl);
cab2aca3
LP
3295 if (r < 0)
3296 return r;
d584f638 3297
6550c24c
LP
3298 if (rl == (uint64_t) -1)
3299 x = RLIM_INFINITY;
cab2aca3 3300 else {
6550c24c
LP
3301 x = (rlim_t) rl;
3302
3303 if ((uint64_t) x != rl)
3304 return -ERANGE;
d584f638
LP
3305 }
3306
6550c24c
LP
3307 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
3308 _cleanup_free_ char *f = NULL;
3309 struct rlimit nl;
3310
3311 if (c->rlimit[ri]) {
3312 nl = *c->rlimit[ri];
3313
3314 if (soft)
3315 nl.rlim_cur = x;
3316 else
3317 nl.rlim_max = x;
3318 } else
3319 /* When the resource limit is not initialized yet, then assign the value to both fields */
3320 nl = (struct rlimit) {
3321 .rlim_cur = x,
3322 .rlim_max = x,
3323 };
3324
3325 r = rlimit_format(&nl, &f);
3326 if (r < 0)
3327 return r;
3328
3329 if (c->rlimit[ri])
3330 *c->rlimit[ri] = nl;
3331 else {
3332 c->rlimit[ri] = newdup(struct rlimit, &nl, 1);
3333 if (!c->rlimit[ri])
3334 return -ENOMEM;
3335 }
3336
3337 unit_write_settingf(u, flags, name, "%s=%s", name, f);
3338 }
3339
3340 return 1;
d584f638
LP
3341 }
3342
b3d13314
LB
3343 } else if (streq(name, "MountImages")) {
3344 _cleanup_free_ char *format_str = NULL;
3345 MountImage *mount_images = NULL;
3346 size_t n_mount_images = 0;
3347 char *source, *destination;
3348 int permissive;
3349
427353f6 3350 r = sd_bus_message_enter_container(message, 'a', "(ssba(ss))");
b3d13314
LB
3351 if (r < 0)
3352 return r;
3353
427353f6
LB
3354 for (;;) {
3355 _cleanup_(mount_options_free_allp) MountOptions *options = NULL;
3356 _cleanup_free_ char *source_escaped = NULL, *destination_escaped = NULL;
b3d13314
LB
3357 char *tuple;
3358
427353f6
LB
3359 r = sd_bus_message_enter_container(message, 'r', "ssba(ss)");
3360 if (r < 0)
3361 return r;
3362
3363 r = sd_bus_message_read(message, "ssb", &source, &destination, &permissive);
3364 if (r <= 0)
3365 break;
3366
b3d13314
LB
3367 if (!path_is_absolute(source))
3368 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Source path %s is not absolute.", source);
3369 if (!path_is_normalized(source))
3370 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Source path %s is not normalized.", source);
3371 if (!path_is_absolute(destination))
3372 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Destination path %s is not absolute.", destination);
3373 if (!path_is_normalized(destination))
3374 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Destination path %s is not normalized.", destination);
3375
427353f6
LB
3376 /* Need to store them in the unit with the escapes, so that they can be parsed again */
3377 source_escaped = shell_escape(source, ":");
3378 if (!source_escaped)
3379 return -ENOMEM;
3380 destination_escaped = shell_escape(destination, ":");
3381 if (!destination_escaped)
3382 return -ENOMEM;
3383
3384 tuple = strjoin(format_str,
3385 format_str ? " " : "",
3386 permissive ? "-" : "",
3387 source_escaped,
3388 ":",
3389 destination_escaped);
b3d13314
LB
3390 if (!tuple)
3391 return -ENOMEM;
3392 free_and_replace(format_str, tuple);
3393
427353f6
LB
3394 r = read_mount_options(message, error, &options, &format_str, ":");
3395 if (r < 0)
3396 return r;
3397
3398 r = sd_bus_message_exit_container(message);
3399 if (r < 0)
3400 return r;
3401
b3d13314
LB
3402 r = mount_image_add(&mount_images, &n_mount_images,
3403 &(MountImage) {
3404 .source = source,
3405 .destination = destination,
427353f6 3406 .mount_options = options,
b3d13314
LB
3407 .ignore_enoent = permissive,
3408 });
3409 if (r < 0)
3410 return r;
3411 }
3412 if (r < 0)
3413 return r;
3414
3415 r = sd_bus_message_exit_container(message);
3416 if (r < 0)
3417 return r;
3418
3419 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
3420 if (n_mount_images == 0) {
3421 c->mount_images = mount_image_free_many(c->mount_images, &c->n_mount_images);
3422
3423 unit_write_settingf(u, flags, name, "%s=", name);
3424 } else {
3425 for (size_t i = 0; i < n_mount_images; ++i) {
3426 r = mount_image_add(&c->mount_images, &c->n_mount_images, &mount_images[i]);
3427 if (r < 0)
3428 return r;
3429 }
3430
3431 unit_write_settingf(u, flags|UNIT_ESCAPE_C|UNIT_ESCAPE_SPECIFIERS,
3432 name,
3433 "%s=%s",
3434 name,
3435 format_str);
3436 }
3437 }
3438
3439 mount_images = mount_image_free_many(mount_images, &n_mount_images);
3440
3441 return 1;
c7040b5d
LP
3442 }
3443
3444 return 0;
3445}