1/* 2 * pdvn.c -- print device name functions for lsof library 3 */ 4 5 6/* 7 * Copyright 1997 Purdue Research Foundation, West Lafayette, Indiana 8 * 47907. All rights reserved. 9 * 10 * Written by Victor A. Abell 11 * 12 * This software is not subject to any license of the American Telephone 13 * and Telegraph Company or the Regents of the University of California. 14 * 15 * Permission is granted to anyone to use this software for any purpose on 16 * any computer system, and to alter it and redistribute it freely, subject 17 * to the following restrictions: 18 * 19 * 1. Neither the authors nor Purdue University are responsible for any 20 * consequences of the use of this software. 21 * 22 * 2. The origin of this software must not be misrepresented, either by 23 * explicit claim or by omission. Credit to the authors and Purdue 24 * University must appear in documentation and sources. 25 * 26 * 3. Altered versions must be plainly marked as such, and must not be 27 * misrepresented as being the original software. 28 * 29 * 4. This notice may not be removed or altered. 30 */ 31 32 33#include "../machine.h" 34 35#if defined(USE_LIB_PRINTDEVNAME) 36 37# if !defined(lint) 38static char copyright[] = 39"@(#) Copyright 1997 Purdue Research Foundation.\nAll rights reserved.\n"; 40static char *rcsid = "$Id: pdvn.c,v 1.8 2008/10/21 16:12:36 abe Exp $"; 41# endif /* !defined(lint) */ 42 43#include "../lsof.h" 44 45#else /* !defined(USE_LIB_PRINTDEVNAME) */ 46char pdvn_d1[] = "d"; char *pdvn_d2 = pdvn_d1; 47#endif /* defined(USE_LIB_PRINTDEVNAME) */ 48 49 50/* 51 * To use this source file: 52 * 53 * 1. Define USE_LIB_PRINTDEVNAME, or both. 54 * 55 * 2. Define HAS_STD_CLONE to enable standard clone searches in 56 * printdevname(). 57 * 58 * 3. Define HASBLDKDEV to enable block device processing. 59 */ 60 61 62/* 63 * Local definitions 64 */ 65 66#define LIKE_BLK_SPEC "like block special" 67#define LIKE_CHR_SPEC "like character special" 68 69 70# if defined(USE_LIB_PRINTDEVNAME) 71/* 72 * printdevname() - print block or character device name 73 */ 74 75int 76printdevname(dev, rdev, f, nty) 77 dev_t *dev; /* device */ 78 dev_t *rdev; /* raw device */ 79 int f; /* 1 = print trailing '\n' */ 80 int nty; /* node type: N_BLK or N_CHR */ 81{ 82 83# if defined(HAS_STD_CLONE) 84 struct clone *c; 85# endif /* defined(HAS_STD_CLONE) */ 86 87 struct l_dev *dp; 88 int r = 1; 89 90# if defined(HASDCACHE) 91 92printdevname_again: 93 94# endif /* defined(HASDCACHE) */ 95 96# if defined(HAS_STD_CLONE) 97/* 98 * Search for clone if this is a character device on the same device as 99 * /dev (or /devices). 100 */ 101 if ((nty == N_CHR) && Lf->is_stream && Clone && (*dev == DevDev)) { 102 r = 0; /* Don't let lkupdev() rebuild the device cache, 103 * because when it has been rebuilt we want to 104 * search again for clones. */ 105 readdev(0); 106 for (c = Clone; c; c = c->next) { 107 if (GET_MAJ_DEV(*rdev) == GET_MIN_DEV(Devtp[c->dx].rdev)) { 108 109# if defined(HASDCACHE) 110 if (DCunsafe && !Devtp[c->dx].v && !vfy_dev(&Devtp[c->dx])) 111 goto printdevname_again; 112# endif /* defined(HASDCACHE) */ 113 114 safestrprt(Devtp[c->dx].name, stdout, f); 115 return(1); 116 } 117 } 118 } 119# endif /* defined(HAS_STD_CLONE) */ 120 121/* 122 * Search appropriate device table for a full match. 123 */ 124 125# if defined(HASBLKDEV) 126 if (nty == N_BLK) 127 dp = lkupbdev(dev, rdev, 1, r); 128 else 129# endif /* defined(HASBLKDEV) */ 130 131 dp = lkupdev(dev, rdev, 1, r); 132 if (dp) { 133 safestrprt(dp->name, stdout, f); 134 return(1); 135 } 136/* 137 * Search device table for a match without inode number and dev. 138 */ 139 140# if defined(HASBLKDEV) 141 if (nty == N_BLK) 142 dp = lkupbdev(&DevDev, rdev, 0, r); 143 else 144# endif /* defined(HASBLKDEV) */ 145 146 dp = lkupdev(&DevDev, rdev, 0, r); 147 if (dp) { 148 /* 149 * A match was found. Record it as a name column addition. 150 */ 151 char *cp, *ttl; 152 int len; 153 154 ttl = (nty == N_BLK) ? LIKE_BLK_SPEC : LIKE_CHR_SPEC; 155 len = (int)(1 + strlen(ttl) + 1 + strlen(dp->name) + 1); 156 if (!(cp = (char *)malloc((MALLOC_S)(len + 1)))) { 157 (void) fprintf(stderr, "%s: no nma space for: (%s %s)\n", 158 Pn, ttl, dp->name); 159 Exit(1); 160 } 161 (void) snpf(cp, len + 1, "(%s %s)", ttl, dp->name); 162 (void) add_nma(cp, len); 163 (void) free((MALLOC_P *)cp); 164 return(0); 165 } 166 167# if defined(HASDCACHE) 168/* 169 * We haven't found a match. 170 * 171 * If rebuilding the device cache was suppressed and the device cache is 172 * "unsafe," rebuild it. 173 */ 174 if (!r && DCunsafe) { 175 (void) rereaddev(); 176 goto printdevname_again; 177 } 178# endif /* defined(HASDCACHE) */ 179 180 return(0); 181} 182#endif /* defined(USE_LIB_PRINTDEVNAME) */ 183