Home | History | Annotate | Line # | Download | only in net
nsdispatch.c revision 1.20
      1  1.20  christos /*	$NetBSD: nsdispatch.c,v 1.20 2004/05/24 16:16:26 christos 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.20  christos __RCSID("$NetBSD: nsdispatch.c,v 1.20 2004/05/24 16:16:26 christos 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.17  christos extern	int	 _nsyyparse __P((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.2     lukem int	_nscmp __P((const void *, const void *));
     99   1.2     lukem 
    100   1.2     lukem 
    101   1.2     lukem int
    102   1.2     lukem _nscmp(a, b)
    103   1.2     lukem 	const void *a;
    104   1.2     lukem 	const void *b;
    105   1.2     lukem {
    106   1.7  christos 	return (strcasecmp(((const ns_dbt *)a)->name,
    107   1.7  christos 	    ((const ns_dbt *)b)->name));
    108   1.2     lukem }
    109   1.2     lukem 
    110   1.2     lukem 
    111  1.15     lukem int
    112   1.2     lukem _nsdbtaddsrc(dbt, src)
    113   1.2     lukem 	ns_dbt		*dbt;
    114   1.2     lukem 	const ns_src	*src;
    115   1.2     lukem {
    116  1.12     lukem 
    117  1.12     lukem 	_DIAGASSERT(dbt != NULL);
    118  1.12     lukem 	_DIAGASSERT(src != NULL);
    119  1.12     lukem 
    120   1.2     lukem 	if ((dbt->srclistsize % NSELEMSPERCHUNK) == 0) {
    121  1.15     lukem 		ns_src *new;
    122  1.15     lukem 
    123  1.15     lukem 		new = (ns_src *)realloc(dbt->srclist,
    124   1.2     lukem 		    (dbt->srclistsize + NSELEMSPERCHUNK) * sizeof(ns_src));
    125  1.15     lukem 		if (new == NULL)
    126  1.15     lukem 			return (-1);
    127  1.15     lukem 		dbt->srclist = new;
    128   1.2     lukem 	}
    129   1.7  christos 	memmove(&dbt->srclist[dbt->srclistsize++], src, sizeof(ns_src));
    130  1.15     lukem 	return (0);
    131   1.2     lukem }
    132   1.2     lukem 
    133   1.2     lukem 
    134   1.2     lukem void
    135   1.2     lukem _nsdbtdump(dbt)
    136   1.2     lukem 	const ns_dbt *dbt;
    137   1.2     lukem {
    138   1.2     lukem 	int i;
    139   1.2     lukem 
    140  1.12     lukem 	_DIAGASSERT(dbt != NULL);
    141  1.12     lukem 
    142   1.2     lukem 	printf("%s (%d source%s):", dbt->name, dbt->srclistsize,
    143   1.2     lukem 	    dbt->srclistsize == 1 ? "" : "s");
    144   1.2     lukem 	for (i = 0; i < dbt->srclistsize; i++) {
    145   1.2     lukem 		printf(" %s", dbt->srclist[i].name);
    146   1.2     lukem 		if (!(dbt->srclist[i].flags &
    147   1.2     lukem 		    (NS_UNAVAIL|NS_NOTFOUND|NS_TRYAGAIN)) &&
    148   1.2     lukem 		    (dbt->srclist[i].flags & NS_SUCCESS))
    149   1.2     lukem 			continue;
    150   1.2     lukem 		printf(" [");
    151   1.2     lukem 		if (!(dbt->srclist[i].flags & NS_SUCCESS))
    152   1.2     lukem 			printf(" SUCCESS=continue");
    153   1.2     lukem 		if (dbt->srclist[i].flags & NS_UNAVAIL)
    154   1.2     lukem 			printf(" UNAVAIL=return");
    155   1.2     lukem 		if (dbt->srclist[i].flags & NS_NOTFOUND)
    156   1.2     lukem 			printf(" NOTFOUND=return");
    157   1.2     lukem 		if (dbt->srclist[i].flags & NS_TRYAGAIN)
    158   1.2     lukem 			printf(" TRYAGAIN=return");
    159   1.2     lukem 		printf(" ]");
    160   1.2     lukem 	}
    161   1.2     lukem 	printf("\n");
    162   1.2     lukem }
    163   1.2     lukem 
    164   1.2     lukem 
    165   1.2     lukem const ns_dbt *
    166   1.2     lukem _nsdbtget(name)
    167   1.2     lukem 	const char	*name;
    168   1.2     lukem {
    169   1.2     lukem 	static	time_t	 confmod;
    170   1.2     lukem 
    171   1.2     lukem 	struct stat	 statbuf;
    172   1.5     lukem 	ns_dbt		 dbt;
    173   1.2     lukem 
    174  1.12     lukem 	_DIAGASSERT(name != NULL);
    175  1.12     lukem 
    176   1.4     lukem 	dbt.name = name;
    177   1.2     lukem 
    178  1.19  christos 	NSLOCK();
    179  1.19  christos 	if (stat(_PATH_NS_CONF, &statbuf) == -1)
    180  1.19  christos 		return (NULL);
    181   1.2     lukem 	if (confmod) {
    182   1.2     lukem 		if (confmod < statbuf.st_mtime) {
    183   1.2     lukem 			int i, j;
    184   1.2     lukem 
    185   1.2     lukem 			for (i = 0; i < _nsmapsize; i++) {
    186   1.2     lukem 				for (j = 0; j < _nsmap[i].srclistsize; j++) {
    187   1.7  christos 					if (_nsmap[i].srclist[j].name != NULL) {
    188   1.7  christos 						/*LINTED const cast*/
    189   1.8     lukem 						free((void *)
    190   1.8     lukem 						    _nsmap[i].srclist[j].name);
    191   1.7  christos 					}
    192   1.2     lukem 				}
    193   1.2     lukem 				if (_nsmap[i].srclist)
    194   1.2     lukem 					free(_nsmap[i].srclist);
    195   1.7  christos 				if (_nsmap[i].name) {
    196   1.7  christos 					/*LINTED const cast*/
    197   1.7  christos 					free((void *)_nsmap[i].name);
    198   1.7  christos 				}
    199   1.2     lukem 			}
    200   1.2     lukem 			if (_nsmap)
    201   1.2     lukem 				free(_nsmap);
    202   1.2     lukem 			_nsmap = NULL;
    203   1.2     lukem 			_nsmapsize = 0;
    204   1.2     lukem 			confmod = 0;
    205   1.2     lukem 		}
    206   1.2     lukem 	}
    207   1.2     lukem 	if (!confmod) {
    208   1.2     lukem 		_nsyyin = fopen(_PATH_NS_CONF, "r");
    209  1.19  christos 		if (_nsyyin == NULL) {
    210  1.19  christos 			NSUNLOCK();
    211   1.5     lukem 			return (NULL);
    212  1.19  christos 		}
    213   1.2     lukem 		_nsyyparse();
    214   1.2     lukem 		(void)fclose(_nsyyin);
    215   1.7  christos 		qsort(_nsmap, (size_t)_nsmapsize, sizeof(ns_dbt), _nscmp);
    216   1.2     lukem 		confmod = statbuf.st_mtime;
    217   1.2     lukem 	}
    218  1.19  christos 	NSUNLOCK();
    219   1.8     lukem 	return (bsearch(&dbt, _nsmap, (size_t)_nsmapsize, sizeof(ns_dbt),
    220   1.8     lukem 	    _nscmp));
    221   1.2     lukem }
    222   1.2     lukem 
    223   1.2     lukem 
    224  1.15     lukem int
    225   1.2     lukem _nsdbtput(dbt)
    226   1.2     lukem 	const ns_dbt *dbt;
    227   1.2     lukem {
    228   1.2     lukem 	int	i;
    229   1.2     lukem 
    230  1.12     lukem 	_DIAGASSERT(dbt != NULL);
    231  1.12     lukem 
    232   1.2     lukem 	for (i = 0; i < _nsmapsize; i++) {
    233   1.2     lukem 		if (_nscmp(dbt, &_nsmap[i]) == 0) {
    234   1.2     lukem 					/* overwrite existing entry */
    235   1.2     lukem 			if (_nsmap[i].srclist != NULL)
    236   1.2     lukem 				free(_nsmap[i].srclist);
    237   1.7  christos 			memmove(&_nsmap[i], dbt, sizeof(ns_dbt));
    238  1.15     lukem 			return (0);
    239   1.2     lukem 		}
    240   1.2     lukem 	}
    241   1.2     lukem 
    242   1.2     lukem 	if ((_nsmapsize % NSELEMSPERCHUNK) == 0) {
    243  1.15     lukem 		ns_dbt *new;
    244  1.15     lukem 
    245  1.15     lukem 		new = (ns_dbt *)realloc(_nsmap,
    246   1.2     lukem 		    (_nsmapsize + NSELEMSPERCHUNK) * sizeof(ns_dbt));
    247  1.20  christos 		if (new == NULL)
    248  1.15     lukem 			return (-1);
    249  1.15     lukem 		_nsmap = new;
    250   1.2     lukem 	}
    251   1.7  christos 	memmove(&_nsmap[_nsmapsize++], dbt, sizeof(ns_dbt));
    252  1.15     lukem 	return (0);
    253   1.2     lukem }
    254   1.2     lukem 
    255   1.2     lukem 
    256   1.2     lukem int
    257  1.11  christos /*ARGSUSED*/
    258   1.5     lukem nsdispatch(void *retval, const ns_dtab disp_tab[], const char *database,
    259   1.5     lukem 	    const char *method, const ns_src defaults[], ...)
    260   1.2     lukem {
    261   1.2     lukem 	va_list		 ap;
    262   1.2     lukem 	int		 i, curdisp, result;
    263   1.2     lukem 	const ns_dbt	*dbt;
    264   1.5     lukem 	const ns_src	*srclist;
    265   1.5     lukem 	int		 srclistsize;
    266  1.12     lukem 
    267  1.12     lukem 	_DIAGASSERT(database != NULL);
    268  1.12     lukem 	_DIAGASSERT(method != NULL);
    269  1.12     lukem 	if (database == NULL || method == NULL)
    270  1.12     lukem 		return (NS_UNAVAIL);
    271   1.2     lukem 
    272   1.2     lukem 	dbt = _nsdbtget(database);
    273   1.5     lukem 	if (dbt != NULL) {
    274   1.5     lukem 		srclist = dbt->srclist;
    275   1.5     lukem 		srclistsize = dbt->srclistsize;
    276   1.5     lukem 	} else {
    277   1.5     lukem 		srclist = defaults;
    278   1.5     lukem 		srclistsize = 0;
    279   1.5     lukem 		while (srclist[srclistsize].name != NULL)
    280   1.5     lukem 			srclistsize++;
    281   1.5     lukem 	}
    282   1.2     lukem 	result = 0;
    283   1.2     lukem 
    284   1.5     lukem 	for (i = 0; i < srclistsize; i++) {
    285   1.2     lukem 		for (curdisp = 0; disp_tab[curdisp].src != NULL; curdisp++)
    286   1.2     lukem 			if (strcasecmp(disp_tab[curdisp].src,
    287   1.5     lukem 			    srclist[i].name) == 0)
    288   1.2     lukem 				break;
    289   1.2     lukem 		result = 0;
    290   1.2     lukem 		if (disp_tab[curdisp].callback) {
    291   1.5     lukem 			va_start(ap, defaults);
    292   1.2     lukem 			result = disp_tab[curdisp].callback(retval,
    293   1.2     lukem 			    disp_tab[curdisp].cb_data, ap);
    294   1.2     lukem 			va_end(ap);
    295   1.5     lukem 			if (result & srclist[i].flags) {
    296   1.2     lukem 				break;
    297   1.2     lukem 			}
    298   1.2     lukem 		}
    299   1.2     lukem 	}
    300   1.2     lukem 	return (result ? result : NS_NOTFOUND);
    301   1.2     lukem }
    302