Update.
[platform/upstream/glibc.git] / nss / getXXent_r.c
1 /* Copyright (C) 1996,97,98,99,2000,2002 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
4
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.
9
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.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
19
20 #include <errno.h>
21 #include <bits/libc-lock.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 |* SETFUNC_NAME  - name of the non-reentrant setXXXent function    *|
31 |*                                                                 *|
32 |* GETFUNC_NAME  - name of the non-reentrant getXXXent function    *|
33 |*                                                                 *|
34 |* ENDFUNC_NAME  - name of the non-reentrant endXXXent function    *|
35 |*                                                                 *|
36 |* DATABASE_NAME - name of the database the function accesses      *|
37 |*                 (e.g., host, services, ...)                     *|
38 |*                                                                 *|
39 |* Optionally the following vars can be defined:                   *|
40 |*                                                                 *|
41 |* STAYOPEN      - variable declaration for setXXXent function     *|
42 |*                                                                 *|
43 |* STAYOPEN_VAR  - variable name for setXXXent function            *|
44 |*                                                                 *|
45 |* NEED_H_ERRNO  - an extra parameter will be passed to point to   *|
46 |*                 the global `h_errno' variable.                  *|
47 |*                                                                 *|
48 \*******************************************************************/
49
50 /* To make the real sources a bit prettier.  */
51 #define REENTRANT_GETNAME APPEND_R (GETFUNC_NAME)
52 #define APPEND_R(Name) CONCAT2_2 (Name, _r)
53 #define INTERNAL(Name) CONCAT2_2 (__, Name)
54 #define CONCAT2_1(Pre, Post) CONCAT2_2 (Pre, Post)
55 #define CONCAT2_2(Pre, Post) Pre##Post
56
57 #define SETFUNC_NAME_STRING STRINGIZE (SETFUNC_NAME)
58 #define GETFUNC_NAME_STRING STRINGIZE (REENTRANT_GETNAME)
59 #define ENDFUNC_NAME_STRING STRINGIZE (ENDFUNC_NAME)
60 #define DATABASE_NAME_STRING STRINGIZE (DATABASE_NAME)
61 #define STRINGIZE(Name) STRINGIZE1 (Name)
62 #define STRINGIZE1(Name) #Name
63
64 #ifndef DB_LOOKUP_FCT
65 # define DB_LOOKUP_FCT CONCAT3_1 (__nss_, DATABASE_NAME, _lookup)
66 # define CONCAT3_1(Pre, Name, Post) CONCAT3_2 (Pre, Name, Post)
67 # define CONCAT3_2(Pre, Name, Post) Pre##Name##Post
68 #endif
69
70 /* Sometimes we need to store error codes in the `h_errno' variable.  */
71 #ifdef NEED_H_ERRNO
72 # define H_ERRNO_PARM , int *h_errnop
73 # define H_ERRNO_VAR , &h_errno
74 # define H_ERRNO_VAR_P &h_errno
75 #else
76 # define H_ERRNO_PARM
77 # define H_ERRNO_VAR
78 # define H_ERRNO_VAR_P NULL
79 #endif
80
81 /* Some databases take the `stayopen' flag.  */
82 #ifdef STAYOPEN
83 # define STAYOPEN_TMP CONCAT2_1 (STAYOPEN, _tmp)
84 # define STAYOPEN_TMPVAR &CONCAT2_1 (STAYOPEN_VAR, _tmp)
85 #else
86 # define STAYOPEN void
87 # define STAYOPEN_VAR 0
88 # define STAYOPEN_TMPVAR NULL
89 #endif
90
91 #ifndef NEED__RES
92 # define NEED__RES 0
93 #endif
94
95 /* This handle for the NSS data base is shared between all
96    set/get/endXXXent functions.  */
97 static service_user *nip;
98 /* Remember the last service used since the last call to  `endXXent'.  */
99 static service_user *last_nip;
100 /* Remember the first service_entry, it's always the same.  */
101 static service_user *startp;
102
103 #ifdef STAYOPEN_TMP
104 /* We need to remember the last `stayopen' flag given by the user
105    since the `setent' function is only called for the first available
106    service.  */
107 static STAYOPEN_TMP;
108 #endif
109
110 /* Protect above variable against multiple uses at the same time.  */
111 __libc_lock_define_initialized (static, lock)
112
113 /* The lookup function for the first entry of this service.  */
114 extern int DB_LOOKUP_FCT (service_user **nip, const char *name, void **fctp);
115 \f
116 void
117 SETFUNC_NAME (STAYOPEN)
118 {
119   int save;
120
121   __libc_lock_lock (lock);
122   __nss_setent (SETFUNC_NAME_STRING, DB_LOOKUP_FCT, &nip, &startp,
123                 &last_nip, STAYOPEN_VAR, STAYOPEN_TMPVAR, NEED__RES);
124
125   save = errno;
126   __libc_lock_unlock (lock);
127   __set_errno (save);
128 }
129
130
131 void
132 ENDFUNC_NAME (void)
133 {
134   int save;
135
136   __libc_lock_lock (lock);
137   __nss_endent (ENDFUNC_NAME_STRING, DB_LOOKUP_FCT, &nip, &startp,
138                 &last_nip, NEED__RES);
139   save = errno;
140   __libc_lock_unlock (lock);
141   __set_errno (save);
142 }
143
144
145 int
146 INTERNAL (REENTRANT_GETNAME) (LOOKUP_TYPE *resbuf, char *buffer, size_t buflen,
147                               LOOKUP_TYPE **result H_ERRNO_PARM)
148 {
149   int status;
150   int save;
151
152   __libc_lock_lock (lock);
153   status = __nss_getent_r (GETFUNC_NAME_STRING, SETFUNC_NAME_STRING,
154                            DB_LOOKUP_FCT, &nip, &startp, &last_nip,
155                            STAYOPEN_TMPVAR, NEED__RES, resbuf, buffer,
156                            buflen, (void **) result, H_ERRNO_VAR_P);
157   save = errno;
158   __libc_lock_unlock (lock);
159   __set_errno (save);
160   return status;
161 }
162
163
164 #include <shlib-compat.h>
165 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_1_2)
166 #define OLD(name) OLD1 (name)
167 #define OLD1(name) __old_##name
168
169 int
170 OLD (REENTRANT_GETNAME) (LOOKUP_TYPE *resbuf, char *buffer, size_t buflen,
171                          LOOKUP_TYPE **result H_ERRNO_PARM)
172 {
173   int ret = INTERNAL (REENTRANT_GETNAME) (resbuf, buffer, buflen,
174                                           result H_ERRNO_VAR);
175
176   if (ret != 0)
177     ret = -1;
178
179   return ret;
180 }
181
182 #define do_symbol_version(real, name, version) \
183   compat_symbol (libc, real, name, version)
184 do_symbol_version (OLD (REENTRANT_GETNAME), REENTRANT_GETNAME, GLIBC_2_0);
185 #endif
186
187 #define do_default_symbol_version(real, name, version) \
188   versioned_symbol (libc, real, name, version)
189 do_default_symbol_version (INTERNAL (REENTRANT_GETNAME),
190                            REENTRANT_GETNAME, GLIBC_2_1_2);