1 /* $NetBSD: devname.c,v 1.9 2000/06/05 06:12:49 simonb 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. All advertising materials mentioning features or use of this software 56 * must display the following acknowledgement: 57 * This product includes software developed by the University of 58 * California, Berkeley and its contributors. 59 * 4. Neither the name of the University nor the names of its contributors 60 * may be used to endorse or promote products derived from this software 61 * without specific prior written permission. 62 * 63 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 64 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 65 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 66 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 67 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 68 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 69 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 70 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 71 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 72 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 73 * SUCH DAMAGE. 74 */ 75 76 #include <sys/cdefs.h> 77 #if defined(LIBC_SCCS) && !defined(lint) 78 #if 0 79 static char sccsid[] = "@(#)devname.c 8.2 (Berkeley) 4/29/95"; 80 #else 81 __RCSID("$NetBSD: devname.c,v 1.9 2000/06/05 06:12:49 simonb Exp $"); 82 #endif 83 #endif /* LIBC_SCCS and not lint */ 84 85 #include "namespace.h" 86 #include <sys/types.h> 87 88 #include <db.h> 89 #include <fcntl.h> 90 #include <paths.h> 91 #include <stdio.h> 92 #include <string.h> 93 #include <stdlib.h> 94 #include <err.h> 95 96 #ifdef __weak_alias 97 __weak_alias(devname,_devname) 98 #endif 99 100 #define DEV_SZ 317 /* show be prime for best results */ 101 #define VALID 1 /* entry and devname are valid */ 102 #define INVALID 2 /* entry valid, devname NOT valid */ 103 104 typedef struct devc { 105 int valid; /* entry valid? */ 106 dev_t dev; /* cached device */ 107 mode_t type; /* cached file type */ 108 char name[NAME_MAX]; /* device name */ 109 } DEVC; 110 111 char * 112 devname(dev, type) 113 dev_t dev; 114 mode_t type; 115 { 116 struct { 117 mode_t type; 118 dev_t dev; 119 } bkey; 120 static DB *db; 121 static int failure; 122 DBT data, key; 123 DEVC *ptr, **pptr; 124 static DEVC **devtb = NULL; 125 126 if (!db && !failure && 127 !(db = dbopen(_PATH_DEVDB, O_RDONLY, 0, DB_HASH, NULL))) { 128 warn("warning: %s", _PATH_DEVDB); 129 failure = 1; 130 } 131 /* initialise dev cache */ 132 if (!failure && devtb == NULL) { 133 devtb = (DEVC **)calloc(DEV_SZ, sizeof(DEVC *)); 134 if (devtb == NULL) 135 failure= 1; 136 } 137 if (failure) 138 return (NULL); 139 140 /* see if we have this dev/type cached */ 141 pptr = devtb + ((dev + type) % DEV_SZ); 142 ptr = *pptr; 143 144 if (ptr && ptr->valid > 0 && ptr->dev == dev && ptr->type == type) { 145 if (ptr->valid == VALID) 146 return (ptr->name); 147 return (NULL); 148 } 149 150 if (ptr == NULL) 151 *pptr = ptr = (DEVC *)malloc(sizeof(DEVC)); 152 153 /* 154 * Keys are a mode_t followed by a dev_t. The former is the type of 155 * the file (mode & S_IFMT), the latter is the st_rdev field. Be 156 * sure to clear any padding that may be found in bkey. 157 */ 158 memset(&bkey, 0, sizeof(bkey)); 159 bkey.dev = dev; 160 bkey.type = type; 161 key.data = &bkey; 162 key.size = sizeof(bkey); 163 if ((db->get)(db, &key, &data, 0) == 0) { 164 if (ptr == NULL) 165 return (char *)data.data; 166 ptr->dev = dev; 167 ptr->type = type; 168 strncpy(ptr->name, (char *)data.data, NAME_MAX); 169 ptr->name[NAME_MAX - 1] = '\0'; 170 ptr->valid = VALID; 171 return (ptr->name); 172 } else { 173 if (ptr == NULL) 174 return (NULL); 175 ptr->dev = dev; 176 ptr->type = type; 177 ptr->valid = INVALID; 178 return (NULL); 179 } 180 } 181