Close all file descriptors

  • Permalink
  • submit to reddit
  • Email
  • Follow


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
Reply ravi 7/25/2004 6:57:33 AM

See related articles to this posting


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
Reply 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
Reply 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
Reply 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
Reply 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
Reply Rich 7/25/2004 7:47:20 PM
comp.unix.programmer 10664 articles. 64 followers. Post

5 Replies
468 Views

Similar Articles

[PageSpeed] 25


  • Permalink
  • submit to reddit
  • Email
  • Follow


Reply:

Similar Artilces:

closing file descriptors
Here's a question I never thought I'd have to ask -- how do I close a file descriptor opened via IO.sysopen ? -mental On Apr 24, 2007, at 5:45 PM, MenTaLguY wrote: > Here's a question I never thought I'd have to ask -- how do I close > a file descriptor opened via IO.sysopen ? IO.new(IO.sysopen(path)).close Seems a bit round-about though. Gary Wright ...

how to close a file descriptor ?
Hello all I must develop an application where I create a file descriptor with open(), then I write some data into the file with write() and then I close the file with close(). The documentation of close() says that there can by errors during this operation. For me it is not important if there is an i/o error or if close fails because there is no space left on the device. It is not important if I loose data during the close operation. For me it is important to close the file handle - to deallocate it. How can I close the file descriptor (I mean to deallocate it) if close...

How to close a file descriptor ?
Hello all I must develop an application where I create a file descriptor with open(), then I write some data into the file with write() and then I close the file with close(). The documentation of close() says that there can by errors during this operation. For me it is not important if there is an i/o error or if close fails because there is no space left on the device. It is not important if I loose data during the close operation. For me it is important to close the file handle - to deallocate it. How can I close the file descriptor (I mean to deallocate it) if close...

Close file descriptor
Hi: The following function intends to close the given fd. After function returned, fd associates to no system resource. Is it correct? int my_close(int fd) { for(;;) { if(close(fd)==0) { return(0); } switch(errno) { case EBADF: return(EBADF); case EIO: return(EIO); /* fd assciates to no resource? */ case EINTR: break; /* try close(fd) again. ok? */ default: return(errno); /* not expected to reach here */ }; } } IJ. Wang wij@seed.net.tw wrote: > Hi: > > The following function ...

how to close a stalled file descriptor?
I'm having trouble closing a file descriptor on a stalled named pipe. To unblock myself if the write takes too long because the pipe is full and there is no reader on the other end of the pipe, I put in place a signal handler. But if the signal handler is invoked and I regain control, on any subsequent attempt to close the file descriptor, it stalls again! How do I force the file descriptor to close so I don't have an infinite buildup of file descriptors in my process, if I want to continue operating? Even a simple "die" or "exit" won't work because it tries ...

closing all open file descriptors
i just discovered a subtle bugette (no harm done except nfs silly name created) in some code i had written that is caused by forking with open file descriptors followed by a close/unlink in the parent. the child never uses the file and so it's not really an 'error' - but on nfs this will cause a sillyname to appear (.nfsxxxxxxxx) and remain until the child exits which, in this case, can be up to five days later and i have dozens of these guys cluttering up my directories since this code runs on dozens of machine simoutaneously. i'm wondering - is there some way in ruby to ob...

Closing socket file descriptors
Hi, I'm experiencing a problem when trying to close the file descriptor for a socket, creating another socket, and then closing the file descriptor for that second socket. I can't tell if my issue is about Python or POSIX. In the following, the first time through, everything works. On the second connection, though, the same file descriptor as the first connection may be re-used, but for some reason, trying to do os.read/close on that file descriptor will cause an error. Thanks in advance for any help on why this problem is occurring and/or how to resolve it (preferrably, I ca...

How to close roque file descriptors?
Let's say a program opens a bunch of file handles and then dies non-gracefully, leaving this(on a linux box): bash-2.05$ /sbin/sysctl fs.file-nr fs.file-nr = 65536 64352 65536 Is there a way to close all those file descriptors without rebooting? Previously, I thought that exiting the parent shell would close them, but I was wrong... Kyndig <kyndig@unixpowered.org> wrote: > Let's say a program opens a bunch of file handles and then dies > non-gracefully, leaving this(on a linux box): > > bash-2.05$ /sbin/sysctl fs.file-nr > fs.file-nr = 6553...

How to close rogue file descriptors?
Let's say a program opens a bunch of file handles and then dies non-gracefully, leaving this(on a linux box): bash-2.05$ /sbin/sysctl fs.file-nr fs.file-nr = 65536 64352 65536 I was under the impression that when a process dies, all descriptors are freed by the kernel; but I must be wrong. Is there a way to close all those file descriptors without rebooting? kyndig@unixpowered.org (Kyndig) wrote in message news:<4a8a6f54.0309090634.8e441b7@posting.google.com>... > Let's say a program opens a bunch of file handles and then dies > non-gracefully, leaving...

how to find and close opened file descriptor
During my testing, I found that ruby doesn't create IO object for each opened file descriptor which are inherited from parent process. So we have to close all these opened file descriptor except stdin, stdout and stderr by following way: 3.upto(1023) do |fd| begin if io = IO::new(fd) io.close end rescue end end But I really don't like this way. Is there better way that I can find all opened file descriptors. And now I am assuming the maximum file no is 1023. Is there any way that I can get the maximum file no? Thank...

exec command
On my Solaris system, the man page for "exec" says the following: ksh With the exec built-in, if arg is given, the command speci- fied by the arguments is executed in place of this shell without creating a new process. Input/output arguments may appear and affect the current process. If no arguments are given the effect of this command is to modify file descrip- tors as prescribed by the input/output redirection list. In this case, any file descriptor numbers greater than 2 that are opened with this mechanism are clos...

cant close file descriptor 3 ???
I developed a program under Debian, wxWidgets 2.6.4. Code::Blocks unicode nightly. (gcc 3.4.2) Now i tried to compile and run this program under WinXp, wxWidgets 2.8.6, Code::blocks unicode nightly. (mingw 3.4.2, gdb for mingw 6.3). My program has the correct includes and libraries path as far as i know. Compiling and linking went fine. Altough the program starts but it drops a 'Test error' with the following messages: Cant close file descriptor 3 (error 0: the operation completed successfully.) The details of this error window shows another two messages: Cant read file de...

Force close of file descriptor from command-line
Hi all, I've written a program which uses socket() and connect() to send data from a client to server. In a particular version, if connect() fails then close() is not called on the file-descriptor (whoops!) so I've got a particular instance which is locked as it has reached the maximum limit for file descriptors as there are about 994 sockets plus a few other FDs. I'd like to close these sockets manually from the command-line without terminating the program (the rest of which is still running fine). Is there a way to do this - I know it 'should' be safe as the file descri...

How to exit without closing open file descriptors
Hi. Sorry, I don't have my books handy and am trying to figure out a way to internally terminate a program without closing open file descriptors. I thought abort() might do it, but online documentation says otherwise. Maybe raise with a 9? Thanks, Brian brass@mailvault.com wrote: > Hi. Sorry, I don't have my books handy and am trying > to figure out a way to internally terminate a program > without closing open file descriptors. I thought abort() > might do it, but online documentation says otherwise. > Maybe raise with a 9? Would you please describe what th...

close file descriptor 0,1,2
Hi, I'm observing the following behavior in one of our systems. It has a close(0) call to close file descriptor 0. This eventually caused some trouble to our system, due to my second question below. So, can someone shed lights on: 1) is it ever legitimate to close(0) or close(1) or close(2)? 2) I did some experiments and found out that once I have close(0), next open call will think 0 is a valid descriptor and start assigning this to open calls. Is this expected? On Thu, 12 Apr 2012 18:45:40 -0700, DE wrote: > 1) is it ever legitimate to close(0) or close(1) or close(2)? Yes. And t...

/bin/su closes open file descriptors ?
It seems that an AIX open file descriptor is not preserved across /bin/su. I have a program that opens a file, then passes that file's file descriptor to an exec'ed program with a command line argument. The exec'ed program reads directly from that fd. No problem here, as file descriptors are kept open across exec() calls. But when I put /bin/su in between the calling and called program, the called program gets errno 9 (Bad file descriptor) when attempting to read. I guess somebody along the way (either su or sh) has closed the file. I thought that perhaps a range of low fd's...

RunRecoveryException due to closed log file descriptor
I'm getting a RunRecoveryException due to a problem with the FileManager's log file. I have no idea why this might be happening, any ideas? There are a few deadlocks going on during multiple threads that properly abort() any transactions, so I'm at a loss. ... Adam Caused by: Environment invalid because of previous exception: com.sleepycat.je.RunRecoveryException: (JE 3.2.23) Channel closed, may be due to thread interr upt at com.sleepycat.je.log.FileManager $LogEndFileDescriptor.force(FileManager.java:1688) at com.sleepycat.je.log.FileManager.syncLo...

FAQ 5.32 How do I close a file descriptor by number?
This message is one of several periodic postings to comp.lang.perl.misc intended to make it easier for perl programmers to find answers to common questions. The core of this message represents an excerpt from the documentation provided with Perl. -------------------------------------------------------------------- 5.32: How do I close a file descriptor by number? This should rarely be necessary, as the Perl close() function is to be used for things that Perl opened itself, even if it was a dup of a numeric descriptor as with MHCONTEXT above. But if you really have to, you ma...

FAQ 5.33 How do I close a file descriptor by number?
This message is one of several periodic postings to comp.lang.perl.misc intended to make it easier for perl programmers to find answers to common questions. The core of this message represents an excerpt from the documentation provided with Perl. -------------------------------------------------------------------- 5.33: How do I close a file descriptor by number? This should rarely be necessary, as the Perl close() function is to be used for things that Perl opened itself, even if it was a dup of a numeric descriptor as with MHCONTEXT above. But if you really have to, you ma...

What happens if I close a file descriptor a memory map is based on?
If I open a file using open() then use the resulting file descriptor in an mmap() call, will there be any issues if I then close that file descriptor but continue to use the memory mapped by mmap? B2003 Boltar <boltar2003@yahoo.co.uk> writes: > If I open a file using open() then use the resulting file descriptor > in an mmap() call, will there be any issues if I then close that file > descriptor but continue to use the memory mapped by mmap? No. That's the way I have used mmap most of the time myself. Rainer Weikusat <rweikusat@mssgmbh.com> writes: > Boltar <...

FAQ 5.34 How do I close a file descriptor by number?
This is an excerpt from the latest version perlfaq5.pod, which comes with the standard Perl distribution. These postings aim to reduce the number of repeated questions as well as allow the community to review and update the answers. The latest version of the complete perlfaq is at http://faq.perl.org . -------------------------------------------------------------------- 5.34: How do I close a file descriptor by number? If, for some reason, you have a file descriptor instead of a filehandle (perhaps you used "POSIX::open"), you can use the "close()" fu...

What does closing file descriptor 1(STDOUT_FILENO) really signify
What does closing file descriptor 1(STDOUT_FILENO) really signify? Does this mean that we cant anymore write on standard out.???? On Mar 22, 12:21 pm, Sanchit <sanchitgupt...@gmail.com> wrote: > What does closing file descriptor 1(STDOUT_FILENO) really signify? It closes the stdout stream that was automatically opened for you when the program started. > Does this mean that we cant anymore write on standard out.???? Yes. The following program: #include <stdio.h> int main (int argc, char **argv) { printf("output 1\n"); close(1); printf("output 2\n&qu...

file descriptors are not being free even after closing the socket connection..
Hi All, My application is giving the error of nofiledesc exceeding while tring to get a socket connection. When I checked with the command lsof total number of file descriptor used on that particular instance, it gave me 1956. And the current setting for total number of file descriptor is 2000 (the default one). So that means really the nofile descriptors are going high. But when I went and cheked that where these much descriptors are being used I saw so many connection with the following details: 6u IPv4 0xf10006000335bb98 0t0 TCP : (CLOSED) 7u IPv4 0xf10006000318c398 0t0 TCP : (CLOSED) 8u ...

File descriptor open in one thread, closed in another?!
Hello gang, My coworker and I are writing a Python class for the other developers within our team. This class is supposed to encapsulate several things, including daemonizing, setting up logging, and spawning a thread to host an XML-RPC server. I'm having a real problem with logging.Logger and threading.Thread. In the main thread, we set up two loggers, each with file handlers. We then spawn the xml-rpc server thread, and allow the parent thread to continue. At some point, the stream of the file handlers appears to be closed from the xml-rpc server's thread, but still open from the P...