ryantimwilson / rpms / systemd

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