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