On Wed, Jun 12, 2002 at 11:21:18AM +0300, Adi Stav wrote: > On Tue, Jun 11, 2002 at 01:25:11PM +0300, Muli Ben-Yehuda wrote: > > Is there a way to open a file (get an fd) and then delete it, in one > > atomic operation? > > > > I need to open a temporary file (but with a fixed name, so mkstemp() > > and friends are not an option) and then make sure it doesn't remain > > behind if the program should die unexpectedly. Doing > > > > open("foo", ...); > > unlink("foo", ...); > > > > is obviously unsafe, since foo might be pointing to something else by > > the time I unlink it. Suggestions? > > Let's see... open() increases the inode reference count, while > unlink() decreases it. What you want to do is to keep it constant, > just _transfer_ the reference (atomically) from the directory to your > process. Transferring an inode reference is rename(). I don't know if > you can rename a file into /proc/self/fd, but you can use an > intermediate private directory:
Using a secure, private directory was indeed the answer. Thanks to everyone who replied. The code, for the curious, is available at http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/syscalltrack/syscalltrack/tests/tester.c?rev=1.25&content-type=text/vnd.viewcvs-markup Look for dir_cleanup() and dir_setup(). Let me know if you poke any holes in it ;) -- Sunday 21 Forelithe 7466 http://vipe.technion.ac.il/~mulix/ http://syscalltrack.sf.net/
msg19988/pgp00000.pgp
Description: PGP signature