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