From 214f34f5a7acc5cdf45d13134224c2ecf9d43dfd Mon Sep 17 00:00:00 2001 From: deraadt Date: Tue, 30 Aug 2016 14:28:31 +0000 Subject: [PATCH] summary() is no longer called from a signal handler, so it can use stdio and does not need the workarounds. ok florian millert --- sbin/ping/ping.c | 38 ++++++++++---------------------------- sbin/ping6/ping6.c | 39 +++++++++++---------------------------- 2 files changed, 21 insertions(+), 56 deletions(-) diff --git a/sbin/ping/ping.c b/sbin/ping/ping.c index dc210c3a5f4..27773508020 100644 --- a/sbin/ping/ping.c +++ b/sbin/ping/ping.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ping.c,v 1.141 2016/08/30 13:58:12 millert Exp $ */ +/* $OpenBSD: ping.c,v 1.142 2016/08/30 14:28:31 deraadt Exp $ */ /* $NetBSD: ping.c,v 1.20 1995/08/11 22:37:58 cgd Exp $ */ /* @@ -1031,47 +1031,29 @@ in_cksum(u_short *addr, int len) void summary(void) { - char buf[8192], buft[8192]; + printf("\n--- %s ping statistics ---\n", hostname); + printf("%lld packets transmitted, ", ntransmitted); + printf("%lld packets received, ", nreceived); - /* XXX - safe to use fprintf here now */ - (void)putchar('\r'); - (void)fflush(stdout); - - snprintf(buft, sizeof buft, "--- %s ping statistics ---\n", - hostname); - strlcat(buf, buft, sizeof buf); - - snprintf(buft, sizeof buft, "%lld packets transmitted, ", ntransmitted); - strlcat(buf, buft, sizeof buf); - snprintf(buft, sizeof buft, "%lld packets received, ", nreceived); - strlcat(buf, buft, sizeof buf); - - if (nrepeats) { - snprintf(buft, sizeof buft, "%lld duplicates, ", nrepeats); - strlcat(buf, buft, sizeof buf); - } + if (nrepeats) + printf("%lld duplicates, ", nrepeats); if (ntransmitted) { if (nreceived > ntransmitted) - snprintf(buft, sizeof buft, - "-- somebody's duplicating packets!"); + printf("-- somebody's duplicating packets!"); else - snprintf(buft, sizeof buft, "%.1f%% packet loss", + printf("%.1f%% packet loss", ((((double)ntransmitted - nreceived) * 100) / ntransmitted)); - strlcat(buf, buft, sizeof buf); } - strlcat(buf, "\n", sizeof buf); + printf("\n"); if (timinginfo) { /* Only display average to microseconds */ double num = nreceived + nrepeats; double avg = tsum / num; double dev = sqrt(fmax(0, tsumsq / num - avg * avg)); - snprintf(buft, sizeof(buft), - "round-trip min/avg/max/std-dev = %.3f/%.3f/%.3f/%.3f ms\n", + printf("round-trip min/avg/max/std-dev = %.3f/%.3f/%.3f/%.3f ms\n", tmin, avg, tmax, dev); - strlcat(buf, buft, sizeof(buf)); } - write(STDOUT_FILENO, buf, strlen(buf)); /* XXX atomicio? */ } /* diff --git a/sbin/ping6/ping6.c b/sbin/ping6/ping6.c index ef0708660ce..aa17eebf501 100644 --- a/sbin/ping6/ping6.c +++ b/sbin/ping6/ping6.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ping6.c,v 1.150 2016/08/30 13:58:24 millert Exp $ */ +/* $OpenBSD: ping6.c,v 1.151 2016/08/30 14:28:31 deraadt Exp $ */ /* $KAME: ping6.c,v 1.163 2002/10/25 02:19:06 itojun Exp $ */ /* @@ -1219,46 +1219,29 @@ get_pathmtu(struct msghdr *mhdr) void summary(void) { - char buf[8192], buft[8192]; - - /* XXX - safe to use fprintf here now */ - snprintf(buft, sizeof(buft), "--- %s ping6 statistics ---\n", - hostname); - strlcat(buf, buft, sizeof(buf)); - snprintf(buft, sizeof(buft), "%lld packets transmitted, ", - ntransmitted); - strlcat(buf, buft, sizeof(buf)); - snprintf(buft, sizeof(buft), "%lld packets received, ", - nreceived); - strlcat(buf, buft, sizeof(buf)); - if (nrepeats) { - snprintf(buft, sizeof(buft), "+%lld duplicates, ", - nrepeats); - strlcat(buf, buft, sizeof(buf)); - } + printf("\n--- %s ping6 statistics ---\n", hostname); + printf("%lld packets transmitted, ", ntransmitted); + printf("%lld packets received, ", nreceived); + + if (nrepeats) + printf("+%lld duplicates, ", nrepeats); if (ntransmitted) { if (nreceived > ntransmitted) - snprintf(buft, sizeof(buft), - "-- somebody's duplicating packets!"); + printf("-- somebody's duplicating packets!"); else - snprintf(buft, sizeof(buft), "%.1lf%% packet loss", + printf("%.1lf%% packet loss", ((((double)ntransmitted - nreceived) * 100) / ntransmitted)); - strlcat(buf, buft, sizeof(buf)); } - strlcat(buf, "\n", sizeof(buf)); + printf("\n"); if (nreceived && timing) { /* Only display average to microseconds */ double num = nreceived + nrepeats; double avg = tsum / num; double dev = sqrt(fmax(0, tsumsq / num - avg * avg)); - snprintf(buft, sizeof(buft), - "round-trip min/avg/max/std-dev = %.3f/%.3f/%.3f/%.3f ms\n", + printf("round-trip min/avg/max/std-dev = %.3f/%.3f/%.3f/%.3f ms\n", tmin, avg, tmax, dev); - strlcat(buf, buft, sizeof(buf)); } - write(STDOUT_FILENO, buf, strlen(buf)); - (void)fflush(stdout); } /* -- 2.20.1