Home | History | Annotate | Line # | Download | only in gen
devname.c revision 1.10
      1  1.10       agc /*	$NetBSD: devname.c,v 1.10 2003/08/07 16:42:47 agc Exp $	*/
      2   1.4       cgd 
      3   1.9    simonb /*-
      4   1.9    simonb  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5   1.9    simonb  * All rights reserved.
      6   1.9    simonb  *
      7   1.9    simonb  * This code is derived from software contributed to The NetBSD Foundation
      8   1.9    simonb  * by Simon Burge.
      9   1.9    simonb  *
     10   1.9    simonb  * Redistribution and use in source and binary forms, with or without
     11   1.9    simonb  * modification, are permitted provided that the following conditions
     12   1.9    simonb  * are met:
     13   1.9    simonb  * 1. Redistributions of source code must retain the above copyright
     14   1.9    simonb  *    notice, this list of conditions and the following disclaimer.
     15   1.9    simonb  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.9    simonb  *    notice, this list of conditions and the following disclaimer in the
     17   1.9    simonb  *    documentation and/or other materials provided with the distribution.
     18   1.9    simonb  * 3. All advertising materials mentioning features or use of this software
     19   1.9    simonb  *    must display the following acknowledgement:
     20   1.9    simonb  *        This product includes software developed by the NetBSD
     21   1.9    simonb  *        Foundation, Inc. and its contributors.
     22   1.9    simonb  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23   1.9    simonb  *    contributors may be used to endorse or promote products derived
     24   1.9    simonb  *    from this software without specific prior written permission.
     25   1.9    simonb  *
     26   1.9    simonb  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27   1.9    simonb  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28   1.9    simonb  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29   1.9    simonb  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30   1.9    simonb  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31   1.9    simonb  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32   1.9    simonb  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33   1.9    simonb  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34   1.9    simonb  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35   1.9    simonb  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36   1.9    simonb  * POSSIBILITY OF SUCH DAMAGE.
     37   1.9    simonb  */
     38   1.9    simonb 
     39   1.9    simonb /*-
     40   1.1       cgd  * Copyright (c) 1989, 1993
     41   1.1       cgd  *	The Regents of the University of California.  All rights reserved.
     42   1.1       cgd  *
     43   1.9    simonb  * This code is derived from software contributed to Berkeley by
     44   1.9    simonb  * Keith Muller of the University of California, San Diego.
     45   1.9    simonb  *
     46   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     47   1.1       cgd  * modification, are permitted provided that the following conditions
     48   1.1       cgd  * are met:
     49   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     50   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     51   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     52   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     53   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     54  1.10       agc  * 3. Neither the name of the University nor the names of its contributors
     55  1.10       agc  *    may be used to endorse or promote products derived from this software
     56  1.10       agc  *    without specific prior written permission.
     57  1.10       agc  *
     58  1.10       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59  1.10       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60  1.10       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61  1.10       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62  1.10       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63  1.10       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64  1.10       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65  1.10       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66  1.10       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67  1.10       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68  1.10       agc  * SUCH DAMAGE.
     69  1.10       agc  */
     70  1.10       agc 
     71  1.10       agc /*-
     72  1.10       agc  * Copyright (c) 1992 Keith Muller.
     73  1.10       agc  *
     74  1.10       agc  * This code is derived from software contributed to Berkeley by
     75  1.10       agc  * Keith Muller of the University of California, San Diego.
     76  1.10       agc  *
     77  1.10       agc  * Redistribution and use in source and binary forms, with or without
     78  1.10       agc  * modification, are permitted provided that the following conditions
     79  1.10       agc  * are met:
     80  1.10       agc  * 1. Redistributions of source code must retain the above copyright
     81  1.10       agc  *    notice, this list of conditions and the following disclaimer.
     82  1.10       agc  * 2. Redistributions in binary form must reproduce the above copyright
     83  1.10       agc  *    notice, this list of conditions and the following disclaimer in the
     84  1.10       agc  *    documentation and/or other materials provided with the distribution.
     85   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     86   1.1       cgd  *    must display the following acknowledgement:
     87   1.1       cgd  *	This product includes software developed by the University of
     88   1.1       cgd  *	California, Berkeley and its contributors.
     89   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     90   1.1       cgd  *    may be used to endorse or promote products derived from this software
     91   1.1       cgd  *    without specific prior written permission.
     92   1.1       cgd  *
     93   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     94   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     95   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     96   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     97   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     98   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     99   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    100   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    101   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    102   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    103   1.1       cgd  * SUCH DAMAGE.
    104   1.1       cgd  */
    105   1.1       cgd 
    106   1.5  christos #include <sys/cdefs.h>
    107   1.1       cgd #if defined(LIBC_SCCS) && !defined(lint)
    108   1.4       cgd #if 0
    109   1.7     perry static char sccsid[] = "@(#)devname.c	8.2 (Berkeley) 4/29/95";
    110   1.4       cgd #else
    111  1.10       agc __RCSID("$NetBSD: devname.c,v 1.10 2003/08/07 16:42:47 agc Exp $");
    112   1.4       cgd #endif
    113   1.1       cgd #endif /* LIBC_SCCS and not lint */
    114   1.1       cgd 
    115   1.5  christos #include "namespace.h"
    116   1.1       cgd #include <sys/types.h>
    117   1.1       cgd 
    118   1.1       cgd #include <db.h>
    119   1.1       cgd #include <fcntl.h>
    120   1.1       cgd #include <paths.h>
    121   1.1       cgd #include <stdio.h>
    122   1.1       cgd #include <string.h>
    123   1.5  christos #include <stdlib.h>
    124   1.5  christos #include <err.h>
    125   1.6       jtc 
    126   1.6       jtc #ifdef __weak_alias
    127   1.8   mycroft __weak_alias(devname,_devname)
    128   1.6       jtc #endif
    129   1.1       cgd 
    130   1.9    simonb #define	DEV_SZ		317	/* show be prime for best results */
    131   1.9    simonb #define	VALID		1	/* entry and devname are valid */
    132   1.9    simonb #define	INVALID		2	/* entry valid, devname NOT valid */
    133   1.9    simonb 
    134   1.9    simonb typedef struct devc {
    135   1.9    simonb 	int valid;		/* entry valid? */
    136   1.9    simonb 	dev_t dev;		/* cached device */
    137   1.9    simonb 	mode_t type;		/* cached file type */
    138   1.9    simonb 	char name[NAME_MAX];	/* device name */
    139   1.9    simonb } DEVC;
    140   1.9    simonb 
    141   1.1       cgd char *
    142   1.1       cgd devname(dev, type)
    143   1.1       cgd 	dev_t dev;
    144   1.1       cgd 	mode_t type;
    145   1.1       cgd {
    146   1.1       cgd 	struct {
    147   1.1       cgd 		mode_t type;
    148   1.1       cgd 		dev_t dev;
    149   1.1       cgd 	} bkey;
    150   1.1       cgd 	static DB *db;
    151   1.1       cgd 	static int failure;
    152   1.1       cgd 	DBT data, key;
    153   1.9    simonb 	DEVC *ptr, **pptr;
    154   1.9    simonb 	static DEVC **devtb = NULL;
    155   1.1       cgd 
    156   1.1       cgd 	if (!db && !failure &&
    157   1.1       cgd 	    !(db = dbopen(_PATH_DEVDB, O_RDONLY, 0, DB_HASH, NULL))) {
    158   1.5  christos 		warn("warning: %s", _PATH_DEVDB);
    159   1.1       cgd 		failure = 1;
    160   1.1       cgd 	}
    161   1.9    simonb 	/* initialise dev cache */
    162   1.9    simonb 	if (!failure && devtb == NULL) {
    163   1.9    simonb 		devtb = (DEVC **)calloc(DEV_SZ, sizeof(DEVC *));
    164   1.9    simonb 		if (devtb == NULL)
    165   1.9    simonb 			failure= 1;
    166   1.9    simonb 	}
    167   1.1       cgd 	if (failure)
    168   1.7     perry 		return (NULL);
    169   1.1       cgd 
    170   1.9    simonb 	/* see if we have this dev/type cached */
    171   1.9    simonb 	pptr = devtb + ((dev + type) % DEV_SZ);
    172   1.9    simonb 	ptr = *pptr;
    173   1.9    simonb 
    174   1.9    simonb 	if (ptr && ptr->valid > 0 && ptr->dev == dev && ptr->type == type) {
    175   1.9    simonb 		if (ptr->valid == VALID)
    176   1.9    simonb 			return (ptr->name);
    177   1.9    simonb 		return (NULL);
    178   1.9    simonb 	}
    179   1.9    simonb 
    180   1.9    simonb 	if (ptr == NULL)
    181   1.9    simonb 		*pptr = ptr = (DEVC *)malloc(sizeof(DEVC));
    182   1.9    simonb 
    183   1.1       cgd 	/*
    184   1.1       cgd 	 * Keys are a mode_t followed by a dev_t.  The former is the type of
    185   1.1       cgd 	 * the file (mode & S_IFMT), the latter is the st_rdev field.  Be
    186   1.1       cgd 	 * sure to clear any padding that may be found in bkey.
    187   1.1       cgd 	 */
    188   1.1       cgd 	memset(&bkey, 0, sizeof(bkey));
    189   1.1       cgd 	bkey.dev = dev;
    190   1.1       cgd 	bkey.type = type;
    191   1.1       cgd 	key.data = &bkey;
    192   1.1       cgd 	key.size = sizeof(bkey);
    193   1.9    simonb 	if ((db->get)(db, &key, &data, 0) == 0) {
    194   1.9    simonb 		if (ptr == NULL)
    195   1.9    simonb 			return (char *)data.data;
    196   1.9    simonb 		ptr->dev = dev;
    197   1.9    simonb 		ptr->type = type;
    198   1.9    simonb 		strncpy(ptr->name, (char *)data.data, NAME_MAX);
    199   1.9    simonb 		ptr->name[NAME_MAX - 1] = '\0';
    200   1.9    simonb 		ptr->valid = VALID;
    201   1.9    simonb 		return (ptr->name);
    202   1.9    simonb 	} else {
    203   1.9    simonb 		if (ptr == NULL)
    204   1.9    simonb 			return (NULL);
    205   1.9    simonb 		ptr->dev = dev;
    206   1.9    simonb 		ptr->type = type;
    207   1.9    simonb 		ptr->valid = INVALID;
    208   1.9    simonb 		return (NULL);
    209   1.9    simonb 	}
    210   1.1       cgd }
    211