Reputation: 8434
I am a little confused about how to handle errors from execvp()
. My code so far looks like this:
int pid = fork();
if (pid < 0) {
// handle error.
}
else if (pid == 0) {
int status = execvp(myCommand,myArgumentVector); // status should be -1 because execvp
// only returns when an error occurs
// We only reach this point as a result of failure from execvp
exit(/* What goes here? */);
}
else {
int status;
int waitedForPid = waitpid(pid,&status,0);
//...
}
There are three cases I'm trying to address:
myCommand,myArgumentVector
are valid and the command executes correctly.myCommand,myArgumentVector
are valid parameters, but something goes wrong in the execution of myCommand
.myCommand,myArgumentVector
are invalid parameters (e.g. myCommand
cannot be found) and the execvp()
call fails.My primary concern is that the parent process will have all the information it needs in order to handle the child's error correctly, and I'm not entirely sure how to do that.
In the first case, the program presumably ended with an exit status of 0. This means that if I were to call WIFEXITED(status)
in the macro, I should get true
. I think this should work fine.
In the second case, the program presumably ended with an exit status other than 0. This means that if I were to call WEXITSTATUS(status)
I should get the specific exit status of the child invocation of myCommand
(please advise if this is incorrect).
The third case is causing me a lot of confusion. So if execvp()
fails then the error is stored in the global variable errno
. But this global variable is only accessible from the child process; the parent as an entirely separate process I don't think can see it. Does this mean that I should be calling exit(errno)
? Or am I supposed to be doing something else here? Also, if I call exit(errno)
how can I get the value of errno
back from status
in the parent?
My grasp is still a little tenuous so what I'm looking for is either confirmation or correction in my understanding of how to handle these three cases.
Upvotes: 3
Views: 30183
Reputation: 7
Here is my code: it works fine. Child status is returned in waitpid hence it tells either child process is executed successfully or not. //declaration of a process id variable pid_t pid, ret_pid;
//fork a child process is assigned
//to the process id
pid=fork();
DFG_STATUS("Forking and executing process in dfgrunufesimulator %d \n", pid);
//code to show that the fork failed
//if the process id is less than 0
if(pid<0)
{
return DFG_FAILURE;
}
else if(pid==0)
{
//this statement creates a specified child process
exec_ret = execvp(res[0],res); //child process
DFG_STATUS("Process failed ALERT exec_ret = %d\n", exec_ret);
exit(errno);
}
//code that exits only once a child
//process has been completed
else
{
ret_pid = waitpid(pid, &status, 0);
if ( ret_pid == -1)
{
perror("waitpid");
return DFG_FAILURE;
}
DFG_STATUS("ret_pid = %d, pid = %d, child status = %d\n",ret_pid,pid,status);
if (WIFEXITED(status)) {
DFG_STATUS("child exited, status=%d\n", WEXITSTATUS(status));
} else if (WIFSIGNALED(status)) {
DFG_STATUS("child killed (signal %d)\n", WTERMSIG(status));
} else if (WIFSTOPPED(status)) {
DFG_STATUS("child stopped (signal %d)\n", WSTOPSIG(status));
#ifdef WIFCONTINUED /* Not all implementations support this */
} else if (WIFCONTINUED(status)) {
DFG_STATUS("child continued\n");
#endif
} else { /* Non-standard case -- may never happen */
DFG_STATUS("Unexpected status (0x%x)\n", status);
}
if(status != 0) /* Child process failed to execute */
return DFG_FAILURE;
result = DFG_SUCCESS;
}
Upvotes: 0
Reputation: 753575
In case 1, the execvp()
does not return. The status returned to the parent process will be the exit status of the child — what it supplies to exit()
or what it returns from main()
, or it may be that the child dies from a signal in which case the exit status is different but detectably so (WIFSIGNALED
, etc). Note that this means that the status need not be zero.
It isn't entirely clear (to me) what you are thinking of with case 2. If the command starts but rejects the options it is called with, it is actually case 1, but the chances of the exit status being zero should be small (though it has been known for programs to exit with status 0 on error). Alternatively, the command can't be found, or is found but is not executable, in which case execvp()
returns and you have case 3.
In case 3, the execvp()
call fails. You know that because it returns; a successful execvp()
never returns. There is no point in testing the return value of execvp()
; the mere fact that it returns means it failed. You can tell why it failed from the setting of errno
. POSIX uses the exit statuses of 126 and 127 — see xargs
and system()
for example. You can look at the error codes from execvp()
to determine when you should return either of those or some other non-zero value.
Upvotes: 2
Reputation: 13
In the third case, errno IS accessible from the parent as well, so you could just exit(errno). However, that is not the best thing to do, since the value of errno could change by the time you exit.
To be more sure that you don't lose errno if you have code between your exec() and exit() calls, assign errno to an int:
execvp(<args>);
int errcode=errno;
/* other code */
exit(errcode);
As for your other question, exit status is not directly comparable to the errno, and you shouldn't be trying to retrieve errno from anything but errno (as above) anyway.
This documentation may help: http://www.gnu.org/software/libc/manual/html_node/Exit-Status.html
Upvotes: 1
Reputation: 982
Here is a simple code that I've tried.
if(fork() == 0){
//do child stuff here
execvp(cmd,arguments); /*since you want to return errno to parent
do a simple exit call with the errno*/
exit(errno);
}
else{
//parent stuff
int status;
wait(&status); /*you made a exit call in child you
need to wait on exit status of child*/
if(WIFEXITED(status))
printf("child exited with = %d\n",WEXITSTATUS(status));
//you should see the errno here
}
Upvotes: 3