Fixed thread issue on Linux with gethostbyname_r().
authorBrian Aker <brian@tangent.org>
Tue, 20 Nov 2007 03:19:40 +0000 (19:19 -0800)
committerBrian Aker <brian@tangent.org>
Tue, 20 Nov 2007 03:19:40 +0000 (19:19 -0800)
ChangeLog
configure.ac
lib/memcached_connect.c

index 022ee4ee8da8015a18a23117045e90b14136c94a..6bb0ee1c96021cee48a296be5c715b3f2fa63661 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -8,6 +8,7 @@
     from memcached errors (aka convert ints to strings)
   * Fixed type in MEMCACHED_HOST_LOOKUP_FAILURE
   * Fixed bug where hostname might not be null terminated
+  * Moved to using gethostbyname_r() on Linux to solve thread safety issue
 
 0.9 Thu Nov 15 07:44:00 PST 2007
   * fix for when no servers are definied.
index 71a8ba53d255772c46d464b3b65d1420009d7f4e..db73e65c8e22f99b37e0b712da2a0c495dac725d 100644 (file)
@@ -68,5 +68,5 @@ fi
 
 AC_C_CONST
 AC_TYPE_SIZE_T
-AC_CHECK_HEADERS(limits.h syslimits.h)
+AC_CHECK_FUNC(gethostbyname_r, AC_DEFINE([HAVE_GETHOSTBYNAME_R], [], [Looking for gethostbyname_r]))
 AC_OUTPUT(Makefile src/Makefile tests/Makefile docs/Makefile lib/Makefile include/Makefile support/Makefile support/libmemcached.pc support/libmemcached.spec)
index f323e79f778422b377b7a14fb79ecb0c3d1d93c2..1a7cde1676dc2e536607fe5547f9d8637f0db6ab 100644 (file)
 static memcached_return set_hostinfo(memcached_server_st *server)
 {
   struct hostent *h;
-
+#ifdef HAVE_GETHOSTBYNAME_R
+  struct hostent h_static;
+  char buffer[SMALL_STRING_LEN];
+  int tmp_error;
+
+  if (gethostbyname_r(server->hostname,
+                      &h_static, buffer, SMALL_STRING_LEN, 
+                      &h, &tmp_error))
+  {
+    WATCHPOINT_STRING(server->hostname);
+    WATCHPOINT_STRING(hstrerror(tmp_error));
+    return MEMCACHED_HOST_LOOKUP_FAILURE;
+  }
+#else
   if ((h= gethostbyname(server->hostname)) == NULL)
   {
     WATCHPOINT_STRING(server->hostname);
     WATCHPOINT_STRING(hstrerror(h_errno));
     return MEMCACHED_HOST_LOOKUP_FAILURE;
   }
+#endif
 
   server->servAddr.sin_family= h->h_addrtype;
   memcpy((char *) &server->servAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
+
   server->servAddr.sin_port = htons(server->port);
 
   return MEMCACHED_SUCCESS;