b11b5f
From a55bdb007ca24900a704f72359305b04125d9e05 Mon Sep 17 00:00:00 2001
b11b5f
From: Michal Sekletar <msekleta@redhat.com>
b11b5f
Date: Wed, 20 Apr 2022 10:13:43 +0200
b11b5f
Subject: [PATCH] logind: remember our idle state and use it to detect idle
b11b5f
 level transitions
b11b5f
b11b5f
Fixes #16391
b11b5f
b11b5f
(cherry picked from commit 4e2cfb778b9ed7f22ee98f48f28cf8678d25ad32)
b11b5f
b11b5f
Resolved: #1866955
b11b5f
---
b11b5f
 src/login/logind.c | 23 +++++++++++++++++++----
b11b5f
 src/login/logind.h |  1 +
b11b5f
 2 files changed, 20 insertions(+), 4 deletions(-)
b11b5f
b11b5f
diff --git a/src/login/logind.c b/src/login/logind.c
b11b5f
index 6b576dad0d..bb1d3f3523 100644
b11b5f
--- a/src/login/logind.c
b11b5f
+++ b/src/login/logind.c
b11b5f
@@ -1027,18 +1027,33 @@ static int manager_dispatch_idle_action(sd_event_source *s, uint64_t t, void *us
b11b5f
         n = now(CLOCK_MONOTONIC);
b11b5f
 
b11b5f
         r = manager_get_idle_hint(m, &since);
b11b5f
-        if (r <= 0)
b11b5f
+        if (r <= 0) {
b11b5f
                 /* Not idle. Let's check if after a timeout it might be idle then. */
b11b5f
                 elapse = n + m->idle_action_usec;
b11b5f
-        else {
b11b5f
+                m->was_idle = false;
b11b5f
+        } else {
b11b5f
+
b11b5f
                 /* Idle! Let's see if it's time to do something, or if
b11b5f
                  * we shall sleep for longer. */
b11b5f
 
b11b5f
                 if (n >= since.monotonic + m->idle_action_usec &&
b11b5f
                     (m->idle_action_not_before_usec <= 0 || n >= m->idle_action_not_before_usec + m->idle_action_usec)) {
b11b5f
-                        log_info("System idle. Taking action.");
b11b5f
+                        bool is_edge = false;
b11b5f
+
b11b5f
+                        /* We weren't idle previously or some activity happened while we were sleeping, and now we are
b11b5f
+                         * idle. Let's remember that for the next time and make this an edge transition. */
b11b5f
+                        if (!m->was_idle || since.monotonic >= m->idle_action_not_before_usec) {
b11b5f
+                                is_edge = true;
b11b5f
+                                m->was_idle = true;
b11b5f
+                        }
b11b5f
+
b11b5f
+                        if (m->idle_action == HANDLE_LOCK && !is_edge)
b11b5f
+                                /* We are idle and we were before so we are actually not taking any action. */
b11b5f
+                                log_debug("System idle.");
b11b5f
+                        else
b11b5f
+                                log_info("System idle. Doing %s operation.", handle_action_to_string(m->idle_action));
b11b5f
 
b11b5f
-                        manager_handle_action(m, 0, m->idle_action, false, false);
b11b5f
+                        manager_handle_action(m, 0, m->idle_action, false, is_edge);
b11b5f
                         m->idle_action_not_before_usec = n;
b11b5f
                 }
b11b5f
 
b11b5f
diff --git a/src/login/logind.h b/src/login/logind.h
b11b5f
index 606adf4fe6..b9b4a5113f 100644
b11b5f
--- a/src/login/logind.h
b11b5f
+++ b/src/login/logind.h
b11b5f
@@ -101,6 +101,7 @@ struct Manager {
b11b5f
         usec_t idle_action_usec;
b11b5f
         usec_t idle_action_not_before_usec;
b11b5f
         HandleAction idle_action;
b11b5f
+        bool was_idle;
b11b5f
 
b11b5f
         usec_t stop_idle_session_usec;
b11b5f