Home | History | Annotate | Line # | Download | only in fstat
misc.c revision 1.2.8.1
      1  1.2.8.1       jym /*	$NetBSD: misc.c,v 1.2.8.1 2009/05/13 19:19:50 jym 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.2.8.1       jym __RCSID("$NetBSD: misc.c,v 1.2.8.1 2009/05/13 19:19:50 jym Exp $");
     34      1.1  christos 
     35      1.2  christos #include <stdbool.h>
     36      1.1  christos #include <sys/param.h>
     37      1.1  christos #include <sys/time.h>
     38      1.1  christos #include <sys/stat.h>
     39      1.2  christos #include <sys/condvar.h>
     40      1.2  christos #include <sys/selinfo.h>
     41      1.2  christos #include <sys/filedesc.h>
     42      1.2  christos #define _KERNEL
     43      1.2  christos #include <sys/mqueue.h>
     44      1.2  christos #include <sys/eventvar.h>
     45      1.2  christos #undef _KERNEL
     46      1.1  christos #include <sys/proc.h>
     47      1.1  christos #define _KERNEL
     48      1.1  christos #include <sys/file.h>
     49      1.1  christos #undef _KERNEL
     50      1.1  christos #include <sys/vnode.h>
     51      1.1  christos #include <sys/mount.h>
     52      1.1  christos 
     53      1.1  christos #include <net/bpfdesc.h>
     54      1.1  christos 
     55      1.1  christos #include <err.h>
     56      1.1  christos #include <kvm.h>
     57      1.1  christos #include "fstat.h"
     58      1.1  christos 
     59      1.1  christos static struct nlist nl[] = {
     60      1.1  christos #define NL_KQUEUE	0
     61      1.1  christos     { .n_name = "kqueueops" },
     62      1.1  christos #define NL_MQUEUE	1
     63      1.1  christos     { .n_name = "mqops" },
     64      1.1  christos #define NL_PIPE		2
     65      1.1  christos     { .n_name = "pipeops" },
     66      1.1  christos #define NL_SOCKET	3
     67      1.1  christos     { .n_name = "socketops" },
     68      1.1  christos #define NL_VNOPS	4
     69      1.1  christos     { .n_name = "vnops" },
     70      1.1  christos #define NL_CRYPTO	5
     71      1.1  christos     { .n_name = "cryptofops" },
     72      1.1  christos #define NL_BPF		6
     73      1.1  christos     { .n_name = "bpf_fileops", },
     74      1.1  christos #define NL_TAP		7
     75      1.1  christos     { .n_name = "tap_fileops", },
     76      1.1  christos #define NL_MAX		8
     77      1.1  christos     { .n_name = NULL }
     78      1.1  christos };
     79      1.1  christos 
     80  1.2.8.1       jym extern int vflg;
     81  1.2.8.1       jym 
     82      1.1  christos 
     83      1.1  christos static int
     84      1.1  christos p_bpf(struct file *f)
     85      1.1  christos {
     86      1.1  christos 	struct bpf_d bpf;
     87      1.1  christos 
     88      1.1  christos 	if (!KVM_READ(f->f_data, &bpf, sizeof (bpf))) {
     89      1.1  christos 		dprintf("can't read bpf at %p for pid %d", f->f_data, Pid);
     90      1.1  christos 		return 0;
     91      1.1  christos 	}
     92      1.1  christos 	(void)printf("* bpf rec=%lu, dr=%lu, cap=%lu, pid=%lu",
     93      1.1  christos 	    bpf.bd_rcount, bpf.bd_dcount, bpf.bd_ccount,
     94      1.1  christos 	    (unsigned long)bpf.bd_pid);
     95      1.1  christos 	if (bpf.bd_promisc)
     96      1.1  christos 		(void)printf(", promisc");
     97      1.1  christos 	if (bpf.bd_immediate)
     98      1.1  christos 		(void)printf(", immed");
     99      1.1  christos 	if (bpf.bd_seesent)
    100      1.1  christos 		(void)printf(", seesent");
    101      1.1  christos 	if (bpf.bd_async)
    102      1.1  christos 		(void)printf(", asyncgrp=%lu", (unsigned long)bpf.bd_pgid);
    103      1.1  christos 	if (bpf.bd_state == BPF_IDLE)
    104      1.1  christos 		(void)printf(", idle");
    105      1.1  christos 	else if (bpf.bd_state == BPF_WAITING)
    106      1.1  christos 		(void)printf(", waiting");
    107      1.1  christos 	else if (bpf.bd_state == BPF_TIMED_OUT)
    108      1.1  christos 		(void)printf(", timeout");
    109      1.1  christos 	(void)printf("\n");
    110      1.1  christos 	return 0;
    111      1.1  christos }
    112      1.1  christos 
    113      1.1  christos static int
    114      1.1  christos p_mqueue(struct file *f)
    115      1.1  christos {
    116      1.1  christos 	struct mqueue mq;
    117      1.1  christos 
    118      1.1  christos 	if (!KVM_READ(f->f_data, &mq, sizeof (mq))) {
    119      1.1  christos 		dprintf("can't read mqueue at %p for pid %d", f->f_data, Pid);
    120      1.1  christos 		return 0;
    121      1.1  christos 	}
    122      1.1  christos 	(void)printf("* mqueue \"%s\"\n", mq.mq_name);
    123      1.1  christos 	return 0;
    124      1.1  christos }
    125      1.2  christos 
    126      1.2  christos static int
    127      1.2  christos p_kqueue(struct file *f)
    128      1.2  christos {
    129      1.2  christos 	struct kqueue kq;
    130      1.2  christos 
    131      1.2  christos 	if (!KVM_READ(f->f_data, &kq, sizeof (kq))) {
    132      1.2  christos 		dprintf("can't read kqueue at %p for pid %d", f->f_data, Pid);
    133      1.2  christos 		return 0;
    134      1.2  christos 	}
    135      1.2  christos 	(void)printf("* kqueue pending %d\n", kq.kq_count);
    136      1.2  christos 	return 0;
    137      1.2  christos }
    138      1.1  christos 
    139      1.1  christos int
    140      1.1  christos pmisc(struct file *f, const char *name)
    141      1.1  christos {
    142      1.1  christos 	size_t i;
    143      1.1  christos 	if (nl[0].n_value == 0) {
    144      1.1  christos 		int n;
    145      1.1  christos 		if ((n = KVM_NLIST(nl)) == -1)
    146      1.1  christos 			errx(1, "Cannot list kernel symbols (%s)",
    147      1.1  christos 			    KVM_GETERR());
    148  1.2.8.1       jym 		else if (n != 0 && vflg)
    149      1.1  christos 			warnx("Could not find %d symbols", n);
    150      1.1  christos 	}
    151      1.1  christos 	for (i = 0; i < NL_MAX; i++)
    152  1.2.8.1       jym 		if ((uintptr_t)f->f_ops == nl[i].n_value)
    153      1.1  christos 			break;
    154      1.1  christos 	switch (i) {
    155      1.1  christos 	case NL_BPF:
    156      1.1  christos 		return p_bpf(f);
    157      1.1  christos 	case NL_MQUEUE:
    158      1.1  christos 		return p_mqueue(f);
    159      1.2  christos 	case NL_KQUEUE:
    160      1.2  christos 		return p_kqueue(f);
    161      1.1  christos 	case NL_TAP:
    162      1.1  christos 		printf("* tap %lu\n", (unsigned long)(intptr_t)f->f_data);
    163      1.1  christos 		return 0;
    164      1.1  christos 	case NL_CRYPTO:
    165      1.2  christos 		printf("* crypto %p\n", f->f_data);
    166      1.2  christos 		return 0;
    167      1.1  christos 	default:
    168      1.1  christos 		printf("* %s %p\n", name, f->f_data);
    169      1.1  christos 		return 0;
    170      1.1  christos 	}
    171      1.1  christos }
    172