Re: How to tell when execv has succeeded



On 2009-01-07, Gordon Burditt <gordonb.h6tb6@xxxxxxxxxxx> wrote:
I'm needing to run other programs from a master program. I'm doing it in the
traditional way using fork() and execv(), but I can't figure a way to tell the
parent that the execv() has succeeded. Failed, yes, because it returns to the
caller, but not succeeded. I need to know whether it is OK before proceeding
to do the next thing.

What's wrong with the rule: if it didn't fail, it succeeded?

Because it is by then running in a child process. That parent needs to know
that it succeeded (and preferably if it didn't then know the error), it can't
just assume that it succeeded until something catastrophic happens when it
expects the child to be running and it isn't, nor can it just hang around
waiting for an exit which hopefully won't happen.

Some
programs use a particular special exit status (used after exec()
returns) to indicate exec() failure (yes, the program you run *could*
spoof this).

That's the opposite way round. If it fails it's easy to find out, it's if it
succeeds that there is a problem.

But is exec() failure really that different from the
program putting out: "blurfl: -n and -v flags may not be used
together", followed by exit(1)?

Exec /failusre/ is not a problem. Exec /success/ is the problem. They are
not symmetrical.

The way to do it, as others pointed out, is to have a pipe set to
close-on-exec, then if the exec succeeds the pipe will be closed giving EOF on
the read in the parent, and if it fails the child can then write to the pipe
(and in fact I'm writing the value of errno to the pipe so that the parent can
take appropriate recovery action).

Chris C
.