multipath: remove unnecessary fds from alias code
authorBenjamin Marzinski <bmarzins@redhat.com>
Fri, 8 Jun 2012 17:28:28 +0000 (12:28 -0500)
committerChristophe Varoqui <christophe.varoqui@opensvc.com>
Sat, 9 Jun 2012 07:46:49 +0000 (09:46 +0200)
Originally, the alias code duped the bindings file fd so that a stream could
be opened on it, and then closed without closing the original fd. Later,
closing the stream was moved to the end of the function, to avoid a locking
bug.  Because of this, there isn't any point to duping the fd.  Also, since
the stream is still opened when the original fd is used, the stream
should be flushed after its done being used.

Signed-off-by: Benjamin Marzinski <bmarzins@redhat.com>
libmultipath/alias.c

index ec3a2257cd598d885b44e2903b8582d9d4465d1d..a6a06ad185be172c39bd3ab49d8625254d970b3c 100644 (file)
@@ -361,7 +361,7 @@ get_user_friendly_alias(char *wwid, char *file, char *prefix,
                        int bindings_read_only)
 {
        char *alias;
-       int fd, scan_fd, id;
+       int fd, id;
        FILE *f;
        int can_write;
 
@@ -374,19 +374,10 @@ get_user_friendly_alias(char *wwid, char *file, char *prefix,
        if (fd < 0)
                return NULL;
 
-       scan_fd = dup(fd);
-       if (scan_fd < 0) {
-               condlog(0, "Cannot dup bindings file descriptor : %s",
-                       strerror(errno));
-               close(fd);
-               return NULL;
-       }
-
-       f = fdopen(scan_fd, "r");
+       f = fdopen(fd, "r");
        if (!f) {
                condlog(0, "cannot fdopen on bindings file descriptor : %s",
                        strerror(errno));
-               close(scan_fd);
                close(fd);
                return NULL;
        }
@@ -394,8 +385,13 @@ get_user_friendly_alias(char *wwid, char *file, char *prefix,
        id = lookup_binding(f, wwid, &alias, prefix);
        if (id < 0) {
                fclose(f);
-               close(scan_fd);
-               close(fd);
+               return NULL;
+       }
+
+       if (fflush(f) != 0) {
+               condlog(0, "cannot fflush bindings file stream : %s",
+                       strerror(errno));
+               fclose(f);
                return NULL;
        }
 
@@ -403,8 +399,6 @@ get_user_friendly_alias(char *wwid, char *file, char *prefix,
                alias = allocate_binding(fd, wwid, id, prefix);
 
        fclose(f);
-       close(scan_fd);
-       close(fd);
        return alias;
 }
 
@@ -412,7 +406,7 @@ char *
 get_user_friendly_wwid(char *alias, char *file)
 {
        char *wwid;
-       int fd, scan_fd, id, unused;
+       int fd, id, unused;
        FILE *f;
 
        if (!alias || *alias == '\0') {
@@ -424,19 +418,10 @@ get_user_friendly_wwid(char *alias, char *file)
        if (fd < 0)
                return NULL;
 
-       scan_fd = dup(fd);
-       if (scan_fd < 0) {
-               condlog(0, "Cannot dup bindings file descriptor : %s",
-                       strerror(errno));
-               close(fd);
-               return NULL;
-       }
-
-       f = fdopen(scan_fd, "r");
+       f = fdopen(fd, "r");
        if (!f) {
                condlog(0, "cannot fdopen on bindings file descriptor : %s",
                        strerror(errno));
-               close(scan_fd);
                close(fd);
                return NULL;
        }
@@ -444,13 +429,9 @@ get_user_friendly_wwid(char *alias, char *file)
        id = rlookup_binding(f, &wwid, alias);
        if (id < 0) {
                fclose(f);
-               close(scan_fd);
-               close(fd);
                return NULL;
        }
 
        fclose(f);
-       close(scan_fd);
-       close(fd);
        return wwid;
 }