From 0104faa2803775d4f1b2c1115f4b6a8025252041 Mon Sep 17 00:00:00 2001 From: Brian Aker Date: Mon, 19 Nov 2007 19:19:40 -0800 Subject: [PATCH] Fixed thread issue on Linux with gethostbyname_r(). --- ChangeLog | 1 + configure.ac | 2 +- lib/memcached_connect.c | 17 ++++++++++++++++- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index 022ee4ee..6bb0ee1c 100644 --- 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. diff --git a/configure.ac b/configure.ac index 71a8ba53..db73e65c 100644 --- a/configure.ac +++ b/configure.ac @@ -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) diff --git a/lib/memcached_connect.c b/lib/memcached_connect.c index f323e79f..1a7cde16 100644 --- a/lib/memcached_connect.c +++ b/lib/memcached_connect.c @@ -10,16 +10,31 @@ 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; -- 2.30.2