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