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