Home | History | Annotate | Line # | Download | only in net
nsdispatch.c revision 1.14.4.1
      1  1.14.4.1  wrstuden /*	$NetBSD: nsdispatch.c,v 1.14.4.1 1999/12/27 18:29:42 wrstuden 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.14.4.1  wrstuden __RCSID("$NetBSD: nsdispatch.c,v 1.14.4.1 1999/12/27 18:29:42 wrstuden 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.2     lukem #include <stdio.h>
     56       1.2     lukem #include <stdlib.h>
     57       1.2     lukem #include <string.h>
     58       1.2     lukem #include <unistd.h>
     59      1.10     lukem 
     60      1.10     lukem #ifdef __STDC__
     61      1.10     lukem #include <stdarg.h>
     62      1.10     lukem #else
     63      1.10     lukem #include <varargs.h>
     64      1.10     lukem #endif
     65       1.6     lukem 
     66       1.6     lukem #ifdef __weak_alias
     67       1.6     lukem __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.2     lukem 
     83       1.2     lukem /*
     84       1.2     lukem  * size of dynamic array chunk for _nsmap and _nsmap[x].srclist
     85       1.2     lukem  */
     86       1.2     lukem #define NSELEMSPERCHUNK		8
     87       1.2     lukem 
     88       1.2     lukem 
     89       1.2     lukem int	_nscmp __P((const void *, const void *));
     90       1.2     lukem 
     91       1.2     lukem 
     92       1.2     lukem int
     93       1.2     lukem _nscmp(a, b)
     94       1.2     lukem 	const void *a;
     95       1.2     lukem 	const void *b;
     96       1.2     lukem {
     97       1.7  christos 	return (strcasecmp(((const ns_dbt *)a)->name,
     98       1.7  christos 	    ((const ns_dbt *)b)->name));
     99       1.2     lukem }
    100       1.2     lukem 
    101       1.2     lukem 
    102  1.14.4.1  wrstuden int
    103       1.2     lukem _nsdbtaddsrc(dbt, src)
    104       1.2     lukem 	ns_dbt		*dbt;
    105       1.2     lukem 	const ns_src	*src;
    106       1.2     lukem {
    107      1.12     lukem 
    108      1.12     lukem 	_DIAGASSERT(dbt != NULL);
    109      1.12     lukem 	_DIAGASSERT(src != NULL);
    110      1.12     lukem 
    111       1.2     lukem 	if ((dbt->srclistsize % NSELEMSPERCHUNK) == 0) {
    112  1.14.4.1  wrstuden 		ns_src *new;
    113  1.14.4.1  wrstuden 
    114  1.14.4.1  wrstuden 		new = (ns_src *)realloc(dbt->srclist,
    115       1.2     lukem 		    (dbt->srclistsize + NSELEMSPERCHUNK) * sizeof(ns_src));
    116  1.14.4.1  wrstuden 		if (new == NULL)
    117  1.14.4.1  wrstuden 			return (-1);
    118  1.14.4.1  wrstuden 		dbt->srclist = new;
    119       1.2     lukem 	}
    120       1.7  christos 	memmove(&dbt->srclist[dbt->srclistsize++], src, sizeof(ns_src));
    121  1.14.4.1  wrstuden 	return (0);
    122       1.2     lukem }
    123       1.2     lukem 
    124       1.2     lukem 
    125       1.2     lukem void
    126       1.2     lukem _nsdbtdump(dbt)
    127       1.2     lukem 	const ns_dbt *dbt;
    128       1.2     lukem {
    129       1.2     lukem 	int i;
    130       1.2     lukem 
    131      1.12     lukem 	_DIAGASSERT(dbt != NULL);
    132      1.12     lukem 
    133       1.2     lukem 	printf("%s (%d source%s):", dbt->name, dbt->srclistsize,
    134       1.2     lukem 	    dbt->srclistsize == 1 ? "" : "s");
    135       1.2     lukem 	for (i = 0; i < dbt->srclistsize; i++) {
    136       1.2     lukem 		printf(" %s", dbt->srclist[i].name);
    137       1.2     lukem 		if (!(dbt->srclist[i].flags &
    138       1.2     lukem 		    (NS_UNAVAIL|NS_NOTFOUND|NS_TRYAGAIN)) &&
    139       1.2     lukem 		    (dbt->srclist[i].flags & NS_SUCCESS))
    140       1.2     lukem 			continue;
    141       1.2     lukem 		printf(" [");
    142       1.2     lukem 		if (!(dbt->srclist[i].flags & NS_SUCCESS))
    143       1.2     lukem 			printf(" SUCCESS=continue");
    144       1.2     lukem 		if (dbt->srclist[i].flags & NS_UNAVAIL)
    145       1.2     lukem 			printf(" UNAVAIL=return");
    146       1.2     lukem 		if (dbt->srclist[i].flags & NS_NOTFOUND)
    147       1.2     lukem 			printf(" NOTFOUND=return");
    148       1.2     lukem 		if (dbt->srclist[i].flags & NS_TRYAGAIN)
    149       1.2     lukem 			printf(" TRYAGAIN=return");
    150       1.2     lukem 		printf(" ]");
    151       1.2     lukem 	}
    152       1.2     lukem 	printf("\n");
    153       1.2     lukem }
    154       1.2     lukem 
    155       1.2     lukem 
    156       1.2     lukem const ns_dbt *
    157       1.2     lukem _nsdbtget(name)
    158       1.2     lukem 	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.2     lukem 	extern	FILE 	*_nsyyin;
    166       1.2     lukem 	extern	int	 _nsyyparse __P((void));
    167       1.2     lukem 
    168      1.12     lukem 	_DIAGASSERT(name != NULL);
    169      1.12     lukem 
    170       1.4     lukem 	dbt.name = name;
    171       1.2     lukem 
    172       1.2     lukem 	if (confmod) {
    173       1.2     lukem 		if (stat(_PATH_NS_CONF, &statbuf) == -1)
    174       1.5     lukem 			return (NULL);
    175       1.2     lukem 		if (confmod < statbuf.st_mtime) {
    176       1.2     lukem 			int i, j;
    177       1.2     lukem 
    178       1.2     lukem 			for (i = 0; i < _nsmapsize; i++) {
    179       1.2     lukem 				for (j = 0; j < _nsmap[i].srclistsize; j++) {
    180       1.7  christos 					if (_nsmap[i].srclist[j].name != NULL) {
    181       1.7  christos 						/*LINTED const cast*/
    182       1.8     lukem 						free((void *)
    183       1.8     lukem 						    _nsmap[i].srclist[j].name);
    184       1.7  christos 					}
    185       1.2     lukem 				}
    186       1.2     lukem 				if (_nsmap[i].srclist)
    187       1.2     lukem 					free(_nsmap[i].srclist);
    188       1.7  christos 				if (_nsmap[i].name) {
    189       1.7  christos 					/*LINTED const cast*/
    190       1.7  christos 					free((void *)_nsmap[i].name);
    191       1.7  christos 				}
    192       1.2     lukem 			}
    193       1.2     lukem 			if (_nsmap)
    194       1.2     lukem 				free(_nsmap);
    195       1.2     lukem 			_nsmap = NULL;
    196       1.2     lukem 			_nsmapsize = 0;
    197       1.2     lukem 			confmod = 0;
    198       1.2     lukem 		}
    199       1.2     lukem 	}
    200       1.2     lukem 	if (!confmod) {
    201       1.2     lukem 		if (stat(_PATH_NS_CONF, &statbuf) == -1)
    202       1.5     lukem 			return (NULL);
    203       1.2     lukem 		_nsyyin = fopen(_PATH_NS_CONF, "r");
    204       1.2     lukem 		if (_nsyyin == NULL)
    205       1.5     lukem 			return (NULL);
    206       1.2     lukem 		_nsyyparse();
    207       1.2     lukem 		(void)fclose(_nsyyin);
    208       1.7  christos 		qsort(_nsmap, (size_t)_nsmapsize, sizeof(ns_dbt), _nscmp);
    209       1.2     lukem 		confmod = statbuf.st_mtime;
    210       1.2     lukem 	}
    211       1.8     lukem 	return (bsearch(&dbt, _nsmap, (size_t)_nsmapsize, sizeof(ns_dbt),
    212       1.8     lukem 	    _nscmp));
    213       1.2     lukem }
    214       1.2     lukem 
    215       1.2     lukem 
    216  1.14.4.1  wrstuden int
    217       1.2     lukem _nsdbtput(dbt)
    218       1.2     lukem 	const ns_dbt *dbt;
    219       1.2     lukem {
    220       1.2     lukem 	int	i;
    221       1.2     lukem 
    222      1.12     lukem 	_DIAGASSERT(dbt != NULL);
    223      1.12     lukem 
    224       1.2     lukem 	for (i = 0; i < _nsmapsize; i++) {
    225       1.2     lukem 		if (_nscmp(dbt, &_nsmap[i]) == 0) {
    226       1.2     lukem 					/* overwrite existing entry */
    227       1.2     lukem 			if (_nsmap[i].srclist != NULL)
    228       1.2     lukem 				free(_nsmap[i].srclist);
    229       1.7  christos 			memmove(&_nsmap[i], dbt, sizeof(ns_dbt));
    230  1.14.4.1  wrstuden 			return (0);
    231       1.2     lukem 		}
    232       1.2     lukem 	}
    233       1.2     lukem 
    234       1.2     lukem 	if ((_nsmapsize % NSELEMSPERCHUNK) == 0) {
    235  1.14.4.1  wrstuden 		ns_dbt *new;
    236  1.14.4.1  wrstuden 
    237  1.14.4.1  wrstuden 		new = (ns_dbt *)realloc(_nsmap,
    238       1.2     lukem 		    (_nsmapsize + NSELEMSPERCHUNK) * sizeof(ns_dbt));
    239  1.14.4.1  wrstuden 		if (new == NULL)
    240  1.14.4.1  wrstuden 			return (-1);
    241  1.14.4.1  wrstuden 		_nsmap = new;
    242       1.2     lukem 	}
    243       1.7  christos 	memmove(&_nsmap[_nsmapsize++], dbt, sizeof(ns_dbt));
    244  1.14.4.1  wrstuden 	return (0);
    245       1.2     lukem }
    246       1.2     lukem 
    247       1.2     lukem 
    248       1.2     lukem int
    249      1.11  christos /*ARGSUSED*/
    250       1.2     lukem #if __STDC__
    251       1.5     lukem nsdispatch(void *retval, const ns_dtab disp_tab[], const char *database,
    252       1.5     lukem 	    const char *method, const ns_src defaults[], ...)
    253       1.2     lukem #else
    254       1.5     lukem nsdispatch(retval, disp_tab, database, method, defaults, va_alist)
    255       1.2     lukem 	void		*retval;
    256       1.2     lukem 	const ns_dtab	 disp_tab[];
    257       1.2     lukem 	const char	*database;
    258       1.5     lukem 	const char	*method;
    259       1.5     lukem 	const ns_src	 defaults[];
    260       1.2     lukem 	va_dcl
    261       1.2     lukem #endif
    262       1.2     lukem {
    263       1.2     lukem 	va_list		 ap;
    264       1.2     lukem 	int		 i, curdisp, result;
    265       1.2     lukem 	const ns_dbt	*dbt;
    266       1.5     lukem 	const ns_src	*srclist;
    267       1.5     lukem 	int		 srclistsize;
    268      1.12     lukem 
    269      1.12     lukem 	_DIAGASSERT(database != NULL);
    270      1.12     lukem 	_DIAGASSERT(method != NULL);
    271      1.12     lukem 	if (database == NULL || method == NULL)
    272      1.12     lukem 		return (NS_UNAVAIL);
    273       1.2     lukem 
    274       1.2     lukem 	dbt = _nsdbtget(database);
    275       1.5     lukem 	if (dbt != NULL) {
    276       1.5     lukem 		srclist = dbt->srclist;
    277       1.5     lukem 		srclistsize = dbt->srclistsize;
    278       1.5     lukem 	} else {
    279       1.5     lukem 		srclist = defaults;
    280       1.5     lukem 		srclistsize = 0;
    281       1.5     lukem 		while (srclist[srclistsize].name != NULL)
    282       1.5     lukem 			srclistsize++;
    283       1.5     lukem 	}
    284       1.2     lukem 	result = 0;
    285       1.2     lukem 
    286       1.5     lukem 	for (i = 0; i < srclistsize; i++) {
    287       1.2     lukem 		for (curdisp = 0; disp_tab[curdisp].src != NULL; curdisp++)
    288       1.2     lukem 			if (strcasecmp(disp_tab[curdisp].src,
    289       1.5     lukem 			    srclist[i].name) == 0)
    290       1.2     lukem 				break;
    291       1.2     lukem 		result = 0;
    292       1.2     lukem 		if (disp_tab[curdisp].callback) {
    293       1.2     lukem #if __STDC__
    294       1.5     lukem 			va_start(ap, defaults);
    295       1.2     lukem #else
    296       1.2     lukem 			va_start(ap);
    297       1.2     lukem #endif
    298       1.2     lukem 			result = disp_tab[curdisp].callback(retval,
    299       1.2     lukem 			    disp_tab[curdisp].cb_data, ap);
    300       1.2     lukem 			va_end(ap);
    301       1.5     lukem 			if (result & srclist[i].flags) {
    302       1.2     lukem 				break;
    303       1.2     lukem 			}
    304       1.2     lukem 		}
    305       1.2     lukem 	}
    306       1.2     lukem 	return (result ? result : NS_NOTFOUND);
    307       1.2     lukem }
    308