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