in reply to Why this daemon can only receive the HUP signal no more than one time?

Do signal handlers persist throughout an exec? My browsing of man 2 execve makes me think that they don't. You may have to reinstate the HUP handler after your exec.

  • Comment on Re: Why this daemon can only receive the HUP signal no more than one time?
  • Download Code

Replies are listed 'Best First'.
Re^2: Why this daemon can only receive the HUP signal no more than one time?
by sunshine_august (Scribe) on Feb 23, 2009 at 07:21 UTC

    I did reinstall the HUP signal handler at the head of my script.

    I think the line  exec "$exeFile"; in relaunch subroutine should execute the whole script again, and also should have execute the  $SIG{HUP}      = \&doSigHup; at the head of the script. This line will reinstall the HUP signal handler.

      That's correct. First (FWIW) I can confirm your observation (SuSE 11.1, 2.6.27.7-9-pae, perl 5.10.0).

      The problem seems to be that your daemon exec's from within the signal-handler (presumably inheriting a blocked signal mask). After applying a quick and dirty work-around, the program reloaded as expected multiple times. The work-around was to set a global variable $PLEASE_RELOAD=1; (initialised to 0) within the HUP-handler and to incorporate something like relaunch( $runDir, $pidFile, $exeFile ) if $PLEASE_RELOAD; into hupMain() allowing to exec outside the HUP signal handler.

      This is ugly, but it seems to locate the problem somewhere near the HUP handler...
      Another hint: you used a variable called $parentPid to save the children/daemons PID.

      Update: Maybe this snipped from perlipc provides a useful hint?

      # POSIX unmasks the sigprocmask properly my $sigset = POSIX::SigSet->new(); my $action = POSIX::SigAction->new('sigHUP_handler', $sigset, &POSIX::SA_NODEFER); POSIX::sigaction(&POSIX::SIGHUP, $action); sub sigHUP_handler { print "got SIGHUP\n"; exec($SELF, @ARGV) or die "Couldn't restart: $!\n"; }
      Update: Hm, forget to note: SIGHUPSIGTERM (Argl. Sorry, confused that, AM is right, see below.) is send by the OS on shutdown (usually followed by SIGKILL a little bit later) - this would relaunch your daemon during shutdown. Maybe it is a better idea to use SIGUSR1 to reload the daemon?

        the OS doesn't send SIGHUP on shutdown. Many daemons use SIGHUP for reloading config. The OS on shutdown will send SIGTERM and then follow with SIGKILL if needed.