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