Re: Write to executable which has done execve



On 12 Sie, 03:37, Pascal Bourguignon <p...@xxxxxxxxxxxxxxxxx> wrote:
David Schwartz <dav...@xxxxxxxxxxxxx> writes:
On Aug 11, 5:30 pm, Bronson <psh...@xxxxxxxxx> wrote:

i am trying to make a program, which will exec another app and this
app will write to the main's program executable. I make execve
(precisely execlp) and try to open old file for writing in new
process. But I returns me ETXTBSY. Why? For example access says, that
i can write to file, but fopen fails with ETXTBSY...
What can i do with that...

You can't do it that way. You cannot modify a program's running image
by modifying the executable file. If you could, self-modifying code
would not work properly.

That's not exactly the point. The problem is that when you exec a
file, it is not loaded into RAM, at least not entirely. It is memory
mapped, and the file is loaded only on demand, when you call the
functions that are not already loaded. Also, swapping out the the
program text can be avoided since we already have a copy in the
program file.

So if modifying the program file was allowed, it would break randomly
the running processes using this program.

What should be done, is to create a new executable, unlink the old one
and rename the new to the old name, so only new processes get the new
program.

Note that even for normal files you should usually do something like
that, to avoid being left with inconsistent data in the files when the
program is killed for any reason in the middle of file writting.

--
__Pascal Bourguignon__ http://www.informatimago.com/

NOTE: The most fundamental particles in this product are held
together by a "gluing" force about which little is currently known
and whose adhesive power can therefore not be permanently
guaranteed.

Could you explain it more precisely... So, i have two apps : main app
and update app (i know that main app could download new version, but
it has to be done on Windows with another app, so i want to do it the
same way in Linux, because i don't want to replace the code...). Main
app execve to udpate app, update download new app to temp file. After
that it renames it to main app and delete the temp file. But how do i
unlink the old app?? I mean, i suposse that rename will fail for the
same reason, why writing to executable fails now. Correct me if i am
wrong.

Thanks,

Przemek

.



Relevant Pages

  • Re: Display contents of text file in the Browser
    ... It sounds like your app is attempting to read from the file at the same time ... that it is writing to it, ... process and attempting to read the file and display it at the same time. ... memory, and return that string to the client, while launching a separate ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Writing to eventlog
    ... > of it is writing to log informations from which table it was readed. ... If your app is running on an intranet server that you or your company ... domain user account and tell your app run under that account. ...
    (microsoft.public.dotnet.framework.aspnet)
  • how to know the parameters of sys_open and sys_unlink system calls of all apps.
    ... I am writing this application that is designed as follows. ... As long as the app is running on the ... Alessandro rubbini's linux device drivers' book with the first couple ... design pointers as to how I accomplish this ...
    (comp.os.linux.development.system)
  • Re: Writing to eventlog
    ... >> end of it is writing to log informations from which table it was readed. ... > a domain user account and tell your app run under that account. ...
    (microsoft.public.dotnet.framework.aspnet)
  • how to know the parameters of sys_open and sys_unlink system calls of all apps.
    ... I am writing this application that is designed as follows. ... As long as the app is running on the ... Alessandro rubbini's linux device drivers' book with the first couple ... design pointers as to how I accomplish this ...
    (comp.os.linux.development.apps)