How can a process easily get a list of all it's open fd?

Mark Atwood (mra@pobox.com)
27 Aug 2002 08:34:04 -0700


I need to close all the none std[in|out|err] open fd's.

I've been told to do it like so:

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

This is very slow, plus I have discovered that I can have open fd's with
values greater than OPEN_MAX.

I thought about getting the max fd from rlimit, but that doesn't work
either. Say I have a rlimit of 1024 open fd's, and I open numbers 3
thru 1023, then I close 3 thru 1022, then I set the rlimit down to
16. rlimit then returns 16, but the largest open fd is still 1023.

So that doesn't work.

And I still have the problem that looping between 3 and whatever I pick
as the top and calling close on each in turn is very slow.

So what's the "right way" to do it?

I would *love* for there to be an ioctl or some syscall that I could
pass a pointer to an int and a pointer to an int array, and it would
come back telling me how many open fd's I've got, and fill in the
array with those fd's.

-- 
Mark Atwood   | Well done is better than well said.
mra@pobox.com | 
http://www.pobox.com/~mra
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/