/*
 * signals.c
 * File Revision 0
 * Signal handling in http server.
 * (c) 2000 Jacob Lundberg, jacob@chaos2.org
 */


/*
 * 2000.11.12	Initial implementation
 */


#include "server.h"


/* Location of symbols for DBG_PRN. */
#define  WHENCE                 "signals.c"


void handle_sigint(int signo) {
/*
 * handle_sigint()
 * We'll exit on ^C methinks.
 */

   DBG_PRN("handle_sigint: signo = %i\n", WHENCE, signo);

   do_exit(1);

}


void handle_sigquit(int signo) {
/*
 * handle_sigquit()
 * Close everything down nice'n'orderly.
 */

   DBG_PRN("handle_sigquit: signo = %i\n", WHENCE, signo);

   do_exit(0);

}


void handle_signals(void) {
/*
 * handle_signals()
 * Arrange to trap the signals we need.
 * We can check sv_fork_level to see if we're parent or child.
 */

   DBG_PRN("handle_signals: sv_fork_level = %i\n", WHENCE, sv_fork_level);

   /* Kill children and reap zombies on 2 (child and parent). */
   signal(SIGINT, handle_sigint);

   /* Parent terminates the listen socket and waits for children on 3. */
   if(sv_fork_level == 0)
      signal(SIGQUIT, handle_sigquit);
   /* Child ignores 3 and completes its current transaction. */
   else
      signal(SIGQUIT, SIG_IGN);

   /* Parent terminates the listen socket and waits for children on 15. */
   if(sv_fork_level == 0)
      signal(SIGTERM, handle_sigquit);
   /* Child ignores 15 and completes its current transaction. */
   else
      signal(SIGTERM, SIG_IGN);

}


void ignore_signals(void) {
/*
 * ignore_signals()
 * During the exit sequence we ignore signals.
 */

   DBG_PRN("ignore_signals\n", WHENCE);

   signal(SIGINT, SIG_IGN);
   signal(SIGQUIT, SIG_IGN);
   signal(SIGTERM, SIG_IGN);

}

