teknoraver / rpms / systemd

Forked from rpms/systemd 2 months ago
Clone

Blame SOURCES/0242-Introduce-_cleanup_endmntent_.patch

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