Home | History | Annotate | Line # | Download | only in gen
devname.c revision 1.21.8.1
      1  1.21.8.1       riz /*	$NetBSD: devname.c,v 1.21.8.1 2012/06/23 22:54:54 riz Exp $	*/
      2       1.4       cgd 
      3       1.9    simonb /*-
      4  1.21.8.1       riz  * Copyright (c) 2012 The NetBSD Foundation, Inc.
      5       1.9    simonb  * All rights reserved.
      6       1.9    simonb  *
      7       1.9    simonb  * This code is derived from software contributed to The NetBSD Foundation
      8       1.9    simonb  * by Simon Burge.
      9       1.9    simonb  *
     10       1.9    simonb  * Redistribution and use in source and binary forms, with or without
     11       1.9    simonb  * modification, are permitted provided that the following conditions
     12       1.9    simonb  * are met:
     13       1.9    simonb  * 1. Redistributions of source code must retain the above copyright
     14       1.9    simonb  *    notice, this list of conditions and the following disclaimer.
     15       1.9    simonb  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.9    simonb  *    notice, this list of conditions and the following disclaimer in the
     17       1.9    simonb  *    documentation and/or other materials provided with the distribution.
     18       1.9    simonb  *
     19       1.9    simonb  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.9    simonb  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.9    simonb  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.9    simonb  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.9    simonb  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.9    simonb  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.9    simonb  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.9    simonb  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.9    simonb  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.9    simonb  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.9    simonb  * POSSIBILITY OF SUCH DAMAGE.
     30       1.9    simonb  */
     31       1.9    simonb 
     32       1.5  christos #include <sys/cdefs.h>
     33  1.21.8.1       riz __RCSID("$NetBSD: devname.c,v 1.21.8.1 2012/06/23 22:54:54 riz Exp $");
     34       1.1       cgd 
     35       1.5  christos #include "namespace.h"
     36  1.21.8.1       riz #include "reentrant.h"
     37      1.15    atatat #include <sys/stat.h>
     38       1.1       cgd 
     39  1.21.8.1       riz #include <cdbr.h>
     40  1.21.8.1       riz #include <errno.h>
     41  1.21.8.1       riz #include <fts.h>
     42  1.21.8.1       riz #include <limits.h>
     43       1.1       cgd #include <paths.h>
     44       1.1       cgd #include <stdio.h>
     45       1.1       cgd #include <string.h>
     46       1.5  christos #include <stdlib.h>
     47       1.6       jtc 
     48  1.21.8.1       riz #ifdef __weak_alias
     49  1.21.8.1       riz __weak_alias(devname_r,_devname_r)
     50  1.21.8.1       riz #endif
     51       1.9    simonb 
     52  1.21.8.1       riz static once_t db_opened = ONCE_INITIALIZER;
     53  1.21.8.1       riz static struct cdbr *db;
     54  1.21.8.1       riz static devmajor_t pts;
     55       1.1       cgd 
     56  1.21.8.1       riz static void
     57  1.21.8.1       riz devname_dbopen(void)
     58  1.21.8.1       riz {
     59  1.21.8.1       riz 	db = cdbr_open(_PATH_DEVCDB, CDBR_DEFAULT);
     60  1.21.8.1       riz 	pts = getdevmajor("pts", S_IFCHR);
     61  1.21.8.1       riz }
     62       1.9    simonb 
     63  1.21.8.1       riz __CTASSERT(sizeof(dev_t) == 8);
     64       1.9    simonb 
     65  1.21.8.1       riz static int
     66  1.21.8.1       riz devname_dblookup(dev_t dev, mode_t type, char *path, size_t len)
     67  1.21.8.1       riz {
     68  1.21.8.1       riz 	const void *data;
     69  1.21.8.1       riz 	size_t datalen;
     70  1.21.8.1       riz 	uint8_t key[10];
     71  1.21.8.1       riz 
     72  1.21.8.1       riz 	le64enc(key, dev);
     73  1.21.8.1       riz 	le16enc(key + 8, type);
     74  1.21.8.1       riz 	if (cdbr_find(db, key, sizeof(key), &data, &datalen) != 0)
     75  1.21.8.1       riz 		return ENOENT;
     76  1.21.8.1       riz 	if (datalen <= sizeof(key))
     77  1.21.8.1       riz 		return ENOENT;
     78  1.21.8.1       riz 	if (memcmp(key, data, sizeof(key)) != 0)
     79  1.21.8.1       riz 		return ENOENT;
     80  1.21.8.1       riz 	data = (const char *)data + sizeof(key);
     81  1.21.8.1       riz 	datalen -= sizeof(key);
     82  1.21.8.1       riz 	if (memchr(data, '\0', datalen) != (const char *)data + datalen - 1)
     83  1.21.8.1       riz 		return ENOENT;
     84  1.21.8.1       riz 	if (datalen > len)
     85  1.21.8.1       riz 		return ERANGE;
     86  1.21.8.1       riz 	memcpy(path, data, datalen);
     87  1.21.8.1       riz 	return 0;
     88  1.21.8.1       riz }
     89  1.21.8.1       riz 
     90  1.21.8.1       riz static int
     91  1.21.8.1       riz devname_ptslookup(dev_t dev, mode_t type, char *path, size_t len)
     92  1.21.8.1       riz {
     93  1.21.8.1       riz 	int rv;
     94  1.21.8.1       riz 
     95  1.21.8.1       riz 	if (type != S_IFCHR || pts == NODEVMAJOR || major(dev) != pts)
     96  1.21.8.1       riz 		return ENOENT;
     97  1.21.8.1       riz 
     98  1.21.8.1       riz 	rv = snprintf(path, len, "%s%d", _PATH_DEV_PTS + sizeof(_PATH_DEV) - 1,
     99  1.21.8.1       riz 	    minor(dev));
    100  1.21.8.1       riz 	if (rv < 0 || (size_t)rv >= len)
    101  1.21.8.1       riz 		return ERANGE;
    102  1.21.8.1       riz 	return 0;
    103  1.21.8.1       riz }
    104  1.21.8.1       riz 
    105  1.21.8.1       riz static int
    106  1.21.8.1       riz devname_fts(dev_t dev, mode_t type, char *path, size_t len)
    107  1.21.8.1       riz {
    108  1.21.8.1       riz 	FTS *ftsp;
    109  1.21.8.1       riz 	FTSENT *fe;
    110  1.21.8.1       riz 	static const char path_dev[] = _PATH_DEV;
    111  1.21.8.1       riz 	static char * const dirs[2] = { __UNCONST(path_dev), NULL };
    112  1.21.8.1       riz 	const size_t len_dev = strlen(path_dev);
    113  1.21.8.1       riz 	int rv;
    114  1.21.8.1       riz 
    115  1.21.8.1       riz 	if ((ftsp = fts_open(dirs, FTS_NOCHDIR | FTS_PHYSICAL, NULL)) == NULL)
    116  1.21.8.1       riz 		return ENOENT;
    117  1.21.8.1       riz 
    118  1.21.8.1       riz 	rv = ENOENT;
    119  1.21.8.1       riz 	while ((fe = fts_read(ftsp)) != NULL) {
    120  1.21.8.1       riz 		if (fe->fts_info != FTS_DEFAULT)
    121  1.21.8.1       riz 			continue;
    122  1.21.8.1       riz 		if (fe->fts_statp->st_rdev != dev)
    123  1.21.8.1       riz 			continue;
    124  1.21.8.1       riz 		if ((type & S_IFMT) != (fe->fts_statp->st_mode & S_IFMT))
    125  1.21.8.1       riz 			continue;
    126  1.21.8.1       riz 		if (strncmp(fe->fts_path, path_dev, len_dev))
    127  1.21.8.1       riz 			continue;
    128  1.21.8.1       riz 		if (strlcpy(path, fe->fts_path + len_dev, len) < len) {
    129  1.21.8.1       riz 			rv = 0;
    130  1.21.8.1       riz 			break;
    131      1.12  christos 		}
    132      1.14    atatat 	}
    133  1.21.8.1       riz 
    134  1.21.8.1       riz 	fts_close(ftsp);
    135  1.21.8.1       riz 	return rv;
    136  1.21.8.1       riz }
    137  1.21.8.1       riz 
    138  1.21.8.1       riz int
    139  1.21.8.1       riz devname_r(dev_t dev, mode_t type, char *path, size_t len)
    140  1.21.8.1       riz {
    141  1.21.8.1       riz 	int rv;
    142  1.21.8.1       riz 
    143  1.21.8.1       riz 	thr_once(&db_opened, devname_dbopen);
    144  1.21.8.1       riz 
    145  1.21.8.1       riz 	if (db != NULL) {
    146  1.21.8.1       riz 		rv = devname_dblookup(dev, type, path, len);
    147  1.21.8.1       riz 		if (rv == 0 || rv == ERANGE)
    148  1.21.8.1       riz 			return rv;
    149  1.21.8.1       riz 	}
    150  1.21.8.1       riz 
    151  1.21.8.1       riz 	rv = devname_ptslookup(dev, type, path, len);
    152  1.21.8.1       riz 	if (rv == 0 || rv == ERANGE)
    153  1.21.8.1       riz 		return rv;
    154  1.21.8.1       riz 
    155  1.21.8.1       riz 	if (db != NULL)
    156  1.21.8.1       riz 		return ENOENT;
    157  1.21.8.1       riz 	rv = devname_fts(dev, type, path, len);
    158  1.21.8.1       riz 	return rv;
    159  1.21.8.1       riz }
    160  1.21.8.1       riz 
    161  1.21.8.1       riz char *
    162  1.21.8.1       riz devname(dev_t dev, mode_t type)
    163  1.21.8.1       riz {
    164  1.21.8.1       riz 	static char path[PATH_MAX];
    165  1.21.8.1       riz 
    166  1.21.8.1       riz 	return devname_r(dev, type, path, sizeof(path)) == 0 ? path : NULL;
    167       1.1       cgd }
    168