~ubuntu-branches/ubuntu/lucid/nfs-utils/lucid

« back to all changes in this revision

Viewing changes to support/nfs/closeall.c

  • Committer: Bazaar Package Importer
  • Author(s): Anibal Monsalve Salazar
  • Date: 2006-07-03 10:36:59 UTC
  • mto: (12.1.1 feisty)
  • mto: This revision was merged to the branch mainline in revision 6.
  • Revision ID: james.westby@ubuntu.com-20060703103659-71qzs6f21zzmjmhx
Tags: upstream-1.0.8
ImportĀ upstreamĀ versionĀ 1.0.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * support/nfs/closeall.c
 
3
 * Close all file descriptors greater than some limit,
 
4
 * Use readdir "/proc/self/fd" to avoid excess close(2) calls.
 
5
 */
 
6
 
 
7
#include <unistd.h>
 
8
#include <stdlib.h>
 
9
#include <dirent.h>
 
10
 
 
11
void
 
12
closeall(int min)
 
13
{
 
14
        DIR *dir = opendir("/proc/self/fd");
 
15
        if (dir != NULL) {
 
16
                int dfd = dirfd(dir);
 
17
                struct dirent *d;
 
18
 
 
19
                while ((d = readdir(dir)) != NULL) {
 
20
                        char *endp;
 
21
                        long n = strtol(d->d_name, &endp, 10);
 
22
                        if (*endp != '\0' && n >= min && n != dfd)
 
23
                                (void) close(n);
 
24
                }
 
25
                closedir(dir);
 
26
        } else {
 
27
                int fd = sysconf(_SC_OPEN_MAX);
 
28
                while (--fd >= min)
 
29
                        (void) close(fd);
 
30
        }
 
31
}