Unix Programming - Close all file descriptors

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > July 2004 > Close all file descriptors





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Close all file descriptors
ravi

2004-07-28, 6:19 pm

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.

Lev Walkin

2004-07-28, 6:19 pm

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
SM Ryan

2004-07-28, 6:19 pm

Lev Walkin <vlm@lionet.info> 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.

Because if you give close() an integer which is not a file descriptor,
close will set errno but not do any damage or abort the program or
anything else untoward.


--
SM Ryan http://www.rawbw.com/~wyrmwif/
Don't say anything. Especially you.
Viktor Lofgren

2004-07-28, 6:19 pm

On Sun, 25 Jul 2004 00:08:29 -0700, Lev Walkin wrote:

> ravi wrote:
>
> 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().
Lev Walkin

2004-07-28, 6:19 pm

Viktor Lofgren wrote:
> On Sun, 25 Jul 2004 00:08:29 -0700, Lev Walkin wrote:
>
>
>
>
> 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
Rich Teer

2004-07-28, 6:19 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
Rich Teer

2004-07-28, 6:19 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
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com