Home | History | Annotate | Line # | Download | only in fstat
misc.c revision 1.8.2.2
      1 /*	$NetBSD: misc.c,v 1.8.2.2 2013/01/16 05:34:05 yamt Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Christos Zoulas
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __RCSID("$NetBSD: misc.c,v 1.8.2.2 2013/01/16 05:34:05 yamt Exp $");
     34 
     35 #define _KMEMUSER
     36 #include <stdbool.h>
     37 #include <sys/param.h>
     38 #include <sys/types.h>
     39 #include <sys/time.h>
     40 #include <sys/stat.h>
     41 #include <sys/condvar.h>
     42 #include <sys/selinfo.h>
     43 #include <sys/filedesc.h>
     44 #define _KERNEL
     45 #include <sys/mqueue.h>
     46 #include <sys/eventvar.h>
     47 #undef _KERNEL
     48 #include <sys/proc.h>
     49 #define _KERNEL
     50 #include <sys/file.h>
     51 #define copyout_t int
     52 #include <sys/ksem.h>
     53 #define _LIB_LIBKERN_LIBKERN_H_
     54 #define mutex_enter(a)
     55 #define mutex_exit(a)
     56 #include <sys/cprng.h>
     57 #undef _KERNEL
     58 #include <sys/vnode.h>
     59 #include <sys/mount.h>
     60 
     61 #include <net/bpfdesc.h>
     62 
     63 #include <err.h>
     64 #include <util.h>
     65 #include <string.h>
     66 #include <kvm.h>
     67 #include "fstat.h"
     68 
     69 static struct nlist nl[] = {
     70 #define NL_BPF		0
     71     { .n_name = "bpf_fileops", },
     72 #define NL_CRYPTO	1
     73     { .n_name = "cryptofops" },
     74 #define NL_DMIO		2
     75     { .n_name = "dmio_fileops", },
     76 #define NL_DRVCTL	3
     77     { .n_name = "drvctl_fileops", },
     78 #define NL_DTV_DEMUX	4
     79     { .n_name = "dtv_demux_fileops", },
     80 #define NL_FILEMON	5
     81     { .n_name = "filemon_fileops", },
     82 #define NL_KQUEUE	6
     83     { .n_name = "kqueueops" },
     84 #define NL_MQUEUE	7
     85     { .n_name = "mqops" },
     86 #define NL_PIPE		8
     87     { .n_name = "pipeops" },
     88 #define NL_PUTTER	9
     89     { .n_name = "putter_fileops", },
     90 #define NL_SEM		10
     91     { .n_name = "semops", },
     92 #define NL_SOCKET	11
     93     { .n_name = "socketops" },
     94 #define NL_SVR4_NET	12
     95     { .n_name = "svr4_netops" },
     96 #define NL_SVR4_32_NET	13
     97     { .n_name = "svr4_32_netops" },
     98 #define NL_TAP		14
     99     { .n_name = "tap_fileops", },
    100 #define NL_VNOPS	15
    101     { .n_name = "vnops" },
    102 #define NL_XENEVT	16
    103     { .n_name = "xenevt_fileops" },
    104 #define NL_RND		17
    105     { .n_name = "rnd_fileops" },
    106 #define NL_MAX		18
    107     { .n_name = NULL }
    108 };
    109 
    110 extern int vflg;
    111 
    112 
    113 static int
    114 p_bpf(struct file *f)
    115 {
    116 	struct bpf_d bpf;
    117 
    118 	if (!KVM_READ(f->f_data, &bpf, sizeof(bpf))) {
    119 		dprintf("can't read bpf at %p for pid %d", f->f_data, Pid);
    120 		return 0;
    121 	}
    122 	(void)printf("* bpf rec=%lu, dr=%lu, cap=%lu, pid=%lu",
    123 	    bpf.bd_rcount, bpf.bd_dcount, bpf.bd_ccount,
    124 	    (unsigned long)bpf.bd_pid);
    125 	if (bpf.bd_promisc)
    126 		(void)printf(", promisc");
    127 	if (bpf.bd_immediate)
    128 		(void)printf(", immed");
    129 	if (bpf.bd_seesent)
    130 		(void)printf(", seesent");
    131 	if (bpf.bd_jitcode != NULL)
    132 		(void)printf(", jit");
    133 	if (bpf.bd_async)
    134 		(void)printf(", asyncgrp=%lu", (unsigned long)bpf.bd_pgid);
    135 	if (bpf.bd_state == BPF_IDLE)
    136 		(void)printf(", idle");
    137 	else if (bpf.bd_state == BPF_WAITING)
    138 		(void)printf(", waiting");
    139 	else if (bpf.bd_state == BPF_TIMED_OUT)
    140 		(void)printf(", timeout");
    141 	(void)printf("\n");
    142 	return 0;
    143 }
    144 
    145 static int
    146 p_sem(struct file *f)
    147 {
    148 	ksem_t ks;
    149 	if (!KVM_READ(f->f_data, &ks, sizeof(ks))) {
    150 		dprintf("can't read sem at %p for pid %d", f->f_data, Pid);
    151 		return 0;
    152 	}
    153 	(void)printf("* ksem ref=%u, value=%u, waiters=%u, flags=0x%x, "
    154 	    "mode=%o, uid=%u, gid=%u", ks.ks_ref, ks.ks_value, ks.ks_waiters,
    155 	    ks.ks_flags, ks.ks_mode, ks.ks_uid, ks.ks_gid);
    156 	if (ks.ks_name && ks.ks_namelen) {
    157 		char buf[64];
    158 		if (ks.ks_namelen >= sizeof(buf))
    159 			ks.ks_namelen = sizeof(buf) - 1;
    160 		if (!KVM_READ(ks.ks_name, buf, ks.ks_namelen)) {
    161 			dprintf("can't read sem name at %p for pid %d",
    162 			    ks.ks_name, Pid);
    163 		} else {
    164 			buf[ks.ks_namelen] = '\0';
    165 			(void)printf(", name=%s\n", buf);
    166 			return 0;
    167 		}
    168 	}
    169 	(void)printf("\n");
    170 	return 0;
    171 }
    172 
    173 static int
    174 p_mqueue(struct file *f)
    175 {
    176 	struct mqueue mq;
    177 
    178 	if (!KVM_READ(f->f_data, &mq, sizeof(mq))) {
    179 		dprintf("can't read mqueue at %p for pid %d", f->f_data, Pid);
    180 		return 0;
    181 	}
    182 	(void)printf("* mqueue \"%s\"\n", mq.mq_name);
    183 	return 0;
    184 }
    185 
    186 static int
    187 p_kqueue(struct file *f)
    188 {
    189 	struct kqueue kq;
    190 
    191 	if (!KVM_READ(f->f_data, &kq, sizeof(kq))) {
    192 		dprintf("can't read kqueue at %p for pid %d", f->f_data, Pid);
    193 		return 0;
    194 	}
    195 	(void)printf("* kqueue pending %d\n", kq.kq_count);
    196 	return 0;
    197 }
    198 
    199 static int
    200 p_rnd(struct file *f)
    201 {
    202 	rp_ctx_t rp;
    203 
    204 	if (!KVM_READ(f->f_data, &rp, sizeof(rp))) {
    205 		dprintf("can't read rnd at %p for pid %d", f->f_data, Pid);
    206 		return 0;
    207 	}
    208 	(void)printf("* rnd ");
    209 	if (rp.hard)
    210 		printf("bytesonkey=%d, ", rp.bytesonkey);
    211 	if (rp.cprng) {
    212 		cprng_strong_t cprng;
    213 		if (!KVM_READ(rp.cprng, &cprng, sizeof(cprng))) {
    214 			dprintf("can't read rnd cprng at %p for pid %d",
    215 			    rp.cprng, Pid);
    216 		} else {
    217 			char buf[128];
    218 			snprintb(buf, sizeof(buf), CPRNG_FMT, cprng.flags);
    219 			(void)printf("name=%s, serial=%d%s, flags=%s\n",
    220 			    cprng.name, cprng.entropy_serial,
    221 			    cprng.reseed_pending ?  ", reseed" : "", buf);
    222 			return 0;
    223 		}
    224 	}
    225 	printf("\n");
    226 	return 0;
    227 }
    228 int
    229 pmisc(struct file *f, const char *name)
    230 {
    231 	size_t i;
    232 	if (nl[0].n_value == 0) {
    233 		int n;
    234 		if ((n = KVM_NLIST(nl)) == -1)
    235 			errx(1, "Cannot list kernel symbols (%s)",
    236 			    KVM_GETERR());
    237 		else if (n != 0 && vflg) {
    238 			char buf[1024];
    239 			buf[0] = '\0';
    240 			for (struct nlist *l = nl; l->n_name != NULL; l++) {
    241 				if (l->n_value != 0)
    242 					continue;
    243 				strlcat(buf, ", ", sizeof(buf));
    244 				strlcat(buf, l->n_name, sizeof(buf));
    245 			}
    246 			warnx("Could not find %d symbols: %s", n, buf + 2);
    247 		}
    248 	}
    249 	for (i = 0; i < NL_MAX; i++)
    250 		if ((uintptr_t)f->f_ops == nl[i].n_value)
    251 			break;
    252 	switch (i) {
    253 	case NL_BPF:
    254 		return p_bpf(f);
    255 	case NL_MQUEUE:
    256 		return p_mqueue(f);
    257 	case NL_KQUEUE:
    258 		return p_kqueue(f);
    259 	case NL_SEM:
    260 		return p_sem(f);
    261 	case NL_RND:
    262 		return p_rnd(f);
    263 	case NL_TAP:
    264 		printf("* tap %lu\n", (unsigned long)(intptr_t)f->f_data);
    265 		return 0;
    266 	case NL_CRYPTO:
    267 		printf("* crypto %p\n", f->f_data);
    268 		return 0;
    269 	case NL_MAX:
    270 		printf("* %s ops=%p %p\n", name, f->f_ops, f->f_data);
    271 		return 0;
    272 	default:
    273 		printf("* %s %p\n", nl[i].n_name, f->f_data);
    274 		return 0;
    275 	}
    276 }
    277