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