]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/dbus-execute.c
Add open_memstream_unlocked() wrapper
[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->cpuset, CPU_ALLOC_SIZE(c->cpuset_ncpus));
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 int bus_property_get_exec_command(
830 sd_bus *bus,
831 const char *path,
832 const char *interface,
833 const char *property,
834 sd_bus_message *reply,
835 void *userdata,
836 sd_bus_error *ret_error) {
837
838 ExecCommand *c = (ExecCommand*) userdata;
839 int r;
840
841 assert(bus);
842 assert(reply);
843
844 r = sd_bus_message_open_container(reply, 'a', "(sasbttttuii)");
845 if (r < 0)
846 return r;
847
848 r = append_exec_command(reply, c);
849 if (r < 0)
850 return r;
851
852 return sd_bus_message_close_container(reply);
853 }
854
855 int bus_property_get_exec_command_list(
856 sd_bus *bus,
857 const char *path,
858 const char *interface,
859 const char *property,
860 sd_bus_message *reply,
861 void *userdata,
862 sd_bus_error *ret_error) {
863
864 ExecCommand *c = *(ExecCommand**) userdata;
865 int r;
866
867 assert(bus);
868 assert(reply);
869
870 r = sd_bus_message_open_container(reply, 'a', "(sasbttttuii)");
871 if (r < 0)
872 return r;
873
874 LIST_FOREACH(command, c, c) {
875 r = append_exec_command(reply, c);
876 if (r < 0)
877 return r;
878 }
879
880 return sd_bus_message_close_container(reply);
881 }
882
883 int bus_set_transient_exec_command(
884 Unit *u,
885 const char *name,
886 ExecCommand **exec_command,
887 sd_bus_message *message,
888 UnitWriteFlags flags,
889 sd_bus_error *error) {
890 unsigned n = 0;
891 int r;
892
893 r = sd_bus_message_enter_container(message, 'a', "(sasb)");
894 if (r < 0)
895 return r;
896
897 while ((r = sd_bus_message_enter_container(message, 'r', "sasb")) > 0) {
898 _cleanup_strv_free_ char **argv = NULL;
899 const char *path;
900 int b;
901
902 r = sd_bus_message_read(message, "s", &path);
903 if (r < 0)
904 return r;
905
906 if (!path_is_absolute(path))
907 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path %s is not absolute.", path);
908
909 r = sd_bus_message_read_strv(message, &argv);
910 if (r < 0)
911 return r;
912
913 r = sd_bus_message_read(message, "b", &b);
914 if (r < 0)
915 return r;
916
917 r = sd_bus_message_exit_container(message);
918 if (r < 0)
919 return r;
920
921 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
922 ExecCommand *c;
923
924 c = new0(ExecCommand, 1);
925 if (!c)
926 return -ENOMEM;
927
928 c->path = strdup(path);
929 if (!c->path) {
930 free(c);
931 return -ENOMEM;
932 }
933
934 c->argv = TAKE_PTR(argv);
935
936 c->flags = b ? EXEC_COMMAND_IGNORE_FAILURE : 0;
937
938 path_simplify(c->path, false);
939 exec_command_append_list(exec_command, c);
940 }
941
942 n++;
943 }
944 if (r < 0)
945 return r;
946
947 r = sd_bus_message_exit_container(message);
948 if (r < 0)
949 return r;
950
951 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
952 _cleanup_free_ char *buf = NULL;
953 _cleanup_fclose_ FILE *f = NULL;
954 ExecCommand *c;
955 size_t size = 0;
956
957 if (n == 0)
958 *exec_command = exec_command_free_list(*exec_command);
959
960 f = open_memstream_unlocked(&buf, &size);
961 if (!f)
962 return -ENOMEM;
963
964 fputs("ExecStart=\n", f);
965
966 LIST_FOREACH(command, c, *exec_command) {
967 _cleanup_free_ char *a = NULL, *t = NULL;
968 const char *p;
969
970 p = unit_escape_setting(c->path, UNIT_ESCAPE_C|UNIT_ESCAPE_SPECIFIERS, &t);
971 if (!p)
972 return -ENOMEM;
973
974 a = unit_concat_strv(c->argv, UNIT_ESCAPE_C|UNIT_ESCAPE_SPECIFIERS);
975 if (!a)
976 return -ENOMEM;
977
978 fprintf(f, "%s=%s@%s %s\n",
979 name,
980 c->flags & EXEC_COMMAND_IGNORE_FAILURE ? "-" : "",
981 p,
982 a);
983 }
984
985 r = fflush_and_check(f);
986 if (r < 0)
987 return r;
988
989 unit_write_setting(u, flags, name, buf);
990 }
991
992 return 1;
993 }
994
995 static int parse_personality(const char *s, unsigned long *p) {
996 unsigned long v;
997
998 assert(p);
999
1000 v = personality_from_string(s);
1001 if (v == PERSONALITY_INVALID)
1002 return -EINVAL;
1003
1004 *p = v;
1005 return 0;
1006 }
1007
1008 static const char* mount_propagation_flags_to_string_with_check(unsigned long n) {
1009 if (!IN_SET(n, 0, MS_SHARED, MS_PRIVATE, MS_SLAVE))
1010 return NULL;
1011
1012 return mount_propagation_flags_to_string(n);
1013 }
1014
1015 static BUS_DEFINE_SET_TRANSIENT(nsec, "t", uint64_t, nsec_t, NSEC_FMT);
1016 static BUS_DEFINE_SET_TRANSIENT_IS_VALID(log_level, "i", int32_t, int, "%" PRIi32, log_level_is_valid);
1017 #if HAVE_SECCOMP
1018 static BUS_DEFINE_SET_TRANSIENT_IS_VALID(errno, "i", int32_t, int, "%" PRIi32, errno_is_valid);
1019 #endif
1020 static BUS_DEFINE_SET_TRANSIENT_PARSE(std_input, ExecInput, exec_input_from_string);
1021 static BUS_DEFINE_SET_TRANSIENT_PARSE(std_output, ExecOutput, exec_output_from_string);
1022 static BUS_DEFINE_SET_TRANSIENT_PARSE(utmp_mode, ExecUtmpMode, exec_utmp_mode_from_string);
1023 static BUS_DEFINE_SET_TRANSIENT_PARSE(protect_system, ProtectSystem, protect_system_from_string);
1024 static BUS_DEFINE_SET_TRANSIENT_PARSE(protect_home, ProtectHome, protect_home_from_string);
1025 static BUS_DEFINE_SET_TRANSIENT_PARSE(keyring_mode, ExecKeyringMode, exec_keyring_mode_from_string);
1026 static BUS_DEFINE_SET_TRANSIENT_PARSE(preserve_mode, ExecPreserveMode, exec_preserve_mode_from_string);
1027 static BUS_DEFINE_SET_TRANSIENT_PARSE_PTR(personality, unsigned long, parse_personality);
1028 static BUS_DEFINE_SET_TRANSIENT_TO_STRING_ALLOC(secure_bits, "i", int32_t, int, "%" PRIi32, secure_bits_to_string_alloc_with_check);
1029 static BUS_DEFINE_SET_TRANSIENT_TO_STRING_ALLOC(capability, "t", uint64_t, uint64_t, "%" PRIu64, capability_set_to_string_alloc);
1030 static BUS_DEFINE_SET_TRANSIENT_TO_STRING_ALLOC(namespace_flag, "t", uint64_t, unsigned long, "%" PRIu64, namespace_flags_to_string);
1031 static BUS_DEFINE_SET_TRANSIENT_TO_STRING(mount_flags, "t", uint64_t, unsigned long, "%" PRIu64, mount_propagation_flags_to_string_with_check);
1032
1033 int bus_exec_context_set_transient_property(
1034 Unit *u,
1035 ExecContext *c,
1036 const char *name,
1037 sd_bus_message *message,
1038 UnitWriteFlags flags,
1039 sd_bus_error *error) {
1040
1041 const char *suffix;
1042 int r;
1043
1044 assert(u);
1045 assert(c);
1046 assert(name);
1047 assert(message);
1048
1049 flags |= UNIT_PRIVATE;
1050
1051 if (streq(name, "User"))
1052 return bus_set_transient_user(u, name, &c->user, message, flags, error);
1053
1054 if (streq(name, "Group"))
1055 return bus_set_transient_user(u, name, &c->group, message, flags, error);
1056
1057 if (streq(name, "TTYPath"))
1058 return bus_set_transient_path(u, name, &c->tty_path, message, flags, error);
1059
1060 if (streq(name, "RootImage"))
1061 return bus_set_transient_path(u, name, &c->root_image, message, flags, error);
1062
1063 if (streq(name, "RootDirectory"))
1064 return bus_set_transient_path(u, name, &c->root_directory, message, flags, error);
1065
1066 if (streq(name, "SyslogIdentifier"))
1067 return bus_set_transient_string(u, name, &c->syslog_identifier, message, flags, error);
1068
1069 if (streq(name, "LogLevelMax"))
1070 return bus_set_transient_log_level(u, name, &c->log_level_max, message, flags, error);
1071
1072 if (streq(name, "LogRateLimitIntervalUSec"))
1073 return bus_set_transient_usec(u, name, &c->log_rate_limit_interval_usec, message, flags, error);
1074
1075 if (streq(name, "LogRateLimitBurst"))
1076 return bus_set_transient_unsigned(u, name, &c->log_rate_limit_burst, message, flags, error);
1077
1078 if (streq(name, "Personality"))
1079 return bus_set_transient_personality(u, name, &c->personality, message, flags, error);
1080
1081 if (streq(name, "StandardInput"))
1082 return bus_set_transient_std_input(u, name, &c->std_input, message, flags, error);
1083
1084 if (streq(name, "StandardOutput"))
1085 return bus_set_transient_std_output(u, name, &c->std_output, message, flags, error);
1086
1087 if (streq(name, "StandardError"))
1088 return bus_set_transient_std_output(u, name, &c->std_error, message, flags, error);
1089
1090 if (streq(name, "IgnoreSIGPIPE"))
1091 return bus_set_transient_bool(u, name, &c->ignore_sigpipe, message, flags, error);
1092
1093 if (streq(name, "TTYVHangup"))
1094 return bus_set_transient_bool(u, name, &c->tty_vhangup, message, flags, error);
1095
1096 if (streq(name, "TTYReset"))
1097 return bus_set_transient_bool(u, name, &c->tty_reset, message, flags, error);
1098
1099 if (streq(name, "TTYVTDisallocate"))
1100 return bus_set_transient_bool(u, name, &c->tty_vt_disallocate, message, flags, error);
1101
1102 if (streq(name, "PrivateTmp"))
1103 return bus_set_transient_bool(u, name, &c->private_tmp, message, flags, error);
1104
1105 if (streq(name, "PrivateDevices"))
1106 return bus_set_transient_bool(u, name, &c->private_devices, message, flags, error);
1107
1108 if (streq(name, "PrivateMounts"))
1109 return bus_set_transient_bool(u, name, &c->private_mounts, message, flags, error);
1110
1111 if (streq(name, "PrivateNetwork"))
1112 return bus_set_transient_bool(u, name, &c->private_network, message, flags, error);
1113
1114 if (streq(name, "PrivateUsers"))
1115 return bus_set_transient_bool(u, name, &c->private_users, message, flags, error);
1116
1117 if (streq(name, "NoNewPrivileges"))
1118 return bus_set_transient_bool(u, name, &c->no_new_privileges, message, flags, error);
1119
1120 if (streq(name, "SyslogLevelPrefix"))
1121 return bus_set_transient_bool(u, name, &c->syslog_level_prefix, message, flags, error);
1122
1123 if (streq(name, "MemoryDenyWriteExecute"))
1124 return bus_set_transient_bool(u, name, &c->memory_deny_write_execute, message, flags, error);
1125
1126 if (streq(name, "RestrictRealtime"))
1127 return bus_set_transient_bool(u, name, &c->restrict_realtime, message, flags, error);
1128
1129 if (streq(name, "RestrictSUIDSGID"))
1130 return bus_set_transient_bool(u, name, &c->restrict_suid_sgid, message, flags, error);
1131
1132 if (streq(name, "DynamicUser"))
1133 return bus_set_transient_bool(u, name, &c->dynamic_user, message, flags, error);
1134
1135 if (streq(name, "RemoveIPC"))
1136 return bus_set_transient_bool(u, name, &c->remove_ipc, message, flags, error);
1137
1138 if (streq(name, "ProtectKernelTunables"))
1139 return bus_set_transient_bool(u, name, &c->protect_kernel_tunables, message, flags, error);
1140
1141 if (streq(name, "ProtectKernelModules"))
1142 return bus_set_transient_bool(u, name, &c->protect_kernel_modules, message, flags, error);
1143
1144 if (streq(name, "ProtectControlGroups"))
1145 return bus_set_transient_bool(u, name, &c->protect_control_groups, message, flags, error);
1146
1147 if (streq(name, "MountAPIVFS"))
1148 return bus_set_transient_bool(u, name, &c->mount_apivfs, message, flags, error);
1149
1150 if (streq(name, "CPUSchedulingResetOnFork"))
1151 return bus_set_transient_bool(u, name, &c->cpu_sched_reset_on_fork, message, flags, error);
1152
1153 if (streq(name, "NonBlocking"))
1154 return bus_set_transient_bool(u, name, &c->non_blocking, message, flags, error);
1155
1156 if (streq(name, "LockPersonality"))
1157 return bus_set_transient_bool(u, name, &c->lock_personality, message, flags, error);
1158
1159 if (streq(name, "ProtectHostname"))
1160 return bus_set_transient_bool(u, name, &c->protect_hostname, message, flags, error);
1161
1162 if (streq(name, "UtmpIdentifier"))
1163 return bus_set_transient_string(u, name, &c->utmp_id, message, flags, error);
1164
1165 if (streq(name, "UtmpMode"))
1166 return bus_set_transient_utmp_mode(u, name, &c->utmp_mode, message, flags, error);
1167
1168 if (streq(name, "PAMName"))
1169 return bus_set_transient_string(u, name, &c->pam_name, message, flags, error);
1170
1171 if (streq(name, "TimerSlackNSec"))
1172 return bus_set_transient_nsec(u, name, &c->timer_slack_nsec, message, flags, error);
1173
1174 if (streq(name, "ProtectSystem"))
1175 return bus_set_transient_protect_system(u, name, &c->protect_system, message, flags, error);
1176
1177 if (streq(name, "ProtectHome"))
1178 return bus_set_transient_protect_home(u, name, &c->protect_home, message, flags, error);
1179
1180 if (streq(name, "KeyringMode"))
1181 return bus_set_transient_keyring_mode(u, name, &c->keyring_mode, message, flags, error);
1182
1183 if (streq(name, "RuntimeDirectoryPreserve"))
1184 return bus_set_transient_preserve_mode(u, name, &c->runtime_directory_preserve_mode, message, flags, error);
1185
1186 if (streq(name, "UMask"))
1187 return bus_set_transient_mode_t(u, name, &c->umask, message, flags, error);
1188
1189 if (streq(name, "RuntimeDirectoryMode"))
1190 return bus_set_transient_mode_t(u, name, &c->directories[EXEC_DIRECTORY_RUNTIME].mode, message, flags, error);
1191
1192 if (streq(name, "StateDirectoryMode"))
1193 return bus_set_transient_mode_t(u, name, &c->directories[EXEC_DIRECTORY_STATE].mode, message, flags, error);
1194
1195 if (streq(name, "CacheDirectoryMode"))
1196 return bus_set_transient_mode_t(u, name, &c->directories[EXEC_DIRECTORY_CACHE].mode, message, flags, error);
1197
1198 if (streq(name, "LogsDirectoryMode"))
1199 return bus_set_transient_mode_t(u, name, &c->directories[EXEC_DIRECTORY_LOGS].mode, message, flags, error);
1200
1201 if (streq(name, "ConfigurationDirectoryMode"))
1202 return bus_set_transient_mode_t(u, name, &c->directories[EXEC_DIRECTORY_CONFIGURATION].mode, message, flags, error);
1203
1204 if (streq(name, "SELinuxContext"))
1205 return bus_set_transient_string(u, name, &c->selinux_context, message, flags, error);
1206
1207 if (streq(name, "SecureBits"))
1208 return bus_set_transient_secure_bits(u, name, &c->secure_bits, message, flags, error);
1209
1210 if (streq(name, "CapabilityBoundingSet"))
1211 return bus_set_transient_capability(u, name, &c->capability_bounding_set, message, flags, error);
1212
1213 if (streq(name, "AmbientCapabilities"))
1214 return bus_set_transient_capability(u, name, &c->capability_ambient_set, message, flags, error);
1215
1216 if (streq(name, "RestrictNamespaces"))
1217 return bus_set_transient_namespace_flag(u, name, &c->restrict_namespaces, message, flags, error);
1218
1219 if (streq(name, "MountFlags"))
1220 return bus_set_transient_mount_flags(u, name, &c->mount_flags, message, flags, error);
1221
1222 if (streq(name, "NetworkNamespacePath"))
1223 return bus_set_transient_path(u, name, &c->network_namespace_path, message, flags, error);
1224
1225 if (streq(name, "SupplementaryGroups")) {
1226 _cleanup_strv_free_ char **l = NULL;
1227 char **p;
1228
1229 r = sd_bus_message_read_strv(message, &l);
1230 if (r < 0)
1231 return r;
1232
1233 STRV_FOREACH(p, l) {
1234 if (!isempty(*p) && !valid_user_group_name_or_id(*p))
1235 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid supplementary group names");
1236 }
1237
1238 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1239 if (strv_isempty(l)) {
1240 c->supplementary_groups = strv_free(c->supplementary_groups);
1241 unit_write_settingf(u, flags, name, "%s=", name);
1242 } else {
1243 _cleanup_free_ char *joined = NULL;
1244
1245 r = strv_extend_strv(&c->supplementary_groups, l, true);
1246 if (r < 0)
1247 return -ENOMEM;
1248
1249 joined = strv_join(c->supplementary_groups, " ");
1250 if (!joined)
1251 return -ENOMEM;
1252
1253 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "%s=%s", name, joined);
1254 }
1255 }
1256
1257 return 1;
1258
1259 } else if (streq(name, "SyslogLevel")) {
1260 int32_t level;
1261
1262 r = sd_bus_message_read(message, "i", &level);
1263 if (r < 0)
1264 return r;
1265
1266 if (!log_level_is_valid(level))
1267 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Log level value out of range");
1268
1269 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1270 c->syslog_priority = (c->syslog_priority & LOG_FACMASK) | level;
1271 unit_write_settingf(u, flags, name, "SyslogLevel=%i", level);
1272 }
1273
1274 return 1;
1275
1276 } else if (streq(name, "SyslogFacility")) {
1277 int32_t facility;
1278
1279 r = sd_bus_message_read(message, "i", &facility);
1280 if (r < 0)
1281 return r;
1282
1283 if (!log_facility_unshifted_is_valid(facility))
1284 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Log facility value out of range");
1285
1286 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1287 c->syslog_priority = (facility << 3) | LOG_PRI(c->syslog_priority);
1288 unit_write_settingf(u, flags, name, "SyslogFacility=%i", facility);
1289 }
1290
1291 return 1;
1292
1293 } else if (streq(name, "LogExtraFields")) {
1294 size_t n = 0;
1295
1296 r = sd_bus_message_enter_container(message, 'a', "ay");
1297 if (r < 0)
1298 return r;
1299
1300 for (;;) {
1301 _cleanup_free_ void *copy = NULL;
1302 struct iovec *t;
1303 const char *eq;
1304 const void *p;
1305 size_t sz;
1306
1307 /* Note that we expect a byte array for each field, instead of a string. That's because on the
1308 * lower-level journal fields can actually contain binary data and are not restricted to text,
1309 * and we should not "lose precision" in our types on the way. That said, I am pretty sure
1310 * actually encoding binary data as unit metadata is not a good idea. Hence we actually refuse
1311 * any actual binary data, and only accept UTF-8. This allows us to eventually lift this
1312 * limitation, should a good, valid usecase arise. */
1313
1314 r = sd_bus_message_read_array(message, 'y', &p, &sz);
1315 if (r < 0)
1316 return r;
1317 if (r == 0)
1318 break;
1319
1320 if (memchr(p, 0, sz))
1321 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Journal field contains zero byte");
1322
1323 eq = memchr(p, '=', sz);
1324 if (!eq)
1325 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Journal field contains no '=' character");
1326 if (!journal_field_valid(p, eq - (const char*) p, false))
1327 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Journal field invalid");
1328
1329 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1330 t = reallocarray(c->log_extra_fields, c->n_log_extra_fields+1, sizeof(struct iovec));
1331 if (!t)
1332 return -ENOMEM;
1333 c->log_extra_fields = t;
1334 }
1335
1336 copy = malloc(sz + 1);
1337 if (!copy)
1338 return -ENOMEM;
1339
1340 memcpy(copy, p, sz);
1341 ((uint8_t*) copy)[sz] = 0;
1342
1343 if (!utf8_is_valid(copy))
1344 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Journal field is not valid UTF-8");
1345
1346 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1347 c->log_extra_fields[c->n_log_extra_fields++] = IOVEC_MAKE(copy, sz);
1348 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS|UNIT_ESCAPE_C, name, "LogExtraFields=%s", (char*) copy);
1349
1350 copy = NULL;
1351 }
1352
1353 n++;
1354 }
1355
1356 r = sd_bus_message_exit_container(message);
1357 if (r < 0)
1358 return r;
1359
1360 if (!UNIT_WRITE_FLAGS_NOOP(flags) && n == 0) {
1361 exec_context_free_log_extra_fields(c);
1362 unit_write_setting(u, flags, name, "LogExtraFields=");
1363 }
1364
1365 return 1;
1366 }
1367
1368 #if HAVE_SECCOMP
1369
1370 if (streq(name, "SystemCallErrorNumber"))
1371 return bus_set_transient_errno(u, name, &c->syscall_errno, message, flags, error);
1372
1373 if (streq(name, "SystemCallFilter")) {
1374 int whitelist;
1375 _cleanup_strv_free_ char **l = NULL;
1376
1377 r = sd_bus_message_enter_container(message, 'r', "bas");
1378 if (r < 0)
1379 return r;
1380
1381 r = sd_bus_message_read(message, "b", &whitelist);
1382 if (r < 0)
1383 return r;
1384
1385 r = sd_bus_message_read_strv(message, &l);
1386 if (r < 0)
1387 return r;
1388
1389 r = sd_bus_message_exit_container(message);
1390 if (r < 0)
1391 return r;
1392
1393 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1394 _cleanup_free_ char *joined = NULL;
1395 SeccompParseFlags invert_flag = whitelist ? 0 : SECCOMP_PARSE_INVERT;
1396 char **s;
1397
1398 if (strv_isempty(l)) {
1399 c->syscall_whitelist = false;
1400 c->syscall_filter = hashmap_free(c->syscall_filter);
1401
1402 unit_write_settingf(u, flags, name, "SystemCallFilter=");
1403 return 1;
1404 }
1405
1406 if (!c->syscall_filter) {
1407 c->syscall_filter = hashmap_new(NULL);
1408 if (!c->syscall_filter)
1409 return log_oom();
1410
1411 c->syscall_whitelist = whitelist;
1412
1413 if (c->syscall_whitelist) {
1414 r = seccomp_parse_syscall_filter("@default",
1415 -1,
1416 c->syscall_filter,
1417 SECCOMP_PARSE_WHITELIST | invert_flag,
1418 u->id,
1419 NULL, 0);
1420 if (r < 0)
1421 return r;
1422 }
1423 }
1424
1425 STRV_FOREACH(s, l) {
1426 _cleanup_free_ char *n = NULL;
1427 int e;
1428
1429 r = parse_syscall_and_errno(*s, &n, &e);
1430 if (r < 0)
1431 return r;
1432
1433 r = seccomp_parse_syscall_filter(n,
1434 e,
1435 c->syscall_filter,
1436 (c->syscall_whitelist ? SECCOMP_PARSE_WHITELIST : 0) | invert_flag,
1437 u->id,
1438 NULL, 0);
1439 if (r < 0)
1440 return r;
1441 }
1442
1443 joined = strv_join(l, " ");
1444 if (!joined)
1445 return -ENOMEM;
1446
1447 unit_write_settingf(u, flags, name, "SystemCallFilter=%s%s", whitelist ? "" : "~", joined);
1448 }
1449
1450 return 1;
1451
1452 } else if (streq(name, "SystemCallArchitectures")) {
1453 _cleanup_strv_free_ char **l = NULL;
1454
1455 r = sd_bus_message_read_strv(message, &l);
1456 if (r < 0)
1457 return r;
1458
1459 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1460 _cleanup_free_ char *joined = NULL;
1461
1462 if (strv_isempty(l))
1463 c->syscall_archs = set_free(c->syscall_archs);
1464 else {
1465 char **s;
1466
1467 r = set_ensure_allocated(&c->syscall_archs, NULL);
1468 if (r < 0)
1469 return r;
1470
1471 STRV_FOREACH(s, l) {
1472 uint32_t a;
1473
1474 r = seccomp_arch_from_string(*s, &a);
1475 if (r < 0)
1476 return r;
1477
1478 r = set_put(c->syscall_archs, UINT32_TO_PTR(a + 1));
1479 if (r < 0)
1480 return r;
1481 }
1482
1483 }
1484
1485 joined = strv_join(l, " ");
1486 if (!joined)
1487 return -ENOMEM;
1488
1489 unit_write_settingf(u, flags, name, "%s=%s", name, joined);
1490 }
1491
1492 return 1;
1493
1494 } else if (streq(name, "RestrictAddressFamilies")) {
1495 int whitelist;
1496 _cleanup_strv_free_ char **l = NULL;
1497
1498 r = sd_bus_message_enter_container(message, 'r', "bas");
1499 if (r < 0)
1500 return r;
1501
1502 r = sd_bus_message_read(message, "b", &whitelist);
1503 if (r < 0)
1504 return r;
1505
1506 r = sd_bus_message_read_strv(message, &l);
1507 if (r < 0)
1508 return r;
1509
1510 r = sd_bus_message_exit_container(message);
1511 if (r < 0)
1512 return r;
1513
1514 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1515 _cleanup_free_ char *joined = NULL;
1516 char **s;
1517
1518 if (strv_isempty(l)) {
1519 c->address_families_whitelist = false;
1520 c->address_families = set_free(c->address_families);
1521
1522 unit_write_settingf(u, flags, name, "RestrictAddressFamilies=");
1523 return 1;
1524 }
1525
1526 if (!c->address_families) {
1527 c->address_families = set_new(NULL);
1528 if (!c->address_families)
1529 return log_oom();
1530
1531 c->address_families_whitelist = whitelist;
1532 }
1533
1534 STRV_FOREACH(s, l) {
1535 int af;
1536
1537 af = af_from_name(*s);
1538 if (af < 0)
1539 return af;
1540
1541 if (whitelist == c->address_families_whitelist) {
1542 r = set_put(c->address_families, INT_TO_PTR(af));
1543 if (r < 0)
1544 return r;
1545 } else
1546 (void) set_remove(c->address_families, INT_TO_PTR(af));
1547 }
1548
1549 joined = strv_join(l, " ");
1550 if (!joined)
1551 return -ENOMEM;
1552
1553 unit_write_settingf(u, flags, name, "RestrictAddressFamilies=%s%s", whitelist ? "" : "~", joined);
1554 }
1555
1556 return 1;
1557 }
1558 #endif
1559 if (streq(name, "CPUAffinity")) {
1560 const void *a;
1561 size_t n = 0;
1562
1563 r = sd_bus_message_read_array(message, 'y', &a, &n);
1564 if (r < 0)
1565 return r;
1566
1567 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1568 if (n == 0) {
1569 c->cpuset = cpu_set_mfree(c->cpuset);
1570 c->cpuset_ncpus = 0;
1571 unit_write_settingf(u, flags, name, "%s=", name);
1572 } else {
1573 _cleanup_free_ char *str = NULL;
1574 size_t allocated = 0, len = 0, i, ncpus;
1575
1576 ncpus = CPU_SIZE_TO_NUM(n);
1577
1578 for (i = 0; i < ncpus; i++) {
1579 _cleanup_free_ char *p = NULL;
1580 size_t add;
1581
1582 if (!CPU_ISSET_S(i, n, (cpu_set_t*) a))
1583 continue;
1584
1585 r = asprintf(&p, "%zu", i);
1586 if (r < 0)
1587 return -ENOMEM;
1588
1589 add = strlen(p);
1590
1591 if (!GREEDY_REALLOC(str, allocated, len + add + 2))
1592 return -ENOMEM;
1593
1594 strcpy(mempcpy(str + len, p, add), " ");
1595 len += add + 1;
1596 }
1597
1598 if (len != 0)
1599 str[len - 1] = '\0';
1600
1601 if (!c->cpuset || c->cpuset_ncpus < ncpus) {
1602 cpu_set_t *cpuset;
1603
1604 cpuset = CPU_ALLOC(ncpus);
1605 if (!cpuset)
1606 return -ENOMEM;
1607
1608 CPU_ZERO_S(n, cpuset);
1609 if (c->cpuset) {
1610 CPU_OR_S(CPU_ALLOC_SIZE(c->cpuset_ncpus), cpuset, c->cpuset, (cpu_set_t*) a);
1611 CPU_FREE(c->cpuset);
1612 } else
1613 CPU_OR_S(n, cpuset, cpuset, (cpu_set_t*) a);
1614
1615 c->cpuset = cpuset;
1616 c->cpuset_ncpus = ncpus;
1617 } else
1618 CPU_OR_S(n, c->cpuset, c->cpuset, (cpu_set_t*) a);
1619
1620 unit_write_settingf(u, flags, name, "%s=%s", name, str);
1621 }
1622 }
1623
1624 return 1;
1625
1626 } else if (streq(name, "Nice")) {
1627 int32_t q;
1628
1629 r = sd_bus_message_read(message, "i", &q);
1630 if (r < 0)
1631 return r;
1632
1633 if (!nice_is_valid(q))
1634 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid Nice value: %i", q);
1635
1636 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1637 c->nice = q;
1638 c->nice_set = true;
1639
1640 unit_write_settingf(u, flags, name, "Nice=%i", q);
1641 }
1642
1643 return 1;
1644
1645 } else if (streq(name, "CPUSchedulingPolicy")) {
1646 int32_t q;
1647
1648 r = sd_bus_message_read(message, "i", &q);
1649 if (r < 0)
1650 return r;
1651
1652 if (!sched_policy_is_valid(q))
1653 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid CPU scheduling policy: %i", q);
1654
1655 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1656 _cleanup_free_ char *s = NULL;
1657
1658 r = sched_policy_to_string_alloc(q, &s);
1659 if (r < 0)
1660 return r;
1661
1662 c->cpu_sched_policy = q;
1663 c->cpu_sched_priority = CLAMP(c->cpu_sched_priority, sched_get_priority_min(q), sched_get_priority_max(q));
1664 c->cpu_sched_set = true;
1665
1666 unit_write_settingf(u, flags, name, "CPUSchedulingPolicy=%s", s);
1667 }
1668
1669 return 1;
1670
1671 } else if (streq(name, "CPUSchedulingPriority")) {
1672 int32_t p, min, max;
1673
1674 r = sd_bus_message_read(message, "i", &p);
1675 if (r < 0)
1676 return r;
1677
1678 min = sched_get_priority_min(c->cpu_sched_policy);
1679 max = sched_get_priority_max(c->cpu_sched_policy);
1680 if (p < min || p > max)
1681 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid CPU scheduling priority: %i", p);
1682
1683 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1684 c->cpu_sched_priority = p;
1685 c->cpu_sched_set = true;
1686
1687 unit_write_settingf(u, flags, name, "CPUSchedulingPriority=%i", p);
1688 }
1689
1690 return 1;
1691
1692 } else if (streq(name, "IOSchedulingClass")) {
1693 int32_t q;
1694
1695 r = sd_bus_message_read(message, "i", &q);
1696 if (r < 0)
1697 return r;
1698
1699 if (!ioprio_class_is_valid(q))
1700 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid IO scheduling class: %i", q);
1701
1702 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1703 _cleanup_free_ char *s = NULL;
1704
1705 r = ioprio_class_to_string_alloc(q, &s);
1706 if (r < 0)
1707 return r;
1708
1709 c->ioprio = IOPRIO_PRIO_VALUE(q, IOPRIO_PRIO_DATA(c->ioprio));
1710 c->ioprio_set = true;
1711
1712 unit_write_settingf(u, flags, name, "IOSchedulingClass=%s", s);
1713 }
1714
1715 return 1;
1716
1717 } else if (streq(name, "IOSchedulingPriority")) {
1718 int32_t p;
1719
1720 r = sd_bus_message_read(message, "i", &p);
1721 if (r < 0)
1722 return r;
1723
1724 if (!ioprio_priority_is_valid(p))
1725 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid IO scheduling priority: %i", p);
1726
1727 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1728 c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_PRIO_CLASS(c->ioprio), p);
1729 c->ioprio_set = true;
1730
1731 unit_write_settingf(u, flags, name, "IOSchedulingPriority=%i", p);
1732 }
1733
1734 return 1;
1735
1736 } else if (streq(name, "WorkingDirectory")) {
1737 const char *s;
1738 bool missing_ok;
1739
1740 r = sd_bus_message_read(message, "s", &s);
1741 if (r < 0)
1742 return r;
1743
1744 if (s[0] == '-') {
1745 missing_ok = true;
1746 s++;
1747 } else
1748 missing_ok = false;
1749
1750 if (!isempty(s) && !streq(s, "~") && !path_is_absolute(s))
1751 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "WorkingDirectory= expects an absolute path or '~'");
1752
1753 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1754 if (streq(s, "~")) {
1755 c->working_directory = mfree(c->working_directory);
1756 c->working_directory_home = true;
1757 } else {
1758 r = free_and_strdup(&c->working_directory, empty_to_null(s));
1759 if (r < 0)
1760 return r;
1761
1762 c->working_directory_home = false;
1763 }
1764
1765 c->working_directory_missing_ok = missing_ok;
1766 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "WorkingDirectory=%s%s", missing_ok ? "-" : "", s);
1767 }
1768
1769 return 1;
1770
1771 } else if (STR_IN_SET(name,
1772 "StandardInputFileDescriptorName", "StandardOutputFileDescriptorName", "StandardErrorFileDescriptorName")) {
1773 const char *s;
1774
1775 r = sd_bus_message_read(message, "s", &s);
1776 if (r < 0)
1777 return r;
1778
1779 if (!isempty(s) && !fdname_is_valid(s))
1780 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid file descriptor name");
1781
1782 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1783
1784 if (streq(name, "StandardInputFileDescriptorName")) {
1785 r = free_and_strdup(c->stdio_fdname + STDIN_FILENO, empty_to_null(s));
1786 if (r < 0)
1787 return r;
1788
1789 c->std_input = EXEC_INPUT_NAMED_FD;
1790 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "StandardInput=fd:%s", exec_context_fdname(c, STDIN_FILENO));
1791
1792 } else if (streq(name, "StandardOutputFileDescriptorName")) {
1793 r = free_and_strdup(c->stdio_fdname + STDOUT_FILENO, empty_to_null(s));
1794 if (r < 0)
1795 return r;
1796
1797 c->std_output = EXEC_OUTPUT_NAMED_FD;
1798 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "StandardOutput=fd:%s", exec_context_fdname(c, STDOUT_FILENO));
1799
1800 } else {
1801 assert(streq(name, "StandardErrorFileDescriptorName"));
1802
1803 r = free_and_strdup(&c->stdio_fdname[STDERR_FILENO], empty_to_null(s));
1804 if (r < 0)
1805 return r;
1806
1807 c->std_error = EXEC_OUTPUT_NAMED_FD;
1808 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "StandardError=fd:%s", exec_context_fdname(c, STDERR_FILENO));
1809 }
1810 }
1811
1812 return 1;
1813
1814 } else if (STR_IN_SET(name,
1815 "StandardInputFile",
1816 "StandardOutputFile", "StandardOutputFileToAppend",
1817 "StandardErrorFile", "StandardErrorFileToAppend")) {
1818 const char *s;
1819
1820 r = sd_bus_message_read(message, "s", &s);
1821 if (r < 0)
1822 return r;
1823
1824 if (!isempty(s)) {
1825 if (!path_is_absolute(s))
1826 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path %s is not absolute", s);
1827 if (!path_is_normalized(s))
1828 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path %s is not normalized", s);
1829 }
1830
1831 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1832
1833 if (streq(name, "StandardInputFile")) {
1834 r = free_and_strdup(&c->stdio_file[STDIN_FILENO], empty_to_null(s));
1835 if (r < 0)
1836 return r;
1837
1838 c->std_input = EXEC_INPUT_FILE;
1839 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "StandardInput=file:%s", s);
1840
1841 } else if (STR_IN_SET(name, "StandardOutputFile", "StandardOutputFileToAppend")) {
1842 r = free_and_strdup(&c->stdio_file[STDOUT_FILENO], empty_to_null(s));
1843 if (r < 0)
1844 return r;
1845
1846 if (streq(name, "StandardOutputFile")) {
1847 c->std_output = EXEC_OUTPUT_FILE;
1848 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "StandardOutput=file:%s", s);
1849 } else {
1850 assert(streq(name, "StandardOutputFileToAppend"));
1851 c->std_output = EXEC_OUTPUT_FILE_APPEND;
1852 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "StandardOutput=append:%s", s);
1853 }
1854 } else {
1855 assert(STR_IN_SET(name, "StandardErrorFile", "StandardErrorFileToAppend"));
1856
1857 r = free_and_strdup(&c->stdio_file[STDERR_FILENO], empty_to_null(s));
1858 if (r < 0)
1859 return r;
1860
1861 if (streq(name, "StandardErrorFile")) {
1862 c->std_error = EXEC_OUTPUT_FILE;
1863 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "StandardError=file:%s", s);
1864 } else {
1865 assert(streq(name, "StandardErrorFileToAppend"));
1866 c->std_error = EXEC_OUTPUT_FILE_APPEND;
1867 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "StandardError=append:%s", s);
1868 }
1869 }
1870 }
1871
1872 return 1;
1873
1874 } else if (streq(name, "StandardInputData")) {
1875 const void *p;
1876 size_t sz;
1877
1878 r = sd_bus_message_read_array(message, 'y', &p, &sz);
1879 if (r < 0)
1880 return r;
1881
1882 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1883 _cleanup_free_ char *encoded = NULL;
1884
1885 if (sz == 0) {
1886 c->stdin_data = mfree(c->stdin_data);
1887 c->stdin_data_size = 0;
1888
1889 unit_write_settingf(u, flags, name, "StandardInputData=");
1890 } else {
1891 void *q;
1892 ssize_t n;
1893
1894 if (c->stdin_data_size + sz < c->stdin_data_size || /* check for overflow */
1895 c->stdin_data_size + sz > EXEC_STDIN_DATA_MAX)
1896 return -E2BIG;
1897
1898 n = base64mem(p, sz, &encoded);
1899 if (n < 0)
1900 return (int) n;
1901
1902 q = realloc(c->stdin_data, c->stdin_data_size + sz);
1903 if (!q)
1904 return -ENOMEM;
1905
1906 memcpy((uint8_t*) q + c->stdin_data_size, p, sz);
1907
1908 c->stdin_data = q;
1909 c->stdin_data_size += sz;
1910
1911 unit_write_settingf(u, flags, name, "StandardInputData=%s", encoded);
1912 }
1913 }
1914
1915 return 1;
1916
1917 } else if (streq(name, "Environment")) {
1918
1919 _cleanup_strv_free_ char **l = NULL;
1920
1921 r = sd_bus_message_read_strv(message, &l);
1922 if (r < 0)
1923 return r;
1924
1925 if (!strv_env_is_valid(l))
1926 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid environment block.");
1927
1928 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1929 if (strv_isempty(l)) {
1930 c->environment = strv_free(c->environment);
1931 unit_write_setting(u, flags, name, "Environment=");
1932 } else {
1933 _cleanup_free_ char *joined = NULL;
1934 char **e;
1935
1936 joined = unit_concat_strv(l, UNIT_ESCAPE_SPECIFIERS|UNIT_ESCAPE_C);
1937 if (!joined)
1938 return -ENOMEM;
1939
1940 e = strv_env_merge(2, c->environment, l);
1941 if (!e)
1942 return -ENOMEM;
1943
1944 strv_free_and_replace(c->environment, e);
1945 unit_write_settingf(u, flags, name, "Environment=%s", joined);
1946 }
1947 }
1948
1949 return 1;
1950
1951 } else if (streq(name, "UnsetEnvironment")) {
1952
1953 _cleanup_strv_free_ char **l = NULL;
1954
1955 r = sd_bus_message_read_strv(message, &l);
1956 if (r < 0)
1957 return r;
1958
1959 if (!strv_env_name_or_assignment_is_valid(l))
1960 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid UnsetEnvironment= list.");
1961
1962 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1963 if (strv_isempty(l)) {
1964 c->unset_environment = strv_free(c->unset_environment);
1965 unit_write_setting(u, flags, name, "UnsetEnvironment=");
1966 } else {
1967 _cleanup_free_ char *joined = NULL;
1968 char **e;
1969
1970 joined = unit_concat_strv(l, UNIT_ESCAPE_SPECIFIERS|UNIT_ESCAPE_C);
1971 if (!joined)
1972 return -ENOMEM;
1973
1974 e = strv_env_merge(2, c->unset_environment, l);
1975 if (!e)
1976 return -ENOMEM;
1977
1978 strv_free_and_replace(c->unset_environment, e);
1979 unit_write_settingf(u, flags, name, "UnsetEnvironment=%s", joined);
1980 }
1981 }
1982
1983 return 1;
1984
1985 } else if (streq(name, "OOMScoreAdjust")) {
1986 int oa;
1987
1988 r = sd_bus_message_read(message, "i", &oa);
1989 if (r < 0)
1990 return r;
1991
1992 if (!oom_score_adjust_is_valid(oa))
1993 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "OOM score adjust value out of range");
1994
1995 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1996 c->oom_score_adjust = oa;
1997 c->oom_score_adjust_set = true;
1998 unit_write_settingf(u, flags, name, "OOMScoreAdjust=%i", oa);
1999 }
2000
2001 return 1;
2002
2003 } else if (streq(name, "EnvironmentFiles")) {
2004
2005 _cleanup_free_ char *joined = NULL;
2006 _cleanup_fclose_ FILE *f = NULL;
2007 _cleanup_strv_free_ char **l = NULL;
2008 size_t size = 0;
2009 char **i;
2010
2011 r = sd_bus_message_enter_container(message, 'a', "(sb)");
2012 if (r < 0)
2013 return r;
2014
2015 f = open_memstream_unlocked(&joined, &size);
2016 if (!f)
2017 return -ENOMEM;
2018
2019 fputs("EnvironmentFile=\n", f);
2020
2021 STRV_FOREACH(i, c->environment_files) {
2022 _cleanup_free_ char *q = NULL;
2023
2024 q = specifier_escape(*i);
2025 if (!q)
2026 return -ENOMEM;
2027
2028 fprintf(f, "EnvironmentFile=%s\n", q);
2029 }
2030
2031 while ((r = sd_bus_message_enter_container(message, 'r', "sb")) > 0) {
2032 const char *path;
2033 int b;
2034
2035 r = sd_bus_message_read(message, "sb", &path, &b);
2036 if (r < 0)
2037 return r;
2038
2039 r = sd_bus_message_exit_container(message);
2040 if (r < 0)
2041 return r;
2042
2043 if (!path_is_absolute(path))
2044 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path %s is not absolute.", path);
2045
2046 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2047 _cleanup_free_ char *q = NULL;
2048 char *buf;
2049
2050 buf = strjoin(b ? "-" : "", path);
2051 if (!buf)
2052 return -ENOMEM;
2053
2054 q = specifier_escape(buf);
2055 if (!q) {
2056 free(buf);
2057 return -ENOMEM;
2058 }
2059
2060 fprintf(f, "EnvironmentFile=%s\n", q);
2061
2062 r = strv_consume(&l, buf);
2063 if (r < 0)
2064 return r;
2065 }
2066 }
2067 if (r < 0)
2068 return r;
2069
2070 r = sd_bus_message_exit_container(message);
2071 if (r < 0)
2072 return r;
2073
2074 r = fflush_and_check(f);
2075 if (r < 0)
2076 return r;
2077
2078 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2079 if (strv_isempty(l)) {
2080 c->environment_files = strv_free(c->environment_files);
2081 unit_write_setting(u, flags, name, "EnvironmentFile=");
2082 } else {
2083 r = strv_extend_strv(&c->environment_files, l, true);
2084 if (r < 0)
2085 return r;
2086
2087 unit_write_setting(u, flags, name, joined);
2088 }
2089 }
2090
2091 return 1;
2092
2093 } else if (streq(name, "PassEnvironment")) {
2094
2095 _cleanup_strv_free_ char **l = NULL;
2096
2097 r = sd_bus_message_read_strv(message, &l);
2098 if (r < 0)
2099 return r;
2100
2101 if (!strv_env_name_is_valid(l))
2102 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid PassEnvironment= block.");
2103
2104 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2105 if (strv_isempty(l)) {
2106 c->pass_environment = strv_free(c->pass_environment);
2107 unit_write_setting(u, flags, name, "PassEnvironment=");
2108 } else {
2109 _cleanup_free_ char *joined = NULL;
2110
2111 r = strv_extend_strv(&c->pass_environment, l, true);
2112 if (r < 0)
2113 return r;
2114
2115 /* We write just the new settings out to file, with unresolved specifiers. */
2116 joined = unit_concat_strv(l, UNIT_ESCAPE_SPECIFIERS);
2117 if (!joined)
2118 return -ENOMEM;
2119
2120 unit_write_settingf(u, flags, name, "PassEnvironment=%s", joined);
2121 }
2122 }
2123
2124 return 1;
2125
2126 } else if (STR_IN_SET(name, "ReadWriteDirectories", "ReadOnlyDirectories", "InaccessibleDirectories",
2127 "ReadWritePaths", "ReadOnlyPaths", "InaccessiblePaths")) {
2128 _cleanup_strv_free_ char **l = NULL;
2129 char ***dirs;
2130 char **p;
2131
2132 r = sd_bus_message_read_strv(message, &l);
2133 if (r < 0)
2134 return r;
2135
2136 STRV_FOREACH(p, l) {
2137 char *i = *p;
2138 size_t offset;
2139
2140 offset = i[0] == '-';
2141 offset += i[offset] == '+';
2142 if (!path_is_absolute(i + offset))
2143 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid %s", name);
2144
2145 path_simplify(i + offset, false);
2146 }
2147
2148 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2149 if (STR_IN_SET(name, "ReadWriteDirectories", "ReadWritePaths"))
2150 dirs = &c->read_write_paths;
2151 else if (STR_IN_SET(name, "ReadOnlyDirectories", "ReadOnlyPaths"))
2152 dirs = &c->read_only_paths;
2153 else /* "InaccessiblePaths" */
2154 dirs = &c->inaccessible_paths;
2155
2156 if (strv_isempty(l)) {
2157 *dirs = strv_free(*dirs);
2158 unit_write_settingf(u, flags, name, "%s=", name);
2159 } else {
2160 _cleanup_free_ char *joined = NULL;
2161
2162 joined = unit_concat_strv(l, UNIT_ESCAPE_SPECIFIERS);
2163 if (!joined)
2164 return -ENOMEM;
2165
2166 r = strv_extend_strv(dirs, l, true);
2167 if (r < 0)
2168 return -ENOMEM;
2169
2170 unit_write_settingf(u, flags, name, "%s=%s", name, joined);
2171 }
2172 }
2173
2174 return 1;
2175
2176 } else if (STR_IN_SET(name, "RuntimeDirectory", "StateDirectory", "CacheDirectory", "LogsDirectory", "ConfigurationDirectory")) {
2177 _cleanup_strv_free_ char **l = NULL;
2178 char **p;
2179
2180 r = sd_bus_message_read_strv(message, &l);
2181 if (r < 0)
2182 return r;
2183
2184 STRV_FOREACH(p, l) {
2185 if (!path_is_normalized(*p))
2186 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "%s= path is not normalized: %s", name, *p);
2187
2188 if (path_is_absolute(*p))
2189 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "%s= path is absolute: %s", name, *p);
2190
2191 if (path_startswith(*p, "private"))
2192 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "%s= path can't be 'private': %s", name, *p);
2193 }
2194
2195 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2196 ExecDirectoryType i;
2197 ExecDirectory *d;
2198
2199 assert_se((i = exec_directory_type_from_string(name)) >= 0);
2200 d = c->directories + i;
2201
2202 if (strv_isempty(l)) {
2203 d->paths = strv_free(d->paths);
2204 unit_write_settingf(u, flags, name, "%s=", name);
2205 } else {
2206 _cleanup_free_ char *joined = NULL;
2207
2208 r = strv_extend_strv(&d->paths, l, true);
2209 if (r < 0)
2210 return r;
2211
2212 joined = unit_concat_strv(l, UNIT_ESCAPE_SPECIFIERS);
2213 if (!joined)
2214 return -ENOMEM;
2215
2216 unit_write_settingf(u, flags, name, "%s=%s", name, joined);
2217 }
2218 }
2219
2220 return 1;
2221
2222 } else if (STR_IN_SET(name, "AppArmorProfile", "SmackProcessLabel")) {
2223 int ignore;
2224 const char *s;
2225
2226 r = sd_bus_message_read(message, "(bs)", &ignore, &s);
2227 if (r < 0)
2228 return r;
2229
2230 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2231 char **p;
2232 bool *b;
2233
2234 if (streq(name, "AppArmorProfile")) {
2235 p = &c->apparmor_profile;
2236 b = &c->apparmor_profile_ignore;
2237 } else { /* "SmackProcessLabel" */
2238 p = &c->smack_process_label;
2239 b = &c->smack_process_label_ignore;
2240 }
2241
2242 if (isempty(s)) {
2243 *p = mfree(*p);
2244 *b = false;
2245 } else {
2246 if (free_and_strdup(p, s) < 0)
2247 return -ENOMEM;
2248 *b = ignore;
2249 }
2250
2251 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "%s=%s%s", name, ignore ? "-" : "", strempty(s));
2252 }
2253
2254 return 1;
2255
2256 } else if (STR_IN_SET(name, "BindPaths", "BindReadOnlyPaths")) {
2257 const char *source, *destination;
2258 int ignore_enoent;
2259 uint64_t mount_flags;
2260 bool empty = true;
2261
2262 r = sd_bus_message_enter_container(message, 'a', "(ssbt)");
2263 if (r < 0)
2264 return r;
2265
2266 while ((r = sd_bus_message_read(message, "(ssbt)", &source, &destination, &ignore_enoent, &mount_flags)) > 0) {
2267
2268 if (!path_is_absolute(source))
2269 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Source path %s is not absolute.", source);
2270 if (!path_is_absolute(destination))
2271 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Destination path %s is not absolute.", destination);
2272 if (!IN_SET(mount_flags, 0, MS_REC))
2273 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown mount flags.");
2274
2275 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2276 r = bind_mount_add(&c->bind_mounts, &c->n_bind_mounts,
2277 &(BindMount) {
2278 .source = strdup(source),
2279 .destination = strdup(destination),
2280 .read_only = !!strstr(name, "ReadOnly"),
2281 .recursive = !!(mount_flags & MS_REC),
2282 .ignore_enoent = ignore_enoent,
2283 });
2284 if (r < 0)
2285 return r;
2286
2287 unit_write_settingf(
2288 u, flags|UNIT_ESCAPE_SPECIFIERS, name,
2289 "%s=%s%s:%s:%s",
2290 name,
2291 ignore_enoent ? "-" : "",
2292 source,
2293 destination,
2294 (mount_flags & MS_REC) ? "rbind" : "norbind");
2295 }
2296
2297 empty = false;
2298 }
2299 if (r < 0)
2300 return r;
2301
2302 r = sd_bus_message_exit_container(message);
2303 if (r < 0)
2304 return r;
2305
2306 if (empty) {
2307 bind_mount_free_many(c->bind_mounts, c->n_bind_mounts);
2308 c->bind_mounts = NULL;
2309 c->n_bind_mounts = 0;
2310
2311 unit_write_settingf(u, flags, name, "%s=", name);
2312 }
2313
2314 return 1;
2315
2316 } else if (streq(name, "TemporaryFileSystem")) {
2317 const char *path, *options;
2318 bool empty = true;
2319
2320 r = sd_bus_message_enter_container(message, 'a', "(ss)");
2321 if (r < 0)
2322 return r;
2323
2324 while ((r = sd_bus_message_read(message, "(ss)", &path, &options)) > 0) {
2325
2326 if (!path_is_absolute(path))
2327 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Mount point %s is not absolute.", path);
2328
2329 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2330 r = temporary_filesystem_add(&c->temporary_filesystems, &c->n_temporary_filesystems, path, options);
2331 if (r < 0)
2332 return r;
2333
2334 unit_write_settingf(
2335 u, flags|UNIT_ESCAPE_SPECIFIERS, name,
2336 "%s=%s:%s",
2337 name,
2338 path,
2339 options);
2340 }
2341
2342 empty = false;
2343 }
2344 if (r < 0)
2345 return r;
2346
2347 r = sd_bus_message_exit_container(message);
2348 if (r < 0)
2349 return r;
2350
2351 if (empty) {
2352 temporary_filesystem_free_many(c->temporary_filesystems, c->n_temporary_filesystems);
2353 c->temporary_filesystems = NULL;
2354 c->n_temporary_filesystems = 0;
2355
2356 unit_write_settingf(u, flags, name, "%s=", name);
2357 }
2358
2359 return 1;
2360
2361 } else if ((suffix = startswith(name, "Limit"))) {
2362 const char *soft = NULL;
2363 int ri;
2364
2365 ri = rlimit_from_string(suffix);
2366 if (ri < 0) {
2367 soft = endswith(suffix, "Soft");
2368 if (soft) {
2369 const char *n;
2370
2371 n = strndupa(suffix, soft - suffix);
2372 ri = rlimit_from_string(n);
2373 if (ri >= 0)
2374 name = strjoina("Limit", n);
2375 }
2376 }
2377
2378 if (ri >= 0) {
2379 uint64_t rl;
2380 rlim_t x;
2381
2382 r = sd_bus_message_read(message, "t", &rl);
2383 if (r < 0)
2384 return r;
2385
2386 if (rl == (uint64_t) -1)
2387 x = RLIM_INFINITY;
2388 else {
2389 x = (rlim_t) rl;
2390
2391 if ((uint64_t) x != rl)
2392 return -ERANGE;
2393 }
2394
2395 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2396 _cleanup_free_ char *f = NULL;
2397 struct rlimit nl;
2398
2399 if (c->rlimit[ri]) {
2400 nl = *c->rlimit[ri];
2401
2402 if (soft)
2403 nl.rlim_cur = x;
2404 else
2405 nl.rlim_max = x;
2406 } else
2407 /* When the resource limit is not initialized yet, then assign the value to both fields */
2408 nl = (struct rlimit) {
2409 .rlim_cur = x,
2410 .rlim_max = x,
2411 };
2412
2413 r = rlimit_format(&nl, &f);
2414 if (r < 0)
2415 return r;
2416
2417 if (c->rlimit[ri])
2418 *c->rlimit[ri] = nl;
2419 else {
2420 c->rlimit[ri] = newdup(struct rlimit, &nl, 1);
2421 if (!c->rlimit[ri])
2422 return -ENOMEM;
2423 }
2424
2425 unit_write_settingf(u, flags, name, "%s=%s", name, f);
2426 }
2427
2428 return 1;
2429 }
2430
2431 }
2432
2433 return 0;
2434 }