File: dfile.c

package info (click to toggle)
lsof 4.99.4%2Bdfsg-2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,924 kB
  • sloc: ansic: 50,680; sh: 8,351; makefile: 1,194; perl: 940; awk: 214
file content (347 lines) | stat: -rw-r--r-- 12,170 bytes parent folder | download
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
/*
 * 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.
 */

#include "common.h"

/*
 * Local structures
 */

/*
 * Local static variables
 */


/*
 * 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!) */
/* hash for Sfile by major device,
 * minor device, and inode, modulo mod
 * (mod must be a power of 2) */
#define SFHASHDEVINO(maj, min, ino, mod)                                       \
    ((int)(((int)((((int)(maj + 1)) * ((int)((min + 1)))) + ino) * 31415) &    \
           (mod - 1)))
#define SFRDHASH                                                               \
    1024 /* Sfile hash by raw device number                                    \
          * bucket count (power of 2!) */
/* 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 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)))
#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(struct lsof_context *ctx) {
    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);
        Error(ctx);
    }
    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);
        Error(ctx);
    }
    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);
        Error(ctx);
    }
    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);
        Error(ctx);
    }
    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);
                    Error(ctx);
                }
                sn->s = s;
                sn->next = sh->next;
                sh->next = sn;
            }
        }
    }
}

/*
 * is_file_named() - is this file named?
 */
int is_file_named(
    /* context */
    struct lsof_context *ctx,
    /* 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
     */
    int search_type,
    /* path name (device and inode are
     * identified via *Lf) */
    char *path,
    /* NFS file system (NULL if not) */
    struct mounts *nfs_mount,
    /* character or block type file --
     * VCHR or VBLK vnode, or S_IFCHR
     * or S_IFBLK inode */
    int cd) {
    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 ((search_type == 2) && path && HbyNmCt) {
        for (sh = &HbyNm[hashbyname(path, SFNMHASH)]; sh; sh = sh->next) {
            if ((s = sh->s) && strcmp(path, s->aname) == 0) {
                f = 2; /* Found match by path */
                break;
            }
        }
    }

    /*
     * Check for a regular file by device and inode number.
     */
    if (!f && (search_type < 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; /* Found match by inode and dev */
                break;
            }
        }
    }

    /*
     * Check for a file system match.
     */
    if (!f && (search_type == 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; /* Found match by fs */
                        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 (path && nfs_mount && nfs_mount->dirl &&
                        nfs_mount->dir && s->name &&
                        !strncmp(nfs_mount->dir, s->name, nfs_mount->dirl)) {
                        f = 1; /* Found match by fs */
                        break;
                    }
                }
            }
        }
    }

    /*
     * Check for a character or block device match.
     */
    if (!f && !search_type && 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; /* Found match by inode and dev */
                break;
            }
        }
    }
    /*
     * Convert the name if a match occurred.
     */
    switch (f) {
    case 0: /* Not found */
        return (0);
    case 1: /* Found match by inode and dev or fs */
        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(ctx, &sz);
                (void)snpf(ep, sz, " (%s)", s->devnm);
            }
        }
        break;
    case 2: /* Found match by path */
        (void)strcpy(Namech, path);
        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(struct lsof_context *ctx, /* context */
                 dev_t *dev,               /* device */
                 dev_t *rdev,              /* raw device */
                 int newline,              /* 1 = follow with '\n' */
                 int node_type)            /* node type: N_BLK or N_chr */
{
    char buf[128];

    (void)snpf(buf, sizeof(buf), "%s device: %d,%d",
               (node_type == N_BLK) ? "BLK" : "CHR", (int)GET_MAJ_DEV(*rdev),
               (int)GET_MIN_DEV(*rdev));
    safestrprt(buf, stdout, newline);
    return (1);
}