1 /* $NetBSD: devname.c,v 1.16 2004/12/16 04:33:03 atatat 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.16 2004/12/16 04:33:03 atatat Exp $"); 78 #endif 79 #endif /* LIBC_SCCS and not lint */ 80 81 #include "namespace.h" 82 #include <sys/types.h> 83 #include <sys/stat.h> 84 #include <sys/param.h> 85 86 #include <db.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 94 #ifdef __weak_alias 95 __weak_alias(devname,_devname) 96 #endif 97 98 #define DEV_SZ 317 /* show be prime for best results */ 99 #define VALID 1 /* entry and devname are valid */ 100 #define INVALID 2 /* entry valid, devname NOT valid */ 101 102 typedef struct devc { 103 int valid; /* entry valid? */ 104 dev_t dev; /* cached device */ 105 mode_t type; /* cached file type */ 106 char name[NAME_MAX]; /* device name */ 107 } DEVC; 108 109 char * 110 devname(dev, type) 111 dev_t dev; 112 mode_t type; 113 { 114 struct { 115 mode_t type; 116 dev_t dev; 117 } bkey; 118 static DB *db; 119 static int failure; 120 DBT data, key; 121 DEVC *ptr, **pptr; 122 static DEVC **devtb = NULL; 123 static dev_t pts = (dev_t)~1; 124 125 if (!db && !failure && 126 !(db = dbopen(_PATH_DEVDB, O_RDONLY, 0, DB_HASH, NULL))) { 127 warn("warning: %s", _PATH_DEVDB); 128 failure = 1; 129 } 130 /* initialise dev cache */ 131 if (!failure && devtb == NULL) { 132 devtb = (DEVC **)calloc(DEV_SZ, sizeof(DEVC *)); 133 if (devtb == NULL) 134 failure= 1; 135 } 136 if (failure) 137 return (NULL); 138 139 /* see if we have this dev/type cached */ 140 pptr = devtb + ((dev + type) % DEV_SZ); 141 ptr = *pptr; 142 143 if (ptr && ptr->valid > 0 && ptr->dev == dev && ptr->type == type) { 144 if (ptr->valid == VALID) 145 return (ptr->name); 146 return (NULL); 147 } 148 149 if (ptr == NULL) 150 *pptr = ptr = (DEVC *)malloc(sizeof(DEVC)); 151 152 /* 153 * Keys are a mode_t followed by a dev_t. The former is the type of 154 * the file (mode & S_IFMT), the latter is the st_rdev field. Be 155 * sure to clear any padding that may be found in bkey. 156 */ 157 memset(&bkey, 0, sizeof(bkey)); 158 bkey.dev = dev; 159 bkey.type = type; 160 key.data = &bkey; 161 key.size = sizeof(bkey); 162 if ((db->get)(db, &key, &data, 0) == 0) { 163 if (ptr == NULL) 164 return (char *)data.data; 165 ptr->dev = dev; 166 ptr->type = type; 167 strncpy(ptr->name, (char *)data.data, NAME_MAX); 168 ptr->name[NAME_MAX - 1] = '\0'; 169 ptr->valid = VALID; 170 } else { 171 if (ptr == NULL) 172 return (NULL); 173 ptr->valid = INVALID; 174 if (type == S_IFCHR) { 175 if (pts == (dev_t)~1) 176 pts = getdevmajor("pts", S_IFCHR); 177 if (pts != (dev_t)~0 && major(dev) == pts) { 178 (void)snprintf(ptr->name, sizeof(ptr->name), 179 "%s%d", _PATH_DEV_PTS + 180 sizeof(_PATH_DEV) - 1, minor(dev)); 181 ptr->valid = VALID; 182 } 183 } 184 ptr->dev = dev; 185 ptr->type = type; 186 } 187 if (ptr->valid == VALID) 188 return (ptr->name); 189 else 190 return (NULL); 191 } 192