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. */
22 #include <nscd/nscd_proto.h>
24 /*******************************************************************\
25 |* Here we assume several symbols to be defined: *|
27 |* LOOKUP_TYPE - the return type of the function *|
29 |* FUNCTION_NAME - name of the non-reentrant function *|
31 |* DATABASE_NAME - name of the database the function accesses *|
32 |* (e.g., host, services, ...) *|
34 |* ADD_PARAMS - additional parameter, can vary in number *|
36 |* ADD_VARIABLES - names of additional parameter *|
38 |* Optionally the following vars can be defined: *|
40 |* NEED_H_ERRNO - an extra parameter will be passed to point to *|
41 |* the global `h_errno' variable. *|
43 |* NEED__RES - the global _res variable might be used so we *|
44 |* will have to initialize it if necessary *|
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
56 # define NSCD_NAME ADD_NSCD (REENTRANT_NAME)
57 # define ADD_NSCD(name) ADD_NSCD1 (name)
58 # define ADD_NSCD1(name) __nscd_##name
61 #define FUNCTION_NAME_STRING STRINGIZE (FUNCTION_NAME)
62 #define REENTRANT_NAME_STRING STRINGIZE (REENTRANT_NAME)
63 #define DATABASE_NAME_STRING STRINGIZE (DATABASE_NAME)
64 #define STRINGIZE(name) STRINGIZE1 (name)
65 #define STRINGIZE1(name) #name
67 #define DB_LOOKUP_FCT CONCAT3_1 (__nss_, DATABASE_NAME, _lookup)
68 #define CONCAT3_1(Pre, Name, Post) CONCAT3_2 (Pre, Name, Post)
69 #define CONCAT3_2(Pre, Name, Post) Pre##Name##Post
71 /* Sometimes we need to store error codes in the `h_errno' variable. */
73 # define H_ERRNO_PARM , int *h_errnop
74 # define H_ERRNO_VAR , h_errnop
81 /* Type of the lookup function we need here. */
82 typedef int (*lookup_function) (ADD_PARAMS, LOOKUP_TYPE *, char *, size_t,
85 /* Some usages of this file might use this variable. */
86 extern struct __res_state _res;
88 /* The lookup function for the first entry of this service. */
89 extern int DB_LOOKUP_FCT (service_user **nip, const char *name, void **fctp);
91 /* Nonzero if the NSCD is not available. This variable will be increased
92 whenever we try to use the NSCD but see it is not avilable. So we
93 can recheck the presence every once in a while. */
94 extern int __nss_nscd_not_available;
95 /* Interval in which we transfer retry to contact the NSCD. */
96 #define NSS_NSCD_RETRY 100
100 INTERNAL (REENTRANT_NAME) (ADD_PARAMS, LOOKUP_TYPE *resbuf, char *buffer,
101 size_t buflen, LOOKUP_TYPE **result H_ERRNO_PARM)
103 static service_user *startp = NULL;
104 static lookup_function start_fct;
108 enum nss_status status = NSS_STATUS_UNAVAIL;
113 #ifdef HANDLE_DIGITS_DOTS
114 # define resbuf (*resbuf)
115 # include "digits_dots.c"
120 if (__nss_nscd_not_available && ++__nss_nscd_not_available > NSS_NSCD_RETRY)
121 __nss_nscd_not_available = 0;
123 if (!__nss_nscd_not_available)
125 nscd_status = NSCD_NAME (ADD_VARIABLES, resbuf, buffer, buflen
129 *result = nscd_status == 0 ? resbuf : NULL;
132 if (nscd_status == 2)
133 /* This return value indicates that contacting the server failed. */
134 __nss_nscd_not_available = 1;
140 no_more = DB_LOOKUP_FCT (&nip, REENTRANT_NAME_STRING, (void **) &fct);
142 startp = (service_user *) -1l;
149 /* The resolver code will really be used so we have to
151 if ((_res.options & RES_INIT) == 0 && res_init () == -1)
153 *h_errnop = NETDB_INTERNAL;
157 #endif /* need _res */
163 no_more = (nip = startp) == (service_user *) -1l;
168 status = (*fct) (ADD_VARIABLES, resbuf, buffer, buflen,
169 __errno_location () H_ERRNO_VAR);
171 /* The status is NSS_STATUS_TRYAGAIN and errno is ERANGE the
172 provided buffer is too small. In this case we should give
173 the user the possibility to enlarge the buffer and we should
174 not simply go on with the next service (even if the TRYAGAIN
175 action tells us so). */
176 if (status == NSS_STATUS_TRYAGAIN
178 && *h_errnop == NETDB_INTERNAL
183 no_more = __nss_next (&nip, REENTRANT_NAME_STRING,
184 (void **) &fct, status, 0);
187 #ifdef HANDLE_DIGITS_DOTS
190 *result = status == NSS_STATUS_SUCCESS ? resbuf : NULL;
191 return status == NSS_STATUS_SUCCESS ? 0 : -1;
194 #define do_weak_alias(n1, n2) weak_alias (n1, (n2))
195 do_weak_alias (INTERNAL (REENTRANT_NAME), REENTRANT_NAME)