]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
logind-action: also check .target unit state when selecting sleep action
authorMike Yuan <me@yhndnzj.com>
Wed, 17 Jan 2024 04:09:11 +0000 (12:09 +0800)
committerLuca Boccassi <luca.boccassi@gmail.com>
Wed, 17 Jan 2024 13:28:19 +0000 (13:28 +0000)
src/login/logind-action.c
src/login/logind-action.h
src/login/logind-dbus.c

index 138952da91da82424c6c4b3f5bd8cd20d44864a4..c86856806b591cd2df6e7485a52ba0356a142b36 100644 (file)
@@ -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.");
index 5ee86486ec3c652e1499d7f0304d74c4e4cd57ae..c78c18c5aad635c16198a5a64cdb817bed760a58 100644 (file)
@@ -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,
index 8221f76fbf267f9d3f671d130080a555abae8099..2f722d2491063cb2a01c2e1c43cc1c38c53614ad 100644 (file)
@@ -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);