File: dproc.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 (576 lines) | stat: -rw-r--r-- 16,234 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
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/*
 * dproc.c - NetBSD process access functions for lsof
 */

/*
 * Copyright 1994 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 1994 Purdue Research Foundation.\nAll rights reserved.\n";
#endif

#include "common.h"

#if __NetBSD_Version__ >= 499006200
/*
 * In NetBSD-4.99.62, struct fdfile was added, struct filedesc::fd_ofiles
 * changed type from struct file ** to struct fdfile **, and
 * fd_ofileflags disappeared from struct filedesc, being
 * replaced by fields in struct fdfile.
 */
#    define HAVE_STRUCT_FDFILE 1
#    define FILESTRUCT struct fdfile
#else
#    undef HAVE_STRUCT_FDFILE
#    define FILESTRUCT struct file
#endif
#if __NetBSD_Version__ >= 599001400
/*
 * Between NetBSD-5.99.13 and 5.99.14, struct fdtab was added, and
 * struct filedesc::fd_ofiles and fd_nfiles were replaced by
 * struct filedesc::fd_dt (a pointer to struct fdtab).
 */
#    define HAVE_STRUCT_FDTAB 1
#    define NFILES(fd, dt) ((dt).dt_nfiles)
#    define OFILES(fd, dt) ((fd).fd_dt->dt_ff)
#else
#    undef HAVE_STRUCT_FDTAB
#    define NFILES(fd, dt) ((fd).fd_nfiles)
#    define OFILES(fd, dt) ((fd).fd_ofiles)
#endif

static void enter_vn_text(struct lsof_context *ctx, KA_T va, int *n);
static void get_kernel_access(struct lsof_context *ctx);
static void process_text(struct lsof_context *ctx, KA_T vm);

/*
 * Local static values
 */

static MALLOC_S Nv = 0; /* allocated Vp[] entries */
static KA_T *Vp = NULL; /* vnode address cache */

/*
 * ckkv - check kernel version
 */

void ckkv(struct lsof_context *ctx, /* context */
          char *d,                  /* dialect */
          char *er,                 /* expected release */
          char *ev,                 /* expected version */
          char *ea)                 /* expected architecture */
{

#if defined(HASKERNIDCK)
    size_t l;
    int m[2];
    char v[64];

    if (Fwarn)
        return;
    /*
     * Read kernel version.
     */
    m[0] = CTL_KERN;
    m[1] = KERN_OSRELEASE;
    l = sizeof(v);
    if (sysctl(m, 2, v, &l, NULL, 0) < 0) {
        (void)fprintf(stderr, "%s: CTL_KERN, KERN_OSRELEASE: %s\n", Pn,
                      strerror(errno));
        Error(ctx);
    }
    /*
     * Warn if the actual and expected releases don't match.
     */
    if (!er || strcmp(v, er))
        (void)fprintf(stderr,
                      "%s: WARNING: compiled for %s release %s; this is %s.\n",
                      Pn, d, er ? er : "UNKNOWN", v);
#endif /* defined(HASKERNIDCK) */
}

/*
 * enter_vn_text() - enter a vnode text reference
 */

static void enter_vn_text(struct lsof_context *ctx, /* context */
                          KA_T va,                  /* vnode address */
                          int *n)                   /* Vp[] entries in use */
{
    int i;
    /*
     * Ignore the request if the vnode has already been entered.
     */
    for (i = 0; i < *n; i++) {
        if (va == Vp[i])
            return;
    }
    /*
     * Save the text file information.
     */
    alloc_lfile(ctx, LSOF_FD_PROGRAM_TEXT, -1);
    Cfp = (struct file *)NULL;
    process_node(ctx, (KA_T)va);
    if (Lf->sf)
        link_lfile(ctx);
    if (i >= Nv) {

        /*
         * Allocate space for remembering the vnode.
         */
        Nv += 10;
        if (!Vp)
            Vp = (KA_T *)malloc((MALLOC_S)(sizeof(struct vnode *) * 10));
        else
            Vp = (KA_T *)realloc((MALLOC_P *)Vp, (MALLOC_S)(Nv * sizeof(KA_T)));
        if (!Vp) {
            (void)fprintf(stderr, "%s: no txt ptr space, PID %d\n", Pn,
                          Lp->pid);
            Error(ctx);
        }
    }
    /*
     * Remember the vnode.
     */
    Vp[*n] = va;
    (*n)++;
}

/*
 * gather_proc_info() -- gather process information
 */

void gather_proc_info(struct lsof_context *ctx) {
    struct filedesc fd;
    int i, nf;
    MALLOC_S nb;
    static FILESTRUCT **ofb = NULL;
    static int ofbb = 0;
    short pss, sf;
    int px;
    uid_t uid;

#if defined(HASCWDINFO)
    struct cwdinfo cw;
#    define CDIR cw.cwdi_cdir
#    define RDIR cw.cwdi_rdir
#else /* !defined(HASCWDINFO) */
#    define CDIR fd.fd_cdir
#    define RDIR fd.fd_rdir
#endif /* defined(HASCWDINFO) */

#if defined(HASFSTRUCT)
    static char *pof = (char *)NULL;
    static int pofb = 0;
#endif /* defined(HASFSTRUCT) */

#if defined(HASKVMGETPROC2)
    struct kinfo_proc2 *p;
#    define KVMPROCSZ2 sizeof(struct kinfo_proc2)
#else  /* !defined(HASKVMGETPROC2) */
    struct kinfo_proc *p;
#endif /* defined(HASKVMGETPROC2) */

#if HAVE_STRUCT_FDTAB
    struct fdtab dt;
#endif /* HAVE_STRUCT_FDTAB */

    /*
     * Read the process table.
     */

#if defined(HASKVMGETPROC2)
    P = kvm_getproc2(Kd, KERN_PROC_ALL, 0, KVMPROCSZ2, &Np);
#else  /* !defined(HASKVMGETPROC2) */
    P = kvm_getprocs(Kd, KERN_PROC_ALL, 0, &Np);
#endif /* defined(HASKVMGETPROC2) */

    if (!P) {
        (void)fprintf(stderr, "%s: can't read process table: %s\n", Pn,
                      kvm_geterr(Kd));
        Error(ctx);
    }
    /*
     * Examine proc structures and their associated information.
     */

    for (p = P, px = 0; px < Np; px++, p++) {
        if (p->P_STAT == 0 || p->P_STAT == SZOMB)
            continue;
        /*
         * Read process information, process group structure (if
         * necessary), and User ID (if necessary).
         *
         * See if process is excluded.
         *
         * Read file structure pointers.
         */
        uid = p->P_UID;
        if (is_proc_excl(ctx, (int)p->P_PID, (int)p->P_PGID, (UID_ARG)uid, &pss,
                         &sf)) {
            continue;
        }
        if (!p->P_FD || kread(ctx, (KA_T)p->P_FD, (char *)&fd, sizeof(fd)))
            continue;
        if (!fd.fd_refcnt)
            continue;
#if HAVE_STRUCT_FDTAB
        if (!fd.fd_dt || kread(ctx, (KA_T)fd.fd_dt, (char *)&dt, sizeof(dt)))
            continue;
#endif /* ! HAVE_STRUCT_FDTAB */
        if (fd.fd_lastfile > NFILES(fd, dt))
            continue;

#if defined(HASCWDINFO)
        if (!p->P_CWDI || kread(ctx, (KA_T)p->P_CWDI, (char *)&cw, sizeof(cw)))
            CDIR = RDIR = (struct vnode *)NULL;
#endif /* defined(HASCWDINFO) */

        /*
         * Allocate a local process structure.
         */
        if (is_cmd_excl(ctx, p->P_COMM, &pss, &sf))
            continue;
        alloc_lproc(ctx, (int)p->P_PID, (int)p->P_PGID, (int)p->P_PPID,
                    (UID_ARG)uid, p->P_COMM, (int)pss, (int)sf);
        Plf = (struct lfile *)NULL;
        Kpa = (KA_T)p->P_ADDR;
        /*
         * Save current working directory information.
         */
        if (CDIR) {
            alloc_lfile(ctx, LSOF_FD_CWD, -1);
            Cfp = (struct file *)NULL;
            process_node(ctx, (KA_T)CDIR);
            if (Lf->sf)
                link_lfile(ctx);
        }
        /*
         * Save root directory information.
         */
        if (RDIR) {
            alloc_lfile(ctx, LSOF_FD_ROOT_DIR, -1);
            Cfp = (struct file *)NULL;
            process_node(ctx, (KA_T)RDIR);
            if (Lf->sf)
                link_lfile(ctx);
        }

        /*
         * Save information on the text file.
         */
        if (p->P_VMSPACE)
            process_text(ctx, (KA_T)p->P_VMSPACE);
        /*
         * Read open file structure pointers.
         */
        if (!OFILES(fd, dt) || (nf = NFILES(fd, dt)) <= 0)
            continue;
        nb = (MALLOC_S)(sizeof(FILESTRUCT *) * nf);
        if (nb > ofbb) {
            if (!ofb)
                ofb = (FILESTRUCT **)malloc(nb);
            else
                ofb = (FILESTRUCT **)realloc((MALLOC_P *)ofb, nb);
            if (!ofb) {
                (void)fprintf(stderr, "%s: PID %d, no file * space\n", Pn,
                              p->P_PID);
                Error(ctx);
            }
            ofbb = nb;
        }
        if (kread(ctx, (KA_T)OFILES(fd, dt), (char *)ofb, nb))
            continue;

#if defined(HASFSTRUCT)
        nb = (MALLOC_S)(sizeof(char) * nf);
        if (nb > pofb) {
            if (!pof)
                pof = (char *)malloc(nb);
            else
                pof = (char *)realloc((MALLOC_P *)pof, nb);
            if (!pof) {
                (void)fprintf(stderr, "%s: PID %d, no file flag space\n", Pn,
                              p->P_PID);
                Error(ctx);
            }
            pofb = nb;
        }
#    if !HAVE_STRUCT_FDFILE
        if (!fd.fd_ofileflags || kread(ctx, (KA_T)fd.fd_ofileflags, pof, nb))
            zeromem(pof, nb);
#    endif /* ! HAVE_STRUCT_FDFILE */
#endif     /* defined(HASFSTRUCT) */

        /*
         * Save information on file descriptors.
         */
        for (i = 0; i < nf; i++) {
            if (ofb[i]) {
#if HAVE_STRUCT_FDFILE
                struct fdfile fdf;
                if (kread(ctx, (KA_T)ofb[i], (char *)&fdf, sizeof(fdf)))
                    continue;
                Cfp = fdf.ff_file;
                if (Cfp == NULL)
                    continue;
                if (pof)
                    pof[i] = fdf.ff_exclose;
#else  /* ! HAVE_STRUCT_FDFILE */
                Cfp = ofb[i];
#endif /* ! HAVE_STRUCT_FDFILE */
                alloc_lfile(ctx, LSOF_FD_NUMERIC, i);
                process_file(ctx, (KA_T)Cfp);
                if (Lf->sf) {

#if defined(HASFSTRUCT)
                    Lf->pof = (long)pof[i];
#endif /* defined(HASFSTRUCT) */

                    link_lfile(ctx);
                }
            }
        }
        /*
         * Examine results.
         */
        if (examine_lproc(ctx))
            return;
    }
}

/*
 * get_kernel_access() - get access to kernel memory
 */

static void get_kernel_access(struct lsof_context *ctx) {
    KA_T v;
    /*
     * Check kernel version.
     */
    (void)ckkv(ctx, "NetBSD", LSOF_VSTR, (char *)NULL, (char *)NULL);
    /*
     * Set name list file path.
     */
    if (!Nmlst)

#if defined(N_UNIX)
        Nmlst = N_UNIX;
#else  /* !defined(N_UNIX) */
    {
        if (!(Nmlst = get_nlist_path(ctx, 1))) {
            (void)fprintf(stderr, "%s: can't get kernel name list path\n", Pn);
            Error(ctx);
        }
    }
#endif /* defined(N_UNIX) */

#if defined(WILLDROPGID)
    /*
     * If kernel memory isn't coming from KMEM, drop setgid permission
     * before attempting to open the (Memory) file.
     */
    if (Memory)
        (void)dropgid(ctx);
#else  /* !defined(WILLDROPGID) */
    /*
     * See if the non-KMEM memory and name list files are readable.
     */
    if ((Memory && !is_readable(Memory, 1)) ||
        (Nmlst && !is_readable(Nmlst, 1)))
        Error(ctx);
#endif /* defined(WILLDROPGID) */

    /*
     * Open kernel memory access.
     */
    if ((Kd = kvm_openfiles(Nmlst, Memory, NULL, O_RDONLY, NULL)) == NULL) {
        (void)fprintf(stderr,
                      "%s: kvm_openfiles(execfile=%s, corefile=%s): %s\n", Pn,
                      Nmlst,
                      Memory ? Memory :

#if defined(_PATH_MEM)
                             _PATH_MEM,
#else  /* !defined(_PATH_MEM) */
                             "default",
#endif /* defined(_PATH_MEM) */

                      strerror(errno));
        Error(ctx);
    }
    (void)build_Nl(ctx, Drive_Nl);
    if (kvm_nlist(Kd, Nl) < 0) {
        (void)fprintf(stderr, "%s: can't read namelist from %s\n", Pn, Nmlst);
        Error(ctx);
    }

#if defined(WILLDROPGID)
    /*
     * Drop setgid permission, if necessary.
     */
    if (!Memory)
        (void)dropgid(ctx);
#endif /* defined(WILLDROPGID) */

    /*
     * Read the kernel's page shift amount, if possible.
     */
    if (get_Nl_value(ctx, "pgshift", Drive_Nl, &v) < 0 || !v ||
        kread(ctx, (KA_T)v, (char *)&pgshift, sizeof(pgshift)))
        pgshift = 0;
}

#if !defined(N_UNIX)
/*
 * get_nlist_path() - get kernel name list path
 */

char *get_nlist_path(struct lsof_context *ctx, /* context */
                     int ap) /* on success, return an allocated path
                              * string pointer if 1; return a
                              * constant character pointer if 0;
                              * return NULL if failure */
{
    const char *bf;
    static char *bfc;
    MALLOC_S bfl;
    /*
     * Get bootfile name.
     */
    if ((bf = getbootfile())) {
        if (!ap)
            return ("");
        bfl = (MALLOC_S)(strlen(bf) + 1);
        if (!(bfc = (char *)malloc(bfl))) {
            (void)fprintf(
                stderr, "%s: can't allocate %d bytes for boot file path: %s\n",
                Pn, bfl, bf);
            Error(ctx);
        }
        (void)snpf(bfc, bfl, "%s", bf);
        return (bfc);
    }
    return ((char *)NULL);
}
#endif /* !defined(N_UNIX) */

/*
 * initialize() - perform all initialization
 */

void initialize(struct lsof_context *ctx) { get_kernel_access(ctx); }

/*
 * kread() - read from kernel memory
 */

int kread(struct lsof_context *ctx, /* context */
          KA_T addr,                /* kernel memory address */
          char *buf,                /* buffer to receive data */
          READLEN_T len)            /* length to read */
{
    int br;

    br = kvm_read(Kd, (u_long)addr, buf, len);
    return ((br == len) ? 0 : 1);
}

/*
 * process_text() - process text information
 */
void process_text(struct lsof_context *ctx, /* context */
                  KA_T vm)                  /* kernel vm space pointer */
{
    int i, j;
    KA_T ka;
    int n = 0;
    struct vm_map_entry vmme, *e;
    struct vmspace vmsp;

#if !defined(UVM)
    struct pager_struct pg;
    struct vm_object vmo;
#endif /* !defined(UVM) */

    /*
     * Read the vmspace structure for the process.
     */
    if (kread(ctx, vm, (char *)&vmsp, sizeof(vmsp)))
        return;
        /*
         * Read the vm_map structure.  Search its vm_map_entry structure list.
         */

#if !defined(UVM)
    if (!vmsp.vm_map.is_main_map)
        return;
#endif /* !defined(UVM) */

    for (i = 0; i < vmsp.vm_map.nentries; i++) {

        /*
         * Read the next vm_map_entry.
         */
        if (!i)
            e = &vmsp.vm_map.header;
        else {
            if (!(ka = (KA_T)e->next))
                return;
            e = &vmme;
            if (kread(ctx, ka, (char *)e, sizeof(vmme)))
                return;
        }

#if defined(UVM)
        /*
         * Process the uvm_obj pointer of a UVM map entry with a UVM_ET_OBJ
         * type as a vnode pointer.
         */
        if ((e->etype > UVM_ET_OBJ) && e->object.uvm_obj)
            (void)enter_vn_text(ctx, (KA_T)e->object.uvm_obj, &n);
#else  /* !defined(UVM) */
        /*
         * Read the map entry's object and the object's shadow.
         * Look for a PG_VNODE pager handle.
         */
        if (e->is_a_map || e->is_sub_map)
            continue;
        for (j = 0, ka = (KA_T)e->object.vm_object; j < 2 && ka;
             j++, ka = (KA_T)vmo.shadow) {
            if (kread(ctx, ka, (char *)&vmo, sizeof(vmo)))
                break;
            if (!(ka = (KA_T)vmo.pager) ||
                kread(ctx, ka, (char *)&pg, sizeof(pg)))
                continue;
            if (!pg.pg_handle || pg.pg_type != PG_VNODE)
                continue;
            (void)enter_vn_text((KA_T)pg.pg_handle, &n);
        }
#endif /* defined(UVM) */
    }
}