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