xref: /openbsd-src/lib/libkvm/kvm_file2.c (revision d4741794dd2f512d997014f8bd85fbb24d935059)
1 /*	$OpenBSD: kvm_file2.c,v 1.51 2016/11/07 00:26:33 guenther Exp $	*/
2 
3 /*
4  * Copyright (c) 2009 Todd C. Miller <Todd.Miller@courtesan.com>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 /*-
20  * Copyright (c) 1989, 1992, 1993
21  *	The Regents of the University of California.  All rights reserved.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the above copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. Neither the name of the University nor the names of its contributors
32  *    may be used to endorse or promote products derived from this software
33  *    without specific prior written permission.
34  *
35  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
36  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
39  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45  * SUCH DAMAGE.
46  */
47 
48 /*
49  * Extended file list interface for kvm.  pstat, fstat and netstat are
50  * users of this code, so we've factored it out into a separate module.
51  * Thus, we keep this grunge out of the other kvm applications (i.e.,
52  * most other applications are interested only in open/close/read/nlist).
53  */
54 
55 #define __need_process
56 
57 #include <sys/param.h>
58 #include <sys/uio.h>
59 #include <sys/ucred.h>
60 #include <sys/proc.h>
61 #define _KERNEL
62 #include <sys/file.h>
63 #include <sys/mount.h>
64 #undef _KERNEL
65 #include <sys/vnode.h>
66 #include <sys/socket.h>
67 #include <sys/socketvar.h>
68 #include <sys/domain.h>
69 #include <sys/protosw.h>
70 #include <sys/event.h>
71 #include <sys/eventvar.h>
72 #include <sys/un.h>
73 #include <sys/unpcb.h>
74 #include <sys/filedesc.h>
75 #include <sys/mbuf.h>
76 #include <sys/pipe.h>
77 #include <sys/stat.h>
78 #include <sys/sysctl.h>
79 #include <sys/specdev.h>
80 
81 #define _KERNEL
82 #include <ufs/ufs/quota.h>
83 #include <ufs/ufs/inode.h>
84 #undef _KERNEL
85 
86 #include <nfs/nfsproto.h>
87 #include <nfs/rpcv2.h>
88 #include <nfs/nfs.h>
89 #include <nfs/nfsnode.h>
90 
91 #include <msdosfs/bpb.h>
92 #include <msdosfs/denode.h>
93 #include <msdosfs/msdosfsmount.h>
94 
95 #include <net/route.h>
96 #include <netinet/in.h>
97 #include <netinet/ip.h>
98 #include <netinet/in_pcb.h>
99 #include <netinet/tcp.h>
100 #include <netinet/tcp_timer.h>
101 #include <netinet/tcp_var.h>
102 
103 #ifdef INET6
104 #include <netinet/ip6.h>
105 #include <netinet6/ip6_var.h>
106 #endif
107 
108 #include <nlist.h>
109 #include <kvm.h>
110 #include <db.h>
111 #include <stddef.h>
112 #include <stdlib.h>
113 #include <string.h>
114 #include <unistd.h>
115 #include <limits.h>
116 #include <errno.h>
117 
118 #include "kvm_private.h"
119 #include "kvm_file.h"
120 
121 static struct kinfo_file *kvm_deadfile_byfile(kvm_t *, int, int,
122     size_t, int *);
123 static struct kinfo_file *kvm_deadfile_byid(kvm_t *, int, int,
124     size_t, int *);
125 static int fill_file(kvm_t *, struct kinfo_file *, struct file *, u_long,
126     struct vnode *, struct process *, int, pid_t);
127 static int filestat(kvm_t *, struct kinfo_file *, struct vnode *);
128 
129 LIST_HEAD(processlist, process);
130 
131 struct kinfo_file *
132 kvm_getfiles(kvm_t *kd, int op, int arg, size_t esize, int *cnt)
133 {
134 	int mib[6], rv;
135 	void *filebase;
136 	size_t size;
137 
138 	if (ISALIVE(kd)) {
139 		mib[0] = CTL_KERN;
140 		mib[1] = KERN_FILE;
141 		mib[2] = op;
142 		mib[3] = arg;
143 		mib[4] = esize;
144 
145 		do {
146 			mib[5] = 0;
147 
148 			/* find size and alloc buffer */
149 			rv = sysctl(mib, 6, NULL, &size, NULL, 0);
150 			if (rv == -1) {
151 				if (errno != ESRCH && kd->vmfd != -1)
152 					goto deadway;
153 				_kvm_syserr(kd, kd->program, "kvm_getfiles");
154 				return (NULL);
155 			}
156 
157 			size += size / 8; /* add ~10% */
158 
159 			filebase = _kvm_realloc(kd, kd->filebase, size);
160 			if (filebase == NULL)
161 				return (NULL);
162 
163 			kd->filebase = filebase;
164 
165 			/* get actual data */
166 			mib[5] = size / esize;
167 			rv = sysctl(mib, 6, kd->filebase, &size, NULL, 0);
168 			if (rv == -1 && errno != ENOMEM) {
169 				_kvm_syserr(kd, kd->program,
170 				    "kvm_getfiles");
171 				return (NULL);
172 			}
173 		} while (rv == -1);
174 
175 		*cnt = size / esize;
176 		return (kd->filebase);
177 	} else {
178 		if (esize > sizeof(struct kinfo_file)) {
179 			_kvm_syserr(kd, kd->program,
180 			    "kvm_getfiles: unknown fields requested: libkvm out of date?");
181 			return (NULL);
182 		}
183 	    deadway:
184 		switch (op) {
185 		case KERN_FILE_BYFILE:
186 			return (kvm_deadfile_byfile(kd, op, arg, esize, cnt));
187 			break;
188 		case KERN_FILE_BYPID:
189 		case KERN_FILE_BYUID:
190 			return (kvm_deadfile_byid(kd, op, arg, esize, cnt));
191 			break;
192 		default:
193 			return (NULL);
194 		}
195 	}
196 }
197 
198 static struct kinfo_file *
199 kvm_deadfile_byfile(kvm_t *kd, int op, int arg, size_t esize, int *cnt)
200 {
201 	struct nlist nl[3], *p;
202 	size_t buflen;
203 	int n = 0;
204 	char *where;
205 	struct kinfo_file kf;
206 	struct file *fp, file;
207 	struct filelist filehead;
208 	int nfiles;
209 
210 	nl[0].n_name = "_filehead";
211 	nl[1].n_name = "_nfiles";
212 	nl[2].n_name = 0;
213 
214 	if (kvm_nlist(kd, nl) != 0) {
215 		for (p = nl; p->n_type != 0; ++p)
216 			;
217 		_kvm_err(kd, kd->program,
218 			 "%s: no such symbol", p->n_name);
219 		return (NULL);
220 	}
221 	if (KREAD(kd, nl[0].n_value, &filehead)) {
222 		_kvm_err(kd, kd->program, "can't read filehead");
223 		return (NULL);
224 	}
225 	if (KREAD(kd, nl[1].n_value, &nfiles)) {
226 		_kvm_err(kd, kd->program, "can't read nfiles");
227 		return (NULL);
228 	}
229 	where = _kvm_reallocarray(kd, kd->filebase, nfiles, esize);
230 	if (where == NULL)
231 		return (NULL);
232 
233 	kd->filebase = (void *)where;
234 	buflen = nfiles * esize;
235 
236 	for (fp = LIST_FIRST(&filehead);
237 	    fp != NULL && esize <= buflen;
238 	    fp = LIST_NEXT(&file, f_list)) {
239 		if (KREAD(kd, (u_long)fp, &file)) {
240 			_kvm_err(kd, kd->program, "can't read kfp");
241 			return (NULL);
242 		}
243 		if (file.f_count == 0)
244 			continue;
245 		if (arg != 0 && file.f_type != arg)
246 			continue;
247 		if (fill_file(kd, &kf, &file, (u_long)fp, NULL, NULL, 0, 0)
248 		    == -1)
249 			return (NULL);
250 		memcpy(where, &kf, esize);
251 		where += esize;
252 		buflen -= esize;
253 		n++;
254 	}
255 	if (n != nfiles) {
256 		_kvm_err(kd, kd->program, "inconsistent nfiles");
257 		return (NULL);
258 	}
259 	*cnt = n;
260 	return (kd->filebase);
261 }
262 
263 static struct kinfo_file *
264 kvm_deadfile_byid(kvm_t *kd, int op, int arg, size_t esize, int *cnt)
265 {
266 	size_t buflen;
267 	struct nlist nl[4], *np;
268 	int n = 0, matched = 0;
269 	char *where;
270 	struct kinfo_file kf;
271 	struct file *fp, file;
272 	struct filelist filehead;
273 	struct filedesc0 filed0;
274 #define filed	filed0.fd_fd
275 	struct processlist allprocess;
276 	struct proc proc;
277 	struct process *pr, process;
278 	struct ucred ucred;
279 	char *filebuf = NULL;
280 	int i, nfiles;
281 
282 	nl[0].n_name = "_filehead";
283 	nl[1].n_name = "_nfiles";
284 	nl[2].n_name = "_allprocess";
285 	nl[3].n_name = 0;
286 
287 	if (kvm_nlist(kd, nl) != 0) {
288 		for (np = nl; np->n_type != 0; ++np)
289 			;
290 		_kvm_err(kd, kd->program,
291 			 "%s: no such symbol", np->n_name);
292 		return (NULL);
293 	}
294 	if (KREAD(kd, nl[0].n_value, &filehead)) {
295 		_kvm_err(kd, kd->program, "can't read filehead");
296 		return (NULL);
297 	}
298 	if (KREAD(kd, nl[1].n_value, &nfiles)) {
299 		_kvm_err(kd, kd->program, "can't read nfiles");
300 		return (NULL);
301 	}
302 	if (KREAD(kd, nl[2].n_value, &allprocess)) {
303 		_kvm_err(kd, kd->program, "can't read allprocess");
304 		return (NULL);
305 	}
306 	/* this may be more room than we need but counting is expensive */
307 	where = _kvm_reallocarray(kd, kd->filebase, nfiles + 10, esize);
308 	if (where == NULL)
309 		return (NULL);
310 
311 	kd->filebase = (void *)where;
312 	buflen = (nfiles + 10) * esize;
313 
314 	if (op != KERN_FILE_BYPID || arg <= 0)
315 		matched = 1;
316 
317 	for (pr = LIST_FIRST(&allprocess);
318 	    pr != NULL;
319 	    pr = LIST_NEXT(&process, ps_list)) {
320 		if (KREAD(kd, (u_long)pr, &process)) {
321 			_kvm_err(kd, kd->program, "can't read process at %lx",
322 			    (u_long)pr);
323 			goto cleanup;
324 		}
325 
326 		/* skip system, exiting, embryonic and undead processes */
327 		if (process.ps_flags & (PS_SYSTEM | PS_EMBRYO | PS_EXITING))
328 			continue;
329 
330 		if (process.ps_mainproc == NULL)
331 			continue;
332 		/* XXX only needed for p_comm now */
333 		if (KREAD(kd, (u_long)process.ps_mainproc, &proc)) {
334 			_kvm_err(kd, kd->program, "can't read proc at %lx",
335 			    (u_long)process.ps_mainproc);
336 			goto cleanup;
337 		}
338 
339 		if (op == KERN_FILE_BYPID) {
340 			/* check if this is the pid we are looking for */
341 			if (arg > 0 && process.ps_pid != (pid_t)arg)
342 				continue;
343 			matched = 1;
344 		}
345 
346 		if (KREAD(kd, (u_long)process.ps_ucred, &ucred)) {
347 			_kvm_err(kd, kd->program, "can't read ucred at %lx",
348 			    (u_long)process.ps_ucred);
349 			goto cleanup;
350 		}
351 		process.ps_mainproc = &proc;
352 		proc.p_p = &process;
353 		process.ps_ucred = &ucred;
354 
355 		if (op == KERN_FILE_BYUID && arg >= 0 &&
356 		    process.ps_ucred->cr_uid != (uid_t)arg) {
357 			/* not the uid we are looking for */
358 			continue;
359 		}
360 
361 		if (KREAD(kd, (u_long)process.ps_fd, &filed0)) {
362 			_kvm_err(kd, kd->program, "can't read filedesc at %lx",
363 			    (u_long)process.ps_fd);
364 			goto cleanup;
365 		}
366 		if ((char *)process.ps_fd + offsetof(struct filedesc0,fd_dfiles)
367 		    == (char *)filed.fd_ofiles) {
368 			filed.fd_ofiles = filed0.fd_dfiles;
369 			filed.fd_ofileflags = filed0.fd_dfileflags;
370 		} else {
371 			size_t fsize;
372 			char *tmp = reallocarray(filebuf,
373 			    filed.fd_nfiles, OFILESIZE);
374 
375 			fsize = filed.fd_nfiles * OFILESIZE;
376 			if (tmp == NULL) {
377 				_kvm_syserr(kd, kd->program, "realloc ofiles");
378 				goto cleanup;
379 			}
380 			filebuf = tmp;
381 			if (kvm_read(kd, (u_long)filed.fd_ofiles, filebuf,
382 			    fsize) != fsize) {
383 				_kvm_err(kd, kd->program,
384 				    "can't read fd_ofiles");
385 				goto cleanup;
386 			}
387 			filed.fd_ofiles = (void *)filebuf;
388 			filed.fd_ofileflags = filebuf +
389 			    (filed.fd_nfiles * sizeof(struct file *));
390 		}
391 		process.ps_fd = &filed;
392 
393 		if (process.ps_textvp) {
394 			if (buflen < esize)
395 				goto done;
396 			if (fill_file(kd, &kf, NULL, 0, process.ps_textvp,
397 			    &process, KERN_FILE_TEXT, process.ps_pid) == -1)
398 				goto cleanup;
399 			memcpy(where, &kf, esize);
400 			where += esize;
401 			buflen -= esize;
402 			n++;
403 		}
404 		if (filed.fd_cdir) {
405 			if (buflen < esize)
406 				goto done;
407 			if (fill_file(kd, &kf, NULL, 0, filed.fd_cdir,
408 			    &process, KERN_FILE_CDIR, process.ps_pid) == -1)
409 				goto cleanup;
410 			memcpy(where, &kf, esize);
411 			where += esize;
412 			buflen -= esize;
413 			n++;
414 		}
415 		if (filed.fd_rdir) {
416 			if (buflen < esize)
417 				goto done;
418 			if (fill_file(kd, &kf, NULL, 0, filed.fd_rdir,
419 			    &process, KERN_FILE_RDIR, process.ps_pid) == -1)
420 				goto cleanup;
421 			memcpy(where, &kf, esize);
422 			where += esize;
423 			buflen -= esize;
424 			n++;
425 		}
426 		if (process.ps_tracevp) {
427 			if (buflen < esize)
428 				goto done;
429 			if (fill_file(kd, &kf, NULL, 0, process.ps_tracevp,
430 			    &process, KERN_FILE_TRACE, process.ps_pid) == -1)
431 				goto cleanup;
432 			memcpy(where, &kf, esize);
433 			where += esize;
434 			buflen -= esize;
435 			n++;
436 		}
437 
438 		if (filed.fd_nfiles < 0 ||
439 		    filed.fd_lastfile >= filed.fd_nfiles ||
440 		    filed.fd_freefile > filed.fd_lastfile + 1) {
441 			_kvm_err(kd, kd->program,
442 			    "filedesc corrupted at %lx for pid %d",
443 			    (u_long)process.ps_fd, process.ps_pid);
444 			goto cleanup;
445 		}
446 
447 		for (i = 0; i < filed.fd_nfiles; i++) {
448 			if (buflen < esize)
449 				goto done;
450 			if ((fp = filed.fd_ofiles[i]) == NULL)
451 				continue;
452 			if (KREAD(kd, (u_long)fp, &file)) {
453 				_kvm_err(kd, kd->program, "can't read file");
454 				goto cleanup;
455 			}
456 			if (fill_file(kd, &kf, &file, (u_long)fp, NULL,
457 			    &process, i, process.ps_pid) == -1)
458 				goto cleanup;
459 			memcpy(where, &kf, esize);
460 			where += esize;
461 			buflen -= esize;
462 			n++;
463 		}
464 	}
465 	if (!matched) {
466 		errno = ESRCH;
467 		goto cleanup;
468 	}
469 done:
470 	*cnt = n;
471 	free(filebuf);
472 	return (kd->filebase);
473 cleanup:
474 	free(filebuf);
475 	return (NULL);
476 }
477 
478 static int
479 fill_file(kvm_t *kd, struct kinfo_file *kf, struct file *fp, u_long fpaddr,
480     struct vnode *vp, struct process *pr, int fd, pid_t pid)
481 {
482 	struct ucred f_cred;
483 
484 	memset(kf, 0, sizeof(*kf));
485 
486 	kf->fd_fd = fd;		/* might not really be an fd */
487 
488 	if (fp != NULL) {
489 		/* Fill in f_cred */
490 		if (KREAD(kd, (u_long)fp->f_cred, &f_cred)) {
491 			_kvm_err(kd, kd->program, "can't read f_cred");
492 			return (-1);
493 		}
494 
495 		kf->f_fileaddr = PTRTOINT64(fpaddr);
496 		kf->f_flag = fp->f_flag;
497 		kf->f_iflags = fp->f_iflags;
498 		kf->f_type = fp->f_type;
499 		kf->f_count = fp->f_count;
500 		kf->f_ucred = PTRTOINT64(fp->f_cred);
501 		kf->f_uid = f_cred.cr_uid;
502 		kf->f_gid = f_cred.cr_gid;
503 		kf->f_ops = PTRTOINT64(fp->f_ops);
504 		kf->f_offset = fp->f_offset;
505 		kf->f_data = PTRTOINT64(fp->f_data);
506 		kf->f_usecount = 0;
507 
508 		kf->f_rxfer = fp->f_rxfer;
509 		kf->f_rwfer = fp->f_wxfer;
510 		kf->f_seek = fp->f_seek;
511 		kf->f_rbytes = fp->f_rbytes;
512 		kf->f_wbytes = fp->f_wbytes;
513 	} else if (vp != NULL) {
514 		/* fake it */
515 		kf->f_type = DTYPE_VNODE;
516 		kf->f_flag = FREAD;
517 		if (fd == KERN_FILE_TRACE)
518 			kf->f_flag |= FWRITE;
519 		kf->f_data = PTRTOINT64(vp);
520 	}
521 
522 	/* information about the object associated with this file */
523 	switch (kf->f_type) {
524 	case DTYPE_VNODE: {
525 		struct vnode vbuf;
526 
527 		if (KREAD(kd, (u_long)(fp ? fp->f_data : vp), &vbuf)) {
528 			_kvm_err(kd, kd->program, "can't read vnode");
529 			return (-1);
530 		}
531 		vp = &vbuf;
532 
533 		kf->v_un = PTRTOINT64(vp->v_un.vu_socket);
534 		kf->v_type = vp->v_type;
535 		kf->v_tag = vp->v_tag;
536 		kf->v_flag = vp->v_flag;
537 		kf->v_data = PTRTOINT64(vp->v_data);
538 		kf->v_mount = PTRTOINT64(vp->v_mount);
539 
540 		if (vp->v_mount != NULL) {
541 			struct mount mount;
542 
543 			if (KREAD(kd, (u_long)vp->v_mount, &mount)) {
544 				_kvm_err(kd, kd->program, "can't read v_mount");
545 				return (-1);
546 			}
547 
548 			strlcpy(kf->f_mntonname, mount.mnt_stat.f_mntonname,
549 			    sizeof(kf->f_mntonname));
550 		}
551 
552 		/* Fill in va_fsid, va_fileid, va_mode, va_size, va_rdev */
553 		filestat(kd, kf, vp);
554 		break;
555 	    }
556 
557 	case DTYPE_SOCKET: {
558 		struct socket sock;
559 		struct sosplice ssp;
560 		struct protosw protosw;
561 		struct domain domain;
562 
563 		if (KREAD(kd, (u_long)fp->f_data, &sock)) {
564 			_kvm_err(kd, kd->program, "can't read socket");
565 			return (-1);
566 		}
567 
568 		kf->so_type = sock.so_type;
569 		kf->so_state = sock.so_state;
570 		kf->so_pcb = PTRTOINT64(sock.so_pcb);
571 		if (KREAD(kd, (u_long)sock.so_proto, &protosw)) {
572 			_kvm_err(kd, kd->program, "can't read protosw");
573 			return (-1);
574 		}
575 		kf->so_protocol = protosw.pr_protocol;
576 		if (KREAD(kd, (u_long)protosw.pr_domain, &domain)) {
577 			_kvm_err(kd, kd->program, "can't read domain");
578 			return (-1);
579 		}
580 		kf->so_family = domain.dom_family;
581 		kf->so_rcv_cc = sock.so_rcv.sb_cc;
582 		kf->so_snd_cc = sock.so_snd.sb_cc;
583 		if (sock.so_sp) {
584 			if (KREAD(kd, (u_long)sock.so_sp, &ssp)) {
585 				_kvm_err(kd, kd->program, "can't read splice");
586 				return (-1);
587 			}
588 			if (ssp.ssp_socket) {
589 				kf->so_splice = PTRTOINT64(ssp.ssp_socket);
590 				kf->so_splicelen = ssp.ssp_len;
591 			} else if (ssp.ssp_soback) {
592 				kf->so_splicelen = -1;
593 			}
594 		}
595 		if (!sock.so_pcb)
596 			break;
597 		switch (kf->so_family) {
598 		case AF_INET: {
599 			struct inpcb inpcb;
600 
601 			if (KREAD(kd, (u_long)sock.so_pcb, &inpcb)) {
602 				_kvm_err(kd, kd->program, "can't read inpcb");
603 				return (-1);
604 			}
605 			kf->inp_ppcb = PTRTOINT64(inpcb.inp_ppcb);
606 			kf->inp_lport = inpcb.inp_lport;
607 			kf->inp_laddru[0] = inpcb.inp_laddr.s_addr;
608 			kf->inp_fport = inpcb.inp_fport;
609 			kf->inp_faddru[0] = inpcb.inp_faddr.s_addr;
610 			kf->inp_rtableid = inpcb.inp_rtableid;
611 			if (sock.so_type == SOCK_RAW)
612 				kf->inp_proto = inpcb.inp_ip.ip_p;
613 			if (protosw.pr_protocol == IPPROTO_TCP) {
614 				struct tcpcb tcpcb;
615 				if (KREAD(kd, (u_long)inpcb.inp_ppcb, &tcpcb)) {
616 					_kvm_err(kd, kd->program,
617 					    "can't read tcpcb");
618 					return (-1);
619 				}
620 				kf->t_rcv_wnd = tcpcb.rcv_wnd;
621 				kf->t_snd_wnd = tcpcb.snd_wnd;
622 				kf->t_snd_cwnd = tcpcb.snd_cwnd;
623 				kf->t_state = tcpcb.t_state;
624 			}
625 			break;
626 		    }
627 		case AF_INET6: {
628 			struct inpcb inpcb;
629 #define s6_addr32 __u6_addr.__u6_addr32
630 
631 			if (KREAD(kd, (u_long)sock.so_pcb, &inpcb)) {
632 				_kvm_err(kd, kd->program, "can't read inpcb");
633 				return (-1);
634 			}
635 			kf->inp_ppcb = PTRTOINT64(inpcb.inp_ppcb);
636 			kf->inp_lport = inpcb.inp_lport;
637 			kf->inp_laddru[0] = inpcb.inp_laddr6.s6_addr32[0];
638 			kf->inp_laddru[1] = inpcb.inp_laddr6.s6_addr32[1];
639 			kf->inp_laddru[2] = inpcb.inp_laddr6.s6_addr32[2];
640 			kf->inp_laddru[3] = inpcb.inp_laddr6.s6_addr32[3];
641 			kf->inp_fport = inpcb.inp_fport;
642 			kf->inp_faddru[0] = inpcb.inp_laddr6.s6_addr32[0];
643 			kf->inp_faddru[1] = inpcb.inp_faddr6.s6_addr32[1];
644 			kf->inp_faddru[2] = inpcb.inp_faddr6.s6_addr32[2];
645 			kf->inp_faddru[3] = inpcb.inp_faddr6.s6_addr32[3];
646 			kf->inp_rtableid = inpcb.inp_rtableid;
647 			if (sock.so_type == SOCK_RAW)
648 				kf->inp_proto = inpcb.inp_ipv6.ip6_nxt;
649 			if (protosw.pr_protocol == IPPROTO_TCP) {
650 				struct tcpcb tcpcb;
651 				if (KREAD(kd, (u_long)inpcb.inp_ppcb, &tcpcb)) {
652 					_kvm_err(kd, kd->program,
653 					    "can't read tcpcb");
654 					return (-1);
655 				}
656 				kf->t_rcv_wnd = tcpcb.rcv_wnd;
657 				kf->t_snd_wnd = tcpcb.snd_wnd;
658 				kf->t_snd_cwnd = tcpcb.snd_cwnd;
659 				kf->t_state = tcpcb.t_state;
660 			}
661 			break;
662 		    }
663 		case AF_UNIX: {
664 			struct unpcb unpcb;
665 
666 			if (KREAD(kd, (u_long)sock.so_pcb, &unpcb)) {
667 				_kvm_err(kd, kd->program, "can't read unpcb");
668 				return (-1);
669 			}
670 			kf->f_msgcount	= unpcb.unp_msgcount;
671 			kf->unp_conn	= PTRTOINT64(unpcb.unp_conn);
672 			kf->unp_refs	= PTRTOINT64(
673 			    SLIST_FIRST(&unpcb.unp_refs));
674 			kf->unp_nextref	= PTRTOINT64(
675 			    SLIST_NEXT(&unpcb, unp_nextref));
676 			kf->v_un	= PTRTOINT64(unpcb.unp_vnode);
677 			if (unpcb.unp_addr != NULL) {
678 				struct mbuf mb;
679 				struct sockaddr_un un;
680 
681 				if (KREAD(kd, (u_long)unpcb.unp_addr, &mb)) {
682 					_kvm_err(kd, kd->program,
683 					    "can't read sockaddr_un mbuf");
684 					return (-1);
685 				}
686 				if (KREAD(kd, (u_long)mb.m_data, &un)) {
687 					_kvm_err(kd, kd->program,
688 					    "can't read sockaddr_un");
689 					return (-1);
690 				}
691 
692 				kf->unp_addr = PTRTOINT64(unpcb.unp_addr);
693 				memcpy(kf->unp_path, un.sun_path, un.sun_len
694 				    - offsetof(struct sockaddr_un,sun_path));
695 			}
696 
697 			break;
698 		    }
699 		}
700 		break;
701 	    }
702 
703 	case DTYPE_PIPE: {
704 		struct pipe pipe;
705 
706 		if (KREAD(kd, (u_long)fp->f_data, &pipe)) {
707 			_kvm_err(kd, kd->program, "can't read pipe");
708 			return (-1);
709 		}
710 		kf->pipe_peer = PTRTOINT64(pipe.pipe_peer);
711 		kf->pipe_state = pipe.pipe_state;
712 		break;
713 	    }
714 
715 	case DTYPE_KQUEUE: {
716 		struct kqueue kqi;
717 
718 		if (KREAD(kd, (u_long)fp->f_data, &kqi)) {
719 			_kvm_err(kd, kd->program, "can't read kqi");
720 			return (-1);
721 		}
722 		kf->kq_count = kqi.kq_count;
723 		kf->kq_state = kqi.kq_state;
724 		break;
725 	    }
726 	}
727 
728 	/* per-process information for KERN_FILE_BY[PU]ID */
729 	if (pr != NULL) {
730 		kf->p_pid = pid;
731 		kf->p_uid = pr->ps_ucred->cr_uid;
732 		kf->p_gid = pr->ps_ucred->cr_gid;
733 		kf->p_tid = -1;
734 		strlcpy(kf->p_comm, pr->ps_mainproc->p_comm,
735 		    sizeof(kf->p_comm));
736 		if (pr->ps_fd != NULL)
737 			kf->fd_ofileflags = pr->ps_fd->fd_ofileflags[fd];
738 	}
739 
740 	return (0);
741 }
742 
743 mode_t
744 _kvm_getftype(enum vtype v_type)
745 {
746 	mode_t ftype = 0;
747 
748 	switch (v_type) {
749 	case VREG:
750 		ftype = S_IFREG;
751 		break;
752 	case VDIR:
753 		ftype = S_IFDIR;
754 		break;
755 	case VBLK:
756 		ftype = S_IFBLK;
757 		break;
758 	case VCHR:
759 		ftype = S_IFCHR;
760 		break;
761 	case VLNK:
762 		ftype = S_IFLNK;
763 		break;
764 	case VSOCK:
765 		ftype = S_IFSOCK;
766 		break;
767 	case VFIFO:
768 		ftype = S_IFIFO;
769 		break;
770 	case VNON:
771 	case VBAD:
772 		break;
773 	}
774 
775 	return (ftype);
776 }
777 
778 static int
779 ufs_filestat(kvm_t *kd, struct kinfo_file *kf, struct vnode *vp)
780 {
781 	struct inode inode;
782 	struct ufs1_dinode di1;
783 
784 	if (KREAD(kd, (u_long)VTOI(vp), &inode)) {
785 		_kvm_err(kd, kd->program, "can't read inode at %p", VTOI(vp));
786 		return (-1);
787 	}
788 
789 	if (KREAD(kd, (u_long)inode.i_din1, &di1)) {
790 		_kvm_err(kd, kd->program, "can't read dinode at %p",
791 		    inode.i_din1);
792 		return (-1);
793 	}
794 
795 	inode.i_din1 = &di1;
796 
797 	kf->va_fsid = inode.i_dev & 0xffff;
798 	kf->va_fileid = (long)inode.i_number;
799 	kf->va_mode = inode.i_ffs1_mode;
800 	kf->va_size = inode.i_ffs1_size;
801 	kf->va_rdev = inode.i_ffs1_rdev;
802 	kf->va_nlink = inode.i_ffs1_nlink;
803 
804 	return (0);
805 }
806 
807 static int
808 ext2fs_filestat(kvm_t *kd, struct kinfo_file *kf, struct vnode *vp)
809 {
810 	struct inode inode;
811 	struct ext2fs_dinode e2di;
812 
813 	if (KREAD(kd, (u_long)VTOI(vp), &inode)) {
814 		_kvm_err(kd, kd->program, "can't read inode at %p", VTOI(vp));
815 		return (-1);
816 	}
817 
818 	if (KREAD(kd, (u_long)inode.i_e2din, &e2di)) {
819 		_kvm_err(kd, kd->program, "can't read dinode at %p",
820 		    inode.i_e2din);
821 		return (-1);
822 	}
823 
824 	inode.i_e2din = &e2di;
825 
826 	kf->va_fsid = inode.i_dev & 0xffff;
827 	kf->va_fileid = (long)inode.i_number;
828 	kf->va_mode = inode.i_e2fs_mode;
829 	kf->va_size = inode.i_e2fs_size;
830 	kf->va_rdev = 0;	/* XXX */
831 	kf->va_nlink = inode.i_e2fs_nlink;
832 
833 	return (0);
834 }
835 
836 static int
837 msdos_filestat(kvm_t *kd, struct kinfo_file *kf, struct vnode *vp)
838 {
839 	struct denode de;
840 	struct msdosfsmount mp;
841 
842 	if (KREAD(kd, (u_long)VTODE(vp), &de)) {
843 		_kvm_err(kd, kd->program, "can't read denode at %p", VTODE(vp));
844 		return (-1);
845 	}
846 	if (KREAD(kd, (u_long)de.de_pmp, &mp)) {
847 		_kvm_err(kd, kd->program, "can't read mount struct at %p",
848 		    de.de_pmp);
849 		return (-1);
850 	}
851 
852 	kf->va_fsid = de.de_dev & 0xffff;
853 	kf->va_fileid = 0; /* XXX see msdosfs_vptofh() for more info */
854 	kf->va_mode = (mp.pm_mask & 0777) | _kvm_getftype(vp->v_type);
855 	kf->va_size = de.de_FileSize;
856 	kf->va_rdev = 0;  /* msdosfs doesn't support device files */
857 	kf->va_nlink = 1;
858 
859 	return (0);
860 }
861 
862 static int
863 nfs_filestat(kvm_t *kd, struct kinfo_file *kf, struct vnode *vp)
864 {
865 	struct nfsnode nfsnode;
866 
867 	if (KREAD(kd, (u_long)VTONFS(vp), &nfsnode)) {
868 		_kvm_err(kd, kd->program, "can't read nfsnode at %p",
869 		    VTONFS(vp));
870 		return (-1);
871 	}
872 	kf->va_fsid = nfsnode.n_vattr.va_fsid;
873 	kf->va_fileid = nfsnode.n_vattr.va_fileid;
874 	kf->va_size = nfsnode.n_size;
875 	kf->va_rdev = nfsnode.n_vattr.va_rdev;
876 	kf->va_mode = (mode_t)nfsnode.n_vattr.va_mode | _kvm_getftype(vp->v_type);
877 	kf->va_nlink = nfsnode.n_vattr.va_nlink;
878 
879 	return (0);
880 }
881 
882 static int
883 spec_filestat(kvm_t *kd, struct kinfo_file *kf, struct vnode *vp)
884 {
885 	struct specinfo		specinfo;
886 	struct vnode		parent;
887 
888 	if (KREAD(kd, (u_long)vp->v_specinfo, &specinfo)) {
889 		_kvm_err(kd, kd->program, "can't read specinfo at %p",
890 		     vp->v_specinfo);
891 		return (-1);
892 	}
893 
894 	vp->v_specinfo = &specinfo;
895 
896 	if (KREAD(kd, (u_long)vp->v_specparent, &parent)) {
897 		_kvm_err(kd, kd->program, "can't read parent vnode at %p",
898 		     vp->v_specparent);
899 		return (-1);
900 	}
901 
902 	if (ufs_filestat(kd, kf, vp))
903 		return (-1);
904 
905 	return (0);
906 }
907 
908 static int
909 filestat(kvm_t *kd, struct kinfo_file *kf, struct vnode *vp)
910 {
911 	int ret = 0;
912 
913 	if (vp->v_type != VNON && vp->v_type != VBAD) {
914 		switch (vp->v_tag) {
915 		case VT_UFS:
916 		case VT_MFS:
917 			ret = ufs_filestat(kd, kf, vp);
918 			break;
919 		case VT_NFS:
920 			ret = nfs_filestat(kd, kf, vp);
921 			break;
922 		case VT_EXT2FS:
923 			ret = ext2fs_filestat(kd, kf, vp);
924 			break;
925 		case VT_ISOFS:
926 			ret = _kvm_stat_cd9660(kd, kf, vp);
927 			break;
928 		case VT_MSDOSFS:
929 			ret = msdos_filestat(kd, kf, vp);
930 			break;
931 		case VT_UDF:
932 			ret = _kvm_stat_udf(kd, kf, vp);
933 			break;
934 		case VT_NTFS:
935 			ret = _kvm_stat_ntfs(kd, kf, vp);
936 			break;
937 		case VT_NON:
938 			if (vp->v_flag & VCLONE)
939 				ret = spec_filestat(kd, kf, vp);
940 			break;
941 		default:
942 			ret = -1;
943 			break;
944 		}
945 	}
946 	return (ret);
947 }
948