]> git.ipfire.org Git - thirdparty/glibc.git/blob - nss/getXXbyYY_r.c
Update.
[thirdparty/glibc.git] / nss / getXXbyYY_r.c
1 /* Copyright (C) 1996, 1997, 1998 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 <errno.h>
21 #include "nsswitch.h"
22 #ifdef USE_NSCD
23 # include <nscd/nscd_proto.h>
24 #endif
25 #ifdef NEED__RES_HCONF
26 # include <resolv/res_hconf.h>
27 #endif
28
29 /*******************************************************************\
30 |* Here we assume several symbols to be defined: *|
31 |* *|
32 |* LOOKUP_TYPE - the return type of the function *|
33 |* *|
34 |* FUNCTION_NAME - name of the non-reentrant function *|
35 |* *|
36 |* DATABASE_NAME - name of the database the function accesses *|
37 |* (e.g., host, services, ...) *|
38 |* *|
39 |* ADD_PARAMS - additional parameter, can vary in number *|
40 |* *|
41 |* ADD_VARIABLES - names of additional parameter *|
42 |* *|
43 |* Optionally the following vars can be defined: *|
44 |* *|
45 |* NEED_H_ERRNO - an extra parameter will be passed to point to *|
46 |* the global `h_errno' variable. *|
47 |* *|
48 |* NEED__RES - the global _res variable might be used so we *|
49 |* will have to initialize it if necessary *|
50 |* *|
51 \*******************************************************************/
52
53 /* To make the real sources a bit prettier. */
54 #define REENTRANT_NAME APPEND_R (FUNCTION_NAME)
55 #define APPEND_R(name) APPEND_R1 (name)
56 #define APPEND_R1(name) name##_r
57 #define INTERNAL(name) INTERNAL1 (name)
58 #define INTERNAL1(name) __##name
59
60 #ifdef USE_NSCD
61 # define NSCD_NAME ADD_NSCD (REENTRANT_NAME)
62 # define ADD_NSCD(name) ADD_NSCD1 (name)
63 # define ADD_NSCD1(name) __nscd_##name
64 # define NOT_USENSCD_NAME ADD_NOT_NSCDUSE (DATABASE_NAME)
65 # define ADD_NOT_NSCDUSE(name) ADD_NOT_NSCDUSE1 (name)
66 # define ADD_NOT_NSCDUSE1(name) __nss_not_use_nscd_##name
67 #endif
68
69 #define FUNCTION_NAME_STRING STRINGIZE (FUNCTION_NAME)
70 #define REENTRANT_NAME_STRING STRINGIZE (REENTRANT_NAME)
71 #define DATABASE_NAME_STRING STRINGIZE (DATABASE_NAME)
72 #define STRINGIZE(name) STRINGIZE1 (name)
73 #define STRINGIZE1(name) #name
74
75 #define DB_LOOKUP_FCT CONCAT3_1 (__nss_, DATABASE_NAME, _lookup)
76 #define CONCAT3_1(Pre, Name, Post) CONCAT3_2 (Pre, Name, Post)
77 #define CONCAT3_2(Pre, Name, Post) Pre##Name##Post
78
79 /* Sometimes we need to store error codes in the `h_errno' variable. */
80 #ifdef NEED_H_ERRNO
81 # define H_ERRNO_PARM , int *h_errnop
82 # define H_ERRNO_VAR , h_errnop
83 #else
84 # define H_ERRNO_PARM
85 # define H_ERRNO_VAR
86 #endif
87
88
89 /* Type of the lookup function we need here. */
90 typedef enum nss_status (*lookup_function) (ADD_PARAMS, LOOKUP_TYPE *, char *,
91 size_t, int * H_ERRNO_PARM);
92
93 /* Some usages of this file might use this variable. */
94 extern struct __res_state _res;
95
96 /* The lookup function for the first entry of this service. */
97 extern int DB_LOOKUP_FCT (service_user **nip, const char *name, void **fctp);
98
99 /* Interval in which we transfer retry to contact the NSCD. */
100 #define NSS_NSCD_RETRY 100
101
102
103 int
104 INTERNAL (REENTRANT_NAME) (ADD_PARAMS, LOOKUP_TYPE *resbuf, char *buffer,
105 size_t buflen, LOOKUP_TYPE **result H_ERRNO_PARM)
106 {
107 static service_user *startp = NULL;
108 static lookup_function start_fct;
109 service_user *nip;
110 lookup_function fct;
111 int no_more;
112 enum nss_status status = NSS_STATUS_UNAVAIL;
113 #ifdef USE_NSCD
114 int nscd_status;
115 #endif
116
117 #ifdef HANDLE_DIGITS_DOTS
118 # define resbuf (*resbuf)
119 # include "digits_dots.c"
120 # undef resbuf
121 #endif
122
123 #ifdef USE_NSCD
124 if (NOT_USENSCD_NAME && ++NOT_USENSCD_NAME > NSS_NSCD_RETRY)
125 NOT_USENSCD_NAME = 0;
126
127 if (!NOT_USENSCD_NAME)
128 {
129 nscd_status = NSCD_NAME (ADD_VARIABLES, resbuf, buffer, buflen
130 H_ERRNO_VAR);
131 if (nscd_status < 1)
132 {
133 *result = nscd_status == 0 ? resbuf : NULL;
134 return nscd_status;
135 }
136 }
137 #endif
138
139 if (startp == NULL)
140 {
141 no_more = DB_LOOKUP_FCT (&nip, REENTRANT_NAME_STRING, (void **) &fct);
142 if (no_more)
143 startp = (service_user *) -1l;
144 else
145 {
146 startp = nip;
147 start_fct = fct;
148
149 #ifdef NEED__RES
150 /* The resolver code will really be used so we have to
151 initialize it. */
152 if ((_res.options & RES_INIT) == 0 && res_init () == -1)
153 {
154 *h_errnop = NETDB_INTERNAL;
155 *result = NULL;
156 return -1;
157 }
158 #endif /* need _res */
159 #ifdef NEED__RES_HCONF
160 if (!_res_hconf.initialized)
161 _res_hconf_init ();
162 #endif /* need _res_hconf */
163 }
164 }
165 else
166 {
167 fct = start_fct;
168 no_more = (nip = startp) == (service_user *) -1l;
169 }
170
171 while (no_more == 0)
172 {
173 status = _CALL_DL_FCT (fct, (ADD_VARIABLES, resbuf, buffer, buflen,
174 &errno H_ERRNO_VAR));
175
176 /* The status is NSS_STATUS_TRYAGAIN and errno is ERANGE the
177 provided buffer is too small. In this case we should give
178 the user the possibility to enlarge the buffer and we should
179 not simply go on with the next service (even if the TRYAGAIN
180 action tells us so). */
181 if (status == NSS_STATUS_TRYAGAIN
182 #ifdef NEED_H_ERRNO
183 && *h_errnop == NETDB_INTERNAL
184 #endif
185 && errno == ERANGE)
186 break;
187
188 no_more = __nss_next (&nip, REENTRANT_NAME_STRING,
189 (void **) &fct, status, 0);
190 }
191
192 #ifdef HANDLE_DIGITS_DOTS
193 done:
194 #endif
195 *result = status == NSS_STATUS_SUCCESS ? resbuf : NULL;
196 #ifdef POSTPROCESS
197 POSTPROCESS;
198 #endif
199 return status == NSS_STATUS_SUCCESS ? 0 : -1;
200 }
201
202 #define do_weak_alias(n1, n2) weak_alias (n1, (n2))
203 do_weak_alias (INTERNAL (REENTRANT_NAME), REENTRANT_NAME)