nsdispatch.c revision 1.21 1 /* $NetBSD: nsdispatch.c,v 1.21 2004/07/16 16:11:43 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1997, 1998, 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Luke Mewburn.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 #if defined(LIBC_SCCS) && !defined(lint)
41 __RCSID("$NetBSD: nsdispatch.c,v 1.21 2004/07/16 16:11:43 thorpej Exp $");
42 #endif /* LIBC_SCCS and not lint */
43
44 #include "namespace.h"
45
46 #include <sys/types.h>
47 #include <sys/param.h>
48 #include <sys/stat.h>
49
50 #include <assert.h>
51 #include <err.h>
52 #include <fcntl.h>
53 #define _NS_PRIVATE
54 #include <nsswitch.h>
55 #include <stdarg.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <unistd.h>
60 #include <threadlib.h>
61
62 extern FILE *_nsyyin;
63 extern int _nsyyparse(void);
64
65
66 #ifdef __weak_alias
67 __weak_alias(nsdispatch,_nsdispatch)
68 #endif
69
70
71 /*
72 * default sourcelist: `files'
73 */
74 const ns_src __nsdefaultsrc[] = {
75 { NSSRC_FILES, NS_SUCCESS },
76 { 0 },
77 };
78
79
80 static int _nsmapsize = 0;
81 static ns_dbt *_nsmap = NULL;
82 #ifdef _REENTRANT
83 static mutex_t _nsmutex = MUTEX_INITIALIZER;
84 #define NSLOCK() mutex_lock(&_nsmutex)
85 #define NSUNLOCK() mutex_unlock(&_nsmutex)
86 #else
87 #define NSLOCK()
88 #define NSUNLOCK()
89 #endif
90
91
92 /*
93 * size of dynamic array chunk for _nsmap and _nsmap[x].srclist
94 */
95 #define NSELEMSPERCHUNK 8
96
97
98 static int
99 _nscmp(const void *a, const void *b)
100 {
101 return (strcasecmp(((const ns_dbt *)a)->name,
102 ((const ns_dbt *)b)->name));
103 }
104
105
106 int
107 _nsdbtaddsrc(ns_dbt *dbt, const ns_src *src)
108 {
109
110 _DIAGASSERT(dbt != NULL);
111 _DIAGASSERT(src != NULL);
112
113 if ((dbt->srclistsize % NSELEMSPERCHUNK) == 0) {
114 ns_src *new;
115
116 new = (ns_src *)realloc(dbt->srclist,
117 (dbt->srclistsize + NSELEMSPERCHUNK) * sizeof(ns_src));
118 if (new == NULL)
119 return (-1);
120 dbt->srclist = new;
121 }
122 memmove(&dbt->srclist[dbt->srclistsize++], src, sizeof(ns_src));
123 return (0);
124 }
125
126
127 void
128 _nsdbtdump(const ns_dbt *dbt)
129 {
130 int i;
131
132 _DIAGASSERT(dbt != NULL);
133
134 printf("%s (%d source%s):", dbt->name, dbt->srclistsize,
135 dbt->srclistsize == 1 ? "" : "s");
136 for (i = 0; i < dbt->srclistsize; i++) {
137 printf(" %s", dbt->srclist[i].name);
138 if (!(dbt->srclist[i].flags &
139 (NS_UNAVAIL|NS_NOTFOUND|NS_TRYAGAIN)) &&
140 (dbt->srclist[i].flags & NS_SUCCESS))
141 continue;
142 printf(" [");
143 if (!(dbt->srclist[i].flags & NS_SUCCESS))
144 printf(" SUCCESS=continue");
145 if (dbt->srclist[i].flags & NS_UNAVAIL)
146 printf(" UNAVAIL=return");
147 if (dbt->srclist[i].flags & NS_NOTFOUND)
148 printf(" NOTFOUND=return");
149 if (dbt->srclist[i].flags & NS_TRYAGAIN)
150 printf(" TRYAGAIN=return");
151 printf(" ]");
152 }
153 printf("\n");
154 }
155
156
157 const ns_dbt *
158 _nsdbtget(const char *name)
159 {
160 static time_t confmod;
161
162 struct stat statbuf;
163 ns_dbt dbt;
164
165 _DIAGASSERT(name != NULL);
166
167 dbt.name = name;
168
169 NSLOCK();
170 if (stat(_PATH_NS_CONF, &statbuf) == -1)
171 return (NULL);
172 if (confmod) {
173 if (confmod < statbuf.st_mtime) {
174 int i, j;
175
176 for (i = 0; i < _nsmapsize; i++) {
177 for (j = 0; j < _nsmap[i].srclistsize; j++) {
178 if (_nsmap[i].srclist[j].name != NULL) {
179 /*LINTED const cast*/
180 free((void *)
181 _nsmap[i].srclist[j].name);
182 }
183 }
184 if (_nsmap[i].srclist)
185 free(_nsmap[i].srclist);
186 if (_nsmap[i].name) {
187 /*LINTED const cast*/
188 free((void *)_nsmap[i].name);
189 }
190 }
191 if (_nsmap)
192 free(_nsmap);
193 _nsmap = NULL;
194 _nsmapsize = 0;
195 confmod = 0;
196 }
197 }
198 if (!confmod) {
199 _nsyyin = fopen(_PATH_NS_CONF, "r");
200 if (_nsyyin == NULL) {
201 NSUNLOCK();
202 return (NULL);
203 }
204 _nsyyparse();
205 (void)fclose(_nsyyin);
206 qsort(_nsmap, (size_t)_nsmapsize, sizeof(ns_dbt), _nscmp);
207 confmod = statbuf.st_mtime;
208 }
209 NSUNLOCK();
210 return (bsearch(&dbt, _nsmap, (size_t)_nsmapsize, sizeof(ns_dbt),
211 _nscmp));
212 }
213
214
215 int
216 _nsdbtput(const ns_dbt *dbt)
217 {
218 int i;
219
220 _DIAGASSERT(dbt != NULL);
221
222 for (i = 0; i < _nsmapsize; i++) {
223 if (_nscmp(dbt, &_nsmap[i]) == 0) {
224 /* overwrite existing entry */
225 if (_nsmap[i].srclist != NULL)
226 free(_nsmap[i].srclist);
227 memmove(&_nsmap[i], dbt, sizeof(ns_dbt));
228 return (0);
229 }
230 }
231
232 if ((_nsmapsize % NSELEMSPERCHUNK) == 0) {
233 ns_dbt *new;
234
235 new = (ns_dbt *)realloc(_nsmap,
236 (_nsmapsize + NSELEMSPERCHUNK) * sizeof(ns_dbt));
237 if (new == NULL)
238 return (-1);
239 _nsmap = new;
240 }
241 memmove(&_nsmap[_nsmapsize++], dbt, sizeof(ns_dbt));
242 return (0);
243 }
244
245
246 int
247 /*ARGSUSED*/
248 nsdispatch(void *retval, const ns_dtab disp_tab[], const char *database,
249 const char *method, const ns_src defaults[], ...)
250 {
251 va_list ap;
252 int i, curdisp, result;
253 const ns_dbt *dbt;
254 const ns_src *srclist;
255 int srclistsize;
256
257 _DIAGASSERT(database != NULL);
258 _DIAGASSERT(method != NULL);
259 if (database == NULL || method == NULL)
260 return (NS_UNAVAIL);
261
262 dbt = _nsdbtget(database);
263 if (dbt != NULL) {
264 srclist = dbt->srclist;
265 srclistsize = dbt->srclistsize;
266 } else {
267 srclist = defaults;
268 srclistsize = 0;
269 while (srclist[srclistsize].name != NULL)
270 srclistsize++;
271 }
272 result = 0;
273
274 for (i = 0; i < srclistsize; i++) {
275 for (curdisp = 0; disp_tab[curdisp].src != NULL; curdisp++)
276 if (strcasecmp(disp_tab[curdisp].src,
277 srclist[i].name) == 0)
278 break;
279 result = 0;
280 if (disp_tab[curdisp].callback) {
281 va_start(ap, defaults);
282 result = disp_tab[curdisp].callback(retval,
283 disp_tab[curdisp].cb_data, ap);
284 va_end(ap);
285 if (result & srclist[i].flags) {
286 break;
287 }
288 }
289 }
290 return (result ? result : NS_NOTFOUND);
291 }
292