xref: /netbsd-src/usr.bin/fstat/misc.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: misc.c,v 1.7 2009/07/13 21:44:32 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.7 2009/07/13 21:44:32 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_KQUEUE	0
63     { .n_name = "kqueueops" },
64 #define NL_MQUEUE	1
65     { .n_name = "mqops" },
66 #define NL_PIPE		2
67     { .n_name = "pipeops" },
68 #define NL_SOCKET	3
69     { .n_name = "socketops" },
70 #define NL_VNOPS	4
71     { .n_name = "vnops" },
72 #define NL_CRYPTO	5
73     { .n_name = "cryptofops" },
74 #define NL_BPF		6
75     { .n_name = "bpf_fileops", },
76 #define NL_TAP		7
77     { .n_name = "tap_fileops", },
78 #define NL_MAX		8
79     { .n_name = NULL }
80 };
81 
82 extern int vflg;
83 
84 
85 static int
86 p_bpf(struct file *f)
87 {
88 	struct bpf_d bpf;
89 
90 	if (!KVM_READ(f->f_data, &bpf, sizeof (bpf))) {
91 		dprintf("can't read bpf at %p for pid %d", f->f_data, Pid);
92 		return 0;
93 	}
94 	(void)printf("* bpf rec=%lu, dr=%lu, cap=%lu, pid=%lu",
95 	    bpf.bd_rcount, bpf.bd_dcount, bpf.bd_ccount,
96 	    (unsigned long)bpf.bd_pid);
97 	if (bpf.bd_promisc)
98 		(void)printf(", promisc");
99 	if (bpf.bd_immediate)
100 		(void)printf(", immed");
101 	if (bpf.bd_seesent)
102 		(void)printf(", seesent");
103 	if (bpf.bd_async)
104 		(void)printf(", asyncgrp=%lu", (unsigned long)bpf.bd_pgid);
105 	if (bpf.bd_state == BPF_IDLE)
106 		(void)printf(", idle");
107 	else if (bpf.bd_state == BPF_WAITING)
108 		(void)printf(", waiting");
109 	else if (bpf.bd_state == BPF_TIMED_OUT)
110 		(void)printf(", timeout");
111 	(void)printf("\n");
112 	return 0;
113 }
114 
115 static int
116 p_mqueue(struct file *f)
117 {
118 	struct mqueue mq;
119 
120 	if (!KVM_READ(f->f_data, &mq, sizeof (mq))) {
121 		dprintf("can't read mqueue at %p for pid %d", f->f_data, Pid);
122 		return 0;
123 	}
124 	(void)printf("* mqueue \"%s\"\n", mq.mq_name);
125 	return 0;
126 }
127 
128 static int
129 p_kqueue(struct file *f)
130 {
131 	struct kqueue kq;
132 
133 	if (!KVM_READ(f->f_data, &kq, sizeof (kq))) {
134 		dprintf("can't read kqueue at %p for pid %d", f->f_data, Pid);
135 		return 0;
136 	}
137 	(void)printf("* kqueue pending %d\n", kq.kq_count);
138 	return 0;
139 }
140 
141 int
142 pmisc(struct file *f, const char *name)
143 {
144 	size_t i;
145 	if (nl[0].n_value == 0) {
146 		int n;
147 		if ((n = KVM_NLIST(nl)) == -1)
148 			errx(1, "Cannot list kernel symbols (%s)",
149 			    KVM_GETERR());
150 		else if (n != 0 && vflg)
151 			warnx("Could not find %d symbols", n);
152 	}
153 	for (i = 0; i < NL_MAX; i++)
154 		if ((uintptr_t)f->f_ops == nl[i].n_value)
155 			break;
156 	switch (i) {
157 	case NL_BPF:
158 		return p_bpf(f);
159 	case NL_MQUEUE:
160 		return p_mqueue(f);
161 	case NL_KQUEUE:
162 		return p_kqueue(f);
163 	case NL_TAP:
164 		printf("* tap %lu\n", (unsigned long)(intptr_t)f->f_data);
165 		return 0;
166 	case NL_CRYPTO:
167 		printf("* crypto %p\n", f->f_data);
168 		return 0;
169 	default:
170 		printf("* %s %p\n", name, f->f_data);
171 		return 0;
172 	}
173 }
174