84b277
From 8e7be40d1386e6053d4663114a00e0390400350f Mon Sep 17 00:00:00 2001
84b277
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
84b277
Date: Thu, 3 Oct 2013 22:13:55 -0400
84b277
Subject: [PATCH] Introduce _cleanup_endmntent_
84b277
84b277
(cherry picked from commit 5862d652ba14178cff46b8a8fc6c6d8392bf32b1)
84b277
84b277
Related: #1098310
84b277
---
84b277
 src/cryptsetup/cryptsetup.c           | 19 ++++++-------------
84b277
 src/fstab-generator/fstab-generator.c | 17 +++++------------
84b277
 src/remount-fs/remount-fs.c           | 13 ++++---------
84b277
 src/shared/util.h                     |  7 +++++++
84b277
 4 files changed, 22 insertions(+), 34 deletions(-)
84b277
84b277
diff --git a/src/cryptsetup/cryptsetup.c b/src/cryptsetup/cryptsetup.c
84b277
index 769c3e4..4f2f52a 100644
84b277
--- a/src/cryptsetup/cryptsetup.c
84b277
+++ b/src/cryptsetup/cryptsetup.c
84b277
@@ -236,31 +236,24 @@ finish:
84b277
 }
84b277
 
84b277
 static char *disk_mount_point(const char *label) {
84b277
-        char *mp = NULL;
84b277
         _cleanup_free_ char *device = NULL;
84b277
-        FILE *f = NULL;
84b277
+        _cleanup_endmntent_ FILE *f = NULL;
84b277
         struct mntent *m;
84b277
 
84b277
         /* Yeah, we don't support native systemd unit files here for now */
84b277
 
84b277
         if (asprintf(&device, "/dev/mapper/%s", label) < 0)
84b277
-                goto finish;
84b277
+                return NULL;
84b277
 
84b277
         f = setmntent("/etc/fstab", "r");
84b277
         if (!f)
84b277
-                goto finish;
84b277
+                return NULL;
84b277
 
84b277
         while ((m = getmntent(f)))
84b277
-                if (path_equal(m->mnt_fsname, device)) {
84b277
-                        mp = strdup(m->mnt_dir);
84b277
-                        break;
84b277
-                }
84b277
-
84b277
-finish:
84b277
-        if (f)
84b277
-                endmntent(f);
84b277
+                if (path_equal(m->mnt_fsname, device))
84b277
+                        return strdup(m->mnt_dir);
84b277
 
84b277
-        return mp;
84b277
+        return NULL;
84b277
 }
84b277
 
84b277
 static int get_password(const char *name, usec_t until, bool accept_cached, char ***passwords) {
84b277
diff --git a/src/fstab-generator/fstab-generator.c b/src/fstab-generator/fstab-generator.c
84b277
index c0c2992..78d7609 100644
84b277
--- a/src/fstab-generator/fstab-generator.c
84b277
+++ b/src/fstab-generator/fstab-generator.c
84b277
@@ -327,15 +327,12 @@ static int add_mount(
84b277
 }
84b277
 
84b277
 static int parse_fstab(const char *prefix, bool initrd) {
84b277
-        _cleanup_free_ char *fstab_path = NULL;
84b277
-        FILE *f;
84b277
+        char *fstab_path;
84b277
+        _cleanup_endmntent_ FILE *f;
84b277
         int r = 0;
84b277
         struct mntent *me;
84b277
 
84b277
-        fstab_path = strjoin(strempty(prefix), "/etc/fstab", NULL);
84b277
-        if (!fstab_path)
84b277
-                return log_oom();
84b277
-
84b277
+        fstab_path = strappenda(strempty(prefix), "/etc/fstab");
84b277
         f = setmntent(fstab_path, "r");
84b277
         if (!f) {
84b277
                 if (errno == ENOENT)
84b277
@@ -354,10 +351,8 @@ static int parse_fstab(const char *prefix, bool initrd) {
84b277
 
84b277
                 what = fstab_node_to_udev_node(me->mnt_fsname);
84b277
                 where = strjoin(strempty(prefix), me->mnt_dir, NULL);
84b277
-                if (!what || !where) {
84b277
-                        r = log_oom();
84b277
-                        goto finish;
84b277
-                }
84b277
+                if (!what || !where)
84b277
+                        return log_oom();
84b277
 
84b277
                 if (is_path(where))
84b277
                         path_kill_slashes(where);
84b277
@@ -395,8 +390,6 @@ static int parse_fstab(const char *prefix, bool initrd) {
84b277
                         r = k;
84b277
         }
84b277
 
84b277
-finish:
84b277
-        endmntent(f);
84b277
         return r;
84b277
 }
84b277
 
84b277
diff --git a/src/remount-fs/remount-fs.c b/src/remount-fs/remount-fs.c
84b277
index f432718..847637a 100644
84b277
--- a/src/remount-fs/remount-fs.c
84b277
+++ b/src/remount-fs/remount-fs.c
84b277
@@ -40,7 +40,7 @@
84b277
 
84b277
 int main(int argc, char *argv[]) {
84b277
         int ret = EXIT_FAILURE;
84b277
-        FILE *f = NULL;
84b277
+        _cleanup_endmntent_ FILE *f = NULL;
84b277
         struct mntent* me;
84b277
         Hashmap *pids = NULL;
84b277
 
84b277
@@ -57,13 +57,11 @@ int main(int argc, char *argv[]) {
84b277
 
84b277
         f = setmntent("/etc/fstab", "r");
84b277
         if (!f) {
84b277
-                if (errno == ENOENT) {
84b277
-                        ret = EXIT_SUCCESS;
84b277
-                        goto finish;
84b277
-                }
84b277
+                if (errno == ENOENT)
84b277
+                        return EXIT_SUCCESS;
84b277
 
84b277
                 log_error("Failed to open /etc/fstab: %m");
84b277
-                goto finish;
84b277
+                return EXIT_FAILURE;
84b277
         }
84b277
 
84b277
         pids = hashmap_new(trivial_hash_func, trivial_compare_func);
84b277
@@ -162,8 +160,5 @@ finish:
84b277
         if (pids)
84b277
                 hashmap_free_free(pids);
84b277
 
84b277
-        if (f)
84b277
-                endmntent(f);
84b277
-
84b277
         return ret;
84b277
 }
84b277
diff --git a/src/shared/util.h b/src/shared/util.h
84b277
index 3a4bc98..5a1e1bc 100644
84b277
--- a/src/shared/util.h
84b277
+++ b/src/shared/util.h
84b277
@@ -39,6 +39,7 @@
84b277
 #include <stddef.h>
84b277
 #include <unistd.h>
84b277
 #include <locale.h>
84b277
+#include <mntent.h>
84b277
 
84b277
 #include "macro.h"
84b277
 #include "time-util.h"
84b277
@@ -579,6 +580,11 @@ static inline void umaskp(mode_t *u) {
84b277
         umask(*u);
84b277
 }
84b277
 
84b277
+static inline void endmntentp(FILE **f) {
84b277
+        if (*f)
84b277
+                endmntent(*f);
84b277
+}
84b277
+
84b277
 #define _cleanup_free_ _cleanup_(freep)
84b277
 #define _cleanup_fclose_ _cleanup_(fclosep)
84b277
 #define _cleanup_pclose_ _cleanup_(pclosep)
84b277
@@ -586,6 +592,7 @@ static inline void umaskp(mode_t *u) {
84b277
 #define _cleanup_closedir_ _cleanup_(closedirp)
84b277
 #define _cleanup_umask_ _cleanup_(umaskp)
84b277
 #define _cleanup_globfree_ _cleanup_(globfree)
84b277
+#define _cleanup_endmntent_ _cleanup_(endmntentp)
84b277
 
84b277
 _malloc_  _alloc_(1, 2) static inline void *malloc_multiply(size_t a, size_t b) {
84b277
         if (_unlikely_(b == 0 || a > ((size_t) -1) / b))