From: Mike Yuan Date: Wed, 17 Jan 2024 04:09:11 +0000 (+0800) Subject: logind-action: also check .target unit state when selecting sleep action X-Git-Tag: v256-rc1~1124 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=d9975dbb3c32841cf225bf9fe7a1a5e8438f2228;p=thirdparty%2Fsystemd.git logind-action: also check .target unit state when selecting sleep action --- diff --git a/src/login/logind-action.c b/src/login/logind-action.c index 138952da91d..c86856806b5 100644 --- a/src/login/logind-action.c +++ b/src/login/logind-action.c @@ -6,6 +6,7 @@ #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" @@ -166,15 +167,24 @@ int handle_action_get_enabled_sleep_actions(HandleActionSleepMask mask, char *** 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; } @@ -263,7 +273,7 @@ static int handle_action_sleep_execute( 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."); diff --git a/src/login/logind-action.h b/src/login/logind-action.h index 5ee86486ec3..c78c18c5aad 100644 --- a/src/login/logind-action.h +++ b/src/login/logind-action.h @@ -70,7 +70,7 @@ struct HandleActionData { }; int handle_action_get_enabled_sleep_actions(HandleActionSleepMask mask, char ***ret); -HandleAction handle_action_sleep_select(HandleActionSleepMask mask); +HandleAction handle_action_sleep_select(Manager *m); int manager_handle_action( Manager *m, diff --git a/src/login/logind-dbus.c b/src/login/logind-dbus.c index 8221f76fbf2..2f722d24910 100644 --- a/src/login/logind-dbus.c +++ b/src/login/logind-dbus.c @@ -2130,7 +2130,7 @@ static int method_do_shutdown_or_sleep( 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"); @@ -2637,7 +2637,7 @@ static int method_can_shutdown_or_sleep( 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; @@ -2650,10 +2650,12 @@ static int method_can_shutdown_or_sleep( 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)) { @@ -2687,7 +2689,7 @@ static int method_can_shutdown_or_sleep( 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);