executing a stream

Mike Oliver moliver at shadow
Thu Jan 17 09:06:23 AEST 1991


In article <1991Jan16.201910.8646 at Think.COM> barmar at think.com (Barry Margolin) writes:
>In article <1991Jan15.204849 at IASTATE.EDU> spam at IASTATE.EDU (Begley Michael L) writes:
>>   uncompress -c microemacs|execute   /* uncompress microemacs.Z     */
>>                                      /* into a stream, and execute  */
>
>It's doable, but not trivial.  Basically, the execute command would have to
>duplicate the functionality of the kernel's exec*() system calls.  It could
>fork a process, and then use something like ptrace() to initialize the
>process from the contents of stdin.  It would have to recognize all the
>appropriate magic numbers, parse executable file headers, locate all the
>sections of the input stream (text, bss, data), perhaps fill in the user
>data structure with something more useful than "execute", fill in argv,
>argc, hardware registers, and set the PC so that execution begins at the
>appropriate place.  I don't think it can be written portably.

Why not just create a file under /tmp (say, /tmp/executable.$$), copy
from stdin into that file, then on feof(stdin) simply close() the file
and exec() it.

A better alternative for the tidy-minded would be to fork() a child to
exec() the file and have the parent wait() and unlink() it on the death
of the child's.

Arguments can be supplied to the parent and passed through the exec().

The only tricky part is deciding where to pick up standard input in the
exec()'d program.  /dev/tty is my best guess, with an option to
override it on the command line.

Or am I missing something obvious ?

>                                  [...] it would be a fine example to use
>whenever the "how do I find the pathname of the current executable"
>question comes up.

It would indeed.

Cheers, Mike.

moliver at pyramid.com
{allegra,decwrl,hplabs,munnari,sun,utai,uunet}!pyramid!moliver



More information about the Comp.unix.internals mailing list