Home | History | Annotate | Line # | Download | only in fstat
misc.c revision 1.1
      1  1.1  christos /*	$NetBSD: misc.c,v 1.1 2008/07/22 22:58:04 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*-
      4  1.1  christos  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      5  1.1  christos  * All rights reserved.
      6  1.1  christos  *
      7  1.1  christos  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  christos  * by Christos Zoulas
      9  1.1  christos  *
     10  1.1  christos  * Redistribution and use in source and binary forms, with or without
     11  1.1  christos  * modification, are permitted provided that the following conditions
     12  1.1  christos  * are met:
     13  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     14  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     15  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  christos  *    documentation and/or other materials provided with the distribution.
     18  1.1  christos  *
     19  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  christos  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  christos  */
     31  1.1  christos 
     32  1.1  christos #include <sys/cdefs.h>
     33  1.1  christos __RCSID("$NetBSD: misc.c,v 1.1 2008/07/22 22:58:04 christos Exp $");
     34  1.1  christos 
     35  1.1  christos #include <sys/param.h>
     36  1.1  christos #include <sys/time.h>
     37  1.1  christos #include <sys/stat.h>
     38  1.1  christos #include <sys/proc.h>
     39  1.1  christos #define _KERNEL
     40  1.1  christos #include <sys/file.h>
     41  1.1  christos #ifdef notyet
     42  1.1  christos #include <sys/mqueue.h>
     43  1.1  christos #endif
     44  1.1  christos #undef _KERNEL
     45  1.1  christos #include <sys/vnode.h>
     46  1.1  christos #include <sys/mount.h>
     47  1.1  christos 
     48  1.1  christos #include <net/bpfdesc.h>
     49  1.1  christos 
     50  1.1  christos #include <err.h>
     51  1.1  christos #include <kvm.h>
     52  1.1  christos #include "fstat.h"
     53  1.1  christos 
     54  1.1  christos static struct nlist nl[] = {
     55  1.1  christos #define NL_KQUEUE	0
     56  1.1  christos     { .n_name = "kqueueops" },
     57  1.1  christos #define NL_MQUEUE	1
     58  1.1  christos     { .n_name = "mqops" },
     59  1.1  christos #define NL_PIPE		2
     60  1.1  christos     { .n_name = "pipeops" },
     61  1.1  christos #define NL_SOCKET	3
     62  1.1  christos     { .n_name = "socketops" },
     63  1.1  christos #define NL_VNOPS	4
     64  1.1  christos     { .n_name = "vnops" },
     65  1.1  christos #define NL_CRYPTO	5
     66  1.1  christos     { .n_name = "cryptofops" },
     67  1.1  christos #define NL_BPF		6
     68  1.1  christos     { .n_name = "bpf_fileops", },
     69  1.1  christos #define NL_TAP		7
     70  1.1  christos     { .n_name = "tap_fileops", },
     71  1.1  christos #define NL_MAX		8
     72  1.1  christos     { .n_name = NULL }
     73  1.1  christos };
     74  1.1  christos 
     75  1.1  christos 
     76  1.1  christos static int
     77  1.1  christos p_bpf(struct file *f)
     78  1.1  christos {
     79  1.1  christos 	struct bpf_d bpf;
     80  1.1  christos 
     81  1.1  christos 	if (!KVM_READ(f->f_data, &bpf, sizeof (bpf))) {
     82  1.1  christos 		dprintf("can't read bpf at %p for pid %d", f->f_data, Pid);
     83  1.1  christos 		return 0;
     84  1.1  christos 	}
     85  1.1  christos 	(void)printf("* bpf rec=%lu, dr=%lu, cap=%lu, pid=%lu",
     86  1.1  christos 	    bpf.bd_rcount, bpf.bd_dcount, bpf.bd_ccount,
     87  1.1  christos 	    (unsigned long)bpf.bd_pid);
     88  1.1  christos 	if (bpf.bd_promisc)
     89  1.1  christos 		(void)printf(", promisc");
     90  1.1  christos 	if (bpf.bd_immediate)
     91  1.1  christos 		(void)printf(", immed");
     92  1.1  christos 	if (bpf.bd_seesent)
     93  1.1  christos 		(void)printf(", seesent");
     94  1.1  christos 	if (bpf.bd_async)
     95  1.1  christos 		(void)printf(", asyncgrp=%lu", (unsigned long)bpf.bd_pgid);
     96  1.1  christos 	if (bpf.bd_state == BPF_IDLE)
     97  1.1  christos 		(void)printf(", idle");
     98  1.1  christos 	else if (bpf.bd_state == BPF_WAITING)
     99  1.1  christos 		(void)printf(", waiting");
    100  1.1  christos 	else if (bpf.bd_state == BPF_TIMED_OUT)
    101  1.1  christos 		(void)printf(", timeout");
    102  1.1  christos 	(void)printf("\n");
    103  1.1  christos 	return 0;
    104  1.1  christos }
    105  1.1  christos 
    106  1.1  christos #ifdef notyet
    107  1.1  christos static int
    108  1.1  christos p_mqueue(struct file *f)
    109  1.1  christos {
    110  1.1  christos 	struct mqueue mq;
    111  1.1  christos 
    112  1.1  christos 	if (!KVM_READ(f->f_data, &mq, sizeof (mq))) {
    113  1.1  christos 		dprintf("can't read mqueue at %p for pid %d", f->f_data, Pid);
    114  1.1  christos 		return 0;
    115  1.1  christos 	}
    116  1.1  christos 	(void)printf("* mqueue \"%s\"\n", mq.mq_name);
    117  1.1  christos 	return 0;
    118  1.1  christos }
    119  1.1  christos #endif
    120  1.1  christos 
    121  1.1  christos int
    122  1.1  christos pmisc(struct file *f, const char *name)
    123  1.1  christos {
    124  1.1  christos 	size_t i;
    125  1.1  christos 	if (nl[0].n_value == 0) {
    126  1.1  christos 		int n;
    127  1.1  christos 		if ((n = KVM_NLIST(nl)) == -1)
    128  1.1  christos 			errx(1, "Cannot list kernel symbols (%s)",
    129  1.1  christos 			    KVM_GETERR());
    130  1.1  christos 		else if (n != 0)
    131  1.1  christos 			warnx("Could not find %d symbols", n);
    132  1.1  christos 	}
    133  1.1  christos 	for (i = 0; i < NL_MAX; i++)
    134  1.1  christos 		if ((intptr_t)f->f_ops == nl[i].n_value)
    135  1.1  christos 			break;
    136  1.1  christos 	switch (i) {
    137  1.1  christos 	case NL_BPF:
    138  1.1  christos 		return p_bpf(f);
    139  1.1  christos #ifdef notyet
    140  1.1  christos 	case NL_MQUEUE:
    141  1.1  christos 		return p_mqueue(f);
    142  1.1  christos #endif
    143  1.1  christos 	case NL_TAP:
    144  1.1  christos 		printf("* tap %lu\n", (unsigned long)(intptr_t)f->f_data);
    145  1.1  christos 		return 0;
    146  1.1  christos 	case NL_CRYPTO:
    147  1.1  christos 	case NL_KQUEUE:
    148  1.1  christos 	default:
    149  1.1  christos 		printf("* %s %p\n", name, f->f_data);
    150  1.1  christos 		return 0;
    151  1.1  christos 	}
    152  1.1  christos }
    153