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
This commit is contained in:
Dag-Erling Smørgrav committed 2026-06-26 16:46:09 +02:00
1 parent f0a861efba
commit 8bda488114
2 files changed
+33 -19

No files matched your search

+16 -9
View File
@@ -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",
+17 -10
View File
@@ -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);