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
);
671 _cleanup_free_
char *word
= NULL
;
673 r
= extract_first_word(&eq
, &word
, NULL
, 0);
679 return log_error_errno(r
, "Failed to parse %s: %s", field
, eq
);
681 r
= in_addr_prefix_from_string_auto(word
, &family
, &prefix
, &prefixlen
);
683 return log_error_errno(r
, "Failed to parse IP address prefix: %s", word
);
685 r
= bus_append_ip_address_access(m
, family
, &prefix
, prefixlen
);
687 return bus_log_create_error(r
);
691 r
= sd_bus_message_close_container(m
);
693 return bus_log_create_error(r
);
695 r
= sd_bus_message_close_container(m
);
697 return bus_log_create_error(r
);
699 r
= sd_bus_message_close_container(m
);
701 return bus_log_create_error(r
);
709 static int bus_append_automount_property(sd_bus_message
*m
, const char *field
, const char *eq
) {
711 if (streq(field
, "Where"))
713 return bus_append_string(m
, field
, eq
);
715 if (streq(field
, "DirectoryMode"))
717 return bus_append_parse_mode(m
, field
, eq
);
719 if (streq(field
, "TimeoutIdleSec"))
721 return bus_append_parse_sec_rename(m
, field
, eq
);
726 static int bus_append_execute_property(sd_bus_message
*m
, const char *field
, const char *eq
) {
730 if (STR_IN_SET(field
,
732 "UtmpIdentifier", "UtmpMode", "PAMName", "TTYPath",
733 "WorkingDirectory", "RootDirectory", "SyslogIdentifier",
734 "ProtectSystem", "ProtectHome", "SELinuxContext", "RootImage",
735 "RuntimeDirectoryPreserve", "Personality", "KeyringMode"))
737 return bus_append_string(m
, field
, eq
);
739 if (STR_IN_SET(field
,
740 "IgnoreSIGPIPE", "TTYVHangup", "TTYReset", "TTYVTDisallocate",
741 "PrivateTmp", "PrivateDevices", "PrivateNetwork", "PrivateUsers",
742 "PrivateMounts", "NoNewPrivileges", "SyslogLevelPrefix",
743 "MemoryDenyWriteExecute", "RestrictRealtime", "DynamicUser", "RemoveIPC",
744 "ProtectKernelTunables", "ProtectKernelModules", "ProtectControlGroups",
745 "MountAPIVFS", "CPUSchedulingResetOnFork", "LockPersonality"))
747 return bus_append_parse_boolean(m
, field
, eq
);
749 if (STR_IN_SET(field
,
750 "ReadWriteDirectories", "ReadOnlyDirectories", "InaccessibleDirectories",
751 "ReadWritePaths", "ReadOnlyPaths", "InaccessiblePaths",
752 "RuntimeDirectory", "StateDirectory", "CacheDirectory", "LogsDirectory", "ConfigurationDirectory",
753 "SupplementaryGroups", "SystemCallArchitectures"))
755 return bus_append_strv(m
, field
, eq
, EXTRACT_QUOTES
);
757 if (STR_IN_SET(field
, "SyslogLevel", "LogLevelMax"))
759 return bus_append_log_level_from_string(m
, field
, eq
);
761 if (streq(field
, "SyslogFacility"))
763 return bus_append_log_facility_unshifted_from_string(m
, field
, eq
);
765 if (streq(field
, "SecureBits"))
767 return bus_append_secure_bits_from_string(m
, field
, eq
);
769 if (streq(field
, "CPUSchedulingPolicy"))
771 return bus_append_sched_policy_from_string(m
, field
, eq
);
773 if (STR_IN_SET(field
, "CPUSchedulingPriority", "OOMScoreAdjust"))
775 return bus_append_safe_atoi(m
, field
, eq
);
777 if (streq(field
, "Nice"))
779 return bus_append_parse_nice(m
, field
, eq
);
781 if (streq(field
, "SystemCallErrorNumber"))
783 return bus_append_parse_errno(m
, field
, eq
);
785 if (streq(field
, "IOSchedulingClass"))
787 return bus_append_ioprio_class_from_string(m
, field
, eq
);
789 if (streq(field
, "IOSchedulingPriority"))
791 return bus_append_ioprio_parse_priority(m
, field
, eq
);
793 if (STR_IN_SET(field
,
794 "RuntimeDirectoryMode", "StateDirectoryMode", "CacheDirectoryMode",
795 "LogsDirectoryMode", "ConfigurationDirectoryMode", "UMask"))
797 return bus_append_parse_mode(m
, field
, eq
);
799 if (streq(field
, "TimerSlackNSec"))
801 return bus_append_parse_nsec(m
, field
, eq
);
803 if (streq(field
, "MountFlags"))
805 return bus_append_mount_propagation_flags_from_string(m
, field
, eq
);
807 if (STR_IN_SET(field
, "Environment", "UnsetEnvironment", "PassEnvironment"))
809 return bus_append_strv(m
, field
, eq
, EXTRACT_QUOTES
|EXTRACT_CUNESCAPE
);
811 if (streq(field
, "EnvironmentFile")) {
814 r
= sd_bus_message_append(m
, "(sv)", "EnvironmentFiles", "a(sb)", 0);
816 r
= sd_bus_message_append(m
, "(sv)", "EnvironmentFiles", "a(sb)", 1,
817 eq
[0] == '-' ? eq
+ 1 : eq
,
820 return bus_log_create_error(r
);
825 if (streq(field
, "LogExtraFields")) {
827 r
= sd_bus_message_open_container(m
, 'r', "sv");
829 return bus_log_create_error(r
);
831 r
= sd_bus_message_append_basic(m
, 's', "LogExtraFields");
833 return bus_log_create_error(r
);
835 r
= sd_bus_message_open_container(m
, 'v', "aay");
837 return bus_log_create_error(r
);
839 r
= sd_bus_message_open_container(m
, 'a', "ay");
841 return bus_log_create_error(r
);
843 r
= sd_bus_message_append_array(m
, 'y', eq
, strlen(eq
));
845 return bus_log_create_error(r
);
847 r
= sd_bus_message_close_container(m
);
849 return bus_log_create_error(r
);
851 r
= sd_bus_message_close_container(m
);
853 return bus_log_create_error(r
);
855 r
= sd_bus_message_close_container(m
);
857 return bus_log_create_error(r
);
862 if (STR_IN_SET(field
, "StandardInput", "StandardOutput", "StandardError")) {
863 const char *n
, *appended
;
865 if ((n
= startswith(eq
, "fd:"))) {
866 appended
= strjoina(field
, "FileDescriptorName");
867 r
= sd_bus_message_append(m
, "(sv)", appended
, "s", n
);
868 } else if ((n
= startswith(eq
, "file:"))) {
869 appended
= strjoina(field
, "File");
870 r
= sd_bus_message_append(m
, "(sv)", appended
, "s", n
);
872 r
= sd_bus_message_append(m
, "(sv)", field
, "s", eq
);
875 return bus_log_create_error(r
);
880 if (streq(field
, "StandardInputText")) {
881 _cleanup_free_
char *unescaped
= NULL
;
883 r
= cunescape(eq
, 0, &unescaped
);
885 return log_error_errno(r
, "Failed to unescape text '%s': %m", eq
);
887 if (!strextend(&unescaped
, "\n", NULL
))
890 /* Note that we don't expand specifiers here, but that should be OK, as this is a programmatic
891 * interface anyway */
893 return bus_append_byte_array(m
, field
, unescaped
, strlen(unescaped
));
896 if (streq(field
, "StandardInputData")) {
897 _cleanup_free_
void *decoded
= NULL
;
900 r
= unbase64mem(eq
, (size_t) -1, &decoded
, &sz
);
902 return log_error_errno(r
, "Failed to decode base64 data '%s': %m", eq
);
904 return bus_append_byte_array(m
, field
, decoded
, sz
);
907 if ((suffix
= startswith(field
, "Limit"))) {
910 rl
= rlimit_from_string(suffix
);
915 r
= rlimit_parse(rl
, eq
, &l
);
917 return log_error_errno(r
, "Failed to parse resource limit: %s", eq
);
919 r
= sd_bus_message_append(m
, "(sv)", field
, "t", l
.rlim_max
);
921 return bus_log_create_error(r
);
923 sn
= strjoina(field
, "Soft");
924 r
= sd_bus_message_append(m
, "(sv)", sn
, "t", l
.rlim_cur
);
926 return bus_log_create_error(r
);
932 if (STR_IN_SET(field
, "AppArmorProfile", "SmackProcessLabel")) {
941 r
= sd_bus_message_append(m
, "(sv)", field
, "(bs)", ignore
, s
);
943 return bus_log_create_error(r
);
948 if (STR_IN_SET(field
, "CapabilityBoundingSet", "AmbientCapabilities")) {
958 r
= capability_set_from_string(p
, &sum
);
960 return log_error_errno(r
, "Failed to parse %s value %s: %m", field
, eq
);
962 sum
= invert
? ~sum
: sum
;
964 r
= sd_bus_message_append(m
, "(sv)", field
, "t", sum
);
966 return bus_log_create_error(r
);
971 if (streq(field
, "CPUAffinity")) {
972 _cleanup_cpu_free_ cpu_set_t
*cpuset
= NULL
;
974 r
= parse_cpu_set(eq
, &cpuset
);
976 return log_error_errno(r
, "Failed to parse %s value: %s", field
, eq
);
978 return bus_append_byte_array(m
, field
, cpuset
, CPU_ALLOC_SIZE(r
));
981 if (STR_IN_SET(field
, "RestrictAddressFamilies", "SystemCallFilter")) {
990 r
= sd_bus_message_open_container(m
, SD_BUS_TYPE_STRUCT
, "sv");
992 return bus_log_create_error(r
);
994 r
= sd_bus_message_append_basic(m
, SD_BUS_TYPE_STRING
, field
);
996 return bus_log_create_error(r
);
998 r
= sd_bus_message_open_container(m
, 'v', "(bas)");
1000 return bus_log_create_error(r
);
1002 r
= sd_bus_message_open_container(m
, 'r', "bas");
1004 return bus_log_create_error(r
);
1006 r
= sd_bus_message_append_basic(m
, 'b', &whitelist
);
1008 return bus_log_create_error(r
);
1010 r
= sd_bus_message_open_container(m
, 'a', "s");
1012 return bus_log_create_error(r
);
1015 _cleanup_free_
char *word
= NULL
;
1017 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
);
1023 return log_error_errno(r
, "Invalid syntax: %s", eq
);
1025 r
= sd_bus_message_append_basic(m
, 's', word
);
1027 return bus_log_create_error(r
);
1030 r
= sd_bus_message_close_container(m
);
1032 return bus_log_create_error(r
);
1034 r
= sd_bus_message_close_container(m
);
1036 return bus_log_create_error(r
);
1038 r
= sd_bus_message_close_container(m
);
1040 return bus_log_create_error(r
);
1042 r
= sd_bus_message_close_container(m
);
1044 return bus_log_create_error(r
);
1049 if (streq(field
, "RestrictNamespaces")) {
1050 bool invert
= false;
1051 unsigned long flags
;
1053 r
= parse_boolean(eq
);
1057 flags
= NAMESPACE_FLAGS_ALL
;
1064 r
= namespace_flags_from_string(eq
, &flags
);
1066 return log_error_errno(r
, "Failed to parse %s value %s.", field
, eq
);
1070 flags
= (~flags
) & NAMESPACE_FLAGS_ALL
;
1072 r
= sd_bus_message_append(m
, "(sv)", field
, "t", (uint64_t) flags
);
1074 return bus_log_create_error(r
);
1079 if (STR_IN_SET(field
, "BindPaths", "BindReadOnlyPaths")) {
1082 r
= sd_bus_message_open_container(m
, SD_BUS_TYPE_STRUCT
, "sv");
1084 return bus_log_create_error(r
);
1086 r
= sd_bus_message_append_basic(m
, SD_BUS_TYPE_STRING
, field
);
1088 return bus_log_create_error(r
);
1090 r
= sd_bus_message_open_container(m
, 'v', "a(ssbt)");
1092 return bus_log_create_error(r
);
1094 r
= sd_bus_message_open_container(m
, 'a', "(ssbt)");
1096 return bus_log_create_error(r
);
1099 _cleanup_free_
char *source
= NULL
, *destination
= NULL
;
1100 char *s
= NULL
, *d
= NULL
;
1101 bool ignore_enoent
= false;
1102 uint64_t flags
= MS_REC
;
1104 r
= extract_first_word(&p
, &source
, ":" WHITESPACE
, EXTRACT_QUOTES
|EXTRACT_DONT_COALESCE_SEPARATORS
);
1106 return log_error_errno(r
, "Failed to parse argument: %m");
1112 ignore_enoent
= true;
1116 if (p
&& p
[-1] == ':') {
1117 r
= extract_first_word(&p
, &destination
, ":" WHITESPACE
, EXTRACT_QUOTES
|EXTRACT_DONT_COALESCE_SEPARATORS
);
1119 return log_error_errno(r
, "Failed to parse argument: %m");
1121 log_error("Missing argument after ':': %s", eq
);
1127 if (p
&& p
[-1] == ':') {
1128 _cleanup_free_
char *options
= NULL
;
1130 r
= extract_first_word(&p
, &options
, NULL
, EXTRACT_QUOTES
);
1132 return log_error_errno(r
, "Failed to parse argument: %m");
1134 if (isempty(options
) || streq(options
, "rbind"))
1136 else if (streq(options
, "norbind"))
1139 log_error("Unknown options: %s", eq
);
1146 r
= sd_bus_message_append(m
, "(ssbt)", s
, d
, ignore_enoent
, flags
);
1148 return bus_log_create_error(r
);
1151 r
= sd_bus_message_close_container(m
);
1153 return bus_log_create_error(r
);
1155 r
= sd_bus_message_close_container(m
);
1157 return bus_log_create_error(r
);
1159 r
= sd_bus_message_close_container(m
);
1161 return bus_log_create_error(r
);
1166 if (streq(field
, "TemporaryFileSystem")) {
1169 r
= sd_bus_message_open_container(m
, SD_BUS_TYPE_STRUCT
, "sv");
1171 return bus_log_create_error(r
);
1173 r
= sd_bus_message_append_basic(m
, SD_BUS_TYPE_STRING
, field
);
1175 return bus_log_create_error(r
);
1177 r
= sd_bus_message_open_container(m
, 'v', "a(ss)");
1179 return bus_log_create_error(r
);
1181 r
= sd_bus_message_open_container(m
, 'a', "(ss)");
1183 return bus_log_create_error(r
);
1186 _cleanup_free_
char *word
= NULL
, *path
= NULL
;
1189 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
);
1191 return log_error_errno(r
, "Failed to parse argument: %m");
1196 r
= extract_first_word(&w
, &path
, ":", EXTRACT_DONT_COALESCE_SEPARATORS
);
1198 return log_error_errno(r
, "Failed to parse argument: %m");
1200 log_error("Failed to parse argument: %s", p
);
1204 r
= sd_bus_message_append(m
, "(ss)", path
, w
);
1206 return bus_log_create_error(r
);
1209 r
= sd_bus_message_close_container(m
);
1211 return bus_log_create_error(r
);
1213 r
= sd_bus_message_close_container(m
);
1215 return bus_log_create_error(r
);
1217 r
= sd_bus_message_close_container(m
);
1219 return bus_log_create_error(r
);
1227 static int bus_append_kill_property(sd_bus_message
*m
, const char *field
, const char *eq
) {
1229 if (streq(field
, "KillMode"))
1231 return bus_append_string(m
, field
, eq
);
1233 if (STR_IN_SET(field
, "SendSIGHUP", "SendSIGKILL"))
1235 return bus_append_parse_boolean(m
, field
, eq
);
1237 if (STR_IN_SET(field
, "KillSignal", "FinalKillSignal", "WatchdogSignal"))
1239 return bus_append_signal_from_string(m
, field
, eq
);
1244 static int bus_append_mount_property(sd_bus_message
*m
, const char *field
, const char *eq
) {
1246 if (STR_IN_SET(field
, "What", "Where", "Options", "Type"))
1248 return bus_append_string(m
, field
, eq
);
1250 if (streq(field
, "TimeoutSec"))
1252 return bus_append_parse_sec_rename(m
, field
, eq
);
1254 if (streq(field
, "DirectoryMode"))
1256 return bus_append_parse_mode(m
, field
, eq
);
1258 if (STR_IN_SET(field
, "SloppyOptions", "LazyUnmount", "ForceUnmount"))
1260 return bus_append_parse_boolean(m
, field
, eq
);
1265 static int bus_append_path_property(sd_bus_message
*m
, const char *field
, const char *eq
) {
1268 if (streq(field
, "MakeDirectory"))
1270 return bus_append_parse_boolean(m
, field
, eq
);
1272 if (streq(field
, "DirectoryMode"))
1274 return bus_append_parse_mode(m
, field
, eq
);
1276 if (STR_IN_SET(field
,
1277 "PathExists", "PathExistsGlob", "PathChanged",
1278 "PathModified", "DirectoryNotEmpty")) {
1281 r
= sd_bus_message_append(m
, "(sv)", "Paths", "a(ss)", 0);
1283 r
= sd_bus_message_append(m
, "(sv)", "Paths", "a(ss)", 1, field
, eq
);
1285 return bus_log_create_error(r
);
1293 static int bus_append_service_property(sd_bus_message
*m
, const char *field
, const char *eq
) {
1296 if (STR_IN_SET(field
,
1297 "PIDFile", "Type", "Restart", "BusName", "NotifyAccess",
1298 "USBFunctionDescriptors", "USBFunctionStrings"))
1300 return bus_append_string(m
, field
, eq
);
1302 if (STR_IN_SET(field
, "PermissionsStartOnly", "RootDirectoryStartOnly", "RemainAfterExit", "GuessMainPID"))
1304 return bus_append_parse_boolean(m
, field
, eq
);
1306 if (STR_IN_SET(field
, "RestartSec", "TimeoutStartSec", "TimeoutStopSec", "RuntimeMaxSec", "WatchdogSec"))
1308 return bus_append_parse_sec_rename(m
, field
, eq
);
1310 if (streq(field
, "TimeoutSec")) {
1312 r
= bus_append_parse_sec_rename(m
, "TimeoutStartSec", eq
);
1316 return bus_append_parse_sec_rename(m
, "TimeoutStopSec", eq
);
1319 if (streq(field
, "FileDescriptorStoreMax"))
1321 return bus_append_safe_atou(m
, field
, eq
);
1323 if (STR_IN_SET(field
,
1324 "ExecStartPre", "ExecStart", "ExecStartPost",
1325 "ExecReload", "ExecStop", "ExecStopPost"))
1327 return bus_append_exec_command(m
, field
, eq
);
1329 if (STR_IN_SET(field
, "RestartPreventExitStatus", "RestartForceExitStatus", "SuccessExitStatus")) {
1330 _cleanup_free_
int *status
= NULL
, *signal
= NULL
;
1331 size_t sz_status
= 0, sz_signal
= 0;
1335 _cleanup_free_
char *word
= NULL
;
1338 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
);
1344 return log_error_errno(r
, "Invalid syntax in %s: %s", field
, eq
);
1346 r
= safe_atoi(word
, &val
);
1348 val
= signal_from_string(word
);
1350 return log_error_errno(r
, "Invalid status or signal %s in %s: %m", word
, field
);
1352 signal
= reallocarray(signal
, sz_signal
+ 1, sizeof(int));
1356 signal
[sz_signal
++] = val
;
1358 status
= reallocarray(status
, sz_status
+ 1, sizeof(int));
1362 status
[sz_status
++] = val
;
1366 r
= sd_bus_message_open_container(m
, SD_BUS_TYPE_STRUCT
, "sv");
1368 return bus_log_create_error(r
);
1370 r
= sd_bus_message_append_basic(m
, SD_BUS_TYPE_STRING
, field
);
1372 return bus_log_create_error(r
);
1374 r
= sd_bus_message_open_container(m
, 'v', "(aiai)");
1376 return bus_log_create_error(r
);
1378 r
= sd_bus_message_open_container(m
, 'r', "aiai");
1380 return bus_log_create_error(r
);
1382 r
= sd_bus_message_append_array(m
, 'i', status
, sz_status
);
1384 return bus_log_create_error(r
);
1386 r
= sd_bus_message_append_array(m
, 'i', signal
, sz_signal
);
1388 return bus_log_create_error(r
);
1390 r
= sd_bus_message_close_container(m
);
1392 return bus_log_create_error(r
);
1394 r
= sd_bus_message_close_container(m
);
1396 return bus_log_create_error(r
);
1398 r
= sd_bus_message_close_container(m
);
1400 return bus_log_create_error(r
);
1408 static int bus_append_socket_property(sd_bus_message
*m
, const char *field
, const char *eq
) {
1411 if (STR_IN_SET(field
,
1412 "Accept", "Writable", "KeepAlive", "NoDelay", "FreeBind", "Transparent", "Broadcast",
1413 "PassCredentials", "PassSecurity", "ReusePort", "RemoveOnStop", "SELinuxContextFromNet"))
1415 return bus_append_parse_boolean(m
, field
, eq
);
1417 if (STR_IN_SET(field
, "Priority", "IPTTL", "Mark"))
1419 return bus_append_safe_atoi(m
, field
, eq
);
1421 if (streq(field
, "IPTOS"))
1423 return bus_append_ip_tos_from_string(m
, field
, eq
);
1425 if (STR_IN_SET(field
, "Backlog", "MaxConnections", "MaxConnectionsPerSource", "KeepAliveProbes", "TriggerLimitBurst"))
1427 return bus_append_safe_atou(m
, field
, eq
);
1429 if (STR_IN_SET(field
, "SocketMode", "DirectoryMode"))
1431 return bus_append_parse_mode(m
, field
, eq
);
1433 if (STR_IN_SET(field
, "MessageQueueMaxMessages", "MessageQueueMessageSize"))
1435 return bus_append_safe_atoi64(m
, field
, eq
);
1437 if (STR_IN_SET(field
, "TimeoutSec", "KeepAliveTimeSec", "KeepAliveIntervalSec", "DeferAcceptSec", "TriggerLimitIntervalSec"))
1439 return bus_append_parse_sec_rename(m
, field
, eq
);
1441 if (STR_IN_SET(field
, "ReceiveBuffer", "SendBuffer", "PipeSize"))
1443 return bus_append_parse_size(m
, field
, eq
, 1024);
1445 if (STR_IN_SET(field
, "ExecStartPre", "ExecStartPost", "ExecReload", "ExecStopPost"))
1447 return bus_append_exec_command(m
, field
, eq
);
1449 if (STR_IN_SET(field
,
1450 "SmackLabel", "SmackLabelIPIn", "SmackLabelIPOut", "TCPCongestion",
1451 "BindToDevice", "BindIPv6Only", "FileDescriptorName",
1452 "SocketUser", "SocketGroup"))
1454 return bus_append_string(m
, field
, eq
);
1456 if (streq(field
, "Symlinks"))
1458 return bus_append_strv(m
, field
, eq
, EXTRACT_QUOTES
);
1460 if (streq(field
, "SocketProtocol"))
1462 return bus_append_socket_protocol_from_name(m
, field
, eq
);
1464 if (STR_IN_SET(field
,
1465 "ListenStream", "ListenDatagram", "ListenSequentialPacket", "ListenNetlink",
1466 "ListenSpecial", "ListenMessageQueue", "ListenFIFO", "ListenUSBFunction")) {
1469 r
= sd_bus_message_append(m
, "(sv)", "Listen", "a(ss)", 0);
1471 r
= sd_bus_message_append(m
, "(sv)", "Listen", "a(ss)", 1, field
+ STRLEN("Listen"), eq
);
1473 return bus_log_create_error(r
);
1480 static int bus_append_timer_property(sd_bus_message
*m
, const char *field
, const char *eq
) {
1483 if (STR_IN_SET(field
, "WakeSystem", "RemainAfterElapse", "Persistent"))
1485 return bus_append_parse_boolean(m
, field
, eq
);
1487 if (STR_IN_SET(field
, "AccuracySec", "RandomizedDelaySec"))
1489 return bus_append_parse_sec_rename(m
, field
, eq
);
1491 if (STR_IN_SET(field
,
1492 "OnActiveSec", "OnBootSec", "OnStartupSec",
1493 "OnUnitActiveSec","OnUnitInactiveSec")) {
1496 r
= sd_bus_message_append(m
, "(sv)", "TimersMonotonic", "a(st)", 0);
1499 r
= parse_sec(eq
, &t
);
1501 return log_error_errno(r
, "Failed to parse %s=%s: %m", field
, eq
);
1503 r
= sd_bus_message_append(m
, "(sv)", "TimersMonotonic", "a(st)", 1, field
, t
);
1506 return bus_log_create_error(r
);
1511 if (streq(field
, "OnCalendar")) {
1514 r
= sd_bus_message_append(m
, "(sv)", "TimersCalendar", "a(ss)", 0);
1516 r
= sd_bus_message_append(m
, "(sv)", "TimersCalendar", "a(ss)", 1, field
, eq
);
1518 return bus_log_create_error(r
);
1526 static int bus_append_unit_property(sd_bus_message
*m
, const char *field
, const char *eq
) {
1527 ConditionType t
= _CONDITION_TYPE_INVALID
;
1528 bool is_condition
= false;
1531 if (STR_IN_SET(field
,
1532 "Description", "SourcePath", "OnFailureJobMode",
1533 "JobTimeoutAction", "JobTimeoutRebootArgument",
1534 "StartLimitAction", "FailureAction", "SuccessAction",
1535 "RebootArgument", "CollectMode"))
1537 return bus_append_string(m
, field
, eq
);
1539 if (STR_IN_SET(field
,
1540 "StopWhenUnneeded", "RefuseManualStart", "RefuseManualStop",
1541 "AllowIsolate", "IgnoreOnIsolate", "DefaultDependencies"))
1543 return bus_append_parse_boolean(m
, field
, eq
);
1545 if (STR_IN_SET(field
, "JobTimeoutSec", "JobRunningTimeoutSec", "StartLimitIntervalSec"))
1547 return bus_append_parse_sec_rename(m
, field
, eq
);
1549 if (streq(field
, "StartLimitBurst"))
1551 return bus_append_safe_atou(m
, field
, eq
);
1553 if (unit_dependency_from_string(field
) >= 0 ||
1554 STR_IN_SET(field
, "Documentation", "RequiresMountsFor"))
1556 return bus_append_strv(m
, field
, eq
, EXTRACT_QUOTES
);
1558 t
= condition_type_from_string(field
);
1560 is_condition
= true;
1562 t
= assert_type_from_string(field
);
1565 r
= sd_bus_message_append(m
, "(sv)", is_condition
? "Conditions" : "Asserts", "a(sbbs)", 0);
1568 int trigger
, negate
;
1570 trigger
= *p
== '|';
1578 r
= sd_bus_message_append(m
, "(sv)", is_condition
? "Conditions" : "Asserts", "a(sbbs)", 1,
1579 field
, trigger
, negate
, p
);
1582 return bus_log_create_error(r
);
1590 int bus_append_unit_property_assignment(sd_bus_message
*m
, UnitType t
, const char *assignment
) {
1591 const char *eq
, *field
;
1597 eq
= strchr(assignment
, '=');
1599 log_error("Not an assignment: %s", assignment
);
1603 field
= strndupa(assignment
, eq
- assignment
);
1608 r
= bus_append_cgroup_property(m
, field
, eq
);
1612 r
= bus_append_execute_property(m
, field
, eq
);
1616 r
= bus_append_kill_property(m
, field
, eq
);
1620 r
= bus_append_service_property(m
, field
, eq
);
1626 r
= bus_append_cgroup_property(m
, field
, eq
);
1630 r
= bus_append_execute_property(m
, field
, eq
);
1634 r
= bus_append_kill_property(m
, field
, eq
);
1638 r
= bus_append_socket_property(m
, field
, eq
);
1644 r
= bus_append_timer_property(m
, field
, eq
);
1650 r
= bus_append_path_property(m
, field
, eq
);
1656 r
= bus_append_cgroup_property(m
, field
, eq
);
1663 if (streq(field
, "TimeoutStopSec"))
1664 return bus_append_parse_sec_rename(m
, field
, eq
);
1666 r
= bus_append_cgroup_property(m
, field
, eq
);
1670 r
= bus_append_kill_property(m
, field
, eq
);
1676 r
= bus_append_cgroup_property(m
, field
, eq
);
1680 r
= bus_append_execute_property(m
, field
, eq
);
1684 r
= bus_append_kill_property(m
, field
, eq
);
1688 r
= bus_append_mount_property(m
, field
, eq
);
1694 case UNIT_AUTOMOUNT
:
1695 r
= bus_append_automount_property(m
, field
, eq
);
1704 log_error("Not supported unit type");
1708 log_error("Invalid unit type");
1712 r
= bus_append_unit_property(m
, field
, eq
);
1716 log_error("Unknown assignment: %s", assignment
);
1720 int bus_append_unit_property_assignment_many(sd_bus_message
*m
, UnitType t
, char **l
) {
1726 STRV_FOREACH(i
, l
) {
1727 r
= bus_append_unit_property_assignment(m
, t
, *i
);
1735 typedef struct BusWaitForJobs
{
1742 sd_bus_slot
*slot_job_removed
;
1743 sd_bus_slot
*slot_disconnected
;
1746 static int match_disconnected(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1749 log_error("Warning! D-Bus connection terminated.");
1750 sd_bus_close(sd_bus_message_get_bus(m
));
1755 static int match_job_removed(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1756 const char *path
, *unit
, *result
;
1757 BusWaitForJobs
*d
= userdata
;
1765 r
= sd_bus_message_read(m
, "uoss", &id
, &path
, &unit
, &result
);
1767 bus_log_parse_error(r
);
1771 found
= set_remove(d
->jobs
, (char*) path
);
1777 if (!isempty(result
))
1778 d
->result
= strdup(result
);
1781 d
->name
= strdup(unit
);
1786 void bus_wait_for_jobs_free(BusWaitForJobs
*d
) {
1790 set_free_free(d
->jobs
);
1792 sd_bus_slot_unref(d
->slot_disconnected
);
1793 sd_bus_slot_unref(d
->slot_job_removed
);
1795 sd_bus_unref(d
->bus
);
1803 int bus_wait_for_jobs_new(sd_bus
*bus
, BusWaitForJobs
**ret
) {
1804 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*d
= NULL
;
1810 d
= new0(BusWaitForJobs
, 1);
1814 d
->bus
= sd_bus_ref(bus
);
1816 /* When we are a bus client we match by sender. Direct
1817 * connections OTOH have no initialized sender field, and
1818 * hence we ignore the sender then */
1819 r
= sd_bus_match_signal_async(
1821 &d
->slot_job_removed
,
1822 bus
->bus_client
? "org.freedesktop.systemd1" : NULL
,
1823 "/org/freedesktop/systemd1",
1824 "org.freedesktop.systemd1.Manager",
1826 match_job_removed
, NULL
, d
);
1830 r
= sd_bus_match_signal_async(
1832 &d
->slot_disconnected
,
1833 "org.freedesktop.DBus.Local",
1835 "org.freedesktop.DBus.Local",
1837 match_disconnected
, NULL
, d
);
1846 static int bus_process_wait(sd_bus
*bus
) {
1850 r
= sd_bus_process(bus
, NULL
);
1856 r
= sd_bus_wait(bus
, (uint64_t) -1);
1862 static int bus_job_get_service_result(BusWaitForJobs
*d
, char **result
) {
1863 _cleanup_free_
char *dbus_path
= NULL
;
1869 if (!endswith(d
->name
, ".service"))
1872 dbus_path
= unit_dbus_path_from_name(d
->name
);
1876 return sd_bus_get_property_string(d
->bus
,
1877 "org.freedesktop.systemd1",
1879 "org.freedesktop.systemd1.Service",
1885 static const struct {
1886 const char *result
, *explanation
;
1887 } explanations
[] = {
1888 { "resources", "of unavailable resources or another system error" },
1889 { "protocol", "the service did not take the steps required by its unit configuration" },
1890 { "timeout", "a timeout was exceeded" },
1891 { "exit-code", "the control process exited with error code" },
1892 { "signal", "a fatal signal was delivered to the control process" },
1893 { "core-dump", "a fatal signal was delivered causing the control process to dump core" },
1894 { "watchdog", "the service failed to send watchdog ping" },
1895 { "start-limit", "start of the service was attempted too often" }
1898 static void log_job_error_with_service_result(const char* service
, const char *result
, const char* const* extra_args
) {
1899 _cleanup_free_
char *service_shell_quoted
= NULL
;
1900 const char *systemctl
= "systemctl", *journalctl
= "journalctl";
1904 service_shell_quoted
= shell_maybe_quote(service
, ESCAPE_BACKSLASH
);
1906 if (!strv_isempty((char**) extra_args
)) {
1907 _cleanup_free_
char *t
;
1909 t
= strv_join((char**) extra_args
, " ");
1910 systemctl
= strjoina("systemctl ", t
? : "<args>");
1911 journalctl
= strjoina("journalctl ", t
? : "<args>");
1914 if (!isempty(result
)) {
1917 for (i
= 0; i
< ELEMENTSOF(explanations
); ++i
)
1918 if (streq(result
, explanations
[i
].result
))
1921 if (i
< ELEMENTSOF(explanations
)) {
1922 log_error("Job for %s failed because %s.\n"
1923 "See \"%s status %s\" and \"%s -xe\" for details.\n",
1925 explanations
[i
].explanation
,
1927 service_shell_quoted
?: "<service>",
1933 log_error("Job for %s failed.\n"
1934 "See \"%s status %s\" and \"%s -xe\" for details.\n",
1937 service_shell_quoted
?: "<service>",
1941 /* For some results maybe additional explanation is required */
1942 if (streq_ptr(result
, "start-limit"))
1943 log_info("To force a start use \"%1$s reset-failed %2$s\"\n"
1944 "followed by \"%1$s start %2$s\" again.",
1946 service_shell_quoted
?: "<service>");
1949 static int check_wait_response(BusWaitForJobs
*d
, bool quiet
, const char* const* extra_args
) {
1953 if (streq(d
->result
, "canceled"))
1954 log_error("Job for %s canceled.", strna(d
->name
));
1955 else if (streq(d
->result
, "timeout"))
1956 log_error("Job for %s timed out.", strna(d
->name
));
1957 else if (streq(d
->result
, "dependency"))
1958 log_error("A dependency job for %s failed. See 'journalctl -xe' for details.", strna(d
->name
));
1959 else if (streq(d
->result
, "invalid"))
1960 log_error("%s is not active, cannot reload.", strna(d
->name
));
1961 else if (streq(d
->result
, "assert"))
1962 log_error("Assertion failed on job for %s.", strna(d
->name
));
1963 else if (streq(d
->result
, "unsupported"))
1964 log_error("Operation on or unit type of %s not supported on this system.", strna(d
->name
));
1965 else if (streq(d
->result
, "collected"))
1966 log_error("Queued job for %s was garbage collected.", strna(d
->name
));
1967 else if (streq(d
->result
, "once"))
1968 log_error("Unit %s was started already once and can't be started again.", strna(d
->name
));
1969 else if (!STR_IN_SET(d
->result
, "done", "skipped")) {
1971 _cleanup_free_
char *result
= NULL
;
1974 q
= bus_job_get_service_result(d
, &result
);
1976 log_debug_errno(q
, "Failed to get Result property of unit %s: %m", d
->name
);
1978 log_job_error_with_service_result(d
->name
, result
, extra_args
);
1980 log_error("Job failed. See \"journalctl -xe\" for details.");
1984 if (STR_IN_SET(d
->result
, "canceled", "collected"))
1986 else if (streq(d
->result
, "timeout"))
1988 else if (streq(d
->result
, "dependency"))
1990 else if (streq(d
->result
, "invalid"))
1992 else if (streq(d
->result
, "assert"))
1994 else if (streq(d
->result
, "unsupported"))
1996 else if (streq(d
->result
, "once"))
1998 else if (STR_IN_SET(d
->result
, "done", "skipped"))
2001 log_debug("Unexpected job result, assuming server side newer than us: %s", d
->result
);
2005 int bus_wait_for_jobs(BusWaitForJobs
*d
, bool quiet
, const char* const* extra_args
) {
2010 while (!set_isempty(d
->jobs
)) {
2013 q
= bus_process_wait(d
->bus
);
2015 return log_error_errno(q
, "Failed to wait for response: %m");
2018 q
= check_wait_response(d
, quiet
, extra_args
);
2019 /* Return the first error as it is most likely to be
2021 if (q
< 0 && r
== 0)
2024 log_debug_errno(q
, "Got result %s/%m for job %s", strna(d
->result
), strna(d
->name
));
2027 d
->name
= mfree(d
->name
);
2028 d
->result
= mfree(d
->result
);
2034 int bus_wait_for_jobs_add(BusWaitForJobs
*d
, const char *path
) {
2039 r
= set_ensure_allocated(&d
->jobs
, &string_hash_ops
);
2043 return set_put_strdup(d
->jobs
, path
);
2046 int bus_wait_for_jobs_one(BusWaitForJobs
*d
, const char *path
, bool quiet
) {
2049 r
= bus_wait_for_jobs_add(d
, path
);
2053 return bus_wait_for_jobs(d
, quiet
, NULL
);
2056 int bus_deserialize_and_dump_unit_file_changes(sd_bus_message
*m
, bool quiet
, UnitFileChange
**changes
, size_t *n_changes
) {
2057 const char *type
, *path
, *source
;
2060 /* changes is dereferenced when calling unit_file_dump_changes() later,
2061 * so we have to make sure this is not NULL. */
2065 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_ARRAY
, "(sss)");
2067 return bus_log_parse_error(r
);
2069 while ((r
= sd_bus_message_read(m
, "(sss)", &type
, &path
, &source
)) > 0) {
2070 /* We expect only "success" changes to be sent over the bus.
2071 Hence, reject anything negative. */
2072 UnitFileChangeType ch
= unit_file_change_type_from_string(type
);
2075 log_notice("Manager reported unknown change type \"%s\" for path \"%s\", ignoring.", type
, path
);
2079 r
= unit_file_changes_add(changes
, n_changes
, ch
, path
, source
);
2084 return bus_log_parse_error(r
);
2086 r
= sd_bus_message_exit_container(m
);
2088 return bus_log_parse_error(r
);
2090 unit_file_dump_changes(0, NULL
, *changes
, *n_changes
, quiet
);
2096 bool is_const
; /* If false, cgroup_path should be free()'d */
2098 Hashmap
*pids
; /* PID → process name */
2101 struct CGroupInfo
*parent
;
2102 LIST_FIELDS(struct CGroupInfo
, siblings
);
2103 LIST_HEAD(struct CGroupInfo
, children
);
2107 static bool IS_ROOT(const char *p
) {
2108 return isempty(p
) || streq(p
, "/");
2111 static int add_cgroup(Hashmap
*cgroups
, const char *path
, bool is_const
, struct CGroupInfo
**ret
) {
2112 struct CGroupInfo
*parent
= NULL
, *cg
;
2121 cg
= hashmap_get(cgroups
, path
);
2127 if (!IS_ROOT(path
)) {
2130 e
= strrchr(path
, '/');
2134 pp
= strndupa(path
, e
- path
);
2138 r
= add_cgroup(cgroups
, pp
, false, &parent
);
2143 cg
= new0(struct CGroupInfo
, 1);
2148 cg
->cgroup_path
= (char*) path
;
2150 cg
->cgroup_path
= strdup(path
);
2151 if (!cg
->cgroup_path
) {
2157 cg
->is_const
= is_const
;
2158 cg
->parent
= parent
;
2160 r
= hashmap_put(cgroups
, cg
->cgroup_path
, cg
);
2163 free(cg
->cgroup_path
);
2169 LIST_PREPEND(siblings
, parent
->children
, cg
);
2170 parent
->n_children
++;
2177 static int add_process(
2183 struct CGroupInfo
*cg
;
2190 r
= add_cgroup(cgroups
, path
, true, &cg
);
2194 r
= hashmap_ensure_allocated(&cg
->pids
, &trivial_hash_ops
);
2198 return hashmap_put(cg
->pids
, PID_TO_PTR(pid
), (void*) name
);
2201 static void remove_cgroup(Hashmap
*cgroups
, struct CGroupInfo
*cg
) {
2205 while (cg
->children
)
2206 remove_cgroup(cgroups
, cg
->children
);
2208 hashmap_remove(cgroups
, cg
->cgroup_path
);
2211 free(cg
->cgroup_path
);
2213 hashmap_free(cg
->pids
);
2216 LIST_REMOVE(siblings
, cg
->parent
->children
, cg
);
2221 static int cgroup_info_compare_func(struct CGroupInfo
* const *a
, struct CGroupInfo
* const *b
) {
2222 return strcmp((*a
)->cgroup_path
, (*b
)->cgroup_path
);
2225 static int dump_processes(
2227 const char *cgroup_path
,
2230 OutputFlags flags
) {
2232 struct CGroupInfo
*cg
;
2237 if (IS_ROOT(cgroup_path
))
2240 cg
= hashmap_get(cgroups
, cgroup_path
);
2244 if (!hashmap_isempty(cg
->pids
)) {
2252 /* Order processes by their PID */
2253 pids
= newa(pid_t
, hashmap_size(cg
->pids
));
2255 HASHMAP_FOREACH_KEY(name
, pidp
, cg
->pids
, j
)
2256 pids
[n
++] = PTR_TO_PID(pidp
);
2258 assert(n
== hashmap_size(cg
->pids
));
2259 typesafe_qsort(pids
, n
, pid_compare_func
);
2261 width
= DECIMAL_STR_WIDTH(pids
[n
-1]);
2263 for (i
= 0; i
< n
; i
++) {
2264 _cleanup_free_
char *e
= NULL
;
2265 const char *special
;
2268 name
= hashmap_get(cg
->pids
, PID_TO_PTR(pids
[i
]));
2271 if (n_columns
!= 0) {
2274 k
= MAX(LESS_BY(n_columns
, 2U + width
+ 1U), 20U);
2276 e
= ellipsize(name
, k
, 100);
2281 more
= i
+1 < n
|| cg
->children
;
2282 special
= special_glyph(more
? TREE_BRANCH
: TREE_RIGHT
);
2284 fprintf(stdout
, "%s%s%*"PID_PRI
" %s\n",
2293 struct CGroupInfo
**children
, *child
;
2296 /* Order subcgroups by their name */
2297 children
= newa(struct CGroupInfo
*, cg
->n_children
);
2298 LIST_FOREACH(siblings
, child
, cg
->children
)
2299 children
[n
++] = child
;
2300 assert(n
== cg
->n_children
);
2301 typesafe_qsort(children
, n
, cgroup_info_compare_func
);
2304 n_columns
= MAX(LESS_BY(n_columns
, 2U), 20U);
2306 for (i
= 0; i
< n
; i
++) {
2307 _cleanup_free_
char *pp
= NULL
;
2308 const char *name
, *special
;
2311 child
= children
[i
];
2313 name
= strrchr(child
->cgroup_path
, '/');
2319 special
= special_glyph(more
? TREE_BRANCH
: TREE_RIGHT
);
2321 fputs(prefix
, stdout
);
2322 fputs(special
, stdout
);
2323 fputs(name
, stdout
);
2324 fputc('\n', stdout
);
2326 special
= special_glyph(more
? TREE_VERTICAL
: TREE_SPACE
);
2328 pp
= strappend(prefix
, special
);
2332 r
= dump_processes(cgroups
, child
->cgroup_path
, pp
, n_columns
, flags
);
2342 static int dump_extra_processes(
2346 OutputFlags flags
) {
2348 _cleanup_free_ pid_t
*pids
= NULL
;
2349 _cleanup_hashmap_free_ Hashmap
*names
= NULL
;
2350 struct CGroupInfo
*cg
;
2351 size_t n_allocated
= 0, n
= 0, k
;
2355 /* Prints the extra processes, i.e. those that are in cgroups we haven't displayed yet. We show them as
2356 * combined, sorted, linear list. */
2358 HASHMAP_FOREACH(cg
, cgroups
, i
) {
2366 if (hashmap_isempty(cg
->pids
))
2369 r
= hashmap_ensure_allocated(&names
, &trivial_hash_ops
);
2373 if (!GREEDY_REALLOC(pids
, n_allocated
, n
+ hashmap_size(cg
->pids
)))
2376 HASHMAP_FOREACH_KEY(name
, pidp
, cg
->pids
, j
) {
2377 pids
[n
++] = PTR_TO_PID(pidp
);
2379 r
= hashmap_put(names
, pidp
, (void*) name
);
2388 typesafe_qsort(pids
, n
, pid_compare_func
);
2389 width
= DECIMAL_STR_WIDTH(pids
[n
-1]);
2391 for (k
= 0; k
< n
; k
++) {
2392 _cleanup_free_
char *e
= NULL
;
2395 name
= hashmap_get(names
, PID_TO_PTR(pids
[k
]));
2398 if (n_columns
!= 0) {
2401 z
= MAX(LESS_BY(n_columns
, 2U + width
+ 1U), 20U);
2403 e
= ellipsize(name
, z
, 100);
2408 fprintf(stdout
, "%s%s %*" PID_PRI
" %s\n",
2410 special_glyph(TRIANGULAR_BULLET
),
2418 int unit_show_processes(
2421 const char *cgroup_path
,
2425 sd_bus_error
*error
) {
2427 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
2428 Hashmap
*cgroups
= NULL
;
2429 struct CGroupInfo
*cg
;
2435 if (flags
& OUTPUT_FULL_WIDTH
)
2437 else if (n_columns
<= 0)
2438 n_columns
= columns();
2440 prefix
= strempty(prefix
);
2442 r
= sd_bus_call_method(
2444 "org.freedesktop.systemd1",
2445 "/org/freedesktop/systemd1",
2446 "org.freedesktop.systemd1.Manager",
2455 cgroups
= hashmap_new(&path_hash_ops
);
2459 r
= sd_bus_message_enter_container(reply
, 'a', "(sus)");
2464 const char *path
= NULL
, *name
= NULL
;
2467 r
= sd_bus_message_read(reply
, "(sus)", &path
, &pid
, &name
);
2473 r
= add_process(cgroups
, path
, pid
, name
);
2478 r
= sd_bus_message_exit_container(reply
);
2482 r
= dump_processes(cgroups
, cgroup_path
, prefix
, n_columns
, flags
);
2486 r
= dump_extra_processes(cgroups
, prefix
, n_columns
, flags
);
2489 while ((cg
= hashmap_first(cgroups
)))
2490 remove_cgroup(cgroups
, cg
);
2492 hashmap_free(cgroups
);
2497 int unit_load_state(sd_bus
*bus
, const char *name
, char **load_state
) {
2498 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2499 _cleanup_free_
char *path
= NULL
;
2502 path
= unit_dbus_path_from_name(name
);
2506 /* This function warns on it's own, because otherwise it'd be awkward to pass
2507 * the dbus error message around. */
2509 r
= sd_bus_get_property_string(
2511 "org.freedesktop.systemd1",
2513 "org.freedesktop.systemd1.Unit",
2518 return log_error_errno(r
, "Failed to get load state of %s: %s", name
, bus_error_message(&error
, r
));