chiark / gitweb /
logind: small simplifications
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 3 Feb 2014 04:58:16 +0000 (23:58 -0500)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 4 Mar 2014 15:02:09 +0000 (10:02 -0500)
src/login/logind-core.c
src/login/logind-seat.c

index b8d03c3..ca34d37 100644 (file)
@@ -42,20 +42,15 @@ int manager_add_device(Manager *m, const char *sysfs, bool master, Device **_dev
         assert(sysfs);
 
         d = hashmap_get(m->devices, sysfs);
-        if (d) {
-                if (_device)
-                        *_device = d;
-
+        if (d)
                 /* we support adding master-flags, but not removing them */
                 d->master = d->master || master;
-
-                return 0;
+        else {
+                d = device_new(m, sysfs, master);
+                if (!d)
+                        return -ENOMEM;
         }
 
-        d = device_new(m, sysfs, master);
-        if (!d)
-                return -ENOMEM;
-
         if (_device)
                 *_device = d;
 
@@ -69,17 +64,12 @@ int manager_add_seat(Manager *m, const char *id, Seat **_seat) {
         assert(id);
 
         s = hashmap_get(m->seats, id);
-        if (s) {
-                if (_seat)
-                        *_seat = s;
-
-                return 0;
+        if (!s) {
+                s = seat_new(m, id);
+                if (!s)
+                        return -ENOMEM;
         }
 
-        s = seat_new(m, id);
-        if (!s)
-                return -ENOMEM;
-
         if (_seat)
                 *_seat = s;
 
@@ -93,17 +83,12 @@ int manager_add_session(Manager *m, const char *id, Session **_session) {
         assert(id);
 
         s = hashmap_get(m->sessions, id);
-        if (s) {
-                if (_session)
-                        *_session = s;
-
-                return 0;
+        if (!s) {
+                s = session_new(m, id);
+                if (!s)
+                        return -ENOMEM;
         }
 
-        s = session_new(m, id);
-        if (!s)
-                return -ENOMEM;
-
         if (_session)
                 *_session = s;
 
@@ -117,17 +102,12 @@ int manager_add_user(Manager *m, uid_t uid, gid_t gid, const char *name, User **
         assert(name);
 
         u = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
-        if (u) {
-                if (_user)
-                        *_user = u;
-
-                return 0;
+        if (!u) {
+                u = user_new(m, uid, gid, name);
+                if (!u)
+                        return -ENOMEM;
         }
 
-        u = user_new(m, uid, gid, name);
-        if (!u)
-                return -ENOMEM;
-
         if (_user)
                 *_user = u;
 
@@ -193,17 +173,12 @@ int manager_add_button(Manager *m, const char *name, Button **_button) {
         assert(name);
 
         b = hashmap_get(m->buttons, name);
-        if (b) {
-                if (_button)
-                        *_button = b;
-
-                return 0;
+        if (!b) {
+                b = button_new(m, name);
+                if (!b)
+                        return -ENOMEM;
         }
 
-        b = button_new(m, name);
-        if (!b)
-                return -ENOMEM;
-
         if (_button)
                 *_button = b;
 
@@ -441,7 +416,8 @@ bool manager_shall_kill(Manager *m, const char *user) {
 
 static int vt_is_busy(unsigned int vtnr) {
         struct vt_stat vt_stat;
-        int r = 0, fd;
+        int r = 0;
+        _cleanup_close_ int fd;
 
         assert(vtnr >= 1);
 
@@ -460,14 +436,12 @@ static int vt_is_busy(unsigned int vtnr) {
         else
                 r = !!(vt_stat.v_state & (1 << vtnr));
 
-        close_nointr_nofail(fd);
-
         return r;
 }
 
 int manager_spawn_autovt(Manager *m, unsigned int vtnr) {
         _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
-        _cleanup_free_ char *name = NULL;
+        char name[sizeof("autovt@tty.service") + DECIMAL_STR_MAX(unsigned int)];
         int r;
 
         assert(m);
@@ -489,9 +463,7 @@ int manager_spawn_autovt(Manager *m, unsigned int vtnr) {
                         return -EBUSY;
         }
 
-        if (asprintf(&name, "autovt@tty%u.service", vtnr) < 0)
-                return log_oom();
-
+        snprintf(name, sizeof(name), "autovt@tty%u.service", vtnr);
         r = sd_bus_call_method(
                         m->bus,
                         "org.freedesktop.systemd1",
index 96cf08e..3f5efdc 100644 (file)
@@ -168,14 +168,12 @@ int seat_load(Seat *s) {
 }
 
 static int vt_allocate(unsigned int vtnr) {
-        _cleanup_free_ char *p = NULL;
+        char p[sizeof("/dev/tty") + DECIMAL_STR_MAX(unsigned int)];
         _cleanup_close_ int fd = -1;
 
         assert(vtnr >= 1);
 
-        if (asprintf(&p, "/dev/tty%u", vtnr) < 0)
-                return -ENOMEM;
-
+        snprintf(p, sizeof(p), "/dev/tty%u", vtnr);
         fd = open_terminal(p, O_RDWR|O_NOCTTY|O_CLOEXEC);
         if (fd < 0)
                 return -errno;