dcavalca / rpms / systemd

Forked from rpms/systemd 3 months ago
Clone
Blob Blame History Raw
From 48ee0228ec2bcfcb48866b717ac8a5a8dbbd506e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
Date: Sat, 12 Oct 2013 20:28:21 -0400
Subject: [PATCH] Introduce udev object cleanup functions

Conflicts:
	src/gpt-auto-generator/gpt-auto-generator.c <-- skipped
	src/shared/fdset.h
	src/shared/util.h
---
 Makefile.am                      |   1 +
 src/backlight/backlight.c        |  55 ++++++---------
 src/core/umount.c                | 143 ++++++++++++++-------------------------
 src/cryptsetup/cryptsetup.c      |  27 +++-----
 src/fsck/fsck.c                  |  13 ++--
 src/journal/journal-internal.h   |   7 +-
 src/login/sysfs-show.c           |  22 ++----
 src/readahead/readahead-common.c |  66 +++++++-----------
 src/shared/fdset.h               |   3 +
 src/shared/install.c             |   6 +-
 src/shared/set.h                 |  10 +--
 src/shared/strv.h                |   7 +-
 src/shared/udev-util.h           |  37 ++++++++++
 src/shared/util.h                |  32 ++++-----
 src/test/test-libudev.c          |   8 +--
 src/test/test-udev.c             |  21 +++---
 src/tmpfiles/tmpfiles.c          |   5 +-
 17 files changed, 194 insertions(+), 269 deletions(-)
 create mode 100644 src/shared/udev-util.h

diff --git a/Makefile.am b/Makefile.am
index efe5aa3..910e780 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -648,6 +648,7 @@ libsystemd_shared_la_SOURCES = \
 	src/shared/sparse-endian.h \
 	src/shared/util.c \
 	src/shared/util.h \
+	src/shared/udev-util.h \
 	src/shared/virt.c \
 	src/shared/virt.h \
 	src/shared/efivars.c \
diff --git a/src/backlight/backlight.c b/src/backlight/backlight.c
index f22deed..c45b2d0 100644
--- a/src/backlight/backlight.c
+++ b/src/backlight/backlight.c
@@ -19,15 +19,15 @@
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
-#include <libudev.h>
-
 #include "util.h"
 #include "mkdir.h"
 #include "fileio.h"
+#include "libudev.h"
+#include "udev-util.h"
 
 int main(int argc, char *argv[]) {
-        struct udev *udev = NULL;
-        struct udev_device *device = NULL;
+        _cleanup_udev_unref_ struct udev *udev = NULL;
+        _cleanup_udev_device_unref_ struct udev_device *device = NULL;
         _cleanup_free_ char *saved = NULL;
         int r;
 
@@ -45,13 +45,13 @@ int main(int argc, char *argv[]) {
         r = mkdir_p("/var/lib/systemd/backlight", 0755);
         if (r < 0) {
                 log_error("Failed to create backlight directory: %s", strerror(-r));
-                goto finish;
+                return EXIT_FAILURE;
         }
 
         udev = udev_new();
         if (!udev) {
-                r = log_oom();
-                goto finish;
+                log_oom();
+                return EXIT_FAILURE;
         }
 
         errno = 0;
@@ -59,26 +59,24 @@ int main(int argc, char *argv[]) {
         if (!device)
                 device = udev_device_new_from_subsystem_sysname(udev, "leds", argv[2]);
         if (!device) {
-                if (errno != 0) {
+                if (errno != 0)
                         log_error("Failed to get backlight device '%s': %m", argv[2]);
-                        r = -errno;
-                } else
+                else
                         r = log_oom();
 
-                goto finish;
+                return EXIT_FAILURE;
         }
 
         if (!streq_ptr(udev_device_get_subsystem(device), "backlight") &&
             !streq_ptr(udev_device_get_subsystem(device), "leds")) {
                 log_error("Not a backlight device: %s", argv[2]);
-                r = -ENODEV;
-                goto finish;
+                return EXIT_FAILURE;
         }
 
         saved = strappend("/var/lib/systemd/backlight/", udev_device_get_sysname(device));
         if (!saved) {
-                r = log_oom();
-                goto finish;
+                log_oom();
+                return EXIT_FAILURE;
         }
 
         if (streq(argv[1], "load")) {
@@ -87,19 +85,17 @@ int main(int argc, char *argv[]) {
                 r = read_one_line_file(saved, &value);
                 if (r < 0) {
 
-                        if (r == -ENOENT) {
-                                r = 0;
-                                goto finish;
-                        }
+                        if (r == -ENOENT)
+                                return EXIT_SUCCESS;
 
                         log_error("Failed to read %s: %s", saved, strerror(-r));
-                        goto finish;
+                        return EXIT_FAILURE;
                 }
 
                 r = udev_device_set_sysattr_value(device, "brightness", value);
                 if (r < 0) {
                         log_error("Failed to write system attribute: %s", strerror(-r));
-                        goto finish;
+                        return EXIT_FAILURE;
                 }
 
         } else if (streq(argv[1], "save")) {
@@ -108,28 +104,19 @@ int main(int argc, char *argv[]) {
                 value = udev_device_get_sysattr_value(device, "brightness");
                 if (!value) {
                         log_error("Failed to read system attribute: %s", strerror(-r));
-                        goto finish;
+                        return EXIT_FAILURE;
                 }
 
                 r = write_string_file(saved, value);
                 if (r < 0) {
                         log_error("Failed to write %s: %s", saved, strerror(-r));
-                        goto finish;
+                        return EXIT_FAILURE;
                 }
 
         } else {
                 log_error("Unknown verb %s.", argv[1]);
-                r = -EINVAL;
-                goto finish;
+                return EXIT_FAILURE;
         }
 
-finish:
-        if (device)
-                udev_device_unref(device);
-
-        if (udev)
-                udev_unref(udev);
-
-        return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
-
+        return EXIT_SUCCESS;
 }
diff --git a/src/core/umount.c b/src/core/umount.c
index 1e95ad7..99dbe27 100644
--- a/src/core/umount.c
+++ b/src/core/umount.c
@@ -27,7 +27,6 @@
 #include <unistd.h>
 #include <linux/loop.h>
 #include <linux/dm-ioctl.h>
-#include <libudev.h>
 
 #include "list.h"
 #include "mount-setup.h"
@@ -35,6 +34,8 @@
 #include "path-util.h"
 #include "util.h"
 #include "virt.h"
+#include "libudev.h"
+#include "udev-util.h"
 
 typedef struct MountPoint {
         char *path;
@@ -201,145 +202,108 @@ finish:
 }
 
 static int loopback_list_get(MountPoint **head) {
-        int r;
-        struct udev *udev;
-        struct udev_enumerate *e = NULL;
+        _cleanup_udev_unref_ struct udev *udev;
+        _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
         struct udev_list_entry *item = NULL, *first = NULL;
 
         assert(head);
 
-        if (!(udev = udev_new())) {
-                r = -ENOMEM;
-                goto finish;
-        }
+        udev = udev_new();
+        if (!udev)
+                return -ENOMEM;
 
-        if (!(e = udev_enumerate_new(udev))) {
-                r = -ENOMEM;
-                goto finish;
-        }
+        e = udev_enumerate_new(udev);
+        if (!e)
+                return -ENOMEM;
 
         if (udev_enumerate_add_match_subsystem(e, "block") < 0 ||
             udev_enumerate_add_match_sysname(e, "loop*") < 0 ||
-            udev_enumerate_add_match_sysattr(e, "loop/backing_file", NULL) < 0) {
-                r = -EIO;
-                goto finish;
-        }
+            udev_enumerate_add_match_sysattr(e, "loop/backing_file", NULL) < 0)
+                return -EIO;
 
-        if (udev_enumerate_scan_devices(e) < 0) {
-                r = -EIO;
-                goto finish;
-        }
+        if (udev_enumerate_scan_devices(e) < 0)
+                return -EIO;
 
         first = udev_enumerate_get_list_entry(e);
         udev_list_entry_foreach(item, first) {
                 MountPoint *lb;
-                struct udev_device *d;
+                _cleanup_udev_device_unref_ struct udev_device *d;
                 char *loop;
                 const char *dn;
 
-                if (!(d = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item)))) {
-                        r = -ENOMEM;
-                        goto finish;
-                }
+                d = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item));
+                if (!d)
+                        return -ENOMEM;
 
-                if (!(dn = udev_device_get_devnode(d))) {
-                        udev_device_unref(d);
+                dn = udev_device_get_devnode(d);
+                if (!dn)
                         continue;
-                }
 
                 loop = strdup(dn);
-                udev_device_unref(d);
-
-                if (!loop) {
-                        r = -ENOMEM;
-                        goto finish;
-                }
+                if (!loop)
+                        return -ENOMEM;
 
-                if (!(lb = new0(MountPoint, 1))) {
+                lb = new0(MountPoint, 1);
+                if (!lb) {
                         free(loop);
-                        r = -ENOMEM;
-                        goto finish;
+                        return -ENOMEM;
                 }
 
                 lb->path = loop;
                 LIST_PREPEND(MountPoint, mount_point, *head, lb);
         }
 
-        r = 0;
-
-finish:
-        if (e)
-                udev_enumerate_unref(e);
-
-        if (udev)
-                udev_unref(udev);
-
-        return r;
+        return 0;
 }
 
 static int dm_list_get(MountPoint **head) {
-        int r;
-        struct udev *udev;
-        struct udev_enumerate *e = NULL;
+        _cleanup_udev_unref_ struct udev *udev;
+        _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
         struct udev_list_entry *item = NULL, *first = NULL;
 
         assert(head);
 
-        if (!(udev = udev_new())) {
-                r = -ENOMEM;
-                goto finish;
-        }
+        udev = udev_new();
+        if (!udev)
+                return -ENOMEM;
 
-        if (!(e = udev_enumerate_new(udev))) {
-                r = -ENOMEM;
-                goto finish;
-        }
+        e = udev_enumerate_new(udev);
+        if (!e)
+                return -ENOMEM;
 
         if (udev_enumerate_add_match_subsystem(e, "block") < 0 ||
-            udev_enumerate_add_match_sysname(e, "dm-*") < 0) {
-                r = -EIO;
-                goto finish;
-        }
+            udev_enumerate_add_match_sysname(e, "dm-*") < 0)
+                return -EIO;
 
-        if (udev_enumerate_scan_devices(e) < 0) {
-                r = -EIO;
-                goto finish;
-        }
+        if (udev_enumerate_scan_devices(e) < 0)
+                return -EIO;
 
         first = udev_enumerate_get_list_entry(e);
 
         udev_list_entry_foreach(item, first) {
                 MountPoint *m;
-                struct udev_device *d;
+                _cleanup_udev_device_unref_ struct udev_device *d;
                 dev_t devnum;
                 char *node;
                 const char *dn;
 
-                if (!(d = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item)))) {
-                        r = -ENOMEM;
-                        goto finish;
-                }
+                d = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item));
+                if (!d)
+                        return -ENOMEM;
 
                 devnum = udev_device_get_devnum(d);
                 dn = udev_device_get_devnode(d);
-
-                if (major(devnum) == 0 || !dn) {
-                        udev_device_unref(d);
+                if (major(devnum) == 0 || !dn)
                         continue;
-                }
 
                 node = strdup(dn);
-                udev_device_unref(d);
-
-                if (!node) {
-                        r = -ENOMEM;
-                        goto finish;
-                }
+                if (!node)
+                        return -ENOMEM;
 
-                if (!(m = new(MountPoint, 1))) {
+                m = new(MountPoint, 1);
+                if (!m) {
                         free(node);
-                        r = -ENOMEM;
-                        goto finish;
+                        return -ENOMEM;
                 }
 
                 m->path = node;
@@ -347,16 +311,7 @@ static int dm_list_get(MountPoint **head) {
                 LIST_PREPEND(MountPoint, mount_point, *head, m);
         }
 
-        r = 0;
-
-finish:
-        if (e)
-                udev_enumerate_unref(e);
-
-        if (udev)
-                udev_unref(udev);
-
-        return r;
+        return 0;
 }
 
 static int delete_loopback(const char *device) {
diff --git a/src/cryptsetup/cryptsetup.c b/src/cryptsetup/cryptsetup.c
index 769c3e4..39f7db7 100644
--- a/src/cryptsetup/cryptsetup.c
+++ b/src/cryptsetup/cryptsetup.c
@@ -25,7 +25,6 @@
 #include <mntent.h>
 
 #include <libcryptsetup.h>
-#include <libudev.h>
 
 #include "fileio.h"
 #include "log.h"
@@ -34,6 +33,8 @@
 #include "strv.h"
 #include "ask-password-api.h"
 #include "def.h"
+#include "libudev.h"
+#include "udev-util.h"
 
 static const char *opt_type = NULL; /* CRYPT_LUKS1, CRYPT_TCRYPT or CRYPT_PLAIN */
 static char *opt_cipher = NULL;
@@ -184,7 +185,7 @@ static void log_glue(int level, const char *msg, void *usrptr) {
         log_debug("%s", msg);
 }
 
-static char *disk_description(const char *path) {
+static char* disk_description(const char *path) {
 
         static const char name_fields[] = {
                 "ID_PART_ENTRY_NAME\0"
@@ -193,10 +194,9 @@ static char *disk_description(const char *path) {
                 "ID_MODEL\0"
         };
 
-        struct udev *udev = NULL;
-        struct udev_device *device = NULL;
+        _cleanup_udev_unref_ struct udev *udev = NULL;
+        _cleanup_udev_device_unref_ struct udev_device *device = NULL;
         struct stat st;
-        char *description = NULL;
         const char *i;
 
         assert(path);
@@ -213,26 +213,17 @@ static char *disk_description(const char *path) {
 
         device = udev_device_new_from_devnum(udev, 'b', st.st_rdev);
         if (!device)
-                goto finish;
+                return NULL;
 
         NULSTR_FOREACH(i, name_fields) {
                 const char *name;
 
                 name = udev_device_get_property_value(device, i);
-                if (!isempty(name)) {
-                        description = strdup(name);
-                        break;
-                }
+                if (!isempty(name))
+                        return strdup(name);
         }
 
-finish:
-        if (device)
-                udev_device_unref(device);
-
-        if (udev)
-                udev_unref(udev);
-
-        return description;
+        return NULL;
 }
 
 static char *disk_mount_point(const char *label) {
diff --git a/src/fsck/fsck.c b/src/fsck/fsck.c
index e23ddc5..9b4e555 100644
--- a/src/fsck/fsck.c
+++ b/src/fsck/fsck.c
@@ -27,7 +27,6 @@
 #include <fcntl.h>
 #include <sys/file.h>
 
-#include <libudev.h>
 #include <dbus/dbus.h>
 
 #include "util.h"
@@ -36,6 +35,8 @@
 #include "bus-errors.h"
 #include "virt.h"
 #include "fileio.h"
+#include "libudev.h"
+#include "udev-util.h"
 
 static bool arg_skip = false;
 static bool arg_force = false;
@@ -241,8 +242,8 @@ int main(int argc, char *argv[]) {
         int i = 0, r = EXIT_FAILURE, q;
         pid_t pid;
         siginfo_t status;
-        struct udev *udev = NULL;
-        struct udev_device *udev_device = NULL;
+        _cleanup_udev_unref_ struct udev *udev = NULL;
+        _cleanup_udev_device_unref_ struct udev_device *udev_device = NULL;
         const char *device;
         bool root_directory;
         int progress_pipe[2] = { -1, -1 };
@@ -393,12 +394,6 @@ int main(int argc, char *argv[]) {
                 touch("/run/systemd/quotacheck");
 
 finish:
-        if (udev_device)
-                udev_device_unref(udev_device);
-
-        if (udev)
-                udev_unref(udev);
-
         close_pipe(progress_pipe);
 
         return r;
diff --git a/src/journal/journal-internal.h b/src/journal/journal-internal.h
index 5bc6535..1bc912c 100644
--- a/src/journal/journal-internal.h
+++ b/src/journal/journal-internal.h
@@ -135,11 +135,8 @@ struct sd_journal {
 char *journal_make_match_string(sd_journal *j);
 void journal_print_header(sd_journal *j);
 
-static inline void journal_closep(sd_journal **j) {
-        sd_journal_close(*j);
-}
-
-#define _cleanup_journal_close_ _cleanup_(journal_closep)
+define_trivial_cleanup_func(sd_journal*, sd_journal_close)
+#define _cleanup_journal_close_ _cleanup_(sd_journal_closep)
 
 #define JOURNAL_FOREACH_DATA_RETVAL(j, data, l, retval)                     \
         for (sd_journal_restart_data(j); ((retval) = sd_journal_enumerate_data((j), &(data), &(l))) > 0; )
diff --git a/src/login/sysfs-show.c b/src/login/sysfs-show.c
index 3c03bd1..7c1adfa 100644
--- a/src/login/sysfs-show.c
+++ b/src/login/sysfs-show.c
@@ -26,6 +26,7 @@
 #include "util.h"
 #include "sysfs-show.h"
 #include "path-util.h"
+#include "udev-util.h"
 
 static int show_sysfs_one(
                 struct udev *udev,
@@ -143,9 +144,9 @@ static int show_sysfs_one(
 }
 
 int show_sysfs(const char *seat, const char *prefix, unsigned n_columns) {
-        struct udev *udev;
+        _cleanup_udev_unref_ struct udev *udev;
+        _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
         struct udev_list_entry *first = NULL;
-        struct udev_enumerate *e;
         int r;
 
         if (n_columns <= 0)
@@ -162,10 +163,8 @@ int show_sysfs(const char *seat, const char *prefix, unsigned n_columns) {
                 return -ENOMEM;
 
         e = udev_enumerate_new(udev);
-        if (!e) {
-                r = -ENOMEM;
-                goto finish;
-        }
+        if (!e)
+                return ENOMEM;
 
         if (!streq(seat, "seat0"))
                 r = udev_enumerate_add_match_tag(e, seat);
@@ -173,22 +172,15 @@ int show_sysfs(const char *seat, const char *prefix, unsigned n_columns) {
                 r = udev_enumerate_add_match_tag(e, "seat");
 
         if (r < 0)
-                goto finish;
+                return r;
 
         r = udev_enumerate_scan_devices(e);
         if (r < 0)
-                goto finish;
+                return r;
 
         first = udev_enumerate_get_list_entry(e);
         if (first)
                 show_sysfs_one(udev, seat, &first, "/", prefix, n_columns);
 
-finish:
-        if (e)
-                udev_enumerate_unref(e);
-
-        if (udev)
-                udev_unref(udev);
-
         return r;
 }
diff --git a/src/readahead/readahead-common.c b/src/readahead/readahead-common.c
index a234a89..aea1fbe 100644
--- a/src/readahead/readahead-common.c
+++ b/src/readahead/readahead-common.c
@@ -27,13 +27,14 @@
 #include <fcntl.h>
 #include <sys/mman.h>
 #include <unistd.h>
-#include <libudev.h>
 
 #include "log.h"
 #include "readahead-common.h"
 #include "util.h"
 #include "missing.h"
 #include "fileio.h"
+#include "libudev.h"
+#include "udev-util.h"
 
 int file_verify(int fd, const char *fn, off_t file_size_max, struct stat *st) {
         assert(fd >= 0);
@@ -60,9 +61,9 @@ int file_verify(int fd, const char *fn, off_t file_size_max, struct stat *st) {
 
 int fs_on_ssd(const char *p) {
         struct stat st;
-        struct udev *udev = NULL;
-        struct udev_device *udev_device = NULL, *look_at = NULL;
-        bool b = false;
+        _cleanup_udev_unref_ struct udev *udev = NULL;
+        _cleanup_udev_device_unref_ struct udev_device *udev_device = NULL;
+        struct udev_device *look_at = NULL;
         const char *devtype, *rotational, *model, *id;
         int r;
 
@@ -128,7 +129,7 @@ int fs_on_ssd(const char *p) {
 
         udev_device = udev_device_new_from_devnum(udev, 'b', st.st_dev);
         if (!udev_device)
-                goto finish;
+                return false;
 
         devtype = udev_device_get_property_value(udev_device, "DEVTYPE");
         if (devtype && streq(devtype, "partition"))
@@ -137,46 +138,34 @@ int fs_on_ssd(const char *p) {
                 look_at = udev_device;
 
         if (!look_at)
-                goto finish;
+                return false;
 
         /* First, try high-level property */
         id = udev_device_get_property_value(look_at, "ID_SSD");
-        if (id) {
-                b = streq(id, "1");
-                goto finish;
-        }
+        if (id)
+                return streq(id, "1");
 
         /* Second, try kernel attribute */
         rotational = udev_device_get_sysattr_value(look_at, "queue/rotational");
-        if (rotational) {
-                b = streq(rotational, "0");
-                goto finish;
-        }
+        if (rotational)
+                return streq(rotational, "0");
 
         /* Finally, fallback to heuristics */
         look_at = udev_device_get_parent(look_at);
         if (!look_at)
-                goto finish;
+                return false;
 
         model = udev_device_get_sysattr_value(look_at, "model");
         if (model)
-                b = !!strstr(model, "SSD");
-
-finish:
-        if (udev_device)
-                udev_device_unref(udev_device);
-
-        if (udev)
-                udev_unref(udev);
+                return !!strstr(model, "SSD");
 
-        return b;
+        return false;
 }
 
 int fs_on_read_only(const char *p) {
         struct stat st;
-        struct udev *udev = NULL;
-        struct udev_device *udev_device = NULL;
-        bool b = false;
+        _cleanup_udev_unref_ struct udev *udev = NULL;
+        _cleanup_udev_device_unref_ struct udev_device *udev_device = NULL;
         const char *read_only;
 
         assert(p);
@@ -187,24 +176,19 @@ int fs_on_read_only(const char *p) {
         if (major(st.st_dev) == 0)
                 return false;
 
-        if (!(udev = udev_new()))
+        udev = udev_new();
+        if (!udev)
                 return -ENOMEM;
 
-        if (!(udev_device = udev_device_new_from_devnum(udev, 'b', st.st_dev)))
-                goto finish;
-
-        if ((read_only = udev_device_get_sysattr_value(udev_device, "ro")))
-                if ((b = streq(read_only, "1")))
-                        goto finish;
-
-finish:
-        if (udev_device)
-                udev_device_unref(udev_device);
+        udev_device = udev_device_new_from_devnum(udev, 'b', st.st_dev);
+        if (!udev_device)
+                return false;
 
-        if (udev)
-                udev_unref(udev);
+        read_only = udev_device_get_sysattr_value(udev_device, "ro");
+        if (read_only)
+                return streq(read_only, "1");
 
-        return b;
+        return false;
 }
 
 bool enough_ram(void) {
diff --git a/src/shared/fdset.h b/src/shared/fdset.h
index a7bd5e2..d0dc875 100644
--- a/src/shared/fdset.h
+++ b/src/shared/fdset.h
@@ -47,3 +47,6 @@ int fdset_iterate(FDSet *s, Iterator *i);
 
 #define FDSET_FOREACH(fd, fds, i) \
         for ((i) = ITERATOR_FIRST, (fd) = fdset_iterate((fds), &(i)); (fd) >= 0; (fd) = fdset_iterate((fds), &(i)))
+
+define_trivial_cleanup_func(FDSet*, fdset_free)
+#define _cleanup_fdset_free_ _cleanup_(fdset_freep)
diff --git a/src/shared/install.c b/src/shared/install.c
index 9722ed4..b9c85b7 100644
--- a/src/shared/install.c
+++ b/src/shared/install.c
@@ -44,10 +44,8 @@ typedef struct {
         Hashmap *have_installed;
 } InstallContext;
 
-#define _cleanup_lookup_paths_free_ \
-        __attribute__((cleanup(lookup_paths_free)))
-#define _cleanup_install_context_done_ \
-        __attribute__((cleanup(install_context_done)))
+#define _cleanup_lookup_paths_free_ _cleanup_(lookup_paths_free)
+#define _cleanup_install_context_done_ _cleanup_(install_context_done)
 
 static int lookup_paths_init_from_scope(LookupPaths *paths, UnitFileScope scope) {
         assert(paths);
diff --git a/src/shared/set.h b/src/shared/set.h
index e5d46e9..a291470 100644
--- a/src/shared/set.h
+++ b/src/shared/set.h
@@ -28,19 +28,13 @@
  * for each set use. */
 
 #include "hashmap.h"
+#include "util.h"
 
 typedef struct Set Set;
 
 Set *set_new(hash_func_t hash_func, compare_func_t compare_func);
 void set_free(Set* s);
-static inline void set_freep(Set **s) {
-        set_free(*s);
-}
-
 void set_free_free(Set *s);
-static inline void set_free_freep(Set **s) {
-        set_free_free(*s);
-}
 
 Set* set_copy(Set *s);
 int set_ensure_allocated(Set **s, hash_func_t hash_func, compare_func_t compare_func);
@@ -79,5 +73,7 @@ char **set_get_strv(Set *s);
 #define SET_FOREACH_BACKWARDS(e, s, i) \
         for ((i) = ITERATOR_LAST, (e) = set_iterate_backwards((s), &(i)); (e); (e) = set_iterate_backwards((s), &(i)))
 
+define_trivial_cleanup_func(Set*, set_free)
+define_trivial_cleanup_func(Set*, set_free_free)
 #define _cleanup_set_free_ _cleanup_(set_freep)
 #define _cleanup_set_free_free_ _cleanup_(set_free_freep)
diff --git a/src/shared/strv.h b/src/shared/strv.h
index d1f2a0e..4d117f8 100644
--- a/src/shared/strv.h
+++ b/src/shared/strv.h
@@ -24,16 +24,13 @@
 #include <stdarg.h>
 #include <stdbool.h>
 
-#include "macro.h"
+#include "util.h"
 
 char *strv_find(char **l, const char *name) _pure_;
 char *strv_find_prefix(char **l, const char *name) _pure_;
 
 void strv_free(char **l);
-static inline void strv_freep(char ***l) {
-        strv_free(*l);
-}
-
+define_trivial_cleanup_func(char**, strv_free)
 #define _cleanup_strv_free_ _cleanup_(strv_freep)
 
 char **strv_copy(char * const *l);
diff --git a/src/shared/udev-util.h b/src/shared/udev-util.h
new file mode 100644
index 0000000..bff8f5f
--- /dev/null
+++ b/src/shared/udev-util.h
@@ -0,0 +1,37 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+#pragma once
+
+/***
+  This file is part of systemd.
+
+  Copyright 2013 Zbigniew Jędrzejewski-Szmek
+
+  systemd is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as published by
+  the Free Software Foundation; either version 2.1 of the License, or
+  (at your option) any later version.
+
+  systemd is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include "udev.h"
+#include "util.h"
+
+define_trivial_cleanup_func(struct udev*, udev_unref)
+define_trivial_cleanup_func(struct udev_device*, udev_device_unref)
+define_trivial_cleanup_func(struct udev_enumerate*, udev_enumerate_unref)
+define_trivial_cleanup_func(struct udev_event*, udev_event_unref)
+define_trivial_cleanup_func(struct udev_rules*, udev_rules_unref)
+
+#define _cleanup_udev_unref_ _cleanup_(udev_unrefp)
+#define _cleanup_udev_device_unref_ _cleanup_(udev_device_unrefp)
+#define _cleanup_udev_enumerate_unref_ _cleanup_(udev_enumerate_unrefp)
+#define _cleanup_udev_event_unref_ _cleanup_(udev_event_unrefp)
+#define _cleanup_udev_rules_unref_ _cleanup_(udev_rules_unrefp)
diff --git a/src/shared/util.h b/src/shared/util.h
index 222abe0..2c41765 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -39,6 +39,7 @@
 #include <stddef.h>
 #include <unistd.h>
 #include <locale.h>
+#include <mntent.h>
 
 #include "macro.h"
 #include "time-util.h"
@@ -554,37 +555,34 @@ static inline void freep(void *p) {
         free(*(void**) p);
 }
 
-static inline void fclosep(FILE **f) {
-        if (*f)
-                fclose(*f);
-}
-
-static inline void pclosep(FILE **f) {
-        if (*f)
-                pclose(*f);
-}
+#define define_trivial_cleanup_func(type, func) \
+        static inline void func##p(type *p) {   \
+        if (*p)                                 \
+                func(*p);                       \
+        }                                       \
 
 static inline void closep(int *fd) {
         if (*fd >= 0)
                 close_nointr_nofail(*fd);
 }
 
-static inline void closedirp(DIR **d) {
-        if (*d)
-                closedir(*d);
-}
-
 static inline void umaskp(mode_t *u) {
         umask(*u);
 }
 
+define_trivial_cleanup_func(FILE*, fclose)
+define_trivial_cleanup_func(FILE*, pclose)
+define_trivial_cleanup_func(DIR*, closedir)
+define_trivial_cleanup_func(FILE*, endmntent)
+
 #define _cleanup_free_ _cleanup_(freep)
-#define _cleanup_fclose_ _cleanup_(fclosep)
-#define _cleanup_pclose_ _cleanup_(pclosep)
 #define _cleanup_close_ _cleanup_(closep)
-#define _cleanup_closedir_ _cleanup_(closedirp)
 #define _cleanup_umask_ _cleanup_(umaskp)
 #define _cleanup_globfree_ _cleanup_(globfree)
+#define _cleanup_fclose_ _cleanup_(fclosep)
+#define _cleanup_pclose_ _cleanup_(pclosep)
+#define _cleanup_closedir_ _cleanup_(closedirp)
+#define _cleanup_endmntent_ _cleanup_(endmntentp)
 
 _malloc_  _alloc_(1, 2) static inline void *malloc_multiply(size_t a, size_t b) {
         if (_unlikely_(b == 0 || a > ((size_t) -1) / b))
diff --git a/src/test/test-libudev.c b/src/test/test-libudev.c
index 716767b..ab7d5a9 100644
--- a/src/test/test-libudev.c
+++ b/src/test/test-libudev.c
@@ -29,6 +29,7 @@
 #include <sys/epoll.h>
 
 #include "libudev.h"
+#include "udev-util.h"
 #include "util.h"
 
 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
@@ -117,7 +118,7 @@ static void print_device(struct udev_device *device)
 
 static int test_device(struct udev *udev, const char *syspath)
 {
-        struct udev_device *device;
+        _cleanup_udev_device_unref_ struct udev_device *device;
 
         printf("looking at device: %s\n", syspath);
         device = udev_device_new_from_syspath(udev, syspath);
@@ -126,13 +127,13 @@ static int test_device(struct udev *udev, const char *syspath)
                 return -1;
         }
         print_device(device);
-        udev_device_unref(device);
+
         return 0;
 }
 
 static int test_device_parents(struct udev *udev, const char *syspath)
 {
-        struct udev_device *device;
+        _cleanup_udev_device_unref_ struct udev_device *device;
         struct udev_device *device_parent;
 
         printf("looking at device: %s\n", syspath);
@@ -153,7 +154,6 @@ static int test_device_parents(struct udev *udev, const char *syspath)
                 print_device(device_parent);
                 device_parent = udev_device_get_parent(device_parent);
         } while (device_parent != NULL);
-        udev_device_unref(device);
 
         return 0;
 }
diff --git a/src/test/test-udev.c b/src/test/test-udev.c
index 52b61b4..17825f1 100644
--- a/src/test/test-udev.c
+++ b/src/test/test-udev.c
@@ -34,6 +34,7 @@
 
 #include "missing.h"
 #include "udev.h"
+#include "udev-util.h"
 
 void udev_main_log(struct udev *udev, int priority,
                    const char *file, int line, const char *fn,
@@ -82,10 +83,10 @@ out:
 
 int main(int argc, char *argv[])
 {
-        struct udev *udev;
-        struct udev_event *event = NULL;
-        struct udev_device *dev = NULL;
-        struct udev_rules *rules = NULL;
+        _cleanup_udev_unref_ struct udev *udev = NULL;
+        _cleanup_udev_event_unref_ struct udev_event *event = NULL;
+        _cleanup_udev_device_unref_ struct udev_device *dev = NULL;
+        _cleanup_udev_rules_unref_ struct udev_rules *rules = NULL;
         char syspath[UTIL_PATH_SIZE];
         const char *devpath;
         const char *action;
@@ -98,7 +99,8 @@ int main(int argc, char *argv[])
 
         udev = udev_new();
         if (udev == NULL)
-                exit(EXIT_FAILURE);
+                return EXIT_FAILURE;
+
         log_debug("version %s\n", VERSION);
         label_init("/dev");
 
@@ -160,12 +162,7 @@ int main(int argc, char *argv[])
 out:
         if (event != NULL && event->fd_signal >= 0)
                 close(event->fd_signal);
-        udev_event_unref(event);
-        udev_device_unref(dev);
-        udev_rules_unref(rules);
         label_finish();
-        udev_unref(udev);
-        if (err != 0)
-                return EXIT_FAILURE;
-        return EXIT_SUCCESS;
+
+        return err ? EXIT_FAILURE : EXIT_SUCCESS;
 }
diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c
index 239e56b..7e873af 100644
--- a/src/tmpfiles/tmpfiles.c
+++ b/src/tmpfiles/tmpfiles.c
@@ -998,10 +998,7 @@ static void item_free(Item *i) {
         free(i);
 }
 
-static inline void item_freep(Item **i) {
-        if (*i)
-                item_free(*i);
-}
+define_trivial_cleanup_func(Item*, item_free)
 #define _cleanup_item_free_ _cleanup_(item_freep)
 
 static bool item_equal(Item *a, Item *b) {