]> git.ipfire.org Git - thirdparty/glibc.git/blob - nss/getXXbyYY_r.c
ecd28ad2c2c49dbb10b5c623b7bac9d7ba1978aa
[thirdparty/glibc.git] / nss / getXXbyYY_r.c
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.
4
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.
9
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.
14
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. */
19
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
29 #ifdef NEED__RES
30 # include <resolv.h>
31 #endif
32 /*******************************************************************\
33 |* Here we assume several symbols to be defined: *|
34 |* *|
35 |* LOOKUP_TYPE - the return type of the function *|
36 |* *|
37 |* FUNCTION_NAME - name of the non-reentrant function *|
38 |* *|
39 |* DATABASE_NAME - name of the database the function accesses *|
40 |* (e.g., host, services, ...) *|
41 |* *|
42 |* ADD_PARAMS - additional parameter, can vary in number *|
43 |* *|
44 |* ADD_VARIABLES - names of additional parameter *|
45 |* *|
46 |* Optionally the following vars can be defined: *|
47 |* *|
48 |* NEED_H_ERRNO - an extra parameter will be passed to point to *|
49 |* the global `h_errno' variable. *|
50 |* *|
51 |* NEED__RES - the global _res variable might be used so we *|
52 |* will have to initialize it if necessary *|
53 |* *|
54 |* PREPROCESS - code run before anything else *|
55 |* *|
56 |* POSTPROCESS - code run after the lookup *|
57 |* *|
58 \*******************************************************************/
59
60 /* To make the real sources a bit prettier. */
61 #define REENTRANT_NAME APPEND_R (FUNCTION_NAME)
62 #define APPEND_R(name) APPEND_R1 (name)
63 #define APPEND_R1(name) name##_r
64 #define INTERNAL(name) INTERNAL1 (name)
65 #define INTERNAL1(name) __##name
66
67 #ifdef USE_NSCD
68 # define NSCD_NAME ADD_NSCD (REENTRANT_NAME)
69 # define ADD_NSCD(name) ADD_NSCD1 (name)
70 # define ADD_NSCD1(name) __nscd_##name
71 # define NOT_USENSCD_NAME ADD_NOT_NSCDUSE (DATABASE_NAME)
72 # define ADD_NOT_NSCDUSE(name) ADD_NOT_NSCDUSE1 (name)
73 # define ADD_NOT_NSCDUSE1(name) __nss_not_use_nscd_##name
74 #endif
75
76 #define FUNCTION_NAME_STRING STRINGIZE (FUNCTION_NAME)
77 #define REENTRANT_NAME_STRING STRINGIZE (REENTRANT_NAME)
78 #define DATABASE_NAME_STRING STRINGIZE (DATABASE_NAME)
79 #define STRINGIZE(name) STRINGIZE1 (name)
80 #define STRINGIZE1(name) #name
81
82 #ifndef DB_LOOKUP_FCT
83 # define DB_LOOKUP_FCT CONCAT3_1 (__nss_, DATABASE_NAME, _lookup)
84 # define CONCAT3_1(Pre, Name, Post) CONCAT3_2 (Pre, Name, Post)
85 # define CONCAT3_2(Pre, Name, Post) Pre##Name##Post
86 #endif
87
88 /* Sometimes we need to store error codes in the `h_errno' variable. */
89 #ifdef NEED_H_ERRNO
90 # define H_ERRNO_PARM , int *h_errnop
91 # define H_ERRNO_VAR , h_errnop
92 # define H_ERRNO_VAR_P h_errnop
93 #else
94 # define H_ERRNO_PARM
95 # define H_ERRNO_VAR
96 # define H_ERRNO_VAR_P NULL
97 #endif
98
99 #ifndef HAVE_TYPE
100 # define TYPE_VAR_P NULL
101 # define FLAGS_VAR 0
102 #endif
103
104 #ifdef HAVE_AF
105 # define AF_VAR_P &af
106 #else
107 # define AF_VAR_P NULL
108 #endif
109
110 /* Type of the lookup function we need here. */
111 typedef enum nss_status (*lookup_function) (ADD_PARAMS, LOOKUP_TYPE *, char *,
112 size_t, int * H_ERRNO_PARM);
113
114 /* The lookup function for the first entry of this service. */
115 extern int DB_LOOKUP_FCT (service_user **nip, const char *name, void **fctp)
116 internal_function;
117
118 /* Interval in which we transfer retry to contact the NSCD. */
119 #define NSS_NSCD_RETRY 100
120
121
122 int
123 INTERNAL (REENTRANT_NAME) (ADD_PARAMS, LOOKUP_TYPE *resbuf, char *buffer,
124 size_t buflen, LOOKUP_TYPE **result H_ERRNO_PARM)
125 {
126 static service_user *startp;
127 static lookup_function start_fct;
128 service_user *nip;
129 lookup_function fct;
130 int no_more;
131 enum nss_status status = NSS_STATUS_UNAVAIL;
132 #ifdef USE_NSCD
133 int nscd_status;
134 #endif
135
136 #ifdef PREPROCESS
137 PREPROCESS;
138 #endif
139
140 #ifdef HANDLE_DIGITS_DOTS
141 switch (__nss_hostname_digits_dots (name, resbuf, &buffer, NULL,
142 buflen, result, &status,
143 TYPE_VAR_P, FLAGS_VAR, AF_VAR_P,
144 H_ERRNO_VAR_P))
145 {
146 case -1:
147 return errno;
148 case 1:
149 goto done;
150 }
151 #endif
152
153 #ifdef USE_NSCD
154 if (NOT_USENSCD_NAME && ++NOT_USENSCD_NAME > NSS_NSCD_RETRY)
155 NOT_USENSCD_NAME = 0;
156
157 if (!NOT_USENSCD_NAME)
158 {
159 nscd_status = NSCD_NAME (ADD_VARIABLES, resbuf, buffer, buflen
160 H_ERRNO_VAR);
161 if (nscd_status >= 0)
162 {
163 *result = nscd_status == 0 ? resbuf : NULL;
164 return nscd_status;
165 }
166 }
167 #endif
168
169 if (startp == NULL)
170 {
171 no_more = DB_LOOKUP_FCT (&nip, REENTRANT_NAME_STRING, (void **) &fct);
172 if (no_more)
173 startp = (service_user *) -1l;
174 else
175 {
176 startp = nip;
177 start_fct = fct;
178
179 #ifdef NEED__RES
180 /* The resolver code will really be used so we have to
181 initialize it. */
182 if ((_res.options & RES_INIT) == 0 && __res_ninit (&_res) == -1)
183 {
184 *h_errnop = NETDB_INTERNAL;
185 *result = NULL;
186 return errno;
187 }
188 #endif /* need _res */
189 #ifdef NEED__RES_HCONF
190 if (!_res_hconf.initialized)
191 _res_hconf_init ();
192 #endif /* need _res_hconf */
193 }
194 }
195 else
196 {
197 fct = start_fct;
198 no_more = (nip = startp) == (service_user *) -1l;
199 }
200
201 while (no_more == 0)
202 {
203 status = DL_CALL_FCT (fct, (ADD_VARIABLES, resbuf, buffer, buflen,
204 &errno H_ERRNO_VAR));
205
206 /* The status is NSS_STATUS_TRYAGAIN and errno is ERANGE the
207 provided buffer is too small. In this case we should give
208 the user the possibility to enlarge the buffer and we should
209 not simply go on with the next service (even if the TRYAGAIN
210 action tells us so). */
211 if (status == NSS_STATUS_TRYAGAIN
212 #ifdef NEED_H_ERRNO
213 && *h_errnop == NETDB_INTERNAL
214 #endif
215 && errno == ERANGE)
216 break;
217
218 no_more = __nss_next (&nip, REENTRANT_NAME_STRING,
219 (void **) &fct, status, 0);
220 }
221
222 #ifdef HANDLE_DIGITS_DOTS
223 done:
224 #endif
225 *result = status == NSS_STATUS_SUCCESS ? resbuf : NULL;
226 #ifdef POSTPROCESS
227 POSTPROCESS;
228 #endif
229 return status == NSS_STATUS_SUCCESS ? 0 : errno;
230 }
231
232
233 #include <shlib-compat.h>
234 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_1_2)
235 #define OLD(name) OLD1 (name)
236 #define OLD1(name) __old_##name
237
238 int
239 OLD (REENTRANT_NAME) (ADD_PARAMS, LOOKUP_TYPE *resbuf, char *buffer,
240 size_t buflen, LOOKUP_TYPE **result H_ERRNO_PARM)
241 {
242 int ret = INTERNAL (REENTRANT_NAME) (ADD_VARIABLES, resbuf, buffer,
243 buflen, result H_ERRNO_VAR);
244
245 if (ret != 0)
246 ret = -1;
247
248 return ret;
249 }
250
251 #define do_symbol_version(real, name, version) \
252 compat_symbol (libc, real, name, version)
253 do_symbol_version (OLD (REENTRANT_NAME), REENTRANT_NAME, GLIBC_2_0);
254 #endif
255
256 #define do_default_symbol_version(real, name, version) \
257 versioned_symbol (libc, real, name, version)
258 do_default_symbol_version (INTERNAL (REENTRANT_NAME),
259 REENTRANT_NAME, GLIBC_2_1_2);