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