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