]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/dbus-execute.c
util-lib: drop json parser
[thirdparty/systemd.git] / src / core / dbus-execute.c
CommitLineData
4139c1b2
LP
1/***
2 This file is part of systemd.
3
4 Copyright 2010 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
5430f7f2
LP
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
4139c1b2
LP
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5430f7f2 14 Lesser General Public License for more details.
4139c1b2 15
5430f7f2 16 You should have received a copy of the GNU Lesser General Public License
4139c1b2
LP
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
82c121a4 20#include <sys/prctl.h>
4139c1b2 21
57183d11
LP
22#ifdef HAVE_SECCOMP
23#include <seccomp.h>
24#endif
25
3ffd4af2 26#include "af-list.h"
b5efdb8a 27#include "alloc-util.h"
718db961 28#include "bus-util.h"
430f0182 29#include "capability-util.h"
3ffd4af2 30#include "dbus-execute.h"
c7040b5d 31#include "env-util.h"
3ffd4af2
LP
32#include "execute.h"
33#include "fd-util.h"
34#include "fileio.h"
35#include "ioprio.h"
36#include "missing.h"
417116f2 37#include "namespace.h"
6bedfcbb 38#include "parse-util.h"
9b15b784 39#include "path-util.h"
7b3e062c 40#include "process-util.h"
78f22b97 41#include "rlimit-util.h"
57183d11
LP
42#ifdef HAVE_SECCOMP
43#include "seccomp-util.h"
44#endif
6bedfcbb 45#include "strv.h"
7ccbd1ae 46#include "syslog-util.h"
6bedfcbb 47#include "utf8.h"
57183d11 48
718db961 49BUS_DEFINE_PROPERTY_GET_ENUM(bus_property_get_exec_output, exec_output, ExecOutput);
82c121a4 50
718db961 51static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_exec_input, exec_input, ExecInput);
8c7be95e 52
023a4f67
LP
53static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_exec_utmp_mode, exec_utmp_mode, ExecUtmpMode);
54
1b8689f9
LP
55static BUS_DEFINE_PROPERTY_GET_ENUM(bus_property_get_protect_home, protect_home, ProtectHome);
56static BUS_DEFINE_PROPERTY_GET_ENUM(bus_property_get_protect_system, protect_system, ProtectSystem);
417116f2 57
718db961
LP
58static int property_get_environment_files(
59 sd_bus *bus,
60 const char *path,
61 const char *interface,
62 const char *property,
63 sd_bus_message *reply,
ebcf1f97
LP
64 void *userdata,
65 sd_bus_error *error) {
8c7be95e 66
718db961
LP
67 ExecContext *c = userdata;
68 char **j;
69 int r;
8c7be95e 70
718db961
LP
71 assert(bus);
72 assert(reply);
73 assert(c);
74
75 r = sd_bus_message_open_container(reply, 'a', "(sb)");
76 if (r < 0)
77 return r;
8c7be95e 78
718db961
LP
79 STRV_FOREACH(j, c->environment_files) {
80 const char *fn = *j;
8c7be95e 81
718db961
LP
82 r = sd_bus_message_append(reply, "(sb)", fn[0] == '-' ? fn + 1 : fn, fn[0] == '-');
83 if (r < 0)
84 return r;
8c7be95e
LP
85 }
86
718db961
LP
87 return sd_bus_message_close_container(reply);
88}
89
718db961
LP
90static int property_get_oom_score_adjust(
91 sd_bus *bus,
92 const char *path,
93 const char *interface,
94 const char *property,
95 sd_bus_message *reply,
ebcf1f97
LP
96 void *userdata,
97 sd_bus_error *error) {
718db961
LP
98
99
100 ExecContext *c = userdata;
82c121a4
LP
101 int32_t n;
102
718db961
LP
103 assert(bus);
104 assert(reply);
82c121a4
LP
105 assert(c);
106
dd6c17b1
LP
107 if (c->oom_score_adjust_set)
108 n = c->oom_score_adjust;
82c121a4 109 else {
68eda4bd 110 _cleanup_free_ char *t = NULL;
82c121a4
LP
111
112 n = 0;
718db961 113 if (read_one_line_file("/proc/self/oom_score_adj", &t) >= 0)
e4e73a63 114 safe_atoi32(t, &n);
82c121a4
LP
115 }
116
718db961 117 return sd_bus_message_append(reply, "i", n);
82c121a4
LP
118}
119
718db961
LP
120static int property_get_nice(
121 sd_bus *bus,
122 const char *path,
123 const char *interface,
124 const char *property,
125 sd_bus_message *reply,
ebcf1f97
LP
126 void *userdata,
127 sd_bus_error *error) {
718db961
LP
128
129
130 ExecContext *c = userdata;
82c121a4
LP
131 int32_t n;
132
718db961
LP
133 assert(bus);
134 assert(reply);
82c121a4
LP
135 assert(c);
136
137 if (c->nice_set)
138 n = c->nice;
718db961
LP
139 else {
140 errno = 0;
82c121a4 141 n = getpriority(PRIO_PROCESS, 0);
b3267152 142 if (errno > 0)
718db961
LP
143 n = 0;
144 }
82c121a4 145
718db961 146 return sd_bus_message_append(reply, "i", n);
82c121a4
LP
147}
148
718db961
LP
149static int property_get_ioprio(
150 sd_bus *bus,
151 const char *path,
152 const char *interface,
153 const char *property,
154 sd_bus_message *reply,
ebcf1f97
LP
155 void *userdata,
156 sd_bus_error *error) {
718db961
LP
157
158
159 ExecContext *c = userdata;
82c121a4
LP
160 int32_t n;
161
718db961
LP
162 assert(bus);
163 assert(reply);
82c121a4
LP
164 assert(c);
165
166 if (c->ioprio_set)
167 n = c->ioprio;
718db961 168 else {
82c121a4 169 n = ioprio_get(IOPRIO_WHO_PROCESS, 0);
718db961
LP
170 if (n < 0)
171 n = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 4);
172 }
82c121a4 173
718db961 174 return sd_bus_message_append(reply, "i", n);
82c121a4
LP
175}
176
718db961
LP
177static int property_get_cpu_sched_policy(
178 sd_bus *bus,
179 const char *path,
180 const char *interface,
181 const char *property,
182 sd_bus_message *reply,
ebcf1f97
LP
183 void *userdata,
184 sd_bus_error *error) {
718db961
LP
185
186 ExecContext *c = userdata;
82c121a4
LP
187 int32_t n;
188
718db961
LP
189 assert(bus);
190 assert(reply);
82c121a4
LP
191 assert(c);
192
193 if (c->cpu_sched_set)
194 n = c->cpu_sched_policy;
718db961 195 else {
82c121a4 196 n = sched_getscheduler(0);
718db961
LP
197 if (n < 0)
198 n = SCHED_OTHER;
199 }
82c121a4 200
718db961 201 return sd_bus_message_append(reply, "i", n);
82c121a4
LP
202}
203
718db961
LP
204static int property_get_cpu_sched_priority(
205 sd_bus *bus,
206 const char *path,
207 const char *interface,
208 const char *property,
209 sd_bus_message *reply,
ebcf1f97
LP
210 void *userdata,
211 sd_bus_error *error) {
718db961
LP
212
213 ExecContext *c = userdata;
82c121a4
LP
214 int32_t n;
215
718db961
LP
216 assert(bus);
217 assert(reply);
82c121a4
LP
218 assert(c);
219
220 if (c->cpu_sched_set)
221 n = c->cpu_sched_priority;
222 else {
b92bea5d 223 struct sched_param p = {};
82c121a4 224
82c121a4
LP
225 if (sched_getparam(0, &p) >= 0)
226 n = p.sched_priority;
e62d8c39
ZJS
227 else
228 n = 0;
82c121a4
LP
229 }
230
718db961 231 return sd_bus_message_append(reply, "i", n);
82c121a4
LP
232}
233
718db961
LP
234static int property_get_cpu_affinity(
235 sd_bus *bus,
236 const char *path,
237 const char *interface,
238 const char *property,
239 sd_bus_message *reply,
ebcf1f97
LP
240 void *userdata,
241 sd_bus_error *error) {
82c121a4 242
718db961 243 ExecContext *c = userdata;
82c121a4 244
718db961
LP
245 assert(bus);
246 assert(reply);
247 assert(c);
82c121a4
LP
248
249 if (c->cpuset)
718db961 250 return sd_bus_message_append_array(reply, 'y', c->cpuset, CPU_ALLOC_SIZE(c->cpuset_ncpus));
82c121a4 251 else
718db961 252 return sd_bus_message_append_array(reply, 'y', NULL, 0);
82c121a4
LP
253}
254
718db961
LP
255static int property_get_timer_slack_nsec(
256 sd_bus *bus,
257 const char *path,
258 const char *interface,
259 const char *property,
260 sd_bus_message *reply,
ebcf1f97
LP
261 void *userdata,
262 sd_bus_error *error) {
718db961
LP
263
264 ExecContext *c = userdata;
82c121a4
LP
265 uint64_t u;
266
718db961
LP
267 assert(bus);
268 assert(reply);
82c121a4
LP
269 assert(c);
270
3a43da28 271 if (c->timer_slack_nsec != NSEC_INFINITY)
03fae018 272 u = (uint64_t) c->timer_slack_nsec;
82c121a4
LP
273 else
274 u = (uint64_t) prctl(PR_GET_TIMERSLACK);
275
718db961 276 return sd_bus_message_append(reply, "t", u);
82c121a4
LP
277}
278
718db961
LP
279static int property_get_capability_bounding_set(
280 sd_bus *bus,
281 const char *path,
282 const char *interface,
283 const char *property,
284 sd_bus_message *reply,
ebcf1f97
LP
285 void *userdata,
286 sd_bus_error *error) {
260abb78 287
718db961
LP
288 ExecContext *c = userdata;
289
290 assert(bus);
291 assert(reply);
260abb78
LP
292 assert(c);
293
a103496c 294 return sd_bus_message_append(reply, "t", c->capability_bounding_set);
260abb78
LP
295}
296
755d4b67
IP
297static int property_get_ambient_capabilities(
298 sd_bus *bus,
299 const char *path,
300 const char *interface,
301 const char *property,
302 sd_bus_message *reply,
303 void *userdata,
304 sd_bus_error *error) {
305
306 ExecContext *c = userdata;
307
308 assert(bus);
309 assert(reply);
310 assert(c);
311
312 return sd_bus_message_append(reply, "t", c->capability_ambient_set);
313}
314
718db961
LP
315static int property_get_capabilities(
316 sd_bus *bus,
317 const char *path,
318 const char *interface,
319 const char *property,
320 sd_bus_message *reply,
ebcf1f97
LP
321 void *userdata,
322 sd_bus_error *error) {
718db961
LP
323
324 ExecContext *c = userdata;
5ce70e5b 325 _cleanup_cap_free_charp_ char *t = NULL;
82c121a4 326 const char *s;
82c121a4 327
718db961
LP
328 assert(bus);
329 assert(reply);
82c121a4
LP
330 assert(c);
331
332 if (c->capabilities)
333 s = t = cap_to_text(c->capabilities, NULL);
334 else
335 s = "";
336
674cdd19 337 if (!s)
82c121a4
LP
338 return -ENOMEM;
339
5ce70e5b 340 return sd_bus_message_append(reply, "s", s);
82c121a4
LP
341}
342
718db961
LP
343static int property_get_syscall_filter(
344 sd_bus *bus,
345 const char *path,
346 const char *interface,
347 const char *property,
348 sd_bus_message *reply,
ebcf1f97
LP
349 void *userdata,
350 sd_bus_error *error) {
82c121a4 351
17df7223
LP
352 ExecContext *c = userdata;
353 _cleanup_strv_free_ char **l = NULL;
57183d11
LP
354 int r;
355
351a19b1 356#ifdef HAVE_SECCOMP
17df7223
LP
357 Iterator i;
358 void *id;
351a19b1 359#endif
17df7223
LP
360
361 assert(bus);
362 assert(reply);
363 assert(c);
364
57183d11
LP
365 r = sd_bus_message_open_container(reply, 'r', "bas");
366 if (r < 0)
367 return r;
368
369 r = sd_bus_message_append(reply, "b", c->syscall_whitelist);
370 if (r < 0)
371 return r;
372
351a19b1 373#ifdef HAVE_SECCOMP
17df7223
LP
374 SET_FOREACH(id, c->syscall_filter, i) {
375 char *name;
376
377 name = seccomp_syscall_resolve_num_arch(SCMP_ARCH_NATIVE, PTR_TO_INT(id) - 1);
378 if (!name)
379 continue;
380
6e18964d
ZJS
381 r = strv_consume(&l, name);
382 if (r < 0)
383 return r;
17df7223 384 }
351a19b1 385#endif
17df7223
LP
386
387 strv_sort(l);
388
57183d11
LP
389 r = sd_bus_message_append_strv(reply, l);
390 if (r < 0)
391 return r;
17df7223 392
57183d11
LP
393 return sd_bus_message_close_container(reply);
394}
17df7223 395
57183d11
LP
396static int property_get_syscall_archs(
397 sd_bus *bus,
398 const char *path,
399 const char *interface,
400 const char *property,
401 sd_bus_message *reply,
402 void *userdata,
403 sd_bus_error *error) {
404
405 ExecContext *c = userdata;
406 _cleanup_strv_free_ char **l = NULL;
407 int r;
408
409#ifdef HAVE_SECCOMP
410 Iterator i;
411 void *id;
412#endif
413
414 assert(bus);
415 assert(reply);
416 assert(c);
17df7223 417
57183d11
LP
418#ifdef HAVE_SECCOMP
419 SET_FOREACH(id, c->syscall_archs, i) {
420 const char *name;
421
422 name = seccomp_arch_to_string(PTR_TO_UINT32(id) - 1);
423 if (!name)
424 continue;
425
426 r = strv_extend(&l, name);
427 if (r < 0)
428 return -ENOMEM;
17df7223 429 }
57183d11
LP
430#endif
431
432 strv_sort(l);
433
434 r = sd_bus_message_append_strv(reply, l);
435 if (r < 0)
436 return r;
17df7223 437
57183d11 438 return 0;
17df7223
LP
439}
440
441static int property_get_syscall_errno(
442 sd_bus *bus,
443 const char *path,
444 const char *interface,
445 const char *property,
446 sd_bus_message *reply,
447 void *userdata,
448 sd_bus_error *error) {
449
718db961 450 ExecContext *c = userdata;
82c121a4 451
718db961
LP
452 assert(bus);
453 assert(reply);
454 assert(c);
82c121a4 455
17df7223 456 return sd_bus_message_append(reply, "i", (int32_t) c->syscall_errno);
718db961 457}
82c121a4 458
5f8640fb
LP
459static int property_get_selinux_context(
460 sd_bus *bus,
461 const char *path,
462 const char *interface,
463 const char *property,
464 sd_bus_message *reply,
465 void *userdata,
466 sd_bus_error *error) {
467
468 ExecContext *c = userdata;
469
470 assert(bus);
471 assert(reply);
472 assert(c);
473
474 return sd_bus_message_append(reply, "(bs)", c->selinux_context_ignore, c->selinux_context);
475}
476
eef65bf3
MS
477static int property_get_apparmor_profile(
478 sd_bus *bus,
479 const char *path,
480 const char *interface,
481 const char *property,
482 sd_bus_message *reply,
483 void *userdata,
484 sd_bus_error *error) {
485
486 ExecContext *c = userdata;
487
488 assert(bus);
489 assert(reply);
490 assert(c);
491
492 return sd_bus_message_append(reply, "(bs)", c->apparmor_profile_ignore, c->apparmor_profile);
493}
494
2ca620c4
WC
495static int property_get_smack_process_label(
496 sd_bus *bus,
497 const char *path,
498 const char *interface,
499 const char *property,
500 sd_bus_message *reply,
501 void *userdata,
502 sd_bus_error *error) {
503
504 ExecContext *c = userdata;
505
506 assert(bus);
507 assert(reply);
508 assert(c);
509
510 return sd_bus_message_append(reply, "(bs)", c->smack_process_label_ignore, c->smack_process_label);
511}
512
ac45f971
LP
513static int property_get_personality(
514 sd_bus *bus,
515 const char *path,
516 const char *interface,
517 const char *property,
518 sd_bus_message *reply,
519 void *userdata,
520 sd_bus_error *error) {
521
522 ExecContext *c = userdata;
523
524 assert(bus);
525 assert(reply);
526 assert(c);
527
528 return sd_bus_message_append(reply, "s", personality_to_string(c->personality));
529}
530
4298d0b5
LP
531static int property_get_address_families(
532 sd_bus *bus,
533 const char *path,
534 const char *interface,
535 const char *property,
536 sd_bus_message *reply,
537 void *userdata,
538 sd_bus_error *error) {
539
540 ExecContext *c = userdata;
541 _cleanup_strv_free_ char **l = NULL;
542 Iterator i;
543 void *af;
544 int r;
545
546 assert(bus);
547 assert(reply);
548 assert(c);
549
550 r = sd_bus_message_open_container(reply, 'r', "bas");
551 if (r < 0)
552 return r;
553
554 r = sd_bus_message_append(reply, "b", c->address_families_whitelist);
555 if (r < 0)
556 return r;
557
558 SET_FOREACH(af, c->address_families, i) {
559 const char *name;
560
561 name = af_to_name(PTR_TO_INT(af));
562 if (!name)
563 continue;
564
565 r = strv_extend(&l, name);
566 if (r < 0)
567 return -ENOMEM;
568 }
569
570 strv_sort(l);
571
572 r = sd_bus_message_append_strv(reply, l);
573 if (r < 0)
574 return r;
575
576 return sd_bus_message_close_container(reply);
577}
578
5f5d8eab
LP
579static int property_get_working_directory(
580 sd_bus *bus,
581 const char *path,
582 const char *interface,
583 const char *property,
584 sd_bus_message *reply,
585 void *userdata,
586 sd_bus_error *error) {
587
588 ExecContext *c = userdata;
589 const char *wd;
590
591 assert(bus);
592 assert(reply);
593 assert(c);
594
595 if (c->working_directory_home)
596 wd = "~";
597 else
598 wd = c->working_directory;
599
600 if (c->working_directory_missing_ok)
601 wd = strjoina("!", wd);
602
603 return sd_bus_message_append(reply, "s", wd);
604}
605
06f2ccf9
EV
606static int property_get_syslog_level(
607 sd_bus *bus,
608 const char *path,
609 const char *interface,
610 const char *property,
611 sd_bus_message *reply,
612 void *userdata,
613 sd_bus_error *error) {
614
615 ExecContext *c = userdata;
616
617 assert(bus);
618 assert(reply);
619 assert(c);
620
621 return sd_bus_message_append(reply, "i", LOG_PRI(c->syslog_priority));
622}
623
624static int property_get_syslog_facility(
625 sd_bus *bus,
626 const char *path,
627 const char *interface,
628 const char *property,
629 sd_bus_message *reply,
630 void *userdata,
631 sd_bus_error *error) {
632
633 ExecContext *c = userdata;
634
635 assert(bus);
636 assert(reply);
637 assert(c);
638
639 return sd_bus_message_append(reply, "i", LOG_FAC(c->syslog_priority));
640}
641
718db961
LP
642const sd_bus_vtable bus_exec_vtable[] = {
643 SD_BUS_VTABLE_START(0),
556089dc
LP
644 SD_BUS_PROPERTY("Environment", "as", NULL, offsetof(ExecContext, environment), SD_BUS_VTABLE_PROPERTY_CONST),
645 SD_BUS_PROPERTY("EnvironmentFiles", "a(sb)", property_get_environment_files, 0, SD_BUS_VTABLE_PROPERTY_CONST),
b4c14404 646 SD_BUS_PROPERTY("PassEnvironment", "as", NULL, offsetof(ExecContext, pass_environment), SD_BUS_VTABLE_PROPERTY_CONST),
556089dc 647 SD_BUS_PROPERTY("UMask", "u", bus_property_get_mode, offsetof(ExecContext, umask), SD_BUS_VTABLE_PROPERTY_CONST),
c9d031c3 648 SD_BUS_PROPERTY("LimitCPU", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_CPU]), SD_BUS_VTABLE_PROPERTY_CONST),
147f6858 649 SD_BUS_PROPERTY("LimitCPUSoft", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_CPU]), SD_BUS_VTABLE_PROPERTY_CONST),
c9d031c3 650 SD_BUS_PROPERTY("LimitFSIZE", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_FSIZE]), SD_BUS_VTABLE_PROPERTY_CONST),
147f6858 651 SD_BUS_PROPERTY("LimitFSIZESoft", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_FSIZE]), SD_BUS_VTABLE_PROPERTY_CONST),
c9d031c3 652 SD_BUS_PROPERTY("LimitDATA", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_DATA]), SD_BUS_VTABLE_PROPERTY_CONST),
147f6858 653 SD_BUS_PROPERTY("LimitDATASoft", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_DATA]), SD_BUS_VTABLE_PROPERTY_CONST),
c9d031c3 654 SD_BUS_PROPERTY("LimitSTACK", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_STACK]), SD_BUS_VTABLE_PROPERTY_CONST),
147f6858 655 SD_BUS_PROPERTY("LimitSTACKSoft", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_STACK]), SD_BUS_VTABLE_PROPERTY_CONST),
c9d031c3 656 SD_BUS_PROPERTY("LimitCORE", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_CORE]), SD_BUS_VTABLE_PROPERTY_CONST),
147f6858 657 SD_BUS_PROPERTY("LimitCORESoft", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_CORE]), SD_BUS_VTABLE_PROPERTY_CONST),
c9d031c3 658 SD_BUS_PROPERTY("LimitRSS", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_RSS]), SD_BUS_VTABLE_PROPERTY_CONST),
147f6858 659 SD_BUS_PROPERTY("LimitRSSSoft", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_RSS]), SD_BUS_VTABLE_PROPERTY_CONST),
c9d031c3 660 SD_BUS_PROPERTY("LimitNOFILE", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_NOFILE]), SD_BUS_VTABLE_PROPERTY_CONST),
147f6858 661 SD_BUS_PROPERTY("LimitNOFILESoft", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_NOFILE]), SD_BUS_VTABLE_PROPERTY_CONST),
c9d031c3 662 SD_BUS_PROPERTY("LimitAS", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_AS]), SD_BUS_VTABLE_PROPERTY_CONST),
147f6858 663 SD_BUS_PROPERTY("LimitASSoft", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_AS]), SD_BUS_VTABLE_PROPERTY_CONST),
c9d031c3 664 SD_BUS_PROPERTY("LimitNPROC", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_NPROC]), SD_BUS_VTABLE_PROPERTY_CONST),
147f6858 665 SD_BUS_PROPERTY("LimitNPROCSoft", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_NPROC]), SD_BUS_VTABLE_PROPERTY_CONST),
c9d031c3 666 SD_BUS_PROPERTY("LimitMEMLOCK", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_MEMLOCK]), SD_BUS_VTABLE_PROPERTY_CONST),
147f6858 667 SD_BUS_PROPERTY("LimitMEMLOCKSoft", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_MEMLOCK]), SD_BUS_VTABLE_PROPERTY_CONST),
c9d031c3 668 SD_BUS_PROPERTY("LimitLOCKS", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_LOCKS]), SD_BUS_VTABLE_PROPERTY_CONST),
147f6858 669 SD_BUS_PROPERTY("LimitLOCKSSoft", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_LOCKS]), SD_BUS_VTABLE_PROPERTY_CONST),
c9d031c3 670 SD_BUS_PROPERTY("LimitSIGPENDING", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_SIGPENDING]), SD_BUS_VTABLE_PROPERTY_CONST),
147f6858 671 SD_BUS_PROPERTY("LimitSIGPENDINGSoft", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_SIGPENDING]), SD_BUS_VTABLE_PROPERTY_CONST),
c9d031c3 672 SD_BUS_PROPERTY("LimitMSGQUEUE", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_MSGQUEUE]), SD_BUS_VTABLE_PROPERTY_CONST),
147f6858 673 SD_BUS_PROPERTY("LimitMSGQUEUESoft", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_MSGQUEUE]), SD_BUS_VTABLE_PROPERTY_CONST),
c9d031c3 674 SD_BUS_PROPERTY("LimitNICE", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_NICE]), SD_BUS_VTABLE_PROPERTY_CONST),
147f6858 675 SD_BUS_PROPERTY("LimitNICESoft", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_NICE]), SD_BUS_VTABLE_PROPERTY_CONST),
c9d031c3 676 SD_BUS_PROPERTY("LimitRTPRIO", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_RTPRIO]), SD_BUS_VTABLE_PROPERTY_CONST),
147f6858 677 SD_BUS_PROPERTY("LimitRTPRIOSoft", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_RTPRIO]), SD_BUS_VTABLE_PROPERTY_CONST),
c9d031c3 678 SD_BUS_PROPERTY("LimitRTTIME", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_RTTIME]), SD_BUS_VTABLE_PROPERTY_CONST),
147f6858 679 SD_BUS_PROPERTY("LimitRTTIMESoft", "t", bus_property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_RTTIME]), SD_BUS_VTABLE_PROPERTY_CONST),
5f5d8eab 680 SD_BUS_PROPERTY("WorkingDirectory", "s", property_get_working_directory, 0, SD_BUS_VTABLE_PROPERTY_CONST),
556089dc
LP
681 SD_BUS_PROPERTY("RootDirectory", "s", NULL, offsetof(ExecContext, root_directory), SD_BUS_VTABLE_PROPERTY_CONST),
682 SD_BUS_PROPERTY("OOMScoreAdjust", "i", property_get_oom_score_adjust, 0, SD_BUS_VTABLE_PROPERTY_CONST),
683 SD_BUS_PROPERTY("Nice", "i", property_get_nice, 0, SD_BUS_VTABLE_PROPERTY_CONST),
684 SD_BUS_PROPERTY("IOScheduling", "i", property_get_ioprio, 0, SD_BUS_VTABLE_PROPERTY_CONST),
685 SD_BUS_PROPERTY("CPUSchedulingPolicy", "i", property_get_cpu_sched_policy, 0, SD_BUS_VTABLE_PROPERTY_CONST),
686 SD_BUS_PROPERTY("CPUSchedulingPriority", "i", property_get_cpu_sched_priority, 0, SD_BUS_VTABLE_PROPERTY_CONST),
687 SD_BUS_PROPERTY("CPUAffinity", "ay", property_get_cpu_affinity, 0, SD_BUS_VTABLE_PROPERTY_CONST),
688 SD_BUS_PROPERTY("TimerSlackNSec", "t", property_get_timer_slack_nsec, 0, SD_BUS_VTABLE_PROPERTY_CONST),
689 SD_BUS_PROPERTY("CPUSchedulingResetOnFork", "b", bus_property_get_bool, offsetof(ExecContext, cpu_sched_reset_on_fork), SD_BUS_VTABLE_PROPERTY_CONST),
690 SD_BUS_PROPERTY("NonBlocking", "b", bus_property_get_bool, offsetof(ExecContext, non_blocking), SD_BUS_VTABLE_PROPERTY_CONST),
691 SD_BUS_PROPERTY("StandardInput", "s", property_get_exec_input, offsetof(ExecContext, std_input), SD_BUS_VTABLE_PROPERTY_CONST),
692 SD_BUS_PROPERTY("StandardOutput", "s", bus_property_get_exec_output, offsetof(ExecContext, std_output), SD_BUS_VTABLE_PROPERTY_CONST),
693 SD_BUS_PROPERTY("StandardError", "s", bus_property_get_exec_output, offsetof(ExecContext, std_error), SD_BUS_VTABLE_PROPERTY_CONST),
694 SD_BUS_PROPERTY("TTYPath", "s", NULL, offsetof(ExecContext, tty_path), SD_BUS_VTABLE_PROPERTY_CONST),
695 SD_BUS_PROPERTY("TTYReset", "b", bus_property_get_bool, offsetof(ExecContext, tty_reset), SD_BUS_VTABLE_PROPERTY_CONST),
696 SD_BUS_PROPERTY("TTYVHangup", "b", bus_property_get_bool, offsetof(ExecContext, tty_vhangup), SD_BUS_VTABLE_PROPERTY_CONST),
697 SD_BUS_PROPERTY("TTYVTDisallocate", "b", bus_property_get_bool, offsetof(ExecContext, tty_vt_disallocate), SD_BUS_VTABLE_PROPERTY_CONST),
698 SD_BUS_PROPERTY("SyslogPriority", "i", bus_property_get_int, offsetof(ExecContext, syslog_priority), SD_BUS_VTABLE_PROPERTY_CONST),
699 SD_BUS_PROPERTY("SyslogIdentifier", "s", NULL, offsetof(ExecContext, syslog_identifier), SD_BUS_VTABLE_PROPERTY_CONST),
700 SD_BUS_PROPERTY("SyslogLevelPrefix", "b", bus_property_get_bool, offsetof(ExecContext, syslog_level_prefix), SD_BUS_VTABLE_PROPERTY_CONST),
06f2ccf9
EV
701 SD_BUS_PROPERTY("SyslogLevel", "i", property_get_syslog_level, 0, SD_BUS_VTABLE_PROPERTY_CONST),
702 SD_BUS_PROPERTY("SyslogFacility", "i", property_get_syslog_facility, 0, SD_BUS_VTABLE_PROPERTY_CONST),
556089dc
LP
703 SD_BUS_PROPERTY("Capabilities", "s", property_get_capabilities, 0, SD_BUS_VTABLE_PROPERTY_CONST),
704 SD_BUS_PROPERTY("SecureBits", "i", bus_property_get_int, offsetof(ExecContext, secure_bits), SD_BUS_VTABLE_PROPERTY_CONST),
705 SD_BUS_PROPERTY("CapabilityBoundingSet", "t", property_get_capability_bounding_set, 0, SD_BUS_VTABLE_PROPERTY_CONST),
755d4b67 706 SD_BUS_PROPERTY("AmbientCapabilities", "t", property_get_ambient_capabilities, 0, SD_BUS_VTABLE_PROPERTY_CONST),
556089dc
LP
707 SD_BUS_PROPERTY("User", "s", NULL, offsetof(ExecContext, user), SD_BUS_VTABLE_PROPERTY_CONST),
708 SD_BUS_PROPERTY("Group", "s", NULL, offsetof(ExecContext, group), SD_BUS_VTABLE_PROPERTY_CONST),
709 SD_BUS_PROPERTY("SupplementaryGroups", "as", NULL, offsetof(ExecContext, supplementary_groups), SD_BUS_VTABLE_PROPERTY_CONST),
556089dc
LP
710 SD_BUS_PROPERTY("PAMName", "s", NULL, offsetof(ExecContext, pam_name), SD_BUS_VTABLE_PROPERTY_CONST),
711 SD_BUS_PROPERTY("ReadWriteDirectories", "as", NULL, offsetof(ExecContext, read_write_dirs), SD_BUS_VTABLE_PROPERTY_CONST),
712 SD_BUS_PROPERTY("ReadOnlyDirectories", "as", NULL, offsetof(ExecContext, read_only_dirs), SD_BUS_VTABLE_PROPERTY_CONST),
713 SD_BUS_PROPERTY("InaccessibleDirectories", "as", NULL, offsetof(ExecContext, inaccessible_dirs), SD_BUS_VTABLE_PROPERTY_CONST),
714 SD_BUS_PROPERTY("MountFlags", "t", bus_property_get_ulong, offsetof(ExecContext, mount_flags), SD_BUS_VTABLE_PROPERTY_CONST),
715 SD_BUS_PROPERTY("PrivateTmp", "b", bus_property_get_bool, offsetof(ExecContext, private_tmp), SD_BUS_VTABLE_PROPERTY_CONST),
716 SD_BUS_PROPERTY("PrivateNetwork", "b", bus_property_get_bool, offsetof(ExecContext, private_network), SD_BUS_VTABLE_PROPERTY_CONST),
7f112f50 717 SD_BUS_PROPERTY("PrivateDevices", "b", bus_property_get_bool, offsetof(ExecContext, private_devices), SD_BUS_VTABLE_PROPERTY_CONST),
1b8689f9
LP
718 SD_BUS_PROPERTY("ProtectHome", "s", bus_property_get_protect_home, offsetof(ExecContext, protect_home), SD_BUS_VTABLE_PROPERTY_CONST),
719 SD_BUS_PROPERTY("ProtectSystem", "s", bus_property_get_protect_system, offsetof(ExecContext, protect_system), SD_BUS_VTABLE_PROPERTY_CONST),
556089dc
LP
720 SD_BUS_PROPERTY("SameProcessGroup", "b", bus_property_get_bool, offsetof(ExecContext, same_pgrp), SD_BUS_VTABLE_PROPERTY_CONST),
721 SD_BUS_PROPERTY("UtmpIdentifier", "s", NULL, offsetof(ExecContext, utmp_id), SD_BUS_VTABLE_PROPERTY_CONST),
023a4f67 722 SD_BUS_PROPERTY("UtmpMode", "s", property_get_exec_utmp_mode, offsetof(ExecContext, utmp_mode), SD_BUS_VTABLE_PROPERTY_CONST),
5f8640fb 723 SD_BUS_PROPERTY("SELinuxContext", "(bs)", property_get_selinux_context, 0, SD_BUS_VTABLE_PROPERTY_CONST),
eef65bf3 724 SD_BUS_PROPERTY("AppArmorProfile", "(bs)", property_get_apparmor_profile, 0, SD_BUS_VTABLE_PROPERTY_CONST),
2ca620c4 725 SD_BUS_PROPERTY("SmackProcessLabel", "(bs)", property_get_smack_process_label, 0, SD_BUS_VTABLE_PROPERTY_CONST),
556089dc
LP
726 SD_BUS_PROPERTY("IgnoreSIGPIPE", "b", bus_property_get_bool, offsetof(ExecContext, ignore_sigpipe), SD_BUS_VTABLE_PROPERTY_CONST),
727 SD_BUS_PROPERTY("NoNewPrivileges", "b", bus_property_get_bool, offsetof(ExecContext, no_new_privileges), SD_BUS_VTABLE_PROPERTY_CONST),
57183d11
LP
728 SD_BUS_PROPERTY("SystemCallFilter", "(bas)", property_get_syscall_filter, 0, SD_BUS_VTABLE_PROPERTY_CONST),
729 SD_BUS_PROPERTY("SystemCallArchitectures", "as", property_get_syscall_archs, 0, SD_BUS_VTABLE_PROPERTY_CONST),
17df7223 730 SD_BUS_PROPERTY("SystemCallErrorNumber", "i", property_get_syscall_errno, 0, SD_BUS_VTABLE_PROPERTY_CONST),
ac45f971 731 SD_BUS_PROPERTY("Personality", "s", property_get_personality, 0, SD_BUS_VTABLE_PROPERTY_CONST),
4298d0b5 732 SD_BUS_PROPERTY("RestrictAddressFamilies", "(bas)", property_get_address_families, 0, SD_BUS_VTABLE_PROPERTY_CONST),
e66cf1a3
LP
733 SD_BUS_PROPERTY("RuntimeDirectoryMode", "u", bus_property_get_mode, offsetof(ExecContext, runtime_directory_mode), SD_BUS_VTABLE_PROPERTY_CONST),
734 SD_BUS_PROPERTY("RuntimeDirectory", "as", NULL, offsetof(ExecContext, runtime_directory), SD_BUS_VTABLE_PROPERTY_CONST),
718db961
LP
735 SD_BUS_VTABLE_END
736};
82c121a4 737
4d4c80d0
LP
738static int append_exec_command(sd_bus_message *reply, ExecCommand *c) {
739 int r;
740
741 assert(reply);
742 assert(c);
743
744 if (!c->path)
745 return 0;
746
747 r = sd_bus_message_open_container(reply, 'r', "sasbttttuii");
748 if (r < 0)
749 return r;
750
751 r = sd_bus_message_append(reply, "s", c->path);
752 if (r < 0)
753 return r;
754
755 r = sd_bus_message_append_strv(reply, c->argv);
756 if (r < 0)
757 return r;
758
759 r = sd_bus_message_append(reply, "bttttuii",
760 c->ignore,
761 c->exec_status.start_timestamp.realtime,
762 c->exec_status.start_timestamp.monotonic,
763 c->exec_status.exit_timestamp.realtime,
764 c->exec_status.exit_timestamp.monotonic,
765 (uint32_t) c->exec_status.pid,
766 (int32_t) c->exec_status.code,
767 (int32_t) c->exec_status.status);
768 if (r < 0)
769 return r;
770
771 return sd_bus_message_close_container(reply);
772}
773
718db961
LP
774int bus_property_get_exec_command(
775 sd_bus *bus,
776 const char *path,
777 const char *interface,
778 const char *property,
779 sd_bus_message *reply,
ebcf1f97
LP
780 void *userdata,
781 sd_bus_error *ret_error) {
fe68089d 782
4d4c80d0 783 ExecCommand *c = (ExecCommand*) userdata;
718db961 784 int r;
fe68089d 785
718db961
LP
786 assert(bus);
787 assert(reply);
fe68089d 788
718db961
LP
789 r = sd_bus_message_open_container(reply, 'a', "(sasbttttuii)");
790 if (r < 0)
791 return r;
fe68089d 792
4d4c80d0
LP
793 r = append_exec_command(reply, c);
794 if (r < 0)
795 return r;
fe68089d 796
4d4c80d0
LP
797 return sd_bus_message_close_container(reply);
798}
718db961 799
4d4c80d0
LP
800int bus_property_get_exec_command_list(
801 sd_bus *bus,
802 const char *path,
803 const char *interface,
804 const char *property,
805 sd_bus_message *reply,
806 void *userdata,
807 sd_bus_error *ret_error) {
718db961 808
4d4c80d0
LP
809 ExecCommand *c = *(ExecCommand**) userdata;
810 int r;
718db961 811
4d4c80d0
LP
812 assert(bus);
813 assert(reply);
814
815 r = sd_bus_message_open_container(reply, 'a', "(sasbttttuii)");
816 if (r < 0)
817 return r;
718db961 818
4d4c80d0
LP
819 LIST_FOREACH(command, c, c) {
820 r = append_exec_command(reply, c);
718db961
LP
821 if (r < 0)
822 return r;
fe68089d
LP
823 }
824
718db961 825 return sd_bus_message_close_container(reply);
8351ceae 826}
c7040b5d
LP
827
828int bus_exec_context_set_transient_property(
829 Unit *u,
830 ExecContext *c,
831 const char *name,
832 sd_bus_message *message,
833 UnitSetPropertiesMode mode,
834 sd_bus_error *error) {
835
cab2aca3
LP
836 const char *soft = NULL;
837 int r, ri;
c7040b5d
LP
838
839 assert(u);
840 assert(c);
841 assert(name);
842 assert(message);
843
844 if (streq(name, "User")) {
845 const char *uu;
846
847 r = sd_bus_message_read(message, "s", &uu);
848 if (r < 0)
849 return r;
850
851 if (mode != UNIT_CHECK) {
852
853 if (isempty(uu)) {
a1e58e8e 854 c->user = mfree(c->user);
c7040b5d
LP
855 } else {
856 char *t;
857
858 t = strdup(uu);
859 if (!t)
860 return -ENOMEM;
861
862 free(c->user);
863 c->user = t;
864 }
865
866 unit_write_drop_in_private_format(u, mode, name, "User=%s\n", uu);
867 }
868
869 return 1;
870
871 } else if (streq(name, "Group")) {
872 const char *gg;
873
874 r = sd_bus_message_read(message, "s", &gg);
875 if (r < 0)
876 return r;
877
878 if (mode != UNIT_CHECK) {
879
880 if (isempty(gg)) {
a1e58e8e 881 c->group = mfree(c->group);
c7040b5d
LP
882 } else {
883 char *t;
884
885 t = strdup(gg);
886 if (!t)
887 return -ENOMEM;
888
889 free(c->group);
890 c->group = t;
891 }
892
893 unit_write_drop_in_private_format(u, mode, name, "Group=%s\n", gg);
894 }
895
896 return 1;
de53c417
EV
897 } else if (streq(name, "SyslogIdentifier")) {
898 const char *id;
c7040b5d 899
de53c417
EV
900 r = sd_bus_message_read(message, "s", &id);
901 if (r < 0)
902 return r;
903
904 if (mode != UNIT_CHECK) {
905
906 if (isempty(id)) {
907 c->syslog_identifier = mfree(c->syslog_identifier);
908 } else {
909 char *t;
910
911 t = strdup(id);
912 if (!t)
913 return -ENOMEM;
914
915 free(c->syslog_identifier);
916 c->syslog_identifier = t;
917 }
918
919 unit_write_drop_in_private_format(u, mode, name, "SyslogIdentifier=%s\n", id);
920 }
921
a8a13575
EV
922 return 1;
923 } else if (streq(name, "SyslogLevel")) {
924 int level;
925
926 r = sd_bus_message_read(message, "i", &level);
927 if (r < 0)
928 return r;
929
e0d6e0fa
EV
930 if (!log_level_is_valid(level))
931 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Log level value out of range");
932
a8a13575
EV
933 if (mode != UNIT_CHECK) {
934 c->syslog_priority = (c->syslog_priority & LOG_FACMASK) | level;
935 unit_write_drop_in_private_format(u, mode, name, "SyslogLevel=%i\n", level);
936 }
937
460ed929
EV
938 return 1;
939 } else if (streq(name, "SyslogFacility")) {
940 int facility;
941
942 r = sd_bus_message_read(message, "i", &facility);
943 if (r < 0)
944 return r;
945
e0d6e0fa
EV
946 if (!log_facility_unshifted_is_valid(facility))
947 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Log facility value out of range");
948
460ed929
EV
949 if (mode != UNIT_CHECK) {
950 c->syslog_priority = (facility << 3) | LOG_PRI(c->syslog_priority);
951 unit_write_drop_in_private_format(u, mode, name, "SyslogFacility=%i\n", facility);
952 }
953
de53c417 954 return 1;
c7040b5d
LP
955 } else if (streq(name, "Nice")) {
956 int n;
957
958 r = sd_bus_message_read(message, "i", &n);
959 if (r < 0)
960 return r;
961
962 if (n < PRIO_MIN || n >= PRIO_MAX)
963 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Nice value out of range");
964
965 if (mode != UNIT_CHECK) {
966 c->nice = n;
967 unit_write_drop_in_private_format(u, mode, name, "Nice=%i\n", n);
968 }
969
970 return 1;
971
5f5d8eab 972 } else if (STR_IN_SET(name, "TTYPath", "RootDirectory")) {
602b8355 973 const char *s;
9b15b784 974
602b8355 975 r = sd_bus_message_read(message, "s", &s);
9b15b784
LP
976 if (r < 0)
977 return r;
978
602b8355
NC
979 if (!path_is_absolute(s))
980 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "%s takes an absolute path", name);
9b15b784
LP
981
982 if (mode != UNIT_CHECK) {
5f5d8eab
LP
983 if (streq(name, "TTYPath"))
984 r = free_and_strdup(&c->tty_path, s);
985 else {
986 assert(streq(name, "RootDirectory"));
987 r = free_and_strdup(&c->root_directory, s);
602b8355 988 }
5f5d8eab
LP
989 if (r < 0)
990 return r;
9b15b784 991
602b8355 992 unit_write_drop_in_private_format(u, mode, name, "%s=%s\n", name, s);
9b15b784
LP
993 }
994
995 return 1;
996
5f5d8eab
LP
997 } else if (streq(name, "WorkingDirectory")) {
998 const char *s;
999 bool missing_ok;
1000
1001 r = sd_bus_message_read(message, "s", &s);
1002 if (r < 0)
1003 return r;
1004
1005 if (s[0] == '-') {
1006 missing_ok = true;
1007 s++;
1008 } else
1009 missing_ok = false;
1010
1011 if (!streq(s, "~") && !path_is_absolute(s))
1012 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "WorkingDirectory= expects an absolute path or '~'");
1013
1014 if (mode != UNIT_CHECK) {
1015 if (streq(s, "~")) {
1016 c->working_directory = mfree(c->working_directory);
1017 c->working_directory_home = true;
1018 } else {
1019 r = free_and_strdup(&c->working_directory, s);
1020 if (r < 0)
1021 return r;
1022
1023 c->working_directory_home = false;
1024 }
1025
1026 c->working_directory_missing_ok = missing_ok;
1027 unit_write_drop_in_private_format(u, mode, name, "WorkingDirectory=%s%s", missing_ok ? "-" : "", s);
1028 }
1029
1030 return 1;
1031
9b15b784
LP
1032 } else if (streq(name, "StandardInput")) {
1033 const char *s;
1034 ExecInput p;
1035
1036 r = sd_bus_message_read(message, "s", &s);
1037 if (r < 0)
1038 return r;
1039
1040 p = exec_input_from_string(s);
1041 if (p < 0)
1042 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid standard input name");
1043
1044 if (mode != UNIT_CHECK) {
1045 c->std_input = p;
1046
1047 unit_write_drop_in_private_format(u, mode, name, "StandardInput=%s\n", exec_input_to_string(p));
1048 }
1049
1050 return 1;
1051
1052
1053 } else if (streq(name, "StandardOutput")) {
1054 const char *s;
1055 ExecOutput p;
1056
1057 r = sd_bus_message_read(message, "s", &s);
1058 if (r < 0)
1059 return r;
1060
1061 p = exec_output_from_string(s);
1062 if (p < 0)
1063 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid standard output name");
1064
1065 if (mode != UNIT_CHECK) {
1066 c->std_output = p;
1067
1068 unit_write_drop_in_private_format(u, mode, name, "StandardOutput=%s\n", exec_output_to_string(p));
1069 }
1070
1071 return 1;
1072
1073 } else if (streq(name, "StandardError")) {
1074 const char *s;
1075 ExecOutput p;
1076
1077 r = sd_bus_message_read(message, "s", &s);
1078 if (r < 0)
1079 return r;
1080
1081 p = exec_output_from_string(s);
1082 if (p < 0)
1083 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid standard error name");
1084
1085 if (mode != UNIT_CHECK) {
1086 c->std_error = p;
1087
1088 unit_write_drop_in_private_format(u, mode, name, "StandardError=%s\n", exec_output_to_string(p));
1089 }
1090
1091 return 1;
1092
b9c50073
GP
1093 } else if (STR_IN_SET(name,
1094 "IgnoreSIGPIPE", "TTYVHangup", "TTYReset",
1095 "PrivateTmp", "PrivateDevices", "PrivateNetwork",
047d9933 1096 "NoNewPrivileges", "SyslogLevelPrefix")) {
506711fd
LP
1097 int b;
1098
1099 r = sd_bus_message_read(message, "b", &b);
1100 if (r < 0)
1101 return r;
1102
1103 if (mode != UNIT_CHECK) {
b9c50073
GP
1104 if (streq(name, "IgnoreSIGPIPE"))
1105 c->ignore_sigpipe = b;
1106 else if (streq(name, "TTYVHangup"))
1107 c->tty_vhangup = b;
1108 else if (streq(name, "TTYReset"))
1109 c->tty_reset = b;
1110 else if (streq(name, "PrivateTmp"))
1111 c->private_tmp = b;
1112 else if (streq(name, "PrivateDevices"))
1113 c->private_devices = b;
1114 else if (streq(name, "PrivateNetwork"))
1115 c->private_network = b;
1116 else if (streq(name, "NoNewPrivileges"))
1117 c->no_new_privileges = b;
047d9933
EV
1118 else if (streq(name, "SyslogLevelPrefix"))
1119 c->syslog_level_prefix = b;
b9c50073
GP
1120
1121 unit_write_drop_in_private_format(u, mode, name, "%s=%s\n", name, yes_no(b));
506711fd
LP
1122 }
1123
1124 return 1;
1125
1126 } else if (streq(name, "UtmpIdentifier")) {
1127 const char *id;
1128
1129 r = sd_bus_message_read(message, "s", &id);
1130 if (r < 0)
1131 return r;
1132
1133 if (mode != UNIT_CHECK) {
1134 if (isempty(id))
1135 c->utmp_id = mfree(c->utmp_id);
1136 else if (free_and_strdup(&c->utmp_id, id) < 0)
1137 return -ENOMEM;
1138
1139 unit_write_drop_in_private_format(u, mode, name, "UtmpIdentifier=%s\n", strempty(id));
1140 }
1141
1142 return 1;
1143
1144 } else if (streq(name, "UtmpMode")) {
1145 const char *s;
1146 ExecUtmpMode m;
1147
1148 r = sd_bus_message_read(message, "s", &s);
1149 if (r < 0)
1150 return r;
1151
1152 m = exec_utmp_mode_from_string(s);
1153 if (m < 0)
1154 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid utmp mode");
1155
1156 if (mode != UNIT_CHECK) {
1157 c->utmp_mode = m;
1158
1159 unit_write_drop_in_private_format(u, mode, name, "UtmpMode=%s\n", exec_utmp_mode_to_string(m));
1160 }
1161
1162 return 1;
1163
1164 } else if (streq(name, "PAMName")) {
1165 const char *n;
1166
1167 r = sd_bus_message_read(message, "s", &n);
1168 if (r < 0)
1169 return r;
1170
1171 if (mode != UNIT_CHECK) {
1172 if (isempty(n))
1173 c->pam_name = mfree(c->pam_name);
1174 else if (free_and_strdup(&c->pam_name, n) < 0)
1175 return -ENOMEM;
1176
1177 unit_write_drop_in_private_format(u, mode, name, "PAMName=%s\n", strempty(n));
1178 }
1179
1180 return 1;
1181
c7040b5d
LP
1182 } else if (streq(name, "Environment")) {
1183
1184 _cleanup_strv_free_ char **l = NULL;
1185
1186 r = sd_bus_message_read_strv(message, &l);
1187 if (r < 0)
1188 return r;
1189
1190 if (!strv_env_is_valid(l))
1191 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid environment block.");
1192
1193 if (mode != UNIT_CHECK) {
1006a62d 1194 _cleanup_free_ char *joined = NULL;
c7040b5d
LP
1195 char **e;
1196
e9876fc9
EV
1197 if (strv_length(l) == 0) {
1198 c->environment = strv_free(c->environment);
1199 unit_write_drop_in_private_format(u, mode, name, "Environment=\n");
1200 } else {
1201 e = strv_env_merge(2, c->environment, l);
1202 if (!e)
1203 return -ENOMEM;
c7040b5d 1204
e9876fc9
EV
1205 strv_free(c->environment);
1206 c->environment = e;
c7040b5d 1207
e9876fc9
EV
1208 joined = strv_join_quoted(c->environment);
1209 if (!joined)
1210 return -ENOMEM;
1211
1212 unit_write_drop_in_private_format(u, mode, name, "Environment=%s\n", joined);
1213 }
c7040b5d
LP
1214 }
1215
d584f638
LP
1216 return 1;
1217
f1db3327
EV
1218 } else if (streq(name, "TimerSlackNSec")) {
1219
1220 nsec_t n;
1221
1222 r = sd_bus_message_read(message, "t", &n);
1223 if (r < 0)
1224 return r;
1225
1226 if (mode != UNIT_CHECK) {
1227 c->timer_slack_nsec = n;
1228 unit_write_drop_in_private_format(u, mode, name, "TimerSlackNSec=" NSEC_FMT "\n", n);
1229 }
1230
1231 return 1;
1232
6b862936
EV
1233 } else if (streq(name, "OOMScoreAdjust")) {
1234 int oa;
1235
1236 r = sd_bus_message_read(message, "i", &oa);
1237 if (r < 0)
1238 return r;
1239
1240 if (!oom_score_adjust_is_valid(oa))
1241 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "OOM score adjust value out of range");
1242
1243 if (mode != UNIT_CHECK) {
1244 c->oom_score_adjust = oa;
1245 c->oom_score_adjust_set = true;
1246 unit_write_drop_in_private_format(u, mode, name, "OOMScoreAdjust=%i\n", oa);
1247 }
1248
1249 return 1;
1250
ceb728cf
NC
1251 } else if (streq(name, "EnvironmentFiles")) {
1252
1253 _cleanup_free_ char *joined = NULL;
1254 _cleanup_fclose_ FILE *f = NULL;
2229f656 1255 _cleanup_free_ char **l = NULL;
ceb728cf 1256 size_t size = 0;
2229f656 1257 char **i;
ceb728cf
NC
1258
1259 r = sd_bus_message_enter_container(message, 'a', "(sb)");
1260 if (r < 0)
1261 return r;
1262
1263 f = open_memstream(&joined, &size);
1264 if (!f)
1265 return -ENOMEM;
1266
2229f656
LP
1267 STRV_FOREACH(i, c->environment_files)
1268 fprintf(f, "EnvironmentFile=%s\n", *i);
ceb728cf
NC
1269
1270 while ((r = sd_bus_message_enter_container(message, 'r', "sb")) > 0) {
1271 const char *path;
1272 int b;
1273
ceb728cf
NC
1274 r = sd_bus_message_read(message, "sb", &path, &b);
1275 if (r < 0)
1276 return r;
1277
1278 r = sd_bus_message_exit_container(message);
1279 if (r < 0)
1280 return r;
1281
1282 if (!isempty(path) && !path_is_absolute(path))
1283 return sd_bus_error_set_errnof(error, EINVAL, "Path %s is not absolute.", path);
1284
1285 if (mode != UNIT_CHECK) {
ceb728cf
NC
1286 char *buf = NULL;
1287
1288 buf = strjoin(b ? "-" : "", path, NULL);
2229f656 1289 if (!buf)
ceb728cf
NC
1290 return -ENOMEM;
1291
1292 fprintf(f, "EnvironmentFile=%s\n", buf);
1293
2229f656 1294 r = strv_consume(&l, buf);
ceb728cf
NC
1295 if (r < 0)
1296 return r;
1297 }
1298 }
1299 if (r < 0)
1300 return r;
1301
b0830e21
LP
1302 r = sd_bus_message_exit_container(message);
1303 if (r < 0)
1304 return r;
1305
2229f656
LP
1306 r = fflush_and_check(f);
1307 if (r < 0)
1308 return r;
ceb728cf 1309
2229f656
LP
1310 if (mode != UNIT_CHECK) {
1311 if (strv_isempty(l)) {
1312 c->environment_files = strv_free(c->environment_files);
ceb728cf 1313 unit_write_drop_in_private(u, mode, name, "EnvironmentFile=\n");
2229f656
LP
1314 } else {
1315 r = strv_extend_strv(&c->environment_files, l, true);
1316 if (r < 0)
1317 return r;
1318
ceb728cf 1319 unit_write_drop_in_private(u, mode, name, joined);
2229f656
LP
1320 }
1321 }
ceb728cf 1322
ceb728cf
NC
1323 return 1;
1324
b4c14404
FB
1325 } else if (streq(name, "PassEnvironment")) {
1326
1327 _cleanup_strv_free_ char **l = NULL;
1328
1329 r = sd_bus_message_read_strv(message, &l);
1330 if (r < 0)
1331 return r;
1332
1333 if (!strv_env_name_is_valid(l))
1334 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid PassEnvironment block.");
1335
1336 if (mode != UNIT_CHECK) {
1337 if (strv_isempty(l)) {
1338 c->pass_environment = strv_free(c->pass_environment);
1339 unit_write_drop_in_private_format(u, mode, name, "PassEnvironment=\n");
1340 } else {
1341 _cleanup_free_ char *joined = NULL;
1342
1343 r = strv_extend_strv(&c->pass_environment, l, true);
1344 if (r < 0)
1345 return r;
1346
1347 joined = strv_join_quoted(c->pass_environment);
1348 if (!joined)
1349 return -ENOMEM;
1350
1351 unit_write_drop_in_private_format(u, mode, name, "PassEnvironment=%s\n", joined);
1352 }
1353 }
1354
1355 return 1;
1356
08596068
EV
1357 } else if (STR_IN_SET(name, "ReadWriteDirectories", "ReadOnlyDirectories", "InaccessibleDirectories")) {
1358
1359 _cleanup_strv_free_ char **l = NULL;
1360 char ***dirs;
1361 char **p;
1362
1363 r = sd_bus_message_read_strv(message, &l);
1364 if (r < 0)
1365 return r;
1366
1367 STRV_FOREACH(p, l) {
1368 int offset;
1369 if (!utf8_is_valid(*p))
1370 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid %s", name);
1371
1372 offset = **p == '-';
1373 if (!path_is_absolute(*p + offset))
1374 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid %s", name);
1375 }
1376
1377 if (mode != UNIT_CHECK) {
1378 _cleanup_free_ char *joined = NULL;
1379
1380 if (streq(name, "ReadWriteDirectories"))
1381 dirs = &c->read_write_dirs;
1382 else if (streq(name, "ReadOnlyDirectories"))
1383 dirs = &c->read_only_dirs;
cb51e6b7 1384 else /* "InaccessibleDirectories" */
08596068
EV
1385 dirs = &c->inaccessible_dirs;
1386
1387 if (strv_length(l) == 0) {
1388 *dirs = strv_free(*dirs);
1389 unit_write_drop_in_private_format(u, mode, name, "%s=\n", name);
1390 } else {
1391 r = strv_extend_strv(dirs, l, true);
1392
1393 if (r < 0)
1394 return -ENOMEM;
1395
1396 joined = strv_join_quoted(*dirs);
1397 if (!joined)
1398 return -ENOMEM;
1399
1400 unit_write_drop_in_private_format(u, mode, name, "%s=%s\n", name, joined);
1401 }
1402
1403 }
1404
1405 return 1;
1406
5664e6cf
EV
1407 } else if (streq(name, "ProtectSystem")) {
1408 const char *s;
1409 ProtectSystem ps;
1410
1411 r = sd_bus_message_read(message, "s", &s);
1412 if (r < 0)
1413 return r;
1414
1415 r = parse_boolean(s);
1416 if (r > 0)
1417 ps = PROTECT_SYSTEM_YES;
1418 else if (r == 0)
1419 ps = PROTECT_SYSTEM_NO;
1420 else {
1421 ps = protect_system_from_string(s);
1422 if (ps < 0)
1423 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Failed to parse protect system value");
1424 }
1425
1426 if (mode != UNIT_CHECK) {
1427 c->protect_system = ps;
1428 unit_write_drop_in_private_format(u, mode, name, "%s=%s\n", name, s);
1429 }
1430
1431 return 1;
1432
eff58074
EV
1433 } else if (streq(name, "ProtectHome")) {
1434 const char *s;
1435 ProtectHome ph;
1436
1437 r = sd_bus_message_read(message, "s", &s);
1438 if (r < 0)
1439 return r;
1440
1441 r = parse_boolean(s);
1442 if (r > 0)
1443 ph = PROTECT_HOME_YES;
1444 else if (r == 0)
1445 ph = PROTECT_HOME_NO;
1446 else {
1447 ph = protect_home_from_string(s);
1448 if (ph < 0)
1449 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Failed to parse protect home value");
1450 }
1451
1452 if (mode != UNIT_CHECK) {
1453 c->protect_home = ph;
1454 unit_write_drop_in_private_format(u, mode, name, "%s=%s\n", name, s);
1455 }
1456
1457 return 1;
1458
fa21b5e3
EV
1459 } else if (streq(name, "RuntimeDirectory")) {
1460 _cleanup_strv_free_ char **l = NULL;
1461 char **p;
1462
1463 r = sd_bus_message_read_strv(message, &l);
1464 if (r < 0)
1465 return r;
1466
1467 STRV_FOREACH(p, l) {
1468 if (!filename_is_valid(*p))
1469 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Runtime directory is not valid %s", *p);
1470 }
1471
1472 if (mode != UNIT_CHECK) {
1473 _cleanup_free_ char *joined = NULL;
1474
1475 if (strv_isempty(l)) {
1476 c->runtime_directory = strv_free(c->runtime_directory);
1477 unit_write_drop_in_private_format(u, mode, name, "%s=\n", name);
1478 } else {
1479 r = strv_extend_strv(&c->runtime_directory, l, true);
1480
1481 if (r < 0)
1482 return -ENOMEM;
1483
1484 joined = strv_join_quoted(c->runtime_directory);
1485 if (!joined)
1486 return -ENOMEM;
1487
1488 unit_write_drop_in_private_format(u, mode, name, "%s=%s\n", name, joined);
1489 }
1490 }
1491
1492 return 1;
1493
cab2aca3
LP
1494 }
1495
1496 ri = rlimit_from_string(name);
1497 if (ri < 0) {
1498 soft = endswith(name, "Soft");
1499 if (soft) {
1500 const char *n;
1501
1502 n = strndupa(name, soft - name);
1503 ri = rlimit_from_string(n);
1504 if (ri >= 0)
1505 name = n;
1506
1507 }
1508 }
1509
1510 if (ri >= 0) {
d584f638
LP
1511 uint64_t rl;
1512 rlim_t x;
1513
1514 r = sd_bus_message_read(message, "t", &rl);
1515 if (r < 0)
1516 return r;
1517
1518 if (rl == (uint64_t) -1)
1519 x = RLIM_INFINITY;
1520 else {
1521 x = (rlim_t) rl;
1522
1523 if ((uint64_t) x != rl)
1524 return -ERANGE;
1525 }
1526
1527 if (mode != UNIT_CHECK) {
cab2aca3
LP
1528 _cleanup_free_ char *f = NULL;
1529 struct rlimit nl;
1530
1531 if (c->rlimit[ri]) {
1532 nl = *c->rlimit[ri];
1533
1534 if (soft)
1535 nl.rlim_cur = x;
1536 else
1537 nl.rlim_max = x;
1538 } else
1539 /* When the resource limit is not initialized yet, then assign the value to both fields */
1540 nl = (struct rlimit) {
1541 .rlim_cur = x,
1542 .rlim_max = x,
1543 };
1544
1545 r = rlimit_format(&nl, &f);
1546 if (r < 0)
1547 return r;
d584f638 1548
cab2aca3
LP
1549 if (c->rlimit[ri])
1550 *c->rlimit[ri] = nl;
1551 else {
1552 c->rlimit[ri] = newdup(struct rlimit, &nl, 1);
1553 if (!c->rlimit[ri])
d584f638
LP
1554 return -ENOMEM;
1555 }
1556
cab2aca3 1557 unit_write_drop_in_private_format(u, mode, name, "%s=%s\n", name, f);
d584f638
LP
1558 }
1559
c7040b5d
LP
1560 return 1;
1561 }
1562
1563 return 0;
1564}