Updated to fedora-glibc-20090510T1842
[glibc/history.git] / nss / getXXbyYY_r.c
blob16dadd70ef493919bf0e5c6db80b962aea88cac7
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
18 02111-1307 USA. */
20 #include <assert.h>
21 #include <atomic.h>
22 #include <errno.h>
23 #include <stdbool.h>
24 #include "nsswitch.h"
25 #include "sysdep.h"
26 #ifdef USE_NSCD
27 # include <nscd/nscd_proto.h>
28 #endif
29 #ifdef NEED__RES_HCONF
30 # include <resolv/res_hconf.h>
31 #endif
32 #ifdef NEED__RES
33 # include <resolv.h>
34 #endif
35 /*******************************************************************\
36 |* Here we assume several symbols to be defined: *|
37 |* *|
38 |* LOOKUP_TYPE - the return type of the function *|
39 |* *|
40 |* FUNCTION_NAME - name of the non-reentrant function *|
41 |* *|
42 |* DATABASE_NAME - name of the database the function accesses *|
43 |* (e.g., host, services, ...) *|
44 |* *|
45 |* ADD_PARAMS - additional parameters, can vary in number *|
46 |* *|
47 |* ADD_VARIABLES - names of additional parameters *|
48 |* *|
49 |* Optionally the following vars can be defined: *|
50 |* *|
51 |* EXTRA_PARAMS - optional parameters, can vary in number *|
52 |* *|
53 |* EXTRA_VARIABLES - names of optional parameter *|
54 |* *|
55 |* FUNCTION_NAME - alternative name of the non-reentrant function *|
56 |* *|
57 |* NEED_H_ERRNO - an extra parameter will be passed to point to *|
58 |* the global `h_errno' variable. *|
59 |* *|
60 |* NEED__RES - the global _res variable might be used so we *|
61 |* will have to initialize it if necessary *|
62 |* *|
63 |* PREPROCESS - code run before anything else *|
64 |* *|
65 |* POSTPROCESS - code run after the lookup *|
66 |* *|
67 \*******************************************************************/
69 /* To make the real sources a bit prettier. */
70 #define REENTRANT_NAME APPEND_R (FUNCTION_NAME)
71 #ifdef FUNCTION2_NAME
72 # define REENTRANT2_NAME APPEND_R (FUNCTION2_NAME)
73 #else
74 # define REENTRANT2_NAME NULL
75 #endif
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
83 #ifdef USE_NSCD
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 #endif
92 #define FUNCTION_NAME_STRING STRINGIZE (FUNCTION_NAME)
93 #define REENTRANT_NAME_STRING STRINGIZE (REENTRANT_NAME)
94 #ifdef FUNCTION2_NAME
95 # define REENTRANT2_NAME_STRING STRINGIZE (REENTRANT2_NAME)
96 #else
97 # define REENTRANT2_NAME_STRING NULL
98 #endif
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
107 #endif
109 /* Sometimes we need to store error codes in the `h_errno' variable. */
110 #ifdef NEED_H_ERRNO
111 # define H_ERRNO_PARM , int *h_errnop
112 # define H_ERRNO_VAR , h_errnop
113 # define H_ERRNO_VAR_P h_errnop
114 #else
115 # define H_ERRNO_PARM
116 # define H_ERRNO_VAR
117 # define H_ERRNO_VAR_P NULL
118 #endif
120 #ifndef EXTRA_PARAMS
121 # define EXTRA_PARAMS
122 #endif
123 #ifndef EXTRA_VARIABLES
124 # define EXTRA_VARIABLES
125 #endif
127 #ifdef HAVE_AF
128 # define AF_VAL af
129 #else
130 # define AF_VAL AF_INET
131 #endif
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
136 EXTRA_PARAMS);
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)
141 internal_function;
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
148 EXTRA_PARAMS)
150 static bool startp_initialized;
151 static service_user *startp;
152 static lookup_function start_fct;
153 service_user *nip;
154 union
156 lookup_function l;
157 void *ptr;
158 } fct;
160 int no_more;
161 enum nss_status status = NSS_STATUS_UNAVAIL;
162 #ifdef USE_NSCD
163 int nscd_status;
164 #endif
165 #ifdef NEED_H_ERRNO
166 bool any_service = false;
167 #endif
169 #ifdef PREPROCESS
170 PREPROCESS;
171 #endif
173 #ifdef HANDLE_DIGITS_DOTS
174 switch (__nss_hostname_digits_dots (name, resbuf, &buffer, NULL,
175 buflen, result, &status, AF_VAL,
176 H_ERRNO_VAR_P))
178 case -1:
179 return errno;
180 case 1:
181 goto done;
183 #endif
185 #ifdef USE_NSCD
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
192 H_ERRNO_VAR);
193 if (nscd_status >= 0)
194 return nscd_status;
196 #endif
198 if (! startp_initialized)
200 no_more = DB_LOOKUP_FCT (&nip, REENTRANT_NAME_STRING,
201 REENTRANT2_NAME_STRING, &fct.ptr);
202 if (no_more)
204 void *tmp_ptr = (service_user *) -1l;
205 PTR_MANGLE (tmp_ptr);
206 startp = tmp_ptr;
208 else
210 #ifdef NEED__RES
211 /* The resolver code will really be used so we have to
212 initialize it. */
213 if (__res_maybe_init (&_res, 0) == -1)
215 *h_errnop = NETDB_INTERNAL;
216 *result = NULL;
217 return errno;
219 #endif /* need _res */
220 #ifdef NEED__RES_HCONF
221 if (!_res_hconf.initialized)
222 _res_hconf_init ();
223 #endif /* need _res_hconf */
225 void *tmp_ptr = fct.l;
226 PTR_MANGLE (tmp_ptr);
227 start_fct = tmp_ptr;
228 tmp_ptr = nip;
229 PTR_MANGLE (tmp_ptr);
230 startp = tmp_ptr;
233 /* Make sure start_fct and startp are written before
234 startp_initialized. */
235 atomic_write_barrier ();
236 startp_initialized = true;
238 else
240 fct.l = start_fct;
241 PTR_DEMANGLE (fct.l);
242 nip = startp;
243 PTR_DEMANGLE (nip);
244 no_more = nip == (service_user *) -1l;
247 while (no_more == 0)
249 #ifdef NEED_H_ERRNO
250 any_service = true;
251 #endif
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
262 #ifdef NEED_H_ERRNO
263 && *h_errnop == NETDB_INTERNAL
264 #endif
265 && errno == ERANGE)
266 break;
268 no_more = __nss_next2 (&nip, REENTRANT_NAME_STRING,
269 REENTRANT2_NAME_STRING, &fct.ptr, status, 0);
272 #ifdef HANDLE_DIGITS_DOTS
273 done:
274 #endif
275 *result = status == NSS_STATUS_SUCCESS ? resbuf : NULL;
276 #ifdef NEED_H_ERRNO
277 if (status != NSS_STATUS_SUCCESS && ! any_service)
278 /* We were not able to use any service. */
279 *h_errnop = NO_RECOVERY;
280 #endif
281 #ifdef POSTPROCESS
282 POSTPROCESS;
283 #endif
285 int res;
286 if (status == NSS_STATUS_SUCCESS || status == NSS_STATUS_NOTFOUND)
287 res = 0;
288 /* Don't pass back ERANGE if this is not for a too-small buffer. */
289 else if (errno == ERANGE && status != NSS_STATUS_TRYAGAIN)
290 res = EINVAL;
291 #ifdef NEED_H_ERRNO
292 /* These functions only set errno if h_errno is NETDB_INTERNAL. */
293 else if (status == NSS_STATUS_TRYAGAIN && *h_errnop != NETDB_INTERNAL)
294 res = EAGAIN;
295 #endif
296 else
297 return errno;
299 __set_errno (res);
300 return res;
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)
321 ret = -1;
323 return ret;
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);
329 # endif
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
333 hidden too. */
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);
340 #endif
342 static_link_warning (REENTRANT_NAME)