xref: /freebsd/lib/libprocstat/libprocstat.c (revision 3b1f7d9e5d6f44b50ff07fde6fd0e1135f213762)
1 /*-
2  * Copyright (c) 2009 Stanislav Sedov <stas@FreeBSD.org>
3  * Copyright (c) 1988, 1993
4  *      The Regents of the University of California.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *      This product includes software developed by the University of
17  *      California, Berkeley and its contributors.
18  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 
38 #include <sys/param.h>
39 #include <sys/elf.h>
40 #include <sys/time.h>
41 #include <sys/resourcevar.h>
42 #define	_WANT_UCRED
43 #include <sys/ucred.h>
44 #undef _WANT_UCRED
45 #include <sys/proc.h>
46 #include <sys/user.h>
47 #include <sys/stat.h>
48 #include <sys/vnode.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/domain.h>
52 #include <sys/protosw.h>
53 #include <sys/un.h>
54 #include <sys/unpcb.h>
55 #include <sys/sysctl.h>
56 #include <sys/tty.h>
57 #include <sys/filedesc.h>
58 #include <sys/queue.h>
59 #define	_WANT_FILE
60 #include <sys/file.h>
61 #include <sys/conf.h>
62 #include <sys/ksem.h>
63 #include <sys/mman.h>
64 #define	_KERNEL
65 #include <sys/mount.h>
66 #include <sys/pipe.h>
67 #include <ufs/ufs/quota.h>
68 #include <ufs/ufs/inode.h>
69 #include <fs/devfs/devfs.h>
70 #include <fs/devfs/devfs_int.h>
71 #undef _KERNEL
72 #include <nfs/nfsproto.h>
73 #include <nfsclient/nfs.h>
74 #include <nfsclient/nfsnode.h>
75 
76 #include <vm/vm.h>
77 #include <vm/vm_map.h>
78 #include <vm/vm_object.h>
79 
80 #include <net/route.h>
81 #include <netinet/in.h>
82 #include <netinet/in_systm.h>
83 #include <netinet/ip.h>
84 #include <netinet/in_pcb.h>
85 
86 #include <assert.h>
87 #include <ctype.h>
88 #include <err.h>
89 #include <fcntl.h>
90 #include <kvm.h>
91 #include <libutil.h>
92 #include <limits.h>
93 #include <paths.h>
94 #include <pwd.h>
95 #include <stdio.h>
96 #include <stdlib.h>
97 #include <stddef.h>
98 #include <string.h>
99 #include <unistd.h>
100 #include <netdb.h>
101 
102 #include <libprocstat.h>
103 #include "libprocstat_internal.h"
104 #include "common_kvm.h"
105 #include "core.h"
106 
107 int     statfs(const char *, struct statfs *);	/* XXX */
108 
109 #define	PROCSTAT_KVM	1
110 #define	PROCSTAT_SYSCTL	2
111 #define	PROCSTAT_CORE	3
112 
113 static char	**getargv(struct procstat *procstat, struct kinfo_proc *kp,
114     size_t nchr, int env);
115 static char	*getmnton(kvm_t *kd, struct mount *m);
116 static struct kinfo_vmentry *	kinfo_getvmmap_core(struct procstat_core *core,
117     int *cntp);
118 static Elf_Auxinfo	*procstat_getauxv_core(struct procstat_core *core,
119     unsigned int *cntp);
120 static Elf_Auxinfo	*procstat_getauxv_sysctl(pid_t pid, unsigned int *cntp);
121 static struct filestat_list	*procstat_getfiles_kvm(
122     struct procstat *procstat, struct kinfo_proc *kp, int mmapped);
123 static struct filestat_list	*procstat_getfiles_sysctl(
124     struct procstat *procstat, struct kinfo_proc *kp, int mmapped);
125 static int	procstat_get_pipe_info_sysctl(struct filestat *fst,
126     struct pipestat *pipe, char *errbuf);
127 static int	procstat_get_pipe_info_kvm(kvm_t *kd, struct filestat *fst,
128     struct pipestat *pipe, char *errbuf);
129 static int	procstat_get_pts_info_sysctl(struct filestat *fst,
130     struct ptsstat *pts, char *errbuf);
131 static int	procstat_get_pts_info_kvm(kvm_t *kd, struct filestat *fst,
132     struct ptsstat *pts, char *errbuf);
133 static int	procstat_get_sem_info_sysctl(struct filestat *fst,
134     struct semstat *sem, char *errbuf);
135 static int	procstat_get_sem_info_kvm(kvm_t *kd, struct filestat *fst,
136     struct semstat *sem, char *errbuf);
137 static int	procstat_get_shm_info_sysctl(struct filestat *fst,
138     struct shmstat *shm, char *errbuf);
139 static int	procstat_get_shm_info_kvm(kvm_t *kd, struct filestat *fst,
140     struct shmstat *shm, char *errbuf);
141 static int	procstat_get_socket_info_sysctl(struct filestat *fst,
142     struct sockstat *sock, char *errbuf);
143 static int	procstat_get_socket_info_kvm(kvm_t *kd, struct filestat *fst,
144     struct sockstat *sock, char *errbuf);
145 static int	to_filestat_flags(int flags);
146 static int	procstat_get_vnode_info_kvm(kvm_t *kd, struct filestat *fst,
147     struct vnstat *vn, char *errbuf);
148 static int	procstat_get_vnode_info_sysctl(struct filestat *fst,
149     struct vnstat *vn, char *errbuf);
150 static gid_t	*procstat_getgroups_core(struct procstat_core *core,
151     unsigned int *count);
152 static gid_t *	procstat_getgroups_kvm(kvm_t *kd, struct kinfo_proc *kp,
153     unsigned int *count);
154 static gid_t	*procstat_getgroups_sysctl(pid_t pid, unsigned int *count);
155 static struct kinfo_kstack	*procstat_getkstack_sysctl(pid_t pid,
156     int *cntp);
157 static int	procstat_getosrel_core(struct procstat_core *core,
158     int *osrelp);
159 static int	procstat_getosrel_kvm(kvm_t *kd, struct kinfo_proc *kp,
160     int *osrelp);
161 static int	procstat_getosrel_sysctl(pid_t pid, int *osrelp);
162 static int	procstat_getpathname_core(struct procstat_core *core,
163     char *pathname, size_t maxlen);
164 static int	procstat_getpathname_sysctl(pid_t pid, char *pathname,
165     size_t maxlen);
166 static int	procstat_getrlimit_core(struct procstat_core *core, int which,
167     struct rlimit* rlimit);
168 static int	procstat_getrlimit_kvm(kvm_t *kd, struct kinfo_proc *kp,
169     int which, struct rlimit* rlimit);
170 static int	procstat_getrlimit_sysctl(pid_t pid, int which,
171     struct rlimit* rlimit);
172 static int	procstat_getumask_core(struct procstat_core *core,
173     unsigned short *maskp);
174 static int	procstat_getumask_kvm(kvm_t *kd, struct kinfo_proc *kp,
175     unsigned short *maskp);
176 static int	procstat_getumask_sysctl(pid_t pid, unsigned short *maskp);
177 static int	vntype2psfsttype(int type);
178 
179 void
180 procstat_close(struct procstat *procstat)
181 {
182 
183 	assert(procstat);
184 	if (procstat->type == PROCSTAT_KVM)
185 		kvm_close(procstat->kd);
186 	else if (procstat->type == PROCSTAT_CORE)
187 		procstat_core_close(procstat->core);
188 	procstat_freeargv(procstat);
189 	procstat_freeenvv(procstat);
190 	free(procstat);
191 }
192 
193 struct procstat *
194 procstat_open_sysctl(void)
195 {
196 	struct procstat *procstat;
197 
198 	procstat = calloc(1, sizeof(*procstat));
199 	if (procstat == NULL) {
200 		warn("malloc()");
201 		return (NULL);
202 	}
203 	procstat->type = PROCSTAT_SYSCTL;
204 	return (procstat);
205 }
206 
207 struct procstat *
208 procstat_open_kvm(const char *nlistf, const char *memf)
209 {
210 	struct procstat *procstat;
211 	kvm_t *kd;
212 	char buf[_POSIX2_LINE_MAX];
213 
214 	procstat = calloc(1, sizeof(*procstat));
215 	if (procstat == NULL) {
216 		warn("malloc()");
217 		return (NULL);
218 	}
219 	kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf);
220 	if (kd == NULL) {
221 		warnx("kvm_openfiles(): %s", buf);
222 		free(procstat);
223 		return (NULL);
224 	}
225 	procstat->type = PROCSTAT_KVM;
226 	procstat->kd = kd;
227 	return (procstat);
228 }
229 
230 struct procstat *
231 procstat_open_core(const char *filename)
232 {
233 	struct procstat *procstat;
234 	struct procstat_core *core;
235 
236 	procstat = calloc(1, sizeof(*procstat));
237 	if (procstat == NULL) {
238 		warn("malloc()");
239 		return (NULL);
240 	}
241 	core = procstat_core_open(filename);
242 	if (core == NULL) {
243 		free(procstat);
244 		return (NULL);
245 	}
246 	procstat->type = PROCSTAT_CORE;
247 	procstat->core = core;
248 	return (procstat);
249 }
250 
251 struct kinfo_proc *
252 procstat_getprocs(struct procstat *procstat, int what, int arg,
253     unsigned int *count)
254 {
255 	struct kinfo_proc *p0, *p;
256 	size_t len, olen;
257 	int name[4];
258 	int cnt;
259 	int error;
260 
261 	assert(procstat);
262 	assert(count);
263 	p = NULL;
264 	if (procstat->type == PROCSTAT_KVM) {
265 		*count = 0;
266 		p0 = kvm_getprocs(procstat->kd, what, arg, &cnt);
267 		if (p0 == NULL || cnt <= 0)
268 			return (NULL);
269 		*count = cnt;
270 		len = *count * sizeof(*p);
271 		p = malloc(len);
272 		if (p == NULL) {
273 			warnx("malloc(%zu)", len);
274 			goto fail;
275 		}
276 		bcopy(p0, p, len);
277 		return (p);
278 	} else if (procstat->type == PROCSTAT_SYSCTL) {
279 		len = 0;
280 		name[0] = CTL_KERN;
281 		name[1] = KERN_PROC;
282 		name[2] = what;
283 		name[3] = arg;
284 		error = sysctl(name, 4, NULL, &len, NULL, 0);
285 		if (error < 0 && errno != EPERM) {
286 			warn("sysctl(kern.proc)");
287 			goto fail;
288 		}
289 		if (len == 0) {
290 			warnx("no processes?");
291 			goto fail;
292 		}
293 		do {
294 			len += len / 10;
295 			p = reallocf(p, len);
296 			if (p == NULL) {
297 				warnx("reallocf(%zu)", len);
298 				goto fail;
299 			}
300 			olen = len;
301 			error = sysctl(name, 4, p, &len, NULL, 0);
302 		} while (error < 0 && errno == ENOMEM && olen == len);
303 		if (error < 0 && errno != EPERM) {
304 			warn("sysctl(kern.proc)");
305 			goto fail;
306 		}
307 		/* Perform simple consistency checks. */
308 		if ((len % sizeof(*p)) != 0 || p->ki_structsize != sizeof(*p)) {
309 			warnx("kinfo_proc structure size mismatch (len = %zu)", len);
310 			goto fail;
311 		}
312 		*count = len / sizeof(*p);
313 		return (p);
314 	} else if (procstat->type == PROCSTAT_CORE) {
315 		p = procstat_core_get(procstat->core, PSC_TYPE_PROC, NULL,
316 		    &len);
317 		if ((len % sizeof(*p)) != 0 || p->ki_structsize != sizeof(*p)) {
318 			warnx("kinfo_proc structure size mismatch");
319 			goto fail;
320 		}
321 		*count = len / sizeof(*p);
322 		return (p);
323 	} else {
324 		warnx("unknown access method: %d", procstat->type);
325 		return (NULL);
326 	}
327 fail:
328 	if (p)
329 		free(p);
330 	return (NULL);
331 }
332 
333 void
334 procstat_freeprocs(struct procstat *procstat __unused, struct kinfo_proc *p)
335 {
336 
337 	if (p != NULL)
338 		free(p);
339 	p = NULL;
340 }
341 
342 struct filestat_list *
343 procstat_getfiles(struct procstat *procstat, struct kinfo_proc *kp, int mmapped)
344 {
345 
346 	switch(procstat->type) {
347 	case PROCSTAT_KVM:
348 		return (procstat_getfiles_kvm(procstat, kp, mmapped));
349 	case PROCSTAT_SYSCTL:
350 	case PROCSTAT_CORE:
351 		return (procstat_getfiles_sysctl(procstat, kp, mmapped));
352 	default:
353 		warnx("unknown access method: %d", procstat->type);
354 		return (NULL);
355 	}
356 }
357 
358 void
359 procstat_freefiles(struct procstat *procstat, struct filestat_list *head)
360 {
361 	struct filestat *fst, *tmp;
362 
363 	STAILQ_FOREACH_SAFE(fst, head, next, tmp) {
364 		if (fst->fs_path != NULL)
365 			free(fst->fs_path);
366 		free(fst);
367 	}
368 	free(head);
369 	if (procstat->vmentries != NULL) {
370 		free(procstat->vmentries);
371 		procstat->vmentries = NULL;
372 	}
373 	if (procstat->files != NULL) {
374 		free(procstat->files);
375 		procstat->files = NULL;
376 	}
377 }
378 
379 static struct filestat *
380 filestat_new_entry(void *typedep, int type, int fd, int fflags, int uflags,
381     int refcount, off_t offset, char *path, cap_rights_t cap_rights)
382 {
383 	struct filestat *entry;
384 
385 	entry = calloc(1, sizeof(*entry));
386 	if (entry == NULL) {
387 		warn("malloc()");
388 		return (NULL);
389 	}
390 	entry->fs_typedep = typedep;
391 	entry->fs_fflags = fflags;
392 	entry->fs_uflags = uflags;
393 	entry->fs_fd = fd;
394 	entry->fs_type = type;
395 	entry->fs_ref_count = refcount;
396 	entry->fs_offset = offset;
397 	entry->fs_path = path;
398 	entry->fs_cap_rights = cap_rights;
399 	return (entry);
400 }
401 
402 static struct vnode *
403 getctty(kvm_t *kd, struct kinfo_proc *kp)
404 {
405 	struct pgrp pgrp;
406 	struct proc proc;
407 	struct session sess;
408 	int error;
409 
410 	assert(kp);
411 	error = kvm_read_all(kd, (unsigned long)kp->ki_paddr, &proc,
412 	    sizeof(proc));
413 	if (error == 0) {
414 		warnx("can't read proc struct at %p for pid %d",
415 		    kp->ki_paddr, kp->ki_pid);
416 		return (NULL);
417 	}
418 	if (proc.p_pgrp == NULL)
419 		return (NULL);
420 	error = kvm_read_all(kd, (unsigned long)proc.p_pgrp, &pgrp,
421 	    sizeof(pgrp));
422 	if (error == 0) {
423 		warnx("can't read pgrp struct at %p for pid %d",
424 		    proc.p_pgrp, kp->ki_pid);
425 		return (NULL);
426 	}
427 	error = kvm_read_all(kd, (unsigned long)pgrp.pg_session, &sess,
428 	    sizeof(sess));
429 	if (error == 0) {
430 		warnx("can't read session struct at %p for pid %d",
431 		    pgrp.pg_session, kp->ki_pid);
432 		return (NULL);
433 	}
434 	return (sess.s_ttyvp);
435 }
436 
437 static struct filestat_list *
438 procstat_getfiles_kvm(struct procstat *procstat, struct kinfo_proc *kp, int mmapped)
439 {
440 	struct file file;
441 	struct filedesc filed;
442 	struct vm_map_entry vmentry;
443 	struct vm_object object;
444 	struct vmspace vmspace;
445 	vm_map_entry_t entryp;
446 	vm_map_t map;
447 	vm_object_t objp;
448 	struct vnode *vp;
449 	struct file **ofiles;
450 	struct filestat *entry;
451 	struct filestat_list *head;
452 	kvm_t *kd;
453 	void *data;
454 	int i, fflags;
455 	int prot, type;
456 	unsigned int nfiles;
457 
458 	assert(procstat);
459 	kd = procstat->kd;
460 	if (kd == NULL)
461 		return (NULL);
462 	if (kp->ki_fd == NULL)
463 		return (NULL);
464 	if (!kvm_read_all(kd, (unsigned long)kp->ki_fd, &filed,
465 	    sizeof(filed))) {
466 		warnx("can't read filedesc at %p", (void *)kp->ki_fd);
467 		return (NULL);
468 	}
469 
470 	/*
471 	 * Allocate list head.
472 	 */
473 	head = malloc(sizeof(*head));
474 	if (head == NULL)
475 		return (NULL);
476 	STAILQ_INIT(head);
477 
478 	/* root directory vnode, if one. */
479 	if (filed.fd_rdir) {
480 		entry = filestat_new_entry(filed.fd_rdir, PS_FST_TYPE_VNODE, -1,
481 		    PS_FST_FFLAG_READ, PS_FST_UFLAG_RDIR, 0, 0, NULL, 0);
482 		if (entry != NULL)
483 			STAILQ_INSERT_TAIL(head, entry, next);
484 	}
485 	/* current working directory vnode. */
486 	if (filed.fd_cdir) {
487 		entry = filestat_new_entry(filed.fd_cdir, PS_FST_TYPE_VNODE, -1,
488 		    PS_FST_FFLAG_READ, PS_FST_UFLAG_CDIR, 0, 0, NULL, 0);
489 		if (entry != NULL)
490 			STAILQ_INSERT_TAIL(head, entry, next);
491 	}
492 	/* jail root, if any. */
493 	if (filed.fd_jdir) {
494 		entry = filestat_new_entry(filed.fd_jdir, PS_FST_TYPE_VNODE, -1,
495 		    PS_FST_FFLAG_READ, PS_FST_UFLAG_JAIL, 0, 0, NULL, 0);
496 		if (entry != NULL)
497 			STAILQ_INSERT_TAIL(head, entry, next);
498 	}
499 	/* ktrace vnode, if one */
500 	if (kp->ki_tracep) {
501 		entry = filestat_new_entry(kp->ki_tracep, PS_FST_TYPE_VNODE, -1,
502 		    PS_FST_FFLAG_READ | PS_FST_FFLAG_WRITE,
503 		    PS_FST_UFLAG_TRACE, 0, 0, NULL, 0);
504 		if (entry != NULL)
505 			STAILQ_INSERT_TAIL(head, entry, next);
506 	}
507 	/* text vnode, if one */
508 	if (kp->ki_textvp) {
509 		entry = filestat_new_entry(kp->ki_textvp, PS_FST_TYPE_VNODE, -1,
510 		    PS_FST_FFLAG_READ, PS_FST_UFLAG_TEXT, 0, 0, NULL, 0);
511 		if (entry != NULL)
512 			STAILQ_INSERT_TAIL(head, entry, next);
513 	}
514 	/* Controlling terminal. */
515 	if ((vp = getctty(kd, kp)) != NULL) {
516 		entry = filestat_new_entry(vp, PS_FST_TYPE_VNODE, -1,
517 		    PS_FST_FFLAG_READ | PS_FST_FFLAG_WRITE,
518 		    PS_FST_UFLAG_CTTY, 0, 0, NULL, 0);
519 		if (entry != NULL)
520 			STAILQ_INSERT_TAIL(head, entry, next);
521 	}
522 
523 	nfiles = filed.fd_lastfile + 1;
524 	ofiles = malloc(nfiles * sizeof(struct file *));
525 	if (ofiles == NULL) {
526 		warn("malloc(%zu)", nfiles * sizeof(struct file *));
527 		goto do_mmapped;
528 	}
529 	if (!kvm_read_all(kd, (unsigned long)filed.fd_ofiles, ofiles,
530 	    nfiles * sizeof(struct file *))) {
531 		warnx("cannot read file structures at %p",
532 		    (void *)filed.fd_ofiles);
533 		free(ofiles);
534 		goto do_mmapped;
535 	}
536 	for (i = 0; i <= filed.fd_lastfile; i++) {
537 		if (ofiles[i] == NULL)
538 			continue;
539 		if (!kvm_read_all(kd, (unsigned long)ofiles[i], &file,
540 		    sizeof(struct file))) {
541 			warnx("can't read file %d at %p", i,
542 			    (void *)ofiles[i]);
543 			continue;
544 		}
545 		switch (file.f_type) {
546 		case DTYPE_VNODE:
547 			type = PS_FST_TYPE_VNODE;
548 			data = file.f_vnode;
549 			break;
550 		case DTYPE_SOCKET:
551 			type = PS_FST_TYPE_SOCKET;
552 			data = file.f_data;
553 			break;
554 		case DTYPE_PIPE:
555 			type = PS_FST_TYPE_PIPE;
556 			data = file.f_data;
557 			break;
558 		case DTYPE_FIFO:
559 			type = PS_FST_TYPE_FIFO;
560 			data = file.f_vnode;
561 			break;
562 #ifdef DTYPE_PTS
563 		case DTYPE_PTS:
564 			type = PS_FST_TYPE_PTS;
565 			data = file.f_data;
566 			break;
567 #endif
568 		case DTYPE_SEM:
569 			type = PS_FST_TYPE_SEM;
570 			data = file.f_data;
571 			break;
572 		case DTYPE_SHM:
573 			type = PS_FST_TYPE_SHM;
574 			data = file.f_data;
575 			break;
576 		default:
577 			continue;
578 		}
579 		/* XXXRW: No capability rights support for kvm yet. */
580 		entry = filestat_new_entry(data, type, i,
581 		    to_filestat_flags(file.f_flag), 0, 0, 0, NULL, 0);
582 		if (entry != NULL)
583 			STAILQ_INSERT_TAIL(head, entry, next);
584 	}
585 	free(ofiles);
586 
587 do_mmapped:
588 
589 	/*
590 	 * Process mmapped files if requested.
591 	 */
592 	if (mmapped) {
593 		if (!kvm_read_all(kd, (unsigned long)kp->ki_vmspace, &vmspace,
594 		    sizeof(vmspace))) {
595 			warnx("can't read vmspace at %p",
596 			    (void *)kp->ki_vmspace);
597 			goto exit;
598 		}
599 		map = &vmspace.vm_map;
600 
601 		for (entryp = map->header.next;
602 		    entryp != &kp->ki_vmspace->vm_map.header;
603 		    entryp = vmentry.next) {
604 			if (!kvm_read_all(kd, (unsigned long)entryp, &vmentry,
605 			    sizeof(vmentry))) {
606 				warnx("can't read vm_map_entry at %p",
607 				    (void *)entryp);
608 				continue;
609 			}
610 			if (vmentry.eflags & MAP_ENTRY_IS_SUB_MAP)
611 				continue;
612 			if ((objp = vmentry.object.vm_object) == NULL)
613 				continue;
614 			for (; objp; objp = object.backing_object) {
615 				if (!kvm_read_all(kd, (unsigned long)objp,
616 				    &object, sizeof(object))) {
617 					warnx("can't read vm_object at %p",
618 					    (void *)objp);
619 					break;
620 				}
621 			}
622 
623 			/* We want only vnode objects. */
624 			if (object.type != OBJT_VNODE)
625 				continue;
626 
627 			prot = vmentry.protection;
628 			fflags = 0;
629 			if (prot & VM_PROT_READ)
630 				fflags = PS_FST_FFLAG_READ;
631 			if ((vmentry.eflags & MAP_ENTRY_COW) == 0 &&
632 			    prot & VM_PROT_WRITE)
633 				fflags |= PS_FST_FFLAG_WRITE;
634 
635 			/*
636 			 * Create filestat entry.
637 			 */
638 			entry = filestat_new_entry(object.handle,
639 			    PS_FST_TYPE_VNODE, -1, fflags,
640 			    PS_FST_UFLAG_MMAP, 0, 0, NULL, 0);
641 			if (entry != NULL)
642 				STAILQ_INSERT_TAIL(head, entry, next);
643 		}
644 	}
645 exit:
646 	return (head);
647 }
648 
649 /*
650  * kinfo types to filestat translation.
651  */
652 static int
653 kinfo_type2fst(int kftype)
654 {
655 	static struct {
656 		int	kf_type;
657 		int	fst_type;
658 	} kftypes2fst[] = {
659 		{ KF_TYPE_CRYPTO, PS_FST_TYPE_CRYPTO },
660 		{ KF_TYPE_FIFO, PS_FST_TYPE_FIFO },
661 		{ KF_TYPE_KQUEUE, PS_FST_TYPE_KQUEUE },
662 		{ KF_TYPE_MQUEUE, PS_FST_TYPE_MQUEUE },
663 		{ KF_TYPE_NONE, PS_FST_TYPE_NONE },
664 		{ KF_TYPE_PIPE, PS_FST_TYPE_PIPE },
665 		{ KF_TYPE_PTS, PS_FST_TYPE_PTS },
666 		{ KF_TYPE_SEM, PS_FST_TYPE_SEM },
667 		{ KF_TYPE_SHM, PS_FST_TYPE_SHM },
668 		{ KF_TYPE_SOCKET, PS_FST_TYPE_SOCKET },
669 		{ KF_TYPE_VNODE, PS_FST_TYPE_VNODE },
670 		{ KF_TYPE_UNKNOWN, PS_FST_TYPE_UNKNOWN }
671 	};
672 #define NKFTYPES	(sizeof(kftypes2fst) / sizeof(*kftypes2fst))
673 	unsigned int i;
674 
675 	for (i = 0; i < NKFTYPES; i++)
676 		if (kftypes2fst[i].kf_type == kftype)
677 			break;
678 	if (i == NKFTYPES)
679 		return (PS_FST_TYPE_UNKNOWN);
680 	return (kftypes2fst[i].fst_type);
681 }
682 
683 /*
684  * kinfo flags to filestat translation.
685  */
686 static int
687 kinfo_fflags2fst(int kfflags)
688 {
689 	static struct {
690 		int	kf_flag;
691 		int	fst_flag;
692 	} kfflags2fst[] = {
693 		{ KF_FLAG_APPEND, PS_FST_FFLAG_APPEND },
694 		{ KF_FLAG_ASYNC, PS_FST_FFLAG_ASYNC },
695 		{ KF_FLAG_CREAT, PS_FST_FFLAG_CREAT },
696 		{ KF_FLAG_DIRECT, PS_FST_FFLAG_DIRECT },
697 		{ KF_FLAG_EXCL, PS_FST_FFLAG_EXCL },
698 		{ KF_FLAG_EXEC, PS_FST_FFLAG_EXEC },
699 		{ KF_FLAG_EXLOCK, PS_FST_FFLAG_EXLOCK },
700 		{ KF_FLAG_FSYNC, PS_FST_FFLAG_SYNC },
701 		{ KF_FLAG_HASLOCK, PS_FST_FFLAG_HASLOCK },
702 		{ KF_FLAG_NOFOLLOW, PS_FST_FFLAG_NOFOLLOW },
703 		{ KF_FLAG_NONBLOCK, PS_FST_FFLAG_NONBLOCK },
704 		{ KF_FLAG_READ, PS_FST_FFLAG_READ },
705 		{ KF_FLAG_SHLOCK, PS_FST_FFLAG_SHLOCK },
706 		{ KF_FLAG_TRUNC, PS_FST_FFLAG_TRUNC },
707 		{ KF_FLAG_WRITE, PS_FST_FFLAG_WRITE }
708 	};
709 #define NKFFLAGS	(sizeof(kfflags2fst) / sizeof(*kfflags2fst))
710 	unsigned int i;
711 	int flags;
712 
713 	flags = 0;
714 	for (i = 0; i < NKFFLAGS; i++)
715 		if ((kfflags & kfflags2fst[i].kf_flag) != 0)
716 			flags |= kfflags2fst[i].fst_flag;
717 	return (flags);
718 }
719 
720 static int
721 kinfo_uflags2fst(int fd)
722 {
723 
724 	switch (fd) {
725 	case KF_FD_TYPE_CTTY:
726 		return (PS_FST_UFLAG_CTTY);
727 	case KF_FD_TYPE_CWD:
728 		return (PS_FST_UFLAG_CDIR);
729 	case KF_FD_TYPE_JAIL:
730 		return (PS_FST_UFLAG_JAIL);
731 	case KF_FD_TYPE_TEXT:
732 		return (PS_FST_UFLAG_TEXT);
733 	case KF_FD_TYPE_TRACE:
734 		return (PS_FST_UFLAG_TRACE);
735 	case KF_FD_TYPE_ROOT:
736 		return (PS_FST_UFLAG_RDIR);
737 	}
738 	return (0);
739 }
740 
741 static struct kinfo_file *
742 kinfo_getfile_core(struct procstat_core *core, int *cntp)
743 {
744 	int cnt;
745 	size_t len;
746 	char *buf, *bp, *eb;
747 	struct kinfo_file *kif, *kp, *kf;
748 
749 	buf = procstat_core_get(core, PSC_TYPE_FILES, NULL, &len);
750 	if (buf == NULL)
751 		return (NULL);
752 	/*
753 	 * XXXMG: The code below is just copy&past from libutil.
754 	 * The code duplication can be avoided if libutil
755 	 * is extended to provide something like:
756 	 *   struct kinfo_file *kinfo_getfile_from_buf(const char *buf,
757 	 *       size_t len, int *cntp);
758 	 */
759 
760 	/* Pass 1: count items */
761 	cnt = 0;
762 	bp = buf;
763 	eb = buf + len;
764 	while (bp < eb) {
765 		kf = (struct kinfo_file *)(uintptr_t)bp;
766 		bp += kf->kf_structsize;
767 		cnt++;
768 	}
769 
770 	kif = calloc(cnt, sizeof(*kif));
771 	if (kif == NULL) {
772 		free(buf);
773 		return (NULL);
774 	}
775 	bp = buf;
776 	eb = buf + len;
777 	kp = kif;
778 	/* Pass 2: unpack */
779 	while (bp < eb) {
780 		kf = (struct kinfo_file *)(uintptr_t)bp;
781 		/* Copy/expand into pre-zeroed buffer */
782 		memcpy(kp, kf, kf->kf_structsize);
783 		/* Advance to next packed record */
784 		bp += kf->kf_structsize;
785 		/* Set field size to fixed length, advance */
786 		kp->kf_structsize = sizeof(*kp);
787 		kp++;
788 	}
789 	free(buf);
790 	*cntp = cnt;
791 	return (kif);	/* Caller must free() return value */
792 }
793 
794 static struct filestat_list *
795 procstat_getfiles_sysctl(struct procstat *procstat, struct kinfo_proc *kp,
796     int mmapped)
797 {
798 	struct kinfo_file *kif, *files;
799 	struct kinfo_vmentry *kve, *vmentries;
800 	struct filestat_list *head;
801 	struct filestat *entry;
802 	char *path;
803 	off_t offset;
804 	int cnt, fd, fflags;
805 	int i, type, uflags;
806 	int refcount;
807 	cap_rights_t cap_rights;
808 
809 	assert(kp);
810 	if (kp->ki_fd == NULL)
811 		return (NULL);
812 	switch(procstat->type) {
813 	case PROCSTAT_SYSCTL:
814 		files = kinfo_getfile(kp->ki_pid, &cnt);
815 		break;
816 	case PROCSTAT_CORE:
817 		files = kinfo_getfile_core(procstat->core, &cnt);
818 		break;
819 	default:
820 		assert(!"invalid type");
821 	}
822 	if (files == NULL && errno != EPERM) {
823 		warn("kinfo_getfile()");
824 		return (NULL);
825 	}
826 	procstat->files = files;
827 
828 	/*
829 	 * Allocate list head.
830 	 */
831 	head = malloc(sizeof(*head));
832 	if (head == NULL)
833 		return (NULL);
834 	STAILQ_INIT(head);
835 	for (i = 0; i < cnt; i++) {
836 		kif = &files[i];
837 
838 		type = kinfo_type2fst(kif->kf_type);
839 		fd = kif->kf_fd >= 0 ? kif->kf_fd : -1;
840 		fflags = kinfo_fflags2fst(kif->kf_flags);
841 		uflags = kinfo_uflags2fst(kif->kf_fd);
842 		refcount = kif->kf_ref_count;
843 		offset = kif->kf_offset;
844 		if (*kif->kf_path != '\0')
845 			path = strdup(kif->kf_path);
846 		else
847 			path = NULL;
848 		cap_rights = kif->kf_cap_rights;
849 
850 		/*
851 		 * Create filestat entry.
852 		 */
853 		entry = filestat_new_entry(kif, type, fd, fflags, uflags,
854 		    refcount, offset, path, cap_rights);
855 		if (entry != NULL)
856 			STAILQ_INSERT_TAIL(head, entry, next);
857 	}
858 	if (mmapped != 0) {
859 		vmentries = procstat_getvmmap(procstat, kp, &cnt);
860 		procstat->vmentries = vmentries;
861 		if (vmentries == NULL || cnt == 0)
862 			goto fail;
863 		for (i = 0; i < cnt; i++) {
864 			kve = &vmentries[i];
865 			if (kve->kve_type != KVME_TYPE_VNODE)
866 				continue;
867 			fflags = 0;
868 			if (kve->kve_protection & KVME_PROT_READ)
869 				fflags = PS_FST_FFLAG_READ;
870 			if ((kve->kve_flags & KVME_FLAG_COW) == 0 &&
871 			    kve->kve_protection & KVME_PROT_WRITE)
872 				fflags |= PS_FST_FFLAG_WRITE;
873 			offset = kve->kve_offset;
874 			refcount = kve->kve_ref_count;
875 			if (*kve->kve_path != '\0')
876 				path = strdup(kve->kve_path);
877 			else
878 				path = NULL;
879 			entry = filestat_new_entry(kve, PS_FST_TYPE_VNODE, -1,
880 			    fflags, PS_FST_UFLAG_MMAP, refcount, offset, path,
881 			    0);
882 			if (entry != NULL)
883 				STAILQ_INSERT_TAIL(head, entry, next);
884 		}
885 	}
886 fail:
887 	return (head);
888 }
889 
890 int
891 procstat_get_pipe_info(struct procstat *procstat, struct filestat *fst,
892     struct pipestat *ps, char *errbuf)
893 {
894 
895 	assert(ps);
896 	if (procstat->type == PROCSTAT_KVM) {
897 		return (procstat_get_pipe_info_kvm(procstat->kd, fst, ps,
898 		    errbuf));
899 	} else if (procstat->type == PROCSTAT_SYSCTL ||
900 		procstat->type == PROCSTAT_CORE) {
901 		return (procstat_get_pipe_info_sysctl(fst, ps, errbuf));
902 	} else {
903 		warnx("unknown access method: %d", procstat->type);
904 		if (errbuf != NULL)
905 			snprintf(errbuf, _POSIX2_LINE_MAX, "error");
906 		return (1);
907 	}
908 }
909 
910 static int
911 procstat_get_pipe_info_kvm(kvm_t *kd, struct filestat *fst,
912     struct pipestat *ps, char *errbuf)
913 {
914 	struct pipe pi;
915 	void *pipep;
916 
917 	assert(kd);
918 	assert(ps);
919 	assert(fst);
920 	bzero(ps, sizeof(*ps));
921 	pipep = fst->fs_typedep;
922 	if (pipep == NULL)
923 		goto fail;
924 	if (!kvm_read_all(kd, (unsigned long)pipep, &pi, sizeof(struct pipe))) {
925 		warnx("can't read pipe at %p", (void *)pipep);
926 		goto fail;
927 	}
928 	ps->addr = (uintptr_t)pipep;
929 	ps->peer = (uintptr_t)pi.pipe_peer;
930 	ps->buffer_cnt = pi.pipe_buffer.cnt;
931 	return (0);
932 
933 fail:
934 	if (errbuf != NULL)
935 		snprintf(errbuf, _POSIX2_LINE_MAX, "error");
936 	return (1);
937 }
938 
939 static int
940 procstat_get_pipe_info_sysctl(struct filestat *fst, struct pipestat *ps,
941     char *errbuf __unused)
942 {
943 	struct kinfo_file *kif;
944 
945 	assert(ps);
946 	assert(fst);
947 	bzero(ps, sizeof(*ps));
948 	kif = fst->fs_typedep;
949 	if (kif == NULL)
950 		return (1);
951 	ps->addr = kif->kf_un.kf_pipe.kf_pipe_addr;
952 	ps->peer = kif->kf_un.kf_pipe.kf_pipe_peer;
953 	ps->buffer_cnt = kif->kf_un.kf_pipe.kf_pipe_buffer_cnt;
954 	return (0);
955 }
956 
957 int
958 procstat_get_pts_info(struct procstat *procstat, struct filestat *fst,
959     struct ptsstat *pts, char *errbuf)
960 {
961 
962 	assert(pts);
963 	if (procstat->type == PROCSTAT_KVM) {
964 		return (procstat_get_pts_info_kvm(procstat->kd, fst, pts,
965 		    errbuf));
966 	} else if (procstat->type == PROCSTAT_SYSCTL ||
967 		procstat->type == PROCSTAT_CORE) {
968 		return (procstat_get_pts_info_sysctl(fst, pts, errbuf));
969 	} else {
970 		warnx("unknown access method: %d", procstat->type);
971 		if (errbuf != NULL)
972 			snprintf(errbuf, _POSIX2_LINE_MAX, "error");
973 		return (1);
974 	}
975 }
976 
977 static int
978 procstat_get_pts_info_kvm(kvm_t *kd, struct filestat *fst,
979     struct ptsstat *pts, char *errbuf)
980 {
981 	struct tty tty;
982 	void *ttyp;
983 
984 	assert(kd);
985 	assert(pts);
986 	assert(fst);
987 	bzero(pts, sizeof(*pts));
988 	ttyp = fst->fs_typedep;
989 	if (ttyp == NULL)
990 		goto fail;
991 	if (!kvm_read_all(kd, (unsigned long)ttyp, &tty, sizeof(struct tty))) {
992 		warnx("can't read tty at %p", (void *)ttyp);
993 		goto fail;
994 	}
995 	pts->dev = dev2udev(kd, tty.t_dev);
996 	(void)kdevtoname(kd, tty.t_dev, pts->devname);
997 	return (0);
998 
999 fail:
1000 	if (errbuf != NULL)
1001 		snprintf(errbuf, _POSIX2_LINE_MAX, "error");
1002 	return (1);
1003 }
1004 
1005 static int
1006 procstat_get_pts_info_sysctl(struct filestat *fst, struct ptsstat *pts,
1007     char *errbuf __unused)
1008 {
1009 	struct kinfo_file *kif;
1010 
1011 	assert(pts);
1012 	assert(fst);
1013 	bzero(pts, sizeof(*pts));
1014 	kif = fst->fs_typedep;
1015 	if (kif == NULL)
1016 		return (0);
1017 	pts->dev = kif->kf_un.kf_pts.kf_pts_dev;
1018 	strlcpy(pts->devname, kif->kf_path, sizeof(pts->devname));
1019 	return (0);
1020 }
1021 
1022 int
1023 procstat_get_sem_info(struct procstat *procstat, struct filestat *fst,
1024     struct semstat *sem, char *errbuf)
1025 {
1026 
1027 	assert(sem);
1028 	if (procstat->type == PROCSTAT_KVM) {
1029 		return (procstat_get_sem_info_kvm(procstat->kd, fst, sem,
1030 		    errbuf));
1031 	} else if (procstat->type == PROCSTAT_SYSCTL ||
1032 	    procstat->type == PROCSTAT_CORE) {
1033 		return (procstat_get_sem_info_sysctl(fst, sem, errbuf));
1034 	} else {
1035 		warnx("unknown access method: %d", procstat->type);
1036 		if (errbuf != NULL)
1037 			snprintf(errbuf, _POSIX2_LINE_MAX, "error");
1038 		return (1);
1039 	}
1040 }
1041 
1042 static int
1043 procstat_get_sem_info_kvm(kvm_t *kd, struct filestat *fst,
1044     struct semstat *sem, char *errbuf)
1045 {
1046 	struct ksem ksem;
1047 	void *ksemp;
1048 	char *path;
1049 	int i;
1050 
1051 	assert(kd);
1052 	assert(sem);
1053 	assert(fst);
1054 	bzero(sem, sizeof(*sem));
1055 	ksemp = fst->fs_typedep;
1056 	if (ksemp == NULL)
1057 		goto fail;
1058 	if (!kvm_read_all(kd, (unsigned long)ksemp, &ksem,
1059 	    sizeof(struct ksem))) {
1060 		warnx("can't read ksem at %p", (void *)ksemp);
1061 		goto fail;
1062 	}
1063 	sem->mode = S_IFREG | ksem.ks_mode;
1064 	sem->value = ksem.ks_value;
1065 	if (fst->fs_path == NULL && ksem.ks_path != NULL) {
1066 		path = malloc(MAXPATHLEN);
1067 		for (i = 0; i < MAXPATHLEN - 1; i++) {
1068 			if (!kvm_read_all(kd, (unsigned long)ksem.ks_path + i,
1069 			    path + i, 1))
1070 				break;
1071 			if (path[i] == '\0')
1072 				break;
1073 		}
1074 		path[i] = '\0';
1075 		if (i == 0)
1076 			free(path);
1077 		else
1078 			fst->fs_path = path;
1079 	}
1080 	return (0);
1081 
1082 fail:
1083 	if (errbuf != NULL)
1084 		snprintf(errbuf, _POSIX2_LINE_MAX, "error");
1085 	return (1);
1086 }
1087 
1088 static int
1089 procstat_get_sem_info_sysctl(struct filestat *fst, struct semstat *sem,
1090     char *errbuf __unused)
1091 {
1092 	struct kinfo_file *kif;
1093 
1094 	assert(sem);
1095 	assert(fst);
1096 	bzero(sem, sizeof(*sem));
1097 	kif = fst->fs_typedep;
1098 	if (kif == NULL)
1099 		return (0);
1100 	sem->value = kif->kf_un.kf_sem.kf_sem_value;
1101 	sem->mode = kif->kf_un.kf_sem.kf_sem_mode;
1102 	return (0);
1103 }
1104 
1105 int
1106 procstat_get_shm_info(struct procstat *procstat, struct filestat *fst,
1107     struct shmstat *shm, char *errbuf)
1108 {
1109 
1110 	assert(shm);
1111 	if (procstat->type == PROCSTAT_KVM) {
1112 		return (procstat_get_shm_info_kvm(procstat->kd, fst, shm,
1113 		    errbuf));
1114 	} else if (procstat->type == PROCSTAT_SYSCTL ||
1115 	    procstat->type == PROCSTAT_CORE) {
1116 		return (procstat_get_shm_info_sysctl(fst, shm, errbuf));
1117 	} else {
1118 		warnx("unknown access method: %d", procstat->type);
1119 		if (errbuf != NULL)
1120 			snprintf(errbuf, _POSIX2_LINE_MAX, "error");
1121 		return (1);
1122 	}
1123 }
1124 
1125 static int
1126 procstat_get_shm_info_kvm(kvm_t *kd, struct filestat *fst,
1127     struct shmstat *shm, char *errbuf)
1128 {
1129 	struct shmfd shmfd;
1130 	void *shmfdp;
1131 	char *path;
1132 	int i;
1133 
1134 	assert(kd);
1135 	assert(shm);
1136 	assert(fst);
1137 	bzero(shm, sizeof(*shm));
1138 	shmfdp = fst->fs_typedep;
1139 	if (shmfdp == NULL)
1140 		goto fail;
1141 	if (!kvm_read_all(kd, (unsigned long)shmfdp, &shmfd,
1142 	    sizeof(struct shmfd))) {
1143 		warnx("can't read shmfd at %p", (void *)shmfdp);
1144 		goto fail;
1145 	}
1146 	shm->mode = S_IFREG | shmfd.shm_mode;
1147 	shm->size = shmfd.shm_size;
1148 	if (fst->fs_path == NULL && shmfd.shm_path != NULL) {
1149 		path = malloc(MAXPATHLEN);
1150 		for (i = 0; i < MAXPATHLEN - 1; i++) {
1151 			if (!kvm_read_all(kd, (unsigned long)shmfd.shm_path + i,
1152 			    path + i, 1))
1153 				break;
1154 			if (path[i] == '\0')
1155 				break;
1156 		}
1157 		path[i] = '\0';
1158 		if (i == 0)
1159 			free(path);
1160 		else
1161 			fst->fs_path = path;
1162 	}
1163 	return (0);
1164 
1165 fail:
1166 	if (errbuf != NULL)
1167 		snprintf(errbuf, _POSIX2_LINE_MAX, "error");
1168 	return (1);
1169 }
1170 
1171 static int
1172 procstat_get_shm_info_sysctl(struct filestat *fst, struct shmstat *shm,
1173     char *errbuf __unused)
1174 {
1175 	struct kinfo_file *kif;
1176 
1177 	assert(shm);
1178 	assert(fst);
1179 	bzero(shm, sizeof(*shm));
1180 	kif = fst->fs_typedep;
1181 	if (kif == NULL)
1182 		return (0);
1183 	shm->size = kif->kf_un.kf_file.kf_file_size;
1184 	shm->mode = kif->kf_un.kf_file.kf_file_mode;
1185 	return (0);
1186 }
1187 
1188 int
1189 procstat_get_vnode_info(struct procstat *procstat, struct filestat *fst,
1190     struct vnstat *vn, char *errbuf)
1191 {
1192 
1193 	assert(vn);
1194 	if (procstat->type == PROCSTAT_KVM) {
1195 		return (procstat_get_vnode_info_kvm(procstat->kd, fst, vn,
1196 		    errbuf));
1197 	} else if (procstat->type == PROCSTAT_SYSCTL ||
1198 		procstat->type == PROCSTAT_CORE) {
1199 		return (procstat_get_vnode_info_sysctl(fst, vn, errbuf));
1200 	} else {
1201 		warnx("unknown access method: %d", procstat->type);
1202 		if (errbuf != NULL)
1203 			snprintf(errbuf, _POSIX2_LINE_MAX, "error");
1204 		return (1);
1205 	}
1206 }
1207 
1208 static int
1209 procstat_get_vnode_info_kvm(kvm_t *kd, struct filestat *fst,
1210     struct vnstat *vn, char *errbuf)
1211 {
1212 	/* Filesystem specific handlers. */
1213 	#define FSTYPE(fst)     {#fst, fst##_filestat}
1214 	struct {
1215 		const char	*tag;
1216 		int		(*handler)(kvm_t *kd, struct vnode *vp,
1217 		    struct vnstat *vn);
1218 	} fstypes[] = {
1219 		FSTYPE(devfs),
1220 		FSTYPE(isofs),
1221 		FSTYPE(msdosfs),
1222 		FSTYPE(nfs),
1223 		FSTYPE(udf),
1224 		FSTYPE(ufs),
1225 #ifdef LIBPROCSTAT_ZFS
1226 		FSTYPE(zfs),
1227 #endif
1228 	};
1229 #define	NTYPES	(sizeof(fstypes) / sizeof(*fstypes))
1230 	struct vnode vnode;
1231 	char tagstr[12];
1232 	void *vp;
1233 	int error, found;
1234 	unsigned int i;
1235 
1236 	assert(kd);
1237 	assert(vn);
1238 	assert(fst);
1239 	vp = fst->fs_typedep;
1240 	if (vp == NULL)
1241 		goto fail;
1242 	error = kvm_read_all(kd, (unsigned long)vp, &vnode, sizeof(vnode));
1243 	if (error == 0) {
1244 		warnx("can't read vnode at %p", (void *)vp);
1245 		goto fail;
1246 	}
1247 	bzero(vn, sizeof(*vn));
1248 	vn->vn_type = vntype2psfsttype(vnode.v_type);
1249 	if (vnode.v_type == VNON || vnode.v_type == VBAD)
1250 		return (0);
1251 	error = kvm_read_all(kd, (unsigned long)vnode.v_tag, tagstr,
1252 	    sizeof(tagstr));
1253 	if (error == 0) {
1254 		warnx("can't read v_tag at %p", (void *)vp);
1255 		goto fail;
1256 	}
1257 	tagstr[sizeof(tagstr) - 1] = '\0';
1258 
1259 	/*
1260 	 * Find appropriate handler.
1261 	 */
1262 	for (i = 0, found = 0; i < NTYPES; i++)
1263 		if (!strcmp(fstypes[i].tag, tagstr)) {
1264 			if (fstypes[i].handler(kd, &vnode, vn) != 0) {
1265 				goto fail;
1266 			}
1267 			break;
1268 		}
1269 	if (i == NTYPES) {
1270 		if (errbuf != NULL)
1271 			snprintf(errbuf, _POSIX2_LINE_MAX, "?(%s)", tagstr);
1272 		return (1);
1273 	}
1274 	vn->vn_mntdir = getmnton(kd, vnode.v_mount);
1275 	if ((vnode.v_type == VBLK || vnode.v_type == VCHR) &&
1276 	    vnode.v_rdev != NULL){
1277 		vn->vn_dev = dev2udev(kd, vnode.v_rdev);
1278 		(void)kdevtoname(kd, vnode.v_rdev, vn->vn_devname);
1279 	} else {
1280 		vn->vn_dev = -1;
1281 	}
1282 	return (0);
1283 
1284 fail:
1285 	if (errbuf != NULL)
1286 		snprintf(errbuf, _POSIX2_LINE_MAX, "error");
1287 	return (1);
1288 }
1289 
1290 /*
1291  * kinfo vnode type to filestat translation.
1292  */
1293 static int
1294 kinfo_vtype2fst(int kfvtype)
1295 {
1296 	static struct {
1297 		int	kf_vtype;
1298 		int	fst_vtype;
1299 	} kfvtypes2fst[] = {
1300 		{ KF_VTYPE_VBAD, PS_FST_VTYPE_VBAD },
1301 		{ KF_VTYPE_VBLK, PS_FST_VTYPE_VBLK },
1302 		{ KF_VTYPE_VCHR, PS_FST_VTYPE_VCHR },
1303 		{ KF_VTYPE_VDIR, PS_FST_VTYPE_VDIR },
1304 		{ KF_VTYPE_VFIFO, PS_FST_VTYPE_VFIFO },
1305 		{ KF_VTYPE_VLNK, PS_FST_VTYPE_VLNK },
1306 		{ KF_VTYPE_VNON, PS_FST_VTYPE_VNON },
1307 		{ KF_VTYPE_VREG, PS_FST_VTYPE_VREG },
1308 		{ KF_VTYPE_VSOCK, PS_FST_VTYPE_VSOCK }
1309 	};
1310 #define	NKFVTYPES	(sizeof(kfvtypes2fst) / sizeof(*kfvtypes2fst))
1311 	unsigned int i;
1312 
1313 	for (i = 0; i < NKFVTYPES; i++)
1314 		if (kfvtypes2fst[i].kf_vtype == kfvtype)
1315 			break;
1316 	if (i == NKFVTYPES)
1317 		return (PS_FST_VTYPE_UNKNOWN);
1318 	return (kfvtypes2fst[i].fst_vtype);
1319 }
1320 
1321 static int
1322 procstat_get_vnode_info_sysctl(struct filestat *fst, struct vnstat *vn,
1323     char *errbuf)
1324 {
1325 	struct statfs stbuf;
1326 	struct kinfo_file *kif;
1327 	struct kinfo_vmentry *kve;
1328 	uint64_t fileid;
1329 	uint64_t size;
1330 	char *name, *path;
1331 	uint32_t fsid;
1332 	uint16_t mode;
1333 	uint32_t rdev;
1334 	int vntype;
1335 	int status;
1336 
1337 	assert(fst);
1338 	assert(vn);
1339 	bzero(vn, sizeof(*vn));
1340 	if (fst->fs_typedep == NULL)
1341 		return (1);
1342 	if (fst->fs_uflags & PS_FST_UFLAG_MMAP) {
1343 		kve = fst->fs_typedep;
1344 		fileid = kve->kve_vn_fileid;
1345 		fsid = kve->kve_vn_fsid;
1346 		mode = kve->kve_vn_mode;
1347 		path = kve->kve_path;
1348 		rdev = kve->kve_vn_rdev;
1349 		size = kve->kve_vn_size;
1350 		vntype = kinfo_vtype2fst(kve->kve_vn_type);
1351 		status = kve->kve_status;
1352 	} else {
1353 		kif = fst->fs_typedep;
1354 		fileid = kif->kf_un.kf_file.kf_file_fileid;
1355 		fsid = kif->kf_un.kf_file.kf_file_fsid;
1356 		mode = kif->kf_un.kf_file.kf_file_mode;
1357 		path = kif->kf_path;
1358 		rdev = kif->kf_un.kf_file.kf_file_rdev;
1359 		size = kif->kf_un.kf_file.kf_file_size;
1360 		vntype = kinfo_vtype2fst(kif->kf_vnode_type);
1361 		status = kif->kf_status;
1362 	}
1363 	vn->vn_type = vntype;
1364 	if (vntype == PS_FST_VTYPE_VNON || vntype == PS_FST_VTYPE_VBAD)
1365 		return (0);
1366 	if ((status & KF_ATTR_VALID) == 0) {
1367 		if (errbuf != NULL) {
1368 			snprintf(errbuf, _POSIX2_LINE_MAX,
1369 			    "? (no info available)");
1370 		}
1371 		return (1);
1372 	}
1373 	if (path && *path) {
1374 		statfs(path, &stbuf);
1375 		vn->vn_mntdir = strdup(stbuf.f_mntonname);
1376 	} else
1377 		vn->vn_mntdir = strdup("-");
1378 	vn->vn_dev = rdev;
1379 	if (vntype == PS_FST_VTYPE_VBLK) {
1380 		name = devname(rdev, S_IFBLK);
1381 		if (name != NULL)
1382 			strlcpy(vn->vn_devname, name,
1383 			    sizeof(vn->vn_devname));
1384 	} else if (vntype == PS_FST_VTYPE_VCHR) {
1385 		name = devname(vn->vn_dev, S_IFCHR);
1386 		if (name != NULL)
1387 			strlcpy(vn->vn_devname, name,
1388 			    sizeof(vn->vn_devname));
1389 	}
1390 	vn->vn_fsid = fsid;
1391 	vn->vn_fileid = fileid;
1392 	vn->vn_size = size;
1393 	vn->vn_mode = mode;
1394 	return (0);
1395 }
1396 
1397 int
1398 procstat_get_socket_info(struct procstat *procstat, struct filestat *fst,
1399     struct sockstat *sock, char *errbuf)
1400 {
1401 
1402 	assert(sock);
1403 	if (procstat->type == PROCSTAT_KVM) {
1404 		return (procstat_get_socket_info_kvm(procstat->kd, fst, sock,
1405 		    errbuf));
1406 	} else if (procstat->type == PROCSTAT_SYSCTL ||
1407 		procstat->type == PROCSTAT_CORE) {
1408 		return (procstat_get_socket_info_sysctl(fst, sock, errbuf));
1409 	} else {
1410 		warnx("unknown access method: %d", procstat->type);
1411 		if (errbuf != NULL)
1412 			snprintf(errbuf, _POSIX2_LINE_MAX, "error");
1413 		return (1);
1414 	}
1415 }
1416 
1417 static int
1418 procstat_get_socket_info_kvm(kvm_t *kd, struct filestat *fst,
1419     struct sockstat *sock, char *errbuf)
1420 {
1421 	struct domain dom;
1422 	struct inpcb inpcb;
1423 	struct protosw proto;
1424 	struct socket s;
1425 	struct unpcb unpcb;
1426 	ssize_t len;
1427 	void *so;
1428 
1429 	assert(kd);
1430 	assert(sock);
1431 	assert(fst);
1432 	bzero(sock, sizeof(*sock));
1433 	so = fst->fs_typedep;
1434 	if (so == NULL)
1435 		goto fail;
1436 	sock->so_addr = (uintptr_t)so;
1437 	/* fill in socket */
1438 	if (!kvm_read_all(kd, (unsigned long)so, &s,
1439 	    sizeof(struct socket))) {
1440 		warnx("can't read sock at %p", (void *)so);
1441 		goto fail;
1442 	}
1443 	/* fill in protosw entry */
1444 	if (!kvm_read_all(kd, (unsigned long)s.so_proto, &proto,
1445 	    sizeof(struct protosw))) {
1446 		warnx("can't read protosw at %p", (void *)s.so_proto);
1447 		goto fail;
1448 	}
1449 	/* fill in domain */
1450 	if (!kvm_read_all(kd, (unsigned long)proto.pr_domain, &dom,
1451 	    sizeof(struct domain))) {
1452 		warnx("can't read domain at %p",
1453 		    (void *)proto.pr_domain);
1454 		goto fail;
1455 	}
1456 	if ((len = kvm_read(kd, (unsigned long)dom.dom_name, sock->dname,
1457 	    sizeof(sock->dname) - 1)) < 0) {
1458 		warnx("can't read domain name at %p", (void *)dom.dom_name);
1459 		sock->dname[0] = '\0';
1460 	}
1461 	else
1462 		sock->dname[len] = '\0';
1463 
1464 	/*
1465 	 * Fill in known data.
1466 	 */
1467 	sock->type = s.so_type;
1468 	sock->proto = proto.pr_protocol;
1469 	sock->dom_family = dom.dom_family;
1470 	sock->so_pcb = (uintptr_t)s.so_pcb;
1471 
1472 	/*
1473 	 * Protocol specific data.
1474 	 */
1475 	switch(dom.dom_family) {
1476 	case AF_INET:
1477 	case AF_INET6:
1478 		if (proto.pr_protocol == IPPROTO_TCP) {
1479 			if (s.so_pcb) {
1480 				if (kvm_read(kd, (u_long)s.so_pcb,
1481 				    (char *)&inpcb, sizeof(struct inpcb))
1482 				    != sizeof(struct inpcb)) {
1483 					warnx("can't read inpcb at %p",
1484 					    (void *)s.so_pcb);
1485 				} else
1486 					sock->inp_ppcb =
1487 					    (uintptr_t)inpcb.inp_ppcb;
1488 			}
1489 		}
1490 		break;
1491 	case AF_UNIX:
1492 		if (s.so_pcb) {
1493 			if (kvm_read(kd, (u_long)s.so_pcb, (char *)&unpcb,
1494 			    sizeof(struct unpcb)) != sizeof(struct unpcb)){
1495 				warnx("can't read unpcb at %p",
1496 				    (void *)s.so_pcb);
1497 			} else if (unpcb.unp_conn) {
1498 				sock->so_rcv_sb_state = s.so_rcv.sb_state;
1499 				sock->so_snd_sb_state = s.so_snd.sb_state;
1500 				sock->unp_conn = (uintptr_t)unpcb.unp_conn;
1501 			}
1502 		}
1503 		break;
1504 	default:
1505 		break;
1506 	}
1507 	return (0);
1508 
1509 fail:
1510 	if (errbuf != NULL)
1511 		snprintf(errbuf, _POSIX2_LINE_MAX, "error");
1512 	return (1);
1513 }
1514 
1515 static int
1516 procstat_get_socket_info_sysctl(struct filestat *fst, struct sockstat *sock,
1517     char *errbuf __unused)
1518 {
1519 	struct kinfo_file *kif;
1520 
1521 	assert(sock);
1522 	assert(fst);
1523 	bzero(sock, sizeof(*sock));
1524 	kif = fst->fs_typedep;
1525 	if (kif == NULL)
1526 		return (0);
1527 
1528 	/*
1529 	 * Fill in known data.
1530 	 */
1531 	sock->type = kif->kf_sock_type;
1532 	sock->proto = kif->kf_sock_protocol;
1533 	sock->dom_family = kif->kf_sock_domain;
1534 	sock->so_pcb = kif->kf_un.kf_sock.kf_sock_pcb;
1535 	strlcpy(sock->dname, kif->kf_path, sizeof(sock->dname));
1536 	bcopy(&kif->kf_sa_local, &sock->sa_local, kif->kf_sa_local.ss_len);
1537 	bcopy(&kif->kf_sa_peer, &sock->sa_peer, kif->kf_sa_peer.ss_len);
1538 
1539 	/*
1540 	 * Protocol specific data.
1541 	 */
1542 	switch(sock->dom_family) {
1543 	case AF_INET:
1544 	case AF_INET6:
1545 		if (sock->proto == IPPROTO_TCP)
1546 			sock->inp_ppcb = kif->kf_un.kf_sock.kf_sock_inpcb;
1547 		break;
1548 	case AF_UNIX:
1549 		if (kif->kf_un.kf_sock.kf_sock_unpconn != 0) {
1550 				sock->so_rcv_sb_state =
1551 				    kif->kf_un.kf_sock.kf_sock_rcv_sb_state;
1552 				sock->so_snd_sb_state =
1553 				    kif->kf_un.kf_sock.kf_sock_snd_sb_state;
1554 				sock->unp_conn =
1555 				    kif->kf_un.kf_sock.kf_sock_unpconn;
1556 		}
1557 		break;
1558 	default:
1559 		break;
1560 	}
1561 	return (0);
1562 }
1563 
1564 /*
1565  * Descriptor flags to filestat translation.
1566  */
1567 static int
1568 to_filestat_flags(int flags)
1569 {
1570 	static struct {
1571 		int flag;
1572 		int fst_flag;
1573 	} fstflags[] = {
1574 		{ FREAD, PS_FST_FFLAG_READ },
1575 		{ FWRITE, PS_FST_FFLAG_WRITE },
1576 		{ O_APPEND, PS_FST_FFLAG_APPEND },
1577 		{ O_ASYNC, PS_FST_FFLAG_ASYNC },
1578 		{ O_CREAT, PS_FST_FFLAG_CREAT },
1579 		{ O_DIRECT, PS_FST_FFLAG_DIRECT },
1580 		{ O_EXCL, PS_FST_FFLAG_EXCL },
1581 		{ O_EXEC, PS_FST_FFLAG_EXEC },
1582 		{ O_EXLOCK, PS_FST_FFLAG_EXLOCK },
1583 		{ O_NOFOLLOW, PS_FST_FFLAG_NOFOLLOW },
1584 		{ O_NONBLOCK, PS_FST_FFLAG_NONBLOCK },
1585 		{ O_SHLOCK, PS_FST_FFLAG_SHLOCK },
1586 		{ O_SYNC, PS_FST_FFLAG_SYNC },
1587 		{ O_TRUNC, PS_FST_FFLAG_TRUNC }
1588 	};
1589 #define NFSTFLAGS	(sizeof(fstflags) / sizeof(*fstflags))
1590 	int fst_flags;
1591 	unsigned int i;
1592 
1593 	fst_flags = 0;
1594 	for (i = 0; i < NFSTFLAGS; i++)
1595 		if (flags & fstflags[i].flag)
1596 			fst_flags |= fstflags[i].fst_flag;
1597 	return (fst_flags);
1598 }
1599 
1600 /*
1601  * Vnode type to filestate translation.
1602  */
1603 static int
1604 vntype2psfsttype(int type)
1605 {
1606 	static struct {
1607 		int	vtype;
1608 		int	fst_vtype;
1609 	} vt2fst[] = {
1610 		{ VBAD, PS_FST_VTYPE_VBAD },
1611 		{ VBLK, PS_FST_VTYPE_VBLK },
1612 		{ VCHR, PS_FST_VTYPE_VCHR },
1613 		{ VDIR, PS_FST_VTYPE_VDIR },
1614 		{ VFIFO, PS_FST_VTYPE_VFIFO },
1615 		{ VLNK, PS_FST_VTYPE_VLNK },
1616 		{ VNON, PS_FST_VTYPE_VNON },
1617 		{ VREG, PS_FST_VTYPE_VREG },
1618 		{ VSOCK, PS_FST_VTYPE_VSOCK }
1619 	};
1620 #define	NVFTYPES	(sizeof(vt2fst) / sizeof(*vt2fst))
1621 	unsigned int i, fst_type;
1622 
1623 	fst_type = PS_FST_VTYPE_UNKNOWN;
1624 	for (i = 0; i < NVFTYPES; i++) {
1625 		if (type == vt2fst[i].vtype) {
1626 			fst_type = vt2fst[i].fst_vtype;
1627 			break;
1628 		}
1629 	}
1630 	return (fst_type);
1631 }
1632 
1633 static char *
1634 getmnton(kvm_t *kd, struct mount *m)
1635 {
1636 	struct mount mnt;
1637 	static struct mtab {
1638 		struct mtab *next;
1639 		struct mount *m;
1640 		char mntonname[MNAMELEN + 1];
1641 	} *mhead = NULL;
1642 	struct mtab *mt;
1643 
1644 	for (mt = mhead; mt != NULL; mt = mt->next)
1645 		if (m == mt->m)
1646 			return (mt->mntonname);
1647 	if (!kvm_read_all(kd, (unsigned long)m, &mnt, sizeof(struct mount))) {
1648 		warnx("can't read mount table at %p", (void *)m);
1649 		return (NULL);
1650 	}
1651 	if ((mt = malloc(sizeof (struct mtab))) == NULL)
1652 		err(1, NULL);
1653 	mt->m = m;
1654 	bcopy(&mnt.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN);
1655 	mt->mntonname[MNAMELEN] = '\0';
1656 	mt->next = mhead;
1657 	mhead = mt;
1658 	return (mt->mntonname);
1659 }
1660 
1661 /*
1662  * Auxiliary structures and functions to get process environment or
1663  * command line arguments.
1664  */
1665 struct argvec {
1666 	char	*buf;
1667 	size_t	bufsize;
1668 	char	**argv;
1669 	size_t	argc;
1670 };
1671 
1672 static struct argvec *
1673 argvec_alloc(size_t bufsize)
1674 {
1675 	struct argvec *av;
1676 
1677 	av = malloc(sizeof(*av));
1678 	if (av == NULL)
1679 		return (NULL);
1680 	av->bufsize = bufsize;
1681 	av->buf = malloc(av->bufsize);
1682 	if (av->buf == NULL) {
1683 		free(av);
1684 		return (NULL);
1685 	}
1686 	av->argc = 32;
1687 	av->argv = malloc(sizeof(char *) * av->argc);
1688 	if (av->argv == NULL) {
1689 		free(av->buf);
1690 		free(av);
1691 		return (NULL);
1692 	}
1693 	return av;
1694 }
1695 
1696 static void
1697 argvec_free(struct argvec * av)
1698 {
1699 
1700 	free(av->argv);
1701 	free(av->buf);
1702 	free(av);
1703 }
1704 
1705 static char **
1706 getargv(struct procstat *procstat, struct kinfo_proc *kp, size_t nchr, int env)
1707 {
1708 	int error, name[4], argc, i;
1709 	struct argvec *av, **avp;
1710 	enum psc_type type;
1711 	size_t len;
1712 	char *p, **argv;
1713 
1714 	assert(procstat);
1715 	assert(kp);
1716 	if (procstat->type == PROCSTAT_KVM) {
1717 		warnx("can't use kvm access method");
1718 		return (NULL);
1719 	}
1720 	if (procstat->type != PROCSTAT_SYSCTL &&
1721 	    procstat->type != PROCSTAT_CORE) {
1722 		warnx("unknown access method: %d", procstat->type);
1723 		return (NULL);
1724 	}
1725 
1726 	if (nchr == 0 || nchr > ARG_MAX)
1727 		nchr = ARG_MAX;
1728 
1729 	avp = (struct argvec **)(env ? &procstat->argv : &procstat->envv);
1730 	av = *avp;
1731 
1732 	if (av == NULL)
1733 	{
1734 		av = argvec_alloc(nchr);
1735 		if (av == NULL)
1736 		{
1737 			warn("malloc(%zu)", nchr);
1738 			return (NULL);
1739 		}
1740 		*avp = av;
1741 	} else if (av->bufsize < nchr) {
1742 		av->buf = reallocf(av->buf, nchr);
1743 		if (av->buf == NULL) {
1744 			warn("malloc(%zu)", nchr);
1745 			return (NULL);
1746 		}
1747 	}
1748 	if (procstat->type == PROCSTAT_SYSCTL) {
1749 		name[0] = CTL_KERN;
1750 		name[1] = KERN_PROC;
1751 		name[2] = env ? KERN_PROC_ENV : KERN_PROC_ARGS;
1752 		name[3] = kp->ki_pid;
1753 		len = nchr;
1754 		error = sysctl(name, 4, av->buf, &len, NULL, 0);
1755 		if (error != 0 && errno != ESRCH && errno != EPERM)
1756 			warn("sysctl(kern.proc.%s)", env ? "env" : "args");
1757 		if (error != 0 || len == 0)
1758 			return (NULL);
1759 	} else /* procstat->type == PROCSTAT_CORE */ {
1760 		type = env ? PSC_TYPE_ENVV : PSC_TYPE_ARGV;
1761 		len = nchr;
1762 		if (procstat_core_get(procstat->core, type, av->buf, &len)
1763 		    == NULL) {
1764 			return (NULL);
1765 		}
1766 	}
1767 
1768 	argv = av->argv;
1769 	argc = av->argc;
1770 	i = 0;
1771 	for (p = av->buf; p < av->buf + len; p += strlen(p) + 1) {
1772 		argv[i++] = p;
1773 		if (i < argc)
1774 			continue;
1775 		/* Grow argv. */
1776 		argc += argc;
1777 		argv = realloc(argv, sizeof(char *) * argc);
1778 		if (argv == NULL) {
1779 			warn("malloc(%zu)", sizeof(char *) * argc);
1780 			return (NULL);
1781 		}
1782 		av->argv = argv;
1783 		av->argc = argc;
1784 	}
1785 	argv[i] = NULL;
1786 
1787 	return (argv);
1788 }
1789 
1790 /*
1791  * Return process command line arguments.
1792  */
1793 char **
1794 procstat_getargv(struct procstat *procstat, struct kinfo_proc *p, size_t nchr)
1795 {
1796 
1797 	return (getargv(procstat, p, nchr, 0));
1798 }
1799 
1800 /*
1801  * Free the buffer allocated by procstat_getargv().
1802  */
1803 void
1804 procstat_freeargv(struct procstat *procstat)
1805 {
1806 
1807 	if (procstat->argv != NULL) {
1808 		argvec_free(procstat->argv);
1809 		procstat->argv = NULL;
1810 	}
1811 }
1812 
1813 /*
1814  * Return process environment.
1815  */
1816 char **
1817 procstat_getenvv(struct procstat *procstat, struct kinfo_proc *p, size_t nchr)
1818 {
1819 
1820 	return (getargv(procstat, p, nchr, 1));
1821 }
1822 
1823 /*
1824  * Free the buffer allocated by procstat_getenvv().
1825  */
1826 void
1827 procstat_freeenvv(struct procstat *procstat)
1828 {
1829 	if (procstat->envv != NULL) {
1830 		argvec_free(procstat->envv);
1831 		procstat->envv = NULL;
1832 	}
1833 }
1834 
1835 static struct kinfo_vmentry *
1836 kinfo_getvmmap_core(struct procstat_core *core, int *cntp)
1837 {
1838 	int cnt;
1839 	size_t len;
1840 	char *buf, *bp, *eb;
1841 	struct kinfo_vmentry *kiv, *kp, *kv;
1842 
1843 	buf = procstat_core_get(core, PSC_TYPE_VMMAP, NULL, &len);
1844 	if (buf == NULL)
1845 		return (NULL);
1846 
1847 	/*
1848 	 * XXXMG: The code below is just copy&past from libutil.
1849 	 * The code duplication can be avoided if libutil
1850 	 * is extended to provide something like:
1851 	 *   struct kinfo_vmentry *kinfo_getvmmap_from_buf(const char *buf,
1852 	 *       size_t len, int *cntp);
1853 	 */
1854 
1855 	/* Pass 1: count items */
1856 	cnt = 0;
1857 	bp = buf;
1858 	eb = buf + len;
1859 	while (bp < eb) {
1860 		kv = (struct kinfo_vmentry *)(uintptr_t)bp;
1861 		bp += kv->kve_structsize;
1862 		cnt++;
1863 	}
1864 
1865 	kiv = calloc(cnt, sizeof(*kiv));
1866 	if (kiv == NULL) {
1867 		free(buf);
1868 		return (NULL);
1869 	}
1870 	bp = buf;
1871 	eb = buf + len;
1872 	kp = kiv;
1873 	/* Pass 2: unpack */
1874 	while (bp < eb) {
1875 		kv = (struct kinfo_vmentry *)(uintptr_t)bp;
1876 		/* Copy/expand into pre-zeroed buffer */
1877 		memcpy(kp, kv, kv->kve_structsize);
1878 		/* Advance to next packed record */
1879 		bp += kv->kve_structsize;
1880 		/* Set field size to fixed length, advance */
1881 		kp->kve_structsize = sizeof(*kp);
1882 		kp++;
1883 	}
1884 	free(buf);
1885 	*cntp = cnt;
1886 	return (kiv);	/* Caller must free() return value */
1887 }
1888 
1889 struct kinfo_vmentry *
1890 procstat_getvmmap(struct procstat *procstat, struct kinfo_proc *kp,
1891     unsigned int *cntp)
1892 {
1893 
1894 	switch(procstat->type) {
1895 	case PROCSTAT_KVM:
1896 		warnx("kvm method is not supported");
1897 		return (NULL);
1898 	case PROCSTAT_SYSCTL:
1899 		return (kinfo_getvmmap(kp->ki_pid, cntp));
1900 	case PROCSTAT_CORE:
1901 		return (kinfo_getvmmap_core(procstat->core, cntp));
1902 	default:
1903 		warnx("unknown access method: %d", procstat->type);
1904 		return (NULL);
1905 	}
1906 }
1907 
1908 void
1909 procstat_freevmmap(struct procstat *procstat __unused,
1910     struct kinfo_vmentry *vmmap)
1911 {
1912 
1913 	free(vmmap);
1914 }
1915 
1916 static gid_t *
1917 procstat_getgroups_kvm(kvm_t *kd, struct kinfo_proc *kp, unsigned int *cntp)
1918 {
1919 	struct proc proc;
1920 	struct ucred ucred;
1921 	gid_t *groups;
1922 	size_t len;
1923 
1924 	assert(kd != NULL);
1925 	assert(kp != NULL);
1926 	if (!kvm_read_all(kd, (unsigned long)kp->ki_paddr, &proc,
1927 	    sizeof(proc))) {
1928 		warnx("can't read proc struct at %p for pid %d",
1929 		    kp->ki_paddr, kp->ki_pid);
1930 		return (NULL);
1931 	}
1932 	if (proc.p_ucred == NOCRED)
1933 		return (NULL);
1934 	if (!kvm_read_all(kd, (unsigned long)proc.p_ucred, &ucred,
1935 	    sizeof(ucred))) {
1936 		warnx("can't read ucred struct at %p for pid %d",
1937 		    proc.p_ucred, kp->ki_pid);
1938 		return (NULL);
1939 	}
1940 	len = ucred.cr_ngroups * sizeof(gid_t);
1941 	groups = malloc(len);
1942 	if (groups == NULL) {
1943 		warn("malloc(%zu)", len);
1944 		return (NULL);
1945 	}
1946 	if (!kvm_read_all(kd, (unsigned long)ucred.cr_groups, groups, len)) {
1947 		warnx("can't read groups at %p for pid %d",
1948 		    ucred.cr_groups, kp->ki_pid);
1949 		free(groups);
1950 		return (NULL);
1951 	}
1952 	*cntp = ucred.cr_ngroups;
1953 	return (groups);
1954 }
1955 
1956 static gid_t *
1957 procstat_getgroups_sysctl(pid_t pid, unsigned int *cntp)
1958 {
1959 	int mib[4];
1960 	size_t len;
1961 	gid_t *groups;
1962 
1963 	mib[0] = CTL_KERN;
1964 	mib[1] = KERN_PROC;
1965 	mib[2] = KERN_PROC_GROUPS;
1966 	mib[3] = pid;
1967 	len = (sysconf(_SC_NGROUPS_MAX) + 1) * sizeof(gid_t);
1968 	groups = malloc(len);
1969 	if (groups == NULL) {
1970 		warn("malloc(%zu)", len);
1971 		return (NULL);
1972 	}
1973 	if (sysctl(mib, 4, groups, &len, NULL, 0) == -1) {
1974 		warn("sysctl: kern.proc.groups: %d", pid);
1975 		free(groups);
1976 		return (NULL);
1977 	}
1978 	*cntp = len / sizeof(gid_t);
1979 	return (groups);
1980 }
1981 
1982 static gid_t *
1983 procstat_getgroups_core(struct procstat_core *core, unsigned int *cntp)
1984 {
1985 	size_t len;
1986 	gid_t *groups;
1987 
1988 	groups = procstat_core_get(core, PSC_TYPE_GROUPS, NULL, &len);
1989 	if (groups == NULL)
1990 		return (NULL);
1991 	*cntp = len / sizeof(gid_t);
1992 	return (groups);
1993 }
1994 
1995 gid_t *
1996 procstat_getgroups(struct procstat *procstat, struct kinfo_proc *kp,
1997     unsigned int *cntp)
1998 {
1999 	switch(procstat->type) {
2000 	case PROCSTAT_KVM:
2001 		return (procstat_getgroups_kvm(procstat->kd, kp, cntp));
2002 	case PROCSTAT_SYSCTL:
2003 		return (procstat_getgroups_sysctl(kp->ki_pid, cntp));
2004 	case PROCSTAT_CORE:
2005 		return (procstat_getgroups_core(procstat->core, cntp));
2006 	default:
2007 		warnx("unknown access method: %d", procstat->type);
2008 		return (NULL);
2009 	}
2010 }
2011 
2012 void
2013 procstat_freegroups(struct procstat *procstat __unused, gid_t *groups)
2014 {
2015 
2016 	free(groups);
2017 }
2018 
2019 static int
2020 procstat_getumask_kvm(kvm_t *kd, struct kinfo_proc *kp, unsigned short *maskp)
2021 {
2022 	struct filedesc fd;
2023 
2024 	assert(kd != NULL);
2025 	assert(kp != NULL);
2026 	if (kp->ki_fd == NULL)
2027 		return (-1);
2028 	if (!kvm_read_all(kd, (unsigned long)kp->ki_fd, &fd, sizeof(fd))) {
2029 		warnx("can't read filedesc at %p for pid %d", kp->ki_fd,
2030 		    kp->ki_pid);
2031 		return (-1);
2032 	}
2033 	*maskp = fd.fd_cmask;
2034 	return (0);
2035 }
2036 
2037 static int
2038 procstat_getumask_sysctl(pid_t pid, unsigned short *maskp)
2039 {
2040 	int error;
2041 	int mib[4];
2042 	size_t len;
2043 
2044 	mib[0] = CTL_KERN;
2045 	mib[1] = KERN_PROC;
2046 	mib[2] = KERN_PROC_UMASK;
2047 	mib[3] = pid;
2048 	len = sizeof(*maskp);
2049 	error = sysctl(mib, 4, maskp, &len, NULL, 0);
2050 	if (error != 0 && errno != ESRCH)
2051 		warn("sysctl: kern.proc.umask: %d", pid);
2052 	return (error);
2053 }
2054 
2055 static int
2056 procstat_getumask_core(struct procstat_core *core, unsigned short *maskp)
2057 {
2058 	size_t len;
2059 	unsigned short *buf;
2060 
2061 	buf = procstat_core_get(core, PSC_TYPE_UMASK, NULL, &len);
2062 	if (buf == NULL)
2063 		return (-1);
2064 	if (len < sizeof(*maskp)) {
2065 		free(buf);
2066 		return (-1);
2067 	}
2068 	*maskp = *buf;
2069 	free(buf);
2070 	return (0);
2071 }
2072 
2073 int
2074 procstat_getumask(struct procstat *procstat, struct kinfo_proc *kp,
2075     unsigned short *maskp)
2076 {
2077 	switch(procstat->type) {
2078 	case PROCSTAT_KVM:
2079 		return (procstat_getumask_kvm(procstat->kd, kp, maskp));
2080 	case PROCSTAT_SYSCTL:
2081 		return (procstat_getumask_sysctl(kp->ki_pid, maskp));
2082 	case PROCSTAT_CORE:
2083 		return (procstat_getumask_core(procstat->core, maskp));
2084 	default:
2085 		warnx("unknown access method: %d", procstat->type);
2086 		return (-1);
2087 	}
2088 }
2089 
2090 static int
2091 procstat_getrlimit_kvm(kvm_t *kd, struct kinfo_proc *kp, int which,
2092     struct rlimit* rlimit)
2093 {
2094 	struct proc proc;
2095 	unsigned long offset;
2096 
2097 	assert(kd != NULL);
2098 	assert(kp != NULL);
2099 	assert(which >= 0 && which < RLIM_NLIMITS);
2100 	if (!kvm_read_all(kd, (unsigned long)kp->ki_paddr, &proc,
2101 	    sizeof(proc))) {
2102 		warnx("can't read proc struct at %p for pid %d",
2103 		    kp->ki_paddr, kp->ki_pid);
2104 		return (-1);
2105 	}
2106 	if (proc.p_limit == NULL)
2107 		return (-1);
2108 	offset = (unsigned long)proc.p_limit + sizeof(struct rlimit) * which;
2109 	if (!kvm_read_all(kd, offset, rlimit, sizeof(*rlimit))) {
2110 		warnx("can't read rlimit struct at %p for pid %d",
2111 		    (void *)offset, kp->ki_pid);
2112 		return (-1);
2113 	}
2114 	return (0);
2115 }
2116 
2117 static int
2118 procstat_getrlimit_sysctl(pid_t pid, int which, struct rlimit* rlimit)
2119 {
2120 	int error, name[5];
2121 	size_t len;
2122 
2123 	name[0] = CTL_KERN;
2124 	name[1] = KERN_PROC;
2125 	name[2] = KERN_PROC_RLIMIT;
2126 	name[3] = pid;
2127 	name[4] = which;
2128 	len = sizeof(struct rlimit);
2129 	error = sysctl(name, 5, rlimit, &len, NULL, 0);
2130 	if (error < 0 && errno != ESRCH) {
2131 		warn("sysctl: kern.proc.rlimit: %d", pid);
2132 		return (-1);
2133 	}
2134 	if (error < 0 || len != sizeof(struct rlimit))
2135 		return (-1);
2136 	return (0);
2137 }
2138 
2139 static int
2140 procstat_getrlimit_core(struct procstat_core *core, int which,
2141     struct rlimit* rlimit)
2142 {
2143 	size_t len;
2144 	struct rlimit* rlimits;
2145 
2146 	if (which < 0 || which >= RLIM_NLIMITS) {
2147 		errno = EINVAL;
2148 		warn("getrlimit: which");
2149 		return (-1);
2150 	}
2151 	rlimits = procstat_core_get(core, PSC_TYPE_RLIMIT, NULL, &len);
2152 	if (rlimits == NULL)
2153 		return (-1);
2154 	if (len < sizeof(struct rlimit) * RLIM_NLIMITS) {
2155 		free(rlimits);
2156 		return (-1);
2157 	}
2158 	*rlimit = rlimits[which];
2159 	return (0);
2160 }
2161 
2162 int
2163 procstat_getrlimit(struct procstat *procstat, struct kinfo_proc *kp, int which,
2164     struct rlimit* rlimit)
2165 {
2166 	switch(procstat->type) {
2167 	case PROCSTAT_KVM:
2168 		return (procstat_getrlimit_kvm(procstat->kd, kp, which,
2169 		    rlimit));
2170 	case PROCSTAT_SYSCTL:
2171 		return (procstat_getrlimit_sysctl(kp->ki_pid, which, rlimit));
2172 	case PROCSTAT_CORE:
2173 		return (procstat_getrlimit_core(procstat->core, which, rlimit));
2174 	default:
2175 		warnx("unknown access method: %d", procstat->type);
2176 		return (-1);
2177 	}
2178 }
2179 
2180 static int
2181 procstat_getpathname_sysctl(pid_t pid, char *pathname, size_t maxlen)
2182 {
2183 	int error, name[4];
2184 	size_t len;
2185 
2186 	name[0] = CTL_KERN;
2187 	name[1] = KERN_PROC;
2188 	name[2] = KERN_PROC_PATHNAME;
2189 	name[3] = pid;
2190 	len = maxlen;
2191 	error = sysctl(name, 4, pathname, &len, NULL, 0);
2192 	if (error != 0 && errno != ESRCH)
2193 		warn("sysctl: kern.proc.pathname: %d", pid);
2194 	if (len == 0)
2195 		pathname[0] = '\0';
2196 	return (error);
2197 }
2198 
2199 static int
2200 procstat_getpathname_core(struct procstat_core *core, char *pathname,
2201     size_t maxlen)
2202 {
2203 	struct kinfo_file *files;
2204 	int cnt, i, result;
2205 
2206 	files = kinfo_getfile_core(core, &cnt);
2207 	if (files == NULL)
2208 		return (-1);
2209 	result = -1;
2210 	for (i = 0; i < cnt; i++) {
2211 		if (files[i].kf_fd != KF_FD_TYPE_TEXT)
2212 			continue;
2213 		strncpy(pathname, files[i].kf_path, maxlen);
2214 		result = 0;
2215 		break;
2216 	}
2217 	free(files);
2218 	return (result);
2219 }
2220 
2221 int
2222 procstat_getpathname(struct procstat *procstat, struct kinfo_proc *kp,
2223     char *pathname, size_t maxlen)
2224 {
2225 	switch(procstat->type) {
2226 	case PROCSTAT_KVM:
2227 		/* XXX: Return empty string. */
2228 		if (maxlen > 0)
2229 			pathname[0] = '\0';
2230 		return (0);
2231 	case PROCSTAT_SYSCTL:
2232 		return (procstat_getpathname_sysctl(kp->ki_pid, pathname,
2233 		    maxlen));
2234 	case PROCSTAT_CORE:
2235 		return (procstat_getpathname_core(procstat->core, pathname,
2236 		    maxlen));
2237 	default:
2238 		warnx("unknown access method: %d", procstat->type);
2239 		return (-1);
2240 	}
2241 }
2242 
2243 static int
2244 procstat_getosrel_kvm(kvm_t *kd, struct kinfo_proc *kp, int *osrelp)
2245 {
2246 	struct proc proc;
2247 
2248 	assert(kd != NULL);
2249 	assert(kp != NULL);
2250 	if (!kvm_read_all(kd, (unsigned long)kp->ki_paddr, &proc,
2251 	    sizeof(proc))) {
2252 		warnx("can't read proc struct at %p for pid %d",
2253 		    kp->ki_paddr, kp->ki_pid);
2254 		return (-1);
2255 	}
2256 	*osrelp = proc.p_osrel;
2257 	return (0);
2258 }
2259 
2260 static int
2261 procstat_getosrel_sysctl(pid_t pid, int *osrelp)
2262 {
2263 	int error, name[4];
2264 	size_t len;
2265 
2266 	name[0] = CTL_KERN;
2267 	name[1] = KERN_PROC;
2268 	name[2] = KERN_PROC_OSREL;
2269 	name[3] = pid;
2270 	len = sizeof(*osrelp);
2271 	error = sysctl(name, 4, osrelp, &len, NULL, 0);
2272 	if (error != 0 && errno != ESRCH)
2273 		warn("sysctl: kern.proc.osrel: %d", pid);
2274 	return (error);
2275 }
2276 
2277 static int
2278 procstat_getosrel_core(struct procstat_core *core, int *osrelp)
2279 {
2280 	size_t len;
2281 	int *buf;
2282 
2283 	buf = procstat_core_get(core, PSC_TYPE_OSREL, NULL, &len);
2284 	if (buf == NULL)
2285 		return (-1);
2286 	if (len < sizeof(*osrelp)) {
2287 		free(buf);
2288 		return (-1);
2289 	}
2290 	*osrelp = *buf;
2291 	free(buf);
2292 	return (0);
2293 }
2294 
2295 int
2296 procstat_getosrel(struct procstat *procstat, struct kinfo_proc *kp, int *osrelp)
2297 {
2298 	switch(procstat->type) {
2299 	case PROCSTAT_KVM:
2300 		return (procstat_getosrel_kvm(procstat->kd, kp, osrelp));
2301 	case PROCSTAT_SYSCTL:
2302 		return (procstat_getosrel_sysctl(kp->ki_pid, osrelp));
2303 	case PROCSTAT_CORE:
2304 		return (procstat_getosrel_core(procstat->core, osrelp));
2305 	default:
2306 		warnx("unknown access method: %d", procstat->type);
2307 		return (-1);
2308 	}
2309 }
2310 
2311 #define PROC_AUXV_MAX	256
2312 
2313 #if __ELF_WORD_SIZE == 64
2314 static const char *elf32_sv_names[] = {
2315 	"Linux ELF32",
2316 	"FreeBSD ELF32",
2317 };
2318 
2319 static int
2320 is_elf32_sysctl(pid_t pid)
2321 {
2322 	int error, name[4];
2323 	size_t len, i;
2324 	static char sv_name[256];
2325 
2326 	name[0] = CTL_KERN;
2327 	name[1] = KERN_PROC;
2328 	name[2] = KERN_PROC_SV_NAME;
2329 	name[3] = pid;
2330 	len = sizeof(sv_name);
2331 	error = sysctl(name, 4, sv_name, &len, NULL, 0);
2332 	if (error != 0 || len == 0)
2333 		return (0);
2334 	for (i = 0; i < sizeof(elf32_sv_names) / sizeof(*elf32_sv_names); i++) {
2335 		if (strncmp(sv_name, elf32_sv_names[i], sizeof(sv_name)) == 0)
2336 			return (1);
2337 	}
2338 	return (0);
2339 }
2340 
2341 static Elf_Auxinfo *
2342 procstat_getauxv32_sysctl(pid_t pid, unsigned int *cntp)
2343 {
2344 	Elf_Auxinfo *auxv;
2345 	Elf32_Auxinfo *auxv32;
2346 	void *ptr;
2347 	size_t len;
2348 	unsigned int i, count;
2349 	int name[4];
2350 
2351 	name[0] = CTL_KERN;
2352 	name[1] = KERN_PROC;
2353 	name[2] = KERN_PROC_AUXV;
2354 	name[3] = pid;
2355 	len = PROC_AUXV_MAX * sizeof(Elf32_Auxinfo);
2356 	auxv = NULL;
2357 	auxv32 = malloc(len);
2358 	if (auxv32 == NULL) {
2359 		warn("malloc(%zu)", len);
2360 		goto out;
2361 	}
2362 	if (sysctl(name, 4, auxv32, &len, NULL, 0) == -1) {
2363 		if (errno != ESRCH && errno != EPERM)
2364 			warn("sysctl: kern.proc.auxv: %d: %d", pid, errno);
2365 		goto out;
2366 	}
2367 	count = len / sizeof(Elf_Auxinfo);
2368 	auxv = malloc(count  * sizeof(Elf_Auxinfo));
2369 	if (auxv == NULL) {
2370 		warn("malloc(%zu)", count * sizeof(Elf_Auxinfo));
2371 		goto out;
2372 	}
2373 	for (i = 0; i < count; i++) {
2374 		/*
2375 		 * XXX: We expect that values for a_type on a 32-bit platform
2376 		 * are directly mapped to values on 64-bit one, which is not
2377 		 * necessarily true.
2378 		 */
2379 		auxv[i].a_type = auxv32[i].a_type;
2380 		ptr = &auxv32[i].a_un;
2381 		auxv[i].a_un.a_val = *((uint32_t *)ptr);
2382 	}
2383 	*cntp = count;
2384 out:
2385 	free(auxv32);
2386 	return (auxv);
2387 }
2388 #endif /* __ELF_WORD_SIZE == 64 */
2389 
2390 static Elf_Auxinfo *
2391 procstat_getauxv_sysctl(pid_t pid, unsigned int *cntp)
2392 {
2393 	Elf_Auxinfo *auxv;
2394 	int name[4];
2395 	size_t len;
2396 
2397 #if __ELF_WORD_SIZE == 64
2398 	if (is_elf32_sysctl(pid))
2399 		return (procstat_getauxv32_sysctl(pid, cntp));
2400 #endif
2401 	name[0] = CTL_KERN;
2402 	name[1] = KERN_PROC;
2403 	name[2] = KERN_PROC_AUXV;
2404 	name[3] = pid;
2405 	len = PROC_AUXV_MAX * sizeof(Elf_Auxinfo);
2406 	auxv = malloc(len);
2407 	if (auxv == NULL) {
2408 		warn("malloc(%zu)", len);
2409 		return (NULL);
2410 	}
2411 	if (sysctl(name, 4, auxv, &len, NULL, 0) == -1) {
2412 		if (errno != ESRCH && errno != EPERM)
2413 			warn("sysctl: kern.proc.auxv: %d: %d", pid, errno);
2414 		free(auxv);
2415 		return (NULL);
2416 	}
2417 	*cntp = len / sizeof(Elf_Auxinfo);
2418 	return (auxv);
2419 }
2420 
2421 static Elf_Auxinfo *
2422 procstat_getauxv_core(struct procstat_core *core, unsigned int *cntp)
2423 {
2424 	Elf_Auxinfo *auxv;
2425 	size_t len;
2426 
2427 	auxv = procstat_core_get(core, PSC_TYPE_AUXV, NULL, &len);
2428 	if (auxv == NULL)
2429 		return (NULL);
2430 	*cntp = len / sizeof(Elf_Auxinfo);
2431 	return (auxv);
2432 }
2433 
2434 Elf_Auxinfo *
2435 procstat_getauxv(struct procstat *procstat, struct kinfo_proc *kp,
2436     unsigned int *cntp)
2437 {
2438 	switch(procstat->type) {
2439 	case PROCSTAT_KVM:
2440 		warnx("kvm method is not supported");
2441 		return (NULL);
2442 	case PROCSTAT_SYSCTL:
2443 		return (procstat_getauxv_sysctl(kp->ki_pid, cntp));
2444 	case PROCSTAT_CORE:
2445 		return (procstat_getauxv_core(procstat->core, cntp));
2446 	default:
2447 		warnx("unknown access method: %d", procstat->type);
2448 		return (NULL);
2449 	}
2450 }
2451 
2452 void
2453 procstat_freeauxv(struct procstat *procstat __unused, Elf_Auxinfo *auxv)
2454 {
2455 
2456 	free(auxv);
2457 }
2458 
2459 static struct kinfo_kstack *
2460 procstat_getkstack_sysctl(pid_t pid, int *cntp)
2461 {
2462 	struct kinfo_kstack *kkstp;
2463 	int error, name[4];
2464 	size_t len;
2465 
2466 	name[0] = CTL_KERN;
2467 	name[1] = KERN_PROC;
2468 	name[2] = KERN_PROC_KSTACK;
2469 	name[3] = pid;
2470 
2471 	len = 0;
2472 	error = sysctl(name, 4, NULL, &len, NULL, 0);
2473 	if (error < 0 && errno != ESRCH && errno != EPERM && errno != ENOENT) {
2474 		warn("sysctl: kern.proc.kstack: %d", pid);
2475 		return (NULL);
2476 	}
2477 	if (error == -1 && errno == ENOENT) {
2478 		warnx("sysctl: kern.proc.kstack unavailable"
2479 		    " (options DDB or options STACK required in kernel)");
2480 		return (NULL);
2481 	}
2482 	if (error == -1)
2483 		return (NULL);
2484 	kkstp = malloc(len);
2485 	if (kkstp == NULL) {
2486 		warn("malloc(%zu)", len);
2487 		return (NULL);
2488 	}
2489 	if (sysctl(name, 4, kkstp, &len, NULL, 0) == -1) {
2490 		warn("sysctl: kern.proc.pid: %d", pid);
2491 		free(kkstp);
2492 		return (NULL);
2493 	}
2494 	*cntp = len / sizeof(*kkstp);
2495 
2496 	return (kkstp);
2497 }
2498 
2499 struct kinfo_kstack *
2500 procstat_getkstack(struct procstat *procstat, struct kinfo_proc *kp,
2501     unsigned int *cntp)
2502 {
2503 	switch(procstat->type) {
2504 	case PROCSTAT_KVM:
2505 		warnx("kvm method is not supported");
2506 		return (NULL);
2507 	case PROCSTAT_SYSCTL:
2508 		return (procstat_getkstack_sysctl(kp->ki_pid, cntp));
2509 	case PROCSTAT_CORE:
2510 		warnx("core method is not supported");
2511 		return (NULL);
2512 	default:
2513 		warnx("unknown access method: %d", procstat->type);
2514 		return (NULL);
2515 	}
2516 }
2517 
2518 void
2519 procstat_freekstack(struct procstat *procstat __unused,
2520     struct kinfo_kstack *kkstp)
2521 {
2522 
2523 	free(kkstp);
2524 }
2525