/*

$Id: signal.c,v 1.2 94/02/18 03:40:55 bert Exp $

*/

#include "extern.h"
#include "proto.h"
#include <signal.h>
#include <sys/wait.h>

/*** SIGNAL HANDLERS ***/

/*
 *  print a notice and exit
 */
void sig_exit(sig)
int sig;
{
  if (sig != SIGINT)
    SYSLOG(LOG_NOTICE,"caught signal %d, exiting", sig);
  exit(10);
}

/*
 *  remove zombie children
 */
void sig_child(sig)
int sig;
{
  int pid, stat, z;
  while ((pid = waitpid(-1, &stat, WNOHANG)) > 0)
#ifdef DEBUG
  {
    if (WIFEXITED(stat)) {
      if ((z = WEXITSTATUS(stat)) != 0)
	SYSLOG(LOG_DEBUG,"child %d exited with status %d", pid, z);
    } else if (WIFSIGNALED(stat))
      SYSLOG(LOG_DEBUG,"child %d terminated by signal %d", pid,WTERMSIG(stat));
    else
      SYSLOG(LOG_DEBUG,"child %d did something weird (%x)", pid, stat);
  }
#endif
  ;
}
/*** **************** ***/

/*
 *  install signal handlers
 */
void deal_with_signals(void)
{
  signal(SIGHUP, sig_exit);
  signal(SIGINT, sig_exit);
  signal(SIGCHLD, sig_child);
}

