Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!thunder.mcrcim.mcgill.edu!snorkelwacker.mit.edu!apple!voder!pyramid!moliver From: moliver@shadow (Mike Oliver) Newsgroups: comp.unix.internals Subject: Re: executing a stream Message-ID: <141325@pyramid.pyramid.com> Date: 16 Jan 91 22:06:23 GMT Sender: daemon@pyramid.pyramid.com Reply-To: moliver@shadow.pyramid.com (Mike Oliver) Organization: Pyramid Technology Corp., Mountain View, CA Lines: 41 In article <1991Jan16.201910.8646@Think.COM> barmar@think.com (Barry Margolin) writes: >In article <1991Jan15.204849@IASTATE.EDU> spam@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@pyramid.com {allegra,decwrl,hplabs,munnari,sun,utai,uunet}!pyramid!moliver