1c39934eaSBrian Somers /*- 2428217f7SBrian Somers * Copyright (c) 1997 - 1999, 2001 Brian Somers <brian@Awfulhak.org> 3c39934eaSBrian Somers * All rights reserved. 4c39934eaSBrian Somers * 5c39934eaSBrian Somers * Redistribution and use in source and binary forms, with or without 6c39934eaSBrian Somers * modification, are permitted provided that the following conditions 7c39934eaSBrian Somers * are met: 8c39934eaSBrian Somers * 1. Redistributions of source code must retain the above copyright 9c39934eaSBrian Somers * notice, this list of conditions and the following disclaimer. 10c39934eaSBrian Somers * 2. Redistributions in binary form must reproduce the above copyright 11c39934eaSBrian Somers * notice, this list of conditions and the following disclaimer in the 12c39934eaSBrian Somers * documentation and/or other materials provided with the distribution. 13c39934eaSBrian Somers * 14c39934eaSBrian Somers * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15c39934eaSBrian Somers * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16c39934eaSBrian Somers * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17c39934eaSBrian Somers * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18c39934eaSBrian Somers * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19c39934eaSBrian Somers * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20c39934eaSBrian Somers * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21c39934eaSBrian Somers * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22c39934eaSBrian Somers * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23c39934eaSBrian Somers * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24c39934eaSBrian Somers * SUCH DAMAGE. 25c39934eaSBrian Somers * 2697d92980SPeter Wemm * $FreeBSD$ 27f5ff0f7cSBrian Somers */ 28f5ff0f7cSBrian Somers 29b08bf2deSBrian Somers #include <sys/types.h> 30b08bf2deSBrian Somers 31f5ff0f7cSBrian Somers #include <signal.h> 3275240ed1SBrian Somers 33f5ff0f7cSBrian Somers #include "log.h" 34b6e82f33SBrian Somers #include "sig.h" 35f5ff0f7cSBrian Somers 3674849645SBrian Somers static int caused[NSIG]; /* An array of pending signals */ 37486105bcSBrian Somers static int necessary; /* Anything set ? */ 38f5ff0f7cSBrian Somers static sig_type handler[NSIG]; /* all start at SIG_DFL */ 39f5ff0f7cSBrian Somers 40f5ff0f7cSBrian Somers 41428217f7SBrian Somers /* 42428217f7SBrian Somers * Record a signal in the "caused" array 43428217f7SBrian Somers * 44428217f7SBrian Somers * This function is the only thing actually called in signal context. It 45428217f7SBrian Somers * records that a signal has been caused and that sig_Handle() should be 46428217f7SBrian Somers * called (in non-signal context) as soon as possible to process that 47428217f7SBrian Somers * signal. 48428217f7SBrian Somers */ 49944f7098SBrian Somers static void 50944f7098SBrian Somers signal_recorder(int sig) 51944f7098SBrian Somers { 52f5ff0f7cSBrian Somers caused[sig - 1]++; 53486105bcSBrian Somers necessary = 1; 54f5ff0f7cSBrian Somers } 55f5ff0f7cSBrian Somers 56f5ff0f7cSBrian Somers 57f5ff0f7cSBrian Somers /* 58428217f7SBrian Somers * Set up signal_recorder to handle the given sig and record ``fn'' as 59428217f7SBrian Somers * the function to ultimately call in sig_Handle(). ``fn'' will not be 60428217f7SBrian Somers * called in signal context (as sig_Handle() is not called in signal 61428217f7SBrian Somers * context). 62f5ff0f7cSBrian Somers */ 63944f7098SBrian Somers sig_type 64dd7e2610SBrian Somers sig_signal(int sig, sig_type fn) 65944f7098SBrian Somers { 66f5ff0f7cSBrian Somers sig_type Result; 67f5ff0f7cSBrian Somers 68f5ff0f7cSBrian Somers if (sig <= 0 || sig > NSIG) { 69f5ff0f7cSBrian Somers /* Oops - we must be a bit out of date (too many sigs ?) */ 70dd7e2610SBrian Somers log_Printf(LogALERT, "Eeek! %s:%d: I must be out of date!\n", 71927145beSBrian Somers __FILE__, __LINE__); 72f5ff0f7cSBrian Somers return signal(sig, fn); 73f5ff0f7cSBrian Somers } 74f5ff0f7cSBrian Somers Result = handler[sig - 1]; 75f5ff0f7cSBrian Somers if (fn == SIG_DFL || fn == SIG_IGN) { 76f5ff0f7cSBrian Somers signal(sig, fn); 77f5ff0f7cSBrian Somers handler[sig - 1] = (sig_type) 0; 78f5ff0f7cSBrian Somers } else { 79f5ff0f7cSBrian Somers handler[sig - 1] = fn; 80f5ff0f7cSBrian Somers signal(sig, signal_recorder); 81f5ff0f7cSBrian Somers } 82f5ff0f7cSBrian Somers caused[sig - 1] = 0; 83f5ff0f7cSBrian Somers return Result; 84f5ff0f7cSBrian Somers } 85f5ff0f7cSBrian Somers 86f5ff0f7cSBrian Somers 87428217f7SBrian Somers /* 88428217f7SBrian Somers * Call the handlers for any pending signals 89428217f7SBrian Somers * 90428217f7SBrian Somers * This function is called from a non-signal context - in fact, it's 91428217f7SBrian Somers * called every time select() in DoLoop() returns - just in case 92428217f7SBrian Somers * select() returned due to a signal being recorded by signal_recorder(). 93428217f7SBrian Somers */ 94486105bcSBrian Somers int 95dd7e2610SBrian Somers sig_Handle() 96944f7098SBrian Somers { 97f5ff0f7cSBrian Somers int sig; 98f5ff0f7cSBrian Somers int got; 99486105bcSBrian Somers int result; 100f5ff0f7cSBrian Somers 101486105bcSBrian Somers result = 0; 102486105bcSBrian Somers if (necessary) { 103486105bcSBrian Somers /* We've *probably* got something in `caused' set */ 104486105bcSBrian Somers necessary = 0; 105486105bcSBrian Somers /* `necessary' might go back to 1 while we're in here.... */ 106f5ff0f7cSBrian Somers do { 107f5ff0f7cSBrian Somers got = 0; 108f5ff0f7cSBrian Somers for (sig = 0; sig < NSIG; sig++) 109f5ff0f7cSBrian Somers if (caused[sig]) { 110f5ff0f7cSBrian Somers caused[sig]--; 111f5ff0f7cSBrian Somers got++; 112486105bcSBrian Somers result++; 113f5ff0f7cSBrian Somers (*handler[sig])(sig + 1); 114f5ff0f7cSBrian Somers } 115f5ff0f7cSBrian Somers } while (got); 116f5ff0f7cSBrian Somers } 117486105bcSBrian Somers 118486105bcSBrian Somers return result; 119486105bcSBrian Somers } 120