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 __minix 49 #include <minix/dmap.h> /* for UNIX98_MINOR */ 50 #endif /* __minix */ 51 52 #ifdef __weak_alias 53 __weak_alias(devname_r,_devname_r) 54 #endif 55 56 static once_t db_opened = ONCE_INITIALIZER; 57 static struct cdbr *db; 58 static devmajor_t pts; 59 60 static void 61 devname_dbopen(void) 62 { 63 db = cdbr_open(_PATH_DEVCDB, CDBR_DEFAULT); 64 pts = getdevmajor("pts", S_IFCHR); 65 } 66 67 __CTASSERT(sizeof(dev_t) == 8); 68 69 static int 70 devname_dblookup(dev_t dev, mode_t type, char *path, size_t len) 71 { 72 const void *data; 73 size_t datalen; 74 uint8_t key[10]; 75 76 le64enc(key, dev); 77 le16enc(key + 8, type); 78 if (cdbr_find(db, key, sizeof(key), &data, &datalen) != 0) 79 return ENOENT; 80 if (datalen <= sizeof(key)) 81 return ENOENT; 82 if (memcmp(key, data, sizeof(key)) != 0) 83 return ENOENT; 84 data = (const char *)data + sizeof(key); 85 datalen -= sizeof(key); 86 if (memchr(data, '\0', datalen) != (const char *)data + datalen - 1) 87 return ENOENT; 88 if (datalen > len) 89 return ERANGE; 90 memcpy(path, data, datalen); 91 return 0; 92 } 93 94 static int 95 devname_ptslookup(dev_t dev, mode_t type, char *path, size_t len) 96 { 97 int rv; 98 99 if (type != S_IFCHR || pts == NODEVMAJOR || major(dev) != pts) 100 return ENOENT; 101 #ifdef __minix 102 /* 103 * MINIX3 does not use an identity mapping for /dev/pts, because the 104 * same major number is also used for PTY masters and legacy PTYs. 105 */ 106 if (minor(dev) < UNIX98_MINOR || !(minor(dev) & 1)) 107 return ENOENT; 108 #endif /* __minix */ 109 110 rv = snprintf(path, len, "%s%d", _PATH_DEV_PTS + sizeof(_PATH_DEV) - 1, 111 #ifndef __minix 112 minor(dev)); 113 #else /* __minix */ 114 (minor(dev) - UNIX98_MINOR) >> 1); 115 #endif /* __minix */ 116 if (rv < 0 || (size_t)rv >= len) 117 return ERANGE; 118 return 0; 119 } 120 121 static int 122 devname_fts(dev_t dev, mode_t type, char *path, size_t len) 123 { 124 FTS *ftsp; 125 FTSENT *fe; 126 static const char path_dev[] = _PATH_DEV; 127 static char * const dirs[2] = { __UNCONST(path_dev), NULL }; 128 const size_t len_dev = strlen(path_dev); 129 int rv; 130 131 if ((ftsp = fts_open(dirs, FTS_NOCHDIR | FTS_PHYSICAL, NULL)) == NULL) 132 return ENOENT; 133 134 rv = ENOENT; 135 while ((fe = fts_read(ftsp)) != NULL) { 136 if (fe->fts_info != FTS_DEFAULT) 137 continue; 138 if (fe->fts_statp->st_rdev != dev) 139 continue; 140 if ((type & S_IFMT) != (fe->fts_statp->st_mode & S_IFMT)) 141 continue; 142 if (strncmp(fe->fts_path, path_dev, len_dev)) 143 continue; 144 if (strlcpy(path, fe->fts_path + len_dev, len) < len) { 145 rv = 0; 146 break; 147 } 148 } 149 150 fts_close(ftsp); 151 return rv; 152 } 153 154 int 155 devname_r(dev_t dev, mode_t type, char *path, size_t len) 156 { 157 int rv; 158 159 thr_once(&db_opened, devname_dbopen); 160 161 if (db != NULL) { 162 rv = devname_dblookup(dev, type, path, len); 163 if (rv == 0 || rv == ERANGE) 164 return rv; 165 } 166 167 rv = devname_ptslookup(dev, type, path, len); 168 if (rv == 0 || rv == ERANGE) 169 return rv; 170 171 if (db != NULL) 172 return ENOENT; 173 rv = devname_fts(dev, type, path, len); 174 return rv; 175 } 176 177 char * 178 devname(dev_t dev, mode_t type) 179 { 180 static char path[PATH_MAX]; 181 182 return devname_r(dev, type, path, sizeof(path)) == 0 ? path : NULL; 183 } 184