Linux kill() error unexpected
Вопрос
Kill(pid, 0) seems to not set the error code correctly...as stated in man for kill
Errors
The kill() function shall fail if:
EINVAL The value of the sig argument is an invalid or unsupported signal number.
EPERM The process does not have permission to send the signal to any receiving process.
ESRCH No process or process group can be found corresponding to that specified by pid. The following sections are informative. 1
It is returning ENOENT (no such file or directory) and then sometimes it returns EINTR (system call interrupted)...
Here is what I am doing:
kill(g_StatusInstance[i].pid, SIGTERM) == -1 && log_fatal_syscall("kill-sigterm");
kill(g_StatusInstance[i].pid, 0);
log_info_console( "Checking process for errors: %s\n", strerror(errno));
if(errno != ENOENT)
{
kill(g_StatusInstance[i].pid, SIGKILL) == -1 && log_fatal_syscall("kill-sigkill");
}
Am I doing something wrong?
Решение
Kill(pid, 0)
seems to not set the error code correctly ... It is returning ENOENT... EINTRHere is what I am doing:
... kill(g_StatusInstance[i].pid, 0); log_info_console( "Checking process for errors: %s\n", strerror(errno));
Am I doing something wrong?
Yes. You are not checking the return value of the kill()
system call. kill()
does not set errno to any particular value in the successful case.
Try this:
if(kill(g_StatusInstance[i].pid, 0) == -1) {
log_info_console( "Checking process for errors: %s\n", strerror(errno));
} else {
log_info_console( "kill returned 0, process still alive\n" );
}
More generally, you ought to check the return value of every system call or library call, unless it is declared to return void
.
Другие советы
Based on the discussion, your question is likely "Why did my kill()
not generate the effect that I expected?"
In order to understand why that is happening, you should first try strace
on the process which is the target of the kill()
. Attach it to your existing process by pid
or invoke it under strace
. strace
will show modifications to the signal mask and indicate when signals arrive. If your signal is arriving, you should debug the process targeted by the kill()
and try to understand what the installed/default signal handler is expected to do.