xref: /netbsd-src/usr.bin/fstat/misc.c (revision deb6f0161a9109e7de9b519dc8dfb9478668dcdd)
1 /*	$NetBSD: misc.c,v 1.20 2018/06/26 10:00:25 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.20 2018/06/26 10:00:25 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 == BPF_D_IN)
146 		(void)printf(", in");
147 	else if (bpf.bd_direction == BPF_D_INOUT)
148 		(void)printf(", inout");
149 	else if (bpf.bd_direction == BPF_D_OUT)
150 		(void)printf(", out");
151 	if (bpf.bd_jitcode != NULL)
152 		(void)printf(", jit");
153 	if (bpf.bd_async)
154 		(void)printf(", asyncgrp=%lu", (unsigned long)bpf.bd_pgid);
155 	if (bpf.bd_state == BPF_IDLE)
156 		(void)printf(", idle");
157 	else if (bpf.bd_state == BPF_WAITING)
158 		(void)printf(", waiting");
159 	else if (bpf.bd_state == BPF_TIMED_OUT)
160 		(void)printf(", timeout");
161 	(void)printf("\n");
162 	return 0;
163 }
164 
165 static int
166 p_sem(struct file *f)
167 {
168 	ksem_t ks;
169 	if (!KVM_READ(f->f_data, &ks, sizeof(ks))) {
170 		dprintf("can't read sem at %p for pid %d", f->f_data, Pid);
171 		return 0;
172 	}
173 	(void)printf("* ksem ref=%u, value=%u, waiters=%u, flags=0x%x, "
174 	    "mode=%o, uid=%u, gid=%u", ks.ks_ref, ks.ks_value, ks.ks_waiters,
175 	    ks.ks_flags, ks.ks_mode, ks.ks_uid, ks.ks_gid);
176 	if (ks.ks_name && ks.ks_namelen) {
177 		char buf[64];
178 		if (ks.ks_namelen >= sizeof(buf))
179 			ks.ks_namelen = sizeof(buf) - 1;
180 		if (!KVM_READ(ks.ks_name, buf, ks.ks_namelen)) {
181 			dprintf("can't read sem name at %p for pid %d",
182 			    ks.ks_name, Pid);
183 		} else {
184 			buf[ks.ks_namelen] = '\0';
185 			(void)printf(", name=%s\n", buf);
186 			return 0;
187 		}
188 	}
189 	(void)printf("\n");
190 	return 0;
191 }
192 
193 static int
194 p_mqueue(struct file *f)
195 {
196 	struct mqueue mq;
197 
198 	if (!KVM_READ(f->f_data, &mq, sizeof(mq))) {
199 		dprintf("can't read mqueue at %p for pid %d", f->f_data, Pid);
200 		return 0;
201 	}
202 	(void)printf("* mqueue \"%s\"\n", mq.mq_name);
203 	return 0;
204 }
205 
206 static int
207 p_rnd(struct file *f)
208 {
209 	struct cprng_strong {
210 		char cs_name[16];
211 		int  cs_flags;
212 		/*...*/
213 	} str;
214 	struct rnd_ctx {
215 		struct cprng_strong *rc_cprng;
216 		bool rc_hard;
217 	} ctx;
218 	char buf[1024];
219 
220 	if (!KVM_READ(f->f_data, &ctx, sizeof(ctx))) {
221 		dprintf("can't read rnd_ctx at %p for pid %d", f->f_data, Pid);
222 		return 0;
223 	}
224 	if (!KVM_READ(ctx.rc_cprng, &str, sizeof(str))) {
225 		dprintf("can't read cprng_strong at %p for pid %d", f->f_data,\
226 		    Pid);
227 		return 0;
228 	}
229 	snprintb(buf, sizeof(buf), CPRNG_FMT, str.cs_flags);
230 	(void)printf("* rnd \"%s\" flags %s\n", str.cs_name, buf);
231 	return 0;
232 }
233 
234 static int
235 p_kqueue(struct file *f)
236 {
237 	struct kqueue kq;
238 
239 	if (!KVM_READ(f->f_data, &kq, sizeof(kq))) {
240 		dprintf("can't read kqueue at %p for pid %d", f->f_data, Pid);
241 		return 0;
242 	}
243 	(void)printf("* kqueue pending %d\n", kq.kq_count);
244 	return 0;
245 }
246 
247 int
248 pmisc(struct file *f, const char *name)
249 {
250 	size_t i;
251 	if (nl[0].n_value == 0) {
252 		int n;
253 		if ((n = KVM_NLIST(nl)) == -1)
254 			errx(1, "Cannot list kernel symbols (%s)",
255 			    KVM_GETERR());
256 		else if (n != 0 && vflg) {
257 			char buf[1024];
258 			buf[0] = '\0';
259 			for (struct nlist *l = nl; l->n_name != NULL; l++) {
260 				if (l->n_value != 0)
261 					continue;
262 				strlcat(buf, ", ", sizeof(buf));
263 				strlcat(buf, l->n_name, sizeof(buf));
264 			}
265 			warnx("Could not find %d symbols: %s", n, buf + 2);
266 		}
267 	}
268 	for (i = 0; i < NL_MAX; i++)
269 		if ((uintptr_t)f->f_ops == nl[i].n_value)
270 			break;
271 	switch (i) {
272 	case NL_BPF:
273 		return p_bpf(f);
274 	case NL_MQUEUE:
275 		return p_mqueue(f);
276 	case NL_KQUEUE:
277 		return p_kqueue(f);
278 	case NL_RND:
279 		return p_rnd(f);
280 	case NL_SEM:
281 		return p_sem(f);
282 	case NL_TAP:
283 		printf("* tap %lu\n", (unsigned long)(intptr_t)f->f_data);
284 		return 0;
285 	case NL_CRYPTO:
286 		printf("* crypto %p\n", f->f_data);
287 		return 0;
288 	case NL_AUDIO:
289 		printf("* audio %p\n", f->f_data);
290 		return 0;
291 	case NL_PAD:
292 		printf("* pad %p\n", f->f_data);
293 		return 0;
294 	case NL_MAX:
295 		printf("* %s ops=%p %p\n", name, f->f_ops, f->f_data);
296 		return 0;
297 	default:
298 		printf("* %s %p\n", nl[i].n_name, f->f_data);
299 		return 0;
300 	}
301 }
302