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