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.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
43 #include <sys/vnode.h>
44 #include <sys/socket.h>
45 #include <sys/socketvar.h>
46 #include <sys/domain.h>
47 #include <sys/protosw.h>
49 #include <sys/unpcb.h>
50 #include <sys/sysctl.h>
52 #include <sys/filedesc.h>
53 #include <sys/queue.h>
58 #include <sys/mount.h>
60 #include <ufs/ufs/quota.h>
61 #include <ufs/ufs/inode.h>
62 #include <fs/devfs/devfs.h>
63 #include <fs/devfs/devfs_int.h>
65 #include <nfs/nfsproto.h>
66 #include <nfsclient/nfs.h>
67 #include <nfsclient/nfsnode.h>
70 #include <vm/vm_map.h>
71 #include <vm/vm_object.h>
73 #include <net/route.h>
74 #include <netinet/in.h>
75 #include <netinet/in_systm.h>
76 #include <netinet/ip.h>
77 #include <netinet/in_pcb.h>
95 #include <libprocstat.h>
96 #include "libprocstat_internal.h"
97 #include "common_kvm.h"
99 int statfs(const char *, struct statfs *); /* XXX */
101 #define PROCSTAT_KVM 1
102 #define PROCSTAT_SYSCTL 2
104 static char *getmnton(kvm_t *kd, struct mount *m);
105 static struct filestat_list *procstat_getfiles_kvm(
106 struct procstat *procstat, struct kinfo_proc *kp, int mmapped);
107 static struct filestat_list *procstat_getfiles_sysctl(
108 struct procstat *procstat, struct kinfo_proc *kp, int mmapped);
109 static int procstat_get_pipe_info_sysctl(struct filestat *fst,
110 struct pipestat *pipe, char *errbuf);
111 static int procstat_get_pipe_info_kvm(kvm_t *kd, struct filestat *fst,
112 struct pipestat *pipe, char *errbuf);
113 static int procstat_get_pts_info_sysctl(struct filestat *fst,
114 struct ptsstat *pts, char *errbuf);
115 static int procstat_get_pts_info_kvm(kvm_t *kd, struct filestat *fst,
116 struct ptsstat *pts, char *errbuf);
117 static int procstat_get_socket_info_sysctl(struct filestat *fst,
118 struct sockstat *sock, char *errbuf);
119 static int procstat_get_socket_info_kvm(kvm_t *kd, struct filestat *fst,
120 struct sockstat *sock, char *errbuf);
121 static int to_filestat_flags(int flags);
122 static int procstat_get_vnode_info_kvm(kvm_t *kd, struct filestat *fst,
123 struct vnstat *vn, char *errbuf);
124 static int procstat_get_vnode_info_sysctl(struct filestat *fst,
125 struct vnstat *vn, char *errbuf);
126 static int vntype2psfsttype(int type);
129 procstat_close(struct procstat *procstat)
133 if (procstat->type == PROCSTAT_KVM)
134 kvm_close(procstat->kd);
139 procstat_open_sysctl(void)
141 struct procstat *procstat;
143 procstat = calloc(1, sizeof(*procstat));
144 if (procstat == NULL) {
148 procstat->type = PROCSTAT_SYSCTL;
153 procstat_open_kvm(const char *nlistf, const char *memf)
155 struct procstat *procstat;
157 char buf[_POSIX2_LINE_MAX];
159 procstat = calloc(1, sizeof(*procstat));
160 if (procstat == NULL) {
164 kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf);
166 warnx("kvm_openfiles(): %s", buf);
170 procstat->type = PROCSTAT_KVM;
176 procstat_getprocs(struct procstat *procstat, int what, int arg,
179 struct kinfo_proc *p0, *p;
187 if (procstat->type == PROCSTAT_KVM) {
188 p0 = kvm_getprocs(procstat->kd, what, arg, count);
189 if (p0 == NULL || count == 0)
191 len = *count * sizeof(*p);
194 warnx("malloc(%zu)", len);
199 } else if (procstat->type == PROCSTAT_SYSCTL) {
205 error = sysctl(name, 4, NULL, &len, NULL, 0);
206 if (error < 0 && errno != EPERM) {
207 warn("sysctl(kern.proc)");
211 warnx("no processes?");
216 warnx("malloc(%zu)", len);
219 error = sysctl(name, 4, p, &len, NULL, 0);
220 if (error < 0 && errno != EPERM) {
221 warn("sysctl(kern.proc)");
224 /* Perform simple consistency checks. */
225 if ((len % sizeof(*p)) != 0 || p->ki_structsize != sizeof(*p)) {
226 warnx("kinfo_proc structure size mismatch");
229 *count = len / sizeof(*p);
232 warnx("unknown access method: %d", procstat->type);
242 procstat_freeprocs(struct procstat *procstat __unused, struct kinfo_proc *p)
250 struct filestat_list *
251 procstat_getfiles(struct procstat *procstat, struct kinfo_proc *kp, int mmapped)
254 if (procstat->type == PROCSTAT_SYSCTL)
255 return (procstat_getfiles_sysctl(procstat, kp, mmapped));
256 else if (procstat->type == PROCSTAT_KVM)
257 return (procstat_getfiles_kvm(procstat, kp, mmapped));
263 procstat_freefiles(struct procstat *procstat, struct filestat_list *head)
265 struct filestat *fst, *tmp;
267 STAILQ_FOREACH_SAFE(fst, head, next, tmp) {
268 if (fst->fs_path != NULL)
273 if (procstat->vmentries != NULL) {
274 free(procstat->vmentries);
275 procstat->vmentries = NULL;
277 if (procstat->files != NULL) {
278 free(procstat->files);
279 procstat->files = NULL;
283 static struct filestat *
284 filestat_new_entry(void *typedep, int type, int fd, int fflags, int uflags,
285 int refcount, off_t offset, char *path, cap_rights_t cap_rights)
287 struct filestat *entry;
289 entry = calloc(1, sizeof(*entry));
294 entry->fs_typedep = typedep;
295 entry->fs_fflags = fflags;
296 entry->fs_uflags = uflags;
298 entry->fs_type = type;
299 entry->fs_ref_count = refcount;
300 entry->fs_offset = offset;
301 entry->fs_path = path;
302 entry->fs_cap_rights = cap_rights;
306 static struct vnode *
307 getctty(kvm_t *kd, struct kinfo_proc *kp)
315 error = kvm_read_all(kd, (unsigned long)kp->ki_paddr, &proc,
318 warnx("can't read proc struct at %p for pid %d",
319 kp->ki_paddr, kp->ki_pid);
322 if (proc.p_pgrp == NULL)
324 error = kvm_read_all(kd, (unsigned long)proc.p_pgrp, &pgrp,
327 warnx("can't read pgrp struct at %p for pid %d",
328 proc.p_pgrp, kp->ki_pid);
331 error = kvm_read_all(kd, (unsigned long)pgrp.pg_session, &sess,
334 warnx("can't read session struct at %p for pid %d",
335 pgrp.pg_session, kp->ki_pid);
338 return (sess.s_ttyvp);
341 static struct filestat_list *
342 procstat_getfiles_kvm(struct procstat *procstat, struct kinfo_proc *kp, int mmapped)
345 struct filedesc filed;
346 struct vm_map_entry vmentry;
347 struct vm_object object;
348 struct vmspace vmspace;
349 vm_map_entry_t entryp;
353 struct file **ofiles;
354 struct filestat *entry;
355 struct filestat_list *head;
366 if (kp->ki_fd == NULL)
368 if (!kvm_read_all(kd, (unsigned long)kp->ki_fd, &filed,
370 warnx("can't read filedesc at %p", (void *)kp->ki_fd);
375 * Allocate list head.
377 head = malloc(sizeof(*head));
382 /* root directory vnode, if one. */
384 entry = filestat_new_entry(filed.fd_rdir, PS_FST_TYPE_VNODE, -1,
385 PS_FST_FFLAG_READ, PS_FST_UFLAG_RDIR, 0, 0, NULL, 0);
387 STAILQ_INSERT_TAIL(head, entry, next);
389 /* current working directory vnode. */
391 entry = filestat_new_entry(filed.fd_cdir, PS_FST_TYPE_VNODE, -1,
392 PS_FST_FFLAG_READ, PS_FST_UFLAG_CDIR, 0, 0, NULL, 0);
394 STAILQ_INSERT_TAIL(head, entry, next);
396 /* jail root, if any. */
398 entry = filestat_new_entry(filed.fd_jdir, PS_FST_TYPE_VNODE, -1,
399 PS_FST_FFLAG_READ, PS_FST_UFLAG_JAIL, 0, 0, NULL, 0);
401 STAILQ_INSERT_TAIL(head, entry, next);
403 /* ktrace vnode, if one */
405 entry = filestat_new_entry(kp->ki_tracep, PS_FST_TYPE_VNODE, -1,
406 PS_FST_FFLAG_READ | PS_FST_FFLAG_WRITE,
407 PS_FST_UFLAG_TRACE, 0, 0, NULL, 0);
409 STAILQ_INSERT_TAIL(head, entry, next);
411 /* text vnode, if one */
413 entry = filestat_new_entry(kp->ki_textvp, PS_FST_TYPE_VNODE, -1,
414 PS_FST_FFLAG_READ, PS_FST_UFLAG_TEXT, 0, 0, NULL, 0);
416 STAILQ_INSERT_TAIL(head, entry, next);
418 /* Controlling terminal. */
419 if ((vp = getctty(kd, kp)) != NULL) {
420 entry = filestat_new_entry(vp, PS_FST_TYPE_VNODE, -1,
421 PS_FST_FFLAG_READ | PS_FST_FFLAG_WRITE,
422 PS_FST_UFLAG_CTTY, 0, 0, NULL, 0);
424 STAILQ_INSERT_TAIL(head, entry, next);
427 nfiles = filed.fd_lastfile + 1;
428 ofiles = malloc(nfiles * sizeof(struct file *));
429 if (ofiles == NULL) {
430 warn("malloc(%zu)", nfiles * sizeof(struct file *));
433 if (!kvm_read_all(kd, (unsigned long)filed.fd_ofiles, ofiles,
434 nfiles * sizeof(struct file *))) {
435 warnx("cannot read file structures at %p",
436 (void *)filed.fd_ofiles);
440 for (i = 0; i <= filed.fd_lastfile; i++) {
441 if (ofiles[i] == NULL)
443 if (!kvm_read_all(kd, (unsigned long)ofiles[i], &file,
444 sizeof(struct file))) {
445 warnx("can't read file %d at %p", i,
449 switch (file.f_type) {
451 type = PS_FST_TYPE_VNODE;
455 type = PS_FST_TYPE_SOCKET;
459 type = PS_FST_TYPE_PIPE;
463 type = PS_FST_TYPE_FIFO;
468 type = PS_FST_TYPE_PTS;
475 /* XXXRW: No capability rights support for kvm yet. */
476 entry = filestat_new_entry(data, type, i,
477 to_filestat_flags(file.f_flag), 0, 0, 0, NULL, 0);
479 STAILQ_INSERT_TAIL(head, entry, next);
486 * Process mmapped files if requested.
489 if (!kvm_read_all(kd, (unsigned long)kp->ki_vmspace, &vmspace,
491 warnx("can't read vmspace at %p",
492 (void *)kp->ki_vmspace);
495 map = &vmspace.vm_map;
497 for (entryp = map->header.next;
498 entryp != &kp->ki_vmspace->vm_map.header;
499 entryp = vmentry.next) {
500 if (!kvm_read_all(kd, (unsigned long)entryp, &vmentry,
502 warnx("can't read vm_map_entry at %p",
506 if (vmentry.eflags & MAP_ENTRY_IS_SUB_MAP)
508 if ((objp = vmentry.object.vm_object) == NULL)
510 for (; objp; objp = object.backing_object) {
511 if (!kvm_read_all(kd, (unsigned long)objp,
512 &object, sizeof(object))) {
513 warnx("can't read vm_object at %p",
519 /* We want only vnode objects. */
520 if (object.type != OBJT_VNODE)
523 prot = vmentry.protection;
525 if (prot & VM_PROT_READ)
526 fflags = PS_FST_FFLAG_READ;
527 if ((vmentry.eflags & MAP_ENTRY_COW) == 0 &&
528 prot & VM_PROT_WRITE)
529 fflags |= PS_FST_FFLAG_WRITE;
532 * Create filestat entry.
534 entry = filestat_new_entry(object.handle,
535 PS_FST_TYPE_VNODE, -1, fflags,
536 PS_FST_UFLAG_MMAP, 0, 0, NULL, 0);
538 STAILQ_INSERT_TAIL(head, entry, next);
546 * kinfo types to filestat translation.
549 kinfo_type2fst(int kftype)
555 { KF_TYPE_CRYPTO, PS_FST_TYPE_CRYPTO },
556 { KF_TYPE_FIFO, PS_FST_TYPE_FIFO },
557 { KF_TYPE_KQUEUE, PS_FST_TYPE_KQUEUE },
558 { KF_TYPE_MQUEUE, PS_FST_TYPE_MQUEUE },
559 { KF_TYPE_NONE, PS_FST_TYPE_NONE },
560 { KF_TYPE_PIPE, PS_FST_TYPE_PIPE },
561 { KF_TYPE_PTS, PS_FST_TYPE_PTS },
562 { KF_TYPE_SEM, PS_FST_TYPE_SEM },
563 { KF_TYPE_SHM, PS_FST_TYPE_SHM },
564 { KF_TYPE_SOCKET, PS_FST_TYPE_SOCKET },
565 { KF_TYPE_VNODE, PS_FST_TYPE_VNODE },
566 { KF_TYPE_UNKNOWN, PS_FST_TYPE_UNKNOWN }
568 #define NKFTYPES (sizeof(kftypes2fst) / sizeof(*kftypes2fst))
571 for (i = 0; i < NKFTYPES; i++)
572 if (kftypes2fst[i].kf_type == kftype)
575 return (PS_FST_TYPE_UNKNOWN);
576 return (kftypes2fst[i].fst_type);
580 * kinfo flags to filestat translation.
583 kinfo_fflags2fst(int kfflags)
589 { KF_FLAG_APPEND, PS_FST_FFLAG_APPEND },
590 { KF_FLAG_ASYNC, PS_FST_FFLAG_ASYNC },
591 { KF_FLAG_CAPABILITY, PS_FST_FFLAG_CAPABILITY },
592 { KF_FLAG_CREAT, PS_FST_FFLAG_CREAT },
593 { KF_FLAG_DIRECT, PS_FST_FFLAG_DIRECT },
594 { KF_FLAG_EXCL, PS_FST_FFLAG_EXCL },
595 { KF_FLAG_EXEC, PS_FST_FFLAG_EXEC },
596 { KF_FLAG_EXLOCK, PS_FST_FFLAG_EXLOCK },
597 { KF_FLAG_FSYNC, PS_FST_FFLAG_SYNC },
598 { KF_FLAG_HASLOCK, PS_FST_FFLAG_HASLOCK },
599 { KF_FLAG_NOFOLLOW, PS_FST_FFLAG_NOFOLLOW },
600 { KF_FLAG_NONBLOCK, PS_FST_FFLAG_NONBLOCK },
601 { KF_FLAG_READ, PS_FST_FFLAG_READ },
602 { KF_FLAG_SHLOCK, PS_FST_FFLAG_SHLOCK },
603 { KF_FLAG_TRUNC, PS_FST_FFLAG_TRUNC },
604 { KF_FLAG_WRITE, PS_FST_FFLAG_WRITE }
606 #define NKFFLAGS (sizeof(kfflags2fst) / sizeof(*kfflags2fst))
611 for (i = 0; i < NKFFLAGS; i++)
612 if ((kfflags & kfflags2fst[i].kf_flag) != 0)
613 flags |= kfflags2fst[i].fst_flag;
618 kinfo_uflags2fst(int fd)
622 case KF_FD_TYPE_CTTY:
623 return (PS_FST_UFLAG_CTTY);
625 return (PS_FST_UFLAG_CDIR);
626 case KF_FD_TYPE_JAIL:
627 return (PS_FST_UFLAG_JAIL);
628 case KF_FD_TYPE_TEXT:
629 return (PS_FST_UFLAG_TEXT);
630 case KF_FD_TYPE_TRACE:
631 return (PS_FST_UFLAG_TRACE);
632 case KF_FD_TYPE_ROOT:
633 return (PS_FST_UFLAG_RDIR);
638 static struct filestat_list *
639 procstat_getfiles_sysctl(struct procstat *procstat, struct kinfo_proc *kp, int mmapped)
641 struct kinfo_file *kif, *files;
642 struct kinfo_vmentry *kve, *vmentries;
643 struct filestat_list *head;
644 struct filestat *entry;
650 cap_rights_t cap_rights;
653 if (kp->ki_fd == NULL)
656 files = kinfo_getfile(kp->ki_pid, &cnt);
657 if (files == NULL && errno != EPERM) {
658 warn("kinfo_getfile()");
661 procstat->files = files;
664 * Allocate list head.
666 head = malloc(sizeof(*head));
670 for (i = 0; i < cnt; i++) {
673 type = kinfo_type2fst(kif->kf_type);
674 fd = kif->kf_fd >= 0 ? kif->kf_fd : -1;
675 fflags = kinfo_fflags2fst(kif->kf_flags);
676 uflags = kinfo_uflags2fst(kif->kf_fd);
677 refcount = kif->kf_ref_count;
678 offset = kif->kf_offset;
679 if (*kif->kf_path != '\0')
680 path = strdup(kif->kf_path);
683 cap_rights = kif->kf_cap_rights;
686 * Create filestat entry.
688 entry = filestat_new_entry(kif, type, fd, fflags, uflags,
689 refcount, offset, path, cap_rights);
691 STAILQ_INSERT_TAIL(head, entry, next);
694 vmentries = kinfo_getvmmap(kp->ki_pid, &cnt);
695 procstat->vmentries = vmentries;
696 if (vmentries == NULL || cnt == 0)
698 for (i = 0; i < cnt; i++) {
700 if (kve->kve_type != KVME_TYPE_VNODE)
703 if (kve->kve_protection & KVME_PROT_READ)
704 fflags = PS_FST_FFLAG_READ;
705 if ((kve->kve_flags & KVME_FLAG_COW) == 0 &&
706 kve->kve_protection & KVME_PROT_WRITE)
707 fflags |= PS_FST_FFLAG_WRITE;
708 offset = kve->kve_offset;
709 refcount = kve->kve_ref_count;
710 if (*kve->kve_path != '\0')
711 path = strdup(kve->kve_path);
714 entry = filestat_new_entry(kve, PS_FST_TYPE_VNODE, -1,
715 fflags, PS_FST_UFLAG_MMAP, refcount, offset, path,
718 STAILQ_INSERT_TAIL(head, entry, next);
726 procstat_get_pipe_info(struct procstat *procstat, struct filestat *fst,
727 struct pipestat *ps, char *errbuf)
731 if (procstat->type == PROCSTAT_KVM) {
732 return (procstat_get_pipe_info_kvm(procstat->kd, fst, ps,
734 } else if (procstat->type == PROCSTAT_SYSCTL) {
735 return (procstat_get_pipe_info_sysctl(fst, ps, errbuf));
737 warnx("unknown access method: %d", procstat->type);
738 snprintf(errbuf, _POSIX2_LINE_MAX, "error");
744 procstat_get_pipe_info_kvm(kvm_t *kd, struct filestat *fst,
745 struct pipestat *ps, char *errbuf)
753 bzero(ps, sizeof(*ps));
754 pipep = fst->fs_typedep;
757 if (!kvm_read_all(kd, (unsigned long)pipep, &pi, sizeof(struct pipe))) {
758 warnx("can't read pipe at %p", (void *)pipep);
761 ps->addr = (uintptr_t)pipep;
762 ps->peer = (uintptr_t)pi.pipe_peer;
763 ps->buffer_cnt = pi.pipe_buffer.cnt;
767 snprintf(errbuf, _POSIX2_LINE_MAX, "error");
772 procstat_get_pipe_info_sysctl(struct filestat *fst, struct pipestat *ps,
773 char *errbuf __unused)
775 struct kinfo_file *kif;
779 bzero(ps, sizeof(*ps));
780 kif = fst->fs_typedep;
783 ps->addr = kif->kf_un.kf_pipe.kf_pipe_addr;
784 ps->peer = kif->kf_un.kf_pipe.kf_pipe_peer;
785 ps->buffer_cnt = kif->kf_un.kf_pipe.kf_pipe_buffer_cnt;
790 procstat_get_pts_info(struct procstat *procstat, struct filestat *fst,
791 struct ptsstat *pts, char *errbuf)
795 if (procstat->type == PROCSTAT_KVM) {
796 return (procstat_get_pts_info_kvm(procstat->kd, fst, pts,
798 } else if (procstat->type == PROCSTAT_SYSCTL) {
799 return (procstat_get_pts_info_sysctl(fst, pts, errbuf));
801 warnx("unknown access method: %d", procstat->type);
802 snprintf(errbuf, _POSIX2_LINE_MAX, "error");
808 procstat_get_pts_info_kvm(kvm_t *kd, struct filestat *fst,
809 struct ptsstat *pts, char *errbuf)
817 bzero(pts, sizeof(*pts));
818 ttyp = fst->fs_typedep;
821 if (!kvm_read_all(kd, (unsigned long)ttyp, &tty, sizeof(struct tty))) {
822 warnx("can't read tty at %p", (void *)ttyp);
825 pts->dev = dev2udev(kd, tty.t_dev);
826 (void)kdevtoname(kd, tty.t_dev, pts->devname);
830 snprintf(errbuf, _POSIX2_LINE_MAX, "error");
835 procstat_get_pts_info_sysctl(struct filestat *fst, struct ptsstat *pts,
836 char *errbuf __unused)
838 struct kinfo_file *kif;
842 bzero(pts, sizeof(*pts));
843 kif = fst->fs_typedep;
846 pts->dev = kif->kf_un.kf_pts.kf_pts_dev;
847 strlcpy(pts->devname, kif->kf_path, sizeof(pts->devname));
852 procstat_get_vnode_info(struct procstat *procstat, struct filestat *fst,
853 struct vnstat *vn, char *errbuf)
857 if (procstat->type == PROCSTAT_KVM) {
858 return (procstat_get_vnode_info_kvm(procstat->kd, fst, vn,
860 } else if (procstat->type == PROCSTAT_SYSCTL) {
861 return (procstat_get_vnode_info_sysctl(fst, vn, errbuf));
863 warnx("unknown access method: %d", procstat->type);
864 snprintf(errbuf, _POSIX2_LINE_MAX, "error");
870 procstat_get_vnode_info_kvm(kvm_t *kd, struct filestat *fst,
871 struct vnstat *vn, char *errbuf)
873 /* Filesystem specific handlers. */
874 #define FSTYPE(fst) {#fst, fst##_filestat}
877 int (*handler)(kvm_t *kd, struct vnode *vp,
885 #ifdef LIBPROCSTAT_NWFS
891 #ifdef LIBPROCSTAT_ZFS
895 #define NTYPES (sizeof(fstypes) / sizeof(*fstypes))
905 vp = fst->fs_typedep;
908 error = kvm_read_all(kd, (unsigned long)vp, &vnode, sizeof(vnode));
910 warnx("can't read vnode at %p", (void *)vp);
913 bzero(vn, sizeof(*vn));
914 vn->vn_type = vntype2psfsttype(vnode.v_type);
915 if (vnode.v_type == VNON || vnode.v_type == VBAD)
917 error = kvm_read_all(kd, (unsigned long)vnode.v_tag, tagstr,
920 warnx("can't read v_tag at %p", (void *)vp);
923 tagstr[sizeof(tagstr) - 1] = '\0';
926 * Find appropriate handler.
928 for (i = 0, found = 0; i < NTYPES; i++)
929 if (!strcmp(fstypes[i].tag, tagstr)) {
930 if (fstypes[i].handler(kd, &vnode, vn) != 0) {
936 snprintf(errbuf, _POSIX2_LINE_MAX, "?(%s)", tagstr);
939 vn->vn_mntdir = getmnton(kd, vnode.v_mount);
940 if ((vnode.v_type == VBLK || vnode.v_type == VCHR) &&
941 vnode.v_rdev != NULL){
942 vn->vn_dev = dev2udev(kd, vnode.v_rdev);
943 (void)kdevtoname(kd, vnode.v_rdev, vn->vn_devname);
950 snprintf(errbuf, _POSIX2_LINE_MAX, "error");
955 * kinfo vnode type to filestat translation.
958 kinfo_vtype2fst(int kfvtype)
964 { KF_VTYPE_VBAD, PS_FST_VTYPE_VBAD },
965 { KF_VTYPE_VBLK, PS_FST_VTYPE_VBLK },
966 { KF_VTYPE_VCHR, PS_FST_VTYPE_VCHR },
967 { KF_VTYPE_VDIR, PS_FST_VTYPE_VDIR },
968 { KF_VTYPE_VFIFO, PS_FST_VTYPE_VFIFO },
969 { KF_VTYPE_VLNK, PS_FST_VTYPE_VLNK },
970 { KF_VTYPE_VNON, PS_FST_VTYPE_VNON },
971 { KF_VTYPE_VREG, PS_FST_VTYPE_VREG },
972 { KF_VTYPE_VSOCK, PS_FST_VTYPE_VSOCK }
974 #define NKFVTYPES (sizeof(kfvtypes2fst) / sizeof(*kfvtypes2fst))
977 for (i = 0; i < NKFVTYPES; i++)
978 if (kfvtypes2fst[i].kf_vtype == kfvtype)
981 return (PS_FST_VTYPE_UNKNOWN);
982 return (kfvtypes2fst[i].fst_vtype);
986 procstat_get_vnode_info_sysctl(struct filestat *fst, struct vnstat *vn,
990 struct kinfo_file *kif;
991 struct kinfo_vmentry *kve;
1003 bzero(vn, sizeof(*vn));
1004 if (fst->fs_typedep == NULL)
1006 if (fst->fs_uflags & PS_FST_UFLAG_MMAP) {
1007 kve = fst->fs_typedep;
1008 fileid = kve->kve_vn_fileid;
1009 fsid = kve->kve_vn_fsid;
1010 mode = kve->kve_vn_mode;
1011 path = kve->kve_path;
1012 rdev = kve->kve_vn_rdev;
1013 size = kve->kve_vn_size;
1014 vntype = kinfo_vtype2fst(kve->kve_vn_type);
1015 status = kve->kve_status;
1017 kif = fst->fs_typedep;
1018 fileid = kif->kf_un.kf_file.kf_file_fileid;
1019 fsid = kif->kf_un.kf_file.kf_file_fsid;
1020 mode = kif->kf_un.kf_file.kf_file_mode;
1021 path = kif->kf_path;
1022 rdev = kif->kf_un.kf_file.kf_file_rdev;
1023 size = kif->kf_un.kf_file.kf_file_size;
1024 vntype = kinfo_vtype2fst(kif->kf_vnode_type);
1025 status = kif->kf_status;
1027 vn->vn_type = vntype;
1028 if (vntype == PS_FST_VTYPE_VNON || vntype == PS_FST_VTYPE_VBAD)
1030 if ((status & KF_ATTR_VALID) == 0) {
1031 snprintf(errbuf, _POSIX2_LINE_MAX, "? (no info available)");
1034 if (path && *path) {
1035 statfs(path, &stbuf);
1036 vn->vn_mntdir = strdup(stbuf.f_mntonname);
1038 vn->vn_mntdir = strdup("-");
1040 if (vntype == PS_FST_VTYPE_VBLK) {
1041 name = devname(rdev, S_IFBLK);
1043 strlcpy(vn->vn_devname, name,
1044 sizeof(vn->vn_devname));
1045 } else if (vntype == PS_FST_VTYPE_VCHR) {
1046 name = devname(vn->vn_dev, S_IFCHR);
1048 strlcpy(vn->vn_devname, name,
1049 sizeof(vn->vn_devname));
1052 vn->vn_fileid = fileid;
1059 procstat_get_socket_info(struct procstat *procstat, struct filestat *fst,
1060 struct sockstat *sock, char *errbuf)
1064 if (procstat->type == PROCSTAT_KVM) {
1065 return (procstat_get_socket_info_kvm(procstat->kd, fst, sock,
1067 } else if (procstat->type == PROCSTAT_SYSCTL) {
1068 return (procstat_get_socket_info_sysctl(fst, sock, errbuf));
1070 warnx("unknown access method: %d", procstat->type);
1071 snprintf(errbuf, _POSIX2_LINE_MAX, "error");
1077 procstat_get_socket_info_kvm(kvm_t *kd, struct filestat *fst,
1078 struct sockstat *sock, char *errbuf)
1082 struct protosw proto;
1091 bzero(sock, sizeof(*sock));
1092 so = fst->fs_typedep;
1095 sock->so_addr = (uintptr_t)so;
1096 /* fill in socket */
1097 if (!kvm_read_all(kd, (unsigned long)so, &s,
1098 sizeof(struct socket))) {
1099 warnx("can't read sock at %p", (void *)so);
1102 /* fill in protosw entry */
1103 if (!kvm_read_all(kd, (unsigned long)s.so_proto, &proto,
1104 sizeof(struct protosw))) {
1105 warnx("can't read protosw at %p", (void *)s.so_proto);
1108 /* fill in domain */
1109 if (!kvm_read_all(kd, (unsigned long)proto.pr_domain, &dom,
1110 sizeof(struct domain))) {
1111 warnx("can't read domain at %p",
1112 (void *)proto.pr_domain);
1115 if ((len = kvm_read(kd, (unsigned long)dom.dom_name, sock->dname,
1116 sizeof(sock->dname) - 1)) < 0) {
1117 warnx("can't read domain name at %p", (void *)dom.dom_name);
1118 sock->dname[0] = '\0';
1121 sock->dname[len] = '\0';
1124 * Fill in known data.
1126 sock->type = s.so_type;
1127 sock->proto = proto.pr_protocol;
1128 sock->dom_family = dom.dom_family;
1129 sock->so_pcb = (uintptr_t)s.so_pcb;
1132 * Protocol specific data.
1134 switch(dom.dom_family) {
1137 if (proto.pr_protocol == IPPROTO_TCP) {
1139 if (kvm_read(kd, (u_long)s.so_pcb,
1140 (char *)&inpcb, sizeof(struct inpcb))
1141 != sizeof(struct inpcb)) {
1142 warnx("can't read inpcb at %p",
1146 (uintptr_t)inpcb.inp_ppcb;
1152 if (kvm_read(kd, (u_long)s.so_pcb, (char *)&unpcb,
1153 sizeof(struct unpcb)) != sizeof(struct unpcb)){
1154 warnx("can't read unpcb at %p",
1156 } else if (unpcb.unp_conn) {
1157 sock->so_rcv_sb_state = s.so_rcv.sb_state;
1158 sock->so_snd_sb_state = s.so_snd.sb_state;
1159 sock->unp_conn = (uintptr_t)unpcb.unp_conn;
1169 snprintf(errbuf, _POSIX2_LINE_MAX, "error");
1174 procstat_get_socket_info_sysctl(struct filestat *fst, struct sockstat *sock,
1175 char *errbuf __unused)
1177 struct kinfo_file *kif;
1181 bzero(sock, sizeof(*sock));
1182 kif = fst->fs_typedep;
1187 * Fill in known data.
1189 sock->type = kif->kf_sock_type;
1190 sock->proto = kif->kf_sock_protocol;
1191 sock->dom_family = kif->kf_sock_domain;
1192 sock->so_pcb = kif->kf_un.kf_sock.kf_sock_pcb;
1193 strlcpy(sock->dname, kif->kf_path, sizeof(sock->dname));
1194 bcopy(&kif->kf_sa_local, &sock->sa_local, kif->kf_sa_local.ss_len);
1195 bcopy(&kif->kf_sa_peer, &sock->sa_peer, kif->kf_sa_peer.ss_len);
1198 * Protocol specific data.
1200 switch(sock->dom_family) {
1203 if (sock->proto == IPPROTO_TCP)
1204 sock->inp_ppcb = kif->kf_un.kf_sock.kf_sock_inpcb;
1207 if (kif->kf_un.kf_sock.kf_sock_unpconn != 0) {
1208 sock->so_rcv_sb_state =
1209 kif->kf_un.kf_sock.kf_sock_rcv_sb_state;
1210 sock->so_snd_sb_state =
1211 kif->kf_un.kf_sock.kf_sock_snd_sb_state;
1213 kif->kf_un.kf_sock.kf_sock_unpconn;
1223 * Descriptor flags to filestat translation.
1226 to_filestat_flags(int flags)
1232 { FREAD, PS_FST_FFLAG_READ },
1233 { FWRITE, PS_FST_FFLAG_WRITE },
1234 { O_APPEND, PS_FST_FFLAG_APPEND },
1235 { O_ASYNC, PS_FST_FFLAG_ASYNC },
1236 { O_CREAT, PS_FST_FFLAG_CREAT },
1237 { O_DIRECT, PS_FST_FFLAG_DIRECT },
1238 { O_EXCL, PS_FST_FFLAG_EXCL },
1239 { O_EXEC, PS_FST_FFLAG_EXEC },
1240 { O_EXLOCK, PS_FST_FFLAG_EXLOCK },
1241 { O_NOFOLLOW, PS_FST_FFLAG_NOFOLLOW },
1242 { O_NONBLOCK, PS_FST_FFLAG_NONBLOCK },
1243 { O_SHLOCK, PS_FST_FFLAG_SHLOCK },
1244 { O_SYNC, PS_FST_FFLAG_SYNC },
1245 { O_TRUNC, PS_FST_FFLAG_TRUNC }
1247 #define NFSTFLAGS (sizeof(fstflags) / sizeof(*fstflags))
1252 for (i = 0; i < NFSTFLAGS; i++)
1253 if (flags & fstflags[i].flag)
1254 fst_flags |= fstflags[i].fst_flag;
1259 * Vnode type to filestate translation.
1262 vntype2psfsttype(int type)
1268 { VBAD, PS_FST_VTYPE_VBAD },
1269 { VBLK, PS_FST_VTYPE_VBLK },
1270 { VCHR, PS_FST_VTYPE_VCHR },
1271 { VDIR, PS_FST_VTYPE_VDIR },
1272 { VFIFO, PS_FST_VTYPE_VFIFO },
1273 { VLNK, PS_FST_VTYPE_VLNK },
1274 { VNON, PS_FST_VTYPE_VNON },
1275 { VREG, PS_FST_VTYPE_VREG },
1276 { VSOCK, PS_FST_VTYPE_VSOCK }
1278 #define NVFTYPES (sizeof(vt2fst) / sizeof(*vt2fst))
1279 unsigned int i, fst_type;
1281 fst_type = PS_FST_VTYPE_UNKNOWN;
1282 for (i = 0; i < NVFTYPES; i++) {
1283 if (type == vt2fst[i].vtype) {
1284 fst_type = vt2fst[i].fst_vtype;
1292 getmnton(kvm_t *kd, struct mount *m)
1295 static struct mtab {
1298 char mntonname[MNAMELEN + 1];
1302 for (mt = mhead; mt != NULL; mt = mt->next)
1304 return (mt->mntonname);
1305 if (!kvm_read_all(kd, (unsigned long)m, &mnt, sizeof(struct mount))) {
1306 warnx("can't read mount table at %p", (void *)m);
1309 if ((mt = malloc(sizeof (struct mtab))) == NULL)
1312 bcopy(&mnt.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN);
1313 mt->mntonname[MNAMELEN] = '\0';
1316 return (mt->mntonname);