sig.c revision 31921
118334Speter/*-
218334Speter * Copyright (c) 1997 Brian Somers <brian@Awfulhak.org>
318334Speter * All rights reserved.
418334Speter *
518334Speter * Redistribution and use in source and binary forms, with or without
618334Speter * modification, are permitted provided that the following conditions
718334Speter * are met:
818334Speter * 1. Redistributions of source code must retain the above copyright
918334Speter *    notice, this list of conditions and the following disclaimer.
1018334Speter * 2. Redistributions in binary form must reproduce the above copyright
1118334Speter *    notice, this list of conditions and the following disclaimer in the
1218334Speter *    documentation and/or other materials provided with the distribution.
1318334Speter *
1418334Speter * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1518334Speter * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1618334Speter * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17132718Skan * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18132718Skan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19132718Skan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20132718Skan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21132718Skan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22132718Skan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23132718Skan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24132718Skan * SUCH DAMAGE.
25132718Skan *
26132718Skan *	$Id$
27132718Skan */
28132718Skan
29#include <sys/types.h>
30
31#include <signal.h>
32
33#include "command.h"
34#include "mbuf.h"
35#include "log.h"
36#include "sig.h"
37
38static caused[NSIG];		/* An array of pending signals */
39static sig_type handler[NSIG];	/* all start at SIG_DFL */
40
41
42/* Record a signal in the "caused" array */
43
44static void
45signal_recorder(int sig)
46{
47  caused[sig - 1]++;
48}
49
50
51/*
52 * Set up signal_recorder, and record handler as the function to ultimately
53 * call in handle_signal()
54*/
55
56sig_type
57pending_signal(int sig, sig_type fn)
58{
59  sig_type Result;
60
61  if (sig <= 0 || sig > NSIG) {
62    /* Oops - we must be a bit out of date (too many sigs ?) */
63    LogPrintf(LogALERT, "Eeek! %s:%s: I must be out of date!\n",
64	      __FILE__, __LINE__);
65    return signal(sig, fn);
66  }
67  Result = handler[sig - 1];
68  if (fn == SIG_DFL || fn == SIG_IGN) {
69    signal(sig, fn);
70    handler[sig - 1] = (sig_type) 0;
71  } else {
72    handler[sig - 1] = fn;
73    signal(sig, signal_recorder);
74  }
75  caused[sig - 1] = 0;
76  return Result;
77}
78
79
80/* Call the handlers for any pending signals */
81
82void
83handle_signals()
84{
85  int sig;
86  int got;
87
88  do {
89    got = 0;
90    for (sig = 0; sig < NSIG; sig++)
91      if (caused[sig]) {
92	caused[sig]--;
93	got++;
94	(*handler[sig]) (sig + 1);
95      }
96  } while (got);
97}
98