mirror of
https://gitlab.freedesktop.org/libbsd/libbsd.git
synced 2025-10-22 08:02:10 +02:00
Refactor file descriptor closure into a new closefrom_close()
This commit is contained in:
@@ -60,6 +60,17 @@
|
|||||||
# define closefrom closefrom_fallback
|
# define closefrom closefrom_fallback
|
||||||
#endif
|
#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.
|
* Close all file descriptors greater than or equal to lowfd.
|
||||||
* This is the expensive (fallback) method.
|
* This is the expensive (fallback) method.
|
||||||
@@ -82,14 +93,8 @@ closefrom_fallback(int lowfd)
|
|||||||
if (maxfd < 0)
|
if (maxfd < 0)
|
||||||
maxfd = OPEN_MAX;
|
maxfd = OPEN_MAX;
|
||||||
|
|
||||||
for (fd = lowfd; fd < maxfd; fd++) {
|
for (fd = lowfd; fd < maxfd; fd++)
|
||||||
#ifdef __APPLE__
|
closefrom_close(fd);
|
||||||
/* Avoid potential libdispatch crash when we close its fds. */
|
|
||||||
(void)fcntl((int)fd, F_SETFD, FD_CLOEXEC);
|
|
||||||
#else
|
|
||||||
(void)close((int)fd);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -138,15 +143,8 @@ closefrom(int lowfd)
|
|||||||
int fd;
|
int fd;
|
||||||
|
|
||||||
fd = strtonum(dent->d_name, lowfd, INT_MAX, &errstr);
|
fd = strtonum(dent->d_name, lowfd, INT_MAX, &errstr);
|
||||||
if (errstr == NULL && fd != dirfd(dirp)) {
|
if (errstr == NULL && fd != dirfd(dirp))
|
||||||
# ifdef __APPLE__
|
closefrom_close(fd);
|
||||||
/* Avoid potential libdispatch crash when we
|
|
||||||
* close its fds. */
|
|
||||||
(void)fcntl(fd, F_SETFD, FD_CLOEXEC);
|
|
||||||
# else
|
|
||||||
(void)close(fd);
|
|
||||||
# endif
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
(void)closedir(dirp);
|
(void)closedir(dirp);
|
||||||
} else
|
} else
|
||||||
|
Reference in New Issue
Block a user