xref: /netbsd-src/usr.bin/fstat/misc.c (revision 6cd39ddb8550f6fa1bff3fed32053d7f19fd0453)
1 /*	$NetBSD: misc.c,v 1.16 2016/01/23 16:12:03 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.16 2016/01/23 16:12:03 christos Exp $");
34 
35 #include <stdbool.h>
36 #include <sys/param.h>
37 #include <sys/types.h>
38 #include <sys/time.h>
39 #include <sys/stat.h>
40 #include <sys/condvar.h>
41 #include <sys/selinfo.h>
42 #include <sys/filedesc.h>
43 #define _KERNEL
44 #include <sys/mqueue.h>
45 #include <sys/eventvar.h>
46 #undef _KERNEL
47 #include <sys/proc.h>
48 #define _KERNEL
49 #include <sys/file.h>
50 #define copyout_t int
51 #include <sys/ksem.h>
52 #define _LIB_LIBKERN_LIBKERN_H_
53 #define mutex_enter(a)
54 #define mutex_exit(a)
55 #undef _KERNEL
56 #include <sys/cprng.h>
57 #include <sys/vnode.h>
58 #include <sys/mount.h>
59 
60 #include <net/bpfdesc.h>
61 
62 #include <err.h>
63 #include <util.h>
64 #include <string.h>
65 #include <kvm.h>
66 #include "fstat.h"
67 
68 static struct nlist nl[] = {
69 #define NL_BPF		0
70     { .n_name = "bpf_fileops", },
71 #define NL_CRYPTO	1
72     { .n_name = "cryptofops" },
73 #define NL_DMIO		2
74     { .n_name = "dmio_fileops", },
75 #define NL_DRVCTL	3
76     { .n_name = "drvctl_fileops", },
77 #define NL_DTV_DEMUX	4
78     { .n_name = "dtv_demux_fileops", },
79 #define NL_FILEMON	5
80     { .n_name = "filemon_fileops", },
81 #define NL_KQUEUE	6
82     { .n_name = "kqueueops" },
83 #define NL_MQUEUE	7
84     { .n_name = "mqops" },
85 #define NL_PIPE		8
86     { .n_name = "pipeops" },
87 #define NL_PUTTER	9
88     { .n_name = "putter_fileops", },
89 #define NL_RND		10
90     { .n_name = "rnd_fileops", },
91 #define NL_SEM		11
92     { .n_name = "semops", },
93 #define NL_SOCKET	12
94     { .n_name = "socketops" },
95 #define NL_SVR4_NET	13
96     { .n_name = "svr4_netops" },
97 #define NL_SVR4_32_NET	14
98     { .n_name = "svr4_32_netops" },
99 #define NL_TAP		15
100     { .n_name = "tap_fileops", },
101 #define NL_VNOPS	16
102     { .n_name = "vnops" },
103 #define NL_XENEVT	17
104     { .n_name = "xenevt_fileops" },
105 #define NL_MAX		18
106     { .n_name = NULL }
107 };
108 
109 extern int vflg;
110 
111 
112 static int
113 p_bpf(struct file *f)
114 {
115 	struct bpf_d bpf;
116 
117 	if (!KVM_READ(f->f_data, &bpf, sizeof(bpf))) {
118 		dprintf("can't read bpf at %p for pid %d", f->f_data, Pid);
119 		return 0;
120 	}
121 	(void)printf("* bpf rec=%lu, dr=%lu, cap=%lu, pid=%lu",
122 	    bpf.bd_rcount, bpf.bd_dcount, bpf.bd_ccount,
123 	    (unsigned long)bpf.bd_pid);
124 	if (bpf.bd_promisc)
125 		(void)printf(", promisc");
126 	if (bpf.bd_immediate)
127 		(void)printf(", immed");
128 	if (bpf.bd_seesent)
129 		(void)printf(", seesent");
130 	if (bpf.bd_jitcode != NULL)
131 		(void)printf(", jit");
132 	if (bpf.bd_async)
133 		(void)printf(", asyncgrp=%lu", (unsigned long)bpf.bd_pgid);
134 	if (bpf.bd_state == BPF_IDLE)
135 		(void)printf(", idle");
136 	else if (bpf.bd_state == BPF_WAITING)
137 		(void)printf(", waiting");
138 	else if (bpf.bd_state == BPF_TIMED_OUT)
139 		(void)printf(", timeout");
140 	(void)printf("\n");
141 	return 0;
142 }
143 
144 static int
145 p_sem(struct file *f)
146 {
147 	ksem_t ks;
148 	if (!KVM_READ(f->f_data, &ks, sizeof(ks))) {
149 		dprintf("can't read sem at %p for pid %d", f->f_data, Pid);
150 		return 0;
151 	}
152 	(void)printf("* ksem ref=%u, value=%u, waiters=%u, flags=0x%x, "
153 	    "mode=%o, uid=%u, gid=%u", ks.ks_ref, ks.ks_value, ks.ks_waiters,
154 	    ks.ks_flags, ks.ks_mode, ks.ks_uid, ks.ks_gid);
155 	if (ks.ks_name && ks.ks_namelen) {
156 		char buf[64];
157 		if (ks.ks_namelen >= sizeof(buf))
158 			ks.ks_namelen = sizeof(buf) - 1;
159 		if (!KVM_READ(ks.ks_name, buf, ks.ks_namelen)) {
160 			dprintf("can't read sem name at %p for pid %d",
161 			    ks.ks_name, Pid);
162 		} else {
163 			buf[ks.ks_namelen] = '\0';
164 			(void)printf(", name=%s\n", buf);
165 			return 0;
166 		}
167 	}
168 	(void)printf("\n");
169 	return 0;
170 }
171 
172 static int
173 p_mqueue(struct file *f)
174 {
175 	struct mqueue mq;
176 
177 	if (!KVM_READ(f->f_data, &mq, sizeof(mq))) {
178 		dprintf("can't read mqueue at %p for pid %d", f->f_data, Pid);
179 		return 0;
180 	}
181 	(void)printf("* mqueue \"%s\"\n", mq.mq_name);
182 	return 0;
183 }
184 
185 static int
186 p_rnd(struct file *f)
187 {
188 	struct cprng_strong {
189 		char cs_name[16];
190 		int  cs_flags;
191 		/*...*/
192 	} str;
193 	struct rnd_ctx {
194 		struct cprng_strong *rc_cprng;
195 		bool rc_hard;
196 	} ctx;
197 	char buf[1024];
198 
199 	if (!KVM_READ(f->f_data, &ctx, sizeof(ctx))) {
200 		dprintf("can't read rnd_ctx at %p for pid %d", f->f_data, Pid);
201 		return 0;
202 	}
203 	if (!KVM_READ(ctx.rc_cprng, &str, sizeof(str))) {
204 		dprintf("can't read cprng_strong at %p for pid %d", f->f_data,\
205 		    Pid);
206 		return 0;
207 	}
208 	snprintb(buf, sizeof(buf), CPRNG_FMT, str.cs_flags);
209 	(void)printf("* rnd \"%s\" flags %s\n", str.cs_name, buf);
210 	return 0;
211 }
212 
213 static int
214 p_kqueue(struct file *f)
215 {
216 	struct kqueue kq;
217 
218 	if (!KVM_READ(f->f_data, &kq, sizeof(kq))) {
219 		dprintf("can't read kqueue at %p for pid %d", f->f_data, Pid);
220 		return 0;
221 	}
222 	(void)printf("* kqueue pending %d\n", kq.kq_count);
223 	return 0;
224 }
225 
226 int
227 pmisc(struct file *f, const char *name)
228 {
229 	size_t i;
230 	if (nl[0].n_value == 0) {
231 		int n;
232 		if ((n = KVM_NLIST(nl)) == -1)
233 			errx(1, "Cannot list kernel symbols (%s)",
234 			    KVM_GETERR());
235 		else if (n != 0 && vflg) {
236 			char buf[1024];
237 			buf[0] = '\0';
238 			for (struct nlist *l = nl; l->n_name != NULL; l++) {
239 				if (l->n_value != 0)
240 					continue;
241 				strlcat(buf, ", ", sizeof(buf));
242 				strlcat(buf, l->n_name, sizeof(buf));
243 			}
244 			warnx("Could not find %d symbols: %s", n, buf + 2);
245 		}
246 	}
247 	for (i = 0; i < NL_MAX; i++)
248 		if ((uintptr_t)f->f_ops == nl[i].n_value)
249 			break;
250 	switch (i) {
251 	case NL_BPF:
252 		return p_bpf(f);
253 	case NL_MQUEUE:
254 		return p_mqueue(f);
255 	case NL_KQUEUE:
256 		return p_kqueue(f);
257 	case NL_RND:
258 		return p_rnd(f);
259 	case NL_SEM:
260 		return p_sem(f);
261 	case NL_TAP:
262 		printf("* tap %lu\n", (unsigned long)(intptr_t)f->f_data);
263 		return 0;
264 	case NL_CRYPTO:
265 		printf("* crypto %p\n", f->f_data);
266 		return 0;
267 	case NL_MAX:
268 		printf("* %s ops=%p %p\n", name, f->f_ops, f->f_data);
269 		return 0;
270 	default:
271 		printf("* %s %p\n", nl[i].n_name, f->f_data);
272 		return 0;
273 	}
274 }
275