Good day! I have a process which was created via posix_spawn
function. How can I get his exit code?
I know about waitpid()
function but it returns status for child process. Also if process still a live and I will call waitpid()
function then I will receive right information what this process isn't closed yet.
Here is my current code:
int GetExitCode()
{
int status;
int rtn = waitpid(pid, &status, WNOHANG);
if (rtn > 0) // still live
{
return -1;
}
rtn = waitpid(pid, &status, WUNTRACED);
if (rtn != -1 || errno != ECHILD)
{
// Here I got rtn = -1 and errno #10
}
if (WIFEXITED(status))
{
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
}
But how I can check exit status for non-child process? Thanks!
Updated. My new code:
int GetExitCode()
{
int status = 0;
int rtn = kill(pid, 0);
if (rtn == -1 && errno == ESRCH)
{
return 0;
}
rtn = waitpid(pid, &status, WNOHANG | WUNTRACED | WCONTINUED);
if (rtn == 0) // still live
{
return 0;
}
std::cout << "Probably success. Errno: " << errno << ". StrError: " << strerror(errno) << std::endl;
if (WIFEXITED(status))
{
return 1;
}
return 0;
}