nsdispatch.c revision 1.14 1 /* $NetBSD: nsdispatch.c,v 1.14 1999/09/20 04:39:16 lukem 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.14 1999/09/20 04:39:16 lukem 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 <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <unistd.h>
59
60 #ifdef __STDC__
61 #include <stdarg.h>
62 #else
63 #include <varargs.h>
64 #endif
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
83 /*
84 * size of dynamic array chunk for _nsmap and _nsmap[x].srclist
85 */
86 #define NSELEMSPERCHUNK 8
87
88
89 int _nscmp __P((const void *, const void *));
90
91
92 int
93 _nscmp(a, b)
94 const void *a;
95 const void *b;
96 {
97 return (strcasecmp(((const ns_dbt *)a)->name,
98 ((const ns_dbt *)b)->name));
99 }
100
101
102 void
103 _nsdbtaddsrc(dbt, src)
104 ns_dbt *dbt;
105 const ns_src *src;
106 {
107
108 _DIAGASSERT(dbt != NULL);
109 _DIAGASSERT(src != NULL);
110
111 if ((dbt->srclistsize % NSELEMSPERCHUNK) == 0) {
112 dbt->srclist = (ns_src *)realloc(dbt->srclist,
113 (dbt->srclistsize + NSELEMSPERCHUNK) * sizeof(ns_src));
114 if (dbt->srclist == NULL)
115 err(1, "nsdispatch: memory allocation failure");
116 }
117 memmove(&dbt->srclist[dbt->srclistsize++], src, sizeof(ns_src));
118 }
119
120
121 void
122 _nsdbtdump(dbt)
123 const ns_dbt *dbt;
124 {
125 int i;
126
127 _DIAGASSERT(dbt != NULL);
128
129 printf("%s (%d source%s):", dbt->name, dbt->srclistsize,
130 dbt->srclistsize == 1 ? "" : "s");
131 for (i = 0; i < dbt->srclistsize; i++) {
132 printf(" %s", dbt->srclist[i].name);
133 if (!(dbt->srclist[i].flags &
134 (NS_UNAVAIL|NS_NOTFOUND|NS_TRYAGAIN)) &&
135 (dbt->srclist[i].flags & NS_SUCCESS))
136 continue;
137 printf(" [");
138 if (!(dbt->srclist[i].flags & NS_SUCCESS))
139 printf(" SUCCESS=continue");
140 if (dbt->srclist[i].flags & NS_UNAVAIL)
141 printf(" UNAVAIL=return");
142 if (dbt->srclist[i].flags & NS_NOTFOUND)
143 printf(" NOTFOUND=return");
144 if (dbt->srclist[i].flags & NS_TRYAGAIN)
145 printf(" TRYAGAIN=return");
146 printf(" ]");
147 }
148 printf("\n");
149 }
150
151
152 const ns_dbt *
153 _nsdbtget(name)
154 const char *name;
155 {
156 static time_t confmod;
157
158 struct stat statbuf;
159 ns_dbt dbt;
160
161 extern FILE *_nsyyin;
162 extern int _nsyyparse __P((void));
163
164 _DIAGASSERT(name != NULL);
165
166 dbt.name = name;
167
168 if (confmod) {
169 if (stat(_PATH_NS_CONF, &statbuf) == -1)
170 return (NULL);
171 if (confmod < statbuf.st_mtime) {
172 int i, j;
173
174 for (i = 0; i < _nsmapsize; i++) {
175 for (j = 0; j < _nsmap[i].srclistsize; j++) {
176 if (_nsmap[i].srclist[j].name != NULL) {
177 /*LINTED const cast*/
178 free((void *)
179 _nsmap[i].srclist[j].name);
180 }
181 }
182 if (_nsmap[i].srclist)
183 free(_nsmap[i].srclist);
184 if (_nsmap[i].name) {
185 /*LINTED const cast*/
186 free((void *)_nsmap[i].name);
187 }
188 }
189 if (_nsmap)
190 free(_nsmap);
191 _nsmap = NULL;
192 _nsmapsize = 0;
193 confmod = 0;
194 }
195 }
196 if (!confmod) {
197 if (stat(_PATH_NS_CONF, &statbuf) == -1)
198 return (NULL);
199 _nsyyin = fopen(_PATH_NS_CONF, "r");
200 if (_nsyyin == NULL)
201 return (NULL);
202 _nsyyparse();
203 (void)fclose(_nsyyin);
204 qsort(_nsmap, (size_t)_nsmapsize, sizeof(ns_dbt), _nscmp);
205 confmod = statbuf.st_mtime;
206 }
207 return (bsearch(&dbt, _nsmap, (size_t)_nsmapsize, sizeof(ns_dbt),
208 _nscmp));
209 }
210
211
212 void
213 _nsdbtput(dbt)
214 const ns_dbt *dbt;
215 {
216 int i;
217
218 _DIAGASSERT(dbt != NULL);
219
220 for (i = 0; i < _nsmapsize; i++) {
221 if (_nscmp(dbt, &_nsmap[i]) == 0) {
222 /* overwrite existing entry */
223 if (_nsmap[i].srclist != NULL)
224 free(_nsmap[i].srclist);
225 memmove(&_nsmap[i], dbt, sizeof(ns_dbt));
226 return;
227 }
228 }
229
230 if ((_nsmapsize % NSELEMSPERCHUNK) == 0) {
231 _nsmap = (ns_dbt *)realloc(_nsmap,
232 (_nsmapsize + NSELEMSPERCHUNK) * sizeof(ns_dbt));
233 if (_nsmap == NULL)
234 err(1, "nsdispatch: memory allocation failure");
235 }
236 memmove(&_nsmap[_nsmapsize++], dbt, sizeof(ns_dbt));
237 }
238
239
240 int
241 /*ARGSUSED*/
242 #if __STDC__
243 nsdispatch(void *retval, const ns_dtab disp_tab[], const char *database,
244 const char *method, const ns_src defaults[], ...)
245 #else
246 nsdispatch(retval, disp_tab, database, method, defaults, va_alist)
247 void *retval;
248 const ns_dtab disp_tab[];
249 const char *database;
250 const char *method;
251 const ns_src defaults[];
252 va_dcl
253 #endif
254 {
255 va_list ap;
256 int i, curdisp, result;
257 const ns_dbt *dbt;
258 const ns_src *srclist;
259 int srclistsize;
260
261 _DIAGASSERT(database != NULL);
262 _DIAGASSERT(method != NULL);
263 if (database == NULL || method == NULL)
264 return (NS_UNAVAIL);
265
266 dbt = _nsdbtget(database);
267 if (dbt != NULL) {
268 srclist = dbt->srclist;
269 srclistsize = dbt->srclistsize;
270 } else {
271 srclist = defaults;
272 srclistsize = 0;
273 while (srclist[srclistsize].name != NULL)
274 srclistsize++;
275 }
276 result = 0;
277
278 for (i = 0; i < srclistsize; i++) {
279 for (curdisp = 0; disp_tab[curdisp].src != NULL; curdisp++)
280 if (strcasecmp(disp_tab[curdisp].src,
281 srclist[i].name) == 0)
282 break;
283 result = 0;
284 if (disp_tab[curdisp].callback) {
285 #if __STDC__
286 va_start(ap, defaults);
287 #else
288 va_start(ap);
289 #endif
290 result = disp_tab[curdisp].callback(retval,
291 disp_tab[curdisp].cb_data, ap);
292 va_end(ap);
293 if (result & srclist[i].flags) {
294 break;
295 }
296 }
297 }
298 return (result ? result : NS_NOTFOUND);
299 }
300