From 5708bc0dd247c433b35251c3aadf76f780cd1797 Mon Sep 17 00:00:00 2001 From: melifaro Date: Sat, 15 Feb 2020 15:39:53 +0000 Subject: [PATCH] Make ping6(1) return code consistent with the man page. When every sendto() call originated by ping6(1) fails, current code always returns 2 ("transmission was successful but no responses were received") which is incorrect. Return EX_OSERR instead as in many cases it indicates some kernel-level problems. MFC after: 3 weeks --- sbin/ping6/ping6.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/sbin/ping6/ping6.c b/sbin/ping6/ping6.c index dd592f38e076..998f4a51ff3a 100644 --- a/sbin/ping6/ping6.c +++ b/sbin/ping6/ping6.c @@ -238,6 +238,7 @@ static long npackets; /* max packets to transmit */ static long nreceived; /* # of packets we got back */ static long nrepeats; /* number of duplicates */ static long ntransmitted; /* sequence # for outbound packets = #sent */ +static long ntransmitfailures; /* number of transmit failures */ static int interval = 1000; /* interval between packets in ms */ static int waittime = MAXWAIT; /* timeout for each packet */ static long nrcvtimeout = 0; /* # of packets we got back after waittime */ @@ -1256,7 +1257,12 @@ main(int argc, char *argv[]) if(packet != NULL) free(packet); - exit(nreceived == 0 ? 2 : 0); + if (nreceived > 0) + exit(0); + else if (ntransmitted > ntransmitfailures) + exit(2); + else + exit(EX_OSERR); } static void @@ -1423,8 +1429,10 @@ pinger(void) i = sendmsg(ssend, &smsghdr, 0); if (i < 0 || i != cc) { - if (i < 0) + if (i < 0) { + ntransmitfailures++; warn("sendmsg"); + } (void)printf("ping6: wrote %s %d chars, ret=%d\n", hostname, cc, i); }