From ca5b31783682c84574148cdb9c211cebbb006c11 Mon Sep 17 00:00:00 2001 From: billf Date: Fri, 14 Jan 2000 23:40:38 +0000 Subject: [PATCH] Add '-t X' which times out after pinging for X seconds. Submitted by: adrian --- sbin/ping/ping.8 | 3 +++ sbin/ping/ping.c | 19 +++++++++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/sbin/ping/ping.8 b/sbin/ping/ping.8 index fca80acb44f8..813381cac641 100644 --- a/sbin/ping/ping.8 +++ b/sbin/ping/ping.8 @@ -219,6 +219,9 @@ force the source address to be something other than the IP address of the interface the probe packet is sent on. If the IP address is not one of this machine's interface addresses, an error is returned and nothing is sent. +.It Fl t Ar timeout +Specify a timeout, in seconds, before ping exits regardless of how +many packets have been recieved. .It Fl T Ar ttl Set the IP Time To Live for multicasted packets. This flag only applies if the ping destination is a multicast address. diff --git a/sbin/ping/ping.c b/sbin/ping/ping.c index 15bac460a394..074f36770d8e 100644 --- a/sbin/ping/ping.c +++ b/sbin/ping/ping.c @@ -217,6 +217,7 @@ main(argc, argv) char *policy_in = NULL; char *policy_out = NULL; #endif + int alarmtimeout = 0; /* * Do the stuff that we need root priv's for *first*, and @@ -233,10 +234,10 @@ main(argc, argv) datap = &outpack[8 + PHDR_LEN]; #ifndef IPSEC - while ((ch = getopt(argc, argv, "I:LQRT:c:adfi:l:np:qrs:v")) != -1) + while ((ch = getopt(argc, argv, "I:LQRT:c:adfi:l:np:qrs:t:v")) != -1) #else #ifdef IPSEC_POLICY_IPSEC - while ((ch = getopt(argc, argv, "I:LQRT:c:adfi:l:np:qrs:vP:")) != -1) + while ((ch = getopt(argc, argv, "I:LQRT:c:adfi:l:np:qrs:t:vP:")) != -1) #endif /*IPSEC_POLICY_IPSEC*/ #endif { @@ -340,6 +341,13 @@ main(argc, argv) case 'S': source = optarg; break; + case 't': + alarmtimeout = (int)strtoul(optarg, &ep, 0); + if (alarmtimeout < 1) + errx(EX_USAGE, "invalid timeout: `%s'", + optarg); + alarm(alarmtimeout); + break; case 'T': /* multicast TTL */ ultmp = strtoul(optarg, &ep, 0); if (*ep || ep == optarg || ultmp > 255) @@ -558,6 +566,9 @@ main(argc, argv) if (sigaction(SIGINFO, &si_sa, 0) == -1) { err(EX_OSERR, "sigaction"); } + si_sa.sa_handler = stopit; + if (sigaction(SIGALRM, &si_sa, 0) == -1) + err(EX_OSERR, "sigaction SIGALRM"); bzero(&msg, sizeof(msg)); msg.msg_name = (caddr_t)&from; @@ -1391,7 +1402,7 @@ usage() "[-P policy] " #endif #endif -"[-s packetsize] [-S src_addr]", - "[host | [-L] [-I iface] [-T ttl] mcast-group]"); +"[-s packetsize] [-S src_addr] [-t timeout]", +" [host | [-L] [-I iface] [-T ttl] mcast-group]"); exit(EX_USAGE); }