1 /* $NetBSD: devname.c,v 1.24 2025/12/16 12:39:02 nia 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.24 2025/12/16 12:39:02 nia Exp $"); 34 35 #include "namespace.h" 36 #include "reentrant.h" 37 #include <sys/stat.h> 38 #include <sys/endian.h> 39 40 #include <cdbr.h> 41 #include <errno.h> 42 #include <fts.h> 43 #include <limits.h> 44 #include <paths.h> 45 #include <stdio.h> 46 #include <string.h> 47 #include <stdlib.h> 48 49 #ifdef __weak_alias 50 __weak_alias(devname_r,_devname_r) 51 #endif 52 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 106 static int 107 devname_fts(dev_t dev, mode_t type, char *path, size_t len) 108 { 109 FTS *ftsp; 110 FTSENT *fe; 111 static const char path_dev[] = _PATH_DEV; 112 static char * const dirs[2] = { __UNCONST(path_dev), NULL }; 113 const size_t len_dev = strlen(path_dev); 114 int rv; 115 116 if ((ftsp = fts_open(dirs, FTS_NOCHDIR | FTS_PHYSICAL, NULL)) == NULL) 117 return ENOENT; 118 119 rv = ENOENT; 120 while ((fe = fts_read(ftsp)) != NULL) { 121 if (fe->fts_info != FTS_DEFAULT) 122 continue; 123 if (fe->fts_statp->st_rdev != dev) 124 continue; 125 if ((type & S_IFMT) != (fe->fts_statp->st_mode & S_IFMT)) 126 continue; 127 if (strncmp(fe->fts_path, path_dev, len_dev)) 128 continue; 129 if (strlcpy(path, fe->fts_path + len_dev, len) < len) { 130 rv = 0; 131 break; 132 } 133 } 134 135 fts_close(ftsp); 136 return rv; 137 } 138 139 int 140 devname_r(dev_t dev, mode_t type, char *path, size_t len) 141 { 142 int rv; 143 144 thr_once(&db_opened, devname_dbopen); 145 146 if (db != NULL) { 147 rv = devname_dblookup(dev, type, path, len); 148 if (rv == 0 || rv == ERANGE) 149 return rv; 150 } 151 152 rv = devname_ptslookup(dev, type, path, len); 153 if (rv == 0 || rv == ERANGE) 154 return rv; 155 156 if (db != NULL) 157 return ENOENT; 158 rv = devname_fts(dev, type, path, len); 159 return rv; 160 } 161 162 char * 163 devname(dev_t dev, mode_t type) 164 { 165 static char path[PATH_MAX]; 166 167 return devname_r(dev, type, path, sizeof(path)) == 0 ? path : NULL; 168 } 169