1*dae7fb3eSchristos /* $NetBSD: devname.c,v 1.23 2019/02/18 20:27:20 christos Exp $ */
22c4d3c4cScgd
3ce00d9acSsimonb /*-
41907ec3aSjoerg * Copyright (c) 2012 The NetBSD Foundation, Inc.
5ce00d9acSsimonb * All rights reserved.
6ce00d9acSsimonb *
7ce00d9acSsimonb * This code is derived from software contributed to The NetBSD Foundation
8ce00d9acSsimonb * by Simon Burge.
9ce00d9acSsimonb *
10ce00d9acSsimonb * Redistribution and use in source and binary forms, with or without
11ce00d9acSsimonb * modification, are permitted provided that the following conditions
12ce00d9acSsimonb * are met:
13ce00d9acSsimonb * 1. Redistributions of source code must retain the above copyright
14ce00d9acSsimonb * notice, this list of conditions and the following disclaimer.
15ce00d9acSsimonb * 2. Redistributions in binary form must reproduce the above copyright
16ce00d9acSsimonb * notice, this list of conditions and the following disclaimer in the
17ce00d9acSsimonb * documentation and/or other materials provided with the distribution.
18ce00d9acSsimonb *
19ce00d9acSsimonb * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20ce00d9acSsimonb * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21ce00d9acSsimonb * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22ce00d9acSsimonb * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23ce00d9acSsimonb * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24ce00d9acSsimonb * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25ce00d9acSsimonb * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26ce00d9acSsimonb * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27ce00d9acSsimonb * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28ce00d9acSsimonb * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29ce00d9acSsimonb * POSSIBILITY OF SUCH DAMAGE.
30ce00d9acSsimonb */
31ce00d9acSsimonb
32bbe90d58Schristos #include <sys/cdefs.h>
33*dae7fb3eSchristos __RCSID("$NetBSD: devname.c,v 1.23 2019/02/18 20:27:20 christos Exp $");
34cd45651cScgd
35bbe90d58Schristos #include "namespace.h"
361907ec3aSjoerg #include "reentrant.h"
374e04a6f6Satatat #include <sys/stat.h>
38cd45651cScgd
391907ec3aSjoerg #include <cdbr.h>
401907ec3aSjoerg #include <errno.h>
411907ec3aSjoerg #include <fts.h>
421907ec3aSjoerg #include <limits.h>
43cd45651cScgd #include <paths.h>
44cd45651cScgd #include <stdio.h>
45cd45651cScgd #include <string.h>
46bbe90d58Schristos #include <stdlib.h>
47cd45651cScgd
481907ec3aSjoerg #ifdef __weak_alias
491907ec3aSjoerg __weak_alias(devname_r,_devname_r)
501907ec3aSjoerg #endif
51ce00d9acSsimonb
521907ec3aSjoerg static once_t db_opened = ONCE_INITIALIZER;
531907ec3aSjoerg static struct cdbr *db;
541907ec3aSjoerg static devmajor_t pts;
551907ec3aSjoerg
561907ec3aSjoerg static void
devname_dbopen(void)571907ec3aSjoerg devname_dbopen(void)
581907ec3aSjoerg {
591907ec3aSjoerg db = cdbr_open(_PATH_DEVCDB, CDBR_DEFAULT);
601907ec3aSjoerg pts = getdevmajor("pts", S_IFCHR);
611907ec3aSjoerg }
621907ec3aSjoerg
631907ec3aSjoerg __CTASSERT(sizeof(dev_t) == 8);
641907ec3aSjoerg
651907ec3aSjoerg static int
devname_dblookup(dev_t dev,mode_t type,char * path,size_t len)661907ec3aSjoerg devname_dblookup(dev_t dev, mode_t type, char *path, size_t len)
671907ec3aSjoerg {
681907ec3aSjoerg const void *data;
691907ec3aSjoerg size_t datalen;
701907ec3aSjoerg uint8_t key[10];
711907ec3aSjoerg
721907ec3aSjoerg le64enc(key, dev);
731907ec3aSjoerg le16enc(key + 8, type);
741907ec3aSjoerg if (cdbr_find(db, key, sizeof(key), &data, &datalen) != 0)
751907ec3aSjoerg return ENOENT;
761907ec3aSjoerg if (datalen <= sizeof(key))
771907ec3aSjoerg return ENOENT;
781907ec3aSjoerg if (memcmp(key, data, sizeof(key)) != 0)
791907ec3aSjoerg return ENOENT;
801907ec3aSjoerg data = (const char *)data + sizeof(key);
811907ec3aSjoerg datalen -= sizeof(key);
821907ec3aSjoerg if (memchr(data, '\0', datalen) != (const char *)data + datalen - 1)
831907ec3aSjoerg return ENOENT;
841907ec3aSjoerg if (datalen > len)
851907ec3aSjoerg return ERANGE;
861907ec3aSjoerg memcpy(path, data, datalen);
871907ec3aSjoerg return 0;
881907ec3aSjoerg }
891907ec3aSjoerg
901907ec3aSjoerg static int
devname_ptslookup(dev_t dev,mode_t type,char * path,size_t len)911907ec3aSjoerg devname_ptslookup(dev_t dev, mode_t type, char *path, size_t len)
921907ec3aSjoerg {
931907ec3aSjoerg int rv;
941907ec3aSjoerg
951907ec3aSjoerg if (type != S_IFCHR || pts == NODEVMAJOR || major(dev) != pts)
961907ec3aSjoerg return ENOENT;
971907ec3aSjoerg
98*dae7fb3eSchristos rv = snprintf(path, len, "%s%d", &_PATH_DEV_PTS[sizeof(_PATH_DEV) - 1],
991907ec3aSjoerg minor(dev));
1001907ec3aSjoerg if (rv < 0 || (size_t)rv >= len)
1011907ec3aSjoerg return ERANGE;
1021907ec3aSjoerg return 0;
1031907ec3aSjoerg }
1041907ec3aSjoerg
1051907ec3aSjoerg static int
devname_fts(dev_t dev,mode_t type,char * path,size_t len)1061907ec3aSjoerg devname_fts(dev_t dev, mode_t type, char *path, size_t len)
1071907ec3aSjoerg {
1081907ec3aSjoerg FTS *ftsp;
1091907ec3aSjoerg FTSENT *fe;
1101907ec3aSjoerg static const char path_dev[] = _PATH_DEV;
1111907ec3aSjoerg static char * const dirs[2] = { __UNCONST(path_dev), NULL };
1121907ec3aSjoerg const size_t len_dev = strlen(path_dev);
1131907ec3aSjoerg int rv;
1141907ec3aSjoerg
1151907ec3aSjoerg if ((ftsp = fts_open(dirs, FTS_NOCHDIR | FTS_PHYSICAL, NULL)) == NULL)
1161907ec3aSjoerg return ENOENT;
1171907ec3aSjoerg
1181907ec3aSjoerg rv = ENOENT;
1191907ec3aSjoerg while ((fe = fts_read(ftsp)) != NULL) {
1201907ec3aSjoerg if (fe->fts_info != FTS_DEFAULT)
1211907ec3aSjoerg continue;
1221907ec3aSjoerg if (fe->fts_statp->st_rdev != dev)
1231907ec3aSjoerg continue;
1241907ec3aSjoerg if ((type & S_IFMT) != (fe->fts_statp->st_mode & S_IFMT))
1251907ec3aSjoerg continue;
1261907ec3aSjoerg if (strncmp(fe->fts_path, path_dev, len_dev))
1271907ec3aSjoerg continue;
1281907ec3aSjoerg if (strlcpy(path, fe->fts_path + len_dev, len) < len) {
1291907ec3aSjoerg rv = 0;
1301907ec3aSjoerg break;
1311907ec3aSjoerg }
1321907ec3aSjoerg }
1331907ec3aSjoerg
1341907ec3aSjoerg fts_close(ftsp);
1351907ec3aSjoerg return rv;
1361907ec3aSjoerg }
1371907ec3aSjoerg
1381907ec3aSjoerg int
devname_r(dev_t dev,mode_t type,char * path,size_t len)1391907ec3aSjoerg devname_r(dev_t dev, mode_t type, char *path, size_t len)
1401907ec3aSjoerg {
1411907ec3aSjoerg int rv;
1421907ec3aSjoerg
1431907ec3aSjoerg thr_once(&db_opened, devname_dbopen);
1441907ec3aSjoerg
1451907ec3aSjoerg if (db != NULL) {
1461907ec3aSjoerg rv = devname_dblookup(dev, type, path, len);
1471907ec3aSjoerg if (rv == 0 || rv == ERANGE)
1481907ec3aSjoerg return rv;
1491907ec3aSjoerg }
1501907ec3aSjoerg
1511907ec3aSjoerg rv = devname_ptslookup(dev, type, path, len);
1521907ec3aSjoerg if (rv == 0 || rv == ERANGE)
1531907ec3aSjoerg return rv;
1541907ec3aSjoerg
1551907ec3aSjoerg if (db != NULL)
1561907ec3aSjoerg return ENOENT;
1571907ec3aSjoerg rv = devname_fts(dev, type, path, len);
1581907ec3aSjoerg return rv;
1591907ec3aSjoerg }
160ce00d9acSsimonb
161cd45651cScgd char *
devname(dev_t dev,mode_t type)1621907ec3aSjoerg devname(dev_t dev, mode_t type)
163cd45651cScgd {
1641907ec3aSjoerg static char path[PATH_MAX];
165cd45651cScgd
1661907ec3aSjoerg return devname_r(dev, type, path, sizeof(path)) == 0 ? path : NULL;
167cd45651cScgd }
168