[uClibc]gethostbyname/alarm

Jim Treadway jim at stardot-tech.com
Tue Apr 2 04:34:47 UTC 2002


I ran into an unexpected use of alarm() in uClibc (well, actually in the 
old "uC-libc" version used by the uClinux-coldfire stuff, but the same 
thing is still present in the CVS for uClibc).

The following patch rewrites the dns_lookup() function to use select() 
for timeouts rather than alarm(); it's against the older version because 
that's all that I can easily test ;) but I think it should be fairly easy 
to merge this into the CVS version.

Jim


diff -u -r1.5 resolv.c
--- ./lib/libc/net/resolv.c	6 Mar 2002 07:07:47 -0000	1.5
+++ ./lib/libc/net/resolv.c	2 Apr 2002 04:22:19 -0000
@@ -13,6 +13,7 @@
 #include <stdio.h>
 #include <signal.h>
 #include <sys/socket.h>
+#include <sys/time.h>
 #include <sys/types.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
@@ -407,11 +408,6 @@
 
 #ifdef L_dnslookup
 
-int dns_caught_signal = 0;
-void dns_catch_signal(int signo) {
-	dns_caught_signal = 1;
-}	
-
 int dns_lookup(const char * name, int type, int nscount, const char ** nsip,
 	unsigned char ** outpacket, struct resolv_answer * a)
 {
@@ -421,8 +417,8 @@
 	int pos;
 	static int ns = 0;
 	struct sockaddr_in sa;
-	int oldalarm;
-	__sighandler_t oldhandler;
+	struct timeval tv;
+	fd_set fds;
 	struct resolv_header h;
 	struct resolv_question q;
 	int retries = 0;
@@ -496,24 +492,19 @@
 
 		send(fd, packet, len, 0);
 
-		dns_caught_signal = 0;
-		oldalarm = alarm(REPLY_TIMEOUT);
-		oldhandler = signal(SIGALRM, dns_catch_signal);
-	
-		i = recv(fd, packet, 512, 0);
-		
-		alarm(0);
-		signal(SIGALRM, oldhandler);
-		alarm(oldalarm);
-		
-		DPRINTF("Timeout=%d, len=%d\n",
-			dns_caught_signal, i);
-		
-		if (dns_caught_signal)
+		FD_ZERO(&fds);
+		FD_SET(fd, &fds);
+		tv.tv_sec = REPLY_TIMEOUT;
+		tv.tv_usec = 0;
+		if (select(fd + 1, &fds, NULL, NULL, &tv) <= 0) {
+			DPRINTF("Timeout\n");
 			/* timed out, so retry send and receive,
-			   to next nameserver on queue */
+			 * to next nameserver on queue */
 			goto again;
-		
+		}
+
+		i = recv(fd, packet, 512, 0);
+	
 		if (i < 12)
 			/* too short ! */
 			goto again;




More information about the uClibc mailing list