Update copyright notices with scripts/update-copyrights
[glibc.git] / nss / getnssent_r.c
blob31d4c91139e8c84816d77e5e9fef24de2b4d31e9
1 /* Copyright (C) 2000-2014 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, see
16 <http://www.gnu.org/licenses/>. */
18 #include <errno.h>
19 #include <netdb.h>
20 #include "nsswitch.h"
22 /* Set up NIP to run through the services. If ALL is zero, use NIP's
23 current location if it's not nil. Return nonzero if there are no
24 services (left). */
25 static int
26 setup (const char *func_name, db_lookup_function lookup_fct,
27 void **fctp, service_user **nip, service_user **startp, int all)
29 int no_more;
30 if (*startp == NULL)
32 no_more = lookup_fct (nip, func_name, NULL, fctp);
33 *startp = no_more ? (service_user *) -1l : *nip;
35 else if (*startp == (service_user *) -1l)
36 /* No services at all. */
37 return 1;
38 else
40 if (all || !*nip)
41 /* Reset to the beginning of the service list. */
42 *nip = *startp;
43 /* Look up the first function. */
44 no_more = __nss_lookup (nip, func_name, NULL, fctp);
46 return no_more;
49 void
50 __nss_setent (const char *func_name, db_lookup_function lookup_fct,
51 service_user **nip, service_user **startp,
52 service_user **last_nip, int stayopen, int *stayopen_tmp,
53 int res)
55 union
57 setent_function f;
58 void *ptr;
59 } fct;
60 int no_more;
62 if (res && __res_maybe_init (&_res, 0) == -1)
64 __set_h_errno (NETDB_INTERNAL);
65 return;
68 /* Cycle through the services and run their `setXXent' functions until
69 we find an available service. */
70 no_more = setup (func_name, lookup_fct, &fct.ptr, nip,
71 startp, 1);
72 while (! no_more)
74 int is_last_nip = *nip == *last_nip;
75 enum nss_status status;
77 if (stayopen_tmp)
78 status = DL_CALL_FCT (fct.f, (*stayopen_tmp));
79 else
80 status = DL_CALL_FCT (fct.f, (0));
82 no_more = __nss_next2 (nip, func_name, NULL, &fct.ptr, status, 0);
83 if (is_last_nip)
84 *last_nip = *nip;
87 if (stayopen_tmp)
88 *stayopen_tmp = stayopen;
92 void
93 __nss_endent (const char *func_name, db_lookup_function lookup_fct,
94 service_user **nip, service_user **startp,
95 service_user **last_nip, int res)
97 union
99 endent_function f;
100 void *ptr;
101 } fct;
102 int no_more;
104 if (res && __res_maybe_init (&_res, 0) == -1)
106 __set_h_errno (NETDB_INTERNAL);
107 return;
110 /* Cycle through all the services and run their endXXent functions. */
111 no_more = setup (func_name, lookup_fct, &fct.ptr, nip, startp, 1);
112 while (! no_more)
114 /* Ignore status, we force check in __NSS_NEXT. */
115 DL_CALL_FCT (fct.f, ());
117 if (*nip == *last_nip)
118 /* We have processed all services which were used. */
119 break;
121 no_more = __nss_next2 (nip, func_name, NULL, &fct.ptr, 0, 1);
123 *last_nip = *nip = NULL;
128 __nss_getent_r (const char *getent_func_name,
129 const char *setent_func_name,
130 db_lookup_function lookup_fct,
131 service_user **nip, service_user **startp,
132 service_user **last_nip, int *stayopen_tmp, int res,
133 void *resbuf, char *buffer, size_t buflen,
134 void **result, int *h_errnop)
136 union
138 getent_function f;
139 void *ptr;
140 } fct;
141 int no_more;
142 enum nss_status status;
144 if (res && __res_maybe_init (&_res, 0) == -1)
146 *h_errnop = NETDB_INTERNAL;
147 *result = NULL;
148 return errno;
151 /* Initialize status to return if no more functions are found. */
152 status = NSS_STATUS_NOTFOUND;
154 /* Run through available functions, starting with the same function last
155 run. We will repeat each function as long as it succeeds, and then go
156 on to the next service action. */
157 no_more = setup (getent_func_name, lookup_fct, &fct.ptr, nip,
158 startp, 0);
159 while (! no_more)
161 int is_last_nip = *nip == *last_nip;
163 status = DL_CALL_FCT (fct.f,
164 (resbuf, buffer, buflen, &errno, &h_errno));
166 /* The status is NSS_STATUS_TRYAGAIN and errno is ERANGE the
167 provided buffer is too small. In this case we should give
168 the user the possibility to enlarge the buffer and we should
169 not simply go on with the next service (even if the TRYAGAIN
170 action tells us so). */
171 if (status == NSS_STATUS_TRYAGAIN
172 && (h_errnop == NULL || *h_errnop == NETDB_INTERNAL)
173 && errno == ERANGE)
174 break;
178 no_more = __nss_next2 (nip, getent_func_name, NULL, &fct.ptr,
179 status, 0);
181 if (is_last_nip)
182 *last_nip = *nip;
184 if (! no_more)
186 /* Call the `setXXent' function. This wasn't done before. */
187 union
189 setent_function f;
190 void *ptr;
191 } sfct;
193 no_more = __nss_lookup (nip, setent_func_name, NULL, &sfct.ptr);
195 if (! no_more)
197 if (stayopen_tmp)
198 status = DL_CALL_FCT (sfct.f, (*stayopen_tmp));
199 else
200 status = DL_CALL_FCT (sfct.f, (0));
202 else
203 status = NSS_STATUS_NOTFOUND;
206 while (! no_more && status != NSS_STATUS_SUCCESS);
209 *result = status == NSS_STATUS_SUCCESS ? resbuf : NULL;
210 return (status == NSS_STATUS_SUCCESS ? 0
211 : status != NSS_STATUS_TRYAGAIN ? ENOENT
212 /* h_errno functions only set errno if h_errno is NETDB_INTERNAL. */
213 : (h_errnop == NULL || *h_errnop == NETDB_INTERNAL) ? errno
214 : EAGAIN);