1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
4 #include "bus-locator.h"
5 #include "systemctl-is-enabled.h"
6 #include "systemctl-sysv-compat.h"
7 #include "systemctl-util.h"
10 static int show_installation_targets_client_side(const char *name
) {
11 UnitFileChange
*changes
= NULL
;
12 size_t n_changes
= 0, i
;
18 flags
= UNIT_FILE_DRY_RUN
|
19 (arg_runtime
? UNIT_FILE_RUNTIME
: 0);
21 r
= unit_file_disable(UNIT_FILE_SYSTEM
, flags
, NULL
, p
, &changes
, &n_changes
);
23 return log_error_errno(r
, "Failed to get file links for %s: %m", name
);
25 for (i
= 0; i
< n_changes
; i
++)
26 if (changes
[i
].type
== UNIT_FILE_UNLINK
)
27 printf(" %s\n", changes
[i
].path
);
32 static int show_installation_targets(sd_bus
*bus
, const char *name
) {
33 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
34 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
38 r
= bus_call_method(bus
, bus_systemd_mgr
, "GetUnitFileLinks", &error
, &reply
, "sb", name
, arg_runtime
);
40 return log_error_errno(r
, "Failed to get unit file links for %s: %s", name
, bus_error_message(&error
, r
));
42 r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_ARRAY
, "s");
44 return bus_log_parse_error(r
);
46 while ((r
= sd_bus_message_read(reply
, "s", &link
)) > 0)
47 printf(" %s\n", link
);
50 return bus_log_parse_error(r
);
52 r
= sd_bus_message_exit_container(reply
);
54 return bus_log_parse_error(r
);
59 int unit_is_enabled(int argc
, char *argv
[], void *userdata
) {
60 _cleanup_strv_free_
char **names
= NULL
;
65 r
= mangle_names("to check", strv_skip(argv
, 1), &names
);
69 r
= enable_sysv_units(argv
[0], names
);
75 if (install_client_side()) {
76 STRV_FOREACH(name
, names
) {
79 r
= unit_file_get_state(arg_scope
, arg_root
, *name
, &state
);
81 return log_error_errno(r
, "Failed to get unit file state for %s: %m", *name
);
85 UNIT_FILE_ENABLED_RUNTIME
,
93 puts(unit_file_state_to_string(state
));
95 r
= show_installation_targets_client_side(*name
);
104 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
107 r
= acquire_bus(BUS_MANAGER
, &bus
);
111 STRV_FOREACH(name
, names
) {
112 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
115 r
= bus_call_method(bus
, bus_systemd_mgr
, "GetUnitFileState", &error
, &reply
, "s", *name
);
117 return log_error_errno(r
, "Failed to get unit file state for %s: %s", *name
, bus_error_message(&error
, r
));
119 r
= sd_bus_message_read(reply
, "s", &s
);
121 return bus_log_parse_error(r
);
123 if (STR_IN_SET(s
, "enabled", "enabled-runtime", "static", "alias", "indirect", "generated"))
129 r
= show_installation_targets(bus
, *name
);
137 return enabled
? EXIT_SUCCESS
: EXIT_FAILURE
;