]>
Commit | Line | Data |
---|---|---|
deb84c43 | 1 | /* Copyright (C) 1996-2000,2002,2004,2007, 2009 Free Software Foundation, Inc. |
2303f5fd UD |
2 | This file is part of the GNU C Library. |
3 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996. | |
5f0e6fc7 | 4 | |
2303f5fd | 5 | The GNU C Library is free software; you can redistribute it and/or |
41bdb6e2 AJ |
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. | |
5f0e6fc7 | 9 | |
2303f5fd UD |
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 | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
5f0e6fc7 | 14 | |
41bdb6e2 AJ |
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. */ | |
5f0e6fc7 | 19 | |
d71b808a | 20 | #include <errno.h> |
5107cf1d | 21 | #include <bits/libc-lock.h> |
5f0e6fc7 RM |
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) | |
2303f5fd UD |
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 | |
bff334e0 UD |
56 | #define NEW(name) NEW1 (name) |
57 | #define NEW1(name) __new_##name | |
5f0e6fc7 RM |
58 | |
59 | #define SETFUNC_NAME_STRING STRINGIZE (SETFUNC_NAME) | |
ddc6fb0c | 60 | #define GETFUNC_NAME_STRING STRINGIZE (REENTRANT_GETNAME) |
5f0e6fc7 RM |
61 | #define ENDFUNC_NAME_STRING STRINGIZE (ENDFUNC_NAME) |
62 | #define DATABASE_NAME_STRING STRINGIZE (DATABASE_NAME) | |
2303f5fd UD |
63 | #define STRINGIZE(Name) STRINGIZE1 (Name) |
64 | #define STRINGIZE1(Name) #Name | |
5f0e6fc7 | 65 | |
77fe0b9c | 66 | #ifndef DB_LOOKUP_FCT |
384ca551 | 67 | # define DB_LOOKUP_FCT CONCAT3_1 (__nss_, DATABASE_NAME, _lookup2) |
77fe0b9c UD |
68 | # define CONCAT3_1(Pre, Name, Post) CONCAT3_2 (Pre, Name, Post) |
69 | # define CONCAT3_2(Pre, Name, Post) Pre##Name##Post | |
70 | #endif | |
5f0e6fc7 RM |
71 | |
72 | /* Sometimes we need to store error codes in the `h_errno' variable. */ | |
73 | #ifdef NEED_H_ERRNO | |
74 | # define H_ERRNO_PARM , int *h_errnop | |
75 | # define H_ERRNO_VAR , &h_errno | |
8b801829 | 76 | # define H_ERRNO_VAR_P &h_errno |
5f0e6fc7 RM |
77 | #else |
78 | # define H_ERRNO_PARM | |
79 | # define H_ERRNO_VAR | |
8b801829 | 80 | # define H_ERRNO_VAR_P NULL |
5f0e6fc7 RM |
81 | #endif |
82 | ||
83 | /* Some databases take the `stayopen' flag. */ | |
2303f5fd UD |
84 | #ifdef STAYOPEN |
85 | # define STAYOPEN_TMP CONCAT2_1 (STAYOPEN, _tmp) | |
8b801829 | 86 | # define STAYOPEN_TMPVAR &CONCAT2_1 (STAYOPEN_VAR, _tmp) |
2303f5fd UD |
87 | #else |
88 | # define STAYOPEN void | |
8b801829 UD |
89 | # define STAYOPEN_VAR 0 |
90 | # define STAYOPEN_TMPVAR NULL | |
5f0e6fc7 RM |
91 | #endif |
92 | ||
8b801829 UD |
93 | #ifndef NEED__RES |
94 | # define NEED__RES 0 | |
95 | #endif | |
5f0e6fc7 RM |
96 | |
97 | /* This handle for the NSS data base is shared between all | |
98 | set/get/endXXXent functions. */ | |
99 | static service_user *nip; | |
d4a089cf UD |
100 | /* Remember the last service used since the last call to `endXXent'. */ |
101 | static service_user *last_nip; | |
5f0e6fc7 RM |
102 | /* Remember the first service_entry, it's always the same. */ |
103 | static service_user *startp; | |
104 | ||
2303f5fd UD |
105 | #ifdef STAYOPEN_TMP |
106 | /* We need to remember the last `stayopen' flag given by the user | |
107 | since the `setent' function is only called for the first available | |
108 | service. */ | |
109 | static STAYOPEN_TMP; | |
110 | #endif | |
111 | ||
5f0e6fc7 | 112 | /* Protect above variable against multiple uses at the same time. */ |
1e16111c | 113 | __libc_lock_define_initialized (static, lock) |
5f0e6fc7 RM |
114 | |
115 | /* The lookup function for the first entry of this service. */ | |
384ca551 UD |
116 | extern int DB_LOOKUP_FCT (service_user **nip, const char *name, |
117 | const char *name2, void **fctp) | |
37ba7d66 UD |
118 | internal_function; |
119 | libc_hidden_proto (DB_LOOKUP_FCT) | |
3dbe1581 | 120 | \f |
5f0e6fc7 | 121 | void |
6dbe2837 | 122 | SETFUNC_NAME (STAYOPEN) |
5f0e6fc7 | 123 | { |
8b801829 | 124 | int save; |
5f0e6fc7 RM |
125 | |
126 | __libc_lock_lock (lock); | |
8b801829 UD |
127 | __nss_setent (SETFUNC_NAME_STRING, DB_LOOKUP_FCT, &nip, &startp, |
128 | &last_nip, STAYOPEN_VAR, STAYOPEN_TMPVAR, NEED__RES); | |
5f0e6fc7 | 129 | |
8b801829 | 130 | save = errno; |
5f0e6fc7 | 131 | __libc_lock_unlock (lock); |
8b801829 | 132 | __set_errno (save); |
5f0e6fc7 RM |
133 | } |
134 | ||
135 | ||
136 | void | |
6dbe2837 | 137 | ENDFUNC_NAME (void) |
5f0e6fc7 | 138 | { |
8b801829 | 139 | int save; |
5f0e6fc7 | 140 | |
226e9fda UD |
141 | /* If the service has not been used before do not do anything. */ |
142 | if (startp != NULL) | |
143 | { | |
144 | __libc_lock_lock (lock); | |
145 | __nss_endent (ENDFUNC_NAME_STRING, DB_LOOKUP_FCT, &nip, &startp, | |
146 | &last_nip, NEED__RES); | |
147 | save = errno; | |
148 | __libc_lock_unlock (lock); | |
149 | __set_errno (save); | |
150 | } | |
5f0e6fc7 RM |
151 | } |
152 | ||
153 | ||
ba1ffaa1 UD |
154 | int |
155 | INTERNAL (REENTRANT_GETNAME) (LOOKUP_TYPE *resbuf, char *buffer, size_t buflen, | |
156 | LOOKUP_TYPE **result H_ERRNO_PARM) | |
5f0e6fc7 | 157 | { |
8b801829 UD |
158 | int status; |
159 | int save; | |
5f0e6fc7 | 160 | |
3dbe1581 | 161 | __libc_lock_lock (lock); |
8b801829 UD |
162 | status = __nss_getent_r (GETFUNC_NAME_STRING, SETFUNC_NAME_STRING, |
163 | DB_LOOKUP_FCT, &nip, &startp, &last_nip, | |
164 | STAYOPEN_TMPVAR, NEED__RES, resbuf, buffer, | |
165 | buflen, (void **) result, H_ERRNO_VAR_P); | |
166 | save = errno; | |
5f0e6fc7 | 167 | __libc_lock_unlock (lock); |
8b801829 UD |
168 | __set_errno (save); |
169 | return status; | |
5f0e6fc7 | 170 | } |
16710d58 RM |
171 | |
172 | ||
deb84c43 UD |
173 | #ifdef NO_COMPAT_NEEDED |
174 | strong_alias (INTERNAL (REENTRANT_GETNAME), REENTRANT_GETNAME); | |
175 | #else | |
176 | # include <shlib-compat.h> | |
177 | # if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_1_2) | |
178 | # define OLD(name) OLD1 (name) | |
179 | # define OLD1(name) __old_##name | |
c2fa5b5a UD |
180 | |
181 | int | |
4a381a81 | 182 | attribute_compat_text_section |
c2fa5b5a UD |
183 | OLD (REENTRANT_GETNAME) (LOOKUP_TYPE *resbuf, char *buffer, size_t buflen, |
184 | LOOKUP_TYPE **result H_ERRNO_PARM) | |
185 | { | |
186 | int ret = INTERNAL (REENTRANT_GETNAME) (resbuf, buffer, buflen, | |
187 | result H_ERRNO_VAR); | |
188 | ||
189 | if (ret != 0) | |
190 | ret = -1; | |
191 | ||
192 | return ret; | |
193 | } | |
194 | ||
deb84c43 | 195 | # define do_symbol_version(real, name, version) \ |
16710d58 RM |
196 | compat_symbol (libc, real, name, version) |
197 | do_symbol_version (OLD (REENTRANT_GETNAME), REENTRANT_GETNAME, GLIBC_2_0); | |
deb84c43 | 198 | # endif |
c2fa5b5a | 199 | |
bff334e0 UD |
200 | /* As INTERNAL (REENTRANT_GETNAME) may be hidden, we need an alias |
201 | in between so that the REENTRANT_GETNAME@@GLIBC_2.1.2 is not | |
202 | hidden too. */ | |
203 | strong_alias (INTERNAL (REENTRANT_GETNAME), NEW (REENTRANT_GETNAME)); | |
204 | ||
deb84c43 | 205 | # define do_default_symbol_version(real, name, version) \ |
16710d58 | 206 | versioned_symbol (libc, real, name, version) |
bff334e0 | 207 | do_default_symbol_version (NEW (REENTRANT_GETNAME), |
16710d58 | 208 | REENTRANT_GETNAME, GLIBC_2_1_2); |
deb84c43 | 209 | #endif |
2f7f7bc6 UD |
210 | |
211 | static_link_warning (SETFUNC_NAME) | |
212 | static_link_warning (ENDFUNC_NAME) | |
213 | static_link_warning (REENTRANT_GETNAME) |