about summary refs log tree commit diff
path: root/src/network
diff options
context:
space:
mode:
authorRich Felker <dalias@aerifal.cx>2011-09-21 23:27:17 -0400
committerRich Felker <dalias@aerifal.cx>2011-09-21 23:27:17 -0400
commit12d547b262c2c8c4e23d0a62eb59581929c2da89 (patch)
tree58cb89a8f9864f58f233e323a563e9a96f0e7a22 /src/network
parentbf027ca5e0c7bc0b90e84dcbb1d7cbea6ed1e3ef (diff)
downloadmusl-12d547b262c2c8c4e23d0a62eb59581929c2da89.tar.gz
musl-12d547b262c2c8c4e23d0a62eb59581929c2da89.tar.xz
musl-12d547b262c2c8c4e23d0a62eb59581929c2da89.zip
make dns lookups (and thus getaddrinfo) cancellable
Diffstat (limited to 'src/network')
-rw-r--r--src/network/__dns.c15
1 files changed, 11 insertions, 4 deletions
diff --git a/src/network/__dns.c b/src/network/__dns.c
index a5458b51..8cd7d5d0 100644
--- a/src/network/__dns.c
+++ b/src/network/__dns.c
@@ -19,6 +19,11 @@
 #define PACKET_MAX 512
 #define PTR_MAX (64 + sizeof ".in-addr.arpa")
 
+static void cleanup(void *p)
+{
+	close((intptr_t)p);
+}
+
 int __dns_doqueries(unsigned char *dest, const char *name, int *rr, int rrcnt)
 {
 	time_t t0 = time(0);
@@ -44,6 +49,8 @@ int __dns_doqueries(unsigned char *dest, const char *name, int *rr, int rrcnt)
 	int id;
 	int cs;
 
+	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
+
 	/* Construct query template - RR and ID will be filled later */
 	if (strlen(name)-1 >= 254U) return EAI_NONAME;
 	q[2] = q[5] = 1;
@@ -81,8 +88,6 @@ int __dns_doqueries(unsigned char *dest, const char *name, int *rr, int rrcnt)
 		sl = sizeof sa.sin;
 	}
 
-	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
-
 	/* Get local address and open/bind a socket */
 	sa.sin.sin_family = family;
 	fd = socket(family, SOCK_DGRAM, 0);
@@ -96,6 +101,9 @@ int __dns_doqueries(unsigned char *dest, const char *name, int *rr, int rrcnt)
 	pfd.fd = fd;
 	pfd.events = POLLIN;
 
+	pthread_cleanup_push(cleanup, (void *)(intptr_t)fd);
+	pthread_setcancelstate(cs, 0);
+
 	/* Loop until we timeout; break early on success */
 	for (; time(0)-t0 < TIMEOUT; ) {
 
@@ -143,8 +151,7 @@ int __dns_doqueries(unsigned char *dest, const char *name, int *rr, int rrcnt)
 		if (got+failed == rrcnt) break;
 	}
 out:
-	close(fd);
-	pthread_setcancelstate(cs, 0);
+	pthread_cleanup_pop(1);
 
 	/* Return the number of results, or an error code if none */
 	if (got) return got;