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