]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
login: keep user->slice constant
authorDavid Herrmann <dh.herrmann@gmail.com>
Tue, 29 Sep 2015 09:03:04 +0000 (11:03 +0200)
committerDavid Herrmann <dh.herrmann@gmail.com>
Mon, 16 Nov 2015 14:34:41 +0000 (15:34 +0100)
Currently, we allocate user->slice when starting a slice, but we never
release it. This is incompatible if we want to re-use a user object once
it was stopped. Hence, make sure user->slice is allocated statically on
the user object and use "u->started || u->stopping" as an indication
whether the slice is actually available on pid1 or not.

src/login/logind-user.c

index 92f3b75a85e3b668e6de0d7c5c0b19916e1bb549..d985d19c466686eb7de52a8fcedac77b2f02c4b1 100644 (file)
@@ -52,7 +52,9 @@
 #include "util.h"
 
 User* user_new(Manager *m, uid_t uid, gid_t gid, const char *name) {
+        char lu[DECIMAL_STR_MAX(uid_t) + 1];
         User *u;
+        int r;
 
         assert(m);
         assert(name);
@@ -71,9 +73,17 @@ User* user_new(Manager *m, uid_t uid, gid_t gid, const char *name) {
         if (asprintf(&u->runtime_path, "/run/user/"UID_FMT, uid) < 0)
                 goto fail;
 
+        sprintf(lu, UID_FMT, uid);
+        r = slice_build_subslice(SPECIAL_USER_SLICE, lu, &u->slice);
+        if (r < 0)
+                goto fail;
+
         if (hashmap_put(m->users, UID_TO_PTR(uid), u) < 0)
                 goto fail;
 
+        if (hashmap_put(m->user_units, u->slice, u) < 0)
+                goto fail;
+
         u->manager = m;
         u->uid = uid;
         u->gid = gid;
@@ -81,6 +91,10 @@ User* user_new(Manager *m, uid_t uid, gid_t gid, const char *name) {
         return u;
 
 fail:
+        if (u->slice)
+                hashmap_remove(m->user_units, u->slice);
+        hashmap_remove(m->users, UID_TO_PTR(uid));
+        free(u->slice);
         free(u->runtime_path);
         free(u->state_file);
         free(u->name);
@@ -98,23 +112,20 @@ void user_free(User *u) {
         while (u->sessions)
                 session_free(u->sessions);
 
-        if (u->slice) {
-                hashmap_remove(u->manager->user_units, u->slice);
-                free(u->slice);
-        }
-
         if (u->service) {
                 hashmap_remove(u->manager->user_units, u->service);
                 free(u->service);
         }
 
+        hashmap_remove(u->manager->user_units, u->slice);
+        hashmap_remove(u->manager->users, UID_TO_PTR(u->uid));
+
         free(u->slice_job);
         free(u->service_job);
 
+        free(u->slice);
         free(u->runtime_path);
 
-        hashmap_remove(u->manager->users, UID_TO_PTR(u->uid));
-
         free(u->name);
         free(u->state_file);
         free(u);
@@ -154,8 +165,6 @@ static int user_save_internal(User *u) {
         if (u->service_job)
                 fprintf(f, "SERVICE_JOB=%s\n", u->service_job);
 
-        if (u->slice)
-                fprintf(f, "SLICE=%s\n", u->slice);
         if (u->slice_job)
                 fprintf(f, "SLICE_JOB=%s\n", u->slice_job);
 
@@ -295,7 +304,6 @@ int user_load(User *u) {
         r = parse_env_file(u->state_file, NEWLINE,
                            "SERVICE",     &u->service,
                            "SERVICE_JOB", &u->service_job,
-                           "SLICE",       &u->slice,
                            "SLICE_JOB",   &u->slice_job,
                            "DISPLAY",     &display,
                            "REALTIME",    &realtime,
@@ -385,52 +393,33 @@ fail:
 }
 
 static int user_start_slice(User *u) {
+        _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
+        const char *description;
+        char *job;
         int r;
 
         assert(u);
 
-        if (!u->slice) {
-                _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
-                char lu[DECIMAL_STR_MAX(uid_t) + 1], *slice, *job;
-                const char *description;
-
-                u->slice_job  = mfree(u->slice_job);
-
-                xsprintf(lu, UID_FMT, u->uid);
-                r = slice_build_subslice(SPECIAL_USER_SLICE, lu, &slice);
-                if (r < 0)
-                        return log_error_errno(r, "Failed to build slice name: %m");
-
-                description = strjoina("User Slice of ", u->name);
-
-                r = manager_start_slice(
-                                u->manager,
-                                slice,
-                                description,
-                                "systemd-logind.service",
-                                "systemd-user-sessions.service",
-                                u->manager->user_tasks_max,
-                                &error,
-                                &job);
-                if (r < 0) {
-
-                        if (sd_bus_error_has_name(&error, BUS_ERROR_UNIT_EXISTS))
-                                /* The slice already exists? If so, that's fine, let's just reuse it */
-                                u->slice = slice;
-                        else {
-                                log_error_errno(r, "Failed to start user slice %s, ignoring: %s (%s)", slice, bus_error_message(&error, r), error.name);
-                                free(slice);
-                                /* we don't fail due to this, let's try to continue */
-                        }
-                } else {
-                        u->slice = slice;
-                        u->slice_job = job;
-                }
+        u->slice_job = mfree(u->slice_job);
+        description = strjoina("User Slice of ", u->name);
+
+        r = manager_start_slice(
+                        u->manager,
+                        u->slice,
+                        description,
+                        "systemd-logind.service",
+                        "systemd-user-sessions.service",
+                        u->manager->user_tasks_max,
+                        &error,
+                        &job);
+        if (r < 0) {
+                /* we don't fail due to this, let's try to continue */
+                if (!sd_bus_error_has_name(&error, BUS_ERROR_UNIT_EXISTS))
+                        log_error_errno(r, "Failed to start user slice %s, ignoring: %s (%s)", u->slice, bus_error_message(&error, r), error.name);
+        } else {
+                u->slice_job = job;
         }
 
-        if (u->slice)
-                (void) hashmap_put(u->manager->user_units, u->slice, u);
-
         return 0;
 }
 
@@ -523,9 +512,6 @@ static int user_stop_slice(User *u) {
 
         assert(u);
 
-        if (!u->slice)
-                return 0;
-
         r = manager_stop_unit(u->manager, u->slice, &error, &job);
         if (r < 0) {
                 log_error("Failed to stop user slice: %s", bus_error_message(&error, r));
@@ -771,9 +757,6 @@ UserState user_get_state(User *u) {
 int user_kill(User *u, int signo) {
         assert(u);
 
-        if (!u->slice)
-                return -ESRCH;
-
         return manager_kill_unit(u->manager, u->slice, KILL_ALL, signo, NULL);
 }