sig.c revision 28679
123840Sbrian/*-
223840Sbrian * Copyright (c) 1997
323840Sbrian *	Brian Somers <brian@awfulhak.demon.co.uk>.  All rights reserved.
423840Sbrian *
523840Sbrian * Redistribution and use in source and binary forms, with or without
623840Sbrian * modification, are permitted provided that the following conditions
723840Sbrian * are met:
823840Sbrian * 1. Redistributions of source code must retain the above copyright
923840Sbrian *    notice, this list of conditions and the following disclaimer.
1023840Sbrian * 2. Redistributions in binary form must reproduce the above copyright
1123840Sbrian *    notice, this list of conditions and the following disclaimer in the
1223840Sbrian *    documentation and/or other materials provided with the distribution.
1323840Sbrian * 3. Neither the name of the University nor the names of its contributors
1423840Sbrian *    may be used to endorse or promote products derived from this software
1523840Sbrian *    without specific prior written permission.
1623840Sbrian *
1723840Sbrian * THIS SOFTWARE IS PROVIDED ``AS IS'' AND
1823840Sbrian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1923840Sbrian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2023840Sbrian * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2123840Sbrian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2223840Sbrian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2323840Sbrian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2423840Sbrian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2523840Sbrian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2623840Sbrian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2723840Sbrian * SUCH DAMAGE.
2823840Sbrian *
2928679Sbrian * $Id: sig.c,v 1.7 1997/06/09 03:27:36 brian Exp $
3023840Sbrian *
3123840Sbrian *  TODO:
3223840Sbrian *
3323840Sbrian */
3423840Sbrian
3523840Sbrian#include "sig.h"
3623840Sbrian#include <sys/types.h>
3723840Sbrian#include <signal.h>
3823840Sbrian#include "mbuf.h"
3923840Sbrian#include "log.h"
4023840Sbrian
4123840Sbrianstatic caused[NSIG];		/* An array of pending signals */
4223840Sbrianstatic sig_type handler[NSIG];	/* all start at SIG_DFL */
4323840Sbrian
4423840Sbrian
4523840Sbrian/* Record a signal in the "caused" array */
4623840Sbrian
4728679Sbrianstatic void
4828679Sbriansignal_recorder(int sig)
4928679Sbrian{
5028679Sbrian  caused[sig - 1]++;
5123840Sbrian}
5223840Sbrian
5323840Sbrian
5423840Sbrian/*
5528679Sbrian * Set up signal_recorder, and record handler as the function to ultimately
5628679Sbrian * call in handle_signal()
5723840Sbrian*/
5823840Sbrian
5928679Sbriansig_type
6028679Sbrianpending_signal(int sig, sig_type fn)
6128679Sbrian{
6228679Sbrian  sig_type Result;
6323840Sbrian
6428679Sbrian  if (sig <= 0 || sig > NSIG) {
6528679Sbrian    /* Oops - we must be a bit out of date (too many sigs ?) */
6628679Sbrian    LogPrintf(LogALERT, "Eeek! %s:%s: I must be out of date!\n",
6728679Sbrian	      __FILE__, __LINE__);
6828679Sbrian    return signal(sig, fn);
6928679Sbrian  }
7028679Sbrian  Result = handler[sig - 1];
7128679Sbrian  if (fn == SIG_DFL || fn == SIG_IGN) {
7228679Sbrian    signal(sig, fn);
7328679Sbrian    handler[sig - 1] = (sig_type) 0;
7428679Sbrian  } else {
7528679Sbrian    handler[sig - 1] = fn;
7628679Sbrian    signal(sig, signal_recorder);
7728679Sbrian  }
7828679Sbrian  caused[sig - 1] = 0;
7928679Sbrian  return Result;
8023840Sbrian}
8123840Sbrian
8223840Sbrian
8323840Sbrian/* Call the handlers for any pending signals */
8423840Sbrian
8528679Sbrianvoid
8628679Sbrianhandle_signals()
8728679Sbrian{
8828679Sbrian  int sig;
8928679Sbrian  int got;
9023840Sbrian
9128679Sbrian  do {
9228679Sbrian    got = 0;
9328679Sbrian    for (sig = 0; sig < NSIG; sig++)
9428679Sbrian      if (caused[sig]) {
9528679Sbrian	caused[sig]--;
9628679Sbrian	got++;
9728679Sbrian	(*handler[sig]) (sig + 1);
9828679Sbrian      }
9928679Sbrian  } while (got);
10023840Sbrian}
101