1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2016 Lennart Poettering
6 #include "alloc-util.h"
7 #include "bus-internal.h"
8 #include "bus-unit-util.h"
11 #include "cgroup-util.h"
12 #include "condition.h"
13 #include "cpu-set-util.h"
15 #include "errno-list.h"
18 #include "hexdecoct.h"
19 #include "hostname-util.h"
20 #include "in-addr-util.h"
22 #include "locale-util.h"
23 #include "mount-util.h"
25 #include "parse-util.h"
26 #include "path-util.h"
27 #include "process-util.h"
28 #include "rlimit-util.h"
29 #include "securebits-util.h"
30 #include "signal-util.h"
31 #include "socket-protocol-list.h"
32 #include "string-util.h"
33 #include "syslog-util.h"
34 #include "terminal-util.h"
36 #include "user-util.h"
40 int bus_parse_unit_info(sd_bus_message
*message
, UnitInfo
*u
) {
46 return sd_bus_message_read(
61 #define DEFINE_BUS_APPEND_PARSE_PTR(bus_type, cast_type, type, parse_func) \
62 static int bus_append_##parse_func( \
69 r = parse_func(eq, &val); \
71 return log_error_errno(r, "Failed to parse %s=%s: %m", field, eq); \
73 r = sd_bus_message_append(m, "(sv)", field, \
74 bus_type, (cast_type) val); \
76 return bus_log_create_error(r); \
81 #define DEFINE_BUS_APPEND_PARSE(bus_type, parse_func) \
82 static int bus_append_##parse_func( \
90 log_error("Failed to parse %s: %s", field, eq); \
94 r = sd_bus_message_append(m, "(sv)", field, \
95 bus_type, (int32_t) r); \
97 return bus_log_create_error(r); \
102 DEFINE_BUS_APPEND_PARSE("b", parse_boolean
);
103 DEFINE_BUS_APPEND_PARSE("i", ioprio_class_from_string
);
104 DEFINE_BUS_APPEND_PARSE("i", ip_tos_from_string
);
105 DEFINE_BUS_APPEND_PARSE("i", log_facility_unshifted_from_string
);
106 DEFINE_BUS_APPEND_PARSE("i", log_level_from_string
);
107 DEFINE_BUS_APPEND_PARSE("i", parse_errno
);
108 DEFINE_BUS_APPEND_PARSE("i", sched_policy_from_string
);
109 DEFINE_BUS_APPEND_PARSE("i", secure_bits_from_string
);
110 DEFINE_BUS_APPEND_PARSE("i", signal_from_string
);
111 DEFINE_BUS_APPEND_PARSE("i", socket_protocol_from_name
);
112 DEFINE_BUS_APPEND_PARSE_PTR("i", int32_t, int, ioprio_parse_priority
);
113 DEFINE_BUS_APPEND_PARSE_PTR("i", int32_t, int, parse_nice
);
114 DEFINE_BUS_APPEND_PARSE_PTR("i", int32_t, int, safe_atoi
);
115 DEFINE_BUS_APPEND_PARSE_PTR("t", uint64_t, nsec_t
, parse_nsec
);
116 DEFINE_BUS_APPEND_PARSE_PTR("t", uint64_t, uint64_t, cg_blkio_weight_parse
);
117 DEFINE_BUS_APPEND_PARSE_PTR("t", uint64_t, uint64_t, cg_cpu_shares_parse
);
118 DEFINE_BUS_APPEND_PARSE_PTR("t", uint64_t, uint64_t, cg_weight_parse
);
119 DEFINE_BUS_APPEND_PARSE_PTR("t", uint64_t, unsigned long, mount_propagation_flags_from_string
);
120 DEFINE_BUS_APPEND_PARSE_PTR("t", uint64_t, uint64_t, safe_atou64
);
121 DEFINE_BUS_APPEND_PARSE_PTR("u", uint32_t, mode_t
, parse_mode
);
122 DEFINE_BUS_APPEND_PARSE_PTR("u", uint32_t, unsigned, safe_atou
);
123 DEFINE_BUS_APPEND_PARSE_PTR("x", int64_t, int64_t, safe_atoi64
);
125 static inline int bus_append_string(sd_bus_message
*m
, const char *field
, const char *eq
) {
128 r
= sd_bus_message_append(m
, "(sv)", field
, "s", eq
);
130 return bus_log_create_error(r
);
135 static int bus_append_strv(sd_bus_message
*m
, const char *field
, const char *eq
, ExtractFlags flags
) {
139 r
= sd_bus_message_open_container(m
, 'r', "sv");
141 return bus_log_create_error(r
);
143 r
= sd_bus_message_append_basic(m
, 's', field
);
145 return bus_log_create_error(r
);
147 r
= sd_bus_message_open_container(m
, 'v', "as");
149 return bus_log_create_error(r
);
151 r
= sd_bus_message_open_container(m
, 'a', "s");
153 return bus_log_create_error(r
);
156 _cleanup_free_
char *word
= NULL
;
158 r
= extract_first_word(&p
, &word
, NULL
, flags
);
164 return log_error_errno(r
, "Invalid syntax: %s", eq
);
166 r
= sd_bus_message_append_basic(m
, 's', word
);
168 return bus_log_create_error(r
);
171 r
= sd_bus_message_close_container(m
);
173 return bus_log_create_error(r
);
175 r
= sd_bus_message_close_container(m
);
177 return bus_log_create_error(r
);
179 r
= sd_bus_message_close_container(m
);
181 return bus_log_create_error(r
);
186 static int bus_append_byte_array(sd_bus_message
*m
, const char *field
, const void *buf
, size_t n
) {
189 r
= sd_bus_message_open_container(m
, SD_BUS_TYPE_STRUCT
, "sv");
191 return bus_log_create_error(r
);
193 r
= sd_bus_message_append_basic(m
, SD_BUS_TYPE_STRING
, field
);
195 return bus_log_create_error(r
);
197 r
= sd_bus_message_open_container(m
, 'v', "ay");
199 return bus_log_create_error(r
);
201 r
= sd_bus_message_append_array(m
, 'y', buf
, n
);
203 return bus_log_create_error(r
);
205 r
= sd_bus_message_close_container(m
);
207 return bus_log_create_error(r
);
209 r
= sd_bus_message_close_container(m
);
211 return bus_log_create_error(r
);
216 static int bus_append_parse_sec_rename(sd_bus_message
*m
, const char *field
, const char *eq
) {
222 r
= parse_sec(eq
, &t
);
224 return log_error_errno(r
, "Failed to parse %s=%s: %m", field
, eq
);
227 n
= newa(char, l
+ 2);
228 /* Change suffix Sec → USec */
229 strcpy(mempcpy(n
, field
, l
- 3), "USec");
231 r
= sd_bus_message_append(m
, "(sv)", n
, "t", t
);
233 return bus_log_create_error(r
);
238 static int bus_append_parse_size(sd_bus_message
*m
, const char *field
, const char *eq
, uint64_t base
) {
242 r
= parse_size(eq
, base
, &v
);
244 return log_error_errno(r
, "Failed to parse %s=%s: %m", field
, eq
);
246 r
= sd_bus_message_append(m
, "(sv)", field
, "t", v
);
248 return bus_log_create_error(r
);
253 static int bus_append_exec_command(sd_bus_message
*m
, const char *field
, const char *eq
) {
254 bool ignore_failure
= false, explicit_path
= false, done
= false;
255 _cleanup_strv_free_
char **l
= NULL
;
256 _cleanup_free_
char *path
= NULL
;
266 ignore_failure
= true;
275 explicit_path
= true;
282 /* The bus API doesn't support +, ! and !! currently, unfortunately. :-( */
283 log_error("Sorry, but +, ! and !! are currently not supported for transient services.");
293 r
= extract_first_word(&eq
, &path
, NULL
, EXTRACT_QUOTES
|EXTRACT_CUNESCAPE
);
295 return log_error_errno(r
, "Failed to parse path: %m");
298 r
= strv_split_extract(&l
, eq
, NULL
, EXTRACT_QUOTES
|EXTRACT_CUNESCAPE
);
300 return log_error_errno(r
, "Failed to parse command line: %m");
302 r
= sd_bus_message_open_container(m
, SD_BUS_TYPE_STRUCT
, "sv");
304 return bus_log_create_error(r
);
306 r
= sd_bus_message_append_basic(m
, SD_BUS_TYPE_STRING
, field
);
308 return bus_log_create_error(r
);
310 r
= sd_bus_message_open_container(m
, 'v', "a(sasb)");
312 return bus_log_create_error(r
);
314 r
= sd_bus_message_open_container(m
, 'a', "(sasb)");
316 return bus_log_create_error(r
);
318 if (!strv_isempty(l
)) {
320 r
= sd_bus_message_open_container(m
, 'r', "sasb");
322 return bus_log_create_error(r
);
324 r
= sd_bus_message_append(m
, "s", path
?: l
[0]);
326 return bus_log_create_error(r
);
328 r
= sd_bus_message_append_strv(m
, l
);
330 return bus_log_create_error(r
);
332 r
= sd_bus_message_append(m
, "b", ignore_failure
);
334 return bus_log_create_error(r
);
336 r
= sd_bus_message_close_container(m
);
338 return bus_log_create_error(r
);
341 r
= sd_bus_message_close_container(m
);
343 return bus_log_create_error(r
);
345 r
= sd_bus_message_close_container(m
);
347 return bus_log_create_error(r
);
349 r
= sd_bus_message_close_container(m
);
351 return bus_log_create_error(r
);
356 static int bus_append_ip_address_access(sd_bus_message
*m
, int family
, const union in_addr_union
*prefix
, unsigned char prefixlen
) {
362 r
= sd_bus_message_open_container(m
, 'r', "iayu");
366 r
= sd_bus_message_append(m
, "i", family
);
370 r
= sd_bus_message_append_array(m
, 'y', prefix
, FAMILY_ADDRESS_SIZE(family
));
374 r
= sd_bus_message_append(m
, "u", prefixlen
);
378 return sd_bus_message_close_container(m
);
381 static int bus_append_cgroup_property(sd_bus_message
*m
, const char *field
, const char *eq
) {
384 if (STR_IN_SET(field
, "DevicePolicy", "Slice"))
386 return bus_append_string(m
, field
, eq
);
388 if (STR_IN_SET(field
,
389 "CPUAccounting", "MemoryAccounting", "IOAccounting", "BlockIOAccounting",
390 "TasksAccounting", "IPAccounting"))
392 return bus_append_parse_boolean(m
, field
, eq
);
394 if (STR_IN_SET(field
, "CPUWeight", "StartupCPUWeight", "IOWeight", "StartupIOWeight"))
396 return bus_append_cg_weight_parse(m
, field
, eq
);
398 if (STR_IN_SET(field
, "CPUShares", "StartupCPUShares"))
400 return bus_append_cg_cpu_shares_parse(m
, field
, eq
);
402 if (STR_IN_SET(field
, "BlockIOWeight", "StartupBlockIOWeight"))
404 return bus_append_cg_blkio_weight_parse(m
, field
, eq
);
406 if (streq(field
, "Delegate")) {
408 r
= parse_boolean(eq
);
410 return bus_append_strv(m
, "DelegateControllers", eq
, EXTRACT_QUOTES
);
412 r
= sd_bus_message_append(m
, "(sv)", "Delegate", "b", r
);
414 return bus_log_create_error(r
);
419 if (STR_IN_SET(field
, "MemoryLow", "MemoryHigh", "MemoryMax", "MemorySwapMax", "MemoryLimit", "TasksMax")) {
421 if (isempty(eq
) || streq(eq
, "infinity")) {
422 r
= sd_bus_message_append(m
, "(sv)", field
, "t", CGROUP_LIMIT_MAX
);
424 return bus_log_create_error(r
);
428 r
= parse_percent(eq
);
432 /* When this is a percentage we'll convert this into a relative value in the range
433 * 0…UINT32_MAX and pass it in the MemoryLowScale property (and related
434 * ones). This way the physical memory size can be determined server-side */
436 n
= strjoina(field
, "Scale");
437 r
= sd_bus_message_append(m
, "(sv)", n
, "u", (uint32_t) (((uint64_t) UINT32_MAX
* r
) / 100U));
439 return bus_log_create_error(r
);
444 if (streq(field
, "TasksMax"))
445 return bus_append_safe_atou64(m
, field
, eq
);
447 return bus_append_parse_size(m
, field
, eq
, 1024);
450 if (streq(field
, "CPUQuota")) {
453 r
= sd_bus_message_append(m
, "(sv)", "CPUQuotaPerSecUSec", "t", USEC_INFINITY
);
455 r
= parse_percent_unbounded(eq
);
457 log_error_errno(r
, "CPU quota '%s' invalid.", eq
);
461 r
= sd_bus_message_append(m
, "(sv)", "CPUQuotaPerSecUSec", "t", (usec_t
) r
* USEC_PER_SEC
/ 100U);
465 return bus_log_create_error(r
);
470 if (streq(field
, "DeviceAllow")) {
473 r
= sd_bus_message_append(m
, "(sv)", field
, "a(ss)", 0);
475 const char *path
= eq
, *rwm
= NULL
, *e
;
479 path
= strndupa(eq
, e
- eq
);
483 r
= sd_bus_message_append(m
, "(sv)", field
, "a(ss)", 1, path
, strempty(rwm
));
487 return bus_log_create_error(r
);
492 if (cgroup_io_limit_type_from_string(field
) >= 0 || STR_IN_SET(field
, "BlockIOReadBandwidth", "BlockIOWriteBandwidth")) {
495 r
= sd_bus_message_append(m
, "(sv)", field
, "a(st)", 0);
497 const char *path
, *bandwidth
, *e
;
502 log_error("Failed to parse %s value %s.", field
, eq
);
506 path
= strndupa(eq
, e
- eq
);
509 if (streq(bandwidth
, "infinity")) {
510 bytes
= CGROUP_LIMIT_MAX
;
512 r
= parse_size(bandwidth
, 1000, &bytes
);
514 return log_error_errno(r
, "Failed to parse byte value %s: %m", bandwidth
);
517 r
= sd_bus_message_append(m
, "(sv)", field
, "a(st)", 1, path
, bytes
);
521 return bus_log_create_error(r
);
526 if (STR_IN_SET(field
, "IODeviceWeight", "BlockIODeviceWeight")) {
529 r
= sd_bus_message_append(m
, "(sv)", field
, "a(st)", 0);
531 const char *path
, *weight
, *e
;
536 log_error("Failed to parse %s value %s.", field
, eq
);
540 path
= strndupa(eq
, e
- eq
);
543 r
= safe_atou64(weight
, &u
);
545 return log_error_errno(r
, "Failed to parse %s value %s: %m", field
, weight
);
547 r
= sd_bus_message_append(m
, "(sv)", field
, "a(st)", 1, path
, u
);
551 return bus_log_create_error(r
);
556 if (STR_IN_SET(field
, "IPAddressAllow", "IPAddressDeny")) {
557 unsigned char prefixlen
;
558 union in_addr_union prefix
= {};
562 r
= sd_bus_message_append(m
, "(sv)", field
, "a(iayu)", 0);
564 return bus_log_create_error(r
);
569 r
= sd_bus_message_open_container(m
, SD_BUS_TYPE_STRUCT
, "sv");
571 return bus_log_create_error(r
);
573 r
= sd_bus_message_append_basic(m
, SD_BUS_TYPE_STRING
, field
);
575 return bus_log_create_error(r
);
577 r
= sd_bus_message_open_container(m
, 'v', "a(iayu)");
579 return bus_log_create_error(r
);
581 r
= sd_bus_message_open_container(m
, 'a', "(iayu)");
583 return bus_log_create_error(r
);
585 if (streq(eq
, "any")) {
586 /* "any" is a shortcut for 0.0.0.0/0 and ::/0 */
588 r
= bus_append_ip_address_access(m
, AF_INET
, &prefix
, 0);
590 return bus_log_create_error(r
);
592 r
= bus_append_ip_address_access(m
, AF_INET6
, &prefix
, 0);
594 return bus_log_create_error(r
);
596 } else if (is_localhost(eq
)) {
597 /* "localhost" is a shortcut for 127.0.0.0/8 and ::1/128 */
599 prefix
.in
.s_addr
= htobe32(0x7f000000);
600 r
= bus_append_ip_address_access(m
, AF_INET
, &prefix
, 8);
602 return bus_log_create_error(r
);
604 prefix
.in6
= (struct in6_addr
) IN6ADDR_LOOPBACK_INIT
;
605 r
= bus_append_ip_address_access(m
, AF_INET6
, &prefix
, 128);
609 } else if (streq(eq
, "link-local")) {
610 /* "link-local" is a shortcut for 169.254.0.0/16 and fe80::/64 */
612 prefix
.in
.s_addr
= htobe32((UINT32_C(169) << 24 | UINT32_C(254) << 16));
613 r
= bus_append_ip_address_access(m
, AF_INET
, &prefix
, 16);
615 return bus_log_create_error(r
);
617 prefix
.in6
= (struct in6_addr
) {
618 .s6_addr32
[0] = htobe32(0xfe800000)
620 r
= bus_append_ip_address_access(m
, AF_INET6
, &prefix
, 64);
622 return bus_log_create_error(r
);
624 } else if (streq(eq
, "multicast")) {
625 /* "multicast" is a shortcut for 224.0.0.0/4 and ff00::/8 */
627 prefix
.in
.s_addr
= htobe32((UINT32_C(224) << 24));
628 r
= bus_append_ip_address_access(m
, AF_INET
, &prefix
, 4);
630 return bus_log_create_error(r
);
632 prefix
.in6
= (struct in6_addr
) {
633 .s6_addr32
[0] = htobe32(0xff000000)
635 r
= bus_append_ip_address_access(m
, AF_INET6
, &prefix
, 8);
637 return bus_log_create_error(r
);
640 r
= in_addr_prefix_from_string_auto(eq
, &family
, &prefix
, &prefixlen
);
642 return log_error_errno(r
, "Failed to parse IP address prefix: %s", eq
);
644 r
= bus_append_ip_address_access(m
, family
, &prefix
, prefixlen
);
646 return bus_log_create_error(r
);
649 r
= sd_bus_message_close_container(m
);
651 return bus_log_create_error(r
);
653 r
= sd_bus_message_close_container(m
);
655 return bus_log_create_error(r
);
657 r
= sd_bus_message_close_container(m
);
659 return bus_log_create_error(r
);
667 static int bus_append_automount_property(sd_bus_message
*m
, const char *field
, const char *eq
) {
669 if (streq(field
, "Where"))
671 return bus_append_string(m
, field
, eq
);
673 if (streq(field
, "DirectoryMode"))
675 return bus_append_parse_mode(m
, field
, eq
);
677 if (streq(field
, "TimeoutIdleSec"))
679 return bus_append_parse_sec_rename(m
, field
, eq
);
684 static int bus_append_execute_property(sd_bus_message
*m
, const char *field
, const char *eq
) {
688 if (STR_IN_SET(field
,
690 "UtmpIdentifier", "UtmpMode", "PAMName", "TTYPath",
691 "WorkingDirectory", "RootDirectory", "SyslogIdentifier",
692 "ProtectSystem", "ProtectHome", "SELinuxContext", "RootImage",
693 "RuntimeDirectoryPreserve", "Personality", "KeyringMode"))
695 return bus_append_string(m
, field
, eq
);
697 if (STR_IN_SET(field
,
698 "IgnoreSIGPIPE", "TTYVHangup", "TTYReset", "TTYVTDisallocate",
699 "PrivateTmp", "PrivateDevices", "PrivateNetwork", "PrivateUsers",
700 "PrivateMounts", "NoNewPrivileges", "SyslogLevelPrefix",
701 "MemoryDenyWriteExecute", "RestrictRealtime", "DynamicUser", "RemoveIPC",
702 "ProtectKernelTunables", "ProtectKernelModules", "ProtectControlGroups",
703 "MountAPIVFS", "CPUSchedulingResetOnFork", "LockPersonality"))
705 return bus_append_parse_boolean(m
, field
, eq
);
707 if (STR_IN_SET(field
,
708 "ReadWriteDirectories", "ReadOnlyDirectories", "InaccessibleDirectories",
709 "ReadWritePaths", "ReadOnlyPaths", "InaccessiblePaths",
710 "RuntimeDirectory", "StateDirectory", "CacheDirectory", "LogsDirectory", "ConfigurationDirectory",
711 "SupplementaryGroups", "SystemCallArchitectures"))
713 return bus_append_strv(m
, field
, eq
, EXTRACT_QUOTES
);
715 if (STR_IN_SET(field
, "SyslogLevel", "LogLevelMax"))
717 return bus_append_log_level_from_string(m
, field
, eq
);
719 if (streq(field
, "SyslogFacility"))
721 return bus_append_log_facility_unshifted_from_string(m
, field
, eq
);
723 if (streq(field
, "SecureBits"))
725 return bus_append_secure_bits_from_string(m
, field
, eq
);
727 if (streq(field
, "CPUSchedulingPolicy"))
729 return bus_append_sched_policy_from_string(m
, field
, eq
);
731 if (STR_IN_SET(field
, "CPUSchedulingPriority", "OOMScoreAdjust"))
733 return bus_append_safe_atoi(m
, field
, eq
);
735 if (streq(field
, "Nice"))
737 return bus_append_parse_nice(m
, field
, eq
);
739 if (streq(field
, "SystemCallErrorNumber"))
741 return bus_append_parse_errno(m
, field
, eq
);
743 if (streq(field
, "IOSchedulingClass"))
745 return bus_append_ioprio_class_from_string(m
, field
, eq
);
747 if (streq(field
, "IOSchedulingPriority"))
749 return bus_append_ioprio_parse_priority(m
, field
, eq
);
751 if (STR_IN_SET(field
,
752 "RuntimeDirectoryMode", "StateDirectoryMode", "CacheDirectoryMode",
753 "LogsDirectoryMode", "ConfigurationDirectoryMode", "UMask"))
755 return bus_append_parse_mode(m
, field
, eq
);
757 if (streq(field
, "TimerSlackNSec"))
759 return bus_append_parse_nsec(m
, field
, eq
);
761 if (streq(field
, "MountFlags"))
763 return bus_append_mount_propagation_flags_from_string(m
, field
, eq
);
765 if (STR_IN_SET(field
, "Environment", "UnsetEnvironment", "PassEnvironment"))
767 return bus_append_strv(m
, field
, eq
, EXTRACT_QUOTES
|EXTRACT_CUNESCAPE
);
769 if (streq(field
, "EnvironmentFile")) {
772 r
= sd_bus_message_append(m
, "(sv)", "EnvironmentFiles", "a(sb)", 0);
774 r
= sd_bus_message_append(m
, "(sv)", "EnvironmentFiles", "a(sb)", 1,
775 eq
[0] == '-' ? eq
+ 1 : eq
,
778 return bus_log_create_error(r
);
783 if (streq(field
, "LogExtraFields")) {
785 r
= sd_bus_message_open_container(m
, 'r', "sv");
787 return bus_log_create_error(r
);
789 r
= sd_bus_message_append_basic(m
, 's', "LogExtraFields");
791 return bus_log_create_error(r
);
793 r
= sd_bus_message_open_container(m
, 'v', "aay");
795 return bus_log_create_error(r
);
797 r
= sd_bus_message_open_container(m
, 'a', "ay");
799 return bus_log_create_error(r
);
801 r
= sd_bus_message_append_array(m
, 'y', eq
, strlen(eq
));
803 return bus_log_create_error(r
);
805 r
= sd_bus_message_close_container(m
);
807 return bus_log_create_error(r
);
809 r
= sd_bus_message_close_container(m
);
811 return bus_log_create_error(r
);
813 r
= sd_bus_message_close_container(m
);
815 return bus_log_create_error(r
);
820 if (STR_IN_SET(field
, "StandardInput", "StandardOutput", "StandardError")) {
821 const char *n
, *appended
;
823 if ((n
= startswith(eq
, "fd:"))) {
824 appended
= strjoina(field
, "FileDescriptorName");
825 r
= sd_bus_message_append(m
, "(sv)", appended
, "s", n
);
826 } else if ((n
= startswith(eq
, "file:"))) {
827 appended
= strjoina(field
, "File");
828 r
= sd_bus_message_append(m
, "(sv)", appended
, "s", n
);
830 r
= sd_bus_message_append(m
, "(sv)", field
, "s", eq
);
833 return bus_log_create_error(r
);
838 if (streq(field
, "StandardInputText")) {
839 _cleanup_free_
char *unescaped
= NULL
;
841 r
= cunescape(eq
, 0, &unescaped
);
843 return log_error_errno(r
, "Failed to unescape text '%s': %m", eq
);
845 if (!strextend(&unescaped
, "\n", NULL
))
848 /* Note that we don't expand specifiers here, but that should be OK, as this is a programmatic
849 * interface anyway */
851 return bus_append_byte_array(m
, field
, unescaped
, strlen(unescaped
));
854 if (streq(field
, "StandardInputData")) {
855 _cleanup_free_
void *decoded
= NULL
;
858 r
= unbase64mem(eq
, (size_t) -1, &decoded
, &sz
);
860 return log_error_errno(r
, "Failed to decode base64 data '%s': %m", eq
);
862 return bus_append_byte_array(m
, field
, decoded
, sz
);
865 if ((suffix
= startswith(field
, "Limit"))) {
868 rl
= rlimit_from_string(suffix
);
873 r
= rlimit_parse(rl
, eq
, &l
);
875 return log_error_errno(r
, "Failed to parse resource limit: %s", eq
);
877 r
= sd_bus_message_append(m
, "(sv)", field
, "t", l
.rlim_max
);
879 return bus_log_create_error(r
);
881 sn
= strjoina(field
, "Soft");
882 r
= sd_bus_message_append(m
, "(sv)", sn
, "t", l
.rlim_cur
);
884 return bus_log_create_error(r
);
890 if (STR_IN_SET(field
, "AppArmorProfile", "SmackProcessLabel")) {
899 r
= sd_bus_message_append(m
, "(sv)", field
, "(bs)", ignore
, s
);
901 return bus_log_create_error(r
);
906 if (STR_IN_SET(field
, "CapabilityBoundingSet", "AmbientCapabilities")) {
916 r
= capability_set_from_string(p
, &sum
);
918 return log_error_errno(r
, "Failed to parse %s value %s: %m", field
, eq
);
920 sum
= invert
? ~sum
: sum
;
922 r
= sd_bus_message_append(m
, "(sv)", field
, "t", sum
);
924 return bus_log_create_error(r
);
929 if (streq(field
, "CPUAffinity")) {
930 _cleanup_cpu_free_ cpu_set_t
*cpuset
= NULL
;
932 r
= parse_cpu_set(eq
, &cpuset
);
934 return log_error_errno(r
, "Failed to parse %s value: %s", field
, eq
);
936 return bus_append_byte_array(m
, field
, cpuset
, CPU_ALLOC_SIZE(r
));
939 if (STR_IN_SET(field
, "RestrictAddressFamilies", "SystemCallFilter")) {
948 r
= sd_bus_message_open_container(m
, SD_BUS_TYPE_STRUCT
, "sv");
950 return bus_log_create_error(r
);
952 r
= sd_bus_message_append_basic(m
, SD_BUS_TYPE_STRING
, field
);
954 return bus_log_create_error(r
);
956 r
= sd_bus_message_open_container(m
, 'v', "(bas)");
958 return bus_log_create_error(r
);
960 r
= sd_bus_message_open_container(m
, 'r', "bas");
962 return bus_log_create_error(r
);
964 r
= sd_bus_message_append_basic(m
, 'b', &whitelist
);
966 return bus_log_create_error(r
);
968 r
= sd_bus_message_open_container(m
, 'a', "s");
970 return bus_log_create_error(r
);
973 _cleanup_free_
char *word
= NULL
;
975 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
);
981 return log_error_errno(r
, "Invalid syntax: %s", eq
);
983 r
= sd_bus_message_append_basic(m
, 's', word
);
985 return bus_log_create_error(r
);
988 r
= sd_bus_message_close_container(m
);
990 return bus_log_create_error(r
);
992 r
= sd_bus_message_close_container(m
);
994 return bus_log_create_error(r
);
996 r
= sd_bus_message_close_container(m
);
998 return bus_log_create_error(r
);
1000 r
= sd_bus_message_close_container(m
);
1002 return bus_log_create_error(r
);
1007 if (streq(field
, "RestrictNamespaces")) {
1008 bool invert
= false;
1009 unsigned long flags
;
1011 r
= parse_boolean(eq
);
1015 flags
= NAMESPACE_FLAGS_ALL
;
1022 r
= namespace_flags_from_string(eq
, &flags
);
1024 return log_error_errno(r
, "Failed to parse %s value %s.", field
, eq
);
1028 flags
= (~flags
) & NAMESPACE_FLAGS_ALL
;
1030 r
= sd_bus_message_append(m
, "(sv)", field
, "t", (uint64_t) flags
);
1032 return bus_log_create_error(r
);
1037 if (STR_IN_SET(field
, "BindPaths", "BindReadOnlyPaths")) {
1040 r
= sd_bus_message_open_container(m
, SD_BUS_TYPE_STRUCT
, "sv");
1042 return bus_log_create_error(r
);
1044 r
= sd_bus_message_append_basic(m
, SD_BUS_TYPE_STRING
, field
);
1046 return bus_log_create_error(r
);
1048 r
= sd_bus_message_open_container(m
, 'v', "a(ssbt)");
1050 return bus_log_create_error(r
);
1052 r
= sd_bus_message_open_container(m
, 'a', "(ssbt)");
1054 return bus_log_create_error(r
);
1057 _cleanup_free_
char *source
= NULL
, *destination
= NULL
;
1058 char *s
= NULL
, *d
= NULL
;
1059 bool ignore_enoent
= false;
1060 uint64_t flags
= MS_REC
;
1062 r
= extract_first_word(&p
, &source
, ":" WHITESPACE
, EXTRACT_QUOTES
|EXTRACT_DONT_COALESCE_SEPARATORS
);
1064 return log_error_errno(r
, "Failed to parse argument: %m");
1070 ignore_enoent
= true;
1074 if (p
&& p
[-1] == ':') {
1075 r
= extract_first_word(&p
, &destination
, ":" WHITESPACE
, EXTRACT_QUOTES
|EXTRACT_DONT_COALESCE_SEPARATORS
);
1077 return log_error_errno(r
, "Failed to parse argument: %m");
1079 log_error("Missing argument after ':': %s", eq
);
1085 if (p
&& p
[-1] == ':') {
1086 _cleanup_free_
char *options
= NULL
;
1088 r
= extract_first_word(&p
, &options
, NULL
, EXTRACT_QUOTES
);
1090 return log_error_errno(r
, "Failed to parse argument: %m");
1092 if (isempty(options
) || streq(options
, "rbind"))
1094 else if (streq(options
, "norbind"))
1097 log_error("Unknown options: %s", eq
);
1104 r
= sd_bus_message_append(m
, "(ssbt)", s
, d
, ignore_enoent
, flags
);
1106 return bus_log_create_error(r
);
1109 r
= sd_bus_message_close_container(m
);
1111 return bus_log_create_error(r
);
1113 r
= sd_bus_message_close_container(m
);
1115 return bus_log_create_error(r
);
1117 r
= sd_bus_message_close_container(m
);
1119 return bus_log_create_error(r
);
1124 if (streq(field
, "TemporaryFileSystem")) {
1127 r
= sd_bus_message_open_container(m
, SD_BUS_TYPE_STRUCT
, "sv");
1129 return bus_log_create_error(r
);
1131 r
= sd_bus_message_append_basic(m
, SD_BUS_TYPE_STRING
, field
);
1133 return bus_log_create_error(r
);
1135 r
= sd_bus_message_open_container(m
, 'v', "a(ss)");
1137 return bus_log_create_error(r
);
1139 r
= sd_bus_message_open_container(m
, 'a', "(ss)");
1141 return bus_log_create_error(r
);
1144 _cleanup_free_
char *word
= NULL
, *path
= NULL
;
1147 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
);
1149 return log_error_errno(r
, "Failed to parse argument: %m");
1154 r
= extract_first_word(&w
, &path
, ":", EXTRACT_DONT_COALESCE_SEPARATORS
);
1156 return log_error_errno(r
, "Failed to parse argument: %m");
1158 log_error("Failed to parse argument: %s", p
);
1162 r
= sd_bus_message_append(m
, "(ss)", path
, w
);
1164 return bus_log_create_error(r
);
1167 r
= sd_bus_message_close_container(m
);
1169 return bus_log_create_error(r
);
1171 r
= sd_bus_message_close_container(m
);
1173 return bus_log_create_error(r
);
1175 r
= sd_bus_message_close_container(m
);
1177 return bus_log_create_error(r
);
1185 static int bus_append_kill_property(sd_bus_message
*m
, const char *field
, const char *eq
) {
1187 if (streq(field
, "KillMode"))
1189 return bus_append_string(m
, field
, eq
);
1191 if (STR_IN_SET(field
, "SendSIGHUP", "SendSIGKILL"))
1193 return bus_append_parse_boolean(m
, field
, eq
);
1195 if (streq(field
, "KillSignal"))
1197 return bus_append_signal_from_string(m
, field
, eq
);
1202 static int bus_append_mount_property(sd_bus_message
*m
, const char *field
, const char *eq
) {
1204 if (STR_IN_SET(field
, "What", "Where", "Options", "Type"))
1206 return bus_append_string(m
, field
, eq
);
1208 if (streq(field
, "TimeoutSec"))
1210 return bus_append_parse_sec_rename(m
, field
, eq
);
1212 if (streq(field
, "DirectoryMode"))
1214 return bus_append_parse_mode(m
, field
, eq
);
1216 if (STR_IN_SET(field
, "SloppyOptions", "LazyUnmount", "ForceUnmount"))
1218 return bus_append_parse_boolean(m
, field
, eq
);
1223 static int bus_append_path_property(sd_bus_message
*m
, const char *field
, const char *eq
) {
1226 if (streq(field
, "MakeDirectory"))
1228 return bus_append_parse_boolean(m
, field
, eq
);
1230 if (streq(field
, "DirectoryMode"))
1232 return bus_append_parse_mode(m
, field
, eq
);
1234 if (STR_IN_SET(field
,
1235 "PathExists", "PathExistsGlob", "PathChanged",
1236 "PathModified", "DirectoryNotEmpty")) {
1239 r
= sd_bus_message_append(m
, "(sv)", "Paths", "a(ss)", 0);
1241 r
= sd_bus_message_append(m
, "(sv)", "Paths", "a(ss)", 1, field
, eq
);
1243 return bus_log_create_error(r
);
1251 static int bus_append_service_property(sd_bus_message
*m
, const char *field
, const char *eq
) {
1254 if (STR_IN_SET(field
,
1255 "PIDFile", "Type", "Restart", "BusName", "NotifyAccess",
1256 "USBFunctionDescriptors", "USBFunctionStrings"))
1258 return bus_append_string(m
, field
, eq
);
1260 if (STR_IN_SET(field
, "PermissionsStartOnly", "RootDirectoryStartOnly", "RemainAfterExit", "GuessMainPID"))
1262 return bus_append_parse_boolean(m
, field
, eq
);
1264 if (STR_IN_SET(field
, "RestartSec", "TimeoutStartSec", "TimeoutStopSec", "RuntimeMaxSec", "WatchdogSec"))
1266 return bus_append_parse_sec_rename(m
, field
, eq
);
1268 if (streq(field
, "TimeoutSec")) {
1270 r
= bus_append_parse_sec_rename(m
, "TimeoutStartSec", eq
);
1274 return bus_append_parse_sec_rename(m
, "TimeoutStopSec", eq
);
1277 if (streq(field
, "FileDescriptorStoreMax"))
1279 return bus_append_safe_atou(m
, field
, eq
);
1281 if (STR_IN_SET(field
,
1282 "ExecStartPre", "ExecStart", "ExecStartPost",
1283 "ExecReload", "ExecStop", "ExecStopPost"))
1285 return bus_append_exec_command(m
, field
, eq
);
1287 if (STR_IN_SET(field
, "RestartPreventExitStatus", "RestartForceExitStatus", "SuccessExitStatus")) {
1288 _cleanup_free_
int *status
= NULL
, *signal
= NULL
;
1289 size_t sz_status
= 0, sz_signal
= 0;
1293 _cleanup_free_
char *word
= NULL
;
1296 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
);
1302 return log_error_errno(r
, "Invalid syntax in %s: %s", field
, eq
);
1304 r
= safe_atoi(word
, &val
);
1306 val
= signal_from_string(word
);
1308 return log_error_errno(r
, "Invalid status or signal %s in %s: %m", word
, field
);
1310 signal
= reallocarray(signal
, sz_signal
+ 1, sizeof(int));
1314 signal
[sz_signal
++] = val
;
1316 status
= reallocarray(status
, sz_status
+ 1, sizeof(int));
1320 status
[sz_status
++] = val
;
1324 r
= sd_bus_message_open_container(m
, SD_BUS_TYPE_STRUCT
, "sv");
1326 return bus_log_create_error(r
);
1328 r
= sd_bus_message_append_basic(m
, SD_BUS_TYPE_STRING
, field
);
1330 return bus_log_create_error(r
);
1332 r
= sd_bus_message_open_container(m
, 'v', "(aiai)");
1334 return bus_log_create_error(r
);
1336 r
= sd_bus_message_open_container(m
, 'r', "aiai");
1338 return bus_log_create_error(r
);
1340 r
= sd_bus_message_append_array(m
, 'i', status
, sz_status
);
1342 return bus_log_create_error(r
);
1344 r
= sd_bus_message_append_array(m
, 'i', signal
, sz_signal
);
1346 return bus_log_create_error(r
);
1348 r
= sd_bus_message_close_container(m
);
1350 return bus_log_create_error(r
);
1352 r
= sd_bus_message_close_container(m
);
1354 return bus_log_create_error(r
);
1356 r
= sd_bus_message_close_container(m
);
1358 return bus_log_create_error(r
);
1366 static int bus_append_socket_property(sd_bus_message
*m
, const char *field
, const char *eq
) {
1369 if (STR_IN_SET(field
,
1370 "Accept", "Writable", "KeepAlive", "NoDelay", "FreeBind", "Transparent", "Broadcast",
1371 "PassCredentials", "PassSecurity", "ReusePort", "RemoveOnStop", "SELinuxContextFromNet"))
1373 return bus_append_parse_boolean(m
, field
, eq
);
1375 if (STR_IN_SET(field
, "Priority", "IPTTL", "Mark"))
1377 return bus_append_safe_atoi(m
, field
, eq
);
1379 if (streq(field
, "IPTOS"))
1381 return bus_append_ip_tos_from_string(m
, field
, eq
);
1383 if (STR_IN_SET(field
, "Backlog", "MaxConnections", "MaxConnectionsPerSource", "KeepAliveProbes", "TriggerLimitBurst"))
1385 return bus_append_safe_atou(m
, field
, eq
);
1387 if (STR_IN_SET(field
, "SocketMode", "DirectoryMode"))
1389 return bus_append_parse_mode(m
, field
, eq
);
1391 if (STR_IN_SET(field
, "MessageQueueMaxMessages", "MessageQueueMessageSize"))
1393 return bus_append_safe_atoi64(m
, field
, eq
);
1395 if (STR_IN_SET(field
, "TimeoutSec", "KeepAliveTimeSec", "KeepAliveIntervalSec", "DeferAcceptSec", "TriggerLimitIntervalSec"))
1397 return bus_append_parse_sec_rename(m
, field
, eq
);
1399 if (STR_IN_SET(field
, "ReceiveBuffer", "SendBuffer", "PipeSize"))
1401 return bus_append_parse_size(m
, field
, eq
, 1024);
1403 if (STR_IN_SET(field
, "ExecStartPre", "ExecStartPost", "ExecReload", "ExecStopPost"))
1405 return bus_append_exec_command(m
, field
, eq
);
1407 if (STR_IN_SET(field
,
1408 "SmackLabel", "SmackLabelIPIn", "SmackLabelIPOut", "TCPCongestion",
1409 "BindToDevice", "BindIPv6Only", "FileDescriptorName",
1410 "SocketUser", "SocketGroup"))
1412 return bus_append_string(m
, field
, eq
);
1414 if (streq(field
, "Symlinks"))
1416 return bus_append_strv(m
, field
, eq
, EXTRACT_QUOTES
);
1418 if (streq(field
, "SocketProtocol"))
1420 return bus_append_socket_protocol_from_name(m
, field
, eq
);
1422 if (STR_IN_SET(field
,
1423 "ListenStream", "ListenDatagram", "ListenSequentialPacket", "ListenNetlink",
1424 "ListenSpecial", "ListenMessageQueue", "ListenFIFO", "ListenUSBFunction")) {
1427 r
= sd_bus_message_append(m
, "(sv)", "Listen", "a(ss)", 0);
1429 r
= sd_bus_message_append(m
, "(sv)", "Listen", "a(ss)", 1, field
+ STRLEN("Listen"), eq
);
1431 return bus_log_create_error(r
);
1438 static int bus_append_timer_property(sd_bus_message
*m
, const char *field
, const char *eq
) {
1441 if (STR_IN_SET(field
, "WakeSystem", "RemainAfterElapse", "Persistent"))
1443 return bus_append_parse_boolean(m
, field
, eq
);
1445 if (STR_IN_SET(field
, "AccuracySec", "RandomizedDelaySec"))
1447 return bus_append_parse_sec_rename(m
, field
, eq
);
1449 if (STR_IN_SET(field
,
1450 "OnActiveSec", "OnBootSec", "OnStartupSec",
1451 "OnUnitActiveSec","OnUnitInactiveSec")) {
1454 r
= sd_bus_message_append(m
, "(sv)", "TimersMonotonic", "a(st)", 0);
1457 r
= parse_sec(eq
, &t
);
1459 return log_error_errno(r
, "Failed to parse %s=%s: %m", field
, eq
);
1461 r
= sd_bus_message_append(m
, "(sv)", "TimersMonotonic", "a(st)", 1, field
, t
);
1464 return bus_log_create_error(r
);
1469 if (streq(field
, "OnCalendar")) {
1472 r
= sd_bus_message_append(m
, "(sv)", "TimersCalendar", "a(ss)", 0);
1474 r
= sd_bus_message_append(m
, "(sv)", "TimersCalendar", "a(ss)", 1, field
, eq
);
1476 return bus_log_create_error(r
);
1484 static int bus_append_unit_property(sd_bus_message
*m
, const char *field
, const char *eq
) {
1485 ConditionType t
= _CONDITION_TYPE_INVALID
;
1486 bool is_condition
= false;
1489 if (STR_IN_SET(field
,
1490 "Description", "SourcePath", "OnFailureJobMode",
1491 "JobTimeoutAction", "JobTimeoutRebootArgument",
1492 "StartLimitAction", "FailureAction", "SuccessAction",
1493 "RebootArgument", "CollectMode"))
1495 return bus_append_string(m
, field
, eq
);
1497 if (STR_IN_SET(field
,
1498 "StopWhenUnneeded", "RefuseManualStart", "RefuseManualStop",
1499 "AllowIsolate", "IgnoreOnIsolate", "DefaultDependencies"))
1501 return bus_append_parse_boolean(m
, field
, eq
);
1503 if (STR_IN_SET(field
, "JobTimeoutSec", "JobRunningTimeoutSec", "StartLimitIntervalSec"))
1505 return bus_append_parse_sec_rename(m
, field
, eq
);
1507 if (streq(field
, "StartLimitBurst"))
1509 return bus_append_safe_atou(m
, field
, eq
);
1511 if (unit_dependency_from_string(field
) >= 0 ||
1512 STR_IN_SET(field
, "Documentation", "RequiresMountsFor"))
1514 return bus_append_strv(m
, field
, eq
, EXTRACT_QUOTES
);
1516 t
= condition_type_from_string(field
);
1518 is_condition
= true;
1520 t
= assert_type_from_string(field
);
1523 r
= sd_bus_message_append(m
, "(sv)", is_condition
? "Conditions" : "Asserts", "a(sbbs)", 0);
1526 int trigger
, negate
;
1528 trigger
= *p
== '|';
1536 r
= sd_bus_message_append(m
, "(sv)", is_condition
? "Conditions" : "Asserts", "a(sbbs)", 1,
1537 field
, trigger
, negate
, p
);
1540 return bus_log_create_error(r
);
1548 int bus_append_unit_property_assignment(sd_bus_message
*m
, UnitType t
, const char *assignment
) {
1549 const char *eq
, *field
;
1555 eq
= strchr(assignment
, '=');
1557 log_error("Not an assignment: %s", assignment
);
1561 field
= strndupa(assignment
, eq
- assignment
);
1566 r
= bus_append_cgroup_property(m
, field
, eq
);
1570 r
= bus_append_execute_property(m
, field
, eq
);
1574 r
= bus_append_kill_property(m
, field
, eq
);
1578 r
= bus_append_service_property(m
, field
, eq
);
1584 r
= bus_append_cgroup_property(m
, field
, eq
);
1588 r
= bus_append_execute_property(m
, field
, eq
);
1592 r
= bus_append_kill_property(m
, field
, eq
);
1596 r
= bus_append_socket_property(m
, field
, eq
);
1602 r
= bus_append_timer_property(m
, field
, eq
);
1608 r
= bus_append_path_property(m
, field
, eq
);
1614 r
= bus_append_cgroup_property(m
, field
, eq
);
1621 if (streq(field
, "TimeoutStopSec"))
1622 return bus_append_parse_sec_rename(m
, field
, eq
);
1624 r
= bus_append_cgroup_property(m
, field
, eq
);
1628 r
= bus_append_kill_property(m
, field
, eq
);
1634 r
= bus_append_cgroup_property(m
, field
, eq
);
1638 r
= bus_append_execute_property(m
, field
, eq
);
1642 r
= bus_append_kill_property(m
, field
, eq
);
1646 r
= bus_append_mount_property(m
, field
, eq
);
1652 case UNIT_AUTOMOUNT
:
1653 r
= bus_append_automount_property(m
, field
, eq
);
1662 log_error("Not supported unit type");
1666 log_error("Invalid unit type");
1670 r
= bus_append_unit_property(m
, field
, eq
);
1674 log_error("Unknown assignment: %s", assignment
);
1678 int bus_append_unit_property_assignment_many(sd_bus_message
*m
, UnitType t
, char **l
) {
1684 STRV_FOREACH(i
, l
) {
1685 r
= bus_append_unit_property_assignment(m
, t
, *i
);
1693 typedef struct BusWaitForJobs
{
1700 sd_bus_slot
*slot_job_removed
;
1701 sd_bus_slot
*slot_disconnected
;
1704 static int match_disconnected(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1707 log_error("Warning! D-Bus connection terminated.");
1708 sd_bus_close(sd_bus_message_get_bus(m
));
1713 static int match_job_removed(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1714 const char *path
, *unit
, *result
;
1715 BusWaitForJobs
*d
= userdata
;
1723 r
= sd_bus_message_read(m
, "uoss", &id
, &path
, &unit
, &result
);
1725 bus_log_parse_error(r
);
1729 found
= set_remove(d
->jobs
, (char*) path
);
1735 if (!isempty(result
))
1736 d
->result
= strdup(result
);
1739 d
->name
= strdup(unit
);
1744 void bus_wait_for_jobs_free(BusWaitForJobs
*d
) {
1748 set_free_free(d
->jobs
);
1750 sd_bus_slot_unref(d
->slot_disconnected
);
1751 sd_bus_slot_unref(d
->slot_job_removed
);
1753 sd_bus_unref(d
->bus
);
1761 int bus_wait_for_jobs_new(sd_bus
*bus
, BusWaitForJobs
**ret
) {
1762 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*d
= NULL
;
1768 d
= new0(BusWaitForJobs
, 1);
1772 d
->bus
= sd_bus_ref(bus
);
1774 /* When we are a bus client we match by sender. Direct
1775 * connections OTOH have no initialized sender field, and
1776 * hence we ignore the sender then */
1777 r
= sd_bus_match_signal_async(
1779 &d
->slot_job_removed
,
1780 bus
->bus_client
? "org.freedesktop.systemd1" : NULL
,
1781 "/org/freedesktop/systemd1",
1782 "org.freedesktop.systemd1.Manager",
1784 match_job_removed
, NULL
, d
);
1788 r
= sd_bus_match_signal_async(
1790 &d
->slot_disconnected
,
1791 "org.freedesktop.DBus.Local",
1793 "org.freedesktop.DBus.Local",
1795 match_disconnected
, NULL
, d
);
1804 static int bus_process_wait(sd_bus
*bus
) {
1808 r
= sd_bus_process(bus
, NULL
);
1814 r
= sd_bus_wait(bus
, (uint64_t) -1);
1820 static int bus_job_get_service_result(BusWaitForJobs
*d
, char **result
) {
1821 _cleanup_free_
char *dbus_path
= NULL
;
1827 if (!endswith(d
->name
, ".service"))
1830 dbus_path
= unit_dbus_path_from_name(d
->name
);
1834 return sd_bus_get_property_string(d
->bus
,
1835 "org.freedesktop.systemd1",
1837 "org.freedesktop.systemd1.Service",
1843 static const struct {
1844 const char *result
, *explanation
;
1845 } explanations
[] = {
1846 { "resources", "of unavailable resources or another system error" },
1847 { "protocol", "the service did not take the steps required by its unit configuration" },
1848 { "timeout", "a timeout was exceeded" },
1849 { "exit-code", "the control process exited with error code" },
1850 { "signal", "a fatal signal was delivered to the control process" },
1851 { "core-dump", "a fatal signal was delivered causing the control process to dump core" },
1852 { "watchdog", "the service failed to send watchdog ping" },
1853 { "start-limit", "start of the service was attempted too often" }
1856 static void log_job_error_with_service_result(const char* service
, const char *result
, const char* const* extra_args
) {
1857 _cleanup_free_
char *service_shell_quoted
= NULL
;
1858 const char *systemctl
= "systemctl", *journalctl
= "journalctl";
1862 service_shell_quoted
= shell_maybe_quote(service
, ESCAPE_BACKSLASH
);
1864 if (!strv_isempty((char**) extra_args
)) {
1865 _cleanup_free_
char *t
;
1867 t
= strv_join((char**) extra_args
, " ");
1868 systemctl
= strjoina("systemctl ", t
? : "<args>");
1869 journalctl
= strjoina("journalctl ", t
? : "<args>");
1872 if (!isempty(result
)) {
1875 for (i
= 0; i
< ELEMENTSOF(explanations
); ++i
)
1876 if (streq(result
, explanations
[i
].result
))
1879 if (i
< ELEMENTSOF(explanations
)) {
1880 log_error("Job for %s failed because %s.\n"
1881 "See \"%s status %s\" and \"%s -xe\" for details.\n",
1883 explanations
[i
].explanation
,
1885 service_shell_quoted
?: "<service>",
1891 log_error("Job for %s failed.\n"
1892 "See \"%s status %s\" and \"%s -xe\" for details.\n",
1895 service_shell_quoted
?: "<service>",
1899 /* For some results maybe additional explanation is required */
1900 if (streq_ptr(result
, "start-limit"))
1901 log_info("To force a start use \"%1$s reset-failed %2$s\"\n"
1902 "followed by \"%1$s start %2$s\" again.",
1904 service_shell_quoted
?: "<service>");
1907 static int check_wait_response(BusWaitForJobs
*d
, bool quiet
, const char* const* extra_args
) {
1911 if (streq(d
->result
, "canceled"))
1912 log_error("Job for %s canceled.", strna(d
->name
));
1913 else if (streq(d
->result
, "timeout"))
1914 log_error("Job for %s timed out.", strna(d
->name
));
1915 else if (streq(d
->result
, "dependency"))
1916 log_error("A dependency job for %s failed. See 'journalctl -xe' for details.", strna(d
->name
));
1917 else if (streq(d
->result
, "invalid"))
1918 log_error("%s is not active, cannot reload.", strna(d
->name
));
1919 else if (streq(d
->result
, "assert"))
1920 log_error("Assertion failed on job for %s.", strna(d
->name
));
1921 else if (streq(d
->result
, "unsupported"))
1922 log_error("Operation on or unit type of %s not supported on this system.", strna(d
->name
));
1923 else if (streq(d
->result
, "collected"))
1924 log_error("Queued job for %s was garbage collected.", strna(d
->name
));
1925 else if (streq(d
->result
, "once"))
1926 log_error("Unit %s was started already once and can't be started again.", strna(d
->name
));
1927 else if (!STR_IN_SET(d
->result
, "done", "skipped")) {
1929 _cleanup_free_
char *result
= NULL
;
1932 q
= bus_job_get_service_result(d
, &result
);
1934 log_debug_errno(q
, "Failed to get Result property of unit %s: %m", d
->name
);
1936 log_job_error_with_service_result(d
->name
, result
, extra_args
);
1938 log_error("Job failed. See \"journalctl -xe\" for details.");
1942 if (STR_IN_SET(d
->result
, "canceled", "collected"))
1944 else if (streq(d
->result
, "timeout"))
1946 else if (streq(d
->result
, "dependency"))
1948 else if (streq(d
->result
, "invalid"))
1950 else if (streq(d
->result
, "assert"))
1952 else if (streq(d
->result
, "unsupported"))
1954 else if (streq(d
->result
, "once"))
1956 else if (STR_IN_SET(d
->result
, "done", "skipped"))
1959 log_debug("Unexpected job result, assuming server side newer than us: %s", d
->result
);
1963 int bus_wait_for_jobs(BusWaitForJobs
*d
, bool quiet
, const char* const* extra_args
) {
1968 while (!set_isempty(d
->jobs
)) {
1971 q
= bus_process_wait(d
->bus
);
1973 return log_error_errno(q
, "Failed to wait for response: %m");
1976 q
= check_wait_response(d
, quiet
, extra_args
);
1977 /* Return the first error as it is most likely to be
1979 if (q
< 0 && r
== 0)
1982 log_debug_errno(q
, "Got result %s/%m for job %s", strna(d
->result
), strna(d
->name
));
1985 d
->name
= mfree(d
->name
);
1986 d
->result
= mfree(d
->result
);
1992 int bus_wait_for_jobs_add(BusWaitForJobs
*d
, const char *path
) {
1997 r
= set_ensure_allocated(&d
->jobs
, &string_hash_ops
);
2001 return set_put_strdup(d
->jobs
, path
);
2004 int bus_wait_for_jobs_one(BusWaitForJobs
*d
, const char *path
, bool quiet
) {
2007 r
= bus_wait_for_jobs_add(d
, path
);
2011 return bus_wait_for_jobs(d
, quiet
, NULL
);
2014 int bus_deserialize_and_dump_unit_file_changes(sd_bus_message
*m
, bool quiet
, UnitFileChange
**changes
, size_t *n_changes
) {
2015 const char *type
, *path
, *source
;
2018 /* changes is dereferenced when calling unit_file_dump_changes() later,
2019 * so we have to make sure this is not NULL. */
2023 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_ARRAY
, "(sss)");
2025 return bus_log_parse_error(r
);
2027 while ((r
= sd_bus_message_read(m
, "(sss)", &type
, &path
, &source
)) > 0) {
2028 /* We expect only "success" changes to be sent over the bus.
2029 Hence, reject anything negative. */
2030 UnitFileChangeType ch
= unit_file_change_type_from_string(type
);
2033 log_notice("Manager reported unknown change type \"%s\" for path \"%s\", ignoring.", type
, path
);
2037 r
= unit_file_changes_add(changes
, n_changes
, ch
, path
, source
);
2042 return bus_log_parse_error(r
);
2044 r
= sd_bus_message_exit_container(m
);
2046 return bus_log_parse_error(r
);
2048 unit_file_dump_changes(0, NULL
, *changes
, *n_changes
, quiet
);
2054 bool is_const
; /* If false, cgroup_path should be free()'d */
2056 Hashmap
*pids
; /* PID → process name */
2059 struct CGroupInfo
*parent
;
2060 LIST_FIELDS(struct CGroupInfo
, siblings
);
2061 LIST_HEAD(struct CGroupInfo
, children
);
2065 static bool IS_ROOT(const char *p
) {
2066 return isempty(p
) || streq(p
, "/");
2069 static int add_cgroup(Hashmap
*cgroups
, const char *path
, bool is_const
, struct CGroupInfo
**ret
) {
2070 struct CGroupInfo
*parent
= NULL
, *cg
;
2079 cg
= hashmap_get(cgroups
, path
);
2085 if (!IS_ROOT(path
)) {
2088 e
= strrchr(path
, '/');
2092 pp
= strndupa(path
, e
- path
);
2096 r
= add_cgroup(cgroups
, pp
, false, &parent
);
2101 cg
= new0(struct CGroupInfo
, 1);
2106 cg
->cgroup_path
= (char*) path
;
2108 cg
->cgroup_path
= strdup(path
);
2109 if (!cg
->cgroup_path
) {
2115 cg
->is_const
= is_const
;
2116 cg
->parent
= parent
;
2118 r
= hashmap_put(cgroups
, cg
->cgroup_path
, cg
);
2121 free(cg
->cgroup_path
);
2127 LIST_PREPEND(siblings
, parent
->children
, cg
);
2128 parent
->n_children
++;
2135 static int add_process(
2141 struct CGroupInfo
*cg
;
2148 r
= add_cgroup(cgroups
, path
, true, &cg
);
2152 r
= hashmap_ensure_allocated(&cg
->pids
, &trivial_hash_ops
);
2156 return hashmap_put(cg
->pids
, PID_TO_PTR(pid
), (void*) name
);
2159 static void remove_cgroup(Hashmap
*cgroups
, struct CGroupInfo
*cg
) {
2163 while (cg
->children
)
2164 remove_cgroup(cgroups
, cg
->children
);
2166 hashmap_remove(cgroups
, cg
->cgroup_path
);
2169 free(cg
->cgroup_path
);
2171 hashmap_free(cg
->pids
);
2174 LIST_REMOVE(siblings
, cg
->parent
->children
, cg
);
2179 static int cgroup_info_compare_func(const void *a
, const void *b
) {
2180 const struct CGroupInfo
*x
= *(const struct CGroupInfo
* const*) a
, *y
= *(const struct CGroupInfo
* const*) b
;
2185 return strcmp(x
->cgroup_path
, y
->cgroup_path
);
2188 static int dump_processes(
2190 const char *cgroup_path
,
2193 OutputFlags flags
) {
2195 struct CGroupInfo
*cg
;
2200 if (IS_ROOT(cgroup_path
))
2203 cg
= hashmap_get(cgroups
, cgroup_path
);
2207 if (!hashmap_isempty(cg
->pids
)) {
2215 /* Order processes by their PID */
2216 pids
= newa(pid_t
, hashmap_size(cg
->pids
));
2218 HASHMAP_FOREACH_KEY(name
, pidp
, cg
->pids
, j
)
2219 pids
[n
++] = PTR_TO_PID(pidp
);
2221 assert(n
== hashmap_size(cg
->pids
));
2222 qsort_safe(pids
, n
, sizeof(pid_t
), pid_compare_func
);
2224 width
= DECIMAL_STR_WIDTH(pids
[n
-1]);
2226 for (i
= 0; i
< n
; i
++) {
2227 _cleanup_free_
char *e
= NULL
;
2228 const char *special
;
2231 name
= hashmap_get(cg
->pids
, PID_TO_PTR(pids
[i
]));
2234 if (n_columns
!= 0) {
2237 k
= MAX(LESS_BY(n_columns
, 2U + width
+ 1U), 20U);
2239 e
= ellipsize(name
, k
, 100);
2244 more
= i
+1 < n
|| cg
->children
;
2245 special
= special_glyph(more
? TREE_BRANCH
: TREE_RIGHT
);
2247 fprintf(stdout
, "%s%s%*"PID_PRI
" %s\n",
2256 struct CGroupInfo
**children
, *child
;
2259 /* Order subcgroups by their name */
2260 children
= newa(struct CGroupInfo
*, cg
->n_children
);
2261 LIST_FOREACH(siblings
, child
, cg
->children
)
2262 children
[n
++] = child
;
2263 assert(n
== cg
->n_children
);
2264 qsort_safe(children
, n
, sizeof(struct CGroupInfo
*), cgroup_info_compare_func
);
2267 n_columns
= MAX(LESS_BY(n_columns
, 2U), 20U);
2269 for (i
= 0; i
< n
; i
++) {
2270 _cleanup_free_
char *pp
= NULL
;
2271 const char *name
, *special
;
2274 child
= children
[i
];
2276 name
= strrchr(child
->cgroup_path
, '/');
2282 special
= special_glyph(more
? TREE_BRANCH
: TREE_RIGHT
);
2284 fputs(prefix
, stdout
);
2285 fputs(special
, stdout
);
2286 fputs(name
, stdout
);
2287 fputc('\n', stdout
);
2289 special
= special_glyph(more
? TREE_VERTICAL
: TREE_SPACE
);
2291 pp
= strappend(prefix
, special
);
2295 r
= dump_processes(cgroups
, child
->cgroup_path
, pp
, n_columns
, flags
);
2305 static int dump_extra_processes(
2309 OutputFlags flags
) {
2311 _cleanup_free_ pid_t
*pids
= NULL
;
2312 _cleanup_hashmap_free_ Hashmap
*names
= NULL
;
2313 struct CGroupInfo
*cg
;
2314 size_t n_allocated
= 0, n
= 0, k
;
2318 /* Prints the extra processes, i.e. those that are in cgroups we haven't displayed yet. We show them as
2319 * combined, sorted, linear list. */
2321 HASHMAP_FOREACH(cg
, cgroups
, i
) {
2329 if (hashmap_isempty(cg
->pids
))
2332 r
= hashmap_ensure_allocated(&names
, &trivial_hash_ops
);
2336 if (!GREEDY_REALLOC(pids
, n_allocated
, n
+ hashmap_size(cg
->pids
)))
2339 HASHMAP_FOREACH_KEY(name
, pidp
, cg
->pids
, j
) {
2340 pids
[n
++] = PTR_TO_PID(pidp
);
2342 r
= hashmap_put(names
, pidp
, (void*) name
);
2351 qsort_safe(pids
, n
, sizeof(pid_t
), pid_compare_func
);
2352 width
= DECIMAL_STR_WIDTH(pids
[n
-1]);
2354 for (k
= 0; k
< n
; k
++) {
2355 _cleanup_free_
char *e
= NULL
;
2358 name
= hashmap_get(names
, PID_TO_PTR(pids
[k
]));
2361 if (n_columns
!= 0) {
2364 z
= MAX(LESS_BY(n_columns
, 2U + width
+ 1U), 20U);
2366 e
= ellipsize(name
, z
, 100);
2371 fprintf(stdout
, "%s%s %*" PID_PRI
" %s\n",
2373 special_glyph(TRIANGULAR_BULLET
),
2381 int unit_show_processes(
2384 const char *cgroup_path
,
2388 sd_bus_error
*error
) {
2390 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
2391 Hashmap
*cgroups
= NULL
;
2392 struct CGroupInfo
*cg
;
2398 if (flags
& OUTPUT_FULL_WIDTH
)
2400 else if (n_columns
<= 0)
2401 n_columns
= columns();
2403 prefix
= strempty(prefix
);
2405 r
= sd_bus_call_method(
2407 "org.freedesktop.systemd1",
2408 "/org/freedesktop/systemd1",
2409 "org.freedesktop.systemd1.Manager",
2418 cgroups
= hashmap_new(&path_hash_ops
);
2422 r
= sd_bus_message_enter_container(reply
, 'a', "(sus)");
2427 const char *path
= NULL
, *name
= NULL
;
2430 r
= sd_bus_message_read(reply
, "(sus)", &path
, &pid
, &name
);
2436 r
= add_process(cgroups
, path
, pid
, name
);
2441 r
= sd_bus_message_exit_container(reply
);
2445 r
= dump_processes(cgroups
, cgroup_path
, prefix
, n_columns
, flags
);
2449 r
= dump_extra_processes(cgroups
, prefix
, n_columns
, flags
);
2452 while ((cg
= hashmap_first(cgroups
)))
2453 remove_cgroup(cgroups
, cg
);
2455 hashmap_free(cgroups
);
2460 int unit_load_state(sd_bus
*bus
, const char *name
, char **load_state
) {
2461 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2462 _cleanup_free_
char *path
= NULL
;
2465 path
= unit_dbus_path_from_name(name
);
2469 /* This function warns on it's own, because otherwise it'd be awkward to pass
2470 * the dbus error message around. */
2472 r
= sd_bus_get_property_string(
2474 "org.freedesktop.systemd1",
2476 "org.freedesktop.systemd1.Unit",
2481 return log_error_errno(r
, "Failed to get load state of %s: %s", name
, bus_error_message(&error
, r
));