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