From 52cc0880db973dfc6b13a1ea982239e7a023f964 Mon Sep 17 00:00:00 2001 From: Brian Somers Date: Wed, 19 Feb 1997 01:14:41 +0000 Subject: [PATCH] Tidy up signal handling. All signal() calls have been changed to pending_signal() calls. pending_signal() is defined in the new sig.c file. It remembers the handler and traps the signal with a function that will remember the signal. main.c now calls handle_signals() to actually call the required handlers (if the above handler was called). If this doesn't close PR2662 (was PR2347), I'll cry. Joerg, I think this should go into 2.2, but I havn't done anything about it because I'm bound to botch it with the new sig.[ch] files. I've just "cvs add"'d sig.[ch] so far.... can you update to 2.2 and tell me what you did ? Thanks. --- usr.sbin/ppp/Makefile | 2 +- usr.sbin/ppp/chat.c | 12 +++--- usr.sbin/ppp/main.c | 58 +++++++++------------------ usr.sbin/ppp/sig.c | 89 ++++++++++++++++++++++++++++++++++++++++++ usr.sbin/ppp/sig.h | 41 +++++++++++++++++++ usr.sbin/ppp/timeout.h | 1 - usr.sbin/ppp/timer.c | 29 ++------------ 7 files changed, 158 insertions(+), 74 deletions(-) create mode 100644 usr.sbin/ppp/sig.c create mode 100644 usr.sbin/ppp/sig.h diff --git a/usr.sbin/ppp/Makefile b/usr.sbin/ppp/Makefile index 4e22fb2170fc..1423624120f2 100644 --- a/usr.sbin/ppp/Makefile +++ b/usr.sbin/ppp/Makefile @@ -5,7 +5,7 @@ SRCS= async.c auth.c ccp.c chap.c chat.c command.c filter.c fsm.c hdlc.c \ ip.c ipcp.c lcp.c lqr.c log.c main.c mbuf.c modem.c os.c \ pap.c pred.c route.c slcompress.c timer.c systems.c uucplock.c vars.c \ vjcomp.c arp.c alias.c alias_db.c alias_ftp.c alias_util.c \ - passwdauth.c + passwdauth.c sig.c #CFLAGS+= -DHAVE_SHELL_CMD_WITH_ANY_MODE CFLAGS += -Wall -DUSE_PERROR -DMSEXT -DPASSWDAUTH LDADD += -lmd -lcrypt -lutil diff --git a/usr.sbin/ppp/chat.c b/usr.sbin/ppp/chat.c index 29f989ccb934..25a33d328284 100644 --- a/usr.sbin/ppp/chat.c +++ b/usr.sbin/ppp/chat.c @@ -34,7 +34,7 @@ #include #include #include -#include +#include "sig.h" #include #include "timeout.h" #include "vars.h" @@ -402,16 +402,16 @@ char *command, *out; pipe(fids); pid = fork(); if (pid == 0) { - signal(SIGINT, SIG_DFL); - signal(SIGQUIT, SIG_DFL); - signal(SIGTERM, SIG_DFL); - signal(SIGHUP, SIG_DFL); + pending_signal(SIGINT, SIG_DFL); + pending_signal(SIGQUIT, SIG_DFL); + pending_signal(SIGTERM, SIG_DFL); + pending_signal(SIGHUP, SIG_DFL); close(fids[0]); dup2(fids[1], 1); close(fids[1]); nb = open("/dev/tty", O_RDWR); dup2(nb, 0); -LogPrintf(LOG_CHAT_BIT, "exec: %s\n", command); + LogPrintf(LOG_CHAT_BIT, "exec: %s\n", command); /* switch back to original privileges */ if (setgid(getgid()) < 0) { LogPrintf(LOG_CHAT_BIT, "setgid: %s\n", strerror(errno)); diff --git a/usr.sbin/ppp/main.c b/usr.sbin/ppp/main.c index 5743be0d2a1c..08354e4ed26e 100644 --- a/usr.sbin/ppp/main.c +++ b/usr.sbin/ppp/main.c @@ -28,7 +28,7 @@ #include #include #include -#include +#include "sig.h" #include #include #include @@ -211,8 +211,8 @@ int signo; static void TerminalCont() { - (void)signal(SIGCONT, SIG_DFL); - (void)signal(SIGTSTP, TerminalStop); + pending_signal(SIGCONT, SIG_DFL); + pending_signal(SIGTSTP, TerminalStop); TtyCommandMode(getpgrp() == tcgetpgrp(0)); } @@ -220,9 +220,9 @@ static void TerminalStop(signo) int signo; { - (void)signal(SIGCONT, TerminalCont); + pending_signal(SIGCONT, TerminalCont); TtyOldMode(); - signal(SIGTSTP, SIG_DFL); + pending_signal(SIGTSTP, SIG_DFL); kill(getpid(), signo); } @@ -346,29 +346,29 @@ char **argv; tcgetattr(0, &oldtio); /* Save original tty mode */ - signal(SIGHUP, Hangup); - signal(SIGTERM, CloseSession); - signal(SIGINT, CloseSession); - signal(SIGQUIT, CloseSession); + pending_signal(SIGHUP, Hangup); + pending_signal(SIGTERM, CloseSession); + pending_signal(SIGINT, CloseSession); + pending_signal(SIGQUIT, CloseSession); #ifdef SIGSEGV - signal(SIGSEGV, Hangup); + pending_signal(SIGSEGV, Hangup); #endif #ifdef SIGPIPE - signal(SIGPIPE, Hangup); + pending_signal(SIGPIPE, Hangup); #endif #ifdef SIGALRM - signal(SIGALRM, SIG_IGN); + pending_signal(SIGALRM, SIG_IGN); #endif if(mode & MODE_INTER) { #ifdef SIGTSTP - signal(SIGTSTP, TerminalStop); + pending_signal(SIGTSTP, TerminalStop); #endif #ifdef SIGTTIN - signal(SIGTTIN, TerminalStop); + pending_signal(SIGTTIN, TerminalStop); #endif #ifdef SIGTTOU - signal(SIGTTOU, SIG_IGN); + pending_signal(SIGTTOU, SIG_IGN); #endif } @@ -791,16 +791,7 @@ DoLoop() usleep(TICKUNIT); TimerService(); #else - if( TimerServiceRequest > 0 ) { -#ifdef DEBUG - logprintf( "Invoking TimerService before select()\n" ); -#endif - /* Maybe a bit cautious.... */ - TimerServiceRequest = -1; - TimerService(); - TimerServiceRequest = 0; - continue; - } + handle_signals(); #endif /* If there are aren't many packets queued, look for some more. */ @@ -834,23 +825,10 @@ DoLoop() continue; } - if( TimerServiceRequest > 0 ) { - /* we want to service any SIGALRMs even if we got it before calling - select. */ - int rem_errno = errno; -#ifdef DEBUG - logprintf( "Invoking TimerService\n" ); -#endif - /* Maybe a bit cautious.... */ - TimerServiceRequest = -1; - TimerService(); - TimerServiceRequest = 0; - errno = rem_errno; - } - if ( i < 0 ) { if ( errno == EINTR ) { - continue; /* Got a signal - should have been dealt with */ + handle_signals(); + continue; } perror("select"); break; diff --git a/usr.sbin/ppp/sig.c b/usr.sbin/ppp/sig.c new file mode 100644 index 000000000000..87a8c4e674ab --- /dev/null +++ b/usr.sbin/ppp/sig.c @@ -0,0 +1,89 @@ +/*- + * Copyright (c) 1997 + * Brian Somers . All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + * + * TODO: + * + */ + +#include "sig.h" +#include +#include "mbuf.h" +#include "log.h" + +#define __MAXSIG (32) /* Sizeof u_long: Make life convenient.... */ +static u_long caused; /* A mask of pending signals */ +static __sighandler_t *handler[ __MAXSIG ]; /* all start at SIG_DFL */ + + +/* Record a signal in the "caused" mask */ + +static void signal_recorder(int sig) { + if (sig > 0 && sig <= __MAXSIG) + caused |= (1<<(sig-1)); +} + + +/* + set up signal_recorder, and record handler as the function to ultimately + call in handle_signal() +*/ + +__sighandler_t *pending_signal(int sig,__sighandler_t *fn) { + __sighandler_t *Result; + + if (sig <= 0 || sig > __MAXSIG) { + /* Oops - we must be a bit out of date (too many sigs ?) */ + logprintf("Eeek! %s:%s: I must be out of date!\n",__FILE__,__LINE__); + return signal(sig,fn); + } + + Result = handler[sig-1]; + if (fn == SIG_DFL || fn == SIG_IGN) { + handler[sig-1] = (__sighandler_t *)0; + signal(sig,fn); + } else { + handler[sig-1] = fn; + signal(sig,signal_recorder); + } + caused &= ~(1<<(sig-1)); + return Result; +} + + +/* Call the handlers for any pending signals */ + +void handle_signals() { + int sig; + + if (caused) + for (sig=0; sig<__MAXSIG; sig++, caused>>=1) + if (caused&1) + (*handler[sig])(sig+1); +} diff --git a/usr.sbin/ppp/sig.h b/usr.sbin/ppp/sig.h new file mode 100644 index 000000000000..d57c97e38afa --- /dev/null +++ b/usr.sbin/ppp/sig.h @@ -0,0 +1,41 @@ +/*- + * Copyright (c) 1997 + * Brian Somers . All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + * + * TODO: + * + */ + +#include + +/* Call this instead of signal() */ +extern __sighandler_t *pending_signal __P((int, __sighandler_t *)); + +/* Call this when you want things to *actually* happen */ +extern void handle_signals __P((void)); diff --git a/usr.sbin/ppp/timeout.h b/usr.sbin/ppp/timeout.h index afba12aef8f5..1da38ef84c2f 100644 --- a/usr.sbin/ppp/timeout.h +++ b/usr.sbin/ppp/timeout.h @@ -42,7 +42,6 @@ struct pppTimer { #define TIMER_EXPIRED 2 struct pppTimer *TimerList; -extern int TimerServiceRequest; /* If this is >0, call TimerService() */ extern void StartTimer __P((struct pppTimer *)); extern void StopTimer __P((struct pppTimer *)); diff --git a/usr.sbin/ppp/timer.c b/usr.sbin/ppp/timer.c index b9f0acf2abb0..66d714112477 100644 --- a/usr.sbin/ppp/timer.c +++ b/usr.sbin/ppp/timer.c @@ -25,6 +25,7 @@ #include #include #include "timeout.h" +#include "sig.h" #ifdef SIGALRM #include #endif @@ -138,24 +139,6 @@ struct pppTimer *tp; tp->state = TIMER_STOPPED; } -/* - This is used to decide at the top level if it's time for a TimerService() - call. This'll work fine as long as select() is interrupted by the - SIGALRM. -*/ -int TimerServiceRequest = 0; - -void -SetTimerServiceRequest( int Sig ) -{ - /* Maybe a bit cautious.... */ - if( TimerServiceRequest >= 0 ) - TimerServiceRequest++; -#ifdef DEBUG - logprintf( "Setting TimerServiceRequest\n" ); -#endif -} - void TimerService() { @@ -287,13 +270,7 @@ void usleep( u_int usec) void InitTimerService( void ) { struct itimerval itimer; - /* - Let's not do this - it's a bit dangerous (potential recursion into the - likes of malloc() etc. - - signal(SIGALRM, (void (*)(int))TimerService); - */ - signal(SIGALRM, SetTimerServiceRequest); + pending_signal(SIGALRM, (void (*)(int))TimerService); itimer.it_interval.tv_sec = itimer.it_value.tv_sec = 0; itimer.it_interval.tv_usec = itimer.it_value.tv_usec = TICKUNIT; setitimer(ITIMER_REAL, &itimer, NULL); @@ -309,6 +286,6 @@ void TermTimerService( void ) { * Notes: after disabling timer here, we will get one * SIGALRM will be got. */ - signal(SIGALRM, SIG_IGN); + pending_signal(SIGALRM, SIG_IGN); } #endif