]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/dbus-execute.c
Merge pull request #14178 from poettering/journal-namespace
[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_WHITELIST | invert_flag,
1591 u->id,
1592 NULL, 0);
1593 if (r < 0)
1594 return r;
1595 }
1596 }
1597
1598 STRV_FOREACH(s, l) {
1599 _cleanup_free_ char *n = NULL;
1600 int e;
1601
1602 r = parse_syscall_and_errno(*s, &n, &e);
1603 if (r < 0)
1604 return r;
1605
1606 r = seccomp_parse_syscall_filter(n,
1607 e,
1608 c->syscall_filter,
1609 (c->syscall_whitelist ? SECCOMP_PARSE_WHITELIST : 0) | invert_flag,
1610 u->id,
1611 NULL, 0);
1612 if (r < 0)
1613 return r;
1614 }
1615
1616 joined = strv_join(l, " ");
1617 if (!joined)
1618 return -ENOMEM;
1619
1620 unit_write_settingf(u, flags, name, "SystemCallFilter=%s%s", whitelist ? "" : "~", joined);
1621 }
1622
1623 return 1;
1624
1625 } else if (streq(name, "SystemCallArchitectures")) {
1626 _cleanup_strv_free_ char **l = NULL;
1627
1628 r = sd_bus_message_read_strv(message, &l);
1629 if (r < 0)
1630 return r;
1631
1632 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1633 _cleanup_free_ char *joined = NULL;
1634
1635 if (strv_isempty(l))
1636 c->syscall_archs = set_free(c->syscall_archs);
1637 else {
1638 char **s;
1639
1640 r = set_ensure_allocated(&c->syscall_archs, NULL);
1641 if (r < 0)
1642 return r;
1643
1644 STRV_FOREACH(s, l) {
1645 uint32_t a;
1646
1647 r = seccomp_arch_from_string(*s, &a);
1648 if (r < 0)
1649 return r;
1650
1651 r = set_put(c->syscall_archs, UINT32_TO_PTR(a + 1));
1652 if (r < 0)
1653 return r;
1654 }
1655
1656 }
1657
1658 joined = strv_join(l, " ");
1659 if (!joined)
1660 return -ENOMEM;
1661
1662 unit_write_settingf(u, flags, name, "%s=%s", name, joined);
1663 }
1664
1665 return 1;
1666
1667 } else if (streq(name, "RestrictAddressFamilies")) {
1668 int whitelist;
1669 _cleanup_strv_free_ char **l = NULL;
1670
1671 r = sd_bus_message_enter_container(message, 'r', "bas");
1672 if (r < 0)
1673 return r;
1674
1675 r = sd_bus_message_read(message, "b", &whitelist);
1676 if (r < 0)
1677 return r;
1678
1679 r = sd_bus_message_read_strv(message, &l);
1680 if (r < 0)
1681 return r;
1682
1683 r = sd_bus_message_exit_container(message);
1684 if (r < 0)
1685 return r;
1686
1687 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1688 _cleanup_free_ char *joined = NULL;
1689 char **s;
1690
1691 if (strv_isempty(l)) {
1692 c->address_families_whitelist = false;
1693 c->address_families = set_free(c->address_families);
1694
1695 unit_write_settingf(u, flags, name, "RestrictAddressFamilies=");
1696 return 1;
1697 }
1698
1699 if (!c->address_families) {
1700 c->address_families = set_new(NULL);
1701 if (!c->address_families)
1702 return log_oom();
1703
1704 c->address_families_whitelist = whitelist;
1705 }
1706
1707 STRV_FOREACH(s, l) {
1708 int af;
1709
1710 af = af_from_name(*s);
1711 if (af < 0)
1712 return af;
1713
1714 if (whitelist == c->address_families_whitelist) {
1715 r = set_put(c->address_families, INT_TO_PTR(af));
1716 if (r < 0)
1717 return r;
1718 } else
1719 (void) set_remove(c->address_families, INT_TO_PTR(af));
1720 }
1721
1722 joined = strv_join(l, " ");
1723 if (!joined)
1724 return -ENOMEM;
1725
1726 unit_write_settingf(u, flags, name, "RestrictAddressFamilies=%s%s", whitelist ? "" : "~", joined);
1727 }
1728
1729 return 1;
1730 }
1731 #endif
1732 if (STR_IN_SET(name, "CPUAffinity", "NUMAMask")) {
1733 const void *a;
1734 size_t n;
1735 bool affinity = streq(name, "CPUAffinity");
1736 _cleanup_(cpu_set_reset) CPUSet set = {};
1737
1738 r = sd_bus_message_read_array(message, 'y', &a, &n);
1739 if (r < 0)
1740 return r;
1741
1742 r = cpu_set_from_dbus(a, n, &set);
1743 if (r < 0)
1744 return r;
1745
1746 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1747 if (n == 0) {
1748 cpu_set_reset(affinity ? &c->cpu_set : &c->numa_policy.nodes);
1749 unit_write_settingf(u, flags, name, "%s=", name);
1750 } else {
1751 _cleanup_free_ char *str = NULL;
1752
1753 str = cpu_set_to_string(&set);
1754 if (!str)
1755 return -ENOMEM;
1756
1757 /* We forego any optimizations here, and always create the structure using
1758 * cpu_set_add_all(), because we don't want to care if the existing size we
1759 * got over dbus is appropriate. */
1760 r = cpu_set_add_all(affinity ? &c->cpu_set : &c->numa_policy.nodes, &set);
1761 if (r < 0)
1762 return r;
1763
1764 unit_write_settingf(u, flags, name, "%s=%s", name, str);
1765 }
1766 }
1767
1768 return 1;
1769
1770 } else if (streq(name, "NUMAPolicy")) {
1771 int32_t type;
1772
1773 r = sd_bus_message_read(message, "i", &type);
1774 if (r < 0)
1775 return r;
1776
1777 if (!mpol_is_valid(type))
1778 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid NUMAPolicy value: %i", type);
1779
1780 if (!UNIT_WRITE_FLAGS_NOOP(flags))
1781 c->numa_policy.type = type;
1782
1783 return 1;
1784 } else if (streq(name, "Nice")) {
1785 int32_t q;
1786
1787 r = sd_bus_message_read(message, "i", &q);
1788 if (r < 0)
1789 return r;
1790
1791 if (!nice_is_valid(q))
1792 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid Nice value: %i", q);
1793
1794 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1795 c->nice = q;
1796 c->nice_set = true;
1797
1798 unit_write_settingf(u, flags, name, "Nice=%i", q);
1799 }
1800
1801 return 1;
1802
1803 } else if (streq(name, "CPUSchedulingPolicy")) {
1804 int32_t q;
1805
1806 r = sd_bus_message_read(message, "i", &q);
1807 if (r < 0)
1808 return r;
1809
1810 if (!sched_policy_is_valid(q))
1811 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid CPU scheduling policy: %i", q);
1812
1813 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1814 _cleanup_free_ char *s = NULL;
1815
1816 r = sched_policy_to_string_alloc(q, &s);
1817 if (r < 0)
1818 return r;
1819
1820 c->cpu_sched_policy = q;
1821 c->cpu_sched_priority = CLAMP(c->cpu_sched_priority, sched_get_priority_min(q), sched_get_priority_max(q));
1822 c->cpu_sched_set = true;
1823
1824 unit_write_settingf(u, flags, name, "CPUSchedulingPolicy=%s", s);
1825 }
1826
1827 return 1;
1828
1829 } else if (streq(name, "CPUSchedulingPriority")) {
1830 int32_t p, min, max;
1831
1832 r = sd_bus_message_read(message, "i", &p);
1833 if (r < 0)
1834 return r;
1835
1836 min = sched_get_priority_min(c->cpu_sched_policy);
1837 max = sched_get_priority_max(c->cpu_sched_policy);
1838 if (p < min || p > max)
1839 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid CPU scheduling priority: %i", p);
1840
1841 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1842 c->cpu_sched_priority = p;
1843 c->cpu_sched_set = true;
1844
1845 unit_write_settingf(u, flags, name, "CPUSchedulingPriority=%i", p);
1846 }
1847
1848 return 1;
1849
1850 } else if (streq(name, "IOSchedulingClass")) {
1851 int32_t q;
1852
1853 r = sd_bus_message_read(message, "i", &q);
1854 if (r < 0)
1855 return r;
1856
1857 if (!ioprio_class_is_valid(q))
1858 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid IO scheduling class: %i", q);
1859
1860 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1861 _cleanup_free_ char *s = NULL;
1862
1863 r = ioprio_class_to_string_alloc(q, &s);
1864 if (r < 0)
1865 return r;
1866
1867 c->ioprio = IOPRIO_PRIO_VALUE(q, IOPRIO_PRIO_DATA(c->ioprio));
1868 c->ioprio_set = true;
1869
1870 unit_write_settingf(u, flags, name, "IOSchedulingClass=%s", s);
1871 }
1872
1873 return 1;
1874
1875 } else if (streq(name, "IOSchedulingPriority")) {
1876 int32_t p;
1877
1878 r = sd_bus_message_read(message, "i", &p);
1879 if (r < 0)
1880 return r;
1881
1882 if (!ioprio_priority_is_valid(p))
1883 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid IO scheduling priority: %i", p);
1884
1885 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1886 c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_PRIO_CLASS(c->ioprio), p);
1887 c->ioprio_set = true;
1888
1889 unit_write_settingf(u, flags, name, "IOSchedulingPriority=%i", p);
1890 }
1891
1892 return 1;
1893
1894 } else if (streq(name, "WorkingDirectory")) {
1895 const char *s;
1896 bool missing_ok;
1897
1898 r = sd_bus_message_read(message, "s", &s);
1899 if (r < 0)
1900 return r;
1901
1902 if (s[0] == '-') {
1903 missing_ok = true;
1904 s++;
1905 } else
1906 missing_ok = false;
1907
1908 if (!isempty(s) && !streq(s, "~") && !path_is_absolute(s))
1909 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "WorkingDirectory= expects an absolute path or '~'");
1910
1911 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1912 if (streq(s, "~")) {
1913 c->working_directory = mfree(c->working_directory);
1914 c->working_directory_home = true;
1915 } else {
1916 r = free_and_strdup(&c->working_directory, empty_to_null(s));
1917 if (r < 0)
1918 return r;
1919
1920 c->working_directory_home = false;
1921 }
1922
1923 c->working_directory_missing_ok = missing_ok;
1924 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "WorkingDirectory=%s%s", missing_ok ? "-" : "", s);
1925 }
1926
1927 return 1;
1928
1929 } else if (STR_IN_SET(name,
1930 "StandardInputFileDescriptorName", "StandardOutputFileDescriptorName", "StandardErrorFileDescriptorName")) {
1931 const char *s;
1932
1933 r = sd_bus_message_read(message, "s", &s);
1934 if (r < 0)
1935 return r;
1936
1937 if (!isempty(s) && !fdname_is_valid(s))
1938 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid file descriptor name");
1939
1940 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1941
1942 if (streq(name, "StandardInputFileDescriptorName")) {
1943 r = free_and_strdup(c->stdio_fdname + STDIN_FILENO, empty_to_null(s));
1944 if (r < 0)
1945 return r;
1946
1947 c->std_input = EXEC_INPUT_NAMED_FD;
1948 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "StandardInput=fd:%s", exec_context_fdname(c, STDIN_FILENO));
1949
1950 } else if (streq(name, "StandardOutputFileDescriptorName")) {
1951 r = free_and_strdup(c->stdio_fdname + STDOUT_FILENO, empty_to_null(s));
1952 if (r < 0)
1953 return r;
1954
1955 c->std_output = EXEC_OUTPUT_NAMED_FD;
1956 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "StandardOutput=fd:%s", exec_context_fdname(c, STDOUT_FILENO));
1957
1958 } else {
1959 assert(streq(name, "StandardErrorFileDescriptorName"));
1960
1961 r = free_and_strdup(&c->stdio_fdname[STDERR_FILENO], empty_to_null(s));
1962 if (r < 0)
1963 return r;
1964
1965 c->std_error = EXEC_OUTPUT_NAMED_FD;
1966 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "StandardError=fd:%s", exec_context_fdname(c, STDERR_FILENO));
1967 }
1968 }
1969
1970 return 1;
1971
1972 } else if (STR_IN_SET(name,
1973 "StandardInputFile",
1974 "StandardOutputFile", "StandardOutputFileToAppend",
1975 "StandardErrorFile", "StandardErrorFileToAppend")) {
1976 const char *s;
1977
1978 r = sd_bus_message_read(message, "s", &s);
1979 if (r < 0)
1980 return r;
1981
1982 if (!isempty(s)) {
1983 if (!path_is_absolute(s))
1984 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path %s is not absolute", s);
1985 if (!path_is_normalized(s))
1986 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path %s is not normalized", s);
1987 }
1988
1989 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1990
1991 if (streq(name, "StandardInputFile")) {
1992 r = free_and_strdup(&c->stdio_file[STDIN_FILENO], empty_to_null(s));
1993 if (r < 0)
1994 return r;
1995
1996 c->std_input = EXEC_INPUT_FILE;
1997 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "StandardInput=file:%s", s);
1998
1999 } else if (STR_IN_SET(name, "StandardOutputFile", "StandardOutputFileToAppend")) {
2000 r = free_and_strdup(&c->stdio_file[STDOUT_FILENO], empty_to_null(s));
2001 if (r < 0)
2002 return r;
2003
2004 if (streq(name, "StandardOutputFile")) {
2005 c->std_output = EXEC_OUTPUT_FILE;
2006 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "StandardOutput=file:%s", s);
2007 } else {
2008 assert(streq(name, "StandardOutputFileToAppend"));
2009 c->std_output = EXEC_OUTPUT_FILE_APPEND;
2010 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "StandardOutput=append:%s", s);
2011 }
2012 } else {
2013 assert(STR_IN_SET(name, "StandardErrorFile", "StandardErrorFileToAppend"));
2014
2015 r = free_and_strdup(&c->stdio_file[STDERR_FILENO], empty_to_null(s));
2016 if (r < 0)
2017 return r;
2018
2019 if (streq(name, "StandardErrorFile")) {
2020 c->std_error = EXEC_OUTPUT_FILE;
2021 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "StandardError=file:%s", s);
2022 } else {
2023 assert(streq(name, "StandardErrorFileToAppend"));
2024 c->std_error = EXEC_OUTPUT_FILE_APPEND;
2025 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "StandardError=append:%s", s);
2026 }
2027 }
2028 }
2029
2030 return 1;
2031
2032 } else if (streq(name, "StandardInputData")) {
2033 const void *p;
2034 size_t sz;
2035
2036 r = sd_bus_message_read_array(message, 'y', &p, &sz);
2037 if (r < 0)
2038 return r;
2039
2040 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2041 _cleanup_free_ char *encoded = NULL;
2042
2043 if (sz == 0) {
2044 c->stdin_data = mfree(c->stdin_data);
2045 c->stdin_data_size = 0;
2046
2047 unit_write_settingf(u, flags, name, "StandardInputData=");
2048 } else {
2049 void *q;
2050 ssize_t n;
2051
2052 if (c->stdin_data_size + sz < c->stdin_data_size || /* check for overflow */
2053 c->stdin_data_size + sz > EXEC_STDIN_DATA_MAX)
2054 return -E2BIG;
2055
2056 n = base64mem(p, sz, &encoded);
2057 if (n < 0)
2058 return (int) n;
2059
2060 q = realloc(c->stdin_data, c->stdin_data_size + sz);
2061 if (!q)
2062 return -ENOMEM;
2063
2064 memcpy((uint8_t*) q + c->stdin_data_size, p, sz);
2065
2066 c->stdin_data = q;
2067 c->stdin_data_size += sz;
2068
2069 unit_write_settingf(u, flags, name, "StandardInputData=%s", encoded);
2070 }
2071 }
2072
2073 return 1;
2074
2075 } else if (streq(name, "Environment")) {
2076
2077 _cleanup_strv_free_ char **l = NULL;
2078
2079 r = sd_bus_message_read_strv(message, &l);
2080 if (r < 0)
2081 return r;
2082
2083 if (!strv_env_is_valid(l))
2084 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid environment block.");
2085
2086 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2087 if (strv_isempty(l)) {
2088 c->environment = strv_free(c->environment);
2089 unit_write_setting(u, flags, name, "Environment=");
2090 } else {
2091 _cleanup_free_ char *joined = NULL;
2092 char **e;
2093
2094 joined = unit_concat_strv(l, UNIT_ESCAPE_SPECIFIERS|UNIT_ESCAPE_C);
2095 if (!joined)
2096 return -ENOMEM;
2097
2098 e = strv_env_merge(2, c->environment, l);
2099 if (!e)
2100 return -ENOMEM;
2101
2102 strv_free_and_replace(c->environment, e);
2103 unit_write_settingf(u, flags, name, "Environment=%s", joined);
2104 }
2105 }
2106
2107 return 1;
2108
2109 } else if (streq(name, "UnsetEnvironment")) {
2110
2111 _cleanup_strv_free_ char **l = NULL;
2112
2113 r = sd_bus_message_read_strv(message, &l);
2114 if (r < 0)
2115 return r;
2116
2117 if (!strv_env_name_or_assignment_is_valid(l))
2118 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid UnsetEnvironment= list.");
2119
2120 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2121 if (strv_isempty(l)) {
2122 c->unset_environment = strv_free(c->unset_environment);
2123 unit_write_setting(u, flags, name, "UnsetEnvironment=");
2124 } else {
2125 _cleanup_free_ char *joined = NULL;
2126 char **e;
2127
2128 joined = unit_concat_strv(l, UNIT_ESCAPE_SPECIFIERS|UNIT_ESCAPE_C);
2129 if (!joined)
2130 return -ENOMEM;
2131
2132 e = strv_env_merge(2, c->unset_environment, l);
2133 if (!e)
2134 return -ENOMEM;
2135
2136 strv_free_and_replace(c->unset_environment, e);
2137 unit_write_settingf(u, flags, name, "UnsetEnvironment=%s", joined);
2138 }
2139 }
2140
2141 return 1;
2142
2143 } else if (streq(name, "OOMScoreAdjust")) {
2144 int oa;
2145
2146 r = sd_bus_message_read(message, "i", &oa);
2147 if (r < 0)
2148 return r;
2149
2150 if (!oom_score_adjust_is_valid(oa))
2151 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "OOM score adjust value out of range");
2152
2153 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2154 c->oom_score_adjust = oa;
2155 c->oom_score_adjust_set = true;
2156 unit_write_settingf(u, flags, name, "OOMScoreAdjust=%i", oa);
2157 }
2158
2159 return 1;
2160
2161 } else if (streq(name, "EnvironmentFiles")) {
2162
2163 _cleanup_free_ char *joined = NULL;
2164 _cleanup_fclose_ FILE *f = NULL;
2165 _cleanup_strv_free_ char **l = NULL;
2166 size_t size = 0;
2167 char **i;
2168
2169 r = sd_bus_message_enter_container(message, 'a', "(sb)");
2170 if (r < 0)
2171 return r;
2172
2173 f = open_memstream_unlocked(&joined, &size);
2174 if (!f)
2175 return -ENOMEM;
2176
2177 fputs("EnvironmentFile=\n", f);
2178
2179 STRV_FOREACH(i, c->environment_files) {
2180 _cleanup_free_ char *q = NULL;
2181
2182 q = specifier_escape(*i);
2183 if (!q)
2184 return -ENOMEM;
2185
2186 fprintf(f, "EnvironmentFile=%s\n", q);
2187 }
2188
2189 while ((r = sd_bus_message_enter_container(message, 'r', "sb")) > 0) {
2190 const char *path;
2191 int b;
2192
2193 r = sd_bus_message_read(message, "sb", &path, &b);
2194 if (r < 0)
2195 return r;
2196
2197 r = sd_bus_message_exit_container(message);
2198 if (r < 0)
2199 return r;
2200
2201 if (!path_is_absolute(path))
2202 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path %s is not absolute.", path);
2203
2204 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2205 _cleanup_free_ char *q = NULL, *buf = NULL;
2206
2207 buf = strjoin(b ? "-" : "", path);
2208 if (!buf)
2209 return -ENOMEM;
2210
2211 q = specifier_escape(buf);
2212 if (!q)
2213 return -ENOMEM;
2214
2215 fprintf(f, "EnvironmentFile=%s\n", q);
2216
2217 r = strv_consume(&l, TAKE_PTR(buf));
2218 if (r < 0)
2219 return r;
2220 }
2221 }
2222 if (r < 0)
2223 return r;
2224
2225 r = sd_bus_message_exit_container(message);
2226 if (r < 0)
2227 return r;
2228
2229 r = fflush_and_check(f);
2230 if (r < 0)
2231 return r;
2232
2233 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2234 if (strv_isempty(l)) {
2235 c->environment_files = strv_free(c->environment_files);
2236 unit_write_setting(u, flags, name, "EnvironmentFile=");
2237 } else {
2238 r = strv_extend_strv(&c->environment_files, l, true);
2239 if (r < 0)
2240 return r;
2241
2242 unit_write_setting(u, flags, name, joined);
2243 }
2244 }
2245
2246 return 1;
2247
2248 } else if (streq(name, "PassEnvironment")) {
2249
2250 _cleanup_strv_free_ char **l = NULL;
2251
2252 r = sd_bus_message_read_strv(message, &l);
2253 if (r < 0)
2254 return r;
2255
2256 if (!strv_env_name_is_valid(l))
2257 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid PassEnvironment= block.");
2258
2259 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2260 if (strv_isempty(l)) {
2261 c->pass_environment = strv_free(c->pass_environment);
2262 unit_write_setting(u, flags, name, "PassEnvironment=");
2263 } else {
2264 _cleanup_free_ char *joined = NULL;
2265
2266 r = strv_extend_strv(&c->pass_environment, l, true);
2267 if (r < 0)
2268 return r;
2269
2270 /* We write just the new settings out to file, with unresolved specifiers. */
2271 joined = unit_concat_strv(l, UNIT_ESCAPE_SPECIFIERS);
2272 if (!joined)
2273 return -ENOMEM;
2274
2275 unit_write_settingf(u, flags, name, "PassEnvironment=%s", joined);
2276 }
2277 }
2278
2279 return 1;
2280
2281 } else if (STR_IN_SET(name, "ReadWriteDirectories", "ReadOnlyDirectories", "InaccessibleDirectories",
2282 "ReadWritePaths", "ReadOnlyPaths", "InaccessiblePaths")) {
2283 _cleanup_strv_free_ char **l = NULL;
2284 char ***dirs;
2285 char **p;
2286
2287 r = sd_bus_message_read_strv(message, &l);
2288 if (r < 0)
2289 return r;
2290
2291 STRV_FOREACH(p, l) {
2292 char *i = *p;
2293 size_t offset;
2294
2295 offset = i[0] == '-';
2296 offset += i[offset] == '+';
2297 if (!path_is_absolute(i + offset))
2298 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid %s", name);
2299
2300 path_simplify(i + offset, false);
2301 }
2302
2303 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2304 if (STR_IN_SET(name, "ReadWriteDirectories", "ReadWritePaths"))
2305 dirs = &c->read_write_paths;
2306 else if (STR_IN_SET(name, "ReadOnlyDirectories", "ReadOnlyPaths"))
2307 dirs = &c->read_only_paths;
2308 else /* "InaccessiblePaths" */
2309 dirs = &c->inaccessible_paths;
2310
2311 if (strv_isempty(l)) {
2312 *dirs = strv_free(*dirs);
2313 unit_write_settingf(u, flags, name, "%s=", name);
2314 } else {
2315 _cleanup_free_ char *joined = NULL;
2316
2317 joined = unit_concat_strv(l, UNIT_ESCAPE_SPECIFIERS);
2318 if (!joined)
2319 return -ENOMEM;
2320
2321 r = strv_extend_strv(dirs, l, true);
2322 if (r < 0)
2323 return -ENOMEM;
2324
2325 unit_write_settingf(u, flags, name, "%s=%s", name, joined);
2326 }
2327 }
2328
2329 return 1;
2330
2331 } else if (STR_IN_SET(name, "RuntimeDirectory", "StateDirectory", "CacheDirectory", "LogsDirectory", "ConfigurationDirectory")) {
2332 _cleanup_strv_free_ char **l = NULL;
2333 char **p;
2334
2335 r = sd_bus_message_read_strv(message, &l);
2336 if (r < 0)
2337 return r;
2338
2339 STRV_FOREACH(p, l) {
2340 if (!path_is_normalized(*p))
2341 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "%s= path is not normalized: %s", name, *p);
2342
2343 if (path_is_absolute(*p))
2344 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "%s= path is absolute: %s", name, *p);
2345
2346 if (path_startswith(*p, "private"))
2347 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "%s= path can't be 'private': %s", name, *p);
2348 }
2349
2350 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2351 ExecDirectoryType i;
2352 ExecDirectory *d;
2353
2354 assert_se((i = exec_directory_type_from_string(name)) >= 0);
2355 d = c->directories + i;
2356
2357 if (strv_isempty(l)) {
2358 d->paths = strv_free(d->paths);
2359 unit_write_settingf(u, flags, name, "%s=", name);
2360 } else {
2361 _cleanup_free_ char *joined = NULL;
2362
2363 r = strv_extend_strv(&d->paths, l, true);
2364 if (r < 0)
2365 return r;
2366
2367 joined = unit_concat_strv(l, UNIT_ESCAPE_SPECIFIERS);
2368 if (!joined)
2369 return -ENOMEM;
2370
2371 unit_write_settingf(u, flags, name, "%s=%s", name, joined);
2372 }
2373 }
2374
2375 return 1;
2376
2377 } else if (STR_IN_SET(name, "AppArmorProfile", "SmackProcessLabel")) {
2378 int ignore;
2379 const char *s;
2380
2381 r = sd_bus_message_read(message, "(bs)", &ignore, &s);
2382 if (r < 0)
2383 return r;
2384
2385 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2386 char **p;
2387 bool *b;
2388
2389 if (streq(name, "AppArmorProfile")) {
2390 p = &c->apparmor_profile;
2391 b = &c->apparmor_profile_ignore;
2392 } else { /* "SmackProcessLabel" */
2393 p = &c->smack_process_label;
2394 b = &c->smack_process_label_ignore;
2395 }
2396
2397 if (isempty(s)) {
2398 *p = mfree(*p);
2399 *b = false;
2400 } else {
2401 if (free_and_strdup(p, s) < 0)
2402 return -ENOMEM;
2403 *b = ignore;
2404 }
2405
2406 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "%s=%s%s", name, ignore ? "-" : "", strempty(s));
2407 }
2408
2409 return 1;
2410
2411 } else if (STR_IN_SET(name, "BindPaths", "BindReadOnlyPaths")) {
2412 char *source, *destination;
2413 int ignore_enoent;
2414 uint64_t mount_flags;
2415 bool empty = true;
2416
2417 r = sd_bus_message_enter_container(message, 'a', "(ssbt)");
2418 if (r < 0)
2419 return r;
2420
2421 while ((r = sd_bus_message_read(message, "(ssbt)", &source, &destination, &ignore_enoent, &mount_flags)) > 0) {
2422
2423 if (!path_is_absolute(source))
2424 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Source path %s is not absolute.", source);
2425 if (!path_is_absolute(destination))
2426 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Destination path %s is not absolute.", destination);
2427 if (!IN_SET(mount_flags, 0, MS_REC))
2428 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown mount flags.");
2429
2430 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2431 r = bind_mount_add(&c->bind_mounts, &c->n_bind_mounts,
2432 &(BindMount) {
2433 .source = source,
2434 .destination = destination,
2435 .read_only = !!strstr(name, "ReadOnly"),
2436 .recursive = !!(mount_flags & MS_REC),
2437 .ignore_enoent = ignore_enoent,
2438 });
2439 if (r < 0)
2440 return r;
2441
2442 unit_write_settingf(
2443 u, flags|UNIT_ESCAPE_SPECIFIERS, name,
2444 "%s=%s%s:%s:%s",
2445 name,
2446 ignore_enoent ? "-" : "",
2447 source,
2448 destination,
2449 (mount_flags & MS_REC) ? "rbind" : "norbind");
2450 }
2451
2452 empty = false;
2453 }
2454 if (r < 0)
2455 return r;
2456
2457 r = sd_bus_message_exit_container(message);
2458 if (r < 0)
2459 return r;
2460
2461 if (empty) {
2462 bind_mount_free_many(c->bind_mounts, c->n_bind_mounts);
2463 c->bind_mounts = NULL;
2464 c->n_bind_mounts = 0;
2465
2466 unit_write_settingf(u, flags, name, "%s=", name);
2467 }
2468
2469 return 1;
2470
2471 } else if (streq(name, "TemporaryFileSystem")) {
2472 const char *path, *options;
2473 bool empty = true;
2474
2475 r = sd_bus_message_enter_container(message, 'a', "(ss)");
2476 if (r < 0)
2477 return r;
2478
2479 while ((r = sd_bus_message_read(message, "(ss)", &path, &options)) > 0) {
2480
2481 if (!path_is_absolute(path))
2482 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Mount point %s is not absolute.", path);
2483
2484 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2485 r = temporary_filesystem_add(&c->temporary_filesystems, &c->n_temporary_filesystems, path, options);
2486 if (r < 0)
2487 return r;
2488
2489 unit_write_settingf(
2490 u, flags|UNIT_ESCAPE_SPECIFIERS, name,
2491 "%s=%s:%s",
2492 name,
2493 path,
2494 options);
2495 }
2496
2497 empty = false;
2498 }
2499 if (r < 0)
2500 return r;
2501
2502 r = sd_bus_message_exit_container(message);
2503 if (r < 0)
2504 return r;
2505
2506 if (empty) {
2507 temporary_filesystem_free_many(c->temporary_filesystems, c->n_temporary_filesystems);
2508 c->temporary_filesystems = NULL;
2509 c->n_temporary_filesystems = 0;
2510
2511 unit_write_settingf(u, flags, name, "%s=", name);
2512 }
2513
2514 return 1;
2515
2516 } else if ((suffix = startswith(name, "Limit"))) {
2517 const char *soft = NULL;
2518 int ri;
2519
2520 ri = rlimit_from_string(suffix);
2521 if (ri < 0) {
2522 soft = endswith(suffix, "Soft");
2523 if (soft) {
2524 const char *n;
2525
2526 n = strndupa(suffix, soft - suffix);
2527 ri = rlimit_from_string(n);
2528 if (ri >= 0)
2529 name = strjoina("Limit", n);
2530 }
2531 }
2532
2533 if (ri >= 0) {
2534 uint64_t rl;
2535 rlim_t x;
2536
2537 r = sd_bus_message_read(message, "t", &rl);
2538 if (r < 0)
2539 return r;
2540
2541 if (rl == (uint64_t) -1)
2542 x = RLIM_INFINITY;
2543 else {
2544 x = (rlim_t) rl;
2545
2546 if ((uint64_t) x != rl)
2547 return -ERANGE;
2548 }
2549
2550 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2551 _cleanup_free_ char *f = NULL;
2552 struct rlimit nl;
2553
2554 if (c->rlimit[ri]) {
2555 nl = *c->rlimit[ri];
2556
2557 if (soft)
2558 nl.rlim_cur = x;
2559 else
2560 nl.rlim_max = x;
2561 } else
2562 /* When the resource limit is not initialized yet, then assign the value to both fields */
2563 nl = (struct rlimit) {
2564 .rlim_cur = x,
2565 .rlim_max = x,
2566 };
2567
2568 r = rlimit_format(&nl, &f);
2569 if (r < 0)
2570 return r;
2571
2572 if (c->rlimit[ri])
2573 *c->rlimit[ri] = nl;
2574 else {
2575 c->rlimit[ri] = newdup(struct rlimit, &nl, 1);
2576 if (!c->rlimit[ri])
2577 return -ENOMEM;
2578 }
2579
2580 unit_write_settingf(u, flags, name, "%s=%s", name, f);
2581 }
2582
2583 return 1;
2584 }
2585
2586 }
2587
2588 return 0;
2589 }