]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/dbus-execute.c
core: add ExecStartXYZEx= with dbus support for executable prefixes
[thirdparty/systemd.git] / src / core / dbus-execute.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
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-util.h"
13 #include "cap-list.h"
14 #include "capability-util.h"
15 #include "cpu-set-util.h"
16 #include "dbus-execute.h"
17 #include "dbus-util.h"
18 #include "env-util.h"
19 #include "errno-list.h"
20 #include "escape.h"
21 #include "execute.h"
22 #include "fd-util.h"
23 #include "fileio.h"
24 #include "hexdecoct.h"
25 #include "io-util.h"
26 #include "ioprio.h"
27 #include "journal-util.h"
28 #include "missing.h"
29 #include "mountpoint-util.h"
30 #include "namespace.h"
31 #include "parse-util.h"
32 #include "path-util.h"
33 #include "process-util.h"
34 #include "rlimit-util.h"
35 #if HAVE_SECCOMP
36 #include "seccomp-util.h"
37 #endif
38 #include "securebits-util.h"
39 #include "specifier.h"
40 #include "strv.h"
41 #include "syslog-util.h"
42 #include "unit-printf.h"
43 #include "user-util.h"
44 #include "utf8.h"
45
46 BUS_DEFINE_PROPERTY_GET_ENUM(bus_property_get_exec_output, exec_output, ExecOutput);
47 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_exec_input, exec_input, ExecInput);
48 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_exec_utmp_mode, exec_utmp_mode, ExecUtmpMode);
49 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_exec_preserve_mode, exec_preserve_mode, ExecPreserveMode);
50 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_exec_keyring_mode, exec_keyring_mode, ExecKeyringMode);
51 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_protect_home, protect_home, ProtectHome);
52 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_protect_system, protect_system, ProtectSystem);
53 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_personality, personality, unsigned long);
54 static BUS_DEFINE_PROPERTY_GET(property_get_ioprio, "i", ExecContext, exec_context_get_effective_ioprio);
55 static BUS_DEFINE_PROPERTY_GET2(property_get_ioprio_class, "i", ExecContext, exec_context_get_effective_ioprio, IOPRIO_PRIO_CLASS);
56 static BUS_DEFINE_PROPERTY_GET2(property_get_ioprio_priority, "i", ExecContext, exec_context_get_effective_ioprio, IOPRIO_PRIO_DATA);
57 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_empty_string, "s", NULL);
58 static BUS_DEFINE_PROPERTY_GET_REF(property_get_syslog_level, "i", int, LOG_PRI);
59 static BUS_DEFINE_PROPERTY_GET_REF(property_get_syslog_facility, "i", int, LOG_FAC);
60
61 static int property_get_environment_files(
62 sd_bus *bus,
63 const char *path,
64 const char *interface,
65 const char *property,
66 sd_bus_message *reply,
67 void *userdata,
68 sd_bus_error *error) {
69
70 ExecContext *c = userdata;
71 char **j;
72 int r;
73
74 assert(bus);
75 assert(reply);
76 assert(c);
77
78 r = sd_bus_message_open_container(reply, 'a', "(sb)");
79 if (r < 0)
80 return r;
81
82 STRV_FOREACH(j, c->environment_files) {
83 const char *fn = *j;
84
85 r = sd_bus_message_append(reply, "(sb)", fn[0] == '-' ? fn + 1 : fn, fn[0] == '-');
86 if (r < 0)
87 return r;
88 }
89
90 return sd_bus_message_close_container(reply);
91 }
92
93 static int property_get_oom_score_adjust(
94 sd_bus *bus,
95 const char *path,
96 const char *interface,
97 const char *property,
98 sd_bus_message *reply,
99 void *userdata,
100 sd_bus_error *error) {
101
102 ExecContext *c = userdata;
103 int32_t n;
104
105 assert(bus);
106 assert(reply);
107 assert(c);
108
109 if (c->oom_score_adjust_set)
110 n = c->oom_score_adjust;
111 else {
112 _cleanup_free_ char *t = NULL;
113
114 n = 0;
115 if (read_one_line_file("/proc/self/oom_score_adj", &t) >= 0)
116 safe_atoi32(t, &n);
117 }
118
119 return sd_bus_message_append(reply, "i", n);
120 }
121
122 static int property_get_nice(
123 sd_bus *bus,
124 const char *path,
125 const char *interface,
126 const char *property,
127 sd_bus_message *reply,
128 void *userdata,
129 sd_bus_error *error) {
130
131 ExecContext *c = userdata;
132 int32_t n;
133
134 assert(bus);
135 assert(reply);
136 assert(c);
137
138 if (c->nice_set)
139 n = c->nice;
140 else {
141 errno = 0;
142 n = getpriority(PRIO_PROCESS, 0);
143 if (errno > 0)
144 n = 0;
145 }
146
147 return sd_bus_message_append(reply, "i", n);
148 }
149
150 static int property_get_cpu_sched_policy(
151 sd_bus *bus,
152 const char *path,
153 const char *interface,
154 const char *property,
155 sd_bus_message *reply,
156 void *userdata,
157 sd_bus_error *error) {
158
159 ExecContext *c = userdata;
160 int32_t n;
161
162 assert(bus);
163 assert(reply);
164 assert(c);
165
166 if (c->cpu_sched_set)
167 n = c->cpu_sched_policy;
168 else {
169 n = sched_getscheduler(0);
170 if (n < 0)
171 n = SCHED_OTHER;
172 }
173
174 return sd_bus_message_append(reply, "i", n);
175 }
176
177 static int property_get_cpu_sched_priority(
178 sd_bus *bus,
179 const char *path,
180 const char *interface,
181 const char *property,
182 sd_bus_message *reply,
183 void *userdata,
184 sd_bus_error *error) {
185
186 ExecContext *c = userdata;
187 int32_t n;
188
189 assert(bus);
190 assert(reply);
191 assert(c);
192
193 if (c->cpu_sched_set)
194 n = c->cpu_sched_priority;
195 else {
196 struct sched_param p = {};
197
198 if (sched_getparam(0, &p) >= 0)
199 n = p.sched_priority;
200 else
201 n = 0;
202 }
203
204 return sd_bus_message_append(reply, "i", n);
205 }
206
207 static int property_get_cpu_affinity(
208 sd_bus *bus,
209 const char *path,
210 const char *interface,
211 const char *property,
212 sd_bus_message *reply,
213 void *userdata,
214 sd_bus_error *error) {
215
216 ExecContext *c = userdata;
217
218 assert(bus);
219 assert(reply);
220 assert(c);
221
222 return sd_bus_message_append_array(reply, 'y', c->cpu_set.set, c->cpu_set.allocated);
223 }
224
225 static int property_get_timer_slack_nsec(
226 sd_bus *bus,
227 const char *path,
228 const char *interface,
229 const char *property,
230 sd_bus_message *reply,
231 void *userdata,
232 sd_bus_error *error) {
233
234 ExecContext *c = userdata;
235 uint64_t u;
236
237 assert(bus);
238 assert(reply);
239 assert(c);
240
241 if (c->timer_slack_nsec != NSEC_INFINITY)
242 u = (uint64_t) c->timer_slack_nsec;
243 else
244 u = (uint64_t) prctl(PR_GET_TIMERSLACK);
245
246 return sd_bus_message_append(reply, "t", u);
247 }
248
249 static int property_get_syscall_filter(
250 sd_bus *bus,
251 const char *path,
252 const char *interface,
253 const char *property,
254 sd_bus_message *reply,
255 void *userdata,
256 sd_bus_error *error) {
257
258 ExecContext *c = userdata;
259 _cleanup_strv_free_ char **l = NULL;
260 int r;
261
262 #if HAVE_SECCOMP
263 Iterator i;
264 void *id, *val;
265 #endif
266
267 assert(bus);
268 assert(reply);
269 assert(c);
270
271 r = sd_bus_message_open_container(reply, 'r', "bas");
272 if (r < 0)
273 return r;
274
275 r = sd_bus_message_append(reply, "b", c->syscall_whitelist);
276 if (r < 0)
277 return r;
278
279 #if HAVE_SECCOMP
280 HASHMAP_FOREACH_KEY(val, id, c->syscall_filter, i) {
281 _cleanup_free_ char *name = NULL;
282 const char *e = NULL;
283 char *s;
284 int num = PTR_TO_INT(val);
285
286 name = seccomp_syscall_resolve_num_arch(SCMP_ARCH_NATIVE, PTR_TO_INT(id) - 1);
287 if (!name)
288 continue;
289
290 if (num >= 0) {
291 e = errno_to_name(num);
292 if (e) {
293 s = strjoin(name, ":", e);
294 if (!s)
295 return -ENOMEM;
296 } else {
297 r = asprintf(&s, "%s:%d", name, num);
298 if (r < 0)
299 return -ENOMEM;
300 }
301 } else
302 s = TAKE_PTR(name);
303
304 r = strv_consume(&l, s);
305 if (r < 0)
306 return r;
307 }
308 #endif
309
310 strv_sort(l);
311
312 r = sd_bus_message_append_strv(reply, l);
313 if (r < 0)
314 return r;
315
316 return sd_bus_message_close_container(reply);
317 }
318
319 static int property_get_syscall_archs(
320 sd_bus *bus,
321 const char *path,
322 const char *interface,
323 const char *property,
324 sd_bus_message *reply,
325 void *userdata,
326 sd_bus_error *error) {
327
328 ExecContext *c = userdata;
329 _cleanup_strv_free_ char **l = NULL;
330 int r;
331
332 #if HAVE_SECCOMP
333 Iterator i;
334 void *id;
335 #endif
336
337 assert(bus);
338 assert(reply);
339 assert(c);
340
341 #if HAVE_SECCOMP
342 SET_FOREACH(id, c->syscall_archs, i) {
343 const char *name;
344
345 name = seccomp_arch_to_string(PTR_TO_UINT32(id) - 1);
346 if (!name)
347 continue;
348
349 r = strv_extend(&l, name);
350 if (r < 0)
351 return -ENOMEM;
352 }
353 #endif
354
355 strv_sort(l);
356
357 r = sd_bus_message_append_strv(reply, l);
358 if (r < 0)
359 return r;
360
361 return 0;
362 }
363
364 static int property_get_selinux_context(
365 sd_bus *bus,
366 const char *path,
367 const char *interface,
368 const char *property,
369 sd_bus_message *reply,
370 void *userdata,
371 sd_bus_error *error) {
372
373 ExecContext *c = userdata;
374
375 assert(bus);
376 assert(reply);
377 assert(c);
378
379 return sd_bus_message_append(reply, "(bs)", c->selinux_context_ignore, c->selinux_context);
380 }
381
382 static int property_get_apparmor_profile(
383 sd_bus *bus,
384 const char *path,
385 const char *interface,
386 const char *property,
387 sd_bus_message *reply,
388 void *userdata,
389 sd_bus_error *error) {
390
391 ExecContext *c = userdata;
392
393 assert(bus);
394 assert(reply);
395 assert(c);
396
397 return sd_bus_message_append(reply, "(bs)", c->apparmor_profile_ignore, c->apparmor_profile);
398 }
399
400 static int property_get_smack_process_label(
401 sd_bus *bus,
402 const char *path,
403 const char *interface,
404 const char *property,
405 sd_bus_message *reply,
406 void *userdata,
407 sd_bus_error *error) {
408
409 ExecContext *c = userdata;
410
411 assert(bus);
412 assert(reply);
413 assert(c);
414
415 return sd_bus_message_append(reply, "(bs)", c->smack_process_label_ignore, c->smack_process_label);
416 }
417
418 static int property_get_address_families(
419 sd_bus *bus,
420 const char *path,
421 const char *interface,
422 const char *property,
423 sd_bus_message *reply,
424 void *userdata,
425 sd_bus_error *error) {
426
427 ExecContext *c = userdata;
428 _cleanup_strv_free_ char **l = NULL;
429 Iterator i;
430 void *af;
431 int r;
432
433 assert(bus);
434 assert(reply);
435 assert(c);
436
437 r = sd_bus_message_open_container(reply, 'r', "bas");
438 if (r < 0)
439 return r;
440
441 r = sd_bus_message_append(reply, "b", c->address_families_whitelist);
442 if (r < 0)
443 return r;
444
445 SET_FOREACH(af, c->address_families, i) {
446 const char *name;
447
448 name = af_to_name(PTR_TO_INT(af));
449 if (!name)
450 continue;
451
452 r = strv_extend(&l, name);
453 if (r < 0)
454 return -ENOMEM;
455 }
456
457 strv_sort(l);
458
459 r = sd_bus_message_append_strv(reply, l);
460 if (r < 0)
461 return r;
462
463 return sd_bus_message_close_container(reply);
464 }
465
466 static int property_get_working_directory(
467 sd_bus *bus,
468 const char *path,
469 const char *interface,
470 const char *property,
471 sd_bus_message *reply,
472 void *userdata,
473 sd_bus_error *error) {
474
475 ExecContext *c = userdata;
476 const char *wd;
477
478 assert(bus);
479 assert(reply);
480 assert(c);
481
482 if (c->working_directory_home)
483 wd = "~";
484 else
485 wd = c->working_directory;
486
487 if (c->working_directory_missing_ok)
488 wd = strjoina("!", wd);
489
490 return sd_bus_message_append(reply, "s", wd);
491 }
492
493 static int property_get_stdio_fdname(
494 sd_bus *bus,
495 const char *path,
496 const char *interface,
497 const char *property,
498 sd_bus_message *reply,
499 void *userdata,
500 sd_bus_error *error) {
501
502 ExecContext *c = userdata;
503 int fileno;
504
505 assert(bus);
506 assert(c);
507 assert(property);
508 assert(reply);
509
510 if (streq(property, "StandardInputFileDescriptorName"))
511 fileno = STDIN_FILENO;
512 else if (streq(property, "StandardOutputFileDescriptorName"))
513 fileno = STDOUT_FILENO;
514 else {
515 assert(streq(property, "StandardErrorFileDescriptorName"));
516 fileno = STDERR_FILENO;
517 }
518
519 return sd_bus_message_append(reply, "s", exec_context_fdname(c, fileno));
520 }
521
522 static int property_get_input_data(
523 sd_bus *bus,
524 const char *path,
525 const char *interface,
526 const char *property,
527 sd_bus_message *reply,
528 void *userdata,
529 sd_bus_error *error) {
530
531 ExecContext *c = userdata;
532
533 assert(bus);
534 assert(c);
535 assert(property);
536 assert(reply);
537
538 return sd_bus_message_append_array(reply, 'y', c->stdin_data, c->stdin_data_size);
539 }
540
541 static int property_get_bind_paths(
542 sd_bus *bus,
543 const char *path,
544 const char *interface,
545 const char *property,
546 sd_bus_message *reply,
547 void *userdata,
548 sd_bus_error *error) {
549
550 ExecContext *c = userdata;
551 unsigned i;
552 bool ro;
553 int r;
554
555 assert(bus);
556 assert(c);
557 assert(property);
558 assert(reply);
559
560 ro = strstr(property, "ReadOnly");
561
562 r = sd_bus_message_open_container(reply, 'a', "(ssbt)");
563 if (r < 0)
564 return r;
565
566 for (i = 0; i < c->n_bind_mounts; i++) {
567
568 if (ro != c->bind_mounts[i].read_only)
569 continue;
570
571 r = sd_bus_message_append(
572 reply, "(ssbt)",
573 c->bind_mounts[i].source,
574 c->bind_mounts[i].destination,
575 c->bind_mounts[i].ignore_enoent,
576 c->bind_mounts[i].recursive ? (uint64_t) MS_REC : (uint64_t) 0);
577 if (r < 0)
578 return r;
579 }
580
581 return sd_bus_message_close_container(reply);
582 }
583
584 static int property_get_temporary_filesystems(
585 sd_bus *bus,
586 const char *path,
587 const char *interface,
588 const char *property,
589 sd_bus_message *reply,
590 void *userdata,
591 sd_bus_error *error) {
592
593 ExecContext *c = userdata;
594 unsigned i;
595 int r;
596
597 assert(bus);
598 assert(c);
599 assert(property);
600 assert(reply);
601
602 r = sd_bus_message_open_container(reply, 'a', "(ss)");
603 if (r < 0)
604 return r;
605
606 for (i = 0; i < c->n_temporary_filesystems; i++) {
607 TemporaryFileSystem *t = c->temporary_filesystems + i;
608
609 r = sd_bus_message_append(
610 reply, "(ss)",
611 t->path,
612 t->options);
613 if (r < 0)
614 return r;
615 }
616
617 return sd_bus_message_close_container(reply);
618 }
619
620 static int property_get_log_extra_fields(
621 sd_bus *bus,
622 const char *path,
623 const char *interface,
624 const char *property,
625 sd_bus_message *reply,
626 void *userdata,
627 sd_bus_error *error) {
628
629 ExecContext *c = userdata;
630 size_t i;
631 int r;
632
633 assert(bus);
634 assert(c);
635 assert(property);
636 assert(reply);
637
638 r = sd_bus_message_open_container(reply, 'a', "ay");
639 if (r < 0)
640 return r;
641
642 for (i = 0; i < c->n_log_extra_fields; i++) {
643 r = sd_bus_message_append_array(reply, 'y', c->log_extra_fields[i].iov_base, c->log_extra_fields[i].iov_len);
644 if (r < 0)
645 return r;
646 }
647
648 return sd_bus_message_close_container(reply);
649 }
650
651 const sd_bus_vtable bus_exec_vtable[] = {
652 SD_BUS_VTABLE_START(0),
653 SD_BUS_PROPERTY("Environment", "as", NULL, offsetof(ExecContext, environment), SD_BUS_VTABLE_PROPERTY_CONST),
654 SD_BUS_PROPERTY("EnvironmentFiles", "a(sb)", property_get_environment_files, 0, SD_BUS_VTABLE_PROPERTY_CONST),
655 SD_BUS_PROPERTY("PassEnvironment", "as", NULL, offsetof(ExecContext, pass_environment), SD_BUS_VTABLE_PROPERTY_CONST),
656 SD_BUS_PROPERTY("UnsetEnvironment", "as", NULL, offsetof(ExecContext, unset_environment), SD_BUS_VTABLE_PROPERTY_CONST),
657 SD_BUS_PROPERTY("UMask", "u", bus_property_get_mode, offsetof(ExecContext, umask), SD_BUS_VTABLE_PROPERTY_CONST),
658 SD_BUS_PROPERTY("LimitCPU", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_CPU]), SD_BUS_VTABLE_PROPERTY_CONST),
659 SD_BUS_PROPERTY("LimitCPUSoft", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_CPU]), SD_BUS_VTABLE_PROPERTY_CONST),
660 SD_BUS_PROPERTY("LimitFSIZE", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_FSIZE]), SD_BUS_VTABLE_PROPERTY_CONST),
661 SD_BUS_PROPERTY("LimitFSIZESoft", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_FSIZE]), SD_BUS_VTABLE_PROPERTY_CONST),
662 SD_BUS_PROPERTY("LimitDATA", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_DATA]), SD_BUS_VTABLE_PROPERTY_CONST),
663 SD_BUS_PROPERTY("LimitDATASoft", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_DATA]), SD_BUS_VTABLE_PROPERTY_CONST),
664 SD_BUS_PROPERTY("LimitSTACK", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_STACK]), SD_BUS_VTABLE_PROPERTY_CONST),
665 SD_BUS_PROPERTY("LimitSTACKSoft", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_STACK]), SD_BUS_VTABLE_PROPERTY_CONST),
666 SD_BUS_PROPERTY("LimitCORE", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_CORE]), SD_BUS_VTABLE_PROPERTY_CONST),
667 SD_BUS_PROPERTY("LimitCORESoft", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_CORE]), SD_BUS_VTABLE_PROPERTY_CONST),
668 SD_BUS_PROPERTY("LimitRSS", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_RSS]), SD_BUS_VTABLE_PROPERTY_CONST),
669 SD_BUS_PROPERTY("LimitRSSSoft", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_RSS]), SD_BUS_VTABLE_PROPERTY_CONST),
670 SD_BUS_PROPERTY("LimitNOFILE", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_NOFILE]), SD_BUS_VTABLE_PROPERTY_CONST),
671 SD_BUS_PROPERTY("LimitNOFILESoft", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_NOFILE]), SD_BUS_VTABLE_PROPERTY_CONST),
672 SD_BUS_PROPERTY("LimitAS", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_AS]), SD_BUS_VTABLE_PROPERTY_CONST),
673 SD_BUS_PROPERTY("LimitASSoft", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_AS]), SD_BUS_VTABLE_PROPERTY_CONST),
674 SD_BUS_PROPERTY("LimitNPROC", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_NPROC]), SD_BUS_VTABLE_PROPERTY_CONST),
675 SD_BUS_PROPERTY("LimitNPROCSoft", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_NPROC]), SD_BUS_VTABLE_PROPERTY_CONST),
676 SD_BUS_PROPERTY("LimitMEMLOCK", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_MEMLOCK]), SD_BUS_VTABLE_PROPERTY_CONST),
677 SD_BUS_PROPERTY("LimitMEMLOCKSoft", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_MEMLOCK]), SD_BUS_VTABLE_PROPERTY_CONST),
678 SD_BUS_PROPERTY("LimitLOCKS", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_LOCKS]), SD_BUS_VTABLE_PROPERTY_CONST),
679 SD_BUS_PROPERTY("LimitLOCKSSoft", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_LOCKS]), SD_BUS_VTABLE_PROPERTY_CONST),
680 SD_BUS_PROPERTY("LimitSIGPENDING", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_SIGPENDING]), SD_BUS_VTABLE_PROPERTY_CONST),
681 SD_BUS_PROPERTY("LimitSIGPENDINGSoft", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_SIGPENDING]), SD_BUS_VTABLE_PROPERTY_CONST),
682 SD_BUS_PROPERTY("LimitMSGQUEUE", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_MSGQUEUE]), SD_BUS_VTABLE_PROPERTY_CONST),
683 SD_BUS_PROPERTY("LimitMSGQUEUESoft", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_MSGQUEUE]), SD_BUS_VTABLE_PROPERTY_CONST),
684 SD_BUS_PROPERTY("LimitNICE", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_NICE]), SD_BUS_VTABLE_PROPERTY_CONST),
685 SD_BUS_PROPERTY("LimitNICESoft", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_NICE]), SD_BUS_VTABLE_PROPERTY_CONST),
686 SD_BUS_PROPERTY("LimitRTPRIO", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_RTPRIO]), SD_BUS_VTABLE_PROPERTY_CONST),
687 SD_BUS_PROPERTY("LimitRTPRIOSoft", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_RTPRIO]), SD_BUS_VTABLE_PROPERTY_CONST),
688 SD_BUS_PROPERTY("LimitRTTIME", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_RTTIME]), SD_BUS_VTABLE_PROPERTY_CONST),
689 SD_BUS_PROPERTY("LimitRTTIMESoft", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_RTTIME]), SD_BUS_VTABLE_PROPERTY_CONST),
690 SD_BUS_PROPERTY("WorkingDirectory", "s", property_get_working_directory, 0, SD_BUS_VTABLE_PROPERTY_CONST),
691 SD_BUS_PROPERTY("RootDirectory", "s", NULL, offsetof(ExecContext, root_directory), SD_BUS_VTABLE_PROPERTY_CONST),
692 SD_BUS_PROPERTY("RootImage", "s", NULL, offsetof(ExecContext, root_image), SD_BUS_VTABLE_PROPERTY_CONST),
693 SD_BUS_PROPERTY("OOMScoreAdjust", "i", property_get_oom_score_adjust, 0, SD_BUS_VTABLE_PROPERTY_CONST),
694 SD_BUS_PROPERTY("Nice", "i", property_get_nice, 0, SD_BUS_VTABLE_PROPERTY_CONST),
695 SD_BUS_PROPERTY("IOSchedulingClass", "i", property_get_ioprio_class, 0, SD_BUS_VTABLE_PROPERTY_CONST),
696 SD_BUS_PROPERTY("IOSchedulingPriority", "i", property_get_ioprio_priority, 0, SD_BUS_VTABLE_PROPERTY_CONST),
697 SD_BUS_PROPERTY("CPUSchedulingPolicy", "i", property_get_cpu_sched_policy, 0, SD_BUS_VTABLE_PROPERTY_CONST),
698 SD_BUS_PROPERTY("CPUSchedulingPriority", "i", property_get_cpu_sched_priority, 0, SD_BUS_VTABLE_PROPERTY_CONST),
699 SD_BUS_PROPERTY("CPUAffinity", "ay", property_get_cpu_affinity, 0, SD_BUS_VTABLE_PROPERTY_CONST),
700 SD_BUS_PROPERTY("TimerSlackNSec", "t", property_get_timer_slack_nsec, 0, SD_BUS_VTABLE_PROPERTY_CONST),
701 SD_BUS_PROPERTY("CPUSchedulingResetOnFork", "b", bus_property_get_bool, offsetof(ExecContext, cpu_sched_reset_on_fork), SD_BUS_VTABLE_PROPERTY_CONST),
702 SD_BUS_PROPERTY("NonBlocking", "b", bus_property_get_bool, offsetof(ExecContext, non_blocking), SD_BUS_VTABLE_PROPERTY_CONST),
703 SD_BUS_PROPERTY("StandardInput", "s", property_get_exec_input, offsetof(ExecContext, std_input), SD_BUS_VTABLE_PROPERTY_CONST),
704 SD_BUS_PROPERTY("StandardInputFileDescriptorName", "s", property_get_stdio_fdname, 0, SD_BUS_VTABLE_PROPERTY_CONST),
705 SD_BUS_PROPERTY("StandardInputData", "ay", property_get_input_data, 0, SD_BUS_VTABLE_PROPERTY_CONST),
706 SD_BUS_PROPERTY("StandardOutput", "s", bus_property_get_exec_output, offsetof(ExecContext, std_output), SD_BUS_VTABLE_PROPERTY_CONST),
707 SD_BUS_PROPERTY("StandardOutputFileDescriptorName", "s", property_get_stdio_fdname, 0, SD_BUS_VTABLE_PROPERTY_CONST),
708 SD_BUS_PROPERTY("StandardError", "s", bus_property_get_exec_output, offsetof(ExecContext, std_error), SD_BUS_VTABLE_PROPERTY_CONST),
709 SD_BUS_PROPERTY("StandardErrorFileDescriptorName", "s", property_get_stdio_fdname, 0, SD_BUS_VTABLE_PROPERTY_CONST),
710 SD_BUS_PROPERTY("TTYPath", "s", NULL, offsetof(ExecContext, tty_path), SD_BUS_VTABLE_PROPERTY_CONST),
711 SD_BUS_PROPERTY("TTYReset", "b", bus_property_get_bool, offsetof(ExecContext, tty_reset), SD_BUS_VTABLE_PROPERTY_CONST),
712 SD_BUS_PROPERTY("TTYVHangup", "b", bus_property_get_bool, offsetof(ExecContext, tty_vhangup), SD_BUS_VTABLE_PROPERTY_CONST),
713 SD_BUS_PROPERTY("TTYVTDisallocate", "b", bus_property_get_bool, offsetof(ExecContext, tty_vt_disallocate), SD_BUS_VTABLE_PROPERTY_CONST),
714 SD_BUS_PROPERTY("SyslogPriority", "i", bus_property_get_int, offsetof(ExecContext, syslog_priority), SD_BUS_VTABLE_PROPERTY_CONST),
715 SD_BUS_PROPERTY("SyslogIdentifier", "s", NULL, offsetof(ExecContext, syslog_identifier), SD_BUS_VTABLE_PROPERTY_CONST),
716 SD_BUS_PROPERTY("SyslogLevelPrefix", "b", bus_property_get_bool, offsetof(ExecContext, syslog_level_prefix), SD_BUS_VTABLE_PROPERTY_CONST),
717 SD_BUS_PROPERTY("SyslogLevel", "i", property_get_syslog_level, offsetof(ExecContext, syslog_priority), SD_BUS_VTABLE_PROPERTY_CONST),
718 SD_BUS_PROPERTY("SyslogFacility", "i", property_get_syslog_facility, offsetof(ExecContext, syslog_priority), SD_BUS_VTABLE_PROPERTY_CONST),
719 SD_BUS_PROPERTY("LogLevelMax", "i", bus_property_get_int, offsetof(ExecContext, log_level_max), SD_BUS_VTABLE_PROPERTY_CONST),
720 SD_BUS_PROPERTY("LogRateLimitIntervalUSec", "t", bus_property_get_usec, offsetof(ExecContext, log_rate_limit_interval_usec), SD_BUS_VTABLE_PROPERTY_CONST),
721 SD_BUS_PROPERTY("LogRateLimitBurst", "u", bus_property_get_unsigned, offsetof(ExecContext, log_rate_limit_burst), SD_BUS_VTABLE_PROPERTY_CONST),
722 SD_BUS_PROPERTY("LogExtraFields", "aay", property_get_log_extra_fields, 0, SD_BUS_VTABLE_PROPERTY_CONST),
723 SD_BUS_PROPERTY("SecureBits", "i", bus_property_get_int, offsetof(ExecContext, secure_bits), SD_BUS_VTABLE_PROPERTY_CONST),
724 SD_BUS_PROPERTY("CapabilityBoundingSet", "t", NULL, offsetof(ExecContext, capability_bounding_set), SD_BUS_VTABLE_PROPERTY_CONST),
725 SD_BUS_PROPERTY("AmbientCapabilities", "t", NULL, offsetof(ExecContext, capability_ambient_set), SD_BUS_VTABLE_PROPERTY_CONST),
726 SD_BUS_PROPERTY("User", "s", NULL, offsetof(ExecContext, user), SD_BUS_VTABLE_PROPERTY_CONST),
727 SD_BUS_PROPERTY("Group", "s", NULL, offsetof(ExecContext, group), SD_BUS_VTABLE_PROPERTY_CONST),
728 SD_BUS_PROPERTY("DynamicUser", "b", bus_property_get_bool, offsetof(ExecContext, dynamic_user), SD_BUS_VTABLE_PROPERTY_CONST),
729 SD_BUS_PROPERTY("RemoveIPC", "b", bus_property_get_bool, offsetof(ExecContext, remove_ipc), SD_BUS_VTABLE_PROPERTY_CONST),
730 SD_BUS_PROPERTY("SupplementaryGroups", "as", NULL, offsetof(ExecContext, supplementary_groups), SD_BUS_VTABLE_PROPERTY_CONST),
731 SD_BUS_PROPERTY("PAMName", "s", NULL, offsetof(ExecContext, pam_name), SD_BUS_VTABLE_PROPERTY_CONST),
732 SD_BUS_PROPERTY("ReadWritePaths", "as", NULL, offsetof(ExecContext, read_write_paths), SD_BUS_VTABLE_PROPERTY_CONST),
733 SD_BUS_PROPERTY("ReadOnlyPaths", "as", NULL, offsetof(ExecContext, read_only_paths), SD_BUS_VTABLE_PROPERTY_CONST),
734 SD_BUS_PROPERTY("InaccessiblePaths", "as", NULL, offsetof(ExecContext, inaccessible_paths), SD_BUS_VTABLE_PROPERTY_CONST),
735 SD_BUS_PROPERTY("MountFlags", "t", bus_property_get_ulong, offsetof(ExecContext, mount_flags), SD_BUS_VTABLE_PROPERTY_CONST),
736 SD_BUS_PROPERTY("PrivateTmp", "b", bus_property_get_bool, offsetof(ExecContext, private_tmp), SD_BUS_VTABLE_PROPERTY_CONST),
737 SD_BUS_PROPERTY("PrivateDevices", "b", bus_property_get_bool, offsetof(ExecContext, private_devices), SD_BUS_VTABLE_PROPERTY_CONST),
738 SD_BUS_PROPERTY("ProtectKernelTunables", "b", bus_property_get_bool, offsetof(ExecContext, protect_kernel_tunables), SD_BUS_VTABLE_PROPERTY_CONST),
739 SD_BUS_PROPERTY("ProtectKernelModules", "b", bus_property_get_bool, offsetof(ExecContext, protect_kernel_modules), SD_BUS_VTABLE_PROPERTY_CONST),
740 SD_BUS_PROPERTY("ProtectControlGroups", "b", bus_property_get_bool, offsetof(ExecContext, protect_control_groups), SD_BUS_VTABLE_PROPERTY_CONST),
741 SD_BUS_PROPERTY("PrivateNetwork", "b", bus_property_get_bool, offsetof(ExecContext, private_network), SD_BUS_VTABLE_PROPERTY_CONST),
742 SD_BUS_PROPERTY("PrivateUsers", "b", bus_property_get_bool, offsetof(ExecContext, private_users), SD_BUS_VTABLE_PROPERTY_CONST),
743 SD_BUS_PROPERTY("PrivateMounts", "b", bus_property_get_bool, offsetof(ExecContext, private_mounts), SD_BUS_VTABLE_PROPERTY_CONST),
744 SD_BUS_PROPERTY("ProtectHome", "s", property_get_protect_home, offsetof(ExecContext, protect_home), SD_BUS_VTABLE_PROPERTY_CONST),
745 SD_BUS_PROPERTY("ProtectSystem", "s", property_get_protect_system, offsetof(ExecContext, protect_system), SD_BUS_VTABLE_PROPERTY_CONST),
746 SD_BUS_PROPERTY("SameProcessGroup", "b", bus_property_get_bool, offsetof(ExecContext, same_pgrp), SD_BUS_VTABLE_PROPERTY_CONST),
747 SD_BUS_PROPERTY("UtmpIdentifier", "s", NULL, offsetof(ExecContext, utmp_id), SD_BUS_VTABLE_PROPERTY_CONST),
748 SD_BUS_PROPERTY("UtmpMode", "s", property_get_exec_utmp_mode, offsetof(ExecContext, utmp_mode), SD_BUS_VTABLE_PROPERTY_CONST),
749 SD_BUS_PROPERTY("SELinuxContext", "(bs)", property_get_selinux_context, 0, SD_BUS_VTABLE_PROPERTY_CONST),
750 SD_BUS_PROPERTY("AppArmorProfile", "(bs)", property_get_apparmor_profile, 0, SD_BUS_VTABLE_PROPERTY_CONST),
751 SD_BUS_PROPERTY("SmackProcessLabel", "(bs)", property_get_smack_process_label, 0, SD_BUS_VTABLE_PROPERTY_CONST),
752 SD_BUS_PROPERTY("IgnoreSIGPIPE", "b", bus_property_get_bool, offsetof(ExecContext, ignore_sigpipe), SD_BUS_VTABLE_PROPERTY_CONST),
753 SD_BUS_PROPERTY("NoNewPrivileges", "b", bus_property_get_bool, offsetof(ExecContext, no_new_privileges), SD_BUS_VTABLE_PROPERTY_CONST),
754 SD_BUS_PROPERTY("SystemCallFilter", "(bas)", property_get_syscall_filter, 0, SD_BUS_VTABLE_PROPERTY_CONST),
755 SD_BUS_PROPERTY("SystemCallArchitectures", "as", property_get_syscall_archs, 0, SD_BUS_VTABLE_PROPERTY_CONST),
756 SD_BUS_PROPERTY("SystemCallErrorNumber", "i", bus_property_get_int, offsetof(ExecContext, syscall_errno), SD_BUS_VTABLE_PROPERTY_CONST),
757 SD_BUS_PROPERTY("Personality", "s", property_get_personality, offsetof(ExecContext, personality), SD_BUS_VTABLE_PROPERTY_CONST),
758 SD_BUS_PROPERTY("LockPersonality", "b", bus_property_get_bool, offsetof(ExecContext, lock_personality), SD_BUS_VTABLE_PROPERTY_CONST),
759 SD_BUS_PROPERTY("RestrictAddressFamilies", "(bas)", property_get_address_families, 0, SD_BUS_VTABLE_PROPERTY_CONST),
760 SD_BUS_PROPERTY("RuntimeDirectoryPreserve", "s", property_get_exec_preserve_mode, offsetof(ExecContext, runtime_directory_preserve_mode), SD_BUS_VTABLE_PROPERTY_CONST),
761 SD_BUS_PROPERTY("RuntimeDirectoryMode", "u", bus_property_get_mode, offsetof(ExecContext, directories[EXEC_DIRECTORY_RUNTIME].mode), SD_BUS_VTABLE_PROPERTY_CONST),
762 SD_BUS_PROPERTY("RuntimeDirectory", "as", NULL, offsetof(ExecContext, directories[EXEC_DIRECTORY_RUNTIME].paths), SD_BUS_VTABLE_PROPERTY_CONST),
763 SD_BUS_PROPERTY("StateDirectoryMode", "u", bus_property_get_mode, offsetof(ExecContext, directories[EXEC_DIRECTORY_STATE].mode), SD_BUS_VTABLE_PROPERTY_CONST),
764 SD_BUS_PROPERTY("StateDirectory", "as", NULL, offsetof(ExecContext, directories[EXEC_DIRECTORY_STATE].paths), SD_BUS_VTABLE_PROPERTY_CONST),
765 SD_BUS_PROPERTY("CacheDirectoryMode", "u", bus_property_get_mode, offsetof(ExecContext, directories[EXEC_DIRECTORY_CACHE].mode), SD_BUS_VTABLE_PROPERTY_CONST),
766 SD_BUS_PROPERTY("CacheDirectory", "as", NULL, offsetof(ExecContext, directories[EXEC_DIRECTORY_CACHE].paths), SD_BUS_VTABLE_PROPERTY_CONST),
767 SD_BUS_PROPERTY("LogsDirectoryMode", "u", bus_property_get_mode, offsetof(ExecContext, directories[EXEC_DIRECTORY_LOGS].mode), SD_BUS_VTABLE_PROPERTY_CONST),
768 SD_BUS_PROPERTY("LogsDirectory", "as", NULL, offsetof(ExecContext, directories[EXEC_DIRECTORY_LOGS].paths), SD_BUS_VTABLE_PROPERTY_CONST),
769 SD_BUS_PROPERTY("ConfigurationDirectoryMode", "u", bus_property_get_mode, offsetof(ExecContext, directories[EXEC_DIRECTORY_CONFIGURATION].mode), SD_BUS_VTABLE_PROPERTY_CONST),
770 SD_BUS_PROPERTY("ConfigurationDirectory", "as", NULL, offsetof(ExecContext, directories[EXEC_DIRECTORY_CONFIGURATION].paths), SD_BUS_VTABLE_PROPERTY_CONST),
771 SD_BUS_PROPERTY("MemoryDenyWriteExecute", "b", bus_property_get_bool, offsetof(ExecContext, memory_deny_write_execute), SD_BUS_VTABLE_PROPERTY_CONST),
772 SD_BUS_PROPERTY("RestrictRealtime", "b", bus_property_get_bool, offsetof(ExecContext, restrict_realtime), SD_BUS_VTABLE_PROPERTY_CONST),
773 SD_BUS_PROPERTY("RestrictSUIDSGID", "b", bus_property_get_bool, offsetof(ExecContext, restrict_suid_sgid), SD_BUS_VTABLE_PROPERTY_CONST),
774 SD_BUS_PROPERTY("RestrictNamespaces", "t", bus_property_get_ulong, offsetof(ExecContext, restrict_namespaces), SD_BUS_VTABLE_PROPERTY_CONST),
775 SD_BUS_PROPERTY("BindPaths", "a(ssbt)", property_get_bind_paths, 0, SD_BUS_VTABLE_PROPERTY_CONST),
776 SD_BUS_PROPERTY("BindReadOnlyPaths", "a(ssbt)", property_get_bind_paths, 0, SD_BUS_VTABLE_PROPERTY_CONST),
777 SD_BUS_PROPERTY("TemporaryFileSystem", "a(ss)", property_get_temporary_filesystems, 0, SD_BUS_VTABLE_PROPERTY_CONST),
778 SD_BUS_PROPERTY("MountAPIVFS", "b", bus_property_get_bool, offsetof(ExecContext, mount_apivfs), SD_BUS_VTABLE_PROPERTY_CONST),
779 SD_BUS_PROPERTY("KeyringMode", "s", property_get_exec_keyring_mode, offsetof(ExecContext, keyring_mode), SD_BUS_VTABLE_PROPERTY_CONST),
780 SD_BUS_PROPERTY("ProtectHostname", "b", bus_property_get_bool, offsetof(ExecContext, protect_hostname), SD_BUS_VTABLE_PROPERTY_CONST),
781 SD_BUS_PROPERTY("NetworkNamespacePath", "s", NULL, offsetof(ExecContext, network_namespace_path), SD_BUS_VTABLE_PROPERTY_CONST),
782
783 /* Obsolete/redundant properties: */
784 SD_BUS_PROPERTY("Capabilities", "s", property_get_empty_string, 0, SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_HIDDEN),
785 SD_BUS_PROPERTY("ReadWriteDirectories", "as", NULL, offsetof(ExecContext, read_write_paths), SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_HIDDEN),
786 SD_BUS_PROPERTY("ReadOnlyDirectories", "as", NULL, offsetof(ExecContext, read_only_paths), SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_HIDDEN),
787 SD_BUS_PROPERTY("InaccessibleDirectories", "as", NULL, offsetof(ExecContext, inaccessible_paths), SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_HIDDEN),
788 SD_BUS_PROPERTY("IOScheduling", "i", property_get_ioprio, 0, SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_HIDDEN),
789
790 SD_BUS_VTABLE_END
791 };
792
793 static int append_exec_command(sd_bus_message *reply, ExecCommand *c) {
794 int r;
795
796 assert(reply);
797 assert(c);
798
799 if (!c->path)
800 return 0;
801
802 r = sd_bus_message_open_container(reply, 'r', "sasbttttuii");
803 if (r < 0)
804 return r;
805
806 r = sd_bus_message_append(reply, "s", c->path);
807 if (r < 0)
808 return r;
809
810 r = sd_bus_message_append_strv(reply, c->argv);
811 if (r < 0)
812 return r;
813
814 r = sd_bus_message_append(reply, "bttttuii",
815 !!(c->flags & EXEC_COMMAND_IGNORE_FAILURE),
816 c->exec_status.start_timestamp.realtime,
817 c->exec_status.start_timestamp.monotonic,
818 c->exec_status.exit_timestamp.realtime,
819 c->exec_status.exit_timestamp.monotonic,
820 (uint32_t) c->exec_status.pid,
821 (int32_t) c->exec_status.code,
822 (int32_t) c->exec_status.status);
823 if (r < 0)
824 return r;
825
826 return sd_bus_message_close_container(reply);
827 }
828
829 static int append_exec_ex_command(sd_bus_message *reply, ExecCommand *c) {
830 _cleanup_strv_free_ char **ex_opts = NULL;
831 int r;
832
833 assert(reply);
834 assert(c);
835
836 if (!c->path)
837 return 0;
838
839 r = sd_bus_message_open_container(reply, 'r', "sasasttttuii");
840 if (r < 0)
841 return r;
842
843 r = sd_bus_message_append(reply, "s", c->path);
844 if (r < 0)
845 return r;
846
847 r = sd_bus_message_append_strv(reply, c->argv);
848 if (r < 0)
849 return r;
850
851 r = exec_command_flags_to_strv(c->flags, &ex_opts);
852 if (r < 0)
853 return r;
854
855 r = sd_bus_message_append_strv(reply, ex_opts);
856 if (r < 0)
857 return r;
858
859 r = sd_bus_message_append(reply, "ttttuii",
860 c->exec_status.start_timestamp.realtime,
861 c->exec_status.start_timestamp.monotonic,
862 c->exec_status.exit_timestamp.realtime,
863 c->exec_status.exit_timestamp.monotonic,
864 (uint32_t) c->exec_status.pid,
865 (int32_t) c->exec_status.code,
866 (int32_t) c->exec_status.status);
867 if (r < 0)
868 return r;
869
870 return sd_bus_message_close_container(reply);
871 }
872
873 int bus_property_get_exec_command(
874 sd_bus *bus,
875 const char *path,
876 const char *interface,
877 const char *property,
878 sd_bus_message *reply,
879 void *userdata,
880 sd_bus_error *ret_error) {
881
882 ExecCommand *c = (ExecCommand*) userdata;
883 int r;
884
885 assert(bus);
886 assert(reply);
887
888 r = sd_bus_message_open_container(reply, 'a', "(sasbttttuii)");
889 if (r < 0)
890 return r;
891
892 r = append_exec_command(reply, c);
893 if (r < 0)
894 return r;
895
896 return sd_bus_message_close_container(reply);
897 }
898
899 int bus_property_get_exec_command_list(
900 sd_bus *bus,
901 const char *path,
902 const char *interface,
903 const char *property,
904 sd_bus_message *reply,
905 void *userdata,
906 sd_bus_error *ret_error) {
907
908 ExecCommand *c = *(ExecCommand**) userdata;
909 int r;
910
911 assert(bus);
912 assert(reply);
913
914 r = sd_bus_message_open_container(reply, 'a', "(sasbttttuii)");
915 if (r < 0)
916 return r;
917
918 LIST_FOREACH(command, c, c) {
919 r = append_exec_command(reply, c);
920 if (r < 0)
921 return r;
922 }
923
924 return sd_bus_message_close_container(reply);
925 }
926
927 int bus_property_get_exec_ex_command_list(
928 sd_bus *bus,
929 const char *path,
930 const char *interface,
931 const char *property,
932 sd_bus_message *reply,
933 void *userdata,
934 sd_bus_error *ret_error) {
935
936 ExecCommand *c, *exec_command = *(ExecCommand**) userdata;
937 int r;
938
939 assert(bus);
940 assert(reply);
941
942 r = sd_bus_message_open_container(reply, 'a', "(sasasttttuii)");
943 if (r < 0)
944 return r;
945
946 LIST_FOREACH(command, c, exec_command) {
947 r = append_exec_ex_command(reply, c);
948 if (r < 0)
949 return r;
950 }
951
952 return sd_bus_message_close_container(reply);
953 }
954
955 static char *exec_command_flags_to_exec_chars(ExecCommandFlags flags) {
956 char *res = NULL;
957
958 asprintf(&res, "%s%s%s%s%s",
959 FLAGS_SET(flags, EXEC_COMMAND_IGNORE_FAILURE) ? "-" : "",
960 FLAGS_SET(flags, EXEC_COMMAND_NO_ENV_EXPAND) ? ":" : "",
961 FLAGS_SET(flags, EXEC_COMMAND_FULLY_PRIVILEGED) ? "+" : "",
962 FLAGS_SET(flags, EXEC_COMMAND_NO_SETUID) ? "!" : "",
963 FLAGS_SET(flags, EXEC_COMMAND_AMBIENT_MAGIC) ? "!!" : "");
964
965 return res;
966 }
967
968 int bus_set_transient_exec_command(
969 Unit *u,
970 const char *name,
971 ExecCommand **exec_command,
972 sd_bus_message *message,
973 UnitWriteFlags flags,
974 sd_bus_error *error) {
975 bool is_ex_prop = endswith(name, "Ex");
976 unsigned n = 0;
977 int r;
978
979 r = sd_bus_message_enter_container(message, 'a', is_ex_prop ? "(sasas)" : "(sasb)");
980 if (r < 0)
981 return r;
982
983 while ((r = sd_bus_message_enter_container(message, 'r', is_ex_prop ? "sasas" : "sasb")) > 0) {
984 _cleanup_strv_free_ char **argv = NULL, **ex_opts = NULL;
985 const char *path;
986 int b;
987
988 r = sd_bus_message_read(message, "s", &path);
989 if (r < 0)
990 return r;
991
992 if (!path_is_absolute(path))
993 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path %s is not absolute.", path);
994
995 r = sd_bus_message_read_strv(message, &argv);
996 if (r < 0)
997 return r;
998
999 r = is_ex_prop ? sd_bus_message_read_strv(message, &ex_opts) : sd_bus_message_read(message, "b", &b);
1000 if (r < 0)
1001 return r;
1002
1003 r = sd_bus_message_exit_container(message);
1004 if (r < 0)
1005 return r;
1006
1007 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1008 ExecCommand *c;
1009
1010 c = new0(ExecCommand, 1);
1011 if (!c)
1012 return -ENOMEM;
1013
1014 c->path = strdup(path);
1015 if (!c->path) {
1016 free(c);
1017 return -ENOMEM;
1018 }
1019
1020 c->argv = TAKE_PTR(argv);
1021
1022 if (is_ex_prop) {
1023 r = exec_command_flags_from_strv(ex_opts, &c->flags);
1024 if (r < 0)
1025 return r;
1026 } else
1027 c->flags = b ? EXEC_COMMAND_IGNORE_FAILURE : 0;
1028
1029 path_simplify(c->path, false);
1030 exec_command_append_list(exec_command, c);
1031 }
1032
1033 n++;
1034 }
1035 if (r < 0)
1036 return r;
1037
1038 r = sd_bus_message_exit_container(message);
1039 if (r < 0)
1040 return r;
1041
1042 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1043 _cleanup_free_ char *buf = NULL;
1044 _cleanup_fclose_ FILE *f = NULL;
1045 ExecCommand *c;
1046 size_t size = 0;
1047
1048 if (n == 0)
1049 *exec_command = exec_command_free_list(*exec_command);
1050
1051 f = open_memstream_unlocked(&buf, &size);
1052 if (!f)
1053 return -ENOMEM;
1054
1055 fputs("ExecStart=\n", f);
1056
1057 LIST_FOREACH(command, c, *exec_command) {
1058 _cleanup_free_ char *a = NULL, *t = NULL, *exec_chars = NULL;
1059 const char *p;
1060
1061 p = unit_escape_setting(c->path, UNIT_ESCAPE_C|UNIT_ESCAPE_SPECIFIERS, &t);
1062 if (!p)
1063 return -ENOMEM;
1064
1065 a = unit_concat_strv(c->argv, UNIT_ESCAPE_C|UNIT_ESCAPE_SPECIFIERS);
1066 if (!a)
1067 return -ENOMEM;
1068
1069 exec_chars = exec_command_flags_to_exec_chars(c->flags);
1070 if (!exec_chars)
1071 return -ENOMEM;
1072
1073 fprintf(f, "%s=%s@%s %s\n", name, exec_chars, p, a);
1074 }
1075
1076 r = fflush_and_check(f);
1077 if (r < 0)
1078 return r;
1079
1080 unit_write_setting(u, flags, name, buf);
1081 }
1082
1083 return 1;
1084 }
1085
1086 static int parse_personality(const char *s, unsigned long *p) {
1087 unsigned long v;
1088
1089 assert(p);
1090
1091 v = personality_from_string(s);
1092 if (v == PERSONALITY_INVALID)
1093 return -EINVAL;
1094
1095 *p = v;
1096 return 0;
1097 }
1098
1099 static const char* mount_propagation_flags_to_string_with_check(unsigned long n) {
1100 if (!IN_SET(n, 0, MS_SHARED, MS_PRIVATE, MS_SLAVE))
1101 return NULL;
1102
1103 return mount_propagation_flags_to_string(n);
1104 }
1105
1106 static BUS_DEFINE_SET_TRANSIENT(nsec, "t", uint64_t, nsec_t, NSEC_FMT);
1107 static BUS_DEFINE_SET_TRANSIENT_IS_VALID(log_level, "i", int32_t, int, "%" PRIi32, log_level_is_valid);
1108 #if HAVE_SECCOMP
1109 static BUS_DEFINE_SET_TRANSIENT_IS_VALID(errno, "i", int32_t, int, "%" PRIi32, errno_is_valid);
1110 #endif
1111 static BUS_DEFINE_SET_TRANSIENT_PARSE(std_input, ExecInput, exec_input_from_string);
1112 static BUS_DEFINE_SET_TRANSIENT_PARSE(std_output, ExecOutput, exec_output_from_string);
1113 static BUS_DEFINE_SET_TRANSIENT_PARSE(utmp_mode, ExecUtmpMode, exec_utmp_mode_from_string);
1114 static BUS_DEFINE_SET_TRANSIENT_PARSE(protect_system, ProtectSystem, protect_system_from_string);
1115 static BUS_DEFINE_SET_TRANSIENT_PARSE(protect_home, ProtectHome, protect_home_from_string);
1116 static BUS_DEFINE_SET_TRANSIENT_PARSE(keyring_mode, ExecKeyringMode, exec_keyring_mode_from_string);
1117 static BUS_DEFINE_SET_TRANSIENT_PARSE(preserve_mode, ExecPreserveMode, exec_preserve_mode_from_string);
1118 static BUS_DEFINE_SET_TRANSIENT_PARSE_PTR(personality, unsigned long, parse_personality);
1119 static BUS_DEFINE_SET_TRANSIENT_TO_STRING_ALLOC(secure_bits, "i", int32_t, int, "%" PRIi32, secure_bits_to_string_alloc_with_check);
1120 static BUS_DEFINE_SET_TRANSIENT_TO_STRING_ALLOC(capability, "t", uint64_t, uint64_t, "%" PRIu64, capability_set_to_string_alloc);
1121 static BUS_DEFINE_SET_TRANSIENT_TO_STRING_ALLOC(namespace_flag, "t", uint64_t, unsigned long, "%" PRIu64, namespace_flags_to_string);
1122 static BUS_DEFINE_SET_TRANSIENT_TO_STRING(mount_flags, "t", uint64_t, unsigned long, "%" PRIu64, mount_propagation_flags_to_string_with_check);
1123
1124 int bus_exec_context_set_transient_property(
1125 Unit *u,
1126 ExecContext *c,
1127 const char *name,
1128 sd_bus_message *message,
1129 UnitWriteFlags flags,
1130 sd_bus_error *error) {
1131
1132 const char *suffix;
1133 int r;
1134
1135 assert(u);
1136 assert(c);
1137 assert(name);
1138 assert(message);
1139
1140 flags |= UNIT_PRIVATE;
1141
1142 if (streq(name, "User"))
1143 return bus_set_transient_user(u, name, &c->user, message, flags, error);
1144
1145 if (streq(name, "Group"))
1146 return bus_set_transient_user(u, name, &c->group, message, flags, error);
1147
1148 if (streq(name, "TTYPath"))
1149 return bus_set_transient_path(u, name, &c->tty_path, message, flags, error);
1150
1151 if (streq(name, "RootImage"))
1152 return bus_set_transient_path(u, name, &c->root_image, message, flags, error);
1153
1154 if (streq(name, "RootDirectory"))
1155 return bus_set_transient_path(u, name, &c->root_directory, message, flags, error);
1156
1157 if (streq(name, "SyslogIdentifier"))
1158 return bus_set_transient_string(u, name, &c->syslog_identifier, message, flags, error);
1159
1160 if (streq(name, "LogLevelMax"))
1161 return bus_set_transient_log_level(u, name, &c->log_level_max, message, flags, error);
1162
1163 if (streq(name, "LogRateLimitIntervalUSec"))
1164 return bus_set_transient_usec(u, name, &c->log_rate_limit_interval_usec, message, flags, error);
1165
1166 if (streq(name, "LogRateLimitBurst"))
1167 return bus_set_transient_unsigned(u, name, &c->log_rate_limit_burst, message, flags, error);
1168
1169 if (streq(name, "Personality"))
1170 return bus_set_transient_personality(u, name, &c->personality, message, flags, error);
1171
1172 if (streq(name, "StandardInput"))
1173 return bus_set_transient_std_input(u, name, &c->std_input, message, flags, error);
1174
1175 if (streq(name, "StandardOutput"))
1176 return bus_set_transient_std_output(u, name, &c->std_output, message, flags, error);
1177
1178 if (streq(name, "StandardError"))
1179 return bus_set_transient_std_output(u, name, &c->std_error, message, flags, error);
1180
1181 if (streq(name, "IgnoreSIGPIPE"))
1182 return bus_set_transient_bool(u, name, &c->ignore_sigpipe, message, flags, error);
1183
1184 if (streq(name, "TTYVHangup"))
1185 return bus_set_transient_bool(u, name, &c->tty_vhangup, message, flags, error);
1186
1187 if (streq(name, "TTYReset"))
1188 return bus_set_transient_bool(u, name, &c->tty_reset, message, flags, error);
1189
1190 if (streq(name, "TTYVTDisallocate"))
1191 return bus_set_transient_bool(u, name, &c->tty_vt_disallocate, message, flags, error);
1192
1193 if (streq(name, "PrivateTmp"))
1194 return bus_set_transient_bool(u, name, &c->private_tmp, message, flags, error);
1195
1196 if (streq(name, "PrivateDevices"))
1197 return bus_set_transient_bool(u, name, &c->private_devices, message, flags, error);
1198
1199 if (streq(name, "PrivateMounts"))
1200 return bus_set_transient_bool(u, name, &c->private_mounts, message, flags, error);
1201
1202 if (streq(name, "PrivateNetwork"))
1203 return bus_set_transient_bool(u, name, &c->private_network, message, flags, error);
1204
1205 if (streq(name, "PrivateUsers"))
1206 return bus_set_transient_bool(u, name, &c->private_users, message, flags, error);
1207
1208 if (streq(name, "NoNewPrivileges"))
1209 return bus_set_transient_bool(u, name, &c->no_new_privileges, message, flags, error);
1210
1211 if (streq(name, "SyslogLevelPrefix"))
1212 return bus_set_transient_bool(u, name, &c->syslog_level_prefix, message, flags, error);
1213
1214 if (streq(name, "MemoryDenyWriteExecute"))
1215 return bus_set_transient_bool(u, name, &c->memory_deny_write_execute, message, flags, error);
1216
1217 if (streq(name, "RestrictRealtime"))
1218 return bus_set_transient_bool(u, name, &c->restrict_realtime, message, flags, error);
1219
1220 if (streq(name, "RestrictSUIDSGID"))
1221 return bus_set_transient_bool(u, name, &c->restrict_suid_sgid, message, flags, error);
1222
1223 if (streq(name, "DynamicUser"))
1224 return bus_set_transient_bool(u, name, &c->dynamic_user, message, flags, error);
1225
1226 if (streq(name, "RemoveIPC"))
1227 return bus_set_transient_bool(u, name, &c->remove_ipc, message, flags, error);
1228
1229 if (streq(name, "ProtectKernelTunables"))
1230 return bus_set_transient_bool(u, name, &c->protect_kernel_tunables, message, flags, error);
1231
1232 if (streq(name, "ProtectKernelModules"))
1233 return bus_set_transient_bool(u, name, &c->protect_kernel_modules, message, flags, error);
1234
1235 if (streq(name, "ProtectControlGroups"))
1236 return bus_set_transient_bool(u, name, &c->protect_control_groups, message, flags, error);
1237
1238 if (streq(name, "MountAPIVFS"))
1239 return bus_set_transient_bool(u, name, &c->mount_apivfs, message, flags, error);
1240
1241 if (streq(name, "CPUSchedulingResetOnFork"))
1242 return bus_set_transient_bool(u, name, &c->cpu_sched_reset_on_fork, message, flags, error);
1243
1244 if (streq(name, "NonBlocking"))
1245 return bus_set_transient_bool(u, name, &c->non_blocking, message, flags, error);
1246
1247 if (streq(name, "LockPersonality"))
1248 return bus_set_transient_bool(u, name, &c->lock_personality, message, flags, error);
1249
1250 if (streq(name, "ProtectHostname"))
1251 return bus_set_transient_bool(u, name, &c->protect_hostname, message, flags, error);
1252
1253 if (streq(name, "UtmpIdentifier"))
1254 return bus_set_transient_string(u, name, &c->utmp_id, message, flags, error);
1255
1256 if (streq(name, "UtmpMode"))
1257 return bus_set_transient_utmp_mode(u, name, &c->utmp_mode, message, flags, error);
1258
1259 if (streq(name, "PAMName"))
1260 return bus_set_transient_string(u, name, &c->pam_name, message, flags, error);
1261
1262 if (streq(name, "TimerSlackNSec"))
1263 return bus_set_transient_nsec(u, name, &c->timer_slack_nsec, message, flags, error);
1264
1265 if (streq(name, "ProtectSystem"))
1266 return bus_set_transient_protect_system(u, name, &c->protect_system, message, flags, error);
1267
1268 if (streq(name, "ProtectHome"))
1269 return bus_set_transient_protect_home(u, name, &c->protect_home, message, flags, error);
1270
1271 if (streq(name, "KeyringMode"))
1272 return bus_set_transient_keyring_mode(u, name, &c->keyring_mode, message, flags, error);
1273
1274 if (streq(name, "RuntimeDirectoryPreserve"))
1275 return bus_set_transient_preserve_mode(u, name, &c->runtime_directory_preserve_mode, message, flags, error);
1276
1277 if (streq(name, "UMask"))
1278 return bus_set_transient_mode_t(u, name, &c->umask, message, flags, error);
1279
1280 if (streq(name, "RuntimeDirectoryMode"))
1281 return bus_set_transient_mode_t(u, name, &c->directories[EXEC_DIRECTORY_RUNTIME].mode, message, flags, error);
1282
1283 if (streq(name, "StateDirectoryMode"))
1284 return bus_set_transient_mode_t(u, name, &c->directories[EXEC_DIRECTORY_STATE].mode, message, flags, error);
1285
1286 if (streq(name, "CacheDirectoryMode"))
1287 return bus_set_transient_mode_t(u, name, &c->directories[EXEC_DIRECTORY_CACHE].mode, message, flags, error);
1288
1289 if (streq(name, "LogsDirectoryMode"))
1290 return bus_set_transient_mode_t(u, name, &c->directories[EXEC_DIRECTORY_LOGS].mode, message, flags, error);
1291
1292 if (streq(name, "ConfigurationDirectoryMode"))
1293 return bus_set_transient_mode_t(u, name, &c->directories[EXEC_DIRECTORY_CONFIGURATION].mode, message, flags, error);
1294
1295 if (streq(name, "SELinuxContext"))
1296 return bus_set_transient_string(u, name, &c->selinux_context, message, flags, error);
1297
1298 if (streq(name, "SecureBits"))
1299 return bus_set_transient_secure_bits(u, name, &c->secure_bits, message, flags, error);
1300
1301 if (streq(name, "CapabilityBoundingSet"))
1302 return bus_set_transient_capability(u, name, &c->capability_bounding_set, message, flags, error);
1303
1304 if (streq(name, "AmbientCapabilities"))
1305 return bus_set_transient_capability(u, name, &c->capability_ambient_set, message, flags, error);
1306
1307 if (streq(name, "RestrictNamespaces"))
1308 return bus_set_transient_namespace_flag(u, name, &c->restrict_namespaces, message, flags, error);
1309
1310 if (streq(name, "MountFlags"))
1311 return bus_set_transient_mount_flags(u, name, &c->mount_flags, message, flags, error);
1312
1313 if (streq(name, "NetworkNamespacePath"))
1314 return bus_set_transient_path(u, name, &c->network_namespace_path, message, flags, error);
1315
1316 if (streq(name, "SupplementaryGroups")) {
1317 _cleanup_strv_free_ char **l = NULL;
1318 char **p;
1319
1320 r = sd_bus_message_read_strv(message, &l);
1321 if (r < 0)
1322 return r;
1323
1324 STRV_FOREACH(p, l) {
1325 if (!isempty(*p) && !valid_user_group_name_or_id(*p))
1326 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid supplementary group names");
1327 }
1328
1329 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1330 if (strv_isempty(l)) {
1331 c->supplementary_groups = strv_free(c->supplementary_groups);
1332 unit_write_settingf(u, flags, name, "%s=", name);
1333 } else {
1334 _cleanup_free_ char *joined = NULL;
1335
1336 r = strv_extend_strv(&c->supplementary_groups, l, true);
1337 if (r < 0)
1338 return -ENOMEM;
1339
1340 joined = strv_join(c->supplementary_groups, " ");
1341 if (!joined)
1342 return -ENOMEM;
1343
1344 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "%s=%s", name, joined);
1345 }
1346 }
1347
1348 return 1;
1349
1350 } else if (streq(name, "SyslogLevel")) {
1351 int32_t level;
1352
1353 r = sd_bus_message_read(message, "i", &level);
1354 if (r < 0)
1355 return r;
1356
1357 if (!log_level_is_valid(level))
1358 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Log level value out of range");
1359
1360 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1361 c->syslog_priority = (c->syslog_priority & LOG_FACMASK) | level;
1362 unit_write_settingf(u, flags, name, "SyslogLevel=%i", level);
1363 }
1364
1365 return 1;
1366
1367 } else if (streq(name, "SyslogFacility")) {
1368 int32_t facility;
1369
1370 r = sd_bus_message_read(message, "i", &facility);
1371 if (r < 0)
1372 return r;
1373
1374 if (!log_facility_unshifted_is_valid(facility))
1375 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Log facility value out of range");
1376
1377 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1378 c->syslog_priority = (facility << 3) | LOG_PRI(c->syslog_priority);
1379 unit_write_settingf(u, flags, name, "SyslogFacility=%i", facility);
1380 }
1381
1382 return 1;
1383
1384 } else if (streq(name, "LogExtraFields")) {
1385 size_t n = 0;
1386
1387 r = sd_bus_message_enter_container(message, 'a', "ay");
1388 if (r < 0)
1389 return r;
1390
1391 for (;;) {
1392 _cleanup_free_ void *copy = NULL;
1393 struct iovec *t;
1394 const char *eq;
1395 const void *p;
1396 size_t sz;
1397
1398 /* Note that we expect a byte array for each field, instead of a string. That's because on the
1399 * lower-level journal fields can actually contain binary data and are not restricted to text,
1400 * and we should not "lose precision" in our types on the way. That said, I am pretty sure
1401 * actually encoding binary data as unit metadata is not a good idea. Hence we actually refuse
1402 * any actual binary data, and only accept UTF-8. This allows us to eventually lift this
1403 * limitation, should a good, valid usecase arise. */
1404
1405 r = sd_bus_message_read_array(message, 'y', &p, &sz);
1406 if (r < 0)
1407 return r;
1408 if (r == 0)
1409 break;
1410
1411 if (memchr(p, 0, sz))
1412 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Journal field contains zero byte");
1413
1414 eq = memchr(p, '=', sz);
1415 if (!eq)
1416 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Journal field contains no '=' character");
1417 if (!journal_field_valid(p, eq - (const char*) p, false))
1418 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Journal field invalid");
1419
1420 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1421 t = reallocarray(c->log_extra_fields, c->n_log_extra_fields+1, sizeof(struct iovec));
1422 if (!t)
1423 return -ENOMEM;
1424 c->log_extra_fields = t;
1425 }
1426
1427 copy = malloc(sz + 1);
1428 if (!copy)
1429 return -ENOMEM;
1430
1431 memcpy(copy, p, sz);
1432 ((uint8_t*) copy)[sz] = 0;
1433
1434 if (!utf8_is_valid(copy))
1435 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Journal field is not valid UTF-8");
1436
1437 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1438 c->log_extra_fields[c->n_log_extra_fields++] = IOVEC_MAKE(copy, sz);
1439 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS|UNIT_ESCAPE_C, name, "LogExtraFields=%s", (char*) copy);
1440
1441 copy = NULL;
1442 }
1443
1444 n++;
1445 }
1446
1447 r = sd_bus_message_exit_container(message);
1448 if (r < 0)
1449 return r;
1450
1451 if (!UNIT_WRITE_FLAGS_NOOP(flags) && n == 0) {
1452 exec_context_free_log_extra_fields(c);
1453 unit_write_setting(u, flags, name, "LogExtraFields=");
1454 }
1455
1456 return 1;
1457 }
1458
1459 #if HAVE_SECCOMP
1460
1461 if (streq(name, "SystemCallErrorNumber"))
1462 return bus_set_transient_errno(u, name, &c->syscall_errno, message, flags, error);
1463
1464 if (streq(name, "SystemCallFilter")) {
1465 int whitelist;
1466 _cleanup_strv_free_ char **l = NULL;
1467
1468 r = sd_bus_message_enter_container(message, 'r', "bas");
1469 if (r < 0)
1470 return r;
1471
1472 r = sd_bus_message_read(message, "b", &whitelist);
1473 if (r < 0)
1474 return r;
1475
1476 r = sd_bus_message_read_strv(message, &l);
1477 if (r < 0)
1478 return r;
1479
1480 r = sd_bus_message_exit_container(message);
1481 if (r < 0)
1482 return r;
1483
1484 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1485 _cleanup_free_ char *joined = NULL;
1486 SeccompParseFlags invert_flag = whitelist ? 0 : SECCOMP_PARSE_INVERT;
1487 char **s;
1488
1489 if (strv_isempty(l)) {
1490 c->syscall_whitelist = false;
1491 c->syscall_filter = hashmap_free(c->syscall_filter);
1492
1493 unit_write_settingf(u, flags, name, "SystemCallFilter=");
1494 return 1;
1495 }
1496
1497 if (!c->syscall_filter) {
1498 c->syscall_filter = hashmap_new(NULL);
1499 if (!c->syscall_filter)
1500 return log_oom();
1501
1502 c->syscall_whitelist = whitelist;
1503
1504 if (c->syscall_whitelist) {
1505 r = seccomp_parse_syscall_filter("@default",
1506 -1,
1507 c->syscall_filter,
1508 SECCOMP_PARSE_WHITELIST | invert_flag,
1509 u->id,
1510 NULL, 0);
1511 if (r < 0)
1512 return r;
1513 }
1514 }
1515
1516 STRV_FOREACH(s, l) {
1517 _cleanup_free_ char *n = NULL;
1518 int e;
1519
1520 r = parse_syscall_and_errno(*s, &n, &e);
1521 if (r < 0)
1522 return r;
1523
1524 r = seccomp_parse_syscall_filter(n,
1525 e,
1526 c->syscall_filter,
1527 (c->syscall_whitelist ? SECCOMP_PARSE_WHITELIST : 0) | invert_flag,
1528 u->id,
1529 NULL, 0);
1530 if (r < 0)
1531 return r;
1532 }
1533
1534 joined = strv_join(l, " ");
1535 if (!joined)
1536 return -ENOMEM;
1537
1538 unit_write_settingf(u, flags, name, "SystemCallFilter=%s%s", whitelist ? "" : "~", joined);
1539 }
1540
1541 return 1;
1542
1543 } else if (streq(name, "SystemCallArchitectures")) {
1544 _cleanup_strv_free_ char **l = NULL;
1545
1546 r = sd_bus_message_read_strv(message, &l);
1547 if (r < 0)
1548 return r;
1549
1550 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1551 _cleanup_free_ char *joined = NULL;
1552
1553 if (strv_isempty(l))
1554 c->syscall_archs = set_free(c->syscall_archs);
1555 else {
1556 char **s;
1557
1558 r = set_ensure_allocated(&c->syscall_archs, NULL);
1559 if (r < 0)
1560 return r;
1561
1562 STRV_FOREACH(s, l) {
1563 uint32_t a;
1564
1565 r = seccomp_arch_from_string(*s, &a);
1566 if (r < 0)
1567 return r;
1568
1569 r = set_put(c->syscall_archs, UINT32_TO_PTR(a + 1));
1570 if (r < 0)
1571 return r;
1572 }
1573
1574 }
1575
1576 joined = strv_join(l, " ");
1577 if (!joined)
1578 return -ENOMEM;
1579
1580 unit_write_settingf(u, flags, name, "%s=%s", name, joined);
1581 }
1582
1583 return 1;
1584
1585 } else if (streq(name, "RestrictAddressFamilies")) {
1586 int whitelist;
1587 _cleanup_strv_free_ char **l = NULL;
1588
1589 r = sd_bus_message_enter_container(message, 'r', "bas");
1590 if (r < 0)
1591 return r;
1592
1593 r = sd_bus_message_read(message, "b", &whitelist);
1594 if (r < 0)
1595 return r;
1596
1597 r = sd_bus_message_read_strv(message, &l);
1598 if (r < 0)
1599 return r;
1600
1601 r = sd_bus_message_exit_container(message);
1602 if (r < 0)
1603 return r;
1604
1605 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1606 _cleanup_free_ char *joined = NULL;
1607 char **s;
1608
1609 if (strv_isempty(l)) {
1610 c->address_families_whitelist = false;
1611 c->address_families = set_free(c->address_families);
1612
1613 unit_write_settingf(u, flags, name, "RestrictAddressFamilies=");
1614 return 1;
1615 }
1616
1617 if (!c->address_families) {
1618 c->address_families = set_new(NULL);
1619 if (!c->address_families)
1620 return log_oom();
1621
1622 c->address_families_whitelist = whitelist;
1623 }
1624
1625 STRV_FOREACH(s, l) {
1626 int af;
1627
1628 af = af_from_name(*s);
1629 if (af < 0)
1630 return af;
1631
1632 if (whitelist == c->address_families_whitelist) {
1633 r = set_put(c->address_families, INT_TO_PTR(af));
1634 if (r < 0)
1635 return r;
1636 } else
1637 (void) set_remove(c->address_families, INT_TO_PTR(af));
1638 }
1639
1640 joined = strv_join(l, " ");
1641 if (!joined)
1642 return -ENOMEM;
1643
1644 unit_write_settingf(u, flags, name, "RestrictAddressFamilies=%s%s", whitelist ? "" : "~", joined);
1645 }
1646
1647 return 1;
1648 }
1649 #endif
1650 if (streq(name, "CPUAffinity")) {
1651 const void *a;
1652 size_t n;
1653 _cleanup_(cpu_set_reset) CPUSet set = {};
1654
1655 r = sd_bus_message_read_array(message, 'y', &a, &n);
1656 if (r < 0)
1657 return r;
1658
1659 r = cpu_set_from_dbus(a, n, &set);
1660 if (r < 0)
1661 return r;
1662
1663 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1664 if (n == 0) {
1665 cpu_set_reset(&c->cpu_set);
1666 unit_write_settingf(u, flags, name, "%s=", name);
1667 } else {
1668 _cleanup_free_ char *str = NULL;
1669
1670 str = cpu_set_to_string(&set);
1671 if (!str)
1672 return -ENOMEM;
1673
1674 /* We forego any optimizations here, and always create the structure using
1675 * cpu_set_add_all(), because we don't want to care if the existing size we
1676 * got over dbus is appropriate. */
1677 r = cpu_set_add_all(&c->cpu_set, &set);
1678 if (r < 0)
1679 return r;
1680
1681 unit_write_settingf(u, flags, name, "%s=%s", name, str);
1682 }
1683 }
1684
1685 return 1;
1686
1687 } else if (streq(name, "Nice")) {
1688 int32_t q;
1689
1690 r = sd_bus_message_read(message, "i", &q);
1691 if (r < 0)
1692 return r;
1693
1694 if (!nice_is_valid(q))
1695 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid Nice value: %i", q);
1696
1697 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1698 c->nice = q;
1699 c->nice_set = true;
1700
1701 unit_write_settingf(u, flags, name, "Nice=%i", q);
1702 }
1703
1704 return 1;
1705
1706 } else if (streq(name, "CPUSchedulingPolicy")) {
1707 int32_t q;
1708
1709 r = sd_bus_message_read(message, "i", &q);
1710 if (r < 0)
1711 return r;
1712
1713 if (!sched_policy_is_valid(q))
1714 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid CPU scheduling policy: %i", q);
1715
1716 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1717 _cleanup_free_ char *s = NULL;
1718
1719 r = sched_policy_to_string_alloc(q, &s);
1720 if (r < 0)
1721 return r;
1722
1723 c->cpu_sched_policy = q;
1724 c->cpu_sched_priority = CLAMP(c->cpu_sched_priority, sched_get_priority_min(q), sched_get_priority_max(q));
1725 c->cpu_sched_set = true;
1726
1727 unit_write_settingf(u, flags, name, "CPUSchedulingPolicy=%s", s);
1728 }
1729
1730 return 1;
1731
1732 } else if (streq(name, "CPUSchedulingPriority")) {
1733 int32_t p, min, max;
1734
1735 r = sd_bus_message_read(message, "i", &p);
1736 if (r < 0)
1737 return r;
1738
1739 min = sched_get_priority_min(c->cpu_sched_policy);
1740 max = sched_get_priority_max(c->cpu_sched_policy);
1741 if (p < min || p > max)
1742 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid CPU scheduling priority: %i", p);
1743
1744 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1745 c->cpu_sched_priority = p;
1746 c->cpu_sched_set = true;
1747
1748 unit_write_settingf(u, flags, name, "CPUSchedulingPriority=%i", p);
1749 }
1750
1751 return 1;
1752
1753 } else if (streq(name, "IOSchedulingClass")) {
1754 int32_t q;
1755
1756 r = sd_bus_message_read(message, "i", &q);
1757 if (r < 0)
1758 return r;
1759
1760 if (!ioprio_class_is_valid(q))
1761 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid IO scheduling class: %i", q);
1762
1763 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1764 _cleanup_free_ char *s = NULL;
1765
1766 r = ioprio_class_to_string_alloc(q, &s);
1767 if (r < 0)
1768 return r;
1769
1770 c->ioprio = IOPRIO_PRIO_VALUE(q, IOPRIO_PRIO_DATA(c->ioprio));
1771 c->ioprio_set = true;
1772
1773 unit_write_settingf(u, flags, name, "IOSchedulingClass=%s", s);
1774 }
1775
1776 return 1;
1777
1778 } else if (streq(name, "IOSchedulingPriority")) {
1779 int32_t p;
1780
1781 r = sd_bus_message_read(message, "i", &p);
1782 if (r < 0)
1783 return r;
1784
1785 if (!ioprio_priority_is_valid(p))
1786 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid IO scheduling priority: %i", p);
1787
1788 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1789 c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_PRIO_CLASS(c->ioprio), p);
1790 c->ioprio_set = true;
1791
1792 unit_write_settingf(u, flags, name, "IOSchedulingPriority=%i", p);
1793 }
1794
1795 return 1;
1796
1797 } else if (streq(name, "WorkingDirectory")) {
1798 const char *s;
1799 bool missing_ok;
1800
1801 r = sd_bus_message_read(message, "s", &s);
1802 if (r < 0)
1803 return r;
1804
1805 if (s[0] == '-') {
1806 missing_ok = true;
1807 s++;
1808 } else
1809 missing_ok = false;
1810
1811 if (!isempty(s) && !streq(s, "~") && !path_is_absolute(s))
1812 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "WorkingDirectory= expects an absolute path or '~'");
1813
1814 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1815 if (streq(s, "~")) {
1816 c->working_directory = mfree(c->working_directory);
1817 c->working_directory_home = true;
1818 } else {
1819 r = free_and_strdup(&c->working_directory, empty_to_null(s));
1820 if (r < 0)
1821 return r;
1822
1823 c->working_directory_home = false;
1824 }
1825
1826 c->working_directory_missing_ok = missing_ok;
1827 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "WorkingDirectory=%s%s", missing_ok ? "-" : "", s);
1828 }
1829
1830 return 1;
1831
1832 } else if (STR_IN_SET(name,
1833 "StandardInputFileDescriptorName", "StandardOutputFileDescriptorName", "StandardErrorFileDescriptorName")) {
1834 const char *s;
1835
1836 r = sd_bus_message_read(message, "s", &s);
1837 if (r < 0)
1838 return r;
1839
1840 if (!isempty(s) && !fdname_is_valid(s))
1841 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid file descriptor name");
1842
1843 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1844
1845 if (streq(name, "StandardInputFileDescriptorName")) {
1846 r = free_and_strdup(c->stdio_fdname + STDIN_FILENO, empty_to_null(s));
1847 if (r < 0)
1848 return r;
1849
1850 c->std_input = EXEC_INPUT_NAMED_FD;
1851 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "StandardInput=fd:%s", exec_context_fdname(c, STDIN_FILENO));
1852
1853 } else if (streq(name, "StandardOutputFileDescriptorName")) {
1854 r = free_and_strdup(c->stdio_fdname + STDOUT_FILENO, empty_to_null(s));
1855 if (r < 0)
1856 return r;
1857
1858 c->std_output = EXEC_OUTPUT_NAMED_FD;
1859 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "StandardOutput=fd:%s", exec_context_fdname(c, STDOUT_FILENO));
1860
1861 } else {
1862 assert(streq(name, "StandardErrorFileDescriptorName"));
1863
1864 r = free_and_strdup(&c->stdio_fdname[STDERR_FILENO], empty_to_null(s));
1865 if (r < 0)
1866 return r;
1867
1868 c->std_error = EXEC_OUTPUT_NAMED_FD;
1869 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "StandardError=fd:%s", exec_context_fdname(c, STDERR_FILENO));
1870 }
1871 }
1872
1873 return 1;
1874
1875 } else if (STR_IN_SET(name,
1876 "StandardInputFile",
1877 "StandardOutputFile", "StandardOutputFileToAppend",
1878 "StandardErrorFile", "StandardErrorFileToAppend")) {
1879 const char *s;
1880
1881 r = sd_bus_message_read(message, "s", &s);
1882 if (r < 0)
1883 return r;
1884
1885 if (!isempty(s)) {
1886 if (!path_is_absolute(s))
1887 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path %s is not absolute", s);
1888 if (!path_is_normalized(s))
1889 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path %s is not normalized", s);
1890 }
1891
1892 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1893
1894 if (streq(name, "StandardInputFile")) {
1895 r = free_and_strdup(&c->stdio_file[STDIN_FILENO], empty_to_null(s));
1896 if (r < 0)
1897 return r;
1898
1899 c->std_input = EXEC_INPUT_FILE;
1900 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "StandardInput=file:%s", s);
1901
1902 } else if (STR_IN_SET(name, "StandardOutputFile", "StandardOutputFileToAppend")) {
1903 r = free_and_strdup(&c->stdio_file[STDOUT_FILENO], empty_to_null(s));
1904 if (r < 0)
1905 return r;
1906
1907 if (streq(name, "StandardOutputFile")) {
1908 c->std_output = EXEC_OUTPUT_FILE;
1909 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "StandardOutput=file:%s", s);
1910 } else {
1911 assert(streq(name, "StandardOutputFileToAppend"));
1912 c->std_output = EXEC_OUTPUT_FILE_APPEND;
1913 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "StandardOutput=append:%s", s);
1914 }
1915 } else {
1916 assert(STR_IN_SET(name, "StandardErrorFile", "StandardErrorFileToAppend"));
1917
1918 r = free_and_strdup(&c->stdio_file[STDERR_FILENO], empty_to_null(s));
1919 if (r < 0)
1920 return r;
1921
1922 if (streq(name, "StandardErrorFile")) {
1923 c->std_error = EXEC_OUTPUT_FILE;
1924 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "StandardError=file:%s", s);
1925 } else {
1926 assert(streq(name, "StandardErrorFileToAppend"));
1927 c->std_error = EXEC_OUTPUT_FILE_APPEND;
1928 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "StandardError=append:%s", s);
1929 }
1930 }
1931 }
1932
1933 return 1;
1934
1935 } else if (streq(name, "StandardInputData")) {
1936 const void *p;
1937 size_t sz;
1938
1939 r = sd_bus_message_read_array(message, 'y', &p, &sz);
1940 if (r < 0)
1941 return r;
1942
1943 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1944 _cleanup_free_ char *encoded = NULL;
1945
1946 if (sz == 0) {
1947 c->stdin_data = mfree(c->stdin_data);
1948 c->stdin_data_size = 0;
1949
1950 unit_write_settingf(u, flags, name, "StandardInputData=");
1951 } else {
1952 void *q;
1953 ssize_t n;
1954
1955 if (c->stdin_data_size + sz < c->stdin_data_size || /* check for overflow */
1956 c->stdin_data_size + sz > EXEC_STDIN_DATA_MAX)
1957 return -E2BIG;
1958
1959 n = base64mem(p, sz, &encoded);
1960 if (n < 0)
1961 return (int) n;
1962
1963 q = realloc(c->stdin_data, c->stdin_data_size + sz);
1964 if (!q)
1965 return -ENOMEM;
1966
1967 memcpy((uint8_t*) q + c->stdin_data_size, p, sz);
1968
1969 c->stdin_data = q;
1970 c->stdin_data_size += sz;
1971
1972 unit_write_settingf(u, flags, name, "StandardInputData=%s", encoded);
1973 }
1974 }
1975
1976 return 1;
1977
1978 } else if (streq(name, "Environment")) {
1979
1980 _cleanup_strv_free_ char **l = NULL;
1981
1982 r = sd_bus_message_read_strv(message, &l);
1983 if (r < 0)
1984 return r;
1985
1986 if (!strv_env_is_valid(l))
1987 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid environment block.");
1988
1989 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1990 if (strv_isempty(l)) {
1991 c->environment = strv_free(c->environment);
1992 unit_write_setting(u, flags, name, "Environment=");
1993 } else {
1994 _cleanup_free_ char *joined = NULL;
1995 char **e;
1996
1997 joined = unit_concat_strv(l, UNIT_ESCAPE_SPECIFIERS|UNIT_ESCAPE_C);
1998 if (!joined)
1999 return -ENOMEM;
2000
2001 e = strv_env_merge(2, c->environment, l);
2002 if (!e)
2003 return -ENOMEM;
2004
2005 strv_free_and_replace(c->environment, e);
2006 unit_write_settingf(u, flags, name, "Environment=%s", joined);
2007 }
2008 }
2009
2010 return 1;
2011
2012 } else if (streq(name, "UnsetEnvironment")) {
2013
2014 _cleanup_strv_free_ char **l = NULL;
2015
2016 r = sd_bus_message_read_strv(message, &l);
2017 if (r < 0)
2018 return r;
2019
2020 if (!strv_env_name_or_assignment_is_valid(l))
2021 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid UnsetEnvironment= list.");
2022
2023 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2024 if (strv_isempty(l)) {
2025 c->unset_environment = strv_free(c->unset_environment);
2026 unit_write_setting(u, flags, name, "UnsetEnvironment=");
2027 } else {
2028 _cleanup_free_ char *joined = NULL;
2029 char **e;
2030
2031 joined = unit_concat_strv(l, UNIT_ESCAPE_SPECIFIERS|UNIT_ESCAPE_C);
2032 if (!joined)
2033 return -ENOMEM;
2034
2035 e = strv_env_merge(2, c->unset_environment, l);
2036 if (!e)
2037 return -ENOMEM;
2038
2039 strv_free_and_replace(c->unset_environment, e);
2040 unit_write_settingf(u, flags, name, "UnsetEnvironment=%s", joined);
2041 }
2042 }
2043
2044 return 1;
2045
2046 } else if (streq(name, "OOMScoreAdjust")) {
2047 int oa;
2048
2049 r = sd_bus_message_read(message, "i", &oa);
2050 if (r < 0)
2051 return r;
2052
2053 if (!oom_score_adjust_is_valid(oa))
2054 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "OOM score adjust value out of range");
2055
2056 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2057 c->oom_score_adjust = oa;
2058 c->oom_score_adjust_set = true;
2059 unit_write_settingf(u, flags, name, "OOMScoreAdjust=%i", oa);
2060 }
2061
2062 return 1;
2063
2064 } else if (streq(name, "EnvironmentFiles")) {
2065
2066 _cleanup_free_ char *joined = NULL;
2067 _cleanup_fclose_ FILE *f = NULL;
2068 _cleanup_strv_free_ char **l = NULL;
2069 size_t size = 0;
2070 char **i;
2071
2072 r = sd_bus_message_enter_container(message, 'a', "(sb)");
2073 if (r < 0)
2074 return r;
2075
2076 f = open_memstream_unlocked(&joined, &size);
2077 if (!f)
2078 return -ENOMEM;
2079
2080 fputs("EnvironmentFile=\n", f);
2081
2082 STRV_FOREACH(i, c->environment_files) {
2083 _cleanup_free_ char *q = NULL;
2084
2085 q = specifier_escape(*i);
2086 if (!q)
2087 return -ENOMEM;
2088
2089 fprintf(f, "EnvironmentFile=%s\n", q);
2090 }
2091
2092 while ((r = sd_bus_message_enter_container(message, 'r', "sb")) > 0) {
2093 const char *path;
2094 int b;
2095
2096 r = sd_bus_message_read(message, "sb", &path, &b);
2097 if (r < 0)
2098 return r;
2099
2100 r = sd_bus_message_exit_container(message);
2101 if (r < 0)
2102 return r;
2103
2104 if (!path_is_absolute(path))
2105 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path %s is not absolute.", path);
2106
2107 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2108 _cleanup_free_ char *q = NULL;
2109 char *buf;
2110
2111 buf = strjoin(b ? "-" : "", path);
2112 if (!buf)
2113 return -ENOMEM;
2114
2115 q = specifier_escape(buf);
2116 if (!q) {
2117 free(buf);
2118 return -ENOMEM;
2119 }
2120
2121 fprintf(f, "EnvironmentFile=%s\n", q);
2122
2123 r = strv_consume(&l, buf);
2124 if (r < 0)
2125 return r;
2126 }
2127 }
2128 if (r < 0)
2129 return r;
2130
2131 r = sd_bus_message_exit_container(message);
2132 if (r < 0)
2133 return r;
2134
2135 r = fflush_and_check(f);
2136 if (r < 0)
2137 return r;
2138
2139 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2140 if (strv_isempty(l)) {
2141 c->environment_files = strv_free(c->environment_files);
2142 unit_write_setting(u, flags, name, "EnvironmentFile=");
2143 } else {
2144 r = strv_extend_strv(&c->environment_files, l, true);
2145 if (r < 0)
2146 return r;
2147
2148 unit_write_setting(u, flags, name, joined);
2149 }
2150 }
2151
2152 return 1;
2153
2154 } else if (streq(name, "PassEnvironment")) {
2155
2156 _cleanup_strv_free_ char **l = NULL;
2157
2158 r = sd_bus_message_read_strv(message, &l);
2159 if (r < 0)
2160 return r;
2161
2162 if (!strv_env_name_is_valid(l))
2163 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid PassEnvironment= block.");
2164
2165 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2166 if (strv_isempty(l)) {
2167 c->pass_environment = strv_free(c->pass_environment);
2168 unit_write_setting(u, flags, name, "PassEnvironment=");
2169 } else {
2170 _cleanup_free_ char *joined = NULL;
2171
2172 r = strv_extend_strv(&c->pass_environment, l, true);
2173 if (r < 0)
2174 return r;
2175
2176 /* We write just the new settings out to file, with unresolved specifiers. */
2177 joined = unit_concat_strv(l, UNIT_ESCAPE_SPECIFIERS);
2178 if (!joined)
2179 return -ENOMEM;
2180
2181 unit_write_settingf(u, flags, name, "PassEnvironment=%s", joined);
2182 }
2183 }
2184
2185 return 1;
2186
2187 } else if (STR_IN_SET(name, "ReadWriteDirectories", "ReadOnlyDirectories", "InaccessibleDirectories",
2188 "ReadWritePaths", "ReadOnlyPaths", "InaccessiblePaths")) {
2189 _cleanup_strv_free_ char **l = NULL;
2190 char ***dirs;
2191 char **p;
2192
2193 r = sd_bus_message_read_strv(message, &l);
2194 if (r < 0)
2195 return r;
2196
2197 STRV_FOREACH(p, l) {
2198 char *i = *p;
2199 size_t offset;
2200
2201 offset = i[0] == '-';
2202 offset += i[offset] == '+';
2203 if (!path_is_absolute(i + offset))
2204 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid %s", name);
2205
2206 path_simplify(i + offset, false);
2207 }
2208
2209 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2210 if (STR_IN_SET(name, "ReadWriteDirectories", "ReadWritePaths"))
2211 dirs = &c->read_write_paths;
2212 else if (STR_IN_SET(name, "ReadOnlyDirectories", "ReadOnlyPaths"))
2213 dirs = &c->read_only_paths;
2214 else /* "InaccessiblePaths" */
2215 dirs = &c->inaccessible_paths;
2216
2217 if (strv_isempty(l)) {
2218 *dirs = strv_free(*dirs);
2219 unit_write_settingf(u, flags, name, "%s=", name);
2220 } else {
2221 _cleanup_free_ char *joined = NULL;
2222
2223 joined = unit_concat_strv(l, UNIT_ESCAPE_SPECIFIERS);
2224 if (!joined)
2225 return -ENOMEM;
2226
2227 r = strv_extend_strv(dirs, l, true);
2228 if (r < 0)
2229 return -ENOMEM;
2230
2231 unit_write_settingf(u, flags, name, "%s=%s", name, joined);
2232 }
2233 }
2234
2235 return 1;
2236
2237 } else if (STR_IN_SET(name, "RuntimeDirectory", "StateDirectory", "CacheDirectory", "LogsDirectory", "ConfigurationDirectory")) {
2238 _cleanup_strv_free_ char **l = NULL;
2239 char **p;
2240
2241 r = sd_bus_message_read_strv(message, &l);
2242 if (r < 0)
2243 return r;
2244
2245 STRV_FOREACH(p, l) {
2246 if (!path_is_normalized(*p))
2247 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "%s= path is not normalized: %s", name, *p);
2248
2249 if (path_is_absolute(*p))
2250 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "%s= path is absolute: %s", name, *p);
2251
2252 if (path_startswith(*p, "private"))
2253 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "%s= path can't be 'private': %s", name, *p);
2254 }
2255
2256 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2257 ExecDirectoryType i;
2258 ExecDirectory *d;
2259
2260 assert_se((i = exec_directory_type_from_string(name)) >= 0);
2261 d = c->directories + i;
2262
2263 if (strv_isempty(l)) {
2264 d->paths = strv_free(d->paths);
2265 unit_write_settingf(u, flags, name, "%s=", name);
2266 } else {
2267 _cleanup_free_ char *joined = NULL;
2268
2269 r = strv_extend_strv(&d->paths, l, true);
2270 if (r < 0)
2271 return r;
2272
2273 joined = unit_concat_strv(l, UNIT_ESCAPE_SPECIFIERS);
2274 if (!joined)
2275 return -ENOMEM;
2276
2277 unit_write_settingf(u, flags, name, "%s=%s", name, joined);
2278 }
2279 }
2280
2281 return 1;
2282
2283 } else if (STR_IN_SET(name, "AppArmorProfile", "SmackProcessLabel")) {
2284 int ignore;
2285 const char *s;
2286
2287 r = sd_bus_message_read(message, "(bs)", &ignore, &s);
2288 if (r < 0)
2289 return r;
2290
2291 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2292 char **p;
2293 bool *b;
2294
2295 if (streq(name, "AppArmorProfile")) {
2296 p = &c->apparmor_profile;
2297 b = &c->apparmor_profile_ignore;
2298 } else { /* "SmackProcessLabel" */
2299 p = &c->smack_process_label;
2300 b = &c->smack_process_label_ignore;
2301 }
2302
2303 if (isempty(s)) {
2304 *p = mfree(*p);
2305 *b = false;
2306 } else {
2307 if (free_and_strdup(p, s) < 0)
2308 return -ENOMEM;
2309 *b = ignore;
2310 }
2311
2312 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "%s=%s%s", name, ignore ? "-" : "", strempty(s));
2313 }
2314
2315 return 1;
2316
2317 } else if (STR_IN_SET(name, "BindPaths", "BindReadOnlyPaths")) {
2318 const char *source, *destination;
2319 int ignore_enoent;
2320 uint64_t mount_flags;
2321 bool empty = true;
2322
2323 r = sd_bus_message_enter_container(message, 'a', "(ssbt)");
2324 if (r < 0)
2325 return r;
2326
2327 while ((r = sd_bus_message_read(message, "(ssbt)", &source, &destination, &ignore_enoent, &mount_flags)) > 0) {
2328
2329 if (!path_is_absolute(source))
2330 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Source path %s is not absolute.", source);
2331 if (!path_is_absolute(destination))
2332 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Destination path %s is not absolute.", destination);
2333 if (!IN_SET(mount_flags, 0, MS_REC))
2334 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown mount flags.");
2335
2336 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2337 r = bind_mount_add(&c->bind_mounts, &c->n_bind_mounts,
2338 &(BindMount) {
2339 .source = strdup(source),
2340 .destination = strdup(destination),
2341 .read_only = !!strstr(name, "ReadOnly"),
2342 .recursive = !!(mount_flags & MS_REC),
2343 .ignore_enoent = ignore_enoent,
2344 });
2345 if (r < 0)
2346 return r;
2347
2348 unit_write_settingf(
2349 u, flags|UNIT_ESCAPE_SPECIFIERS, name,
2350 "%s=%s%s:%s:%s",
2351 name,
2352 ignore_enoent ? "-" : "",
2353 source,
2354 destination,
2355 (mount_flags & MS_REC) ? "rbind" : "norbind");
2356 }
2357
2358 empty = false;
2359 }
2360 if (r < 0)
2361 return r;
2362
2363 r = sd_bus_message_exit_container(message);
2364 if (r < 0)
2365 return r;
2366
2367 if (empty) {
2368 bind_mount_free_many(c->bind_mounts, c->n_bind_mounts);
2369 c->bind_mounts = NULL;
2370 c->n_bind_mounts = 0;
2371
2372 unit_write_settingf(u, flags, name, "%s=", name);
2373 }
2374
2375 return 1;
2376
2377 } else if (streq(name, "TemporaryFileSystem")) {
2378 const char *path, *options;
2379 bool empty = true;
2380
2381 r = sd_bus_message_enter_container(message, 'a', "(ss)");
2382 if (r < 0)
2383 return r;
2384
2385 while ((r = sd_bus_message_read(message, "(ss)", &path, &options)) > 0) {
2386
2387 if (!path_is_absolute(path))
2388 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Mount point %s is not absolute.", path);
2389
2390 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2391 r = temporary_filesystem_add(&c->temporary_filesystems, &c->n_temporary_filesystems, path, options);
2392 if (r < 0)
2393 return r;
2394
2395 unit_write_settingf(
2396 u, flags|UNIT_ESCAPE_SPECIFIERS, name,
2397 "%s=%s:%s",
2398 name,
2399 path,
2400 options);
2401 }
2402
2403 empty = false;
2404 }
2405 if (r < 0)
2406 return r;
2407
2408 r = sd_bus_message_exit_container(message);
2409 if (r < 0)
2410 return r;
2411
2412 if (empty) {
2413 temporary_filesystem_free_many(c->temporary_filesystems, c->n_temporary_filesystems);
2414 c->temporary_filesystems = NULL;
2415 c->n_temporary_filesystems = 0;
2416
2417 unit_write_settingf(u, flags, name, "%s=", name);
2418 }
2419
2420 return 1;
2421
2422 } else if ((suffix = startswith(name, "Limit"))) {
2423 const char *soft = NULL;
2424 int ri;
2425
2426 ri = rlimit_from_string(suffix);
2427 if (ri < 0) {
2428 soft = endswith(suffix, "Soft");
2429 if (soft) {
2430 const char *n;
2431
2432 n = strndupa(suffix, soft - suffix);
2433 ri = rlimit_from_string(n);
2434 if (ri >= 0)
2435 name = strjoina("Limit", n);
2436 }
2437 }
2438
2439 if (ri >= 0) {
2440 uint64_t rl;
2441 rlim_t x;
2442
2443 r = sd_bus_message_read(message, "t", &rl);
2444 if (r < 0)
2445 return r;
2446
2447 if (rl == (uint64_t) -1)
2448 x = RLIM_INFINITY;
2449 else {
2450 x = (rlim_t) rl;
2451
2452 if ((uint64_t) x != rl)
2453 return -ERANGE;
2454 }
2455
2456 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2457 _cleanup_free_ char *f = NULL;
2458 struct rlimit nl;
2459
2460 if (c->rlimit[ri]) {
2461 nl = *c->rlimit[ri];
2462
2463 if (soft)
2464 nl.rlim_cur = x;
2465 else
2466 nl.rlim_max = x;
2467 } else
2468 /* When the resource limit is not initialized yet, then assign the value to both fields */
2469 nl = (struct rlimit) {
2470 .rlim_cur = x,
2471 .rlim_max = x,
2472 };
2473
2474 r = rlimit_format(&nl, &f);
2475 if (r < 0)
2476 return r;
2477
2478 if (c->rlimit[ri])
2479 *c->rlimit[ri] = nl;
2480 else {
2481 c->rlimit[ri] = newdup(struct rlimit, &nl, 1);
2482 if (!c->rlimit[ri])
2483 return -ENOMEM;
2484 }
2485
2486 unit_write_settingf(u, flags, name, "%s=%s", name, f);
2487 }
2488
2489 return 1;
2490 }
2491
2492 }
2493
2494 return 0;
2495 }