1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2016 Lennart Poettering
8 #include "alloc-util.h"
9 #include "bus-internal.h"
10 #include "bus-unit-util.h"
13 #include "cgroup-util.h"
14 #include "condition.h"
15 #include "cpu-set-util.h"
17 #include "errno-list.h"
20 #include "hexdecoct.h"
21 #include "hostname-util.h"
22 #include "in-addr-util.h"
24 #include "locale-util.h"
25 #include "mount-util.h"
27 #include "parse-util.h"
28 #include "path-util.h"
29 #include "process-util.h"
30 #include "rlimit-util.h"
31 #include "securebits-util.h"
32 #include "signal-util.h"
33 #include "socket-protocol-list.h"
34 #include "string-util.h"
35 #include "syslog-util.h"
36 #include "terminal-util.h"
38 #include "user-util.h"
42 int bus_parse_unit_info(sd_bus_message
*message
, UnitInfo
*u
) {
48 return sd_bus_message_read(
63 #define DEFINE_BUS_APPEND_PARSE_PTR(bus_type, cast_type, type, parse_func) \
64 static int bus_append_##parse_func( \
71 r = parse_func(eq, &val); \
73 return log_error_errno(r, "Failed to parse %s=%s: %m", field, eq); \
75 r = sd_bus_message_append(m, "(sv)", field, \
76 bus_type, (cast_type) val); \
78 return bus_log_create_error(r); \
83 #define DEFINE_BUS_APPEND_PARSE(bus_type, parse_func) \
84 static int bus_append_##parse_func( \
92 log_error("Failed to parse %s: %s", field, eq); \
96 r = sd_bus_message_append(m, "(sv)", field, \
97 bus_type, (int32_t) r); \
99 return bus_log_create_error(r); \
104 DEFINE_BUS_APPEND_PARSE("b", parse_boolean
);
105 DEFINE_BUS_APPEND_PARSE("i", ioprio_class_from_string
);
106 DEFINE_BUS_APPEND_PARSE("i", ip_tos_from_string
);
107 DEFINE_BUS_APPEND_PARSE("i", log_facility_unshifted_from_string
);
108 DEFINE_BUS_APPEND_PARSE("i", log_level_from_string
);
109 DEFINE_BUS_APPEND_PARSE("i", parse_errno
);
110 DEFINE_BUS_APPEND_PARSE("i", sched_policy_from_string
);
111 DEFINE_BUS_APPEND_PARSE("i", secure_bits_from_string
);
112 DEFINE_BUS_APPEND_PARSE("i", signal_from_string_try_harder
);
113 DEFINE_BUS_APPEND_PARSE("i", socket_protocol_from_name
);
114 DEFINE_BUS_APPEND_PARSE_PTR("i", int32_t, int, ioprio_parse_priority
);
115 DEFINE_BUS_APPEND_PARSE_PTR("i", int32_t, int, parse_nice
);
116 DEFINE_BUS_APPEND_PARSE_PTR("i", int32_t, int, safe_atoi
);
117 DEFINE_BUS_APPEND_PARSE_PTR("t", uint64_t, nsec_t
, parse_nsec
);
118 DEFINE_BUS_APPEND_PARSE_PTR("t", uint64_t, uint64_t, cg_blkio_weight_parse
);
119 DEFINE_BUS_APPEND_PARSE_PTR("t", uint64_t, uint64_t, cg_cpu_shares_parse
);
120 DEFINE_BUS_APPEND_PARSE_PTR("t", uint64_t, uint64_t, cg_weight_parse
);
121 DEFINE_BUS_APPEND_PARSE_PTR("t", uint64_t, unsigned long, mount_propagation_flags_from_string
);
122 DEFINE_BUS_APPEND_PARSE_PTR("t", uint64_t, uint64_t, safe_atou64
);
123 DEFINE_BUS_APPEND_PARSE_PTR("u", uint32_t, mode_t
, parse_mode
);
124 DEFINE_BUS_APPEND_PARSE_PTR("u", uint32_t, unsigned, safe_atou
);
125 DEFINE_BUS_APPEND_PARSE_PTR("x", int64_t, int64_t, safe_atoi64
);
127 static inline int bus_append_string(sd_bus_message
*m
, const char *field
, const char *eq
) {
130 r
= sd_bus_message_append(m
, "(sv)", field
, "s", eq
);
132 return bus_log_create_error(r
);
137 static int bus_append_strv(sd_bus_message
*m
, const char *field
, const char *eq
, ExtractFlags flags
) {
141 r
= sd_bus_message_open_container(m
, 'r', "sv");
143 return bus_log_create_error(r
);
145 r
= sd_bus_message_append_basic(m
, 's', field
);
147 return bus_log_create_error(r
);
149 r
= sd_bus_message_open_container(m
, 'v', "as");
151 return bus_log_create_error(r
);
153 r
= sd_bus_message_open_container(m
, 'a', "s");
155 return bus_log_create_error(r
);
158 _cleanup_free_
char *word
= NULL
;
160 r
= extract_first_word(&p
, &word
, NULL
, flags
);
166 return log_error_errno(r
, "Invalid syntax: %s", eq
);
168 r
= sd_bus_message_append_basic(m
, 's', word
);
170 return bus_log_create_error(r
);
173 r
= sd_bus_message_close_container(m
);
175 return bus_log_create_error(r
);
177 r
= sd_bus_message_close_container(m
);
179 return bus_log_create_error(r
);
181 r
= sd_bus_message_close_container(m
);
183 return bus_log_create_error(r
);
188 static int bus_append_byte_array(sd_bus_message
*m
, const char *field
, const void *buf
, size_t n
) {
191 r
= sd_bus_message_open_container(m
, SD_BUS_TYPE_STRUCT
, "sv");
193 return bus_log_create_error(r
);
195 r
= sd_bus_message_append_basic(m
, SD_BUS_TYPE_STRING
, field
);
197 return bus_log_create_error(r
);
199 r
= sd_bus_message_open_container(m
, 'v', "ay");
201 return bus_log_create_error(r
);
203 r
= sd_bus_message_append_array(m
, 'y', buf
, n
);
205 return bus_log_create_error(r
);
207 r
= sd_bus_message_close_container(m
);
209 return bus_log_create_error(r
);
211 r
= sd_bus_message_close_container(m
);
213 return bus_log_create_error(r
);
218 static int bus_append_parse_sec_rename(sd_bus_message
*m
, const char *field
, const char *eq
) {
224 r
= parse_sec(eq
, &t
);
226 return log_error_errno(r
, "Failed to parse %s=%s: %m", field
, eq
);
229 n
= newa(char, l
+ 2);
230 /* Change suffix Sec → USec */
231 strcpy(mempcpy(n
, field
, l
- 3), "USec");
233 r
= sd_bus_message_append(m
, "(sv)", n
, "t", t
);
235 return bus_log_create_error(r
);
240 static int bus_append_parse_size(sd_bus_message
*m
, const char *field
, const char *eq
, uint64_t base
) {
244 r
= parse_size(eq
, base
, &v
);
246 return log_error_errno(r
, "Failed to parse %s=%s: %m", field
, eq
);
248 r
= sd_bus_message_append(m
, "(sv)", field
, "t", v
);
250 return bus_log_create_error(r
);
255 static int bus_append_exec_command(sd_bus_message
*m
, const char *field
, const char *eq
) {
256 bool ignore_failure
= false, explicit_path
= false, done
= false;
257 _cleanup_strv_free_
char **l
= NULL
;
258 _cleanup_free_
char *path
= NULL
;
268 ignore_failure
= true;
277 explicit_path
= true;
284 /* The bus API doesn't support +, ! and !! currently, unfortunately. :-( */
285 log_error("Sorry, but +, ! and !! are currently not supported for transient services.");
295 r
= extract_first_word(&eq
, &path
, NULL
, EXTRACT_QUOTES
|EXTRACT_CUNESCAPE
);
297 return log_error_errno(r
, "Failed to parse path: %m");
300 r
= strv_split_extract(&l
, eq
, NULL
, EXTRACT_QUOTES
|EXTRACT_CUNESCAPE
);
302 return log_error_errno(r
, "Failed to parse command line: %m");
304 r
= sd_bus_message_open_container(m
, SD_BUS_TYPE_STRUCT
, "sv");
306 return bus_log_create_error(r
);
308 r
= sd_bus_message_append_basic(m
, SD_BUS_TYPE_STRING
, field
);
310 return bus_log_create_error(r
);
312 r
= sd_bus_message_open_container(m
, 'v', "a(sasb)");
314 return bus_log_create_error(r
);
316 r
= sd_bus_message_open_container(m
, 'a', "(sasb)");
318 return bus_log_create_error(r
);
320 if (!strv_isempty(l
)) {
322 r
= sd_bus_message_open_container(m
, 'r', "sasb");
324 return bus_log_create_error(r
);
326 r
= sd_bus_message_append(m
, "s", path
?: l
[0]);
328 return bus_log_create_error(r
);
330 r
= sd_bus_message_append_strv(m
, l
);
332 return bus_log_create_error(r
);
334 r
= sd_bus_message_append(m
, "b", ignore_failure
);
336 return bus_log_create_error(r
);
338 r
= sd_bus_message_close_container(m
);
340 return bus_log_create_error(r
);
343 r
= sd_bus_message_close_container(m
);
345 return bus_log_create_error(r
);
347 r
= sd_bus_message_close_container(m
);
349 return bus_log_create_error(r
);
351 r
= sd_bus_message_close_container(m
);
353 return bus_log_create_error(r
);
358 static int bus_append_ip_address_access(sd_bus_message
*m
, int family
, const union in_addr_union
*prefix
, unsigned char prefixlen
) {
364 r
= sd_bus_message_open_container(m
, 'r', "iayu");
368 r
= sd_bus_message_append(m
, "i", family
);
372 r
= sd_bus_message_append_array(m
, 'y', prefix
, FAMILY_ADDRESS_SIZE(family
));
376 r
= sd_bus_message_append(m
, "u", prefixlen
);
380 return sd_bus_message_close_container(m
);
383 static int bus_append_cgroup_property(sd_bus_message
*m
, const char *field
, const char *eq
) {
386 if (STR_IN_SET(field
, "DevicePolicy", "Slice"))
388 return bus_append_string(m
, field
, eq
);
390 if (STR_IN_SET(field
,
391 "CPUAccounting", "MemoryAccounting", "IOAccounting", "BlockIOAccounting",
392 "TasksAccounting", "IPAccounting"))
394 return bus_append_parse_boolean(m
, field
, eq
);
396 if (STR_IN_SET(field
, "CPUWeight", "StartupCPUWeight", "IOWeight", "StartupIOWeight"))
398 return bus_append_cg_weight_parse(m
, field
, eq
);
400 if (STR_IN_SET(field
, "CPUShares", "StartupCPUShares"))
402 return bus_append_cg_cpu_shares_parse(m
, field
, eq
);
404 if (STR_IN_SET(field
, "BlockIOWeight", "StartupBlockIOWeight"))
406 return bus_append_cg_blkio_weight_parse(m
, field
, eq
);
408 if (streq(field
, "Delegate")) {
410 r
= parse_boolean(eq
);
412 return bus_append_strv(m
, "DelegateControllers", eq
, EXTRACT_QUOTES
);
414 r
= sd_bus_message_append(m
, "(sv)", "Delegate", "b", r
);
416 return bus_log_create_error(r
);
421 if (STR_IN_SET(field
, "MemoryLow", "MemoryHigh", "MemoryMax", "MemorySwapMax", "MemoryLimit", "TasksMax")) {
423 if (isempty(eq
) || streq(eq
, "infinity")) {
424 r
= sd_bus_message_append(m
, "(sv)", field
, "t", CGROUP_LIMIT_MAX
);
426 return bus_log_create_error(r
);
430 r
= parse_percent(eq
);
434 /* When this is a percentage we'll convert this into a relative value in the range
435 * 0…UINT32_MAX and pass it in the MemoryLowScale property (and related
436 * ones). This way the physical memory size can be determined server-side */
438 n
= strjoina(field
, "Scale");
439 r
= sd_bus_message_append(m
, "(sv)", n
, "u", (uint32_t) (((uint64_t) UINT32_MAX
* r
) / 100U));
441 return bus_log_create_error(r
);
446 if (streq(field
, "TasksMax"))
447 return bus_append_safe_atou64(m
, field
, eq
);
449 return bus_append_parse_size(m
, field
, eq
, 1024);
452 if (streq(field
, "CPUQuota")) {
455 r
= sd_bus_message_append(m
, "(sv)", "CPUQuotaPerSecUSec", "t", USEC_INFINITY
);
457 r
= parse_percent_unbounded(eq
);
459 log_error_errno(r
, "CPU quota '%s' invalid.", eq
);
463 r
= sd_bus_message_append(m
, "(sv)", "CPUQuotaPerSecUSec", "t", (usec_t
) r
* USEC_PER_SEC
/ 100U);
467 return bus_log_create_error(r
);
472 if (streq(field
, "DeviceAllow")) {
475 r
= sd_bus_message_append(m
, "(sv)", field
, "a(ss)", 0);
477 const char *path
= eq
, *rwm
= NULL
, *e
;
481 path
= strndupa(eq
, e
- eq
);
485 r
= sd_bus_message_append(m
, "(sv)", field
, "a(ss)", 1, path
, strempty(rwm
));
489 return bus_log_create_error(r
);
494 if (cgroup_io_limit_type_from_string(field
) >= 0 || STR_IN_SET(field
, "BlockIOReadBandwidth", "BlockIOWriteBandwidth")) {
497 r
= sd_bus_message_append(m
, "(sv)", field
, "a(st)", 0);
499 const char *path
, *bandwidth
, *e
;
504 log_error("Failed to parse %s value %s.", field
, eq
);
508 path
= strndupa(eq
, e
- eq
);
511 if (streq(bandwidth
, "infinity")) {
512 bytes
= CGROUP_LIMIT_MAX
;
514 r
= parse_size(bandwidth
, 1000, &bytes
);
516 return log_error_errno(r
, "Failed to parse byte value %s: %m", bandwidth
);
519 r
= sd_bus_message_append(m
, "(sv)", field
, "a(st)", 1, path
, bytes
);
523 return bus_log_create_error(r
);
528 if (STR_IN_SET(field
, "IODeviceWeight", "BlockIODeviceWeight")) {
531 r
= sd_bus_message_append(m
, "(sv)", field
, "a(st)", 0);
533 const char *path
, *weight
, *e
;
538 log_error("Failed to parse %s value %s.", field
, eq
);
542 path
= strndupa(eq
, e
- eq
);
545 r
= safe_atou64(weight
, &u
);
547 return log_error_errno(r
, "Failed to parse %s value %s: %m", field
, weight
);
549 r
= sd_bus_message_append(m
, "(sv)", field
, "a(st)", 1, path
, u
);
553 return bus_log_create_error(r
);
558 if (STR_IN_SET(field
, "IPAddressAllow", "IPAddressDeny")) {
559 unsigned char prefixlen
;
560 union in_addr_union prefix
= {};
564 r
= sd_bus_message_append(m
, "(sv)", field
, "a(iayu)", 0);
566 return bus_log_create_error(r
);
571 r
= sd_bus_message_open_container(m
, SD_BUS_TYPE_STRUCT
, "sv");
573 return bus_log_create_error(r
);
575 r
= sd_bus_message_append_basic(m
, SD_BUS_TYPE_STRING
, field
);
577 return bus_log_create_error(r
);
579 r
= sd_bus_message_open_container(m
, 'v', "a(iayu)");
581 return bus_log_create_error(r
);
583 r
= sd_bus_message_open_container(m
, 'a', "(iayu)");
585 return bus_log_create_error(r
);
587 if (streq(eq
, "any")) {
588 /* "any" is a shortcut for 0.0.0.0/0 and ::/0 */
590 r
= bus_append_ip_address_access(m
, AF_INET
, &prefix
, 0);
592 return bus_log_create_error(r
);
594 r
= bus_append_ip_address_access(m
, AF_INET6
, &prefix
, 0);
596 return bus_log_create_error(r
);
598 } else if (is_localhost(eq
)) {
599 /* "localhost" is a shortcut for 127.0.0.0/8 and ::1/128 */
601 prefix
.in
.s_addr
= htobe32(0x7f000000);
602 r
= bus_append_ip_address_access(m
, AF_INET
, &prefix
, 8);
604 return bus_log_create_error(r
);
606 prefix
.in6
= (struct in6_addr
) IN6ADDR_LOOPBACK_INIT
;
607 r
= bus_append_ip_address_access(m
, AF_INET6
, &prefix
, 128);
611 } else if (streq(eq
, "link-local")) {
612 /* "link-local" is a shortcut for 169.254.0.0/16 and fe80::/64 */
614 prefix
.in
.s_addr
= htobe32((UINT32_C(169) << 24 | UINT32_C(254) << 16));
615 r
= bus_append_ip_address_access(m
, AF_INET
, &prefix
, 16);
617 return bus_log_create_error(r
);
619 prefix
.in6
= (struct in6_addr
) {
620 .s6_addr32
[0] = htobe32(0xfe800000)
622 r
= bus_append_ip_address_access(m
, AF_INET6
, &prefix
, 64);
624 return bus_log_create_error(r
);
626 } else if (streq(eq
, "multicast")) {
627 /* "multicast" is a shortcut for 224.0.0.0/4 and ff00::/8 */
629 prefix
.in
.s_addr
= htobe32((UINT32_C(224) << 24));
630 r
= bus_append_ip_address_access(m
, AF_INET
, &prefix
, 4);
632 return bus_log_create_error(r
);
634 prefix
.in6
= (struct in6_addr
) {
635 .s6_addr32
[0] = htobe32(0xff000000)
637 r
= bus_append_ip_address_access(m
, AF_INET6
, &prefix
, 8);
639 return bus_log_create_error(r
);
642 r
= in_addr_prefix_from_string_auto(eq
, &family
, &prefix
, &prefixlen
);
644 return log_error_errno(r
, "Failed to parse IP address prefix: %s", eq
);
646 r
= bus_append_ip_address_access(m
, family
, &prefix
, prefixlen
);
648 return bus_log_create_error(r
);
651 r
= sd_bus_message_close_container(m
);
653 return bus_log_create_error(r
);
655 r
= sd_bus_message_close_container(m
);
657 return bus_log_create_error(r
);
659 r
= sd_bus_message_close_container(m
);
661 return bus_log_create_error(r
);
669 static int bus_append_automount_property(sd_bus_message
*m
, const char *field
, const char *eq
) {
671 if (streq(field
, "Where"))
673 return bus_append_string(m
, field
, eq
);
675 if (streq(field
, "DirectoryMode"))
677 return bus_append_parse_mode(m
, field
, eq
);
679 if (streq(field
, "TimeoutIdleSec"))
681 return bus_append_parse_sec_rename(m
, field
, eq
);
686 static int bus_append_execute_property(sd_bus_message
*m
, const char *field
, const char *eq
) {
689 if (STR_IN_SET(field
,
691 "UtmpIdentifier", "UtmpMode", "PAMName", "TTYPath",
692 "WorkingDirectory", "RootDirectory", "SyslogIdentifier",
693 "ProtectSystem", "ProtectHome", "SELinuxContext", "RootImage",
694 "RuntimeDirectoryPreserve", "Personality", "KeyringMode"))
696 return bus_append_string(m
, field
, eq
);
698 if (STR_IN_SET(field
,
699 "IgnoreSIGPIPE", "TTYVHangup", "TTYReset", "TTYVTDisallocate",
700 "PrivateTmp", "PrivateDevices", "PrivateNetwork", "PrivateUsers",
701 "NoNewPrivileges", "SyslogLevelPrefix",
702 "MemoryDenyWriteExecute", "RestrictRealtime", "DynamicUser", "RemoveIPC",
703 "ProtectKernelTunables", "ProtectKernelModules", "ProtectControlGroups",
704 "MountAPIVFS", "CPUSchedulingResetOnFork", "LockPersonality"))
706 return bus_append_parse_boolean(m
, field
, eq
);
708 if (STR_IN_SET(field
,
709 "ReadWriteDirectories", "ReadOnlyDirectories", "InaccessibleDirectories",
710 "ReadWritePaths", "ReadOnlyPaths", "InaccessiblePaths",
711 "RuntimeDirectory", "StateDirectory", "CacheDirectory", "LogsDirectory", "ConfigurationDirectory",
712 "SupplementaryGroups", "SystemCallArchitectures"))
714 return bus_append_strv(m
, field
, eq
, EXTRACT_QUOTES
);
716 if (STR_IN_SET(field
, "SyslogLevel", "LogLevelMax"))
718 return bus_append_log_level_from_string(m
, field
, eq
);
720 if (streq(field
, "SyslogFacility"))
722 return bus_append_log_facility_unshifted_from_string(m
, field
, eq
);
724 if (streq(field
, "SecureBits"))
726 return bus_append_secure_bits_from_string(m
, field
, eq
);
728 if (streq(field
, "CPUSchedulingPolicy"))
730 return bus_append_sched_policy_from_string(m
, field
, eq
);
732 if (STR_IN_SET(field
, "CPUSchedulingPriority", "OOMScoreAdjust"))
734 return bus_append_safe_atoi(m
, field
, eq
);
736 if (streq(field
, "Nice"))
738 return bus_append_parse_nice(m
, field
, eq
);
740 if (streq(field
, "SystemCallErrorNumber"))
742 return bus_append_parse_errno(m
, field
, eq
);
744 if (streq(field
, "IOSchedulingClass"))
746 return bus_append_ioprio_class_from_string(m
, field
, eq
);
748 if (streq(field
, "IOSchedulingPriority"))
750 return bus_append_ioprio_parse_priority(m
, field
, eq
);
752 if (STR_IN_SET(field
,
753 "RuntimeDirectoryMode", "StateDirectoryMode", "CacheDirectoryMode",
754 "LogsDirectoryMode", "ConfigurationDirectoryMode", "UMask"))
756 return bus_append_parse_mode(m
, field
, eq
);
758 if (streq(field
, "TimerSlackNSec"))
760 return bus_append_parse_nsec(m
, field
, eq
);
762 if (streq(field
, "MountFlags"))
764 return bus_append_mount_propagation_flags_from_string(m
, field
, eq
);
766 if (STR_IN_SET(field
, "Environment", "UnsetEnvironment", "PassEnvironment"))
768 return bus_append_strv(m
, field
, eq
, EXTRACT_QUOTES
|EXTRACT_CUNESCAPE
);
770 if (streq(field
, "EnvironmentFile")) {
773 r
= sd_bus_message_append(m
, "(sv)", "EnvironmentFiles", "a(sb)", 0);
775 r
= sd_bus_message_append(m
, "(sv)", "EnvironmentFiles", "a(sb)", 1,
776 eq
[0] == '-' ? eq
+ 1 : eq
,
779 return bus_log_create_error(r
);
784 if (streq(field
, "LogExtraFields")) {
786 r
= sd_bus_message_open_container(m
, 'r', "sv");
788 return bus_log_create_error(r
);
790 r
= sd_bus_message_append_basic(m
, 's', "LogExtraFields");
792 return bus_log_create_error(r
);
794 r
= sd_bus_message_open_container(m
, 'v', "aay");
796 return bus_log_create_error(r
);
798 r
= sd_bus_message_open_container(m
, 'a', "ay");
800 return bus_log_create_error(r
);
802 r
= sd_bus_message_append_array(m
, 'y', eq
, strlen(eq
));
804 return bus_log_create_error(r
);
806 r
= sd_bus_message_close_container(m
);
808 return bus_log_create_error(r
);
810 r
= sd_bus_message_close_container(m
);
812 return bus_log_create_error(r
);
814 r
= sd_bus_message_close_container(m
);
816 return bus_log_create_error(r
);
821 if (STR_IN_SET(field
, "StandardInput", "StandardOutput", "StandardError")) {
822 const char *n
, *appended
;
824 if ((n
= startswith(eq
, "fd:"))) {
825 appended
= strjoina(field
, "FileDescriptorName");
826 r
= sd_bus_message_append(m
, "(sv)", appended
, "s", n
);
827 } else if ((n
= startswith(eq
, "file:"))) {
828 appended
= strjoina(field
, "File");
829 r
= sd_bus_message_append(m
, "(sv)", appended
, "s", n
);
831 r
= sd_bus_message_append(m
, "(sv)", field
, "s", eq
);
834 return bus_log_create_error(r
);
839 if (streq(field
, "StandardInputText")) {
840 _cleanup_free_
char *unescaped
= NULL
;
842 r
= cunescape(eq
, 0, &unescaped
);
844 return log_error_errno(r
, "Failed to unescape text '%s': %m", eq
);
846 if (!strextend(&unescaped
, "\n", NULL
))
849 /* Note that we don't expand specifiers here, but that should be OK, as this is a programmatic
850 * interface anyway */
852 return bus_append_byte_array(m
, field
, unescaped
, strlen(unescaped
));
855 if (streq(field
, "StandardInputData")) {
856 _cleanup_free_
void *decoded
= NULL
;
859 r
= unbase64mem(eq
, (size_t) -1, &decoded
, &sz
);
861 return log_error_errno(r
, "Failed to decode base64 data '%s': %m", eq
);
863 return bus_append_byte_array(m
, field
, decoded
, sz
);
866 rl
= rlimit_from_string(field
);
871 r
= rlimit_parse(rl
, eq
, &l
);
873 return log_error_errno(r
, "Failed to parse resource limit: %s", eq
);
875 r
= sd_bus_message_append(m
, "(sv)", field
, "t", l
.rlim_max
);
877 return bus_log_create_error(r
);
879 sn
= strjoina(field
, "Soft");
880 r
= sd_bus_message_append(m
, "(sv)", sn
, "t", l
.rlim_cur
);
882 return bus_log_create_error(r
);
887 if (STR_IN_SET(field
, "AppArmorProfile", "SmackProcessLabel")) {
896 r
= sd_bus_message_append(m
, "(sv)", field
, "(bs)", ignore
, s
);
898 return bus_log_create_error(r
);
903 if (STR_IN_SET(field
, "CapabilityBoundingSet", "AmbientCapabilities")) {
913 r
= capability_set_from_string(p
, &sum
);
915 return log_error_errno(r
, "Failed to parse %s value %s: %m", field
, eq
);
917 sum
= invert
? ~sum
: sum
;
919 r
= sd_bus_message_append(m
, "(sv)", field
, "t", sum
);
921 return bus_log_create_error(r
);
926 if (streq(field
, "CPUAffinity")) {
927 _cleanup_cpu_free_ cpu_set_t
*cpuset
= NULL
;
929 r
= parse_cpu_set(eq
, &cpuset
);
931 return log_error_errno(r
, "Failed to parse %s value: %s", field
, eq
);
933 return bus_append_byte_array(m
, field
, cpuset
, CPU_ALLOC_SIZE(r
));
936 if (STR_IN_SET(field
, "RestrictAddressFamilies", "SystemCallFilter")) {
945 r
= sd_bus_message_open_container(m
, SD_BUS_TYPE_STRUCT
, "sv");
947 return bus_log_create_error(r
);
949 r
= sd_bus_message_append_basic(m
, SD_BUS_TYPE_STRING
, field
);
951 return bus_log_create_error(r
);
953 r
= sd_bus_message_open_container(m
, 'v', "(bas)");
955 return bus_log_create_error(r
);
957 r
= sd_bus_message_open_container(m
, 'r', "bas");
959 return bus_log_create_error(r
);
961 r
= sd_bus_message_append_basic(m
, 'b', &whitelist
);
963 return bus_log_create_error(r
);
965 r
= sd_bus_message_open_container(m
, 'a', "s");
967 return bus_log_create_error(r
);
970 _cleanup_free_
char *word
= NULL
;
972 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
);
978 return log_error_errno(r
, "Invalid syntax: %s", eq
);
980 r
= sd_bus_message_append_basic(m
, 's', word
);
982 return bus_log_create_error(r
);
985 r
= sd_bus_message_close_container(m
);
987 return bus_log_create_error(r
);
989 r
= sd_bus_message_close_container(m
);
991 return bus_log_create_error(r
);
993 r
= sd_bus_message_close_container(m
);
995 return bus_log_create_error(r
);
997 r
= sd_bus_message_close_container(m
);
999 return bus_log_create_error(r
);
1004 if (streq(field
, "RestrictNamespaces")) {
1005 bool invert
= false;
1006 unsigned long flags
= 0;
1013 r
= parse_boolean(eq
);
1017 flags
= NAMESPACE_FLAGS_ALL
;
1019 r
= namespace_flag_from_string_many(eq
, &flags
);
1021 return log_error_errno(r
, "Failed to parse %s value %s.", field
, eq
);
1025 flags
= (~flags
) & NAMESPACE_FLAGS_ALL
;
1027 r
= sd_bus_message_append(m
, "(sv)", field
, "t", (uint64_t) flags
);
1029 return bus_log_create_error(r
);
1034 if (STR_IN_SET(field
, "BindPaths", "BindReadOnlyPaths")) {
1037 r
= sd_bus_message_open_container(m
, SD_BUS_TYPE_STRUCT
, "sv");
1039 return bus_log_create_error(r
);
1041 r
= sd_bus_message_append_basic(m
, SD_BUS_TYPE_STRING
, field
);
1043 return bus_log_create_error(r
);
1045 r
= sd_bus_message_open_container(m
, 'v', "a(ssbt)");
1047 return bus_log_create_error(r
);
1049 r
= sd_bus_message_open_container(m
, 'a', "(ssbt)");
1051 return bus_log_create_error(r
);
1054 _cleanup_free_
char *source
= NULL
, *destination
= NULL
;
1055 char *s
= NULL
, *d
= NULL
;
1056 bool ignore_enoent
= false;
1057 uint64_t flags
= MS_REC
;
1059 r
= extract_first_word(&p
, &source
, ":" WHITESPACE
, EXTRACT_QUOTES
|EXTRACT_DONT_COALESCE_SEPARATORS
);
1061 return log_error_errno(r
, "Failed to parse argument: %m");
1067 ignore_enoent
= true;
1071 if (p
&& p
[-1] == ':') {
1072 r
= extract_first_word(&p
, &destination
, ":" WHITESPACE
, EXTRACT_QUOTES
|EXTRACT_DONT_COALESCE_SEPARATORS
);
1074 return log_error_errno(r
, "Failed to parse argument: %m");
1076 log_error("Missing argument after ':': %s", eq
);
1082 if (p
&& p
[-1] == ':') {
1083 _cleanup_free_
char *options
= NULL
;
1085 r
= extract_first_word(&p
, &options
, NULL
, EXTRACT_QUOTES
);
1087 return log_error_errno(r
, "Failed to parse argument: %m");
1089 if (isempty(options
) || streq(options
, "rbind"))
1091 else if (streq(options
, "norbind"))
1094 log_error("Unknown options: %s", eq
);
1101 r
= sd_bus_message_append(m
, "(ssbt)", s
, d
, ignore_enoent
, flags
);
1103 return bus_log_create_error(r
);
1106 r
= sd_bus_message_close_container(m
);
1108 return bus_log_create_error(r
);
1110 r
= sd_bus_message_close_container(m
);
1112 return bus_log_create_error(r
);
1114 r
= sd_bus_message_close_container(m
);
1116 return bus_log_create_error(r
);
1121 if (streq(field
, "TemporaryFileSystem")) {
1124 r
= sd_bus_message_open_container(m
, SD_BUS_TYPE_STRUCT
, "sv");
1126 return bus_log_create_error(r
);
1128 r
= sd_bus_message_append_basic(m
, SD_BUS_TYPE_STRING
, field
);
1130 return bus_log_create_error(r
);
1132 r
= sd_bus_message_open_container(m
, 'v', "a(ss)");
1134 return bus_log_create_error(r
);
1136 r
= sd_bus_message_open_container(m
, 'a', "(ss)");
1138 return bus_log_create_error(r
);
1141 _cleanup_free_
char *word
= NULL
, *path
= NULL
;
1144 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
);
1146 return log_error_errno(r
, "Failed to parse argument: %m");
1151 r
= extract_first_word(&w
, &path
, ":", EXTRACT_DONT_COALESCE_SEPARATORS
);
1153 return log_error_errno(r
, "Failed to parse argument: %m");
1155 return log_error("Failed to parse argument: %m");
1157 r
= sd_bus_message_append(m
, "(ss)", path
, w
);
1159 return bus_log_create_error(r
);
1162 r
= sd_bus_message_close_container(m
);
1164 return bus_log_create_error(r
);
1166 r
= sd_bus_message_close_container(m
);
1168 return bus_log_create_error(r
);
1170 r
= sd_bus_message_close_container(m
);
1172 return bus_log_create_error(r
);
1180 static int bus_append_kill_property(sd_bus_message
*m
, const char *field
, const char *eq
) {
1182 if (streq(field
, "KillMode"))
1184 return bus_append_string(m
, field
, eq
);
1186 if (STR_IN_SET(field
, "SendSIGHUP", "SendSIGKILL"))
1188 return bus_append_parse_boolean(m
, field
, eq
);
1190 if (streq(field
, "KillSignal"))
1192 return bus_append_signal_from_string_try_harder(m
, field
, eq
);
1197 static int bus_append_mount_property(sd_bus_message
*m
, const char *field
, const char *eq
) {
1199 if (STR_IN_SET(field
, "What", "Where", "Options", "Type"))
1201 return bus_append_string(m
, field
, eq
);
1203 if (streq(field
, "TimeoutSec"))
1205 return bus_append_parse_sec_rename(m
, field
, eq
);
1207 if (streq(field
, "DirectoryMode"))
1209 return bus_append_parse_mode(m
, field
, eq
);
1211 if (STR_IN_SET(field
, "SloppyOptions", "LazyUnmount", "ForceUnmount"))
1213 return bus_append_parse_boolean(m
, field
, eq
);
1218 static int bus_append_path_property(sd_bus_message
*m
, const char *field
, const char *eq
) {
1221 if (streq(field
, "MakeDirectory"))
1223 return bus_append_parse_boolean(m
, field
, eq
);
1225 if (streq(field
, "DirectoryMode"))
1227 return bus_append_parse_mode(m
, field
, eq
);
1229 if (STR_IN_SET(field
,
1230 "PathExists", "PathExistsGlob", "PathChanged",
1231 "PathModified", "DirectoryNotEmpty")) {
1234 r
= sd_bus_message_append(m
, "(sv)", "Paths", "a(ss)", 0);
1236 r
= sd_bus_message_append(m
, "(sv)", "Paths", "a(ss)", 1, field
, eq
);
1238 return bus_log_create_error(r
);
1246 static int bus_append_service_property(sd_bus_message
*m
, const char *field
, const char *eq
) {
1249 if (STR_IN_SET(field
,
1250 "PIDFile", "Type", "Restart", "BusName", "NotifyAccess",
1251 "USBFunctionDescriptors", "USBFunctionStrings"))
1253 return bus_append_string(m
, field
, eq
);
1255 if (STR_IN_SET(field
, "PermissionsStartOnly", "RootDirectoryStartOnly", "RemainAfterExit", "GuessMainPID"))
1257 return bus_append_parse_boolean(m
, field
, eq
);
1259 if (STR_IN_SET(field
, "RestartSec", "TimeoutStartSec", "TimeoutStopSec", "RuntimeMaxSec", "WatchdogSec"))
1261 return bus_append_parse_sec_rename(m
, field
, eq
);
1263 if (streq(field
, "TimeoutSec")) {
1265 r
= bus_append_parse_sec_rename(m
, "TimeoutStartSec", eq
);
1269 return bus_append_parse_sec_rename(m
, "TimeoutStopSec", eq
);
1272 if (streq(field
, "FileDescriptorStoreMax"))
1274 return bus_append_safe_atou(m
, field
, eq
);
1276 if (STR_IN_SET(field
,
1277 "ExecStartPre", "ExecStart", "ExecStartPost",
1278 "ExecReload", "ExecStop", "ExecStopPost"))
1280 return bus_append_exec_command(m
, field
, eq
);
1282 if (STR_IN_SET(field
, "RestartPreventExitStatus", "RestartForceExitStatus", "SuccessExitStatus")) {
1283 _cleanup_free_
int *status
= NULL
, *signal
= NULL
;
1284 size_t sz_status
= 0, sz_signal
= 0;
1288 _cleanup_free_
char *word
= NULL
;
1291 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
);
1297 return log_error_errno(r
, "Invalid syntax in %s: %s", field
, eq
);
1299 r
= safe_atoi(word
, &val
);
1301 val
= signal_from_string_try_harder(word
);
1303 return log_error_errno(r
, "Invalid status or signal %s in %s: %m", word
, field
);
1305 signal
= reallocarray(signal
, sz_signal
+ 1, sizeof(int));
1309 signal
[sz_signal
++] = val
;
1311 status
= reallocarray(status
, sz_status
+ 1, sizeof(int));
1315 status
[sz_status
++] = val
;
1319 r
= sd_bus_message_open_container(m
, SD_BUS_TYPE_STRUCT
, "sv");
1321 return bus_log_create_error(r
);
1323 r
= sd_bus_message_append_basic(m
, SD_BUS_TYPE_STRING
, field
);
1325 return bus_log_create_error(r
);
1327 r
= sd_bus_message_open_container(m
, 'v', "(aiai)");
1329 return bus_log_create_error(r
);
1331 r
= sd_bus_message_open_container(m
, 'r', "aiai");
1333 return bus_log_create_error(r
);
1335 r
= sd_bus_message_append_array(m
, 'i', status
, sz_status
);
1337 return bus_log_create_error(r
);
1339 r
= sd_bus_message_append_array(m
, 'i', signal
, sz_signal
);
1341 return bus_log_create_error(r
);
1343 r
= sd_bus_message_close_container(m
);
1345 return bus_log_create_error(r
);
1347 r
= sd_bus_message_close_container(m
);
1349 return bus_log_create_error(r
);
1351 r
= sd_bus_message_close_container(m
);
1353 return bus_log_create_error(r
);
1361 static int bus_append_socket_property(sd_bus_message
*m
, const char *field
, const char *eq
) {
1364 if (STR_IN_SET(field
,
1365 "Accept", "Writable", "KeepAlive", "NoDelay", "FreeBind", "Transparent", "Broadcast",
1366 "PassCredentials", "PassSecurity", "ReusePort", "RemoveOnStop", "SELinuxContextFromNet"))
1368 return bus_append_parse_boolean(m
, field
, eq
);
1370 if (STR_IN_SET(field
, "Priority", "IPTTL", "Mark"))
1372 return bus_append_safe_atoi(m
, field
, eq
);
1374 if (streq(field
, "IPTOS"))
1376 return bus_append_ip_tos_from_string(m
, field
, eq
);
1378 if (STR_IN_SET(field
, "Backlog", "MaxConnections", "MaxConnectionsPerSource", "KeepAliveProbes", "TriggerLimitBurst"))
1380 return bus_append_safe_atou(m
, field
, eq
);
1382 if (STR_IN_SET(field
, "SocketMode", "DirectoryMode"))
1384 return bus_append_parse_mode(m
, field
, eq
);
1386 if (STR_IN_SET(field
, "MessageQueueMaxMessages", "MessageQueueMessageSize"))
1388 return bus_append_safe_atoi64(m
, field
, eq
);
1390 if (STR_IN_SET(field
, "TimeoutSec", "KeepAliveTimeSec", "KeepAliveIntervalSec", "DeferAcceptSec", "TriggerLimitIntervalSec"))
1392 return bus_append_parse_sec_rename(m
, field
, eq
);
1394 if (STR_IN_SET(field
, "ReceiveBuffer", "SendBuffer", "PipeSize"))
1396 return bus_append_parse_size(m
, field
, eq
, 1024);
1398 if (STR_IN_SET(field
, "ExecStartPre", "ExecStartPost", "ExecReload", "ExecStopPost"))
1400 return bus_append_exec_command(m
, field
, eq
);
1402 if (STR_IN_SET(field
,
1403 "SmackLabel", "SmackLabelIPIn", "SmackLabelIPOut", "TCPCongestion",
1404 "BindToDevice", "BindIPv6Only", "FileDescriptorName",
1405 "SocketUser", "SocketGroup"))
1407 return bus_append_string(m
, field
, eq
);
1409 if (streq(field
, "Symlinks"))
1411 return bus_append_strv(m
, field
, eq
, EXTRACT_QUOTES
);
1413 if (streq(field
, "SocketProtocol"))
1415 return bus_append_socket_protocol_from_name(m
, field
, eq
);
1417 if (STR_IN_SET(field
,
1418 "ListenStream", "ListenDatagram", "ListenSequentialPacket", "ListenNetlink",
1419 "ListenSpecial", "ListenMessageQueue", "ListenFIFO", "ListenUSBFunction")) {
1422 r
= sd_bus_message_append(m
, "(sv)", "Listen", "a(ss)", 0);
1424 r
= sd_bus_message_append(m
, "(sv)", "Listen", "a(ss)", 1, field
+ STRLEN("Listen"), eq
);
1426 return bus_log_create_error(r
);
1433 static int bus_append_timer_property(sd_bus_message
*m
, const char *field
, const char *eq
) {
1436 if (STR_IN_SET(field
, "WakeSystem", "RemainAfterElapse", "Persistent"))
1438 return bus_append_parse_boolean(m
, field
, eq
);
1440 if (STR_IN_SET(field
, "AccuracySec", "RandomizedDelaySec"))
1442 return bus_append_parse_sec_rename(m
, field
, eq
);
1444 if (STR_IN_SET(field
,
1445 "OnActiveSec", "OnBootSec", "OnStartupSec",
1446 "OnUnitActiveSec","OnUnitInactiveSec")) {
1449 r
= sd_bus_message_append(m
, "(sv)", "TimersMonotonic", "a(st)", 0);
1452 r
= parse_sec(eq
, &t
);
1454 return log_error_errno(r
, "Failed to parse %s=%s: %m", field
, eq
);
1456 r
= sd_bus_message_append(m
, "(sv)", "TimersMonotonic", "a(st)", 1, field
, t
);
1459 return bus_log_create_error(r
);
1464 if (streq(field
, "OnCalendar")) {
1467 r
= sd_bus_message_append(m
, "(sv)", "TimersCalendar", "a(ss)", 0);
1469 r
= sd_bus_message_append(m
, "(sv)", "TimersCalendar", "a(ss)", 1, field
, eq
);
1471 return bus_log_create_error(r
);
1479 static int bus_append_unit_property(sd_bus_message
*m
, const char *field
, const char *eq
) {
1480 ConditionType t
= _CONDITION_TYPE_INVALID
;
1481 bool is_condition
= false;
1484 if (STR_IN_SET(field
,
1485 "Description", "SourcePath", "OnFailureJobMode",
1486 "JobTimeoutAction", "JobTimeoutRebootArgument",
1487 "StartLimitAction", "FailureAction", "SuccessAction",
1488 "RebootArgument", "CollectMode"))
1490 return bus_append_string(m
, field
, eq
);
1492 if (STR_IN_SET(field
,
1493 "StopWhenUnneeded", "RefuseManualStart", "RefuseManualStop",
1494 "AllowIsolate", "IgnoreOnIsolate", "DefaultDependencies"))
1496 return bus_append_parse_boolean(m
, field
, eq
);
1498 if (STR_IN_SET(field
, "JobTimeoutSec", "JobRunningTimeoutSec", "StartLimitIntervalSec"))
1500 return bus_append_parse_sec_rename(m
, field
, eq
);
1502 if (streq(field
, "StartLimitBurst"))
1504 return bus_append_safe_atou(m
, field
, eq
);
1506 if (unit_dependency_from_string(field
) >= 0 ||
1507 STR_IN_SET(field
, "Documentation", "RequiresMountsFor"))
1509 return bus_append_strv(m
, field
, eq
, EXTRACT_QUOTES
);
1511 t
= condition_type_from_string(field
);
1513 is_condition
= true;
1515 t
= assert_type_from_string(field
);
1518 r
= sd_bus_message_append(m
, "(sv)", is_condition
? "Conditions" : "Asserts", "a(sbbs)", 0);
1521 int trigger
, negate
;
1523 trigger
= *p
== '|';
1531 r
= sd_bus_message_append(m
, "(sv)", is_condition
? "Conditions" : "Asserts", "a(sbbs)", 1,
1532 field
, trigger
, negate
, p
);
1535 return bus_log_create_error(r
);
1543 int bus_append_unit_property_assignment(sd_bus_message
*m
, UnitType t
, const char *assignment
) {
1544 const char *eq
, *field
;
1550 eq
= strchr(assignment
, '=');
1552 log_error("Not an assignment: %s", assignment
);
1556 field
= strndupa(assignment
, eq
- assignment
);
1561 r
= bus_append_cgroup_property(m
, field
, eq
);
1565 r
= bus_append_execute_property(m
, field
, eq
);
1569 r
= bus_append_kill_property(m
, field
, eq
);
1573 r
= bus_append_service_property(m
, field
, eq
);
1579 r
= bus_append_cgroup_property(m
, field
, eq
);
1583 r
= bus_append_execute_property(m
, field
, eq
);
1587 r
= bus_append_kill_property(m
, field
, eq
);
1591 r
= bus_append_socket_property(m
, field
, eq
);
1597 r
= bus_append_timer_property(m
, field
, eq
);
1603 r
= bus_append_path_property(m
, field
, eq
);
1609 r
= bus_append_cgroup_property(m
, field
, eq
);
1616 if (streq(field
, "TimeoutStopSec"))
1617 return bus_append_parse_sec_rename(m
, field
, eq
);
1619 r
= bus_append_cgroup_property(m
, field
, eq
);
1623 r
= bus_append_kill_property(m
, field
, eq
);
1629 r
= bus_append_cgroup_property(m
, field
, eq
);
1633 r
= bus_append_execute_property(m
, field
, eq
);
1637 r
= bus_append_kill_property(m
, field
, eq
);
1641 r
= bus_append_mount_property(m
, field
, eq
);
1647 case UNIT_AUTOMOUNT
:
1648 r
= bus_append_automount_property(m
, field
, eq
);
1657 log_error("Not supported unit type");
1661 log_error("Invalid unit type");
1665 r
= bus_append_unit_property(m
, field
, eq
);
1669 log_error("Unknown assignment: %s", assignment
);
1673 int bus_append_unit_property_assignment_many(sd_bus_message
*m
, UnitType t
, char **l
) {
1679 STRV_FOREACH(i
, l
) {
1680 r
= bus_append_unit_property_assignment(m
, t
, *i
);
1688 typedef struct BusWaitForJobs
{
1695 sd_bus_slot
*slot_job_removed
;
1696 sd_bus_slot
*slot_disconnected
;
1699 static int match_disconnected(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1702 log_error("Warning! D-Bus connection terminated.");
1703 sd_bus_close(sd_bus_message_get_bus(m
));
1708 static int match_job_removed(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1709 const char *path
, *unit
, *result
;
1710 BusWaitForJobs
*d
= userdata
;
1718 r
= sd_bus_message_read(m
, "uoss", &id
, &path
, &unit
, &result
);
1720 bus_log_parse_error(r
);
1724 found
= set_remove(d
->jobs
, (char*) path
);
1730 if (!isempty(result
))
1731 d
->result
= strdup(result
);
1734 d
->name
= strdup(unit
);
1739 void bus_wait_for_jobs_free(BusWaitForJobs
*d
) {
1743 set_free_free(d
->jobs
);
1745 sd_bus_slot_unref(d
->slot_disconnected
);
1746 sd_bus_slot_unref(d
->slot_job_removed
);
1748 sd_bus_unref(d
->bus
);
1756 int bus_wait_for_jobs_new(sd_bus
*bus
, BusWaitForJobs
**ret
) {
1757 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*d
= NULL
;
1763 d
= new0(BusWaitForJobs
, 1);
1767 d
->bus
= sd_bus_ref(bus
);
1769 /* When we are a bus client we match by sender. Direct
1770 * connections OTOH have no initialized sender field, and
1771 * hence we ignore the sender then */
1772 r
= sd_bus_match_signal_async(
1774 &d
->slot_job_removed
,
1775 bus
->bus_client
? "org.freedesktop.systemd1" : NULL
,
1776 "/org/freedesktop/systemd1",
1777 "org.freedesktop.systemd1.Manager",
1779 match_job_removed
, NULL
, d
);
1783 r
= sd_bus_match_signal_async(
1785 &d
->slot_disconnected
,
1786 "org.freedesktop.DBus.Local",
1788 "org.freedesktop.DBus.Local",
1790 match_disconnected
, NULL
, d
);
1799 static int bus_process_wait(sd_bus
*bus
) {
1803 r
= sd_bus_process(bus
, NULL
);
1809 r
= sd_bus_wait(bus
, (uint64_t) -1);
1815 static int bus_job_get_service_result(BusWaitForJobs
*d
, char **result
) {
1816 _cleanup_free_
char *dbus_path
= NULL
;
1822 if (!endswith(d
->name
, ".service"))
1825 dbus_path
= unit_dbus_path_from_name(d
->name
);
1829 return sd_bus_get_property_string(d
->bus
,
1830 "org.freedesktop.systemd1",
1832 "org.freedesktop.systemd1.Service",
1838 static const struct {
1839 const char *result
, *explanation
;
1840 } explanations
[] = {
1841 { "resources", "of unavailable resources or another system error" },
1842 { "protocol", "the service did not take the steps required by its unit configuration" },
1843 { "timeout", "a timeout was exceeded" },
1844 { "exit-code", "the control process exited with error code" },
1845 { "signal", "a fatal signal was delivered to the control process" },
1846 { "core-dump", "a fatal signal was delivered causing the control process to dump core" },
1847 { "watchdog", "the service failed to send watchdog ping" },
1848 { "start-limit", "start of the service was attempted too often" }
1851 static void log_job_error_with_service_result(const char* service
, const char *result
, const char* const* extra_args
) {
1852 _cleanup_free_
char *service_shell_quoted
= NULL
;
1853 const char *systemctl
= "systemctl", *journalctl
= "journalctl";
1857 service_shell_quoted
= shell_maybe_quote(service
, ESCAPE_BACKSLASH
);
1859 if (!strv_isempty((char**) extra_args
)) {
1860 _cleanup_free_
char *t
;
1862 t
= strv_join((char**) extra_args
, " ");
1863 systemctl
= strjoina("systemctl ", t
? : "<args>");
1864 journalctl
= strjoina("journalctl ", t
? : "<args>");
1867 if (!isempty(result
)) {
1870 for (i
= 0; i
< ELEMENTSOF(explanations
); ++i
)
1871 if (streq(result
, explanations
[i
].result
))
1874 if (i
< ELEMENTSOF(explanations
)) {
1875 log_error("Job for %s failed because %s.\n"
1876 "See \"%s status %s\" and \"%s -xe\" for details.\n",
1878 explanations
[i
].explanation
,
1880 service_shell_quoted
?: "<service>",
1886 log_error("Job for %s failed.\n"
1887 "See \"%s status %s\" and \"%s -xe\" for details.\n",
1890 service_shell_quoted
?: "<service>",
1894 /* For some results maybe additional explanation is required */
1895 if (streq_ptr(result
, "start-limit"))
1896 log_info("To force a start use \"%1$s reset-failed %2$s\"\n"
1897 "followed by \"%1$s start %2$s\" again.",
1899 service_shell_quoted
?: "<service>");
1902 static int check_wait_response(BusWaitForJobs
*d
, bool quiet
, const char* const* extra_args
) {
1906 if (streq(d
->result
, "canceled"))
1907 log_error("Job for %s canceled.", strna(d
->name
));
1908 else if (streq(d
->result
, "timeout"))
1909 log_error("Job for %s timed out.", strna(d
->name
));
1910 else if (streq(d
->result
, "dependency"))
1911 log_error("A dependency job for %s failed. See 'journalctl -xe' for details.", strna(d
->name
));
1912 else if (streq(d
->result
, "invalid"))
1913 log_error("%s is not active, cannot reload.", strna(d
->name
));
1914 else if (streq(d
->result
, "assert"))
1915 log_error("Assertion failed on job for %s.", strna(d
->name
));
1916 else if (streq(d
->result
, "unsupported"))
1917 log_error("Operation on or unit type of %s not supported on this system.", strna(d
->name
));
1918 else if (streq(d
->result
, "collected"))
1919 log_error("Queued job for %s was garbage collected.", strna(d
->name
));
1920 else if (streq(d
->result
, "once"))
1921 log_error("Unit %s was started already once and can't be started again.", strna(d
->name
));
1922 else if (!STR_IN_SET(d
->result
, "done", "skipped")) {
1924 _cleanup_free_
char *result
= NULL
;
1927 q
= bus_job_get_service_result(d
, &result
);
1929 log_debug_errno(q
, "Failed to get Result property of unit %s: %m", d
->name
);
1931 log_job_error_with_service_result(d
->name
, result
, extra_args
);
1933 log_error("Job failed. See \"journalctl -xe\" for details.");
1937 if (STR_IN_SET(d
->result
, "canceled", "collected"))
1939 else if (streq(d
->result
, "timeout"))
1941 else if (streq(d
->result
, "dependency"))
1943 else if (streq(d
->result
, "invalid"))
1945 else if (streq(d
->result
, "assert"))
1947 else if (streq(d
->result
, "unsupported"))
1949 else if (streq(d
->result
, "once"))
1951 else if (STR_IN_SET(d
->result
, "done", "skipped"))
1954 log_debug("Unexpected job result, assuming server side newer than us: %s", d
->result
);
1958 int bus_wait_for_jobs(BusWaitForJobs
*d
, bool quiet
, const char* const* extra_args
) {
1963 while (!set_isempty(d
->jobs
)) {
1966 q
= bus_process_wait(d
->bus
);
1968 return log_error_errno(q
, "Failed to wait for response: %m");
1971 q
= check_wait_response(d
, quiet
, extra_args
);
1972 /* Return the first error as it is most likely to be
1974 if (q
< 0 && r
== 0)
1977 log_debug_errno(q
, "Got result %s/%m for job %s", strna(d
->result
), strna(d
->name
));
1980 d
->name
= mfree(d
->name
);
1981 d
->result
= mfree(d
->result
);
1987 int bus_wait_for_jobs_add(BusWaitForJobs
*d
, const char *path
) {
1992 r
= set_ensure_allocated(&d
->jobs
, &string_hash_ops
);
1996 return set_put_strdup(d
->jobs
, path
);
1999 int bus_wait_for_jobs_one(BusWaitForJobs
*d
, const char *path
, bool quiet
) {
2002 r
= bus_wait_for_jobs_add(d
, path
);
2006 return bus_wait_for_jobs(d
, quiet
, NULL
);
2009 int bus_deserialize_and_dump_unit_file_changes(sd_bus_message
*m
, bool quiet
, UnitFileChange
**changes
, size_t *n_changes
) {
2010 const char *type
, *path
, *source
;
2013 /* changes is dereferenced when calling unit_file_dump_changes() later,
2014 * so we have to make sure this is not NULL. */
2018 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_ARRAY
, "(sss)");
2020 return bus_log_parse_error(r
);
2022 while ((r
= sd_bus_message_read(m
, "(sss)", &type
, &path
, &source
)) > 0) {
2023 /* We expect only "success" changes to be sent over the bus.
2024 Hence, reject anything negative. */
2025 UnitFileChangeType ch
= unit_file_change_type_from_string(type
);
2028 log_notice("Manager reported unknown change type \"%s\" for path \"%s\", ignoring.", type
, path
);
2032 r
= unit_file_changes_add(changes
, n_changes
, ch
, path
, source
);
2037 return bus_log_parse_error(r
);
2039 r
= sd_bus_message_exit_container(m
);
2041 return bus_log_parse_error(r
);
2043 unit_file_dump_changes(0, NULL
, *changes
, *n_changes
, quiet
);
2049 bool is_const
; /* If false, cgroup_path should be free()'d */
2051 Hashmap
*pids
; /* PID → process name */
2054 struct CGroupInfo
*parent
;
2055 LIST_FIELDS(struct CGroupInfo
, siblings
);
2056 LIST_HEAD(struct CGroupInfo
, children
);
2060 static bool IS_ROOT(const char *p
) {
2061 return isempty(p
) || streq(p
, "/");
2064 static int add_cgroup(Hashmap
*cgroups
, const char *path
, bool is_const
, struct CGroupInfo
**ret
) {
2065 struct CGroupInfo
*parent
= NULL
, *cg
;
2074 cg
= hashmap_get(cgroups
, path
);
2080 if (!IS_ROOT(path
)) {
2083 e
= strrchr(path
, '/');
2087 pp
= strndupa(path
, e
- path
);
2091 r
= add_cgroup(cgroups
, pp
, false, &parent
);
2096 cg
= new0(struct CGroupInfo
, 1);
2101 cg
->cgroup_path
= (char*) path
;
2103 cg
->cgroup_path
= strdup(path
);
2104 if (!cg
->cgroup_path
) {
2110 cg
->is_const
= is_const
;
2111 cg
->parent
= parent
;
2113 r
= hashmap_put(cgroups
, cg
->cgroup_path
, cg
);
2116 free(cg
->cgroup_path
);
2122 LIST_PREPEND(siblings
, parent
->children
, cg
);
2123 parent
->n_children
++;
2130 static int add_process(
2136 struct CGroupInfo
*cg
;
2143 r
= add_cgroup(cgroups
, path
, true, &cg
);
2147 r
= hashmap_ensure_allocated(&cg
->pids
, &trivial_hash_ops
);
2151 return hashmap_put(cg
->pids
, PID_TO_PTR(pid
), (void*) name
);
2154 static void remove_cgroup(Hashmap
*cgroups
, struct CGroupInfo
*cg
) {
2158 while (cg
->children
)
2159 remove_cgroup(cgroups
, cg
->children
);
2161 hashmap_remove(cgroups
, cg
->cgroup_path
);
2164 free(cg
->cgroup_path
);
2166 hashmap_free(cg
->pids
);
2169 LIST_REMOVE(siblings
, cg
->parent
->children
, cg
);
2174 static int cgroup_info_compare_func(const void *a
, const void *b
) {
2175 const struct CGroupInfo
*x
= *(const struct CGroupInfo
* const*) a
, *y
= *(const struct CGroupInfo
* const*) b
;
2180 return strcmp(x
->cgroup_path
, y
->cgroup_path
);
2183 static int dump_processes(
2185 const char *cgroup_path
,
2188 OutputFlags flags
) {
2190 struct CGroupInfo
*cg
;
2195 if (IS_ROOT(cgroup_path
))
2198 cg
= hashmap_get(cgroups
, cgroup_path
);
2202 if (!hashmap_isempty(cg
->pids
)) {
2210 /* Order processes by their PID */
2211 pids
= newa(pid_t
, hashmap_size(cg
->pids
));
2213 HASHMAP_FOREACH_KEY(name
, pidp
, cg
->pids
, j
)
2214 pids
[n
++] = PTR_TO_PID(pidp
);
2216 assert(n
== hashmap_size(cg
->pids
));
2217 qsort_safe(pids
, n
, sizeof(pid_t
), pid_compare_func
);
2219 width
= DECIMAL_STR_WIDTH(pids
[n
-1]);
2221 for (i
= 0; i
< n
; i
++) {
2222 _cleanup_free_
char *e
= NULL
;
2223 const char *special
;
2226 name
= hashmap_get(cg
->pids
, PID_TO_PTR(pids
[i
]));
2229 if (n_columns
!= 0) {
2232 k
= MAX(LESS_BY(n_columns
, 2U + width
+ 1U), 20U);
2234 e
= ellipsize(name
, k
, 100);
2239 more
= i
+1 < n
|| cg
->children
;
2240 special
= special_glyph(more
? TREE_BRANCH
: TREE_RIGHT
);
2242 fprintf(stdout
, "%s%s%*"PID_PRI
" %s\n",
2251 struct CGroupInfo
**children
, *child
;
2254 /* Order subcgroups by their name */
2255 children
= newa(struct CGroupInfo
*, cg
->n_children
);
2256 LIST_FOREACH(siblings
, child
, cg
->children
)
2257 children
[n
++] = child
;
2258 assert(n
== cg
->n_children
);
2259 qsort_safe(children
, n
, sizeof(struct CGroupInfo
*), cgroup_info_compare_func
);
2262 n_columns
= MAX(LESS_BY(n_columns
, 2U), 20U);
2264 for (i
= 0; i
< n
; i
++) {
2265 _cleanup_free_
char *pp
= NULL
;
2266 const char *name
, *special
;
2269 child
= children
[i
];
2271 name
= strrchr(child
->cgroup_path
, '/');
2277 special
= special_glyph(more
? TREE_BRANCH
: TREE_RIGHT
);
2279 fputs(prefix
, stdout
);
2280 fputs(special
, stdout
);
2281 fputs(name
, stdout
);
2282 fputc('\n', stdout
);
2284 special
= special_glyph(more
? TREE_VERTICAL
: TREE_SPACE
);
2286 pp
= strappend(prefix
, special
);
2290 r
= dump_processes(cgroups
, child
->cgroup_path
, pp
, n_columns
, flags
);
2300 static int dump_extra_processes(
2304 OutputFlags flags
) {
2306 _cleanup_free_ pid_t
*pids
= NULL
;
2307 _cleanup_hashmap_free_ Hashmap
*names
= NULL
;
2308 struct CGroupInfo
*cg
;
2309 size_t n_allocated
= 0, n
= 0, k
;
2313 /* Prints the extra processes, i.e. those that are in cgroups we haven't displayed yet. We show them as
2314 * combined, sorted, linear list. */
2316 HASHMAP_FOREACH(cg
, cgroups
, i
) {
2324 if (hashmap_isempty(cg
->pids
))
2327 r
= hashmap_ensure_allocated(&names
, &trivial_hash_ops
);
2331 if (!GREEDY_REALLOC(pids
, n_allocated
, n
+ hashmap_size(cg
->pids
)))
2334 HASHMAP_FOREACH_KEY(name
, pidp
, cg
->pids
, j
) {
2335 pids
[n
++] = PTR_TO_PID(pidp
);
2337 r
= hashmap_put(names
, pidp
, (void*) name
);
2346 qsort_safe(pids
, n
, sizeof(pid_t
), pid_compare_func
);
2347 width
= DECIMAL_STR_WIDTH(pids
[n
-1]);
2349 for (k
= 0; k
< n
; k
++) {
2350 _cleanup_free_
char *e
= NULL
;
2353 name
= hashmap_get(names
, PID_TO_PTR(pids
[k
]));
2356 if (n_columns
!= 0) {
2359 z
= MAX(LESS_BY(n_columns
, 2U + width
+ 1U), 20U);
2361 e
= ellipsize(name
, z
, 100);
2366 fprintf(stdout
, "%s%s %*" PID_PRI
" %s\n",
2368 special_glyph(TRIANGULAR_BULLET
),
2376 int unit_show_processes(
2379 const char *cgroup_path
,
2383 sd_bus_error
*error
) {
2385 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
2386 Hashmap
*cgroups
= NULL
;
2387 struct CGroupInfo
*cg
;
2393 if (flags
& OUTPUT_FULL_WIDTH
)
2395 else if (n_columns
<= 0)
2396 n_columns
= columns();
2398 prefix
= strempty(prefix
);
2400 r
= sd_bus_call_method(
2402 "org.freedesktop.systemd1",
2403 "/org/freedesktop/systemd1",
2404 "org.freedesktop.systemd1.Manager",
2413 cgroups
= hashmap_new(&path_hash_ops
);
2417 r
= sd_bus_message_enter_container(reply
, 'a', "(sus)");
2422 const char *path
= NULL
, *name
= NULL
;
2425 r
= sd_bus_message_read(reply
, "(sus)", &path
, &pid
, &name
);
2431 r
= add_process(cgroups
, path
, pid
, name
);
2436 r
= sd_bus_message_exit_container(reply
);
2440 r
= dump_processes(cgroups
, cgroup_path
, prefix
, n_columns
, flags
);
2444 r
= dump_extra_processes(cgroups
, prefix
, n_columns
, flags
);
2447 while ((cg
= hashmap_first(cgroups
)))
2448 remove_cgroup(cgroups
, cg
);
2450 hashmap_free(cgroups
);
2455 int unit_load_state(sd_bus
*bus
, const char *name
, char **load_state
) {
2456 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2457 _cleanup_free_
char *path
= NULL
;
2460 path
= unit_dbus_path_from_name(name
);
2464 /* This function warns on it's own, because otherwise it'd be awkward to pass
2465 * the dbus error message around. */
2467 r
= sd_bus_get_property_string(
2469 "org.freedesktop.systemd1",
2471 "org.freedesktop.systemd1.Unit",
2476 return log_error_errno(r
, "Failed to get load state of %s: %s", name
, bus_error_message(&error
, r
));