Update.
[platform/upstream/glibc.git] / nss / getXXbyYY_r.c
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.
4
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.
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    Library General Public License for more details.
14
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.  */
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 #define DB_LOOKUP_FCT CONCAT3_1 (__nss_, DATABASE_NAME, _lookup)
83 #define CONCAT3_1(Pre, Name, Post) CONCAT3_2 (Pre, Name, Post)
84 #define CONCAT3_2(Pre, Name, Post) Pre##Name##Post
85
86 /* Sometimes we need to store error codes in the `h_errno' variable.  */
87 #ifdef NEED_H_ERRNO
88 # define H_ERRNO_PARM , int *h_errnop
89 # define H_ERRNO_VAR , h_errnop
90 #else
91 # define H_ERRNO_PARM
92 # define H_ERRNO_VAR
93 #endif
94
95
96 /* Type of the lookup function we need here.  */
97 typedef enum nss_status (*lookup_function) (ADD_PARAMS, LOOKUP_TYPE *, char *,
98                                             size_t, int * H_ERRNO_PARM);
99
100 /* The lookup function for the first entry of this service.  */
101 extern int DB_LOOKUP_FCT (service_user **nip, const char *name, void **fctp);
102
103 /* Interval in which we transfer retry to contact the NSCD.  */
104 #define NSS_NSCD_RETRY  100
105
106
107 int
108 INTERNAL (REENTRANT_NAME) (ADD_PARAMS, LOOKUP_TYPE *resbuf, char *buffer,
109                            size_t buflen, LOOKUP_TYPE **result H_ERRNO_PARM)
110 {
111   static service_user *startp;
112   static lookup_function start_fct;
113   service_user *nip;
114   lookup_function fct;
115   int no_more;
116   enum nss_status status = NSS_STATUS_UNAVAIL;
117 #ifdef USE_NSCD
118   int nscd_status;
119 #endif
120
121 #ifdef PREPROCESS
122   PREPROCESS;
123 #endif
124
125 #ifdef HANDLE_DIGITS_DOTS
126   /* We have to test for the use of IPv6 which can only be done by
127      examining `_res'.  */
128   if ((_res.options & RES_INIT) == 0 && __res_ninit (&_res) == -1)
129     {
130       *h_errnop = NETDB_INTERNAL;
131       *result = NULL;
132       return errno;
133     }
134 # define resbuf (*resbuf)
135 # include "digits_dots.c"
136 # undef resbuf
137 #endif
138
139 #ifdef USE_NSCD
140   if (NOT_USENSCD_NAME && ++NOT_USENSCD_NAME > NSS_NSCD_RETRY)
141     NOT_USENSCD_NAME = 0;
142
143   if (!NOT_USENSCD_NAME)
144     {
145       nscd_status = NSCD_NAME (ADD_VARIABLES, resbuf, buffer, buflen
146                                H_ERRNO_VAR);
147       if (nscd_status >= 0)
148         {
149           *result = nscd_status == 0 ? resbuf : NULL;
150           return nscd_status;
151         }
152     }
153 #endif
154
155   if (startp == NULL)
156     {
157       no_more = DB_LOOKUP_FCT (&nip, REENTRANT_NAME_STRING, (void **) &fct);
158       if (no_more)
159         startp = (service_user *) -1l;
160       else
161         {
162           startp = nip;
163           start_fct = fct;
164
165 #ifdef NEED__RES
166           /* The resolver code will really be used so we have to
167              initialize it.  */
168           if ((_res.options & RES_INIT) == 0 && __res_ninit (&_res) == -1)
169             {
170               *h_errnop = NETDB_INTERNAL;
171               *result = NULL;
172               return errno;
173             }
174 #endif /* need _res */
175 #ifdef NEED__RES_HCONF
176           if (!_res_hconf.initialized)
177             _res_hconf_init ();
178 #endif /* need _res_hconf */
179         }
180     }
181   else
182     {
183       fct = start_fct;
184       no_more = (nip = startp) == (service_user *) -1l;
185     }
186
187   while (no_more == 0)
188     {
189       status = DL_CALL_FCT (fct, (ADD_VARIABLES, resbuf, buffer, buflen,
190                                    &errno H_ERRNO_VAR));
191
192       /* The status is NSS_STATUS_TRYAGAIN and errno is ERANGE the
193          provided buffer is too small.  In this case we should give
194          the user the possibility to enlarge the buffer and we should
195          not simply go on with the next service (even if the TRYAGAIN
196          action tells us so).  */
197       if (status == NSS_STATUS_TRYAGAIN
198 #ifdef NEED_H_ERRNO
199           && *h_errnop == NETDB_INTERNAL
200 #endif
201           && errno == ERANGE)
202         break;
203
204       no_more = __nss_next (&nip, REENTRANT_NAME_STRING,
205                             (void **) &fct, status, 0);
206     }
207
208 #ifdef HANDLE_DIGITS_DOTS
209 done:
210 #endif
211   *result = status == NSS_STATUS_SUCCESS ? resbuf : NULL;
212 #ifdef POSTPROCESS
213   POSTPROCESS;
214 #endif
215   return status == NSS_STATUS_SUCCESS ? 0 : errno;
216 }
217
218 #if defined SHARED && DO_VERSIONING
219 #define OLD(name) OLD1 (name)
220 #define OLD1(name) __old_##name
221
222 int
223 OLD (REENTRANT_NAME) (ADD_PARAMS, LOOKUP_TYPE *resbuf, char *buffer,
224                       size_t buflen, LOOKUP_TYPE **result H_ERRNO_PARM)
225 {
226   int ret = INTERNAL (REENTRANT_NAME) (ADD_VARIABLES, resbuf, buffer,
227                                        buflen, result H_ERRNO_VAR);
228
229   if (ret != 0)
230     ret = -1;
231
232   return ret;
233 }
234
235 #define do_symbol_version(real, name, version) \
236   symbol_version(real, name, version)
237 do_symbol_version(OLD (REENTRANT_NAME), REENTRANT_NAME, GLIBC_2.0);
238
239 #define do_default_symbol_version(real, name, version) \
240   default_symbol_version(real, name, version)
241 do_default_symbol_version(INTERNAL (REENTRANT_NAME), REENTRANT_NAME,
242                           GLIBC_2.1.2);
243 #else
244 #define do_weak_alias(n1, n2) weak_alias (n1, n2)
245 do_weak_alias (INTERNAL (REENTRANT_NAME), REENTRANT_NAME)
246 #endif