aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDag-Erling Smørgrav <des@FreeBSD.org>2026-06-26 14:46:09 +0000
committerDag-Erling Smørgrav <des@FreeBSD.org>2026-06-26 14:46:09 +0000
commit8bda488114f330d758e6741ff44882bb12b76c0d (patch)
treebcee69b05422fac911e73920ead9999fa237bd77
parentf0a861efbafeb81428d5e8c23dac9da73fe14007 (diff)
ping: Don't mix stdio and direct writes
POSIX does not allow mixing direct writes to STDOUT_FILENO with stdio operations on stdout like we do here. More importantly, it causes tests to fail randomly (or not-so-randomly after I added an fflush in a previous commit). MFC after: 1 week Reviewed by: markj Differential Revision: https://reviews.freebsd.org/D57884
-rw-r--r--sbin/ping/ping.c25
-rw-r--r--sbin/ping/ping6.c27
2 files changed, 33 insertions, 19 deletions
diff --git a/sbin/ping/ping.c b/sbin/ping/ping.c
index 19790af083f3..362264ff2428 100644
--- a/sbin/ping/ping.c
+++ b/sbin/ping/ping.c
@@ -982,8 +982,10 @@ ping(int argc, char *const *argv)
(void)clock_gettime(CLOCK_MONOTONIC, &last);
if (ntransmitted - nreceived - 1 > nmissedmax) {
nmissedmax = ntransmitted - nreceived - 1;
- if (options & F_MISSED)
- (void)write(STDOUT_FILENO, &BBELL, 1);
+ if (options & F_MISSED) {
+ (void)putc(BBELL, stdout);
+ (void)fflush(stdout);
+ }
}
}
}
@@ -1084,8 +1086,10 @@ pinger(void)
}
ntransmitted++;
sntransmitted++;
- if (!(options & F_QUIET) && options & F_DOT)
- (void)write(STDOUT_FILENO, &DOT[DOTidx++ % DOTlen], 1);
+ if (!(options & F_QUIET) && options & F_DOT) {
+ (void)putc(DOT[DOTidx++ % DOTlen], stdout);
+ (void)fflush(stdout);
+ }
}
/*
@@ -1207,9 +1211,10 @@ pr_pack(char *buf, ssize_t cc, struct sockaddr_in *from, struct timespec *tv)
return;
}
- if (options & F_DOT)
- (void)write(STDOUT_FILENO, &BSPACE, 1);
- else {
+ if (options & F_DOT) {
+ (void)putc(BSPACE, stdout);
+ (void)fflush(stdout);
+ } else {
(void)printf("%zd bytes from %s: icmp_seq=%u", cc,
pr_addr(from->sin_addr), seq);
(void)printf(" ttl=%d", ip.ip_ttl);
@@ -1217,8 +1222,10 @@ pr_pack(char *buf, ssize_t cc, struct sockaddr_in *from, struct timespec *tv)
(void)printf(" time=%.3f ms", triptime);
if (dupflag)
(void)printf(" (DUP!)");
- if (options & F_AUDIBLE)
- (void)write(STDOUT_FILENO, &BBELL, 1);
+ if (options & F_AUDIBLE) {
+ (void)putc(BBELL, stdout);
+ (void)fflush(stdout);
+ }
if (options & F_MASK) {
/* Just prentend this cast isn't ugly */
(void)printf(" mask=%s",
diff --git a/sbin/ping/ping6.c b/sbin/ping/ping6.c
index 1a4b2472b1bd..c7221cfeae8d 100644
--- a/sbin/ping/ping6.c
+++ b/sbin/ping/ping6.c
@@ -1242,8 +1242,10 @@ ping6(int argc, char *argv[])
clock_gettime(CLOCK_MONOTONIC, &last);
if (ntransmitted - nreceived - 1 > nmissedmax) {
nmissedmax = ntransmitted - nreceived - 1;
- if (options & F_MISSED)
- (void)write(STDOUT_FILENO, &BBELL, 1);
+ if (options & F_MISSED) {
+ (void)putc(BBELL, stdout);
+ (void)fflush(stdout);
+ }
}
}
}
@@ -1419,8 +1421,10 @@ pinger(void)
(void)printf("ping: wrote %s %d chars, ret=%d\n",
hostname, cc, i);
}
- if (!(options & F_QUIET) && options & F_DOT)
- (void)write(STDOUT_FILENO, &DOT[DOTidx++ % DOTlen], 1);
+ if (!(options & F_QUIET) && options & F_DOT) {
+ (void)putc(DOT[DOTidx++ % DOTlen], stdout);
+ (void)fflush(stdout);
+ }
return(0);
}
@@ -1617,11 +1621,14 @@ pr_pack(u_char *buf, int cc, struct msghdr *mhdr)
return;
}
- if (options & F_DOT)
- (void)write(STDOUT_FILENO, &BSPACE, 1);
- else {
- if (options & F_AUDIBLE)
- (void)write(STDOUT_FILENO, &BBELL, 1);
+ if (options & F_DOT) {
+ (void)putc(BSPACE, stdout);
+ (void)fflush(stdout);
+ } else {
+ if (options & F_AUDIBLE) {
+ (void)putc(BBELL, stdout);
+ (void)fflush(stdout);
+ }
(void)printf("%d bytes from %s, icmp_seq=%u", cc,
pr_addr(from, fromlen), seq);
(void)printf(" hlim=%d", hoplim);
@@ -1810,7 +1817,7 @@ pr_pack(u_char *buf, int cc, struct msghdr *mhdr)
}
if (!(options & F_DOT)) {
- (void)putchar('\n');
+ (void)putc('\n', stdout);
if (options & F_VERBOSE)
pr_exthdrs(mhdr);
(void)fflush(stdout);