exec returning 255
I have my code running on the Mac and I am getting a 255 return code from exec. The following is the code:
ret = execvp(pArgs[0], pArgs);
if (ret < 0)
{
ret = errno;
exit(ret);
return false;
开发者_如何学编程 }
else if (processId < 0)
{
// fork() failed
return false;
}
else if(Wait)
{
// forked successfuly so wait for exit
if(waitpid(processId, &childstatus, 0) == processId)
{
// codesign command terminted, get return code
if(WIFEXITED(childstatus))
{
if(pCmdRetStatus != NULL)
*pCmdRetStatus = WEXITSTATUS(childstatus);
}
}
}
Any thoughts on why the 255? Essentially an hdiutil call, a lot of times, I get 255.
UNIX (and therefore Mac OS X) exit statuses are forced into the range 0-255 unsigned.
So a return value of -1
from your call to execvp
would be processed as -1 in your C code but would become 255 at the operating-system level due to the rules of the exit()
function specification. Consider the following example:
bash> bash
bash2> exit -1
bash> echo $? # The exit status of the last command (bash2)
255
execvp returns an integer (-1) on error (and sets errno, which you should check/print (hint: perror
)) which you pass to exit
. Exit really only knows about EXIT_FAILURE
and EXIT_SUCCESS
, but it generally just passed on the value to the OS (which can usually handle 0-127 / 0-255, but don't count on it).
The only possible return values for exec
are 0 and is -1. I'm guessing the type of the variable ret
is wrong (unsigned char
instead of int
) and thus -1 is getting converted modulo 256 to 255.
精彩评论