1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
|
/*
* dfile.c - Linux file processing functions for /proc-based lsof
*/
/*
* Copyright 1997 Purdue Research Foundation, West Lafayette, Indiana
* 47907. All rights reserved.
*
* Written by Victor A. Abell
*
* This software is not subject to any license of the American Telephone
* and Telegraph Company or the Regents of the University of California.
*
* Permission is granted to anyone to use this software for any purpose on
* any computer system, and to alter it and redistribute it freely, subject
* to the following restrictions:
*
* 1. Neither the authors nor Purdue University are responsible for any
* consequences of the use of this software.
*
* 2. The origin of this software must not be misrepresented, either by
* explicit claim or by omission. Credit to the authors and Purdue
* University must appear in documentation and sources.
*
* 3. Altered versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 4. This notice may not be removed or altered.
*/
#ifndef lint
static char copyright[] =
"@(#) Copyright 1997 Purdue Research Foundation.\nAll rights reserved.\n";
static char *rcsid = "$Id: dfile.c,v 1.8 2012/04/10 16:39:50 abe Exp abe $";
#endif
#include "lsof.h"
/*
* Local structures
*/
struct hsfile {
struct sfile *s; /* the Sfile table address */
struct hsfile *next; /* the next hash bucket entry */
};
/*
* Local static variables
*/
static struct hsfile *HbyFdi = /* hash by file (dev,ino) buckets */
(struct hsfile *)NULL;
static int HbyFdiCt = 0; /* HbyFdi entry count */
static struct hsfile *HbyFrd = /* hash by file raw device buckets */
(struct hsfile *)NULL;
static int HbyFrdCt = 0; /* HbyFrd entry count */
static struct hsfile *HbyFsd = /* hash by file system buckets */
(struct hsfile *)NULL;
static int HbyFsdCt = 0; /* HbyFsd entry count */
static struct hsfile *HbyNm = /* hash by name buckets */
(struct hsfile *)NULL;
static int HbyNmCt = 0; /* HbyNm entry count */
/*
* Local definitions
*/
#define SFDIHASH 4094 /* Sfile hash by (device,inode) number
* pair bucket count (power of 2!) */
#define SFFSHASH 1024 /* Sfile hash by file system device
* number bucket count (power of 2!) */
#define SFHASHDEVINO(maj, min, ino, mod) ((int)(((int)((((int)(maj+1))*((int)((min+1))))+ino)*31415)&(mod-1)))
/* hash for Sfile by major device,
* minor device, and inode, modulo mod
* (mod must be a power of 2) */
#define SFRDHASH 1024 /* Sfile hash by raw device number
* bucket count (power of 2!) */
#define SFHASHRDEVI(maj, min, rmaj, rmin, ino, mod) ((int)(((int)((((int)(maj+1))*((int)((min+1))))+((int)(rmaj+1)*(int)(rmin+1))+ino)*31415)&(mod-1)))
/* hash for Sfile by major device,
* minor device, major raw device,
* minor raw device, and inode, modulo
* mod (mod must be a power of 2) */
#define SFNMHASH 4096 /* Sfile hash by name bucket count
* (must be a power of 2!) */
/*
* hashSfile() - hash Sfile entries for use in is_file_named() searches
*/
void
hashSfile()
{
static int hs = 0;
int i;
struct sfile *s;
struct hsfile *sh, *sn;
/*
* Do nothing if there are no file search arguments cached or if the
* hashes have already been constructed.
*/
if (!Sfile || hs)
return;
/*
* Allocate hash buckets by (device,inode), file system device, and file name.
*/
if (!(HbyFdi = (struct hsfile *)calloc((MALLOC_S)SFDIHASH,
sizeof(struct hsfile))))
{
(void) fprintf(stderr,
"%s: can't allocate space for %d (dev,ino) hash buckets\n",
Pn, SFDIHASH);
Exit(1);
}
if (!(HbyFrd = (struct hsfile *)calloc((MALLOC_S)SFRDHASH,
sizeof(struct hsfile))))
{
(void) fprintf(stderr,
"%s: can't allocate space for %d rdev hash buckets\n",
Pn, SFRDHASH);
Exit(1);
}
if (!(HbyFsd = (struct hsfile *)calloc((MALLOC_S)SFFSHASH,
sizeof(struct hsfile))))
{
(void) fprintf(stderr,
"%s: can't allocate space for %d file sys hash buckets\n",
Pn, SFFSHASH);
Exit(1);
}
if (!(HbyNm = (struct hsfile *)calloc((MALLOC_S)SFNMHASH,
sizeof(struct hsfile))))
{
(void) fprintf(stderr,
"%s: can't allocate space for %d name hash buckets\n",
Pn, SFNMHASH);
Exit(1);
}
hs++;
/*
* Scan the Sfile chain, building file, file system, raw device, and file
* name hash bucket chains.
*/
for (s = Sfile; s; s = s->next) {
for (i = 0; i < 3; i++) {
switch (i) {
case 0: /* hash by name */
if (!s->aname)
continue;
sh = &HbyNm[hashbyname(s->aname, SFNMHASH)];
HbyNmCt++;
break;
case 1: /* hash by device and inode, or file
* system device */
if (s->type) {
sh = &HbyFdi[SFHASHDEVINO(GET_MAJ_DEV(s->dev),
GET_MIN_DEV(s->dev), s->i,
SFDIHASH)];
HbyFdiCt++;
} else {
sh = &HbyFsd[SFHASHDEVINO(GET_MAJ_DEV(s->dev),
GET_MIN_DEV(s->dev),
0,
SFFSHASH)];
HbyFsdCt++;
}
break;
case 2: /* hash by file's raw device */
if ((s->mode == S_IFCHR) || (s->mode == S_IFBLK)) {
sh = &HbyFrd[SFHASHRDEVI(GET_MAJ_DEV(s->dev),
GET_MIN_DEV(s->dev),
GET_MAJ_DEV(s->rdev),
GET_MIN_DEV(s->rdev),
s->i,
SFRDHASH)];
HbyFrdCt++;
} else
continue;
}
/*
* Add hash to the bucket's chain, allocating new entries for
* all after the first.
*/
if (!sh->s) {
sh->s = s;
sh->next = (struct hsfile *)NULL;
continue;
} else {
if (!(sn = (struct hsfile *)malloc(
(MALLOC_S)sizeof(struct hsfile))))
{
(void) fprintf(stderr,
"%s: can't allocate hsfile bucket for: %s\n",
Pn, s->aname);
Exit(1);
}
sn->s = s;
sn->next = sh->next;
sh->next = sn;
}
}
}
}
/*
* is_file_named() - is this file named?
*/
int
is_file_named(ty, p, mp, cd)
int ty; /* search type: 0 = only by device
* and inode
* 1 = by device and
* inode, or by file
* system device and
* path for NFS file
* systems
* 2 = only by path
*/
char *p; /* path name (device and inode are
* identified via *Lf) */
struct mounts *mp; /* NFS file system (NULL if not) */
int cd; /* character or block type file --
* VCHR or VBLK vnode, or S_IFCHR
* or S_IFBLK inode */
{
char *ep;
int f = 0;
struct mounts *smp;
struct sfile *s = (struct sfile *)NULL;
struct hsfile *sh;
size_t sz;
/*
* Check for a path name match, as requested.
*/
if ((ty == 2) && p && HbyNmCt) {
for (sh = &HbyNm[hashbyname(p, SFNMHASH)]; sh; sh = sh->next) {
if ((s = sh->s) && strcmp(p, s->aname) == 0) {
f = 2;
break;
}
}
}
/*
* Check for a regular file by device and inode number.
*/
if (!f && (ty < 2) && HbyFdiCt && Lf->dev_def
&& (Lf->inp_ty == 1 || Lf->inp_ty == 3))
{
for (sh = &HbyFdi[SFHASHDEVINO(GET_MAJ_DEV(Lf->dev),
GET_MIN_DEV(Lf->dev),
Lf->inode,
SFDIHASH)];
sh;
sh = sh->next)
{
if ((s = sh->s) && (Lf->dev == s->dev)
&& (Lf->inode == s->i)) {
f = 1;
break;
}
}
}
/*
* Check for a file system match.
*/
if (!f && (ty == 1) && HbyFsdCt && Lf->dev_def) {
for (sh = &HbyFsd[SFHASHDEVINO(GET_MAJ_DEV(Lf->dev),
GET_MIN_DEV(Lf->dev), 0,
SFFSHASH)];
sh;
sh = sh->next)
{
if ((s = sh->s) && (s->dev == Lf->dev)) {
if (Lf->ntype != N_NFS) {
/*
* A non-NFS file matches to a non-NFS file system by
* device.
*/
if (!(smp = s->mp) || (smp->ty != N_NFS)) {
f = 1;
break;
}
} else {
/*
* An NFS file must also match to a file system by the
* the path name of the file system -- i.e., the first
* part of the file's path. This terrible, non-UNIX
* hack is forced on lsof by an egregious error in
* Linux NFS that can assign the same device number
* to two different NFS mounts.
*/
if (p && mp && mp->dirl && mp->dir && s->name
&& !strncmp(mp->dir, s->name, mp->dirl))
{
f = 1;
break;
}
}
}
}
}
/*
* Check for a character or block device match.
*/
if (!f && !ty && HbyFrdCt && cd
&& Lf->dev_def && (Lf->dev == DevDev)
&& Lf->rdev_def
&& (Lf->inp_ty == 1 || Lf->inp_ty == 3))
{
for (sh = &HbyFrd[SFHASHRDEVI(GET_MAJ_DEV(Lf->dev),
GET_MIN_DEV(Lf->dev),
GET_MAJ_DEV(Lf->rdev),
GET_MIN_DEV(Lf->rdev),
Lf->inode, SFRDHASH)];
sh;
sh = sh->next)
{
if ((s = sh->s) && (s->dev == Lf->dev)
&& (s->rdev == Lf->rdev) && (s->i == Lf->inode))
{
f = 1;
break;
}
}
}
/*
* Convert the name if a match occurred.
*/
switch (f) {
case 0:
return(0);
case 1:
if (s->type) {
/*
* If the search argument isn't a file system, propagate it
* to Namech[]; otherwise, let printname() compose the name.
*/
(void) snpf(Namech, Namechl, "%s", s->name);
if (s->devnm) {
ep = endnm(&sz);
(void) snpf(ep, sz, " (%s)", s->devnm);
}
}
break;
case 2:
(void) strcpy(Namech, p);
break;
}
if (s)
s->f = 1;
return(1);
}
/*
* printdevname() - print character device name
*
* Note: this function should not be needed in /proc-based lsof, but
* since it is called by printname() in print.c, an ersatz one
* is provided here.
*/
int
printdevname(dev, rdev, f, nty)
dev_t *dev; /* device */
dev_t *rdev; /* raw device */
int f; /* 1 = follow with '\n' */
int nty; /* node type: N_BLK or N_chr */
{
char buf[128];
(void) snpf(buf, sizeof(buf), "%s device: %d,%d",
(nty == N_BLK) ? "BLK" : "CHR",
(int)GET_MAJ_DEV(*rdev), (int)GET_MIN_DEV(*rdev));
safestrprt(buf, stdout, f);
return(1);
}
|