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 ?