diff options
author | Theo de Raadt <deraadt@cvs.openbsd.org> | 2002-05-31 04:53:11 +0000 |
---|---|---|
committer | Theo de Raadt <deraadt@cvs.openbsd.org> | 2002-05-31 04:53:11 +0000 |
commit | 13f56b4ffe5f71c649ee9b9a09a2371d4ee5564a (patch) | |
tree | f2bd2befa11a091270e0354f8e2954a892df309d /sbin | |
parent | 715c735cdcbbc79564ca587ff30ce78bdc6a897c (diff) |
kill the signal races
Diffstat (limited to 'sbin')
-rw-r--r-- | sbin/ping/ping.c | 65 |
1 files changed, 43 insertions, 22 deletions
diff --git a/sbin/ping/ping.c b/sbin/ping/ping.c index 56a07f76c3a..37813e4db91 100644 --- a/sbin/ping/ping.c +++ b/sbin/ping/ping.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ping.c,v 1.51 2002/05/31 01:11:31 itojun Exp $ */ +/* $OpenBSD: ping.c,v 1.52 2002/05/31 04:53:10 deraadt Exp $ */ /* $NetBSD: ping.c,v 1.20 1995/08/11 22:37:58 cgd Exp $ */ /* @@ -47,7 +47,7 @@ static char copyright[] = #if 0 static char sccsid[] = "@(#)ping.c 8.1 (Berkeley) 6/5/93"; #else -static char rcsid[] = "$OpenBSD: ping.c,v 1.51 2002/05/31 01:11:31 itojun Exp $"; +static char rcsid[] = "$OpenBSD: ping.c,v 1.52 2002/05/31 04:53:10 deraadt Exp $"; #endif #endif /* not lint */ @@ -173,7 +173,7 @@ int reset_kerninfo; int bufspace = IP_MAXPACKET; void fill(char *, char *); -void catcher(), prtsig(), finish(), summary(int); +void catcher(), prtsig(), finish(), summary(int, int); int in_cksum(u_short *, int); void pinger(); char *pr_addr(in_addr_t); @@ -573,7 +573,7 @@ prtsig() { int save_errno = errno; - summary(0); + summary(0, 1); errno = save_errno; } @@ -589,8 +589,8 @@ void pinger() { struct icmp *icp; - int cc; - int i; + char buf[8192]; + int cc, i; char *packet = outpack; icp = (struct icmp *)outpack; @@ -632,8 +632,9 @@ pinger() if (i < 0 || i != cc) { if (i < 0) perror("ping: sendto"); - (void)printf("ping: wrote %s %d chars, ret=%d\n", + snprintf(buf, sizeof buf, "ping: wrote %s %d chars, ret=%d\n", hostname, cc, i); + write(STDOUT_FILENO, buf, strlen(buf)); } if (!(options & F_QUIET) && options & F_FLOOD) (void)write(STDOUT_FILENO, &DOT, 1); @@ -901,39 +902,59 @@ in_cksum(addr, len) } void -summary(header) - int header; +summary(header, sig) + int header, sig; { - (void)putchar('\r'); - (void)fflush(stdout); + char buf[8192], buft[8192]; + + buf[0] = '\0'; + + if (!sig) { + (void)putchar('\r'); + (void)fflush(stdout); + } else + strlcat(buf, "\r", sizeof buf); - if (header) - (void)printf("--- %s ping statistics ---\n", hostname); + if (header) { + snprintf(buft, sizeof buft, "--- %s ping statistics ---\n", + hostname); + strlcat(buf, buft, sizeof buf); + } + + snprintf(buft, sizeof buft, "%ld packets transmitted, ", ntransmitted); + strlcat(buf, buft, sizeof buf); + snprintf(buft, sizeof buft, "%ld packets received, ", nreceived); + strlcat(buf, buft, sizeof buf); - (void)printf("%ld packets transmitted, ", ntransmitted); - (void)printf("%ld packets received, ", nreceived); - if (nrepeats) - (void)printf("%ld duplicates, ", nrepeats); + if (nrepeats) { + snprintf(buft, sizeof buft, "%ld duplicates, ", nrepeats); + strlcat(buf, buft, sizeof buf); + } if (ntransmitted) { if (nreceived > ntransmitted) - (void)printf("-- somebody's duplicating packets!"); + snprintf(buft, sizeof buft, + "-- somebody's duplicating packets!"); else - (void)printf("%d%% packet loss", + snprintf(buft, sizeof buft, "%d%% packet loss", (int) (((ntransmitted - nreceived) * 100) / ntransmitted)); + strlcat(buf, buft, sizeof buf); } - (void)putchar('\n'); + strlcat(buf, "\n", sizeof buf); if (nreceived && timing) { quad_t num = nreceived + nrepeats; quad_t avg = tsum / num; quad_t dev = qsqrt(tsumsq / num - avg * avg); - (void)printf("round-trip min/avg/max/std-dev = " + + snprintf(buft, sizeof buft, "round-trip min/avg/max/std-dev = " "%d.%03d/%d.%03d/%d.%03d/%d.%03d ms\n", (int)(tmin / 1000), (int)(tmin % 1000), (int)(avg / 1000), (int)(avg % 1000), (int)(tmax / 1000), (int)(tmax % 1000), (int)(dev / 1000), (int)(dev % 1000)); + strlcat(buf, buft, sizeof buf); } + write(STDOUT_FILENO, buf, strlen(buf)); /* XXX atomicio? */ } quad_t @@ -963,7 +984,7 @@ finish() { (void)signal(SIGINT, SIG_IGN); - summary(1); + summary(1, 0); exit(nreceived ? 0 : 1); } |