1f14fb602SLionel Sambuc /* $NetBSD: devname.c,v 1.22 2012/06/03 21:42:46 joerg Exp $ */
22fe8fb19SBen Gras
32fe8fb19SBen Gras /*-
4f14fb602SLionel Sambuc * Copyright (c) 2012 The NetBSD Foundation, Inc.
52fe8fb19SBen Gras * All rights reserved.
62fe8fb19SBen Gras *
72fe8fb19SBen Gras * This code is derived from software contributed to The NetBSD Foundation
82fe8fb19SBen Gras * by Simon Burge.
92fe8fb19SBen Gras *
102fe8fb19SBen Gras * Redistribution and use in source and binary forms, with or without
112fe8fb19SBen Gras * modification, are permitted provided that the following conditions
122fe8fb19SBen Gras * are met:
132fe8fb19SBen Gras * 1. Redistributions of source code must retain the above copyright
142fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer.
152fe8fb19SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
162fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer in the
172fe8fb19SBen Gras * documentation and/or other materials provided with the distribution.
182fe8fb19SBen Gras *
192fe8fb19SBen Gras * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
202fe8fb19SBen Gras * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
212fe8fb19SBen Gras * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
222fe8fb19SBen Gras * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
232fe8fb19SBen Gras * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
242fe8fb19SBen Gras * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
252fe8fb19SBen Gras * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
262fe8fb19SBen Gras * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
272fe8fb19SBen Gras * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
282fe8fb19SBen Gras * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
292fe8fb19SBen Gras * POSSIBILITY OF SUCH DAMAGE.
302fe8fb19SBen Gras */
312fe8fb19SBen Gras
322fe8fb19SBen Gras #include <sys/cdefs.h>
33f14fb602SLionel Sambuc __RCSID("$NetBSD: devname.c,v 1.22 2012/06/03 21:42:46 joerg Exp $");
342fe8fb19SBen Gras
352fe8fb19SBen Gras #include "namespace.h"
36f14fb602SLionel Sambuc #include "reentrant.h"
372fe8fb19SBen Gras #include <sys/stat.h>
382fe8fb19SBen Gras
39f14fb602SLionel Sambuc #include <cdbr.h>
40f14fb602SLionel Sambuc #include <errno.h>
41f14fb602SLionel Sambuc #include <fts.h>
42f14fb602SLionel Sambuc #include <limits.h>
432fe8fb19SBen Gras #include <paths.h>
442fe8fb19SBen Gras #include <stdio.h>
452fe8fb19SBen Gras #include <string.h>
462fe8fb19SBen Gras #include <stdlib.h>
472fe8fb19SBen Gras
48*180e7470SDavid van Moolenbroek #ifdef __minix
49*180e7470SDavid van Moolenbroek #include <minix/dmap.h> /* for UNIX98_MINOR */
50*180e7470SDavid van Moolenbroek #endif /* __minix */
51*180e7470SDavid van Moolenbroek
52f14fb602SLionel Sambuc #ifdef __weak_alias
53f14fb602SLionel Sambuc __weak_alias(devname_r,_devname_r)
54f14fb602SLionel Sambuc #endif
552fe8fb19SBen Gras
56f14fb602SLionel Sambuc static once_t db_opened = ONCE_INITIALIZER;
57f14fb602SLionel Sambuc static struct cdbr *db;
58f14fb602SLionel Sambuc static devmajor_t pts;
59f14fb602SLionel Sambuc
60f14fb602SLionel Sambuc static void
devname_dbopen(void)61f14fb602SLionel Sambuc devname_dbopen(void)
62f14fb602SLionel Sambuc {
63f14fb602SLionel Sambuc db = cdbr_open(_PATH_DEVCDB, CDBR_DEFAULT);
64f14fb602SLionel Sambuc pts = getdevmajor("pts", S_IFCHR);
65f14fb602SLionel Sambuc }
66f14fb602SLionel Sambuc
67f14fb602SLionel Sambuc __CTASSERT(sizeof(dev_t) == 8);
68f14fb602SLionel Sambuc
69f14fb602SLionel Sambuc static int
devname_dblookup(dev_t dev,mode_t type,char * path,size_t len)70f14fb602SLionel Sambuc devname_dblookup(dev_t dev, mode_t type, char *path, size_t len)
71f14fb602SLionel Sambuc {
72f14fb602SLionel Sambuc const void *data;
73f14fb602SLionel Sambuc size_t datalen;
74f14fb602SLionel Sambuc uint8_t key[10];
75f14fb602SLionel Sambuc
76f14fb602SLionel Sambuc le64enc(key, dev);
77f14fb602SLionel Sambuc le16enc(key + 8, type);
78f14fb602SLionel Sambuc if (cdbr_find(db, key, sizeof(key), &data, &datalen) != 0)
79f14fb602SLionel Sambuc return ENOENT;
80f14fb602SLionel Sambuc if (datalen <= sizeof(key))
81f14fb602SLionel Sambuc return ENOENT;
82f14fb602SLionel Sambuc if (memcmp(key, data, sizeof(key)) != 0)
83f14fb602SLionel Sambuc return ENOENT;
84f14fb602SLionel Sambuc data = (const char *)data + sizeof(key);
85f14fb602SLionel Sambuc datalen -= sizeof(key);
86f14fb602SLionel Sambuc if (memchr(data, '\0', datalen) != (const char *)data + datalen - 1)
87f14fb602SLionel Sambuc return ENOENT;
88f14fb602SLionel Sambuc if (datalen > len)
89f14fb602SLionel Sambuc return ERANGE;
90f14fb602SLionel Sambuc memcpy(path, data, datalen);
91f14fb602SLionel Sambuc return 0;
92f14fb602SLionel Sambuc }
93f14fb602SLionel Sambuc
94f14fb602SLionel Sambuc static int
devname_ptslookup(dev_t dev,mode_t type,char * path,size_t len)95f14fb602SLionel Sambuc devname_ptslookup(dev_t dev, mode_t type, char *path, size_t len)
96f14fb602SLionel Sambuc {
97f14fb602SLionel Sambuc int rv;
98f14fb602SLionel Sambuc
99f14fb602SLionel Sambuc if (type != S_IFCHR || pts == NODEVMAJOR || major(dev) != pts)
100f14fb602SLionel Sambuc return ENOENT;
101*180e7470SDavid van Moolenbroek #ifdef __minix
102*180e7470SDavid van Moolenbroek /*
103*180e7470SDavid van Moolenbroek * MINIX3 does not use an identity mapping for /dev/pts, because the
104*180e7470SDavid van Moolenbroek * same major number is also used for PTY masters and legacy PTYs.
105*180e7470SDavid van Moolenbroek */
106*180e7470SDavid van Moolenbroek if (minor(dev) < UNIX98_MINOR || !(minor(dev) & 1))
107*180e7470SDavid van Moolenbroek return ENOENT;
108*180e7470SDavid van Moolenbroek #endif /* __minix */
109f14fb602SLionel Sambuc
110f14fb602SLionel Sambuc rv = snprintf(path, len, "%s%d", _PATH_DEV_PTS + sizeof(_PATH_DEV) - 1,
111*180e7470SDavid van Moolenbroek #ifndef __minix
112f14fb602SLionel Sambuc minor(dev));
113*180e7470SDavid van Moolenbroek #else /* __minix */
114*180e7470SDavid van Moolenbroek (minor(dev) - UNIX98_MINOR) >> 1);
115*180e7470SDavid van Moolenbroek #endif /* __minix */
116f14fb602SLionel Sambuc if (rv < 0 || (size_t)rv >= len)
117f14fb602SLionel Sambuc return ERANGE;
118f14fb602SLionel Sambuc return 0;
119f14fb602SLionel Sambuc }
120f14fb602SLionel Sambuc
121f14fb602SLionel Sambuc static int
devname_fts(dev_t dev,mode_t type,char * path,size_t len)122f14fb602SLionel Sambuc devname_fts(dev_t dev, mode_t type, char *path, size_t len)
123f14fb602SLionel Sambuc {
124f14fb602SLionel Sambuc FTS *ftsp;
125f14fb602SLionel Sambuc FTSENT *fe;
126f14fb602SLionel Sambuc static const char path_dev[] = _PATH_DEV;
127f14fb602SLionel Sambuc static char * const dirs[2] = { __UNCONST(path_dev), NULL };
128f14fb602SLionel Sambuc const size_t len_dev = strlen(path_dev);
129f14fb602SLionel Sambuc int rv;
130f14fb602SLionel Sambuc
131f14fb602SLionel Sambuc if ((ftsp = fts_open(dirs, FTS_NOCHDIR | FTS_PHYSICAL, NULL)) == NULL)
132f14fb602SLionel Sambuc return ENOENT;
133f14fb602SLionel Sambuc
134f14fb602SLionel Sambuc rv = ENOENT;
135f14fb602SLionel Sambuc while ((fe = fts_read(ftsp)) != NULL) {
136f14fb602SLionel Sambuc if (fe->fts_info != FTS_DEFAULT)
137f14fb602SLionel Sambuc continue;
138f14fb602SLionel Sambuc if (fe->fts_statp->st_rdev != dev)
139f14fb602SLionel Sambuc continue;
140f14fb602SLionel Sambuc if ((type & S_IFMT) != (fe->fts_statp->st_mode & S_IFMT))
141f14fb602SLionel Sambuc continue;
142f14fb602SLionel Sambuc if (strncmp(fe->fts_path, path_dev, len_dev))
143f14fb602SLionel Sambuc continue;
144f14fb602SLionel Sambuc if (strlcpy(path, fe->fts_path + len_dev, len) < len) {
145f14fb602SLionel Sambuc rv = 0;
146f14fb602SLionel Sambuc break;
147f14fb602SLionel Sambuc }
148f14fb602SLionel Sambuc }
149f14fb602SLionel Sambuc
150f14fb602SLionel Sambuc fts_close(ftsp);
151f14fb602SLionel Sambuc return rv;
152f14fb602SLionel Sambuc }
153f14fb602SLionel Sambuc
154f14fb602SLionel Sambuc int
devname_r(dev_t dev,mode_t type,char * path,size_t len)155f14fb602SLionel Sambuc devname_r(dev_t dev, mode_t type, char *path, size_t len)
156f14fb602SLionel Sambuc {
157f14fb602SLionel Sambuc int rv;
158f14fb602SLionel Sambuc
159f14fb602SLionel Sambuc thr_once(&db_opened, devname_dbopen);
160f14fb602SLionel Sambuc
161f14fb602SLionel Sambuc if (db != NULL) {
162f14fb602SLionel Sambuc rv = devname_dblookup(dev, type, path, len);
163f14fb602SLionel Sambuc if (rv == 0 || rv == ERANGE)
164f14fb602SLionel Sambuc return rv;
165f14fb602SLionel Sambuc }
166f14fb602SLionel Sambuc
167f14fb602SLionel Sambuc rv = devname_ptslookup(dev, type, path, len);
168f14fb602SLionel Sambuc if (rv == 0 || rv == ERANGE)
169f14fb602SLionel Sambuc return rv;
170f14fb602SLionel Sambuc
171f14fb602SLionel Sambuc if (db != NULL)
172f14fb602SLionel Sambuc return ENOENT;
173f14fb602SLionel Sambuc rv = devname_fts(dev, type, path, len);
174f14fb602SLionel Sambuc return rv;
175f14fb602SLionel Sambuc }
1762fe8fb19SBen Gras
1772fe8fb19SBen Gras char *
devname(dev_t dev,mode_t type)178f14fb602SLionel Sambuc devname(dev_t dev, mode_t type)
1792fe8fb19SBen Gras {
180f14fb602SLionel Sambuc static char path[PATH_MAX];
1812fe8fb19SBen Gras
182f14fb602SLionel Sambuc return devname_r(dev, type, path, sizeof(path)) == 0 ? path : NULL;
1832fe8fb19SBen Gras }
184