1 /* $OpenBSD: devname.c,v 1.13 2016/07/06 04:35:12 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 <string.h>
41 #include <stdlib.h>
42
43 static char *
devname_nodb(dev_t dev,mode_t type)44 devname_nodb(dev_t dev, mode_t type)
45 {
46 static char buf[NAME_MAX + 1];
47 char *name = NULL;
48 struct dirent *dp;
49 struct stat sb;
50 DIR *dirp;
51
52 if ((dirp = opendir(_PATH_DEV)) == NULL)
53 return (NULL);
54 while ((dp = readdir(dirp)) != NULL) {
55 if (dp->d_type != DT_UNKNOWN && DTTOIF(dp->d_type) != type)
56 continue;
57 if (fstatat(dirfd(dirp), dp->d_name, &sb, AT_SYMLINK_NOFOLLOW)
58 || sb.st_rdev != dev || (sb.st_mode & S_IFMT) != type)
59 continue;
60 strlcpy(buf, dp->d_name, sizeof(buf));
61 name = buf;
62 break;
63 }
64 closedir(dirp);
65 return (name);
66 }
67
68 /*
69 * Keys in dev.db are a mode_t followed by a dev_t. The former is the
70 * type of the file (mode & S_IFMT), the latter is the st_rdev field.
71 * Note that the structure may contain padding.
72 */
73 char *
devname(dev_t dev,mode_t type)74 devname(dev_t dev, mode_t type)
75 {
76 static DB *db;
77 static bool failure;
78 struct {
79 mode_t type;
80 dev_t dev;
81 } bkey;
82 DBT data, key;
83 char *name = NULL;
84
85 if (!db && !failure) {
86 if (!(db = dbopen(_PATH_DEVDB, O_RDONLY, 0, DB_HASH, NULL)))
87 failure = true;
88 }
89 if (!failure) {
90 /* Be sure to clear any padding that may be found in bkey. */
91 memset(&bkey, 0, sizeof(bkey));
92 bkey.dev = dev;
93 bkey.type = type;
94 key.data = &bkey;
95 key.size = sizeof(bkey);
96 if ((db->get)(db, &key, &data, 0) == 0)
97 name = data.data;
98 } else {
99 name = devname_nodb(dev, type);
100 }
101 return (name ? name : "??");
102 }
103 DEF_WEAK(devname);
104