ETXTBSY: Text file busy
On a recent Freakshow episode, the question arose what would happen if a running executable were to be changed while still running. As it turns out, the behaviour is different, depending on the platform and even possibly the filesystem.*)
On a Solaris 10 (x86) system, the following happened:
$ gcc hallo.c -o hallo.exe $ gcc hallo_capital.c -o hallo_capital.exe $ ./hallo.exe Hallo Welt! Hallo Welt! Hallo Welt! [...]Now, on a different terminal we overwrite (but don't unlink) it:
$ cat hallo_capital.exe > hallo.exeOn the first terminal again, "
Welt
" changes to "WELT
":
[...] Hallo Welt! Hallo WELT! Hallo WELT! Hallo WELT!When I changed even more (e.g. using printf instead of puts), the program would stop or even crash.
So, what do other platforms do? On Linux 3.12.0-rc5 we're not able to overwrite the running executable and ETXTBSY is returned:
$ cat hallo_capital.exe > hallo.exe bash: hallo.exe: Text file busyOr, via strace:
open("hallo_capital.exe", O_RDONLY|O_LARGEFILE) = 3 dup2(3, 0) = 0 close(3) = 0 _llseek(0, 0, [0], SEEK_CUR) = 0 open("hallo.exe", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0666) = -1 ETXTBSY (Text file busy)The same happens on a FreeBSD 9.1 system.
Interestingly, the running executable can be overwritten on Darwin (so much for the BSD part in MacOS), but the output does not change, i.e. the running binary is not modified.
*) [Citation needed]