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