Move procfs based implementation into a new closefrom_procfs() function

This commit is contained in:
Guillem Jover 2014-11-02 23:58:23 +01:00
parent 34df142665
commit 4cc43915f2

View File

@ -123,11 +123,12 @@ closefrom(int lowfd)
} }
} }
#elif defined(HAVE_DIRFD) #elif defined(HAVE_DIRFD)
void static int
closefrom(int lowfd) closefrom_procfs(int lowfd)
{ {
const char *path; const char *path;
DIR *dirp; DIR *dirp;
struct dirent *dent;
/* Use /proc/self/fd (or /dev/fd on FreeBSD) if it exists. */ /* Use /proc/self/fd (or /dev/fd on FreeBSD) if it exists. */
# if defined(__FreeBSD__) || defined(__APPLE__) # if defined(__FreeBSD__) || defined(__APPLE__)
@ -135,19 +136,30 @@ closefrom(int lowfd)
# else # else
path = "/proc/self/fd"; path = "/proc/self/fd";
# endif # endif
if ((dirp = opendir(path)) != NULL) { dirp = opendir(path);
struct dirent *dent; if (dirp == NULL)
return -1;
while ((dent = readdir(dirp)) != NULL) { while ((dent = readdir(dirp)) != NULL) {
const char *errstr; const char *errstr;
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))
closefrom_close(fd); closefrom_close(fd);
} }
(void)closedir(dirp);
} else (void)closedir(dirp);
closefrom_fallback(lowfd);
return 0;
}
void
closefrom(int lowfd)
{
if (closefrom_procfs(lowfd) == 0)
return;
closefrom_fallback(lowfd);
} }
#endif /* HAVE_FCNTL_CLOSEM */ #endif /* HAVE_FCNTL_CLOSEM */