xref: /netbsd-src/usr.bin/fstat/misc.c (revision c9496f6b604074a9451a67df576a5b423068e71e)
1 /*	$NetBSD: misc.c,v 1.17 2016/12/30 21:08:23 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.17 2016/12/30 21:08:23 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 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_MAX		18
106     { .n_name = NULL }
107 };
108 
109 extern int vflg;
110 
111 
112 static int
113 p_bpf(struct file *f)
114 {
115 	struct bpf_d bpf;
116 	struct bpf_if bi;
117 	struct ifnet ifn;
118 
119 	strlcpy(ifn.if_xname, "???", sizeof(ifn.if_xname));
120 
121 	if (!KVM_READ(f->f_data, &bpf, sizeof(bpf))) {
122 		dprintf("can't read bpf at %p for pid %d", f->f_data, Pid);
123 		return 0;
124 	}
125 	if (bpf.bd_bif != NULL) {
126 		if (!KVM_READ(bpf.bd_bif, &bi, sizeof(bi)))
127 			dprintf("can't read bpf interface at %p for pid %d",
128 			    bpf.bd_bif, Pid);
129 		if (bi.bif_ifp != NULL)
130 			if (!KVM_READ(bi.bif_ifp, &ifn, sizeof(ifn)))
131 				dprintf("can't read net interfsace"
132 				    " at %p for pid %d", bi.bif_ifp, Pid);
133 	}
134 	(void)printf("* bpf@%s rec=%lu, dr=%lu, cap=%lu, pid=%lu", ifn.if_xname,
135 	    bpf.bd_rcount, bpf.bd_dcount, bpf.bd_ccount,
136 	    (unsigned long)bpf.bd_pid);
137 	if (bpf.bd_promisc)
138 		(void)printf(", promisc");
139 	if (bpf.bd_immediate)
140 		(void)printf(", immed");
141 	if (bpf.bd_seesent)
142 		(void)printf(", seesent");
143 	if (bpf.bd_jitcode != NULL)
144 		(void)printf(", jit");
145 	if (bpf.bd_async)
146 		(void)printf(", asyncgrp=%lu", (unsigned long)bpf.bd_pgid);
147 	if (bpf.bd_state == BPF_IDLE)
148 		(void)printf(", idle");
149 	else if (bpf.bd_state == BPF_WAITING)
150 		(void)printf(", waiting");
151 	else if (bpf.bd_state == BPF_TIMED_OUT)
152 		(void)printf(", timeout");
153 	(void)printf("\n");
154 	return 0;
155 }
156 
157 static int
158 p_sem(struct file *f)
159 {
160 	ksem_t ks;
161 	if (!KVM_READ(f->f_data, &ks, sizeof(ks))) {
162 		dprintf("can't read sem at %p for pid %d", f->f_data, Pid);
163 		return 0;
164 	}
165 	(void)printf("* ksem ref=%u, value=%u, waiters=%u, flags=0x%x, "
166 	    "mode=%o, uid=%u, gid=%u", ks.ks_ref, ks.ks_value, ks.ks_waiters,
167 	    ks.ks_flags, ks.ks_mode, ks.ks_uid, ks.ks_gid);
168 	if (ks.ks_name && ks.ks_namelen) {
169 		char buf[64];
170 		if (ks.ks_namelen >= sizeof(buf))
171 			ks.ks_namelen = sizeof(buf) - 1;
172 		if (!KVM_READ(ks.ks_name, buf, ks.ks_namelen)) {
173 			dprintf("can't read sem name at %p for pid %d",
174 			    ks.ks_name, Pid);
175 		} else {
176 			buf[ks.ks_namelen] = '\0';
177 			(void)printf(", name=%s\n", buf);
178 			return 0;
179 		}
180 	}
181 	(void)printf("\n");
182 	return 0;
183 }
184 
185 static int
186 p_mqueue(struct file *f)
187 {
188 	struct mqueue mq;
189 
190 	if (!KVM_READ(f->f_data, &mq, sizeof(mq))) {
191 		dprintf("can't read mqueue at %p for pid %d", f->f_data, Pid);
192 		return 0;
193 	}
194 	(void)printf("* mqueue \"%s\"\n", mq.mq_name);
195 	return 0;
196 }
197 
198 static int
199 p_rnd(struct file *f)
200 {
201 	struct cprng_strong {
202 		char cs_name[16];
203 		int  cs_flags;
204 		/*...*/
205 	} str;
206 	struct rnd_ctx {
207 		struct cprng_strong *rc_cprng;
208 		bool rc_hard;
209 	} ctx;
210 	char buf[1024];
211 
212 	if (!KVM_READ(f->f_data, &ctx, sizeof(ctx))) {
213 		dprintf("can't read rnd_ctx at %p for pid %d", f->f_data, Pid);
214 		return 0;
215 	}
216 	if (!KVM_READ(ctx.rc_cprng, &str, sizeof(str))) {
217 		dprintf("can't read cprng_strong at %p for pid %d", f->f_data,\
218 		    Pid);
219 		return 0;
220 	}
221 	snprintb(buf, sizeof(buf), CPRNG_FMT, str.cs_flags);
222 	(void)printf("* rnd \"%s\" flags %s\n", str.cs_name, buf);
223 	return 0;
224 }
225 
226 static int
227 p_kqueue(struct file *f)
228 {
229 	struct kqueue kq;
230 
231 	if (!KVM_READ(f->f_data, &kq, sizeof(kq))) {
232 		dprintf("can't read kqueue at %p for pid %d", f->f_data, Pid);
233 		return 0;
234 	}
235 	(void)printf("* kqueue pending %d\n", kq.kq_count);
236 	return 0;
237 }
238 
239 int
240 pmisc(struct file *f, const char *name)
241 {
242 	size_t i;
243 	if (nl[0].n_value == 0) {
244 		int n;
245 		if ((n = KVM_NLIST(nl)) == -1)
246 			errx(1, "Cannot list kernel symbols (%s)",
247 			    KVM_GETERR());
248 		else if (n != 0 && vflg) {
249 			char buf[1024];
250 			buf[0] = '\0';
251 			for (struct nlist *l = nl; l->n_name != NULL; l++) {
252 				if (l->n_value != 0)
253 					continue;
254 				strlcat(buf, ", ", sizeof(buf));
255 				strlcat(buf, l->n_name, sizeof(buf));
256 			}
257 			warnx("Could not find %d symbols: %s", n, buf + 2);
258 		}
259 	}
260 	for (i = 0; i < NL_MAX; i++)
261 		if ((uintptr_t)f->f_ops == nl[i].n_value)
262 			break;
263 	switch (i) {
264 	case NL_BPF:
265 		return p_bpf(f);
266 	case NL_MQUEUE:
267 		return p_mqueue(f);
268 	case NL_KQUEUE:
269 		return p_kqueue(f);
270 	case NL_RND:
271 		return p_rnd(f);
272 	case NL_SEM:
273 		return p_sem(f);
274 	case NL_TAP:
275 		printf("* tap %lu\n", (unsigned long)(intptr_t)f->f_data);
276 		return 0;
277 	case NL_CRYPTO:
278 		printf("* crypto %p\n", f->f_data);
279 		return 0;
280 	case NL_MAX:
281 		printf("* %s ops=%p %p\n", name, f->f_ops, f->f_data);
282 		return 0;
283 	default:
284 		printf("* %s %p\n", nl[i].n_name, f->f_data);
285 		return 0;
286 	}
287 }
288