richardphibel / rpms / systemd

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