1 /* Copyright (C) 1996-2004,2006,2007,2009,2010 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
90 # define CONCAT2(arg1, arg2) CONCAT2_2 (arg1, arg2)
91 # define CONCAT2_2(arg1, arg2) arg1##arg2
94 #define FUNCTION_NAME_STRING STRINGIZE (FUNCTION_NAME)
95 #define REENTRANT_NAME_STRING STRINGIZE (REENTRANT_NAME)
97 # define REENTRANT2_NAME_STRING STRINGIZE (REENTRANT2_NAME)
99 # define REENTRANT2_NAME_STRING NULL
101 #define DATABASE_NAME_STRING STRINGIZE (DATABASE_NAME)
102 #define STRINGIZE(name) STRINGIZE1 (name)
103 #define STRINGIZE1(name) #name
105 #ifndef DB_LOOKUP_FCT
106 # define DB_LOOKUP_FCT CONCAT3_1 (__nss_, DATABASE_NAME, _lookup2)
107 # define CONCAT3_1(Pre, Name, Post) CONCAT3_2 (Pre, Name, Post)
108 # define CONCAT3_2(Pre, Name, Post) Pre##Name##Post
111 /* Sometimes we need to store error codes in the `h_errno' variable. */
113 # define H_ERRNO_PARM , int *h_errnop
114 # define H_ERRNO_VAR , h_errnop
115 # define H_ERRNO_VAR_P h_errnop
117 # define H_ERRNO_PARM
119 # define H_ERRNO_VAR_P NULL
123 # define EXTRA_PARAMS
125 #ifndef EXTRA_VARIABLES
126 # define EXTRA_VARIABLES
132 # define AF_VAL AF_INET
135 /* Type of the lookup function we need here. */
136 typedef enum nss_status (*lookup_function
) (ADD_PARAMS
, LOOKUP_TYPE
*, char *,
137 size_t, int * H_ERRNO_PARM
140 /* The lookup function for the first entry of this service. */
141 extern int DB_LOOKUP_FCT (service_user
**nip
, const char *name
,
142 const char *name2
, void **fctp
)
144 libc_hidden_proto (DB_LOOKUP_FCT
)
148 INTERNAL (REENTRANT_NAME
) (ADD_PARAMS
, LOOKUP_TYPE
*resbuf
, char *buffer
,
149 size_t buflen
, LOOKUP_TYPE
**result H_ERRNO_PARM
152 static bool startp_initialized
;
153 static service_user
*startp
;
154 static lookup_function start_fct
;
163 enum nss_status status
= NSS_STATUS_UNAVAIL
;
168 bool any_service
= false;
175 #ifdef HANDLE_DIGITS_DOTS
176 switch (__nss_hostname_digits_dots (name
, resbuf
, &buffer
, NULL
,
177 buflen
, result
, &status
, AF_VAL
,
188 if (NOT_USENSCD_NAME
> 0 && ++NOT_USENSCD_NAME
> NSS_NSCD_RETRY
)
189 NOT_USENSCD_NAME
= 0;
191 if (!NOT_USENSCD_NAME
192 && !__nss_database_custom
[CONCAT2 (NSS_DBSIDX_
, DATABASE_NAME
)])
194 nscd_status
= NSCD_NAME (ADD_VARIABLES
, resbuf
, buffer
, buflen
, result
196 if (nscd_status
>= 0)
201 if (! startp_initialized
)
203 no_more
= DB_LOOKUP_FCT (&nip
, REENTRANT_NAME_STRING
,
204 REENTRANT2_NAME_STRING
, &fct
.ptr
);
207 void *tmp_ptr
= (service_user
*) -1l;
208 PTR_MANGLE (tmp_ptr
);
214 /* The resolver code will really be used so we have to
216 if (__res_maybe_init (&_res
, 0) == -1)
218 *h_errnop
= NETDB_INTERNAL
;
222 #endif /* need _res */
223 #ifdef NEED__RES_HCONF
224 if (!_res_hconf
.initialized
)
226 #endif /* need _res_hconf */
228 void *tmp_ptr
= fct
.l
;
229 PTR_MANGLE (tmp_ptr
);
232 PTR_MANGLE (tmp_ptr
);
236 /* Make sure start_fct and startp are written before
237 startp_initialized. */
238 atomic_write_barrier ();
239 startp_initialized
= true;
244 PTR_DEMANGLE (fct
.l
);
247 no_more
= nip
== (service_user
*) -1l;
256 status
= DL_CALL_FCT (fct
.l
, (ADD_VARIABLES
, resbuf
, buffer
, buflen
,
257 &errno H_ERRNO_VAR EXTRA_VARIABLES
));
259 /* The status is NSS_STATUS_TRYAGAIN and errno is ERANGE the
260 provided buffer is too small. In this case we should give
261 the user the possibility to enlarge the buffer and we should
262 not simply go on with the next service (even if the TRYAGAIN
263 action tells us so). */
264 if (status
== NSS_STATUS_TRYAGAIN
266 && *h_errnop
== NETDB_INTERNAL
271 no_more
= __nss_next2 (&nip
, REENTRANT_NAME_STRING
,
272 REENTRANT2_NAME_STRING
, &fct
.ptr
, status
, 0);
275 #ifdef HANDLE_DIGITS_DOTS
278 *result
= status
== NSS_STATUS_SUCCESS
? resbuf
: NULL
;
280 if (status
!= NSS_STATUS_SUCCESS
&& ! any_service
)
281 /* We were not able to use any service. */
282 *h_errnop
= NO_RECOVERY
;
289 if (status
== NSS_STATUS_SUCCESS
|| status
== NSS_STATUS_NOTFOUND
)
291 /* Don't pass back ERANGE if this is not for a too-small buffer. */
292 else if (errno
== ERANGE
&& status
!= NSS_STATUS_TRYAGAIN
)
295 /* These functions only set errno if h_errno is NETDB_INTERNAL. */
296 else if (status
== NSS_STATUS_TRYAGAIN
&& *h_errnop
!= NETDB_INTERNAL
)
307 #ifdef NO_COMPAT_NEEDED
308 strong_alias (INTERNAL (REENTRANT_NAME
), REENTRANT_NAME
);
309 #elif !defined FUNCTION2_NAME
310 # include <shlib-compat.h>
311 # if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_1_2)
312 # define OLD(name) OLD1 (name)
313 # define OLD1(name) __old_##name
316 attribute_compat_text_section
317 OLD (REENTRANT_NAME
) (ADD_PARAMS
, LOOKUP_TYPE
*resbuf
, char *buffer
,
318 size_t buflen
, LOOKUP_TYPE
**result H_ERRNO_PARM
)
320 int ret
= INTERNAL (REENTRANT_NAME
) (ADD_VARIABLES
, resbuf
, buffer
,
321 buflen
, result H_ERRNO_VAR
);
323 if (ret
!= 0 || result
== NULL
)
329 # define do_symbol_version(real, name, version) \
330 compat_symbol (libc, real, name, version)
331 do_symbol_version (OLD (REENTRANT_NAME
), REENTRANT_NAME
, GLIBC_2_0
);
334 /* As INTERNAL (REENTRANT_NAME) may be hidden, we need an alias
335 in between so that the REENTRANT_NAME@@GLIBC_2.1.2 is not
337 strong_alias (INTERNAL (REENTRANT_NAME
), NEW (REENTRANT_NAME
));
339 # define do_default_symbol_version(real, name, version) \
340 versioned_symbol (libc, real, name, version)
341 do_default_symbol_version (NEW (REENTRANT_NAME
),
342 REENTRANT_NAME
, GLIBC_2_1_2
);
345 static_link_warning (REENTRANT_NAME
)