summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGuillem Jover <guillem@hadrons.org>2014-11-02 23:58:23 +0100
committerGuillem Jover <guillem@hadrons.org>2015-09-23 07:59:34 +0200
commit4cc43915f22c282ded2a9c3875ad7865fcb91fa5 (patch)
tree0453a04da2287469a86577b3feb2e3425f37c662 /src
parent34df1426653e79d170de399dfadc5b2709f85815 (diff)
Move procfs based implementation into a new closefrom_procfs() function
Diffstat (limited to 'src')
-rw-r--r--src/closefrom.c44
1 files changed, 28 insertions, 16 deletions
diff --git a/src/closefrom.c b/src/closefrom.c
index 03e84d8..27aa26f 100644
--- a/src/closefrom.c
+++ b/src/closefrom.c
@@ -123,11 +123,12 @@ closefrom(int lowfd)
}
}
#elif defined(HAVE_DIRFD)
-void
-closefrom(int lowfd)
+static int
+closefrom_procfs(int lowfd)
{
const char *path;
DIR *dirp;
+ struct dirent *dent;
/* Use /proc/self/fd (or /dev/fd on FreeBSD) if it exists. */
# if defined(__FreeBSD__) || defined(__APPLE__)
@@ -135,19 +136,30 @@ closefrom(int lowfd)
# else
path = "/proc/self/fd";
# endif
- if ((dirp = opendir(path)) != NULL) {
- struct dirent *dent;
-
- while ((dent = readdir(dirp)) != NULL) {
- const char *errstr;
- int fd;
-
- fd = strtonum(dent->d_name, lowfd, INT_MAX, &errstr);
- if (errstr == NULL && fd != dirfd(dirp))
- closefrom_close(fd);
- }
- (void)closedir(dirp);
- } else
- closefrom_fallback(lowfd);
+ dirp = opendir(path);
+ if (dirp == NULL)
+ return -1;
+
+ while ((dent = readdir(dirp)) != NULL) {
+ const char *errstr;
+ int fd;
+
+ fd = strtonum(dent->d_name, lowfd, INT_MAX, &errstr);
+ if (errstr == NULL && fd != dirfd(dirp))
+ closefrom_close(fd);
+ }
+
+ (void)closedir(dirp);
+
+ return 0;
+}
+
+void
+closefrom(int lowfd)
+{
+ if (closefrom_procfs(lowfd) == 0)
+ return;
+
+ closefrom_fallback(lowfd);
}
#endif /* HAVE_FCNTL_CLOSEM */