1

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?

JonH
  • 501
  • 7
  • 13
  • 25
  • What is interrupting? What do your signal handlers do? What does log_fatal_syscall() do? Something else is setting ENOENT... or did you really mean ESRCH? – pilcrow Oct 25 '12 at 18:30
  • Did you make sure `errno` is 0 before calling `kill`? – n. m. could be an AI Oct 25 '12 at 18:32
  • when i press ctrl c it calls the function that does this and basically there is a loop that loops through all the processes currently started by my program...then kills them – JonH Oct 25 '12 at 18:32
  • no i didnt set errno to anything before kill – JonH Oct 25 '12 at 18:33
  • "*It is returning ENOENT ... sometimes it returns EINTER*" - How do you know? I don't see you storing the result anywhere. A typical error with errno is not storinng a copy of it immediately. Subsequent library or system calls can set it if you don't examine it immediately. – Robᵩ Oct 25 '12 at 18:38
  • im sorry im doing a log_info_console( "Checking process for errors: %s\n", strerror(errno)); right after the second call to kill - will update question. – JonH Oct 25 '12 at 18:39
  • Also, you aren't checking the return value from `kill()`. `errno` is only valid if `kill()` returns `-1`. – Robᵩ Oct 25 '12 at 18:39
  • `kill(pid, 0)` only does error checking no signal is sent... – JonH Oct 25 '12 at 18:44
  • OK let me phrase it differently. Did you call any `errno`-setting function before `kill`, and if so, did yo check the result and set `errno` back to 0? – n. m. could be an AI Oct 25 '12 at 18:56
  • no - i updated the code in the question to include the call to strerror() - i called kill, the kill again to error check, then did the strerror... – JonH Oct 25 '12 at 18:59
  • "*`kill(pid, 0)` only does error checking*" - But you are ignoring the result of the error check! You must examine the return value from `kill()` to determine if an error occurred. – Robᵩ Oct 25 '12 at 19:00
  • 2
    If kill is returning 0, then the value of `errno` is essential indeterminate. It could be ENOENT, it could be EINTR, it could be anything. – Robᵩ Oct 25 '12 at 19:07

2 Answers2

4

Kill(pid, 0) seems to not set the error code correctly ... It is returning ENOENT... EINTR

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

Robᵩ
  • 163,533
  • 20
  • 239
  • 308
0

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.

Brian Cain
  • 14,403
  • 3
  • 50
  • 88