2007-09-26 03:44:30

by John Z. Bohach

[permalink] [raw]
Subject: signals dropped when proc. run as daemon, but not when in the foreground

Hello,

I'm writing a system app. that becomes a daemon by the usual fork() ->
setsid() -> fork() method.

It then registers a signal handler for SIGCHLD via sigaction (SA_SIGINFO
style).

chldAction.sa_sigaction = sigChld;
sigemptyset(&(chldAction.sa_mask));
chldAction.sa_flags = SA_SIGINFO;
rc = sigaction(SIGCHLD, &chldAction, NULL);

Its a network service server, so it does the usual
socket->bind->listen->select->accept->fork sequence to handle a new
connection.

The child processes the connection, then exits. This should generate
the standard SIGCHLD signal to the parent, which catches it via the
sigaction with the following handler:

static void
sigChld(int signum, siginfo_t * siginfo, void * ucontext)
{
int childStat;

log_dbg("received signal %d\n", signum);

if (signum != SIGCHLD) /* technically can't happen here... */
return; /* but ignore it if it does */

if (waitpid(siginfo->si_pid, &childStat, 0) > 0)
{
if (WIFEXITED(childStat))
log_dbg("child %d WIFEXITED with childStat %d\n",
siginfo->si_pid, WEXITSTATUS(childStat));

if (WIFSIGNALED(childStat))
{
log_dbg("child %d WIFSIGNALED with childStat %d\n",
siginfo->si_pid, WTERMSIG(childStat));
}
}

return;
}

Here's the problem: when run as a daemon, as above, I don't get the
SIGCHLD. I've instrumented it to show the child exiting, and it does,
and it even becomes defunct (on account of the lack of waitpid from the
signal handler).

HOWEVER, if I run the program in the foreground, without
fork()->setsid()->fork() sequence, EVERYTHING is FINE!

I am going insane? This is on a 2.6.20.6 kernel, and I just can't see
that this would be a kernel bug, but I don't know if/what I'm doing
wrong myself?

Is there something I could do to test something...I've been at this for
a few days now and I'm out of ideas...

Thanks,
John