xref: /netbsd-src/usr.bin/fstat/misc.c (revision 466a16a118933bd295a8a104f095714fadf9cf68)
1 /*	$NetBSD: misc.c,v 1.2 2008/07/23 11:59:43 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.2 2008/07/23 11:59:43 christos 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 
81 static int
82 p_bpf(struct file *f)
83 {
84 	struct bpf_d bpf;
85 
86 	if (!KVM_READ(f->f_data, &bpf, sizeof (bpf))) {
87 		dprintf("can't read bpf at %p for pid %d", f->f_data, Pid);
88 		return 0;
89 	}
90 	(void)printf("* bpf rec=%lu, dr=%lu, cap=%lu, pid=%lu",
91 	    bpf.bd_rcount, bpf.bd_dcount, bpf.bd_ccount,
92 	    (unsigned long)bpf.bd_pid);
93 	if (bpf.bd_promisc)
94 		(void)printf(", promisc");
95 	if (bpf.bd_immediate)
96 		(void)printf(", immed");
97 	if (bpf.bd_seesent)
98 		(void)printf(", seesent");
99 	if (bpf.bd_async)
100 		(void)printf(", asyncgrp=%lu", (unsigned long)bpf.bd_pgid);
101 	if (bpf.bd_state == BPF_IDLE)
102 		(void)printf(", idle");
103 	else if (bpf.bd_state == BPF_WAITING)
104 		(void)printf(", waiting");
105 	else if (bpf.bd_state == BPF_TIMED_OUT)
106 		(void)printf(", timeout");
107 	(void)printf("\n");
108 	return 0;
109 }
110 
111 static int
112 p_mqueue(struct file *f)
113 {
114 	struct mqueue mq;
115 
116 	if (!KVM_READ(f->f_data, &mq, sizeof (mq))) {
117 		dprintf("can't read mqueue at %p for pid %d", f->f_data, Pid);
118 		return 0;
119 	}
120 	(void)printf("* mqueue \"%s\"\n", mq.mq_name);
121 	return 0;
122 }
123 
124 static int
125 p_kqueue(struct file *f)
126 {
127 	struct kqueue kq;
128 
129 	if (!KVM_READ(f->f_data, &kq, sizeof (kq))) {
130 		dprintf("can't read kqueue at %p for pid %d", f->f_data, Pid);
131 		return 0;
132 	}
133 	(void)printf("* kqueue pending %d\n", kq.kq_count);
134 	return 0;
135 }
136 
137 int
138 pmisc(struct file *f, const char *name)
139 {
140 	size_t i;
141 	if (nl[0].n_value == 0) {
142 		int n;
143 		if ((n = KVM_NLIST(nl)) == -1)
144 			errx(1, "Cannot list kernel symbols (%s)",
145 			    KVM_GETERR());
146 		else if (n != 0)
147 			warnx("Could not find %d symbols", n);
148 	}
149 	for (i = 0; i < NL_MAX; i++)
150 		if ((intptr_t)f->f_ops == nl[i].n_value)
151 			break;
152 	switch (i) {
153 	case NL_BPF:
154 		return p_bpf(f);
155 	case NL_MQUEUE:
156 		return p_mqueue(f);
157 	case NL_KQUEUE:
158 		return p_kqueue(f);
159 	case NL_TAP:
160 		printf("* tap %lu\n", (unsigned long)(intptr_t)f->f_data);
161 		return 0;
162 	case NL_CRYPTO:
163 		printf("* crypto %p\n", f->f_data);
164 		return 0;
165 	default:
166 		printf("* %s %p\n", name, f->f_data);
167 		return 0;
168 	}
169 }
170