Re: Write to executable which has done execve



David Schwartz <davids@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.
.



Relevant Pages

  • Re: ListView-style column headers?
    ... writing from scratch (or modifying a CodeProject component) is probably ... articles on CodeProject on how to do this. ... Writing a Treeview/Listview control from scratch is not a trivial task. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: self modifying exe
    ... i want it be modifying itself and writing back changes to disk. ... Self-modifying EXEs don't work because Windows itself locks the EXE file. ... Modifying EXEs in general usually won't work because ordinary users don't ...
    (microsoft.public.vc.language)
  • RE: vb.net - datagrids inserting records
    ... modifying the generated code, we have to create the DataAdapter by writing ... Prev by Date: ...
    (microsoft.public.data.ado)
  • Re: self modifying exe
    ... i want it be modifying itself and writing back changes to disk. ... >> can anyone help me writing self modifying exe??? ...
    (microsoft.public.vc.language)