1 /* $NetBSD: devname.c,v 1.13 2004/11/11 04:03:23 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.13 2004/11/11 04:03:23 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; 116 struct dirent *dp; 117 struct stat st; 118 char buf[MAXPATHLEN]; 119 120 if ((dirp = opendir(_PATH_DEV_PTS)) == NULL) 121 return (mode_t)~0; 122 123 while ((dp = readdir(dirp)) != NULL) { 124 if (dp->d_name[0] == '.') 125 continue; 126 (void)snprintf(buf, sizeof(buf), "%s%s", _PATH_DEV_PTS, 127 dp->d_name); 128 if (stat(buf, &st) == -1) 129 continue; 130 (void)closedir(dirp); 131 return major(st.st_rdev); 132 } 133 (void)closedir(dirp); 134 return (mode_t)~0; 135 } 136 137 138 char * 139 devname(dev, type) 140 dev_t dev; 141 mode_t type; 142 { 143 struct { 144 mode_t type; 145 dev_t dev; 146 } bkey; 147 static DB *db; 148 static int failure; 149 DBT data, key; 150 DEVC *ptr, **pptr; 151 static DEVC **devtb = NULL; 152 static mode_t pts = 0; 153 154 155 if (!db && !failure && 156 !(db = dbopen(_PATH_DEVDB, O_RDONLY, 0, DB_HASH, NULL))) { 157 warn("warning: %s", _PATH_DEVDB); 158 failure = 1; 159 } 160 /* initialise dev cache */ 161 if (!failure && devtb == NULL) { 162 devtb = (DEVC **)calloc(DEV_SZ, sizeof(DEVC *)); 163 if (devtb == NULL) 164 failure= 1; 165 } 166 if (failure) 167 return (NULL); 168 169 /* see if we have this dev/type cached */ 170 pptr = devtb + ((dev + type) % DEV_SZ); 171 ptr = *pptr; 172 173 if (ptr && ptr->valid > 0 && ptr->dev == dev && ptr->type == type) { 174 if (ptr->valid == VALID) 175 return (ptr->name); 176 return (NULL); 177 } 178 179 if (ptr == NULL) 180 *pptr = ptr = (DEVC *)malloc(sizeof(DEVC)); 181 182 /* 183 * Keys are a mode_t followed by a dev_t. The former is the type of 184 * the file (mode & S_IFMT), the latter is the st_rdev field. Be 185 * sure to clear any padding that may be found in bkey. 186 */ 187 memset(&bkey, 0, sizeof(bkey)); 188 bkey.dev = dev; 189 bkey.type = type; 190 key.data = &bkey; 191 key.size = sizeof(bkey); 192 if ((db->get)(db, &key, &data, 0) == 0) { 193 if (ptr == NULL) 194 return (char *)data.data; 195 ptr->dev = dev; 196 ptr->type = type; 197 strncpy(ptr->name, (char *)data.data, NAME_MAX); 198 ptr->name[NAME_MAX - 1] = '\0'; 199 ptr->valid = VALID; 200 return (ptr->name); 201 } else { 202 if (ptr == NULL) 203 return (NULL); 204 ptr->valid = INVALID; 205 if (type == S_IFCHR) { 206 if (pts == 0) 207 pts = getptsmajor(); 208 if (pts != (mode_t)~0 && major(dev) == pts) { 209 (void)snprintf(ptr->name, sizeof(ptr->name), 210 "%s%d", _PATH_DEV_PTS + 211 sizeof(_PATH_DEV) - 1, minor(dev)); 212 ptr->valid = VALID; 213 } 214 } 215 ptr->dev = dev; 216 ptr->type = type; 217 return (NULL); 218 } 219 } 220