#include "alloc-util.h"
#include "bus-error.h"
+#include "bus-unit-util.h"
#include "bus-util.h"
#include "conf-parser.h"
#include "format-util.h"
return 0;
}
-HandleAction handle_action_sleep_select(HandleActionSleepMask mask) {
+HandleAction handle_action_sleep_select(Manager *m) {
+ assert(m);
FOREACH_ARRAY(i, sleep_actions, ELEMENTSOF(sleep_actions)) {
- HandleActionSleepMask a = 1U << *i;
+ HandleActionSleepMask action_mask = 1U << *i;
+ const HandleActionData *a;
+ _cleanup_free_ char *load_state = NULL;
+
+ if (!FLAGS_SET(m->handle_action_sleep_mask, action_mask))
+ continue;
+
+ a = ASSERT_PTR(handle_action_lookup(*i));
- if (!FLAGS_SET(mask, a))
+ if (sleep_supported(a->sleep_operation) <= 0)
continue;
- if (handle_action_sleep_supported(*i))
+ (void) unit_load_state(m->bus, a->target, &load_state);
+ if (streq_ptr(load_state, "loaded"))
return *i;
}
if (handle == HANDLE_SLEEP) {
HandleAction a;
- a = handle_action_sleep_select(m->handle_action_sleep_mask);
+ a = handle_action_sleep_select(m);
if (a < 0)
return log_warning_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
"None of the configured sleep operations are supported, ignoring.");
if (action == HANDLE_SLEEP) {
HandleAction selected;
- selected = handle_action_sleep_select(m->handle_action_sleep_mask);
+ selected = handle_action_sleep_select(m);
if (selected < 0)
return sd_bus_error_set(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED,
"None of the configured sleep operations are supported");
sd_bus_error *error) {
_cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
- bool multiple_sessions, challenge, blocked;
+ bool multiple_sessions, challenge, blocked, check_unit_state = true;
const HandleActionData *a;
const char *result = NULL;
uid_t uid;
if (action == HANDLE_SLEEP) {
HandleAction selected;
- selected = handle_action_sleep_select(m->handle_action_sleep_mask);
+ selected = handle_action_sleep_select(m);
if (selected < 0)
return sd_bus_reply_method_return(message, "s", "na");
+ check_unit_state = false; /* Already handled by handle_action_sleep_select */
+
assert_se(a = handle_action_lookup(selected));
} else if (HANDLE_ACTION_IS_SLEEP(action)) {
multiple_sessions = r > 0;
blocked = manager_is_inhibited(m, a->inhibit_what, INHIBIT_BLOCK, NULL, false, true, uid, NULL);
- if (a->target) {
+ if (check_unit_state && a->target) {
_cleanup_free_ char *load_state = NULL;
r = unit_load_state(m->bus, a->target, &load_state);