devname.c revision 65955
12786Ssos/* 22786Ssos * Copyright (c) 1989, 1993 32786Ssos * The Regents of the University of California. All rights reserved. 42786Ssos * 52786Ssos * Redistribution and use in source and binary forms, with or without 62786Ssos * modification, are permitted provided that the following conditions 72786Ssos * are met: 82786Ssos * 1. Redistributions of source code must retain the above copyright 92786Ssos * notice, this list of conditions and the following disclaimer. 102786Ssos * 2. Redistributions in binary form must reproduce the above copyright 112786Ssos * notice, this list of conditions and the following disclaimer in the 122786Ssos * documentation and/or other materials provided with the distribution. 132786Ssos * 3. All advertising materials mentioning features or use of this software 142786Ssos * must display the following acknowledgement: 152786Ssos * This product includes software developed by the University of 162786Ssos * California, Berkeley and its contributors. 172786Ssos * 4. Neither the name of the University nor the names of its contributors 182786Ssos * may be used to endorse or promote products derived from this software 192786Ssos * without specific prior written permission. 202786Ssos * 212786Ssos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 222786Ssos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 232786Ssos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 242786Ssos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 252786Ssos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 262786Ssos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 272786Ssos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 282786Ssos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 292786Ssos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 302786Ssos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 312786Ssos * SUCH DAMAGE. 322786Ssos * 332786Ssos * $FreeBSD: head/lib/libc/gen/devname.c 65955 2000-09-16 21:58:53Z phk $ 342786Ssos */ 352786Ssos 362786Ssos#if defined(LIBC_SCCS) && !defined(lint) 372786Ssosstatic char sccsid[] = "@(#)devname.c 8.2 (Berkeley) 4/29/95"; 382786Ssos#endif /* LIBC_SCCS and not lint */ 392786Ssos 402786Ssos#include <sys/types.h> 412786Ssos#include <sys/sysctl.h> 422786Ssos 432786Ssos#include <db.h> 442786Ssos#include <err.h> 452786Ssos#include <errno.h> 462786Ssos#include <fcntl.h> 472786Ssos#include <paths.h> 482786Ssos#include <stdio.h> 492786Ssos#include <string.h> 502786Ssos#include <sys/param.h> 512786Ssos#include <sys/stat.h> 522786Ssos 532786Ssosstatic char * 542786Ssosxdevname(dev, type) 552786Ssos dev_t dev; 562786Ssos mode_t type; 572786Ssos{ 582786Ssos struct { 592786Ssos mode_t type; 602786Ssos dev_t dev; 612786Ssos } bkey; 622786Ssos static DB *db; 632786Ssos static int failure; 642786Ssos DBT data, key; 652786Ssos 662786Ssos if (!db && !failure && 672786Ssos !(db = dbopen(_PATH_DEVDB, O_RDONLY, 0, DB_HASH, NULL))) 682786Ssos failure = 1; 692786Ssos if (failure) 702786Ssos return (NULL); 712786Ssos 722786Ssos /* 732786Ssos * Keys are a mode_t followed by a dev_t. The former is the type of 742786Ssos * the file (mode & S_IFMT), the latter is the st_rdev field. Be 752786Ssos * sure to clear any padding that may be found in bkey. 762786Ssos */ 772786Ssos memset(&bkey, 0, sizeof(bkey)); 782786Ssos bkey.dev = dev; 792786Ssos bkey.type = type; 802786Ssos key.data = &bkey; 815994Ssos key.size = sizeof(bkey); 822786Ssos return ((db->get)(db, &key, &data, 0) ? NULL : (char *)data.data); 832786Ssos} 842786Ssos 852786Ssoschar * 862786Ssosdevname(dev, type) 872786Ssos dev_t dev; 885994Ssos mode_t type; 892786Ssos{ 902786Ssos static char buf[SPECNAMELEN + 1]; 912786Ssos int i, j; 922786Ssos char *r; 932786Ssos 942786Ssos /* First check the DB file. */ 952786Ssos r = xdevname(dev, type); 962786Ssos if (r != NULL) 972786Ssos return (r); 982786Ssos 992786Ssos /* Then ask the kernel. */ 1002786Ssos if ((type & S_IFMT) == S_IFCHR) { 1012786Ssos j = sizeof(buf); 1022786Ssos i = sysctlbyname("kern.devname", buf, &j, &dev, sizeof (dev)); 1032786Ssos if (i == 0) 1042786Ssos return (buf); 1052786Ssos } 1062786Ssos 1072786Ssos /* Finally just format it */ 1085994Ssos if (minor(dev) > 255) 1092786Ssos r = "#%c:%d:0x%x"; 1105994Ssos else 1115994Ssos r = "#%c:%d:0x%d"; 1125994Ssos snprintf(buf, SPECNAMELEN + 1, r, 113 (type & S_IFMT) == S_IFCHR ? 'C' : 'B', major(dev), minor(dev)); 114 return (buf); 115} 116