86b31d
From 24439b08e3a3437b423553c385cde1d4cddf18f6 Mon Sep 17 00:00:00 2001
86b31d
From: Michal Sekletar <msekleta@redhat.com>
86b31d
Date: Mon, 8 Aug 2022 09:13:50 +0200
86b31d
Subject: [PATCH] logind: add option to stop idle sessions after specified
86b31d
 timeout
86b31d
86b31d
Thanks to Jan Pazdziora <jpazdziora@redhat.com> for providing a patch
86b31d
which implemeted a PoC of this feature.
86b31d
86b31d
(cherry picked from commit 82325af3ae41bc7efb3d5cd8f56a4652fef498c2)
86b31d
86b31d
Resolves: #2122288
86b31d
---
86b31d
 man/logind.conf.xml          | 11 ++++++
86b31d
 src/login/logind-core.c      |  2 +
86b31d
 src/login/logind-dbus.c      |  1 +
86b31d
 src/login/logind-gperf.gperf |  1 +
86b31d
 src/login/logind-session.c   | 72 +++++++++++++++++++++++++++++++++---
86b31d
 src/login/logind-session.h   |  2 +
86b31d
 src/login/logind.conf.in     |  1 +
86b31d
 src/login/logind.h           |  2 +
86b31d
 8 files changed, 86 insertions(+), 6 deletions(-)
86b31d
86b31d
diff --git a/man/logind.conf.xml b/man/logind.conf.xml
86b31d
index 0cf8a7d1f2..00b5b1f2e8 100644
86b31d
--- a/man/logind.conf.xml
86b31d
+++ b/man/logind.conf.xml
86b31d
@@ -333,6 +333,17 @@
86b31d
         are excluded from the effect of this setting. Defaults to <literal>no</literal>.</para></listitem>
86b31d
       </varlistentry>
86b31d
 
86b31d
+      <varlistentry>
86b31d
+        <term><varname>StopIdleSessionSec=</varname></term>
86b31d
+
86b31d
+        <listitem><para>Specifies a timeout in seconds, or a time span value after which
86b31d
+        <filename>systemd-logind</filename> checks the idle state of all sessions. Every session that is idle for
86b31d
+        longer then the timeout will be stopped. Defaults to <literal>infinity</literal>
86b31d
+        (<filename>systemd-logind</filename> is not checking the idle state of sessions). For details about the syntax
86b31d
+        of time spans, see
86b31d
+        <citerefentry><refentrytitle>systemd.time</refentrytitle><manvolnum>7</manvolnum></citerefentry>.
86b31d
+        </para></listitem>
86b31d
+      </varlistentry>
86b31d
     </variablelist>
86b31d
   </refsect1>
86b31d
 
86b31d
diff --git a/src/login/logind-core.c b/src/login/logind-core.c
86b31d
index a1943b6f9d..abe6eecffb 100644
86b31d
--- a/src/login/logind-core.c
86b31d
+++ b/src/login/logind-core.c
86b31d
@@ -58,6 +58,8 @@ void manager_reset_config(Manager *m) {
86b31d
 
86b31d
         m->kill_only_users = strv_free(m->kill_only_users);
86b31d
         m->kill_exclude_users = strv_free(m->kill_exclude_users);
86b31d
+
86b31d
+        m->stop_idle_session_usec = USEC_INFINITY;
86b31d
 }
86b31d
 
86b31d
 int manager_parse_config_file(Manager *m) {
86b31d
diff --git a/src/login/logind-dbus.c b/src/login/logind-dbus.c
86b31d
index 01bfef4ff7..81aacb4eed 100644
86b31d
--- a/src/login/logind-dbus.c
86b31d
+++ b/src/login/logind-dbus.c
86b31d
@@ -2720,6 +2720,7 @@ const sd_bus_vtable manager_vtable[] = {
86b31d
         SD_BUS_PROPERTY("SessionsMax", "t", NULL, offsetof(Manager, sessions_max), SD_BUS_VTABLE_PROPERTY_CONST),
86b31d
         SD_BUS_PROPERTY("NCurrentSessions", "t", property_get_hashmap_size, offsetof(Manager, sessions), 0),
86b31d
         SD_BUS_PROPERTY("UserTasksMax", "t", property_get_compat_user_tasks_max, 0, SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_HIDDEN),
86b31d
+        SD_BUS_PROPERTY("StopIdleSessionUSec", "t", NULL, offsetof(Manager, stop_idle_session_usec), SD_BUS_VTABLE_PROPERTY_CONST),
86b31d
 
86b31d
         SD_BUS_METHOD_WITH_NAMES("GetSession",
86b31d
                                  "s",
86b31d
diff --git a/src/login/logind-gperf.gperf b/src/login/logind-gperf.gperf
86b31d
index 8829ce7d85..214ac5c4a3 100644
86b31d
--- a/src/login/logind-gperf.gperf
86b31d
+++ b/src/login/logind-gperf.gperf
86b31d
@@ -42,3 +42,4 @@ Login.RemoveIPC,                    config_parse_bool,                  0, offse
86b31d
 Login.InhibitorsMax,                config_parse_uint64,                0, offsetof(Manager, inhibitors_max)
86b31d
 Login.SessionsMax,                  config_parse_uint64,                0, offsetof(Manager, sessions_max)
86b31d
 Login.UserTasksMax,                 config_parse_compat_user_tasks_max, 0, offsetof(Manager, user_tasks_max)
86b31d
+Login.StopIdleSessionSec,           config_parse_sec_fix_0,             0, offsetof(Manager, stop_idle_session_usec)
86b31d
diff --git a/src/login/logind-session.c b/src/login/logind-session.c
86b31d
index cc838ca383..56f40fbec4 100644
86b31d
--- a/src/login/logind-session.c
86b31d
+++ b/src/login/logind-session.c
86b31d
@@ -29,6 +29,7 @@
86b31d
 #include "string-table.h"
86b31d
 #include "strv.h"
86b31d
 #include "terminal-util.h"
86b31d
+#include "time-util.h"
86b31d
 #include "user-util.h"
86b31d
 #include "util.h"
86b31d
 
86b31d
@@ -139,6 +140,8 @@ Session* session_free(Session *s) {
86b31d
 
86b31d
         free(s->state_file);
86b31d
 
86b31d
+        sd_event_source_unref(s->stop_on_idle_event_source);
86b31d
+
86b31d
         return mfree(s);
86b31d
 }
86b31d
 
86b31d
@@ -658,6 +661,55 @@ static int session_start_scope(Session *s, sd_bus_message *properties, sd_bus_er
86b31d
         return 0;
86b31d
 }
86b31d
 
86b31d
+static int session_dispatch_stop_on_idle(sd_event_source *source, uint64_t t, void *userdata) {
86b31d
+        Session *s = userdata;
86b31d
+        dual_timestamp ts;
86b31d
+        int r, idle;
86b31d
+
86b31d
+        assert(s);
86b31d
+
86b31d
+        if (s->stopping)
86b31d
+                return 0;
86b31d
+
86b31d
+        idle = session_get_idle_hint(s, &ts);
86b31d
+        if (idle) {
86b31d
+                log_debug("Session \"%s\" of user \"%s\" is idle, stopping.", s->id, s->user->name);
86b31d
+
86b31d
+                return session_stop(s, /* force */ true);
86b31d
+        }
86b31d
+
86b31d
+        r = sd_event_source_set_time(source, usec_add(ts.monotonic, s->manager->stop_idle_session_usec));
86b31d
+        if (r < 0)
86b31d
+                return log_error_errno(r, "Failed to configure stop on idle session event source: %m");
86b31d
+
86b31d
+        r = sd_event_source_set_enabled(source, SD_EVENT_ONESHOT);
86b31d
+        if (r < 0)
86b31d
+                return log_error_errno(r, "Failed to enable stop on idle session event source: %m");
86b31d
+
86b31d
+        return 1;
86b31d
+}
86b31d
+
86b31d
+static int session_setup_stop_on_idle_timer(Session *s) {
86b31d
+        int r;
86b31d
+
86b31d
+        assert(s);
86b31d
+
86b31d
+        if (s->manager->stop_idle_session_usec == USEC_INFINITY)
86b31d
+                return 0;
86b31d
+
86b31d
+        r = sd_event_add_time_relative(
86b31d
+                        s->manager->event,
86b31d
+                        &s->stop_on_idle_event_source,
86b31d
+                        CLOCK_MONOTONIC,
86b31d
+                        s->manager->stop_idle_session_usec,
86b31d
+                        0,
86b31d
+                        session_dispatch_stop_on_idle, s);
86b31d
+        if (r < 0)
86b31d
+                return log_error_errno(r, "Failed to add stop on idle session event source: %m");
86b31d
+
86b31d
+        return 0;
86b31d
+}
86b31d
+
86b31d
 int session_start(Session *s, sd_bus_message *properties, sd_bus_error *error) {
86b31d
         int r;
86b31d
 
86b31d
@@ -680,6 +732,10 @@ int session_start(Session *s, sd_bus_message *properties, sd_bus_error *error) {
86b31d
         if (r < 0)
86b31d
                 return r;
86b31d
 
86b31d
+        r = session_setup_stop_on_idle_timer(s);
86b31d
+        if (r < 0)
86b31d
+                return r;
86b31d
+
86b31d
         log_struct(s->class == SESSION_BACKGROUND ? LOG_DEBUG : LOG_INFO,
86b31d
                    "MESSAGE_ID=" SD_MESSAGE_SESSION_START_STR,
86b31d
                    "SESSION_ID=%s", s->id,
86b31d
@@ -917,7 +973,7 @@ static int get_process_ctty_atime(pid_t pid, usec_t *atime) {
86b31d
 }
86b31d
 
86b31d
 int session_get_idle_hint(Session *s, dual_timestamp *t) {
86b31d
-        usec_t atime = 0, n;
86b31d
+        usec_t atime = 0, dtime = 0;
86b31d
         int r;
86b31d
 
86b31d
         assert(s);
86b31d
@@ -961,12 +1017,16 @@ found_atime:
86b31d
         if (t)
86b31d
                 dual_timestamp_from_realtime(t, atime);
86b31d
 
86b31d
-        n = now(CLOCK_REALTIME);
86b31d
-
86b31d
-        if (s->manager->idle_action_usec <= 0)
86b31d
-                return 0;
86b31d
+        if (s->manager->idle_action_usec > 0 && s->manager->stop_idle_session_usec != USEC_INFINITY)
86b31d
+                dtime = MIN(s->manager->idle_action_usec, s->manager->stop_idle_session_usec);
86b31d
+        else if (s->manager->idle_action_usec > 0)
86b31d
+                dtime = s->manager->idle_action_usec;
86b31d
+        else if (s->manager->stop_idle_session_usec != USEC_INFINITY)
86b31d
+                dtime = s->manager->stop_idle_session_usec;
86b31d
+        else
86b31d
+                return false;
86b31d
 
86b31d
-        return atime + s->manager->idle_action_usec <= n;
86b31d
+        return usec_add(atime, dtime) <= now(CLOCK_REALTIME);
86b31d
 }
86b31d
 
86b31d
 void session_set_idle_hint(Session *s, bool b) {
86b31d
diff --git a/src/login/logind-session.h b/src/login/logind-session.h
86b31d
index 8c7d0301f2..6678441bb9 100644
86b31d
--- a/src/login/logind-session.h
86b31d
+++ b/src/login/logind-session.h
86b31d
@@ -112,6 +112,8 @@ struct Session {
86b31d
         Hashmap *devices;
86b31d
         sd_bus_track *track;
86b31d
 
86b31d
+        sd_event_source *stop_on_idle_event_source;
86b31d
+
86b31d
         LIST_FIELDS(Session, sessions_by_user);
86b31d
         LIST_FIELDS(Session, sessions_by_seat);
86b31d
 
86b31d
diff --git a/src/login/logind.conf.in b/src/login/logind.conf.in
86b31d
index c7346f9819..a62c2b0b57 100644
86b31d
--- a/src/login/logind.conf.in
86b31d
+++ b/src/login/logind.conf.in
86b31d
@@ -35,3 +35,4 @@
86b31d
 #RemoveIPC=no
86b31d
 #InhibitorsMax=8192
86b31d
 #SessionsMax=8192
86b31d
+#StopIdleSessionSec=infinity
86b31d
diff --git a/src/login/logind.h b/src/login/logind.h
86b31d
index 7f94dea2f6..606adf4fe6 100644
86b31d
--- a/src/login/logind.h
86b31d
+++ b/src/login/logind.h
86b31d
@@ -102,6 +102,8 @@ struct Manager {
86b31d
         usec_t idle_action_not_before_usec;
86b31d
         HandleAction idle_action;
86b31d
 
86b31d
+        usec_t stop_idle_session_usec;
86b31d
+
86b31d
         HandleAction handle_power_key;
86b31d
         HandleAction handle_suspend_key;
86b31d
         HandleAction handle_hibernate_key;