Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
/*	$NetBSD: nsdispatch.c,v 1.39 2022/04/19 20:32:15 rillig Exp $	*/

/*-
 * Copyright (c) 1997, 1998, 1999, 2004 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Luke Mewburn; and by Jason R. Thorpe.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*-
 * Copyright (c) 2003 Networks Associates Technology, Inc.
 * All rights reserved.
 *
 * Portions of this software were developed for the FreeBSD Project by
 * Jacques A. Vidrine, Safeport Network Services, and Network
 * Associates Laboratories, the Security Research Division of Network
 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
 * ("CBOSS"), as part of the DARPA CHATS research program.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
__RCSID("$NetBSD: nsdispatch.c,v 1.39 2022/04/19 20:32:15 rillig Exp $");
#endif /* LIBC_SCCS and not lint */

#include "namespace.h"

#include <sys/types.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/queue.h>

#include <assert.h>
#ifdef __ELF__
#include <dlfcn.h>
#endif /* __ELF__ */
#include <err.h>
#include <fcntl.h>
#define _NS_PRIVATE
#include <nsswitch.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "reentrant.h"

extern	FILE 	*_nsyyin;
extern	int	 _nsyyparse(void);


#ifdef __weak_alias
__weak_alias(nsdispatch,_nsdispatch)
#endif


/*
 * default sourcelist: `files'
 */
const ns_src __nsdefaultsrc[] = {
	{ NSSRC_FILES,	NS_SUCCESS },
	{ 0, 0 },
};

const ns_src __nsdefaultcompat[] = {
	{ NSSRC_COMPAT,	NS_SUCCESS },
	{ 0, 0 }
};

const ns_src __nsdefaultcompat_forceall[] = {
	{ NSSRC_COMPAT,	NS_SUCCESS | NS_FORCEALL },
	{ 0, 0 }
};

const ns_src __nsdefaultfiles[] = {
	{ NSSRC_FILES,	NS_SUCCESS },
	{ 0, 0 },
};

const ns_src __nsdefaultfiles_forceall[] = {
	{ NSSRC_FILES,	NS_SUCCESS | NS_FORCEALL },
	{ 0, 0 },
};

const ns_src __nsdefaultnis[] = {
	{ NSSRC_NIS,	NS_SUCCESS },
	{ 0, 0 }
};

const ns_src __nsdefaultnis_forceall[] = {
	{ NSSRC_NIS,	NS_SUCCESS | NS_FORCEALL },
	{ 0, 0 }
};


/* Database, source mappings. */
static	u_int			 _nsmapsize;
static	ns_dbt			*_nsmap;

/* Nsswitch modules. */
static	u_int			 _nsmodsize;
static	ns_mod			*_nsmod;

/* Placeholder for built-in modules' dlopen() handles. */
static	void			*_nsbuiltin = &_nsbuiltin;

#ifdef _REENTRANT
/*
 * Global nsswitch data structures are mostly read-only, but we update them
 * when we read or re-read nsswitch.conf.
 */
static 	rwlock_t		_nslock = RWLOCK_INITIALIZER;

/*
 * List of threads currently in nsdispatch().  We use this to detect
 * recursive calls and avoid reloading configuration in such cases,
 * which could cause deadlock.
 */
struct _ns_drec {
	LIST_ENTRY(_ns_drec)	list;
	thr_t			thr;
};
static LIST_HEAD(, _ns_drec) _ns_drec = LIST_HEAD_INITIALIZER(&_ns_drec);
static mutex_t _ns_drec_lock = MUTEX_INITIALIZER;
#endif /* _REENTRANT */


/*
 * Runtime determination of whether we are dynamically linked or not.
 */
#ifndef __ELF__
#define	is_dynamic()		(0)	/* don't bother - switch to ELF! */
#else
__weakref_visible int rtld_DYNAMIC __weak_reference(_DYNAMIC);
#define	is_dynamic()		(&rtld_DYNAMIC != NULL)
#endif


/*
 * size of dynamic array chunk for _nsmap and _nsmap[x].srclist (and other
 * growing arrays).
 */
#define NSELEMSPERCHUNK		8

/*
 * Dynamically growable arrays are used for lists of databases, sources,
 * and modules.  The following "vector" API is used to isolate the
 * common operations.
 */
typedef void	(*_nsvect_free_elem)(void *);

static void *
_nsvect_append(const void *elem, void *vec, u_int *count, size_t esize)
{
	void	*p;

	if ((*count % NSELEMSPERCHUNK) == 0) {
		p = realloc(vec, (*count + NSELEMSPERCHUNK) * esize);
		if (p == NULL)
			return (NULL);
		vec = p;
	}
	memmove((void *)(((uintptr_t)vec) + (*count * esize)), elem, esize);
	(*count)++;
	return (vec);
}

static void *
_nsvect_elem(u_int i, void *vec, u_int count, size_t esize)
{

	if (i < count)
		return ((void *)((uintptr_t)vec + (i * esize)));
	else
		return (NULL);
}

static void
_nsvect_free(void *vec, u_int *count, size_t esize, _nsvect_free_elem free_elem)
{
	void	*elem;
	u_int	 i;

	for (i = 0; i < *count; i++) {
		elem = _nsvect_elem(i, vec, *count, esize);
		if (elem != NULL)
			(*free_elem)(elem);
	}
	if (vec != NULL)
		free(vec);
	*count = 0;
}
#define	_NSVECT_FREE(v, c, s, f)					\
do {									\
	_nsvect_free((v), (c), (s), (f));				\
	(v) = NULL;							\
} while (0)

static int
_nsdbtcmp(const void *a, const void *b)
{

	return (strcasecmp(((const ns_dbt *)a)->name,
	    ((const ns_dbt *)b)->name));
}

static int
_nsmodcmp(const void *a, const void *b)
{

	return (strcasecmp(((const ns_mod *)a)->name,
	    ((const ns_mod *)b)->name));
}

static int
_nsmtabcmp(const void *a, const void *b)
{
	int	cmp;

	cmp = strcmp(((const ns_mtab *)a)->name,
	    ((const ns_mtab *)b)->name);
	if (cmp)
		return (cmp);

	return (strcasecmp(((const ns_mtab *)a)->database,
	    ((const ns_mtab *)b)->database));
}

static void
_nsmodfree(ns_mod *mod)
{

	free(__UNCONST(mod->name));
	if (mod->handle == NULL)
		return;
	if (mod->unregister != NULL)
		(*mod->unregister)(mod->mtab, mod->mtabsize);
#ifdef __ELF__
	if (mod->handle != _nsbuiltin)
		(void) dlclose(mod->handle);
#endif /* __ELF__ */
}

/*
 * Load a built-in or dyanamically linked module.  If the `reg_fn'
 * argument is non-NULL, assume a built-in module and use `reg_fn'
 * to register it.  Otherwise, search for a dynamic nsswitch module.
 */
static int
_nsloadmod(const char *source, nss_module_register_fn reg_fn)
{
#ifdef __ELF__
	char	buf[PATH_MAX];
#endif
	ns_mod	mod, *new;

	memset(&mod, 0, sizeof(mod));
	mod.name = strdup(source);
	if (mod.name == NULL)
		return (-1);

	if (reg_fn != NULL) {
		/*
		 * The placeholder is required, as a NULL handle
		 * represents an invalid module.
		 */
		mod.handle = _nsbuiltin;
	} else if (!is_dynamic()) {
		goto out;
	} else {
#ifdef __ELF__
		if (snprintf(buf, sizeof(buf), "nss_%s.so.%d", mod.name,
		    NSS_MODULE_INTERFACE_VERSION) >= (int)sizeof(buf))
			goto out;
		mod.handle = dlopen(buf, RTLD_LOCAL | RTLD_LAZY);
		if (mod.handle == NULL) {
#ifdef _NSS_DEBUG
			/*
			 * This gets pretty annoying, since the built-in
			 * sources are not yet modules.
			 */
			/* XXX log some error? */
#endif
			goto out;
		}
		reg_fn = (nss_module_register_fn) dlsym(mod.handle,
		    "nss_module_register");
		if (reg_fn == NULL) {
			(void) dlclose(mod.handle);
			mod.handle = NULL;
			/* XXX log some error? */
			goto out;
		}
#else /* ! __ELF__ */
		mod.handle = NULL;
#endif /* __ELF__ */
	}
	mod.mtab = (*reg_fn)(mod.name, &mod.mtabsize, &mod.unregister);
	if (mod.mtab == NULL || mod.mtabsize == 0) {
#ifdef __ELF__
		if (mod.handle != _nsbuiltin)
			(void) dlclose(mod.handle);
#endif /* __ELF__ */
		mod.handle = NULL;
		/* XXX log some error? */
		goto out;
	}
	if (mod.mtabsize > 1)
		qsort(mod.mtab, mod.mtabsize, sizeof(mod.mtab[0]),
		    _nsmtabcmp);
 out:
	new = _nsvect_append(&mod, _nsmod, &_nsmodsize, sizeof(*_nsmod));
	if (new == NULL) {
		_nsmodfree(&mod);
		return (-1);
	}
	_nsmod = new;
	/* _nsmodsize already incremented */

	qsort(_nsmod, _nsmodsize, sizeof(*_nsmod), _nsmodcmp);
	return (0);
}

static void
_nsloadbuiltin(void)
{

	/* Do nothing, for now. */
}

int
_nsdbtaddsrc(ns_dbt *dbt, const ns_src *src)
{
	void		*new;
	const ns_mod	*mod;
	ns_mod		 modkey;

	_DIAGASSERT(dbt != NULL);
	_DIAGASSERT(src != NULL);

	new = _nsvect_append(src, dbt->srclist, &dbt->srclistsize,
	    sizeof(*src));
	if (new == NULL)
		return (-1);
	dbt->srclist = new;
	/* dbt->srclistsize already incremented */

	modkey.name = src->name;
	mod = bsearch(&modkey, _nsmod, _nsmodsize, sizeof(*_nsmod), _nsmodcmp);
	if (mod == NULL)
		return (_nsloadmod(src->name, NULL));

	return (0);
}

void
_nsdbtdump(const ns_dbt *dbt)
{
	unsigned int	i;

	_DIAGASSERT(dbt != NULL);

	printf("%s (%d source%s):", dbt->name, dbt->srclistsize,
	    dbt->srclistsize == 1 ? "" : "s");
	for (i = 0; i < dbt->srclistsize; i++) {
		printf(" %s", dbt->srclist[i].name);
		if (!(dbt->srclist[i].flags &
		    (NS_UNAVAIL|NS_NOTFOUND|NS_TRYAGAIN)) &&
		    (dbt->srclist[i].flags & NS_SUCCESS))
			continue;
		printf(" [");
		if (!(dbt->srclist[i].flags & NS_SUCCESS))
			printf(" SUCCESS=continue");
		if (dbt->srclist[i].flags & NS_UNAVAIL)
			printf(" UNAVAIL=return");
		if (dbt->srclist[i].flags & NS_NOTFOUND)
			printf(" NOTFOUND=return");
		if (dbt->srclist[i].flags & NS_TRYAGAIN)
			printf(" TRYAGAIN=return");
		printf(" ]");
	}
	printf("\n");
}

static void
_nssrclist_free(ns_src **src, u_int srclistsize)
{
	u_int	i;

	for (i = 0; i < srclistsize; i++) {
		if ((*src)[i].name != NULL)
			free(__UNCONST((*src)[i].name));
	}
	free(*src);
	*src = NULL;
}

static void
_nsdbtfree(ns_dbt *dbt)
{

	_nssrclist_free(&dbt->srclist, dbt->srclistsize);
	if (dbt->name != NULL)
		free(__UNCONST(dbt->name));
}

int
_nsdbtput(const ns_dbt *dbt)
{
	ns_dbt	*p;
	void	*new;
	u_int	i;

	_DIAGASSERT(dbt != NULL);

	for (i = 0; i < _nsmapsize; i++) {
		p = _nsvect_elem(i, _nsmap, _nsmapsize, sizeof(*_nsmap));
		if (strcasecmp(dbt->name, p->name) == 0) {
					/* overwrite existing entry */
			if (p->srclist != NULL)
				_nssrclist_free(&p->srclist, p->srclistsize);
			memmove(p, dbt, sizeof(*dbt));
			return (0);
		}
	}
	new = _nsvect_append(dbt, _nsmap, &_nsmapsize, sizeof(*_nsmap));
	if (new == NULL)
		return (-1);
	_nsmap = new;
	/* _nsmapsize already incremented */

	return (0);
}

/*
 * This function is called each time nsdispatch() is called.  If this
 * is the first call, or if the configuration has changed, (re-)prepare
 * the global data used by NSS.
 */
static int
_nsconfigure(void)
{
#ifdef _REENTRANT
	static mutex_t	_nsconflock = MUTEX_INITIALIZER;
#endif
	static time_t	_nsconfmod;
	struct stat	statbuf;

	mutex_lock(&_nsconflock);

	if (stat(_PATH_NS_CONF, &statbuf) == -1) {
		/*
		 * No nsswitch.conf; just use whatever configuration we
		 * currently have, or fall back on the defaults specified
		 * by the caller.
		 */
		mutex_unlock(&_nsconflock);
		return (0);
	}

	if (statbuf.st_mtime <= _nsconfmod) {
		/* Internal state is up-to-date with nsswitch.conf. */
		mutex_unlock(&_nsconflock);
		return (0);
	}

	/*
	 * Ok, we've decided we need to update the nsswitch configuration
	 * structures.  Acquire a write-lock on _nslock while continuing
	 * to hold _nsconflock.  Acquiring a write-lock blocks while
	 * waiting for other threads already holding a read-lock to clear.
	 * We hold _nsconflock for the duration, and update the time stamp
	 * at the end of the update operation, at which time we release
	 * both locks.
	 */
	rwlock_wrlock(&_nslock);

	_nsyyin = fopen(_PATH_NS_CONF, "re");
	if (_nsyyin == NULL) {
		/*
		 * Unable to open nsswitch.conf; behave as though the
		 * stat() above failed.  Even though we have already
		 * updated _nsconfmod, if the file reappears, the
		 * mtime will change.
		 */
		goto out;
	}

	_NSVECT_FREE(_nsmap, &_nsmapsize, sizeof(*_nsmap),
	    (_nsvect_free_elem) _nsdbtfree);
	_NSVECT_FREE(_nsmod, &_nsmodsize, sizeof(*_nsmod),
	    (_nsvect_free_elem) _nsmodfree);

	_nsloadbuiltin();

	_nsyyparse();
	(void) fclose(_nsyyin);
	if (_nsmapsize != 0)
		qsort(_nsmap, _nsmapsize, sizeof(*_nsmap), _nsdbtcmp);

	_nsconfmod = statbuf.st_mtime;

 out:
	rwlock_unlock(&_nslock);
	mutex_unlock(&_nsconflock);
	return (0);
}

static nss_method
_nsmethod(const char *source, const char *database, const char *method,
    const ns_dtab disp_tab[], void **cb_data)
{
	int	curdisp;
	ns_mod	*mod, modkey;
	ns_mtab	*mtab, mtabkey;

	if (disp_tab != NULL) {
		for (curdisp = 0; disp_tab[curdisp].src != NULL; curdisp++) {
			if (strcasecmp(source, disp_tab[curdisp].src) == 0) {
				*cb_data = disp_tab[curdisp].cb_data;
				return (disp_tab[curdisp].callback);
			}
		}
	}

	modkey.name = source;
	mod = bsearch(&modkey, _nsmod, _nsmodsize, sizeof(*_nsmod),
	    _nsmodcmp);
	if (mod != NULL && mod->handle != NULL) {
		mtabkey.database = database;
		mtabkey.name = method;
		mtab = bsearch(&mtabkey, mod->mtab, mod->mtabsize,
		    sizeof(mod->mtab[0]), _nsmtabcmp);
		if (mtab != NULL) {
			*cb_data = mtab->mdata;
			return (mtab->method);
		}
	}

	*cb_data = NULL;
	return (NULL);
}

int
/*ARGSUSED*/
nsdispatch(void *retval, const ns_dtab disp_tab[], const char *database,
	    const char *method, const ns_src defaults[], ...)
{
	static int	 _nsdispatching;
#ifdef _REENTRANT
	struct _ns_drec	 drec, *ldrec;
#endif
	va_list		 ap;
	int		 i, result;
	ns_dbt		 key;
	const ns_dbt	*dbt;
	const ns_src	*srclist;
	int		 srclistsize;
	nss_method	 cb;
	void		*cb_data;

	/* retval may be NULL */
	/* disp_tab may be NULL */
	_DIAGASSERT(database != NULL);
	_DIAGASSERT(method != NULL);
	_DIAGASSERT(defaults != NULL);
	if (database == NULL || method == NULL || defaults == NULL)
		return (NS_UNAVAIL);

	/*
	 * In both the threaded and non-threaded cases, avoid reloading
	 * the configuration if the current thread is already running
	 * nsdispatch() (i.e. recursive call).
	 *
	 * In the non-threaded case, this avoids changing the data structures
	 * while we're using them.
	 *
	 * In the threaded case, this avoids trying to take a write lock
	 * while the current thread holds a read lock (which would result
	 * in deadlock).
	 */
#ifdef _REENTRANT
	if (__isthreaded) {
		drec.thr = thr_self();
		mutex_lock(&_ns_drec_lock);
		LIST_FOREACH(ldrec, &_ns_drec, list) {
			if (ldrec->thr == drec.thr)
				break;
		}
		LIST_INSERT_HEAD(&_ns_drec, &drec, list);
		mutex_unlock(&_ns_drec_lock);
		if (ldrec == NULL && _nsconfigure()) {
			mutex_lock(&_ns_drec_lock);
			LIST_REMOVE(&drec, list);
			mutex_unlock(&_ns_drec_lock);
			return (NS_UNAVAIL);
		}
	} else
#endif /* _REENTRANT */
	if (_nsdispatching++ == 0 && _nsconfigure()) {
		_nsdispatching--;
		return (NS_UNAVAIL);
	}

	rwlock_rdlock(&_nslock);

	key.name = database;
	dbt = bsearch(&key, _nsmap, _nsmapsize, sizeof(*_nsmap), _nsdbtcmp);
	if (dbt != NULL) {
		srclist = dbt->srclist;
		srclistsize = dbt->srclistsize;
	} else {
		srclist = defaults;
		srclistsize = 0;
		while (srclist[srclistsize].name != NULL)
			srclistsize++;
	}
	result = 0;

	for (i = 0; i < srclistsize; i++) {
		cb = _nsmethod(srclist[i].name, database, method,
		    disp_tab, &cb_data);
		result = 0;
		if (cb != NULL) {
			va_start(ap, defaults);
			result = (*cb)(retval, cb_data, ap);
			va_end(ap);
			if (defaults[0].flags & NS_FORCEALL)
				continue;
			if (result & srclist[i].flags)
				break;
		}
	}
	result &= NS_STATUSMASK;	/* clear private flags in result */

	rwlock_unlock(&_nslock);

#ifdef _REENTRANT
	if (__isthreaded) {
		mutex_lock(&_ns_drec_lock);
		LIST_REMOVE(&drec, list);
		mutex_unlock(&_ns_drec_lock);
	} else
#endif /* _REENTRANT */
		_nsdispatching--;

	return (result ? result : NS_NOTFOUND);
}