* sysdeps/unix/sysv/linux/m68k/sysdep-cancel.h: Support cancellation
[glibc.git] / nss / getnssent_r.c
blob7545b80c4a11e91588b6c3d589fc4a5fd2b2e123
1 /* Copyright (C) 2000,02 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #include <errno.h>
20 #include <netdb.h>
21 #include "nsswitch.h"
23 /* Set up NIP to run through the services. If ALL is zero, use NIP's
24 current location if it's not nil. Return nonzero if there are no
25 services (left). */
26 static int
27 setup (const char *func_name, db_lookup_function lookup_fct,
28 void **fctp, service_user **nip, service_user **startp, int all)
30 int no_more;
31 if (*startp == NULL)
33 no_more = lookup_fct (nip, func_name, fctp);
34 *startp = no_more ? (service_user *) -1l : *nip;
36 else if (*startp == (service_user *) -1l)
37 /* No services at all. */
38 return 1;
39 else
41 if (all || !*nip)
42 /* Reset to the beginning of the service list. */
43 *nip = *startp;
44 /* Look up the first function. */
45 no_more = __nss_lookup (nip, func_name, fctp);
47 return no_more;
50 void
51 __nss_setent (const char *func_name, db_lookup_function lookup_fct,
52 service_user **nip, service_user **startp,
53 service_user **last_nip, int stayopen, int *stayopen_tmp,
54 int res)
56 union
58 setent_function f;
59 void *ptr;
60 } fct;
61 int no_more;
63 if (res && (_res.options & RES_INIT) == 0
64 && __res_ninit (&_res) == -1)
66 __set_h_errno (NETDB_INTERNAL);
67 return;
70 /* Cycle through the services and run their `setXXent' functions until
71 we find an available service. */
72 no_more = setup (func_name, lookup_fct, &fct.ptr, nip,
73 startp, 1);
74 while (! no_more)
76 int is_last_nip = *nip == *last_nip;
77 enum nss_status status;
79 if (stayopen_tmp)
80 status = DL_CALL_FCT (fct.f, (*stayopen_tmp));
81 else
82 status = DL_CALL_FCT (fct.f, (0));
84 no_more = __nss_next (nip, func_name, &fct.ptr,
85 status, 0);
86 if (is_last_nip)
87 *last_nip = *nip;
90 if (stayopen_tmp)
91 *stayopen_tmp = stayopen;
95 void
96 __nss_endent (const char *func_name, db_lookup_function lookup_fct,
97 service_user **nip, service_user **startp,
98 service_user **last_nip, int res)
100 union
102 endent_function f;
103 void *ptr;
104 } fct;
105 int no_more;
107 if (res && (_res.options & RES_INIT) == 0
108 && __res_ninit (&_res) == -1)
110 __set_h_errno (NETDB_INTERNAL);
111 return;
114 /* Cycle through all the services and run their endXXent functions. */
115 no_more = setup (func_name, lookup_fct, &fct.ptr, nip, startp, 1);
116 while (! no_more)
118 /* Ignore status, we force check in __NSS_NEXT. */
119 DL_CALL_FCT (fct.f, ());
121 if (*nip == *last_nip)
122 /* We have processed all services which were used. */
123 break;
125 no_more = __nss_next (nip, func_name, &fct.ptr, 0, 1);
127 *last_nip = *nip = NULL;
132 __nss_getent_r (const char *getent_func_name,
133 const char *setent_func_name,
134 db_lookup_function lookup_fct,
135 service_user **nip, service_user **startp,
136 service_user **last_nip, int *stayopen_tmp, int res,
137 void *resbuf, char *buffer, size_t buflen,
138 void **result, int *h_errnop)
140 union
142 getent_function f;
143 void *ptr;
144 } fct;
145 int no_more;
146 enum nss_status status;
148 if (res && (_res.options & RES_INIT) == 0
149 && __res_ninit (&_res) == -1)
151 *h_errnop = NETDB_INTERNAL;
152 *result = NULL;
153 return errno;
156 /* Initialize status to return if no more functions are found. */
157 status = NSS_STATUS_NOTFOUND;
159 /* Run through available functions, starting with the same function last
160 run. We will repeat each function as long as it succeeds, and then go
161 on to the next service action. */
162 no_more = setup (getent_func_name, lookup_fct, &fct.ptr, nip,
163 startp, 0);
164 while (! no_more)
166 int is_last_nip = *nip == *last_nip;
168 status = DL_CALL_FCT (fct.f,
169 (resbuf, buffer, buflen, &errno, &h_errno));
171 /* The 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
177 && (h_errnop == NULL || *h_errnop == NETDB_INTERNAL)
178 && errno == ERANGE)
179 break;
183 no_more = __nss_next (nip, getent_func_name, &fct.ptr,
184 status, 0);
186 if (is_last_nip)
187 *last_nip = *nip;
189 if (! no_more)
191 /* Call the `setXXent' function. This wasn't done before. */
192 union
194 setent_function f;
195 void *ptr;
196 } sfct;
198 no_more = __nss_lookup (nip, setent_func_name,
199 &sfct.ptr);
201 if (! no_more)
203 if (stayopen_tmp)
204 status = DL_CALL_FCT (sfct.f, (*stayopen_tmp));
205 else
206 status = DL_CALL_FCT (sfct.f, (0));
208 else
209 status = NSS_STATUS_NOTFOUND;
212 while (! no_more && status != NSS_STATUS_SUCCESS);
215 *result = status == NSS_STATUS_SUCCESS ? resbuf : NULL;
216 return (status == NSS_STATUS_SUCCESS ? 0
217 : status != NSS_STATUS_TRYAGAIN ? ENOENT
218 /* h_errno functions only set errno if h_errno is NETDB_INTERNAL. */
219 : (h_errnop == NULL || *h_errnop == NETDB_INTERNAL) ? errno
220 : EAGAIN);