utils: Check for dirfd(3)

Not all POSIX compatible systems might provide it yet.  If not, we close
the lowest FD to close and hope it gets reused by opendir().
This commit is contained in:
Tobias Brunner 2015-08-04 19:17:37 +02:00
parent f25f4192c7
commit 6d9cd1d66b
2 changed files with 10 additions and 1 deletions

View File

@ -583,7 +583,7 @@ AC_CHECK_FUNC(
)
AC_CHECK_FUNCS(prctl mallinfo getpass closefrom getpwnam_r getgrnam_r getpwuid_r)
AC_CHECK_FUNCS(fmemopen funopen mmap memrchr setlinebuf strptime)
AC_CHECK_FUNCS(fmemopen funopen mmap memrchr setlinebuf strptime dirfd)
AC_CHECK_FUNC([syslog], [
AC_DEFINE([HAVE_SYSLOG], [], [have syslog(3) and friends])

View File

@ -175,10 +175,19 @@ void closefrom(int low_fd)
DIR *dir;
struct dirent *entry;
#ifndef HAVE_DIRFD
/* if we don't have dirfd() lets close the lowest FD and hope it gets reused
* by opendir() */
close(low_fd);
dir_fd = low_fd++;
#endif
dir = opendir(FD_DIR);
if (dir)
{
#ifdef HAVE_DIRFD
dir_fd = dirfd(dir);
#endif
while ((entry = readdir(dir)))
{
if (!isdigit(entry->d_name[0]))