(VERSION): Update from 2.3 release.
[glibc.git] / nss / getXXbyYY_r.c
blob630cea41584d1854afef57a7340ce5a139d3adf3
1 /* Copyright (C) 1996,97,98,99,2000,2001,2002 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 <errno.h>
22 #include <stdbool.h>
23 #include "nsswitch.h"
24 #ifdef USE_NSCD
25 # include <nscd/nscd_proto.h>
26 #endif
27 #ifdef NEED__RES_HCONF
28 # include <resolv/res_hconf.h>
29 #endif
30 #ifdef NEED__RES
31 # include <resolv.h>
32 #endif
33 /*******************************************************************\
34 |* Here we assume several symbols to be defined: *|
35 |* *|
36 |* LOOKUP_TYPE - the return type of the function *|
37 |* *|
38 |* FUNCTION_NAME - name of the non-reentrant function *|
39 |* *|
40 |* DATABASE_NAME - name of the database the function accesses *|
41 |* (e.g., host, services, ...) *|
42 |* *|
43 |* ADD_PARAMS - additional parameter, can vary in number *|
44 |* *|
45 |* ADD_VARIABLES - names of additional parameter *|
46 |* *|
47 |* Optionally the following vars can be defined: *|
48 |* *|
49 |* NEED_H_ERRNO - an extra parameter will be passed to point to *|
50 |* the global `h_errno' variable. *|
51 |* *|
52 |* NEED__RES - the global _res variable might be used so we *|
53 |* will have to initialize it if necessary *|
54 |* *|
55 |* PREPROCESS - code run before anything else *|
56 |* *|
57 |* POSTPROCESS - code run after the lookup *|
58 |* *|
59 \*******************************************************************/
61 /* To make the real sources a bit prettier. */
62 #define REENTRANT_NAME APPEND_R (FUNCTION_NAME)
63 #define APPEND_R(name) APPEND_R1 (name)
64 #define APPEND_R1(name) name##_r
65 #define INTERNAL(name) INTERNAL1 (name)
66 #define INTERNAL1(name) __##name
67 #define NEW(name) NEW1 (name)
68 #define NEW1(name) __new_##name
70 #ifdef USE_NSCD
71 # define NSCD_NAME ADD_NSCD (REENTRANT_NAME)
72 # define ADD_NSCD(name) ADD_NSCD1 (name)
73 # define ADD_NSCD1(name) __nscd_##name
74 # define NOT_USENSCD_NAME ADD_NOT_NSCDUSE (DATABASE_NAME)
75 # define ADD_NOT_NSCDUSE(name) ADD_NOT_NSCDUSE1 (name)
76 # define ADD_NOT_NSCDUSE1(name) __nss_not_use_nscd_##name
77 #endif
79 #define FUNCTION_NAME_STRING STRINGIZE (FUNCTION_NAME)
80 #define REENTRANT_NAME_STRING STRINGIZE (REENTRANT_NAME)
81 #define DATABASE_NAME_STRING STRINGIZE (DATABASE_NAME)
82 #define STRINGIZE(name) STRINGIZE1 (name)
83 #define STRINGIZE1(name) #name
85 #ifndef DB_LOOKUP_FCT
86 # define DB_LOOKUP_FCT CONCAT3_1 (__nss_, DATABASE_NAME, _lookup)
87 # define CONCAT3_1(Pre, Name, Post) CONCAT3_2 (Pre, Name, Post)
88 # define CONCAT3_2(Pre, Name, Post) Pre##Name##Post
89 #endif
91 /* Sometimes we need to store error codes in the `h_errno' variable. */
92 #ifdef NEED_H_ERRNO
93 # define H_ERRNO_PARM , int *h_errnop
94 # define H_ERRNO_VAR , h_errnop
95 # define H_ERRNO_VAR_P h_errnop
96 #else
97 # define H_ERRNO_PARM
98 # define H_ERRNO_VAR
99 # define H_ERRNO_VAR_P NULL
100 #endif
102 #ifndef HAVE_TYPE
103 # define TYPE_VAR_P NULL
104 # define FLAGS_VAR 0
105 #endif
107 #ifdef HAVE_AF
108 # define AF_VAR_P &af
109 #else
110 # define AF_VAR_P NULL
111 #endif
113 /* Type of the lookup function we need here. */
114 typedef enum nss_status (*lookup_function) (ADD_PARAMS, LOOKUP_TYPE *, char *,
115 size_t, int * H_ERRNO_PARM);
117 /* The lookup function for the first entry of this service. */
118 extern int DB_LOOKUP_FCT (service_user **nip, const char *name, void **fctp)
119 internal_function;
120 libc_hidden_proto (DB_LOOKUP_FCT)
122 /* Interval in which we transfer retry to contact the NSCD. */
123 #define NSS_NSCD_RETRY 100
127 INTERNAL (REENTRANT_NAME) (ADD_PARAMS, LOOKUP_TYPE *resbuf, char *buffer,
128 size_t buflen, LOOKUP_TYPE **result H_ERRNO_PARM)
130 static service_user *startp;
131 static lookup_function start_fct;
132 service_user *nip;
133 lookup_function fct;
134 int no_more;
135 enum nss_status status = NSS_STATUS_UNAVAIL;
136 #ifdef USE_NSCD
137 int nscd_status;
138 #endif
139 #ifdef NEED_H_ERRNO
140 bool any_service = false;
141 #endif
143 #ifdef PREPROCESS
144 PREPROCESS;
145 #endif
147 #ifdef HANDLE_DIGITS_DOTS
148 switch (__nss_hostname_digits_dots (name, resbuf, &buffer, NULL,
149 buflen, result, &status,
150 TYPE_VAR_P, FLAGS_VAR, AF_VAR_P,
151 H_ERRNO_VAR_P))
153 case -1:
154 return errno;
155 case 1:
156 goto done;
158 #endif
160 #ifdef USE_NSCD
161 if (NOT_USENSCD_NAME && ++NOT_USENSCD_NAME > NSS_NSCD_RETRY)
162 NOT_USENSCD_NAME = 0;
164 if (!NOT_USENSCD_NAME)
166 nscd_status = NSCD_NAME (ADD_VARIABLES, resbuf, buffer, buflen
167 H_ERRNO_VAR);
168 if (nscd_status >= 0)
170 *result = nscd_status == 0 ? resbuf : NULL;
171 return nscd_status;
174 #endif
176 if (startp == NULL)
178 no_more = DB_LOOKUP_FCT (&nip, REENTRANT_NAME_STRING, (void **) &fct);
179 if (no_more)
180 startp = (service_user *) -1l;
181 else
183 startp = nip;
184 start_fct = fct;
186 #ifdef NEED__RES
187 /* The resolver code will really be used so we have to
188 initialize it. */
189 if ((_res.options & RES_INIT) == 0 && __res_ninit (&_res) == -1)
191 *h_errnop = NETDB_INTERNAL;
192 *result = NULL;
193 return errno;
195 #endif /* need _res */
196 #ifdef NEED__RES_HCONF
197 if (!_res_hconf.initialized)
198 _res_hconf_init ();
199 #endif /* need _res_hconf */
202 else
204 fct = start_fct;
205 no_more = (nip = startp) == (service_user *) -1l;
208 while (no_more == 0)
210 #ifdef NEED_H_ERRNO
211 any_service = true;
212 #endif
214 status = DL_CALL_FCT (fct, (ADD_VARIABLES, resbuf, buffer, buflen,
215 &errno H_ERRNO_VAR));
217 /* The status is NSS_STATUS_TRYAGAIN and errno is ERANGE the
218 provided buffer is too small. In this case we should give
219 the user the possibility to enlarge the buffer and we should
220 not simply go on with the next service (even if the TRYAGAIN
221 action tells us so). */
222 if (status == NSS_STATUS_TRYAGAIN
223 #ifdef NEED_H_ERRNO
224 && *h_errnop == NETDB_INTERNAL
225 #endif
226 && errno == ERANGE)
227 break;
229 no_more = __nss_next (&nip, REENTRANT_NAME_STRING,
230 (void **) &fct, status, 0);
233 #ifdef HANDLE_DIGITS_DOTS
234 done:
235 #endif
236 *result = status == NSS_STATUS_SUCCESS ? resbuf : NULL;
237 #ifdef NEED_H_ERRNO
238 if (status != NSS_STATUS_SUCCESS && ! any_service)
239 /* We were not able to use any service. */
240 *h_errnop = NO_RECOVERY;
241 #endif
242 #ifdef POSTPROCESS
243 POSTPROCESS;
244 #endif
245 return (status == NSS_STATUS_SUCCESS
246 ? 0 : (status == NSS_STATUS_TRYAGAIN ? EAGAIN : ENOENT));
250 #include <shlib-compat.h>
251 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_1_2)
252 #define OLD(name) OLD1 (name)
253 #define OLD1(name) __old_##name
256 OLD (REENTRANT_NAME) (ADD_PARAMS, LOOKUP_TYPE *resbuf, char *buffer,
257 size_t buflen, LOOKUP_TYPE **result H_ERRNO_PARM)
259 int ret = INTERNAL (REENTRANT_NAME) (ADD_VARIABLES, resbuf, buffer,
260 buflen, result H_ERRNO_VAR);
262 if (ret != 0)
263 ret = -1;
265 return ret;
268 #define do_symbol_version(real, name, version) \
269 compat_symbol (libc, real, name, version)
270 do_symbol_version (OLD (REENTRANT_NAME), REENTRANT_NAME, GLIBC_2_0);
271 #endif
273 /* As INTERNAL (REENTRANT_NAME) may be hidden, we need an alias
274 in between so that the REENTRANT_NAME@@GLIBC_2.1.2 is not
275 hidden too. */
276 strong_alias (INTERNAL (REENTRANT_NAME), NEW (REENTRANT_NAME));
278 #define do_default_symbol_version(real, name, version) \
279 versioned_symbol (libc, real, name, version)
280 do_default_symbol_version (NEW (REENTRANT_NAME),
281 REENTRANT_NAME, GLIBC_2_1_2);