devname.c revision 1.21.6.1 1 1.21.6.1 yamt /* $NetBSD: devname.c,v 1.21.6.1 2012/10/30 18:58:45 yamt Exp $ */
2 1.4 cgd
3 1.9 simonb /*-
4 1.21.6.1 yamt * Copyright (c) 2012 The NetBSD Foundation, Inc.
5 1.9 simonb * All rights reserved.
6 1.9 simonb *
7 1.9 simonb * This code is derived from software contributed to The NetBSD Foundation
8 1.9 simonb * by Simon Burge.
9 1.9 simonb *
10 1.9 simonb * Redistribution and use in source and binary forms, with or without
11 1.9 simonb * modification, are permitted provided that the following conditions
12 1.9 simonb * are met:
13 1.9 simonb * 1. Redistributions of source code must retain the above copyright
14 1.9 simonb * notice, this list of conditions and the following disclaimer.
15 1.9 simonb * 2. Redistributions in binary form must reproduce the above copyright
16 1.9 simonb * notice, this list of conditions and the following disclaimer in the
17 1.9 simonb * documentation and/or other materials provided with the distribution.
18 1.9 simonb *
19 1.9 simonb * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.9 simonb * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.9 simonb * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.9 simonb * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.9 simonb * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.9 simonb * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.9 simonb * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.9 simonb * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.9 simonb * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.9 simonb * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.9 simonb * POSSIBILITY OF SUCH DAMAGE.
30 1.9 simonb */
31 1.9 simonb
32 1.5 christos #include <sys/cdefs.h>
33 1.21.6.1 yamt __RCSID("$NetBSD: devname.c,v 1.21.6.1 2012/10/30 18:58:45 yamt Exp $");
34 1.1 cgd
35 1.5 christos #include "namespace.h"
36 1.21.6.1 yamt #include "reentrant.h"
37 1.15 atatat #include <sys/stat.h>
38 1.1 cgd
39 1.21.6.1 yamt #include <cdbr.h>
40 1.21.6.1 yamt #include <errno.h>
41 1.21.6.1 yamt #include <fts.h>
42 1.21.6.1 yamt #include <limits.h>
43 1.1 cgd #include <paths.h>
44 1.1 cgd #include <stdio.h>
45 1.1 cgd #include <string.h>
46 1.5 christos #include <stdlib.h>
47 1.6 jtc
48 1.21.6.1 yamt #ifdef __weak_alias
49 1.21.6.1 yamt __weak_alias(devname_r,_devname_r)
50 1.21.6.1 yamt #endif
51 1.9 simonb
52 1.21.6.1 yamt static once_t db_opened = ONCE_INITIALIZER;
53 1.21.6.1 yamt static struct cdbr *db;
54 1.21.6.1 yamt static devmajor_t pts;
55 1.1 cgd
56 1.21.6.1 yamt static void
57 1.21.6.1 yamt devname_dbopen(void)
58 1.21.6.1 yamt {
59 1.21.6.1 yamt db = cdbr_open(_PATH_DEVCDB, CDBR_DEFAULT);
60 1.21.6.1 yamt pts = getdevmajor("pts", S_IFCHR);
61 1.21.6.1 yamt }
62 1.9 simonb
63 1.21.6.1 yamt __CTASSERT(sizeof(dev_t) == 8);
64 1.9 simonb
65 1.21.6.1 yamt static int
66 1.21.6.1 yamt devname_dblookup(dev_t dev, mode_t type, char *path, size_t len)
67 1.21.6.1 yamt {
68 1.21.6.1 yamt const void *data;
69 1.21.6.1 yamt size_t datalen;
70 1.21.6.1 yamt uint8_t key[10];
71 1.21.6.1 yamt
72 1.21.6.1 yamt le64enc(key, dev);
73 1.21.6.1 yamt le16enc(key + 8, type);
74 1.21.6.1 yamt if (cdbr_find(db, key, sizeof(key), &data, &datalen) != 0)
75 1.21.6.1 yamt return ENOENT;
76 1.21.6.1 yamt if (datalen <= sizeof(key))
77 1.21.6.1 yamt return ENOENT;
78 1.21.6.1 yamt if (memcmp(key, data, sizeof(key)) != 0)
79 1.21.6.1 yamt return ENOENT;
80 1.21.6.1 yamt data = (const char *)data + sizeof(key);
81 1.21.6.1 yamt datalen -= sizeof(key);
82 1.21.6.1 yamt if (memchr(data, '\0', datalen) != (const char *)data + datalen - 1)
83 1.21.6.1 yamt return ENOENT;
84 1.21.6.1 yamt if (datalen > len)
85 1.21.6.1 yamt return ERANGE;
86 1.21.6.1 yamt memcpy(path, data, datalen);
87 1.21.6.1 yamt return 0;
88 1.21.6.1 yamt }
89 1.21.6.1 yamt
90 1.21.6.1 yamt static int
91 1.21.6.1 yamt devname_ptslookup(dev_t dev, mode_t type, char *path, size_t len)
92 1.21.6.1 yamt {
93 1.21.6.1 yamt int rv;
94 1.21.6.1 yamt
95 1.21.6.1 yamt if (type != S_IFCHR || pts == NODEVMAJOR || major(dev) != pts)
96 1.21.6.1 yamt return ENOENT;
97 1.21.6.1 yamt
98 1.21.6.1 yamt rv = snprintf(path, len, "%s%d", _PATH_DEV_PTS + sizeof(_PATH_DEV) - 1,
99 1.21.6.1 yamt minor(dev));
100 1.21.6.1 yamt if (rv < 0 || (size_t)rv >= len)
101 1.21.6.1 yamt return ERANGE;
102 1.21.6.1 yamt return 0;
103 1.21.6.1 yamt }
104 1.21.6.1 yamt
105 1.21.6.1 yamt static int
106 1.21.6.1 yamt devname_fts(dev_t dev, mode_t type, char *path, size_t len)
107 1.21.6.1 yamt {
108 1.21.6.1 yamt FTS *ftsp;
109 1.21.6.1 yamt FTSENT *fe;
110 1.21.6.1 yamt static const char path_dev[] = _PATH_DEV;
111 1.21.6.1 yamt static char * const dirs[2] = { __UNCONST(path_dev), NULL };
112 1.21.6.1 yamt const size_t len_dev = strlen(path_dev);
113 1.21.6.1 yamt int rv;
114 1.21.6.1 yamt
115 1.21.6.1 yamt if ((ftsp = fts_open(dirs, FTS_NOCHDIR | FTS_PHYSICAL, NULL)) == NULL)
116 1.21.6.1 yamt return ENOENT;
117 1.21.6.1 yamt
118 1.21.6.1 yamt rv = ENOENT;
119 1.21.6.1 yamt while ((fe = fts_read(ftsp)) != NULL) {
120 1.21.6.1 yamt if (fe->fts_info != FTS_DEFAULT)
121 1.21.6.1 yamt continue;
122 1.21.6.1 yamt if (fe->fts_statp->st_rdev != dev)
123 1.21.6.1 yamt continue;
124 1.21.6.1 yamt if ((type & S_IFMT) != (fe->fts_statp->st_mode & S_IFMT))
125 1.21.6.1 yamt continue;
126 1.21.6.1 yamt if (strncmp(fe->fts_path, path_dev, len_dev))
127 1.21.6.1 yamt continue;
128 1.21.6.1 yamt if (strlcpy(path, fe->fts_path + len_dev, len) < len) {
129 1.21.6.1 yamt rv = 0;
130 1.21.6.1 yamt break;
131 1.12 christos }
132 1.14 atatat }
133 1.21.6.1 yamt
134 1.21.6.1 yamt fts_close(ftsp);
135 1.21.6.1 yamt return rv;
136 1.21.6.1 yamt }
137 1.21.6.1 yamt
138 1.21.6.1 yamt int
139 1.21.6.1 yamt devname_r(dev_t dev, mode_t type, char *path, size_t len)
140 1.21.6.1 yamt {
141 1.21.6.1 yamt int rv;
142 1.21.6.1 yamt
143 1.21.6.1 yamt thr_once(&db_opened, devname_dbopen);
144 1.21.6.1 yamt
145 1.21.6.1 yamt if (db != NULL) {
146 1.21.6.1 yamt rv = devname_dblookup(dev, type, path, len);
147 1.21.6.1 yamt if (rv == 0 || rv == ERANGE)
148 1.21.6.1 yamt return rv;
149 1.21.6.1 yamt }
150 1.21.6.1 yamt
151 1.21.6.1 yamt rv = devname_ptslookup(dev, type, path, len);
152 1.21.6.1 yamt if (rv == 0 || rv == ERANGE)
153 1.21.6.1 yamt return rv;
154 1.21.6.1 yamt
155 1.21.6.1 yamt if (db != NULL)
156 1.21.6.1 yamt return ENOENT;
157 1.21.6.1 yamt rv = devname_fts(dev, type, path, len);
158 1.21.6.1 yamt return rv;
159 1.21.6.1 yamt }
160 1.21.6.1 yamt
161 1.21.6.1 yamt char *
162 1.21.6.1 yamt devname(dev_t dev, mode_t type)
163 1.21.6.1 yamt {
164 1.21.6.1 yamt static char path[PATH_MAX];
165 1.21.6.1 yamt
166 1.21.6.1 yamt return devname_r(dev, type, path, sizeof(path)) == 0 ? path : NULL;
167 1.1 cgd }
168