The branch stable/14 has been updated by des:

URL: 
https://cgit.FreeBSD.org/src/commit/?id=b38b0be29bfdc4b7bf17a4e4ef952fe170fa7567

commit b38b0be29bfdc4b7bf17a4e4ef952fe170fa7567
Author:     Dag-Erling Smørgrav <[email protected]>
AuthorDate: 2026-06-23 15:20:22 +0000
Commit:     Dag-Erling Smørgrav <[email protected]>
CommitDate: 2026-07-07 07:07:56 +0000

    ping: Flush stdout after printing initial status
    
    MFC after:      1 week
    Reviewed by:    markj
    Differential Revision:  https://reviews.freebsd.org/D57765
    
    (cherry picked from commit 141bb8579882f9d1f7e95351573e30f10689f4bf)
    
    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
    
    (cherry picked from commit 8bda488114f330d758e6741ff44882bb12b76c0d)
---
 sbin/ping/ping.c  | 27 +++++++++++++++++----------
 sbin/ping/ping6.c | 28 ++++++++++++++++++----------
 2 files changed, 35 insertions(+), 20 deletions(-)

diff --git a/sbin/ping/ping.c b/sbin/ping/ping.c
index 3b8663772e87..d3cef7c20470 100644
--- a/sbin/ping/ping.c
+++ b/sbin/ping/ping.c
@@ -841,7 +841,6 @@ ping(int argc, char *const *argv)
                            sweepmin, sweepmax);
                else
                        (void)printf(": %d data bytes\n", datalen);
-
        } else {
                if (sweepmax)
                        (void)printf("PING %s: (%d ... %d) data bytes\n",
@@ -849,6 +848,7 @@ ping(int argc, char *const *argv)
                else
                        (void)printf("PING %s: %d data bytes\n", hostname, 
datalen);
        }
+       (void)fflush(stdout);
 
        /*
         * Use sigaction() instead of signal() to get unambiguous semantics,
@@ -994,8 +994,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);
+                               }
                        }
                }
        }
@@ -1089,8 +1091,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);
+       }
 }
 
 /*
@@ -1212,9 +1216,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);
@@ -1222,8 +1227,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 de2f34dd78db..406ea1875b9c 100644
--- a/sbin/ping/ping6.c
+++ b/sbin/ping/ping6.c
@@ -1124,6 +1124,7 @@ ping6(int argc, char *argv[])
            (unsigned long)(pingerlen() - 8));
        printf("%s --> ", pr_addr((struct sockaddr *)&src, sizeof(src)));
        printf("%s\n", pr_addr((struct sockaddr *)&dst, sizeof(dst)));
+       fflush(stdout);
 
        if (preload == 0)
                pinger();
@@ -1250,8 +1251,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);
+                               }
                        }
                }
        }
@@ -1427,8 +1430,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);
 }
@@ -1625,11 +1630,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);
@@ -1818,7 +1826,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);

Reply via email to