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