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