1 /* Copyright (C) 1996, 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 <libc-lock.h>
24 /*******************************************************************\
25 |* Here we assume several symbols to be defined: *|
27 |* LOOKUP_TYPE - the return type of the function *|
29 |* SETFUNC_NAME - name of the non-reentrant setXXXent function *|
31 |* GETFUNC_NAME - name of the non-reentrant getXXXent function *|
33 |* ENDFUNC_NAME - name of the non-reentrant endXXXent function *|
35 |* DATABASE_NAME - name of the database the function accesses *|
36 |* (e.g., host, services, ...) *|
38 |* Optionally the following vars can be defined: *|
40 |* STAYOPEN - variable declaration for setXXXent function *|
42 |* STAYOPEN_VAR - variable name for setXXXent function *|
44 |* NEED_H_ERRNO - an extra parameter will be passed to point to *|
45 |* the global `h_errno' variable. *|
47 \*******************************************************************/
49 /* To make the real sources a bit prettier. */
50 #define REENTRANT_GETNAME APPEND_R (GETFUNC_NAME)
51 #define APPEND_R(Name) CONCAT2_2 (Name, _r)
52 #define INTERNAL(Name) CONCAT2_2 (__, Name)
53 #define CONCAT2_1(Pre, Post) CONCAT2_2 (Pre, Post)
54 #define CONCAT2_2(Pre, Post) Pre##Post
56 #define SETFUNC_NAME_STRING STRINGIZE (SETFUNC_NAME)
57 #define GETFUNC_NAME_STRING STRINGIZE (REENTRANT_GETNAME)
58 #define ENDFUNC_NAME_STRING STRINGIZE (ENDFUNC_NAME)
59 #define DATABASE_NAME_STRING STRINGIZE (DATABASE_NAME)
60 #define STRINGIZE(Name) STRINGIZE1 (Name)
61 #define STRINGIZE1(Name) #Name
63 #define DB_LOOKUP_FCT CONCAT3_1 (__nss_, DATABASE_NAME, _lookup)
64 #define CONCAT3_1(Pre, Name, Post) CONCAT3_2 (Pre, Name, Post)
65 #define CONCAT3_2(Pre, Name, Post) Pre##Name##Post
67 /* Sometimes we need to store error codes in the `h_errno' variable. */
69 # define H_ERRNO_PARM , int *h_errnop
70 # define H_ERRNO_VAR , &h_errno
76 /* Some databases take the `stayopen' flag. */
78 # define STAYOPEN_TMP CONCAT2_1 (STAYOPEN, _tmp)
79 # define STAYOPEN_TMPVAR CONCAT2_1 (STAYOPEN_VAR, _tmp)
81 # define STAYOPEN void
83 # define STAYOPEN_TMPVAR
86 /* Prototype for the setXXXent functions we use here. */
87 typedef int (*set_function
) (STAYOPEN
);
89 /* Prototype for the endXXXent functions we use here. */
90 typedef int (*end_function
) (void);
92 /* Prototype for the setXXXent functions we use here. */
93 typedef int (*get_function
) (LOOKUP_TYPE
*, char *, size_t H_ERRNO_PARM
);
96 /* This handle for the NSS data base is shared between all
97 set/get/endXXXent functions. */
98 static service_user
*nip
;
99 /* Remember the last service used since the last call to `endXXent'. */
100 static service_user
*last_nip
;
101 /* Remember the first service_entry, it's always the same. */
102 static service_user
*startp
;
105 /* We need to remember the last `stayopen' flag given by the user
106 since the `setent' function is only called for the first available
111 /* Protect above variable against multiple uses at the same time. */
112 __libc_lock_define_initialized (static, lock
)
114 /* The lookup function for the first entry of this service. */
115 extern int DB_LOOKUP_FCT (service_user
**nip
, const char *name
, void **fctp
);
117 /* Set up NIP to run through the services. If ALL is zero, use NIP's
118 current location if it's not nil. Return nonzero if there are no
120 static enum nss_status
121 setup (void **fctp
, const char *func_name
, int all
)
126 no_more
= DB_LOOKUP_FCT (&nip
, func_name
, fctp
);
127 startp
= no_more
? (service_user
*) -1l : nip
;
129 else if (startp
== (service_user
*) -1l)
130 /* No services at all. */
135 /* Reset to the beginning of the service list. */
137 /* Look up the first function. */
138 no_more
= __nss_lookup (&nip
, func_name
, fctp
);
144 SETFUNC_NAME (STAYOPEN
)
150 if ((_res
.options
& RES_INIT
) == 0 && res_init () == -1)
152 __set_h_errno (NETDB_INTERNAL
);
155 #endif /* need _res */
157 __libc_lock_lock (lock
);
159 /* Cycle through the services and run their `setXXent' functions until
160 we find an available service. */
161 no_more
= setup ((void **) &fct
, SETFUNC_NAME_STRING
, 1);
164 int is_last_nip
= nip
== last_nip
;
165 enum nss_status status
= (*fct
) (STAYOPEN_VAR
);
167 no_more
= __nss_next (&nip
, SETFUNC_NAME_STRING
, (void **) &fct
,
174 STAYOPEN_TMPVAR
= STAYOPEN_VAR
;
177 __libc_lock_unlock (lock
);
188 if ((_res
.options
& RES_INIT
) == 0 && res_init () == -1)
190 __set_h_errno (NETDB_INTERNAL
);
193 #endif /* need _res */
195 __libc_lock_lock (lock
);
197 /* Cycle through all the services and run their endXXent functions. */
198 no_more
= setup ((void **) &fct
, ENDFUNC_NAME_STRING
, 1);
201 /* Ignore status, we force check in __NSS_NEXT. */
205 /* We have processed all services which were used. */
208 no_more
= __nss_next (&nip
, ENDFUNC_NAME_STRING
, (void **) &fct
, 0, 1);
210 last_nip
= nip
= NULL
;
212 __libc_lock_unlock (lock
);
217 INTERNAL (REENTRANT_GETNAME
) (LOOKUP_TYPE
*resbuf
, char *buffer
, size_t buflen
,
218 LOOKUP_TYPE
**result H_ERRNO_PARM
)
222 enum nss_status status
;
225 if ((_res
.options
& RES_INIT
) == 0 && res_init () == -1)
227 __set_h_errno (NETDB_INTERNAL
);
231 #endif /* need _res */
233 /* Initialize status to return if no more functions are found. */
234 status
= NSS_STATUS_NOTFOUND
;
236 __libc_lock_lock (lock
);
238 /* Run through available functions, starting with the same function last
239 run. We will repeat each function as long as it succeeds, and then go
240 on to the next service action. */
241 no_more
= setup ((void **) &fct
, GETFUNC_NAME_STRING
, 0);
244 int is_last_nip
= nip
== last_nip
;
246 status
= (*fct
) (resbuf
, buffer
, buflen H_ERRNO_VAR
);
248 /* The the status is NSS_STATUS_TRYAGAIN and errno is ERANGE the
249 provided buffer is too small. In this case we should give
250 the user the possibility to enlarge the buffer and we should
251 not simply go on with the next service (even if the TRYAGAIN
252 action tells us so). */
253 if (status
== NSS_STATUS_TRYAGAIN
255 && *h_errnop
== NETDB_INTERNAL
262 no_more
= __nss_next (&nip
, GETFUNC_NAME_STRING
, (void **) &fct
,
270 /* Call the `setXXent' function. This wasn't done before. */
273 no_more
= __nss_lookup (&nip
, SETFUNC_NAME_STRING
,
277 status
= (*sfct
) (STAYOPEN_TMPVAR
);
279 status
= NSS_STATUS_NOTFOUND
;
282 while (! no_more
&& status
!= NSS_STATUS_SUCCESS
);
285 __libc_lock_unlock (lock
);
287 *result
= status
== NSS_STATUS_SUCCESS
? resbuf
: NULL
;
288 return status
== NSS_STATUS_SUCCESS
? 0 : -1;
290 #define do_weak_alias(n1, n2) weak_alias (n1, n2)
291 do_weak_alias (INTERNAL (REENTRANT_GETNAME
), REENTRANT_GETNAME
)