richardphibel / rpms / systemd

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