84b277
From 0aad0e32d93d848e707a3cd0edd78c58a4c21829 Mon Sep 17 00:00:00 2001
84b277
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
84b277
Date: Sat, 12 Oct 2013 20:28:21 -0400
84b277
Subject: [PATCH] Introduce udev object cleanup functions
84b277
84b277
Conflicts:
84b277
	src/backlight/backlight.c
84b277
	src/gpt-auto-generator/gpt-auto-generator.c
84b277
84b277
(cherry picked from commit 1ca208fb4f93e5869704af1812cbff7130a2fc03)
84b277
84b277
Related: #1098310
84b277
---
84b277
 Makefile.am                                 |   1 +
84b277
 src/backlight/backlight.c                   |  57 +++++------
84b277
 src/core/umount.c                           | 143 ++++++++++------------------
84b277
 src/cryptsetup/cryptsetup.c                 |  27 ++----
84b277
 src/fsck/fsck.c                             |  13 +--
84b277
 src/gpt-auto-generator/gpt-auto-generator.c | 121 +++++++----------------
84b277
 src/journal/journal-internal.h              |   7 +-
84b277
 src/login/sysfs-show.c                      |  22 ++---
84b277
 src/readahead/readahead-common.c            |  66 +++++--------
84b277
 src/shared/fdset.h                          |   5 +-
84b277
 src/shared/install.c                        |   6 +-
84b277
 src/shared/set.h                            |  10 +-
84b277
 src/shared/strv.h                           |   7 +-
84b277
 src/shared/udev-util.h                      |  37 +++++++
84b277
 src/shared/util.h                           |  33 +++----
84b277
 src/test/test-libudev.c                     |   8 +-
84b277
 src/test/test-udev.c                        |  21 ++--
84b277
 src/tmpfiles/tmpfiles.c                     |   5 +-
84b277
 18 files changed, 227 insertions(+), 362 deletions(-)
84b277
 create mode 100644 src/shared/udev-util.h
84b277
84b277
diff --git a/Makefile.am b/Makefile.am
84b277
index fa2fba6..ef73138 100644
84b277
--- a/Makefile.am
84b277
+++ b/Makefile.am
84b277
@@ -647,6 +647,7 @@ libsystemd_shared_la_SOURCES = \
84b277
 	src/shared/sparse-endian.h \
84b277
 	src/shared/util.c \
84b277
 	src/shared/util.h \
84b277
+	src/shared/udev-util.h \
84b277
 	src/shared/virt.c \
84b277
 	src/shared/virt.h \
84b277
 	src/shared/efivars.c \
84b277
diff --git a/src/backlight/backlight.c b/src/backlight/backlight.c
84b277
index 9b2eada..aa80c29 100644
84b277
--- a/src/backlight/backlight.c
84b277
+++ b/src/backlight/backlight.c
84b277
@@ -19,15 +19,15 @@
84b277
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
84b277
 ***/
84b277
 
84b277
-#include <libudev.h>
84b277
-
84b277
 #include "util.h"
84b277
 #include "mkdir.h"
84b277
 #include "fileio.h"
84b277
+#include "libudev.h"
84b277
+#include "udev-util.h"
84b277
 
84b277
 int main(int argc, char *argv[]) {
84b277
-        struct udev *udev = NULL;
84b277
-        struct udev_device *device = NULL;
84b277
+        _cleanup_udev_unref_ struct udev *udev = NULL;
84b277
+        _cleanup_udev_device_unref_ struct udev_device *device = NULL;
84b277
         _cleanup_free_ char *saved = NULL;
84b277
         int r;
84b277
 
84b277
@@ -45,37 +45,35 @@ int main(int argc, char *argv[]) {
84b277
         r = mkdir_p("/var/lib/systemd/backlight", 0755);
84b277
         if (r < 0) {
84b277
                 log_error("Failed to create backlight directory: %s", strerror(-r));
84b277
-                goto finish;
84b277
+                return EXIT_FAILURE;
84b277
         }
84b277
 
84b277
         udev = udev_new();
84b277
         if (!udev) {
84b277
-                r = log_oom();
84b277
-                goto finish;
84b277
+                log_oom();
84b277
+                return EXIT_FAILURE;
84b277
         }
84b277
 
84b277
         errno = 0;
84b277
         device = udev_device_new_from_subsystem_sysname(udev, "backlight", argv[2]);
84b277
         if (!device) {
84b277
-                if (errno != 0) {
84b277
+                if (errno != 0)
84b277
                         log_error("Failed to get backlight device: %m");
84b277
-                        r = -errno;
84b277
-                } else
84b277
-                        r = log_oom();
84b277
+                else
84b277
+                        log_oom();
84b277
 
84b277
-                goto finish;
84b277
+                return EXIT_FAILURE;
84b277
         }
84b277
 
84b277
         if (!streq_ptr(udev_device_get_subsystem(device), "backlight")) {
84b277
                 log_error("Not a backlight device: %s", argv[2]);
84b277
-                r = -ENODEV;
84b277
-                goto finish;
84b277
+                return EXIT_FAILURE;
84b277
         }
84b277
 
84b277
         saved = strappend("/var/lib/systemd/backlight/", udev_device_get_sysname(device));
84b277
         if (!saved) {
84b277
-                r = log_oom();
84b277
-                goto finish;
84b277
+                log_oom();
84b277
+                return EXIT_FAILURE;
84b277
         }
84b277
 
84b277
         if (streq(argv[1], "load")) {
84b277
@@ -84,19 +82,17 @@ int main(int argc, char *argv[]) {
84b277
                 r = read_one_line_file(saved, &value);
84b277
                 if (r < 0) {
84b277
 
84b277
-                        if (r == -ENOENT) {
84b277
-                                r = 0;
84b277
-                                goto finish;
84b277
-                        }
84b277
+                        if (r == -ENOENT)
84b277
+                                return EXIT_SUCCESS;
84b277
 
84b277
                         log_error("Failed to read %s: %s", saved, strerror(-r));
84b277
-                        goto finish;
84b277
+                        return EXIT_FAILURE;
84b277
                 }
84b277
 
84b277
                 r = udev_device_set_sysattr_value(device, "brightness", value);
84b277
                 if (r < 0) {
84b277
                         log_error("Failed to write system attribute: %s", strerror(-r));
84b277
-                        goto finish;
84b277
+                        return EXIT_FAILURE;
84b277
                 }
84b277
 
84b277
         } else if (streq(argv[1], "save")) {
84b277
@@ -105,28 +101,19 @@ int main(int argc, char *argv[]) {
84b277
                 value = udev_device_get_sysattr_value(device, "brightness");
84b277
                 if (!value) {
84b277
                         log_error("Failed to read system attribute: %s", strerror(-r));
84b277
-                        goto finish;
84b277
+                        return EXIT_FAILURE;
84b277
                 }
84b277
 
84b277
                 r = write_string_file(saved, value);
84b277
                 if (r < 0) {
84b277
                         log_error("Failed to write %s: %s", saved, strerror(-r));
84b277
-                        goto finish;
84b277
+                        return EXIT_FAILURE;
84b277
                 }
84b277
 
84b277
         } else {
84b277
                 log_error("Unknown verb %s.", argv[1]);
84b277
-                r = -EINVAL;
84b277
-                goto finish;
84b277
+                return EXIT_FAILURE;
84b277
         }
84b277
 
84b277
-finish:
84b277
-        if (device)
84b277
-                udev_device_unref(device);
84b277
-
84b277
-        if (udev)
84b277
-                udev_unref(udev);
84b277
-
84b277
-        return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
84b277
-
84b277
+        return EXIT_SUCCESS;
84b277
 }
84b277
diff --git a/src/core/umount.c b/src/core/umount.c
84b277
index 1e95ad7..99dbe27 100644
84b277
--- a/src/core/umount.c
84b277
+++ b/src/core/umount.c
84b277
@@ -27,7 +27,6 @@
84b277
 #include <unistd.h>
84b277
 #include <linux/loop.h>
84b277
 #include <linux/dm-ioctl.h>
84b277
-#include <libudev.h>
84b277
 
84b277
 #include "list.h"
84b277
 #include "mount-setup.h"
84b277
@@ -35,6 +34,8 @@
84b277
 #include "path-util.h"
84b277
 #include "util.h"
84b277
 #include "virt.h"
84b277
+#include "libudev.h"
84b277
+#include "udev-util.h"
84b277
 
84b277
 typedef struct MountPoint {
84b277
         char *path;
84b277
@@ -201,145 +202,108 @@ finish:
84b277
 }
84b277
 
84b277
 static int loopback_list_get(MountPoint **head) {
84b277
-        int r;
84b277
-        struct udev *udev;
84b277
-        struct udev_enumerate *e = NULL;
84b277
+        _cleanup_udev_unref_ struct udev *udev;
84b277
+        _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
84b277
         struct udev_list_entry *item = NULL, *first = NULL;
84b277
 
84b277
         assert(head);
84b277
 
84b277
-        if (!(udev = udev_new())) {
84b277
-                r = -ENOMEM;
84b277
-                goto finish;
84b277
-        }
84b277
+        udev = udev_new();
84b277
+        if (!udev)
84b277
+                return -ENOMEM;
84b277
 
84b277
-        if (!(e = udev_enumerate_new(udev))) {
84b277
-                r = -ENOMEM;
84b277
-                goto finish;
84b277
-        }
84b277
+        e = udev_enumerate_new(udev);
84b277
+        if (!e)
84b277
+                return -ENOMEM;
84b277
 
84b277
         if (udev_enumerate_add_match_subsystem(e, "block") < 0 ||
84b277
             udev_enumerate_add_match_sysname(e, "loop*") < 0 ||
84b277
-            udev_enumerate_add_match_sysattr(e, "loop/backing_file", NULL) < 0) {
84b277
-                r = -EIO;
84b277
-                goto finish;
84b277
-        }
84b277
+            udev_enumerate_add_match_sysattr(e, "loop/backing_file", NULL) < 0)
84b277
+                return -EIO;
84b277
 
84b277
-        if (udev_enumerate_scan_devices(e) < 0) {
84b277
-                r = -EIO;
84b277
-                goto finish;
84b277
-        }
84b277
+        if (udev_enumerate_scan_devices(e) < 0)
84b277
+                return -EIO;
84b277
 
84b277
         first = udev_enumerate_get_list_entry(e);
84b277
         udev_list_entry_foreach(item, first) {
84b277
                 MountPoint *lb;
84b277
-                struct udev_device *d;
84b277
+                _cleanup_udev_device_unref_ struct udev_device *d;
84b277
                 char *loop;
84b277
                 const char *dn;
84b277
 
84b277
-                if (!(d = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item)))) {
84b277
-                        r = -ENOMEM;
84b277
-                        goto finish;
84b277
-                }
84b277
+                d = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item));
84b277
+                if (!d)
84b277
+                        return -ENOMEM;
84b277
 
84b277
-                if (!(dn = udev_device_get_devnode(d))) {
84b277
-                        udev_device_unref(d);
84b277
+                dn = udev_device_get_devnode(d);
84b277
+                if (!dn)
84b277
                         continue;
84b277
-                }
84b277
 
84b277
                 loop = strdup(dn);
84b277
-                udev_device_unref(d);
84b277
-
84b277
-                if (!loop) {
84b277
-                        r = -ENOMEM;
84b277
-                        goto finish;
84b277
-                }
84b277
+                if (!loop)
84b277
+                        return -ENOMEM;
84b277
 
84b277
-                if (!(lb = new0(MountPoint, 1))) {
84b277
+                lb = new0(MountPoint, 1);
84b277
+                if (!lb) {
84b277
                         free(loop);
84b277
-                        r = -ENOMEM;
84b277
-                        goto finish;
84b277
+                        return -ENOMEM;
84b277
                 }
84b277
 
84b277
                 lb->path = loop;
84b277
                 LIST_PREPEND(MountPoint, mount_point, *head, lb);
84b277
         }
84b277
 
84b277
-        r = 0;
84b277
-
84b277
-finish:
84b277
-        if (e)
84b277
-                udev_enumerate_unref(e);
84b277
-
84b277
-        if (udev)
84b277
-                udev_unref(udev);
84b277
-
84b277
-        return r;
84b277
+        return 0;
84b277
 }
84b277
 
84b277
 static int dm_list_get(MountPoint **head) {
84b277
-        int r;
84b277
-        struct udev *udev;
84b277
-        struct udev_enumerate *e = NULL;
84b277
+        _cleanup_udev_unref_ struct udev *udev;
84b277
+        _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
84b277
         struct udev_list_entry *item = NULL, *first = NULL;
84b277
 
84b277
         assert(head);
84b277
 
84b277
-        if (!(udev = udev_new())) {
84b277
-                r = -ENOMEM;
84b277
-                goto finish;
84b277
-        }
84b277
+        udev = udev_new();
84b277
+        if (!udev)
84b277
+                return -ENOMEM;
84b277
 
84b277
-        if (!(e = udev_enumerate_new(udev))) {
84b277
-                r = -ENOMEM;
84b277
-                goto finish;
84b277
-        }
84b277
+        e = udev_enumerate_new(udev);
84b277
+        if (!e)
84b277
+                return -ENOMEM;
84b277
 
84b277
         if (udev_enumerate_add_match_subsystem(e, "block") < 0 ||
84b277
-            udev_enumerate_add_match_sysname(e, "dm-*") < 0) {
84b277
-                r = -EIO;
84b277
-                goto finish;
84b277
-        }
84b277
+            udev_enumerate_add_match_sysname(e, "dm-*") < 0)
84b277
+                return -EIO;
84b277
 
84b277
-        if (udev_enumerate_scan_devices(e) < 0) {
84b277
-                r = -EIO;
84b277
-                goto finish;
84b277
-        }
84b277
+        if (udev_enumerate_scan_devices(e) < 0)
84b277
+                return -EIO;
84b277
 
84b277
         first = udev_enumerate_get_list_entry(e);
84b277
 
84b277
         udev_list_entry_foreach(item, first) {
84b277
                 MountPoint *m;
84b277
-                struct udev_device *d;
84b277
+                _cleanup_udev_device_unref_ struct udev_device *d;
84b277
                 dev_t devnum;
84b277
                 char *node;
84b277
                 const char *dn;
84b277
 
84b277
-                if (!(d = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item)))) {
84b277
-                        r = -ENOMEM;
84b277
-                        goto finish;
84b277
-                }
84b277
+                d = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item));
84b277
+                if (!d)
84b277
+                        return -ENOMEM;
84b277
 
84b277
                 devnum = udev_device_get_devnum(d);
84b277
                 dn = udev_device_get_devnode(d);
84b277
-
84b277
-                if (major(devnum) == 0 || !dn) {
84b277
-                        udev_device_unref(d);
84b277
+                if (major(devnum) == 0 || !dn)
84b277
                         continue;
84b277
-                }
84b277
 
84b277
                 node = strdup(dn);
84b277
-                udev_device_unref(d);
84b277
-
84b277
-                if (!node) {
84b277
-                        r = -ENOMEM;
84b277
-                        goto finish;
84b277
-                }
84b277
+                if (!node)
84b277
+                        return -ENOMEM;
84b277
 
84b277
-                if (!(m = new(MountPoint, 1))) {
84b277
+                m = new(MountPoint, 1);
84b277
+                if (!m) {
84b277
                         free(node);
84b277
-                        r = -ENOMEM;
84b277
-                        goto finish;
84b277
+                        return -ENOMEM;
84b277
                 }
84b277
 
84b277
                 m->path = node;
84b277
@@ -347,16 +311,7 @@ static int dm_list_get(MountPoint **head) {
84b277
                 LIST_PREPEND(MountPoint, mount_point, *head, m);
84b277
         }
84b277
 
84b277
-        r = 0;
84b277
-
84b277
-finish:
84b277
-        if (e)
84b277
-                udev_enumerate_unref(e);
84b277
-
84b277
-        if (udev)
84b277
-                udev_unref(udev);
84b277
-
84b277
-        return r;
84b277
+        return 0;
84b277
 }
84b277
 
84b277
 static int delete_loopback(const char *device) {
84b277
diff --git a/src/cryptsetup/cryptsetup.c b/src/cryptsetup/cryptsetup.c
84b277
index 4f2f52a..0a15b50 100644
84b277
--- a/src/cryptsetup/cryptsetup.c
84b277
+++ b/src/cryptsetup/cryptsetup.c
84b277
@@ -25,7 +25,6 @@
84b277
 #include <mntent.h>
84b277
 
84b277
 #include <libcryptsetup.h>
84b277
-#include <libudev.h>
84b277
 
84b277
 #include "fileio.h"
84b277
 #include "log.h"
84b277
@@ -34,6 +33,8 @@
84b277
 #include "strv.h"
84b277
 #include "ask-password-api.h"
84b277
 #include "def.h"
84b277
+#include "libudev.h"
84b277
+#include "udev-util.h"
84b277
 
84b277
 static const char *opt_type = NULL; /* CRYPT_LUKS1, CRYPT_TCRYPT or CRYPT_PLAIN */
84b277
 static char *opt_cipher = NULL;
84b277
@@ -184,7 +185,7 @@ static void log_glue(int level, const char *msg, void *usrptr) {
84b277
         log_debug("%s", msg);
84b277
 }
84b277
 
84b277
-static char *disk_description(const char *path) {
84b277
+static char* disk_description(const char *path) {
84b277
 
84b277
         static const char name_fields[] = {
84b277
                 "ID_PART_ENTRY_NAME\0"
84b277
@@ -193,10 +194,9 @@ static char *disk_description(const char *path) {
84b277
                 "ID_MODEL\0"
84b277
         };
84b277
 
84b277
-        struct udev *udev = NULL;
84b277
-        struct udev_device *device = NULL;
84b277
+        _cleanup_udev_unref_ struct udev *udev = NULL;
84b277
+        _cleanup_udev_device_unref_ struct udev_device *device = NULL;
84b277
         struct stat st;
84b277
-        char *description = NULL;
84b277
         const char *i;
84b277
 
84b277
         assert(path);
84b277
@@ -213,26 +213,17 @@ static char *disk_description(const char *path) {
84b277
 
84b277
         device = udev_device_new_from_devnum(udev, 'b', st.st_rdev);
84b277
         if (!device)
84b277
-                goto finish;
84b277
+                return NULL;
84b277
 
84b277
         NULSTR_FOREACH(i, name_fields) {
84b277
                 const char *name;
84b277
 
84b277
                 name = udev_device_get_property_value(device, i);
84b277
-                if (!isempty(name)) {
84b277
-                        description = strdup(name);
84b277
-                        break;
84b277
-                }
84b277
+                if (!isempty(name))
84b277
+                        return strdup(name);
84b277
         }
84b277
 
84b277
-finish:
84b277
-        if (device)
84b277
-                udev_device_unref(device);
84b277
-
84b277
-        if (udev)
84b277
-                udev_unref(udev);
84b277
-
84b277
-        return description;
84b277
+        return NULL;
84b277
 }
84b277
 
84b277
 static char *disk_mount_point(const char *label) {
84b277
diff --git a/src/fsck/fsck.c b/src/fsck/fsck.c
84b277
index f298cf7..96a79dd 100644
84b277
--- a/src/fsck/fsck.c
84b277
+++ b/src/fsck/fsck.c
84b277
@@ -27,7 +27,6 @@
84b277
 #include <fcntl.h>
84b277
 #include <sys/file.h>
84b277
 
84b277
-#include <libudev.h>
84b277
 #include <dbus/dbus.h>
84b277
 
84b277
 #include "util.h"
84b277
@@ -36,6 +35,8 @@
84b277
 #include "bus-errors.h"
84b277
 #include "virt.h"
84b277
 #include "fileio.h"
84b277
+#include "libudev.h"
84b277
+#include "udev-util.h"
84b277
 
84b277
 static bool arg_skip = false;
84b277
 static bool arg_force = false;
84b277
@@ -251,8 +252,8 @@ int main(int argc, char *argv[]) {
84b277
         int i = 0, r = EXIT_FAILURE, q;
84b277
         pid_t pid;
84b277
         siginfo_t status;
84b277
-        struct udev *udev = NULL;
84b277
-        struct udev_device *udev_device = NULL;
84b277
+        _cleanup_udev_unref_ struct udev *udev = NULL;
84b277
+        _cleanup_udev_device_unref_ struct udev_device *udev_device = NULL;
84b277
         const char *device;
84b277
         bool root_directory;
84b277
         int progress_pipe[2] = { -1, -1 };
84b277
@@ -400,12 +401,6 @@ int main(int argc, char *argv[]) {
84b277
                 touch("/run/systemd/quotacheck");
84b277
 
84b277
 finish:
84b277
-        if (udev_device)
84b277
-                udev_device_unref(udev_device);
84b277
-
84b277
-        if (udev)
84b277
-                udev_unref(udev);
84b277
-
84b277
         close_pipe(progress_pipe);
84b277
 
84b277
         return r;
84b277
diff --git a/src/gpt-auto-generator/gpt-auto-generator.c b/src/gpt-auto-generator/gpt-auto-generator.c
84b277
index adbf71d..8436d26 100644
84b277
--- a/src/gpt-auto-generator/gpt-auto-generator.c
84b277
+++ b/src/gpt-auto-generator/gpt-auto-generator.c
84b277
@@ -36,6 +36,7 @@
84b277
 #include "missing.h"
84b277
 #include "sd-id128.h"
84b277
 #include "libudev.h"
84b277
+#include "udev-util.h"
84b277
 #include "special.h"
84b277
 #include "unit-name.h"
84b277
 #include "virt.h"
84b277
@@ -50,10 +51,7 @@
84b277
 
84b277
 static const char *arg_dest = "/tmp";
84b277
 
84b277
-static inline void blkid_free_probep(blkid_probe *b) {
84b277
-        if (*b)
84b277
-                blkid_free_probe(*b);
84b277
-}
84b277
+define_trivial_cleanup_func(blkid_probe, blkid_free_probe)
84b277
 #define _cleanup_blkid_freep_probe_ _cleanup_(blkid_free_probep)
84b277
 
84b277
 static int verify_gpt_partition(const char *node, sd_id128_t *type, unsigned *nr, char **fstype) {
84b277
@@ -236,75 +234,62 @@ static int add_home(const char *path, const char *fstype) {
84b277
 }
84b277
 
84b277
 static int enumerate_partitions(struct udev *udev, dev_t dev) {
84b277
-        struct udev_enumerate *e = NULL;
84b277
-        struct udev_device *parent = NULL, *d = NULL;
84b277
+        struct udev_device *parent = NULL;
84b277
+        _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
84b277
+        _cleanup_udev_device_unref_ struct udev_device *d = NULL;
84b277
         struct udev_list_entry *first, *item;
84b277
         unsigned home_nr = (unsigned) -1;
84b277
         _cleanup_free_ char *home = NULL, *home_fstype = NULL;
84b277
         int r;
84b277
 
84b277
         e = udev_enumerate_new(udev);
84b277
-        if (!e) {
84b277
-                r = log_oom();
84b277
-                goto finish;
84b277
-        }
84b277
+        if (!e)
84b277
+                return log_oom();
84b277
 
84b277
         d = udev_device_new_from_devnum(udev, 'b', dev);
84b277
-        if (!d) {
84b277
-                r = log_oom();
84b277
-                goto finish;
84b277
-        }
84b277
+        if (!d)
84b277
+                return log_oom();
84b277
 
84b277
         parent = udev_device_get_parent(d);
84b277
-        if (!parent) {
84b277
-                r = log_oom();
84b277
-                goto finish;
84b277
-        }
84b277
+        if (!parent)
84b277
+                return log_oom();
84b277
 
84b277
         r = udev_enumerate_add_match_parent(e, parent);
84b277
-        if (r < 0) {
84b277
-                r = log_oom();
84b277
-                goto finish;
84b277
-        }
84b277
+        if (r < 0)
84b277
+                return log_oom();
84b277
 
84b277
         r = udev_enumerate_add_match_subsystem(e, "block");
84b277
-        if (r < 0) {
84b277
-                r = log_oom();
84b277
-                goto finish;
84b277
-        }
84b277
+        if (r < 0)
84b277
+                return log_oom();
84b277
 
84b277
         r = udev_enumerate_scan_devices(e);
84b277
         if (r < 0) {
84b277
                 log_error("Failed to enumerate partitions on /dev/block/%u:%u: %s",
84b277
                           major(dev), minor(dev), strerror(-r));
84b277
-                goto finish;
84b277
+                return r;
84b277
         }
84b277
 
84b277
         first = udev_enumerate_get_list_entry(e);
84b277
         udev_list_entry_foreach(item, first) {
84b277
                 _cleanup_free_ char *fstype = NULL;
84b277
                 const char *node = NULL;
84b277
-                struct udev_device *q;
84b277
+                _cleanup_udev_device_unref_ struct udev_device *q;
84b277
                 sd_id128_t type_id;
84b277
                 unsigned nr;
84b277
 
84b277
                 q = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item));
84b277
-                if (!q) {
84b277
-                        r = log_oom();
84b277
-                        goto finish;
84b277
-                }
84b277
+                if (!q)
84b277
+                        return log_oom();
84b277
 
84b277
                 if (udev_device_get_devnum(q) == udev_device_get_devnum(d))
84b277
-                        goto skip;
84b277
+                        continue;
84b277
 
84b277
                 if (udev_device_get_devnum(q) == udev_device_get_devnum(parent))
84b277
-                        goto skip;
84b277
+                        continue;
84b277
 
84b277
                 node = udev_device_get_devnode(q);
84b277
-                if (!node) {
84b277
-                        r = log_oom();
84b277
-                        goto finish;
84b277
-                }
84b277
+                if (!node)
84b277
+                        return log_oom();
84b277
 
84b277
                 r = verify_gpt_partition(node, &type_id, &nr, &fstype);
84b277
                 if (r < 0) {
84b277
@@ -313,11 +298,10 @@ static int enumerate_partitions(struct udev *udev, dev_t dev) {
84b277
                                 continue;
84b277
                         log_error("Failed to verify GPT partition %s: %s",
84b277
                                   node, strerror(-r));
84b277
-                        udev_device_unref(q);
84b277
-                        goto finish;
84b277
+                        return r;
84b277
                 }
84b277
                 if (r == 0)
84b277
-                        goto skip;
84b277
+                        continue;
84b277
 
84b277
                 if (sd_id128_equal(type_id, SD_ID128_MAKE(06,57,fd,6d,a4,ab,43,c4,84,e5,09,33,c8,4b,4f,4f)))
84b277
                         add_swap(node, fstype);
84b277
@@ -326,10 +310,8 @@ static int enumerate_partitions(struct udev *udev, dev_t dev) {
84b277
                         if (!home || nr < home_nr) {
84b277
                                 free(home);
84b277
                                 home = strdup(node);
84b277
-                                if (!home) {
84b277
-                                        r = log_oom();
84b277
-                                        goto finish;
84b277
-                                }
84b277
+                                if (!home)
84b277
+                                        return log_oom();
84b277
 
84b277
                                 home_nr = nr;
84b277
 
84b277
@@ -338,22 +320,11 @@ static int enumerate_partitions(struct udev *udev, dev_t dev) {
84b277
                                 fstype = NULL;
84b277
                         }
84b277
                 }
84b277
-
84b277
-        skip:
84b277
-                udev_device_unref(q);
84b277
         }
84b277
 
84b277
         if (home && home_fstype)
84b277
                 add_home(home, home_fstype);
84b277
 
84b277
-finish:
84b277
-        if (d)
84b277
-                udev_device_unref(d);
84b277
-
84b277
-        if (e)
84b277
-                udev_enumerate_unref(e);
84b277
-
84b277
-
84b277
         return r;
84b277
 }
84b277
 
84b277
@@ -432,44 +403,31 @@ static int get_block_device(const char *path, dev_t *dev) {
84b277
 }
84b277
 
84b277
 static int devno_to_devnode(struct udev *udev, dev_t devno, char **ret) {
84b277
-        struct udev_device *d = NULL;
84b277
+        _cleanup_udev_device_unref_ struct udev_device *d;
84b277
         const char *t;
84b277
         char *n;
84b277
-        int r;
84b277
 
84b277
         d = udev_device_new_from_devnum(udev, 'b', devno);
84b277
-        if (!d) {
84b277
-                r = log_oom();
84b277
-                goto finish;
84b277
-        }
84b277
+        if (!d)
84b277
+                return log_oom();
84b277
 
84b277
         t = udev_device_get_devnode(d);
84b277
-        if (!t) {
84b277
-                r = -ENODEV;
84b277
-                goto finish;
84b277
-        }
84b277
+        if (!t)
84b277
+                return -ENODEV;
84b277
 
84b277
         n = strdup(t);
84b277
-        if (!n) {
84b277
-                r = -ENOMEM;
84b277
-                goto finish;
84b277
-        }
84b277
+        if (!n)
84b277
+                return -ENOMEM;
84b277
 
84b277
         *ret = n;
84b277
-        r = 0;
84b277
-
84b277
-finish:
84b277
-        if (d)
84b277
-                udev_device_unref(d);
84b277
-
84b277
-        return r;
84b277
+        return 0;
84b277
 }
84b277
 
84b277
 int main(int argc, char *argv[]) {
84b277
         _cleanup_free_ char *node = NULL;
84b277
-        struct udev *udev = NULL;
84b277
+        _cleanup_udev_unref_ struct udev *udev = NULL;
84b277
         dev_t devno;
84b277
-        int r;
84b277
+        int r = 0;
84b277
 
84b277
         if (argc > 1 && argc != 4) {
84b277
                 log_error("This program takes three or no arguments.");
84b277
@@ -488,13 +446,11 @@ int main(int argc, char *argv[]) {
84b277
 
84b277
         if (in_initrd()) {
84b277
                 log_debug("In initrd, exiting.");
84b277
-                r = 0;
84b277
                 goto finish;
84b277
         }
84b277
 
84b277
         if (detect_container(NULL) > 0) {
84b277
                 log_debug("In a container, exiting.");
84b277
-                r = 0;
84b277
                 goto finish;
84b277
         }
84b277
 
84b277
@@ -533,8 +489,5 @@ int main(int argc, char *argv[]) {
84b277
         r = enumerate_partitions(udev, devno);
84b277
 
84b277
 finish:
84b277
-        if (udev)
84b277
-                udev_unref(udev);
84b277
-
84b277
         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
84b277
 }
84b277
diff --git a/src/journal/journal-internal.h b/src/journal/journal-internal.h
84b277
index 5bc6535..1bc912c 100644
84b277
--- a/src/journal/journal-internal.h
84b277
+++ b/src/journal/journal-internal.h
84b277
@@ -135,11 +135,8 @@ struct sd_journal {
84b277
 char *journal_make_match_string(sd_journal *j);
84b277
 void journal_print_header(sd_journal *j);
84b277
 
84b277
-static inline void journal_closep(sd_journal **j) {
84b277
-        sd_journal_close(*j);
84b277
-}
84b277
-
84b277
-#define _cleanup_journal_close_ _cleanup_(journal_closep)
84b277
+define_trivial_cleanup_func(sd_journal*, sd_journal_close)
84b277
+#define _cleanup_journal_close_ _cleanup_(sd_journal_closep)
84b277
 
84b277
 #define JOURNAL_FOREACH_DATA_RETVAL(j, data, l, retval)                     \
84b277
         for (sd_journal_restart_data(j); ((retval) = sd_journal_enumerate_data((j), &(data), &(l))) > 0; )
84b277
diff --git a/src/login/sysfs-show.c b/src/login/sysfs-show.c
84b277
index 14de7d8..9fc3054 100644
84b277
--- a/src/login/sysfs-show.c
84b277
+++ b/src/login/sysfs-show.c
84b277
@@ -26,6 +26,7 @@
84b277
 #include "util.h"
84b277
 #include "sysfs-show.h"
84b277
 #include "path-util.h"
84b277
+#include "udev-util.h"
84b277
 
84b277
 static int show_sysfs_one(
84b277
                 struct udev *udev,
84b277
@@ -143,9 +144,9 @@ static int show_sysfs_one(
84b277
 }
84b277
 
84b277
 int show_sysfs(const char *seat, const char *prefix, unsigned n_columns) {
84b277
-        struct udev *udev;
84b277
+        _cleanup_udev_unref_ struct udev *udev;
84b277
+        _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
84b277
         struct udev_list_entry *first = NULL;
84b277
-        struct udev_enumerate *e;
84b277
         int r;
84b277
 
84b277
         if (n_columns <= 0)
84b277
@@ -162,10 +163,8 @@ int show_sysfs(const char *seat, const char *prefix, unsigned n_columns) {
84b277
                 return -ENOMEM;
84b277
 
84b277
         e = udev_enumerate_new(udev);
84b277
-        if (!e) {
84b277
-                r = -ENOMEM;
84b277
-                goto finish;
84b277
-        }
84b277
+        if (!e)
84b277
+                return ENOMEM;
84b277
 
84b277
         if (!streq(seat, "seat0"))
84b277
                 r = udev_enumerate_add_match_tag(e, seat);
84b277
@@ -173,11 +172,11 @@ int show_sysfs(const char *seat, const char *prefix, unsigned n_columns) {
84b277
                 r = udev_enumerate_add_match_tag(e, "seat");
84b277
 
84b277
         if (r < 0)
84b277
-                goto finish;
84b277
+                return r;
84b277
 
84b277
         r = udev_enumerate_scan_devices(e);
84b277
         if (r < 0)
84b277
-                goto finish;
84b277
+                return r;
84b277
 
84b277
         first = udev_enumerate_get_list_entry(e);
84b277
         if (first)
84b277
@@ -185,12 +184,5 @@ int show_sysfs(const char *seat, const char *prefix, unsigned n_columns) {
84b277
         else
84b277
                 printf("%s%s%s\n", prefix, draw_special_char(DRAW_TREE_RIGHT), "(none)");
84b277
 
84b277
-finish:
84b277
-        if (e)
84b277
-                udev_enumerate_unref(e);
84b277
-
84b277
-        if (udev)
84b277
-                udev_unref(udev);
84b277
-
84b277
         return r;
84b277
 }
84b277
diff --git a/src/readahead/readahead-common.c b/src/readahead/readahead-common.c
84b277
index a234a89..aea1fbe 100644
84b277
--- a/src/readahead/readahead-common.c
84b277
+++ b/src/readahead/readahead-common.c
84b277
@@ -27,13 +27,14 @@
84b277
 #include <fcntl.h>
84b277
 #include <sys/mman.h>
84b277
 #include <unistd.h>
84b277
-#include <libudev.h>
84b277
 
84b277
 #include "log.h"
84b277
 #include "readahead-common.h"
84b277
 #include "util.h"
84b277
 #include "missing.h"
84b277
 #include "fileio.h"
84b277
+#include "libudev.h"
84b277
+#include "udev-util.h"
84b277
 
84b277
 int file_verify(int fd, const char *fn, off_t file_size_max, struct stat *st) {
84b277
         assert(fd >= 0);
84b277
@@ -60,9 +61,9 @@ int file_verify(int fd, const char *fn, off_t file_size_max, struct stat *st) {
84b277
 
84b277
 int fs_on_ssd(const char *p) {
84b277
         struct stat st;
84b277
-        struct udev *udev = NULL;
84b277
-        struct udev_device *udev_device = NULL, *look_at = NULL;
84b277
-        bool b = false;
84b277
+        _cleanup_udev_unref_ struct udev *udev = NULL;
84b277
+        _cleanup_udev_device_unref_ struct udev_device *udev_device = NULL;
84b277
+        struct udev_device *look_at = NULL;
84b277
         const char *devtype, *rotational, *model, *id;
84b277
         int r;
84b277
 
84b277
@@ -128,7 +129,7 @@ int fs_on_ssd(const char *p) {
84b277
 
84b277
         udev_device = udev_device_new_from_devnum(udev, 'b', st.st_dev);
84b277
         if (!udev_device)
84b277
-                goto finish;
84b277
+                return false;
84b277
 
84b277
         devtype = udev_device_get_property_value(udev_device, "DEVTYPE");
84b277
         if (devtype && streq(devtype, "partition"))
84b277
@@ -137,46 +138,34 @@ int fs_on_ssd(const char *p) {
84b277
                 look_at = udev_device;
84b277
 
84b277
         if (!look_at)
84b277
-                goto finish;
84b277
+                return false;
84b277
 
84b277
         /* First, try high-level property */
84b277
         id = udev_device_get_property_value(look_at, "ID_SSD");
84b277
-        if (id) {
84b277
-                b = streq(id, "1");
84b277
-                goto finish;
84b277
-        }
84b277
+        if (id)
84b277
+                return streq(id, "1");
84b277
 
84b277
         /* Second, try kernel attribute */
84b277
         rotational = udev_device_get_sysattr_value(look_at, "queue/rotational");
84b277
-        if (rotational) {
84b277
-                b = streq(rotational, "0");
84b277
-                goto finish;
84b277
-        }
84b277
+        if (rotational)
84b277
+                return streq(rotational, "0");
84b277
 
84b277
         /* Finally, fallback to heuristics */
84b277
         look_at = udev_device_get_parent(look_at);
84b277
         if (!look_at)
84b277
-                goto finish;
84b277
+                return false;
84b277
 
84b277
         model = udev_device_get_sysattr_value(look_at, "model");
84b277
         if (model)
84b277
-                b = !!strstr(model, "SSD");
84b277
-
84b277
-finish:
84b277
-        if (udev_device)
84b277
-                udev_device_unref(udev_device);
84b277
-
84b277
-        if (udev)
84b277
-                udev_unref(udev);
84b277
+                return !!strstr(model, "SSD");
84b277
 
84b277
-        return b;
84b277
+        return false;
84b277
 }
84b277
 
84b277
 int fs_on_read_only(const char *p) {
84b277
         struct stat st;
84b277
-        struct udev *udev = NULL;
84b277
-        struct udev_device *udev_device = NULL;
84b277
-        bool b = false;
84b277
+        _cleanup_udev_unref_ struct udev *udev = NULL;
84b277
+        _cleanup_udev_device_unref_ struct udev_device *udev_device = NULL;
84b277
         const char *read_only;
84b277
 
84b277
         assert(p);
84b277
@@ -187,24 +176,19 @@ int fs_on_read_only(const char *p) {
84b277
         if (major(st.st_dev) == 0)
84b277
                 return false;
84b277
 
84b277
-        if (!(udev = udev_new()))
84b277
+        udev = udev_new();
84b277
+        if (!udev)
84b277
                 return -ENOMEM;
84b277
 
84b277
-        if (!(udev_device = udev_device_new_from_devnum(udev, 'b', st.st_dev)))
84b277
-                goto finish;
84b277
-
84b277
-        if ((read_only = udev_device_get_sysattr_value(udev_device, "ro")))
84b277
-                if ((b = streq(read_only, "1")))
84b277
-                        goto finish;
84b277
-
84b277
-finish:
84b277
-        if (udev_device)
84b277
-                udev_device_unref(udev_device);
84b277
+        udev_device = udev_device_new_from_devnum(udev, 'b', st.st_dev);
84b277
+        if (!udev_device)
84b277
+                return false;
84b277
 
84b277
-        if (udev)
84b277
-                udev_unref(udev);
84b277
+        read_only = udev_device_get_sysattr_value(udev_device, "ro");
84b277
+        if (read_only)
84b277
+                return streq(read_only, "1");
84b277
 
84b277
-        return b;
84b277
+        return false;
84b277
 }
84b277
 
84b277
 bool enough_ram(void) {
84b277
diff --git a/src/shared/fdset.h b/src/shared/fdset.h
84b277
index 1a26005..6277e46 100644
84b277
--- a/src/shared/fdset.h
84b277
+++ b/src/shared/fdset.h
84b277
@@ -49,8 +49,5 @@ int fdset_iterate(FDSet *s, Iterator *i);
84b277
 #define FDSET_FOREACH(fd, fds, i) \
84b277
         for ((i) = ITERATOR_FIRST, (fd) = fdset_iterate((fds), &(i)); (fd) >= 0; (fd) = fdset_iterate((fds), &(i)))
84b277
 
84b277
-static inline void fdset_freep(FDSet **fds) {
84b277
-        if (*fds)
84b277
-                fdset_free(*fds);
84b277
-}
84b277
+define_trivial_cleanup_func(FDSet*, fdset_free)
84b277
 #define _cleanup_fdset_free_ _cleanup_(fdset_freep)
84b277
diff --git a/src/shared/install.c b/src/shared/install.c
84b277
index 9722ed4..b9c85b7 100644
84b277
--- a/src/shared/install.c
84b277
+++ b/src/shared/install.c
84b277
@@ -44,10 +44,8 @@ typedef struct {
84b277
         Hashmap *have_installed;
84b277
 } InstallContext;
84b277
 
84b277
-#define _cleanup_lookup_paths_free_ \
84b277
-        __attribute__((cleanup(lookup_paths_free)))
84b277
-#define _cleanup_install_context_done_ \
84b277
-        __attribute__((cleanup(install_context_done)))
84b277
+#define _cleanup_lookup_paths_free_ _cleanup_(lookup_paths_free)
84b277
+#define _cleanup_install_context_done_ _cleanup_(install_context_done)
84b277
 
84b277
 static int lookup_paths_init_from_scope(LookupPaths *paths, UnitFileScope scope) {
84b277
         assert(paths);
84b277
diff --git a/src/shared/set.h b/src/shared/set.h
84b277
index e5d46e9..a291470 100644
84b277
--- a/src/shared/set.h
84b277
+++ b/src/shared/set.h
84b277
@@ -28,19 +28,13 @@
84b277
  * for each set use. */
84b277
 
84b277
 #include "hashmap.h"
84b277
+#include "util.h"
84b277
 
84b277
 typedef struct Set Set;
84b277
 
84b277
 Set *set_new(hash_func_t hash_func, compare_func_t compare_func);
84b277
 void set_free(Set* s);
84b277
-static inline void set_freep(Set **s) {
84b277
-        set_free(*s);
84b277
-}
84b277
-
84b277
 void set_free_free(Set *s);
84b277
-static inline void set_free_freep(Set **s) {
84b277
-        set_free_free(*s);
84b277
-}
84b277
 
84b277
 Set* set_copy(Set *s);
84b277
 int set_ensure_allocated(Set **s, hash_func_t hash_func, compare_func_t compare_func);
84b277
@@ -79,5 +73,7 @@ char **set_get_strv(Set *s);
84b277
 #define SET_FOREACH_BACKWARDS(e, s, i) \
84b277
         for ((i) = ITERATOR_LAST, (e) = set_iterate_backwards((s), &(i)); (e); (e) = set_iterate_backwards((s), &(i)))
84b277
 
84b277
+define_trivial_cleanup_func(Set*, set_free)
84b277
+define_trivial_cleanup_func(Set*, set_free_free)
84b277
 #define _cleanup_set_free_ _cleanup_(set_freep)
84b277
 #define _cleanup_set_free_free_ _cleanup_(set_free_freep)
84b277
diff --git a/src/shared/strv.h b/src/shared/strv.h
84b277
index d1f2a0e..4d117f8 100644
84b277
--- a/src/shared/strv.h
84b277
+++ b/src/shared/strv.h
84b277
@@ -24,16 +24,13 @@
84b277
 #include <stdarg.h>
84b277
 #include <stdbool.h>
84b277
 
84b277
-#include "macro.h"
84b277
+#include "util.h"
84b277
 
84b277
 char *strv_find(char **l, const char *name) _pure_;
84b277
 char *strv_find_prefix(char **l, const char *name) _pure_;
84b277
 
84b277
 void strv_free(char **l);
84b277
-static inline void strv_freep(char ***l) {
84b277
-        strv_free(*l);
84b277
-}
84b277
-
84b277
+define_trivial_cleanup_func(char**, strv_free)
84b277
 #define _cleanup_strv_free_ _cleanup_(strv_freep)
84b277
 
84b277
 char **strv_copy(char * const *l);
84b277
diff --git a/src/shared/udev-util.h b/src/shared/udev-util.h
84b277
new file mode 100644
84b277
index 0000000..bff8f5f
84b277
--- /dev/null
84b277
+++ b/src/shared/udev-util.h
84b277
@@ -0,0 +1,37 @@
84b277
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
84b277
+
84b277
+#pragma once
84b277
+
84b277
+/***
84b277
+  This file is part of systemd.
84b277
+
84b277
+  Copyright 2013 Zbigniew Jędrzejewski-Szmek
84b277
+
84b277
+  systemd is free software; you can redistribute it and/or modify it
84b277
+  under the terms of the GNU Lesser General Public License as published by
84b277
+  the Free Software Foundation; either version 2.1 of the License, or
84b277
+  (at your option) any later version.
84b277
+
84b277
+  systemd is distributed in the hope that it will be useful, but
84b277
+  WITHOUT ANY WARRANTY; without even the implied warranty of
84b277
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
84b277
+  Lesser General Public License for more details.
84b277
+
84b277
+  You should have received a copy of the GNU Lesser General Public License
84b277
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
84b277
+***/
84b277
+
84b277
+#include "udev.h"
84b277
+#include "util.h"
84b277
+
84b277
+define_trivial_cleanup_func(struct udev*, udev_unref)
84b277
+define_trivial_cleanup_func(struct udev_device*, udev_device_unref)
84b277
+define_trivial_cleanup_func(struct udev_enumerate*, udev_enumerate_unref)
84b277
+define_trivial_cleanup_func(struct udev_event*, udev_event_unref)
84b277
+define_trivial_cleanup_func(struct udev_rules*, udev_rules_unref)
84b277
+
84b277
+#define _cleanup_udev_unref_ _cleanup_(udev_unrefp)
84b277
+#define _cleanup_udev_device_unref_ _cleanup_(udev_device_unrefp)
84b277
+#define _cleanup_udev_enumerate_unref_ _cleanup_(udev_enumerate_unrefp)
84b277
+#define _cleanup_udev_event_unref_ _cleanup_(udev_event_unrefp)
84b277
+#define _cleanup_udev_rules_unref_ _cleanup_(udev_rules_unrefp)
84b277
diff --git a/src/shared/util.h b/src/shared/util.h
84b277
index 5a1e1bc..e577ef0 100644
84b277
--- a/src/shared/util.h
84b277
+++ b/src/shared/util.h
84b277
@@ -556,42 +556,33 @@ static inline void freep(void *p) {
84b277
         free(*(void**) p);
84b277
 }
84b277
 
84b277
-static inline void fclosep(FILE **f) {
84b277
-        if (*f)
84b277
-                fclose(*f);
84b277
-}
84b277
-
84b277
-static inline void pclosep(FILE **f) {
84b277
-        if (*f)
84b277
-                pclose(*f);
84b277
-}
84b277
+#define define_trivial_cleanup_func(type, func) \
84b277
+        static inline void func##p(type *p) {   \
84b277
+        if (*p)                                 \
84b277
+                func(*p);                       \
84b277
+        }                                       \
84b277
 
84b277
 static inline void closep(int *fd) {
84b277
         if (*fd >= 0)
84b277
                 close_nointr_nofail(*fd);
84b277
 }
84b277
 
84b277
-static inline void closedirp(DIR **d) {
84b277
-        if (*d)
84b277
-                closedir(*d);
84b277
-}
84b277
-
84b277
 static inline void umaskp(mode_t *u) {
84b277
         umask(*u);
84b277
 }
84b277
 
84b277
-static inline void endmntentp(FILE **f) {
84b277
-        if (*f)
84b277
-                endmntent(*f);
84b277
-}
84b277
+define_trivial_cleanup_func(FILE*, fclose)
84b277
+define_trivial_cleanup_func(FILE*, pclose)
84b277
+define_trivial_cleanup_func(DIR*, closedir)
84b277
+define_trivial_cleanup_func(FILE*, endmntent)
84b277
 
84b277
 #define _cleanup_free_ _cleanup_(freep)
84b277
-#define _cleanup_fclose_ _cleanup_(fclosep)
84b277
-#define _cleanup_pclose_ _cleanup_(pclosep)
84b277
 #define _cleanup_close_ _cleanup_(closep)
84b277
-#define _cleanup_closedir_ _cleanup_(closedirp)
84b277
 #define _cleanup_umask_ _cleanup_(umaskp)
84b277
 #define _cleanup_globfree_ _cleanup_(globfree)
84b277
+#define _cleanup_fclose_ _cleanup_(fclosep)
84b277
+#define _cleanup_pclose_ _cleanup_(pclosep)
84b277
+#define _cleanup_closedir_ _cleanup_(closedirp)
84b277
 #define _cleanup_endmntent_ _cleanup_(endmntentp)
84b277
 
84b277
 _malloc_  _alloc_(1, 2) static inline void *malloc_multiply(size_t a, size_t b) {
84b277
diff --git a/src/test/test-libudev.c b/src/test/test-libudev.c
84b277
index 716767b..ab7d5a9 100644
84b277
--- a/src/test/test-libudev.c
84b277
+++ b/src/test/test-libudev.c
84b277
@@ -29,6 +29,7 @@
84b277
 #include <sys/epoll.h>
84b277
 
84b277
 #include "libudev.h"
84b277
+#include "udev-util.h"
84b277
 #include "util.h"
84b277
 
84b277
 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
84b277
@@ -117,7 +118,7 @@ static void print_device(struct udev_device *device)
84b277
 
84b277
 static int test_device(struct udev *udev, const char *syspath)
84b277
 {
84b277
-        struct udev_device *device;
84b277
+        _cleanup_udev_device_unref_ struct udev_device *device;
84b277
 
84b277
         printf("looking at device: %s\n", syspath);
84b277
         device = udev_device_new_from_syspath(udev, syspath);
84b277
@@ -126,13 +127,13 @@ static int test_device(struct udev *udev, const char *syspath)
84b277
                 return -1;
84b277
         }
84b277
         print_device(device);
84b277
-        udev_device_unref(device);
84b277
+
84b277
         return 0;
84b277
 }
84b277
 
84b277
 static int test_device_parents(struct udev *udev, const char *syspath)
84b277
 {
84b277
-        struct udev_device *device;
84b277
+        _cleanup_udev_device_unref_ struct udev_device *device;
84b277
         struct udev_device *device_parent;
84b277
 
84b277
         printf("looking at device: %s\n", syspath);
84b277
@@ -153,7 +154,6 @@ static int test_device_parents(struct udev *udev, const char *syspath)
84b277
                 print_device(device_parent);
84b277
                 device_parent = udev_device_get_parent(device_parent);
84b277
         } while (device_parent != NULL);
84b277
-        udev_device_unref(device);
84b277
 
84b277
         return 0;
84b277
 }
84b277
diff --git a/src/test/test-udev.c b/src/test/test-udev.c
84b277
index 52b61b4..17825f1 100644
84b277
--- a/src/test/test-udev.c
84b277
+++ b/src/test/test-udev.c
84b277
@@ -34,6 +34,7 @@
84b277
 
84b277
 #include "missing.h"
84b277
 #include "udev.h"
84b277
+#include "udev-util.h"
84b277
 
84b277
 void udev_main_log(struct udev *udev, int priority,
84b277
                    const char *file, int line, const char *fn,
84b277
@@ -82,10 +83,10 @@ out:
84b277
 
84b277
 int main(int argc, char *argv[])
84b277
 {
84b277
-        struct udev *udev;
84b277
-        struct udev_event *event = NULL;
84b277
-        struct udev_device *dev = NULL;
84b277
-        struct udev_rules *rules = NULL;
84b277
+        _cleanup_udev_unref_ struct udev *udev = NULL;
84b277
+        _cleanup_udev_event_unref_ struct udev_event *event = NULL;
84b277
+        _cleanup_udev_device_unref_ struct udev_device *dev = NULL;
84b277
+        _cleanup_udev_rules_unref_ struct udev_rules *rules = NULL;
84b277
         char syspath[UTIL_PATH_SIZE];
84b277
         const char *devpath;
84b277
         const char *action;
84b277
@@ -98,7 +99,8 @@ int main(int argc, char *argv[])
84b277
 
84b277
         udev = udev_new();
84b277
         if (udev == NULL)
84b277
-                exit(EXIT_FAILURE);
84b277
+                return EXIT_FAILURE;
84b277
+
84b277
         log_debug("version %s\n", VERSION);
84b277
         label_init("/dev");
84b277
 
84b277
@@ -160,12 +162,7 @@ int main(int argc, char *argv[])
84b277
 out:
84b277
         if (event != NULL && event->fd_signal >= 0)
84b277
                 close(event->fd_signal);
84b277
-        udev_event_unref(event);
84b277
-        udev_device_unref(dev);
84b277
-        udev_rules_unref(rules);
84b277
         label_finish();
84b277
-        udev_unref(udev);
84b277
-        if (err != 0)
84b277
-                return EXIT_FAILURE;
84b277
-        return EXIT_SUCCESS;
84b277
+
84b277
+        return err ? EXIT_FAILURE : EXIT_SUCCESS;
84b277
 }
84b277
diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c
84b277
index 67b13bc..309fa07 100644
84b277
--- a/src/tmpfiles/tmpfiles.c
84b277
+++ b/src/tmpfiles/tmpfiles.c
84b277
@@ -996,10 +996,7 @@ static void item_free(Item *i) {
84b277
         free(i);
84b277
 }
84b277
 
84b277
-static inline void item_freep(Item **i) {
84b277
-        if (*i)
84b277
-                item_free(*i);
84b277
-}
84b277
+define_trivial_cleanup_func(Item*, item_free)
84b277
 #define _cleanup_item_free_ _cleanup_(item_freep)
84b277
 
84b277
 static bool item_equal(Item *a, Item *b) {