basic C question

Lennart Sorensen lsorense-1wCw9BSqJbv44Nm34jS7GywD8/FfD2ys at public.gmane.org
Mon Jul 12 20:13:12 UTC 2004


On Mon, Jul 12, 2004 at 03:54:28PM -0400, fcsoft-3Emkkp+1Olsmp8TqCH86vg at public.gmane.org wrote:
> I've been struggling off and on with problems of this nature for a while.
> 
> Essentially the problem is how to detect that a file descriptor which was 
> previously successfully opened no longer points to a valid inode.
> 
> Here is some code which uses a standard file which illustrates quite simply  
> that write() will not fail when the inode is removed after a successful 
> open().
> 
> Compile this code and run it.      It will write "go1" and "go2" into a file 
> called bob.dat separated by a sleep of 20 sec.
> 
> Repeat the test,  but after the first write go to another console and remove 
> the bob.dat file.      The second write will still not return an error.    
> ie. file it is supposedly writing to is gone but the write() apparently quite 
> happily succeeds.
> 
> The question remains how can we verify the veracity of the file descriptor we 
> are about to write to before we actually allow the write to "succeed" 
> regardless.
> 
> Any help appreciated.

The file is not actually removed until all handles to it are closed.
The entry is removed from the directory so there is no way anyone else
can open that inode, but your program keeps access to that inode until
it closes the file.

If you want to check if the file you have open has been deleted, you
probably have to check the directory contents, or open the file a second
time.  You could always close and reopen the file between writes (open
in no create mode to no make a new file).

Lennart Sorensen
--
The Toronto Linux Users Group.      Meetings: http://tlug.ss.org
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml





More information about the Legacy mailing list