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
503 Views

Similar Articles

[PageSpeed] 11


  • Permalink
  • submit to reddit
  • Email
  • Follow


Reply:

Similar Artilces:

Open folder, read file, close file, open new folder, read file close file etc....
Hello, My current directory has several folders within it. Each folder has the same type of file within it. How do i go from the current directory, into one of the folders, edit the file, then come out of the folder, then go into another folder, edit a file then come out of the folder then into a new one etc etc etc. If this does not make sense i apologise and will try to explain my problem a little better. Any help would be much appreciated. Cheers, Phil Phil: I'm not sure it makes sense. Personally I use the mouse and keyboard for doing this. Were you thinking of something else?...

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...

File descriptors missing on startup: stderr; Bad file descriptor
I've recently started getting these messages in my mail log. I'm puzzled, because I know it used to be OK, and I don't recall changing anything that could cause this problem. Fortunately sendmail seems to carry on regardless. I've tried restarting sendmail (and rebooting the whole box) to no avail. There doesn't seem any obviously wrong: lsof gives the results at the end, which show files on open on fd 0, 1, 2 I see from google that this keeps popping up - but there seems to have been no definitive answer, and it looks something of a mystery. This is with 8.12.8p1 on ...

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...

Closing relatef files with script step "close files"
Hello, I would like to create a script to update my database created in Filemaker Pro 7. To do this I also created a file called "updater" in which I can perform scriptsteps with the files that have to be updated to a new version. At a certain point I would like to close all open files exept the active file, being "update". For this I use "Close File". This doesn't work because the files that have to be closed have relations with other files. Therefore while closing one file, Filemaker automatically opens the file again if this file has a relationship ...

How to find New File,Write file and close file?
Hi Anyone can show me how to find New File,Write File and close file in block diagram.The picture of file names are as show in pics. Thanks &nbsp; Nyan Lin File find.JPG: http://forums.ni.com/attachments/ni/170/322096/1/File find.JPG Hi. The functions You're looking for are under "File I/O --&gt; Advanced File Functions" (At least the NEW File) The 2 others are in the "File I/O" menu on the functions palette. Good luck.Message Edited by BCL@Servodan on 05-08-2008 03:38 AM Hi again. My mistake. I was looking in my LabView7. In 8.5.1 the Icons have changed.....

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...

Reading of file by next of map file and by next of file descriptor.
How many times is map file faster than reading of file by next of file descriptor and when both times are so aqual one? Thanks in advance. * Mirosław Makowiecki: > How many times is map file faster than reading of file by next of file > descriptor and when both times are so aqual one? > Thanks in advance. Measure. -- A: Because it messes up the order in which people normally read text. Q: Why is it such a bad thing? A: Top-posting. Q: What is the most annoying thing on usenet and in e-mail? ...

Grabbing file from file descriptors
Hi all ! A question about file descriptors: supposing I don't have lsof installed, how can I know what file descriptors 5 , 6 and 7 correspond to ? I mean I'd like to know what file or socket they're actually pointing...... I admit I don't have too much Solaris culture but with Linux I'm accustomed to find in /proc/ProcNum the file descriptors opened and as symbolic link, to what file they're actually pointing. I'd like to know how can I achieve it with Solaris. P.s. Is it possible to compile Solaris kernel so that it shows more information in the /proc file syste...

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...

File descriptor to file object
Hi, tempfile.mkstemp returns a file name and a file descriptor (as returned by os.open). Can I somehow convert this descriptor to a file object? Thanks! Nathan ...

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...

extracting file from the file descriptor
I am using Linux gcc 4.1.1 version I have a file descriptor and I want to have a FILE* pointer to the file from the file descriptor. OR if I can have the file name using the file descriptor.. Please help.. On 14 Juni, 12:38, aka...@rediffmail.com wrote: > I am using Linux gcc 4.1.1 version > I have a file descriptor and I want to have a FILE* pointer to the > file from the file descriptor. man 3 fopen -- Erik Wikstr=F6m Erik Wikstr�m wrote: > On 14 Juni, 12:38, aka...@rediffmail.com wrote: >> I am using Linux gcc 4.1.1 version >> I have a file descriptor and I ...

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...