1

My daemon (linux only) has the following signal handler:

static void signal_handler(int id, siginfo_t *si, void *context) {
    if (id == SIGTERM) {
        /* prevent suicide - see below */
        if (si->si_pid == getpid()) {
            printf("Warning: received SIGTERM from own process\n");
            return;
        }
        /* rest of code omitted */
    }
    /* rest of code omitted */
}

... which is installed like this in main():

struct sigaction sa;

memset(&sa, 0, sizeof(sa));

sa.sa_sigaction = &signal_handler;
sa.sa_flags = SA_SIGINFO;

sigaction(SIGINT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGHUP, &sa, NULL);

The reason for the suicide check in the signal handler is that from time to time (once in 4 weeks) my daemon terminated because it received a SIGTERM from itself.

I am unable to find the cause. The only single kill() call used in the program is this one:

int kill_wrapper(pid_t pid, int sig) {
    if (pid <= 0 || pid == getpid())
        return -1;

    return kill(pid, sig);
}

The code has no single raise() or abort() calls.

I wonder which possible (maybe external) reasons might exist that can cause this program to receive SIGTERM from itself under Linux ?

gollum
  • 2,472
  • 1
  • 18
  • 21
  • Fun problem! You could try to attach a debugger to observe what's happening. Or attach an `strace`. – fuz Jan 11 '16 at 19:44
  • What exactly does happen with the current code? Is the warning printed and the process continues running? – Ctx Jan 11 '16 at 20:07
  • @Ctx I don't know because until now only the warning was in the code and the handler signalled the termination event. The return call is not in production yet. So I don't know if the process still would get terminated. I guess you suspect the kernel? – gollum Jan 11 '16 at 20:12
  • @FUZxxl Sure but you saw the "once in 4 weeks" ;) – gollum Jan 11 '16 at 20:23

1 Answers1

1

See this discussion. The bottom line is that si_pid is meaningful in very few cases.

Community
  • 1
  • 1
user58697
  • 7,808
  • 1
  • 14
  • 28
  • Thanks for link. However, that would require someone deliberately modifying these values which is highly unlikely in the environment the daemon is running. – gollum Jan 11 '16 at 22:36
  • Although not helpful for my problem +1 for the knowledge I've gathered from your link. I'm giving https://github.com/nfedera/kernel-siglog a try and see if it can identify the culprit. – gollum Feb 23 '16 at 20:29