daandemeyer / rpms / systemd

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