What are the reasons that an exec (execl,execlp, etc.) can fail? If you make a call to exec and it returns, are there any best practices other than just panicking and calling exit?
The problem with handling exec
failure is that usually exec
is performed in a child process, and you want to do the error handling in the parent process. But you can't just exit(errno)
because (1) you don't know if error codes fit in an exit code, and (2), you can't distinguish between failure to exec
and failure exit codes from the new program you exec
.
The best solution I know is using pipes to communicate the success or failure of exec
:
exec
, since close-on-exec made successful exec
close the writing end of the pipe. Or, if exec
failed, the parent reads the error code and can proceed accordingly. Either way, the parent blocks until the child calls exec
.