misc.c revision 1.14 1 /* $NetBSD: misc.c,v 1.14 2014/08/10 16:44:37 tls 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.14 2014/08/10 16:44:37 tls 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_SEM 10
91 { .n_name = "semops", },
92 #define NL_SOCKET 11
93 { .n_name = "socketops" },
94 #define NL_SVR4_NET 12
95 { .n_name = "svr4_netops" },
96 #define NL_SVR4_32_NET 13
97 { .n_name = "svr4_32_netops" },
98 #define NL_TAP 14
99 { .n_name = "tap_fileops", },
100 #define NL_VNOPS 15
101 { .n_name = "vnops" },
102 #define NL_XENEVT 16
103 { .n_name = "xenevt_fileops" },
104 #define NL_MAX 18
105 { .n_name = NULL }
106 };
107
108 extern int vflg;
109
110
111 static int
112 p_bpf(struct file *f)
113 {
114 struct bpf_d bpf;
115
116 if (!KVM_READ(f->f_data, &bpf, sizeof(bpf))) {
117 dprintf("can't read bpf at %p for pid %d", f->f_data, Pid);
118 return 0;
119 }
120 (void)printf("* bpf rec=%lu, dr=%lu, cap=%lu, pid=%lu",
121 bpf.bd_rcount, bpf.bd_dcount, bpf.bd_ccount,
122 (unsigned long)bpf.bd_pid);
123 if (bpf.bd_promisc)
124 (void)printf(", promisc");
125 if (bpf.bd_immediate)
126 (void)printf(", immed");
127 if (bpf.bd_seesent)
128 (void)printf(", seesent");
129 if (bpf.bd_jitcode != NULL)
130 (void)printf(", jit");
131 if (bpf.bd_async)
132 (void)printf(", asyncgrp=%lu", (unsigned long)bpf.bd_pgid);
133 if (bpf.bd_state == BPF_IDLE)
134 (void)printf(", idle");
135 else if (bpf.bd_state == BPF_WAITING)
136 (void)printf(", waiting");
137 else if (bpf.bd_state == BPF_TIMED_OUT)
138 (void)printf(", timeout");
139 (void)printf("\n");
140 return 0;
141 }
142
143 static int
144 p_sem(struct file *f)
145 {
146 ksem_t ks;
147 if (!KVM_READ(f->f_data, &ks, sizeof(ks))) {
148 dprintf("can't read sem at %p for pid %d", f->f_data, Pid);
149 return 0;
150 }
151 (void)printf("* ksem ref=%u, value=%u, waiters=%u, flags=0x%x, "
152 "mode=%o, uid=%u, gid=%u", ks.ks_ref, ks.ks_value, ks.ks_waiters,
153 ks.ks_flags, ks.ks_mode, ks.ks_uid, ks.ks_gid);
154 if (ks.ks_name && ks.ks_namelen) {
155 char buf[64];
156 if (ks.ks_namelen >= sizeof(buf))
157 ks.ks_namelen = sizeof(buf) - 1;
158 if (!KVM_READ(ks.ks_name, buf, ks.ks_namelen)) {
159 dprintf("can't read sem name at %p for pid %d",
160 ks.ks_name, Pid);
161 } else {
162 buf[ks.ks_namelen] = '\0';
163 (void)printf(", name=%s\n", buf);
164 return 0;
165 }
166 }
167 (void)printf("\n");
168 return 0;
169 }
170
171 static int
172 p_mqueue(struct file *f)
173 {
174 struct mqueue mq;
175
176 if (!KVM_READ(f->f_data, &mq, sizeof(mq))) {
177 dprintf("can't read mqueue at %p for pid %d", f->f_data, Pid);
178 return 0;
179 }
180 (void)printf("* mqueue \"%s\"\n", mq.mq_name);
181 return 0;
182 }
183
184 static int
185 p_kqueue(struct file *f)
186 {
187 struct kqueue kq;
188
189 if (!KVM_READ(f->f_data, &kq, sizeof(kq))) {
190 dprintf("can't read kqueue at %p for pid %d", f->f_data, Pid);
191 return 0;
192 }
193 (void)printf("* kqueue pending %d\n", kq.kq_count);
194 return 0;
195 }
196
197 int
198 pmisc(struct file *f, const char *name)
199 {
200 size_t i;
201 if (nl[0].n_value == 0) {
202 int n;
203 if ((n = KVM_NLIST(nl)) == -1)
204 errx(1, "Cannot list kernel symbols (%s)",
205 KVM_GETERR());
206 else if (n != 0 && vflg) {
207 char buf[1024];
208 buf[0] = '\0';
209 for (struct nlist *l = nl; l->n_name != NULL; l++) {
210 if (l->n_value != 0)
211 continue;
212 strlcat(buf, ", ", sizeof(buf));
213 strlcat(buf, l->n_name, sizeof(buf));
214 }
215 warnx("Could not find %d symbols: %s", n, buf + 2);
216 }
217 }
218 for (i = 0; i < NL_MAX; i++)
219 if ((uintptr_t)f->f_ops == nl[i].n_value)
220 break;
221 switch (i) {
222 case NL_BPF:
223 return p_bpf(f);
224 case NL_MQUEUE:
225 return p_mqueue(f);
226 case NL_KQUEUE:
227 return p_kqueue(f);
228 case NL_SEM:
229 return p_sem(f);
230 case NL_TAP:
231 printf("* tap %lu\n", (unsigned long)(intptr_t)f->f_data);
232 return 0;
233 case NL_CRYPTO:
234 printf("* crypto %p\n", f->f_data);
235 return 0;
236 case NL_MAX:
237 printf("* %s ops=%p %p\n", name, f->f_ops, f->f_data);
238 return 0;
239 default:
240 printf("* %s %p\n", nl[i].n_name, f->f_data);
241 return 0;
242 }
243 }
244