richardphibel / rpms / systemd

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