]> git.ipfire.org Git - thirdparty/libbsd.git/commitdiff
Refactor file descriptor closure into a new closefrom_close()
authorGuillem Jover <guillem@hadrons.org>
Wed, 5 Nov 2014 19:01:53 +0000 (20:01 +0100)
committerGuillem Jover <guillem@hadrons.org>
Wed, 23 Sep 2015 05:59:34 +0000 (07:59 +0200)
src/closefrom.c

index 423a004916837cbfcccb8d390f082a7887810a70..03e84d88aa6afc58b1e08014bfc15ee29414f538 100644 (file)
 # define closefrom     closefrom_fallback
 #endif
 
+static inline void
+closefrom_close(int fd)
+{
+#ifdef __APPLE__
+       /* Avoid potential libdispatch crash when we close its fds. */
+       (void)fcntl(fd, F_SETFD, FD_CLOEXEC);
+#else
+       (void)close(fd);
+#endif
+}
+
 /*
  * Close all file descriptors greater than or equal to lowfd.
  * This is the expensive (fallback) method.
@@ -82,14 +93,8 @@ closefrom_fallback(int lowfd)
        if (maxfd < 0)
                maxfd = OPEN_MAX;
 
-       for (fd = lowfd; fd < maxfd; fd++) {
-#ifdef __APPLE__
-               /* Avoid potential libdispatch crash when we close its fds. */
-               (void)fcntl((int)fd, F_SETFD, FD_CLOEXEC);
-#else
-               (void)close((int)fd);
-#endif
-       }
+       for (fd = lowfd; fd < maxfd; fd++)
+               closefrom_close(fd);
 }
 
 /*
@@ -138,15 +143,8 @@ closefrom(int lowfd)
                        int fd;
 
                        fd = strtonum(dent->d_name, lowfd, INT_MAX, &errstr);
-                       if (errstr == NULL && fd != dirfd(dirp)) {
-# ifdef __APPLE__
-                               /* Avoid potential libdispatch crash when we
-                                * close its fds. */
-                               (void)fcntl(fd, F_SETFD, FD_CLOEXEC);
-# else
-                               (void)close(fd);
-# endif
-                       }
+                       if (errstr == NULL && fd != dirfd(dirp))
+                               closefrom_close(fd);
                }
                (void)closedir(dirp);
        } else