summaryrefslogtreecommitdiff
path: root/src/login/logind-seat.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2018-08-03 18:53:09 +0200
committerLennart Poettering <lennart@poettering.net>2018-10-13 12:59:29 +0200
commit8c29a4570993105fecc12288596d2ee77c7f82b8 (patch)
tree9b3b515812f3949e6a95b6cdaaabcc3ac037da49 /src/login/logind-seat.c
parentfd8879498d50b19c5abd0a90daa8f87be6ff6e9f (diff)
downloadsystemd-8c29a4570993105fecc12288596d2ee77c7f82b8.tar.gz
logind: rework Seat/Session/User object allocation and freeing a bit
Let's update things a bit to follow current practices: - User structure initialization rather than zero-initialized allocation - Always propagate proper errors from allocation functions - Use _cleanup_ for freeing objects when allocation fails half-way - Make destructors return NULL
Diffstat (limited to 'src/login/logind-seat.c')
-rw-r--r--src/login/logind-seat.c38
1 files changed, 24 insertions, 14 deletions
diff --git a/src/login/logind-seat.c b/src/login/logind-seat.c
index f24fe96841..398c74d712 100644
--- a/src/login/logind-seat.c
+++ b/src/login/logind-seat.c
@@ -21,33 +21,42 @@
#include "terminal-util.h"
#include "util.h"
-Seat *seat_new(Manager *m, const char *id) {
- Seat *s;
+int seat_new(Seat** ret, Manager *m, const char *id) {
+ _cleanup_(seat_freep) Seat *s = NULL;
+ int r;
+ assert(ret);
assert(m);
assert(id);
- s = new0(Seat, 1);
+ if (!seat_name_is_valid(id))
+ return -EINVAL;
+
+ s = new(Seat, 1);
if (!s)
- return NULL;
+ return -ENOMEM;
+
+ *s = (Seat) {
+ .manager = m,
+ };
s->state_file = strappend("/run/systemd/seats/", id);
if (!s->state_file)
- return mfree(s);
+ return -ENOMEM;
s->id = basename(s->state_file);
- s->manager = m;
- if (hashmap_put(m->seats, s->id, s) < 0) {
- free(s->state_file);
- return mfree(s);
- }
+ r = hashmap_put(m->seats, s->id, s);
+ if (r < 0)
+ return r;
- return s;
+ *ret = TAKE_PTR(s);
+ return 0;
}
-void seat_free(Seat *s) {
- assert(s);
+Seat* seat_free(Seat *s) {
+ if (!s)
+ return NULL;
if (s->in_gc_queue)
LIST_REMOVE(gc_queue, s->manager->seat_gc_queue, s);
@@ -64,7 +73,8 @@ void seat_free(Seat *s) {
free(s->positions);
free(s->state_file);
- free(s);
+
+ return mfree(s);
}
int seat_save(Seat *s) {