sig.c revision 34331
111894Speter/*-
29Sjkh * Copyright (c) 1997 Brian Somers <brian@Awfulhak.org>
311894Speter * All rights reserved.
411894Speter *
59Sjkh * Redistribution and use in source and binary forms, with or without
611894Speter * modification, are permitted provided that the following conditions
79Sjkh * are met:
811894Speter * 1. Redistributions of source code must retain the above copyright
911894Speter *    notice, this list of conditions and the following disclaimer.
1011894Speter * 2. Redistributions in binary form must reproduce the above copyright
1111894Speter *    notice, this list of conditions and the following disclaimer in the
129Sjkh *    documentation and/or other materials provided with the distribution.
1311894Speter *
1411894Speter * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1511894Speter * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1611894Speter * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
179Sjkh * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1811894Speter * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1911894Speter * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2011894Speter * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2111894Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
229Sjkh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2311894Speter * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
249Sjkh * SUCH DAMAGE.
2511894Speter *
269Sjkh *	$Id: sig.c,v 1.11 1997/12/21 12:11:08 brian Exp $
2711894Speter */
289Sjkh
2911894Speter#include <sys/types.h>
3011894Speter
3111894Speter#include <signal.h>
3211894Speter
3311894Speter#include "command.h"
3411894Speter#include "mbuf.h"
3511894Speter#include "log.h"
3611894Speter#include "sig.h"
3711894Speter
3811894Speterstatic int caused[NSIG];	/* An array of pending signals */
3911894Speterstatic sig_type handler[NSIG];	/* all start at SIG_DFL */
4011894Speter
419Sjkh
4211894Speter/* Record a signal in the "caused" array */
4311894Speter
4411894Speterstatic void
459Sjkhsignal_recorder(int sig)
4611894Speter{
479Sjkh  caused[sig - 1]++;
4811894Speter}
4950472Speter
509Sjkh
519Sjkh/*
5211894Speter * Set up signal_recorder, and record handler as the function to ultimately
539Sjkh * call in handle_signal()
5411894Speter*/
559Sjkh
5611894Spetersig_type
5711894Speterpending_signal(int sig, sig_type fn)
5811894Speter{
599Sjkh  sig_type Result;
609Sjkh
6111894Speter  if (sig <= 0 || sig > NSIG) {
6211894Speter    /* Oops - we must be a bit out of date (too many sigs ?) */
6311894Speter    LogPrintf(LogALERT, "Eeek! %s:%s: I must be out of date!\n",
6411894Speter	      __FILE__, __LINE__);
6511894Speter    return signal(sig, fn);
6611894Speter  }
6711894Speter  Result = handler[sig - 1];
6811894Speter  if (fn == SIG_DFL || fn == SIG_IGN) {
6911894Speter    signal(sig, fn);
7011894Speter    handler[sig - 1] = (sig_type) 0;
7111894Speter  } else {
7211894Speter    handler[sig - 1] = fn;
7311894Speter    signal(sig, signal_recorder);
7411894Speter  }
7511894Speter  caused[sig - 1] = 0;
769Sjkh  return Result;
779Sjkh}
7811894Speter
7911894Speter
8011894Speter/* Call the handlers for any pending signals */
8111894Speter
829Sjkhvoid
8311894Speterhandle_signals()
8411894Speter{
8511894Speter  int sig;
8611894Speter  int got;
8711894Speter
8811894Speter  do {
8911894Speter    got = 0;
9011894Speter    for (sig = 0; sig < NSIG; sig++)
9111894Speter      if (caused[sig]) {
9211894Speter	caused[sig]--;
9311894Speter	got++;
9411894Speter	(*handler[sig]) (sig + 1);
9511894Speter      }
9611894Speter  } while (got);
9711894Speter}
9811894Speter