1 /* Copyright (C) 1996-2022 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, see
16    <https://www.gnu.org/licenses/>.  */
17 
18 #include <errno.h>
19 #include <libc-lock.h>
20 #include <stdlib.h>
21 
22 #include "nsswitch.h"
23 
24 /*******************************************************************\
25 |* Here we assume several symbols to be defined:		   *|
26 |*								   *|
27 |* LOOKUP_TYPE   - the return type of the function		   *|
28 |*								   *|
29 |* GETFUNC_NAME  - name of the non-reentrant getXXXent function	   *|
30 |*								   *|
31 |* BUFLEN	 - size of static buffer			   *|
32 |*								   *|
33 |* Optionally the following vars can be defined:		   *|
34 |*								   *|
35 |* NEED_H_ERRNO  - an extra parameter will be passed to point to   *|
36 |*		   the global `h_errno' variable.		   *|
37 |*								   *|
38 \*******************************************************************/
39 
40 /* To make the real sources a bit prettier.  */
41 #define REENTRANT_GETNAME APPEND_R (GETFUNC_NAME)
42 #define APPEND_R(name) APPEND_R1 (name)
43 #define APPEND_R1(name) name##_r
44 #define INTERNAL(name) INTERNAL1 (name)
45 #define INTERNAL1(name) __##name
46 
47 /* Sometimes we need to store error codes in the `h_errno' variable.  */
48 #ifdef NEED_H_ERRNO
49 # define H_ERRNO_PARM , int *h_errnop
50 # define H_ERRNO_VAR &h_errno
51 #else
52 # define H_ERRNO_PARM
53 # define H_ERRNO_VAR NULL
54 #endif
55 
56 /* Prototype of the reentrant version.  */
57 extern int INTERNAL (REENTRANT_GETNAME) (LOOKUP_TYPE *resbuf, char *buffer,
58 					 size_t buflen, LOOKUP_TYPE **result
59 					 H_ERRNO_PARM) attribute_hidden;
60 
61 /* We need to protect the dynamic buffer handling.  */
62 __libc_lock_define_initialized (static, lock);
63 
64 /* This points to the static buffer used.  */
65 libc_freeres_ptr (static char *buffer);
66 
67 
68 LOOKUP_TYPE *
GETFUNC_NAME(void)69 GETFUNC_NAME (void)
70 {
71   static size_t buffer_size;
72   static union
73   {
74     LOOKUP_TYPE l;
75     void *ptr;
76   } resbuf;
77   LOOKUP_TYPE *result;
78   int save;
79 
80   /* Get lock.  */
81   __libc_lock_lock (lock);
82 
83   result = (LOOKUP_TYPE *)
84     __nss_getent ((getent_r_function) INTERNAL (REENTRANT_GETNAME),
85 		  &resbuf.ptr, &buffer, BUFLEN, &buffer_size,
86 		  H_ERRNO_VAR);
87 
88   save = errno;
89   __libc_lock_unlock (lock);
90   __set_errno (save);
91   return result;
92 }
93 
94 nss_interface_function (GETFUNC_NAME)
95