Coverage Report

Created: 2019-06-19 13:33

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