how to find all open file descriptors

how to find all open file descriptors

Post by Antoine Mathy » Thu, 19 Jun 2003 05:24:23



Hello,

How can I get the list of all the file descriptors a process has
currently open? I need to write a function that can loop over every open
file descriptors (except 0, 1, 2) and close them.

Irix 6.5 has a getdtablehi() function that returns the index of the
largest open file descriptor. I couldn't seem to find a POSIX equivalent.

Thanks for your help.

A. Mathys

 
 
 

how to find all open file descriptors

Post by Dragan Cvetkovi » Thu, 19 Jun 2003 05:48:57



> Hello,

> How can I get the list of all the file descriptors a process has currently
> open? I need to write a function that can loop over every open file
> descriptors (except 0, 1, 2) and close them.

> Irix 6.5 has a getdtablehi() function that returns the index of the largest
> open file descriptor. I couldn't seem to find a POSIX equivalent.

I don't think that there is a POSIX (portable way). On systems with /proc
file system (Solaris, Linux, new AIX, ...) you can have a look into
/proc/<pid>/fd directory, e.g.

(on Solaris)

$ ls -al /proc/$$/fd
total 4
dr-x------   2 dragan   staff        528 Jun 17 08:30 ./
dr-x--x--x   5 dragan   staff        800 Jun 17 08:30 ../
c---------   1 dragan   tty       24,  6 Jun 17 16:37 15
c---------   1 dragan   tty       24,  6 Jun 17 16:37 16
c---------   1 dragan   tty       24,  6 Jun 17 16:37 17
c---------   1 dragan   tty       24,  6 Jun 17 16:37 18
c---------   1 dragan   tty       24,  6 Jun 17 16:37 19
D---------   1 root     root           0 Jun 17 08:28 3>

which agrees with /bin/pfiles:

pfiles $$
344:    tcsh
  Current rlimit: 256 file descriptors
   3: S_IFDOOR mode:0444 dev:191,0 ino:8203 uid:0 gid:0 size:0
      O_RDONLY|O_LARGEFILE FD_CLOEXEC  door to nscd[210]
  15: S_IFCHR mode:0620 dev:182,0 ino:12582928 uid:1001 gid:7 rdev:24,6
      O_RDWR FD_CLOEXEC
  16: S_IFCHR mode:0620 dev:182,0 ino:12582928 uid:1001 gid:7 rdev:24,6
      O_RDWR FD_CLOEXEC
  17: S_IFCHR mode:0620 dev:182,0 ino:12582928 uid:1001 gid:7 rdev:24,6
      O_RDWR FD_CLOEXEC
  18: S_IFCHR mode:0620 dev:182,0 ino:12582928 uid:1001 gid:7 rdev:24,6
      O_RDWR FD_CLOEXEC
  19: S_IFCHR mode:0620 dev:182,0 ino:12582928 uid:1001 gid:7 rdev:24,6
      O_RDWR FD_CLOEXEC

HTH, Dragan

--
Dragan Cvetkovic,

To be or not to be is true. G. Boole      No it isn't.  L. E. J. Brouwer

 
 
 

how to find all open file descriptors

Post by Pascal Bourguigno » Thu, 19 Jun 2003 06:50:19




> > Hello,

> > How can I get the list of all the file descriptors a process has currently
> > open? I need to write a function that can loop over every open file
> > descriptors (except 0, 1, 2) and close them.

> > Irix 6.5 has a getdtablehi() function that returns the index of the largest
> > open file descriptor. I couldn't seem to find a POSIX equivalent.

> I don't think that there is a POSIX (portable way).

You can get the highest fd in a POSIX portable way (severay ways).

But basically, you can do:

for(fd=3;fd<OPEN_MAX;fd++){ close(fd); }

See: http://www.opengroup.org/onlinepubs/007904975/functions/dup2.html

--
__Pascal_Bourguignon__                   http://www.informatimago.com/
----------------------------------------------------------------------
Do not adjust your mind, there is a fault in reality.

 
 
 

how to find all open file descriptors

Post by Shaun Clowe » Thu, 19 Jun 2003 07:17:15



...

Quote:> How can I get the list of all the file descriptors a process has
> currently open?

There is no cross platform way to do this that I'm aware of, nor a cross
platform way to get the current maximum fd.

Quote:> I need to write a function that can loop over every open
> file descriptors (except 0, 1, 2) and close them.

The cross platform code I have seen to do this closes from 3 to
sysconf(_SC_OPEN_MAX). Obviously that's usually ridiculously wasteful
because most processes will have nowhere near the maximum fds open. If
you're on AIX you may find the F_CLOSEM fcntl to be helpful.

Cheers,
Shaun

 
 
 

how to find all open file descriptors

Post by Antoine Mathy » Thu, 19 Jun 2003 07:17:12


Hello,

Quote:> You can get the highest fd in a POSIX portable way (severay ways).

I'd be curious to know them.

Quote:> But basically, you can do:
> for(fd=3;fd<OPEN_MAX;fd++){ close(fd); }

1. OPEN_MAX is not necessarily defined on systems which do not have a
fixed, uniformed limit for this parameter. In this case,
sysconf(_SC_OPEN_MAX) won't give you a limit either.

2. On my system, OPEN_MAX is defined to 1024. I can't afford to try to
close that many descriptors.

Quote:> See: http://www.opengroup.org/onlinepubs/007904975/functions/dup2.html

dup() won't do. It will use the first unnused descriptor, not the one
following the highest.

A.M.

 
 
 

how to find all open file descriptors

Post by Dragan Cvetkovi » Thu, 19 Jun 2003 07:19:01





> > > Hello,

> > > How can I get the list of all the file descriptors a process has currently
> > > open? I need to write a function that can loop over every open file
> > > descriptors (except 0, 1, 2) and close them.

> > > Irix 6.5 has a getdtablehi() function that returns the index of the largest
> > > open file descriptor. I couldn't seem to find a POSIX equivalent.

> > I don't think that there is a POSIX (portable way).

> You can get the highest fd in a POSIX portable way (severay ways).

> But basically, you can do:

> for(fd=3;fd<OPEN_MAX;fd++){ close(fd); }

> See: http://www.opengroup.org/onlinepubs/007904975/functions/dup2.html

How does it work on AIX 4.x? For example, I see the following here:

ulimit  -a
core file size (blocks)     1024
data seg size (kbytes)      131072
file size (blocks)          1048575
max memory size (kbytes)    32768
open files                  unlimited
pipe size (512 bytes)       64
stack size (kbytes)         32768
cpu time (seconds)          unlimited
max user processes          40
virtual memory (kbytes)     unlimited

Note "open files" field.

Bye, Dragan

--
Dragan Cvetkovic,

To be or not to be is true. G. Boole      No it isn't.  L. E. J. Brouwer

 
 
 

how to find all open file descriptors

Post by Dragan Cvetkovi » Thu, 19 Jun 2003 07:35:49





> ...
> > How can I get the list of all the file descriptors a process has
> > currently open?

[snip]

Quote:> If you're on AIX you may find the F_CLOSEM fcntl to be helpful.

Hey, that's cool, didn't know that!

Bye, Dragan

--
Dragan Cvetkovic,

To be or not to be is true. G. Boole      No it isn't.  L. E. J. Brouwer