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