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