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