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