misc.c revision 1.5 1 1.5 christos /* $NetBSD: misc.c,v 1.5 2009/07/13 17:57:35 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.5 christos __RCSID("$NetBSD: misc.c,v 1.5 2009/07/13 17:57:35 christos Exp $");
34 1.1 christos
35 1.2 christos #include <stdbool.h>
36 1.1 christos #include <sys/param.h>
37 1.5 christos #include <sys/types.h>
38 1.1 christos #include <sys/time.h>
39 1.1 christos #include <sys/stat.h>
40 1.2 christos #include <sys/condvar.h>
41 1.2 christos #include <sys/selinfo.h>
42 1.2 christos #include <sys/filedesc.h>
43 1.2 christos #define _KERNEL
44 1.2 christos #include <sys/mqueue.h>
45 1.2 christos #include <sys/eventvar.h>
46 1.2 christos #undef _KERNEL
47 1.1 christos #include <sys/proc.h>
48 1.1 christos #define _KERNEL
49 1.1 christos #include <sys/file.h>
50 1.1 christos #undef _KERNEL
51 1.1 christos #include <sys/vnode.h>
52 1.1 christos #include <sys/mount.h>
53 1.1 christos
54 1.1 christos #include <net/bpfdesc.h>
55 1.1 christos
56 1.1 christos #include <err.h>
57 1.1 christos #include <kvm.h>
58 1.1 christos #include "fstat.h"
59 1.1 christos
60 1.1 christos static struct nlist nl[] = {
61 1.1 christos #define NL_KQUEUE 0
62 1.1 christos { .n_name = "kqueueops" },
63 1.1 christos #define NL_MQUEUE 1
64 1.1 christos { .n_name = "mqops" },
65 1.1 christos #define NL_PIPE 2
66 1.1 christos { .n_name = "pipeops" },
67 1.1 christos #define NL_SOCKET 3
68 1.1 christos { .n_name = "socketops" },
69 1.1 christos #define NL_VNOPS 4
70 1.1 christos { .n_name = "vnops" },
71 1.1 christos #define NL_CRYPTO 5
72 1.1 christos { .n_name = "cryptofops" },
73 1.1 christos #define NL_BPF 6
74 1.1 christos { .n_name = "bpf_fileops", },
75 1.1 christos #define NL_TAP 7
76 1.1 christos { .n_name = "tap_fileops", },
77 1.1 christos #define NL_MAX 8
78 1.1 christos { .n_name = NULL }
79 1.1 christos };
80 1.1 christos
81 1.3 christos extern int vflg;
82 1.3 christos
83 1.1 christos
84 1.1 christos static int
85 1.1 christos p_bpf(struct file *f)
86 1.1 christos {
87 1.1 christos struct bpf_d bpf;
88 1.1 christos
89 1.1 christos if (!KVM_READ(f->f_data, &bpf, sizeof (bpf))) {
90 1.1 christos dprintf("can't read bpf at %p for pid %d", f->f_data, Pid);
91 1.1 christos return 0;
92 1.1 christos }
93 1.1 christos (void)printf("* bpf rec=%lu, dr=%lu, cap=%lu, pid=%lu",
94 1.1 christos bpf.bd_rcount, bpf.bd_dcount, bpf.bd_ccount,
95 1.1 christos (unsigned long)bpf.bd_pid);
96 1.1 christos if (bpf.bd_promisc)
97 1.1 christos (void)printf(", promisc");
98 1.1 christos if (bpf.bd_immediate)
99 1.1 christos (void)printf(", immed");
100 1.1 christos if (bpf.bd_seesent)
101 1.1 christos (void)printf(", seesent");
102 1.1 christos if (bpf.bd_async)
103 1.1 christos (void)printf(", asyncgrp=%lu", (unsigned long)bpf.bd_pgid);
104 1.1 christos if (bpf.bd_state == BPF_IDLE)
105 1.1 christos (void)printf(", idle");
106 1.1 christos else if (bpf.bd_state == BPF_WAITING)
107 1.1 christos (void)printf(", waiting");
108 1.1 christos else if (bpf.bd_state == BPF_TIMED_OUT)
109 1.1 christos (void)printf(", timeout");
110 1.1 christos (void)printf("\n");
111 1.1 christos return 0;
112 1.1 christos }
113 1.1 christos
114 1.1 christos static int
115 1.1 christos p_mqueue(struct file *f)
116 1.1 christos {
117 1.1 christos struct mqueue mq;
118 1.1 christos
119 1.1 christos if (!KVM_READ(f->f_data, &mq, sizeof (mq))) {
120 1.1 christos dprintf("can't read mqueue at %p for pid %d", f->f_data, Pid);
121 1.1 christos return 0;
122 1.1 christos }
123 1.1 christos (void)printf("* mqueue \"%s\"\n", mq.mq_name);
124 1.1 christos return 0;
125 1.1 christos }
126 1.2 christos
127 1.2 christos static int
128 1.2 christos p_kqueue(struct file *f)
129 1.2 christos {
130 1.2 christos struct kqueue kq;
131 1.2 christos
132 1.2 christos if (!KVM_READ(f->f_data, &kq, sizeof (kq))) {
133 1.2 christos dprintf("can't read kqueue at %p for pid %d", f->f_data, Pid);
134 1.2 christos return 0;
135 1.2 christos }
136 1.2 christos (void)printf("* kqueue pending %d\n", kq.kq_count);
137 1.2 christos return 0;
138 1.2 christos }
139 1.1 christos
140 1.1 christos int
141 1.1 christos pmisc(struct file *f, const char *name)
142 1.1 christos {
143 1.1 christos size_t i;
144 1.1 christos if (nl[0].n_value == 0) {
145 1.1 christos int n;
146 1.1 christos if ((n = KVM_NLIST(nl)) == -1)
147 1.1 christos errx(1, "Cannot list kernel symbols (%s)",
148 1.1 christos KVM_GETERR());
149 1.3 christos else if (n != 0 && vflg)
150 1.1 christos warnx("Could not find %d symbols", n);
151 1.1 christos }
152 1.1 christos for (i = 0; i < NL_MAX; i++)
153 1.4 lukem if ((uintptr_t)f->f_ops == nl[i].n_value)
154 1.1 christos break;
155 1.1 christos switch (i) {
156 1.1 christos case NL_BPF:
157 1.1 christos return p_bpf(f);
158 1.1 christos case NL_MQUEUE:
159 1.1 christos return p_mqueue(f);
160 1.2 christos case NL_KQUEUE:
161 1.2 christos return p_kqueue(f);
162 1.1 christos case NL_TAP:
163 1.1 christos printf("* tap %lu\n", (unsigned long)(intptr_t)f->f_data);
164 1.1 christos return 0;
165 1.1 christos case NL_CRYPTO:
166 1.2 christos printf("* crypto %p\n", f->f_data);
167 1.2 christos return 0;
168 1.1 christos default:
169 1.1 christos printf("* %s %p\n", name, f->f_data);
170 1.1 christos return 0;
171 1.1 christos }
172 1.1 christos }
173