[systemd-commits] 8 commits - man/journalctl.xml src/core src/hostname src/journal src/locale src/login src/python-systemd src/shared src/timedate

Zbigniew Jędrzejewski-Szmek zbyszek at kemper.freedesktop.org
Mon Mar 18 17:58:55 PDT 2013


 man/journalctl.xml                |    2 
 src/core/dbus-job.c               |    4 
 src/core/dbus-manager.c           |    2 
 src/core/dbus-snapshot.c          |    2 
 src/core/dbus-unit.c              |    6 
 src/core/dbus.c                   |    2 
 src/hostname/hostnamed.c          |    2 
 src/journal/coredumpctl.c         |   14 -
 src/journal/journalctl.c          |   49 ++----
 src/journal/journald-server.c     |    3 
 src/journal/sd-journal.c          |   83 ++---------
 src/journal/test-journal-enum.c   |    5 
 src/journal/test-journal-match.c  |    7 
 src/journal/test-journal-stream.c |   10 -
 src/locale/localed.c              |    2 
 src/login/inhibit.c               |   47 +-----
 src/login/loginctl.c              |  286 ++++++++++++++++----------------------
 src/login/logind-dbus.c           |   55 ++-----
 src/login/logind-seat-dbus.c      |   70 ++-------
 src/login/logind-session-dbus.c   |   78 ++--------
 src/login/logind-user-dbus.c      |   58 ++-----
 src/python-systemd/_reader.c      |   32 ++++
 src/shared/logs-show.c            |   14 -
 src/shared/macro.h                |    1 
 src/shared/util.h                 |    5 
 src/timedate/timedated.c          |    2 
 26 files changed, 325 insertions(+), 516 deletions(-)

New commits:
commit 4654e558a3c297a71f05c3b2db6a2744fcf3cdea
Author: Zbigniew Jędrzejewski-Szmek <zbyszek at in.waw.pl>
Date:   Mon Mar 18 20:31:21 2013 -0400

    logind: exploit previous cleanups and simplify returns

diff --git a/src/login/inhibit.c b/src/login/inhibit.c
index e94843b..9cb321f 100644
--- a/src/login/inhibit.c
+++ b/src/login/inhibit.c
@@ -64,7 +64,7 @@ static int inhibit(DBusConnection *bus, DBusError *error) {
         if (!dbus_message_get_args(reply, error,
                                    DBUS_TYPE_UNIX_FD, &r,
                                    DBUS_TYPE_INVALID))
-                r = -EIO;
+                return -EIO;
 
         return r;
 }
@@ -85,27 +85,21 @@ static int print_inhibitors(DBusConnection *bus, DBusError *error) {
                         NULL,
                         DBUS_TYPE_INVALID);
         if (r < 0)
-                goto finish;
+                return r;
 
-        if (!dbus_message_iter_init(reply, &iter)) {
-                r = -ENOMEM;
-                goto finish;
-        }
+        if (!dbus_message_iter_init(reply, &iter))
+                return -ENOMEM;
 
-        if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) {
-                r = -EIO;
-                goto finish;
-        }
+        if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY)
+                return -EIO;
 
         dbus_message_iter_recurse(&iter, &sub);
         while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
                 const char *what, *who, *why, *mode;
                 dbus_uint32_t uid, pid;
 
-                if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
-                        r = -EIO;
-                        goto finish;
-                }
+                if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT)
+                        return -EIO;
 
                 dbus_message_iter_recurse(&sub, &sub2);
 
@@ -114,10 +108,8 @@ static int print_inhibitors(DBusConnection *bus, DBusError *error) {
                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &why, true) < 0 ||
                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &mode, true) < 0 ||
                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &uid, true) < 0 ||
-                    bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, false) < 0) {
-                        r = -EIO;
-                        goto finish;
-                }
+                    bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, false) < 0)
+                        return -EIO;
 
                 printf("     Who: %s (UID %lu, PID %lu)\n"
                        "    What: %s\n"
@@ -134,10 +126,7 @@ static int print_inhibitors(DBusConnection *bus, DBusError *error) {
         }
 
         printf("%u inhibitors listed.\n", n);
-        r = 0;
-
-finish:
-        return r;
+        return 0;
 }
 
 static int help(void) {
@@ -240,7 +229,7 @@ int main(int argc, char *argv[]) {
         int r, exit_code = 0;
         DBusConnection *bus = NULL;
         DBusError error;
-        int fd = -1;
+        int _cleanup_close_ fd = -1;
 
         dbus_error_init(&error);
 
@@ -313,8 +302,5 @@ finish:
 
         dbus_error_free(&error);
 
-        if (fd >= 0)
-                close_nointr_nofail(fd);
-
         return r < 0 ? EXIT_FAILURE : exit_code;
 }
diff --git a/src/login/loginctl.c b/src/login/loginctl.c
index 3870953..6c229c6 100644
--- a/src/login/loginctl.c
+++ b/src/login/loginctl.c
@@ -89,14 +89,13 @@ static int list_sessions(DBusConnection *bus, char **args, unsigned n) {
                         NULL,
                         DBUS_TYPE_INVALID);
         if (r)
-                goto finish;
+                return r;
 
         if (!dbus_message_iter_init(reply, &iter) ||
             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
             dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT)  {
                 log_error("Failed to parse reply.");
-                r = -EIO;
-                goto finish;
+                return -EIO;
         }
 
         dbus_message_iter_recurse(&iter, &sub);
@@ -110,8 +109,7 @@ static int list_sessions(DBusConnection *bus, char **args, unsigned n) {
 
                 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
                         log_error("Failed to parse reply.");
-                        r = -EIO;
-                        goto finish;
+                        return -EIO;
                 }
 
                 dbus_message_iter_recurse(&sub, &sub2);
@@ -122,8 +120,7 @@ static int list_sessions(DBusConnection *bus, char **args, unsigned n) {
                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &seat, true) < 0 ||
                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &object, false) < 0) {
                         log_error("Failed to parse reply.");
-                        r = -EIO;
-                        goto finish;
+                        return -EIO;
                 }
 
                 printf("%10s %10u %-16s %-16s\n", id, (unsigned) uid, user, seat);
@@ -136,10 +133,7 @@ static int list_sessions(DBusConnection *bus, char **args, unsigned n) {
         if (on_tty())
                 printf("\n%u sessions listed.\n", k);
 
-        r = 0;
-
-finish:
-        return r;
+        return 0;
 }
 
 static int list_users(DBusConnection *bus, char **args, unsigned n) {
@@ -160,14 +154,13 @@ static int list_users(DBusConnection *bus, char **args, unsigned n) {
                         NULL,
                         DBUS_TYPE_INVALID);
         if (r)
-              goto finish;
+                return r;
 
         if (!dbus_message_iter_init(reply, &iter) ||
             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
             dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT)  {
                 log_error("Failed to parse reply.");
-                r = -EIO;
-                goto finish;
+                return -EIO;
         }
 
         dbus_message_iter_recurse(&iter, &sub);
@@ -181,8 +174,7 @@ static int list_users(DBusConnection *bus, char **args, unsigned n) {
 
                 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
                         log_error("Failed to parse reply.");
-                        r = -EIO;
-                        goto finish;
+                        return -EIO;
                 }
 
                 dbus_message_iter_recurse(&sub, &sub2);
@@ -191,8 +183,7 @@ static int list_users(DBusConnection *bus, char **args, unsigned n) {
                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &user, true) < 0 ||
                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &object, false) < 0) {
                         log_error("Failed to parse reply.");
-                        r = -EIO;
-                        goto finish;
+                        return -EIO;
                 }
 
                 printf("%10u %-16s\n", (unsigned) uid, user);
@@ -205,10 +196,7 @@ static int list_users(DBusConnection *bus, char **args, unsigned n) {
         if (on_tty())
                 printf("\n%u users listed.\n", k);
 
-        r = 0;
-
-finish:
-        return r;
+        return 0;
 }
 
 static int list_seats(DBusConnection *bus, char **args, unsigned n) {
@@ -229,14 +217,13 @@ static int list_seats(DBusConnection *bus, char **args, unsigned n) {
                         NULL,
                         DBUS_TYPE_INVALID);
         if (r)
-                goto finish;
+                return r;
 
         if (!dbus_message_iter_init(reply, &iter) ||
             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
             dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT)  {
                 log_error("Failed to parse reply.");
-                r = -EIO;
-                goto finish;
+                return -EIO;
         }
 
         dbus_message_iter_recurse(&iter, &sub);
@@ -249,8 +236,7 @@ static int list_seats(DBusConnection *bus, char **args, unsigned n) {
 
                 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
                         log_error("Failed to parse reply.");
-                        r = -EIO;
-                        goto finish;
+                        return -EIO;
                 }
 
                 dbus_message_iter_recurse(&sub, &sub2);
@@ -258,8 +244,7 @@ static int list_seats(DBusConnection *bus, char **args, unsigned n) {
                 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &seat, true) < 0 ||
                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &object, false) < 0) {
                         log_error("Failed to parse reply.");
-                        r = -EIO;
-                        goto finish;
+                        return -EIO;
                 }
 
                 printf("%-16s\n", seat);
@@ -272,10 +257,7 @@ static int list_seats(DBusConnection *bus, char **args, unsigned n) {
         if (on_tty())
                 printf("\n%u seats listed.\n", k);
 
-        r = 0;
-
-finish:
-        return r;
+        return 0;
 }
 
 typedef struct SessionStatusInfo {
@@ -1077,7 +1059,6 @@ finish:
 }
 
 static int kill_session(DBusConnection *bus, char **args, unsigned n) {
-        int ret = 0;
         unsigned i;
 
         assert(args);
@@ -1086,28 +1067,28 @@ static int kill_session(DBusConnection *bus, char **args, unsigned n) {
                 arg_kill_who = "all";
 
         for (i = 1; i < n; i++) {
-                ret = bus_method_call_with_reply (
-                                bus,
-                                "org.freedesktop.login1",
-                                "/org/freedesktop/login1",
-                                "org.freedesktop.login1.Manager",
-                                "KillSession",
-                                NULL,
-                                NULL,
-                                DBUS_TYPE_STRING, &args[i],
-                                DBUS_TYPE_STRING, &arg_kill_who,
-                                DBUS_TYPE_INT32, &arg_signal,
-                                DBUS_TYPE_INVALID);
-                if (ret)
-                        goto finish;
+                int r;
+
+                r = bus_method_call_with_reply (
+                        bus,
+                        "org.freedesktop.login1",
+                        "/org/freedesktop/login1",
+                        "org.freedesktop.login1.Manager",
+                        "KillSession",
+                        NULL,
+                        NULL,
+                        DBUS_TYPE_STRING, &args[i],
+                        DBUS_TYPE_STRING, &arg_kill_who,
+                        DBUS_TYPE_INT32, &arg_signal,
+                        DBUS_TYPE_INVALID);
+                if (r)
+                        return r;
         }
 
-finish:
-        return ret;
+        return 0;
 }
 
 static int enable_linger(DBusConnection *bus, char **args, unsigned n) {
-        int ret = 0;
         unsigned i;
         dbus_bool_t b, interactive = true;
 
@@ -1120,36 +1101,35 @@ static int enable_linger(DBusConnection *bus, char **args, unsigned n) {
         for (i = 1; i < n; i++) {
                 uint32_t u;
                 uid_t uid;
+                int r;
 
-                ret = get_user_creds((const char**) (args+i), &uid, NULL, NULL, NULL);
-                if (ret < 0) {
-                        log_error("Failed to resolve user %s: %s", args[i], strerror(-ret));
-                        goto finish;
+                r = get_user_creds((const char**) (args+i), &uid, NULL, NULL, NULL);
+                if (r < 0) {
+                        log_error("Failed to resolve user %s: %s", args[i], strerror(-r));
+                        return r;
                 }
 
                 u = (uint32_t) uid;
-                ret = bus_method_call_with_reply (
-                                bus,
-                                "org.freedesktop.login1",
-                                "/org/freedesktop/login1",
-                                "org.freedesktop.login1.Manager",
-                                "SetUserLinger",
-                                NULL,
-                                NULL,
-                                DBUS_TYPE_UINT32, &u,
-                                DBUS_TYPE_BOOLEAN, &b,
-                                DBUS_TYPE_BOOLEAN, &interactive,
-                                DBUS_TYPE_INVALID);
-                if (ret)
-                        goto finish;
+                r = bus_method_call_with_reply (
+                        bus,
+                        "org.freedesktop.login1",
+                        "/org/freedesktop/login1",
+                        "org.freedesktop.login1.Manager",
+                        "SetUserLinger",
+                        NULL,
+                        NULL,
+                        DBUS_TYPE_UINT32, &u,
+                        DBUS_TYPE_BOOLEAN, &b,
+                        DBUS_TYPE_BOOLEAN, &interactive,
+                        DBUS_TYPE_INVALID);
+                if (r)
+                        return r;
         }
 
-finish:
-        return ret;
+        return 0;
 }
 
 static int terminate_user(DBusConnection *bus, char **args, unsigned n) {
-        int ret = 0;
         unsigned i;
 
         assert(args);
@@ -1157,34 +1137,33 @@ static int terminate_user(DBusConnection *bus, char **args, unsigned n) {
         for (i = 1; i < n; i++) {
                 uint32_t u;
                 uid_t uid;
+                int r;
 
-                ret = get_user_creds((const char**) (args+i), &uid, NULL, NULL, NULL);
-                if (ret < 0) {
-                        log_error("Failed to look up user %s: %s", args[i], strerror(-ret));
-                        goto finish;
+                r = get_user_creds((const char**) (args+i), &uid, NULL, NULL, NULL);
+                if (r < 0) {
+                        log_error("Failed to look up user %s: %s", args[i], strerror(-r));
+                        return r;
                 }
 
                 u = (uint32_t) uid;
-                ret = bus_method_call_with_reply (
-                                bus,
-                                "org.freedesktop.login1",
-                                "/org/freedesktop/login1",
-                                "org.freedesktop.login1.Manager",
-                                "TerminateUser",
-                                NULL,
-                                NULL,
-                                DBUS_TYPE_UINT32, &u,
-                                DBUS_TYPE_INVALID);
-                if (ret)
-                        goto finish;
+                r = bus_method_call_with_reply (
+                        bus,
+                        "org.freedesktop.login1",
+                        "/org/freedesktop/login1",
+                        "org.freedesktop.login1.Manager",
+                        "TerminateUser",
+                        NULL,
+                        NULL,
+                        DBUS_TYPE_UINT32, &u,
+                        DBUS_TYPE_INVALID);
+                if (r)
+                        return r;
         }
 
-finish:
-        return ret;
+        return 0;
 }
 
 static int kill_user(DBusConnection *bus, char **args, unsigned n) {
-        int ret = 0;
         unsigned i;
 
         assert(args);
@@ -1195,35 +1174,34 @@ static int kill_user(DBusConnection *bus, char **args, unsigned n) {
         for (i = 1; i < n; i++) {
                 uid_t uid;
                 uint32_t u;
+                int r;
 
-                ret = get_user_creds((const char**) (args+i), &uid, NULL, NULL, NULL);
-                if (ret < 0) {
-                        log_error("Failed to look up user %s: %s", args[i], strerror(-ret));
-                        goto finish;
+                r = get_user_creds((const char**) (args+i), &uid, NULL, NULL, NULL);
+                if (r < 0) {
+                        log_error("Failed to look up user %s: %s", args[i], strerror(-r));
+                        return r;
                 }
 
                 u = (uint32_t) uid;
-                ret = bus_method_call_with_reply (
-                                bus,
-                                "org.freedesktop.login1",
-                                "/org/freedesktop/login1",
-                                "org.freedesktop.login1.Manager",
-                                "KillUser",
-                                NULL,
-                                NULL,
-                                DBUS_TYPE_UINT32, &u,
-                                DBUS_TYPE_INT32, &arg_signal,
-                                DBUS_TYPE_INVALID);
-                if (ret)
-                        goto finish;
+                r = bus_method_call_with_reply (
+                        bus,
+                        "org.freedesktop.login1",
+                        "/org/freedesktop/login1",
+                        "org.freedesktop.login1.Manager",
+                        "KillUser",
+                        NULL,
+                        NULL,
+                        DBUS_TYPE_UINT32, &u,
+                        DBUS_TYPE_INT32, &arg_signal,
+                        DBUS_TYPE_INVALID);
+                if (r)
+                        return r;
         }
 
-finish:
-        return ret;
+        return 0;
 }
 
 static int attach(DBusConnection *bus, char **args, unsigned n) {
-        int ret = 0;
         unsigned i;
         dbus_bool_t interactive = true;
 
@@ -1232,24 +1210,25 @@ static int attach(DBusConnection *bus, char **args, unsigned n) {
         polkit_agent_open_if_enabled();
 
         for (i = 2; i < n; i++) {
-                ret = bus_method_call_with_reply (
-                                bus,
-                                "org.freedesktop.login1",
-                                "/org/freedesktop/login1",
-                                "org.freedesktop.login1.Manager",
-                                "AttachDevice",
-                                NULL,
-                                NULL,
-                                DBUS_TYPE_STRING, &args[1],
-                                DBUS_TYPE_STRING, &args[i],
-                                DBUS_TYPE_BOOLEAN, &interactive,
-                                DBUS_TYPE_INVALID);
-                if (ret)
-                        goto finish;
+                int r;
+
+                r = bus_method_call_with_reply (
+                        bus,
+                        "org.freedesktop.login1",
+                        "/org/freedesktop/login1",
+                        "org.freedesktop.login1.Manager",
+                        "AttachDevice",
+                        NULL,
+                        NULL,
+                        DBUS_TYPE_STRING, &args[1],
+                        DBUS_TYPE_STRING, &args[i],
+                        DBUS_TYPE_BOOLEAN, &interactive,
+                        DBUS_TYPE_INVALID);
+                if (r)
+                        return r;
         }
 
-finish:
-        return ret;
+        return 0;
 }
 
 static int flush_devices(DBusConnection *bus, char **args, unsigned n) {
@@ -1288,28 +1267,28 @@ static int lock_sessions(DBusConnection *bus, char **args, unsigned n) {
 }
 
 static int terminate_seat(DBusConnection *bus, char **args, unsigned n) {
-        int ret = 0;
         unsigned i;
 
         assert(args);
 
         for (i = 1; i < n; i++) {
-                ret = bus_method_call_with_reply (
-                                bus,
-                                "org.freedesktop.login1",
-                                "/org/freedesktop/login1",
-                                "org.freedesktop.login1.Manager",
-                                "TerminateSeat",
-                                NULL,
-                                NULL,
-                                DBUS_TYPE_STRING, &args[i],
-                                DBUS_TYPE_INVALID);
-                if (ret)
-                        goto finish;
+                int r;
+
+                r = bus_method_call_with_reply (
+                        bus,
+                        "org.freedesktop.login1",
+                        "/org/freedesktop/login1",
+                        "org.freedesktop.login1.Manager",
+                        "TerminateSeat",
+                        NULL,
+                        NULL,
+                        DBUS_TYPE_STRING, &args[i],
+                        DBUS_TYPE_INVALID);
+                if (r)
+                        return r;
         }
 
-finish:
-        return ret;
+        return 0;
 }
 
 static int help(void) {
diff --git a/src/login/logind-dbus.c b/src/login/logind-dbus.c
index ed2ce97..4ae8362 100644
--- a/src/login/logind-dbus.c
+++ b/src/login/logind-dbus.c
@@ -901,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;
 
@@ -943,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);
 
@@ -953,7 +950,7 @@ finish:
 }
 
 static int flush_devices(Manager *m) {
-        DIR *d;
+        DIR _cleanup_closedir_ *d;
 
         assert(m);
 
@@ -978,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);
@@ -1222,9 +1217,8 @@ finish:
                         reply,
                         DBUS_TYPE_STRING, &result,
                         DBUS_TYPE_INVALID);
-        if (!b) {
+        if (!b)
                 return -ENOMEM;
-        }
 
         *_reply = reply;
         reply = NULL;
@@ -2406,21 +2400,19 @@ DBusHandlerResult bus_message_filter(
 
 int manager_send_changed(Manager *manager, const char *properties) {
         _cleanup_dbus_message_unref_ DBusMessage *m = NULL;
-        int r = -ENOMEM;
 
         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;
+                return -ENOMEM;
 
-finish:
-        return r;
+        return 0;
 }
 
 int manager_dispatch_delayed(Manager *manager) {
diff --git a/src/login/logind-seat-dbus.c b/src/login/logind-seat-dbus.c
index 585e0c1..82f25a0 100644
--- a/src/login/logind-seat-dbus.c
+++ b/src/login/logind-seat-dbus.c
@@ -61,7 +61,7 @@ static int bus_seat_append_active(DBusMessageIter *i, const char *property, void
         DBusMessageIter sub;
         Seat *s = data;
         const char *id, *path;
-        char *p = NULL;
+        char _cleanup_free_ *p = NULL;
 
         assert(i);
         assert(property);
@@ -82,12 +82,8 @@ static int bus_seat_append_active(DBusMessageIter *i, const char *property, void
         }
 
         if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &id) ||
-            !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &path)) {
-                free(p);
+            !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &path))
                 return -ENOMEM;
-        }
-
-        free(p);
 
         if (!dbus_message_iter_close_container(i, &sub))
                 return -ENOMEM;
@@ -108,7 +104,7 @@ static int bus_seat_append_sessions(DBusMessageIter *i, const char *property, vo
                 return -ENOMEM;
 
         LIST_FOREACH(sessions_by_seat, session, s->sessions) {
-                char *p;
+                char _cleanup_free_ *p = NULL;
 
                 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
                         return -ENOMEM;
@@ -118,12 +114,8 @@ static int bus_seat_append_sessions(DBusMessageIter *i, const char *property, vo
                         return -ENOMEM;
 
                 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &session->id) ||
-                    !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
-                        free(p);
+                    !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p))
                         return -ENOMEM;
-                }
-
-                free(p);
 
                 if (!dbus_message_iter_close_container(&sub, &sub2))
                         return -ENOMEM;
@@ -356,7 +348,7 @@ const DBusObjectPathVTable bus_seat_vtable = {
 };
 
 char *seat_bus_path(Seat *s) {
-        char *t, *r;
+        char _cleanup_free_ *t;
 
         assert(s);
 
@@ -364,15 +356,11 @@ char *seat_bus_path(Seat *s) {
         if (!t)
                 return NULL;
 
-        r = strappend("/org/freedesktop/login1/seat/", t);
-        free(t);
-
-        return r;
+        return strappend("/org/freedesktop/login1/seat/", t);
 }
 
 int seat_send_signal(Seat *s, bool new_seat) {
         _cleanup_dbus_message_unref_ DBusMessage *m = NULL;
-        int r = -ENOMEM;
         _cleanup_free_ char *p = NULL;
 
         assert(s);
@@ -380,33 +368,28 @@ int seat_send_signal(Seat *s, bool new_seat) {
         m = dbus_message_new_signal("/org/freedesktop/login1",
                                     "org.freedesktop.login1.Manager",
                                     new_seat ? "SeatNew" : "SeatRemoved");
-
         if (!m)
                 return -ENOMEM;
 
         p = seat_bus_path(s);
         if (!p)
-                goto finish;
+                return -ENOMEM;
 
         if (!dbus_message_append_args(
                             m,
                             DBUS_TYPE_STRING, &s->id,
                             DBUS_TYPE_OBJECT_PATH, &p,
                             DBUS_TYPE_INVALID))
-                goto finish;
+                return -ENOMEM;
 
         if (!dbus_connection_send(s->manager->bus, m, NULL))
-                goto finish;
-
-        r = 0;
+                return -ENOMEM;
 
-finish:
-        return r;
+        return 0;
 }
 
 int seat_send_changed(Seat *s, const char *properties) {
         _cleanup_dbus_message_unref_ DBusMessage *m = NULL;
-        int r = -ENOMEM;
         _cleanup_free_ char *p = NULL;
 
         assert(s);
@@ -420,13 +403,10 @@ int seat_send_changed(Seat *s, const char *properties) {
 
         m = bus_properties_changed_new(p, "org.freedesktop.login1.Seat", properties);
         if (!m)
-                goto finish;
+                return -ENOMEM;
 
         if (!dbus_connection_send(s->manager->bus, m, NULL))
-                goto finish;
-
-        r = 0;
+                return -ENOMEM;
 
-finish:
-        return r;
+        return 0;
 }
diff --git a/src/login/logind-session-dbus.c b/src/login/logind-session-dbus.c
index 1f64c44..0e425e4 100644
--- a/src/login/logind-session-dbus.c
+++ b/src/login/logind-session-dbus.c
@@ -108,12 +108,8 @@ static int bus_session_append_seat(DBusMessageIter *i, const char *property, voi
         }
 
         if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &id) ||
-            !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &path)) {
-                free(p);
+            !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &path))
                 return -ENOMEM;
-        }
-
-        free(p);
 
         if (!dbus_message_iter_close_container(i, &sub))
                 return -ENOMEM;
@@ -124,7 +120,7 @@ static int bus_session_append_seat(DBusMessageIter *i, const char *property, voi
 static int bus_session_append_user(DBusMessageIter *i, const char *property, void *data) {
         DBusMessageIter sub;
         User *u = data;
-        char *p = NULL;
+        char _cleanup_free_ *p = NULL;
 
         assert(i);
         assert(property);
@@ -138,12 +134,8 @@ static int bus_session_append_user(DBusMessageIter *i, const char *property, voi
                 return -ENOMEM;
 
         if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_UINT32, &u->uid) ||
-            !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) {
-                free(p);
+            !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p))
                 return -ENOMEM;
-        }
-
-        free(p);
 
         if (!dbus_message_iter_close_container(i, &sub))
                 return -ENOMEM;
@@ -205,7 +197,7 @@ static int bus_session_append_idle_hint_since(DBusMessageIter *i, const char *pr
 
 static int bus_session_append_default_cgroup(DBusMessageIter *i, const char *property, void *data) {
         Session *s = data;
-        char *t;
+        char _cleanup_free_ *t = NULL;
         int r;
         bool success;
 
@@ -218,8 +210,6 @@ static int bus_session_append_default_cgroup(DBusMessageIter *i, const char *pro
                 return r;
 
         success = dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t);
-        free(t);
-
         return success ? 0 : -ENOMEM;
 }
 
@@ -458,7 +448,7 @@ const DBusObjectPathVTable bus_session_vtable = {
 };
 
 char *session_bus_path(Session *s) {
-        char *t, *r;
+        char _cleanup_free_ *t;
 
         assert(s);
 
@@ -466,15 +456,11 @@ char *session_bus_path(Session *s) {
         if (!t)
                 return NULL;
 
-        r = strappend("/org/freedesktop/login1/session/", t);
-        free(t);
-
-        return r;
+        return strappend("/org/freedesktop/login1/session/", t);
 }
 
 int session_send_signal(Session *s, bool new_session) {
         _cleanup_dbus_message_unref_ DBusMessage *m = NULL;
-        int r = -ENOMEM;
         _cleanup_free_ char *p = NULL;
 
         assert(s);
@@ -488,27 +474,23 @@ int session_send_signal(Session *s, bool new_session) {
 
         p = session_bus_path(s);
         if (!p)
-                goto finish;
+                return -ENOMEM;
 
         if (!dbus_message_append_args(
                             m,
                             DBUS_TYPE_STRING, &s->id,
                             DBUS_TYPE_OBJECT_PATH, &p,
                             DBUS_TYPE_INVALID))
-                goto finish;
+                return -ENOMEM;
 
         if (!dbus_connection_send(s->manager->bus, m, NULL))
-                goto finish;
-
-        r = 0;
+                return -ENOMEM;
 
-finish:
-        return r;
+        return 0;
 }
 
 int session_send_changed(Session *s, const char *properties) {
         _cleanup_dbus_message_unref_ DBusMessage *m = NULL;
-        int r = -ENOMEM;
         _cleanup_free_ char *p = NULL;
 
         assert(s);
@@ -522,15 +504,12 @@ int session_send_changed(Session *s, const char *properties) {
 
         m = bus_properties_changed_new(p, "org.freedesktop.login1.Session", properties);
         if (!m)
-                goto finish;
+                return -ENOMEM;
 
         if (!dbus_connection_send(s->manager->bus, m, NULL))
-                goto finish;
-
-        r = 0;
+                return -ENOMEM;
 
-finish:
-        return r;
+        return 0;
 }
 
 int session_send_lock(Session *s, bool lock) {
diff --git a/src/login/logind-user-dbus.c b/src/login/logind-user-dbus.c
index 32f4062..8cc1dd4 100644
--- a/src/login/logind-user-dbus.c
+++ b/src/login/logind-user-dbus.c
@@ -65,7 +65,7 @@ static int bus_user_append_display(DBusMessageIter *i, const char *property, voi
         DBusMessageIter sub;
         User *u = data;
         const char *id, *path;
-        char *p = NULL;
+        char _cleanup_free_ *p = NULL;
 
         assert(i);
         assert(property);
@@ -86,12 +86,8 @@ static int bus_user_append_display(DBusMessageIter *i, const char *property, voi
         }
 
         if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &id) ||
-            !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &path)) {
-                free(p);
+            !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &path))
                 return -ENOMEM;
-        }
-
-        free(p);
 
         if (!dbus_message_iter_close_container(i, &sub))
                 return -ENOMEM;
@@ -191,7 +187,7 @@ static int bus_user_append_idle_hint_since(DBusMessageIter *i, const char *prope
 
 static int bus_user_append_default_cgroup(DBusMessageIter *i, const char *property, void *data) {
         User *u = data;
-        char *t;
+        char _cleanup_free_ *t = NULL;
         int r;
         bool success;
 
@@ -204,8 +200,6 @@ static int bus_user_append_default_cgroup(DBusMessageIter *i, const char *proper
                 return r;
 
         success = dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t);
-        free(t);
-
         return success ? 0 : -ENOMEM;
 }
 
@@ -362,7 +356,6 @@ char *user_bus_path(User *u) {
 
 int user_send_signal(User *u, bool new_user) {
         _cleanup_dbus_message_unref_ DBusMessage *m = NULL;
-        int r = -ENOMEM;
         _cleanup_free_ char *p = NULL;
         uint32_t uid;
 
@@ -377,7 +370,7 @@ int user_send_signal(User *u, bool new_user) {
 
         p = user_bus_path(u);
         if (!p)
-                goto finish;
+                return -ENOMEM;
 
         uid = u->uid;
 
@@ -386,20 +379,16 @@ int user_send_signal(User *u, bool new_user) {
                             DBUS_TYPE_UINT32, &uid,
                             DBUS_TYPE_OBJECT_PATH, &p,
                             DBUS_TYPE_INVALID))
-                goto finish;
+                return -ENOMEM;
 
         if (!dbus_connection_send(u->manager->bus, m, NULL))
-                goto finish;
-
-        r = 0;
+                return -ENOMEM;
 
-finish:
-        return r;
+        return 0;
 }
 
 int user_send_changed(User *u, const char *properties) {
         _cleanup_dbus_message_unref_ DBusMessage *m = NULL;
-        int r = -ENOMEM;
         _cleanup_free_ char *p = NULL;
 
         assert(u);
@@ -413,13 +402,10 @@ int user_send_changed(User *u, const char *properties) {
 
         m = bus_properties_changed_new(p, "org.freedesktop.login1.User", properties);
         if (!m)
-                goto finish;
+                return -ENOMEM;
 
         if (!dbus_connection_send(u->manager->bus, m, NULL))
-                goto finish;
-
-        r = 0;
+                return -ENOMEM;
 
-finish:
-        return r;
+        return 0;
 }

commit ce0fc5f5f6debc6e37ac3ab0a3ea1c9c35b3ed99
Author: Colin Walters <walters at verbum.org>
Date:   Mon Mar 18 14:38:48 2013 -0400

    logind: Make more use of cleanup macros

diff --git a/src/login/inhibit.c b/src/login/inhibit.c
index a812195..e94843b 100644
--- a/src/login/inhibit.c
+++ b/src/login/inhibit.c
@@ -42,7 +42,7 @@ static enum {
 } arg_action = ACTION_INHIBIT;
 
 static int inhibit(DBusConnection *bus, DBusError *error) {
-        DBusMessage *reply = NULL;
+        _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
         int r;
 
         r = bus_method_call_with_reply(
@@ -66,13 +66,11 @@ static int inhibit(DBusConnection *bus, DBusError *error) {
                                    DBUS_TYPE_INVALID))
                 r = -EIO;
 
-        dbus_message_unref(reply);
-
         return r;
 }
 
 static int print_inhibitors(DBusConnection *bus, DBusError *error) {
-        DBusMessage *reply = NULL;
+        _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
         unsigned n = 0;
         DBusMessageIter iter, sub, sub2;
         int r;
@@ -139,9 +137,6 @@ static int print_inhibitors(DBusConnection *bus, DBusError *error) {
         r = 0;
 
 finish:
-        if (reply)
-                dbus_message_unref(reply);
-
         return r;
 }
 
diff --git a/src/login/loginctl.c b/src/login/loginctl.c
index fe27003..3870953 100644
--- a/src/login/loginctl.c
+++ b/src/login/loginctl.c
@@ -72,7 +72,7 @@ static void polkit_agent_open_if_enabled(void) {
 }
 
 static int list_sessions(DBusConnection *bus, char **args, unsigned n) {
-        DBusMessage *reply = NULL;
+        _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
         int r;
         DBusMessageIter iter, sub, sub2;
         unsigned k = 0;
@@ -139,14 +139,11 @@ static int list_sessions(DBusConnection *bus, char **args, unsigned n) {
         r = 0;
 
 finish:
-        if (reply)
-                dbus_message_unref(reply);
-
         return r;
 }
 
 static int list_users(DBusConnection *bus, char **args, unsigned n) {
-        DBusMessage *reply = NULL;
+        _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
         int r;
         DBusMessageIter iter, sub, sub2;
         unsigned k = 0;
@@ -211,14 +208,11 @@ static int list_users(DBusConnection *bus, char **args, unsigned n) {
         r = 0;
 
 finish:
-        if (reply)
-                dbus_message_unref(reply);
-
         return r;
 }
 
 static int list_seats(DBusConnection *bus, char **args, unsigned n) {
-        DBusMessage *reply = NULL;
+        _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
         int r;
         DBusMessageIter iter, sub, sub2;
         unsigned k = 0;
@@ -281,9 +275,6 @@ static int list_seats(DBusConnection *bus, char **args, unsigned n) {
         r = 0;
 
 finish:
-        if (reply)
-                dbus_message_unref(reply);
-
         return r;
 }
 
@@ -961,7 +952,7 @@ finish:
 }
 
 static int show(DBusConnection *bus, char **args, unsigned n) {
-        DBusMessage *reply = NULL;
+        _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
         int r, ret = 0;
         DBusError error;
         unsigned i;
@@ -1048,15 +1039,9 @@ static int show(DBusConnection *bus, char **args, unsigned n) {
                 r = show_one(args[0], bus, path, show_properties, &new_line);
                 if (r != 0)
                         ret = r;
-
-                dbus_message_unref(reply);
-                reply = NULL;
         }
 
 finish:
-        if (reply)
-                dbus_message_unref(reply);
-
         dbus_error_free(&error);
 
         return ret;
diff --git a/src/login/logind-dbus.c b/src/login/logind-dbus.c
index 1dcdf04..ed2ce97 100644
--- a/src/login/logind-dbus.c
+++ b/src/login/logind-dbus.c
@@ -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;
 }
 
@@ -1126,7 +1123,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;
 
@@ -1226,11 +1223,11 @@ finish:
                         DBUS_TYPE_STRING, &result,
                         DBUS_TYPE_INVALID);
         if (!b) {
-                dbus_message_unref(reply);
                 return -ENOMEM;
         }
 
         *_reply = reply;
+        reply = NULL;
         return 0;
 }
 
@@ -1430,7 +1427,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 +2335,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,7 +2405,7 @@ DBusHandlerResult bus_message_filter(
 }
 
 int manager_send_changed(Manager *manager, const char *properties) {
-        DBusMessage *m;
+        _cleanup_dbus_message_unref_ DBusMessage *m = NULL;
         int r = -ENOMEM;
 
         assert(manager);
@@ -2428,9 +2420,6 @@ int manager_send_changed(Manager *manager, const char *properties) {
         r = 0;
 
 finish:
-        if (m)
-                dbus_message_unref(m);
-
         return r;
 }
 
diff --git a/src/login/logind-seat-dbus.c b/src/login/logind-seat-dbus.c
index 4da076e..585e0c1 100644
--- a/src/login/logind-seat-dbus.c
+++ b/src/login/logind-seat-dbus.c
@@ -260,7 +260,7 @@ static DBusHandlerResult seat_message_dispatch(
                 DBusMessage *message) {
 
         DBusError error;
-        DBusMessage *reply = NULL;
+        _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
         int r;
 
         assert(s);
@@ -312,16 +312,11 @@ static DBusHandlerResult seat_message_dispatch(
         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;
@@ -376,9 +371,9 @@ char *seat_bus_path(Seat *s) {
 }
 
 int seat_send_signal(Seat *s, bool new_seat) {
-        DBusMessage *m;
+        _cleanup_dbus_message_unref_ DBusMessage *m = NULL;
         int r = -ENOMEM;
-        char *p = NULL;
+        _cleanup_free_ char *p = NULL;
 
         assert(s);
 
@@ -406,16 +401,13 @@ int seat_send_signal(Seat *s, bool new_seat) {
         r = 0;
 
 finish:
-        dbus_message_unref(m);
-        free(p);
-
         return r;
 }
 
 int seat_send_changed(Seat *s, const char *properties) {
-        DBusMessage *m;
+        _cleanup_dbus_message_unref_ DBusMessage *m = NULL;
         int r = -ENOMEM;
-        char *p = NULL;
+        _cleanup_free_ char *p = NULL;
 
         assert(s);
 
@@ -436,9 +428,5 @@ int seat_send_changed(Seat *s, const char *properties) {
         r = 0;
 
 finish:
-        if (m)
-                dbus_message_unref(m);
-        free(p);
-
         return r;
 }
diff --git a/src/login/logind-session-dbus.c b/src/login/logind-session-dbus.c
index c4db315..1f64c44 100644
--- a/src/login/logind-session-dbus.c
+++ b/src/login/logind-session-dbus.c
@@ -307,7 +307,7 @@ static DBusHandlerResult session_message_dispatch(
                 DBusMessage *message) {
 
         DBusError error;
-        DBusMessage *reply = NULL;
+        _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
         int r;
 
         assert(s);
@@ -414,16 +414,11 @@ static DBusHandlerResult session_message_dispatch(
         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;
@@ -478,9 +473,9 @@ char *session_bus_path(Session *s) {
 }
 
 int session_send_signal(Session *s, bool new_session) {
-        DBusMessage *m;
+        _cleanup_dbus_message_unref_ DBusMessage *m = NULL;
         int r = -ENOMEM;
-        char *p = NULL;
+        _cleanup_free_ char *p = NULL;
 
         assert(s);
 
@@ -508,16 +503,13 @@ int session_send_signal(Session *s, bool new_session) {
         r = 0;
 
 finish:
-        dbus_message_unref(m);
-        free(p);
-
         return r;
 }
 
 int session_send_changed(Session *s, const char *properties) {
-        DBusMessage *m;
+        _cleanup_dbus_message_unref_ DBusMessage *m = NULL;
         int r = -ENOMEM;
-        char *p = NULL;
+        _cleanup_free_ char *p = NULL;
 
         assert(s);
 
@@ -538,17 +530,13 @@ int session_send_changed(Session *s, const char *properties) {
         r = 0;
 
 finish:
-        if (m)
-                dbus_message_unref(m);
-        free(p);
-
         return r;
 }
 
 int session_send_lock(Session *s, bool lock) {
-        DBusMessage *m;
+        _cleanup_dbus_message_unref_ DBusMessage *m = NULL;
         bool b;
-        char *p;
+        _cleanup_free_ char *p = NULL;
 
         assert(s);
 
@@ -557,14 +545,11 @@ int session_send_lock(Session *s, bool lock) {
                 return -ENOMEM;
 
         m = dbus_message_new_signal(p, "org.freedesktop.login1.Session", lock ? "Lock" : "Unlock");
-        free(p);
 
         if (!m)
                 return -ENOMEM;
 
         b = dbus_connection_send(s->manager->bus, m, NULL);
-        dbus_message_unref(m);
-
         if (!b)
                 return -ENOMEM;
 
diff --git a/src/login/logind-user-dbus.c b/src/login/logind-user-dbus.c
index 5951c38..32f4062 100644
--- a/src/login/logind-user-dbus.c
+++ b/src/login/logind-user-dbus.c
@@ -257,7 +257,7 @@ static DBusHandlerResult user_message_dispatch(
                 DBusMessage *message) {
 
         DBusError error;
-        DBusMessage *reply = NULL;
+        _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
         int r;
 
         assert(u);
@@ -306,16 +306,11 @@ static DBusHandlerResult user_message_dispatch(
         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;
@@ -366,9 +361,9 @@ char *user_bus_path(User *u) {
 }
 
 int user_send_signal(User *u, bool new_user) {
-        DBusMessage *m;
+        _cleanup_dbus_message_unref_ DBusMessage *m = NULL;
         int r = -ENOMEM;
-        char *p = NULL;
+        _cleanup_free_ char *p = NULL;
         uint32_t uid;
 
         assert(u);
@@ -399,16 +394,13 @@ int user_send_signal(User *u, bool new_user) {
         r = 0;
 
 finish:
-        dbus_message_unref(m);
-        free(p);
-
         return r;
 }
 
 int user_send_changed(User *u, const char *properties) {
-        DBusMessage *m;
+        _cleanup_dbus_message_unref_ DBusMessage *m = NULL;
         int r = -ENOMEM;
-        char *p = NULL;
+        _cleanup_free_ char *p = NULL;
 
         assert(u);
 
@@ -429,9 +421,5 @@ int user_send_changed(User *u, const char *properties) {
         r = 0;
 
 finish:
-        if (m)
-                dbus_message_unref(m);
-        free(p);
-
         return r;
 }

commit c6a818c82035da91e7987920510f0dda61d8781a
Author: Colin Walters <walters at verbum.org>
Date:   Mon Mar 18 14:38:24 2013 -0400

    Use bus_maybe_send_reply() where applicable
    
    This is a followup to: commit 1a37b9b9043ef83e9900e460a9a1fccced3acf89
    
    It will fix denial messages from dbus-daemon between gdm and
    systemd-logind on logging into GNOME due to this.
    
    See the previous commit for more details.

diff --git a/src/core/dbus-job.c b/src/core/dbus-job.c
index 20c2a62..096542b 100644
--- a/src/core/dbus-job.c
+++ b/src/core/dbus-job.c
@@ -116,7 +116,7 @@ static DBusHandlerResult bus_job_message_dispatch(Job *j, DBusConnection *connec
                 return bus_default_message_handler(connection, message, INTROSPECTION, INTERFACES_LIST, bps);
         }
 
-        if (!dbus_connection_send(connection, reply, NULL))
+        if (!bus_maybe_send_reply(connection, message, reply))
                 return DBUS_HANDLER_RESULT_NEED_MEMORY;
 
         return DBUS_HANDLER_RESULT_HANDLED;
@@ -185,7 +185,7 @@ static DBusHandlerResult bus_job_message_handler(DBusConnection *connection, DBu
 
                         free(introspection);
 
-                        if (!dbus_connection_send(connection, reply, NULL))
+                        if (!bus_maybe_send_reply(connection, message, reply))
                                 goto oom;
 
                         return DBUS_HANDLER_RESULT_HANDLED;
diff --git a/src/core/dbus-manager.c b/src/core/dbus-manager.c
index 8f4bbc5..75a341f 100644
--- a/src/core/dbus-manager.c
+++ b/src/core/dbus-manager.c
@@ -1894,7 +1894,7 @@ static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection,
         }
 
         if (reply)
-                if (!dbus_connection_send(connection, reply, NULL))
+                if (!bus_maybe_send_reply(connection, message, reply))
                         goto oom;
 
         return DBUS_HANDLER_RESULT_HANDLED;
diff --git a/src/core/dbus-snapshot.c b/src/core/dbus-snapshot.c
index 02cfcb1..2ae8574 100644
--- a/src/core/dbus-snapshot.c
+++ b/src/core/dbus-snapshot.c
@@ -77,7 +77,7 @@ DBusHandlerResult bus_snapshot_message_handler(Unit *u, DBusConnection *c, DBusM
                 return bus_default_message_handler(c, message, INTROSPECTION, INTERFACES_LIST, bps);
         }
 
-        if (!dbus_connection_send(c, reply, NULL))
+        if (!bus_maybe_send_reply(c, message, reply))
                 return DBUS_HANDLER_RESULT_NEED_MEMORY;
 
         return DBUS_HANDLER_RESULT_HANDLED;
diff --git a/src/core/dbus-unit.c b/src/core/dbus-unit.c
index 7c23e1e..dc7d1f1 100644
--- a/src/core/dbus-unit.c
+++ b/src/core/dbus-unit.c
@@ -582,7 +582,7 @@ static DBusHandlerResult bus_unit_message_dispatch(Unit *u, DBusConnection *conn
         }
 
         if (reply)
-                if (!dbus_connection_send(connection, reply, NULL))
+                if (!bus_maybe_send_reply(connection, message, reply))
                         goto oom;
 
         return DBUS_HANDLER_RESULT_HANDLED;
@@ -673,7 +673,7 @@ static DBusHandlerResult bus_unit_message_handler(DBusConnection *connection, DB
 
                         free(introspection);
 
-                        if (!dbus_connection_send(connection, reply, NULL))
+                        if (!bus_maybe_send_reply(connection, message, reply))
                                 goto oom;
 
                         return DBUS_HANDLER_RESULT_HANDLED;
@@ -886,7 +886,7 @@ DBusHandlerResult bus_unit_queue_job(
                             DBUS_TYPE_INVALID))
                 goto oom;
 
-        if (!dbus_connection_send(connection, reply, NULL))
+        if (!bus_maybe_send_reply(connection, message, reply))
                 goto oom;
 
         return DBUS_HANDLER_RESULT_HANDLED;
diff --git a/src/core/dbus.c b/src/core/dbus.c
index 08aff1f..236ebc9 100644
--- a/src/core/dbus.c
+++ b/src/core/dbus.c
@@ -406,7 +406,7 @@ static DBusHandlerResult api_bus_message_filter(DBusConnection *connection, DBus
         dbus_error_free(&error);
 
         if (reply) {
-                if (!dbus_connection_send(connection, reply, NULL))
+                if (!bus_maybe_send_reply(connection, message, reply))
                         goto oom;
 
                 dbus_message_unref(reply);
diff --git a/src/hostname/hostnamed.c b/src/hostname/hostnamed.c
index 979dcfd..25e154b 100644
--- a/src/hostname/hostnamed.c
+++ b/src/hostname/hostnamed.c
@@ -605,7 +605,7 @@ static DBusHandlerResult hostname_message_handler(
         if (!reply)
                 goto oom;
 
-        if (!dbus_connection_send(connection, reply, NULL))
+        if (!bus_maybe_send_reply(connection, message, reply))
                 goto oom;
 
         dbus_message_unref(reply);
diff --git a/src/locale/localed.c b/src/locale/localed.c
index 4f85b8b..60083b7 100644
--- a/src/locale/localed.c
+++ b/src/locale/localed.c
@@ -1277,7 +1277,7 @@ static DBusHandlerResult locale_message_handler(
         if (!(reply = dbus_message_new_method_return(message)))
                 goto oom;
 
-        if (!dbus_connection_send(connection, reply, NULL))
+        if (!bus_maybe_send_reply(connection, message, reply))
                 goto oom;
 
         dbus_message_unref(reply);
diff --git a/src/login/logind-seat-dbus.c b/src/login/logind-seat-dbus.c
index 7833d70..4da076e 100644
--- a/src/login/logind-seat-dbus.c
+++ b/src/login/logind-seat-dbus.c
@@ -310,7 +310,7 @@ static DBusHandlerResult seat_message_dispatch(
         }
 
         if (reply) {
-                if (!dbus_connection_send(connection, reply, NULL))
+                if (!bus_maybe_send_reply(connection, message, reply))
                         goto oom;
 
                 dbus_message_unref(reply);
diff --git a/src/login/logind-session-dbus.c b/src/login/logind-session-dbus.c
index ef73cd4..c4db315 100644
--- a/src/login/logind-session-dbus.c
+++ b/src/login/logind-session-dbus.c
@@ -412,7 +412,7 @@ static DBusHandlerResult session_message_dispatch(
         }
 
         if (reply) {
-                if (!dbus_connection_send(connection, reply, NULL))
+                if (!bus_maybe_send_reply(connection, message, reply))
                         goto oom;
 
                 dbus_message_unref(reply);
diff --git a/src/login/logind-user-dbus.c b/src/login/logind-user-dbus.c
index ddf9d9d..5951c38 100644
--- a/src/login/logind-user-dbus.c
+++ b/src/login/logind-user-dbus.c
@@ -304,7 +304,7 @@ static DBusHandlerResult user_message_dispatch(
         }
 
         if (reply) {
-                if (!dbus_connection_send(connection, reply, NULL))
+                if (!bus_maybe_send_reply(connection, message, reply))
                         goto oom;
 
                 dbus_message_unref(reply);
diff --git a/src/timedate/timedated.c b/src/timedate/timedated.c
index 7753493..85506f4 100644
--- a/src/timedate/timedated.c
+++ b/src/timedate/timedated.c
@@ -889,7 +889,7 @@ static DBusHandlerResult timedate_message_handler(
         if (!(reply = dbus_message_new_method_return(message)))
                 goto oom;
 
-        if (!dbus_connection_send(connection, reply, NULL))
+        if (!bus_maybe_send_reply(connection, message, reply))
                 goto oom;
 
         dbus_message_unref(reply);

commit 8cb17a6dc88cecfcee3189765031e9e8a1f4106b
Author: Zbigniew Jędrzejewski-Szmek <zbyszek at in.waw.pl>
Date:   Sun Nov 4 16:19:04 2012 +0100

    sd-journal: do not require path to be absolute
    
    Seems natural to be able to specify relative directory,
    e.g. with journalctl -D. And even if, this should be checked
    in front-end code, not in the library.

diff --git a/man/journalctl.xml b/man/journalctl.xml
index 632a813..8883da2 100644
--- a/man/journalctl.xml
+++ b/man/journalctl.xml
@@ -437,7 +437,7 @@
                                 <term><option>-D</option></term>
                                 <term><option>--directory=</option></term>
 
-                                <listitem><para>Takes an absolute
+                                <listitem><para>Takes a
                                 directory path as argument. If
                                 specified journalctl will operate on the
                                 specified journal directory instead of
diff --git a/src/journal/sd-journal.c b/src/journal/sd-journal.c
index 7ffbaf7..ef4b9b2 100644
--- a/src/journal/sd-journal.c
+++ b/src/journal/sd-journal.c
@@ -1596,7 +1596,7 @@ _public_ int sd_journal_open_directory(sd_journal **ret, const char *path, int f
         if (!ret)
                 return -EINVAL;
 
-        if (!path || !path_is_absolute(path))
+        if (!path)
                 return -EINVAL;
 
         if (flags != 0)

commit 763c7aa288485cf5ab627fe1d25ff58e76f9dacb
Author: Zbigniew Jędrzejewski-Szmek <zbyszek at in.waw.pl>
Date:   Sun Mar 17 23:36:25 2013 -0400

    journal,shared: add _cleanup_journal_close_

diff --git a/src/journal/coredumpctl.c b/src/journal/coredumpctl.c
index 8bfab00..99ca269 100644
--- a/src/journal/coredumpctl.c
+++ b/src/journal/coredumpctl.c
@@ -34,6 +34,7 @@
 #include "log.h"
 #include "path-util.h"
 #include "pager.h"
+#include "macro.h"
 
 static enum {
         ACTION_NONE,
@@ -42,7 +43,6 @@ static enum {
         ACTION_GDB,
 } arg_action = ACTION_LIST;
 
-static Set *matches = NULL;
 static FILE* output = NULL;
 static char* field = NULL;
 
@@ -139,7 +139,7 @@ fail:
         return r;
 }
 
-static int parse_argv(int argc, char *argv[]) {
+static int parse_argv(int argc, char *argv[], Set *matches) {
         enum {
                 ARG_VERSION = 0x100,
                 ARG_NO_PAGER,
@@ -519,10 +519,11 @@ finish:
 }
 
 int main(int argc, char *argv[]) {
-        sd_journal *j = NULL;
+        sd_journal _cleanup_journal_close_ *j = NULL;
         const char* match;
         Iterator it;
         int r = 0;
+        Set _cleanup_set_free_free_ *matches = NULL;
 
         setlocale(LC_ALL, "");
         log_parse_environment();
@@ -534,7 +535,7 @@ int main(int argc, char *argv[]) {
                 goto end;
         }
 
-        r = parse_argv(argc, argv);
+        r = parse_argv(argc, argv, matches);
         if (r < 0)
                 goto end;
 
@@ -578,11 +579,6 @@ int main(int argc, char *argv[]) {
         }
 
 end:
-        if (j)
-                sd_journal_close(j);
-
-        set_free_free(matches);
-
         pager_close();
 
         if (output)
diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c
index 65114b2..a6ad055 100644
--- a/src/journal/journalctl.c
+++ b/src/journal/journalctl.c
@@ -897,7 +897,7 @@ static int access_check(void) {
 
 int main(int argc, char *argv[]) {
         int r;
-        sd_journal *j = NULL;
+        sd_journal _cleanup_journal_close_ *j = NULL;
         bool need_seek = false;
         sd_id128_t previous_boot_id;
         bool previous_boot_id_valid = false, first_line = true;
@@ -937,7 +937,7 @@ int main(int argc, char *argv[]) {
 
         r = access_check();
         if (r < 0)
-                goto finish;
+                return EXIT_FAILURE;
 
         if (arg_directory)
                 r = sd_journal_open_directory(&j, arg_directory, 0);
@@ -945,7 +945,7 @@ int main(int argc, char *argv[]) {
                 r = sd_journal_open(&j, arg_merge ? 0 : SD_JOURNAL_LOCAL_ONLY);
         if (r < 0) {
                 log_error("Failed to open journal: %s", strerror(-r));
-                goto finish;
+                return EXIT_FAILURE;
         }
 
         if (arg_action == ACTION_VERIFY) {
@@ -955,8 +955,7 @@ int main(int argc, char *argv[]) {
 
         if (arg_action == ACTION_PRINT_HEADER) {
                 journal_print_header(j);
-                r = 0;
-                goto finish;
+                return EXIT_SUCCESS;
         }
 
         if (arg_action == ACTION_DISK_USAGE) {
@@ -965,33 +964,33 @@ int main(int argc, char *argv[]) {
 
                 r = sd_journal_get_usage(j, &bytes);
                 if (r < 0)
-                        goto finish;
+                        return EXIT_FAILURE;
 
-                printf("Journals take up %s on disk.\n", format_bytes(sbytes, sizeof(sbytes), bytes));
-                r = 0;
-                goto finish;
+                printf("Journals take up %s on disk.\n",
+                       format_bytes(sbytes, sizeof(sbytes), bytes));
+                return EXIT_SUCCESS;
         }
 
         r = add_this_boot(j);
         if (r < 0)
-                goto finish;
+                return EXIT_FAILURE;
 
         r = add_unit(j);
         if (r < 0)
-                goto finish;
+                return EXIT_FAILURE;
 
         r = add_matches(j, argv + optind);
         if (r < 0)
-                goto finish;
+                return EXIT_FAILURE;
 
         r = add_priorities(j);
         if (r < 0)
-                goto finish;
+                return EXIT_FAILURE;
 
         /* Opening the fd now means the first sd_journal_wait() will actually wait */
         r = sd_journal_get_fd(j);
         if (r < 0)
-                goto finish;
+                return EXIT_FAILURE;
 
         if (arg_field) {
                 const void *data;
@@ -1000,7 +999,7 @@ int main(int argc, char *argv[]) {
                 r = sd_journal_query_unique(j, arg_field);
                 if (r < 0) {
                         log_error("Failed to query unique data objects: %s", strerror(-r));
-                        goto finish;
+                        return EXIT_FAILURE;
                 }
 
                 SD_JOURNAL_FOREACH_UNIQUE(j, data, size) {
@@ -1018,15 +1017,14 @@ int main(int argc, char *argv[]) {
                         n_shown ++;
                 }
 
-                r = 0;
-                goto finish;
+                return EXIT_SUCCESS;
         }
 
         if (arg_cursor) {
                 r = sd_journal_seek_cursor(j, arg_cursor);
                 if (r < 0) {
                         log_error("Failed to seek to cursor: %s", strerror(-r));
-                        goto finish;
+                        return EXIT_FAILURE;
                 }
                 if (!arg_reverse)
                         r = sd_journal_next(j);
@@ -1037,7 +1035,7 @@ int main(int argc, char *argv[]) {
                 r = sd_journal_seek_realtime_usec(j, arg_since);
                 if (r < 0) {
                         log_error("Failed to seek to date: %s", strerror(-r));
-                        goto finish;
+                        return EXIT_FAILURE;
                 }
                 r = sd_journal_next(j);
 
@@ -1045,7 +1043,7 @@ int main(int argc, char *argv[]) {
                 r = sd_journal_seek_realtime_usec(j, arg_until);
                 if (r < 0) {
                         log_error("Failed to seek to date: %s", strerror(-r));
-                        goto finish;
+                        return EXIT_FAILURE;
                 }
                 r = sd_journal_previous(j);
 
@@ -1053,7 +1051,7 @@ int main(int argc, char *argv[]) {
                 r = sd_journal_seek_tail(j);
                 if (r < 0) {
                         log_error("Failed to seek to tail: %s", strerror(-r));
-                        goto finish;
+                        return EXIT_FAILURE;
                 }
 
                 r = sd_journal_previous_skip(j, arg_lines);
@@ -1062,7 +1060,7 @@ int main(int argc, char *argv[]) {
                 r = sd_journal_seek_tail(j);
                 if (r < 0) {
                         log_error("Failed to seek to tail: %s", strerror(-r));
-                        goto finish;
+                        return EXIT_FAILURE;
                 }
 
                 r = sd_journal_previous(j);
@@ -1071,7 +1069,7 @@ int main(int argc, char *argv[]) {
                 r = sd_journal_seek_head(j);
                 if (r < 0) {
                         log_error("Failed to seek to head: %s", strerror(-r));
-                        goto finish;
+                        return EXIT_FAILURE;
                 }
 
                 r = sd_journal_next(j);
@@ -1079,7 +1077,7 @@ int main(int argc, char *argv[]) {
 
         if (r < 0) {
                 log_error("Failed to iterate through journal: %s", strerror(-r));
-                goto finish;
+                return EXIT_FAILURE;
         }
 
         if (!arg_no_pager && !arg_follow)
@@ -1189,9 +1187,6 @@ int main(int argc, char *argv[]) {
         }
 
 finish:
-        if (j)
-                sd_journal_close(j);
-
         pager_close();
 
         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
diff --git a/src/journal/journald-server.c b/src/journal/journald-server.c
index 364ab0f..855430a 100644
--- a/src/journal/journald-server.c
+++ b/src/journal/journald-server.c
@@ -960,8 +960,7 @@ finish:
         if (r >= 0)
                 rm_rf("/run/log/journal", false, true, false);
 
-        if (j)
-                sd_journal_close(j);
+        sd_journal_close(j);
 
         return r;
 }
diff --git a/src/journal/test-journal-enum.c b/src/journal/test-journal-enum.c
index 8a843ec..88f583e 100644
--- a/src/journal/test-journal-enum.c
+++ b/src/journal/test-journal-enum.c
@@ -23,10 +23,12 @@
 
 #include "log.h"
 #include "sd-journal.h"
+#include "macro.h"
+#include "util.h"
 
 int main(int argc, char *argv[]) {
         unsigned n = 0;
-        sd_journal *j;
+        sd_journal _cleanup_journal_close_ *j = NULL;
 
         log_set_max_level(LOG_DEBUG);
 
@@ -48,6 +50,5 @@ int main(int argc, char *argv[]) {
                         break;
         }
 
-        sd_journal_close(j);
         return 0;
 }
diff --git a/src/journal/test-journal-match.c b/src/journal/test-journal-match.c
index fa22814..2ca2337 100644
--- a/src/journal/test-journal-match.c
+++ b/src/journal/test-journal-match.c
@@ -28,8 +28,8 @@
 #include "log.h"
 
 int main(int argc, char *argv[]) {
-        sd_journal *j;
-        char *t;
+        sd_journal _cleanup_journal_close_ *j;
+        char _cleanup_free_ *t;
 
         log_set_max_level(LOG_DEBUG);
 
@@ -59,9 +59,6 @@ int main(int argc, char *argv[]) {
         assert_se(streq(t, "((TWO=two AND (ONE=two OR ONE=one)) OR (PIFF=paff AND (QUUX=yyyyy OR QUUX=xxxxx OR QUUX=mmmm) AND (HALLO= OR HALLO=WALDO)))"));
 
         printf("resulting match expression is: %s\n", t);
-        free(t);
-
-        sd_journal_close(j);
 
         return 0;
 }
diff --git a/src/journal/test-journal-stream.c b/src/journal/test-journal-stream.c
index b3e816d..4b73ac7 100644
--- a/src/journal/test-journal-stream.c
+++ b/src/journal/test-journal-stream.c
@@ -75,7 +75,7 @@ int main(int argc, char *argv[]) {
         JournalFile *one, *two, *three;
         char t[] = "/tmp/journal-stream-XXXXXX";
         unsigned i;
-        sd_journal *j;
+        sd_journal _cleanup_journal_close_ *j = NULL;
         char *z;
         const void *data;
         size_t l;
@@ -126,25 +126,23 @@ int main(int argc, char *argv[]) {
 
         assert_se(sd_journal_add_match(j, "MAGIC=quux", 0) >= 0);
         SD_JOURNAL_FOREACH_BACKWARDS(j) {
-                char *c;
+                char _cleanup_free_ *c;
 
                 assert_se(sd_journal_get_data(j, "NUMBER", &data, &l) >= 0);
                 printf("\t%.*s\n", (int) l, (const char*) data);
 
                 assert_se(sd_journal_get_cursor(j, &c) >= 0);
                 assert_se(sd_journal_test_cursor(j, c) > 0);
-                free(c);
         }
 
         SD_JOURNAL_FOREACH(j) {
-                char *c;
+                char _cleanup_free_ *c;
 
                 assert_se(sd_journal_get_data(j, "NUMBER", &data, &l) >= 0);
                 printf("\t%.*s\n", (int) l, (const char*) data);
 
                 assert_se(sd_journal_get_cursor(j, &c) >= 0);
                 assert_se(sd_journal_test_cursor(j, c) > 0);
-                free(c);
         }
 
         sd_journal_flush_matches(j);
@@ -177,8 +175,6 @@ int main(int argc, char *argv[]) {
         SD_JOURNAL_FOREACH_UNIQUE(j, data, l)
                 printf("%.*s\n", (int) l, (const char*) data);
 
-        sd_journal_close(j);
-
         assert_se(rm_rf_dangerous(t, false, true, false) >= 0);
 
         return 0;
diff --git a/src/shared/logs-show.c b/src/shared/logs-show.c
index 4338684..8897a10 100644
--- a/src/shared/logs-show.c
+++ b/src/shared/logs-show.c
@@ -941,7 +941,7 @@ int show_journal_by_unit(
                 OutputFlags flags,
                 bool system) {
 
-        sd_journal *j = NULL;
+        sd_journal _cleanup_journal_close_ *j = NULL;
         int r;
         int jflags = SD_JOURNAL_LOCAL_ONLY | system * SD_JOURNAL_SYSTEM_ONLY;
 
@@ -954,24 +954,20 @@ int show_journal_by_unit(
 
         r = sd_journal_open(&j, jflags);
         if (r < 0)
-                goto finish;
+                return r;
 
         if (system)
                 r = add_matches_for_unit(j, unit);
         else
                 r = add_matches_for_user_unit(j, unit, uid);
         if (r < 0)
-                goto finish;
+                return r;
 
         r = show_journal(f, j, mode, n_columns, not_before, how_many, flags);
         if (r < 0)
-                goto finish;
-
-finish:
-        if (j)
-                sd_journal_close(j);
+                return r;
 
-        return r;
+        return 0;
 }
 
 static const char *const output_mode_table[_OUTPUT_MODE_MAX] = {
diff --git a/src/shared/macro.h b/src/shared/macro.h
index 0559190..90a663b 100644
--- a/src/shared/macro.h
+++ b/src/shared/macro.h
@@ -203,6 +203,7 @@ static inline size_t IOVEC_INCREMENT(struct iovec *i, unsigned n, size_t k) {
 #define _cleanup_set_free_ __attribute__((cleanup(set_freep)))
 #define _cleanup_set_free_free_ __attribute__((cleanup(set_free_freep)))
 #define _cleanup_strv_free_ __attribute__((cleanup(strv_freep)))
+#define _cleanup_journal_close_ __attribute__((cleanup(journal_closep)))
 
 #define VA_FORMAT_ADVANCE(format, ap)                                   \
 do {                                                                    \
diff --git a/src/shared/util.h b/src/shared/util.h
index 8ac4bbc..4be0b61 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -36,6 +36,7 @@
 #include <dirent.h>
 #include <sys/resource.h>
 #include <stddef.h>
+#include <systemd/sd-journal.h>
 
 #include "macro.h"
 #include "time-util.h"
@@ -531,6 +532,10 @@ static inline void umaskp(mode_t *u) {
         umask(*u);
 }
 
+static inline void journal_closep(sd_journal **j) {
+        sd_journal_close(*j);
+}
+
 _malloc_  static inline void *malloc_multiply(size_t a, size_t b) {
         if (_unlikely_(b == 0 || a > ((size_t) -1) / b))
                 return NULL;

commit a50d7d4389217c0d3b527ee260eabf89e4a76caa
Author: Zbigniew Jędrzejewski-Szmek <zbyszek at in.waw.pl>
Date:   Sun Mar 17 22:00:01 2013 -0400

    journal: use _cleanup_
    
    One log_debug() moved to match order in other functions.

diff --git a/src/journal/sd-journal.c b/src/journal/sd-journal.c
index 2c5ee3f..7ffbaf7 100644
--- a/src/journal/sd-journal.c
+++ b/src/journal/sd-journal.c
@@ -1215,7 +1215,7 @@ static void check_network(sd_journal *j, int fd) {
 }
 
 static int add_file(sd_journal *j, const char *prefix, const char *filename) {
-        char *path;
+        char _cleanup_free_ *path = NULL;
         int r;
         JournalFile *f;
 
@@ -1234,20 +1234,15 @@ static int add_file(sd_journal *j, const char *prefix, const char *filename) {
         if (!path)
                 return -ENOMEM;
 
-        if (hashmap_get(j->files, path)) {
-                free(path);
+        if (hashmap_get(j->files, path))
                 return 0;
-        }
 
         if (hashmap_size(j->files) >= JOURNAL_FILES_MAX) {
                 log_debug("Too many open journal files, not adding %s, ignoring.", path);
-                free(path);
                 return 0;
         }
 
         r = journal_file_open(path, O_RDONLY, 0, false, false, NULL, j->mmap, NULL, &f);
-        free(path);
-
         if (r < 0) {
                 if (errno == ENOENT)
                         return 0;
@@ -1263,12 +1258,12 @@ static int add_file(sd_journal *j, const char *prefix, const char *filename) {
                 return r;
         }
 
+        log_debug("File %s got added.", f->path);
+
         check_network(j, f->fd);
 
         j->current_invalidate_counter ++;
 
-        log_debug("File %s got added.", f->path);
-
         return 0;
 }
 
@@ -1311,9 +1306,9 @@ static int remove_file(sd_journal *j, const char *prefix, const char *filename)
 }
 
 static int add_directory(sd_journal *j, const char *prefix, const char *dirname) {
-        char *path;
+        char _cleanup_free_ *path = NULL;
         int r;
-        DIR *d;
+        DIR _cleanup_closedir_ *d = NULL;
         sd_id128_t id, mid;
         Directory *m;
 
@@ -1336,8 +1331,6 @@ static int add_directory(sd_journal *j, const char *prefix, const char *dirname)
         d = opendir(path);
         if (!d) {
                 log_debug("Failed to open %s: %m", path);
-                free(path);
-
                 if (errno == ENOENT)
                         return 0;
                 return -errno;
@@ -1346,32 +1339,24 @@ static int add_directory(sd_journal *j, const char *prefix, const char *dirname)
         m = hashmap_get(j->directories_by_path, path);
         if (!m) {
                 m = new0(Directory, 1);
-                if (!m) {
-                        closedir(d);
-                        free(path);
+                if (!m)
                         return -ENOMEM;
-                }
 
                 m->is_root = false;
                 m->path = path;
 
                 if (hashmap_put(j->directories_by_path, m->path, m) < 0) {
-                        closedir(d);
-                        free(m->path);
                         free(m);
                         return -ENOMEM;
                 }
 
+                path = NULL; /* avoid freeing in cleanup */
                 j->current_invalidate_counter ++;
 
                 log_debug("Directory %s got added.", m->path);
 
-        } else if (m->is_root) {
-                free (path);
-                closedir(d);
+        } else if (m->is_root)
                 return 0;
-        }  else
-                free(path);
 
         if (m->wd <= 0 && j->inotify_fd >= 0) {
 
@@ -1402,13 +1387,11 @@ static int add_directory(sd_journal *j, const char *prefix, const char *dirname)
 
         check_network(j, dirfd(d));
 
-        closedir(d);
-
         return 0;
 }
 
 static int add_root_directory(sd_journal *j, const char *p) {
-        DIR *d;
+        DIR _cleanup_closedir_ *d = NULL;
         Directory *m;
         int r;
 
@@ -1426,21 +1409,17 @@ static int add_root_directory(sd_journal *j, const char *p) {
         m = hashmap_get(j->directories_by_path, p);
         if (!m) {
                 m = new0(Directory, 1);
-                if (!m) {
-                        closedir(d);
+                if (!m)
                         return -ENOMEM;
-                }
 
                 m->is_root = true;
                 m->path = strdup(p);
                 if (!m->path) {
-                        closedir(d);
                         free(m);
                         return -ENOMEM;
                 }
 
                 if (hashmap_put(j->directories_by_path, m->path, m) < 0) {
-                        closedir(d);
                         free(m->path);
                         free(m);
                         return -ENOMEM;
@@ -1450,10 +1429,8 @@ static int add_root_directory(sd_journal *j, const char *p) {
 
                 log_debug("Root directory %s got added.", m->path);
 
-        } else if (!m->is_root) {
-                closedir(d);
+        } else if (!m->is_root)
                 return 0;
-        }
 
         if (m->wd <= 0 && j->inotify_fd >= 0) {
 
@@ -1491,8 +1468,6 @@ static int add_root_directory(sd_journal *j, const char *p) {
 
         check_network(j, dirfd(d));
 
-        closedir(d);
-
         return 0;
 }
 

commit 6180fc611b415e2a26c64658d2ce700f457f4a67
Author: Zbigniew Jędrzejewski-Szmek <zbyszek at in.waw.pl>
Date:   Sun Mar 17 21:52:57 2013 -0400

    journal: use sd_journal_close on error in sd_journal_new

diff --git a/src/journal/sd-journal.c b/src/journal/sd-journal.c
index fa04bfd..2c5ee3f 100644
--- a/src/journal/sd-journal.c
+++ b/src/journal/sd-journal.c
@@ -1568,37 +1568,21 @@ static sd_journal *journal_new(int flags, const char *path) {
 
         if (path) {
                 j->path = strdup(path);
-                if (!j->path) {
-                        free(j);
-                        return NULL;
-                }
+                if (!j->path)
+                        goto fail;
         }
 
         j->files = hashmap_new(string_hash_func, string_compare_func);
-        if (!j->files) {
-                free(j->path);
-                free(j);
-                return NULL;
-        }
-
         j->directories_by_path = hashmap_new(string_hash_func, string_compare_func);
-        if (!j->directories_by_path) {
-                hashmap_free(j->files);
-                free(j->path);
-                free(j);
-                return NULL;
-        }
-
         j->mmap = mmap_cache_new();
-        if (!j->mmap) {
-                hashmap_free(j->files);
-                hashmap_free(j->directories_by_path);
-                free(j->path);
-                free(j);
-                return NULL;
-        }
+        if (!j->files || !j->directories_by_path || !j->mmap)
+                goto fail;
 
         return j;
+
+fail:
+        sd_journal_close(j);
+        return NULL;
 }
 
 _public_ int sd_journal_open(sd_journal **ret, int flags) {

commit 85b2850ba94a2a0c15239893c446b3db39b36638
Author: Zbigniew Jędrzejewski-Szmek <zbyszek at in.waw.pl>
Date:   Mon Mar 18 01:12:25 2013 -0400

    systemd-python: allow Reader to be used as a context manager

diff --git a/src/python-systemd/_reader.c b/src/python-systemd/_reader.c
index 96634a1..6759555 100644
--- a/src/python-systemd/_reader.c
+++ b/src/python-systemd/_reader.c
@@ -153,6 +153,35 @@ PyDoc_STRVAR(Reader_close__doc__,
              "See man:sd_journal_close(3).");
 static PyObject* Reader_close(Reader *self, PyObject *args)
 {
+    assert(self);
+    assert(!args);
+
+    sd_journal_close(self->j);
+    self->j = NULL;
+    Py_RETURN_NONE;
+}
+
+PyDoc_STRVAR(Reader___enter____doc__,
+             "__enter__() -> self\n\n"
+             "Part of the context manager protocol.\n"
+             "Returns self.\n");
+static PyObject* Reader___enter__(PyObject *self, PyObject *args)
+{
+    assert(self);
+    assert(!args);
+
+    Py_INCREF(self);
+    return self;
+}
+
+PyDoc_STRVAR(Reader___exit____doc__,
+             "__exit__(type, value, traceback) -> None\n\n"
+             "Part of the context manager protocol.\n"
+             "Closes the journal.\n");
+static PyObject* Reader___exit__(Reader *self, PyObject *args)
+{
+    assert(self);
+
     sd_journal_close(self->j);
     self->j = NULL;
     Py_RETURN_NONE;
@@ -650,6 +679,9 @@ static PyMethodDef Reader_methods[] = {
     {"fileno",          (PyCFunction) Reader_fileno, METH_NOARGS, Reader_fileno__doc__},
     {"reliable_fd",     (PyCFunction) Reader_reliable_fd, METH_NOARGS, Reader_reliable_fd__doc__},
     {"close",           (PyCFunction) Reader_close, METH_NOARGS, Reader_close__doc__},
+    {"__enter__",       (PyCFunction) Reader___enter__, METH_NOARGS, Reader___enter____doc__},
+    {"__exit__",        (PyCFunction) Reader___exit__, METH_VARARGS, Reader___exit____doc__},
+    {"close",           (PyCFunction) Reader_close, METH_NOARGS, Reader_close__doc__},
     {"get_next",        (PyCFunction) Reader_get_next, METH_VARARGS, Reader_get_next__doc__},
     {"get_previous",    (PyCFunction) Reader_get_previous, METH_VARARGS, Reader_get_previous__doc__},
     {"add_match",       (PyCFunction) Reader_add_match, METH_VARARGS|METH_KEYWORDS, Reader_add_match__doc__},



More information about the systemd-commits mailing list