Refactor file descriptor closure into a new closefrom_close()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
diff --git a/src/closefrom.c b/src/closefrom.c
index 423a004..03e84d8 100644
--- a/src/closefrom.c
+++ b/src/closefrom.c
@@ -60,6 +60,17 @@
# 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