1 /* Copyright (C) 1996-2004, 2006, 2007, 2009 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
27 # include <nscd/nscd_proto.h>
29 #ifdef NEED__RES_HCONF
30 # include <resolv/res_hconf.h>
35 /*******************************************************************\
36 |* Here we assume several symbols to be defined: *|
38 |* LOOKUP_TYPE - the return type of the function *|
40 |* FUNCTION_NAME - name of the non-reentrant function *|
42 |* DATABASE_NAME - name of the database the function accesses *|
43 |* (e.g., host, services, ...) *|
45 |* ADD_PARAMS - additional parameters, can vary in number *|
47 |* ADD_VARIABLES - names of additional parameters *|
49 |* Optionally the following vars can be defined: *|
51 |* EXTRA_PARAMS - optional parameters, can vary in number *|
53 |* EXTRA_VARIABLES - names of optional parameter *|
55 |* FUNCTION_NAME - alternative name of the non-reentrant function *|
57 |* NEED_H_ERRNO - an extra parameter will be passed to point to *|
58 |* the global `h_errno' variable. *|
60 |* NEED__RES - the global _res variable might be used so we *|
61 |* will have to initialize it if necessary *|
63 |* PREPROCESS - code run before anything else *|
65 |* POSTPROCESS - code run after the lookup *|
67 \*******************************************************************/
69 /* To make the real sources a bit prettier. */
70 #define REENTRANT_NAME APPEND_R (FUNCTION_NAME)
72 # define REENTRANT2_NAME APPEND_R (FUNCTION2_NAME)
74 # define REENTRANT2_NAME NULL
76 #define APPEND_R(name) APPEND_R1 (name)
77 #define APPEND_R1(name) name##_r
78 #define INTERNAL(name) INTERNAL1 (name)
79 #define INTERNAL1(name) __##name
80 #define NEW(name) NEW1 (name)
81 #define NEW1(name) __new_##name
84 # define NSCD_NAME ADD_NSCD (REENTRANT_NAME)
85 # define ADD_NSCD(name) ADD_NSCD1 (name)
86 # define ADD_NSCD1(name) __nscd_##name
87 # define NOT_USENSCD_NAME ADD_NOT_NSCDUSE (DATABASE_NAME)
88 # define ADD_NOT_NSCDUSE(name) ADD_NOT_NSCDUSE1 (name)
89 # define ADD_NOT_NSCDUSE1(name) __nss_not_use_nscd_##name
92 #define FUNCTION_NAME_STRING STRINGIZE (FUNCTION_NAME)
93 #define REENTRANT_NAME_STRING STRINGIZE (REENTRANT_NAME)
95 # define REENTRANT2_NAME_STRING STRINGIZE (REENTRANT2_NAME)
97 # define REENTRANT2_NAME_STRING NULL
99 #define DATABASE_NAME_STRING STRINGIZE (DATABASE_NAME)
100 #define STRINGIZE(name) STRINGIZE1 (name)
101 #define STRINGIZE1(name) #name
103 #ifndef DB_LOOKUP_FCT
104 # define DB_LOOKUP_FCT CONCAT3_1 (__nss_, DATABASE_NAME, _lookup2)
105 # define CONCAT3_1(Pre, Name, Post) CONCAT3_2 (Pre, Name, Post)
106 # define CONCAT3_2(Pre, Name, Post) Pre##Name##Post
109 /* Sometimes we need to store error codes in the `h_errno' variable. */
111 # define H_ERRNO_PARM , int *h_errnop
112 # define H_ERRNO_VAR , h_errnop
113 # define H_ERRNO_VAR_P h_errnop
115 # define H_ERRNO_PARM
117 # define H_ERRNO_VAR_P NULL
121 # define EXTRA_PARAMS
123 #ifndef EXTRA_VARIABLES
124 # define EXTRA_VARIABLES
130 # define AF_VAL AF_INET
133 /* Type of the lookup function we need here. */
134 typedef enum nss_status (*lookup_function
) (ADD_PARAMS
, LOOKUP_TYPE
*, char *,
135 size_t, int * H_ERRNO_PARM
138 /* The lookup function for the first entry of this service. */
139 extern int DB_LOOKUP_FCT (service_user
**nip
, const char *name
,
140 const char *name2
, void **fctp
)
142 libc_hidden_proto (DB_LOOKUP_FCT
)
146 INTERNAL (REENTRANT_NAME
) (ADD_PARAMS
, LOOKUP_TYPE
*resbuf
, char *buffer
,
147 size_t buflen
, LOOKUP_TYPE
**result H_ERRNO_PARM
150 static bool startp_initialized
;
151 static service_user
*startp
;
152 static lookup_function start_fct
;
161 enum nss_status status
= NSS_STATUS_UNAVAIL
;
166 bool any_service
= false;
173 #ifdef HANDLE_DIGITS_DOTS
174 switch (__nss_hostname_digits_dots (name
, resbuf
, &buffer
, NULL
,
175 buflen
, result
, &status
, AF_VAL
,
186 if (NOT_USENSCD_NAME
> 0 && ++NOT_USENSCD_NAME
> NSS_NSCD_RETRY
)
187 NOT_USENSCD_NAME
= 0;
189 if (!NOT_USENSCD_NAME
)
191 nscd_status
= NSCD_NAME (ADD_VARIABLES
, resbuf
, buffer
, buflen
, result
193 if (nscd_status
>= 0)
198 if (! startp_initialized
)
200 no_more
= DB_LOOKUP_FCT (&nip
, REENTRANT_NAME_STRING
,
201 REENTRANT2_NAME_STRING
, &fct
.ptr
);
204 void *tmp_ptr
= (service_user
*) -1l;
205 PTR_MANGLE (tmp_ptr
);
211 /* The resolver code will really be used so we have to
213 if (__res_maybe_init (&_res
, 0) == -1)
215 *h_errnop
= NETDB_INTERNAL
;
219 #endif /* need _res */
220 #ifdef NEED__RES_HCONF
221 if (!_res_hconf
.initialized
)
223 #endif /* need _res_hconf */
225 void *tmp_ptr
= fct
.l
;
226 PTR_MANGLE (tmp_ptr
);
229 PTR_MANGLE (tmp_ptr
);
233 /* Make sure start_fct and startp are written before
234 startp_initialized. */
235 atomic_write_barrier ();
236 startp_initialized
= true;
241 PTR_DEMANGLE (fct
.l
);
244 no_more
= nip
== (service_user
*) -1l;
253 status
= DL_CALL_FCT (fct
.l
, (ADD_VARIABLES
, resbuf
, buffer
, buflen
,
254 &errno H_ERRNO_VAR EXTRA_VARIABLES
));
256 /* The status is NSS_STATUS_TRYAGAIN and errno is ERANGE the
257 provided buffer is too small. In this case we should give
258 the user the possibility to enlarge the buffer and we should
259 not simply go on with the next service (even if the TRYAGAIN
260 action tells us so). */
261 if (status
== NSS_STATUS_TRYAGAIN
263 && *h_errnop
== NETDB_INTERNAL
268 no_more
= __nss_next2 (&nip
, REENTRANT_NAME_STRING
,
269 REENTRANT2_NAME_STRING
, &fct
.ptr
, status
, 0);
272 #ifdef HANDLE_DIGITS_DOTS
275 *result
= status
== NSS_STATUS_SUCCESS
? resbuf
: NULL
;
277 if (status
!= NSS_STATUS_SUCCESS
&& ! any_service
)
278 /* We were not able to use any service. */
279 *h_errnop
= NO_RECOVERY
;
286 if (status
== NSS_STATUS_SUCCESS
|| status
== NSS_STATUS_NOTFOUND
)
288 /* Don't pass back ERANGE if this is not for a too-small buffer. */
289 else if (errno
== ERANGE
&& status
!= NSS_STATUS_TRYAGAIN
)
292 /* These functions only set errno if h_errno is NETDB_INTERNAL. */
293 else if (status
== NSS_STATUS_TRYAGAIN
&& *h_errnop
!= NETDB_INTERNAL
)
304 #ifdef NO_COMPAT_NEEDED
305 strong_alias (INTERNAL (REENTRANT_NAME
), REENTRANT_NAME
);
306 #elif !defined FUNCTION2_NAME
307 # include <shlib-compat.h>
308 # if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_1_2)
309 # define OLD(name) OLD1 (name)
310 # define OLD1(name) __old_##name
313 attribute_compat_text_section
314 OLD (REENTRANT_NAME
) (ADD_PARAMS
, LOOKUP_TYPE
*resbuf
, char *buffer
,
315 size_t buflen
, LOOKUP_TYPE
**result H_ERRNO_PARM
)
317 int ret
= INTERNAL (REENTRANT_NAME
) (ADD_VARIABLES
, resbuf
, buffer
,
318 buflen
, result H_ERRNO_VAR
);
320 if (ret
!= 0 || result
== NULL
)
326 # define do_symbol_version(real, name, version) \
327 compat_symbol (libc, real, name, version)
328 do_symbol_version (OLD (REENTRANT_NAME
), REENTRANT_NAME
, GLIBC_2_0
);
331 /* As INTERNAL (REENTRANT_NAME) may be hidden, we need an alias
332 in between so that the REENTRANT_NAME@@GLIBC_2.1.2 is not
334 strong_alias (INTERNAL (REENTRANT_NAME
), NEW (REENTRANT_NAME
));
336 # define do_default_symbol_version(real, name, version) \
337 versioned_symbol (libc, real, name, version)
338 do_default_symbol_version (NEW (REENTRANT_NAME
),
339 REENTRANT_NAME
, GLIBC_2_1_2
);
342 static_link_warning (REENTRANT_NAME
)