1f5ff0f7cSBrian Somers /*- 2f5ff0f7cSBrian Somers * Copyright (c) 1997 3f5ff0f7cSBrian Somers * Brian Somers <brian@awfulhak.demon.co.uk>. All rights reserved. 4f5ff0f7cSBrian Somers * 5f5ff0f7cSBrian Somers * Redistribution and use in source and binary forms, with or without 6f5ff0f7cSBrian Somers * modification, are permitted provided that the following conditions 7f5ff0f7cSBrian Somers * are met: 8f5ff0f7cSBrian Somers * 1. Redistributions of source code must retain the above copyright 9f5ff0f7cSBrian Somers * notice, this list of conditions and the following disclaimer. 10f5ff0f7cSBrian Somers * 2. Redistributions in binary form must reproduce the above copyright 11f5ff0f7cSBrian Somers * notice, this list of conditions and the following disclaimer in the 12f5ff0f7cSBrian Somers * documentation and/or other materials provided with the distribution. 13f5ff0f7cSBrian Somers * 3. Neither the name of the University nor the names of its contributors 14f5ff0f7cSBrian Somers * may be used to endorse or promote products derived from this software 15f5ff0f7cSBrian Somers * without specific prior written permission. 16f5ff0f7cSBrian Somers * 17f5ff0f7cSBrian Somers * THIS SOFTWARE IS PROVIDED ``AS IS'' AND 18f5ff0f7cSBrian Somers * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19f5ff0f7cSBrian Somers * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20f5ff0f7cSBrian Somers * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21f5ff0f7cSBrian Somers * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22f5ff0f7cSBrian Somers * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23f5ff0f7cSBrian Somers * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24f5ff0f7cSBrian Somers * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25f5ff0f7cSBrian Somers * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26f5ff0f7cSBrian Somers * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27f5ff0f7cSBrian Somers * SUCH DAMAGE. 28f5ff0f7cSBrian Somers * 29f5ff0f7cSBrian Somers * $Id: sig.c,v 1.4 1997/02/25 14:05:10 brian Exp $ 30f5ff0f7cSBrian Somers * 31f5ff0f7cSBrian Somers * TODO: 32f5ff0f7cSBrian Somers * 33f5ff0f7cSBrian Somers */ 34f5ff0f7cSBrian Somers 35f5ff0f7cSBrian Somers #include <sys/cdefs.h> 36f5ff0f7cSBrian Somers #include "sig.h" 37f5ff0f7cSBrian Somers #include <sys/types.h> 38f5ff0f7cSBrian Somers #include <signal.h> 39f5ff0f7cSBrian Somers #include "mbuf.h" 40f5ff0f7cSBrian Somers #include "log.h" 41f5ff0f7cSBrian Somers 42f5ff0f7cSBrian Somers static caused[NSIG]; /* An array of pending signals */ 43f5ff0f7cSBrian Somers static sig_type handler[NSIG]; /* all start at SIG_DFL */ 44f5ff0f7cSBrian Somers 45f5ff0f7cSBrian Somers 46f5ff0f7cSBrian Somers /* Record a signal in the "caused" array */ 47f5ff0f7cSBrian Somers 48f5ff0f7cSBrian Somers static void signal_recorder(int sig) { 49f5ff0f7cSBrian Somers caused[sig-1]++; 50f5ff0f7cSBrian Somers } 51f5ff0f7cSBrian Somers 52f5ff0f7cSBrian Somers 53f5ff0f7cSBrian Somers /* 54f5ff0f7cSBrian Somers set up signal_recorder, and record handler as the function to ultimately 55f5ff0f7cSBrian Somers call in handle_signal() 56f5ff0f7cSBrian Somers */ 57f5ff0f7cSBrian Somers 58f5ff0f7cSBrian Somers sig_type pending_signal(int sig,sig_type fn) { 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 ?) */ 63f5ff0f7cSBrian Somers logprintf("Eeek! %s:%s: I must be out of date!\n",__FILE__,__LINE__); 64f5ff0f7cSBrian Somers return signal(sig,fn); 65f5ff0f7cSBrian Somers } 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 82f5ff0f7cSBrian Somers void handle_signals() { 83f5ff0f7cSBrian Somers int sig; 84f5ff0f7cSBrian Somers int got; 85f5ff0f7cSBrian Somers 86f5ff0f7cSBrian Somers do { 87f5ff0f7cSBrian Somers got = 0; 88f5ff0f7cSBrian Somers for (sig = 0; sig < NSIG; sig++) 89f5ff0f7cSBrian Somers if (caused[sig]) { 90f5ff0f7cSBrian Somers caused[sig]--; 91f5ff0f7cSBrian Somers got++; 92f5ff0f7cSBrian Somers (*handler[sig])(sig+1); 93f5ff0f7cSBrian Somers } 94f5ff0f7cSBrian Somers } while(got); 95f5ff0f7cSBrian Somers } 96