naccyde / rpms / systemd

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