xref: /minix/lib/libc/gen/devname.c (revision ebfedea0)
1 /*	$NetBSD: devname.c,v 1.22 2012/06/03 21:42:46 joerg Exp $	*/
2 
3 /*-
4  * Copyright (c) 2012 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  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __RCSID("$NetBSD: devname.c,v 1.22 2012/06/03 21:42:46 joerg Exp $");
34 
35 #include "namespace.h"
36 #include "reentrant.h"
37 #include <sys/stat.h>
38 
39 #include <cdbr.h>
40 #include <errno.h>
41 #include <fts.h>
42 #include <limits.h>
43 #include <paths.h>
44 #include <stdio.h>
45 #include <string.h>
46 #include <stdlib.h>
47 
48 #ifdef __weak_alias
49 __weak_alias(devname_r,_devname_r)
50 #endif
51 
52 #if !defined(__minix)
53 static once_t db_opened = ONCE_INITIALIZER;
54 static struct cdbr *db;
55 static devmajor_t pts;
56 
57 static void
58 devname_dbopen(void)
59 {
60 	db = cdbr_open(_PATH_DEVCDB, CDBR_DEFAULT);
61 	pts = getdevmajor("pts", S_IFCHR);
62 }
63 
64 __CTASSERT(sizeof(dev_t) == 8);
65 
66 static int
67 devname_dblookup(dev_t dev, mode_t type, char *path, size_t len)
68 {
69 	const void *data;
70 	size_t datalen;
71 	uint8_t key[10];
72 
73 	le64enc(key, dev);
74 	le16enc(key + 8, type);
75 	if (cdbr_find(db, key, sizeof(key), &data, &datalen) != 0)
76 		return ENOENT;
77 	if (datalen <= sizeof(key))
78 		return ENOENT;
79 	if (memcmp(key, data, sizeof(key)) != 0)
80 		return ENOENT;
81 	data = (const char *)data + sizeof(key);
82 	datalen -= sizeof(key);
83 	if (memchr(data, '\0', datalen) != (const char *)data + datalen - 1)
84 		return ENOENT;
85 	if (datalen > len)
86 		return ERANGE;
87 	memcpy(path, data, datalen);
88 	return 0;
89 }
90 
91 static int
92 devname_ptslookup(dev_t dev, mode_t type, char *path, size_t len)
93 {
94 	int rv;
95 
96 	if (type != S_IFCHR || pts == NODEVMAJOR || major(dev) != pts)
97 		return ENOENT;
98 
99 	rv = snprintf(path, len, "%s%d", _PATH_DEV_PTS + sizeof(_PATH_DEV) - 1,
100 	    minor(dev));
101 	if (rv < 0 || (size_t)rv >= len)
102 		return ERANGE;
103 	return 0;
104 }
105 #endif /* !defined(__minix) */
106 
107 static int
108 devname_fts(dev_t dev, mode_t type, char *path, size_t len)
109 {
110 	FTS *ftsp;
111 	FTSENT *fe;
112 	static const char path_dev[] = _PATH_DEV;
113 	static char * const dirs[2] = { __UNCONST(path_dev), NULL };
114 	const size_t len_dev = strlen(path_dev);
115 	int rv;
116 
117 	if ((ftsp = fts_open(dirs, FTS_NOCHDIR | FTS_PHYSICAL, NULL)) == NULL)
118 		return ENOENT;
119 
120 	rv = ENOENT;
121 	while ((fe = fts_read(ftsp)) != NULL) {
122 		if (fe->fts_info != FTS_DEFAULT)
123 			continue;
124 		if (fe->fts_statp->st_rdev != dev)
125 			continue;
126 		if ((type & S_IFMT) != (fe->fts_statp->st_mode & S_IFMT))
127 			continue;
128 		if (strncmp(fe->fts_path, path_dev, len_dev))
129 			continue;
130 		if (strlcpy(path, fe->fts_path + len_dev, len) < len) {
131 			rv = 0;
132 			break;
133 		}
134 	}
135 
136 	fts_close(ftsp);
137 	return rv;
138 }
139 
140 int
141 devname_r(dev_t dev, mode_t type, char *path, size_t len)
142 {
143 	int rv;
144 
145 #if !defined(__minix)
146 	thr_once(&db_opened, devname_dbopen);
147 
148 	if (db != NULL) {
149 		rv = devname_dblookup(dev, type, path, len);
150 		if (rv == 0 || rv == ERANGE)
151 			return rv;
152 	}
153 
154 	rv = devname_ptslookup(dev, type, path, len);
155 	if (rv == 0 || rv == ERANGE)
156 		return rv;
157 
158 	if (db != NULL)
159 		return ENOENT;
160 #endif /* !defined(__minix) */
161 	rv = devname_fts(dev, type, path, len);
162 	return rv;
163 }
164 
165 char *
166 devname(dev_t dev, mode_t type)
167 {
168 	static char path[PATH_MAX];
169 
170 	return devname_r(dev, type, path, sizeof(path)) == 0 ? path : NULL;
171 }
172