From 5c5c3ffeb213c6c32be4a78651bf2f82689e9df4 Mon Sep 17 00:00:00 2001 From: sef Date: Sun, 13 Jul 1997 06:16:44 +0000 Subject: Fix a problem introduced with a recent change that caused a hang with unreachable hosts. Note that most of this consists of telling SIGINT and SIGALRM to interrupt the system call, instead of restarting them. Also try to get rid of some potential races Bruce didn't like; hopefully they aren't a problem (potential or otherwise) now. Reviewed by: julian --- sbin/ping/ping.c | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) (limited to 'sbin') diff --git a/sbin/ping/ping.c b/sbin/ping/ping.c index 5f89641..5479d64 100644 --- a/sbin/ping/ping.c +++ b/sbin/ping/ping.c @@ -45,7 +45,7 @@ static const char copyright[] = static char sccsid[] = "@(#)ping.c 8.1 (Berkeley) 6/5/93"; */ static const char rcsid[] = - "$Id: ping.c,v 1.22 1997/07/09 19:40:43 julian Exp $"; + "$Id: ping.c,v 1.23 1997/07/09 20:33:58 julian Exp $"; #endif /* not lint */ /* @@ -423,16 +423,25 @@ main(argc, argv) else (void)printf("PING %s: %d data bytes\n", hostname, datalen); - (void)signal(SIGINT, stopit); - (void)signal(SIGALRM, catcher); - /* - * Use sigaction instead of signal() to get unambiguous semantics - * for SIGINFO, in particular with SA_RESTART not set. + * Use sigaction() instead of signal() to get unambiguous semantics, + * in particular with SA_RESTART not set. */ - si_sa.sa_handler = status; + sigemptyset(&si_sa.sa_mask); si_sa.sa_flags = 0; + + si_sa.sa_handler = stopit; + if (sigaction(SIGINT, &si_sa, 0) == -1) { + err(EX_OSERR, "sigaction SIGINT"); + } + + si_sa.sa_handler = catcher; + if (sigaction(SIGALRM, &si_sa, 0) == -1) { + err(EX_OSERR, "sigaction SIGALRM"); + } + + si_sa.sa_handler = status; if (sigaction(SIGINFO, &si_sa, 0) == -1) { err(EX_OSERR, "sigaction"); } @@ -508,11 +517,12 @@ static void catcher(int sig) { int waittime; + struct sigaction si_sa; pinger(); - (void)signal(SIGALRM, catcher); + if (!npackets || ntransmitted < npackets) - alarm((u_int)interval); + (void)alarm((u_int)interval); else { if (nreceived) { waittime = 2 * tmax / 1000; @@ -520,7 +530,14 @@ catcher(int sig) waittime = 1; } else waittime = MAXWAIT; - (void)signal(SIGALRM, stopit); + + si_sa.sa_handler = stopit; + sigemptyset(&si_sa.sa_mask); + si_sa.sa_flags = 0; + if (sigaction(SIGALRM, &si_sa, 0) == -1) { + finish_up = 1; + return; + } (void)alarm((u_int)waittime); } } @@ -889,6 +906,7 @@ finish(int sig) struct termios ts; (void)signal(SIGINT, SIG_IGN); + (void)signal(SIGALRM, SIG_IGN); (void)putchar('\n'); (void)fflush(stdout); (void)printf("--- %s ping statistics ---\n", hostname); -- cgit v1.1