xref: /openbsd-src/lib/libkvm/kvm_file2.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /*	$OpenBSD: kvm_file2.c,v 1.36 2014/07/04 05:58:31 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 #include <dev/systrace.h>
65 #undef _KERNEL
66 #include <sys/vnode.h>
67 #include <sys/socket.h>
68 #include <sys/socketvar.h>
69 #include <sys/domain.h>
70 #include <sys/protosw.h>
71 #include <sys/event.h>
72 #include <sys/eventvar.h>
73 #include <sys/un.h>
74 #include <sys/unpcb.h>
75 #include <sys/filedesc.h>
76 #include <sys/mbuf.h>
77 #include <sys/pipe.h>
78 #include <sys/stat.h>
79 #include <sys/sysctl.h>
80 #include <sys/specdev.h>
81 
82 #define _KERNEL
83 #include <ufs/ufs/quota.h>
84 #include <ufs/ufs/inode.h>
85 #undef _KERNEL
86 
87 #include <nfs/nfsproto.h>
88 #include <nfs/rpcv2.h>
89 #include <nfs/nfs.h>
90 #include <nfs/nfsnode.h>
91 
92 #include <msdosfs/bpb.h>
93 #include <msdosfs/denode.h>
94 #include <msdosfs/msdosfsmount.h>
95 
96 #include <net/route.h>
97 #include <netinet/in.h>
98 #include <netinet/in_systm.h>
99 #include <netinet/ip.h>
100 #include <netinet/in_pcb.h>
101 
102 #ifdef INET6
103 #include <netinet/ip6.h>
104 #include <netinet6/ip6_var.h>
105 #endif
106 
107 #include <nlist.h>
108 #include <kvm.h>
109 #include <db.h>
110 #include <stddef.h>
111 #include <stdlib.h>
112 #include <stddef.h>
113 #include <string.h>
114 #include <unistd.h>
115 
116 #include "kvm_private.h"
117 #include "kvm_file.h"
118 
119 static struct kinfo_file *kvm_deadfile_byfile(kvm_t *, int, int,
120     size_t, int *);
121 static struct kinfo_file *kvm_deadfile_byid(kvm_t *, int, int,
122     size_t, int *);
123 static int fill_file(kvm_t *, struct kinfo_file *, struct file *, u_long,
124     struct vnode *, struct process *, int, pid_t);
125 static int filestat(kvm_t *, struct kinfo_file *, struct vnode *);
126 
127 LIST_HEAD(processlist, process);
128 
129 struct kinfo_file *
130 kvm_getfiles(kvm_t *kd, int op, int arg, size_t esize, int *cnt)
131 {
132 	int mib[6], rv;
133 	size_t size;
134 
135 	if (kd->filebase != NULL) {
136 		free(kd->filebase);
137 		/*
138 		 * Clear this pointer in case this call fails.  Otherwise,
139 		 * kvm_close() will free it again.
140 		 */
141 		kd->filebase = 0;
142 	}
143 
144 	if (ISALIVE(kd)) {
145 		mib[0] = CTL_KERN;
146 		mib[1] = KERN_FILE;
147 		mib[2] = op;
148 		mib[3] = arg;
149 		mib[4] = esize;
150 		mib[5] = 0;
151 
152 		/* find size and alloc buffer */
153 		rv = sysctl(mib, 6, NULL, &size, NULL, 0);
154 		if (rv == -1) {
155 			if (kd->vmfd != -1)
156 				goto deadway;
157 			_kvm_syserr(kd, kd->program, "kvm_getfiles");
158 			return (NULL);
159 		}
160 		kd->filebase = _kvm_malloc(kd, size);
161 		if (kd->filebase == NULL)
162 			return (NULL);
163 
164 		/* get actual data */
165 		mib[5] = size / esize;
166 		rv = sysctl(mib, 6, kd->filebase, &size, NULL, 0);
167 		if (rv == -1) {
168 			_kvm_syserr(kd, kd->program, "kvm_getfiles");
169 			return (NULL);
170 		}
171 		*cnt = size / esize;
172 		return (kd->filebase);
173 	} else {
174 		if (esize > sizeof(struct kinfo_file)) {
175 			_kvm_syserr(kd, kd->program,
176 			    "kvm_getfiles: unknown fields requested: libkvm out of date?");
177 			return (NULL);
178 		}
179 	    deadway:
180 		switch (op) {
181 		case KERN_FILE_BYFILE:
182 			if (arg != 0) {
183 				_kvm_err(kd, kd->program,
184 				    "%d: invalid argument", arg);
185 				return (NULL);
186 			}
187 			return (kvm_deadfile_byfile(kd, op, arg, esize, cnt));
188 			break;
189 		case KERN_FILE_BYPID:
190 		case KERN_FILE_BYUID:
191 			return (kvm_deadfile_byid(kd, op, arg, esize, cnt));
192 			break;
193 		default:
194 			return (NULL);
195 		}
196 	}
197 }
198 
199 static struct kinfo_file *
200 kvm_deadfile_byfile(kvm_t *kd, int op, int arg, size_t esize, int *cnt)
201 {
202 	struct nlist nl[3], *p;
203 	size_t buflen;
204 	int n = 0;
205 	char *where;
206 	struct kinfo_file kf;
207 	struct file *fp, file;
208 	struct filelist filehead;
209 	int nfiles;
210 
211 	nl[0].n_name = "_filehead";
212 	nl[1].n_name = "_nfiles";
213 	nl[2].n_name = 0;
214 
215 	if (kvm_nlist(kd, nl) != 0) {
216 		for (p = nl; p->n_type != 0; ++p)
217 			;
218 		_kvm_err(kd, kd->program,
219 			 "%s: no such symbol", p->n_name);
220 		return (NULL);
221 	}
222 	if (KREAD(kd, nl[0].n_value, &filehead)) {
223 		_kvm_err(kd, kd->program, "can't read filehead");
224 		return (NULL);
225 	}
226 	if (KREAD(kd, nl[1].n_value, &nfiles)) {
227 		_kvm_err(kd, kd->program, "can't read nfiles");
228 		return (NULL);
229 	}
230 	buflen = nfiles * esize;
231 	where = _kvm_malloc(kd, buflen);
232 	kd->filebase = (void *)where;
233 	if (kd->filebase == NULL)
234 		return (NULL);
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 (fill_file(kd, &kf, &file, (u_long)fp, NULL, NULL, 0, 0)
244 		    == -1)
245 			return (NULL);
246 		memcpy(where, &kf, esize);
247 		where += esize;
248 		buflen -= esize;
249 		n++;
250 	}
251 	if (n != nfiles) {
252 		_kvm_err(kd, kd->program, "inconsistent nfiles");
253 		return (NULL);
254 	}
255 	*cnt = n;
256 	return (kd->filebase);
257 }
258 
259 static struct kinfo_file *
260 kvm_deadfile_byid(kvm_t *kd, int op, int arg, size_t esize, int *cnt)
261 {
262 	size_t buflen;
263 	struct nlist nl[4], *np;
264 	int n = 0;
265 	char *where;
266 	struct kinfo_file kf;
267 	struct file *fp, file;
268 	struct filelist filehead;
269 	struct filedesc0 filed0;
270 #define filed	filed0.fd_fd
271 	struct processlist allprocess;
272 	struct proc proc;
273 	struct process *pr, process;
274 	struct ucred ucred;
275 	char *filebuf = NULL;
276 	int i, nfiles;
277 
278 	nl[0].n_name = "_filehead";
279 	nl[1].n_name = "_nfiles";
280 	nl[2].n_name = "_allprocess";
281 	nl[3].n_name = 0;
282 
283 	if (kvm_nlist(kd, nl) != 0) {
284 		for (np = nl; np->n_type != 0; ++np)
285 			;
286 		_kvm_err(kd, kd->program,
287 			 "%s: no such symbol", np->n_name);
288 		return (NULL);
289 	}
290 	if (KREAD(kd, nl[0].n_value, &filehead)) {
291 		_kvm_err(kd, kd->program, "can't read filehead");
292 		return (NULL);
293 	}
294 	if (KREAD(kd, nl[1].n_value, &nfiles)) {
295 		_kvm_err(kd, kd->program, "can't read nfiles");
296 		return (NULL);
297 	}
298 	if (KREAD(kd, nl[2].n_value, &allprocess)) {
299 		_kvm_err(kd, kd->program, "can't read allprocess");
300 		return (NULL);
301 	}
302 	/* this may be more room than we need but counting is expensive */
303 	buflen = (nfiles + 10) * esize;
304 	where = _kvm_malloc(kd, buflen);
305 	kd->filebase = (void *)where;
306 	if (kd->filebase == NULL)
307 		return (NULL);
308 
309 	for (pr = LIST_FIRST(&allprocess);
310 	    pr != NULL;
311 	    pr = LIST_NEXT(&process, ps_list)) {
312 		if (KREAD(kd, (u_long)pr, &process)) {
313 			_kvm_err(kd, kd->program, "can't read process at %lx",
314 			    (u_long)pr);
315 			goto cleanup;
316 		}
317 
318 		/* skip system, exiting, embryonic and undead processes */
319 		if (process.ps_flags & (PS_SYSTEM | PS_EMBRYO | PS_EXITING))
320 			continue;
321 
322 		if (process.ps_mainproc == NULL)
323 			continue;
324 		if (KREAD(kd, (u_long)process.ps_mainproc, &proc)) {
325 			_kvm_err(kd, kd->program, "can't read proc at %lx",
326 			    (u_long)process.ps_mainproc);
327 			goto cleanup;
328 		}
329 
330 		if (op == KERN_FILE_BYPID && arg > 0 &&
331 		    proc.p_pid != (pid_t)arg) {
332 				/* not the pid we are looking for */
333 				continue;
334 		}
335 
336 		if (KREAD(kd, (u_long)process.ps_ucred, &ucred)) {
337 			_kvm_err(kd, kd->program, "can't read ucred at %lx",
338 			    (u_long)process.ps_ucred);
339 			goto cleanup;
340 		}
341 		process.ps_mainproc = &proc;
342 		proc.p_p = &process;
343 		process.ps_ucred = &ucred;
344 
345 		if (op == KERN_FILE_BYUID && arg >= 0 &&
346 		    process.ps_ucred->cr_uid != (uid_t)arg) {
347 			/* not the uid we are looking for */
348 			continue;
349 		}
350 
351 		if (KREAD(kd, (u_long)process.ps_fd, &filed0)) {
352 			_kvm_err(kd, kd->program, "can't read filedesc at %lx",
353 			    (u_long)process.ps_fd);
354 			goto cleanup;
355 		}
356 		if ((char *)process.ps_fd + offsetof(struct filedesc0,fd_dfiles)
357 		    == (char *)filed.fd_ofiles) {
358 			filed.fd_ofiles = filed0.fd_dfiles;
359 			filed.fd_ofileflags = filed0.fd_dfileflags;
360 		} else {
361 			size_t fsize = filed.fd_nfiles * OFILESIZE;
362 			char *tmp = realloc(filebuf, fsize);
363 
364 			if (tmp == NULL) {
365 				_kvm_syserr(kd, kd->program, "realloc ofiles");
366 				goto cleanup;
367 			}
368 			filebuf = tmp;
369 			if (kvm_read(kd, (u_long)filed.fd_ofiles, filebuf,
370 			    fsize) != fsize) {
371 				_kvm_err(kd, kd->program,
372 				    "can't read fd_ofiles");
373 				goto cleanup;
374 			}
375 			filed.fd_ofiles = (void *)filebuf;
376 			filed.fd_ofileflags = filebuf +
377 			    (filed.fd_nfiles * sizeof(struct file *));
378 		}
379 		process.ps_fd = &filed;
380 
381 		if (process.ps_textvp) {
382 			if (buflen < esize)
383 				goto done;
384 			if (fill_file(kd, &kf, NULL, 0, process.ps_textvp,
385 			    &process, KERN_FILE_TEXT, proc.p_pid) == -1)
386 				goto cleanup;
387 			memcpy(where, &kf, esize);
388 			where += esize;
389 			buflen -= esize;
390 			n++;
391 		}
392 		if (filed.fd_cdir) {
393 			if (buflen < esize)
394 				goto done;
395 			if (fill_file(kd, &kf, NULL, 0, filed.fd_cdir,
396 			    &process, KERN_FILE_CDIR, proc.p_pid) == -1)
397 				goto cleanup;
398 			memcpy(where, &kf, esize);
399 			where += esize;
400 			buflen -= esize;
401 			n++;
402 		}
403 		if (filed.fd_rdir) {
404 			if (buflen < esize)
405 				goto done;
406 			if (fill_file(kd, &kf, NULL, 0, filed.fd_rdir,
407 			    &process, KERN_FILE_RDIR, proc.p_pid) == -1)
408 				goto cleanup;
409 			memcpy(where, &kf, esize);
410 			where += esize;
411 			buflen -= esize;
412 			n++;
413 		}
414 		if (process.ps_tracevp) {
415 			if (buflen < esize)
416 				goto done;
417 			if (fill_file(kd, &kf, NULL, 0, process.ps_tracevp,
418 			    &process, KERN_FILE_TRACE, proc.p_pid) == -1)
419 				goto cleanup;
420 			memcpy(where, &kf, esize);
421 			where += esize;
422 			buflen -= esize;
423 			n++;
424 		}
425 
426 		if (filed.fd_nfiles < 0 ||
427 		    filed.fd_lastfile >= filed.fd_nfiles ||
428 		    filed.fd_freefile > filed.fd_lastfile + 1) {
429 			_kvm_err(kd, kd->program,
430 			    "filedesc corrupted at %lx for pid %d",
431 			    (u_long)process.ps_fd, proc.p_pid);
432 			goto cleanup;
433 		}
434 
435 		for (i = 0; i < filed.fd_nfiles; i++) {
436 			if (buflen < esize)
437 				goto done;
438 			if ((fp = filed.fd_ofiles[i]) == NULL)
439 				continue;
440 			if (KREAD(kd, (u_long)fp, &file)) {
441 				_kvm_err(kd, kd->program, "can't read file");
442 				goto cleanup;
443 			}
444 			if (fill_file(kd, &kf, &file, (u_long)fp, NULL,
445 			    &process, i, proc.p_pid) == -1)
446 				goto cleanup;
447 			memcpy(where, &kf, esize);
448 			where += esize;
449 			buflen -= esize;
450 			n++;
451 		}
452 	}
453 done:
454 	*cnt = n;
455 	free(filebuf);
456 	return (kd->filebase);
457 cleanup:
458 	free(filebuf);
459 	return (NULL);
460 }
461 
462 static int
463 fill_file(kvm_t *kd, struct kinfo_file *kf, struct file *fp, u_long fpaddr,
464     struct vnode *vp, struct process *pr, int fd, pid_t pid)
465 {
466 	struct ucred f_cred;
467 
468 	memset(kf, 0, sizeof(*kf));
469 
470 	kf->fd_fd = fd;		/* might not really be an fd */
471 
472 	if (fp != NULL) {
473 		/* Fill in f_cred */
474 		if (KREAD(kd, (u_long)fp->f_cred, &f_cred)) {
475 			_kvm_err(kd, kd->program, "can't read f_cred");
476 			return (-1);
477 		}
478 
479 		kf->f_fileaddr = PTRTOINT64(fpaddr);
480 		kf->f_flag = fp->f_flag;
481 		kf->f_iflags = fp->f_iflags;
482 		kf->f_type = fp->f_type;
483 		kf->f_count = fp->f_count;
484 		kf->f_msgcount = fp->f_msgcount;
485 		kf->f_ucred = PTRTOINT64(fp->f_cred);
486 		kf->f_uid = f_cred.cr_uid;
487 		kf->f_gid = f_cred.cr_gid;
488 		kf->f_ops = PTRTOINT64(fp->f_ops);
489 		kf->f_offset = fp->f_offset;
490 		kf->f_data = PTRTOINT64(fp->f_data);
491 		kf->f_usecount = 0;
492 
493 		kf->f_rxfer = fp->f_rxfer;
494 		kf->f_rwfer = fp->f_wxfer;
495 		kf->f_seek = fp->f_seek;
496 		kf->f_rbytes = fp->f_rbytes;
497 		kf->f_wbytes = fp->f_wbytes;
498 	} else if (vp != NULL) {
499 		/* fake it */
500 		kf->f_type = DTYPE_VNODE;
501 		kf->f_flag = FREAD;
502 		if (fd == KERN_FILE_TRACE)
503 			kf->f_flag |= FWRITE;
504 		kf->f_data = PTRTOINT64(vp);
505 	}
506 
507 	/* information about the object associated with this file */
508 	switch (kf->f_type) {
509 	case DTYPE_VNODE: {
510 		struct vnode vbuf;
511 
512 		if (KREAD(kd, (u_long)(fp ? fp->f_data : vp), &vbuf)) {
513 			_kvm_err(kd, kd->program, "can't read vnode");
514 			return (-1);
515 		}
516 		vp = &vbuf;
517 
518 		kf->v_un = PTRTOINT64(vp->v_un.vu_socket);
519 		kf->v_type = vp->v_type;
520 		kf->v_tag = vp->v_tag;
521 		kf->v_flag = vp->v_flag;
522 		kf->v_data = PTRTOINT64(vp->v_data);
523 		kf->v_mount = PTRTOINT64(vp->v_mount);
524 
525 		if (vp->v_mount != NULL) {
526 			struct mount mount;
527 
528 			if (KREAD(kd, (u_long)vp->v_mount, &mount)) {
529 				_kvm_err(kd, kd->program, "can't read v_mount");
530 				return (-1);
531 			}
532 
533 			strlcpy(kf->f_mntonname, mount.mnt_stat.f_mntonname,
534 			    sizeof(kf->f_mntonname));
535 		}
536 
537 		/* Fill in va_fsid, va_fileid, va_mode, va_size, va_rdev */
538 		filestat(kd, kf, vp);
539 		break;
540 	    }
541 
542 	case DTYPE_SOCKET: {
543 		struct socket sock;
544 		struct protosw protosw;
545 		struct domain domain;
546 
547 		if (KREAD(kd, (u_long)fp->f_data, &sock)) {
548 			_kvm_err(kd, kd->program, "can't read socket");
549 			return (-1);
550 		}
551 
552 		kf->so_type = sock.so_type;
553 		kf->so_state = sock.so_state;
554 		kf->so_pcb = PTRTOINT64(sock.so_pcb);
555 		if (KREAD(kd, (u_long)sock.so_proto, &protosw)) {
556 			_kvm_err(kd, kd->program, "can't read protosw");
557 			return (-1);
558 		}
559 		kf->so_protocol = protosw.pr_protocol;
560 		if (KREAD(kd, (u_long)protosw.pr_domain, &domain)) {
561 			_kvm_err(kd, kd->program, "can't read domain");
562 			return (-1);
563 		}
564 		kf->so_family = domain.dom_family;
565 		kf->so_rcv_cc = sock.so_rcv.sb_cc;
566 		kf->so_snd_cc = sock.so_snd.sb_cc;
567 		if (sock.so_splice) {
568 			kf->so_splice = PTRTOINT64(sock.so_splice);
569 			kf->so_splicelen = sock.so_splicelen;
570 		} else if (sock.so_spliceback)
571 			kf->so_splicelen = -1;
572 		if (!sock.so_pcb)
573 			break;
574 		switch (kf->so_family) {
575 		case AF_INET: {
576 			struct inpcb inpcb;
577 
578 			if (KREAD(kd, (u_long)sock.so_pcb, &inpcb)) {
579 				_kvm_err(kd, kd->program, "can't read inpcb");
580 				return (-1);
581 			}
582 			kf->inp_ppcb = PTRTOINT64(inpcb.inp_ppcb);
583 			kf->inp_lport = inpcb.inp_lport;
584 			kf->inp_laddru[0] = inpcb.inp_laddr.s_addr;
585 			kf->inp_fport = inpcb.inp_fport;
586 			kf->inp_faddru[0] = inpcb.inp_faddr.s_addr;
587 			kf->inp_rtableid = inpcb.inp_rtableid;
588 			break;
589 		    }
590 		case AF_INET6: {
591 			struct inpcb inpcb;
592 #define s6_addr32 __u6_addr.__u6_addr32
593 
594 			if (KREAD(kd, (u_long)sock.so_pcb, &inpcb)) {
595 				_kvm_err(kd, kd->program, "can't read inpcb");
596 				return (-1);
597 			}
598 			kf->inp_ppcb = PTRTOINT64(inpcb.inp_ppcb);
599 			kf->inp_lport = inpcb.inp_lport;
600 			kf->inp_laddru[0] = inpcb.inp_laddr6.s6_addr32[0];
601 			kf->inp_laddru[1] = inpcb.inp_laddr6.s6_addr32[1];
602 			kf->inp_laddru[2] = inpcb.inp_laddr6.s6_addr32[2];
603 			kf->inp_laddru[3] = inpcb.inp_laddr6.s6_addr32[3];
604 			kf->inp_fport = inpcb.inp_fport;
605 			kf->inp_faddru[0] = inpcb.inp_laddr6.s6_addr32[0];
606 			kf->inp_faddru[1] = inpcb.inp_faddr6.s6_addr32[1];
607 			kf->inp_faddru[2] = inpcb.inp_faddr6.s6_addr32[2];
608 			kf->inp_faddru[3] = inpcb.inp_faddr6.s6_addr32[3];
609 			kf->inp_rtableid = inpcb.inp_rtableid;
610 			break;
611 		    }
612 		case AF_UNIX: {
613 			struct unpcb unpcb;
614 
615 			if (KREAD(kd, (u_long)sock.so_pcb, &unpcb)) {
616 				_kvm_err(kd, kd->program, "can't read unpcb");
617 				return (-1);
618 			}
619 			kf->unp_conn	= PTRTOINT64(unpcb.unp_conn);
620 			kf->unp_refs	= PTRTOINT64(unpcb.unp_refs);
621 			kf->unp_nextref	= PTRTOINT64(unpcb.unp_nextref);
622 			kf->v_un	= PTRTOINT64(unpcb.unp_vnode);
623 			if (unpcb.unp_addr != NULL) {
624 				struct mbuf mb;
625 				struct sockaddr_un un;
626 
627 				if (KREAD(kd, (u_long)unpcb.unp_addr, &mb)) {
628 					_kvm_err(kd, kd->program,
629 					    "can't read sockaddr_un mbuf");
630 					return (-1);
631 				}
632 				if (KREAD(kd, (u_long)mb.m_data, &un)) {
633 					_kvm_err(kd, kd->program,
634 					    "can't read sockaddr_un");
635 					return (-1);
636 				}
637 
638 				kf->unp_addr = PTRTOINT64(unpcb.unp_addr);
639 				memcpy(kf->unp_path, un.sun_path, un.sun_len
640 				    - offsetof(struct sockaddr_un,sun_path));
641 			}
642 
643 			break;
644 		    }
645 		}
646 		break;
647 	    }
648 
649 	case DTYPE_PIPE: {
650 		struct pipe pipe;
651 
652 		if (KREAD(kd, (u_long)fp->f_data, &pipe)) {
653 			_kvm_err(kd, kd->program, "can't read pipe");
654 			return (-1);
655 		}
656 		kf->pipe_peer = PTRTOINT64(pipe.pipe_peer);
657 		kf->pipe_state = pipe.pipe_state;
658 		break;
659 	    }
660 
661 	case DTYPE_KQUEUE: {
662 		struct kqueue kqi;
663 
664 		if (KREAD(kd, (u_long)fp->f_data, &kqi)) {
665 			_kvm_err(kd, kd->program, "can't read kqi");
666 			return (-1);
667 		}
668 		kf->kq_count = kqi.kq_count;
669 		kf->kq_state = kqi.kq_state;
670 		break;
671 	    }
672 	case DTYPE_SYSTRACE: {
673 		struct fsystrace f;
674 
675 		if (KREAD(kd, (u_long)fp->f_data, &f)) {
676 			_kvm_err(kd, kd->program, "can't read fsystrace");
677 			return (-1);
678 		}
679 		kf->str_npolicies = f.npolicies;
680 		break;
681 	    }
682 	}
683 
684 	/* per-process information for KERN_FILE_BY[PU]ID */
685 	if (pr != NULL) {
686 		kf->p_pid = pid;
687 		kf->p_uid = pr->ps_ucred->cr_uid;
688 		kf->p_gid = pr->ps_ucred->cr_gid;
689 		kf->p_tid = -1;
690 		strlcpy(kf->p_comm, pr->ps_mainproc->p_comm,
691 		    sizeof(kf->p_comm));
692 		if (pr->ps_fd != NULL)
693 			kf->fd_ofileflags = pr->ps_fd->fd_ofileflags[fd];
694 	}
695 
696 	return (0);
697 }
698 
699 mode_t
700 _kvm_getftype(enum vtype v_type)
701 {
702 	mode_t ftype = 0;
703 
704 	switch (v_type) {
705 	case VREG:
706 		ftype = S_IFREG;
707 		break;
708 	case VDIR:
709 		ftype = S_IFDIR;
710 		break;
711 	case VBLK:
712 		ftype = S_IFBLK;
713 		break;
714 	case VCHR:
715 		ftype = S_IFCHR;
716 		break;
717 	case VLNK:
718 		ftype = S_IFLNK;
719 		break;
720 	case VSOCK:
721 		ftype = S_IFSOCK;
722 		break;
723 	case VFIFO:
724 		ftype = S_IFIFO;
725 		break;
726 	case VNON:
727 	case VBAD:
728 		break;
729 	}
730 
731 	return (ftype);
732 }
733 
734 static int
735 ufs_filestat(kvm_t *kd, struct kinfo_file *kf, struct vnode *vp)
736 {
737 	struct inode inode;
738 	struct ufs1_dinode di1;
739 
740 	if (KREAD(kd, (u_long)VTOI(vp), &inode)) {
741 		_kvm_err(kd, kd->program, "can't read inode at %p", VTOI(vp));
742 		return (-1);
743 	}
744 
745 	if (KREAD(kd, (u_long)inode.i_din1, &di1)) {
746 		_kvm_err(kd, kd->program, "can't read dinode at %p",
747 		    inode.i_din1);
748 		return (-1);
749 	}
750 
751 	inode.i_din1 = &di1;
752 
753 	kf->va_fsid = inode.i_dev & 0xffff;
754 	kf->va_fileid = (long)inode.i_number;
755 	kf->va_mode = inode.i_ffs1_mode;
756 	kf->va_size = inode.i_ffs1_size;
757 	kf->va_rdev = inode.i_ffs1_rdev;
758 
759 	return (0);
760 }
761 
762 static int
763 ext2fs_filestat(kvm_t *kd, struct kinfo_file *kf, struct vnode *vp)
764 {
765 	struct inode inode;
766 	struct ext2fs_dinode e2di;
767 
768 	if (KREAD(kd, (u_long)VTOI(vp), &inode)) {
769 		_kvm_err(kd, kd->program, "can't read inode at %p", VTOI(vp));
770 		return (-1);
771 	}
772 
773 	if (KREAD(kd, (u_long)inode.i_e2din, &e2di)) {
774 		_kvm_err(kd, kd->program, "can't read dinode at %p",
775 		    inode.i_e2din);
776 		return (-1);
777 	}
778 
779 	inode.i_e2din = &e2di;
780 
781 	kf->va_fsid = inode.i_dev & 0xffff;
782 	kf->va_fileid = (long)inode.i_number;
783 	kf->va_mode = inode.i_e2fs_mode;
784 	kf->va_size = inode.i_e2fs_size;
785 	kf->va_rdev = 0;	/* XXX */
786 
787 	return (0);
788 }
789 
790 static int
791 msdos_filestat(kvm_t *kd, struct kinfo_file *kf, struct vnode *vp)
792 {
793 	struct denode de;
794 	struct msdosfsmount mp;
795 
796 	if (KREAD(kd, (u_long)VTODE(vp), &de)) {
797 		_kvm_err(kd, kd->program, "can't read denode at %p", VTODE(vp));
798 		return (-1);
799 	}
800 	if (KREAD(kd, (u_long)de.de_pmp, &mp)) {
801 		_kvm_err(kd, kd->program, "can't read mount struct at %p",
802 		    de.de_pmp);
803 		return (-1);
804 	}
805 
806 	kf->va_fsid = de.de_dev & 0xffff;
807 	kf->va_fileid = 0; /* XXX see msdosfs_vptofh() for more info */
808 	kf->va_mode = (mp.pm_mask & 0777) | _kvm_getftype(vp->v_type);
809 	kf->va_size = de.de_FileSize;
810 	kf->va_rdev = 0;  /* msdosfs doesn't support device files */
811 
812 	return (0);
813 }
814 
815 static int
816 nfs_filestat(kvm_t *kd, struct kinfo_file *kf, struct vnode *vp)
817 {
818 	struct nfsnode nfsnode;
819 
820 	if (KREAD(kd, (u_long)VTONFS(vp), &nfsnode)) {
821 		_kvm_err(kd, kd->program, "can't read nfsnode at %p",
822 		    VTONFS(vp));
823 		return (-1);
824 	}
825 	kf->va_fsid = nfsnode.n_vattr.va_fsid;
826 	kf->va_fileid = nfsnode.n_vattr.va_fileid;
827 	kf->va_size = nfsnode.n_size;
828 	kf->va_rdev = nfsnode.n_vattr.va_rdev;
829 	kf->va_mode = (mode_t)nfsnode.n_vattr.va_mode | _kvm_getftype(vp->v_type);
830 
831 	return (0);
832 }
833 
834 static int
835 spec_filestat(kvm_t *kd, struct kinfo_file *kf, struct vnode *vp)
836 {
837 	struct specinfo		specinfo;
838 	struct vnode		parent;
839 
840 	if (KREAD(kd, (u_long)vp->v_specinfo, &specinfo)) {
841 		_kvm_err(kd, kd->program, "can't read specinfo at %p",
842 		     vp->v_specinfo);
843 		return (-1);
844 	}
845 
846 	vp->v_specinfo = &specinfo;
847 
848 	if (KREAD(kd, (u_long)vp->v_specparent, &parent)) {
849 		_kvm_err(kd, kd->program, "can't read parent vnode at %p",
850 		     vp->v_specparent);
851 		return (-1);
852 	}
853 
854 	if (ufs_filestat(kd, kf, vp))
855 		return (-1);
856 
857 	return (0);
858 }
859 
860 static int
861 filestat(kvm_t *kd, struct kinfo_file *kf, struct vnode *vp)
862 {
863 	int ret = 0;
864 
865 	if (vp->v_type != VNON && vp->v_type != VBAD) {
866 		switch (vp->v_tag) {
867 		case VT_UFS:
868 		case VT_MFS:
869 			ret = ufs_filestat(kd, kf, vp);
870 			break;
871 		case VT_NFS:
872 			ret = nfs_filestat(kd, kf, vp);
873 			break;
874 		case VT_EXT2FS:
875 			ret = ext2fs_filestat(kd, kf, vp);
876 			break;
877 		case VT_ISOFS:
878 			ret = _kvm_stat_cd9660(kd, kf, vp);
879 			break;
880 		case VT_MSDOSFS:
881 			ret = msdos_filestat(kd, kf, vp);
882 			break;
883 		case VT_UDF:
884 			ret = _kvm_stat_udf(kd, kf, vp);
885 			break;
886 		case VT_NTFS:
887 			ret = _kvm_stat_ntfs(kd, kf, vp);
888 			break;
889 		case VT_NON:
890 			if (vp->v_flag & VCLONE)
891 				ret = spec_filestat(kd, kf, vp);
892 			break;
893 		default:
894 			ret = -1;
895 			break;
896 		}
897 	}
898 	return (ret);
899 }
900