Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sun-barr!lll-winken!elroy.jpl.nasa.gov!swrinde!zaphod.mps.ohio-state.edu!rpi!uupsi!netfs.dnd.ca!crc.skl.dnd.ca!rosenqui From: rosenqui@crc.skl.dnd.ca (Eric Rosenquist) Newsgroups: comp.unix.programmer Subject: Re: Detecting exec(2) failing after performing fork(2)? Keywords: exec,fork Message-ID: <1991Mar1.205944.13198@crc.skl.dnd.ca> Date: 1 Mar 91 20:59:44 GMT References: Organization: Software Kinetics Limited Lines: 37 In article shj@login.dkuug.dk (Stig Jacobsen) writes: >When my application desires to spawn off a background process, >I use something roughly like this: > >int spawn(char *path, ...) >{ > > if (fork() == 0) > execlp(path, NULL); > >} > >This is fine if the exec..() call goes well. However, if the exec() >call fails, the error is not reported back to the parent. I get >a SIGCLD of course, but what I'd really like is that my spawn() >function just returns an error, if the exec() call fails. ... > ... It's too late once the fork() has completed. What you need to do is have the child exit in a particular way that your SIGCLD handler looks for if the exec() fails. In your code snippet the child keeps executing! if (fork() == 0) { execlp(path, NULL); /* if you get here, the exec() failed */ exit(SOME_STATUS_CODE); /* or */ abort() or kill(...) etc. } Eric @ SKL ------------- Eric.Rosenquist@crc.skl.dnd.ca Software Kinetics Limited 65 Iber Road, Stittsville, Ontario Canada - K2S 1E7 Phone (613) 831-0888