misc.c revision 1.15 1 1.15 christos /* $NetBSD: misc.c,v 1.15 2015/12/30 18:15:38 christos Exp $ */
2 1.1 christos
3 1.1 christos /*-
4 1.1 christos * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 1.1 christos * All rights reserved.
6 1.1 christos *
7 1.1 christos * This code is derived from software contributed to The NetBSD Foundation
8 1.1 christos * by Christos Zoulas
9 1.1 christos *
10 1.1 christos * Redistribution and use in source and binary forms, with or without
11 1.1 christos * modification, are permitted provided that the following conditions
12 1.1 christos * are met:
13 1.1 christos * 1. Redistributions of source code must retain the above copyright
14 1.1 christos * notice, this list of conditions and the following disclaimer.
15 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 christos * notice, this list of conditions and the following disclaimer in the
17 1.1 christos * documentation and/or other materials provided with the distribution.
18 1.1 christos *
19 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 christos * POSSIBILITY OF SUCH DAMAGE.
30 1.1 christos */
31 1.1 christos
32 1.1 christos #include <sys/cdefs.h>
33 1.15 christos __RCSID("$NetBSD: misc.c,v 1.15 2015/12/30 18:15:38 christos Exp $");
34 1.1 christos
35 1.7 christos #define _KMEMUSER
36 1.2 christos #include <stdbool.h>
37 1.1 christos #include <sys/param.h>
38 1.5 christos #include <sys/types.h>
39 1.1 christos #include <sys/time.h>
40 1.1 christos #include <sys/stat.h>
41 1.2 christos #include <sys/condvar.h>
42 1.2 christos #include <sys/selinfo.h>
43 1.2 christos #include <sys/filedesc.h>
44 1.2 christos #define _KERNEL
45 1.2 christos #include <sys/mqueue.h>
46 1.2 christos #include <sys/eventvar.h>
47 1.2 christos #undef _KERNEL
48 1.1 christos #include <sys/proc.h>
49 1.1 christos #define _KERNEL
50 1.1 christos #include <sys/file.h>
51 1.10 christos #define copyout_t int
52 1.10 christos #include <sys/ksem.h>
53 1.11 christos #define _LIB_LIBKERN_LIBKERN_H_
54 1.11 christos #define mutex_enter(a)
55 1.11 christos #define mutex_exit(a)
56 1.14 tls #undef _KERNEL
57 1.11 christos #include <sys/cprng.h>
58 1.1 christos #include <sys/vnode.h>
59 1.1 christos #include <sys/mount.h>
60 1.1 christos
61 1.1 christos #include <net/bpfdesc.h>
62 1.1 christos
63 1.1 christos #include <err.h>
64 1.11 christos #include <util.h>
65 1.10 christos #include <string.h>
66 1.1 christos #include <kvm.h>
67 1.1 christos #include "fstat.h"
68 1.1 christos
69 1.1 christos static struct nlist nl[] = {
70 1.8 christos #define NL_BPF 0
71 1.8 christos { .n_name = "bpf_fileops", },
72 1.8 christos #define NL_CRYPTO 1
73 1.8 christos { .n_name = "cryptofops" },
74 1.8 christos #define NL_DMIO 2
75 1.8 christos { .n_name = "dmio_fileops", },
76 1.8 christos #define NL_DRVCTL 3
77 1.8 christos { .n_name = "drvctl_fileops", },
78 1.8 christos #define NL_DTV_DEMUX 4
79 1.8 christos { .n_name = "dtv_demux_fileops", },
80 1.8 christos #define NL_FILEMON 5
81 1.8 christos { .n_name = "filemon_fileops", },
82 1.8 christos #define NL_KQUEUE 6
83 1.1 christos { .n_name = "kqueueops" },
84 1.8 christos #define NL_MQUEUE 7
85 1.1 christos { .n_name = "mqops" },
86 1.8 christos #define NL_PIPE 8
87 1.1 christos { .n_name = "pipeops" },
88 1.8 christos #define NL_PUTTER 9
89 1.8 christos { .n_name = "putter_fileops", },
90 1.15 christos #define NL_RND 10
91 1.15 christos { .n_name = "rnd_fileops", },
92 1.15 christos #define NL_SEM 11
93 1.8 christos { .n_name = "semops", },
94 1.15 christos #define NL_SOCKET 12
95 1.1 christos { .n_name = "socketops" },
96 1.15 christos #define NL_SVR4_NET 13
97 1.8 christos { .n_name = "svr4_netops" },
98 1.15 christos #define NL_SVR4_32_NET 14
99 1.8 christos { .n_name = "svr4_32_netops" },
100 1.15 christos #define NL_TAP 15
101 1.8 christos { .n_name = "tap_fileops", },
102 1.15 christos #define NL_VNOPS 16
103 1.1 christos { .n_name = "vnops" },
104 1.15 christos #define NL_XENEVT 17
105 1.8 christos { .n_name = "xenevt_fileops" },
106 1.11 christos #define NL_MAX 18
107 1.1 christos { .n_name = NULL }
108 1.1 christos };
109 1.1 christos
110 1.3 christos extern int vflg;
111 1.3 christos
112 1.1 christos
113 1.1 christos static int
114 1.1 christos p_bpf(struct file *f)
115 1.1 christos {
116 1.1 christos struct bpf_d bpf;
117 1.1 christos
118 1.10 christos if (!KVM_READ(f->f_data, &bpf, sizeof(bpf))) {
119 1.1 christos dprintf("can't read bpf at %p for pid %d", f->f_data, Pid);
120 1.1 christos return 0;
121 1.1 christos }
122 1.1 christos (void)printf("* bpf rec=%lu, dr=%lu, cap=%lu, pid=%lu",
123 1.1 christos bpf.bd_rcount, bpf.bd_dcount, bpf.bd_ccount,
124 1.1 christos (unsigned long)bpf.bd_pid);
125 1.1 christos if (bpf.bd_promisc)
126 1.1 christos (void)printf(", promisc");
127 1.1 christos if (bpf.bd_immediate)
128 1.1 christos (void)printf(", immed");
129 1.1 christos if (bpf.bd_seesent)
130 1.1 christos (void)printf(", seesent");
131 1.9 alnsn if (bpf.bd_jitcode != NULL)
132 1.9 alnsn (void)printf(", jit");
133 1.1 christos if (bpf.bd_async)
134 1.1 christos (void)printf(", asyncgrp=%lu", (unsigned long)bpf.bd_pgid);
135 1.1 christos if (bpf.bd_state == BPF_IDLE)
136 1.1 christos (void)printf(", idle");
137 1.1 christos else if (bpf.bd_state == BPF_WAITING)
138 1.1 christos (void)printf(", waiting");
139 1.1 christos else if (bpf.bd_state == BPF_TIMED_OUT)
140 1.1 christos (void)printf(", timeout");
141 1.1 christos (void)printf("\n");
142 1.1 christos return 0;
143 1.1 christos }
144 1.1 christos
145 1.1 christos static int
146 1.10 christos p_sem(struct file *f)
147 1.10 christos {
148 1.10 christos ksem_t ks;
149 1.10 christos if (!KVM_READ(f->f_data, &ks, sizeof(ks))) {
150 1.10 christos dprintf("can't read sem at %p for pid %d", f->f_data, Pid);
151 1.10 christos return 0;
152 1.10 christos }
153 1.10 christos (void)printf("* ksem ref=%u, value=%u, waiters=%u, flags=0x%x, "
154 1.10 christos "mode=%o, uid=%u, gid=%u", ks.ks_ref, ks.ks_value, ks.ks_waiters,
155 1.10 christos ks.ks_flags, ks.ks_mode, ks.ks_uid, ks.ks_gid);
156 1.10 christos if (ks.ks_name && ks.ks_namelen) {
157 1.10 christos char buf[64];
158 1.10 christos if (ks.ks_namelen >= sizeof(buf))
159 1.10 christos ks.ks_namelen = sizeof(buf) - 1;
160 1.10 christos if (!KVM_READ(ks.ks_name, buf, ks.ks_namelen)) {
161 1.10 christos dprintf("can't read sem name at %p for pid %d",
162 1.10 christos ks.ks_name, Pid);
163 1.10 christos } else {
164 1.10 christos buf[ks.ks_namelen] = '\0';
165 1.10 christos (void)printf(", name=%s\n", buf);
166 1.10 christos return 0;
167 1.10 christos }
168 1.10 christos }
169 1.10 christos (void)printf("\n");
170 1.10 christos return 0;
171 1.10 christos }
172 1.10 christos
173 1.10 christos static int
174 1.1 christos p_mqueue(struct file *f)
175 1.1 christos {
176 1.1 christos struct mqueue mq;
177 1.1 christos
178 1.10 christos if (!KVM_READ(f->f_data, &mq, sizeof(mq))) {
179 1.1 christos dprintf("can't read mqueue at %p for pid %d", f->f_data, Pid);
180 1.1 christos return 0;
181 1.1 christos }
182 1.1 christos (void)printf("* mqueue \"%s\"\n", mq.mq_name);
183 1.1 christos return 0;
184 1.1 christos }
185 1.2 christos
186 1.2 christos static int
187 1.15 christos p_rnd(struct file *f)
188 1.15 christos {
189 1.15 christos struct cprng_strong {
190 1.15 christos char cs_name[16];
191 1.15 christos int cs_flags;
192 1.15 christos /*...*/
193 1.15 christos } str;
194 1.15 christos struct rnd_ctx {
195 1.15 christos struct cprng_strong *rc_cprng;
196 1.15 christos bool rc_hard;
197 1.15 christos } ctx;
198 1.15 christos char buf[1024];
199 1.15 christos
200 1.15 christos if (!KVM_READ(f->f_data, &ctx, sizeof(ctx))) {
201 1.15 christos dprintf("can't read rnd_ctx at %p for pid %d", f->f_data, Pid);
202 1.15 christos return 0;
203 1.15 christos }
204 1.15 christos if (!KVM_READ(ctx.rc_cprng, &str, sizeof(str))) {
205 1.15 christos dprintf("can't read cprng_strong at %p for pid %d", f->f_data,\
206 1.15 christos Pid);
207 1.15 christos return 0;
208 1.15 christos }
209 1.15 christos snprintb(buf, sizeof(buf), CPRNG_FMT, str.cs_flags);
210 1.15 christos (void)printf("* rnd \"%s\" flags %s\n", str.cs_name, buf);
211 1.15 christos return 0;
212 1.15 christos }
213 1.15 christos
214 1.15 christos static int
215 1.2 christos p_kqueue(struct file *f)
216 1.2 christos {
217 1.2 christos struct kqueue kq;
218 1.2 christos
219 1.10 christos if (!KVM_READ(f->f_data, &kq, sizeof(kq))) {
220 1.2 christos dprintf("can't read kqueue at %p for pid %d", f->f_data, Pid);
221 1.2 christos return 0;
222 1.2 christos }
223 1.2 christos (void)printf("* kqueue pending %d\n", kq.kq_count);
224 1.2 christos return 0;
225 1.2 christos }
226 1.1 christos
227 1.1 christos int
228 1.1 christos pmisc(struct file *f, const char *name)
229 1.1 christos {
230 1.1 christos size_t i;
231 1.1 christos if (nl[0].n_value == 0) {
232 1.1 christos int n;
233 1.1 christos if ((n = KVM_NLIST(nl)) == -1)
234 1.1 christos errx(1, "Cannot list kernel symbols (%s)",
235 1.1 christos KVM_GETERR());
236 1.10 christos else if (n != 0 && vflg) {
237 1.10 christos char buf[1024];
238 1.10 christos buf[0] = '\0';
239 1.10 christos for (struct nlist *l = nl; l->n_name != NULL; l++) {
240 1.10 christos if (l->n_value != 0)
241 1.10 christos continue;
242 1.10 christos strlcat(buf, ", ", sizeof(buf));
243 1.10 christos strlcat(buf, l->n_name, sizeof(buf));
244 1.10 christos }
245 1.10 christos warnx("Could not find %d symbols: %s", n, buf + 2);
246 1.10 christos }
247 1.1 christos }
248 1.1 christos for (i = 0; i < NL_MAX; i++)
249 1.4 lukem if ((uintptr_t)f->f_ops == nl[i].n_value)
250 1.1 christos break;
251 1.1 christos switch (i) {
252 1.1 christos case NL_BPF:
253 1.1 christos return p_bpf(f);
254 1.1 christos case NL_MQUEUE:
255 1.1 christos return p_mqueue(f);
256 1.2 christos case NL_KQUEUE:
257 1.2 christos return p_kqueue(f);
258 1.15 christos case NL_RND:
259 1.15 christos return p_rnd(f);
260 1.10 christos case NL_SEM:
261 1.10 christos return p_sem(f);
262 1.1 christos case NL_TAP:
263 1.1 christos printf("* tap %lu\n", (unsigned long)(intptr_t)f->f_data);
264 1.1 christos return 0;
265 1.1 christos case NL_CRYPTO:
266 1.2 christos printf("* crypto %p\n", f->f_data);
267 1.2 christos return 0;
268 1.8 christos case NL_MAX:
269 1.11 christos printf("* %s ops=%p %p\n", name, f->f_ops, f->f_data);
270 1.8 christos return 0;
271 1.1 christos default:
272 1.8 christos printf("* %s %p\n", nl[i].n_name, f->f_data);
273 1.1 christos return 0;
274 1.1 christos }
275 1.1 christos }
276