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.
This commit is contained in:
Brian Somers 1997-02-19 01:14:41 +00:00
parent 4d04269a37
commit 52cc0880db
7 changed files with 158 additions and 74 deletions

View File

@ -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

View File

@ -34,7 +34,7 @@
#include <sys/time.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#include "sig.h"
#include <sys/wait.h>
#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));

View File

@ -28,7 +28,7 @@
#include <paths.h>
#include <sys/time.h>
#include <termios.h>
#include <signal.h>
#include "sig.h"
#include <sys/wait.h>
#include <errno.h>
#include <netdb.h>
@ -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;

89
usr.sbin/ppp/sig.c Normal file
View File

@ -0,0 +1,89 @@
/*-
* Copyright (c) 1997
* Brian Somers <brian@awfulhak.demon.co.uk>. 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 <sys/types.h>
#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);
}

41
usr.sbin/ppp/sig.h Normal file
View File

@ -0,0 +1,41 @@
/*-
* Copyright (c) 1997
* Brian Somers <brian@awfulhak.demon.co.uk>. 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 <sys/signal.h>
/* 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));

View File

@ -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 *));

View File

@ -25,6 +25,7 @@
#include <sys/time.h>
#include <signal.h>
#include "timeout.h"
#include "sig.h"
#ifdef SIGALRM
#include <errno.h>
#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