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