daandemeyer / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
b12df0
From 35455408393cb29a5e49fd769c4823b6a6f886b4 Mon Sep 17 00:00:00 2001
b12df0
From: Lennart Poettering <lennart@poettering.net>
b12df0
Date: Tue, 7 Aug 2018 11:02:00 +0200
b12df0
Subject: [PATCH] logind: optionally, keep the user@.service instance for
b12df0
 eached logged in user around for a while
b12df0
b12df0
This should speed up rapid logout/login cycles a bit.
b12df0
b12df0
By default this timeout is now set to 10s.
b12df0
b12df0
Fixes: #8410
b12df0
Replaces: #4434
b12df0
(cherry picked from commit 9afe9efb9340588db553950727a2a9672dc3db24)
b12df0
b12df0
Resolves: #1642460
b12df0
---
b12df0
 man/logind.conf.xml          | 11 +++++
b12df0
 src/login/logind-core.c      |  2 +
b12df0
 src/login/logind-dbus.c      |  1 +
b12df0
 src/login/logind-gperf.gperf |  1 +
b12df0
 src/login/logind-session.c   |  4 ++
b12df0
 src/login/logind-user.c      | 88 +++++++++++++++++++++++++++++++++---
b12df0
 src/login/logind-user.h      |  7 ++-
b12df0
 src/login/logind.h           |  1 +
b12df0
 8 files changed, 107 insertions(+), 8 deletions(-)
b12df0
b12df0
diff --git a/man/logind.conf.xml b/man/logind.conf.xml
b12df0
index 7d7e869a26..0cf8a7d1f2 100644
b12df0
--- a/man/logind.conf.xml
b12df0
+++ b/man/logind.conf.xml
b12df0
@@ -184,6 +184,17 @@
b12df0
         5.</para></listitem>
b12df0
       </varlistentry>
b12df0
 
b12df0
+      <varlistentry>
b12df0
+        <term><varname>UserStopDelaySec=</varname></term>
b12df0
+
b12df0
+        <listitem><para>Specifies how long to keep the user record and per-user service
b12df0
+        <filename>user@.service</filename> around for a user after they logged out fully. If set to zero, the per-user
b12df0
+        service is terminated immediately when the last session of the user has ended. If this option is configured to
b12df0
+        non-zero rapid logout/login cycles are sped up, as the user's service manager is not constantly restarted. If
b12df0
+        set to <literal>infinity</literal> the per-user service for a user is never terminated again after first login,
b12df0
+        and continues to run until system shutdown. Defaults to 10s.</para></listitem>
b12df0
+      </varlistentry>
b12df0
+
b12df0
       <varlistentry>
b12df0
         <term><varname>HandlePowerKey=</varname></term>
b12df0
         <term><varname>HandleSuspendKey=</varname></term>
b12df0
diff --git a/src/login/logind-core.c b/src/login/logind-core.c
b12df0
index f598bbaa1c..678c708df1 100644
b12df0
--- a/src/login/logind-core.c
b12df0
+++ b/src/login/logind-core.c
b12df0
@@ -27,6 +27,8 @@ void manager_reset_config(Manager *m) {
b12df0
         m->reserve_vt = 6;
b12df0
         m->remove_ipc = false;
b12df0
         m->inhibit_delay_max = 5 * USEC_PER_SEC;
b12df0
+        m->user_stop_delay = 10 * USEC_PER_SEC;
b12df0
+
b12df0
         m->handle_power_key = HANDLE_POWEROFF;
b12df0
         m->handle_suspend_key = HANDLE_SUSPEND;
b12df0
         m->handle_hibernate_key = HANDLE_HIBERNATE;
b12df0
diff --git a/src/login/logind-dbus.c b/src/login/logind-dbus.c
b12df0
index 7eba617fff..6586280269 100644
b12df0
--- a/src/login/logind-dbus.c
b12df0
+++ b/src/login/logind-dbus.c
b12df0
@@ -2695,6 +2695,7 @@ const sd_bus_vtable manager_vtable[] = {
b12df0
         SD_BUS_PROPERTY("BlockInhibited", "s", property_get_inhibited, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
b12df0
         SD_BUS_PROPERTY("DelayInhibited", "s", property_get_inhibited, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
b12df0
         SD_BUS_PROPERTY("InhibitDelayMaxUSec", "t", NULL, offsetof(Manager, inhibit_delay_max), SD_BUS_VTABLE_PROPERTY_CONST),
b12df0
+        SD_BUS_PROPERTY("UserStopDelayUSec", "t", NULL, offsetof(Manager, user_stop_delay), SD_BUS_VTABLE_PROPERTY_CONST),
b12df0
         SD_BUS_PROPERTY("HandlePowerKey", "s", property_get_handle_action, offsetof(Manager, handle_power_key), SD_BUS_VTABLE_PROPERTY_CONST),
b12df0
         SD_BUS_PROPERTY("HandleSuspendKey", "s", property_get_handle_action, offsetof(Manager, handle_suspend_key), SD_BUS_VTABLE_PROPERTY_CONST),
b12df0
         SD_BUS_PROPERTY("HandleHibernateKey", "s", property_get_handle_action, offsetof(Manager, handle_hibernate_key), SD_BUS_VTABLE_PROPERTY_CONST),
b12df0
diff --git a/src/login/logind-gperf.gperf b/src/login/logind-gperf.gperf
b12df0
index c85339dcd3..8829ce7d85 100644
b12df0
--- a/src/login/logind-gperf.gperf
b12df0
+++ b/src/login/logind-gperf.gperf
b12df0
@@ -23,6 +23,7 @@ Login.KillUserProcesses,            config_parse_bool,                  0, offse
b12df0
 Login.KillOnlyUsers,                config_parse_strv,                  0, offsetof(Manager, kill_only_users)
b12df0
 Login.KillExcludeUsers,             config_parse_strv,                  0, offsetof(Manager, kill_exclude_users)
b12df0
 Login.InhibitDelayMaxSec,           config_parse_sec,                   0, offsetof(Manager, inhibit_delay_max)
b12df0
+Login.UserStopDelaySec,             config_parse_sec,                   0, offsetof(Manager, user_stop_delay)
b12df0
 Login.HandlePowerKey,               config_parse_handle_action,         0, offsetof(Manager, handle_power_key)
b12df0
 Login.HandleSuspendKey,             config_parse_handle_action,         0, offsetof(Manager, handle_suspend_key)
b12df0
 Login.HandleHibernateKey,           config_parse_handle_action,         0, offsetof(Manager, handle_hibernate_key)
b12df0
diff --git a/src/login/logind-session.c b/src/login/logind-session.c
b12df0
index d56b48a732..dd4ac9482a 100644
b12df0
--- a/src/login/logind-session.c
b12df0
+++ b/src/login/logind-session.c
b12df0
@@ -101,6 +101,8 @@ Session* session_free(Session *s) {
b12df0
 
b12df0
                 if (s->user->display == s)
b12df0
                         s->user->display = NULL;
b12df0
+
b12df0
+                user_update_last_session_timer(s->user);
b12df0
         }
b12df0
 
b12df0
         if (s->seat) {
b12df0
@@ -142,6 +144,8 @@ void session_set_user(Session *s, User *u) {
b12df0
 
b12df0
         s->user = u;
b12df0
         LIST_PREPEND(sessions_by_user, u->sessions, s);
b12df0
+
b12df0
+        user_update_last_session_timer(u);
b12df0
 }
b12df0
 
b12df0
 static void session_save_devices(Session *s, FILE *f) {
b12df0
diff --git a/src/login/logind-user.c b/src/login/logind-user.c
b12df0
index 39fc76f4dc..f23fcbe674 100644
b12df0
--- a/src/login/logind-user.c
b12df0
+++ b/src/login/logind-user.c
b12df0
@@ -47,6 +47,7 @@ int user_new(User **ret, Manager *m, uid_t uid, gid_t gid, const char *name) {
b12df0
                 .manager = m,
b12df0
                 .uid = uid,
b12df0
                 .gid = gid,
b12df0
+                .last_session_timestamp = USEC_INFINITY,
b12df0
         };
b12df0
 
b12df0
         u->name = strdup(name);
b12df0
@@ -113,6 +114,8 @@ User *user_free(User *u) {
b12df0
 
b12df0
         hashmap_remove_value(u->manager->users, UID_TO_PTR(u->uid), u);
b12df0
 
b12df0
+        (void) sd_event_source_unref(u->timer_event_source);
b12df0
+
b12df0
         u->service_job = mfree(u->service_job);
b12df0
 
b12df0
         u->service = mfree(u->service);
b12df0
@@ -170,6 +173,10 @@ static int user_save_internal(User *u) {
b12df0
                         u->timestamp.realtime,
b12df0
                         u->timestamp.monotonic);
b12df0
 
b12df0
+        if (u->last_session_timestamp != USEC_INFINITY)
b12df0
+                fprintf(f, "LAST_SESSION_TIMESTAMP=" USEC_FMT "\n",
b12df0
+                        u->last_session_timestamp);
b12df0
+
b12df0
         if (u->sessions) {
b12df0
                 Session *i;
b12df0
                 bool first;
b12df0
@@ -287,16 +294,17 @@ int user_save(User *u) {
b12df0
 }
b12df0
 
b12df0
 int user_load(User *u) {
b12df0
-        _cleanup_free_ char *realtime = NULL, *monotonic = NULL, *stopping = NULL;
b12df0
+        _cleanup_free_ char *realtime = NULL, *monotonic = NULL, *stopping = NULL, *last_session_timestamp = NULL;
b12df0
         int r;
b12df0
 
b12df0
         assert(u);
b12df0
 
b12df0
         r = parse_env_file(NULL, u->state_file, NEWLINE,
b12df0
-                           "SERVICE_JOB", &u->service_job,
b12df0
-                           "STOPPING",    &stopping,
b12df0
-                           "REALTIME",    &realtime,
b12df0
-                           "MONOTONIC",   &monotonic,
b12df0
+                           "SERVICE_JOB",            &u->service_job,
b12df0
+                           "STOPPING",               &stopping,
b12df0
+                           "REALTIME",               &realtime,
b12df0
+                           "MONOTONIC",              &monotonic,
b12df0
+                           "LAST_SESSION_TIMESTAMP", &last_session_timestamp,
b12df0
                            NULL);
b12df0
         if (r == -ENOENT)
b12df0
                 return 0;
b12df0
@@ -312,9 +320,11 @@ int user_load(User *u) {
b12df0
         }
b12df0
 
b12df0
         if (realtime)
b12df0
-                timestamp_deserialize(realtime, &u->timestamp.realtime);
b12df0
+                (void) timestamp_deserialize(realtime, &u->timestamp.realtime);
b12df0
         if (monotonic)
b12df0
-                timestamp_deserialize(monotonic, &u->timestamp.monotonic);
b12df0
+                (void) timestamp_deserialize(monotonic, &u->timestamp.monotonic);
b12df0
+        if (last_session_timestamp)
b12df0
+                (void) timestamp_deserialize(last_session_timestamp, &u->last_session_timestamp);
b12df0
 
b12df0
         return 0;
b12df0
 }
b12df0
@@ -524,6 +534,17 @@ bool user_may_gc(User *u, bool drop_not_started) {
b12df0
         if (u->sessions)
b12df0
                 return false;
b12df0
 
b12df0
+        if (u->last_session_timestamp != USEC_INFINITY) {
b12df0
+                /* All sessions have been closed. Let's see if we shall leave the user record around for a bit */
b12df0
+
b12df0
+                if (u->manager->user_stop_delay == USEC_INFINITY)
b12df0
+                        return false; /* Leave it around forever! */
b12df0
+                if (u->manager->user_stop_delay > 0 &&
b12df0
+                    now(CLOCK_MONOTONIC) < usec_add(u->last_session_timestamp, u->manager->user_stop_delay))
b12df0
+                        return false; /* Leave it around for a bit longer. */
b12df0
+        }
b12df0
+
b12df0
+        /* Is this a user that shall stay around forever? */
b12df0
         if (user_check_linger_file(u) > 0)
b12df0
                 return false;
b12df0
 
b12df0
@@ -649,6 +670,59 @@ void user_elect_display(User *u) {
b12df0
         }
b12df0
 }
b12df0
 
b12df0
+static int user_stop_timeout_callback(sd_event_source *es, uint64_t usec, void *userdata) {
b12df0
+        User *u = userdata;
b12df0
+
b12df0
+        assert(u);
b12df0
+        user_add_to_gc_queue(u);
b12df0
+
b12df0
+        return 0;
b12df0
+}
b12df0
+
b12df0
+void user_update_last_session_timer(User *u) {
b12df0
+        int r;
b12df0
+
b12df0
+        assert(u);
b12df0
+
b12df0
+        if (u->sessions) {
b12df0
+                /* There are sessions, turn off the timer */
b12df0
+                u->last_session_timestamp = USEC_INFINITY;
b12df0
+                u->timer_event_source = sd_event_source_unref(u->timer_event_source);
b12df0
+                return;
b12df0
+        }
b12df0
+
b12df0
+        if (u->last_session_timestamp != USEC_INFINITY)
b12df0
+                return; /* Timer already started */
b12df0
+
b12df0
+        u->last_session_timestamp = now(CLOCK_MONOTONIC);
b12df0
+
b12df0
+        assert(!u->timer_event_source);
b12df0
+
b12df0
+        if (u->manager->user_stop_delay == 0 || u->manager->user_stop_delay == USEC_INFINITY)
b12df0
+                return;
b12df0
+
b12df0
+        if (sd_event_get_state(u->manager->event) == SD_EVENT_FINISHED) {
b12df0
+                log_debug("Not allocating user stop timeout, since we are already exiting.");
b12df0
+                return;
b12df0
+        }
b12df0
+
b12df0
+        r = sd_event_add_time(u->manager->event,
b12df0
+                              &u->timer_event_source,
b12df0
+                              CLOCK_MONOTONIC,
b12df0
+                              usec_add(u->last_session_timestamp, u->manager->user_stop_delay), 0,
b12df0
+                              user_stop_timeout_callback, u);
b12df0
+        if (r < 0)
b12df0
+                log_warning_errno(r, "Failed to enqueue user stop event source, ignoring: %m");
b12df0
+
b12df0
+        if (DEBUG_LOGGING) {
b12df0
+                char s[FORMAT_TIMESPAN_MAX];
b12df0
+
b12df0
+                log_debug("Last session of user '%s' logged out, terminating user context in %s.",
b12df0
+                          u->name,
b12df0
+                          format_timespan(s, sizeof(s), u->manager->user_stop_delay, USEC_PER_MSEC));
b12df0
+        }
b12df0
+}
b12df0
+
b12df0
 static const char* const user_state_table[_USER_STATE_MAX] = {
b12df0
         [USER_OFFLINE] = "offline",
b12df0
         [USER_OPENING] = "opening",
b12df0
diff --git a/src/login/logind-user.h b/src/login/logind-user.h
b12df0
index 5e1f7b813a..e05646adc9 100644
b12df0
--- a/src/login/logind-user.h
b12df0
+++ b/src/login/logind-user.h
b12df0
@@ -34,7 +34,11 @@ struct User {
b12df0
 
b12df0
         Session *display;
b12df0
 
b12df0
-        dual_timestamp timestamp;
b12df0
+        dual_timestamp timestamp;      /* When this User object was 'started' the first time */
b12df0
+        usec_t last_session_timestamp; /* When the number of sessions of this user went from 1 to 0 the last time */
b12df0
+
b12df0
+        /* Set up when the last session of the user logs out */
b12df0
+        sd_event_source *timer_event_source;
b12df0
 
b12df0
         bool in_gc_queue:1;
b12df0
 
b12df0
@@ -62,6 +66,7 @@ int user_load(User *u);
b12df0
 int user_kill(User *u, int signo);
b12df0
 int user_check_linger_file(User *u);
b12df0
 void user_elect_display(User *u);
b12df0
+void user_update_last_session_timer(User *u);
b12df0
 
b12df0
 extern const sd_bus_vtable user_vtable[];
b12df0
 int user_node_enumerator(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *error);
b12df0
diff --git a/src/login/logind.h b/src/login/logind.h
b12df0
index ae4d74076b..7288dd7445 100644
b12df0
--- a/src/login/logind.h
b12df0
+++ b/src/login/logind.h
b12df0
@@ -62,6 +62,7 @@ struct Manager {
b12df0
         Hashmap *user_units;
b12df0
 
b12df0
         usec_t inhibit_delay_max;
b12df0
+        usec_t user_stop_delay;
b12df0
 
b12df0
         /* If an action is currently being executed or is delayed,
b12df0
          * this is != 0 and encodes what is being done */