Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 04 Sep 2003 11:20:00 -0700
From:      Tim Kientzle <kientzle@acm.org>
To:        Paul Richards <paul@inty.com>
Cc:        current@freebsd.org
Subject:   Re: Text file busy
Message-ID:  <3F578250.7020100@acm.org>
In-Reply-To: <1062686653.67807.77.camel@localhost>
References:  <1062686653.67807.77.camel@localhost>

next in thread | previous in thread | raw e-mail | index | archive | help
Paul Richards wrote:
> Overwriting a file that's currently executing results in a "Text file
> busy" error.

I guess there are folks around who don't know this:

When you execute a program, the program is not simply copied
into memory.  Instead, the kernel keeps the file open and pages the
executable in as necessary.  This is called "demand-paging of
executables" and it's an old performance optimization that
improves VM operation (executable code never needs to be copied
out to swap; it can just be dumped and paged back in later) and
quickens application startup (only the immediately-required
parts of the application are read into memory immediately).
I'm not certain, but I suspect it first appeared in Unix in
the mid-1970s.

In essence, the file _is_ the executable contents of memory.
Overwriting it is almost always a bad idea; if the
system has to swap in another part of that executable,
the program is almost certain to crash.

> This was something that was fixed way back on FreeBSD but it seems to be
> a problem again.

Depends on how you're installing the binary.  It has always been
safe to do either of the following:
   * Rename the current executable and then install the new one.
   * Unlink the current executable and then install the new one.
Many tools that claim to "overwrite" really do the latter, which
causes a certain amount of understandable confusion.  (I'm pretty
sure "install" does unlink/copy by default and will do rename/copy
if you specify -b.)

True overwriting of in-use executable files (e.g., "cat new > old")
is dangerous and should be prohibited.

Tim

P.S. I wonder if demand-paging of executables is still a win for
program startup on modern systems with dynamically-linked executables?
Large reads are a lot more efficient, and it seems that dynamic
linking might cause more startup thrashing.  Hmmm...



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?3F578250.7020100>