Home | History | Annotate | Line # | Download | only in fstat
misc.c revision 1.19
      1 /*	$NetBSD: misc.c,v 1.19 2018/06/26 06:48:03 msaitoh 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.19 2018/06/26 06:48:03 msaitoh Exp $");
     34 
     35 #include <stdbool.h>
     36 #include <sys/param.h>
     37 #include <sys/types.h>
     38 #include <sys/time.h>
     39 #include <sys/stat.h>
     40 #include <sys/condvar.h>
     41 #include <sys/selinfo.h>
     42 #include <sys/filedesc.h>
     43 #define _KERNEL
     44 #include <sys/mqueue.h>
     45 #include <sys/eventvar.h>
     46 #undef _KERNEL
     47 #include <sys/proc.h>
     48 #define _KERNEL
     49 #include <sys/file.h>
     50 #define copyout_t int
     51 #include <sys/ksem.h>
     52 #define _LIB_LIBKERN_LIBKERN_H_
     53 #define mutex_enter(a)
     54 #define mutex_exit(a)
     55 #undef _KERNEL
     56 #include <sys/cprng.h>
     57 #include <sys/vnode.h>
     58 #include <sys/mount.h>
     59 
     60 #include <net/bpfdesc.h>
     61 
     62 #include <err.h>
     63 #include <util.h>
     64 #include <string.h>
     65 #include <kvm.h>
     66 #include "fstat.h"
     67 
     68 static struct nlist nl[] = {
     69 #define NL_BPF		0
     70     { .n_name = "bpf_fileops", },
     71 #define NL_CRYPTO	1
     72     { .n_name = "cryptofops" },
     73 #define NL_DMIO		2
     74     { .n_name = "dmio_fileops", },
     75 #define NL_DRVCTL	3
     76     { .n_name = "drvctl_fileops", },
     77 #define NL_DTV_DEMUX	4
     78     { .n_name = "dtv_demux_fileops", },
     79 #define NL_FILEMON	5
     80     { .n_name = "filemon_fileops", },
     81 #define NL_KQUEUE	6
     82     { .n_name = "kqueueops" },
     83 #define NL_MQUEUE	7
     84     { .n_name = "mqops" },
     85 #define NL_PIPE		8
     86     { .n_name = "pipeops" },
     87 #define NL_PUTTER	9
     88     { .n_name = "putter_fileops", },
     89 #define NL_RND		10
     90     { .n_name = "rnd_fileops", },
     91 #define NL_SEM		11
     92     { .n_name = "semops", },
     93 #define NL_SOCKET	12
     94     { .n_name = "socketops" },
     95 #define NL_SVR4_NET	13
     96     { .n_name = "svr4_netops" },
     97 #define NL_SVR4_32_NET	14
     98     { .n_name = "svr4_32_netops" },
     99 #define NL_TAP		15
    100     { .n_name = "tap_fileops", },
    101 #define NL_VNOPS	16
    102     { .n_name = "vnops" },
    103 #define NL_XENEVT	17
    104     { .n_name = "xenevt_fileops" },
    105 #define NL_AUDIO	18
    106     { .n_name = "audio_fileops" },
    107 #define NL_PAD		19
    108     { .n_name = "pad_fileops" },
    109 #define NL_MAX		20
    110     { .n_name = NULL }
    111 };
    112 
    113 extern int vflg;
    114 
    115 
    116 static int
    117 p_bpf(struct file *f)
    118 {
    119 	struct bpf_d bpf;
    120 	struct bpf_if bi;
    121 	struct ifnet ifn;
    122 
    123 	strlcpy(ifn.if_xname, "???", sizeof(ifn.if_xname));
    124 
    125 	if (!KVM_READ(f->f_data, &bpf, sizeof(bpf))) {
    126 		dprintf("can't read bpf at %p for pid %d", f->f_data, Pid);
    127 		return 0;
    128 	}
    129 	if (bpf.bd_bif != NULL) {
    130 		if (!KVM_READ(bpf.bd_bif, &bi, sizeof(bi)))
    131 			dprintf("can't read bpf interface at %p for pid %d",
    132 			    bpf.bd_bif, Pid);
    133 		if (bi.bif_ifp != NULL)
    134 			if (!KVM_READ(bi.bif_ifp, &ifn, sizeof(ifn)))
    135 				dprintf("can't read net interfsace"
    136 				    " at %p for pid %d", bi.bif_ifp, Pid);
    137 	}
    138 	(void)printf("* bpf@%s rec=%lu, dr=%lu, cap=%lu, pid=%lu", ifn.if_xname,
    139 	    bpf.bd_rcount, bpf.bd_dcount, bpf.bd_ccount,
    140 	    (unsigned long)bpf.bd_pid);
    141 	if (bpf.bd_promisc)
    142 		(void)printf(", promisc");
    143 	if (bpf.bd_immediate)
    144 		(void)printf(", immed");
    145 	if (bpf.bd_direction)
    146 		(void)printf(", direction");
    147 	if (bpf.bd_jitcode != NULL)
    148 		(void)printf(", jit");
    149 	if (bpf.bd_async)
    150 		(void)printf(", asyncgrp=%lu", (unsigned long)bpf.bd_pgid);
    151 	if (bpf.bd_state == BPF_IDLE)
    152 		(void)printf(", idle");
    153 	else if (bpf.bd_state == BPF_WAITING)
    154 		(void)printf(", waiting");
    155 	else if (bpf.bd_state == BPF_TIMED_OUT)
    156 		(void)printf(", timeout");
    157 	(void)printf("\n");
    158 	return 0;
    159 }
    160 
    161 static int
    162 p_sem(struct file *f)
    163 {
    164 	ksem_t ks;
    165 	if (!KVM_READ(f->f_data, &ks, sizeof(ks))) {
    166 		dprintf("can't read sem at %p for pid %d", f->f_data, Pid);
    167 		return 0;
    168 	}
    169 	(void)printf("* ksem ref=%u, value=%u, waiters=%u, flags=0x%x, "
    170 	    "mode=%o, uid=%u, gid=%u", ks.ks_ref, ks.ks_value, ks.ks_waiters,
    171 	    ks.ks_flags, ks.ks_mode, ks.ks_uid, ks.ks_gid);
    172 	if (ks.ks_name && ks.ks_namelen) {
    173 		char buf[64];
    174 		if (ks.ks_namelen >= sizeof(buf))
    175 			ks.ks_namelen = sizeof(buf) - 1;
    176 		if (!KVM_READ(ks.ks_name, buf, ks.ks_namelen)) {
    177 			dprintf("can't read sem name at %p for pid %d",
    178 			    ks.ks_name, Pid);
    179 		} else {
    180 			buf[ks.ks_namelen] = '\0';
    181 			(void)printf(", name=%s\n", buf);
    182 			return 0;
    183 		}
    184 	}
    185 	(void)printf("\n");
    186 	return 0;
    187 }
    188 
    189 static int
    190 p_mqueue(struct file *f)
    191 {
    192 	struct mqueue mq;
    193 
    194 	if (!KVM_READ(f->f_data, &mq, sizeof(mq))) {
    195 		dprintf("can't read mqueue at %p for pid %d", f->f_data, Pid);
    196 		return 0;
    197 	}
    198 	(void)printf("* mqueue \"%s\"\n", mq.mq_name);
    199 	return 0;
    200 }
    201 
    202 static int
    203 p_rnd(struct file *f)
    204 {
    205 	struct cprng_strong {
    206 		char cs_name[16];
    207 		int  cs_flags;
    208 		/*...*/
    209 	} str;
    210 	struct rnd_ctx {
    211 		struct cprng_strong *rc_cprng;
    212 		bool rc_hard;
    213 	} ctx;
    214 	char buf[1024];
    215 
    216 	if (!KVM_READ(f->f_data, &ctx, sizeof(ctx))) {
    217 		dprintf("can't read rnd_ctx at %p for pid %d", f->f_data, Pid);
    218 		return 0;
    219 	}
    220 	if (!KVM_READ(ctx.rc_cprng, &str, sizeof(str))) {
    221 		dprintf("can't read cprng_strong at %p for pid %d", f->f_data,\
    222 		    Pid);
    223 		return 0;
    224 	}
    225 	snprintb(buf, sizeof(buf), CPRNG_FMT, str.cs_flags);
    226 	(void)printf("* rnd \"%s\" flags %s\n", str.cs_name, buf);
    227 	return 0;
    228 }
    229 
    230 static int
    231 p_kqueue(struct file *f)
    232 {
    233 	struct kqueue kq;
    234 
    235 	if (!KVM_READ(f->f_data, &kq, sizeof(kq))) {
    236 		dprintf("can't read kqueue at %p for pid %d", f->f_data, Pid);
    237 		return 0;
    238 	}
    239 	(void)printf("* kqueue pending %d\n", kq.kq_count);
    240 	return 0;
    241 }
    242 
    243 int
    244 pmisc(struct file *f, const char *name)
    245 {
    246 	size_t i;
    247 	if (nl[0].n_value == 0) {
    248 		int n;
    249 		if ((n = KVM_NLIST(nl)) == -1)
    250 			errx(1, "Cannot list kernel symbols (%s)",
    251 			    KVM_GETERR());
    252 		else if (n != 0 && vflg) {
    253 			char buf[1024];
    254 			buf[0] = '\0';
    255 			for (struct nlist *l = nl; l->n_name != NULL; l++) {
    256 				if (l->n_value != 0)
    257 					continue;
    258 				strlcat(buf, ", ", sizeof(buf));
    259 				strlcat(buf, l->n_name, sizeof(buf));
    260 			}
    261 			warnx("Could not find %d symbols: %s", n, buf + 2);
    262 		}
    263 	}
    264 	for (i = 0; i < NL_MAX; i++)
    265 		if ((uintptr_t)f->f_ops == nl[i].n_value)
    266 			break;
    267 	switch (i) {
    268 	case NL_BPF:
    269 		return p_bpf(f);
    270 	case NL_MQUEUE:
    271 		return p_mqueue(f);
    272 	case NL_KQUEUE:
    273 		return p_kqueue(f);
    274 	case NL_RND:
    275 		return p_rnd(f);
    276 	case NL_SEM:
    277 		return p_sem(f);
    278 	case NL_TAP:
    279 		printf("* tap %lu\n", (unsigned long)(intptr_t)f->f_data);
    280 		return 0;
    281 	case NL_CRYPTO:
    282 		printf("* crypto %p\n", f->f_data);
    283 		return 0;
    284 	case NL_AUDIO:
    285 		printf("* audio %p\n", f->f_data);
    286 		return 0;
    287 	case NL_PAD:
    288 		printf("* pad %p\n", f->f_data);
    289 		return 0;
    290 	case NL_MAX:
    291 		printf("* %s ops=%p %p\n", name, f->f_ops, f->f_data);
    292 		return 0;
    293 	default:
    294 		printf("* %s %p\n", nl[i].n_name, f->f_data);
    295 		return 0;
    296 	}
    297 }
    298