1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include "alloc-util.h"
4 #include "bus-internal.h"
5 #include "bus-unit-util.h"
8 #include "cgroup-util.h"
10 #include "cpu-set-util.h"
12 #include "errno-list.h"
15 #include "hexdecoct.h"
16 #include "hostname-util.h"
17 #include "in-addr-util.h"
19 #include "locale-util.h"
20 #include "mount-util.h"
22 #include "parse-util.h"
23 #include "path-util.h"
24 #include "process-util.h"
25 #include "rlimit-util.h"
26 #include "securebits-util.h"
27 #include "signal-util.h"
28 #include "socket-protocol-list.h"
29 #include "string-util.h"
30 #include "syslog-util.h"
31 #include "terminal-util.h"
33 #include "user-util.h"
37 int bus_parse_unit_info(sd_bus_message
*message
, UnitInfo
*u
) {
43 return sd_bus_message_read(
58 #define DEFINE_BUS_APPEND_PARSE_PTR(bus_type, cast_type, type, parse_func) \
59 static int bus_append_##parse_func( \
66 r = parse_func(eq, &val); \
68 return log_error_errno(r, "Failed to parse %s=%s: %m", field, eq); \
70 r = sd_bus_message_append(m, "(sv)", field, \
71 bus_type, (cast_type) val); \
73 return bus_log_create_error(r); \
78 #define DEFINE_BUS_APPEND_PARSE(bus_type, parse_func) \
79 static int bus_append_##parse_func( \
87 log_error("Failed to parse %s: %s", field, eq); \
91 r = sd_bus_message_append(m, "(sv)", field, \
92 bus_type, (int32_t) r); \
94 return bus_log_create_error(r); \
99 DEFINE_BUS_APPEND_PARSE("b", parse_boolean
);
100 DEFINE_BUS_APPEND_PARSE("i", ioprio_class_from_string
);
101 DEFINE_BUS_APPEND_PARSE("i", ip_tos_from_string
);
102 DEFINE_BUS_APPEND_PARSE("i", log_facility_unshifted_from_string
);
103 DEFINE_BUS_APPEND_PARSE("i", log_level_from_string
);
104 DEFINE_BUS_APPEND_PARSE("i", parse_errno
);
105 DEFINE_BUS_APPEND_PARSE("i", sched_policy_from_string
);
106 DEFINE_BUS_APPEND_PARSE("i", secure_bits_from_string
);
107 DEFINE_BUS_APPEND_PARSE("i", signal_from_string
);
108 DEFINE_BUS_APPEND_PARSE("i", socket_protocol_from_name
);
109 DEFINE_BUS_APPEND_PARSE_PTR("i", int32_t, int, ioprio_parse_priority
);
110 DEFINE_BUS_APPEND_PARSE_PTR("i", int32_t, int, parse_nice
);
111 DEFINE_BUS_APPEND_PARSE_PTR("i", int32_t, int, safe_atoi
);
112 DEFINE_BUS_APPEND_PARSE_PTR("t", uint64_t, nsec_t
, parse_nsec
);
113 DEFINE_BUS_APPEND_PARSE_PTR("t", uint64_t, uint64_t, cg_blkio_weight_parse
);
114 DEFINE_BUS_APPEND_PARSE_PTR("t", uint64_t, uint64_t, cg_cpu_shares_parse
);
115 DEFINE_BUS_APPEND_PARSE_PTR("t", uint64_t, uint64_t, cg_weight_parse
);
116 DEFINE_BUS_APPEND_PARSE_PTR("t", uint64_t, unsigned long, mount_propagation_flags_from_string
);
117 DEFINE_BUS_APPEND_PARSE_PTR("t", uint64_t, uint64_t, safe_atou64
);
118 DEFINE_BUS_APPEND_PARSE_PTR("u", uint32_t, mode_t
, parse_mode
);
119 DEFINE_BUS_APPEND_PARSE_PTR("u", uint32_t, unsigned, safe_atou
);
120 DEFINE_BUS_APPEND_PARSE_PTR("x", int64_t, int64_t, safe_atoi64
);
122 static inline int bus_append_string(sd_bus_message
*m
, const char *field
, const char *eq
) {
125 r
= sd_bus_message_append(m
, "(sv)", field
, "s", eq
);
127 return bus_log_create_error(r
);
132 static int bus_append_strv(sd_bus_message
*m
, const char *field
, const char *eq
, ExtractFlags flags
) {
136 r
= sd_bus_message_open_container(m
, 'r', "sv");
138 return bus_log_create_error(r
);
140 r
= sd_bus_message_append_basic(m
, 's', field
);
142 return bus_log_create_error(r
);
144 r
= sd_bus_message_open_container(m
, 'v', "as");
146 return bus_log_create_error(r
);
148 r
= sd_bus_message_open_container(m
, 'a', "s");
150 return bus_log_create_error(r
);
153 _cleanup_free_
char *word
= NULL
;
155 r
= extract_first_word(&p
, &word
, NULL
, flags
);
161 return log_error_errno(r
, "Invalid syntax: %s", eq
);
163 r
= sd_bus_message_append_basic(m
, 's', word
);
165 return bus_log_create_error(r
);
168 r
= sd_bus_message_close_container(m
);
170 return bus_log_create_error(r
);
172 r
= sd_bus_message_close_container(m
);
174 return bus_log_create_error(r
);
176 r
= sd_bus_message_close_container(m
);
178 return bus_log_create_error(r
);
183 static int bus_append_byte_array(sd_bus_message
*m
, const char *field
, const void *buf
, size_t n
) {
186 r
= sd_bus_message_open_container(m
, SD_BUS_TYPE_STRUCT
, "sv");
188 return bus_log_create_error(r
);
190 r
= sd_bus_message_append_basic(m
, SD_BUS_TYPE_STRING
, field
);
192 return bus_log_create_error(r
);
194 r
= sd_bus_message_open_container(m
, 'v', "ay");
196 return bus_log_create_error(r
);
198 r
= sd_bus_message_append_array(m
, 'y', buf
, n
);
200 return bus_log_create_error(r
);
202 r
= sd_bus_message_close_container(m
);
204 return bus_log_create_error(r
);
206 r
= sd_bus_message_close_container(m
);
208 return bus_log_create_error(r
);
213 static int bus_append_parse_sec_rename(sd_bus_message
*m
, const char *field
, const char *eq
) {
219 r
= parse_sec(eq
, &t
);
221 return log_error_errno(r
, "Failed to parse %s=%s: %m", field
, eq
);
224 n
= newa(char, l
+ 2);
225 /* Change suffix Sec → USec */
226 strcpy(mempcpy(n
, field
, l
- 3), "USec");
228 r
= sd_bus_message_append(m
, "(sv)", n
, "t", t
);
230 return bus_log_create_error(r
);
235 static int bus_append_parse_size(sd_bus_message
*m
, const char *field
, const char *eq
, uint64_t base
) {
239 r
= parse_size(eq
, base
, &v
);
241 return log_error_errno(r
, "Failed to parse %s=%s: %m", field
, eq
);
243 r
= sd_bus_message_append(m
, "(sv)", field
, "t", v
);
245 return bus_log_create_error(r
);
250 static int bus_append_exec_command(sd_bus_message
*m
, const char *field
, const char *eq
) {
251 bool ignore_failure
= false, explicit_path
= false, done
= false;
252 _cleanup_strv_free_
char **l
= NULL
;
253 _cleanup_free_
char *path
= NULL
;
263 ignore_failure
= true;
272 explicit_path
= true;
279 /* The bus API doesn't support +, ! and !! currently, unfortunately. :-( */
280 log_error("Sorry, but +, ! and !! are currently not supported for transient services.");
290 r
= extract_first_word(&eq
, &path
, NULL
, EXTRACT_QUOTES
|EXTRACT_CUNESCAPE
);
292 return log_error_errno(r
, "Failed to parse path: %m");
295 r
= strv_split_extract(&l
, eq
, NULL
, EXTRACT_QUOTES
|EXTRACT_CUNESCAPE
);
297 return log_error_errno(r
, "Failed to parse command line: %m");
299 r
= sd_bus_message_open_container(m
, SD_BUS_TYPE_STRUCT
, "sv");
301 return bus_log_create_error(r
);
303 r
= sd_bus_message_append_basic(m
, SD_BUS_TYPE_STRING
, field
);
305 return bus_log_create_error(r
);
307 r
= sd_bus_message_open_container(m
, 'v', "a(sasb)");
309 return bus_log_create_error(r
);
311 r
= sd_bus_message_open_container(m
, 'a', "(sasb)");
313 return bus_log_create_error(r
);
315 if (!strv_isempty(l
)) {
317 r
= sd_bus_message_open_container(m
, 'r', "sasb");
319 return bus_log_create_error(r
);
321 r
= sd_bus_message_append(m
, "s", path
?: l
[0]);
323 return bus_log_create_error(r
);
325 r
= sd_bus_message_append_strv(m
, l
);
327 return bus_log_create_error(r
);
329 r
= sd_bus_message_append(m
, "b", ignore_failure
);
331 return bus_log_create_error(r
);
333 r
= sd_bus_message_close_container(m
);
335 return bus_log_create_error(r
);
338 r
= sd_bus_message_close_container(m
);
340 return bus_log_create_error(r
);
342 r
= sd_bus_message_close_container(m
);
344 return bus_log_create_error(r
);
346 r
= sd_bus_message_close_container(m
);
348 return bus_log_create_error(r
);
353 static int bus_append_ip_address_access(sd_bus_message
*m
, int family
, const union in_addr_union
*prefix
, unsigned char prefixlen
) {
359 r
= sd_bus_message_open_container(m
, 'r', "iayu");
363 r
= sd_bus_message_append(m
, "i", family
);
367 r
= sd_bus_message_append_array(m
, 'y', prefix
, FAMILY_ADDRESS_SIZE(family
));
371 r
= sd_bus_message_append(m
, "u", prefixlen
);
375 return sd_bus_message_close_container(m
);
378 static int bus_append_cgroup_property(sd_bus_message
*m
, const char *field
, const char *eq
) {
381 if (STR_IN_SET(field
, "DevicePolicy", "Slice"))
383 return bus_append_string(m
, field
, eq
);
385 if (STR_IN_SET(field
,
386 "CPUAccounting", "MemoryAccounting", "IOAccounting", "BlockIOAccounting",
387 "TasksAccounting", "IPAccounting"))
389 return bus_append_parse_boolean(m
, field
, eq
);
391 if (STR_IN_SET(field
, "CPUWeight", "StartupCPUWeight", "IOWeight", "StartupIOWeight"))
393 return bus_append_cg_weight_parse(m
, field
, eq
);
395 if (STR_IN_SET(field
, "CPUShares", "StartupCPUShares"))
397 return bus_append_cg_cpu_shares_parse(m
, field
, eq
);
399 if (STR_IN_SET(field
, "BlockIOWeight", "StartupBlockIOWeight"))
401 return bus_append_cg_blkio_weight_parse(m
, field
, eq
);
403 if (streq(field
, "Delegate")) {
405 r
= parse_boolean(eq
);
407 return bus_append_strv(m
, "DelegateControllers", eq
, EXTRACT_QUOTES
);
409 r
= sd_bus_message_append(m
, "(sv)", "Delegate", "b", r
);
411 return bus_log_create_error(r
);
416 if (STR_IN_SET(field
, "MemoryMin", "MemoryLow", "MemoryHigh", "MemoryMax", "MemorySwapMax", "MemoryLimit", "TasksMax")) {
418 if (isempty(eq
) || streq(eq
, "infinity")) {
419 r
= sd_bus_message_append(m
, "(sv)", field
, "t", CGROUP_LIMIT_MAX
);
421 return bus_log_create_error(r
);
425 r
= parse_permille(eq
);
429 /* When this is a percentage we'll convert this into a relative value in the range 0…UINT32_MAX
430 * and pass it in the MemoryLowScale property (and related ones). This way the physical memory
431 * size can be determined server-side. */
433 n
= strjoina(field
, "Scale");
434 r
= sd_bus_message_append(m
, "(sv)", n
, "u", (uint32_t) (((uint64_t) r
* UINT32_MAX
) / 1000U));
436 return bus_log_create_error(r
);
441 if (streq(field
, "TasksMax"))
442 return bus_append_safe_atou64(m
, field
, eq
);
444 return bus_append_parse_size(m
, field
, eq
, 1024);
447 if (streq(field
, "CPUQuota")) {
450 r
= sd_bus_message_append(m
, "(sv)", "CPUQuotaPerSecUSec", "t", USEC_INFINITY
);
452 r
= parse_permille_unbounded(eq
);
454 log_error("CPU quota too small.");
458 return log_error_errno(r
, "CPU quota '%s' invalid.", eq
);
460 r
= sd_bus_message_append(m
, "(sv)", "CPUQuotaPerSecUSec", "t", (((uint64_t) r
* USEC_PER_SEC
) / 1000U));
464 return bus_log_create_error(r
);
469 if (streq(field
, "DeviceAllow")) {
472 r
= sd_bus_message_append(m
, "(sv)", field
, "a(ss)", 0);
474 const char *path
= eq
, *rwm
= NULL
, *e
;
478 path
= strndupa(eq
, e
- eq
);
482 r
= sd_bus_message_append(m
, "(sv)", field
, "a(ss)", 1, path
, strempty(rwm
));
486 return bus_log_create_error(r
);
491 if (cgroup_io_limit_type_from_string(field
) >= 0 || STR_IN_SET(field
, "BlockIOReadBandwidth", "BlockIOWriteBandwidth")) {
494 r
= sd_bus_message_append(m
, "(sv)", field
, "a(st)", 0);
496 const char *path
, *bandwidth
, *e
;
501 log_error("Failed to parse %s value %s.", field
, eq
);
505 path
= strndupa(eq
, e
- eq
);
508 if (streq(bandwidth
, "infinity"))
509 bytes
= CGROUP_LIMIT_MAX
;
511 r
= parse_size(bandwidth
, 1000, &bytes
);
513 return log_error_errno(r
, "Failed to parse byte value %s: %m", bandwidth
);
516 r
= sd_bus_message_append(m
, "(sv)", field
, "a(st)", 1, path
, bytes
);
520 return bus_log_create_error(r
);
525 if (STR_IN_SET(field
, "IODeviceWeight", "BlockIODeviceWeight")) {
528 r
= sd_bus_message_append(m
, "(sv)", field
, "a(st)", 0);
530 const char *path
, *weight
, *e
;
535 log_error("Failed to parse %s value %s.", field
, eq
);
539 path
= strndupa(eq
, e
- eq
);
542 r
= safe_atou64(weight
, &u
);
544 return log_error_errno(r
, "Failed to parse %s value %s: %m", field
, weight
);
546 r
= sd_bus_message_append(m
, "(sv)", field
, "a(st)", 1, path
, u
);
550 return bus_log_create_error(r
);
555 if (streq(field
, "IODeviceLatencyTargetSec")) {
556 const char *field_usec
= "IODeviceLatencyTargetUSec";
559 r
= sd_bus_message_append(m
, "(sv)", field_usec
, "a(st)", USEC_INFINITY
);
561 const char *path
, *target
, *e
;
566 log_error("Failed to parse %s value %s.", field
, eq
);
570 path
= strndupa(eq
, e
- eq
);
573 r
= parse_sec(target
, &usec
);
575 return log_error_errno(r
, "Failed to parse %s value %s: %m", field
, target
);
577 r
= sd_bus_message_append(m
, "(sv)", field_usec
, "a(st)", 1, path
, usec
);
581 return bus_log_create_error(r
);
586 if (STR_IN_SET(field
, "IPAddressAllow", "IPAddressDeny")) {
587 unsigned char prefixlen
;
588 union in_addr_union prefix
= {};
592 r
= sd_bus_message_append(m
, "(sv)", field
, "a(iayu)", 0);
594 return bus_log_create_error(r
);
599 r
= sd_bus_message_open_container(m
, SD_BUS_TYPE_STRUCT
, "sv");
601 return bus_log_create_error(r
);
603 r
= sd_bus_message_append_basic(m
, SD_BUS_TYPE_STRING
, field
);
605 return bus_log_create_error(r
);
607 r
= sd_bus_message_open_container(m
, 'v', "a(iayu)");
609 return bus_log_create_error(r
);
611 r
= sd_bus_message_open_container(m
, 'a', "(iayu)");
613 return bus_log_create_error(r
);
615 if (streq(eq
, "any")) {
616 /* "any" is a shortcut for 0.0.0.0/0 and ::/0 */
618 r
= bus_append_ip_address_access(m
, AF_INET
, &prefix
, 0);
620 return bus_log_create_error(r
);
622 r
= bus_append_ip_address_access(m
, AF_INET6
, &prefix
, 0);
624 return bus_log_create_error(r
);
626 } else if (is_localhost(eq
)) {
627 /* "localhost" is a shortcut for 127.0.0.0/8 and ::1/128 */
629 prefix
.in
.s_addr
= htobe32(0x7f000000);
630 r
= bus_append_ip_address_access(m
, AF_INET
, &prefix
, 8);
632 return bus_log_create_error(r
);
634 prefix
.in6
= (struct in6_addr
) IN6ADDR_LOOPBACK_INIT
;
635 r
= bus_append_ip_address_access(m
, AF_INET6
, &prefix
, 128);
639 } else if (streq(eq
, "link-local")) {
640 /* "link-local" is a shortcut for 169.254.0.0/16 and fe80::/64 */
642 prefix
.in
.s_addr
= htobe32((UINT32_C(169) << 24 | UINT32_C(254) << 16));
643 r
= bus_append_ip_address_access(m
, AF_INET
, &prefix
, 16);
645 return bus_log_create_error(r
);
647 prefix
.in6
= (struct in6_addr
) {
648 .s6_addr32
[0] = htobe32(0xfe800000)
650 r
= bus_append_ip_address_access(m
, AF_INET6
, &prefix
, 64);
652 return bus_log_create_error(r
);
654 } else if (streq(eq
, "multicast")) {
655 /* "multicast" is a shortcut for 224.0.0.0/4 and ff00::/8 */
657 prefix
.in
.s_addr
= htobe32((UINT32_C(224) << 24));
658 r
= bus_append_ip_address_access(m
, AF_INET
, &prefix
, 4);
660 return bus_log_create_error(r
);
662 prefix
.in6
= (struct in6_addr
) {
663 .s6_addr32
[0] = htobe32(0xff000000)
665 r
= bus_append_ip_address_access(m
, AF_INET6
, &prefix
, 8);
667 return bus_log_create_error(r
);
670 r
= in_addr_prefix_from_string_auto(eq
, &family
, &prefix
, &prefixlen
);
672 return log_error_errno(r
, "Failed to parse IP address prefix: %s", eq
);
674 r
= bus_append_ip_address_access(m
, family
, &prefix
, prefixlen
);
676 return bus_log_create_error(r
);
679 r
= sd_bus_message_close_container(m
);
681 return bus_log_create_error(r
);
683 r
= sd_bus_message_close_container(m
);
685 return bus_log_create_error(r
);
687 r
= sd_bus_message_close_container(m
);
689 return bus_log_create_error(r
);
697 static int bus_append_automount_property(sd_bus_message
*m
, const char *field
, const char *eq
) {
699 if (streq(field
, "Where"))
701 return bus_append_string(m
, field
, eq
);
703 if (streq(field
, "DirectoryMode"))
705 return bus_append_parse_mode(m
, field
, eq
);
707 if (streq(field
, "TimeoutIdleSec"))
709 return bus_append_parse_sec_rename(m
, field
, eq
);
714 static int bus_append_execute_property(sd_bus_message
*m
, const char *field
, const char *eq
) {
718 if (STR_IN_SET(field
,
720 "UtmpIdentifier", "UtmpMode", "PAMName", "TTYPath",
721 "WorkingDirectory", "RootDirectory", "SyslogIdentifier",
722 "ProtectSystem", "ProtectHome", "SELinuxContext", "RootImage",
723 "RuntimeDirectoryPreserve", "Personality", "KeyringMode"))
725 return bus_append_string(m
, field
, eq
);
727 if (STR_IN_SET(field
,
728 "IgnoreSIGPIPE", "TTYVHangup", "TTYReset", "TTYVTDisallocate",
729 "PrivateTmp", "PrivateDevices", "PrivateNetwork", "PrivateUsers",
730 "PrivateMounts", "NoNewPrivileges", "SyslogLevelPrefix",
731 "MemoryDenyWriteExecute", "RestrictRealtime", "DynamicUser", "RemoveIPC",
732 "ProtectKernelTunables", "ProtectKernelModules", "ProtectControlGroups",
733 "MountAPIVFS", "CPUSchedulingResetOnFork", "LockPersonality"))
735 return bus_append_parse_boolean(m
, field
, eq
);
737 if (STR_IN_SET(field
,
738 "ReadWriteDirectories", "ReadOnlyDirectories", "InaccessibleDirectories",
739 "ReadWritePaths", "ReadOnlyPaths", "InaccessiblePaths",
740 "RuntimeDirectory", "StateDirectory", "CacheDirectory", "LogsDirectory", "ConfigurationDirectory",
741 "SupplementaryGroups", "SystemCallArchitectures"))
743 return bus_append_strv(m
, field
, eq
, EXTRACT_QUOTES
);
745 if (STR_IN_SET(field
, "SyslogLevel", "LogLevelMax"))
747 return bus_append_log_level_from_string(m
, field
, eq
);
749 if (streq(field
, "SyslogFacility"))
751 return bus_append_log_facility_unshifted_from_string(m
, field
, eq
);
753 if (streq(field
, "SecureBits"))
755 return bus_append_secure_bits_from_string(m
, field
, eq
);
757 if (streq(field
, "CPUSchedulingPolicy"))
759 return bus_append_sched_policy_from_string(m
, field
, eq
);
761 if (STR_IN_SET(field
, "CPUSchedulingPriority", "OOMScoreAdjust"))
763 return bus_append_safe_atoi(m
, field
, eq
);
765 if (streq(field
, "Nice"))
767 return bus_append_parse_nice(m
, field
, eq
);
769 if (streq(field
, "SystemCallErrorNumber"))
771 return bus_append_parse_errno(m
, field
, eq
);
773 if (streq(field
, "IOSchedulingClass"))
775 return bus_append_ioprio_class_from_string(m
, field
, eq
);
777 if (streq(field
, "IOSchedulingPriority"))
779 return bus_append_ioprio_parse_priority(m
, field
, eq
);
781 if (STR_IN_SET(field
,
782 "RuntimeDirectoryMode", "StateDirectoryMode", "CacheDirectoryMode",
783 "LogsDirectoryMode", "ConfigurationDirectoryMode", "UMask"))
785 return bus_append_parse_mode(m
, field
, eq
);
787 if (streq(field
, "TimerSlackNSec"))
789 return bus_append_parse_nsec(m
, field
, eq
);
791 if (streq(field
, "MountFlags"))
793 return bus_append_mount_propagation_flags_from_string(m
, field
, eq
);
795 if (STR_IN_SET(field
, "Environment", "UnsetEnvironment", "PassEnvironment"))
797 return bus_append_strv(m
, field
, eq
, EXTRACT_QUOTES
|EXTRACT_CUNESCAPE
);
799 if (streq(field
, "EnvironmentFile")) {
802 r
= sd_bus_message_append(m
, "(sv)", "EnvironmentFiles", "a(sb)", 0);
804 r
= sd_bus_message_append(m
, "(sv)", "EnvironmentFiles", "a(sb)", 1,
805 eq
[0] == '-' ? eq
+ 1 : eq
,
808 return bus_log_create_error(r
);
813 if (streq(field
, "LogExtraFields")) {
815 r
= sd_bus_message_open_container(m
, 'r', "sv");
817 return bus_log_create_error(r
);
819 r
= sd_bus_message_append_basic(m
, 's', "LogExtraFields");
821 return bus_log_create_error(r
);
823 r
= sd_bus_message_open_container(m
, 'v', "aay");
825 return bus_log_create_error(r
);
827 r
= sd_bus_message_open_container(m
, 'a', "ay");
829 return bus_log_create_error(r
);
831 r
= sd_bus_message_append_array(m
, 'y', eq
, strlen(eq
));
833 return bus_log_create_error(r
);
835 r
= sd_bus_message_close_container(m
);
837 return bus_log_create_error(r
);
839 r
= sd_bus_message_close_container(m
);
841 return bus_log_create_error(r
);
843 r
= sd_bus_message_close_container(m
);
845 return bus_log_create_error(r
);
850 if (STR_IN_SET(field
, "StandardInput", "StandardOutput", "StandardError")) {
851 const char *n
, *appended
;
853 if ((n
= startswith(eq
, "fd:"))) {
854 appended
= strjoina(field
, "FileDescriptorName");
855 r
= sd_bus_message_append(m
, "(sv)", appended
, "s", n
);
856 } else if ((n
= startswith(eq
, "file:"))) {
857 appended
= strjoina(field
, "File");
858 r
= sd_bus_message_append(m
, "(sv)", appended
, "s", n
);
860 r
= sd_bus_message_append(m
, "(sv)", field
, "s", eq
);
863 return bus_log_create_error(r
);
868 if (streq(field
, "StandardInputText")) {
869 _cleanup_free_
char *unescaped
= NULL
;
871 r
= cunescape(eq
, 0, &unescaped
);
873 return log_error_errno(r
, "Failed to unescape text '%s': %m", eq
);
875 if (!strextend(&unescaped
, "\n", NULL
))
878 /* Note that we don't expand specifiers here, but that should be OK, as this is a programmatic
879 * interface anyway */
881 return bus_append_byte_array(m
, field
, unescaped
, strlen(unescaped
));
884 if (streq(field
, "StandardInputData")) {
885 _cleanup_free_
void *decoded
= NULL
;
888 r
= unbase64mem(eq
, (size_t) -1, &decoded
, &sz
);
890 return log_error_errno(r
, "Failed to decode base64 data '%s': %m", eq
);
892 return bus_append_byte_array(m
, field
, decoded
, sz
);
895 if ((suffix
= startswith(field
, "Limit"))) {
898 rl
= rlimit_from_string(suffix
);
903 r
= rlimit_parse(rl
, eq
, &l
);
905 return log_error_errno(r
, "Failed to parse resource limit: %s", eq
);
907 r
= sd_bus_message_append(m
, "(sv)", field
, "t", l
.rlim_max
);
909 return bus_log_create_error(r
);
911 sn
= strjoina(field
, "Soft");
912 r
= sd_bus_message_append(m
, "(sv)", sn
, "t", l
.rlim_cur
);
914 return bus_log_create_error(r
);
920 if (STR_IN_SET(field
, "AppArmorProfile", "SmackProcessLabel")) {
929 r
= sd_bus_message_append(m
, "(sv)", field
, "(bs)", ignore
, s
);
931 return bus_log_create_error(r
);
936 if (STR_IN_SET(field
, "CapabilityBoundingSet", "AmbientCapabilities")) {
946 r
= capability_set_from_string(p
, &sum
);
948 return log_error_errno(r
, "Failed to parse %s value %s: %m", field
, eq
);
950 sum
= invert
? ~sum
: sum
;
952 r
= sd_bus_message_append(m
, "(sv)", field
, "t", sum
);
954 return bus_log_create_error(r
);
959 if (streq(field
, "CPUAffinity")) {
960 _cleanup_cpu_free_ cpu_set_t
*cpuset
= NULL
;
962 r
= parse_cpu_set(eq
, &cpuset
);
964 return log_error_errno(r
, "Failed to parse %s value: %s", field
, eq
);
966 return bus_append_byte_array(m
, field
, cpuset
, CPU_ALLOC_SIZE(r
));
969 if (STR_IN_SET(field
, "RestrictAddressFamilies", "SystemCallFilter")) {
978 r
= sd_bus_message_open_container(m
, SD_BUS_TYPE_STRUCT
, "sv");
980 return bus_log_create_error(r
);
982 r
= sd_bus_message_append_basic(m
, SD_BUS_TYPE_STRING
, field
);
984 return bus_log_create_error(r
);
986 r
= sd_bus_message_open_container(m
, 'v', "(bas)");
988 return bus_log_create_error(r
);
990 r
= sd_bus_message_open_container(m
, 'r', "bas");
992 return bus_log_create_error(r
);
994 r
= sd_bus_message_append_basic(m
, 'b', &whitelist
);
996 return bus_log_create_error(r
);
998 r
= sd_bus_message_open_container(m
, 'a', "s");
1000 return bus_log_create_error(r
);
1003 _cleanup_free_
char *word
= NULL
;
1005 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
);
1011 return log_error_errno(r
, "Invalid syntax: %s", eq
);
1013 r
= sd_bus_message_append_basic(m
, 's', word
);
1015 return bus_log_create_error(r
);
1018 r
= sd_bus_message_close_container(m
);
1020 return bus_log_create_error(r
);
1022 r
= sd_bus_message_close_container(m
);
1024 return bus_log_create_error(r
);
1026 r
= sd_bus_message_close_container(m
);
1028 return bus_log_create_error(r
);
1030 r
= sd_bus_message_close_container(m
);
1032 return bus_log_create_error(r
);
1037 if (streq(field
, "RestrictNamespaces")) {
1038 bool invert
= false;
1039 unsigned long flags
;
1041 r
= parse_boolean(eq
);
1045 flags
= NAMESPACE_FLAGS_ALL
;
1052 r
= namespace_flags_from_string(eq
, &flags
);
1054 return log_error_errno(r
, "Failed to parse %s value %s.", field
, eq
);
1058 flags
= (~flags
) & NAMESPACE_FLAGS_ALL
;
1060 r
= sd_bus_message_append(m
, "(sv)", field
, "t", (uint64_t) flags
);
1062 return bus_log_create_error(r
);
1067 if (STR_IN_SET(field
, "BindPaths", "BindReadOnlyPaths")) {
1070 r
= sd_bus_message_open_container(m
, SD_BUS_TYPE_STRUCT
, "sv");
1072 return bus_log_create_error(r
);
1074 r
= sd_bus_message_append_basic(m
, SD_BUS_TYPE_STRING
, field
);
1076 return bus_log_create_error(r
);
1078 r
= sd_bus_message_open_container(m
, 'v', "a(ssbt)");
1080 return bus_log_create_error(r
);
1082 r
= sd_bus_message_open_container(m
, 'a', "(ssbt)");
1084 return bus_log_create_error(r
);
1087 _cleanup_free_
char *source
= NULL
, *destination
= NULL
;
1088 char *s
= NULL
, *d
= NULL
;
1089 bool ignore_enoent
= false;
1090 uint64_t flags
= MS_REC
;
1092 r
= extract_first_word(&p
, &source
, ":" WHITESPACE
, EXTRACT_QUOTES
|EXTRACT_DONT_COALESCE_SEPARATORS
);
1094 return log_error_errno(r
, "Failed to parse argument: %m");
1100 ignore_enoent
= true;
1104 if (p
&& p
[-1] == ':') {
1105 r
= extract_first_word(&p
, &destination
, ":" WHITESPACE
, EXTRACT_QUOTES
|EXTRACT_DONT_COALESCE_SEPARATORS
);
1107 return log_error_errno(r
, "Failed to parse argument: %m");
1109 log_error("Missing argument after ':': %s", eq
);
1115 if (p
&& p
[-1] == ':') {
1116 _cleanup_free_
char *options
= NULL
;
1118 r
= extract_first_word(&p
, &options
, NULL
, EXTRACT_QUOTES
);
1120 return log_error_errno(r
, "Failed to parse argument: %m");
1122 if (isempty(options
) || streq(options
, "rbind"))
1124 else if (streq(options
, "norbind"))
1127 log_error("Unknown options: %s", eq
);
1134 r
= sd_bus_message_append(m
, "(ssbt)", s
, d
, ignore_enoent
, flags
);
1136 return bus_log_create_error(r
);
1139 r
= sd_bus_message_close_container(m
);
1141 return bus_log_create_error(r
);
1143 r
= sd_bus_message_close_container(m
);
1145 return bus_log_create_error(r
);
1147 r
= sd_bus_message_close_container(m
);
1149 return bus_log_create_error(r
);
1154 if (streq(field
, "TemporaryFileSystem")) {
1157 r
= sd_bus_message_open_container(m
, SD_BUS_TYPE_STRUCT
, "sv");
1159 return bus_log_create_error(r
);
1161 r
= sd_bus_message_append_basic(m
, SD_BUS_TYPE_STRING
, field
);
1163 return bus_log_create_error(r
);
1165 r
= sd_bus_message_open_container(m
, 'v', "a(ss)");
1167 return bus_log_create_error(r
);
1169 r
= sd_bus_message_open_container(m
, 'a', "(ss)");
1171 return bus_log_create_error(r
);
1174 _cleanup_free_
char *word
= NULL
, *path
= NULL
;
1177 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
);
1179 return log_error_errno(r
, "Failed to parse argument: %m");
1184 r
= extract_first_word(&w
, &path
, ":", EXTRACT_DONT_COALESCE_SEPARATORS
);
1186 return log_error_errno(r
, "Failed to parse argument: %m");
1188 log_error("Failed to parse argument: %s", p
);
1192 r
= sd_bus_message_append(m
, "(ss)", path
, w
);
1194 return bus_log_create_error(r
);
1197 r
= sd_bus_message_close_container(m
);
1199 return bus_log_create_error(r
);
1201 r
= sd_bus_message_close_container(m
);
1203 return bus_log_create_error(r
);
1205 r
= sd_bus_message_close_container(m
);
1207 return bus_log_create_error(r
);
1215 static int bus_append_kill_property(sd_bus_message
*m
, const char *field
, const char *eq
) {
1217 if (streq(field
, "KillMode"))
1219 return bus_append_string(m
, field
, eq
);
1221 if (STR_IN_SET(field
, "SendSIGHUP", "SendSIGKILL"))
1223 return bus_append_parse_boolean(m
, field
, eq
);
1225 if (STR_IN_SET(field
, "KillSignal", "FinalKillSignal", "WatchdogSignal"))
1227 return bus_append_signal_from_string(m
, field
, eq
);
1232 static int bus_append_mount_property(sd_bus_message
*m
, const char *field
, const char *eq
) {
1234 if (STR_IN_SET(field
, "What", "Where", "Options", "Type"))
1236 return bus_append_string(m
, field
, eq
);
1238 if (streq(field
, "TimeoutSec"))
1240 return bus_append_parse_sec_rename(m
, field
, eq
);
1242 if (streq(field
, "DirectoryMode"))
1244 return bus_append_parse_mode(m
, field
, eq
);
1246 if (STR_IN_SET(field
, "SloppyOptions", "LazyUnmount", "ForceUnmount"))
1248 return bus_append_parse_boolean(m
, field
, eq
);
1253 static int bus_append_path_property(sd_bus_message
*m
, const char *field
, const char *eq
) {
1256 if (streq(field
, "MakeDirectory"))
1258 return bus_append_parse_boolean(m
, field
, eq
);
1260 if (streq(field
, "DirectoryMode"))
1262 return bus_append_parse_mode(m
, field
, eq
);
1264 if (STR_IN_SET(field
,
1265 "PathExists", "PathExistsGlob", "PathChanged",
1266 "PathModified", "DirectoryNotEmpty")) {
1269 r
= sd_bus_message_append(m
, "(sv)", "Paths", "a(ss)", 0);
1271 r
= sd_bus_message_append(m
, "(sv)", "Paths", "a(ss)", 1, field
, eq
);
1273 return bus_log_create_error(r
);
1281 static int bus_append_service_property(sd_bus_message
*m
, const char *field
, const char *eq
) {
1284 if (STR_IN_SET(field
,
1285 "PIDFile", "Type", "Restart", "BusName", "NotifyAccess",
1286 "USBFunctionDescriptors", "USBFunctionStrings"))
1288 return bus_append_string(m
, field
, eq
);
1290 if (STR_IN_SET(field
, "PermissionsStartOnly", "RootDirectoryStartOnly", "RemainAfterExit", "GuessMainPID"))
1292 return bus_append_parse_boolean(m
, field
, eq
);
1294 if (STR_IN_SET(field
, "RestartSec", "TimeoutStartSec", "TimeoutStopSec", "RuntimeMaxSec", "WatchdogSec"))
1296 return bus_append_parse_sec_rename(m
, field
, eq
);
1298 if (streq(field
, "TimeoutSec")) {
1300 r
= bus_append_parse_sec_rename(m
, "TimeoutStartSec", eq
);
1304 return bus_append_parse_sec_rename(m
, "TimeoutStopSec", eq
);
1307 if (streq(field
, "FileDescriptorStoreMax"))
1309 return bus_append_safe_atou(m
, field
, eq
);
1311 if (STR_IN_SET(field
,
1312 "ExecStartPre", "ExecStart", "ExecStartPost",
1313 "ExecReload", "ExecStop", "ExecStopPost"))
1315 return bus_append_exec_command(m
, field
, eq
);
1317 if (STR_IN_SET(field
, "RestartPreventExitStatus", "RestartForceExitStatus", "SuccessExitStatus")) {
1318 _cleanup_free_
int *status
= NULL
, *signal
= NULL
;
1319 size_t sz_status
= 0, sz_signal
= 0;
1323 _cleanup_free_
char *word
= NULL
;
1326 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
);
1332 return log_error_errno(r
, "Invalid syntax in %s: %s", field
, eq
);
1334 r
= safe_atoi(word
, &val
);
1336 val
= signal_from_string(word
);
1338 return log_error_errno(r
, "Invalid status or signal %s in %s: %m", word
, field
);
1340 signal
= reallocarray(signal
, sz_signal
+ 1, sizeof(int));
1344 signal
[sz_signal
++] = val
;
1346 status
= reallocarray(status
, sz_status
+ 1, sizeof(int));
1350 status
[sz_status
++] = val
;
1354 r
= sd_bus_message_open_container(m
, SD_BUS_TYPE_STRUCT
, "sv");
1356 return bus_log_create_error(r
);
1358 r
= sd_bus_message_append_basic(m
, SD_BUS_TYPE_STRING
, field
);
1360 return bus_log_create_error(r
);
1362 r
= sd_bus_message_open_container(m
, 'v', "(aiai)");
1364 return bus_log_create_error(r
);
1366 r
= sd_bus_message_open_container(m
, 'r', "aiai");
1368 return bus_log_create_error(r
);
1370 r
= sd_bus_message_append_array(m
, 'i', status
, sz_status
);
1372 return bus_log_create_error(r
);
1374 r
= sd_bus_message_append_array(m
, 'i', signal
, sz_signal
);
1376 return bus_log_create_error(r
);
1378 r
= sd_bus_message_close_container(m
);
1380 return bus_log_create_error(r
);
1382 r
= sd_bus_message_close_container(m
);
1384 return bus_log_create_error(r
);
1386 r
= sd_bus_message_close_container(m
);
1388 return bus_log_create_error(r
);
1396 static int bus_append_socket_property(sd_bus_message
*m
, const char *field
, const char *eq
) {
1399 if (STR_IN_SET(field
,
1400 "Accept", "Writable", "KeepAlive", "NoDelay", "FreeBind", "Transparent", "Broadcast",
1401 "PassCredentials", "PassSecurity", "ReusePort", "RemoveOnStop", "SELinuxContextFromNet"))
1403 return bus_append_parse_boolean(m
, field
, eq
);
1405 if (STR_IN_SET(field
, "Priority", "IPTTL", "Mark"))
1407 return bus_append_safe_atoi(m
, field
, eq
);
1409 if (streq(field
, "IPTOS"))
1411 return bus_append_ip_tos_from_string(m
, field
, eq
);
1413 if (STR_IN_SET(field
, "Backlog", "MaxConnections", "MaxConnectionsPerSource", "KeepAliveProbes", "TriggerLimitBurst"))
1415 return bus_append_safe_atou(m
, field
, eq
);
1417 if (STR_IN_SET(field
, "SocketMode", "DirectoryMode"))
1419 return bus_append_parse_mode(m
, field
, eq
);
1421 if (STR_IN_SET(field
, "MessageQueueMaxMessages", "MessageQueueMessageSize"))
1423 return bus_append_safe_atoi64(m
, field
, eq
);
1425 if (STR_IN_SET(field
, "TimeoutSec", "KeepAliveTimeSec", "KeepAliveIntervalSec", "DeferAcceptSec", "TriggerLimitIntervalSec"))
1427 return bus_append_parse_sec_rename(m
, field
, eq
);
1429 if (STR_IN_SET(field
, "ReceiveBuffer", "SendBuffer", "PipeSize"))
1431 return bus_append_parse_size(m
, field
, eq
, 1024);
1433 if (STR_IN_SET(field
, "ExecStartPre", "ExecStartPost", "ExecReload", "ExecStopPost"))
1435 return bus_append_exec_command(m
, field
, eq
);
1437 if (STR_IN_SET(field
,
1438 "SmackLabel", "SmackLabelIPIn", "SmackLabelIPOut", "TCPCongestion",
1439 "BindToDevice", "BindIPv6Only", "FileDescriptorName",
1440 "SocketUser", "SocketGroup"))
1442 return bus_append_string(m
, field
, eq
);
1444 if (streq(field
, "Symlinks"))
1446 return bus_append_strv(m
, field
, eq
, EXTRACT_QUOTES
);
1448 if (streq(field
, "SocketProtocol"))
1450 return bus_append_socket_protocol_from_name(m
, field
, eq
);
1452 if (STR_IN_SET(field
,
1453 "ListenStream", "ListenDatagram", "ListenSequentialPacket", "ListenNetlink",
1454 "ListenSpecial", "ListenMessageQueue", "ListenFIFO", "ListenUSBFunction")) {
1457 r
= sd_bus_message_append(m
, "(sv)", "Listen", "a(ss)", 0);
1459 r
= sd_bus_message_append(m
, "(sv)", "Listen", "a(ss)", 1, field
+ STRLEN("Listen"), eq
);
1461 return bus_log_create_error(r
);
1468 static int bus_append_timer_property(sd_bus_message
*m
, const char *field
, const char *eq
) {
1471 if (STR_IN_SET(field
, "WakeSystem", "RemainAfterElapse", "Persistent"))
1473 return bus_append_parse_boolean(m
, field
, eq
);
1475 if (STR_IN_SET(field
, "AccuracySec", "RandomizedDelaySec"))
1477 return bus_append_parse_sec_rename(m
, field
, eq
);
1479 if (STR_IN_SET(field
,
1480 "OnActiveSec", "OnBootSec", "OnStartupSec",
1481 "OnUnitActiveSec","OnUnitInactiveSec")) {
1484 r
= sd_bus_message_append(m
, "(sv)", "TimersMonotonic", "a(st)", 0);
1487 r
= parse_sec(eq
, &t
);
1489 return log_error_errno(r
, "Failed to parse %s=%s: %m", field
, eq
);
1491 r
= sd_bus_message_append(m
, "(sv)", "TimersMonotonic", "a(st)", 1, field
, t
);
1494 return bus_log_create_error(r
);
1499 if (streq(field
, "OnCalendar")) {
1502 r
= sd_bus_message_append(m
, "(sv)", "TimersCalendar", "a(ss)", 0);
1504 r
= sd_bus_message_append(m
, "(sv)", "TimersCalendar", "a(ss)", 1, field
, eq
);
1506 return bus_log_create_error(r
);
1514 static int bus_append_unit_property(sd_bus_message
*m
, const char *field
, const char *eq
) {
1515 ConditionType t
= _CONDITION_TYPE_INVALID
;
1516 bool is_condition
= false;
1519 if (STR_IN_SET(field
,
1520 "Description", "SourcePath", "OnFailureJobMode",
1521 "JobTimeoutAction", "JobTimeoutRebootArgument",
1522 "StartLimitAction", "FailureAction", "SuccessAction",
1523 "RebootArgument", "CollectMode"))
1525 return bus_append_string(m
, field
, eq
);
1527 if (STR_IN_SET(field
,
1528 "StopWhenUnneeded", "RefuseManualStart", "RefuseManualStop",
1529 "AllowIsolate", "IgnoreOnIsolate", "DefaultDependencies"))
1531 return bus_append_parse_boolean(m
, field
, eq
);
1533 if (STR_IN_SET(field
, "JobTimeoutSec", "JobRunningTimeoutSec", "StartLimitIntervalSec"))
1535 return bus_append_parse_sec_rename(m
, field
, eq
);
1537 if (streq(field
, "StartLimitBurst"))
1539 return bus_append_safe_atou(m
, field
, eq
);
1541 if (unit_dependency_from_string(field
) >= 0 ||
1542 STR_IN_SET(field
, "Documentation", "RequiresMountsFor"))
1544 return bus_append_strv(m
, field
, eq
, EXTRACT_QUOTES
);
1546 t
= condition_type_from_string(field
);
1548 is_condition
= true;
1550 t
= assert_type_from_string(field
);
1553 r
= sd_bus_message_append(m
, "(sv)", is_condition
? "Conditions" : "Asserts", "a(sbbs)", 0);
1556 int trigger
, negate
;
1558 trigger
= *p
== '|';
1566 r
= sd_bus_message_append(m
, "(sv)", is_condition
? "Conditions" : "Asserts", "a(sbbs)", 1,
1567 field
, trigger
, negate
, p
);
1570 return bus_log_create_error(r
);
1578 int bus_append_unit_property_assignment(sd_bus_message
*m
, UnitType t
, const char *assignment
) {
1579 const char *eq
, *field
;
1585 eq
= strchr(assignment
, '=');
1587 log_error("Not an assignment: %s", assignment
);
1591 field
= strndupa(assignment
, eq
- assignment
);
1596 r
= bus_append_cgroup_property(m
, field
, eq
);
1600 r
= bus_append_execute_property(m
, field
, eq
);
1604 r
= bus_append_kill_property(m
, field
, eq
);
1608 r
= bus_append_service_property(m
, field
, eq
);
1614 r
= bus_append_cgroup_property(m
, field
, eq
);
1618 r
= bus_append_execute_property(m
, field
, eq
);
1622 r
= bus_append_kill_property(m
, field
, eq
);
1626 r
= bus_append_socket_property(m
, field
, eq
);
1632 r
= bus_append_timer_property(m
, field
, eq
);
1638 r
= bus_append_path_property(m
, field
, eq
);
1644 r
= bus_append_cgroup_property(m
, field
, eq
);
1651 if (streq(field
, "TimeoutStopSec"))
1652 return bus_append_parse_sec_rename(m
, field
, eq
);
1654 r
= bus_append_cgroup_property(m
, field
, eq
);
1658 r
= bus_append_kill_property(m
, field
, eq
);
1664 r
= bus_append_cgroup_property(m
, field
, eq
);
1668 r
= bus_append_execute_property(m
, field
, eq
);
1672 r
= bus_append_kill_property(m
, field
, eq
);
1676 r
= bus_append_mount_property(m
, field
, eq
);
1682 case UNIT_AUTOMOUNT
:
1683 r
= bus_append_automount_property(m
, field
, eq
);
1692 log_error("Not supported unit type");
1696 log_error("Invalid unit type");
1700 r
= bus_append_unit_property(m
, field
, eq
);
1704 log_error("Unknown assignment: %s", assignment
);
1708 int bus_append_unit_property_assignment_many(sd_bus_message
*m
, UnitType t
, char **l
) {
1714 STRV_FOREACH(i
, l
) {
1715 r
= bus_append_unit_property_assignment(m
, t
, *i
);
1723 typedef struct BusWaitForJobs
{
1730 sd_bus_slot
*slot_job_removed
;
1731 sd_bus_slot
*slot_disconnected
;
1734 static int match_disconnected(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1737 log_error("Warning! D-Bus connection terminated.");
1738 sd_bus_close(sd_bus_message_get_bus(m
));
1743 static int match_job_removed(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1744 const char *path
, *unit
, *result
;
1745 BusWaitForJobs
*d
= userdata
;
1753 r
= sd_bus_message_read(m
, "uoss", &id
, &path
, &unit
, &result
);
1755 bus_log_parse_error(r
);
1759 found
= set_remove(d
->jobs
, (char*) path
);
1765 if (!isempty(result
))
1766 d
->result
= strdup(result
);
1769 d
->name
= strdup(unit
);
1774 void bus_wait_for_jobs_free(BusWaitForJobs
*d
) {
1778 set_free_free(d
->jobs
);
1780 sd_bus_slot_unref(d
->slot_disconnected
);
1781 sd_bus_slot_unref(d
->slot_job_removed
);
1783 sd_bus_unref(d
->bus
);
1791 int bus_wait_for_jobs_new(sd_bus
*bus
, BusWaitForJobs
**ret
) {
1792 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*d
= NULL
;
1798 d
= new0(BusWaitForJobs
, 1);
1802 d
->bus
= sd_bus_ref(bus
);
1804 /* When we are a bus client we match by sender. Direct
1805 * connections OTOH have no initialized sender field, and
1806 * hence we ignore the sender then */
1807 r
= sd_bus_match_signal_async(
1809 &d
->slot_job_removed
,
1810 bus
->bus_client
? "org.freedesktop.systemd1" : NULL
,
1811 "/org/freedesktop/systemd1",
1812 "org.freedesktop.systemd1.Manager",
1814 match_job_removed
, NULL
, d
);
1818 r
= sd_bus_match_signal_async(
1820 &d
->slot_disconnected
,
1821 "org.freedesktop.DBus.Local",
1823 "org.freedesktop.DBus.Local",
1825 match_disconnected
, NULL
, d
);
1834 static int bus_process_wait(sd_bus
*bus
) {
1838 r
= sd_bus_process(bus
, NULL
);
1844 r
= sd_bus_wait(bus
, (uint64_t) -1);
1850 static int bus_job_get_service_result(BusWaitForJobs
*d
, char **result
) {
1851 _cleanup_free_
char *dbus_path
= NULL
;
1857 if (!endswith(d
->name
, ".service"))
1860 dbus_path
= unit_dbus_path_from_name(d
->name
);
1864 return sd_bus_get_property_string(d
->bus
,
1865 "org.freedesktop.systemd1",
1867 "org.freedesktop.systemd1.Service",
1873 static const struct {
1874 const char *result
, *explanation
;
1875 } explanations
[] = {
1876 { "resources", "of unavailable resources or another system error" },
1877 { "protocol", "the service did not take the steps required by its unit configuration" },
1878 { "timeout", "a timeout was exceeded" },
1879 { "exit-code", "the control process exited with error code" },
1880 { "signal", "a fatal signal was delivered to the control process" },
1881 { "core-dump", "a fatal signal was delivered causing the control process to dump core" },
1882 { "watchdog", "the service failed to send watchdog ping" },
1883 { "start-limit", "start of the service was attempted too often" }
1886 static void log_job_error_with_service_result(const char* service
, const char *result
, const char* const* extra_args
) {
1887 _cleanup_free_
char *service_shell_quoted
= NULL
;
1888 const char *systemctl
= "systemctl", *journalctl
= "journalctl";
1892 service_shell_quoted
= shell_maybe_quote(service
, ESCAPE_BACKSLASH
);
1894 if (!strv_isempty((char**) extra_args
)) {
1895 _cleanup_free_
char *t
;
1897 t
= strv_join((char**) extra_args
, " ");
1898 systemctl
= strjoina("systemctl ", t
? : "<args>");
1899 journalctl
= strjoina("journalctl ", t
? : "<args>");
1902 if (!isempty(result
)) {
1905 for (i
= 0; i
< ELEMENTSOF(explanations
); ++i
)
1906 if (streq(result
, explanations
[i
].result
))
1909 if (i
< ELEMENTSOF(explanations
)) {
1910 log_error("Job for %s failed because %s.\n"
1911 "See \"%s status %s\" and \"%s -xe\" for details.\n",
1913 explanations
[i
].explanation
,
1915 service_shell_quoted
?: "<service>",
1921 log_error("Job for %s failed.\n"
1922 "See \"%s status %s\" and \"%s -xe\" for details.\n",
1925 service_shell_quoted
?: "<service>",
1929 /* For some results maybe additional explanation is required */
1930 if (streq_ptr(result
, "start-limit"))
1931 log_info("To force a start use \"%1$s reset-failed %2$s\"\n"
1932 "followed by \"%1$s start %2$s\" again.",
1934 service_shell_quoted
?: "<service>");
1937 static int check_wait_response(BusWaitForJobs
*d
, bool quiet
, const char* const* extra_args
) {
1941 if (streq(d
->result
, "canceled"))
1942 log_error("Job for %s canceled.", strna(d
->name
));
1943 else if (streq(d
->result
, "timeout"))
1944 log_error("Job for %s timed out.", strna(d
->name
));
1945 else if (streq(d
->result
, "dependency"))
1946 log_error("A dependency job for %s failed. See 'journalctl -xe' for details.", strna(d
->name
));
1947 else if (streq(d
->result
, "invalid"))
1948 log_error("%s is not active, cannot reload.", strna(d
->name
));
1949 else if (streq(d
->result
, "assert"))
1950 log_error("Assertion failed on job for %s.", strna(d
->name
));
1951 else if (streq(d
->result
, "unsupported"))
1952 log_error("Operation on or unit type of %s not supported on this system.", strna(d
->name
));
1953 else if (streq(d
->result
, "collected"))
1954 log_error("Queued job for %s was garbage collected.", strna(d
->name
));
1955 else if (streq(d
->result
, "once"))
1956 log_error("Unit %s was started already once and can't be started again.", strna(d
->name
));
1957 else if (!STR_IN_SET(d
->result
, "done", "skipped")) {
1959 _cleanup_free_
char *result
= NULL
;
1962 q
= bus_job_get_service_result(d
, &result
);
1964 log_debug_errno(q
, "Failed to get Result property of unit %s: %m", d
->name
);
1966 log_job_error_with_service_result(d
->name
, result
, extra_args
);
1968 log_error("Job failed. See \"journalctl -xe\" for details.");
1972 if (STR_IN_SET(d
->result
, "canceled", "collected"))
1974 else if (streq(d
->result
, "timeout"))
1976 else if (streq(d
->result
, "dependency"))
1978 else if (streq(d
->result
, "invalid"))
1980 else if (streq(d
->result
, "assert"))
1982 else if (streq(d
->result
, "unsupported"))
1984 else if (streq(d
->result
, "once"))
1986 else if (STR_IN_SET(d
->result
, "done", "skipped"))
1989 log_debug("Unexpected job result, assuming server side newer than us: %s", d
->result
);
1993 int bus_wait_for_jobs(BusWaitForJobs
*d
, bool quiet
, const char* const* extra_args
) {
1998 while (!set_isempty(d
->jobs
)) {
2001 q
= bus_process_wait(d
->bus
);
2003 return log_error_errno(q
, "Failed to wait for response: %m");
2006 q
= check_wait_response(d
, quiet
, extra_args
);
2007 /* Return the first error as it is most likely to be
2009 if (q
< 0 && r
== 0)
2012 log_debug_errno(q
, "Got result %s/%m for job %s", strna(d
->result
), strna(d
->name
));
2015 d
->name
= mfree(d
->name
);
2016 d
->result
= mfree(d
->result
);
2022 int bus_wait_for_jobs_add(BusWaitForJobs
*d
, const char *path
) {
2027 r
= set_ensure_allocated(&d
->jobs
, &string_hash_ops
);
2031 return set_put_strdup(d
->jobs
, path
);
2034 int bus_wait_for_jobs_one(BusWaitForJobs
*d
, const char *path
, bool quiet
) {
2037 r
= bus_wait_for_jobs_add(d
, path
);
2041 return bus_wait_for_jobs(d
, quiet
, NULL
);
2044 int bus_deserialize_and_dump_unit_file_changes(sd_bus_message
*m
, bool quiet
, UnitFileChange
**changes
, size_t *n_changes
) {
2045 const char *type
, *path
, *source
;
2048 /* changes is dereferenced when calling unit_file_dump_changes() later,
2049 * so we have to make sure this is not NULL. */
2053 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_ARRAY
, "(sss)");
2055 return bus_log_parse_error(r
);
2057 while ((r
= sd_bus_message_read(m
, "(sss)", &type
, &path
, &source
)) > 0) {
2058 /* We expect only "success" changes to be sent over the bus.
2059 Hence, reject anything negative. */
2060 UnitFileChangeType ch
= unit_file_change_type_from_string(type
);
2063 log_notice("Manager reported unknown change type \"%s\" for path \"%s\", ignoring.", type
, path
);
2067 r
= unit_file_changes_add(changes
, n_changes
, ch
, path
, source
);
2072 return bus_log_parse_error(r
);
2074 r
= sd_bus_message_exit_container(m
);
2076 return bus_log_parse_error(r
);
2078 unit_file_dump_changes(0, NULL
, *changes
, *n_changes
, quiet
);
2084 bool is_const
; /* If false, cgroup_path should be free()'d */
2086 Hashmap
*pids
; /* PID → process name */
2089 struct CGroupInfo
*parent
;
2090 LIST_FIELDS(struct CGroupInfo
, siblings
);
2091 LIST_HEAD(struct CGroupInfo
, children
);
2095 static bool IS_ROOT(const char *p
) {
2096 return isempty(p
) || streq(p
, "/");
2099 static int add_cgroup(Hashmap
*cgroups
, const char *path
, bool is_const
, struct CGroupInfo
**ret
) {
2100 struct CGroupInfo
*parent
= NULL
, *cg
;
2109 cg
= hashmap_get(cgroups
, path
);
2115 if (!IS_ROOT(path
)) {
2118 e
= strrchr(path
, '/');
2122 pp
= strndupa(path
, e
- path
);
2126 r
= add_cgroup(cgroups
, pp
, false, &parent
);
2131 cg
= new0(struct CGroupInfo
, 1);
2136 cg
->cgroup_path
= (char*) path
;
2138 cg
->cgroup_path
= strdup(path
);
2139 if (!cg
->cgroup_path
) {
2145 cg
->is_const
= is_const
;
2146 cg
->parent
= parent
;
2148 r
= hashmap_put(cgroups
, cg
->cgroup_path
, cg
);
2151 free(cg
->cgroup_path
);
2157 LIST_PREPEND(siblings
, parent
->children
, cg
);
2158 parent
->n_children
++;
2165 static int add_process(
2171 struct CGroupInfo
*cg
;
2178 r
= add_cgroup(cgroups
, path
, true, &cg
);
2182 r
= hashmap_ensure_allocated(&cg
->pids
, &trivial_hash_ops
);
2186 return hashmap_put(cg
->pids
, PID_TO_PTR(pid
), (void*) name
);
2189 static void remove_cgroup(Hashmap
*cgroups
, struct CGroupInfo
*cg
) {
2193 while (cg
->children
)
2194 remove_cgroup(cgroups
, cg
->children
);
2196 hashmap_remove(cgroups
, cg
->cgroup_path
);
2199 free(cg
->cgroup_path
);
2201 hashmap_free(cg
->pids
);
2204 LIST_REMOVE(siblings
, cg
->parent
->children
, cg
);
2209 static int cgroup_info_compare_func(struct CGroupInfo
* const *a
, struct CGroupInfo
* const *b
) {
2210 return strcmp((*a
)->cgroup_path
, (*b
)->cgroup_path
);
2213 static int dump_processes(
2215 const char *cgroup_path
,
2218 OutputFlags flags
) {
2220 struct CGroupInfo
*cg
;
2225 if (IS_ROOT(cgroup_path
))
2228 cg
= hashmap_get(cgroups
, cgroup_path
);
2232 if (!hashmap_isempty(cg
->pids
)) {
2240 /* Order processes by their PID */
2241 pids
= newa(pid_t
, hashmap_size(cg
->pids
));
2243 HASHMAP_FOREACH_KEY(name
, pidp
, cg
->pids
, j
)
2244 pids
[n
++] = PTR_TO_PID(pidp
);
2246 assert(n
== hashmap_size(cg
->pids
));
2247 typesafe_qsort(pids
, n
, pid_compare_func
);
2249 width
= DECIMAL_STR_WIDTH(pids
[n
-1]);
2251 for (i
= 0; i
< n
; i
++) {
2252 _cleanup_free_
char *e
= NULL
;
2253 const char *special
;
2256 name
= hashmap_get(cg
->pids
, PID_TO_PTR(pids
[i
]));
2259 if (n_columns
!= 0) {
2262 k
= MAX(LESS_BY(n_columns
, 2U + width
+ 1U), 20U);
2264 e
= ellipsize(name
, k
, 100);
2269 more
= i
+1 < n
|| cg
->children
;
2270 special
= special_glyph(more
? TREE_BRANCH
: TREE_RIGHT
);
2272 fprintf(stdout
, "%s%s%*"PID_PRI
" %s\n",
2281 struct CGroupInfo
**children
, *child
;
2284 /* Order subcgroups by their name */
2285 children
= newa(struct CGroupInfo
*, cg
->n_children
);
2286 LIST_FOREACH(siblings
, child
, cg
->children
)
2287 children
[n
++] = child
;
2288 assert(n
== cg
->n_children
);
2289 typesafe_qsort(children
, n
, cgroup_info_compare_func
);
2292 n_columns
= MAX(LESS_BY(n_columns
, 2U), 20U);
2294 for (i
= 0; i
< n
; i
++) {
2295 _cleanup_free_
char *pp
= NULL
;
2296 const char *name
, *special
;
2299 child
= children
[i
];
2301 name
= strrchr(child
->cgroup_path
, '/');
2307 special
= special_glyph(more
? TREE_BRANCH
: TREE_RIGHT
);
2309 fputs(prefix
, stdout
);
2310 fputs(special
, stdout
);
2311 fputs(name
, stdout
);
2312 fputc('\n', stdout
);
2314 special
= special_glyph(more
? TREE_VERTICAL
: TREE_SPACE
);
2316 pp
= strappend(prefix
, special
);
2320 r
= dump_processes(cgroups
, child
->cgroup_path
, pp
, n_columns
, flags
);
2330 static int dump_extra_processes(
2334 OutputFlags flags
) {
2336 _cleanup_free_ pid_t
*pids
= NULL
;
2337 _cleanup_hashmap_free_ Hashmap
*names
= NULL
;
2338 struct CGroupInfo
*cg
;
2339 size_t n_allocated
= 0, n
= 0, k
;
2343 /* Prints the extra processes, i.e. those that are in cgroups we haven't displayed yet. We show them as
2344 * combined, sorted, linear list. */
2346 HASHMAP_FOREACH(cg
, cgroups
, i
) {
2354 if (hashmap_isempty(cg
->pids
))
2357 r
= hashmap_ensure_allocated(&names
, &trivial_hash_ops
);
2361 if (!GREEDY_REALLOC(pids
, n_allocated
, n
+ hashmap_size(cg
->pids
)))
2364 HASHMAP_FOREACH_KEY(name
, pidp
, cg
->pids
, j
) {
2365 pids
[n
++] = PTR_TO_PID(pidp
);
2367 r
= hashmap_put(names
, pidp
, (void*) name
);
2376 typesafe_qsort(pids
, n
, pid_compare_func
);
2377 width
= DECIMAL_STR_WIDTH(pids
[n
-1]);
2379 for (k
= 0; k
< n
; k
++) {
2380 _cleanup_free_
char *e
= NULL
;
2383 name
= hashmap_get(names
, PID_TO_PTR(pids
[k
]));
2386 if (n_columns
!= 0) {
2389 z
= MAX(LESS_BY(n_columns
, 2U + width
+ 1U), 20U);
2391 e
= ellipsize(name
, z
, 100);
2396 fprintf(stdout
, "%s%s %*" PID_PRI
" %s\n",
2398 special_glyph(TRIANGULAR_BULLET
),
2406 int unit_show_processes(
2409 const char *cgroup_path
,
2413 sd_bus_error
*error
) {
2415 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
2416 Hashmap
*cgroups
= NULL
;
2417 struct CGroupInfo
*cg
;
2423 if (flags
& OUTPUT_FULL_WIDTH
)
2425 else if (n_columns
<= 0)
2426 n_columns
= columns();
2428 prefix
= strempty(prefix
);
2430 r
= sd_bus_call_method(
2432 "org.freedesktop.systemd1",
2433 "/org/freedesktop/systemd1",
2434 "org.freedesktop.systemd1.Manager",
2443 cgroups
= hashmap_new(&path_hash_ops
);
2447 r
= sd_bus_message_enter_container(reply
, 'a', "(sus)");
2452 const char *path
= NULL
, *name
= NULL
;
2455 r
= sd_bus_message_read(reply
, "(sus)", &path
, &pid
, &name
);
2461 r
= add_process(cgroups
, path
, pid
, name
);
2466 r
= sd_bus_message_exit_container(reply
);
2470 r
= dump_processes(cgroups
, cgroup_path
, prefix
, n_columns
, flags
);
2474 r
= dump_extra_processes(cgroups
, prefix
, n_columns
, flags
);
2477 while ((cg
= hashmap_first(cgroups
)))
2478 remove_cgroup(cgroups
, cg
);
2480 hashmap_free(cgroups
);
2485 int unit_load_state(sd_bus
*bus
, const char *name
, char **load_state
) {
2486 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2487 _cleanup_free_
char *path
= NULL
;
2490 path
= unit_dbus_path_from_name(name
);
2494 /* This function warns on it's own, because otherwise it'd be awkward to pass
2495 * the dbus error message around. */
2497 r
= sd_bus_get_property_string(
2499 "org.freedesktop.systemd1",
2501 "org.freedesktop.systemd1.Unit",
2506 return log_error_errno(r
, "Failed to get load state of %s: %s", name
, bus_error_message(&error
, r
));