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