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