b12df0
From 019b3a5d7530c51aa8f7f1e5f5cb5eb81113d4db Mon Sep 17 00:00:00 2001
b12df0
From: Lennart Poettering <lennart@poettering.net>
b12df0
Date: Fri, 3 Aug 2018 18:53:09 +0200
b12df0
Subject: [PATCH] logind: rework Seat/Session/User object allocation and
b12df0
 freeing a bit
b12df0
b12df0
Let's update things a bit to follow current practices:
b12df0
b12df0
- User structure initialization rather than zero-initialized allocation
b12df0
b12df0
- Always propagate proper errors from allocation functions
b12df0
b12df0
- Use _cleanup_ for freeing objects when allocation fails half-way
b12df0
b12df0
- Make destructors return NULL
b12df0
b12df0
(cherry picked from commit 8c29a4570993105fecc12288596d2ee77c7f82b8)
b12df0
b12df0
Related: #1642460
b12df0
---
b12df0
 src/login/logind-core.c    | 14 ++++++----
b12df0
 src/login/logind-seat.c    | 38 +++++++++++++++----------
b12df0
 src/login/logind-seat.h    |  6 ++--
b12df0
 src/login/logind-session.c | 57 +++++++++++++++++++++-----------------
b12df0
 src/login/logind-session.h |  7 +++--
b12df0
 src/login/logind-user.c    | 21 +++++++-------
b12df0
 6 files changed, 83 insertions(+), 60 deletions(-)
b12df0
b12df0
diff --git a/src/login/logind-core.c b/src/login/logind-core.c
b12df0
index cff5536ac0..f598bbaa1c 100644
b12df0
--- a/src/login/logind-core.c
b12df0
+++ b/src/login/logind-core.c
b12df0
@@ -88,15 +88,16 @@ int manager_add_device(Manager *m, const char *sysfs, bool master, Device **_dev
b12df0
 
b12df0
 int manager_add_seat(Manager *m, const char *id, Seat **_seat) {
b12df0
         Seat *s;
b12df0
+        int r;
b12df0
 
b12df0
         assert(m);
b12df0
         assert(id);
b12df0
 
b12df0
         s = hashmap_get(m->seats, id);
b12df0
         if (!s) {
b12df0
-                s = seat_new(m, id);
b12df0
-                if (!s)
b12df0
-                        return -ENOMEM;
b12df0
+                r = seat_new(&s, m, id);
b12df0
+                if (r < 0)
b12df0
+                        return r;
b12df0
         }
b12df0
 
b12df0
         if (_seat)
b12df0
@@ -107,15 +108,16 @@ int manager_add_seat(Manager *m, const char *id, Seat **_seat) {
b12df0
 
b12df0
 int manager_add_session(Manager *m, const char *id, Session **_session) {
b12df0
         Session *s;
b12df0
+        int r;
b12df0
 
b12df0
         assert(m);
b12df0
         assert(id);
b12df0
 
b12df0
         s = hashmap_get(m->sessions, id);
b12df0
         if (!s) {
b12df0
-                s = session_new(m, id);
b12df0
-                if (!s)
b12df0
-                        return -ENOMEM;
b12df0
+                r = session_new(&s, m, id);
b12df0
+                if (r < 0)
b12df0
+                        return r;
b12df0
         }
b12df0
 
b12df0
         if (_session)
b12df0
diff --git a/src/login/logind-seat.c b/src/login/logind-seat.c
b12df0
index 63253db5bf..f68fc0ceaa 100644
b12df0
--- a/src/login/logind-seat.c
b12df0
+++ b/src/login/logind-seat.c
b12df0
@@ -21,33 +21,42 @@
b12df0
 #include "terminal-util.h"
b12df0
 #include "util.h"
b12df0
 
b12df0
-Seat *seat_new(Manager *m, const char *id) {
b12df0
-        Seat *s;
b12df0
+int seat_new(Seat** ret, Manager *m, const char *id) {
b12df0
+        _cleanup_(seat_freep) Seat *s = NULL;
b12df0
+        int r;
b12df0
 
b12df0
+        assert(ret);
b12df0
         assert(m);
b12df0
         assert(id);
b12df0
 
b12df0
-        s = new0(Seat, 1);
b12df0
+        if (!seat_name_is_valid(id))
b12df0
+                return -EINVAL;
b12df0
+
b12df0
+        s = new(Seat, 1);
b12df0
         if (!s)
b12df0
-                return NULL;
b12df0
+                return -ENOMEM;
b12df0
+
b12df0
+        *s = (Seat) {
b12df0
+                .manager = m,
b12df0
+        };
b12df0
 
b12df0
         s->state_file = strappend("/run/systemd/seats/", id);
b12df0
         if (!s->state_file)
b12df0
-                return mfree(s);
b12df0
+                return -ENOMEM;
b12df0
 
b12df0
         s->id = basename(s->state_file);
b12df0
-        s->manager = m;
b12df0
 
b12df0
-        if (hashmap_put(m->seats, s->id, s) < 0) {
b12df0
-                free(s->state_file);
b12df0
-                return mfree(s);
b12df0
-        }
b12df0
+        r = hashmap_put(m->seats, s->id, s);
b12df0
+        if (r < 0)
b12df0
+                return r;
b12df0
 
b12df0
-        return s;
b12df0
+        *ret = TAKE_PTR(s);
b12df0
+        return 0;
b12df0
 }
b12df0
 
b12df0
-void seat_free(Seat *s) {
b12df0
-        assert(s);
b12df0
+Seat* seat_free(Seat *s) {
b12df0
+        if (!s)
b12df0
+                return NULL;
b12df0
 
b12df0
         if (s->in_gc_queue)
b12df0
                 LIST_REMOVE(gc_queue, s->manager->seat_gc_queue, s);
b12df0
@@ -64,7 +73,8 @@ void seat_free(Seat *s) {
b12df0
 
b12df0
         free(s->positions);
b12df0
         free(s->state_file);
b12df0
-        free(s);
b12df0
+
b12df0
+        return mfree(s);
b12df0
 }
b12df0
 
b12df0
 int seat_save(Seat *s) {
b12df0
diff --git a/src/login/logind-seat.h b/src/login/logind-seat.h
b12df0
index 70878bbe52..51cd468e26 100644
b12df0
--- a/src/login/logind-seat.h
b12df0
+++ b/src/login/logind-seat.h
b12df0
@@ -27,8 +27,10 @@ struct Seat {
b12df0
         LIST_FIELDS(Seat, gc_queue);
b12df0
 };
b12df0
 
b12df0
-Seat *seat_new(Manager *m, const char *id);
b12df0
-void seat_free(Seat *s);
b12df0
+int seat_new(Seat **ret, Manager *m, const char *id);
b12df0
+Seat* seat_free(Seat *s);
b12df0
+
b12df0
+DEFINE_TRIVIAL_CLEANUP_FUNC(Seat *, seat_free);
b12df0
 
b12df0
 int seat_save(Seat *s);
b12df0
 int seat_load(Seat *s);
b12df0
diff --git a/src/login/logind-session.c b/src/login/logind-session.c
b12df0
index 69d5a10319..5621d59a41 100644
b12df0
--- a/src/login/logind-session.c
b12df0
+++ b/src/login/logind-session.c
b12df0
@@ -24,57 +24,61 @@
b12df0
 #include "mkdir.h"
b12df0
 #include "parse-util.h"
b12df0
 #include "path-util.h"
b12df0
+#include "process-util.h"
b12df0
 #include "string-table.h"
b12df0
 #include "terminal-util.h"
b12df0
 #include "user-util.h"
b12df0
 #include "util.h"
b12df0
-#include "process-util.h"
b12df0
 
b12df0
 #define RELEASE_USEC (20*USEC_PER_SEC)
b12df0
 
b12df0
 static void session_remove_fifo(Session *s);
b12df0
 
b12df0
-Session* session_new(Manager *m, const char *id) {
b12df0
-        Session *s;
b12df0
+int session_new(Session **ret, Manager *m, const char *id) {
b12df0
+        _cleanup_(session_freep) Session *s = NULL;
b12df0
+        int r;
b12df0
 
b12df0
+        assert(ret);
b12df0
         assert(m);
b12df0
         assert(id);
b12df0
-        assert(session_id_valid(id));
b12df0
 
b12df0
-        s = new0(Session, 1);
b12df0
+        if (!session_id_valid(id))
b12df0
+                return -EINVAL;
b12df0
+
b12df0
+        s = new(Session, 1);
b12df0
         if (!s)
b12df0
-                return NULL;
b12df0
+                return -ENOMEM;
b12df0
+
b12df0
+        *s = (Session) {
b12df0
+                .manager = m,
b12df0
+                .fifo_fd = -1,
b12df0
+                .vtfd = -1,
b12df0
+                .audit_id = AUDIT_SESSION_INVALID,
b12df0
+        };
b12df0
 
b12df0
         s->state_file = strappend("/run/systemd/sessions/", id);
b12df0
         if (!s->state_file)
b12df0
-                return mfree(s);
b12df0
-
b12df0
-        s->devices = hashmap_new(&devt_hash_ops);
b12df0
-        if (!s->devices) {
b12df0
-                free(s->state_file);
b12df0
-                return mfree(s);
b12df0
-        }
b12df0
+                return -ENOMEM;
b12df0
 
b12df0
         s->id = basename(s->state_file);
b12df0
 
b12df0
-        if (hashmap_put(m->sessions, s->id, s) < 0) {
b12df0
-                hashmap_free(s->devices);
b12df0
-                free(s->state_file);
b12df0
-                return mfree(s);
b12df0
-        }
b12df0
+        s->devices = hashmap_new(&devt_hash_ops);
b12df0
+        if (!s->devices)
b12df0
+                return -ENOMEM;
b12df0
 
b12df0
-        s->manager = m;
b12df0
-        s->fifo_fd = -1;
b12df0
-        s->vtfd = -1;
b12df0
-        s->audit_id = AUDIT_SESSION_INVALID;
b12df0
+        r = hashmap_put(m->sessions, s->id, s);
b12df0
+        if (r < 0)
b12df0
+                return r;
b12df0
 
b12df0
-        return s;
b12df0
+        *ret = TAKE_PTR(s);
b12df0
+        return 0;
b12df0
 }
b12df0
 
b12df0
-void session_free(Session *s) {
b12df0
+Session* session_free(Session *s) {
b12df0
         SessionDevice *sd;
b12df0
 
b12df0
-        assert(s);
b12df0
+        if (!s)
b12df0
+                return NULL;
b12df0
 
b12df0
         if (s->in_gc_queue)
b12df0
                 LIST_REMOVE(gc_queue, s->manager->session_gc_queue, s);
b12df0
@@ -126,7 +130,8 @@ void session_free(Session *s) {
b12df0
         hashmap_remove(s->manager->sessions, s->id);
b12df0
 
b12df0
         free(s->state_file);
b12df0
-        free(s);
b12df0
+
b12df0
+        return mfree(s);
b12df0
 }
b12df0
 
b12df0
 void session_set_user(Session *s, User *u) {
b12df0
diff --git a/src/login/logind-session.h b/src/login/logind-session.h
b12df0
index 29ca399daf..572f2545c1 100644
b12df0
--- a/src/login/logind-session.h
b12df0
+++ b/src/login/logind-session.h
b12df0
@@ -109,8 +109,11 @@ struct Session {
b12df0
         LIST_FIELDS(Session, gc_queue);
b12df0
 };
b12df0
 
b12df0
-Session *session_new(Manager *m, const char *id);
b12df0
-void session_free(Session *s);
b12df0
+int session_new(Session **ret, Manager *m, const char *id);
b12df0
+Session* session_free(Session *s);
b12df0
+
b12df0
+DEFINE_TRIVIAL_CLEANUP_FUNC(Session *, session_free);
b12df0
+
b12df0
 void session_set_user(Session *s, User *u);
b12df0
 bool session_may_gc(Session *s, bool drop_not_started);
b12df0
 void session_add_to_gc_queue(Session *s);
b12df0
diff --git a/src/login/logind-user.c b/src/login/logind-user.c
b12df0
index 56b8066f12..60ccd62abb 100644
b12df0
--- a/src/login/logind-user.c
b12df0
+++ b/src/login/logind-user.c
b12df0
@@ -30,23 +30,24 @@
b12df0
 #include "user-util.h"
b12df0
 #include "util.h"
b12df0
 
b12df0
-int user_new(User **out, Manager *m, uid_t uid, gid_t gid, const char *name) {
b12df0
+int user_new(User **ret, Manager *m, uid_t uid, gid_t gid, const char *name) {
b12df0
         _cleanup_(user_freep) User *u = NULL;
b12df0
         char lu[DECIMAL_STR_MAX(uid_t) + 1];
b12df0
         int r;
b12df0
 
b12df0
-        assert(out);
b12df0
+        assert(ret);
b12df0
         assert(m);
b12df0
         assert(name);
b12df0
 
b12df0
-        u = new0(User, 1);
b12df0
+        u = new(User, 1);
b12df0
         if (!u)
b12df0
                 return -ENOMEM;
b12df0
 
b12df0
-        u->manager = m;
b12df0
-        u->uid = uid;
b12df0
-        u->gid = gid;
b12df0
-        xsprintf(lu, UID_FMT, uid);
b12df0
+        *u = (User) {
b12df0
+                .manager = m,
b12df0
+                .uid = uid,
b12df0
+                .gid = gid,
b12df0
+        };
b12df0
 
b12df0
         u->name = strdup(name);
b12df0
         if (!u->name)
b12df0
@@ -58,6 +59,7 @@ int user_new(User **out, Manager *m, uid_t uid, gid_t gid, const char *name) {
b12df0
         if (asprintf(&u->runtime_path, "/run/user/"UID_FMT, uid) < 0)
b12df0
                 return -ENOMEM;
b12df0
 
b12df0
+        xsprintf(lu, UID_FMT, uid);
b12df0
         r = slice_build_subslice(SPECIAL_USER_SLICE, lu, &u->slice);
b12df0
         if (r < 0)
b12df0
                 return r;
b12df0
@@ -78,8 +80,7 @@ int user_new(User **out, Manager *m, uid_t uid, gid_t gid, const char *name) {
b12df0
         if (r < 0)
b12df0
                 return r;
b12df0
 
b12df0
-        *out = TAKE_PTR(u);
b12df0
-
b12df0
+        *ret = TAKE_PTR(u);
b12df0
         return 0;
b12df0
 }
b12df0
 
b12df0
@@ -272,7 +273,7 @@ int user_save(User *u) {
b12df0
         if (!u->started)
b12df0
                 return 0;
b12df0
 
b12df0
-        return user_save_internal (u);
b12df0
+        return user_save_internal(u);
b12df0
 }
b12df0
 
b12df0
 int user_load(User *u) {