]> git.decadent.org.uk Git - nfs-utils.git/blobdiff - support/nfs/closeall.c
The closeall function is broken in such a way that it almost never
[nfs-utils.git] / support / nfs / closeall.c
index cc7fb3bd86164cf775eb71b1951bf3b064f1ef51..38fb162c3a3ccb89d7336cc264df18f094311bbb 100644 (file)
@@ -7,19 +7,24 @@
 #include <unistd.h>
 #include <stdlib.h>
 #include <dirent.h>
 #include <unistd.h>
 #include <stdlib.h>
 #include <dirent.h>
+#include <errno.h>
 
 void
 closeall(int min)
 {
 
 void
 closeall(int min)
 {
+       char *endp;
+       long n;
        DIR *dir = opendir("/proc/self/fd");
        DIR *dir = opendir("/proc/self/fd");
+
        if (dir != NULL) {
                int dfd = dirfd(dir);
                struct dirent *d;
 
                while ((d = readdir(dir)) != NULL) {
        if (dir != NULL) {
                int dfd = dirfd(dir);
                struct dirent *d;
 
                while ((d = readdir(dir)) != NULL) {
-                       char *endp;
-                       long n = strtol(d->d_name, &endp, 10);
-                       if (*endp != '\0' && n >= min && n != dfd)
+                       errno = 0;
+                       n = strtol(d->d_name, &endp, 10);
+                       if (!errno && *endp == '\0' && endp != d->d_name &&
+                           n >= min && n != dfd)
                                (void) close(n);
                }
                closedir(dir);
                                (void) close(n);
                }
                closedir(dir);