Close all file descriptors
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Close all file descriptors




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Close all file descriptors  
ravi


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
07-28-04 11: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.






[ Post a follow-up to this message ]



    Re: Close all file descriptors  
Lev Walkin


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
07-28-04 11: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





[ Post a follow-up to this message ]



    Re: Close all file descriptors  
SM Ryan


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
07-28-04 11: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.





[ Post a follow-up to this message ]



    Re: Close all file descriptors  
Viktor Lofgren


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
07-28-04 11: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().





[ Post a follow-up to this message ]



    Re: Close all file descriptors  
Lev Walkin


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
07-28-04 11: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





[ Post a follow-up to this message ]



    Re: Close all file descriptors  
Rich Teer


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
07-28-04 11: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





[ Post a follow-up to this message ]



    Re: Close all file descriptors  
Rich Teer


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
07-28-04 11: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





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 11:19 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register