Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 14 Nov 2001 17:57:12 +0000
From:      David Hill <david@phobia.ms>
To:        freebsd-audit@freebsd.org
Subject:   ping.c patch - select() -> kqueue()
Message-ID:  <20011114175712.46522c21.david@phobia.ms>

next in thread | raw e-mail | index | archive | help

[-- Attachment #1 --]
I am not sure if converting ping from using select() to kqueue() is worth anything, but here is a patch.

David Hill
david@phobia.ms


[-- Attachment #2 --]
--- ping.c.orig	Tue Sep 25 20:22:33 2001
+++ ping.c	Wed Nov 14 17:05:39 2001
@@ -81,6 +81,8 @@
 #include <termios.h>
 #include <unistd.h>
 
+#include <sys/types.h>
+#include <sys/event.h>
 #include <sys/socket.h>
 #include <sys/time.h>
 #include <sys/uio.h>
@@ -201,8 +203,9 @@
 	struct hostent *hp;
 	struct sockaddr_in *to, sin;
 	struct termios ts;
+	struct kevent ke;
 	register int i;
-	int ch, hold, packlen, preload, sockerrno, almost_done = 0, ttl;
+	int ch, hold, packlen, preload, sockerrno, almost_done = 0, ttl, kq;
 	struct in_addr ifaddr;
 	unsigned char mttl, loop;
 	u_char *datap, *packet;
@@ -633,16 +636,21 @@
 
 	pinger();			/* send the first ping */
 	(void)gettimeofday(&last, NULL);
-
+	
+	if ((kq = kqueue()) < 0)
+		err(EX_OSERR, "kqueue");
+	
+	EV_SET(&ke, s, EVFILT_READ, EV_ADD | EV_ENABLE, NULL, 0, NULL);
+	if (kevent(kq, &ke, 1, NULL, 0, NULL) < 0)
+		err(EX_OSERR, "kevent");
+		
 	while (!finish_up) {
 		register int cc;
 		int n;
 		struct timeval timeout, now;
-		fd_set rfds;
+		struct timespec ts;
 
 		check_status();
-		FD_ZERO(&rfds);
-		FD_SET(s, &rfds);
 		(void)gettimeofday(&now, NULL);
 		timeout.tv_sec = last.tv_sec + intvl.tv_sec - now.tv_sec;
 		timeout.tv_usec = last.tv_usec + intvl.tv_usec - now.tv_usec;
@@ -656,7 +664,11 @@
 		}
 		if (timeout.tv_sec < 0)
 			timeout.tv_sec = timeout.tv_usec = 0;
-		n = select(s + 1, &rfds, NULL, NULL, &timeout);
+			
+		ts.tv_sec = timeout.tv_sec;
+		ts.tv_nsec = timeout.tv_usec * 1000;
+		
+		n = kevent(kq, NULL, 0, &ke, 1, &ts);
 		if (n < 0)
 			continue;	/* Must be EINTR. */
 		if (n == 1) {
@@ -714,6 +726,9 @@
 			}
 		}
 	}
+	
+	close(s);
+	close(kq);
 	finish();
 	/* NOTREACHED */
 	exit(0);	/* Make the compiler happy */

Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20011114175712.46522c21.david>