Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

c - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...