Databases
cancel
Showing results for 
Search instead for 
Did you mean: 

One call to close()/fclose() closes all file descriptors ?

Shay Zukerman
Occasional Advisor

One call to close()/fclose() closes all file descriptors ?

Hi
A strange question, due to a strange thing ...
A program of that was invoked from a ksh script had destroyed a binary file.
Looking at the file, we found that the program had written an error message, which should have been written to STDERR.
This we believe had happend since somewhere, the STDERR file or its handle (2)were closed.
As a result, the handle - 2 was assigned to the binary file, which was later corrupted.
So far, I could not find any place where the STDERR was closed.
Using lsof, I'd found that the script file, which had invoked the problem had redirected STDOUT (1) and STDERR (2) to a file, which was later opened again by the problematic program.
To sum things up - this file was opened 3 times - 2 by the calling script (which had passed its open files to the program), and by the program itself.
When the program ends. it closes that file once, but there're cases where I believe/think (no sure..) all the file handles are closed - including STDERR.
Right now, I'm looking for an answer whether - one call to fclose() or close() functions close or release ANY/ALL file descriptor(s), that are assinged to the file that is closed.
Thanks for your assistance.
2 REPLIES
A. Clay Stephenson
Acclaimed Contributor

Re: One call to close()/fclose() closes all file descriptors ?

The close() system call or its higher level equivalents, fclose() and pclose(), close only one file descriptor. Even in the case of the most closely coupled file descriptors created using the dup() system call, a close is required for both file descriptors. I've never seen any flavor of UNIX behave differently.


man 2 close and 2 dup for more details.
If it ain't broke, I can fix that.
Shay Zukerman
Occasional Advisor

Re: One call to close()/fclose() closes all file descriptors ?

Hi

Thanks for your very quick reply.
I had to send my question after a long time that I could not find an answer.
The problem only exists on one installation out of more that 5000 of my company's software, and so far, I could find a reasonable explaination (the ony one I have now that it's simply a tricky bug ... I'll try to keep you all update if I find anything.

Thanks
Shay