chiark / gitweb /
Apply https://sourceware.org/git/?p=glibc.git;a=commit;h=d5dd6189d506068ed11c8bfa1e1e...
[eglibc.git] / nss / getXXbyYY_r.c
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.
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 <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 \*******************************************************************/
68
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
82
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
91
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
102
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
108
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
119
120 #ifndef EXTRA_PARAMS
121 # define EXTRA_PARAMS
122 #endif
123 #ifndef EXTRA_VARIABLES
124 # define EXTRA_VARIABLES
125 #endif
126
127 #ifdef HAVE_AF
128 # define AF_VAL af
129 #else
130 # define AF_VAL AF_INET
131 #endif
132
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);
137
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)
143
144
145 int
146 INTERNAL (REENTRANT_NAME) (ADD_PARAMS, LOOKUP_TYPE *resbuf, char *buffer,
147                            size_t buflen, LOOKUP_TYPE **result H_ERRNO_PARM
148                            EXTRA_PARAMS)
149 {
150   static bool startp_initialized;
151   static service_user *startp;
152   static lookup_function start_fct;
153   service_user *nip;
154   union
155   {
156     lookup_function l;
157     void *ptr;
158   } fct;
159
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
168
169 #ifdef PREPROCESS
170   PREPROCESS;
171 #endif
172
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))
177     {
178     case -1:
179       return errno;
180     case 1:
181 #ifdef NEED_H_ERRNO
182       any_service = true;
183 #endif
184       goto done;
185     }
186 #endif
187
188 #ifdef USE_NSCD
189   if (NOT_USENSCD_NAME > 0 && ++NOT_USENSCD_NAME > NSS_NSCD_RETRY)
190     NOT_USENSCD_NAME = 0;
191
192   if (!NOT_USENSCD_NAME)
193     {
194       nscd_status = NSCD_NAME (ADD_VARIABLES, resbuf, buffer, buflen, result
195                                H_ERRNO_VAR);
196       if (nscd_status >= 0)
197         return nscd_status;
198     }
199 #endif
200
201   if (! startp_initialized)
202     {
203       no_more = DB_LOOKUP_FCT (&nip, REENTRANT_NAME_STRING,
204                                REENTRANT2_NAME_STRING, &fct.ptr);
205       if (no_more)
206         {
207           void *tmp_ptr = (service_user *) -1l;
208           PTR_MANGLE (tmp_ptr);
209           startp = tmp_ptr;
210         }
211       else
212         {
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)
217             {
218               *h_errnop = NETDB_INTERNAL;
219               *result = NULL;
220               return errno;
221             }
222 #endif /* need _res */
223 #ifdef NEED__RES_HCONF
224           if (!_res_hconf.initialized)
225             _res_hconf_init ();
226 #endif /* need _res_hconf */
227
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;
234         }
235
236       /* Make sure start_fct and startp are written before
237          startp_initialized.  */
238       atomic_write_barrier ();
239       startp_initialized = true;
240     }
241   else
242     {
243       fct.l = start_fct;
244       PTR_DEMANGLE (fct.l);
245       nip = startp;
246       PTR_DEMANGLE (nip);
247       no_more = nip == (service_user *) -1l;
248     }
249
250   while (no_more == 0)
251     {
252 #ifdef NEED_H_ERRNO
253       any_service = true;
254 #endif
255
256       status = DL_CALL_FCT (fct.l, (ADD_VARIABLES, resbuf, buffer, buflen,
257                                     &errno H_ERRNO_VAR EXTRA_VARIABLES));
258
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;
270
271       no_more = __nss_next2 (&nip, REENTRANT_NAME_STRING,
272                              REENTRANT2_NAME_STRING, &fct.ptr, status, 0);
273     }
274
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
287
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;
301
302   __set_errno (res);
303   return res;
304 }
305
306
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
314
315 int
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)
319 {
320   int ret = INTERNAL (REENTRANT_NAME) (ADD_VARIABLES, resbuf, buffer,
321                                        buflen, result H_ERRNO_VAR);
322
323   if (ret != 0 || result == NULL)
324     ret = -1;
325
326   return ret;
327 }
328
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
333
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));
338
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
344
345 static_link_warning (REENTRANT_NAME)