chiark / gitweb /
logind: exploit previous cleanups and simplify returns
[elogind.git] / src / login / logind-dbus.c
index 1dcdf04b2925462169d126a9d3a4ec7f87b75221..4ae8362211122b094ac3056c9c2bf789cf8f9d26 100644 (file)
@@ -319,7 +319,7 @@ static int bus_manager_create_session(Manager *m, DBusMessage *message, DBusMess
         int r;
         uint32_t vtnr = 0;
         _cleanup_close_ int fifo_fd = -1;
-        DBusMessage *reply = NULL;
+        _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
         Session *session = NULL;
         User *user = NULL;
         Seat *seat = NULL;
@@ -536,6 +536,7 @@ static int bus_manager_create_session(Manager *m, DBusMessage *message, DBusMess
                 }
 
                 *_reply = reply;
+                reply = NULL;
 
                 return 0;
         }
@@ -681,6 +682,7 @@ static int bus_manager_create_session(Manager *m, DBusMessage *message, DBusMess
         }
 
         *_reply = reply;
+        reply = NULL;
 
         return 0;
 
@@ -691,9 +693,6 @@ fail:
         if (user)
                 user_add_to_gc_queue(user);
 
-        if (reply)
-                dbus_message_unref(reply);
-
         return r;
 }
 
@@ -712,7 +711,7 @@ static int bus_manager_inhibit(
         InhibitMode mm;
         unsigned long ul;
         int r, fifo_fd = -1;
-        DBusMessage *reply = NULL;
+        _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
 
         assert(m);
         assert(connection);
@@ -833,6 +832,7 @@ static int bus_manager_inhibit(
 
         close_nointr_nofail(fifo_fd);
         *_reply = reply;
+        reply = NULL;
 
         inhibitor_start(i);
 
@@ -845,9 +845,6 @@ fail:
         if (fifo_fd >= 0)
                 close_nointr_nofail(fifo_fd);
 
-        if (reply)
-                dbus_message_unref(reply);
-
         return r;
 }
 
@@ -904,7 +901,7 @@ finish:
 
 static int attach_device(Manager *m, const char *seat, const char *sysfs) {
         struct udev_device *d;
-        char *rule = NULL, *file = NULL;
+        char _cleanup_free_ *rule = NULL, *file = NULL;
         const char *id_for_seat;
         int r;
 
@@ -946,9 +943,6 @@ static int attach_device(Manager *m, const char *seat, const char *sysfs) {
         r = trigger_device(m, d);
 
 finish:
-        free(rule);
-        free(file);
-
         if (d)
                 udev_device_unref(d);
 
@@ -956,7 +950,7 @@ finish:
 }
 
 static int flush_devices(Manager *m) {
-        DIR *d;
+        DIR _cleanup_closedir_ *d;
 
         assert(m);
 
@@ -981,8 +975,6 @@ static int flush_devices(Manager *m) {
                         if (unlinkat(dirfd(d), de->d_name, 0) < 0)
                                 log_warning("Failed to unlink %s: %m", de->d_name);
                 }
-
-                closedir(d);
         }
 
         return trigger_device(m, NULL);
@@ -1126,7 +1118,7 @@ static int bus_manager_can_shutdown_or_sleep(
 
         bool multiple_sessions, challenge, blocked, b;
         const char *result;
-        DBusMessage *reply = NULL;
+        _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
         int r;
         unsigned long ul;
 
@@ -1225,12 +1217,11 @@ finish:
                         reply,
                         DBUS_TYPE_STRING, &result,
                         DBUS_TYPE_INVALID);
-        if (!b) {
-                dbus_message_unref(reply);
+        if (!b)
                 return -ENOMEM;
-        }
 
         *_reply = reply;
+        reply = NULL;
         return 0;
 }
 
@@ -1430,7 +1421,7 @@ static DBusHandlerResult manager_message_handler(
         Manager *m = userdata;
 
         DBusError error;
-        DBusMessage *reply = NULL;
+        _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
         int r;
 
         assert(connection);
@@ -2338,16 +2329,11 @@ static DBusHandlerResult manager_message_handler(
         if (reply) {
                 if (!bus_maybe_send_reply(connection, message, reply))
                                 goto oom;
-
-                dbus_message_unref(reply);
         }
 
         return DBUS_HANDLER_RESULT_HANDLED;
 
 oom:
-        if (reply)
-                dbus_message_unref(reply);
-
         dbus_error_free(&error);
 
         return DBUS_HANDLER_RESULT_NEED_MEMORY;
@@ -2413,25 +2399,20 @@ DBusHandlerResult bus_message_filter(
 }
 
 int manager_send_changed(Manager *manager, const char *properties) {
-        DBusMessage *m;
-        int r = -ENOMEM;
+        _cleanup_dbus_message_unref_ DBusMessage *m = NULL;
 
         assert(manager);
 
-        m = bus_properties_changed_new("/org/freedesktop/login1", "org.freedesktop.login1.Manager", properties);
+        m = bus_properties_changed_new("/org/freedesktop/login1",
+                                       "org.freedesktop.login1.Manager",
+                                       properties);
         if (!m)
-                goto finish;
+                return -ENOMEM;
 
         if (!dbus_connection_send(manager->bus, m, NULL))
-                goto finish;
-
-        r = 0;
-
-finish:
-        if (m)
-                dbus_message_unref(m);
+                return -ENOMEM;
 
-        return r;
+        return 0;
 }
 
 int manager_dispatch_delayed(Manager *manager) {