[net.unix-wizards] Process control query

brett@wjvax.UUCP (Brett Galloway) (01/17/86)

I am working on a program which must be able to fork and execvp an arbitrary
relative path name (as in popen(3)).  My question is whether there is any
reliable for the parent to tell if the exec() in the forked child succeeded or
failed.  I tried doing a kill(pid,0) in the parent immediately after the fork
(assuming that the fork succeeded), but that produces false negative results
when the child executed very quickly.  It might also produce false positive
results if the parent gets to the kill(pid,0) before the child gets to the
exec, although since I am using vfork() that has not happened to me.

I would be interested to know how popen(), and how the shells (sh and csh)
determine the success or failure of their exec()'s.  Note that I cannot
merely use an access(2) call on the pathname, since I am using execvp()
to find relative paths.

Thanks in advance.

Brett Galloway

-------------
Brett Galloway
{pesnta,twg,ios,qubix,turtlevax,tymix,vecpyr,certes,isi}!wjvax!brett

brett@wjvax.UUCP (Brett Galloway) (01/24/86)

Last week I posted a query to the net about communicating the success or
failure of an execve() from inside a fork() to the parent process.  I got
several responses (thank you).  I finally decided to create a pipe between
the child and the parent.  If the child's execve() fails, it writes

	write(fd,&errno,sizeof(errno))
	
down the pipe and _exit()'s.  If the child's execve() succeeds, the pipe gets
closed automatically, because I explicitly mark the close on exec flag for
the pipe in the parent).  Meanwhile, the parent sits on the other end of
the pipe.  If it sees EOF (read(fd,&errno_save,sizeof(int) == 0)), it
knows the execve() succeeded.  Otherwise, it gets the error code
returned by exeve().

Apparently, the only way for the parent to detect the difference between the
child's execve() failing or succeeding and exiting very quickly is by
using a scheme as above, which explicitly communicates the success/failure of
the execve() from the child to the parent.

-------------
Brett Galloway
{pesnta,twg,ios,qubix,turtlevax,tymix,vecpyr,certes,isi}!wjvax!brett