Update.
[glibc.git] / nss / getXXbyYY_r.c
bloba3130e41ffcce0e66293e5e9579e001a5d189ddd
1 /* Copyright (C) 1996, 1997, 1998, 1999 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <assert.h>
21 #include <errno.h>
22 #include "nsswitch.h"
23 #ifdef USE_NSCD
24 # include <nscd/nscd_proto.h>
25 #endif
26 #ifdef NEED__RES_HCONF
27 # include <resolv/res_hconf.h>
28 #endif
30 /*******************************************************************\
31 |* Here we assume several symbols to be defined: *|
32 |* *|
33 |* LOOKUP_TYPE - the return type of the function *|
34 |* *|
35 |* FUNCTION_NAME - name of the non-reentrant function *|
36 |* *|
37 |* DATABASE_NAME - name of the database the function accesses *|
38 |* (e.g., host, services, ...) *|
39 |* *|
40 |* ADD_PARAMS - additional parameter, can vary in number *|
41 |* *|
42 |* ADD_VARIABLES - names of additional parameter *|
43 |* *|
44 |* Optionally the following vars can be defined: *|
45 |* *|
46 |* NEED_H_ERRNO - an extra parameter will be passed to point to *|
47 |* the global `h_errno' variable. *|
48 |* *|
49 |* NEED__RES - the global _res variable might be used so we *|
50 |* will have to initialize it if necessary *|
51 |* *|
52 |* PREPROCESS - code run before anything else *|
53 |* *|
54 |* POSTPROCESS - code run after the lookup *|
55 |* *|
56 \*******************************************************************/
58 /* To make the real sources a bit prettier. */
59 #define REENTRANT_NAME APPEND_R (FUNCTION_NAME)
60 #define APPEND_R(name) APPEND_R1 (name)
61 #define APPEND_R1(name) name##_r
62 #define INTERNAL(name) INTERNAL1 (name)
63 #define INTERNAL1(name) __##name
65 #ifdef USE_NSCD
66 # define NSCD_NAME ADD_NSCD (REENTRANT_NAME)
67 # define ADD_NSCD(name) ADD_NSCD1 (name)
68 # define ADD_NSCD1(name) __nscd_##name
69 # define NOT_USENSCD_NAME ADD_NOT_NSCDUSE (DATABASE_NAME)
70 # define ADD_NOT_NSCDUSE(name) ADD_NOT_NSCDUSE1 (name)
71 # define ADD_NOT_NSCDUSE1(name) __nss_not_use_nscd_##name
72 #endif
74 #define FUNCTION_NAME_STRING STRINGIZE (FUNCTION_NAME)
75 #define REENTRANT_NAME_STRING STRINGIZE (REENTRANT_NAME)
76 #define DATABASE_NAME_STRING STRINGIZE (DATABASE_NAME)
77 #define STRINGIZE(name) STRINGIZE1 (name)
78 #define STRINGIZE1(name) #name
80 #define DB_LOOKUP_FCT CONCAT3_1 (__nss_, DATABASE_NAME, _lookup)
81 #define CONCAT3_1(Pre, Name, Post) CONCAT3_2 (Pre, Name, Post)
82 #define CONCAT3_2(Pre, Name, Post) Pre##Name##Post
84 /* Sometimes we need to store error codes in the `h_errno' variable. */
85 #ifdef NEED_H_ERRNO
86 # define H_ERRNO_PARM , int *h_errnop
87 # define H_ERRNO_VAR , h_errnop
88 #else
89 # define H_ERRNO_PARM
90 # define H_ERRNO_VAR
91 #endif
94 /* Type of the lookup function we need here. */
95 typedef enum nss_status (*lookup_function) (ADD_PARAMS, LOOKUP_TYPE *, char *,
96 size_t, int * H_ERRNO_PARM);
98 /* Some usages of this file might use this variable. */
99 extern struct __res_state _res;
101 /* The lookup function for the first entry of this service. */
102 extern int DB_LOOKUP_FCT (service_user **nip, const char *name, void **fctp);
104 /* Interval in which we transfer retry to contact the NSCD. */
105 #define NSS_NSCD_RETRY 100
109 INTERNAL (REENTRANT_NAME) (ADD_PARAMS, LOOKUP_TYPE *resbuf, char *buffer,
110 size_t buflen, LOOKUP_TYPE **result H_ERRNO_PARM)
112 static service_user *startp;
113 static lookup_function start_fct;
114 service_user *nip;
115 lookup_function fct;
116 int no_more;
117 enum nss_status status = NSS_STATUS_UNAVAIL;
118 #ifdef USE_NSCD
119 int nscd_status;
120 #endif
122 #ifdef PREPROCESS
123 PREPROCESS;
124 #endif
126 #ifdef HANDLE_DIGITS_DOTS
127 /* We have to test for the use of IPv6 which can only be done by
128 examining `_res'. */
129 if ((_res.options & RES_INIT) == 0 && res_init () == -1)
131 *h_errnop = NETDB_INTERNAL;
132 *result = NULL;
133 return errno;
135 # define resbuf (*resbuf)
136 # include "digits_dots.c"
137 # undef resbuf
138 #endif
140 #ifdef USE_NSCD
141 if (NOT_USENSCD_NAME && ++NOT_USENSCD_NAME > NSS_NSCD_RETRY)
142 NOT_USENSCD_NAME = 0;
144 if (!NOT_USENSCD_NAME)
146 nscd_status = NSCD_NAME (ADD_VARIABLES, resbuf, buffer, buflen
147 H_ERRNO_VAR);
148 if (nscd_status >= 0)
150 *result = nscd_status == 0 ? resbuf : NULL;
151 return nscd_status;
154 #endif
156 if (startp == NULL)
158 no_more = DB_LOOKUP_FCT (&nip, REENTRANT_NAME_STRING, (void **) &fct);
159 if (no_more)
160 startp = (service_user *) -1l;
161 else
163 startp = nip;
164 start_fct = fct;
166 #ifdef NEED__RES
167 /* The resolver code will really be used so we have to
168 initialize it. */
169 if ((_res.options & RES_INIT) == 0 && res_init () == -1)
171 *h_errnop = NETDB_INTERNAL;
172 *result = NULL;
173 return errno;
175 #endif /* need _res */
176 #ifdef NEED__RES_HCONF
177 if (!_res_hconf.initialized)
178 _res_hconf_init ();
179 #endif /* need _res_hconf */
182 else
184 fct = start_fct;
185 no_more = (nip = startp) == (service_user *) -1l;
188 while (no_more == 0)
190 status = DL_CALL_FCT (fct, (ADD_VARIABLES, resbuf, buffer, buflen,
191 &errno H_ERRNO_VAR));
193 /* The status is NSS_STATUS_TRYAGAIN and errno is ERANGE the
194 provided buffer is too small. In this case we should give
195 the user the possibility to enlarge the buffer and we should
196 not simply go on with the next service (even if the TRYAGAIN
197 action tells us so). */
198 if (status == NSS_STATUS_TRYAGAIN
199 #ifdef NEED_H_ERRNO
200 && *h_errnop == NETDB_INTERNAL
201 #endif
202 && errno == ERANGE)
203 break;
205 no_more = __nss_next (&nip, REENTRANT_NAME_STRING,
206 (void **) &fct, status, 0);
209 #ifdef HANDLE_DIGITS_DOTS
210 done:
211 #endif
212 *result = status == NSS_STATUS_SUCCESS ? resbuf : NULL;
213 #ifdef POSTPROCESS
214 POSTPROCESS;
215 #endif
216 return status == NSS_STATUS_SUCCESS ? 0 : errno;
219 #if defined SHARED && DO_VERSIONING
220 #define OLD(name) OLD1 (name)
221 #define OLD1(name) __old_##name
224 OLD (REENTRANT_NAME) (ADD_PARAMS, LOOKUP_TYPE *resbuf, char *buffer,
225 size_t buflen, LOOKUP_TYPE **result H_ERRNO_PARM)
227 int ret = INTERNAL (REENTRANT_NAME) (ADD_VARIABLES, resbuf, buffer,
228 buflen, result H_ERRNO_VAR);
230 if (ret != 0)
231 ret = -1;
233 return ret;
236 #define do_symbol_version(real, name, version) \
237 symbol_version(real, name, version)
238 do_symbol_version(OLD (REENTRANT_NAME), REENTRANT_NAME, GLIBC_2.0);
240 #define do_default_symbol_version(real, name, version) \
241 default_symbol_version(real, name, version)
242 do_default_symbol_version(INTERNAL (REENTRANT_NAME), REENTRANT_NAME,
243 GLIBC_2.1.2);
244 #else
245 #define do_weak_alias(n1, n2) weak_alias (n1, n2)
246 do_weak_alias (INTERNAL (REENTRANT_NAME), REENTRANT_NAME)
247 #endif