Blame nss/getXXent_r.c

Packit 6c4009
/* Copyright (C) 1996-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <libc-lock.h>
Packit 6c4009
Packit 6c4009
#include "nsswitch.h"
Packit 6c4009
Packit 6c4009
/*******************************************************************\
Packit 6c4009
|* Here we assume several symbols to be defined:		   *|
Packit 6c4009
|* 								   *|
Packit 6c4009
|* LOOKUP_TYPE   - the return type of the function		   *|
Packit 6c4009
|* 								   *|
Packit 6c4009
|* SETFUNC_NAME  - name of the non-reentrant setXXXent function	   *|
Packit 6c4009
|* 								   *|
Packit 6c4009
|* GETFUNC_NAME  - name of the non-reentrant getXXXent function	   *|
Packit 6c4009
|* 								   *|
Packit 6c4009
|* ENDFUNC_NAME  - name of the non-reentrant endXXXent function	   *|
Packit 6c4009
|* 								   *|
Packit 6c4009
|* DATABASE_NAME - name of the database the function accesses	   *|
Packit 6c4009
|*		   (e.g., host, services, ...)			   *|
Packit 6c4009
|* 								   *|
Packit 6c4009
|* Optionally the following vars can be defined:		   *|
Packit 6c4009
|* 								   *|
Packit 6c4009
|* STAYOPEN      - variable declaration for setXXXent function	   *|
Packit 6c4009
|* 								   *|
Packit 6c4009
|* STAYOPEN_VAR  - variable name for setXXXent function		   *|
Packit 6c4009
|* 								   *|
Packit 6c4009
|* NEED_H_ERRNO  - an extra parameter will be passed to point to   *|
Packit 6c4009
|*		   the global `h_errno' variable.		   *|
Packit 6c4009
|* 								   *|
Packit 6c4009
\*******************************************************************/
Packit 6c4009
Packit 6c4009
/* To make the real sources a bit prettier.  */
Packit 6c4009
#define REENTRANT_GETNAME APPEND_R (GETFUNC_NAME)
Packit 6c4009
#define APPEND_R(Name) CONCAT2_2 (Name, _r)
Packit 6c4009
#define INTERNAL(Name) CONCAT2_2 (__, Name)
Packit 6c4009
#define CONCAT2_1(Pre, Post) CONCAT2_2 (Pre, Post)
Packit 6c4009
#define CONCAT2_2(Pre, Post) Pre##Post
Packit 6c4009
#define NEW(name) NEW1 (name)
Packit 6c4009
#define NEW1(name) __new_##name
Packit 6c4009
Packit 6c4009
#define SETFUNC_NAME_STRING STRINGIZE (SETFUNC_NAME)
Packit 6c4009
#define GETFUNC_NAME_STRING STRINGIZE (REENTRANT_GETNAME)
Packit 6c4009
#define ENDFUNC_NAME_STRING STRINGIZE (ENDFUNC_NAME)
Packit 6c4009
#define DATABASE_NAME_STRING STRINGIZE (DATABASE_NAME)
Packit 6c4009
#define STRINGIZE(Name) STRINGIZE1 (Name)
Packit 6c4009
#define STRINGIZE1(Name) #Name
Packit 6c4009
Packit 6c4009
#ifndef DB_LOOKUP_FCT
Packit 6c4009
# define DB_LOOKUP_FCT CONCAT3_1 (__nss_, DATABASE_NAME, _lookup2)
Packit 6c4009
# define CONCAT3_1(Pre, Name, Post) CONCAT3_2 (Pre, Name, Post)
Packit 6c4009
# define CONCAT3_2(Pre, Name, Post) Pre##Name##Post
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
/* Sometimes we need to store error codes in the `h_errno' variable.  */
Packit 6c4009
#ifdef NEED_H_ERRNO
Packit 6c4009
# define H_ERRNO_PARM , int *h_errnop
Packit 6c4009
# define H_ERRNO_VAR , &h_errno
Packit 6c4009
# define H_ERRNO_VAR_P &h_errno
Packit 6c4009
#else
Packit 6c4009
# define H_ERRNO_PARM
Packit 6c4009
# define H_ERRNO_VAR
Packit 6c4009
# define H_ERRNO_VAR_P NULL
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
/* Some databases take the `stayopen' flag.  */
Packit 6c4009
#ifdef STAYOPEN
Packit 6c4009
# define STAYOPEN_TMP CONCAT2_1 (STAYOPEN, _tmp)
Packit 6c4009
# define STAYOPEN_TMPVAR &CONCAT2_1 (STAYOPEN_VAR, _tmp)
Packit 6c4009
#else
Packit 6c4009
# define STAYOPEN void
Packit 6c4009
# define STAYOPEN_VAR 0
Packit 6c4009
# define STAYOPEN_TMPVAR NULL
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#ifndef NEED__RES
Packit 6c4009
# define NEED__RES 0
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
/* This handle for the NSS data base is shared between all
Packit 6c4009
   set/get/endXXXent functions.  */
Packit 6c4009
static service_user *nip;
Packit 6c4009
/* Remember the last service used since the last call to  `endXXent'.  */
Packit 6c4009
static service_user *last_nip;
Packit 6c4009
/* Remember the first service_entry, it's always the same.  */
Packit 6c4009
static service_user *startp;
Packit 6c4009
Packit 6c4009
#ifdef STAYOPEN_TMP
Packit 6c4009
/* We need to remember the last `stayopen' flag given by the user
Packit 6c4009
   since the `setent' function is only called for the first available
Packit 6c4009
   service.  */
Packit 6c4009
static STAYOPEN_TMP;
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
/* Protect above variable against multiple uses at the same time.  */
Packit 6c4009
__libc_lock_define_initialized (static, lock)
Packit 6c4009
Packit 6c4009
/* The lookup function for the first entry of this service.  */
Packit 6c4009
extern int DB_LOOKUP_FCT (service_user **nip, const char *name,
Packit 6c4009
			  const char *name2, void **fctp);
Packit 6c4009
libc_hidden_proto (DB_LOOKUP_FCT)
Packit 6c4009

Packit 6c4009
void
Packit 6c4009
SETFUNC_NAME (STAYOPEN)
Packit 6c4009
{
Packit 6c4009
  int save;
Packit 6c4009
Packit 6c4009
  __libc_lock_lock (lock);
Packit 6c4009
  __nss_setent (SETFUNC_NAME_STRING, DB_LOOKUP_FCT, &nip, &startp,
Packit 6c4009
		&last_nip, STAYOPEN_VAR, STAYOPEN_TMPVAR, NEED__RES);
Packit 6c4009
Packit 6c4009
  save = errno;
Packit 6c4009
  __libc_lock_unlock (lock);
Packit 6c4009
  __set_errno (save);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
ENDFUNC_NAME (void)
Packit 6c4009
{
Packit 6c4009
  int save;
Packit 6c4009
Packit 6c4009
  /* If the service has not been used before do not do anything.  */
Packit 6c4009
  if (startp != NULL)
Packit 6c4009
    {
Packit 6c4009
      __libc_lock_lock (lock);
Packit 6c4009
      __nss_endent (ENDFUNC_NAME_STRING, DB_LOOKUP_FCT, &nip, &startp,
Packit 6c4009
		    &last_nip, NEED__RES);
Packit 6c4009
      save = errno;
Packit 6c4009
      __libc_lock_unlock (lock);
Packit 6c4009
      __set_errno (save);
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
INTERNAL (REENTRANT_GETNAME) (LOOKUP_TYPE *resbuf, char *buffer, size_t buflen,
Packit 6c4009
			      LOOKUP_TYPE **result H_ERRNO_PARM)
Packit 6c4009
{
Packit 6c4009
  int status;
Packit 6c4009
  int save;
Packit 6c4009
Packit 6c4009
  __libc_lock_lock (lock);
Packit 6c4009
  status = __nss_getent_r (GETFUNC_NAME_STRING, SETFUNC_NAME_STRING,
Packit 6c4009
			   DB_LOOKUP_FCT, &nip, &startp, &last_nip,
Packit 6c4009
			   STAYOPEN_TMPVAR, NEED__RES, resbuf, buffer,
Packit 6c4009
			   buflen, (void **) result, H_ERRNO_VAR_P);
Packit 6c4009
  save = errno;
Packit 6c4009
  __libc_lock_unlock (lock);
Packit 6c4009
  __set_errno (save);
Packit 6c4009
  return status;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
#ifdef NO_COMPAT_NEEDED
Packit 6c4009
strong_alias (INTERNAL (REENTRANT_GETNAME), REENTRANT_GETNAME);
Packit 6c4009
#else
Packit 6c4009
# include <shlib-compat.h>
Packit 6c4009
# if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_1_2)
Packit 6c4009
#  define OLD(name) OLD1 (name)
Packit 6c4009
#  define OLD1(name) __old_##name
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
attribute_compat_text_section
Packit 6c4009
OLD (REENTRANT_GETNAME) (LOOKUP_TYPE *resbuf, char *buffer, size_t buflen,
Packit 6c4009
			 LOOKUP_TYPE **result H_ERRNO_PARM)
Packit 6c4009
{
Packit 6c4009
  int ret = INTERNAL (REENTRANT_GETNAME) (resbuf, buffer, buflen,
Packit 6c4009
					  result H_ERRNO_VAR);
Packit 6c4009
Packit 6c4009
  if (ret != 0)
Packit 6c4009
    ret = -1;
Packit 6c4009
Packit 6c4009
  return ret;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#  define do_symbol_version(real, name, version) \
Packit 6c4009
  compat_symbol (libc, real, name, version)
Packit 6c4009
do_symbol_version (OLD (REENTRANT_GETNAME), REENTRANT_GETNAME, GLIBC_2_0);
Packit 6c4009
# endif
Packit 6c4009
Packit 6c4009
/* As INTERNAL (REENTRANT_GETNAME) may be hidden, we need an alias
Packit 6c4009
   in between so that the REENTRANT_GETNAME@@GLIBC_2.1.2 is not
Packit 6c4009
   hidden too.  */
Packit 6c4009
strong_alias (INTERNAL (REENTRANT_GETNAME), NEW (REENTRANT_GETNAME));
Packit 6c4009
Packit 6c4009
# define do_default_symbol_version(real, name, version) \
Packit 6c4009
  versioned_symbol (libc, real, name, version)
Packit 6c4009
do_default_symbol_version (NEW (REENTRANT_GETNAME),
Packit 6c4009
			   REENTRANT_GETNAME, GLIBC_2_1_2);
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
nss_interface_function (SETFUNC_NAME)
Packit 6c4009
nss_interface_function (ENDFUNC_NAME)
Packit 6c4009
nss_interface_function (REENTRANT_GETNAME)