Home | History | Annotate | Line # | Download | only in gen
devname.c revision 1.22
      1  1.22     joerg /*	$NetBSD: devname.c,v 1.22 2012/06/03 21:42:46 joerg Exp $	*/
      2   1.4       cgd 
      3   1.9    simonb /*-
      4  1.22     joerg  * 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.22     joerg __RCSID("$NetBSD: devname.c,v 1.22 2012/06/03 21:42:46 joerg Exp $");
     34   1.1       cgd 
     35   1.5  christos #include "namespace.h"
     36  1.22     joerg #include "reentrant.h"
     37  1.15    atatat #include <sys/stat.h>
     38   1.1       cgd 
     39  1.22     joerg #include <cdbr.h>
     40  1.22     joerg #include <errno.h>
     41  1.22     joerg #include <fts.h>
     42  1.22     joerg #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.22     joerg #ifdef __weak_alias
     49  1.22     joerg __weak_alias(devname_r,_devname_r)
     50  1.22     joerg #endif
     51  1.22     joerg 
     52  1.22     joerg static once_t db_opened = ONCE_INITIALIZER;
     53  1.22     joerg static struct cdbr *db;
     54  1.22     joerg static devmajor_t pts;
     55  1.22     joerg 
     56  1.22     joerg static void
     57  1.22     joerg devname_dbopen(void)
     58  1.22     joerg {
     59  1.22     joerg 	db = cdbr_open(_PATH_DEVCDB, CDBR_DEFAULT);
     60  1.22     joerg 	pts = getdevmajor("pts", S_IFCHR);
     61  1.22     joerg }
     62  1.22     joerg 
     63  1.22     joerg __CTASSERT(sizeof(dev_t) == 8);
     64  1.22     joerg 
     65  1.22     joerg static int
     66  1.22     joerg devname_dblookup(dev_t dev, mode_t type, char *path, size_t len)
     67  1.22     joerg {
     68  1.22     joerg 	const void *data;
     69  1.22     joerg 	size_t datalen;
     70  1.22     joerg 	uint8_t key[10];
     71  1.22     joerg 
     72  1.22     joerg 	le64enc(key, dev);
     73  1.22     joerg 	le16enc(key + 8, type);
     74  1.22     joerg 	if (cdbr_find(db, key, sizeof(key), &data, &datalen) != 0)
     75  1.22     joerg 		return ENOENT;
     76  1.22     joerg 	if (datalen <= sizeof(key))
     77  1.22     joerg 		return ENOENT;
     78  1.22     joerg 	if (memcmp(key, data, sizeof(key)) != 0)
     79  1.22     joerg 		return ENOENT;
     80  1.22     joerg 	data = (const char *)data + sizeof(key);
     81  1.22     joerg 	datalen -= sizeof(key);
     82  1.22     joerg 	if (memchr(data, '\0', datalen) != (const char *)data + datalen - 1)
     83  1.22     joerg 		return ENOENT;
     84  1.22     joerg 	if (datalen > len)
     85  1.22     joerg 		return ERANGE;
     86  1.22     joerg 	memcpy(path, data, datalen);
     87  1.22     joerg 	return 0;
     88  1.22     joerg }
     89  1.22     joerg 
     90  1.22     joerg static int
     91  1.22     joerg devname_ptslookup(dev_t dev, mode_t type, char *path, size_t len)
     92  1.22     joerg {
     93  1.22     joerg 	int rv;
     94  1.22     joerg 
     95  1.22     joerg 	if (type != S_IFCHR || pts == NODEVMAJOR || major(dev) != pts)
     96  1.22     joerg 		return ENOENT;
     97   1.9    simonb 
     98  1.22     joerg 	rv = snprintf(path, len, "%s%d", _PATH_DEV_PTS + sizeof(_PATH_DEV) - 1,
     99  1.22     joerg 	    minor(dev));
    100  1.22     joerg 	if (rv < 0 || (size_t)rv >= len)
    101  1.22     joerg 		return ERANGE;
    102  1.22     joerg 	return 0;
    103  1.22     joerg }
    104  1.22     joerg 
    105  1.22     joerg static int
    106  1.22     joerg devname_fts(dev_t dev, mode_t type, char *path, size_t len)
    107  1.22     joerg {
    108  1.22     joerg 	FTS *ftsp;
    109  1.22     joerg 	FTSENT *fe;
    110  1.22     joerg 	static const char path_dev[] = _PATH_DEV;
    111  1.22     joerg 	static char * const dirs[2] = { __UNCONST(path_dev), NULL };
    112  1.22     joerg 	const size_t len_dev = strlen(path_dev);
    113  1.22     joerg 	int rv;
    114  1.22     joerg 
    115  1.22     joerg 	if ((ftsp = fts_open(dirs, FTS_NOCHDIR | FTS_PHYSICAL, NULL)) == NULL)
    116  1.22     joerg 		return ENOENT;
    117  1.22     joerg 
    118  1.22     joerg 	rv = ENOENT;
    119  1.22     joerg 	while ((fe = fts_read(ftsp)) != NULL) {
    120  1.22     joerg 		if (fe->fts_info != FTS_DEFAULT)
    121  1.22     joerg 			continue;
    122  1.22     joerg 		if (fe->fts_statp->st_rdev != dev)
    123  1.22     joerg 			continue;
    124  1.22     joerg 		if ((type & S_IFMT) != (fe->fts_statp->st_mode & S_IFMT))
    125  1.22     joerg 			continue;
    126  1.22     joerg 		if (strncmp(fe->fts_path, path_dev, len_dev))
    127  1.22     joerg 			continue;
    128  1.22     joerg 		if (strlcpy(path, fe->fts_path + len_dev, len) < len) {
    129  1.22     joerg 			rv = 0;
    130  1.22     joerg 			break;
    131  1.22     joerg 		}
    132   1.1       cgd 	}
    133  1.22     joerg 
    134  1.22     joerg 	fts_close(ftsp);
    135  1.22     joerg 	return rv;
    136  1.22     joerg }
    137  1.22     joerg 
    138  1.22     joerg int
    139  1.22     joerg devname_r(dev_t dev, mode_t type, char *path, size_t len)
    140  1.22     joerg {
    141  1.22     joerg 	int rv;
    142  1.22     joerg 
    143  1.22     joerg 	thr_once(&db_opened, devname_dbopen);
    144  1.22     joerg 
    145  1.22     joerg 	if (db != NULL) {
    146  1.22     joerg 		rv = devname_dblookup(dev, type, path, len);
    147  1.22     joerg 		if (rv == 0 || rv == ERANGE)
    148  1.22     joerg 			return rv;
    149   1.9    simonb 	}
    150   1.1       cgd 
    151  1.22     joerg 	rv = devname_ptslookup(dev, type, path, len);
    152  1.22     joerg 	if (rv == 0 || rv == ERANGE)
    153  1.22     joerg 		return rv;
    154  1.22     joerg 
    155  1.22     joerg 	if (db != NULL)
    156  1.22     joerg 		return ENOENT;
    157  1.22     joerg 	rv = devname_fts(dev, type, path, len);
    158  1.22     joerg 	return rv;
    159  1.22     joerg }
    160   1.9    simonb 
    161  1.22     joerg char *
    162  1.22     joerg devname(dev_t dev, mode_t type)
    163  1.22     joerg {
    164  1.22     joerg 	static char path[PATH_MAX];
    165   1.9    simonb 
    166  1.22     joerg 	return devname_r(dev, type, path, sizeof(path)) == 0 ? path : NULL;
    167   1.1       cgd }
    168