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