[powerpc] get_rounding_mode: utilize faster method to get rounding mode
[glibc.git] / nss / getXXbyYY_r.c
blobcf867210fc06305e14cc24d2d414013c57bd0caf
1 /* Copyright (C) 1996-2019 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 Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <assert.h>
20 #include <atomic.h>
21 #include <errno.h>
22 #include <stdbool.h>
23 #include "nsswitch.h"
24 #include "sysdep.h"
25 #ifdef USE_NSCD
26 # include <nscd/nscd_proto.h>
27 #endif
28 #ifdef NEED__RES
29 # include <resolv/resolv_context.h>
30 #endif
31 /*******************************************************************\
32 |* Here we assume several symbols to be defined: *|
33 |* *|
34 |* LOOKUP_TYPE - the return type of the function *|
35 |* *|
36 |* FUNCTION_NAME - name of the non-reentrant function *|
37 |* *|
38 |* DATABASE_NAME - name of the database the function accesses *|
39 |* (e.g., host, services, ...) *|
40 |* *|
41 |* ADD_PARAMS - additional parameters, can vary in number *|
42 |* *|
43 |* ADD_VARIABLES - names of additional parameters *|
44 |* *|
45 |* Optionally the following vars can be defined: *|
46 |* *|
47 |* EXTRA_PARAMS - optional parameters, can vary in number *|
48 |* *|
49 |* EXTRA_VARIABLES - names of optional parameter *|
50 |* *|
51 |* FUNCTION2_NAME - alternative name of the non-reentrant function *|
52 |* *|
53 |* NEED_H_ERRNO - an extra parameter will be passed to point to *|
54 |* the global `h_errno' variable. *|
55 |* *|
56 |* NEED__RES - obtain a struct resolv_context resolver context *|
57 |* *|
58 |* PREPROCESS - code run before anything else *|
59 |* *|
60 |* POSTPROCESS - code run after the lookup *|
61 |* *|
62 \*******************************************************************/
64 /* To make the real sources a bit prettier. */
65 #define REENTRANT_NAME APPEND_R (FUNCTION_NAME)
66 #ifdef FUNCTION2_NAME
67 # define REENTRANT2_NAME APPEND_R (FUNCTION2_NAME)
68 #else
69 # define REENTRANT2_NAME NULL
70 #endif
71 #define APPEND_R(name) APPEND_R1 (name)
72 #define APPEND_R1(name) name##_r
73 #define INTERNAL(name) INTERNAL1 (name)
74 #define INTERNAL1(name) __##name
75 #define NEW(name) NEW1 (name)
76 #define NEW1(name) __new_##name
78 #ifdef USE_NSCD
79 # define NSCD_NAME ADD_NSCD (REENTRANT_NAME)
80 # define ADD_NSCD(name) ADD_NSCD1 (name)
81 # define ADD_NSCD1(name) __nscd_##name
82 # define NOT_USENSCD_NAME ADD_NOT_NSCDUSE (DATABASE_NAME)
83 # define ADD_NOT_NSCDUSE(name) ADD_NOT_NSCDUSE1 (name)
84 # define ADD_NOT_NSCDUSE1(name) __nss_not_use_nscd_##name
85 # define CONCAT2(arg1, arg2) CONCAT2_2 (arg1, arg2)
86 # define CONCAT2_2(arg1, arg2) arg1##arg2
87 #endif
89 #define FUNCTION_NAME_STRING STRINGIZE (FUNCTION_NAME)
90 #define REENTRANT_NAME_STRING STRINGIZE (REENTRANT_NAME)
91 #ifdef FUNCTION2_NAME
92 # define REENTRANT2_NAME_STRING STRINGIZE (REENTRANT2_NAME)
93 #else
94 # define REENTRANT2_NAME_STRING NULL
95 #endif
96 #define DATABASE_NAME_STRING STRINGIZE (DATABASE_NAME)
97 #define STRINGIZE(name) STRINGIZE1 (name)
98 #define STRINGIZE1(name) #name
100 #ifndef DB_LOOKUP_FCT
101 # define DB_LOOKUP_FCT CONCAT3_1 (__nss_, DATABASE_NAME, _lookup2)
102 # define CONCAT3_1(Pre, Name, Post) CONCAT3_2 (Pre, Name, Post)
103 # define CONCAT3_2(Pre, Name, Post) Pre##Name##Post
104 #endif
106 /* Sometimes we need to store error codes in the `h_errno' variable. */
107 #ifdef NEED_H_ERRNO
108 # define H_ERRNO_PARM , int *h_errnop
109 # define H_ERRNO_VAR , h_errnop
110 # define H_ERRNO_VAR_P h_errnop
111 #else
112 # define H_ERRNO_PARM
113 # define H_ERRNO_VAR
114 # define H_ERRNO_VAR_P NULL
115 #endif
117 #ifndef EXTRA_PARAMS
118 # define EXTRA_PARAMS
119 #endif
120 #ifndef EXTRA_VARIABLES
121 # define EXTRA_VARIABLES
122 #endif
124 #ifdef HAVE_AF
125 # define AF_VAL af
126 #else
127 # define AF_VAL AF_INET
128 #endif
131 /* Set defaults for merge functions that haven't been defined. */
132 #ifndef DEEPCOPY_FN
133 static inline int
134 __copy_einval (LOOKUP_TYPE a,
135 const size_t b,
136 LOOKUP_TYPE *c,
137 char *d,
138 char **e)
140 return EINVAL;
142 # define DEEPCOPY_FN __copy_einval
143 #endif
145 #ifndef MERGE_FN
146 static inline int
147 __merge_einval (LOOKUP_TYPE *a,
148 char *b,
149 char *c,
150 size_t d,
151 LOOKUP_TYPE *e,
152 char *f)
154 return EINVAL;
156 # define MERGE_FN __merge_einval
157 #endif
159 #define CHECK_MERGE(err, status) \
160 ({ \
161 do \
163 if (err) \
165 __set_errno (err); \
166 if (err == ERANGE) \
167 status = NSS_STATUS_TRYAGAIN; \
168 else \
169 status = NSS_STATUS_UNAVAIL; \
170 break; \
173 while (0); \
176 /* Type of the lookup function we need here. */
177 typedef enum nss_status (*lookup_function) (ADD_PARAMS, LOOKUP_TYPE *, char *,
178 size_t, int * H_ERRNO_PARM
179 EXTRA_PARAMS);
181 /* The lookup function for the first entry of this service. */
182 extern int DB_LOOKUP_FCT (service_user **nip, const char *name,
183 const char *name2, void **fctp);
184 libc_hidden_proto (DB_LOOKUP_FCT)
188 INTERNAL (REENTRANT_NAME) (ADD_PARAMS, LOOKUP_TYPE *resbuf, char *buffer,
189 size_t buflen, LOOKUP_TYPE **result H_ERRNO_PARM
190 EXTRA_PARAMS)
192 static bool startp_initialized;
193 static service_user *startp;
194 static lookup_function start_fct;
195 service_user *nip;
196 int do_merge = 0;
197 LOOKUP_TYPE mergegrp;
198 char *mergebuf = NULL;
199 char *endptr = NULL;
200 union
202 lookup_function l;
203 void *ptr;
204 } fct;
205 int no_more, err;
206 enum nss_status status = NSS_STATUS_UNAVAIL;
207 #ifdef USE_NSCD
208 int nscd_status;
209 #endif
210 #ifdef NEED_H_ERRNO
211 bool any_service = false;
212 #endif
214 #ifdef NEED__RES
215 /* The HANDLE_DIGITS_DOTS case below already needs the resolver
216 configuration, so this has to happen early. */
217 struct resolv_context *res_ctx = __resolv_context_get ();
218 if (res_ctx == NULL)
220 *h_errnop = NETDB_INTERNAL;
221 *result = NULL;
222 return errno;
224 #endif /* NEED__RES */
226 #ifdef PREPROCESS
227 PREPROCESS;
228 #endif
230 #ifdef HANDLE_DIGITS_DOTS
231 switch (__nss_hostname_digits_dots (name, resbuf, &buffer, NULL,
232 buflen, result, &status, AF_VAL,
233 H_ERRNO_VAR_P))
235 case -1:
236 # ifdef NEED__RES
237 __resolv_context_put (res_ctx);
238 # endif
239 return errno;
240 case 1:
241 #ifdef NEED_H_ERRNO
242 any_service = true;
243 #endif
244 goto done;
246 #endif
248 #ifdef USE_NSCD
249 if (NOT_USENSCD_NAME > 0 && ++NOT_USENSCD_NAME > NSS_NSCD_RETRY)
250 NOT_USENSCD_NAME = 0;
252 if (!NOT_USENSCD_NAME
253 && !__nss_database_custom[CONCAT2 (NSS_DBSIDX_, DATABASE_NAME)])
255 nscd_status = NSCD_NAME (ADD_VARIABLES, resbuf, buffer, buflen, result
256 H_ERRNO_VAR);
257 if (nscd_status >= 0)
259 # ifdef NEED__RES
260 __resolv_context_put (res_ctx);
261 # endif
262 return nscd_status;
265 #endif
267 if (! startp_initialized)
269 no_more = DB_LOOKUP_FCT (&nip, REENTRANT_NAME_STRING,
270 REENTRANT2_NAME_STRING, &fct.ptr);
271 if (no_more)
273 void *tmp_ptr = (service_user *) -1l;
274 #ifdef PTR_MANGLE
275 PTR_MANGLE (tmp_ptr);
276 #endif
277 startp = tmp_ptr;
279 else
281 void *tmp_ptr = fct.l;
282 #ifdef PTR_MANGLE
283 PTR_MANGLE (tmp_ptr);
284 #endif
285 start_fct = tmp_ptr;
286 tmp_ptr = nip;
287 #ifdef PTR_MANGLE
288 PTR_MANGLE (tmp_ptr);
289 #endif
290 startp = tmp_ptr;
293 /* Make sure start_fct and startp are written before
294 startp_initialized. */
295 atomic_write_barrier ();
296 startp_initialized = true;
298 else
300 fct.l = start_fct;
301 nip = startp;
302 #ifdef PTR_DEMANGLE
303 PTR_DEMANGLE (fct.l);
304 PTR_DEMANGLE (nip);
305 #endif
306 no_more = nip == (service_user *) -1l;
309 while (no_more == 0)
311 #ifdef NEED_H_ERRNO
312 any_service = true;
313 #endif
315 status = DL_CALL_FCT (fct.l, (ADD_VARIABLES, resbuf, buffer, buflen,
316 &errno H_ERRNO_VAR EXTRA_VARIABLES));
318 /* The status is NSS_STATUS_TRYAGAIN and errno is ERANGE the
319 provided buffer is too small. In this case we should give
320 the user the possibility to enlarge the buffer and we should
321 not simply go on with the next service (even if the TRYAGAIN
322 action tells us so). */
323 if (status == NSS_STATUS_TRYAGAIN
324 #ifdef NEED_H_ERRNO
325 && *h_errnop == NETDB_INTERNAL
326 #endif
327 && errno == ERANGE)
328 break;
330 if (do_merge)
333 if (status == NSS_STATUS_SUCCESS)
335 /* The previous loop saved a buffer for merging.
336 Perform the merge now. */
337 err = MERGE_FN (&mergegrp, mergebuf, endptr, buflen, resbuf,
338 buffer);
339 CHECK_MERGE (err,status);
340 do_merge = 0;
342 else
344 /* If the result wasn't SUCCESS, copy the saved buffer back
345 into the result buffer and set the status back to
346 NSS_STATUS_SUCCESS to match the previous pass through the
347 loop.
348 * If the next action is CONTINUE, it will overwrite the value
349 currently in the buffer and return the new value.
350 * If the next action is RETURN, we'll return the previously-
351 acquired values.
352 * If the next action is MERGE, then it will be added to the
353 buffer saved from the previous source. */
354 err = DEEPCOPY_FN (mergegrp, buflen, resbuf, buffer, NULL);
355 CHECK_MERGE (err, status);
356 status = NSS_STATUS_SUCCESS;
360 /* If we were are configured to merge this value with the next one,
361 save the current value of the group struct. */
362 if (nss_next_action (nip, status) == NSS_ACTION_MERGE
363 && status == NSS_STATUS_SUCCESS)
365 /* Copy the current values into a buffer to be merged with the next
366 set of retrieved values. */
367 if (mergebuf == NULL)
369 /* Only allocate once and reuse it for as many merges as we need
370 to perform. */
371 mergebuf = malloc (buflen);
372 if (mergebuf == NULL)
374 __set_errno (ENOMEM);
375 status = NSS_STATUS_UNAVAIL;
376 break;
380 err = DEEPCOPY_FN (*resbuf, buflen, &mergegrp, mergebuf, &endptr);
381 CHECK_MERGE (err, status);
382 do_merge = 1;
385 no_more = __nss_next2 (&nip, REENTRANT_NAME_STRING,
386 REENTRANT2_NAME_STRING, &fct.ptr, status, 0);
388 free (mergebuf);
389 mergebuf = NULL;
391 #ifdef HANDLE_DIGITS_DOTS
392 done:
393 #endif
394 *result = status == NSS_STATUS_SUCCESS ? resbuf : NULL;
395 #ifdef NEED_H_ERRNO
396 if (status == NSS_STATUS_UNAVAIL && !any_service && errno != ENOENT)
397 /* This happens when we weren't able to use a service for reasons other
398 than the module not being found. In such a case, we'd want to tell the
399 caller that errno has the real reason for failure. */
400 *h_errnop = NETDB_INTERNAL;
401 else if (status != NSS_STATUS_SUCCESS && !any_service)
402 /* We were not able to use any service. */
403 *h_errnop = NO_RECOVERY;
404 #endif
405 #ifdef POSTPROCESS
406 POSTPROCESS;
407 #endif
409 #ifdef NEED__RES
410 /* This has to happen late because the POSTPROCESS stage above might
411 need the resolver context. */
412 __resolv_context_put (res_ctx);
413 #endif /* NEED__RES */
415 int res;
416 if (status == NSS_STATUS_SUCCESS || status == NSS_STATUS_NOTFOUND)
417 res = 0;
418 /* Don't pass back ERANGE if this is not for a too-small buffer. */
419 else if (errno == ERANGE && status != NSS_STATUS_TRYAGAIN)
420 res = EINVAL;
421 #ifdef NEED_H_ERRNO
422 /* These functions only set errno if h_errno is NETDB_INTERNAL. */
423 else if (status == NSS_STATUS_TRYAGAIN && *h_errnop != NETDB_INTERNAL)
424 res = EAGAIN;
425 #endif
426 else
427 return errno;
429 __set_errno (res);
430 return res;
434 #ifdef NO_COMPAT_NEEDED
435 strong_alias (INTERNAL (REENTRANT_NAME), REENTRANT_NAME);
436 #elif !defined FUNCTION2_NAME
437 # include <shlib-compat.h>
438 # if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_1_2)
439 # define OLD(name) OLD1 (name)
440 # define OLD1(name) __old_##name
443 attribute_compat_text_section
444 OLD (REENTRANT_NAME) (ADD_PARAMS, LOOKUP_TYPE *resbuf, char *buffer,
445 size_t buflen, LOOKUP_TYPE **result H_ERRNO_PARM)
447 int ret = INTERNAL (REENTRANT_NAME) (ADD_VARIABLES, resbuf, buffer,
448 buflen, result H_ERRNO_VAR);
450 if (ret != 0 || result == NULL)
451 ret = -1;
453 return ret;
456 # define do_symbol_version(real, name, version) \
457 compat_symbol (libc, real, name, version)
458 do_symbol_version (OLD (REENTRANT_NAME), REENTRANT_NAME, GLIBC_2_0);
459 # endif
461 /* As INTERNAL (REENTRANT_NAME) may be hidden, we need an alias
462 in between so that the REENTRANT_NAME@@GLIBC_2.1.2 is not
463 hidden too. */
464 strong_alias (INTERNAL (REENTRANT_NAME), NEW (REENTRANT_NAME));
466 # define do_default_symbol_version(real, name, version) \
467 versioned_symbol (libc, real, name, version)
468 do_default_symbol_version (NEW (REENTRANT_NAME),
469 REENTRANT_NAME, GLIBC_2_1_2);
470 #endif
472 nss_interface_function (REENTRANT_NAME)