ryantimwilson / rpms / systemd

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