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