nss_db: Quash read implicit declaration warning
[platform/upstream/glibc.git] / nss / getnssent_r.c
1 /* Copyright (C) 2000, 2002, 2004, 2007, 2011 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3
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.
8
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.
13
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.  */
18
19 #include <errno.h>
20 #include <netdb.h>
21 #include "nsswitch.h"
22
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)
29 {
30   int no_more;
31   if (*startp == NULL)
32     {
33       no_more = lookup_fct (nip, func_name, NULL, fctp);
34       *startp = no_more ? (service_user *) -1l : *nip;
35     }
36   else if (*startp == (service_user *) -1l)
37     /* No services at all.  */
38     return 1;
39   else
40     {
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, NULL, fctp);
46     }
47   return no_more;
48 }
49 \f
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)
55 {
56   union
57   {
58     setent_function f;
59     void *ptr;
60   } fct;
61   int no_more;
62
63   if (res && __res_maybe_init (&_res, 0) == -1)
64     {
65       __set_h_errno (NETDB_INTERNAL);
66       return;
67     }
68
69   /* Cycle through the services and run their `setXXent' functions until
70      we find an available service.  */
71   no_more = setup (func_name, lookup_fct, &fct.ptr, nip,
72                    startp, 1);
73   while (! no_more)
74     {
75       int is_last_nip = *nip == *last_nip;
76       enum nss_status status;
77
78       if (stayopen_tmp)
79         status = DL_CALL_FCT (fct.f, (*stayopen_tmp));
80       else
81         status = DL_CALL_FCT (fct.f, (0));
82
83       no_more = __nss_next2 (nip, func_name, NULL, &fct.ptr, status, 0);
84       if (is_last_nip)
85         *last_nip = *nip;
86     }
87
88   if (stayopen_tmp)
89     *stayopen_tmp = stayopen;
90 }
91
92
93 void
94 __nss_endent (const char *func_name, db_lookup_function lookup_fct,
95               service_user **nip, service_user **startp,
96               service_user **last_nip, int res)
97 {
98   union
99   {
100     endent_function f;
101     void *ptr;
102   } fct;
103   int no_more;
104
105   if (res && __res_maybe_init (&_res, 0) == -1)
106     {
107       __set_h_errno (NETDB_INTERNAL);
108       return;
109     }
110
111   /* Cycle through all the services and run their endXXent functions.  */
112   no_more = setup (func_name, lookup_fct, &fct.ptr, nip, startp, 1);
113   while (! no_more)
114     {
115       /* Ignore status, we force check in __NSS_NEXT.  */
116       DL_CALL_FCT (fct.f, ());
117
118       if (*nip == *last_nip)
119         /* We have processed all services which were used.  */
120         break;
121
122       no_more = __nss_next2 (nip, func_name, NULL, &fct.ptr, 0, 1);
123     }
124   *last_nip = *nip = NULL;
125 }
126
127
128 int
129 __nss_getent_r (const char *getent_func_name,
130                 const char *setent_func_name,
131                 db_lookup_function lookup_fct,
132                 service_user **nip, service_user **startp,
133                 service_user **last_nip, int *stayopen_tmp, int res,
134                 void *resbuf, char *buffer, size_t buflen,
135                 void **result, int *h_errnop)
136 {
137   union
138   {
139     getent_function f;
140     void *ptr;
141   } fct;
142   int no_more;
143   enum nss_status status;
144
145   if (res && __res_maybe_init (&_res, 0) == -1)
146     {
147       *h_errnop = NETDB_INTERNAL;
148       *result = NULL;
149       return errno;
150     }
151
152   /* Initialize status to return if no more functions are found.  */
153   status = NSS_STATUS_NOTFOUND;
154
155   /* Run through available functions, starting with the same function last
156      run.  We will repeat each function as long as it succeeds, and then go
157      on to the next service action.  */
158   no_more = setup (getent_func_name, lookup_fct, &fct.ptr, nip,
159                    startp, 0);
160   while (! no_more)
161     {
162       int is_last_nip = *nip == *last_nip;
163
164       status = DL_CALL_FCT (fct.f,
165                             (resbuf, buffer, buflen, &errno, &h_errno));
166
167       /* The status is NSS_STATUS_TRYAGAIN and errno is ERANGE the
168          provided buffer is too small.  In this case we should give
169          the user the possibility to enlarge the buffer and we should
170          not simply go on with the next service (even if the TRYAGAIN
171          action tells us so).  */
172       if (status == NSS_STATUS_TRYAGAIN
173           && (h_errnop == NULL || *h_errnop == NETDB_INTERNAL)
174           && errno == ERANGE)
175         break;
176
177       do
178         {
179           no_more = __nss_next2 (nip, getent_func_name, NULL, &fct.ptr,
180                                  status, 0);
181
182           if (is_last_nip)
183             *last_nip = *nip;
184
185           if (! no_more)
186             {
187               /* Call the `setXXent' function.  This wasn't done before.  */
188               union
189               {
190                 setent_function f;
191                 void *ptr;
192               } sfct;
193
194               no_more = __nss_lookup (nip, setent_func_name, NULL, &sfct.ptr);
195
196               if (! no_more)
197                 {
198                   if (stayopen_tmp)
199                     status = DL_CALL_FCT (sfct.f, (*stayopen_tmp));
200                   else
201                     status = DL_CALL_FCT (sfct.f, (0));
202                 }
203               else
204                 status = NSS_STATUS_NOTFOUND;
205             }
206         }
207       while (! no_more && status != NSS_STATUS_SUCCESS);
208     }
209
210   *result = status == NSS_STATUS_SUCCESS ? resbuf : NULL;
211   return (status == NSS_STATUS_SUCCESS ? 0
212           : status != NSS_STATUS_TRYAGAIN ? ENOENT
213           /* h_errno functions only set errno if h_errno is NETDB_INTERNAL.  */
214           : (h_errnop == NULL || *h_errnop == NETDB_INTERNAL) ? errno
215           : EAGAIN);
216 }