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