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