xref: /netbsd-src/usr.bin/fstat/misc.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /*	$NetBSD: misc.c,v 1.23 2020/05/02 18:42:30 christos 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.23 2020/05/02 18:42:30 christos 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 copyin_t int
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 #undef _KERNEL
57 #include <sys/cprng.h>
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_RND		10
91     { .n_name = "rnd_fileops", },
92 #define NL_SEM		11
93     { .n_name = "semops", },
94 #define NL_SOCKET	12
95     { .n_name = "socketops" },
96 #define NL_SVR4_NET	13
97     { .n_name = "svr4_netops" },
98 #define NL_SVR4_32_NET	14
99     { .n_name = "svr4_32_netops" },
100 #define NL_TAP		15
101     { .n_name = "tap_fileops", },
102 #define NL_VNOPS	16
103     { .n_name = "vnops" },
104 #define NL_XENEVT	17
105     { .n_name = "xenevt_fileops" },
106 #define NL_AUDIO	18
107     { .n_name = "audio_fileops" },
108 #define NL_PAD		19
109     { .n_name = "pad_fileops" },
110 #define NL_MAX		20
111     { .n_name = NULL }
112 };
113 
114 extern int vflg;
115 
116 
117 static int
118 p_bpf(struct file *f)
119 {
120 	struct bpf_d bpf;
121 	struct bpf_if bi;
122 	struct ifnet ifn;
123 
124 	strlcpy(ifn.if_xname, "???", sizeof(ifn.if_xname));
125 
126 	if (!KVM_READ(f->f_data, &bpf, sizeof(bpf))) {
127 		dprintf("can't read bpf at %p for pid %d", f->f_data, Pid);
128 		return 0;
129 	}
130 	if (bpf.bd_bif != NULL) {
131 		if (!KVM_READ(bpf.bd_bif, &bi, sizeof(bi)))
132 			dprintf("can't read bpf interface at %p for pid %d",
133 			    bpf.bd_bif, Pid);
134 		if (bi.bif_ifp != NULL)
135 			if (!KVM_READ(bi.bif_ifp, &ifn, sizeof(ifn)))
136 				dprintf("can't read net interfsace"
137 				    " at %p for pid %d", bi.bif_ifp, Pid);
138 	}
139 	(void)printf("* bpf@%s rec=%lu, dr=%lu, cap=%lu, pid=%lu", ifn.if_xname,
140 	    bpf.bd_rcount, bpf.bd_dcount, bpf.bd_ccount,
141 	    (unsigned long)bpf.bd_pid);
142 	if (bpf.bd_promisc)
143 		(void)printf(", promisc");
144 	if (bpf.bd_immediate)
145 		(void)printf(", immed");
146 	if (bpf.bd_direction == BPF_D_IN)
147 		(void)printf(", in");
148 	else if (bpf.bd_direction == BPF_D_INOUT)
149 		(void)printf(", inout");
150 	else if (bpf.bd_direction == BPF_D_OUT)
151 		(void)printf(", out");
152 	if (bpf.bd_jitcode != NULL)
153 		(void)printf(", jit");
154 	if (bpf.bd_async)
155 		(void)printf(", asyncgrp=%lu", (unsigned long)bpf.bd_pgid);
156 	if (bpf.bd_state == BPF_IDLE)
157 		(void)printf(", idle");
158 	else if (bpf.bd_state == BPF_WAITING)
159 		(void)printf(", waiting");
160 	else if (bpf.bd_state == BPF_TIMED_OUT)
161 		(void)printf(", timeout");
162 	oprint(f, "\n");
163 	return 0;
164 }
165 
166 static int
167 p_sem(struct file *f)
168 {
169 	ksem_t ks;
170 	if (!KVM_READ(f->f_data, &ks, sizeof(ks))) {
171 		dprintf("can't read sem at %p for pid %d", f->f_data, Pid);
172 		return 0;
173 	}
174 	(void)printf("* ksem ref=%u, value=%u, waiters=%u, flags=0x%x, "
175 	    "mode=%o, uid=%u, gid=%u", ks.ks_ref, ks.ks_value, ks.ks_waiters,
176 	    ks.ks_flags, ks.ks_mode, ks.ks_uid, ks.ks_gid);
177 	if (ks.ks_name && ks.ks_namelen) {
178 		char buf[64];
179 		if (ks.ks_namelen >= sizeof(buf))
180 			ks.ks_namelen = sizeof(buf) - 1;
181 		if (!KVM_READ(ks.ks_name, buf, ks.ks_namelen)) {
182 			dprintf("can't read sem name at %p for pid %d",
183 			    ks.ks_name, Pid);
184 		} else {
185 			buf[ks.ks_namelen] = '\0';
186 			(void)printf(", name=%s", buf);
187 			oprint(f, "\n");
188 			return 0;
189 		}
190 	}
191 	oprint(f, "\n");
192 	return 0;
193 }
194 
195 static int
196 p_mqueue(struct file *f)
197 {
198 	struct mqueue mq;
199 
200 	if (!KVM_READ(f->f_data, &mq, sizeof(mq))) {
201 		dprintf("can't read mqueue at %p for pid %d", f->f_data, Pid);
202 		return 0;
203 	}
204 	(void)printf("* mqueue \"%s\"", mq.mq_name);
205 	oprint(f, "\n");
206 	return 0;
207 }
208 
209 static int
210 p_kqueue(struct file *f)
211 {
212 	struct kqueue kq;
213 
214 	if (!KVM_READ(f->f_data, &kq, sizeof(kq))) {
215 		dprintf("can't read kqueue at %p for pid %d", f->f_data, Pid);
216 		return 0;
217 	}
218 	(void)printf("* kqueue pending %d", kq.kq_count);
219 	oprint(f, "\n");
220 	return 0;
221 }
222 
223 int
224 pmisc(struct file *f, const char *name)
225 {
226 	size_t i;
227 	if (nl[0].n_value == 0) {
228 		int n;
229 		if ((n = KVM_NLIST(nl)) == -1)
230 			errx(1, "Cannot list kernel symbols (%s)",
231 			    KVM_GETERR());
232 		else if (n != 0 && vflg) {
233 			char buf[1024];
234 			buf[0] = '\0';
235 			for (struct nlist *l = nl; l->n_name != NULL; l++) {
236 				if (l->n_value != 0)
237 					continue;
238 				strlcat(buf, ", ", sizeof(buf));
239 				strlcat(buf, l->n_name, sizeof(buf));
240 			}
241 			warnx("Could not find %d symbols: %s", n, buf + 2);
242 		}
243 	}
244 	for (i = 0; i < NL_MAX; i++)
245 		if ((uintptr_t)f->f_ops == nl[i].n_value)
246 			break;
247 	switch (i) {
248 	case NL_BPF:
249 		return p_bpf(f);
250 	case NL_MQUEUE:
251 		return p_mqueue(f);
252 	case NL_KQUEUE:
253 		return p_kqueue(f);
254 	case NL_RND:
255 		printf("* random %p", f->f_data);
256 		break;
257 	case NL_SEM:
258 		return p_sem(f);
259 	case NL_TAP:
260 		printf("* tap %lu", (unsigned long)(intptr_t)f->f_data);
261 		break;
262 	case NL_CRYPTO:
263 		printf("* crypto %p", f->f_data);
264 		break;
265 	case NL_AUDIO:
266 		printf("* audio %p", f->f_data);
267 		break;
268 	case NL_PAD:
269 		printf("* pad %p", f->f_data);
270 		break;
271 	case NL_MAX:
272 		printf("* %s ops=%p %p", name, f->f_ops, f->f_data);
273 		break;
274 	default:
275 		printf("* %s %p", nl[i].n_name, f->f_data);
276 		break;
277 	}
278 	oprint(f, "\n");
279 	return 0;
280 }
281