Detecting exec(2) failing after performing fork(2)?

Eric Rosenquist rosenqui at crc.skl.dnd.ca
Sat Mar 2 07:59:44 AEST 1991


In article <shj.667793966 at dkuugin> shj at 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 at crc.skl.dnd.ca
Software Kinetics Limited
65 Iber Road, Stittsville, Ontario
Canada - K2S 1E7  Phone (613) 831-0888



More information about the Comp.unix.programmer mailing list