From 1e6db6b76fb2fbbfbabce028c18eec62cc593650 Mon Sep 17 00:00:00 2001 From: Daniel Rossier Date: Sun, 23 Aug 2026 17:04:14 +0200 Subject: [PATCH] usr: say what actually went wrong in ping MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit "Packet receive failed!!" was printed for the ordinary case — a host that does not answer — and said nothing about the rare case where something really did go wrong. Same for "Packet sending failed!!", which hid a plain "No route to host" when ping ran before DHCP had brought the interface up, and for the socket error, which hid "Function not implemented" when CONFIG_NET is off. A receive timeout (SO_RCVTIMEO, reported by lwIP as EWOULDBLOCK) now prints the usual "Request timeout for icmp_seq=N"; anything else prints strerror(errno), as do the send and socket paths. --- so3/usr/src/ping.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/so3/usr/src/ping.c b/so3/usr/src/ping.c index c7cb8511e..20703220e 100644 --- a/so3/usr/src/ping.c +++ b/so3/usr/src/ping.c @@ -21,6 +21,7 @@ /* ping: send ICMP echo requests to a host and report the replies, including * the round-trip times. */ +#include #include #include #include @@ -233,7 +234,7 @@ int main(int argc, char **argv) s = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); if (s < 0) { - printf("Impossible to obtain a socket file descriptor!!\n"); + printf("Cannot open the ICMP socket: %s\n", strerror(errno)); return 1; } @@ -265,7 +266,7 @@ int main(int argc, char **argv) gettimeofday(&start, NULL); if (sendto(s, &packet, sizeof(packet), 0, (struct sockaddr *) &ping_addr, sizeof(ping_addr)) <= 0) { - printf("Packet sending failed!!\n"); + printf("Cannot send icmp_seq=%d: %s\n", msg_count, strerror(errno)); continue; } @@ -273,10 +274,16 @@ int main(int argc, char **argv) len = recvfrom(s, reply, sizeof(reply), 0, (struct sockaddr *) &recv_addr, &size); - /* A timeout (SO_RCVTIMEO) lands here too, which is the normal - * outcome for a host that never answers. */ + /* A host that never answers is the normal case, not a failure: + * SO_RCVTIMEO expires and lwIP reports it as EWOULDBLOCK (EAGAIN, + * the same value in musl). Anything else is a real error and says + * which one. */ if (len <= 0) { - printf("Packet receive failed!!\n"); + if ((len < 0) && (errno != EAGAIN)) + printf("Cannot receive icmp_seq=%d: %s\n", msg_count, strerror(errno)); + else + printf("Request timeout for icmp_seq=%d\n", msg_count); + continue; }