Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 21 Nov 2001 22:32:02 +0000
From:      David Hill <david@phobia.ms>
To:        freebsd-audit@freebsd.org, freebsd-current@freebsd.org
Subject:   libfetch kqueue patch
Message-ID:  <20011121223202.0e85d777.david@phobia.ms>

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

[-- Attachment #1 --]
Hello -
This patch converts libfetch to use kqueue instead of select.

Thanks
David Hill
[-- Attachment #2 --]
--- /usr/src/lib/libfetch/common.c.orig	Wed Nov 21 22:11:23 2001
+++ /usr/src/lib/libfetch/common.c	Wed Nov 21 22:21:50 2001
@@ -32,6 +32,7 @@
 #include <sys/param.h>
 #include <sys/socket.h>
 #include <sys/time.h>
+#include <sys/event.h>
 #include <sys/uio.h>
 #include <netinet/in.h>
 
@@ -256,8 +257,9 @@
 _fetch_getln(int fd, char **buf, size_t *size, size_t *len)
 {
     struct timeval now, timeout, wait;
-    fd_set readfds;
-    int r;
+    struct timespec waitkq;
+	struct kevent ke;
+    int r, kq;
     char c;
     
     if (*buf == NULL) {
@@ -274,12 +276,17 @@
     if (fetchTimeout) {
 	gettimeofday(&timeout, NULL);
 	timeout.tv_sec += fetchTimeout;
-	FD_ZERO(&readfds);
     }
     
-    do {
+	if ((kq = kqueue()) < 0)
+		return -1;
+		
+	EV_SET(&ke, fd, EVFILT_READ, EV_ADD | EV_ENABLE, 0, NULL, NULL);
+	if (kevent(kq, &ke, 1, NULL, 0, NULL) < 0)
+		return -1;
+    
+	do {
 	if (fetchTimeout) {
-	    FD_SET(fd, &readfds);
 	    gettimeofday(&now, NULL);
 	    wait.tv_sec = timeout.tv_sec - now.tv_sec;
 	    wait.tv_usec = timeout.tv_usec - now.tv_usec;
@@ -291,15 +298,15 @@
 		errno = ETIMEDOUT;
 		return -1;
 	    }
-	    r = select(fd+1, &readfds, NULL, NULL, &wait);
+		waitkq.tv_sec = wait.tv_sec;
+		waitkq.tv_nsec = wait.tv_usec * 1000;
+		r = kevent(kq, NULL, 0, &ke, 1, &waitkq);
 	    if (r == -1) {
 		if (errno == EINTR && fetchRestartCalls)
 		    continue;
 		/* EBADF or EINVAL: shouldn't happen */
 		return -1;
 	    }
-	    if (!FD_ISSET(fd, &readfds))
-		continue;
 	}
 	r = read(fd, &c, 1);
 	if (r == 0)
@@ -325,6 +332,8 @@
     } while (c != '\n');
     
     DEBUG(fprintf(stderr, "\033[1m<<< %.*s\033[m", (int)*len, *buf));
+	
+	close(kq);
     return 0;
 }
 

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