1 /*- 2 * Copyright (c) 1997 - 1999, 2001 Brian Somers <brian (at) 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.18.40.1 2010/12/21 17:10:29 kensmith Exp $ 27 */ 28 29 #include <sys/types.h> 30 31 #include <signal.h> 32 33 #include "log.h" 34 #include "sig.h" 35 36 static int caused[NSIG]; /* An array of pending signals */ 37 static int necessary; /* Anything set ? */ 38 static sig_type handler[NSIG]; /* all start at SIG_DFL */ 39 40 41 /* 42 * Record a signal in the "caused" array 43 * 44 * This function is the only thing actually called in signal context. It 45 * records that a signal has been caused and that sig_Handle() should be 46 * called (in non-signal context) as soon as possible to process that 47 * signal. 48 */ 49 static void 50 signal_recorder(int sig) 51 { 52 caused[sig - 1]++; 53 necessary = 1; 54 } 55 56 57 /* 58 * Set up signal_recorder to handle the given sig and record ``fn'' as 59 * the function to ultimately call in sig_Handle(). ``fn'' will not be 60 * called in signal context (as sig_Handle() is not called in signal 61 * context). 62 */ 63 sig_type 64 sig_signal(int sig, sig_type fn) 65 { 66 sig_type Result; 67 68 if (sig <= 0 || sig > NSIG) { 69 /* Oops - we must be a bit out of date (too many sigs ?) */ 70 log_Printf(LogALERT, "Eeek! %s:%d: I must be out of date!\n", 71 __FILE__, __LINE__); 72 return signal(sig, fn); 73 } 74 Result = handler[sig - 1]; 75 if (fn == SIG_DFL || fn == SIG_IGN) { 76 signal(sig, fn); 77 handler[sig - 1] = (sig_type) 0; 78 } else { 79 handler[sig - 1] = fn; 80 signal(sig, signal_recorder); 81 } 82 caused[sig - 1] = 0; 83 return Result; 84 } 85 86 87 /* 88 * Call the handlers for any pending signals 89 * 90 * This function is called from a non-signal context - in fact, it's 91 * called every time select() in DoLoop() returns - just in case 92 * select() returned due to a signal being recorded by signal_recorder(). 93 */ 94 int 95 sig_Handle() 96 { 97 int sig; 98 int got; 99 int result; 100 101 result = 0; 102 if (necessary) { 103 /* We've *probably* got something in `caused' set */ 104 necessary = 0; 105 /* `necessary' might go back to 1 while we're in here.... */ 106 do { 107 got = 0; 108 for (sig = 0; sig < NSIG; sig++) 109 if (caused[sig]) { 110 caused[sig]--; 111 got++; 112 result++; 113 (*handler[sig])(sig + 1); 114 } 115 } while (got); 116 } 117 118 return result; 119 } 120