diff options
author | neilbrown <neilbrown> | 2005-10-06 05:20:19 +0000 |
---|---|---|
committer | neilbrown <neilbrown> | 2005-10-06 05:20:19 +0000 |
commit | f73e7b9f69835d483cee95e6a20b6307b9d16b77 (patch) | |
tree | 3c6f5f0c0677f0bc30018b9619ea32ffc9efebb9 /support/nfs/closeall.c | |
parent | b51ebed58a610b3104e9f218d609715cd468b0ea (diff) | |
download | nfs-utils-f73e7b9f69835d483cee95e6a20b6307b9d16b77.tar.gz nfs-utils-f73e7b9f69835d483cee95e6a20b6307b9d16b77.tar.xz nfs-utils-f73e7b9f69835d483cee95e6a20b6307b9d16b77.zip |
Assorted changes from Steve Dickson
Diffstat (limited to 'support/nfs/closeall.c')
-rw-r--r-- | support/nfs/closeall.c | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/support/nfs/closeall.c b/support/nfs/closeall.c new file mode 100644 index 0000000..cc7fb3b --- /dev/null +++ b/support/nfs/closeall.c @@ -0,0 +1,31 @@ +/* + * support/nfs/closeall.c + * Close all file descriptors greater than some limit, + * Use readdir "/proc/self/fd" to avoid excess close(2) calls. + */ + +#include <unistd.h> +#include <stdlib.h> +#include <dirent.h> + +void +closeall(int min) +{ + DIR *dir = opendir("/proc/self/fd"); + if (dir != NULL) { + int dfd = dirfd(dir); + struct dirent *d; + + while ((d = readdir(dir)) != NULL) { + char *endp; + long n = strtol(d->d_name, &endp, 10); + if (*endp != '\0' && n >= min && n != dfd) + (void) close(n); + } + closedir(dir); + } else { + int fd = sysconf(_SC_OPEN_MAX); + while (--fd >= min) + (void) close(fd); + } +} |