1c39934eaSBrian Somers /*- 2c39934eaSBrian Somers * Copyright (c) 1997 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 * 2674849645SBrian Somers * $Id: sig.c,v 1.11 1997/12/21 12:11:08 brian Exp $ 27f5ff0f7cSBrian Somers */ 28f5ff0f7cSBrian Somers 29f5ff0f7cSBrian Somers #include <sys/types.h> 3075240ed1SBrian Somers 31f5ff0f7cSBrian Somers #include <signal.h> 3275240ed1SBrian Somers 33b6e82f33SBrian Somers #include "command.h" 34f5ff0f7cSBrian Somers #include "mbuf.h" 35f5ff0f7cSBrian Somers #include "log.h" 36b6e82f33SBrian Somers #include "sig.h" 37f5ff0f7cSBrian Somers 3874849645SBrian Somers static int caused[NSIG]; /* An array of pending signals */ 39f5ff0f7cSBrian Somers static sig_type handler[NSIG]; /* all start at SIG_DFL */ 40f5ff0f7cSBrian Somers 41f5ff0f7cSBrian Somers 42f5ff0f7cSBrian Somers /* Record a signal in the "caused" array */ 43f5ff0f7cSBrian Somers 44944f7098SBrian Somers static void 45944f7098SBrian Somers signal_recorder(int sig) 46944f7098SBrian Somers { 47f5ff0f7cSBrian Somers caused[sig - 1]++; 48f5ff0f7cSBrian Somers } 49f5ff0f7cSBrian Somers 50f5ff0f7cSBrian Somers 51f5ff0f7cSBrian Somers /* 52944f7098SBrian Somers * Set up signal_recorder, and record handler as the function to ultimately 53944f7098SBrian Somers * call in handle_signal() 54f5ff0f7cSBrian Somers */ 55f5ff0f7cSBrian Somers 56944f7098SBrian Somers sig_type 57944f7098SBrian Somers pending_signal(int sig, sig_type fn) 58944f7098SBrian Somers { 59f5ff0f7cSBrian Somers sig_type Result; 60f5ff0f7cSBrian Somers 61f5ff0f7cSBrian Somers if (sig <= 0 || sig > NSIG) { 62f5ff0f7cSBrian Somers /* Oops - we must be a bit out of date (too many sigs ?) */ 63927145beSBrian Somers LogPrintf(LogALERT, "Eeek! %s:%s: I must be out of date!\n", 64927145beSBrian Somers __FILE__, __LINE__); 65f5ff0f7cSBrian Somers return signal(sig, fn); 66f5ff0f7cSBrian Somers } 67f5ff0f7cSBrian Somers Result = handler[sig - 1]; 68f5ff0f7cSBrian Somers if (fn == SIG_DFL || fn == SIG_IGN) { 69f5ff0f7cSBrian Somers signal(sig, fn); 70f5ff0f7cSBrian Somers handler[sig - 1] = (sig_type) 0; 71f5ff0f7cSBrian Somers } else { 72f5ff0f7cSBrian Somers handler[sig - 1] = fn; 73f5ff0f7cSBrian Somers signal(sig, signal_recorder); 74f5ff0f7cSBrian Somers } 75f5ff0f7cSBrian Somers caused[sig - 1] = 0; 76f5ff0f7cSBrian Somers return Result; 77f5ff0f7cSBrian Somers } 78f5ff0f7cSBrian Somers 79f5ff0f7cSBrian Somers 80f5ff0f7cSBrian Somers /* Call the handlers for any pending signals */ 81f5ff0f7cSBrian Somers 82944f7098SBrian Somers void 83944f7098SBrian Somers handle_signals() 84944f7098SBrian Somers { 85f5ff0f7cSBrian Somers int sig; 86f5ff0f7cSBrian Somers int got; 87f5ff0f7cSBrian Somers 88f5ff0f7cSBrian Somers do { 89f5ff0f7cSBrian Somers got = 0; 90f5ff0f7cSBrian Somers for (sig = 0; sig < NSIG; sig++) 91f5ff0f7cSBrian Somers if (caused[sig]) { 92f5ff0f7cSBrian Somers caused[sig]--; 93f5ff0f7cSBrian Somers got++; 94f5ff0f7cSBrian Somers (*handler[sig]) (sig + 1); 95f5ff0f7cSBrian Somers } 96f5ff0f7cSBrian Somers } while (got); 97f5ff0f7cSBrian Somers } 98