misc.c revision 1.8.2.1 1 1.8.2.1 yamt /* $NetBSD: misc.c,v 1.8.2.1 2012/10/30 19:00:17 yamt 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.8.2.1 yamt __RCSID("$NetBSD: misc.c,v 1.8.2.1 2012/10/30 19:00:17 yamt 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.1 christos #undef _KERNEL
52 1.1 christos #include <sys/vnode.h>
53 1.1 christos #include <sys/mount.h>
54 1.1 christos
55 1.1 christos #include <net/bpfdesc.h>
56 1.1 christos
57 1.1 christos #include <err.h>
58 1.1 christos #include <kvm.h>
59 1.1 christos #include "fstat.h"
60 1.1 christos
61 1.1 christos static struct nlist nl[] = {
62 1.8 christos #define NL_BPF 0
63 1.8 christos { .n_name = "bpf_fileops", },
64 1.8 christos #define NL_CRYPTO 1
65 1.8 christos { .n_name = "cryptofops" },
66 1.8 christos #define NL_DMIO 2
67 1.8 christos { .n_name = "dmio_fileops", },
68 1.8 christos #define NL_DRVCTL 3
69 1.8 christos { .n_name = "drvctl_fileops", },
70 1.8 christos #define NL_DTV_DEMUX 4
71 1.8 christos { .n_name = "dtv_demux_fileops", },
72 1.8 christos #define NL_FILEMON 5
73 1.8 christos { .n_name = "filemon_fileops", },
74 1.8 christos #define NL_KQUEUE 6
75 1.1 christos { .n_name = "kqueueops" },
76 1.8 christos #define NL_MQUEUE 7
77 1.1 christos { .n_name = "mqops" },
78 1.8 christos #define NL_PIPE 8
79 1.1 christos { .n_name = "pipeops" },
80 1.8 christos #define NL_PUTTER 9
81 1.8 christos { .n_name = "putter_fileops", },
82 1.8 christos #define NL_SEM 10
83 1.8 christos { .n_name = "semops", },
84 1.8 christos #define NL_SOCKET 11
85 1.1 christos { .n_name = "socketops" },
86 1.8 christos #define NL_SVR4_NET 12
87 1.8 christos { .n_name = "svr4_netops" },
88 1.8 christos #define NL_SVR4_32_NET 13
89 1.8 christos { .n_name = "svr4_32_netops" },
90 1.8 christos #define NL_TAP 14
91 1.8 christos { .n_name = "tap_fileops", },
92 1.8 christos #define NL_VNOPS 15
93 1.1 christos { .n_name = "vnops" },
94 1.8 christos #define NL_XENEVT 16
95 1.8 christos { .n_name = "xenevt_fileops" },
96 1.8 christos #define NL_MAX 17
97 1.1 christos { .n_name = NULL }
98 1.1 christos };
99 1.1 christos
100 1.3 christos extern int vflg;
101 1.3 christos
102 1.1 christos
103 1.1 christos static int
104 1.1 christos p_bpf(struct file *f)
105 1.1 christos {
106 1.1 christos struct bpf_d bpf;
107 1.1 christos
108 1.1 christos if (!KVM_READ(f->f_data, &bpf, sizeof (bpf))) {
109 1.1 christos dprintf("can't read bpf at %p for pid %d", f->f_data, Pid);
110 1.1 christos return 0;
111 1.1 christos }
112 1.1 christos (void)printf("* bpf rec=%lu, dr=%lu, cap=%lu, pid=%lu",
113 1.1 christos bpf.bd_rcount, bpf.bd_dcount, bpf.bd_ccount,
114 1.1 christos (unsigned long)bpf.bd_pid);
115 1.1 christos if (bpf.bd_promisc)
116 1.1 christos (void)printf(", promisc");
117 1.1 christos if (bpf.bd_immediate)
118 1.1 christos (void)printf(", immed");
119 1.1 christos if (bpf.bd_seesent)
120 1.1 christos (void)printf(", seesent");
121 1.8.2.1 yamt if (bpf.bd_jitcode != NULL)
122 1.8.2.1 yamt (void)printf(", jit");
123 1.1 christos if (bpf.bd_async)
124 1.1 christos (void)printf(", asyncgrp=%lu", (unsigned long)bpf.bd_pgid);
125 1.1 christos if (bpf.bd_state == BPF_IDLE)
126 1.1 christos (void)printf(", idle");
127 1.1 christos else if (bpf.bd_state == BPF_WAITING)
128 1.1 christos (void)printf(", waiting");
129 1.1 christos else if (bpf.bd_state == BPF_TIMED_OUT)
130 1.1 christos (void)printf(", timeout");
131 1.1 christos (void)printf("\n");
132 1.1 christos return 0;
133 1.1 christos }
134 1.1 christos
135 1.1 christos static int
136 1.1 christos p_mqueue(struct file *f)
137 1.1 christos {
138 1.1 christos struct mqueue mq;
139 1.1 christos
140 1.1 christos if (!KVM_READ(f->f_data, &mq, sizeof (mq))) {
141 1.1 christos dprintf("can't read mqueue at %p for pid %d", f->f_data, Pid);
142 1.1 christos return 0;
143 1.1 christos }
144 1.1 christos (void)printf("* mqueue \"%s\"\n", mq.mq_name);
145 1.1 christos return 0;
146 1.1 christos }
147 1.2 christos
148 1.2 christos static int
149 1.2 christos p_kqueue(struct file *f)
150 1.2 christos {
151 1.2 christos struct kqueue kq;
152 1.2 christos
153 1.2 christos if (!KVM_READ(f->f_data, &kq, sizeof (kq))) {
154 1.2 christos dprintf("can't read kqueue at %p for pid %d", f->f_data, Pid);
155 1.2 christos return 0;
156 1.2 christos }
157 1.2 christos (void)printf("* kqueue pending %d\n", kq.kq_count);
158 1.2 christos return 0;
159 1.2 christos }
160 1.1 christos
161 1.1 christos int
162 1.1 christos pmisc(struct file *f, const char *name)
163 1.1 christos {
164 1.1 christos size_t i;
165 1.1 christos if (nl[0].n_value == 0) {
166 1.1 christos int n;
167 1.1 christos if ((n = KVM_NLIST(nl)) == -1)
168 1.1 christos errx(1, "Cannot list kernel symbols (%s)",
169 1.1 christos KVM_GETERR());
170 1.3 christos else if (n != 0 && vflg)
171 1.1 christos warnx("Could not find %d symbols", n);
172 1.1 christos }
173 1.1 christos for (i = 0; i < NL_MAX; i++)
174 1.4 lukem if ((uintptr_t)f->f_ops == nl[i].n_value)
175 1.1 christos break;
176 1.1 christos switch (i) {
177 1.1 christos case NL_BPF:
178 1.1 christos return p_bpf(f);
179 1.1 christos case NL_MQUEUE:
180 1.1 christos return p_mqueue(f);
181 1.2 christos case NL_KQUEUE:
182 1.2 christos return p_kqueue(f);
183 1.1 christos case NL_TAP:
184 1.1 christos printf("* tap %lu\n", (unsigned long)(intptr_t)f->f_data);
185 1.1 christos return 0;
186 1.1 christos case NL_CRYPTO:
187 1.2 christos printf("* crypto %p\n", f->f_data);
188 1.2 christos return 0;
189 1.8 christos case NL_MAX:
190 1.8 christos printf("* %s %p\n", name, f->f_data);
191 1.8 christos return 0;
192 1.1 christos default:
193 1.8 christos printf("* %s %p\n", nl[i].n_name, f->f_data);
194 1.1 christos return 0;
195 1.1 christos }
196 1.1 christos }
197