Merge branch 'vendor/BIND' into bind_vendor2
[dragonfly.git] / usr.sbin / ppp / sig.c
1 /*-
2  * Copyright (c) 1997 - 1999, 2001 Brian Somers <brian@Awfulhak.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/usr.sbin/ppp/sig.c,v 1.15.2.1 2002/09/01 02:12:32 brian Exp $
27  * $DragonFly: src/usr.sbin/ppp/sig.c,v 1.4 2005/11/24 23:42:54 swildner Exp $
28  */
29
30 #include <sys/types.h>
31
32 #include <signal.h>
33
34 #include "log.h"
35 #include "sig.h"
36
37 static int caused[NSIG];        /* An array of pending signals */
38 static int necessary;           /* Anything set ? */
39 static sig_type handler[NSIG];  /* all start at SIG_DFL */
40
41
42 /*
43  * Record a signal in the "caused" array
44  *
45  * This function is the only thing actually called in signal context.  It
46  * records that a signal has been caused and that sig_Handle() should be
47  * called (in non-signal context) as soon as possible to process that
48  * signal.
49  */
50 static void
51 signal_recorder(int sig)
52 {
53   caused[sig - 1]++;
54   necessary = 1;
55 }
56
57
58 /*
59  * Set up signal_recorder to handle the given sig and record ``fn'' as
60  * the function to ultimately call in sig_Handle().  ``fn'' will not be
61  * called in signal context (as sig_Handle() is not called in signal
62  * context).
63  */
64 sig_type
65 sig_signal(int sig, sig_type fn)
66 {
67   sig_type Result;
68
69   if (sig <= 0 || sig >= NSIG) {
70     /* Oops - we must be a bit out of date (too many sigs ?) */
71     log_Printf(LogALERT, "Eeek! %s:%d: I must be out of date!\n",
72               __FILE__, __LINE__);
73     return signal(sig, fn);
74   }
75   Result = handler[sig - 1];
76   if (fn == SIG_DFL || fn == SIG_IGN) {
77     signal(sig, fn);
78     handler[sig - 1] = (sig_type) 0;
79   } else {
80     handler[sig - 1] = fn;
81     signal(sig, signal_recorder);
82   }
83   caused[sig - 1] = 0;
84   return Result;
85 }
86
87
88 /*
89  * Call the handlers for any pending signals
90  *
91  * This function is called from a non-signal context - in fact, it's
92  * called every time select() in DoLoop() returns - just in case
93  * select() returned due to a signal being recorded by signal_recorder().
94  */
95 int
96 sig_Handle(void)
97 {
98   int sig;
99   int got;
100   int result;
101
102   result = 0;
103   if (necessary) {
104     /* We've *probably* got something in `caused' set */
105     necessary = 0;
106     /* `necessary' might go back to 1 while we're in here.... */
107     do {
108       got = 0;
109       for (sig = 0; sig < NSIG; sig++)
110         if (caused[sig]) {
111           caused[sig]--;
112           got++;
113           result++;
114           (*handler[sig])(sig + 1);
115         }
116     } while (got);
117   }
118
119   return result;
120 }