xref: /openbsd-src/lib/libc/gen/devname.c (revision 9b9d2a55a62c8e82206c25f94fcc7f4e2765250e)
1 /*	$OpenBSD: devname.c,v 1.11 2015/08/26 01:54:09 guenther Exp $ */
2 /*
3  * Copyright (c) 1989, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the University nor the names of its contributors
15  *    may be used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/stat.h>
32 #include <sys/types.h>
33 
34 #include <db.h>
35 #include <dirent.h>
36 #include <fcntl.h>
37 #include <limits.h>
38 #include <paths.h>
39 #include <stdbool.h>
40 #include <stdio.h>
41 #include <string.h>
42 #include <stdlib.h>
43 
44 static char *
45 devname_nodb(dev_t dev, mode_t type)
46 {
47 	static char buf[sizeof(_PATH_DEV) + NAME_MAX];
48 	char *name = NULL;
49 	struct dirent *dp;
50 	struct stat sb;
51 	DIR *dirp;
52 
53 	if ((dirp = opendir(_PATH_DEV)) == NULL)
54 		return (NULL);
55 	if (strlcpy(buf, _PATH_DEV, sizeof(buf)) >= sizeof(buf))
56 		return (NULL);
57 	while ((dp = readdir(dirp)) != NULL) {
58 		if (dp->d_type != DT_UNKNOWN && DTTOIF(dp->d_type) != type)
59 			continue;
60 		buf[sizeof(_PATH_DEV) - 1] = '\0';
61 		if (strlcat(buf, dp->d_name, sizeof(buf)) >= sizeof(buf))
62 			continue;
63 		if (lstat(buf, &sb) == -1)
64 			continue;
65 		if (sb.st_rdev != dev || (sb.st_mode & S_IFMT) != type)
66 			continue;
67 		name = buf + sizeof(_PATH_DEV) - 1;
68 		break;
69 	}
70 	closedir(dirp);
71 	return (name);
72 }
73 
74 /*
75  * Keys in dev.db are a mode_t followed by a dev_t.  The former is the
76  * type of the file (mode & S_IFMT), the latter is the st_rdev field.
77  * Note that the structure may contain padding.
78  */
79 char *
80 devname(dev_t dev, mode_t type)
81 {
82 	static DB *db;
83 	static bool failure;
84 	struct {
85 		mode_t type;
86 		dev_t dev;
87 	} bkey;
88 	DBT data, key;
89 	char *name = NULL;
90 
91 	if (!db && !failure) {
92 		if (!(db = dbopen(_PATH_DEVDB, O_RDONLY, 0, DB_HASH, NULL)))
93 			failure = true;
94 	}
95 	if (!failure) {
96 		/* Be sure to clear any padding that may be found in bkey.  */
97 		memset(&bkey, 0, sizeof(bkey));
98 		bkey.dev = dev;
99 		bkey.type = type;
100 		key.data = &bkey;
101 		key.size = sizeof(bkey);
102 		if ((db->get)(db, &key, &data, 0) == 0)
103 			name = data.data;
104 	} else {
105 		name = devname_nodb(dev, type);
106 	}
107 	return (name ? name : "??");
108 }
109