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