]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/bus-unit-util.c
pid1: fix setting of DefaultTimeoutAbortSec
[thirdparty/systemd.git] / src / shared / bus-unit-util.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
291d565a
LP
2
3#include "alloc-util.h"
e45c81b8 4#include "bus-error.h"
291d565a
LP
5#include "bus-unit-util.h"
6#include "bus-util.h"
cffaed83 7#include "cap-list.h"
fdb3deca 8#include "cgroup-setup.h"
291d565a 9#include "cgroup-util.h"
3d63c749 10#include "condition.h"
cffaed83 11#include "cpu-set-util.h"
291d565a 12#include "escape.h"
b3d59367 13#include "exec-util.h"
2e2ed880 14#include "exit-status.h"
08f3be7a 15#include "hexdecoct.h"
3dc5ca97
LP
16#include "hostname-util.h"
17#include "in-addr-util.h"
da96ad5a 18#include "ip-protocol-list.h"
291d565a 19#include "locale-util.h"
e45c81b8 20#include "log.h"
89382935 21#include "missing_fs.h"
049af8ad 22#include "mountpoint-util.h"
add00535 23#include "nsflags.h"
291d565a 24#include "parse-util.h"
291d565a
LP
25#include "process-util.h"
26#include "rlimit-util.h"
cffaed83 27#include "securebits-util.h"
291d565a 28#include "signal-util.h"
e45c81b8 29#include "socket-util.h"
760877e9 30#include "sort-util.h"
291d565a
LP
31#include "string-util.h"
32#include "syslog-util.h"
33#include "terminal-util.h"
89ada3ba 34#include "unit-def.h"
cffaed83 35#include "user-util.h"
291d565a 36#include "utf8.h"
291d565a 37
20b16441
LP
38int bus_parse_unit_info(sd_bus_message *message, UnitInfo *u) {
39 assert(message);
40 assert(u);
41
42 u->machine = NULL;
43
44 return sd_bus_message_read(
45 message,
46 "(ssssssouso)",
47 &u->id,
48 &u->description,
49 &u->load_state,
50 &u->active_state,
51 &u->sub_state,
52 &u->following,
53 &u->unit_path,
54 &u->job_id,
55 &u->job_type,
56 &u->job_path);
57}
58
0cf6628e
YW
59#define DEFINE_BUS_APPEND_PARSE_PTR(bus_type, cast_type, type, parse_func) \
60 static int bus_append_##parse_func( \
61 sd_bus_message *m, \
62 const char *field, \
63 const char *eq) { \
64 type val; \
65 int r; \
66 \
67 r = parse_func(eq, &val); \
68 if (r < 0) \
69 return log_error_errno(r, "Failed to parse %s=%s: %m", field, eq); \
70 \
71 r = sd_bus_message_append(m, "(sv)", field, \
72 bus_type, (cast_type) val); \
73 if (r < 0) \
74 return bus_log_create_error(r); \
75 \
76 return 1; \
f6a8265b 77 }
3dc5ca97 78
0cf6628e
YW
79#define DEFINE_BUS_APPEND_PARSE(bus_type, parse_func) \
80 static int bus_append_##parse_func( \
81 sd_bus_message *m, \
82 const char *field, \
83 const char *eq) { \
84 int r; \
85 \
86 r = parse_func(eq); \
baaa35ad
ZJS
87 if (r < 0) \
88 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to parse %s: %s", field, eq); \
0cf6628e
YW
89 \
90 r = sd_bus_message_append(m, "(sv)", field, \
91 bus_type, (int32_t) r); \
92 if (r < 0) \
93 return bus_log_create_error(r); \
94 \
95 return 1; \
f6a8265b 96 }
3dc5ca97 97
0cf6628e
YW
98DEFINE_BUS_APPEND_PARSE("b", parse_boolean);
99DEFINE_BUS_APPEND_PARSE("i", ioprio_class_from_string);
100DEFINE_BUS_APPEND_PARSE("i", ip_tos_from_string);
101DEFINE_BUS_APPEND_PARSE("i", log_facility_unshifted_from_string);
102DEFINE_BUS_APPEND_PARSE("i", log_level_from_string);
103DEFINE_BUS_APPEND_PARSE("i", parse_errno);
104DEFINE_BUS_APPEND_PARSE("i", sched_policy_from_string);
105DEFINE_BUS_APPEND_PARSE("i", secure_bits_from_string);
29a3db75 106DEFINE_BUS_APPEND_PARSE("i", signal_from_string);
d2b42d63 107DEFINE_BUS_APPEND_PARSE("i", parse_ip_protocol);
0cf6628e
YW
108DEFINE_BUS_APPEND_PARSE_PTR("i", int32_t, int, ioprio_parse_priority);
109DEFINE_BUS_APPEND_PARSE_PTR("i", int32_t, int, parse_nice);
110DEFINE_BUS_APPEND_PARSE_PTR("i", int32_t, int, safe_atoi);
111DEFINE_BUS_APPEND_PARSE_PTR("t", uint64_t, nsec_t, parse_nsec);
112DEFINE_BUS_APPEND_PARSE_PTR("t", uint64_t, uint64_t, cg_blkio_weight_parse);
113DEFINE_BUS_APPEND_PARSE_PTR("t", uint64_t, uint64_t, cg_cpu_shares_parse);
114DEFINE_BUS_APPEND_PARSE_PTR("t", uint64_t, uint64_t, cg_weight_parse);
115DEFINE_BUS_APPEND_PARSE_PTR("t", uint64_t, unsigned long, mount_propagation_flags_from_string);
116DEFINE_BUS_APPEND_PARSE_PTR("t", uint64_t, uint64_t, safe_atou64);
117DEFINE_BUS_APPEND_PARSE_PTR("u", uint32_t, mode_t, parse_mode);
118DEFINE_BUS_APPEND_PARSE_PTR("u", uint32_t, unsigned, safe_atou);
119DEFINE_BUS_APPEND_PARSE_PTR("x", int64_t, int64_t, safe_atoi64);
89ada3ba 120
a1e92eee 121static int bus_append_string(sd_bus_message *m, const char *field, const char *eq) {
89ada3ba
YW
122 int r;
123
124 r = sd_bus_message_append(m, "(sv)", field, "s", eq);
3dc5ca97 125 if (r < 0)
89ada3ba 126 return bus_log_create_error(r);
3dc5ca97 127
89ada3ba
YW
128 return 1;
129}
130
131static int bus_append_strv(sd_bus_message *m, const char *field, const char *eq, ExtractFlags flags) {
132 const char *p;
133 int r;
134
135 r = sd_bus_message_open_container(m, 'r', "sv");
3dc5ca97 136 if (r < 0)
89ada3ba 137 return bus_log_create_error(r);
3dc5ca97 138
89ada3ba 139 r = sd_bus_message_append_basic(m, 's', field);
3dc5ca97 140 if (r < 0)
89ada3ba 141 return bus_log_create_error(r);
3dc5ca97 142
89ada3ba 143 r = sd_bus_message_open_container(m, 'v', "as");
3dc5ca97 144 if (r < 0)
89ada3ba 145 return bus_log_create_error(r);
3dc5ca97 146
89ada3ba
YW
147 r = sd_bus_message_open_container(m, 'a', "s");
148 if (r < 0)
149 return bus_log_create_error(r);
3dc5ca97 150
89ada3ba
YW
151 for (p = eq;;) {
152 _cleanup_free_ char *word = NULL;
20b16441 153
89ada3ba
YW
154 r = extract_first_word(&p, &word, NULL, flags);
155 if (r == 0)
156 break;
157 if (r == -ENOMEM)
158 return log_oom();
159 if (r < 0)
160 return log_error_errno(r, "Invalid syntax: %s", eq);
20b16441 161
89ada3ba
YW
162 r = sd_bus_message_append_basic(m, 's', word);
163 if (r < 0)
164 return bus_log_create_error(r);
20b16441
LP
165 }
166
89ada3ba 167 r = sd_bus_message_close_container(m);
20b16441
LP
168 if (r < 0)
169 return bus_log_create_error(r);
170
89ada3ba
YW
171 r = sd_bus_message_close_container(m);
172 if (r < 0)
173 return bus_log_create_error(r);
20b16441 174
89ada3ba
YW
175 r = sd_bus_message_close_container(m);
176 if (r < 0)
177 return bus_log_create_error(r);
20b16441 178
89ada3ba
YW
179 return 1;
180}
20b16441 181
89ada3ba
YW
182static int bus_append_byte_array(sd_bus_message *m, const char *field, const void *buf, size_t n) {
183 int r;
20b16441 184
89ada3ba
YW
185 r = sd_bus_message_open_container(m, SD_BUS_TYPE_STRUCT, "sv");
186 if (r < 0)
187 return bus_log_create_error(r);
20b16441 188
89ada3ba
YW
189 r = sd_bus_message_append_basic(m, SD_BUS_TYPE_STRING, field);
190 if (r < 0)
191 return bus_log_create_error(r);
20b16441 192
89ada3ba
YW
193 r = sd_bus_message_open_container(m, 'v', "ay");
194 if (r < 0)
195 return bus_log_create_error(r);
20b16441 196
89ada3ba
YW
197 r = sd_bus_message_append_array(m, 'y', buf, n);
198 if (r < 0)
199 return bus_log_create_error(r);
9184ca48 200
89ada3ba
YW
201 r = sd_bus_message_close_container(m);
202 if (r < 0)
203 return bus_log_create_error(r);
20b16441 204
89ada3ba
YW
205 r = sd_bus_message_close_container(m);
206 if (r < 0)
207 return bus_log_create_error(r);
20b16441 208
89ada3ba
YW
209 return 1;
210}
d58d600e 211
89ada3ba
YW
212static int bus_append_parse_sec_rename(sd_bus_message *m, const char *field, const char *eq) {
213 char *n;
214 usec_t t;
215 size_t l;
216 int r;
d3070fbd 217
89ada3ba
YW
218 r = parse_sec(eq, &t);
219 if (r < 0)
220 return log_error_errno(r, "Failed to parse %s=%s: %m", field, eq);
d3070fbd 221
89ada3ba
YW
222 l = strlen(field);
223 n = newa(char, l + 2);
224 /* Change suffix Sec → USec */
225 strcpy(mempcpy(n, field, l - 3), "USec");
d3070fbd 226
89ada3ba
YW
227 r = sd_bus_message_append(m, "(sv)", n, "t", t);
228 if (r < 0)
229 return bus_log_create_error(r);
d3070fbd 230
89ada3ba
YW
231 return 1;
232}
d3070fbd 233
b48e508d 234static int bus_append_parse_size(sd_bus_message *m, const char *field, const char *eq, uint64_t base) {
89ada3ba
YW
235 uint64_t v;
236 int r;
d3070fbd 237
b48e508d
YW
238 r = parse_size(eq, base, &v);
239 if (r < 0)
89ada3ba 240 return log_error_errno(r, "Failed to parse %s=%s: %m", field, eq);
d3070fbd 241
89ada3ba
YW
242 r = sd_bus_message_append(m, "(sv)", field, "t", v);
243 if (r < 0)
244 return bus_log_create_error(r);
d58d600e 245
89ada3ba
YW
246 return 1;
247}
d58d600e 248
89ada3ba 249static int bus_append_exec_command(sd_bus_message *m, const char *field, const char *eq) {
b3d59367
AZ
250 bool explicit_path = false, done = false;
251 _cleanup_strv_free_ char **l = NULL, **ex_opts = NULL;
252 _cleanup_free_ char *path = NULL, *upgraded_name = NULL;
253 ExecCommandFlags flags = 0;
254 bool is_ex_prop = endswith(field, "Ex");
89ada3ba 255 int r;
d58d600e 256
89ada3ba
YW
257 do {
258 switch (*eq) {
d58d600e 259
89ada3ba 260 case '-':
b3d59367 261 if (FLAGS_SET(flags, EXEC_COMMAND_IGNORE_FAILURE))
89ada3ba
YW
262 done = true;
263 else {
b3d59367 264 flags |= EXEC_COMMAND_IGNORE_FAILURE;
89ada3ba 265 eq++;
d58d600e 266 }
89ada3ba 267 break;
02638280 268
89ada3ba
YW
269 case '@':
270 if (explicit_path)
271 done = true;
272 else {
273 explicit_path = true;
274 eq++;
02638280 275 }
89ada3ba 276 break;
02638280 277
b3d59367
AZ
278 case ':':
279 if (FLAGS_SET(flags, EXEC_COMMAND_NO_ENV_EXPAND))
280 done = true;
281 else {
282 flags |= EXEC_COMMAND_NO_ENV_EXPAND;
283 eq++;
284 }
285 break;
286
89ada3ba 287 case '+':
b3d59367
AZ
288 if (flags & (EXEC_COMMAND_FULLY_PRIVILEGED|EXEC_COMMAND_NO_SETUID|EXEC_COMMAND_AMBIENT_MAGIC))
289 done = true;
290 else {
291 flags |= EXEC_COMMAND_FULLY_PRIVILEGED;
292 eq++;
293 }
294 break;
295
89ada3ba 296 case '!':
b3d59367
AZ
297 if (flags & (EXEC_COMMAND_FULLY_PRIVILEGED|EXEC_COMMAND_AMBIENT_MAGIC))
298 done = true;
299 else if (FLAGS_SET(flags, EXEC_COMMAND_NO_SETUID)) {
300 flags &= ~EXEC_COMMAND_NO_SETUID;
301 flags |= EXEC_COMMAND_AMBIENT_MAGIC;
302 eq++;
303 } else {
304 flags |= EXEC_COMMAND_NO_SETUID;
305 eq++;
306 }
307 break;
83f8e808 308
89ada3ba
YW
309 default:
310 done = true;
311 break;
83f8e808 312 }
89ada3ba 313 } while (!done);
83f8e808 314
b3d59367
AZ
315 if (!is_ex_prop && (flags & (EXEC_COMMAND_NO_ENV_EXPAND|EXEC_COMMAND_FULLY_PRIVILEGED|EXEC_COMMAND_NO_SETUID|EXEC_COMMAND_AMBIENT_MAGIC))) {
316 /* Upgrade the ExecXYZ= property to ExecXYZEx= for convenience */
317 is_ex_prop = true;
b910cc72 318 upgraded_name = strjoin(field, "Ex");
b3d59367
AZ
319 if (!upgraded_name)
320 return log_oom();
321 }
322
323 if (is_ex_prop) {
324 r = exec_command_flags_to_strv(flags, &ex_opts);
325 if (r < 0)
326 return log_error_errno(r, "Failed to convert ExecCommandFlags to strv: %m");
327 }
328
89ada3ba 329 if (explicit_path) {
4ec85141 330 r = extract_first_word(&eq, &path, NULL, EXTRACT_UNQUOTE|EXTRACT_CUNESCAPE);
89ada3ba
YW
331 if (r < 0)
332 return log_error_errno(r, "Failed to parse path: %m");
333 }
2038c3f5 334
4ec85141 335 r = strv_split_extract(&l, eq, NULL, EXTRACT_UNQUOTE|EXTRACT_CUNESCAPE);
89ada3ba
YW
336 if (r < 0)
337 return log_error_errno(r, "Failed to parse command line: %m");
2038c3f5 338
89ada3ba
YW
339 r = sd_bus_message_open_container(m, SD_BUS_TYPE_STRUCT, "sv");
340 if (r < 0)
341 return bus_log_create_error(r);
2038c3f5 342
b3d59367 343 r = sd_bus_message_append_basic(m, SD_BUS_TYPE_STRING, upgraded_name ?: field);
89ada3ba
YW
344 if (r < 0)
345 return bus_log_create_error(r);
2038c3f5 346
b3d59367 347 r = sd_bus_message_open_container(m, 'v', is_ex_prop ? "a(sasas)" : "a(sasb)");
89ada3ba
YW
348 if (r < 0)
349 return bus_log_create_error(r);
08f3be7a 350
b3d59367 351 r = sd_bus_message_open_container(m, 'a', is_ex_prop ? "(sasas)" : "(sasb)");
89ada3ba
YW
352 if (r < 0)
353 return bus_log_create_error(r);
08f3be7a 354
89ada3ba 355 if (!strv_isempty(l)) {
08f3be7a 356
b3d59367 357 r = sd_bus_message_open_container(m, 'r', is_ex_prop ? "sasas" : "sasb");
89ada3ba
YW
358 if (r < 0)
359 return bus_log_create_error(r);
08f3be7a 360
89ada3ba 361 r = sd_bus_message_append(m, "s", path ?: l[0]);
08f3be7a
LP
362 if (r < 0)
363 return bus_log_create_error(r);
364
89ada3ba 365 r = sd_bus_message_append_strv(m, l);
08f3be7a
LP
366 if (r < 0)
367 return bus_log_create_error(r);
368
b3d59367 369 r = is_ex_prop ? sd_bus_message_append_strv(m, ex_opts) : sd_bus_message_append(m, "b", FLAGS_SET(flags, EXEC_COMMAND_IGNORE_FAILURE));
08f3be7a
LP
370 if (r < 0)
371 return bus_log_create_error(r);
372
373 r = sd_bus_message_close_container(m);
89ada3ba
YW
374 if (r < 0)
375 return bus_log_create_error(r);
20b16441
LP
376 }
377
89ada3ba 378 r = sd_bus_message_close_container(m);
20b16441
LP
379 if (r < 0)
380 return bus_log_create_error(r);
381
89ada3ba
YW
382 r = sd_bus_message_close_container(m);
383 if (r < 0)
384 return bus_log_create_error(r);
20b16441 385
89ada3ba
YW
386 r = sd_bus_message_close_container(m);
387 if (r < 0)
388 return bus_log_create_error(r);
20b16441 389
89ada3ba
YW
390 return 1;
391}
20b16441 392
89ada3ba
YW
393static int bus_append_ip_address_access(sd_bus_message *m, int family, const union in_addr_union *prefix, unsigned char prefixlen) {
394 int r;
20b16441 395
89ada3ba
YW
396 assert(m);
397 assert(prefix);
20b16441 398
89ada3ba
YW
399 r = sd_bus_message_open_container(m, 'r', "iayu");
400 if (r < 0)
401 return r;
20b16441 402
89ada3ba
YW
403 r = sd_bus_message_append(m, "i", family);
404 if (r < 0)
405 return r;
66ebf6c0 406
89ada3ba
YW
407 r = sd_bus_message_append_array(m, 'y', prefix, FAMILY_ADDRESS_SIZE(family));
408 if (r < 0)
409 return r;
66ebf6c0 410
89ada3ba
YW
411 r = sd_bus_message_append(m, "u", prefixlen);
412 if (r < 0)
413 return r;
66ebf6c0 414
89ada3ba
YW
415 return sd_bus_message_close_container(m);
416}
20b16441 417
89ada3ba
YW
418static int bus_append_cgroup_property(sd_bus_message *m, const char *field, const char *eq) {
419 int r;
20b16441 420
89ada3ba 421 if (STR_IN_SET(field, "DevicePolicy", "Slice"))
89ada3ba 422 return bus_append_string(m, field, eq);
13c31542 423
c57d2a76
ZJS
424 if (STR_IN_SET(field, "CPUAccounting",
425 "MemoryAccounting",
426 "IOAccounting",
427 "BlockIOAccounting",
428 "TasksAccounting",
429 "IPAccounting"))
89ada3ba 430 return bus_append_parse_boolean(m, field, eq);
13c31542 431
c57d2a76
ZJS
432 if (STR_IN_SET(field, "CPUWeight",
433 "StartupCPUWeight",
434 "IOWeight",
435 "StartupIOWeight"))
89ada3ba 436 return bus_append_cg_weight_parse(m, field, eq);
20b16441 437
c57d2a76
ZJS
438 if (STR_IN_SET(field, "CPUShares",
439 "StartupCPUShares"))
89ada3ba 440 return bus_append_cg_cpu_shares_parse(m, field, eq);
268833ed 441
c57d2a76
ZJS
442 if (STR_IN_SET(field, "AllowedCPUs",
443 "AllowedMemoryNodes")) {
047f5d63
PH
444 _cleanup_(cpu_set_reset) CPUSet cpuset = {};
445 _cleanup_free_ uint8_t *array = NULL;
446 size_t allocated;
447
448 r = parse_cpu_set(eq, &cpuset);
449 if (r < 0)
450 return log_error_errno(r, "Failed to parse %s value: %s", field, eq);
451
452 r = cpu_set_to_dbus(&cpuset, &array, &allocated);
453 if (r < 0)
454 return log_error_errno(r, "Failed to serialize CPUSet: %m");
455
456 return bus_append_byte_array(m, field, array, allocated);
457 }
458
c57d2a76
ZJS
459 if (STR_IN_SET(field, "BlockIOWeight",
460 "StartupBlockIOWeight"))
89ada3ba 461 return bus_append_cg_blkio_weight_parse(m, field, eq);
08f3be7a 462
25cc30c4 463 if (streq(field, "DisableControllers"))
4ec85141 464 return bus_append_strv(m, "DisableControllers", eq, EXTRACT_UNQUOTE);
25cc30c4 465
89ada3ba 466 if (streq(field, "Delegate")) {
89ada3ba 467 r = parse_boolean(eq);
08f3be7a 468 if (r < 0)
4ec85141 469 return bus_append_strv(m, "DelegateControllers", eq, EXTRACT_UNQUOTE);
08f3be7a 470
89ada3ba 471 r = sd_bus_message_append(m, "(sv)", "Delegate", "b", r);
08f3be7a
LP
472 if (r < 0)
473 return bus_log_create_error(r);
474
89ada3ba
YW
475 return 1;
476 }
08f3be7a 477
27e946a5
ZJS
478 if (STR_IN_SET(field, "MemoryMin",
479 "DefaultMemoryLow",
480 "DefaultMemoryMin",
481 "MemoryLow",
482 "MemoryHigh",
483 "MemoryMax",
484 "MemorySwapMax",
485 "MemoryLimit",
486 "TasksMax")) {
cffaed83 487
89ada3ba
YW
488 if (isempty(eq) || streq(eq, "infinity")) {
489 r = sd_bus_message_append(m, "(sv)", field, "t", CGROUP_LIMIT_MAX);
490 if (r < 0)
491 return bus_log_create_error(r);
492 return 1;
cffaed83
YW
493 }
494
f806dfd3 495 r = parse_permille(eq);
89ada3ba
YW
496 if (r >= 0) {
497 char *n;
20b16441 498
f806dfd3
LP
499 /* When this is a percentage we'll convert this into a relative value in the range 0…UINT32_MAX
500 * and pass it in the MemoryLowScale property (and related ones). This way the physical memory
501 * size can be determined server-side. */
20b16441 502
89ada3ba 503 n = strjoina(field, "Scale");
f806dfd3 504 r = sd_bus_message_append(m, "(sv)", n, "u", (uint32_t) (((uint64_t) r * UINT32_MAX) / 1000U));
89ada3ba
YW
505 if (r < 0)
506 return bus_log_create_error(r);
20b16441 507
89ada3ba 508 return 1;
20b16441
LP
509 }
510
89ada3ba 511 if (streq(field, "TasksMax"))
62b749a9 512 return bus_append_safe_atou64(m, field, eq);
20b16441 513
62b749a9 514 return bus_append_parse_size(m, field, eq, 1024);
89ada3ba 515 }
cffaed83 516
89ada3ba 517 if (streq(field, "CPUQuota")) {
89ada3ba
YW
518 if (isempty(eq))
519 r = sd_bus_message_append(m, "(sv)", "CPUQuotaPerSecUSec", "t", USEC_INFINITY);
520 else {
f806dfd3 521 r = parse_permille_unbounded(eq);
baaa35ad
ZJS
522 if (r == 0)
523 return log_error_errno(SYNTHETIC_ERRNO(ERANGE),
524 "CPU quota too small.");
f806dfd3
LP
525 if (r < 0)
526 return log_error_errno(r, "CPU quota '%s' invalid.", eq);
89ada3ba 527
f806dfd3 528 r = sd_bus_message_append(m, "(sv)", "CPUQuotaPerSecUSec", "t", (((uint64_t) r * USEC_PER_SEC) / 1000U));
cffaed83
YW
529 }
530
6bbfdc67 531 if (r < 0)
89ada3ba 532 return bus_log_create_error(r);
cffaed83 533
89ada3ba
YW
534 return 1;
535 }
cffaed83 536
10f28641
FB
537 if (streq(field, "CPUQuotaPeriodSec")) {
538 usec_t u = USEC_INFINITY;
539
540 r = parse_sec_def_infinity(eq, &u);
541 if (r < 0)
542 return log_error_errno(r, "CPU quota period '%s' invalid.", eq);
543
544 r = sd_bus_message_append(m, "(sv)", "CPUQuotaPeriodUSec", "t", u);
545 if (r < 0)
546 return bus_log_create_error(r);
547
548 return 1;
549 }
550
89ada3ba 551 if (streq(field, "DeviceAllow")) {
20b16441 552 if (isempty(eq))
89ada3ba 553 r = sd_bus_message_append(m, "(sv)", field, "a(ss)", 0);
20b16441 554 else {
d9f7305f 555 const char *path = eq, *rwm = NULL, *e;
20b16441
LP
556
557 e = strchr(eq, ' ');
558 if (e) {
559 path = strndupa(eq, e - eq);
560 rwm = e+1;
20b16441
LP
561 }
562
d9f7305f 563 r = sd_bus_message_append(m, "(sv)", field, "a(ss)", 1, path, strempty(rwm));
20b16441
LP
564 }
565
89ada3ba
YW
566 if (r < 0)
567 return bus_log_create_error(r);
568
569 return 1;
570 }
571
572 if (cgroup_io_limit_type_from_string(field) >= 0 || STR_IN_SET(field, "BlockIOReadBandwidth", "BlockIOWriteBandwidth")) {
20b16441 573 if (isempty(eq))
89ada3ba 574 r = sd_bus_message_append(m, "(sv)", field, "a(st)", 0);
20b16441
LP
575 else {
576 const char *path, *bandwidth, *e;
577 uint64_t bytes;
578
579 e = strchr(eq, ' ');
baaa35ad
ZJS
580 if (!e)
581 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
582 "Failed to parse %s value %s.",
583 field, eq);
20b16441 584
d9f7305f
YW
585 path = strndupa(eq, e - eq);
586 bandwidth = e+1;
20b16441 587
79d53eb8 588 if (streq(bandwidth, "infinity"))
13c31542 589 bytes = CGROUP_LIMIT_MAX;
79d53eb8 590 else {
13c31542 591 r = parse_size(bandwidth, 1000, &bytes);
6bbfdc67
LP
592 if (r < 0)
593 return log_error_errno(r, "Failed to parse byte value %s: %m", bandwidth);
20b16441
LP
594 }
595
89ada3ba 596 r = sd_bus_message_append(m, "(sv)", field, "a(st)", 1, path, bytes);
20b16441
LP
597 }
598
89ada3ba
YW
599 if (r < 0)
600 return bus_log_create_error(r);
601
602 return 1;
603 }
604
c57d2a76
ZJS
605 if (STR_IN_SET(field, "IODeviceWeight",
606 "BlockIODeviceWeight")) {
20b16441 607 if (isempty(eq))
89ada3ba 608 r = sd_bus_message_append(m, "(sv)", field, "a(st)", 0);
20b16441
LP
609 else {
610 const char *path, *weight, *e;
611 uint64_t u;
612
613 e = strchr(eq, ' ');
baaa35ad
ZJS
614 if (!e)
615 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
616 "Failed to parse %s value %s.",
617 field, eq);
20b16441 618
d9f7305f
YW
619 path = strndupa(eq, e - eq);
620 weight = e+1;
20b16441
LP
621
622 r = safe_atou64(weight, &u);
6bbfdc67
LP
623 if (r < 0)
624 return log_error_errno(r, "Failed to parse %s value %s: %m", field, weight);
625
89ada3ba 626 r = sd_bus_message_append(m, "(sv)", field, "a(st)", 1, path, u);
20b16441
LP
627 }
628
89ada3ba
YW
629 if (r < 0)
630 return bus_log_create_error(r);
3dc5ca97 631
89ada3ba
YW
632 return 1;
633 }
3dc5ca97 634
6ae4283c
TH
635 if (streq(field, "IODeviceLatencyTargetSec")) {
636 const char *field_usec = "IODeviceLatencyTargetUSec";
637
638 if (isempty(eq))
639 r = sd_bus_message_append(m, "(sv)", field_usec, "a(st)", USEC_INFINITY);
640 else {
641 const char *path, *target, *e;
642 usec_t usec;
643
644 e = strchr(eq, ' ');
baaa35ad
ZJS
645 if (!e)
646 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
647 "Failed to parse %s value %s.",
648 field, eq);
6ae4283c
TH
649
650 path = strndupa(eq, e - eq);
651 target = e+1;
652
653 r = parse_sec(target, &usec);
654 if (r < 0)
655 return log_error_errno(r, "Failed to parse %s value %s: %m", field, target);
656
657 r = sd_bus_message_append(m, "(sv)", field_usec, "a(st)", 1, path, usec);
658 }
659
660 if (r < 0)
661 return bus_log_create_error(r);
662
663 return 1;
664 }
665
c57d2a76
ZJS
666 if (STR_IN_SET(field, "IPAddressAllow",
667 "IPAddressDeny")) {
89ada3ba
YW
668 unsigned char prefixlen;
669 union in_addr_union prefix = {};
670 int family;
3dc5ca97 671
89ada3ba
YW
672 if (isempty(eq)) {
673 r = sd_bus_message_append(m, "(sv)", field, "a(iayu)", 0);
3dc5ca97
LP
674 if (r < 0)
675 return bus_log_create_error(r);
676
89ada3ba
YW
677 return 1;
678 }
3dc5ca97 679
89ada3ba
YW
680 r = sd_bus_message_open_container(m, SD_BUS_TYPE_STRUCT, "sv");
681 if (r < 0)
682 return bus_log_create_error(r);
3dc5ca97 683
89ada3ba
YW
684 r = sd_bus_message_append_basic(m, SD_BUS_TYPE_STRING, field);
685 if (r < 0)
686 return bus_log_create_error(r);
3dc5ca97 687
89ada3ba
YW
688 r = sd_bus_message_open_container(m, 'v', "a(iayu)");
689 if (r < 0)
690 return bus_log_create_error(r);
3dc5ca97 691
89ada3ba
YW
692 r = sd_bus_message_open_container(m, 'a', "(iayu)");
693 if (r < 0)
694 return bus_log_create_error(r);
3dc5ca97 695
89ada3ba
YW
696 if (streq(eq, "any")) {
697 /* "any" is a shortcut for 0.0.0.0/0 and ::/0 */
3dc5ca97 698
89ada3ba
YW
699 r = bus_append_ip_address_access(m, AF_INET, &prefix, 0);
700 if (r < 0)
701 return bus_log_create_error(r);
3dc5ca97 702
89ada3ba
YW
703 r = bus_append_ip_address_access(m, AF_INET6, &prefix, 0);
704 if (r < 0)
705 return bus_log_create_error(r);
3dc5ca97 706
89ada3ba
YW
707 } else if (is_localhost(eq)) {
708 /* "localhost" is a shortcut for 127.0.0.0/8 and ::1/128 */
3dc5ca97 709
89ada3ba
YW
710 prefix.in.s_addr = htobe32(0x7f000000);
711 r = bus_append_ip_address_access(m, AF_INET, &prefix, 8);
712 if (r < 0)
713 return bus_log_create_error(r);
3dc5ca97 714
89ada3ba
YW
715 prefix.in6 = (struct in6_addr) IN6ADDR_LOOPBACK_INIT;
716 r = bus_append_ip_address_access(m, AF_INET6, &prefix, 128);
717 if (r < 0)
718 return r;
3dc5ca97 719
89ada3ba
YW
720 } else if (streq(eq, "link-local")) {
721 /* "link-local" is a shortcut for 169.254.0.0/16 and fe80::/64 */
3dc5ca97 722
89ada3ba
YW
723 prefix.in.s_addr = htobe32((UINT32_C(169) << 24 | UINT32_C(254) << 16));
724 r = bus_append_ip_address_access(m, AF_INET, &prefix, 16);
725 if (r < 0)
726 return bus_log_create_error(r);
3dc5ca97 727
89ada3ba
YW
728 prefix.in6 = (struct in6_addr) {
729 .s6_addr32[0] = htobe32(0xfe800000)
730 };
731 r = bus_append_ip_address_access(m, AF_INET6, &prefix, 64);
732 if (r < 0)
733 return bus_log_create_error(r);
3dc5ca97 734
89ada3ba
YW
735 } else if (streq(eq, "multicast")) {
736 /* "multicast" is a shortcut for 224.0.0.0/4 and ff00::/8 */
3dc5ca97 737
89ada3ba
YW
738 prefix.in.s_addr = htobe32((UINT32_C(224) << 24));
739 r = bus_append_ip_address_access(m, AF_INET, &prefix, 4);
740 if (r < 0)
741 return bus_log_create_error(r);
3dc5ca97 742
89ada3ba
YW
743 prefix.in6 = (struct in6_addr) {
744 .s6_addr32[0] = htobe32(0xff000000)
745 };
746 r = bus_append_ip_address_access(m, AF_INET6, &prefix, 8);
3dc5ca97
LP
747 if (r < 0)
748 return bus_log_create_error(r);
749
89ada3ba 750 } else {
afc1feae
RC
751 for (;;) {
752 _cleanup_free_ char *word = NULL;
753
754 r = extract_first_word(&eq, &word, NULL, 0);
755 if (r == 0)
756 break;
757 if (r == -ENOMEM)
758 return log_oom();
759 if (r < 0)
760 return log_error_errno(r, "Failed to parse %s: %s", field, eq);
89ada3ba 761
afc1feae
RC
762 r = in_addr_prefix_from_string_auto(word, &family, &prefix, &prefixlen);
763 if (r < 0)
764 return log_error_errno(r, "Failed to parse IP address prefix: %s", word);
765
766 r = bus_append_ip_address_access(m, family, &prefix, prefixlen);
767 if (r < 0)
768 return bus_log_create_error(r);
769 }
3dc5ca97
LP
770 }
771
89ada3ba
YW
772 r = sd_bus_message_close_container(m);
773 if (r < 0)
774 return bus_log_create_error(r);
cffaed83 775
89ada3ba
YW
776 r = sd_bus_message_close_container(m);
777 if (r < 0)
778 return bus_log_create_error(r);
cffaed83 779
89ada3ba
YW
780 r = sd_bus_message_close_container(m);
781 if (r < 0)
782 return bus_log_create_error(r);
fab34748
KL
783
784 return 1;
785 }
786
c57d2a76
ZJS
787 if (STR_IN_SET(field, "IPIngressFilterPath",
788 "IPEgressFilterPath")) {
fab34748
KL
789 if (isempty(eq))
790 r = sd_bus_message_append(m, "(sv)", field, "as", 0);
791 else
792 r = sd_bus_message_append(m, "(sv)", field, "as", 1, eq);
793
794 if (r < 0)
795 return bus_log_create_error(r);
cffaed83 796
89ada3ba
YW
797 return 1;
798 }
cffaed83 799
89ada3ba
YW
800 return 0;
801}
cffaed83 802
3d63c749 803static int bus_append_automount_property(sd_bus_message *m, const char *field, const char *eq) {
3d63c749 804 if (streq(field, "Where"))
3d63c749
YW
805 return bus_append_string(m, field, eq);
806
807 if (streq(field, "DirectoryMode"))
3d63c749
YW
808 return bus_append_parse_mode(m, field, eq);
809
810 if (streq(field, "TimeoutIdleSec"))
3d63c749
YW
811 return bus_append_parse_sec_rename(m, field, eq);
812
813 return 0;
814}
815
89ada3ba 816static int bus_append_execute_property(sd_bus_message *m, const char *field, const char *eq) {
6550c24c
LP
817 const char *suffix;
818 int r;
cffaed83 819
c57d2a76
ZJS
820 if (STR_IN_SET(field, "User",
821 "Group",
822 "UtmpIdentifier",
823 "UtmpMode",
824 "PAMName",
825 "TTYPath",
826 "WorkingDirectory",
827 "RootDirectory",
828 "SyslogIdentifier",
829 "ProtectSystem",
830 "ProtectHome",
831 "SELinuxContext",
832 "RootImage",
833 "RuntimeDirectoryPreserve",
834 "Personality",
835 "KeyringMode",
836 "NetworkNamespacePath"))
89ada3ba 837 return bus_append_string(m, field, eq);
cffaed83 838
c57d2a76
ZJS
839 if (STR_IN_SET(field, "IgnoreSIGPIPE",
840 "TTYVHangup",
841 "TTYReset",
842 "TTYVTDisallocate",
843 "PrivateTmp",
844 "PrivateDevices",
845 "PrivateNetwork",
846 "PrivateUsers",
847 "PrivateMounts",
848 "NoNewPrivileges",
849 "SyslogLevelPrefix",
850 "MemoryDenyWriteExecute",
851 "RestrictRealtime",
852 "DynamicUser",
853 "RemoveIPC",
854 "ProtectKernelTunables",
855 "ProtectKernelModules",
856 "ProtectKernelLogs",
857 "ProtectControlGroups",
858 "MountAPIVFS",
859 "CPUSchedulingResetOnFork",
860 "LockPersonality",
861 "ProtectHostname",
862 "RestrictSUIDSGID"))
89ada3ba 863 return bus_append_parse_boolean(m, field, eq);
cffaed83 864
c57d2a76
ZJS
865 if (STR_IN_SET(field, "ReadWriteDirectories",
866 "ReadOnlyDirectories",
867 "InaccessibleDirectories",
868 "ReadWritePaths",
869 "ReadOnlyPaths",
870 "InaccessiblePaths",
871 "RuntimeDirectory",
872 "StateDirectory",
873 "CacheDirectory",
874 "LogsDirectory",
875 "ConfigurationDirectory",
876 "SupplementaryGroups",
877 "SystemCallArchitectures"))
4ec85141 878 return bus_append_strv(m, field, eq, EXTRACT_UNQUOTE);
20b16441 879
c57d2a76
ZJS
880 if (STR_IN_SET(field, "SyslogLevel",
881 "LogLevelMax"))
89ada3ba 882 return bus_append_log_level_from_string(m, field, eq);
20b16441 883
89ada3ba 884 if (streq(field, "SyslogFacility"))
89ada3ba 885 return bus_append_log_facility_unshifted_from_string(m, field, eq);
cffaed83 886
89ada3ba 887 if (streq(field, "SecureBits"))
89ada3ba 888 return bus_append_secure_bits_from_string(m, field, eq);
cffaed83 889
89ada3ba 890 if (streq(field, "CPUSchedulingPolicy"))
89ada3ba 891 return bus_append_sched_policy_from_string(m, field, eq);
cffaed83 892
c57d2a76
ZJS
893 if (STR_IN_SET(field, "CPUSchedulingPriority",
894 "OOMScoreAdjust"))
89ada3ba 895 return bus_append_safe_atoi(m, field, eq);
3f856a28 896
89ada3ba 897 if (streq(field, "Nice"))
89ada3ba 898 return bus_append_parse_nice(m, field, eq);
3f856a28 899
89ada3ba 900 if (streq(field, "SystemCallErrorNumber"))
89ada3ba 901 return bus_append_parse_errno(m, field, eq);
cffaed83 902
89ada3ba 903 if (streq(field, "IOSchedulingClass"))
89ada3ba 904 return bus_append_ioprio_class_from_string(m, field, eq);
cffaed83 905
89ada3ba 906 if (streq(field, "IOSchedulingPriority"))
89ada3ba 907 return bus_append_ioprio_parse_priority(m, field, eq);
cffaed83 908
c57d2a76
ZJS
909 if (STR_IN_SET(field, "RuntimeDirectoryMode",
910 "StateDirectoryMode",
911 "CacheDirectoryMode",
912 "LogsDirectoryMode",
913 "ConfigurationDirectoryMode",
914 "UMask"))
89ada3ba 915 return bus_append_parse_mode(m, field, eq);
cffaed83 916
89ada3ba 917 if (streq(field, "TimerSlackNSec"))
89ada3ba 918 return bus_append_parse_nsec(m, field, eq);
cffaed83 919
691d6f6d 920 if (streq(field, "LogRateLimitIntervalSec"))
90fc172e
AZ
921 return bus_append_parse_sec_rename(m, field, eq);
922
923 if (streq(field, "LogRateLimitBurst"))
90fc172e
AZ
924 return bus_append_safe_atou(m, field, eq);
925
89ada3ba 926 if (streq(field, "MountFlags"))
89ada3ba 927 return bus_append_mount_propagation_flags_from_string(m, field, eq);
cffaed83 928
c57d2a76
ZJS
929 if (STR_IN_SET(field, "Environment",
930 "UnsetEnvironment",
931 "PassEnvironment"))
4ec85141 932 return bus_append_strv(m, field, eq, EXTRACT_UNQUOTE|EXTRACT_CUNESCAPE);
cffaed83 933
89ada3ba 934 if (streq(field, "EnvironmentFile")) {
89ada3ba
YW
935 if (isempty(eq))
936 r = sd_bus_message_append(m, "(sv)", "EnvironmentFiles", "a(sb)", 0);
937 else
938 r = sd_bus_message_append(m, "(sv)", "EnvironmentFiles", "a(sb)", 1,
939 eq[0] == '-' ? eq + 1 : eq,
940 eq[0] == '-');
941 if (r < 0)
942 return bus_log_create_error(r);
cffaed83 943
89ada3ba
YW
944 return 1;
945 }
cffaed83 946
89ada3ba 947 if (streq(field, "LogExtraFields")) {
89ada3ba 948 r = sd_bus_message_open_container(m, 'r', "sv");
3f856a28
YW
949 if (r < 0)
950 return bus_log_create_error(r);
951
89ada3ba 952 r = sd_bus_message_append_basic(m, 's', "LogExtraFields");
3f856a28
YW
953 if (r < 0)
954 return bus_log_create_error(r);
955
89ada3ba 956 r = sd_bus_message_open_container(m, 'v', "aay");
3f856a28
YW
957 if (r < 0)
958 return bus_log_create_error(r);
959
89ada3ba 960 r = sd_bus_message_open_container(m, 'a', "ay");
cffaed83
YW
961 if (r < 0)
962 return bus_log_create_error(r);
963
89ada3ba 964 r = sd_bus_message_append_array(m, 'y', eq, strlen(eq));
3f856a28
YW
965 if (r < 0)
966 return bus_log_create_error(r);
cffaed83 967
3f856a28
YW
968 r = sd_bus_message_close_container(m);
969 if (r < 0)
970 return bus_log_create_error(r);
9f617cd0 971
89ada3ba
YW
972 r = sd_bus_message_close_container(m);
973 if (r < 0)
974 return bus_log_create_error(r);
9efb9df9 975
89ada3ba 976 r = sd_bus_message_close_container(m);
9efb9df9 977 if (r < 0)
89ada3ba
YW
978 return bus_log_create_error(r);
979
980 return 1;
981 }
9efb9df9 982
c57d2a76
ZJS
983 if (STR_IN_SET(field, "StandardInput",
984 "StandardOutput",
985 "StandardError")) {
89ada3ba 986 const char *n, *appended;
9efb9df9 987
89ada3ba
YW
988 if ((n = startswith(eq, "fd:"))) {
989 appended = strjoina(field, "FileDescriptorName");
990 r = sd_bus_message_append(m, "(sv)", appended, "s", n);
991 } else if ((n = startswith(eq, "file:"))) {
992 appended = strjoina(field, "File");
993 r = sd_bus_message_append(m, "(sv)", appended, "s", n);
8d33232e
LP
994 } else if ((n = startswith(eq, "append:"))) {
995 appended = strjoina(field, "FileToAppend");
996 r = sd_bus_message_append(m, "(sv)", appended, "s", n);
89ada3ba
YW
997 } else
998 r = sd_bus_message_append(m, "(sv)", field, "s", eq);
89ada3ba
YW
999 if (r < 0)
1000 return bus_log_create_error(r);
7f452159 1001
89ada3ba
YW
1002 return 1;
1003 }
7f452159 1004
89ada3ba
YW
1005 if (streq(field, "StandardInputText")) {
1006 _cleanup_free_ char *unescaped = NULL;
7f452159 1007
89ada3ba 1008 r = cunescape(eq, 0, &unescaped);
7f452159 1009 if (r < 0)
89ada3ba 1010 return log_error_errno(r, "Failed to unescape text '%s': %m", eq);
7f452159 1011
89ada3ba
YW
1012 if (!strextend(&unescaped, "\n", NULL))
1013 return log_oom();
7f452159 1014
89ada3ba
YW
1015 /* Note that we don't expand specifiers here, but that should be OK, as this is a programmatic
1016 * interface anyway */
20b16441 1017
89ada3ba
YW
1018 return bus_append_byte_array(m, field, unescaped, strlen(unescaped));
1019 }
20b16441 1020
89ada3ba
YW
1021 if (streq(field, "StandardInputData")) {
1022 _cleanup_free_ void *decoded = NULL;
1023 size_t sz;
1024
1025 r = unbase64mem(eq, (size_t) -1, &decoded, &sz);
20b16441 1026 if (r < 0)
89ada3ba 1027 return log_error_errno(r, "Failed to decode base64 data '%s': %m", eq);
20b16441 1028
89ada3ba
YW
1029 return bus_append_byte_array(m, field, decoded, sz);
1030 }
20b16441 1031
6550c24c
LP
1032 if ((suffix = startswith(field, "Limit"))) {
1033 int rl;
20b16441 1034
6550c24c
LP
1035 rl = rlimit_from_string(suffix);
1036 if (rl >= 0) {
1037 const char *sn;
1038 struct rlimit l;
20b16441 1039
6550c24c
LP
1040 r = rlimit_parse(rl, eq, &l);
1041 if (r < 0)
1042 return log_error_errno(r, "Failed to parse resource limit: %s", eq);
20b16441 1043
6550c24c
LP
1044 r = sd_bus_message_append(m, "(sv)", field, "t", l.rlim_max);
1045 if (r < 0)
1046 return bus_log_create_error(r);
20b16441 1047
6550c24c
LP
1048 sn = strjoina(field, "Soft");
1049 r = sd_bus_message_append(m, "(sv)", sn, "t", l.rlim_cur);
1050 if (r < 0)
1051 return bus_log_create_error(r);
1052
1053 return 1;
1054 }
89ada3ba 1055 }
20b16441 1056
c57d2a76
ZJS
1057 if (STR_IN_SET(field, "AppArmorProfile",
1058 "SmackProcessLabel")) {
89ada3ba
YW
1059 int ignore = 0;
1060 const char *s = eq;
20b16441 1061
89ada3ba
YW
1062 if (eq[0] == '-') {
1063 ignore = 1;
1064 s = eq + 1;
20b16441
LP
1065 }
1066
89ada3ba 1067 r = sd_bus_message_append(m, "(sv)", field, "(bs)", ignore, s);
6bbfdc67 1068 if (r < 0)
89ada3ba 1069 return bus_log_create_error(r);
20b16441 1070
89ada3ba
YW
1071 return 1;
1072 }
20b16441 1073
c57d2a76
ZJS
1074 if (STR_IN_SET(field, "CapabilityBoundingSet",
1075 "AmbientCapabilities")) {
89ada3ba
YW
1076 uint64_t sum = 0;
1077 bool invert = false;
1078 const char *p = eq;
afcb1cd3 1079
89ada3ba
YW
1080 if (*p == '~') {
1081 invert = true;
1082 p++;
1083 }
20b16441 1084
89ada3ba 1085 r = capability_set_from_string(p, &sum);
20b16441 1086 if (r < 0)
89ada3ba 1087 return log_error_errno(r, "Failed to parse %s value %s: %m", field, eq);
20b16441 1088
89ada3ba
YW
1089 sum = invert ? ~sum : sum;
1090
1091 r = sd_bus_message_append(m, "(sv)", field, "t", sum);
20b16441
LP
1092 if (r < 0)
1093 return bus_log_create_error(r);
1094
89ada3ba
YW
1095 return 1;
1096 }
20b16441 1097
89ada3ba 1098 if (streq(field, "CPUAffinity")) {
0985c7c4 1099 _cleanup_(cpu_set_reset) CPUSet cpuset = {};
c367f996
MS
1100 _cleanup_free_ uint8_t *array = NULL;
1101 size_t allocated;
20b16441 1102
89ada3ba
YW
1103 r = parse_cpu_set(eq, &cpuset);
1104 if (r < 0)
1105 return log_error_errno(r, "Failed to parse %s value: %s", field, eq);
20b16441 1106
c367f996
MS
1107 r = cpu_set_to_dbus(&cpuset, &array, &allocated);
1108 if (r < 0)
1109 return log_error_errno(r, "Failed to serialize CPUAffinity: %m");
1110
1111 return bus_append_byte_array(m, field, array, allocated);
89ada3ba 1112 }
20b7a007 1113
b070c7c0
MS
1114 if (streq(field, "NUMAPolicy")) {
1115 r = mpol_from_string(eq);
1116 if (r < 0)
1117 return log_error_errno(r, "Failed to parse %s value: %s", field, eq);
1118
1119 r = sd_bus_message_append(m, "(sv)", field, "i", (int32_t) r);
1120 if (r < 0)
1121 return bus_log_create_error(r);
1122
1123 return 1;
1124 }
1125
1126 if (streq(field, "NUMAMask")) {
1127 _cleanup_(cpu_set_reset) CPUSet nodes = {};
1128 _cleanup_free_ uint8_t *array = NULL;
1129 size_t allocated;
1130
1131 r = parse_cpu_set(eq, &nodes);
1132 if (r < 0)
1133 return log_error_errno(r, "Failed to parse %s value: %s", field, eq);
1134
1135 r = cpu_set_to_dbus(&nodes, &array, &allocated);
1136 if (r < 0)
1137 return log_error_errno(r, "Failed to serialize NUMAMask: %m");
1138
1139 return bus_append_byte_array(m, field, array, allocated);
1140 }
1141
c57d2a76
ZJS
1142 if (STR_IN_SET(field, "RestrictAddressFamilies",
1143 "SystemCallFilter")) {
89ada3ba
YW
1144 int whitelist = 1;
1145 const char *p = eq;
20b16441 1146
89ada3ba
YW
1147 if (*p == '~') {
1148 whitelist = 0;
1149 p++;
20b16441
LP
1150 }
1151
89ada3ba 1152 r = sd_bus_message_open_container(m, SD_BUS_TYPE_STRUCT, "sv");
20b16441
LP
1153 if (r < 0)
1154 return bus_log_create_error(r);
1155
89ada3ba
YW
1156 r = sd_bus_message_append_basic(m, SD_BUS_TYPE_STRING, field);
1157 if (r < 0)
1158 return bus_log_create_error(r);
1159
1160 r = sd_bus_message_open_container(m, 'v', "(bas)");
1161 if (r < 0)
1162 return bus_log_create_error(r);
20b16441 1163
89ada3ba
YW
1164 r = sd_bus_message_open_container(m, 'r', "bas");
1165 if (r < 0)
1166 return bus_log_create_error(r);
cffaed83 1167
89ada3ba 1168 r = sd_bus_message_append_basic(m, 'b', &whitelist);
cffaed83
YW
1169 if (r < 0)
1170 return bus_log_create_error(r);
1171
1172 r = sd_bus_message_open_container(m, 'a', "s");
1173 if (r < 0)
1174 return bus_log_create_error(r);
1175
98008caa 1176 for (;;) {
cffaed83
YW
1177 _cleanup_free_ char *word = NULL;
1178
4ec85141 1179 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
cffaed83
YW
1180 if (r == 0)
1181 break;
89ada3ba
YW
1182 if (r == -ENOMEM)
1183 return log_oom();
1184 if (r < 0)
1185 return log_error_errno(r, "Invalid syntax: %s", eq);
cffaed83
YW
1186
1187 r = sd_bus_message_append_basic(m, 's', word);
1188 if (r < 0)
1189 return bus_log_create_error(r);
1190 }
1191
1192 r = sd_bus_message_close_container(m);
1193 if (r < 0)
1194 return bus_log_create_error(r);
1195
1196 r = sd_bus_message_close_container(m);
20b16441
LP
1197 if (r < 0)
1198 return bus_log_create_error(r);
1199
89ada3ba 1200 r = sd_bus_message_close_container(m);
20b16441
LP
1201 if (r < 0)
1202 return bus_log_create_error(r);
1203
20b16441
LP
1204 r = sd_bus_message_close_container(m);
1205 if (r < 0)
1206 return bus_log_create_error(r);
1207
89ada3ba
YW
1208 return 1;
1209 }
20b16441 1210
89ada3ba 1211 if (streq(field, "RestrictNamespaces")) {
add00535 1212 bool invert = false;
aa9d574d 1213 unsigned long flags;
add00535
LP
1214
1215 r = parse_boolean(eq);
1216 if (r > 0)
1217 flags = 0;
1218 else if (r == 0)
1219 flags = NAMESPACE_FLAGS_ALL;
1220 else {
aa9d574d
YW
1221 if (eq[0] == '~') {
1222 invert = true;
1223 eq++;
1224 }
1225
86c2a9f1 1226 r = namespace_flags_from_string(eq, &flags);
add00535
LP
1227 if (r < 0)
1228 return log_error_errno(r, "Failed to parse %s value %s.", field, eq);
1229 }
1230
1231 if (invert)
1232 flags = (~flags) & NAMESPACE_FLAGS_ALL;
1233
89ada3ba
YW
1234 r = sd_bus_message_append(m, "(sv)", field, "t", (uint64_t) flags);
1235 if (r < 0)
1236 return bus_log_create_error(r);
afcb1cd3 1237
89ada3ba
YW
1238 return 1;
1239 }
afcb1cd3 1240
c57d2a76
ZJS
1241 if (STR_IN_SET(field, "BindPaths",
1242 "BindReadOnlyPaths")) {
89ada3ba 1243 const char *p = eq;
83555251 1244
89ada3ba 1245 r = sd_bus_message_open_container(m, SD_BUS_TYPE_STRUCT, "sv");
c7383828 1246 if (r < 0)
89ada3ba 1247 return bus_log_create_error(r);
afcb1cd3 1248
89ada3ba
YW
1249 r = sd_bus_message_append_basic(m, SD_BUS_TYPE_STRING, field);
1250 if (r < 0)
1251 return bus_log_create_error(r);
d2d6c096
LP
1252
1253 r = sd_bus_message_open_container(m, 'v', "a(ssbt)");
1254 if (r < 0)
89ada3ba 1255 return bus_log_create_error(r);
d2d6c096
LP
1256
1257 r = sd_bus_message_open_container(m, 'a', "(ssbt)");
1258 if (r < 0)
89ada3ba 1259 return bus_log_create_error(r);
d2d6c096
LP
1260
1261 for (;;) {
1262 _cleanup_free_ char *source = NULL, *destination = NULL;
1263 char *s = NULL, *d = NULL;
1264 bool ignore_enoent = false;
1265 uint64_t flags = MS_REC;
1266
4ec85141 1267 r = extract_first_word(&p, &source, ":" WHITESPACE, EXTRACT_UNQUOTE|EXTRACT_DONT_COALESCE_SEPARATORS);
d2d6c096
LP
1268 if (r < 0)
1269 return log_error_errno(r, "Failed to parse argument: %m");
1270 if (r == 0)
1271 break;
1272
1273 s = source;
1274 if (s[0] == '-') {
1275 ignore_enoent = true;
1276 s++;
1277 }
1278
1279 if (p && p[-1] == ':') {
4ec85141 1280 r = extract_first_word(&p, &destination, ":" WHITESPACE, EXTRACT_UNQUOTE|EXTRACT_DONT_COALESCE_SEPARATORS);
d2d6c096
LP
1281 if (r < 0)
1282 return log_error_errno(r, "Failed to parse argument: %m");
baaa35ad
ZJS
1283 if (r == 0)
1284 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1285 "Missing argument after ':': %s",
1286 eq);
d2d6c096
LP
1287
1288 d = destination;
1289
1290 if (p && p[-1] == ':') {
1291 _cleanup_free_ char *options = NULL;
1292
4ec85141 1293 r = extract_first_word(&p, &options, NULL, EXTRACT_UNQUOTE);
d2d6c096
LP
1294 if (r < 0)
1295 return log_error_errno(r, "Failed to parse argument: %m");
1296
1297 if (isempty(options) || streq(options, "rbind"))
1298 flags = MS_REC;
1299 else if (streq(options, "norbind"))
1300 flags = 0;
baaa35ad
ZJS
1301 else
1302 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1303 "Unknown options: %s",
1304 eq);
d2d6c096
LP
1305 }
1306 } else
1307 d = s;
1308
d2d6c096
LP
1309 r = sd_bus_message_append(m, "(ssbt)", s, d, ignore_enoent, flags);
1310 if (r < 0)
89ada3ba 1311 return bus_log_create_error(r);
d2d6c096
LP
1312 }
1313
1314 r = sd_bus_message_close_container(m);
1315 if (r < 0)
89ada3ba
YW
1316 return bus_log_create_error(r);
1317
1318 r = sd_bus_message_close_container(m);
1319 if (r < 0)
1320 return bus_log_create_error(r);
d2d6c096
LP
1321
1322 r = sd_bus_message_close_container(m);
89ada3ba
YW
1323 if (r < 0)
1324 return bus_log_create_error(r);
f6c66be1 1325
89ada3ba
YW
1326 return 1;
1327 }
f6c66be1 1328
784ad252
YW
1329 if (streq(field, "TemporaryFileSystem")) {
1330 const char *p = eq;
1331
1332 r = sd_bus_message_open_container(m, SD_BUS_TYPE_STRUCT, "sv");
1333 if (r < 0)
1334 return bus_log_create_error(r);
1335
1336 r = sd_bus_message_append_basic(m, SD_BUS_TYPE_STRING, field);
1337 if (r < 0)
1338 return bus_log_create_error(r);
1339
1340 r = sd_bus_message_open_container(m, 'v', "a(ss)");
1341 if (r < 0)
1342 return bus_log_create_error(r);
1343
1344 r = sd_bus_message_open_container(m, 'a', "(ss)");
1345 if (r < 0)
1346 return bus_log_create_error(r);
1347
1348 for (;;) {
1349 _cleanup_free_ char *word = NULL, *path = NULL;
1350 const char *w;
1351
4ec85141 1352 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
784ad252
YW
1353 if (r < 0)
1354 return log_error_errno(r, "Failed to parse argument: %m");
1355 if (r == 0)
1356 break;
1357
1358 w = word;
1359 r = extract_first_word(&w, &path, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
1360 if (r < 0)
1361 return log_error_errno(r, "Failed to parse argument: %m");
baaa35ad
ZJS
1362 if (r == 0)
1363 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1364 "Failed to parse argument: %s",
1365 p);
784ad252
YW
1366
1367 r = sd_bus_message_append(m, "(ss)", path, w);
1368 if (r < 0)
1369 return bus_log_create_error(r);
1370 }
1371
1372 r = sd_bus_message_close_container(m);
1373 if (r < 0)
1374 return bus_log_create_error(r);
1375
1376 r = sd_bus_message_close_container(m);
1377 if (r < 0)
1378 return bus_log_create_error(r);
1379
1380 r = sd_bus_message_close_container(m);
1381 if (r < 0)
1382 return bus_log_create_error(r);
1383
1384 return 1;
1385 }
1386
89ada3ba
YW
1387 return 0;
1388}
f6c66be1 1389
89ada3ba 1390static int bus_append_kill_property(sd_bus_message *m, const char *field, const char *eq) {
89ada3ba 1391 if (streq(field, "KillMode"))
89ada3ba 1392 return bus_append_string(m, field, eq);
f6c66be1 1393
c57d2a76
ZJS
1394 if (STR_IN_SET(field, "SendSIGHUP",
1395 "SendSIGKILL"))
89ada3ba 1396 return bus_append_parse_boolean(m, field, eq);
f6c66be1 1397
c57d2a76
ZJS
1398 if (STR_IN_SET(field, "KillSignal",
1399 "RestartKillSignal",
1400 "FinalKillSignal",
1401 "WatchdogSignal"))
29a3db75 1402 return bus_append_signal_from_string(m, field, eq);
89ada3ba
YW
1403
1404 return 0;
1405}
1406
3d63c749 1407static int bus_append_mount_property(sd_bus_message *m, const char *field, const char *eq) {
89ada3ba 1408
c57d2a76
ZJS
1409 if (STR_IN_SET(field, "What",
1410 "Where",
1411 "Options",
1412 "Type"))
89ada3ba
YW
1413 return bus_append_string(m, field, eq);
1414
3d63c749 1415 if (streq(field, "TimeoutSec"))
3d63c749
YW
1416 return bus_append_parse_sec_rename(m, field, eq);
1417
1418 if (streq(field, "DirectoryMode"))
3d63c749
YW
1419 return bus_append_parse_mode(m, field, eq);
1420
c57d2a76
ZJS
1421 if (STR_IN_SET(field, "SloppyOptions",
1422 "LazyUnmount",
1423 "ForceUnmount"))
3d63c749
YW
1424 return bus_append_parse_boolean(m, field, eq);
1425
1426 return 0;
1427}
1428
1429static int bus_append_path_property(sd_bus_message *m, const char *field, const char *eq) {
1430 int r;
1431
89ada3ba 1432 if (streq(field, "MakeDirectory"))
89ada3ba
YW
1433 return bus_append_parse_boolean(m, field, eq);
1434
1435 if (streq(field, "DirectoryMode"))
89ada3ba
YW
1436 return bus_append_parse_mode(m, field, eq);
1437
c57d2a76
ZJS
1438 if (STR_IN_SET(field, "PathExists",
1439 "PathExistsGlob",
1440 "PathChanged",
1441 "PathModified",
1442 "DirectoryNotEmpty")) {
3d63c749
YW
1443 if (isempty(eq))
1444 r = sd_bus_message_append(m, "(sv)", "Paths", "a(ss)", 0);
1445 else
1446 r = sd_bus_message_append(m, "(sv)", "Paths", "a(ss)", 1, field, eq);
1447 if (r < 0)
1448 return bus_log_create_error(r);
1449
1450 return 1;
1451 }
1452
89ada3ba
YW
1453 return 0;
1454}
1455
5a70a68f 1456static int bus_append_scope_property(sd_bus_message *m, const char *field, const char *eq) {
9ed7de60 1457 if (streq(field, "RuntimeMaxSec"))
9ed7de60
PW
1458 return bus_append_parse_sec_rename(m, field, eq);
1459
5a70a68f 1460 if (streq(field, "TimeoutStopSec"))
5a70a68f
PW
1461 return bus_append_parse_sec_rename(m, field, eq);
1462
1463 return 0;
1464}
1465
89ada3ba 1466static int bus_append_service_property(sd_bus_message *m, const char *field, const char *eq) {
3d63c749 1467 int r;
89ada3ba 1468
c57d2a76
ZJS
1469 if (STR_IN_SET(field, "PIDFile",
1470 "Type",
1471 "Restart",
1472 "BusName",
1473 "NotifyAccess",
1474 "USBFunctionDescriptors",
1475 "USBFunctionStrings",
1476 "OOMPolicy"))
89ada3ba 1477 return bus_append_string(m, field, eq);
f6c66be1 1478
c57d2a76
ZJS
1479 if (STR_IN_SET(field, "PermissionsStartOnly",
1480 "RootDirectoryStartOnly",
1481 "RemainAfterExit",
1482 "GuessMainPID"))
89ada3ba
YW
1483 return bus_append_parse_boolean(m, field, eq);
1484
c57d2a76
ZJS
1485 if (STR_IN_SET(field, "RestartSec",
1486 "TimeoutStartSec",
1487 "TimeoutStopSec",
1488 "RuntimeMaxSec",
1489 "WatchdogSec"))
89ada3ba
YW
1490 return bus_append_parse_sec_rename(m, field, eq);
1491
3d63c749 1492 if (streq(field, "TimeoutSec")) {
3d63c749
YW
1493 r = bus_append_parse_sec_rename(m, "TimeoutStartSec", eq);
1494 if (r < 0)
1495 return r;
1496
1497 return bus_append_parse_sec_rename(m, "TimeoutStopSec", eq);
1498 }
1499
89ada3ba 1500 if (streq(field, "FileDescriptorStoreMax"))
89ada3ba
YW
1501 return bus_append_safe_atou(m, field, eq);
1502
c57d2a76
ZJS
1503 if (STR_IN_SET(field, "ExecCondition",
1504 "ExecStartPre",
1505 "ExecStart",
1506 "ExecStartPost",
1507 "ExecConditionEx",
1508 "ExecStartPreEx",
1509 "ExecStartEx",
1510 "ExecStartPostEx",
1511 "ExecReload",
1512 "ExecStop",
1513 "ExecStopPost",
1514 "ExecReloadEx",
1515 "ExecStopEx",
1516 "ExecStopPostEx"))
89ada3ba
YW
1517 return bus_append_exec_command(m, field, eq);
1518
c57d2a76
ZJS
1519 if (STR_IN_SET(field, "RestartPreventExitStatus",
1520 "RestartForceExitStatus",
1521 "SuccessExitStatus")) {
3d63c749 1522 _cleanup_free_ int *status = NULL, *signal = NULL;
62b21e2e 1523 size_t n_status = 0, n_signal = 0;
3d63c749
YW
1524 const char *p;
1525
1526 for (p = eq;;) {
1527 _cleanup_free_ char *word = NULL;
3d63c749 1528
4ec85141 1529 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
3d63c749
YW
1530 if (r == 0)
1531 break;
1532 if (r == -ENOMEM)
1533 return log_oom();
1534 if (r < 0)
1535 return log_error_errno(r, "Invalid syntax in %s: %s", field, eq);
1536
2e2ed880
ZJS
1537 /* We need to call exit_status_from_string() first, because we want
1538 * to parse numbers as exit statuses, not signals. */
3d63c749 1539
2e2ed880
ZJS
1540 r = exit_status_from_string(word);
1541 if (r >= 0) {
1542 assert(r >= 0 && r < 256);
3d63c749 1543
62b21e2e 1544 status = reallocarray(status, n_status + 1, sizeof(int));
3d63c749
YW
1545 if (!status)
1546 return log_oom();
1547
2e2ed880
ZJS
1548 status[n_status++] = r;
1549
1550 } else if ((r = signal_from_string(word)) >= 0) {
1551 signal = reallocarray(signal, n_signal + 1, sizeof(int));
1552 if (!signal)
1553 return log_oom();
1554
1555 signal[n_signal++] = r;
1556
1557 } else
1558 /* original r from exit_status_to_string() */
1559 return log_error_errno(r, "Invalid status or signal %s in %s: %m",
1560 word, field);
3d63c749
YW
1561 }
1562
1563 r = sd_bus_message_open_container(m, SD_BUS_TYPE_STRUCT, "sv");
1564 if (r < 0)
1565 return bus_log_create_error(r);
1566
1567 r = sd_bus_message_append_basic(m, SD_BUS_TYPE_STRING, field);
1568 if (r < 0)
1569 return bus_log_create_error(r);
1570
1571 r = sd_bus_message_open_container(m, 'v', "(aiai)");
1572 if (r < 0)
1573 return bus_log_create_error(r);
1574
1575 r = sd_bus_message_open_container(m, 'r', "aiai");
1576 if (r < 0)
1577 return bus_log_create_error(r);
1578
62b21e2e 1579 r = sd_bus_message_append_array(m, 'i', status, n_status * sizeof(int));
3d63c749
YW
1580 if (r < 0)
1581 return bus_log_create_error(r);
1582
62b21e2e 1583 r = sd_bus_message_append_array(m, 'i', signal, n_signal * sizeof(int));
3d63c749
YW
1584 if (r < 0)
1585 return bus_log_create_error(r);
1586
1587 r = sd_bus_message_close_container(m);
1588 if (r < 0)
1589 return bus_log_create_error(r);
1590
1591 r = sd_bus_message_close_container(m);
1592 if (r < 0)
1593 return bus_log_create_error(r);
1594
1595 r = sd_bus_message_close_container(m);
1596 if (r < 0)
1597 return bus_log_create_error(r);
1598
1599 return 1;
1600 }
1601
89ada3ba
YW
1602 return 0;
1603}
1604
1605static int bus_append_socket_property(sd_bus_message *m, const char *field, const char *eq) {
1606 int r;
1607
c57d2a76
ZJS
1608 if (STR_IN_SET(field, "Accept",
1609 "Writable",
1610 "KeepAlive",
1611 "NoDelay",
1612 "FreeBind",
1613 "Transparent",
1614 "Broadcast",
1615 "PassCredentials",
1616 "PassSecurity",
1617 "ReusePort",
1618 "RemoveOnStop",
1619 "SELinuxContextFromNet"))
89ada3ba
YW
1620 return bus_append_parse_boolean(m, field, eq);
1621
c57d2a76
ZJS
1622 if (STR_IN_SET(field, "Priority",
1623 "IPTTL",
1624 "Mark"))
89ada3ba
YW
1625 return bus_append_safe_atoi(m, field, eq);
1626
1627 if (streq(field, "IPTOS"))
89ada3ba
YW
1628 return bus_append_ip_tos_from_string(m, field, eq);
1629
c57d2a76
ZJS
1630 if (STR_IN_SET(field, "Backlog",
1631 "MaxConnections",
1632 "MaxConnectionsPerSource",
1633 "KeepAliveProbes",
1634 "TriggerLimitBurst"))
89ada3ba
YW
1635 return bus_append_safe_atou(m, field, eq);
1636
c57d2a76
ZJS
1637 if (STR_IN_SET(field, "SocketMode",
1638 "DirectoryMode"))
89ada3ba
YW
1639 return bus_append_parse_mode(m, field, eq);
1640
c57d2a76
ZJS
1641 if (STR_IN_SET(field, "MessageQueueMaxMessages",
1642 "MessageQueueMessageSize"))
89ada3ba
YW
1643 return bus_append_safe_atoi64(m, field, eq);
1644
c57d2a76
ZJS
1645 if (STR_IN_SET(field, "TimeoutSec",
1646 "KeepAliveTimeSec",
1647 "KeepAliveIntervalSec",
1648 "DeferAcceptSec",
1649 "TriggerLimitIntervalSec"))
89ada3ba
YW
1650 return bus_append_parse_sec_rename(m, field, eq);
1651
c57d2a76
ZJS
1652 if (STR_IN_SET(field, "ReceiveBuffer",
1653 "SendBuffer",
1654 "PipeSize"))
b48e508d 1655 return bus_append_parse_size(m, field, eq, 1024);
89ada3ba 1656
c57d2a76
ZJS
1657 if (STR_IN_SET(field, "ExecStartPre",
1658 "ExecStartPost",
1659 "ExecReload",
1660 "ExecStopPost"))
89ada3ba
YW
1661 return bus_append_exec_command(m, field, eq);
1662
c57d2a76
ZJS
1663 if (STR_IN_SET(field, "SmackLabel",
1664 "SmackLabelIPIn",
1665 "SmackLabelIPOut",
1666 "TCPCongestion",
1667 "BindToDevice",
1668 "BindIPv6Only",
1669 "FileDescriptorName",
1670 "SocketUser",
1671 "SocketGroup"))
89ada3ba
YW
1672 return bus_append_string(m, field, eq);
1673
1674 if (streq(field, "Symlinks"))
4ec85141 1675 return bus_append_strv(m, field, eq, EXTRACT_UNQUOTE);
89ada3ba 1676
e045e325 1677 if (streq(field, "SocketProtocol"))
d2b42d63 1678 return bus_append_parse_ip_protocol(m, field, eq);
89ada3ba 1679
c57d2a76
ZJS
1680 if (STR_IN_SET(field, "ListenStream",
1681 "ListenDatagram",
1682 "ListenSequentialPacket",
1683 "ListenNetlink",
1684 "ListenSpecial",
1685 "ListenMessageQueue",
1686 "ListenFIFO",
1687 "ListenUSBFunction")) {
3d63c749
YW
1688 if (isempty(eq))
1689 r = sd_bus_message_append(m, "(sv)", "Listen", "a(ss)", 0);
1690 else
81b1dc27 1691 r = sd_bus_message_append(m, "(sv)", "Listen", "a(ss)", 1, field + STRLEN("Listen"), eq);
f6c66be1 1692 if (r < 0)
89ada3ba
YW
1693 return bus_log_create_error(r);
1694
1695 return 1;
1696 }
1697
1698 return 0;
1699}
1700static int bus_append_timer_property(sd_bus_message *m, const char *field, const char *eq) {
3d63c749 1701 int r;
89ada3ba 1702
c57d2a76
ZJS
1703 if (STR_IN_SET(field, "WakeSystem",
1704 "RemainAfterElapse",
1705 "Persistent",
1706 "OnTimezoneChange",
1707 "OnClockChange"))
89ada3ba
YW
1708 return bus_append_parse_boolean(m, field, eq);
1709
c57d2a76
ZJS
1710 if (STR_IN_SET(field, "AccuracySec",
1711 "RandomizedDelaySec"))
3d63c749
YW
1712 return bus_append_parse_sec_rename(m, field, eq);
1713
c57d2a76
ZJS
1714 if (STR_IN_SET(field, "OnActiveSec",
1715 "OnBootSec",
1716 "OnStartupSec",
1717 "OnUnitActiveSec",
1718 "OnUnitInactiveSec")) {
3d63c749
YW
1719 if (isempty(eq))
1720 r = sd_bus_message_append(m, "(sv)", "TimersMonotonic", "a(st)", 0);
1721 else {
1722 usec_t t;
1723 r = parse_sec(eq, &t);
1724 if (r < 0)
1725 return log_error_errno(r, "Failed to parse %s=%s: %m", field, eq);
89ada3ba 1726
3d63c749
YW
1727 r = sd_bus_message_append(m, "(sv)", "TimersMonotonic", "a(st)", 1, field, t);
1728 }
1729 if (r < 0)
1730 return bus_log_create_error(r);
89ada3ba 1731
3d63c749
YW
1732 return 1;
1733 }
1734
1735 if (streq(field, "OnCalendar")) {
3d63c749
YW
1736 if (isempty(eq))
1737 r = sd_bus_message_append(m, "(sv)", "TimersCalendar", "a(ss)", 0);
1738 else
1739 r = sd_bus_message_append(m, "(sv)", "TimersCalendar", "a(ss)", 1, field, eq);
1740 if (r < 0)
1741 return bus_log_create_error(r);
1742
1743 return 1;
1744 }
89ada3ba
YW
1745
1746 return 0;
1747}
1748
1749static int bus_append_unit_property(sd_bus_message *m, const char *field, const char *eq) {
3d63c749
YW
1750 ConditionType t = _CONDITION_TYPE_INVALID;
1751 bool is_condition = false;
1752 int r;
f6c66be1 1753
c57d2a76
ZJS
1754 if (STR_IN_SET(field, "Description",
1755 "SourcePath",
1756 "OnFailureJobMode",
1757 "JobTimeoutAction",
1758 "JobTimeoutRebootArgument",
1759 "StartLimitAction",
1760 "FailureAction",
1761 "SuccessAction",
1762 "RebootArgument",
1763 "CollectMode"))
89ada3ba
YW
1764 return bus_append_string(m, field, eq);
1765
c57d2a76
ZJS
1766 if (STR_IN_SET(field, "StopWhenUnneeded",
1767 "RefuseManualStart",
1768 "RefuseManualStop",
1769 "AllowIsolate",
1770 "IgnoreOnIsolate",
1771 "DefaultDependencies"))
89ada3ba
YW
1772 return bus_append_parse_boolean(m, field, eq);
1773
c57d2a76
ZJS
1774 if (STR_IN_SET(field, "JobTimeoutSec",
1775 "JobRunningTimeoutSec",
1776 "StartLimitIntervalSec"))
3d63c749
YW
1777 return bus_append_parse_sec_rename(m, field, eq);
1778
1779 if (streq(field, "StartLimitBurst"))
3d63c749
YW
1780 return bus_append_safe_atou(m, field, eq);
1781
c57d2a76
ZJS
1782 if (STR_IN_SET(field, "SuccessActionExitStatus",
1783 "FailureActionExitStatus")) {
7af67e9a
LP
1784 if (isempty(eq))
1785 r = sd_bus_message_append(m, "(sv)", field, "i", -1);
1786 else {
1787 uint8_t u;
1788
1789 r = safe_atou8(eq, &u);
1790 if (r < 0)
1791 return log_error_errno(r, "Failed to parse %s=%s", field, eq);
1792
1793 r = sd_bus_message_append(m, "(sv)", field, "i", (int) u);
1794 }
1795 if (r < 0)
1796 return bus_log_create_error(r);
1797
1798 return 1;
1799 }
1800
3d63c749 1801 if (unit_dependency_from_string(field) >= 0 ||
c57d2a76
ZJS
1802 STR_IN_SET(field, "Documentation",
1803 "RequiresMountsFor"))
4ec85141 1804 return bus_append_strv(m, field, eq, EXTRACT_UNQUOTE);
89ada3ba 1805
3d63c749
YW
1806 t = condition_type_from_string(field);
1807 if (t >= 0)
1808 is_condition = true;
1809 else
1810 t = assert_type_from_string(field);
1811 if (t >= 0) {
1812 if (isempty(eq))
1813 r = sd_bus_message_append(m, "(sv)", is_condition ? "Conditions" : "Asserts", "a(sbbs)", 0);
1814 else {
1815 const char *p = eq;
1816 int trigger, negate;
1817
1818 trigger = *p == '|';
1819 if (trigger)
1820 p++;
1821
1822 negate = *p == '!';
1823 if (negate)
1824 p++;
1825
1826 r = sd_bus_message_append(m, "(sv)", is_condition ? "Conditions" : "Asserts", "a(sbbs)", 1,
1827 field, trigger, negate, p);
1828 }
1829 if (r < 0)
1830 return bus_log_create_error(r);
1831
1832 return 1;
1833 }
1834
89ada3ba
YW
1835 return 0;
1836}
1837
1838int bus_append_unit_property_assignment(sd_bus_message *m, UnitType t, const char *assignment) {
1839 const char *eq, *field;
1840 int r;
1841
1842 assert(m);
1843 assert(assignment);
1844
1845 eq = strchr(assignment, '=');
baaa35ad
ZJS
1846 if (!eq)
1847 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1848 "Not an assignment: %s", assignment);
89ada3ba
YW
1849
1850 field = strndupa(assignment, eq - assignment);
1851 eq++;
1852
1853 switch (t) {
1854 case UNIT_SERVICE:
1855 r = bus_append_cgroup_property(m, field, eq);
1856 if (r != 0)
f6c66be1
LP
1857 return r;
1858
89ada3ba
YW
1859 r = bus_append_execute_property(m, field, eq);
1860 if (r != 0)
1861 return r;
f6c66be1 1862
89ada3ba
YW
1863 r = bus_append_kill_property(m, field, eq);
1864 if (r != 0)
1865 return r;
f6c66be1 1866
89ada3ba
YW
1867 r = bus_append_service_property(m, field, eq);
1868 if (r != 0)
1869 return r;
1870 break;
f6c66be1 1871
89ada3ba
YW
1872 case UNIT_SOCKET:
1873 r = bus_append_cgroup_property(m, field, eq);
1874 if (r != 0)
1875 return r;
f6c66be1 1876
89ada3ba
YW
1877 r = bus_append_execute_property(m, field, eq);
1878 if (r != 0)
1879 return r;
f6c66be1 1880
89ada3ba
YW
1881 r = bus_append_kill_property(m, field, eq);
1882 if (r != 0)
1883 return r;
f6c66be1 1884
89ada3ba
YW
1885 r = bus_append_socket_property(m, field, eq);
1886 if (r != 0)
f6c66be1 1887 return r;
89ada3ba 1888 break;
f6c66be1 1889
89ada3ba
YW
1890 case UNIT_TIMER:
1891 r = bus_append_timer_property(m, field, eq);
1892 if (r != 0)
1893 return r;
1894 break;
f6c66be1 1895
89ada3ba
YW
1896 case UNIT_PATH:
1897 r = bus_append_path_property(m, field, eq);
1898 if (r != 0)
1899 return r;
1900 break;
268833ed 1901
89ada3ba
YW
1902 case UNIT_SLICE:
1903 r = bus_append_cgroup_property(m, field, eq);
1904 if (r != 0)
1905 return r;
1906 break;
268833ed 1907
89ada3ba
YW
1908 case UNIT_SCOPE:
1909 r = bus_append_cgroup_property(m, field, eq);
1910 if (r != 0)
1911 return r;
268833ed 1912
89ada3ba 1913 r = bus_append_kill_property(m, field, eq);
5a70a68f
PW
1914 if (r != 0)
1915 return r;
1916
1917 r = bus_append_scope_property(m, field, eq);
89ada3ba
YW
1918 if (r != 0)
1919 return r;
1920 break;
535e0d19 1921
89ada3ba 1922 case UNIT_MOUNT:
3d63c749
YW
1923 r = bus_append_cgroup_property(m, field, eq);
1924 if (r != 0)
1925 return r;
1926
1927 r = bus_append_execute_property(m, field, eq);
1928 if (r != 0)
1929 return r;
1930
1931 r = bus_append_kill_property(m, field, eq);
1932 if (r != 0)
1933 return r;
1934
1935 r = bus_append_mount_property(m, field, eq);
1936 if (r != 0)
1937 return r;
1938
1939 break;
1940
89ada3ba 1941 case UNIT_AUTOMOUNT:
3d63c749
YW
1942 r = bus_append_automount_property(m, field, eq);
1943 if (r != 0)
1944 return r;
1945
89ada3ba 1946 break;
535e0d19 1947
89ada3ba
YW
1948 case UNIT_TARGET:
1949 case UNIT_DEVICE:
1950 case UNIT_SWAP:
baaa35ad
ZJS
1951 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1952 "Not supported unit type");
535e0d19 1953
89ada3ba 1954 default:
baaa35ad
ZJS
1955 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1956 "Invalid unit type");
20b16441
LP
1957 }
1958
89ada3ba
YW
1959 r = bus_append_unit_property(m, field, eq);
1960 if (r != 0)
1961 return r;
20b16441 1962
baaa35ad
ZJS
1963 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1964 "Unknown assignment: %s", assignment);
20b16441
LP
1965}
1966
89ada3ba 1967int bus_append_unit_property_assignment_many(sd_bus_message *m, UnitType t, char **l) {
8673cf13
LP
1968 char **i;
1969 int r;
1970
1971 assert(m);
1972
1973 STRV_FOREACH(i, l) {
89ada3ba 1974 r = bus_append_unit_property_assignment(m, t, *i);
8673cf13
LP
1975 if (r < 0)
1976 return r;
1977 }
1978
1979 return 0;
1980}
1981
da6053d0 1982int bus_deserialize_and_dump_unit_file_changes(sd_bus_message *m, bool quiet, UnitFileChange **changes, size_t *n_changes) {
20b16441
LP
1983 const char *type, *path, *source;
1984 int r;
1985
acc0269c
CH
1986 /* changes is dereferenced when calling unit_file_dump_changes() later,
1987 * so we have to make sure this is not NULL. */
1988 assert(changes);
1989 assert(n_changes);
1990
20b16441
LP
1991 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sss)");
1992 if (r < 0)
1993 return bus_log_parse_error(r);
1994
1995 while ((r = sd_bus_message_read(m, "(sss)", &type, &path, &source)) > 0) {
1996 /* We expect only "success" changes to be sent over the bus.
1997 Hence, reject anything negative. */
1998 UnitFileChangeType ch = unit_file_change_type_from_string(type);
1999
2000 if (ch < 0) {
2001 log_notice("Manager reported unknown change type \"%s\" for path \"%s\", ignoring.", type, path);
2002 continue;
2003 }
2004
2005 r = unit_file_changes_add(changes, n_changes, ch, path, source);
2006 if (r < 0)
2007 return r;
2008 }
2009 if (r < 0)
2010 return bus_log_parse_error(r);
2011
2012 r = sd_bus_message_exit_container(m);
2013 if (r < 0)
2014 return bus_log_parse_error(r);
2015
35d379b2 2016 unit_file_dump_changes(0, NULL, *changes, *n_changes, quiet);
20b16441
LP
2017 return 0;
2018}
2019
bd062910
ZJS
2020int unit_load_state(sd_bus *bus, const char *name, char **load_state) {
2021 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2022 _cleanup_free_ char *path = NULL;
2023 int r;
2024
2025 path = unit_dbus_path_from_name(name);
2026 if (!path)
2027 return log_oom();
2028
2029 /* This function warns on it's own, because otherwise it'd be awkward to pass
2030 * the dbus error message around. */
2031
2032 r = sd_bus_get_property_string(
2033 bus,
2034 "org.freedesktop.systemd1",
2035 path,
2036 "org.freedesktop.systemd1.Unit",
2037 "LoadState",
2038 &error,
2039 load_state);
2040 if (r < 0)
2041 return log_error_errno(r, "Failed to get load state of %s: %s", name, bus_error_message(&error, r));
2042
2043 return 0;
2044}