nsdispatch.c revision 1.21 1 1.21 thorpej /* $NetBSD: nsdispatch.c,v 1.21 2004/07/16 16:11:43 thorpej 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.21 thorpej __RCSID("$NetBSD: nsdispatch.c,v 1.21 2004/07/16 16:11:43 thorpej 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.19 christos #include <threadlib.h>
61 1.10 lukem
62 1.17 christos extern FILE *_nsyyin;
63 1.21 thorpej extern int _nsyyparse(void);
64 1.17 christos
65 1.17 christos
66 1.6 lukem #ifdef __weak_alias
67 1.16 mycroft __weak_alias(nsdispatch,_nsdispatch)
68 1.6 lukem #endif
69 1.2 lukem
70 1.2 lukem
71 1.5 lukem /*
72 1.5 lukem * default sourcelist: `files'
73 1.5 lukem */
74 1.5 lukem const ns_src __nsdefaultsrc[] = {
75 1.5 lukem { NSSRC_FILES, NS_SUCCESS },
76 1.5 lukem { 0 },
77 1.5 lukem };
78 1.5 lukem
79 1.5 lukem
80 1.2 lukem static int _nsmapsize = 0;
81 1.2 lukem static ns_dbt *_nsmap = NULL;
82 1.19 christos #ifdef _REENTRANT
83 1.19 christos static mutex_t _nsmutex = MUTEX_INITIALIZER;
84 1.19 christos #define NSLOCK() mutex_lock(&_nsmutex)
85 1.19 christos #define NSUNLOCK() mutex_unlock(&_nsmutex)
86 1.19 christos #else
87 1.19 christos #define NSLOCK()
88 1.19 christos #define NSUNLOCK()
89 1.19 christos #endif
90 1.19 christos
91 1.2 lukem
92 1.2 lukem /*
93 1.2 lukem * size of dynamic array chunk for _nsmap and _nsmap[x].srclist
94 1.2 lukem */
95 1.2 lukem #define NSELEMSPERCHUNK 8
96 1.2 lukem
97 1.2 lukem
98 1.21 thorpej static int
99 1.21 thorpej _nscmp(const void *a, const void *b)
100 1.2 lukem {
101 1.7 christos return (strcasecmp(((const ns_dbt *)a)->name,
102 1.7 christos ((const ns_dbt *)b)->name));
103 1.2 lukem }
104 1.2 lukem
105 1.2 lukem
106 1.15 lukem int
107 1.21 thorpej _nsdbtaddsrc(ns_dbt *dbt, const ns_src *src)
108 1.2 lukem {
109 1.12 lukem
110 1.12 lukem _DIAGASSERT(dbt != NULL);
111 1.12 lukem _DIAGASSERT(src != NULL);
112 1.12 lukem
113 1.2 lukem if ((dbt->srclistsize % NSELEMSPERCHUNK) == 0) {
114 1.15 lukem ns_src *new;
115 1.15 lukem
116 1.15 lukem new = (ns_src *)realloc(dbt->srclist,
117 1.2 lukem (dbt->srclistsize + NSELEMSPERCHUNK) * sizeof(ns_src));
118 1.15 lukem if (new == NULL)
119 1.15 lukem return (-1);
120 1.15 lukem dbt->srclist = new;
121 1.2 lukem }
122 1.7 christos memmove(&dbt->srclist[dbt->srclistsize++], src, sizeof(ns_src));
123 1.15 lukem return (0);
124 1.2 lukem }
125 1.2 lukem
126 1.2 lukem
127 1.2 lukem void
128 1.21 thorpej _nsdbtdump(const ns_dbt *dbt)
129 1.2 lukem {
130 1.2 lukem int i;
131 1.2 lukem
132 1.12 lukem _DIAGASSERT(dbt != NULL);
133 1.12 lukem
134 1.2 lukem printf("%s (%d source%s):", dbt->name, dbt->srclistsize,
135 1.2 lukem dbt->srclistsize == 1 ? "" : "s");
136 1.2 lukem for (i = 0; i < dbt->srclistsize; i++) {
137 1.2 lukem printf(" %s", dbt->srclist[i].name);
138 1.2 lukem if (!(dbt->srclist[i].flags &
139 1.2 lukem (NS_UNAVAIL|NS_NOTFOUND|NS_TRYAGAIN)) &&
140 1.2 lukem (dbt->srclist[i].flags & NS_SUCCESS))
141 1.2 lukem continue;
142 1.2 lukem printf(" [");
143 1.2 lukem if (!(dbt->srclist[i].flags & NS_SUCCESS))
144 1.2 lukem printf(" SUCCESS=continue");
145 1.2 lukem if (dbt->srclist[i].flags & NS_UNAVAIL)
146 1.2 lukem printf(" UNAVAIL=return");
147 1.2 lukem if (dbt->srclist[i].flags & NS_NOTFOUND)
148 1.2 lukem printf(" NOTFOUND=return");
149 1.2 lukem if (dbt->srclist[i].flags & NS_TRYAGAIN)
150 1.2 lukem printf(" TRYAGAIN=return");
151 1.2 lukem printf(" ]");
152 1.2 lukem }
153 1.2 lukem printf("\n");
154 1.2 lukem }
155 1.2 lukem
156 1.2 lukem
157 1.2 lukem const ns_dbt *
158 1.21 thorpej _nsdbtget(const char *name)
159 1.2 lukem {
160 1.2 lukem static time_t confmod;
161 1.2 lukem
162 1.2 lukem struct stat statbuf;
163 1.5 lukem ns_dbt dbt;
164 1.2 lukem
165 1.12 lukem _DIAGASSERT(name != NULL);
166 1.12 lukem
167 1.4 lukem dbt.name = name;
168 1.2 lukem
169 1.19 christos NSLOCK();
170 1.19 christos if (stat(_PATH_NS_CONF, &statbuf) == -1)
171 1.19 christos return (NULL);
172 1.2 lukem if (confmod) {
173 1.2 lukem if (confmod < statbuf.st_mtime) {
174 1.2 lukem int i, j;
175 1.2 lukem
176 1.2 lukem for (i = 0; i < _nsmapsize; i++) {
177 1.2 lukem for (j = 0; j < _nsmap[i].srclistsize; j++) {
178 1.7 christos if (_nsmap[i].srclist[j].name != NULL) {
179 1.7 christos /*LINTED const cast*/
180 1.8 lukem free((void *)
181 1.8 lukem _nsmap[i].srclist[j].name);
182 1.7 christos }
183 1.2 lukem }
184 1.2 lukem if (_nsmap[i].srclist)
185 1.2 lukem free(_nsmap[i].srclist);
186 1.7 christos if (_nsmap[i].name) {
187 1.7 christos /*LINTED const cast*/
188 1.7 christos free((void *)_nsmap[i].name);
189 1.7 christos }
190 1.2 lukem }
191 1.2 lukem if (_nsmap)
192 1.2 lukem free(_nsmap);
193 1.2 lukem _nsmap = NULL;
194 1.2 lukem _nsmapsize = 0;
195 1.2 lukem confmod = 0;
196 1.2 lukem }
197 1.2 lukem }
198 1.2 lukem if (!confmod) {
199 1.2 lukem _nsyyin = fopen(_PATH_NS_CONF, "r");
200 1.19 christos if (_nsyyin == NULL) {
201 1.19 christos NSUNLOCK();
202 1.5 lukem return (NULL);
203 1.19 christos }
204 1.2 lukem _nsyyparse();
205 1.2 lukem (void)fclose(_nsyyin);
206 1.7 christos qsort(_nsmap, (size_t)_nsmapsize, sizeof(ns_dbt), _nscmp);
207 1.2 lukem confmod = statbuf.st_mtime;
208 1.2 lukem }
209 1.19 christos NSUNLOCK();
210 1.8 lukem return (bsearch(&dbt, _nsmap, (size_t)_nsmapsize, sizeof(ns_dbt),
211 1.8 lukem _nscmp));
212 1.2 lukem }
213 1.2 lukem
214 1.2 lukem
215 1.15 lukem int
216 1.21 thorpej _nsdbtput(const ns_dbt *dbt)
217 1.2 lukem {
218 1.2 lukem int i;
219 1.2 lukem
220 1.12 lukem _DIAGASSERT(dbt != NULL);
221 1.12 lukem
222 1.2 lukem for (i = 0; i < _nsmapsize; i++) {
223 1.2 lukem if (_nscmp(dbt, &_nsmap[i]) == 0) {
224 1.2 lukem /* overwrite existing entry */
225 1.2 lukem if (_nsmap[i].srclist != NULL)
226 1.2 lukem free(_nsmap[i].srclist);
227 1.7 christos memmove(&_nsmap[i], dbt, sizeof(ns_dbt));
228 1.15 lukem return (0);
229 1.2 lukem }
230 1.2 lukem }
231 1.2 lukem
232 1.2 lukem if ((_nsmapsize % NSELEMSPERCHUNK) == 0) {
233 1.15 lukem ns_dbt *new;
234 1.15 lukem
235 1.15 lukem new = (ns_dbt *)realloc(_nsmap,
236 1.2 lukem (_nsmapsize + NSELEMSPERCHUNK) * sizeof(ns_dbt));
237 1.20 christos if (new == NULL)
238 1.15 lukem return (-1);
239 1.15 lukem _nsmap = new;
240 1.2 lukem }
241 1.7 christos memmove(&_nsmap[_nsmapsize++], dbt, sizeof(ns_dbt));
242 1.15 lukem return (0);
243 1.2 lukem }
244 1.2 lukem
245 1.2 lukem
246 1.2 lukem int
247 1.11 christos /*ARGSUSED*/
248 1.5 lukem nsdispatch(void *retval, const ns_dtab disp_tab[], const char *database,
249 1.5 lukem const char *method, const ns_src defaults[], ...)
250 1.2 lukem {
251 1.2 lukem va_list ap;
252 1.2 lukem int i, curdisp, result;
253 1.2 lukem const ns_dbt *dbt;
254 1.5 lukem const ns_src *srclist;
255 1.5 lukem int srclistsize;
256 1.12 lukem
257 1.12 lukem _DIAGASSERT(database != NULL);
258 1.12 lukem _DIAGASSERT(method != NULL);
259 1.12 lukem if (database == NULL || method == NULL)
260 1.12 lukem return (NS_UNAVAIL);
261 1.2 lukem
262 1.2 lukem dbt = _nsdbtget(database);
263 1.5 lukem if (dbt != NULL) {
264 1.5 lukem srclist = dbt->srclist;
265 1.5 lukem srclistsize = dbt->srclistsize;
266 1.5 lukem } else {
267 1.5 lukem srclist = defaults;
268 1.5 lukem srclistsize = 0;
269 1.5 lukem while (srclist[srclistsize].name != NULL)
270 1.5 lukem srclistsize++;
271 1.5 lukem }
272 1.2 lukem result = 0;
273 1.2 lukem
274 1.5 lukem for (i = 0; i < srclistsize; i++) {
275 1.2 lukem for (curdisp = 0; disp_tab[curdisp].src != NULL; curdisp++)
276 1.2 lukem if (strcasecmp(disp_tab[curdisp].src,
277 1.5 lukem srclist[i].name) == 0)
278 1.2 lukem break;
279 1.2 lukem result = 0;
280 1.2 lukem if (disp_tab[curdisp].callback) {
281 1.5 lukem va_start(ap, defaults);
282 1.2 lukem result = disp_tab[curdisp].callback(retval,
283 1.2 lukem disp_tab[curdisp].cb_data, ap);
284 1.2 lukem va_end(ap);
285 1.5 lukem if (result & srclist[i].flags) {
286 1.2 lukem break;
287 1.2 lukem }
288 1.2 lukem }
289 1.2 lukem }
290 1.2 lukem return (result ? result : NS_NOTFOUND);
291 1.2 lukem }
292