1 Check if we can use thread-local storage, and if we can, use one to avoid a
2 self-deadlock if we recurse into our own host resolution routines from inside
3 of another lookup attempt. Revised from patch originally submitted for #340.
5 diff -up nss_ldap-265/config.h.in nss_ldap-265/config.h.in
6 --- nss_ldap-265/config.h.in 2009-11-06 05:28:08.000000000 -0500
7 +++ nss_ldap-265/config.h.in 2010-01-08 17:29:49.000000000 -0500
9 /* Define to 1 if you have the <thread.h> header file. */
12 +/* Define if your toolchain supports thread-local storage, which can be used
13 + for detecting self- and mutual-recursion problems when performing
14 + host/address lookups. */
15 +#undef HAVE_THREAD_LOCAL_STORAGE
17 /* Define to 1 if you have the <unistd.h> header file. */
20 diff -up nss_ldap-265/configure.in nss_ldap-265/configure.in
21 --- nss_ldap-265/configure.in 2009-11-06 05:28:08.000000000 -0500
22 +++ nss_ldap-265/configure.in 2010-01-08 17:29:49.000000000 -0500
23 @@ -27,6 +27,14 @@ dnl
25 AC_ARG_ENABLE(debugging, [ --enable-debugging enable debug code ], [AC_DEFINE(DEBUG)])
27 +AC_MSG_CHECKING(for thread-local storage)
28 +AC_TRY_COMPILE([],[static __thread int _nss_ldap_recursion_count;],
31 + AC_DEFINE(HAVE_THREAD_LOCAL_STORAGE,1,[Define if your toolchain supports thread-local storage, which can be used for detecting self- and mutual-recursion problems when performing host/address lookups.])
36 dnl --enable-paged-results is now deprecated; if this option is set,
37 dnl then paged results will be enabled by default. However, it can
38 diff -up nss_ldap-265/depth.c nss_ldap-265/depth.c
39 --- nss_ldap-265/depth.c 2010-01-08 17:29:49.000000000 -0500
40 +++ nss_ldap-265/depth.c 2010-01-08 17:29:49.000000000 -0500
45 +#ifdef HAVE_THREAD_LOCAL_STORAGE
46 +static __thread int depth = 0;
49 +_nss_ldap_get_depth (void)
55 +_nss_ldap_inc_depth (void)
61 +_nss_ldap_dec_depth (void)
66 diff -up nss_ldap-265/depth.h nss_ldap-265/depth.h
67 --- nss_ldap-265/depth.h 2010-01-08 17:29:49.000000000 -0500
68 +++ nss_ldap-265/depth.h 2010-01-08 17:29:49.000000000 -0500
70 +int _nss_ldap_get_depth (void);
71 +int _nss_ldap_inc_depth (void);
72 +int _nss_ldap_dec_depth (void);
73 diff -up nss_ldap-265/ldap-hosts.c nss_ldap-265/ldap-hosts.c
74 --- nss_ldap-265/ldap-hosts.c 2009-11-06 05:28:08.000000000 -0500
75 +++ nss_ldap-265/ldap-hosts.c 2010-01-08 17:33:38.000000000 -0500
76 @@ -66,6 +66,7 @@ static char rcsId[] =
78 #include "ldap-hosts.h"
82 #ifdef HAVE_PORT_AFTER_H
83 #include <port_after.h>
84 @@ -280,6 +281,11 @@ _nss_ldap_gethostbyname2_r (const char *
88 +#ifdef HAVE_THREAD_LOCAL_STORAGE
89 + if (_nss_ldap_get_depth() > 0)
90 + return NSS_STATUS_UNAVAIL;
95 LA_TYPE (a) = LA_TYPE_STRING;
96 @@ -355,6 +361,11 @@ _nss_ldap_gethostbyaddr_r (struct in_add
100 +#ifdef HAVE_THREAD_LOCAL_STORAGE
101 + if (_nss_ldap_get_depth() > 0)
102 + return NSS_STATUS_UNAVAIL;
105 /* if querying by IPv6 address, make sure the address is "normalized" --
106 * it should contain no leading zeros and all components of the address.
107 * still we can't fit an IPv6 address in an int, so who cares for now.
108 @@ -391,6 +402,11 @@ _nss_ldap_sethostent_r (nss_backend_t *
110 #if defined(HAVE_NSS_H) || defined(HAVE_NSSWITCH_H)
112 +#ifdef HAVE_THREAD_LOCAL_STORAGE
113 + if (_nss_ldap_get_depth() > 0)
114 + return NSS_STATUS_UNAVAIL;
117 LOOKUP_SETENT (hosts_context);
120 @@ -403,6 +419,11 @@ _nss_ldap_endhostent_r (nss_backend_t *
122 #if defined(HAVE_NSS_H) || defined(HAVE_NSSWITCH_H)
124 +#ifdef HAVE_THREAD_LOCAL_STORAGE
125 + if (_nss_ldap_get_depth() > 0)
126 + return NSS_STATUS_UNAVAIL;
129 LOOKUP_ENDENT (hosts_context);
132 @@ -435,6 +456,11 @@ _nss_ldap_gethostent_r (struct hostent *
136 +#ifdef HAVE_THREAD_LOCAL_STORAGE
137 + if (_nss_ldap_get_depth() > 0)
138 + return NSS_STATUS_UNAVAIL;
141 status = _nss_ldap_getent (&hosts_context,
144 diff -up nss_ldap-265/ldap-nss.c nss_ldap-265/ldap-nss.c
145 --- nss_ldap-265/ldap-nss.c 2009-11-06 05:28:08.000000000 -0500
146 +++ nss_ldap-265/ldap-nss.c 2010-01-08 17:29:49.000000000 -0500
147 @@ -93,6 +93,7 @@ static char rcsId[] =
149 #include "dnsconfig.h"
150 #include "pagectrl.h"
153 #if defined(HAVE_THREAD_H) && !defined(_AIX)
154 #ifdef HAVE_PTHREAD_ATFORK
155 @@ -578,6 +579,9 @@ _nss_ldap_enter (void)
156 debug ("==> _nss_ldap_enter");
158 NSS_LDAP_LOCK (__lock);
159 +#ifdef HAVE_THREAD_LOCAL_STORAGE
160 + _nss_ldap_inc_depth();
164 * Patch for Debian Bug 130006:
165 @@ -623,6 +627,9 @@ _nss_ldap_leave (void)
167 #endif /* HAVE_SIGACTION */
169 +#ifdef HAVE_THREAD_LOCAL_STORAGE
170 + _nss_ldap_dec_depth();
172 NSS_LDAP_UNLOCK (__lock);
174 debug ("<== _nss_ldap_leave");
175 diff -up nss_ldap-265/Makefile.am nss_ldap-265/Makefile.am
176 --- nss_ldap-265/Makefile.am 2009-11-06 05:28:08.000000000 -0500
177 +++ nss_ldap-265/Makefile.am 2010-01-08 17:31:45.000000000 -0500
178 @@ -23,7 +23,7 @@ nss_ldap_so_SOURCES = ldap-nss.c ldap-pw
179 ldap-alias.c ldap-service.c ldap-schema.c ldap-ethers.c \
180 ldap-bp.c ldap-automount.c util.c ltf.c snprintf.c resolve.c \
181 dnsconfig.c irs-nss.c pagectrl.c ldap-sldap.c ldap-init-krb5-cache.c \
185 nss_ldap_so_LDFLAGS = @nss_ldap_so_LDFLAGS@