Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 6 Jul 1996 19:15:25 GMT
From:      James Raynard <fqueries@jraynard.demon.co.uk>
To:        tst@titan.cs.mci.com
Cc:        freebsd-questions@freebsd.org
Subject:   Re: How do you write to an executable (binary)?
Message-ID:  <199607061915.TAA02113@jraynard.demon.co.uk>
In-Reply-To: <Pine.OSF.3.91.960705112043.18146B-100000@titan.cs.mci.com> (tst@titan.cs.mci.com)

next in thread | previous in thread | raw e-mail | index | archive | help
> I have a program that will prompt the user for a value.  I would like to 
> write that value to the executable (binary) file.  (Using open, lseek, 
> write, close)

Ugh. Presumably you need this for an installation program which asks
for the licence number?

> When I open the file I get the following error:
> 
> "Error: Text file busy".  The message number is [ETXTBSY].

>From open(2):-

The named file is opened unless:

  [ETXTBSY]     The file is a pure procedure (shared text) file that is be-
                ing executed and the open() call requests write access.

However, it seems you *can* write to a running binary , but only if
you do it interactively:-

[a.out is a program that sleeps for 60 seconds and prints "Hello from
a.out", temp prints "Hello world!" immediately and exits]

$ ./a.out &
[1] 1952
$ cat temp > a.out
ksh: cannot create a.out: Text file busy
$ mv temp a.out
override rwxrwxr-x  james/staff for a.out? y
$ ./a.out 
Hello world!		# The new a.out prints its message
$ Hello from a.out	# 60 seconds later, the old a.out returns

> I'm able to do this with other OS.  How can I get this to work with 
> FreeBSD? Any ideas or suggestions would be greatly appreciated.

If you *really* have to do this, rename() can write over an executing
binary, however I would think very carefully about your program design
first...

$ cat temp.c
#include <stdio.h>
#include <unistd.h>

main() {
        if (rename("a.out", "temp2") < 0)
                perror("rename");
        return 0;
}

$ gcc temp.c
$ ./temp2 &	# The old temp2 - sleeps for 60 seconds and prints a message
$ ./a.out
$ ./temp2
rename: No such file or directory # The new temp2 can't find a.out!
$ Hello from temp2		  # The old temp2 returns

-- 
James Raynard, Edinburgh, Scotland
james@jraynard.demon.co.uk
http://www.freebsd.org/~jraynard/



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