richardphibel / rpms / systemd

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