richardphibel / rpms / systemd

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