in reply to Self Deletion

Just so you know, this is a very unportable Unix behavior. Under AIX 3 (poss. 4) and under HPUX you can't delete a file that's being exec'd -- whether it's a loadable object file or a script.

I wrote a user-space filesystem caching program for Ford Motor, and one of the problems was trying to delete (cache out) a file in use. The support scripts for the caching program often had to be removed and couldn't.

The error returned by unlink is ETXTBUSY. A workaround is to detect ETXTBUSY on unlink, and move the file up a directory in the tree with link, and then delete the original with unlink. The i-node isn't removed (and this makes the OS happy) but the directory is emptied (and this makes the programmer happy).

Replies are listed 'Best First'.
Re: Re: Self Deletion
by bluto (Curate) on Jul 07, 2001 at 19:43 UTC
    You can rm running executables on AIX 4 (AIX 3 is really old). You just can't cp over them or recompile over them. A common thing to do in AIX Makefiles is to rm them first before recompiling.

    FWIW, unlinking a running executable is usually only a problem when it resides on an NFS server, since in that case the code really does go away immediately (leading to interesting results). So I imagine this is pretty much the same on HPUX since it is really just a way to keep someone from hurting themselves, but not an absolute necessity. Of course I'm probably mistaken...

    bluto

      You can rm running executables on AIX 4 (AIX 3 is really old). You just can't cp over them or recompile over them. A common thing to do in AIX Makefiles is to rm them first before recompiling.

      It's not just AIX, it's most unices. When a program is run, part of the program is mmap'd so copying over the file would corrupt the program's memory (whereas deleting the file would not, due to the way unix handles open files with a 0 link count).