sig.c revision 30715
1/*
2 * $Id: $
3 */
4
5#include <sys/types.h>
6
7#include <signal.h>
8
9#include "sig.h"
10#include "mbuf.h"
11#include "log.h"
12
13static caused[NSIG];		/* An array of pending signals */
14static sig_type handler[NSIG];	/* all start at SIG_DFL */
15
16
17/* Record a signal in the "caused" array */
18
19static void
20signal_recorder(int sig)
21{
22  caused[sig - 1]++;
23}
24
25
26/*
27 * Set up signal_recorder, and record handler as the function to ultimately
28 * call in handle_signal()
29*/
30
31sig_type
32pending_signal(int sig, sig_type fn)
33{
34  sig_type Result;
35
36  if (sig <= 0 || sig > NSIG) {
37    /* Oops - we must be a bit out of date (too many sigs ?) */
38    LogPrintf(LogALERT, "Eeek! %s:%s: I must be out of date!\n",
39	      __FILE__, __LINE__);
40    return signal(sig, fn);
41  }
42  Result = handler[sig - 1];
43  if (fn == SIG_DFL || fn == SIG_IGN) {
44    signal(sig, fn);
45    handler[sig - 1] = (sig_type) 0;
46  } else {
47    handler[sig - 1] = fn;
48    signal(sig, signal_recorder);
49  }
50  caused[sig - 1] = 0;
51  return Result;
52}
53
54
55/* Call the handlers for any pending signals */
56
57void
58handle_signals()
59{
60  int sig;
61  int got;
62
63  do {
64    got = 0;
65    for (sig = 0; sig < NSIG; sig++)
66      if (caused[sig]) {
67	caused[sig]--;
68	got++;
69	(*handler[sig]) (sig + 1);
70      }
71  } while (got);
72}
73