Fix extension of array in extended printf format handling
[glibc.git] / nss / getXXbyYY_r.c
blob0dbb0030e93e61880e120d0ab5793afbab97679d
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
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 # define CONCAT2(arg1, arg2) CONCAT2_2 (arg1, arg2)
91 # define CONCAT2_2(arg1, arg2) arg1##arg2
92 #endif
94 #define FUNCTION_NAME_STRING STRINGIZE (FUNCTION_NAME)
95 #define REENTRANT_NAME_STRING STRINGIZE (REENTRANT_NAME)
96 #ifdef FUNCTION2_NAME
97 # define REENTRANT2_NAME_STRING STRINGIZE (REENTRANT2_NAME)
98 #else
99 # define REENTRANT2_NAME_STRING NULL
100 #endif
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
109 #endif
111 /* Sometimes we need to store error codes in the `h_errno' variable. */
112 #ifdef NEED_H_ERRNO
113 # define H_ERRNO_PARM , int *h_errnop
114 # define H_ERRNO_VAR , h_errnop
115 # define H_ERRNO_VAR_P h_errnop
116 #else
117 # define H_ERRNO_PARM
118 # define H_ERRNO_VAR
119 # define H_ERRNO_VAR_P NULL
120 #endif
122 #ifndef EXTRA_PARAMS
123 # define EXTRA_PARAMS
124 #endif
125 #ifndef EXTRA_VARIABLES
126 # define EXTRA_VARIABLES
127 #endif
129 #ifdef HAVE_AF
130 # define AF_VAL af
131 #else
132 # define AF_VAL AF_INET
133 #endif
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
138 EXTRA_PARAMS);
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)
143 internal_function;
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
150 EXTRA_PARAMS)
152 static bool startp_initialized;
153 static service_user *startp;
154 static lookup_function start_fct;
155 service_user *nip;
156 union
158 lookup_function l;
159 void *ptr;
160 } fct;
162 int no_more;
163 enum nss_status status = NSS_STATUS_UNAVAIL;
164 #ifdef USE_NSCD
165 int nscd_status;
166 #endif
167 #ifdef NEED_H_ERRNO
168 bool any_service = false;
169 #endif
171 #ifdef PREPROCESS
172 PREPROCESS;
173 #endif
175 #ifdef HANDLE_DIGITS_DOTS
176 switch (__nss_hostname_digits_dots (name, resbuf, &buffer, NULL,
177 buflen, result, &status, AF_VAL,
178 H_ERRNO_VAR_P))
180 case -1:
181 return errno;
182 case 1:
183 goto done;
185 #endif
187 #ifdef USE_NSCD
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
195 H_ERRNO_VAR);
196 if (nscd_status >= 0)
197 return nscd_status;
199 #endif
201 if (! startp_initialized)
203 no_more = DB_LOOKUP_FCT (&nip, REENTRANT_NAME_STRING,
204 REENTRANT2_NAME_STRING, &fct.ptr);
205 if (no_more)
207 void *tmp_ptr = (service_user *) -1l;
208 PTR_MANGLE (tmp_ptr);
209 startp = tmp_ptr;
211 else
213 #ifdef NEED__RES
214 /* The resolver code will really be used so we have to
215 initialize it. */
216 if (__res_maybe_init (&_res, 0) == -1)
218 *h_errnop = NETDB_INTERNAL;
219 *result = NULL;
220 return errno;
222 #endif /* need _res */
223 #ifdef NEED__RES_HCONF
224 if (!_res_hconf.initialized)
225 _res_hconf_init ();
226 #endif /* need _res_hconf */
228 void *tmp_ptr = fct.l;
229 PTR_MANGLE (tmp_ptr);
230 start_fct = tmp_ptr;
231 tmp_ptr = nip;
232 PTR_MANGLE (tmp_ptr);
233 startp = tmp_ptr;
236 /* Make sure start_fct and startp are written before
237 startp_initialized. */
238 atomic_write_barrier ();
239 startp_initialized = true;
241 else
243 fct.l = start_fct;
244 PTR_DEMANGLE (fct.l);
245 nip = startp;
246 PTR_DEMANGLE (nip);
247 no_more = nip == (service_user *) -1l;
250 while (no_more == 0)
252 #ifdef NEED_H_ERRNO
253 any_service = true;
254 #endif
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
265 #ifdef NEED_H_ERRNO
266 && *h_errnop == NETDB_INTERNAL
267 #endif
268 && errno == ERANGE)
269 break;
271 no_more = __nss_next2 (&nip, REENTRANT_NAME_STRING,
272 REENTRANT2_NAME_STRING, &fct.ptr, status, 0);
275 #ifdef HANDLE_DIGITS_DOTS
276 done:
277 #endif
278 *result = status == NSS_STATUS_SUCCESS ? resbuf : NULL;
279 #ifdef NEED_H_ERRNO
280 if (status != NSS_STATUS_SUCCESS && ! any_service)
281 /* We were not able to use any service. */
282 *h_errnop = NO_RECOVERY;
283 #endif
284 #ifdef POSTPROCESS
285 POSTPROCESS;
286 #endif
288 int res;
289 if (status == NSS_STATUS_SUCCESS || status == NSS_STATUS_NOTFOUND)
290 res = 0;
291 /* Don't pass back ERANGE if this is not for a too-small buffer. */
292 else if (errno == ERANGE && status != NSS_STATUS_TRYAGAIN)
293 res = EINVAL;
294 #ifdef NEED_H_ERRNO
295 /* These functions only set errno if h_errno is NETDB_INTERNAL. */
296 else if (status == NSS_STATUS_TRYAGAIN && *h_errnop != NETDB_INTERNAL)
297 res = EAGAIN;
298 #endif
299 else
300 return errno;
302 __set_errno (res);
303 return res;
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)
324 ret = -1;
326 return ret;
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);
332 # endif
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
336 hidden too. */
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);
343 #endif
345 static_link_warning (REENTRANT_NAME)