From owner-freebsd-questions Sun Jul 7 01:53:29 1996 Return-Path: owner-questions Received: (from root@localhost) by freefall.freebsd.org (8.7.5/8.7.3) id BAA07799 for questions-outgoing; Sun, 7 Jul 1996 01:53:29 -0700 (PDT) Received: from relay-2.mail.demon.net (disperse.demon.co.uk [158.152.1.77]) by freefall.freebsd.org (8.7.5/8.7.3) with SMTP id BAA07790 for ; Sun, 7 Jul 1996 01:53:25 -0700 (PDT) Received: from post.demon.co.uk ([158.152.1.72]) by relay-2.mail.demon.net id ab27561; 7 Jul 96 9:53 +0100 Received: from jraynard.demon.co.uk ([158.152.42.77]) by relay-3.mail.demon.net id aa26055; 6 Jul 96 22:27 +0100 Received: (from fqueries@localhost) by jraynard.demon.co.uk (8.6.12/8.6.12) id TAA02113; Sat, 6 Jul 1996 19:15:25 GMT Date: Sat, 6 Jul 1996 19:15:25 GMT Message-Id: <199607061915.TAA02113@jraynard.demon.co.uk> From: James Raynard To: tst@titan.cs.mci.com CC: freebsd-questions@freebsd.org In-reply-to: (tst@titan.cs.mci.com) Subject: Re: How do you write to an executable (binary)? Sender: owner-questions@freebsd.org X-Loop: FreeBSD.org Precedence: bulk > 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 #include 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/