1

Is it possible to modify source binary in Linux if any process spawned from that binary is still running?

Will OS allow to perform write operation on that binary?

1 Answers1

3

Try it:

cd $(mktemp -d)
cp /bin/sleep .
./sleep 120 &
echo test > sleep

The shell’s redirection operator changes the file in-place, and this fails with a “text file busy” error.

It is however possible to replace the file:

cp /bin/ls sleep

This is how, for example, packages can be updated while the program they contain is running. The old file remains accessible to the running process as long as it keeps running.

Stephen Kitt
  • 411,918
  • 54
  • 1,065
  • 1,164
  • I may be missing something obvious, but that `ETXTBUSY` looks more like a historical feature kept because of tradition: the same protection is [not extended to shared libraries](https://unix.stackexchange.com/a/539531/308316). –  Mar 20 '20 at 17:39
  • Yes, I took a narrow interpretation of “source binary”... – Stephen Kitt Mar 20 '20 at 18:08