f



Close all file descriptors

How do i close all the open file descriptors, without knowing how many
descriptors are open.
Is it not bad to close a file descriptor which is not open.

0
ravi
7/25/2004 6:57:33 AM
comp.unix.programmer 10827 articles. 0 followers. kokososo56 (349) is leader. Post Follow

5 Replies
757 Views

Similar Articles

[PageSpeed] 57

ravi wrote:
> How do i close all the open file descriptors, without knowing how many
> descriptors are open.
> Is it not bad to close a file descriptor which is not open.

man getdtablesize

Use this information to go over al descriptors between 0 (or 3)
and the value returned by getdtablesize() call.

-- 
Lev Walkin
vlm@lionet.info
0
Lev
7/25/2004 7:08:29 AM
On Sun, 25 Jul 2004 00:08:29 -0700, Lev Walkin wrote:

> ravi wrote:
>> How do i close all the open file descriptors, without knowing how many
>> descriptors are open.
>> Is it not bad to close a file descriptor which is not open.
> 
> man getdtablesize
> 
> Use this information to go over al descriptors between 0 (or 3)
> and the value returned by getdtablesize() call.

Baad programming habits.

Much better to keep track of what you've opened, or use clone().
0
Viktor
7/25/2004 12:54:13 PM
Viktor Lofgren wrote:
> On Sun, 25 Jul 2004 00:08:29 -0700, Lev Walkin wrote:
> 
> 
>>ravi wrote:
>>
>>>How do i close all the open file descriptors, without knowing how many
>>>descriptors are open.
>>>Is it not bad to close a file descriptor which is not open.
>>
>>man getdtablesize
>>
>>Use this information to go over al descriptors between 0 (or 3)
>>and the value returned by getdtablesize() call.
> 
> 
> Baad programming habits.
> 
> Much better to keep track of what you've opened, or use clone().

If we're talking about programming practices, it might be even better
to use fcntl(, F_SETFD, &FD_CLOEXEC), in cases where exec() is planned
down the road.

However, neither "tracking what you've opened", nor "habitually setting
close-on-exec flag" are universally applicable in the real world programming.
The things is, many libraries (including libc) open file descriptors
"behind the scenes". If a program does not depend on any third-party
libraries and does not use certain libc functions (such as
openlog(3)/syslog(3), getpwnam(3), etc), then yes, keeping track is key.
Otherwise, just closing every possible file descriptor is just plain
better than closing certain ones which are accounted for, and hoping that
accounting was right and no other library opened shadow fds behind
the scenes.

-- 
Lev Walkin
vlm@lionet.info
0
Lev
7/25/2004 1:16:14 PM
On Sun, 25 Jul 2004, Viktor Lofgren wrote:

> Much better to keep track of what you've opened, or use clone().

Clone is not portable.

-- 
Rich Teer, SCNA, SCSA

President,
Rite Online Inc.

Voice: +1 (250) 979-1638
URL: http://www.rite-online.net
0
Rich
7/25/2004 7:45:22 PM
On Sat, 24 Jul 2004, ravi wrote:

> How do i close all the open file descriptors, without knowing how many
> descriptors are open.

If you're on a recent version of Solaris, closefrom() will
do what you want.

> Is it not bad to close a file descriptor which is not open.

Nope.  The worst that will happen is that you'll waste a few
ms of CPU time trying to close files that are not open.

-- 
Rich Teer, SCNA, SCSA

President,
Rite Online Inc.

Voice: +1 (250) 979-1638
URL: http://www.rite-online.net
0
Rich
7/25/2004 7:47:20 PM
Reply: