xref: /netbsd-src/usr.bin/fstat/misc.c (revision 82ad575716605df31379cf04a2f3efbc97b8a6f5)
1 /*	$NetBSD: misc.c,v 1.9 2012/10/27 22:38:07 alnsn 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.9 2012/10/27 22:38:07 alnsn 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 #undef _KERNEL
52 #include <sys/vnode.h>
53 #include <sys/mount.h>
54 
55 #include <net/bpfdesc.h>
56 
57 #include <err.h>
58 #include <kvm.h>
59 #include "fstat.h"
60 
61 static struct nlist nl[] = {
62 #define NL_BPF		0
63     { .n_name = "bpf_fileops", },
64 #define NL_CRYPTO	1
65     { .n_name = "cryptofops" },
66 #define NL_DMIO		2
67     { .n_name = "dmio_fileops", },
68 #define NL_DRVCTL	3
69     { .n_name = "drvctl_fileops", },
70 #define NL_DTV_DEMUX	4
71     { .n_name = "dtv_demux_fileops", },
72 #define NL_FILEMON	5
73     { .n_name = "filemon_fileops", },
74 #define NL_KQUEUE	6
75     { .n_name = "kqueueops" },
76 #define NL_MQUEUE	7
77     { .n_name = "mqops" },
78 #define NL_PIPE		8
79     { .n_name = "pipeops" },
80 #define NL_PUTTER	9
81     { .n_name = "putter_fileops", },
82 #define NL_SEM		10
83     { .n_name = "semops", },
84 #define NL_SOCKET	11
85     { .n_name = "socketops" },
86 #define NL_SVR4_NET	12
87     { .n_name = "svr4_netops" },
88 #define NL_SVR4_32_NET	13
89     { .n_name = "svr4_32_netops" },
90 #define NL_TAP		14
91     { .n_name = "tap_fileops", },
92 #define NL_VNOPS	15
93     { .n_name = "vnops" },
94 #define NL_XENEVT	16
95     { .n_name = "xenevt_fileops" },
96 #define NL_MAX		17
97     { .n_name = NULL }
98 };
99 
100 extern int vflg;
101 
102 
103 static int
104 p_bpf(struct file *f)
105 {
106 	struct bpf_d bpf;
107 
108 	if (!KVM_READ(f->f_data, &bpf, sizeof (bpf))) {
109 		dprintf("can't read bpf at %p for pid %d", f->f_data, Pid);
110 		return 0;
111 	}
112 	(void)printf("* bpf rec=%lu, dr=%lu, cap=%lu, pid=%lu",
113 	    bpf.bd_rcount, bpf.bd_dcount, bpf.bd_ccount,
114 	    (unsigned long)bpf.bd_pid);
115 	if (bpf.bd_promisc)
116 		(void)printf(", promisc");
117 	if (bpf.bd_immediate)
118 		(void)printf(", immed");
119 	if (bpf.bd_seesent)
120 		(void)printf(", seesent");
121 	if (bpf.bd_jitcode != NULL)
122 		(void)printf(", jit");
123 	if (bpf.bd_async)
124 		(void)printf(", asyncgrp=%lu", (unsigned long)bpf.bd_pgid);
125 	if (bpf.bd_state == BPF_IDLE)
126 		(void)printf(", idle");
127 	else if (bpf.bd_state == BPF_WAITING)
128 		(void)printf(", waiting");
129 	else if (bpf.bd_state == BPF_TIMED_OUT)
130 		(void)printf(", timeout");
131 	(void)printf("\n");
132 	return 0;
133 }
134 
135 static int
136 p_mqueue(struct file *f)
137 {
138 	struct mqueue mq;
139 
140 	if (!KVM_READ(f->f_data, &mq, sizeof (mq))) {
141 		dprintf("can't read mqueue at %p for pid %d", f->f_data, Pid);
142 		return 0;
143 	}
144 	(void)printf("* mqueue \"%s\"\n", mq.mq_name);
145 	return 0;
146 }
147 
148 static int
149 p_kqueue(struct file *f)
150 {
151 	struct kqueue kq;
152 
153 	if (!KVM_READ(f->f_data, &kq, sizeof (kq))) {
154 		dprintf("can't read kqueue at %p for pid %d", f->f_data, Pid);
155 		return 0;
156 	}
157 	(void)printf("* kqueue pending %d\n", kq.kq_count);
158 	return 0;
159 }
160 
161 int
162 pmisc(struct file *f, const char *name)
163 {
164 	size_t i;
165 	if (nl[0].n_value == 0) {
166 		int n;
167 		if ((n = KVM_NLIST(nl)) == -1)
168 			errx(1, "Cannot list kernel symbols (%s)",
169 			    KVM_GETERR());
170 		else if (n != 0 && vflg)
171 			warnx("Could not find %d symbols", n);
172 	}
173 	for (i = 0; i < NL_MAX; i++)
174 		if ((uintptr_t)f->f_ops == nl[i].n_value)
175 			break;
176 	switch (i) {
177 	case NL_BPF:
178 		return p_bpf(f);
179 	case NL_MQUEUE:
180 		return p_mqueue(f);
181 	case NL_KQUEUE:
182 		return p_kqueue(f);
183 	case NL_TAP:
184 		printf("* tap %lu\n", (unsigned long)(intptr_t)f->f_data);
185 		return 0;
186 	case NL_CRYPTO:
187 		printf("* crypto %p\n", f->f_data);
188 		return 0;
189 	case NL_MAX:
190 		printf("* %s %p\n", name, f->f_data);
191 		return 0;
192 	default:
193 		printf("* %s %p\n", nl[i].n_name, f->f_data);
194 		return 0;
195 	}
196 }
197