diff --git a/sbin/ping/main.c b/sbin/ping/main.c --- a/sbin/ping/main.c +++ b/sbin/ping/main.c @@ -39,6 +39,7 @@ #include #include #include +#include #include #include "main.h" @@ -70,6 +71,7 @@ #endif #if defined(INET) && defined(INET6) struct addrinfo hints, *res, *ai; + const char *target; int error; #endif int opt; @@ -119,6 +121,7 @@ usage(); #if defined(INET) && defined(INET6) + target = argv[argc - 1]; memset(&hints, 0, sizeof(hints)); hints.ai_socktype = SOCK_RAW; if (feature_present("inet") && !feature_present("inet6")) @@ -127,9 +130,10 @@ hints.ai_family = AF_INET6; else hints.ai_family = AF_UNSPEC; - error = getaddrinfo(argv[argc - 1], NULL, &hints, &res); + error = getaddrinfo(target, NULL, &hints, &res); if (res == NULL) - errx(1, "%s", gai_strerror(error)); + errx(EX_NOHOST, "cannot resolve %s: %s", + target, gai_strerror(error)); for (ai = res; ai != NULL; ai = ai->ai_next) { if (ai->ai_family == AF_INET) { freeaddrinfo(res); @@ -141,7 +145,7 @@ } } freeaddrinfo(res); - errx(1, "Unknown host"); + errx(EX_NOHOST, "cannot resolve %s", target); #endif #ifdef INET ping4: diff --git a/sbin/ping/ping6.c b/sbin/ping/ping6.c --- a/sbin/ping/ping6.c +++ b/sbin/ping/ping6.c @@ -677,14 +677,15 @@ error = cap_getaddrinfo(capdns, target, NULL, &hints, &res); if (error) - errx(1, "%s", gai_strerror(error)); + errx(EX_NOHOST, "cannot resolve %s: %s", + target, gai_strerror(error)); if (res->ai_canonname) hostname = strdup(res->ai_canonname); else hostname = target; if (!res->ai_addr) - errx(1, "cap_getaddrinfo failed"); + errx(EX_NOHOST, "cannot resolve %s", target); (void)memcpy(&dst, res->ai_addr, res->ai_addrlen); diff --git a/sbin/ping/tests/ping_test.sh b/sbin/ping/tests/ping_test.sh --- a/sbin/ping/tests/ping_test.sh +++ b/sbin/ping/tests/ping_test.sh @@ -165,6 +165,39 @@ ping6 -4 localhost } +atf_test_case ping_nohost +ping_nohost_head() +{ + atf_set "descr" "ping a nonexistent host" +} +ping_nohost_body() +{ + atf_check -s exit:68 -e match:"cannot resolve" \ + ping nonexistent.in-addr.arpa. +} + +atf_test_case ping4_nohost +ping4_nohost_head() +{ + atf_set "descr" "ping -4 a nonexistent host" +} +ping4_nohost_body() +{ + atf_check -s exit:68 -e match:"cannot resolve" \ + ping -4 nonexistent.in-addr.arpa. +} + +atf_test_case ping6_nohost +ping6_nohost_head() +{ + atf_set "descr" "ping -6 a nonexistent host" +} +ping6_nohost_body() +{ + atf_check -s exit:68 -e match:"cannot resolve" \ + ping -6 nnonexistent.in-addr.arpa. +} + atf_test_case "inject_opts" "cleanup" inject_opts_head() { @@ -227,6 +260,9 @@ atf_add_test_case ping_46 atf_add_test_case ping_64 atf_add_test_case ping6_4 + atf_add_test_case ping_nohost + atf_add_test_case ping4_nohost + atf_add_test_case ping6_nohost atf_add_test_case inject_opts atf_add_test_case inject_pip atf_add_test_case inject_reply