daandemeyer / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
b677e7
From 89dd5e016a50da082e51129eecb3c5e04b8f0cf5 Mon Sep 17 00:00:00 2001
b677e7
From: Lennart Poettering <lennart@poettering.net>
b677e7
Date: Wed, 8 Aug 2018 16:04:40 +0200
b677e7
Subject: [PATCH] logind: automatically GC lingering users for who now
b677e7
 user@.service (nor slice, not runtime dir service) is running anymore
b677e7
b677e7
This heavily borrows from @intelfx' PR #5546, but watches all three
b677e7
units that are associated with a user now: the slice, the user@.service
b677e7
and user-runtime-dir@.service.
b677e7
b677e7
The logic and reasoning behind it is the same though: there's no value
b677e7
in keeping lingering users around if all their three services are gone.
b677e7
b677e7
Replaces: #5546
b677e7
Fixes: #4162
b677e7
(cherry picked from commit 4e5b605af202c770dfc8e3562d0f8d0440b2fe14)
b677e7
b677e7
Related: #1642460
b677e7
---
b677e7
 src/login/logind-user.c | 28 +++++++++++++++++++++++++---
b677e7
 1 file changed, 25 insertions(+), 3 deletions(-)
b677e7
b677e7
diff --git a/src/login/logind-user.c b/src/login/logind-user.c
b677e7
index 3fd28fc66c..bba3158d1a 100644
b677e7
--- a/src/login/logind-user.c
b677e7
+++ b/src/login/logind-user.c
b677e7
@@ -26,6 +26,7 @@
b677e7
 #include "special.h"
b677e7
 #include "stdio-util.h"
b677e7
 #include "string-table.h"
b677e7
+#include "strv.h"
b677e7
 #include "unit-name.h"
b677e7
 #include "user-util.h"
b677e7
 #include "util.h"
b677e7
@@ -542,6 +543,25 @@ int user_check_linger_file(User *u) {
b677e7
         return true;
b677e7
 }
b677e7
 
b677e7
+static bool user_unit_active(User *u) {
b677e7
+        const char *i;
b677e7
+        int r;
b677e7
+
b677e7
+        assert(u->service);
b677e7
+        assert(u->runtime_dir_service);
b677e7
+        assert(u->slice);
b677e7
+
b677e7
+        FOREACH_STRING(i, u->service, u->runtime_dir_service, u->slice) {
b677e7
+                r = manager_unit_is_active(u->manager, i);
b677e7
+                if (r < 0)
b677e7
+                        log_debug_errno(r, "Failed to determine whether unit '%s' is active, ignoring", u->service);
b677e7
+                if (r != 0)
b677e7
+                        return true;
b677e7
+        }
b677e7
+
b677e7
+        return false;
b677e7
+}
b677e7
+
b677e7
 bool user_may_gc(User *u, bool drop_not_started) {
b677e7
         assert(u);
b677e7
 
b677e7
@@ -561,8 +581,10 @@ bool user_may_gc(User *u, bool drop_not_started) {
b677e7
                         return false; /* Leave it around for a bit longer. */
b677e7
         }
b677e7
 
b677e7
-        /* Is this a user that shall stay around forever? */
b677e7
-        if (user_check_linger_file(u) > 0)
b677e7
+        /* Is this a user that shall stay around forever ("linger")? Before we say "no" to GC'ing for lingering users, let's check
b677e7
+         * if any of the three units that we maintain for this user is still around. If none of them is,
b677e7
+         * there's no need to keep this user around even if lingering is enabled. */
b677e7
+        if (user_check_linger_file(u) > 0 && user_unit_active(u))
b677e7
                 return false;
b677e7
 
b677e7
         if (u->service_job && manager_job_is_active(u->manager, u->service_job))
b677e7
@@ -608,7 +630,7 @@ UserState user_get_state(User *u) {
b677e7
                 return all_closing ? USER_CLOSING : USER_ONLINE;
b677e7
         }
b677e7
 
b677e7
-        if (user_check_linger_file(u) > 0)
b677e7
+        if (user_check_linger_file(u) > 0 && user_unit_active(u))
b677e7
                 return USER_LINGERING;
b677e7
 
b677e7
         return USER_CLOSING;