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