misc.c revision 1.20.2.1 1 /* $NetBSD: misc.c,v 1.20.2.1 2019/06/10 22:10:19 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.20.2.1 2019/06/10 22:10:19 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 (void)printf("\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\n", buf);
187 return 0;
188 }
189 }
190 (void)printf("\n");
191 return 0;
192 }
193
194 static int
195 p_mqueue(struct file *f)
196 {
197 struct mqueue mq;
198
199 if (!KVM_READ(f->f_data, &mq, sizeof(mq))) {
200 dprintf("can't read mqueue at %p for pid %d", f->f_data, Pid);
201 return 0;
202 }
203 (void)printf("* mqueue \"%s\"\n", mq.mq_name);
204 return 0;
205 }
206
207 static int
208 p_rnd(struct file *f)
209 {
210 struct cprng_strong {
211 char cs_name[16];
212 int cs_flags;
213 /*...*/
214 } str;
215 struct rnd_ctx {
216 struct cprng_strong *rc_cprng;
217 bool rc_hard;
218 } ctx;
219 char buf[1024];
220
221 if (!KVM_READ(f->f_data, &ctx, sizeof(ctx))) {
222 dprintf("can't read rnd_ctx at %p for pid %d", f->f_data, Pid);
223 return 0;
224 }
225 if (!KVM_READ(ctx.rc_cprng, &str, sizeof(str))) {
226 dprintf("can't read cprng_strong at %p for pid %d", f->f_data,\
227 Pid);
228 return 0;
229 }
230 snprintb(buf, sizeof(buf), CPRNG_FMT, str.cs_flags);
231 (void)printf("* rnd \"%s\" flags %s\n", str.cs_name, buf);
232 return 0;
233 }
234
235 static int
236 p_kqueue(struct file *f)
237 {
238 struct kqueue kq;
239
240 if (!KVM_READ(f->f_data, &kq, sizeof(kq))) {
241 dprintf("can't read kqueue at %p for pid %d", f->f_data, Pid);
242 return 0;
243 }
244 (void)printf("* kqueue pending %d\n", kq.kq_count);
245 return 0;
246 }
247
248 int
249 pmisc(struct file *f, const char *name)
250 {
251 size_t i;
252 if (nl[0].n_value == 0) {
253 int n;
254 if ((n = KVM_NLIST(nl)) == -1)
255 errx(1, "Cannot list kernel symbols (%s)",
256 KVM_GETERR());
257 else if (n != 0 && vflg) {
258 char buf[1024];
259 buf[0] = '\0';
260 for (struct nlist *l = nl; l->n_name != NULL; l++) {
261 if (l->n_value != 0)
262 continue;
263 strlcat(buf, ", ", sizeof(buf));
264 strlcat(buf, l->n_name, sizeof(buf));
265 }
266 warnx("Could not find %d symbols: %s", n, buf + 2);
267 }
268 }
269 for (i = 0; i < NL_MAX; i++)
270 if ((uintptr_t)f->f_ops == nl[i].n_value)
271 break;
272 switch (i) {
273 case NL_BPF:
274 return p_bpf(f);
275 case NL_MQUEUE:
276 return p_mqueue(f);
277 case NL_KQUEUE:
278 return p_kqueue(f);
279 case NL_RND:
280 return p_rnd(f);
281 case NL_SEM:
282 return p_sem(f);
283 case NL_TAP:
284 printf("* tap %lu\n", (unsigned long)(intptr_t)f->f_data);
285 return 0;
286 case NL_CRYPTO:
287 printf("* crypto %p\n", f->f_data);
288 return 0;
289 case NL_AUDIO:
290 printf("* audio %p\n", f->f_data);
291 return 0;
292 case NL_PAD:
293 printf("* pad %p\n", f->f_data);
294 return 0;
295 case NL_MAX:
296 printf("* %s ops=%p %p\n", name, f->f_ops, f->f_data);
297 return 0;
298 default:
299 printf("* %s %p\n", nl[i].n_name, f->f_data);
300 return 0;
301 }
302 }
303