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