WTERMSIG() 如何计算出大于 63 的值,例如 67 或 123?
事情是这样发生的。我正在使用 popen() 运行 unbuffer ,而 unbuffer 又运行一个 shell 脚本。下一个代码块是一个 while() 循环,它从 popen() 返回的文件描述符中读取,直到它收到 NULL。紧随其后的是 pclose()。
在极少数情况下,pclose() 将返回一个值!= -1 并将 WIFSIGNALED() 设置为 true。奇怪的是,WTERMSIG() 然后计算出大于 63 的值,例如 67 或 123。这可能是什么原因?
shell 脚本正在完成它的所有任务,所以它不会在中途被打断。我可以理解 1-31(标准信号)或 32-63(扩展实时信号)。尝试在我们的平台 (Timesys Linux) 上生成信号 67 或 123 会导致错误。
以下是一些相关代码:
FILE* fp = popen(command.c_str(), "r");
if (fp == NULL) {
// Handle error
}
while (fgets(line, sizeof(line), fp) != NULL) {
// Do stuff with output of child process
}
pclose_status = pclose(fp);
pclose_errno = errno;
if (pclose_status != -1 && WIFEXITED(pclose_status)) {
// Happy ending, occurs 99% of the time
return (WEXITSTATUS(pclose_status)));
}
// Check if pclose returned an error. If so, log the result.
if (pclose_status == -1) {
// Log an error and pclose_errno
}
else {
if (WIFSIGNALED(pclose_status)) {
// This happens maybe 1% of the time, but the value
// printed is > 63.
Syslog::error("Child process terminated by signal %d",
WTERMSIG(pclose_status));
}
if (WIFSTOPPED(pclose_status)) {
// Hasn't happened.
Syslog::error("Child process stopped by delivery of signal %d",
WSTOPSIG(pclose_status));
}
if (WIFCONTINUED(pclose_status)) {
// Hasn't happened.
Syslog::error("Child process resumed by delivery of SIGCONT");
}
}