aboutsummaryrefslogtreecommitdiff
path: root/lib/libprocstat
diff options
context:
space:
mode:
Diffstat (limited to 'lib/libprocstat')
-rw-r--r--lib/libprocstat/Makefile74
-rw-r--r--lib/libprocstat/Makefile.depend19
-rw-r--r--lib/libprocstat/Symbol.map57
-rw-r--r--lib/libprocstat/cd9660.c88
-rw-r--r--lib/libprocstat/common_kvm.c212
-rw-r--r--lib/libprocstat/common_kvm.h51
-rw-r--r--lib/libprocstat/core.c516
-rw-r--r--lib/libprocstat/core.h58
-rw-r--r--lib/libprocstat/libprocstat.3600
-rw-r--r--lib/libprocstat/libprocstat.c2922
-rw-r--r--lib/libprocstat/libprocstat.h270
-rw-r--r--lib/libprocstat/libprocstat_compat.c259
-rw-r--r--lib/libprocstat/libprocstat_internal.h42
-rw-r--r--lib/libprocstat/msdosfs.c147
-rw-r--r--lib/libprocstat/smbfs.c78
-rw-r--r--lib/libprocstat/udf.c101
-rw-r--r--lib/libprocstat/zfs.c112
17 files changed, 5606 insertions, 0 deletions
diff --git a/lib/libprocstat/Makefile b/lib/libprocstat/Makefile
new file mode 100644
index 000000000000..e20518910ba3
--- /dev/null
+++ b/lib/libprocstat/Makefile
@@ -0,0 +1,74 @@
+.include <src.opts.mk>
+
+LIB= procstat
+
+SRCS= cd9660.c \
+ common_kvm.c \
+ core.c \
+ libprocstat.c \
+ libprocstat_compat.c \
+ msdosfs.c \
+ smbfs.c \
+ udf.c
+
+VERSION_DEF= ${LIBCSRCDIR}/Versions.def
+SYMBOL_MAPS= ${.CURDIR}/Symbol.map
+
+INCS= libprocstat.h
+CFLAGS+= -I. -I${.CURDIR} -D_KVM_VNODE
+SHLIB_MAJOR= 1
+
+LIBADD= elf kvm util
+
+MAN= libprocstat.3
+MLINKS+=libprocstat.3 procstat_close.3 \
+ libprocstat.3 procstat_freeargv.3 \
+ libprocstat.3 procstat_freeauxv.3 \
+ libprocstat.3 procstat_freeenvv.3 \
+ libprocstat.3 procstat_freefiles.3 \
+ libprocstat.3 procstat_freegroups.3 \
+ libprocstat.3 procstat_freekstack.3 \
+ libprocstat.3 procstat_freeprocs.3 \
+ libprocstat.3 procstat_freevmmap.3 \
+ libprocstat.3 procstat_get_pipe_info.3 \
+ libprocstat.3 procstat_get_pts_info.3 \
+ libprocstat.3 procstat_get_sem_info.3 \
+ libprocstat.3 procstat_get_shm_info.3 \
+ libprocstat.3 procstat_get_socket_info.3 \
+ libprocstat.3 procstat_get_vnode_info.3 \
+ libprocstat.3 procstat_getargv.3 \
+ libprocstat.3 procstat_getauxv.3 \
+ libprocstat.3 procstat_getenvv.3 \
+ libprocstat.3 procstat_getfiles.3 \
+ libprocstat.3 procstat_getgroups.3 \
+ libprocstat.3 procstat_getkstack.3 \
+ libprocstat.3 procstat_getosrel.3 \
+ libprocstat.3 procstat_getpathname.3 \
+ libprocstat.3 procstat_getprocs.3 \
+ libprocstat.3 procstat_getrlimit.3 \
+ libprocstat.3 procstat_getumask.3 \
+ libprocstat.3 procstat_getvmmap.3 \
+ libprocstat.3 procstat_open_core.3 \
+ libprocstat.3 procstat_open_kvm.3 \
+ libprocstat.3 procstat_open_sysctl.3
+
+.if ${MK_CDDL} != "no"
+CFLAGS+= -DLIBPROCSTAT_ZFS
+SRCS+= zfs.c
+ZFSTOP= ${SRCTOP}/sys/contrib/openzfs
+CFLAGS.zfs.c+= -DIN_BASE
+CFLAGS.zfs.c+= -DHAVE_ISSETUGID
+CFLAGS.zfs.c+= -DZFS_DEBUG
+CFLAGS.zfs.c+= -I${ZFSTOP}/include
+CFLAGS.zfs.c+= -I${ZFSTOP}/lib/libspl/include
+CFLAGS.zfs.c+= -I${ZFSTOP}/lib/libspl/include/os/freebsd
+CFLAGS.zfs.c+= -I${ZFSTOP}/include/os/freebsd/zfs
+CFLAGS.zfs.c+= -I${ZFSTOP}/module/icp/include
+CFLAGS.zfs.c+= -I${SRCTOP}/sys
+CFLAGS.zfs.c+= -I${SRCTOP}/sys/modules/zfs
+CFLAGS.zfs.c+= -include ${ZFSTOP}/include/os/freebsd/spl/sys/ccompile.h
+CFLAGS.zfs.c+= -include ${SRCTOP}/sys/modules/zfs/zfs_config.h
+CFLAGS.zfs.c+= -Wno-cast-qual
+.endif
+
+.include <bsd.lib.mk>
diff --git a/lib/libprocstat/Makefile.depend b/lib/libprocstat/Makefile.depend
new file mode 100644
index 000000000000..7ae9232842b4
--- /dev/null
+++ b/lib/libprocstat/Makefile.depend
@@ -0,0 +1,19 @@
+# Autogenerated - do NOT edit!
+
+DIRDEPS = \
+ include \
+ include/xlocale \
+ lib/${CSU_DIR} \
+ lib/libc \
+ lib/libcompiler_rt \
+ lib/libelf \
+ lib/libkvm \
+ lib/libprocstat/zfs \
+ lib/libutil \
+
+
+.include <dirdeps.mk>
+
+.if ${DEP_RELDIR} == ${_DEP_RELDIR}
+# local dependencies - needed for -jN in clean tree
+.endif
diff --git a/lib/libprocstat/Symbol.map b/lib/libprocstat/Symbol.map
new file mode 100644
index 000000000000..3d71c3eaae4a
--- /dev/null
+++ b/lib/libprocstat/Symbol.map
@@ -0,0 +1,57 @@
+/*
+ */
+FBSD_1.2 {
+ procstat_close;
+ procstat_freefiles;
+ procstat_freeprocs;
+ procstat_get_pipe_info;
+ procstat_getfiles;
+ procstat_getprocs;
+ procstat_open_kvm;
+ procstat_open_sysctl;
+};
+
+FBSD_1.3 {
+ procstat_freeargv;
+ procstat_freeauxv;
+ procstat_freeenvv;
+ procstat_freegroups;
+ procstat_freekstack;
+ procstat_freevmmap;
+ procstat_getargv;
+ procstat_getauxv;
+ procstat_getenvv;
+ procstat_getgroups;
+ procstat_getkstack;
+ procstat_getosrel;
+ procstat_getpathname;
+ procstat_getrlimit;
+ procstat_getumask;
+ procstat_getvmmap;
+ procstat_open_core;
+};
+
+FBSD_1.5 {
+ procstat_freeptlwpinfo;
+ procstat_getptlwpinfo;
+ procstat_get_sem_info;
+ procstat_get_shm_info;
+ procstat_get_socket_info;
+};
+
+FBSD_1.6 {
+ procstat_get_pts_info;
+ procstat_get_vnode_info;
+};
+
+FBSD_1.7 {
+ procstat_getadvlock;
+ procstat_freeadvlock;
+};
+
+FBSD_1.8 {
+ procstat_get_kqueue_info;
+ procstat_getrlimitusage;
+ procstat_freekqinfo;
+ procstat_freerlimitusage;
+}; \ No newline at end of file
diff --git a/lib/libprocstat/cd9660.c b/lib/libprocstat/cd9660.c
new file mode 100644
index 000000000000..ec37f49df0f6
--- /dev/null
+++ b/lib/libprocstat/cd9660.c
@@ -0,0 +1,88 @@
+/*
+ * SPDX-License-Identifier: BSD-4-Clause
+ *
+ * Copyright (c) 2000 Peter Edwards
+ * Copyright (c) 1988, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * This code is derived from software contributed to Berkeley by Peter Edwards
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by the University of
+ * California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/*
+ * XXX -
+ * This had to be separated from fstat.c because cd9660s has namespace
+ * conflicts with UFS.
+ */
+
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <sys/vnode.h>
+#include <sys/mount.h>
+
+#include <netinet/in.h>
+
+#include <err.h>
+
+#define _WANT_ISO_MNT
+#include <isofs/cd9660/iso.h>
+#include <isofs/cd9660/cd9660_node.h>
+
+#include <kvm.h>
+#include <stdio.h>
+
+#include "libprocstat.h"
+#include "common_kvm.h"
+
+int
+isofs_filestat(kvm_t *kd, struct vnode *vp, struct vnstat *vn)
+{
+ struct iso_node isonode;
+ struct iso_mnt mnt;
+
+ if (!kvm_read_all(kd, (unsigned long)VTOI(vp), &isonode,
+ sizeof(isonode))) {
+ warnx("can't read iso_node at %p",
+ (void *)VTOI(vp));
+ return (1);
+ }
+ if (!kvm_read_all(kd, (unsigned long)isonode.i_mnt, &mnt,
+ sizeof(mnt))) {
+ warnx("can't read iso_mnt at %p",
+ (void *)VTOI(vp));
+ return (1);
+ }
+ vn->vn_fsid = dev2udev(kd, mnt.im_dev);
+ vn->vn_mode = (mode_t)isonode.inode.iso_mode;
+ vn->vn_fileid = isonode.i_number;
+ vn->vn_size = isonode.i_size;
+ return (0);
+}
diff --git a/lib/libprocstat/common_kvm.c b/lib/libprocstat/common_kvm.c
new file mode 100644
index 000000000000..a129605e7fa0
--- /dev/null
+++ b/lib/libprocstat/common_kvm.c
@@ -0,0 +1,212 @@
+/*-
+ * SPDX-License-Identifier: BSD-4-Clause
+ *
+ * Copyright (c) 2009 Stanislav Sedov <stas@FreeBSD.org>
+ * Copyright (c) 1988, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by the University of
+ * California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/param.h>
+#include <sys/user.h>
+#include <sys/stat.h>
+#include <sys/vnode.h>
+#include <sys/conf.h>
+#include <sys/pipe.h>
+#define _WANT_MOUNT
+#include <sys/mount.h>
+#include <ufs/ufs/quota.h>
+#include <ufs/ufs/inode.h>
+#include <ufs/ufs/extattr.h>
+#include <ufs/ufs/ufsmount.h>
+#include <fs/devfs/devfs.h>
+#include <fs/devfs/devfs_int.h>
+#include <nfs/nfsproto.h>
+#include <nfsclient/nfs.h>
+#include <nfsclient/nfsnode.h>
+
+#include <assert.h>
+#include <err.h>
+#include <kvm.h>
+#include <stddef.h>
+#include <string.h>
+
+#include <libprocstat.h>
+#include "common_kvm.h"
+
+int
+kvm_read_all(kvm_t *kd, unsigned long addr, void *buf, size_t nbytes)
+{
+ ssize_t error;
+
+ if (nbytes >= SSIZE_MAX)
+ return (0);
+ error = kvm_read(kd, addr, buf, nbytes);
+ return (error == (ssize_t)(nbytes));
+}
+
+int
+kdevtoname(kvm_t *kd, struct cdev *dev, char *buf)
+{
+ struct cdev si;
+
+ assert(buf);
+ if (!kvm_read_all(kd, (unsigned long)dev, &si, sizeof(si)))
+ return (1);
+ strlcpy(buf, si.si_name, SPECNAMELEN + 1);
+ return (0);
+}
+
+int
+ufs_filestat(kvm_t *kd, struct vnode *vp, struct vnstat *vn)
+{
+ struct inode inode;
+ struct ufsmount um;
+
+ if (!kvm_read_all(kd, (unsigned long)VTOI(vp), &inode, sizeof(inode))) {
+ warnx("can't read inode at %p", (void *)VTOI(vp));
+ return (1);
+ }
+ if (!kvm_read_all(kd, (unsigned long)inode.i_ump, &um, sizeof(um))) {
+ warnx("can't read ufsmount at %p", (void *)inode.i_ump);
+ return (1);
+ }
+ /*
+ * The st_dev from stat(2) is a dev_t. These kernel structures
+ * contain cdev pointers. We need to convert to dev_t to make
+ * comparisons
+ */
+ vn->vn_fsid = dev2udev(kd, um.um_dev);
+ vn->vn_fileid = inode.i_number;
+ vn->vn_mode = (mode_t)inode.i_mode;
+ vn->vn_size = inode.i_size;
+ return (0);
+}
+
+int
+devfs_filestat(kvm_t *kd, struct vnode *vp, struct vnstat *vn)
+{
+ struct devfs_dirent devfs_dirent;
+ struct mount mount;
+
+ if (!kvm_read_all(kd, (unsigned long)getvnodedata(vp), &devfs_dirent,
+ sizeof(devfs_dirent))) {
+ warnx("can't read devfs_dirent at %p",
+ (void *)vp->v_data);
+ return (1);
+ }
+ if (!kvm_read_all(kd, (unsigned long)getvnodemount(vp), &mount,
+ sizeof(mount))) {
+ warnx("can't read mount at %p",
+ (void *)getvnodemount(vp));
+ return (1);
+ }
+ vn->vn_fsid = mount.mnt_stat.f_fsid.val[0];
+ vn->vn_fileid = devfs_dirent.de_inode;
+ vn->vn_mode = (devfs_dirent.de_mode & ~S_IFMT) | S_IFCHR;
+ vn->vn_size = 0;
+ return (0);
+}
+
+int
+nfs_filestat(kvm_t *kd, struct vnode *vp, struct vnstat *vn)
+{
+ struct nfsnode nfsnode;
+ mode_t mode;
+
+ if (!kvm_read_all(kd, (unsigned long)VTONFS(vp), &nfsnode,
+ sizeof(nfsnode))) {
+ warnx("can't read nfsnode at %p",
+ (void *)VTONFS(vp));
+ return (1);
+ }
+ vn->vn_fsid = nfsnode.n_vattr.va_fsid;
+ vn->vn_fileid = nfsnode.n_vattr.va_fileid;
+ vn->vn_size = nfsnode.n_size;
+ mode = (mode_t)nfsnode.n_vattr.va_mode;
+ switch (vp->v_type) {
+ case VREG:
+ mode |= S_IFREG;
+ break;
+ case VDIR:
+ mode |= S_IFDIR;
+ break;
+ case VBLK:
+ mode |= S_IFBLK;
+ break;
+ case VCHR:
+ mode |= S_IFCHR;
+ break;
+ case VLNK:
+ mode |= S_IFLNK;
+ break;
+ case VSOCK:
+ mode |= S_IFSOCK;
+ break;
+ case VFIFO:
+ mode |= S_IFIFO;
+ break;
+ default:
+ break;
+ };
+ vn->vn_mode = mode;
+ return (0);
+}
+
+/*
+ * Read the cdev structure in the kernel in order to work out the
+ * associated dev_t
+ */
+dev_t
+dev2udev(kvm_t *kd, struct cdev *dev)
+{
+ struct cdev_priv priv;
+
+ assert(kd);
+ if (kvm_read_all(kd, (unsigned long)cdev2priv(dev), &priv,
+ sizeof(priv))) {
+ return ((dev_t)priv.cdp_inode);
+ } else {
+ warnx("can't convert cdev *%p to a dev_t\n", dev);
+ return (-1);
+ }
+}
+
+void *
+getvnodedata(struct vnode *vp)
+{
+ return (vp->v_data);
+}
+
+struct mount *
+getvnodemount(struct vnode *vp)
+{
+ return (vp->v_mount);
+}
diff --git a/lib/libprocstat/common_kvm.h b/lib/libprocstat/common_kvm.h
new file mode 100644
index 000000000000..567211d98728
--- /dev/null
+++ b/lib/libprocstat/common_kvm.h
@@ -0,0 +1,51 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2009 Stanislav Sedov <stas@FreeBSD.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef _COMMON_KVM_H_
+#define _COMMON_KVM_H_
+
+dev_t dev2udev(kvm_t *kd, struct cdev *dev);
+int kdevtoname(kvm_t *kd, struct cdev *dev, char *);
+int kvm_read_all(kvm_t *kd, unsigned long addr, void *buf,
+ size_t nbytes);
+
+/*
+ * Filesystems specific access routines.
+ */
+int devfs_filestat(kvm_t *kd, struct vnode *vp, struct vnstat *vn);
+int isofs_filestat(kvm_t *kd, struct vnode *vp, struct vnstat *vn);
+int msdosfs_filestat(kvm_t *kd, struct vnode *vp, struct vnstat *vn);
+int nfs_filestat(kvm_t *kd, struct vnode *vp, struct vnstat *vn);
+int smbfs_filestat(kvm_t *kd, struct vnode *vp, struct vnstat *vn);
+int udf_filestat(kvm_t *kd, struct vnode *vp, struct vnstat *vn);
+int ufs_filestat(kvm_t *kd, struct vnode *vp, struct vnstat *vn);
+int zfs_filestat(kvm_t *kd, struct vnode *vp, struct vnstat *vn);
+void *getvnodedata(struct vnode *vp);
+struct mount *getvnodemount(struct vnode *vp);
+
+#endif /* _COMMON_KVM_H_ */
diff --git a/lib/libprocstat/core.c b/lib/libprocstat/core.c
new file mode 100644
index 000000000000..b27b35de3bab
--- /dev/null
+++ b/lib/libprocstat/core.c
@@ -0,0 +1,516 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2013 Mikolaj Golub <trociny@FreeBSD.org>
+ * Copyright (c) 2017 Dell EMC
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/param.h>
+#include <sys/elf.h>
+#include <sys/exec.h>
+#include <sys/ptrace.h>
+#include <sys/user.h>
+
+#include <assert.h>
+#include <err.h>
+#include <fcntl.h>
+#include <gelf.h>
+#include <libelf.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "core.h"
+
+#define PROCSTAT_CORE_MAGIC 0x012DADB8
+struct procstat_core
+{
+ int pc_magic;
+ int pc_fd;
+ Elf *pc_elf;
+ GElf_Ehdr pc_ehdr;
+ GElf_Phdr pc_phdr;
+};
+
+static const struct psc_type_info {
+ unsigned int n_type;
+ int structsize;
+} psc_type_info[PSC_TYPE_MAX] = {
+ [PSC_TYPE_PROC] = {
+ .n_type = NT_PROCSTAT_PROC,
+ .structsize = sizeof(struct kinfo_proc)
+ },
+ [PSC_TYPE_FILES] = {
+ .n_type = NT_PROCSTAT_FILES,
+ .structsize = sizeof(struct kinfo_file)
+ },
+ [PSC_TYPE_VMMAP] = {
+ .n_type = NT_PROCSTAT_VMMAP,
+ .structsize = sizeof(struct kinfo_vmentry)
+ },
+ [PSC_TYPE_GROUPS] = {
+ .n_type = NT_PROCSTAT_GROUPS,
+ .structsize = sizeof(gid_t)
+ },
+ [PSC_TYPE_UMASK] = {
+ .n_type = NT_PROCSTAT_UMASK,
+ .structsize = sizeof(u_short)
+ },
+ [PSC_TYPE_RLIMIT] = {
+ .n_type = NT_PROCSTAT_RLIMIT,
+ .structsize = sizeof(struct rlimit) * RLIM_NLIMITS
+ },
+ [PSC_TYPE_OSREL] = {
+ .n_type = NT_PROCSTAT_OSREL,
+ .structsize = sizeof(int)
+ },
+ [PSC_TYPE_PSSTRINGS] = {
+ .n_type = NT_PROCSTAT_PSSTRINGS,
+ .structsize = sizeof(vm_offset_t)
+ },
+ [PSC_TYPE_ARGV] = {
+ .n_type = NT_PROCSTAT_PSSTRINGS,
+ .structsize = sizeof(vm_offset_t)
+ },
+ [PSC_TYPE_ENVV] = {
+ .n_type = NT_PROCSTAT_PSSTRINGS,
+ .structsize = sizeof(vm_offset_t)
+ },
+ [PSC_TYPE_AUXV] = {
+ .n_type = NT_PROCSTAT_AUXV,
+ .structsize = sizeof(Elf_Auxinfo)
+ },
+ [PSC_TYPE_PTLWPINFO] = {
+ .n_type = NT_PTLWPINFO,
+ .structsize = sizeof(struct ptrace_lwpinfo)
+ },
+ [PSC_TYPE_KQUEUES] = {
+ .n_type = NT_PROCSTAT_KQUEUES,
+ .structsize = sizeof(struct kinfo_knote)
+ },
+};
+
+static bool core_offset(struct procstat_core *core, off_t offset);
+static bool core_read(struct procstat_core *core, void *buf, size_t len);
+static ssize_t core_read_mem(struct procstat_core *core, void *buf,
+ size_t len, vm_offset_t addr, bool readall);
+static void *get_args(struct procstat_core *core, vm_offset_t psstrings,
+ enum psc_type type, void *buf, size_t *lenp);
+
+struct procstat_core *
+procstat_core_open(const char *filename)
+{
+ struct procstat_core *core;
+ Elf *e;
+ GElf_Ehdr ehdr;
+ GElf_Phdr phdr;
+ size_t nph;
+ int fd, i;
+
+ if (elf_version(EV_CURRENT) == EV_NONE) {
+ warnx("ELF library too old");
+ return (NULL);
+ }
+ fd = open(filename, O_RDONLY, 0);
+ if (fd == -1) {
+ warn("open(%s)", filename);
+ return (NULL);
+ }
+ e = elf_begin(fd, ELF_C_READ, NULL);
+ if (e == NULL) {
+ warnx("elf_begin: %s", elf_errmsg(-1));
+ goto fail;
+ }
+ if (elf_kind(e) != ELF_K_ELF) {
+ warnx("%s is not an ELF object", filename);
+ goto fail;
+ }
+ if (gelf_getehdr(e, &ehdr) == NULL) {
+ warnx("gelf_getehdr: %s", elf_errmsg(-1));
+ goto fail;
+ }
+ if (ehdr.e_type != ET_CORE) {
+ warnx("%s is not a CORE file", filename);
+ goto fail;
+ }
+ if (elf_getphdrnum(e, &nph) == -1) {
+ warnx("program headers not found");
+ goto fail;
+ }
+ for (i = 0; i < ehdr.e_phnum; i++) {
+ if (gelf_getphdr(e, i, &phdr) != &phdr) {
+ warnx("gelf_getphdr: %s", elf_errmsg(-1));
+ goto fail;
+ }
+ if (phdr.p_type == PT_NOTE)
+ break;
+ }
+ if (i == ehdr.e_phnum) {
+ warnx("NOTE program header not found");
+ goto fail;
+ }
+ core = malloc(sizeof(struct procstat_core));
+ if (core == NULL) {
+ warn("malloc(%zu)", sizeof(struct procstat_core));
+ goto fail;
+ }
+ core->pc_magic = PROCSTAT_CORE_MAGIC;
+ core->pc_fd = fd;
+ core->pc_elf = e;
+ core->pc_ehdr = ehdr;
+ core->pc_phdr = phdr;
+
+ return (core);
+fail:
+ if (e != NULL)
+ elf_end(e);
+ close(fd);
+
+ return (NULL);
+}
+
+void
+procstat_core_close(struct procstat_core *core)
+{
+
+ assert(core->pc_magic == PROCSTAT_CORE_MAGIC);
+
+ elf_end(core->pc_elf);
+ close(core->pc_fd);
+ free(core);
+}
+
+void *
+procstat_core_get(struct procstat_core *core, enum psc_type type, void *buf,
+ size_t *lenp)
+{
+ Elf_Note nhdr;
+ off_t offset, eoffset;
+ vm_offset_t psstrings;
+ void *freebuf;
+ size_t len, curlen;
+ int cstructsize;
+ char nbuf[8];
+
+ assert(core->pc_magic == PROCSTAT_CORE_MAGIC);
+
+ if (type >= PSC_TYPE_MAX) {
+ warnx("unknown core stat type: %d", type);
+ return (NULL);
+ }
+
+ offset = core->pc_phdr.p_offset;
+ eoffset = offset + core->pc_phdr.p_filesz;
+ curlen = 0;
+
+ while (offset < eoffset) {
+ if (!core_offset(core, offset))
+ return (NULL);
+ if (!core_read(core, &nhdr, sizeof(nhdr)))
+ return (NULL);
+
+ offset += sizeof(nhdr) +
+ roundup2(nhdr.n_namesz, sizeof(Elf32_Size)) +
+ roundup2(nhdr.n_descsz, sizeof(Elf32_Size));
+
+ if (nhdr.n_namesz == 0 && nhdr.n_descsz == 0)
+ break;
+ if (nhdr.n_type != psc_type_info[type].n_type)
+ continue;
+ if (nhdr.n_namesz != 8)
+ continue;
+ if (!core_read(core, nbuf, sizeof(nbuf)))
+ return (NULL);
+ if (strcmp(nbuf, "FreeBSD") != 0)
+ continue;
+ if (nhdr.n_descsz < sizeof(cstructsize)) {
+ warnx("corrupted core file");
+ return (NULL);
+ }
+ if (!core_read(core, &cstructsize, sizeof(cstructsize)))
+ return (NULL);
+ if (cstructsize != psc_type_info[type].structsize) {
+ warnx("version mismatch");
+ return (NULL);
+ }
+ len = nhdr.n_descsz - sizeof(cstructsize);
+ if (len == 0)
+ return (NULL);
+ if (buf != NULL) {
+ len = MIN(len, *lenp);
+ freebuf = NULL;
+ } else {
+ freebuf = buf = malloc(len);
+ if (buf == NULL) {
+ warn("malloc(%zu)", len);
+ return (NULL);
+ }
+ }
+ if (!core_read(core, (char *)buf + curlen, len)) {
+ free(freebuf);
+ return (NULL);
+ }
+ if (type == PSC_TYPE_ARGV || type == PSC_TYPE_ENVV) {
+ if (len < sizeof(psstrings)) {
+ free(freebuf);
+ return (NULL);
+ }
+ psstrings = *(vm_offset_t *)buf;
+ if (freebuf == NULL)
+ len = *lenp;
+ else
+ buf = NULL;
+ free(freebuf);
+ buf = get_args(core, psstrings, type, buf, &len);
+ } else if (type == PSC_TYPE_PTLWPINFO) {
+ *lenp -= len;
+ curlen += len;
+ continue;
+ }
+ *lenp = len;
+ return (buf);
+ }
+
+ if (curlen != 0) {
+ *lenp = curlen;
+ return (buf);
+ }
+
+ return (NULL);
+}
+
+static bool
+core_offset(struct procstat_core *core, off_t offset)
+{
+
+ assert(core->pc_magic == PROCSTAT_CORE_MAGIC);
+
+ if (lseek(core->pc_fd, offset, SEEK_SET) == -1) {
+ warn("core: lseek(%jd)", (intmax_t)offset);
+ return (false);
+ }
+ return (true);
+}
+
+static bool
+core_read(struct procstat_core *core, void *buf, size_t len)
+{
+ ssize_t n;
+
+ assert(core->pc_magic == PROCSTAT_CORE_MAGIC);
+
+ n = read(core->pc_fd, buf, len);
+ if (n == -1) {
+ warn("core: read");
+ return (false);
+ }
+ if (n < (ssize_t)len) {
+ warnx("core: short read");
+ return (false);
+ }
+ return (true);
+}
+
+static ssize_t
+core_read_mem(struct procstat_core *core, void *buf, size_t len,
+ vm_offset_t addr, bool readall)
+{
+ GElf_Phdr phdr;
+ off_t offset;
+ int i;
+
+ assert(core->pc_magic == PROCSTAT_CORE_MAGIC);
+
+ for (i = 0; i < core->pc_ehdr.e_phnum; i++) {
+ if (gelf_getphdr(core->pc_elf, i, &phdr) != &phdr) {
+ warnx("gelf_getphdr: %s", elf_errmsg(-1));
+ return (-1);
+ }
+ if (phdr.p_type != PT_LOAD)
+ continue;
+ if (addr < phdr.p_vaddr || addr > phdr.p_vaddr + phdr.p_memsz)
+ continue;
+ offset = phdr.p_offset + (addr - phdr.p_vaddr);
+ if ((phdr.p_vaddr + phdr.p_memsz) - addr < len) {
+ if (readall) {
+ warnx("format error: "
+ "attempt to read out of segment");
+ return (-1);
+ }
+ len = (phdr.p_vaddr + phdr.p_memsz) - addr;
+ }
+ if (!core_offset(core, offset))
+ return (-1);
+ if (!core_read(core, buf, len))
+ return (-1);
+ return (len);
+ }
+ warnx("format error: address %ju not found", (uintmax_t)addr);
+ return (-1);
+}
+
+#define ARGS_CHUNK_SZ 256 /* Chunk size (bytes) for get_args operations. */
+
+static void *
+get_args(struct procstat_core *core, vm_offset_t psstrings, enum psc_type type,
+ void *args, size_t *lenp)
+{
+ struct ps_strings pss;
+ void *freeargs;
+ vm_offset_t addr;
+ char **argv, *p;
+ size_t chunksz, done, len, nchr, size;
+ ssize_t n;
+ u_int i, nstr;
+
+ assert(type == PSC_TYPE_ARGV || type == PSC_TYPE_ENVV);
+
+ if (core_read_mem(core, &pss, sizeof(pss), psstrings, true) == -1)
+ return (NULL);
+ if (type == PSC_TYPE_ARGV) {
+ addr = (vm_offset_t)pss.ps_argvstr;
+ nstr = pss.ps_nargvstr;
+ } else /* type == PSC_TYPE_ENVV */ {
+ addr = (vm_offset_t)pss.ps_envstr;
+ nstr = pss.ps_nenvstr;
+ }
+ if (addr == 0 || nstr == 0)
+ return (NULL);
+ if (nstr > ARG_MAX) {
+ warnx("format error");
+ return (NULL);
+ }
+ size = nstr * sizeof(char *);
+ argv = malloc(size);
+ if (argv == NULL) {
+ warn("malloc(%zu)", size);
+ return (NULL);
+ }
+ done = 0;
+ freeargs = NULL;
+ if (core_read_mem(core, argv, size, addr, true) == -1)
+ goto fail;
+ if (args != NULL) {
+ nchr = MIN(ARG_MAX, *lenp);
+ } else {
+ nchr = ARG_MAX;
+ freeargs = args = malloc(nchr);
+ if (args == NULL) {
+ warn("malloc(%zu)", nchr);
+ goto fail;
+ }
+ }
+ p = args;
+ for (i = 0; ; i++) {
+ if (i == nstr)
+ goto done;
+ /*
+ * The program may have scribbled into its argv array, e.g. to
+ * remove some arguments. If that has happened, break out
+ * before trying to read from NULL.
+ */
+ if (argv[i] == NULL)
+ goto done;
+ for (addr = (vm_offset_t)argv[i]; ; addr += chunksz) {
+ chunksz = MIN(ARGS_CHUNK_SZ, nchr - 1 - done);
+ if (chunksz <= 0)
+ goto done;
+ n = core_read_mem(core, p, chunksz, addr, false);
+ if (n == -1)
+ goto fail;
+ len = strnlen(p, chunksz);
+ p += len;
+ done += len;
+ if (len != chunksz)
+ break;
+ }
+ *p++ = '\0';
+ done++;
+ }
+fail:
+ free(freeargs);
+ args = NULL;
+done:
+ *lenp = done;
+ free(argv);
+ return (args);
+}
+
+int
+procstat_core_note_count(struct procstat_core *core, enum psc_type type)
+{
+ Elf_Note nhdr;
+ off_t offset, eoffset;
+ int cstructsize;
+ char nbuf[8];
+ int n;
+
+ if (type >= PSC_TYPE_MAX) {
+ warnx("unknown core stat type: %d", type);
+ return (0);
+ }
+
+ offset = core->pc_phdr.p_offset;
+ eoffset = offset + core->pc_phdr.p_filesz;
+
+ for (n = 0; offset < eoffset; n++) {
+ if (!core_offset(core, offset))
+ return (0);
+ if (!core_read(core, &nhdr, sizeof(nhdr)))
+ return (0);
+
+ offset += sizeof(nhdr) +
+ roundup2(nhdr.n_namesz, sizeof(Elf32_Size)) +
+ roundup2(nhdr.n_descsz, sizeof(Elf32_Size));
+
+ if (nhdr.n_namesz == 0 && nhdr.n_descsz == 0)
+ break;
+ if (nhdr.n_type != psc_type_info[type].n_type)
+ continue;
+ if (nhdr.n_namesz != 8)
+ continue;
+ if (!core_read(core, nbuf, sizeof(nbuf)))
+ return (0);
+ if (strcmp(nbuf, "FreeBSD") != 0)
+ continue;
+ if (nhdr.n_descsz < sizeof(cstructsize)) {
+ warnx("corrupted core file");
+ return (0);
+ }
+ if (!core_read(core, &cstructsize, sizeof(cstructsize)))
+ return (0);
+ if (cstructsize != psc_type_info[type].structsize) {
+ warnx("version mismatch");
+ return (0);
+ }
+ if (nhdr.n_descsz - sizeof(cstructsize) == 0)
+ return (0);
+ }
+
+ return (n);
+}
diff --git a/lib/libprocstat/core.h b/lib/libprocstat/core.h
new file mode 100644
index 000000000000..f4276fbdf09e
--- /dev/null
+++ b/lib/libprocstat/core.h
@@ -0,0 +1,58 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2013 Mikolaj Golub <trociny@FreeBSD.org>
+ * Copyright (c) 2017 Dell EMC
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef _CORE_H
+#define _CORE_H
+
+enum psc_type {
+ PSC_TYPE_PROC = 0,
+ PSC_TYPE_FILES,
+ PSC_TYPE_VMMAP,
+ PSC_TYPE_GROUPS,
+ PSC_TYPE_UMASK,
+ PSC_TYPE_RLIMIT,
+ PSC_TYPE_OSREL,
+ PSC_TYPE_PSSTRINGS,
+ PSC_TYPE_ARGV,
+ PSC_TYPE_ENVV,
+ PSC_TYPE_AUXV,
+ PSC_TYPE_PTLWPINFO,
+ PSC_TYPE_KQUEUES,
+ PSC_TYPE_MAX
+};
+
+struct procstat_core;
+
+void procstat_core_close(struct procstat_core *core);
+void *procstat_core_get(struct procstat_core *core, enum psc_type type,
+ void * buf, size_t *lenp);
+int procstat_core_note_count(struct procstat_core *core, enum psc_type type);
+struct procstat_core *procstat_core_open(const char *filename);
+
+#endif /* !_CORE_H_ */
diff --git a/lib/libprocstat/libprocstat.3 b/lib/libprocstat/libprocstat.3
new file mode 100644
index 000000000000..2617a8827a6d
--- /dev/null
+++ b/lib/libprocstat/libprocstat.3
@@ -0,0 +1,600 @@
+.\" Copyright (c) 2011 Sergey Kandaurov <pluknet@FreeBSD.org>
+.\" All rights reserved.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\" notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\" notice, this list of conditions and the following disclaimer in the
+.\" documentation and/or other materials provided with the distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+.\" SUCH DAMAGE.
+.\"
+.Dd April 3, 2022
+.Dt LIBPROCSTAT 3
+.Os
+.Sh NAME
+.Nm procstat_close ,
+.Nm procstat_freeadvlock ,
+.Nm procstat_freeargv ,
+.Nm procstat_freeauxv ,
+.Nm procstat_freeenvv ,
+.Nm procstat_freefiles ,
+.Nm procstat_freegroups ,
+.Nm procstat_freekstack ,
+.Nm procstat_freeprocs ,
+.Nm procstat_freeptlwpinfo ,
+.Nm procstat_freevmmap ,
+.Nm procstat_get_pipe_info ,
+.Nm procstat_get_pts_info ,
+.Nm procstat_get_sem_info ,
+.Nm procstat_get_shm_info ,
+.Nm procstat_get_socket_info ,
+.Nm procstat_get_vnode_info ,
+.Nm procstat_getadvlock ,
+.Nm procstat_getargv ,
+.Nm procstat_getauxv ,
+.Nm procstat_getenvv ,
+.Nm procstat_getfiles ,
+.Nm procstat_getgroups ,
+.Nm procstat_getkstack ,
+.Nm procstat_getosrel ,
+.Nm procstat_getpathname ,
+.Nm procstat_getprocs ,
+.Nm procstat_getptlwpinfo ,
+.Nm procstat_getrlimit ,
+.Nm procstat_getumask ,
+.Nm procstat_getvmmap ,
+.Nm procstat_open_core ,
+.Nm procstat_open_kvm ,
+.Nm procstat_open_sysctl
+.Nd library interface for file and process information retrieval
+.Sh LIBRARY
+.Lb libprocstat
+.Sh SYNOPSIS
+.In sys/param.h
+.In sys/queue.h
+.In sys/socket.h
+.In libprocstat.h
+.Ft void
+.Fn procstat_close "struct procstat *procstat"
+.Ft void
+.Fn procstat_freeadvlock "struct procstat *procstat" "struct advlock_list *list"
+.Ft void
+.Fo procstat_freeargv
+.Fa "struct procstat *procstat"
+.Fc
+.Ft void
+.Fo procstat_freeauxv
+.Fa "struct procstat *procstat"
+.Fa "Elf_Auxinfo *auxv"
+.Fc
+.Ft void
+.Fo procstat_freeenvv
+.Fa "struct procstat *procstat"
+.Fc
+.Ft void
+.Fo procstat_freefiles
+.Fa "struct procstat *procstat"
+.Fa "struct filestat_list *head"
+.Fc
+.Ft void
+.Fo procstat_freegroups
+.Fa "struct procstat *procstat"
+.Fa "gid_t *groups"
+.Fc
+.Ft void
+.Fo procstat_freekstack
+.Fa "struct procstat *procstat"
+.Fa "struct kinfo_kstack *kkstp"
+.Fc
+.Ft void
+.Fn procstat_freeprocs "struct procstat *procstat" "struct kinfo_proc *p"
+.Ft void
+.Fo procstat_freevmmap
+.Fa "struct procstat *procstat"
+.Fa "struct kinfo_vmentry *vmmap"
+.Fc
+.Ft void
+.Fo procstat_freeptlwpinfo
+.Fa "struct procstat *procstat"
+.Fa "struct ptrace_lwpinfo *pl"
+.Fc
+.Ft int
+.Fo procstat_get_pipe_info
+.Fa "struct procstat *procstat"
+.Fa "struct filestat *fst"
+.Fa "struct pipestat *pipe"
+.Fa "char *errbuf"
+.Fc
+.Ft int
+.Fo procstat_get_pts_info
+.Fa "struct procstat *procstat"
+.Fa "struct filestat *fst"
+.Fa "struct ptsstat *pts"
+.Fa "char *errbuf"
+.Fc
+.Ft int
+.Fo procstat_get_sem_info
+.Fa "struct procstat *procstat"
+.Fa "struct filestat *fst"
+.Fa "struct semstat *sem"
+.Fa "char *errbuf"
+.Fc
+.Ft int
+.Fo procstat_get_shm_info
+.Fa "struct procstat *procstat"
+.Fa "struct filestat *fst"
+.Fa "struct shmstat *shm"
+.Fa "char *errbuf"
+.Fc
+.Ft int
+.Fo procstat_get_socket_info
+.Fa "struct procstat *procstat"
+.Fa "struct filestat *fst"
+.Fa "struct sockstat *sock"
+.Fa "char *errbuf"
+.Fc
+.Ft int
+.Fo procstat_get_vnode_info
+.Fa "struct procstat *procstat"
+.Fa "struct filestat *fst"
+.Fa "struct vnstat *vn"
+.Fa "char *errbuf"
+.Fc
+.Ft "struct advlock_list *"
+.Fo procstat_getadvlock
+.Fa "struct procstat *procstat"
+.Fc
+.Ft "char **"
+.Fo procstat_getargv
+.Fa "struct procstat *procstat"
+.Fa "const struct kinfo_proc *kp"
+.Fa "size_t nchr"
+.Fc
+.Ft "Elf_Auxinfo *"
+.Fo procstat_getauxv
+.Fa "struct procstat *procstat"
+.Fa "struct kinfo_proc *kp"
+.Fa "unsigned int *count"
+.Fc
+.Ft "char **"
+.Fo procstat_getenvv
+.Fa "struct procstat *procstat"
+.Fa "const struct kinfo_proc *kp"
+.Fa "size_t nchr"
+.Fc
+.Ft "struct filestat_list *"
+.Fo procstat_getfiles
+.Fa "struct procstat *procstat"
+.Fa "struct kinfo_proc *kp"
+.Fa "int mmapped"
+.Fc
+.Ft "gid_t *"
+.Fo procstat_getgroups
+.Fa "struct procstat *procstat"
+.Fa "struct kinfo_proc *kp"
+.Fa "unsigned int *count"
+.Fc
+.Ft "struct kinfo_kstack *"
+.Fo procstat_getkstack
+.Fa "struct procstat *procstat"
+.Fa "struct kinfo_proc *kp"
+.Fa "unsigned int *count"
+.Fc
+.Ft int
+.Fo procstat_getosrel
+.Fa "struct procstat *procstat"
+.Fa "struct kinfo_proc *kp"
+.Fa "int *osrelp"
+.Fc
+.Ft "int"
+.Fo procstat_getpathname
+.Fa "struct procstat *procstat"
+.Fa "struct kinfo_proc *kp"
+.Fa "char *pathname"
+.Fa "size_t maxlen"
+.Fc
+.Ft "struct kinfo_proc *"
+.Fo procstat_getprocs
+.Fa "struct procstat *procstat"
+.Fa "int what"
+.Fa "int arg"
+.Fa "unsigned int *count"
+.Fc
+.Ft "struct ptrace_lwpinfo *"
+.Fo procstat_getptlwpinfo
+.Fa "struct procstat *procstat"
+.Fa "unsigned int *count"
+.Fc
+.Ft "int"
+.Fo procstat_getrlimit
+.Fa "struct procstat *procstat"
+.Fa "struct kinfo_proc *kp"
+.Fa "int which"
+.Fa "struct rlimit* rlimit"
+.Fc
+.Ft "int"
+.Fo procstat_getumask
+.Fa "struct procstat *procstat"
+.Fa "struct kinfo_proc *kp"
+.Fa "unsigned short *maskp"
+.Fc
+.Ft "struct kinfo_vmentry *"
+.Fo procstat_getvmmap
+.Fa "struct procstat *procstat"
+.Fa "struct kinfo_proc *kp"
+.Fa "unsigned int *count"
+.Fc
+.Ft "struct procstat *"
+.Fn procstat_open_core "const char *filename"
+.Ft "struct procstat *"
+.Fn procstat_open_kvm "const char *nlistf" "const char *memf"
+.Ft "struct procstat *"
+.Fn procstat_open_sysctl void
+.Sh DESCRIPTION
+The
+.Nm libprocstat
+library contains the API for runtime file and process information
+retrieval from the running kernel via the
+.Xr sysctl 3
+library backend, and for post-mortem analysis via the
+.Xr kvm 3
+library backend, or from the process
+.Xr core 5
+file, searching for statistics in special
+.Xr elf 3
+note sections.
+.Pp
+The
+.Fn procstat_open_kvm
+and
+.Fn procstat_open_sysctl
+functions use the
+.Xr kvm 3
+or
+.Xr sysctl 3
+library routines, respectively, to access kernel state information
+used to retrieve processes and files states.
+The
+.Fn procstat_open_core
+uses
+.Xr elf 3
+routines to access statistics stored as a set of notes in a process
+.Xr core 5
+file, written by the kernel at the moment of the process abnormal termination.
+The
+.Fa filename
+argument is the process core file name.
+The
+.Fa nlistf
+argument is the executable image of the kernel being examined.
+If this argument is
+.Dv NULL ,
+the currently running kernel is assumed.
+The
+.Fa memf
+argument is the kernel memory device file.
+If this argument is
+.Dv NULL ,
+then
+.Pa /dev/mem
+is assumed.
+See
+.Xr kvm_open 3
+for more details.
+The functions dynamically allocate and return a
+.Vt procstat
+structure pointer used in the rest of the
+.Nm libprocstat
+library routines until the corresponding
+.Fn procstat_close
+call that cleans up the resources allocated by the
+.Fn procstat_open_*
+functions.
+.Pp
+The
+.Fn procstat_getprocs
+function gets a pointer to the
+.Vt procstat
+structure from one of the
+.Fn procstat_open_*
+functions and returns a dynamically allocated (sub-)set of active processes
+in the kernel filled in to array of
+.Vt kinfo_proc
+structures.
+The
+.Fa what
+and
+.Fa arg
+arguments constitute a filtering predicate as described in the
+.Xr kvm_getprocs 3
+function.
+The number of processes found is returned in the reference parameter
+.Fa cnt .
+The caller is responsible to free the allocated memory with a subsequent
+.Fn procstat_freeprocs
+function call.
+.Pp
+The
+.Fn procstat_getptlwpinfo
+function gets a pointer to the
+.Vt procstat
+structure from the
+.Fn procstat_open_core
+function and returns a dynamically allocated set of signals intercepted by a
+process in the process's core file.
+The number of processes found is returned in the reference parameter
+.Fa cnt .
+The caller is responsible to free the allocated memory with a subsequent
+.Fn procstat_freeptlwpinfo
+function call.
+.Pp
+The
+.Fn procstat_getargv
+function gets a pointer to the
+.Vt procstat
+structure from one of the
+.Fn procstat_open_*
+functions, a pointer to
+.Vt kinfo_proc
+structure from the array obtained from the
+.Fn procstat_getprocs
+function, and returns a null-terminated argument vector that corresponds to
+the command line arguments passed to the process.
+The
+.Fa nchr
+argument indicates the maximum number of characters, including null bytes,
+to use in building the strings.
+If this amount is exceeded, the string causing the overflow is truncated and
+the partial result is returned.
+This is handy for programs that print only a one line summary of a
+command and should not copy out large amounts of text only to ignore it.
+If
+.Fa nchr
+is zero, no limit is imposed and all argument strings are returned.
+The values of the returned argument vector refer the strings stored
+in the
+.Vt procstat
+internal buffer.
+A subsequent call of the function with the same
+.Vt procstat
+argument will reuse the buffer.
+To free the allocated memory
+.Fn procstat_freeargv
+function call can be used, or it will be released on
+.Fn procstat_close .
+.Pp
+The
+.Fn procstat_getenvv
+function is similar to
+.Fn procstat_getargv
+but returns the vector of environment strings.
+The caller may free the allocated memory with a subsequent
+.Fn procstat_freeenvv
+function call.
+.Pp
+The
+.Fn procstat_getauxv
+function gets a pointer to the
+.Vt procstat
+structure, a pointer to
+.Vt kinfo_proc
+structure, and returns the auxiliary vector as a dynamically allocated array of
+.Vt Elf_Auxinfo
+elements.
+The caller is responsible to free the allocated memory with a subsequent
+.Fn procstat_freeauxv
+function call.
+.Pp
+The
+.Fn procstat_getfiles
+function gets a pointer to the
+.Vt procstat
+structure initialized with one of the
+.Fn procstat_open_*
+functions, a pointer to
+.Vt kinfo_proc
+structure from the array obtained from the
+.Fn procstat_getprocs
+function, and returns a dynamically allocated linked list of filled in
+.Vt filestat_list
+structures using the STAILQ macros defined in
+.Xr queue 3 .
+The caller is responsible to free the allocated memory with a subsequent
+.Fn procstat_freefiles
+function call.
+.Pp
+The
+.Fn procstat_getgroups
+function gets a pointer to the
+.Vt procstat
+structure, a pointer to
+.Vt kinfo_proc
+structure, and returns the process groups as a dynamically allocated array of
+.Vt gid_t
+elements.
+The caller is responsible to free the allocated memory with a subsequent
+.Fn procstat_freegroups
+function call.
+.Pp
+The
+.Fn procstat_getkstack
+function gets a pointer to the
+.Vt procstat
+structure initialized with one of the
+.Fn procstat_open_*
+functions, a pointer to
+.Vt kinfo_proc
+structure, and returns kernel stacks of the process as a dynamically allocated
+array of
+.Vt kinfo_kstack
+structures.
+The caller is responsible to free the allocated memory with a subsequent
+.Fn procstat_freekstack
+function call.
+.Pp
+The
+.Fn procstat_getosrel
+function gets a pointer to the
+.Vt procstat
+structure, a pointer to
+.Vt kinfo_proc
+structure, and returns osrel date in the 3rd reference parameter.
+.Pp
+The
+.Fn procstat_getpathname
+function gets a pointer to the
+.Vt procstat
+structure, a pointer to
+.Vt kinfo_proc
+structure, and copies the path of the process executable to
+.Fa pathname
+buffer, limiting to
+.Fa maxlen
+characters.
+.Pp
+The
+.Fn procstat_getrlimit
+function gets a pointer to the
+.Vt procstat
+structure, a pointer to
+.Vt kinfo_proc
+structure, resource index
+.Fa which ,
+and returns the actual resource limit in the 4th reference parameter.
+.Pp
+The
+.Fn procstat_getumask
+function gets a pointer to the
+.Vt procstat
+structure, a pointer to
+.Vt kinfo_proc
+structure, and returns the process umask in the 3rd reference parameter.
+.Pp
+The
+.Fn procstat_getvmmap
+function gets a pointer to the
+.Vt procstat
+structure initialized with one of the
+.Fn procstat_open_*
+functions, a pointer to
+.Vt kinfo_proc
+structure, and returns VM layout of the process as a dynamically allocated
+array of
+.Vt kinfo_vmentry
+structures.
+The caller is responsible to free the allocated memory with a subsequent
+.Fn procstat_freevmmap
+function call.
+.Pp
+The
+.Fn procstat_getadvlock
+function returns a dynamically allocated list of
+.Va struct advlock
+structures, providing a snapshot of the currently
+acquired advisory locks in the system.
+Both locally acquired POSIX (
+.Xr fcntl 2 )
+and BSD-style (
+.Xr flock 2 )
+locks are reported, as well as locks established by remote file
+system protocols.
+For each lock, unique identifiers for the locked file and its mount point
+are guaranteed to be provided.
+If a path for the locked file can be reconstructed, it is provided as well.
+The returned list must be freed with the
+.Fn procstat_freeadvlock
+function.
+.Pp
+The
+.Fn procstat_get_pipe_info ,
+.Fn procstat_get_pts_info ,
+.Fn procstat_get_sem_info ,
+.Fn procstat_get_shm_info ,
+.Fn procstat_get_socket_info
+and
+.Fn procstat_get_vnode_info
+functions are used to retrieve information about pipes, pseudo-terminals,
+semaphores, shared memory objects,
+sockets, and vnodes, respectively.
+Each of them have a similar interface API.
+The
+.Fa procstat
+argument is a pointer obtained from one of
+.Fn procstat_open_*
+functions.
+The
+.Ft filestat Fa fst
+argument is an element of STAILQ linked list as obtained from the
+.Fn procstat_getfiles
+function.
+The
+.Ft filestat
+structure contains a
+.Fa fs_type
+field that specifies a file type and a corresponding function to be
+called among the
+.Nm procstat_get_*_info
+function family.
+The actual object is returned in the 3rd reference parameter.
+The
+.Fa errbuf
+argument indicates an actual error message in case of failure.
+.Pp
+.Bl -tag -width 20n -compact -offset indent
+.It Li PS_FST_TYPE_FIFO
+.Nm procstat_get_vnode_info
+.It Li PS_FST_TYPE_VNODE
+.Nm procstat_get_vnode_info
+.It Li PS_FST_TYPE_SOCKET
+.Nm procstat_get_socket_info
+.It Li PS_FST_TYPE_PIPE
+.Nm procstat_get_pipe_info
+.It Li PS_FST_TYPE_PTS
+.Nm procstat_get_pts_info
+.It Li PS_FST_TYPE_SEM
+.Nm procstat_get_sem_info
+.It Li PS_FST_TYPE_SHM
+.Nm procstat_get_shm_info
+.El
+.Sh SEE ALSO
+.Xr fstat 1 ,
+.Xr fuser 1 ,
+.Xr pipe 2 ,
+.Xr shm_open 2 ,
+.Xr socket 2 ,
+.Xr elf 3 ,
+.Xr kvm 3 ,
+.Xr queue 3 ,
+.Xr sem_open 3 ,
+.Xr sysctl 3 ,
+.Xr pts 4 ,
+.Xr core 5 ,
+.Xr vnode 9
+.Sh HISTORY
+The
+.Nm libprocstat
+library appeared in
+.Fx 9.0 .
+.Sh AUTHORS
+.An -nosplit
+The
+.Nm libprocstat
+library was written by
+.An Stanislav Sedov Aq Mt stas@FreeBSD.org .
+.Pp
+This manual page was written by
+.An Sergey Kandaurov Aq Mt pluknet@FreeBSD.org .
diff --git a/lib/libprocstat/libprocstat.c b/lib/libprocstat/libprocstat.c
new file mode 100644
index 000000000000..df77cabcd36b
--- /dev/null
+++ b/lib/libprocstat/libprocstat.c
@@ -0,0 +1,2922 @@
+/*-
+ * SPDX-License-Identifier: BSD-4-Clause
+ *
+ * Copyright (c) 2017 Dell EMC
+ * Copyright (c) 2009 Stanislav Sedov <stas@FreeBSD.org>
+ * Copyright (c) 1988, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by the University of
+ * California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/param.h>
+#include <sys/elf.h>
+#include <sys/time.h>
+#include <sys/resourcevar.h>
+#define _WANT_UCRED
+#include <sys/ucred.h>
+#undef _WANT_UCRED
+#include <sys/proc.h>
+#include <sys/user.h>
+#include <sys/stat.h>
+#include <sys/vnode.h>
+#include <sys/socket.h>
+#define _WANT_SOCKET
+#include <sys/socketvar.h>
+#include <sys/domain.h>
+#define _WANT_PROTOSW
+#include <sys/protosw.h>
+#include <sys/un.h>
+#define _WANT_UNPCB
+#include <sys/unpcb.h>
+#include <sys/sysctl.h>
+#include <sys/tty.h>
+#include <sys/filedesc.h>
+#include <sys/queue.h>
+#define _WANT_FILE
+#include <sys/file.h>
+#include <sys/conf.h>
+#include <sys/ksem.h>
+#include <sys/mman.h>
+#include <sys/capsicum.h>
+#include <sys/ptrace.h>
+#define _WANT_MOUNT
+#include <sys/mount.h>
+#include <sys/filedesc.h>
+#include <sys/pipe.h>
+#include <fs/devfs/devfs.h>
+#include <fs/devfs/devfs_int.h>
+#include <nfs/nfsproto.h>
+#include <nfsclient/nfs.h>
+#include <nfsclient/nfsnode.h>
+
+#include <vm/vm.h>
+#include <vm/vm_map.h>
+#include <vm/vm_object.h>
+
+#include <net/route.h>
+#include <netinet/in.h>
+#include <netinet/in_systm.h>
+#include <netinet/ip.h>
+
+#include <assert.h>
+#include <ctype.h>
+#include <err.h>
+#include <fcntl.h>
+#include <kvm.h>
+#include <libutil.h>
+#include <limits.h>
+#include <paths.h>
+#include <pwd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stddef.h>
+#include <string.h>
+#include <unistd.h>
+#include <netdb.h>
+
+#include <libprocstat.h>
+#include "libprocstat_internal.h"
+#include "common_kvm.h"
+#include "core.h"
+
+int statfs(const char *, struct statfs *); /* XXX */
+
+#define PROCSTAT_KVM 1
+#define PROCSTAT_SYSCTL 2
+#define PROCSTAT_CORE 3
+
+static char **getargv(struct procstat *procstat, struct kinfo_proc *kp,
+ size_t nchr, int env);
+static char *getmnton(kvm_t *kd, struct mount *m);
+static struct kinfo_vmentry * kinfo_getvmmap_core(struct procstat_core *core,
+ int *cntp);
+static Elf_Auxinfo *procstat_getauxv_core(struct procstat_core *core,
+ unsigned int *cntp);
+static Elf_Auxinfo *procstat_getauxv_sysctl(pid_t pid, unsigned int *cntp);
+static struct filestat_list *procstat_getfiles_kvm(
+ struct procstat *procstat, struct kinfo_proc *kp, int mmapped);
+static struct filestat_list *procstat_getfiles_sysctl(
+ struct procstat *procstat, struct kinfo_proc *kp, int mmapped);
+static int procstat_get_pipe_info_sysctl(struct filestat *fst,
+ struct pipestat *pipe, char *errbuf);
+static int procstat_get_pipe_info_kvm(kvm_t *kd, struct filestat *fst,
+ struct pipestat *pipe, char *errbuf);
+static int procstat_get_pts_info_sysctl(struct filestat *fst,
+ struct ptsstat *pts, char *errbuf);
+static int procstat_get_pts_info_kvm(kvm_t *kd, struct filestat *fst,
+ struct ptsstat *pts, char *errbuf);
+static int procstat_get_sem_info_sysctl(struct filestat *fst,
+ struct semstat *sem, char *errbuf);
+static int procstat_get_sem_info_kvm(kvm_t *kd, struct filestat *fst,
+ struct semstat *sem, char *errbuf);
+static int procstat_get_shm_info_sysctl(struct filestat *fst,
+ struct shmstat *shm, char *errbuf);
+static int procstat_get_shm_info_kvm(kvm_t *kd, struct filestat *fst,
+ struct shmstat *shm, char *errbuf);
+static int procstat_get_socket_info_sysctl(struct filestat *fst,
+ struct sockstat *sock, char *errbuf);
+static int procstat_get_socket_info_kvm(kvm_t *kd, struct filestat *fst,
+ struct sockstat *sock, char *errbuf);
+static int to_filestat_flags(int flags);
+static int procstat_get_vnode_info_kvm(kvm_t *kd, struct filestat *fst,
+ struct vnstat *vn, char *errbuf);
+static int procstat_get_vnode_info_sysctl(struct filestat *fst,
+ struct vnstat *vn, char *errbuf);
+static gid_t *procstat_getgroups_core(struct procstat_core *core,
+ unsigned int *count);
+static gid_t * procstat_getgroups_kvm(kvm_t *kd, struct kinfo_proc *kp,
+ unsigned int *count);
+static gid_t *procstat_getgroups_sysctl(pid_t pid, unsigned int *count);
+static struct kinfo_kstack *procstat_getkstack_sysctl(pid_t pid,
+ int *cntp);
+static int procstat_getosrel_core(struct procstat_core *core,
+ int *osrelp);
+static int procstat_getosrel_kvm(kvm_t *kd, struct kinfo_proc *kp,
+ int *osrelp);
+static int procstat_getosrel_sysctl(pid_t pid, int *osrelp);
+static int procstat_getpathname_core(struct procstat_core *core,
+ char *pathname, size_t maxlen);
+static int procstat_getpathname_sysctl(pid_t pid, char *pathname,
+ size_t maxlen);
+static int procstat_getrlimit_core(struct procstat_core *core, int which,
+ struct rlimit* rlimit);
+static int procstat_getrlimit_kvm(kvm_t *kd, struct kinfo_proc *kp,
+ int which, struct rlimit* rlimit);
+static int procstat_getrlimit_sysctl(pid_t pid, int which,
+ struct rlimit* rlimit);
+static int procstat_getumask_core(struct procstat_core *core,
+ unsigned short *maskp);
+static int procstat_getumask_kvm(kvm_t *kd, struct kinfo_proc *kp,
+ unsigned short *maskp);
+static int procstat_getumask_sysctl(pid_t pid, unsigned short *maskp);
+static int vntype2psfsttype(int type);
+
+void
+procstat_close(struct procstat *procstat)
+{
+
+ assert(procstat);
+ if (procstat->type == PROCSTAT_KVM)
+ kvm_close(procstat->kd);
+ else if (procstat->type == PROCSTAT_CORE)
+ procstat_core_close(procstat->core);
+ procstat_freeargv(procstat);
+ procstat_freeenvv(procstat);
+ free(procstat);
+}
+
+struct procstat *
+procstat_open_sysctl(void)
+{
+ struct procstat *procstat;
+
+ procstat = calloc(1, sizeof(*procstat));
+ if (procstat == NULL) {
+ warn("malloc()");
+ return (NULL);
+ }
+ procstat->type = PROCSTAT_SYSCTL;
+ return (procstat);
+}
+
+struct procstat *
+procstat_open_kvm(const char *nlistf, const char *memf)
+{
+ struct procstat *procstat;
+ kvm_t *kd;
+ char buf[_POSIX2_LINE_MAX];
+
+ procstat = calloc(1, sizeof(*procstat));
+ if (procstat == NULL) {
+ warn("malloc()");
+ return (NULL);
+ }
+ kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf);
+ if (kd == NULL) {
+ warnx("kvm_openfiles(): %s", buf);
+ free(procstat);
+ return (NULL);
+ }
+ procstat->type = PROCSTAT_KVM;
+ procstat->kd = kd;
+ return (procstat);
+}
+
+struct procstat *
+procstat_open_core(const char *filename)
+{
+ struct procstat *procstat;
+ struct procstat_core *core;
+
+ procstat = calloc(1, sizeof(*procstat));
+ if (procstat == NULL) {
+ warn("malloc()");
+ return (NULL);
+ }
+ core = procstat_core_open(filename);
+ if (core == NULL) {
+ free(procstat);
+ return (NULL);
+ }
+ procstat->type = PROCSTAT_CORE;
+ procstat->core = core;
+ return (procstat);
+}
+
+struct kinfo_proc *
+procstat_getprocs(struct procstat *procstat, int what, int arg,
+ unsigned int *count)
+{
+ struct kinfo_proc *p0, *p;
+ size_t len, olen;
+ int name[4];
+ int cnt;
+ int error;
+
+ assert(procstat);
+ assert(count);
+ p = NULL;
+ if (procstat->type == PROCSTAT_KVM) {
+ *count = 0;
+ p0 = kvm_getprocs(procstat->kd, what, arg, &cnt);
+ if (p0 == NULL || cnt <= 0)
+ return (NULL);
+ *count = cnt;
+ len = *count * sizeof(*p);
+ p = malloc(len);
+ if (p == NULL) {
+ warnx("malloc(%zu)", len);
+ goto fail;
+ }
+ bcopy(p0, p, len);
+ return (p);
+ } else if (procstat->type == PROCSTAT_SYSCTL) {
+ len = 0;
+ name[0] = CTL_KERN;
+ name[1] = KERN_PROC;
+ name[2] = what;
+ name[3] = arg;
+ error = sysctl(name, nitems(name), NULL, &len, NULL, 0);
+ if (error < 0 && errno != EPERM) {
+ warn("sysctl(kern.proc)");
+ goto fail;
+ }
+ if (len == 0) {
+ warnx("no processes?");
+ goto fail;
+ }
+ do {
+ len += len / 10;
+ p = reallocf(p, len);
+ if (p == NULL) {
+ warnx("reallocf(%zu)", len);
+ goto fail;
+ }
+ olen = len;
+ error = sysctl(name, nitems(name), p, &len, NULL, 0);
+ } while (error < 0 && errno == ENOMEM && olen == len);
+ if (error < 0 && errno != EPERM) {
+ warn("sysctl(kern.proc)");
+ goto fail;
+ }
+ /* Perform simple consistency checks. */
+ if ((len % sizeof(*p)) != 0 || p->ki_structsize != sizeof(*p)) {
+ warnx("kinfo_proc structure size mismatch (len = %zu)", len);
+ goto fail;
+ }
+ *count = len / sizeof(*p);
+ return (p);
+ } else if (procstat->type == PROCSTAT_CORE) {
+ p = procstat_core_get(procstat->core, PSC_TYPE_PROC, NULL,
+ &len);
+ if ((len % sizeof(*p)) != 0 || p->ki_structsize != sizeof(*p)) {
+ warnx("kinfo_proc structure size mismatch");
+ goto fail;
+ }
+ *count = len / sizeof(*p);
+ return (p);
+ } else {
+ warnx("unknown access method: %d", procstat->type);
+ return (NULL);
+ }
+fail:
+ if (p)
+ free(p);
+ return (NULL);
+}
+
+void
+procstat_freeprocs(struct procstat *procstat __unused, struct kinfo_proc *p)
+{
+
+ if (p != NULL)
+ free(p);
+ p = NULL;
+}
+
+struct filestat_list *
+procstat_getfiles(struct procstat *procstat, struct kinfo_proc *kp, int mmapped)
+{
+
+ switch (procstat->type) {
+ case PROCSTAT_KVM:
+ return (procstat_getfiles_kvm(procstat, kp, mmapped));
+ case PROCSTAT_SYSCTL:
+ case PROCSTAT_CORE:
+ return (procstat_getfiles_sysctl(procstat, kp, mmapped));
+ default:
+ warnx("unknown access method: %d", procstat->type);
+ return (NULL);
+ }
+}
+
+void
+procstat_freefiles(struct procstat *procstat, struct filestat_list *head)
+{
+ struct filestat *fst, *tmp;
+
+ STAILQ_FOREACH_SAFE(fst, head, next, tmp) {
+ if (fst->fs_path != NULL)
+ free(fst->fs_path);
+ free(fst);
+ }
+ free(head);
+ if (procstat->vmentries != NULL) {
+ free(procstat->vmentries);
+ procstat->vmentries = NULL;
+ }
+ if (procstat->files != NULL) {
+ free(procstat->files);
+ procstat->files = NULL;
+ }
+}
+
+static struct filestat *
+filestat_new_entry(void *typedep, int type, int fd, int fflags, int uflags,
+ int refcount, off_t offset, char *path, cap_rights_t *cap_rightsp)
+{
+ struct filestat *entry;
+
+ entry = calloc(1, sizeof(*entry));
+ if (entry == NULL) {
+ warn("malloc()");
+ return (NULL);
+ }
+ entry->fs_typedep = typedep;
+ entry->fs_fflags = fflags;
+ entry->fs_uflags = uflags;
+ entry->fs_fd = fd;
+ entry->fs_type = type;
+ entry->fs_ref_count = refcount;
+ entry->fs_offset = offset;
+ entry->fs_path = path;
+ if (cap_rightsp != NULL)
+ entry->fs_cap_rights = *cap_rightsp;
+ else
+ cap_rights_init(&entry->fs_cap_rights);
+ return (entry);
+}
+
+static struct vnode *
+getctty(kvm_t *kd, struct kinfo_proc *kp)
+{
+ struct pgrp pgrp;
+ struct proc proc;
+ struct session sess;
+ int error;
+
+ assert(kp);
+ error = kvm_read_all(kd, (unsigned long)kp->ki_paddr, &proc,
+ sizeof(proc));
+ if (error == 0) {
+ warnx("can't read proc struct at %p for pid %d",
+ kp->ki_paddr, kp->ki_pid);
+ return (NULL);
+ }
+ if (proc.p_pgrp == NULL)
+ return (NULL);
+ error = kvm_read_all(kd, (unsigned long)proc.p_pgrp, &pgrp,
+ sizeof(pgrp));
+ if (error == 0) {
+ warnx("can't read pgrp struct at %p for pid %d",
+ proc.p_pgrp, kp->ki_pid);
+ return (NULL);
+ }
+ error = kvm_read_all(kd, (unsigned long)pgrp.pg_session, &sess,
+ sizeof(sess));
+ if (error == 0) {
+ warnx("can't read session struct at %p for pid %d",
+ pgrp.pg_session, kp->ki_pid);
+ return (NULL);
+ }
+ return (sess.s_ttyvp);
+}
+
+static int
+procstat_vm_map_reader(void *token, vm_map_entry_t addr, vm_map_entry_t dest)
+{
+ kvm_t *kd;
+
+ kd = (kvm_t *)token;
+ return (kvm_read_all(kd, (unsigned long)addr, dest, sizeof(*dest)));
+}
+
+static struct filestat_list *
+procstat_getfiles_kvm(struct procstat *procstat, struct kinfo_proc *kp, int mmapped)
+{
+ struct file file;
+ struct filedesc filed;
+ struct pwddesc pathsd;
+ struct fdescenttbl *fdt;
+ struct pwd pwd;
+ unsigned long pwd_addr;
+ struct vm_map_entry vmentry;
+ struct vm_object object;
+ struct vmspace vmspace;
+ vm_map_entry_t entryp;
+ vm_object_t objp;
+ struct vnode *vp;
+ struct filestat *entry;
+ struct filestat_list *head;
+ kvm_t *kd;
+ void *data;
+ int fflags;
+ unsigned int i;
+ int prot, type;
+ size_t fdt_size;
+ unsigned int nfiles;
+ bool haspwd;
+
+ assert(procstat);
+ kd = procstat->kd;
+ if (kd == NULL)
+ return (NULL);
+ if (kp->ki_fd == NULL || kp->ki_pd == NULL)
+ return (NULL);
+ if (!kvm_read_all(kd, (unsigned long)kp->ki_fd, &filed,
+ sizeof(filed))) {
+ warnx("can't read filedesc at %p", (void *)kp->ki_fd);
+ return (NULL);
+ }
+ if (!kvm_read_all(kd, (unsigned long)kp->ki_pd, &pathsd,
+ sizeof(pathsd))) {
+ warnx("can't read pwddesc at %p", (void *)kp->ki_pd);
+ return (NULL);
+ }
+ haspwd = false;
+ pwd_addr = (unsigned long)(PWDDESC_KVM_LOAD_PWD(&pathsd));
+ if (pwd_addr != 0) {
+ if (!kvm_read_all(kd, pwd_addr, &pwd, sizeof(pwd))) {
+ warnx("can't read fd_pwd at %p", (void *)pwd_addr);
+ return (NULL);
+ }
+ haspwd = true;
+ }
+
+ /*
+ * Allocate list head.
+ */
+ head = malloc(sizeof(*head));
+ if (head == NULL)
+ return (NULL);
+ STAILQ_INIT(head);
+
+ /* root directory vnode, if one. */
+ if (haspwd) {
+ if (pwd.pwd_rdir) {
+ entry = filestat_new_entry(pwd.pwd_rdir, PS_FST_TYPE_VNODE, -1,
+ PS_FST_FFLAG_READ, PS_FST_UFLAG_RDIR, 0, 0, NULL, NULL);
+ if (entry != NULL)
+ STAILQ_INSERT_TAIL(head, entry, next);
+ }
+ /* current working directory vnode. */
+ if (pwd.pwd_cdir) {
+ entry = filestat_new_entry(pwd.pwd_cdir, PS_FST_TYPE_VNODE, -1,
+ PS_FST_FFLAG_READ, PS_FST_UFLAG_CDIR, 0, 0, NULL, NULL);
+ if (entry != NULL)
+ STAILQ_INSERT_TAIL(head, entry, next);
+ }
+ /* jail root, if any. */
+ if (pwd.pwd_jdir) {
+ entry = filestat_new_entry(pwd.pwd_jdir, PS_FST_TYPE_VNODE, -1,
+ PS_FST_FFLAG_READ, PS_FST_UFLAG_JAIL, 0, 0, NULL, NULL);
+ if (entry != NULL)
+ STAILQ_INSERT_TAIL(head, entry, next);
+ }
+ }
+ /* ktrace vnode, if one */
+ if (kp->ki_tracep) {
+ entry = filestat_new_entry(kp->ki_tracep, PS_FST_TYPE_VNODE, -1,
+ PS_FST_FFLAG_READ | PS_FST_FFLAG_WRITE,
+ PS_FST_UFLAG_TRACE, 0, 0, NULL, NULL);
+ if (entry != NULL)
+ STAILQ_INSERT_TAIL(head, entry, next);
+ }
+ /* text vnode, if one */
+ if (kp->ki_textvp) {
+ entry = filestat_new_entry(kp->ki_textvp, PS_FST_TYPE_VNODE, -1,
+ PS_FST_FFLAG_READ, PS_FST_UFLAG_TEXT, 0, 0, NULL, NULL);
+ if (entry != NULL)
+ STAILQ_INSERT_TAIL(head, entry, next);
+ }
+ /* Controlling terminal. */
+ if ((vp = getctty(kd, kp)) != NULL) {
+ entry = filestat_new_entry(vp, PS_FST_TYPE_VNODE, -1,
+ PS_FST_FFLAG_READ | PS_FST_FFLAG_WRITE,
+ PS_FST_UFLAG_CTTY, 0, 0, NULL, NULL);
+ if (entry != NULL)
+ STAILQ_INSERT_TAIL(head, entry, next);
+ }
+
+ if (!kvm_read_all(kd, (unsigned long)filed.fd_files, &nfiles,
+ sizeof(nfiles))) {
+ warnx("can't read fd_files at %p", (void *)filed.fd_files);
+ return (NULL);
+ }
+
+ fdt_size = sizeof(*fdt) + nfiles * sizeof(struct filedescent);
+ fdt = malloc(fdt_size);
+ if (fdt == NULL) {
+ warn("malloc(%zu)", fdt_size);
+ goto do_mmapped;
+ }
+ if (!kvm_read_all(kd, (unsigned long)filed.fd_files, fdt, fdt_size)) {
+ warnx("cannot read file structures at %p", (void *)filed.fd_files);
+ free(fdt);
+ goto do_mmapped;
+ }
+ for (i = 0; i < nfiles; i++) {
+ if (fdt->fdt_ofiles[i].fde_file == NULL) {
+ continue;
+ }
+ if (!kvm_read_all(kd, (unsigned long)fdt->fdt_ofiles[i].fde_file, &file,
+ sizeof(struct file))) {
+ warnx("can't read file %d at %p", i,
+ (void *)fdt->fdt_ofiles[i].fde_file);
+ continue;
+ }
+ switch (file.f_type) {
+ case DTYPE_VNODE:
+ type = PS_FST_TYPE_VNODE;
+ data = file.f_vnode;
+ break;
+ case DTYPE_SOCKET:
+ type = PS_FST_TYPE_SOCKET;
+ data = file.f_data;
+ break;
+ case DTYPE_PIPE:
+ type = PS_FST_TYPE_PIPE;
+ data = file.f_data;
+ break;
+ case DTYPE_FIFO:
+ type = PS_FST_TYPE_FIFO;
+ data = file.f_vnode;
+ break;
+#ifdef DTYPE_PTS
+ case DTYPE_PTS:
+ type = PS_FST_TYPE_PTS;
+ data = file.f_data;
+ break;
+#endif
+ case DTYPE_SEM:
+ type = PS_FST_TYPE_SEM;
+ data = file.f_data;
+ break;
+ case DTYPE_SHM:
+ type = PS_FST_TYPE_SHM;
+ data = file.f_data;
+ break;
+ case DTYPE_PROCDESC:
+ type = PS_FST_TYPE_PROCDESC;
+ data = file.f_data;
+ break;
+ case DTYPE_DEV:
+ type = PS_FST_TYPE_DEV;
+ data = file.f_data;
+ break;
+ case DTYPE_EVENTFD:
+ type = PS_FST_TYPE_EVENTFD;
+ data = file.f_data;
+ break;
+ case DTYPE_INOTIFY:
+ type = PS_FST_TYPE_INOTIFY;
+ data = file.f_data;
+ break;
+ default:
+ continue;
+ }
+ /* XXXRW: No capability rights support for kvm yet. */
+ entry = filestat_new_entry(data, type, i,
+ to_filestat_flags(file.f_flag), 0, 0, 0, NULL, NULL);
+ if (entry != NULL)
+ STAILQ_INSERT_TAIL(head, entry, next);
+ }
+ free(fdt);
+
+do_mmapped:
+
+ /*
+ * Process mmapped files if requested.
+ */
+ if (mmapped) {
+ if (!kvm_read_all(kd, (unsigned long)kp->ki_vmspace, &vmspace,
+ sizeof(vmspace))) {
+ warnx("can't read vmspace at %p",
+ (void *)kp->ki_vmspace);
+ goto exit;
+ }
+
+ vmentry = vmspace.vm_map.header;
+ for (entryp = vm_map_entry_read_succ(kd, &vmentry, procstat_vm_map_reader);
+ entryp != NULL && entryp != &kp->ki_vmspace->vm_map.header;
+ entryp = vm_map_entry_read_succ(kd, &vmentry, procstat_vm_map_reader)) {
+ if (vmentry.eflags & MAP_ENTRY_IS_SUB_MAP)
+ continue;
+ if ((objp = vmentry.object.vm_object) == NULL)
+ continue;
+ for (; objp; objp = object.backing_object) {
+ if (!kvm_read_all(kd, (unsigned long)objp,
+ &object, sizeof(object))) {
+ warnx("can't read vm_object at %p",
+ (void *)objp);
+ break;
+ }
+ }
+
+ /* We want only vnode objects. */
+ if (object.type != OBJT_VNODE)
+ continue;
+
+ prot = vmentry.protection;
+ fflags = 0;
+ if (prot & VM_PROT_READ)
+ fflags = PS_FST_FFLAG_READ;
+ if ((vmentry.eflags & MAP_ENTRY_COW) == 0 &&
+ prot & VM_PROT_WRITE)
+ fflags |= PS_FST_FFLAG_WRITE;
+
+ /*
+ * Create filestat entry.
+ */
+ entry = filestat_new_entry(object.handle,
+ PS_FST_TYPE_VNODE, -1, fflags,
+ PS_FST_UFLAG_MMAP, 0, 0, NULL, NULL);
+ if (entry != NULL)
+ STAILQ_INSERT_TAIL(head, entry, next);
+ }
+ if (entryp == NULL)
+ warnx("can't read vm_map_entry");
+ }
+exit:
+ return (head);
+}
+
+/*
+ * kinfo types to filestat translation.
+ */
+static int
+kinfo_type2fst(int kftype)
+{
+ static struct {
+ int kf_type;
+ int fst_type;
+ } kftypes2fst[] = {
+ { KF_TYPE_PROCDESC, PS_FST_TYPE_PROCDESC },
+ { KF_TYPE_DEV, PS_FST_TYPE_DEV },
+ { KF_TYPE_FIFO, PS_FST_TYPE_FIFO },
+ { KF_TYPE_KQUEUE, PS_FST_TYPE_KQUEUE },
+ { KF_TYPE_MQUEUE, PS_FST_TYPE_MQUEUE },
+ { KF_TYPE_NONE, PS_FST_TYPE_NONE },
+ { KF_TYPE_PIPE, PS_FST_TYPE_PIPE },
+ { KF_TYPE_PTS, PS_FST_TYPE_PTS },
+ { KF_TYPE_SEM, PS_FST_TYPE_SEM },
+ { KF_TYPE_SHM, PS_FST_TYPE_SHM },
+ { KF_TYPE_SOCKET, PS_FST_TYPE_SOCKET },
+ { KF_TYPE_VNODE, PS_FST_TYPE_VNODE },
+ { KF_TYPE_EVENTFD, PS_FST_TYPE_EVENTFD },
+ { KF_TYPE_INOTIFY, PS_FST_TYPE_INOTIFY },
+ { KF_TYPE_UNKNOWN, PS_FST_TYPE_UNKNOWN }
+ };
+#define NKFTYPES (sizeof(kftypes2fst) / sizeof(*kftypes2fst))
+ unsigned int i;
+
+ for (i = 0; i < NKFTYPES; i++)
+ if (kftypes2fst[i].kf_type == kftype)
+ break;
+ if (i == NKFTYPES)
+ return (PS_FST_TYPE_UNKNOWN);
+ return (kftypes2fst[i].fst_type);
+}
+
+/*
+ * kinfo flags to filestat translation.
+ */
+static int
+kinfo_fflags2fst(int kfflags)
+{
+ static struct {
+ int kf_flag;
+ int fst_flag;
+ } kfflags2fst[] = {
+ { KF_FLAG_APPEND, PS_FST_FFLAG_APPEND },
+ { KF_FLAG_ASYNC, PS_FST_FFLAG_ASYNC },
+ { KF_FLAG_CREAT, PS_FST_FFLAG_CREAT },
+ { KF_FLAG_DIRECT, PS_FST_FFLAG_DIRECT },
+ { KF_FLAG_EXCL, PS_FST_FFLAG_EXCL },
+ { KF_FLAG_EXEC, PS_FST_FFLAG_EXEC },
+ { KF_FLAG_EXLOCK, PS_FST_FFLAG_EXLOCK },
+ { KF_FLAG_FSYNC, PS_FST_FFLAG_SYNC },
+ { KF_FLAG_HASLOCK, PS_FST_FFLAG_HASLOCK },
+ { KF_FLAG_NOFOLLOW, PS_FST_FFLAG_NOFOLLOW },
+ { KF_FLAG_NONBLOCK, PS_FST_FFLAG_NONBLOCK },
+ { KF_FLAG_READ, PS_FST_FFLAG_READ },
+ { KF_FLAG_SHLOCK, PS_FST_FFLAG_SHLOCK },
+ { KF_FLAG_TRUNC, PS_FST_FFLAG_TRUNC },
+ { KF_FLAG_WRITE, PS_FST_FFLAG_WRITE }
+ };
+#define NKFFLAGS (sizeof(kfflags2fst) / sizeof(*kfflags2fst))
+ unsigned int i;
+ int flags;
+
+ flags = 0;
+ for (i = 0; i < NKFFLAGS; i++)
+ if ((kfflags & kfflags2fst[i].kf_flag) != 0)
+ flags |= kfflags2fst[i].fst_flag;
+ return (flags);
+}
+
+static int
+kinfo_uflags2fst(int fd)
+{
+
+ switch (fd) {
+ case KF_FD_TYPE_CTTY:
+ return (PS_FST_UFLAG_CTTY);
+ case KF_FD_TYPE_CWD:
+ return (PS_FST_UFLAG_CDIR);
+ case KF_FD_TYPE_JAIL:
+ return (PS_FST_UFLAG_JAIL);
+ case KF_FD_TYPE_TEXT:
+ return (PS_FST_UFLAG_TEXT);
+ case KF_FD_TYPE_TRACE:
+ return (PS_FST_UFLAG_TRACE);
+ case KF_FD_TYPE_ROOT:
+ return (PS_FST_UFLAG_RDIR);
+ }
+ return (0);
+}
+
+static struct kinfo_file *
+kinfo_getfile_core(struct procstat_core *core, int *cntp)
+{
+ int cnt;
+ size_t len;
+ char *buf, *bp, *eb;
+ struct kinfo_file *kif, *kp, *kf;
+
+ buf = procstat_core_get(core, PSC_TYPE_FILES, NULL, &len);
+ if (buf == NULL)
+ return (NULL);
+ /*
+ * XXXMG: The code below is just copy&past from libutil.
+ * The code duplication can be avoided if libutil
+ * is extended to provide something like:
+ * struct kinfo_file *kinfo_getfile_from_buf(const char *buf,
+ * size_t len, int *cntp);
+ */
+
+ /* Pass 1: count items */
+ cnt = 0;
+ bp = buf;
+ eb = buf + len;
+ while (bp < eb) {
+ kf = (struct kinfo_file *)(uintptr_t)bp;
+ if (kf->kf_structsize == 0)
+ break;
+ bp += kf->kf_structsize;
+ cnt++;
+ }
+
+ kif = calloc(cnt, sizeof(*kif));
+ if (kif == NULL) {
+ free(buf);
+ return (NULL);
+ }
+ bp = buf;
+ eb = buf + len;
+ kp = kif;
+ /* Pass 2: unpack */
+ while (bp < eb) {
+ kf = (struct kinfo_file *)(uintptr_t)bp;
+ if (kf->kf_structsize == 0)
+ break;
+ /* Copy/expand into pre-zeroed buffer */
+ memcpy(kp, kf, kf->kf_structsize);
+ /* Advance to next packed record */
+ bp += kf->kf_structsize;
+ /* Set field size to fixed length, advance */
+ kp->kf_structsize = sizeof(*kp);
+ kp++;
+ }
+ free(buf);
+ *cntp = cnt;
+ return (kif); /* Caller must free() return value */
+}
+
+static struct filestat_list *
+procstat_getfiles_sysctl(struct procstat *procstat, struct kinfo_proc *kp,
+ int mmapped)
+{
+ struct kinfo_file *kif, *files;
+ struct kinfo_vmentry *kve, *vmentries;
+ struct filestat_list *head;
+ struct filestat *entry;
+ char *path;
+ off_t offset;
+ int cnt, fd, fflags;
+ int i, type, uflags;
+ int refcount;
+ cap_rights_t cap_rights;
+
+ assert(kp);
+ switch (procstat->type) {
+ case PROCSTAT_SYSCTL:
+ files = kinfo_getfile(kp->ki_pid, &cnt);
+ break;
+ case PROCSTAT_CORE:
+ files = kinfo_getfile_core(procstat->core, &cnt);
+ break;
+ default:
+ assert(!"invalid type");
+ }
+ if (files == NULL && errno != EPERM) {
+ warn("kinfo_getfile()");
+ return (NULL);
+ }
+ procstat->files = files;
+
+ /*
+ * Allocate list head.
+ */
+ head = malloc(sizeof(*head));
+ if (head == NULL)
+ return (NULL);
+ STAILQ_INIT(head);
+ for (i = 0; i < cnt; i++) {
+ kif = &files[i];
+
+ type = kinfo_type2fst(kif->kf_type);
+ fd = kif->kf_fd >= 0 ? kif->kf_fd : -1;
+ fflags = kinfo_fflags2fst(kif->kf_flags);
+ uflags = kinfo_uflags2fst(kif->kf_fd);
+ refcount = kif->kf_ref_count;
+ offset = kif->kf_offset;
+ if (*kif->kf_path != '\0')
+ path = strdup(kif->kf_path);
+ else
+ path = NULL;
+ cap_rights = kif->kf_cap_rights;
+
+ /*
+ * Create filestat entry.
+ */
+ entry = filestat_new_entry(kif, type, fd, fflags, uflags,
+ refcount, offset, path, &cap_rights);
+ if (entry != NULL)
+ STAILQ_INSERT_TAIL(head, entry, next);
+ }
+ if (mmapped != 0) {
+ vmentries = procstat_getvmmap(procstat, kp, &cnt);
+ procstat->vmentries = vmentries;
+ if (vmentries == NULL || cnt == 0)
+ goto fail;
+ for (i = 0; i < cnt; i++) {
+ kve = &vmentries[i];
+ if (kve->kve_type != KVME_TYPE_VNODE)
+ continue;
+ fflags = 0;
+ if (kve->kve_protection & KVME_PROT_READ)
+ fflags = PS_FST_FFLAG_READ;
+ if ((kve->kve_flags & KVME_FLAG_COW) == 0 &&
+ kve->kve_protection & KVME_PROT_WRITE)
+ fflags |= PS_FST_FFLAG_WRITE;
+ offset = kve->kve_offset;
+ refcount = kve->kve_ref_count;
+ if (*kve->kve_path != '\0')
+ path = strdup(kve->kve_path);
+ else
+ path = NULL;
+ entry = filestat_new_entry(kve, PS_FST_TYPE_VNODE, -1,
+ fflags, PS_FST_UFLAG_MMAP, refcount, offset, path,
+ NULL);
+ if (entry != NULL)
+ STAILQ_INSERT_TAIL(head, entry, next);
+ }
+ }
+fail:
+ return (head);
+}
+
+int
+procstat_get_pipe_info(struct procstat *procstat, struct filestat *fst,
+ struct pipestat *ps, char *errbuf)
+{
+
+ assert(ps);
+ if (procstat->type == PROCSTAT_KVM) {
+ return (procstat_get_pipe_info_kvm(procstat->kd, fst, ps,
+ errbuf));
+ } else if (procstat->type == PROCSTAT_SYSCTL ||
+ procstat->type == PROCSTAT_CORE) {
+ return (procstat_get_pipe_info_sysctl(fst, ps, errbuf));
+ } else {
+ warnx("unknown access method: %d", procstat->type);
+ if (errbuf != NULL)
+ snprintf(errbuf, _POSIX2_LINE_MAX, "error");
+ return (1);
+ }
+}
+
+static int
+procstat_get_pipe_info_kvm(kvm_t *kd, struct filestat *fst,
+ struct pipestat *ps, char *errbuf)
+{
+ struct pipe pi;
+ void *pipep;
+
+ assert(kd);
+ assert(ps);
+ assert(fst);
+ bzero(ps, sizeof(*ps));
+ pipep = fst->fs_typedep;
+ if (pipep == NULL)
+ goto fail;
+ if (!kvm_read_all(kd, (unsigned long)pipep, &pi, sizeof(struct pipe))) {
+ warnx("can't read pipe at %p", (void *)pipep);
+ goto fail;
+ }
+ ps->addr = (uintptr_t)pipep;
+ ps->peer = (uintptr_t)pi.pipe_peer;
+ ps->buffer_cnt = pi.pipe_buffer.cnt;
+ return (0);
+
+fail:
+ if (errbuf != NULL)
+ snprintf(errbuf, _POSIX2_LINE_MAX, "error");
+ return (1);
+}
+
+static int
+procstat_get_pipe_info_sysctl(struct filestat *fst, struct pipestat *ps,
+ char *errbuf __unused)
+{
+ struct kinfo_file *kif;
+
+ assert(ps);
+ assert(fst);
+ bzero(ps, sizeof(*ps));
+ kif = fst->fs_typedep;
+ if (kif == NULL)
+ return (1);
+ ps->addr = kif->kf_un.kf_pipe.kf_pipe_addr;
+ ps->peer = kif->kf_un.kf_pipe.kf_pipe_peer;
+ ps->buffer_cnt = kif->kf_un.kf_pipe.kf_pipe_buffer_cnt;
+ return (0);
+}
+
+int
+procstat_get_pts_info(struct procstat *procstat, struct filestat *fst,
+ struct ptsstat *pts, char *errbuf)
+{
+
+ assert(pts);
+ if (procstat->type == PROCSTAT_KVM) {
+ return (procstat_get_pts_info_kvm(procstat->kd, fst, pts,
+ errbuf));
+ } else if (procstat->type == PROCSTAT_SYSCTL ||
+ procstat->type == PROCSTAT_CORE) {
+ return (procstat_get_pts_info_sysctl(fst, pts, errbuf));
+ } else {
+ warnx("unknown access method: %d", procstat->type);
+ if (errbuf != NULL)
+ snprintf(errbuf, _POSIX2_LINE_MAX, "error");
+ return (1);
+ }
+}
+
+static int
+procstat_get_pts_info_kvm(kvm_t *kd, struct filestat *fst,
+ struct ptsstat *pts, char *errbuf)
+{
+ struct tty tty;
+ void *ttyp;
+
+ assert(kd);
+ assert(pts);
+ assert(fst);
+ bzero(pts, sizeof(*pts));
+ ttyp = fst->fs_typedep;
+ if (ttyp == NULL)
+ goto fail;
+ if (!kvm_read_all(kd, (unsigned long)ttyp, &tty, sizeof(struct tty))) {
+ warnx("can't read tty at %p", (void *)ttyp);
+ goto fail;
+ }
+ pts->dev = dev2udev(kd, tty.t_dev);
+ (void)kdevtoname(kd, tty.t_dev, pts->devname);
+ return (0);
+
+fail:
+ if (errbuf != NULL)
+ snprintf(errbuf, _POSIX2_LINE_MAX, "error");
+ return (1);
+}
+
+static int
+procstat_get_pts_info_sysctl(struct filestat *fst, struct ptsstat *pts,
+ char *errbuf __unused)
+{
+ struct kinfo_file *kif;
+
+ assert(pts);
+ assert(fst);
+ bzero(pts, sizeof(*pts));
+ kif = fst->fs_typedep;
+ if (kif == NULL)
+ return (0);
+ pts->dev = kif->kf_un.kf_pts.kf_pts_dev;
+ strlcpy(pts->devname, kif->kf_path, sizeof(pts->devname));
+ return (0);
+}
+
+int
+procstat_get_sem_info(struct procstat *procstat, struct filestat *fst,
+ struct semstat *sem, char *errbuf)
+{
+
+ assert(sem);
+ if (procstat->type == PROCSTAT_KVM) {
+ return (procstat_get_sem_info_kvm(procstat->kd, fst, sem,
+ errbuf));
+ } else if (procstat->type == PROCSTAT_SYSCTL ||
+ procstat->type == PROCSTAT_CORE) {
+ return (procstat_get_sem_info_sysctl(fst, sem, errbuf));
+ } else {
+ warnx("unknown access method: %d", procstat->type);
+ if (errbuf != NULL)
+ snprintf(errbuf, _POSIX2_LINE_MAX, "error");
+ return (1);
+ }
+}
+
+static int
+procstat_get_sem_info_kvm(kvm_t *kd, struct filestat *fst,
+ struct semstat *sem, char *errbuf)
+{
+ struct ksem ksem;
+ void *ksemp;
+ char *path;
+ int i;
+
+ assert(kd);
+ assert(sem);
+ assert(fst);
+ bzero(sem, sizeof(*sem));
+ ksemp = fst->fs_typedep;
+ if (ksemp == NULL)
+ goto fail;
+ if (!kvm_read_all(kd, (unsigned long)ksemp, &ksem,
+ sizeof(struct ksem))) {
+ warnx("can't read ksem at %p", (void *)ksemp);
+ goto fail;
+ }
+ sem->mode = S_IFREG | ksem.ks_mode;
+ sem->value = ksem.ks_value;
+ if (fst->fs_path == NULL && ksem.ks_path != NULL) {
+ path = malloc(MAXPATHLEN);
+ for (i = 0; i < MAXPATHLEN - 1; i++) {
+ if (!kvm_read_all(kd, (unsigned long)ksem.ks_path + i,
+ path + i, 1))
+ break;
+ if (path[i] == '\0')
+ break;
+ }
+ path[i] = '\0';
+ if (i == 0)
+ free(path);
+ else
+ fst->fs_path = path;
+ }
+ return (0);
+
+fail:
+ if (errbuf != NULL)
+ snprintf(errbuf, _POSIX2_LINE_MAX, "error");
+ return (1);
+}
+
+static int
+procstat_get_sem_info_sysctl(struct filestat *fst, struct semstat *sem,
+ char *errbuf __unused)
+{
+ struct kinfo_file *kif;
+
+ assert(sem);
+ assert(fst);
+ bzero(sem, sizeof(*sem));
+ kif = fst->fs_typedep;
+ if (kif == NULL)
+ return (0);
+ sem->value = kif->kf_un.kf_sem.kf_sem_value;
+ sem->mode = kif->kf_un.kf_sem.kf_sem_mode;
+ return (0);
+}
+
+int
+procstat_get_shm_info(struct procstat *procstat, struct filestat *fst,
+ struct shmstat *shm, char *errbuf)
+{
+
+ assert(shm);
+ if (procstat->type == PROCSTAT_KVM) {
+ return (procstat_get_shm_info_kvm(procstat->kd, fst, shm,
+ errbuf));
+ } else if (procstat->type == PROCSTAT_SYSCTL ||
+ procstat->type == PROCSTAT_CORE) {
+ return (procstat_get_shm_info_sysctl(fst, shm, errbuf));
+ } else {
+ warnx("unknown access method: %d", procstat->type);
+ if (errbuf != NULL)
+ snprintf(errbuf, _POSIX2_LINE_MAX, "error");
+ return (1);
+ }
+}
+
+static int
+procstat_get_shm_info_kvm(kvm_t *kd, struct filestat *fst,
+ struct shmstat *shm, char *errbuf)
+{
+ struct shmfd shmfd;
+ void *shmfdp;
+ char *path;
+ int i;
+
+ assert(kd);
+ assert(shm);
+ assert(fst);
+ bzero(shm, sizeof(*shm));
+ shmfdp = fst->fs_typedep;
+ if (shmfdp == NULL)
+ goto fail;
+ if (!kvm_read_all(kd, (unsigned long)shmfdp, &shmfd,
+ sizeof(struct shmfd))) {
+ warnx("can't read shmfd at %p", (void *)shmfdp);
+ goto fail;
+ }
+ shm->mode = S_IFREG | shmfd.shm_mode;
+ shm->size = shmfd.shm_size;
+ if (fst->fs_path == NULL && shmfd.shm_path != NULL) {
+ path = malloc(MAXPATHLEN);
+ for (i = 0; i < MAXPATHLEN - 1; i++) {
+ if (!kvm_read_all(kd, (unsigned long)shmfd.shm_path + i,
+ path + i, 1))
+ break;
+ if (path[i] == '\0')
+ break;
+ }
+ path[i] = '\0';
+ if (i == 0)
+ free(path);
+ else
+ fst->fs_path = path;
+ }
+ return (0);
+
+fail:
+ if (errbuf != NULL)
+ snprintf(errbuf, _POSIX2_LINE_MAX, "error");
+ return (1);
+}
+
+static int
+procstat_get_shm_info_sysctl(struct filestat *fst, struct shmstat *shm,
+ char *errbuf __unused)
+{
+ struct kinfo_file *kif;
+
+ assert(shm);
+ assert(fst);
+ bzero(shm, sizeof(*shm));
+ kif = fst->fs_typedep;
+ if (kif == NULL)
+ return (0);
+ shm->size = kif->kf_un.kf_file.kf_file_size;
+ shm->mode = kif->kf_un.kf_file.kf_file_mode;
+ return (0);
+}
+
+int
+procstat_get_vnode_info(struct procstat *procstat, struct filestat *fst,
+ struct vnstat *vn, char *errbuf)
+{
+
+ assert(vn);
+ if (procstat->type == PROCSTAT_KVM) {
+ return (procstat_get_vnode_info_kvm(procstat->kd, fst, vn,
+ errbuf));
+ } else if (procstat->type == PROCSTAT_SYSCTL ||
+ procstat->type == PROCSTAT_CORE) {
+ return (procstat_get_vnode_info_sysctl(fst, vn, errbuf));
+ } else {
+ warnx("unknown access method: %d", procstat->type);
+ if (errbuf != NULL)
+ snprintf(errbuf, _POSIX2_LINE_MAX, "error");
+ return (1);
+ }
+}
+
+static int
+procstat_get_vnode_info_kvm(kvm_t *kd, struct filestat *fst,
+ struct vnstat *vn, char *errbuf)
+{
+ /* Filesystem specific handlers. */
+ #define FSTYPE(fst) {#fst, fst##_filestat}
+ struct {
+ const char *tag;
+ int (*handler)(kvm_t *kd, struct vnode *vp,
+ struct vnstat *vn);
+ } fstypes[] = {
+ FSTYPE(devfs),
+ FSTYPE(isofs),
+ FSTYPE(msdosfs),
+ FSTYPE(nfs),
+ FSTYPE(smbfs),
+ FSTYPE(udf),
+ FSTYPE(ufs),
+#ifdef LIBPROCSTAT_ZFS
+ FSTYPE(zfs),
+#endif
+ };
+#define NTYPES (sizeof(fstypes) / sizeof(*fstypes))
+ struct vnode vnode;
+ char tagstr[12];
+ void *vp;
+ int error;
+ unsigned int i;
+
+ assert(kd);
+ assert(vn);
+ assert(fst);
+ vp = fst->fs_typedep;
+ if (vp == NULL)
+ goto fail;
+ error = kvm_read_all(kd, (unsigned long)vp, &vnode, sizeof(vnode));
+ if (error == 0) {
+ warnx("can't read vnode at %p", (void *)vp);
+ goto fail;
+ }
+ bzero(vn, sizeof(*vn));
+ vn->vn_type = vntype2psfsttype(vnode.v_type);
+ if (vnode.v_type == VNON || vnode.v_type == VBAD)
+ return (0);
+ error = kvm_read_all(kd, (unsigned long)vnode.v_lock.lock_object.lo_name,
+ tagstr, sizeof(tagstr));
+ if (error == 0) {
+ warnx("can't read lo_name at %p", (void *)vp);
+ goto fail;
+ }
+ tagstr[sizeof(tagstr) - 1] = '\0';
+
+ /*
+ * Find appropriate handler.
+ */
+ for (i = 0; i < NTYPES; i++)
+ if (!strcmp(fstypes[i].tag, tagstr)) {
+ if (fstypes[i].handler(kd, &vnode, vn) != 0) {
+ goto fail;
+ }
+ break;
+ }
+ if (i == NTYPES) {
+ if (errbuf != NULL)
+ snprintf(errbuf, _POSIX2_LINE_MAX, "?(%s)", tagstr);
+ return (1);
+ }
+ vn->vn_mntdir = getmnton(kd, vnode.v_mount);
+ if (VTYPE_ISDEV(vnode.v_type) && vnode.v_rdev != NULL) {
+ vn->vn_dev = dev2udev(kd, vnode.v_rdev);
+ (void)kdevtoname(kd, vnode.v_rdev, vn->vn_devname);
+ } else {
+ vn->vn_dev = -1;
+ }
+ return (0);
+
+fail:
+ if (errbuf != NULL)
+ snprintf(errbuf, _POSIX2_LINE_MAX, "error");
+ return (1);
+}
+
+/*
+ * kinfo vnode type to filestat translation.
+ */
+static int
+kinfo_vtype2fst(int kfvtype)
+{
+ static struct {
+ int kf_vtype;
+ int fst_vtype;
+ } kfvtypes2fst[] = {
+ { KF_VTYPE_VBAD, PS_FST_VTYPE_VBAD },
+ { KF_VTYPE_VBLK, PS_FST_VTYPE_VBLK },
+ { KF_VTYPE_VCHR, PS_FST_VTYPE_VCHR },
+ { KF_VTYPE_VDIR, PS_FST_VTYPE_VDIR },
+ { KF_VTYPE_VFIFO, PS_FST_VTYPE_VFIFO },
+ { KF_VTYPE_VLNK, PS_FST_VTYPE_VLNK },
+ { KF_VTYPE_VNON, PS_FST_VTYPE_VNON },
+ { KF_VTYPE_VREG, PS_FST_VTYPE_VREG },
+ { KF_VTYPE_VSOCK, PS_FST_VTYPE_VSOCK }
+ };
+#define NKFVTYPES (sizeof(kfvtypes2fst) / sizeof(*kfvtypes2fst))
+ unsigned int i;
+
+ for (i = 0; i < NKFVTYPES; i++)
+ if (kfvtypes2fst[i].kf_vtype == kfvtype)
+ break;
+ if (i == NKFVTYPES)
+ return (PS_FST_VTYPE_UNKNOWN);
+ return (kfvtypes2fst[i].fst_vtype);
+}
+
+static int
+procstat_get_vnode_info_sysctl(struct filestat *fst, struct vnstat *vn,
+ char *errbuf)
+{
+ struct statfs stbuf;
+ struct kinfo_file *kif;
+ struct kinfo_vmentry *kve;
+ char *name, *path;
+ uint64_t fileid;
+ uint64_t size;
+ uint64_t fsid;
+ uint64_t rdev;
+ uint16_t mode;
+ int vntype;
+ int status;
+
+ assert(fst);
+ assert(vn);
+ bzero(vn, sizeof(*vn));
+ if (fst->fs_typedep == NULL)
+ return (1);
+ if (fst->fs_uflags & PS_FST_UFLAG_MMAP) {
+ kve = fst->fs_typedep;
+ fileid = kve->kve_vn_fileid;
+ fsid = kve->kve_vn_fsid;
+ mode = kve->kve_vn_mode;
+ path = kve->kve_path;
+ rdev = kve->kve_vn_rdev;
+ size = kve->kve_vn_size;
+ vntype = kinfo_vtype2fst(kve->kve_vn_type);
+ status = kve->kve_status;
+ } else {
+ kif = fst->fs_typedep;
+ fileid = kif->kf_un.kf_file.kf_file_fileid;
+ fsid = kif->kf_un.kf_file.kf_file_fsid;
+ mode = kif->kf_un.kf_file.kf_file_mode;
+ path = kif->kf_path;
+ rdev = kif->kf_un.kf_file.kf_file_rdev;
+ size = kif->kf_un.kf_file.kf_file_size;
+ vntype = kinfo_vtype2fst(kif->kf_vnode_type);
+ status = kif->kf_status;
+ }
+ vn->vn_type = vntype;
+ if (vntype == PS_FST_VTYPE_VNON || vntype == PS_FST_VTYPE_VBAD)
+ return (0);
+ if ((status & KF_ATTR_VALID) == 0) {
+ if (errbuf != NULL) {
+ snprintf(errbuf, _POSIX2_LINE_MAX,
+ "? (no info available)");
+ }
+ return (1);
+ }
+ if (path && *path) {
+ statfs(path, &stbuf);
+ vn->vn_mntdir = strdup(stbuf.f_mntonname);
+ } else
+ vn->vn_mntdir = strdup("-");
+ vn->vn_dev = rdev;
+ if (vntype == PS_FST_VTYPE_VBLK) {
+ name = devname(rdev, S_IFBLK);
+ if (name != NULL)
+ strlcpy(vn->vn_devname, name,
+ sizeof(vn->vn_devname));
+ } else if (vntype == PS_FST_VTYPE_VCHR) {
+ name = devname(vn->vn_dev, S_IFCHR);
+ if (name != NULL)
+ strlcpy(vn->vn_devname, name,
+ sizeof(vn->vn_devname));
+ }
+ vn->vn_fsid = fsid;
+ vn->vn_fileid = fileid;
+ vn->vn_size = size;
+ vn->vn_mode = mode;
+ return (0);
+}
+
+int
+procstat_get_socket_info(struct procstat *procstat, struct filestat *fst,
+ struct sockstat *sock, char *errbuf)
+{
+
+ assert(sock);
+ if (procstat->type == PROCSTAT_KVM) {
+ return (procstat_get_socket_info_kvm(procstat->kd, fst, sock,
+ errbuf));
+ } else if (procstat->type == PROCSTAT_SYSCTL ||
+ procstat->type == PROCSTAT_CORE) {
+ return (procstat_get_socket_info_sysctl(fst, sock, errbuf));
+ } else {
+ warnx("unknown access method: %d", procstat->type);
+ if (errbuf != NULL)
+ snprintf(errbuf, _POSIX2_LINE_MAX, "error");
+ return (1);
+ }
+}
+
+static int
+procstat_get_socket_info_kvm(kvm_t *kd, struct filestat *fst,
+ struct sockstat *sock, char *errbuf)
+{
+ struct domain dom;
+ struct protosw proto;
+ struct socket s;
+ struct unpcb unpcb;
+ ssize_t len;
+ void *so;
+
+ assert(kd);
+ assert(sock);
+ assert(fst);
+ bzero(sock, sizeof(*sock));
+ so = fst->fs_typedep;
+ if (so == NULL)
+ goto fail;
+ sock->so_addr = (uintptr_t)so;
+ /* fill in socket */
+ if (!kvm_read_all(kd, (unsigned long)so, &s,
+ sizeof(struct socket))) {
+ warnx("can't read sock at %p", (void *)so);
+ goto fail;
+ }
+ /* fill in protosw entry */
+ if (!kvm_read_all(kd, (unsigned long)s.so_proto, &proto,
+ sizeof(struct protosw))) {
+ warnx("can't read protosw at %p", (void *)s.so_proto);
+ goto fail;
+ }
+ /* fill in domain */
+ if (!kvm_read_all(kd, (unsigned long)proto.pr_domain, &dom,
+ sizeof(struct domain))) {
+ warnx("can't read domain at %p",
+ (void *)proto.pr_domain);
+ goto fail;
+ }
+ if ((len = kvm_read(kd, (unsigned long)dom.dom_name, sock->dname,
+ sizeof(sock->dname) - 1)) < 0) {
+ warnx("can't read domain name at %p", (void *)dom.dom_name);
+ sock->dname[0] = '\0';
+ }
+ else
+ sock->dname[len] = '\0';
+
+ /*
+ * Fill in known data.
+ */
+ sock->type = s.so_type;
+ sock->proto = proto.pr_protocol;
+ sock->dom_family = dom.dom_family;
+ sock->so_pcb = (uintptr_t)s.so_pcb;
+ sock->sendq = s.so_snd.sb_ccc;
+ sock->recvq = s.so_rcv.sb_ccc;
+ sock->so_rcv_sb_state = s.so_rcv.sb_state;
+ sock->so_snd_sb_state = s.so_snd.sb_state;
+
+ /*
+ * Protocol specific data.
+ */
+ switch (dom.dom_family) {
+ case AF_UNIX:
+ if (s.so_pcb) {
+ if (kvm_read(kd, (u_long)s.so_pcb, (char *)&unpcb,
+ sizeof(struct unpcb)) != sizeof(struct unpcb)){
+ warnx("can't read unpcb at %p",
+ (void *)s.so_pcb);
+ } else if (unpcb.unp_conn) {
+ sock->unp_conn = (uintptr_t)unpcb.unp_conn;
+ }
+ }
+ break;
+ default:
+ break;
+ }
+ return (0);
+
+fail:
+ if (errbuf != NULL)
+ snprintf(errbuf, _POSIX2_LINE_MAX, "error");
+ return (1);
+}
+
+static int
+procstat_get_socket_info_sysctl(struct filestat *fst, struct sockstat *sock,
+ char *errbuf __unused)
+{
+ struct kinfo_file *kif;
+
+ assert(sock);
+ assert(fst);
+ bzero(sock, sizeof(*sock));
+ kif = fst->fs_typedep;
+ if (kif == NULL)
+ return (0);
+
+ /*
+ * Fill in known data.
+ */
+ sock->type = kif->kf_sock_type;
+ sock->proto = kif->kf_sock_protocol;
+ sock->dom_family = kif->kf_sock_domain;
+ sock->so_pcb = kif->kf_un.kf_sock.kf_sock_pcb;
+ strlcpy(sock->dname, kif->kf_path, sizeof(sock->dname));
+ bcopy(&kif->kf_un.kf_sock.kf_sa_local, &sock->sa_local,
+ kif->kf_un.kf_sock.kf_sa_local.ss_len);
+ bcopy(&kif->kf_un.kf_sock.kf_sa_peer, &sock->sa_peer,
+ kif->kf_un.kf_sock.kf_sa_peer.ss_len);
+
+ /*
+ * Protocol specific data.
+ */
+ switch (sock->dom_family) {
+ case AF_INET:
+ case AF_INET6:
+ if (sock->proto == IPPROTO_TCP) {
+ sock->sendq = kif->kf_un.kf_sock.kf_sock_sendq;
+ sock->recvq = kif->kf_un.kf_sock.kf_sock_recvq;
+ }
+ break;
+ case AF_UNIX:
+ if (kif->kf_un.kf_sock.kf_sock_unpconn != 0) {
+ sock->so_rcv_sb_state =
+ kif->kf_un.kf_sock.kf_sock_rcv_sb_state;
+ sock->so_snd_sb_state =
+ kif->kf_un.kf_sock.kf_sock_snd_sb_state;
+ sock->unp_conn =
+ kif->kf_un.kf_sock.kf_sock_unpconn;
+ sock->sendq = kif->kf_un.kf_sock.kf_sock_sendq;
+ sock->recvq = kif->kf_un.kf_sock.kf_sock_recvq;
+ }
+ break;
+ default:
+ break;
+ }
+ return (0);
+}
+
+/*
+ * Descriptor flags to filestat translation.
+ */
+static int
+to_filestat_flags(int flags)
+{
+ static struct {
+ int flag;
+ int fst_flag;
+ } fstflags[] = {
+ { FREAD, PS_FST_FFLAG_READ },
+ { FWRITE, PS_FST_FFLAG_WRITE },
+ { O_APPEND, PS_FST_FFLAG_APPEND },
+ { O_ASYNC, PS_FST_FFLAG_ASYNC },
+ { O_CREAT, PS_FST_FFLAG_CREAT },
+ { O_DIRECT, PS_FST_FFLAG_DIRECT },
+ { O_EXCL, PS_FST_FFLAG_EXCL },
+ { O_EXEC, PS_FST_FFLAG_EXEC },
+ { O_EXLOCK, PS_FST_FFLAG_EXLOCK },
+ { O_NOFOLLOW, PS_FST_FFLAG_NOFOLLOW },
+ { O_NONBLOCK, PS_FST_FFLAG_NONBLOCK },
+ { O_SHLOCK, PS_FST_FFLAG_SHLOCK },
+ { O_SYNC, PS_FST_FFLAG_SYNC },
+ { O_TRUNC, PS_FST_FFLAG_TRUNC }
+ };
+#define NFSTFLAGS (sizeof(fstflags) / sizeof(*fstflags))
+ int fst_flags;
+ unsigned int i;
+
+ fst_flags = 0;
+ for (i = 0; i < NFSTFLAGS; i++)
+ if (flags & fstflags[i].flag)
+ fst_flags |= fstflags[i].fst_flag;
+ return (fst_flags);
+}
+
+/*
+ * Vnode type to filestate translation.
+ */
+static int
+vntype2psfsttype(int type)
+{
+ static struct {
+ int vtype;
+ int fst_vtype;
+ } vt2fst[] = {
+ { VBAD, PS_FST_VTYPE_VBAD },
+ { VBLK, PS_FST_VTYPE_VBLK },
+ { VCHR, PS_FST_VTYPE_VCHR },
+ { VDIR, PS_FST_VTYPE_VDIR },
+ { VFIFO, PS_FST_VTYPE_VFIFO },
+ { VLNK, PS_FST_VTYPE_VLNK },
+ { VNON, PS_FST_VTYPE_VNON },
+ { VREG, PS_FST_VTYPE_VREG },
+ { VSOCK, PS_FST_VTYPE_VSOCK }
+ };
+#define NVFTYPES (sizeof(vt2fst) / sizeof(*vt2fst))
+ unsigned int i, fst_type;
+
+ fst_type = PS_FST_VTYPE_UNKNOWN;
+ for (i = 0; i < NVFTYPES; i++) {
+ if (type == vt2fst[i].vtype) {
+ fst_type = vt2fst[i].fst_vtype;
+ break;
+ }
+ }
+ return (fst_type);
+}
+
+static char *
+getmnton(kvm_t *kd, struct mount *m)
+{
+ struct mount mnt;
+ static struct mtab {
+ struct mtab *next;
+ struct mount *m;
+ char mntonname[MNAMELEN + 1];
+ } *mhead = NULL;
+ struct mtab *mt;
+
+ for (mt = mhead; mt != NULL; mt = mt->next)
+ if (m == mt->m)
+ return (mt->mntonname);
+ if (!kvm_read_all(kd, (unsigned long)m, &mnt, sizeof(struct mount))) {
+ warnx("can't read mount table at %p", (void *)m);
+ return (NULL);
+ }
+ if ((mt = malloc(sizeof (struct mtab))) == NULL)
+ err(1, NULL);
+ mt->m = m;
+ bcopy(&mnt.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN);
+ mt->mntonname[MNAMELEN] = '\0';
+ mt->next = mhead;
+ mhead = mt;
+ return (mt->mntonname);
+}
+
+/*
+ * Auxiliary structures and functions to get process environment or
+ * command line arguments.
+ */
+struct argvec {
+ char *buf;
+ size_t bufsize;
+ char **argv;
+ size_t argc;
+};
+
+static struct argvec *
+argvec_alloc(size_t bufsize)
+{
+ struct argvec *av;
+
+ av = malloc(sizeof(*av));
+ if (av == NULL)
+ return (NULL);
+ av->bufsize = bufsize;
+ av->buf = malloc(av->bufsize);
+ if (av->buf == NULL) {
+ free(av);
+ return (NULL);
+ }
+ av->argc = 32;
+ av->argv = malloc(sizeof(char *) * av->argc);
+ if (av->argv == NULL) {
+ free(av->buf);
+ free(av);
+ return (NULL);
+ }
+ return av;
+}
+
+static void
+argvec_free(struct argvec * av)
+{
+
+ free(av->argv);
+ free(av->buf);
+ free(av);
+}
+
+static char **
+getargv(struct procstat *procstat, struct kinfo_proc *kp, size_t nchr, int env)
+{
+ int error, name[4], argc, i;
+ struct argvec *av, **avp;
+ enum psc_type type;
+ size_t len;
+ char *p, **argv;
+
+ assert(procstat);
+ assert(kp);
+ if (procstat->type == PROCSTAT_KVM) {
+ warnx("can't use kvm access method");
+ return (NULL);
+ }
+ if (procstat->type != PROCSTAT_SYSCTL &&
+ procstat->type != PROCSTAT_CORE) {
+ warnx("unknown access method: %d", procstat->type);
+ return (NULL);
+ }
+
+ if (nchr == 0 || nchr > ARG_MAX)
+ nchr = ARG_MAX;
+
+ avp = (struct argvec **)(env ? &procstat->argv : &procstat->envv);
+ av = *avp;
+
+ if (av == NULL)
+ {
+ av = argvec_alloc(nchr);
+ if (av == NULL)
+ {
+ warn("malloc(%zu)", nchr);
+ return (NULL);
+ }
+ *avp = av;
+ } else if (av->bufsize < nchr) {
+ av->buf = reallocf(av->buf, nchr);
+ if (av->buf == NULL) {
+ warn("malloc(%zu)", nchr);
+ return (NULL);
+ }
+ }
+ if (procstat->type == PROCSTAT_SYSCTL) {
+ name[0] = CTL_KERN;
+ name[1] = KERN_PROC;
+ name[2] = env ? KERN_PROC_ENV : KERN_PROC_ARGS;
+ name[3] = kp->ki_pid;
+ len = nchr;
+ error = sysctl(name, nitems(name), av->buf, &len, NULL, 0);
+ if (error != 0 && errno != ESRCH && errno != EPERM)
+ warn("sysctl(kern.proc.%s)", env ? "env" : "args");
+ if (error != 0 || len == 0)
+ return (NULL);
+ } else /* procstat->type == PROCSTAT_CORE */ {
+ type = env ? PSC_TYPE_ENVV : PSC_TYPE_ARGV;
+ len = nchr;
+ if (procstat_core_get(procstat->core, type, av->buf, &len)
+ == NULL) {
+ return (NULL);
+ }
+ }
+
+ argv = av->argv;
+ argc = av->argc;
+ i = 0;
+ for (p = av->buf; p < av->buf + len; p += strlen(p) + 1) {
+ argv[i++] = p;
+ if (i < argc)
+ continue;
+ /* Grow argv. */
+ argc += argc;
+ argv = realloc(argv, sizeof(char *) * argc);
+ if (argv == NULL) {
+ warn("malloc(%zu)", sizeof(char *) * argc);
+ return (NULL);
+ }
+ av->argv = argv;
+ av->argc = argc;
+ }
+ argv[i] = NULL;
+
+ return (argv);
+}
+
+/*
+ * Return process command line arguments.
+ */
+char **
+procstat_getargv(struct procstat *procstat, struct kinfo_proc *p, size_t nchr)
+{
+
+ return (getargv(procstat, p, nchr, 0));
+}
+
+/*
+ * Free the buffer allocated by procstat_getargv().
+ */
+void
+procstat_freeargv(struct procstat *procstat)
+{
+
+ if (procstat->argv != NULL) {
+ argvec_free(procstat->argv);
+ procstat->argv = NULL;
+ }
+}
+
+/*
+ * Return process environment.
+ */
+char **
+procstat_getenvv(struct procstat *procstat, struct kinfo_proc *p, size_t nchr)
+{
+
+ return (getargv(procstat, p, nchr, 1));
+}
+
+/*
+ * Free the buffer allocated by procstat_getenvv().
+ */
+void
+procstat_freeenvv(struct procstat *procstat)
+{
+ if (procstat->envv != NULL) {
+ argvec_free(procstat->envv);
+ procstat->envv = NULL;
+ }
+}
+
+static struct kinfo_vmentry *
+kinfo_getvmmap_core(struct procstat_core *core, int *cntp)
+{
+ int cnt;
+ size_t len;
+ char *buf, *bp, *eb;
+ struct kinfo_vmentry *kiv, *kp, *kv;
+
+ buf = procstat_core_get(core, PSC_TYPE_VMMAP, NULL, &len);
+ if (buf == NULL)
+ return (NULL);
+
+ /*
+ * XXXMG: The code below is just copy&past from libutil.
+ * The code duplication can be avoided if libutil
+ * is extended to provide something like:
+ * struct kinfo_vmentry *kinfo_getvmmap_from_buf(const char *buf,
+ * size_t len, int *cntp);
+ */
+
+ /* Pass 1: count items */
+ cnt = 0;
+ bp = buf;
+ eb = buf + len;
+ while (bp < eb) {
+ kv = (struct kinfo_vmentry *)(uintptr_t)bp;
+ if (kv->kve_structsize == 0)
+ break;
+ bp += kv->kve_structsize;
+ cnt++;
+ }
+
+ kiv = calloc(cnt, sizeof(*kiv));
+ if (kiv == NULL) {
+ free(buf);
+ return (NULL);
+ }
+ bp = buf;
+ eb = buf + len;
+ kp = kiv;
+ /* Pass 2: unpack */
+ while (bp < eb) {
+ kv = (struct kinfo_vmentry *)(uintptr_t)bp;
+ if (kv->kve_structsize == 0)
+ break;
+ /* Copy/expand into pre-zeroed buffer */
+ memcpy(kp, kv, kv->kve_structsize);
+ /* Advance to next packed record */
+ bp += kv->kve_structsize;
+ /* Set field size to fixed length, advance */
+ kp->kve_structsize = sizeof(*kp);
+ kp++;
+ }
+ free(buf);
+ *cntp = cnt;
+ return (kiv); /* Caller must free() return value */
+}
+
+struct kinfo_vmentry *
+procstat_getvmmap(struct procstat *procstat, struct kinfo_proc *kp,
+ unsigned int *cntp)
+{
+
+ switch (procstat->type) {
+ case PROCSTAT_KVM:
+ warnx("kvm method is not supported");
+ return (NULL);
+ case PROCSTAT_SYSCTL:
+ return (kinfo_getvmmap(kp->ki_pid, cntp));
+ case PROCSTAT_CORE:
+ return (kinfo_getvmmap_core(procstat->core, cntp));
+ default:
+ warnx("unknown access method: %d", procstat->type);
+ return (NULL);
+ }
+}
+
+void
+procstat_freevmmap(struct procstat *procstat __unused,
+ struct kinfo_vmentry *vmmap)
+{
+
+ free(vmmap);
+}
+
+static gid_t *
+procstat_getgroups_kvm(kvm_t *kd, struct kinfo_proc *kp, unsigned int *cntp)
+{
+ struct proc proc;
+ struct ucred ucred;
+ gid_t *groups;
+ size_t len;
+ unsigned int ngroups;
+
+ assert(kd != NULL);
+ assert(kp != NULL);
+ if (!kvm_read_all(kd, (unsigned long)kp->ki_paddr, &proc,
+ sizeof(proc))) {
+ warnx("can't read proc struct at %p for pid %d",
+ kp->ki_paddr, kp->ki_pid);
+ return (NULL);
+ }
+ if (proc.p_ucred == NOCRED)
+ return (NULL);
+ if (!kvm_read_all(kd, (unsigned long)proc.p_ucred, &ucred,
+ sizeof(ucred))) {
+ warnx("can't read ucred struct at %p for pid %d",
+ proc.p_ucred, kp->ki_pid);
+ return (NULL);
+ }
+ ngroups = 1 + ucred.cr_ngroups;
+ len = ngroups * sizeof(gid_t);
+ groups = malloc(len);
+ if (groups == NULL) {
+ warn("malloc(%zu)", len);
+ return (NULL);
+ }
+ groups[0] = ucred.cr_gid;
+ if (!kvm_read_all(kd, (unsigned long)ucred.cr_groups, groups + 1,
+ len - sizeof(gid_t))) {
+ warnx("can't read groups at %p for pid %d",
+ ucred.cr_groups, kp->ki_pid);
+ free(groups);
+ return (NULL);
+ }
+ *cntp = ngroups;
+ return (groups);
+}
+
+static gid_t *
+procstat_getgroups_sysctl(pid_t pid, unsigned int *cntp)
+{
+ int mib[4];
+ size_t len;
+ gid_t *groups;
+
+ mib[0] = CTL_KERN;
+ mib[1] = KERN_PROC;
+ mib[2] = KERN_PROC_GROUPS;
+ mib[3] = pid;
+ len = (sysconf(_SC_NGROUPS_MAX) + 1) * sizeof(gid_t);
+ groups = malloc(len);
+ if (groups == NULL) {
+ warn("malloc(%zu)", len);
+ return (NULL);
+ }
+ if (sysctl(mib, nitems(mib), groups, &len, NULL, 0) == -1) {
+ warn("sysctl: kern.proc.groups: %d", pid);
+ free(groups);
+ return (NULL);
+ }
+ *cntp = len / sizeof(gid_t);
+ return (groups);
+}
+
+static gid_t *
+procstat_getgroups_core(struct procstat_core *core, unsigned int *cntp)
+{
+ size_t len;
+ gid_t *groups;
+
+ groups = procstat_core_get(core, PSC_TYPE_GROUPS, NULL, &len);
+ if (groups == NULL)
+ return (NULL);
+ *cntp = len / sizeof(gid_t);
+ return (groups);
+}
+
+gid_t *
+procstat_getgroups(struct procstat *procstat, struct kinfo_proc *kp,
+ unsigned int *cntp)
+{
+ switch (procstat->type) {
+ case PROCSTAT_KVM:
+ return (procstat_getgroups_kvm(procstat->kd, kp, cntp));
+ case PROCSTAT_SYSCTL:
+ return (procstat_getgroups_sysctl(kp->ki_pid, cntp));
+ case PROCSTAT_CORE:
+ return (procstat_getgroups_core(procstat->core, cntp));
+ default:
+ warnx("unknown access method: %d", procstat->type);
+ return (NULL);
+ }
+}
+
+void
+procstat_freegroups(struct procstat *procstat __unused, gid_t *groups)
+{
+
+ free(groups);
+}
+
+static int
+procstat_getumask_kvm(kvm_t *kd, struct kinfo_proc *kp, unsigned short *maskp)
+{
+ struct pwddesc pd;
+
+ assert(kd != NULL);
+ assert(kp != NULL);
+ if (kp->ki_pd == NULL)
+ return (-1);
+ if (!kvm_read_all(kd, (unsigned long)kp->ki_pd, &pd, sizeof(pd))) {
+ warnx("can't read pwddesc at %p for pid %d", kp->ki_pd,
+ kp->ki_pid);
+ return (-1);
+ }
+ *maskp = pd.pd_cmask;
+ return (0);
+}
+
+static int
+procstat_getumask_sysctl(pid_t pid, unsigned short *maskp)
+{
+ int error;
+ int mib[4];
+ size_t len;
+
+ mib[0] = CTL_KERN;
+ mib[1] = KERN_PROC;
+ mib[2] = KERN_PROC_UMASK;
+ mib[3] = pid;
+ len = sizeof(*maskp);
+ error = sysctl(mib, nitems(mib), maskp, &len, NULL, 0);
+ if (error != 0 && errno != ESRCH && errno != EPERM)
+ warn("sysctl: kern.proc.umask: %d", pid);
+ return (error);
+}
+
+static int
+procstat_getumask_core(struct procstat_core *core, unsigned short *maskp)
+{
+ size_t len;
+ unsigned short *buf;
+
+ buf = procstat_core_get(core, PSC_TYPE_UMASK, NULL, &len);
+ if (buf == NULL)
+ return (-1);
+ if (len < sizeof(*maskp)) {
+ free(buf);
+ return (-1);
+ }
+ *maskp = *buf;
+ free(buf);
+ return (0);
+}
+
+int
+procstat_getumask(struct procstat *procstat, struct kinfo_proc *kp,
+ unsigned short *maskp)
+{
+ switch (procstat->type) {
+ case PROCSTAT_KVM:
+ return (procstat_getumask_kvm(procstat->kd, kp, maskp));
+ case PROCSTAT_SYSCTL:
+ return (procstat_getumask_sysctl(kp->ki_pid, maskp));
+ case PROCSTAT_CORE:
+ return (procstat_getumask_core(procstat->core, maskp));
+ default:
+ warnx("unknown access method: %d", procstat->type);
+ return (-1);
+ }
+}
+
+static int
+procstat_getrlimit_kvm(kvm_t *kd, struct kinfo_proc *kp, int which,
+ struct rlimit* rlimit)
+{
+ struct proc proc;
+ unsigned long offset;
+
+ assert(kd != NULL);
+ assert(kp != NULL);
+ assert(which >= 0 && which < RLIM_NLIMITS);
+ if (!kvm_read_all(kd, (unsigned long)kp->ki_paddr, &proc,
+ sizeof(proc))) {
+ warnx("can't read proc struct at %p for pid %d",
+ kp->ki_paddr, kp->ki_pid);
+ return (-1);
+ }
+ if (proc.p_limit == NULL)
+ return (-1);
+ offset = (unsigned long)proc.p_limit + sizeof(struct rlimit) * which;
+ if (!kvm_read_all(kd, offset, rlimit, sizeof(*rlimit))) {
+ warnx("can't read rlimit struct at %p for pid %d",
+ (void *)offset, kp->ki_pid);
+ return (-1);
+ }
+ return (0);
+}
+
+static int
+procstat_getrlimit_sysctl(pid_t pid, int which, struct rlimit* rlimit)
+{
+ int error, name[5];
+ size_t len;
+
+ name[0] = CTL_KERN;
+ name[1] = KERN_PROC;
+ name[2] = KERN_PROC_RLIMIT;
+ name[3] = pid;
+ name[4] = which;
+ len = sizeof(struct rlimit);
+ error = sysctl(name, nitems(name), rlimit, &len, NULL, 0);
+ if (error < 0 && errno != ESRCH) {
+ warn("sysctl: kern.proc.rlimit: %d", pid);
+ return (-1);
+ }
+ if (error < 0 || len != sizeof(struct rlimit))
+ return (-1);
+ return (0);
+}
+
+static int
+procstat_getrlimit_core(struct procstat_core *core, int which,
+ struct rlimit* rlimit)
+{
+ size_t len;
+ struct rlimit* rlimits;
+
+ if (which < 0 || which >= RLIM_NLIMITS) {
+ errno = EINVAL;
+ warn("getrlimit: which");
+ return (-1);
+ }
+ rlimits = procstat_core_get(core, PSC_TYPE_RLIMIT, NULL, &len);
+ if (rlimits == NULL)
+ return (-1);
+ if (len < sizeof(struct rlimit) * RLIM_NLIMITS) {
+ free(rlimits);
+ return (-1);
+ }
+ *rlimit = rlimits[which];
+ free(rlimits);
+ return (0);
+}
+
+int
+procstat_getrlimit(struct procstat *procstat, struct kinfo_proc *kp, int which,
+ struct rlimit* rlimit)
+{
+ switch (procstat->type) {
+ case PROCSTAT_KVM:
+ return (procstat_getrlimit_kvm(procstat->kd, kp, which,
+ rlimit));
+ case PROCSTAT_SYSCTL:
+ return (procstat_getrlimit_sysctl(kp->ki_pid, which, rlimit));
+ case PROCSTAT_CORE:
+ return (procstat_getrlimit_core(procstat->core, which, rlimit));
+ default:
+ warnx("unknown access method: %d", procstat->type);
+ return (-1);
+ }
+}
+
+static int
+procstat_getpathname_sysctl(pid_t pid, char *pathname, size_t maxlen)
+{
+ int error, name[4];
+ size_t len;
+
+ name[0] = CTL_KERN;
+ name[1] = KERN_PROC;
+ name[2] = KERN_PROC_PATHNAME;
+ name[3] = pid;
+ len = maxlen;
+ error = sysctl(name, nitems(name), pathname, &len, NULL, 0);
+ if (error != 0 && errno != ESRCH)
+ warn("sysctl: kern.proc.pathname: %d", pid);
+ if (len == 0)
+ pathname[0] = '\0';
+ return (error);
+}
+
+static int
+procstat_getpathname_core(struct procstat_core *core, char *pathname,
+ size_t maxlen)
+{
+ struct kinfo_file *files;
+ int cnt, i, result;
+
+ files = kinfo_getfile_core(core, &cnt);
+ if (files == NULL)
+ return (-1);
+ result = -1;
+ for (i = 0; i < cnt; i++) {
+ if (files[i].kf_fd != KF_FD_TYPE_TEXT)
+ continue;
+ strncpy(pathname, files[i].kf_path, maxlen);
+ result = 0;
+ break;
+ }
+ free(files);
+ return (result);
+}
+
+int
+procstat_getpathname(struct procstat *procstat, struct kinfo_proc *kp,
+ char *pathname, size_t maxlen)
+{
+ switch (procstat->type) {
+ case PROCSTAT_KVM:
+ /* XXX: Return empty string. */
+ if (maxlen > 0)
+ pathname[0] = '\0';
+ return (0);
+ case PROCSTAT_SYSCTL:
+ return (procstat_getpathname_sysctl(kp->ki_pid, pathname,
+ maxlen));
+ case PROCSTAT_CORE:
+ return (procstat_getpathname_core(procstat->core, pathname,
+ maxlen));
+ default:
+ warnx("unknown access method: %d", procstat->type);
+ return (-1);
+ }
+}
+
+static int
+procstat_getosrel_kvm(kvm_t *kd, struct kinfo_proc *kp, int *osrelp)
+{
+ struct proc proc;
+
+ assert(kd != NULL);
+ assert(kp != NULL);
+ if (!kvm_read_all(kd, (unsigned long)kp->ki_paddr, &proc,
+ sizeof(proc))) {
+ warnx("can't read proc struct at %p for pid %d",
+ kp->ki_paddr, kp->ki_pid);
+ return (-1);
+ }
+ *osrelp = proc.p_osrel;
+ return (0);
+}
+
+static int
+procstat_getosrel_sysctl(pid_t pid, int *osrelp)
+{
+ int error, name[4];
+ size_t len;
+
+ name[0] = CTL_KERN;
+ name[1] = KERN_PROC;
+ name[2] = KERN_PROC_OSREL;
+ name[3] = pid;
+ len = sizeof(*osrelp);
+ error = sysctl(name, nitems(name), osrelp, &len, NULL, 0);
+ if (error != 0 && errno != ESRCH)
+ warn("sysctl: kern.proc.osrel: %d", pid);
+ return (error);
+}
+
+static int
+procstat_getosrel_core(struct procstat_core *core, int *osrelp)
+{
+ size_t len;
+ int *buf;
+
+ buf = procstat_core_get(core, PSC_TYPE_OSREL, NULL, &len);
+ if (buf == NULL)
+ return (-1);
+ if (len < sizeof(*osrelp)) {
+ free(buf);
+ return (-1);
+ }
+ *osrelp = *buf;
+ free(buf);
+ return (0);
+}
+
+int
+procstat_getosrel(struct procstat *procstat, struct kinfo_proc *kp, int *osrelp)
+{
+ switch (procstat->type) {
+ case PROCSTAT_KVM:
+ return (procstat_getosrel_kvm(procstat->kd, kp, osrelp));
+ case PROCSTAT_SYSCTL:
+ return (procstat_getosrel_sysctl(kp->ki_pid, osrelp));
+ case PROCSTAT_CORE:
+ return (procstat_getosrel_core(procstat->core, osrelp));
+ default:
+ warnx("unknown access method: %d", procstat->type);
+ return (-1);
+ }
+}
+
+#define PROC_AUXV_MAX 256
+
+#ifdef PS_ARCH_HAS_FREEBSD32
+static const char *elf32_sv_names[] = {
+ "Linux ELF32",
+ "FreeBSD ELF32",
+};
+
+static int
+is_elf32_sysctl(pid_t pid)
+{
+ int error, name[4];
+ size_t len, i;
+ char sv_name[32];
+
+ name[0] = CTL_KERN;
+ name[1] = KERN_PROC;
+ name[2] = KERN_PROC_SV_NAME;
+ name[3] = pid;
+ len = sizeof(sv_name);
+ error = sysctl(name, nitems(name), sv_name, &len, NULL, 0);
+ if (error != 0 || len == 0)
+ return (0);
+ for (i = 0; i < sizeof(elf32_sv_names) / sizeof(*elf32_sv_names); i++) {
+ if (strncmp(sv_name, elf32_sv_names[i], sizeof(sv_name)) == 0)
+ return (1);
+ }
+ return (0);
+}
+
+static Elf_Auxinfo *
+procstat_getauxv32_sysctl(pid_t pid, unsigned int *cntp)
+{
+ Elf_Auxinfo *auxv;
+ Elf32_Auxinfo *auxv32;
+ size_t len;
+ unsigned int i, count;
+ int name[4];
+
+ name[0] = CTL_KERN;
+ name[1] = KERN_PROC;
+ name[2] = KERN_PROC_AUXV;
+ name[3] = pid;
+ len = PROC_AUXV_MAX * sizeof(Elf32_Auxinfo);
+ auxv = NULL;
+ auxv32 = malloc(len);
+ if (auxv32 == NULL) {
+ warn("malloc(%zu)", len);
+ goto out;
+ }
+ if (sysctl(name, nitems(name), auxv32, &len, NULL, 0) == -1) {
+ if (errno != ESRCH && errno != EPERM)
+ warn("sysctl: kern.proc.auxv: %d: %d", pid, errno);
+ goto out;
+ }
+ count = len / sizeof(Elf32_Auxinfo);
+ auxv = malloc(count * sizeof(Elf_Auxinfo));
+ if (auxv == NULL) {
+ warn("malloc(%zu)", count * sizeof(Elf_Auxinfo));
+ goto out;
+ }
+ for (i = 0; i < count; i++) {
+ /*
+ * XXX: We expect that values for a_type on a 32-bit platform
+ * are directly mapped to values on 64-bit one, which is not
+ * necessarily true.
+ */
+ auxv[i].a_type = auxv32[i].a_type;
+ /*
+ * Don't sign extend values. Existing entries are positive
+ * integers or pointers. Under freebsd32, programs typically
+ * have a full [0, 2^32) address space (perhaps minus the last
+ * page) and treating this as a signed integer would be
+ * confusing since these are not kernel pointers.
+ *
+ * XXX: A more complete translation would be ABI and
+ * type-aware.
+ */
+ auxv[i].a_un.a_val = (uint32_t)auxv32[i].a_un.a_val;
+ }
+ *cntp = count;
+out:
+ free(auxv32);
+ return (auxv);
+}
+#endif /* PS_ARCH_HAS_FREEBSD32 */
+
+static Elf_Auxinfo *
+procstat_getauxv_sysctl(pid_t pid, unsigned int *cntp)
+{
+ Elf_Auxinfo *auxv;
+ int name[4];
+ size_t len;
+
+#ifdef PS_ARCH_HAS_FREEBSD32
+ if (is_elf32_sysctl(pid))
+ return (procstat_getauxv32_sysctl(pid, cntp));
+#endif
+ name[0] = CTL_KERN;
+ name[1] = KERN_PROC;
+ name[2] = KERN_PROC_AUXV;
+ name[3] = pid;
+ len = PROC_AUXV_MAX * sizeof(Elf_Auxinfo);
+ auxv = malloc(len);
+ if (auxv == NULL) {
+ warn("malloc(%zu)", len);
+ return (NULL);
+ }
+ if (sysctl(name, nitems(name), auxv, &len, NULL, 0) == -1) {
+ if (errno != ESRCH && errno != EPERM)
+ warn("sysctl: kern.proc.auxv: %d: %d", pid, errno);
+ free(auxv);
+ return (NULL);
+ }
+ *cntp = len / sizeof(Elf_Auxinfo);
+ return (auxv);
+}
+
+static Elf_Auxinfo *
+procstat_getauxv_core(struct procstat_core *core, unsigned int *cntp)
+{
+ Elf_Auxinfo *auxv;
+ size_t len;
+
+ auxv = procstat_core_get(core, PSC_TYPE_AUXV, NULL, &len);
+ if (auxv == NULL)
+ return (NULL);
+ *cntp = len / sizeof(Elf_Auxinfo);
+ return (auxv);
+}
+
+Elf_Auxinfo *
+procstat_getauxv(struct procstat *procstat, struct kinfo_proc *kp,
+ unsigned int *cntp)
+{
+ switch (procstat->type) {
+ case PROCSTAT_KVM:
+ warnx("kvm method is not supported");
+ return (NULL);
+ case PROCSTAT_SYSCTL:
+ return (procstat_getauxv_sysctl(kp->ki_pid, cntp));
+ case PROCSTAT_CORE:
+ return (procstat_getauxv_core(procstat->core, cntp));
+ default:
+ warnx("unknown access method: %d", procstat->type);
+ return (NULL);
+ }
+}
+
+void
+procstat_freeauxv(struct procstat *procstat __unused, Elf_Auxinfo *auxv)
+{
+
+ free(auxv);
+}
+
+static struct ptrace_lwpinfo *
+procstat_getptlwpinfo_core(struct procstat_core *core, unsigned int *cntp)
+{
+ void *buf;
+ struct ptrace_lwpinfo *pl;
+ unsigned int cnt;
+ size_t len;
+
+ cnt = procstat_core_note_count(core, PSC_TYPE_PTLWPINFO);
+ if (cnt == 0)
+ return (NULL);
+
+ len = cnt * sizeof(*pl);
+ buf = calloc(1, len);
+ pl = procstat_core_get(core, PSC_TYPE_PTLWPINFO, buf, &len);
+ if (pl == NULL) {
+ free(buf);
+ return (NULL);
+ }
+ *cntp = len / sizeof(*pl);
+ return (pl);
+}
+
+struct ptrace_lwpinfo *
+procstat_getptlwpinfo(struct procstat *procstat, unsigned int *cntp)
+{
+ switch (procstat->type) {
+ case PROCSTAT_KVM:
+ warnx("kvm method is not supported");
+ return (NULL);
+ case PROCSTAT_SYSCTL:
+ warnx("sysctl method is not supported");
+ return (NULL);
+ case PROCSTAT_CORE:
+ return (procstat_getptlwpinfo_core(procstat->core, cntp));
+ default:
+ warnx("unknown access method: %d", procstat->type);
+ return (NULL);
+ }
+}
+
+void
+procstat_freeptlwpinfo(struct procstat *procstat __unused,
+ struct ptrace_lwpinfo *pl)
+{
+ free(pl);
+}
+
+static struct kinfo_kstack *
+procstat_getkstack_sysctl(pid_t pid, int *cntp)
+{
+ struct kinfo_kstack *kkstp;
+ int error, name[4];
+ size_t len;
+
+ name[0] = CTL_KERN;
+ name[1] = KERN_PROC;
+ name[2] = KERN_PROC_KSTACK;
+ name[3] = pid;
+
+ len = 0;
+ error = sysctl(name, nitems(name), NULL, &len, NULL, 0);
+ if (error < 0 && errno != ESRCH && errno != EPERM && errno != ENOENT) {
+ warn("sysctl: kern.proc.kstack: %d", pid);
+ return (NULL);
+ }
+ if (error == -1 && errno == ENOENT) {
+ warnx("sysctl: kern.proc.kstack unavailable"
+ " (options DDB or options STACK required in kernel)");
+ return (NULL);
+ }
+ if (error == -1)
+ return (NULL);
+ kkstp = malloc(len);
+ if (kkstp == NULL) {
+ warn("malloc(%zu)", len);
+ return (NULL);
+ }
+ if (sysctl(name, nitems(name), kkstp, &len, NULL, 0) == -1 &&
+ errno != ENOMEM) {
+ warn("sysctl: kern.proc.pid: %d", pid);
+ free(kkstp);
+ return (NULL);
+ }
+ *cntp = len / sizeof(*kkstp);
+
+ return (kkstp);
+}
+
+struct kinfo_kstack *
+procstat_getkstack(struct procstat *procstat, struct kinfo_proc *kp,
+ unsigned int *cntp)
+{
+ switch (procstat->type) {
+ case PROCSTAT_KVM:
+ warnx("kvm method is not supported");
+ return (NULL);
+ case PROCSTAT_SYSCTL:
+ return (procstat_getkstack_sysctl(kp->ki_pid, cntp));
+ case PROCSTAT_CORE:
+ warnx("core method is not supported");
+ return (NULL);
+ default:
+ warnx("unknown access method: %d", procstat->type);
+ return (NULL);
+ }
+}
+
+void
+procstat_freekstack(struct procstat *procstat __unused,
+ struct kinfo_kstack *kkstp)
+{
+
+ free(kkstp);
+}
+
+static struct advlock_list *
+procstat_getadvlock_sysctl(struct procstat *procstat __unused)
+{
+ struct advlock_list *res;
+ struct advlock *a;
+ void *buf;
+ char *c;
+ struct kinfo_lockf *kl;
+ size_t buf_len;
+ int error;
+ static const int kl_name[] = { CTL_KERN, KERN_LOCKF };
+
+ res = malloc(sizeof(*res));
+ if (res == NULL)
+ return (NULL);
+ STAILQ_INIT(res);
+ buf = NULL;
+
+ buf_len = 0;
+ error = sysctl(kl_name, nitems(kl_name), NULL, &buf_len, NULL, 0);
+ if (error != 0) {
+ warn("sysctl KERN_LOCKF size");
+ goto fail;
+ }
+ buf_len *= 2;
+ buf = malloc(buf_len);
+ if (buf == NULL) {
+ warn("malloc");
+ goto fail;
+ }
+ error = sysctl(kl_name, nitems(kl_name), buf, &buf_len, NULL, 0);
+ if (error != 0) {
+ warn("sysctl KERN_LOCKF data");
+ goto fail;
+ }
+
+ for (c = buf; (char *)c < (char *)buf + buf_len;
+ c += kl->kl_structsize) {
+ kl = (struct kinfo_lockf *)(void *)c;
+ if (sizeof(*kl) < (size_t)kl->kl_structsize) {
+ warn("ABI broken");
+ goto fail;
+ }
+ a = malloc(sizeof(*a));
+ if (a == NULL) {
+ warn("malloc advlock");
+ goto fail;
+ }
+ switch (kl->kl_rw) {
+ case KLOCKF_RW_READ:
+ a->rw = PS_ADVLOCK_RO;
+ break;
+ case KLOCKF_RW_WRITE:
+ a->rw = PS_ADVLOCK_RW;
+ break;
+ default:
+ warn("ABI broken");
+ free(a);
+ goto fail;
+ }
+ switch (kl->kl_type) {
+ case KLOCKF_TYPE_FLOCK:
+ a->type = PS_ADVLOCK_TYPE_FLOCK;
+ break;
+ case KLOCKF_TYPE_PID:
+ a->type = PS_ADVLOCK_TYPE_PID;
+ break;
+ case KLOCKF_TYPE_REMOTE:
+ a->type = PS_ADVLOCK_TYPE_REMOTE;
+ break;
+ default:
+ warn("ABI broken");
+ free(a);
+ goto fail;
+ }
+ a->pid = kl->kl_pid;
+ a->sysid = kl->kl_sysid;
+ a->file_fsid = kl->kl_file_fsid;
+ a->file_rdev = kl->kl_file_rdev;
+ a->file_fileid = kl->kl_file_fileid;
+ a->start = kl->kl_start;
+ a->len = kl->kl_len;
+ if (kl->kl_path[0] != '\0') {
+ a->path = strdup(kl->kl_path);
+ if (a->path == NULL) {
+ warn("malloc");
+ free(a);
+ goto fail;
+ }
+ } else
+ a->path = NULL;
+ STAILQ_INSERT_TAIL(res, a, next);
+ }
+
+ free(buf);
+ return (res);
+
+fail:
+ free(buf);
+ procstat_freeadvlock(procstat, res);
+ return (NULL);
+}
+
+struct advlock_list *
+procstat_getadvlock(struct procstat *procstat)
+{
+ switch (procstat->type) {
+ case PROCSTAT_KVM:
+ warnx("kvm method is not supported");
+ return (NULL);
+ case PROCSTAT_SYSCTL:
+ return (procstat_getadvlock_sysctl(procstat));
+ case PROCSTAT_CORE:
+ warnx("core method is not supported");
+ return (NULL);
+ default:
+ warnx("unknown access method: %d", procstat->type);
+ return (NULL);
+ }
+}
+
+void
+procstat_freeadvlock(struct procstat *procstat __unused,
+ struct advlock_list *lst)
+{
+ struct advlock *a, *a1;
+
+ STAILQ_FOREACH_SAFE(a, lst, next, a1) {
+ free(__DECONST(char *, a->path));
+ free(a);
+ }
+ free(lst);
+}
+
+static rlim_t *
+procstat_getrlimitusage_sysctl(pid_t pid, unsigned *cntp)
+{
+ int error, name[4];
+ rlim_t *val;
+ size_t len;
+
+ name[0] = CTL_KERN;
+ name[1] = KERN_PROC;
+ name[2] = KERN_PROC_RLIMIT_USAGE;
+ name[3] = pid;
+
+ len = 0;
+ error = sysctl(name, nitems(name), NULL, &len, NULL, 0);
+ if (error == -1)
+ return (NULL);
+ val = malloc(len);
+ if (val == NULL)
+ return (NULL);
+
+ error = sysctl(name, nitems(name), val, &len, NULL, 0);
+ if (error == -1) {
+ free(val);
+ return (NULL);
+ }
+ *cntp = len / sizeof(rlim_t);
+ return (val);
+}
+
+rlim_t *
+procstat_getrlimitusage(struct procstat *procstat, struct kinfo_proc *kp,
+ unsigned int *cntp)
+{
+ switch (procstat->type) {
+ case PROCSTAT_KVM:
+ warnx("kvm method is not supported");
+ return (NULL);
+ case PROCSTAT_SYSCTL:
+ return (procstat_getrlimitusage_sysctl(kp->ki_pid, cntp));
+ case PROCSTAT_CORE:
+ warnx("core method is not supported");
+ return (NULL);
+ default:
+ warnx("unknown access method: %d", procstat->type);
+ return (NULL);
+ }
+}
+
+void
+procstat_freerlimitusage(struct procstat *procstat __unused, rlim_t *resusage)
+{
+ free(resusage);
+}
+
+static struct kinfo_knote *
+procstat_get_kqueue_info_sysctl(pid_t pid, int kqfd, unsigned int *cntp,
+ char *errbuf)
+{
+ int error, name[5];
+ struct kinfo_knote *val;
+ size_t len;
+
+ name[0] = CTL_KERN;
+ name[1] = KERN_PROC;
+ name[2] = KERN_PROC_KQUEUE;
+ name[3] = pid;
+ name[4] = kqfd;
+
+ len = 0;
+ error = sysctl(name, nitems(name), NULL, &len, NULL, 0);
+ if (error == -1) {
+ snprintf(errbuf, _POSIX2_LINE_MAX,
+ "KERN_PROC_KQUEUE.pid<%d>.kq<%d> (size q) failed: %s",
+ pid, kqfd, strerror(errno));
+ return (NULL);
+ }
+ val = malloc(len);
+ if (val == NULL) {
+ snprintf(errbuf, _POSIX2_LINE_MAX, "no memory");
+ return (NULL);
+ }
+
+ error = sysctl(name, nitems(name), val, &len, NULL, 0);
+ if (error == -1) {
+ snprintf(errbuf, _POSIX2_LINE_MAX,
+ "KERN_PROC_KQUEUE.pid<%d>.kq<%d> failed: %s",
+ pid, kqfd, strerror(errno));
+ free(val);
+ return (NULL);
+ }
+ *cntp = len / sizeof(*val);
+ return (val);
+}
+
+struct kinfo_knote *
+procstat_get_kqueue_info(struct procstat *procstat,
+ struct kinfo_proc *kp, int kqfd, unsigned int *count, char *errbuf)
+{
+ struct kinfo_knote *kn, *k, *res, *rn;
+ size_t len, kqn;
+
+ switch (procstat->type) {
+ case PROCSTAT_KVM:
+ warnx("kvm method is not supported");
+ return (NULL);
+ case PROCSTAT_SYSCTL:
+ return (procstat_get_kqueue_info_sysctl(kp->ki_pid, kqfd,
+ count, errbuf));
+ case PROCSTAT_CORE:
+ k = procstat_core_get(procstat->core, PSC_TYPE_KQUEUES,
+ NULL, &len);
+ if (k == NULL) {
+ snprintf(errbuf, _POSIX2_LINE_MAX,
+ "getting NT_PROCSTAT_KQUEUES note failed");
+ *count = 0;
+ return (NULL);
+ }
+ for (kqn = 0, kn = k; kn < k + len / sizeof(*kn); kn++) {
+ if (kn->knt_kq_fd == kqfd)
+ kqn++;
+ }
+ res = calloc(kqn, sizeof(*res));
+ if (res == NULL) {
+ free(k);
+ snprintf(errbuf, _POSIX2_LINE_MAX,
+ "no memory");
+ return (NULL);
+ }
+ for (kn = k, rn = res; kn < k + len / sizeof(*kn); kn++) {
+ if (kn->knt_kq_fd != kqfd)
+ continue;
+ *rn = *kn;
+ rn++;
+ }
+ *count = kqn;
+ free(k);
+ return (res);
+ default:
+ warnx("unknown access method: %d", procstat->type);
+ return (NULL);
+ }
+}
+
+void
+procstat_freekqinfo(struct procstat *procstat __unused, struct kinfo_knote *v)
+{
+ free(v);
+}
diff --git a/lib/libprocstat/libprocstat.h b/lib/libprocstat/libprocstat.h
new file mode 100644
index 000000000000..548747f90171
--- /dev/null
+++ b/lib/libprocstat/libprocstat.h
@@ -0,0 +1,270 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2009 Stanislav Sedov <stas@FreeBSD.org>
+ * Copyright (c) 2017 Dell EMC
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef _LIBPROCSTAT_H_
+#define _LIBPROCSTAT_H_
+
+/*
+ * XXX: sys/elf.h conflicts with zfs_context.h. Workaround this by not
+ * including conflicting parts when building zfs code.
+ */
+#ifndef ZFS
+#include <sys/elf.h>
+#endif
+#include <sys/caprights.h>
+
+/*
+ * Vnode types.
+ */
+#define PS_FST_VTYPE_VNON 1
+#define PS_FST_VTYPE_VREG 2
+#define PS_FST_VTYPE_VDIR 3
+#define PS_FST_VTYPE_VBLK 4
+#define PS_FST_VTYPE_VCHR 5
+#define PS_FST_VTYPE_VLNK 6
+#define PS_FST_VTYPE_VSOCK 7
+#define PS_FST_VTYPE_VFIFO 8
+#define PS_FST_VTYPE_VBAD 9
+#define PS_FST_VTYPE_UNKNOWN 255
+
+/*
+ * Descriptor types.
+ */
+#define PS_FST_TYPE_VNODE 1
+#define PS_FST_TYPE_FIFO 2
+#define PS_FST_TYPE_SOCKET 3
+#define PS_FST_TYPE_PIPE 4
+#define PS_FST_TYPE_PTS 5
+#define PS_FST_TYPE_KQUEUE 6
+/* was PS_FST_TYPE_CRYPTO 7 */
+#define PS_FST_TYPE_MQUEUE 8
+#define PS_FST_TYPE_SHM 9
+#define PS_FST_TYPE_SEM 10
+#define PS_FST_TYPE_UNKNOWN 11
+#define PS_FST_TYPE_NONE 12
+#define PS_FST_TYPE_PROCDESC 13
+#define PS_FST_TYPE_DEV 14
+#define PS_FST_TYPE_EVENTFD 15
+#define PS_FST_TYPE_INOTIFY 16
+
+/*
+ * Special descriptor numbers.
+ */
+#define PS_FST_UFLAG_RDIR 0x0001
+#define PS_FST_UFLAG_CDIR 0x0002
+#define PS_FST_UFLAG_JAIL 0x0004
+#define PS_FST_UFLAG_TRACE 0x0008
+#define PS_FST_UFLAG_TEXT 0x0010
+#define PS_FST_UFLAG_MMAP 0x0020
+#define PS_FST_UFLAG_CTTY 0x0040
+
+/*
+ * Descriptor flags.
+ */
+#define PS_FST_FFLAG_READ 0x0001
+#define PS_FST_FFLAG_WRITE 0x0002
+#define PS_FST_FFLAG_NONBLOCK 0x0004
+#define PS_FST_FFLAG_APPEND 0x0008
+#define PS_FST_FFLAG_SHLOCK 0x0010
+#define PS_FST_FFLAG_EXLOCK 0x0020
+#define PS_FST_FFLAG_ASYNC 0x0040
+#define PS_FST_FFLAG_SYNC 0x0080
+#define PS_FST_FFLAG_NOFOLLOW 0x0100
+#define PS_FST_FFLAG_CREAT 0x0200
+#define PS_FST_FFLAG_TRUNC 0x0400
+#define PS_FST_FFLAG_EXCL 0x0800
+#define PS_FST_FFLAG_DIRECT 0x1000
+#define PS_FST_FFLAG_EXEC 0x2000
+#define PS_FST_FFLAG_HASLOCK 0x4000
+
+#if !defined(__ILP32__) && !defined(__riscv)
+/* Target architecture supports 32-bit compat */
+#define PS_ARCH_HAS_FREEBSD32 1
+#endif
+
+struct kinfo_kstack;
+struct kinfo_proc;
+struct kinfo_vmentry;
+struct kinfo_knote;
+struct procstat;
+struct ptrace_lwpinfo;
+struct rlimit;
+struct filestat {
+ int fs_type; /* Descriptor type. */
+ int fs_flags; /* filestat specific flags. */
+ int fs_fflags; /* Descriptor access flags. */
+ int fs_uflags; /* How this file is used. */
+ int fs_fd; /* File descriptor number. */
+ int fs_ref_count; /* Reference count. */
+ off_t fs_offset; /* Seek location. */
+ void *fs_typedep; /* Type dependent data. */
+ char *fs_path;
+ STAILQ_ENTRY(filestat) next;
+ cap_rights_t fs_cap_rights; /* Capability rights, if flag set. */
+};
+struct vnstat {
+ uint64_t vn_fileid;
+ uint64_t vn_size;
+ uint64_t vn_dev;
+ uint64_t vn_fsid;
+ char *vn_mntdir;
+ int vn_type;
+ uint16_t vn_mode;
+ char vn_devname[SPECNAMELEN + 1];
+};
+struct ptsstat {
+ uint64_t dev;
+ char devname[SPECNAMELEN + 1];
+};
+struct pipestat {
+ size_t buffer_cnt;
+ uint64_t addr;
+ uint64_t peer;
+};
+struct semstat {
+ uint32_t value;
+ uint16_t mode;
+};
+struct shmstat {
+ uint64_t size;
+ uint16_t mode;
+};
+struct sockstat {
+ uint64_t so_addr;
+ uint64_t so_pcb;
+ uint64_t unp_conn;
+ int dom_family;
+ int proto;
+ int so_rcv_sb_state;
+ int so_snd_sb_state;
+ struct sockaddr_storage sa_local; /* Socket address. */
+ struct sockaddr_storage sa_peer; /* Peer address. */
+ int type;
+ char dname[32];
+ unsigned int sendq;
+ unsigned int recvq;
+};
+
+STAILQ_HEAD(filestat_list, filestat);
+
+struct advlock {
+ int rw; /* PS_ADVLOCK_RO/RW */
+ int type; /* PS_ADVLOCK_TYPE_ */
+ int pid;
+ int sysid;
+ uint64_t file_fsid;
+ uint64_t file_rdev;
+ uint64_t file_fileid;
+ off_t start;
+ off_t len; /* len == 0 till the EOF */
+ const char *path;
+ STAILQ_ENTRY(advlock) next;
+};
+
+#define PS_ADVLOCK_RO 0x01
+#define PS_ADVLOCK_RW 0x02
+
+#define PS_ADVLOCK_TYPE_FLOCK 0x01
+#define PS_ADVLOCK_TYPE_PID 0x02
+#define PS_ADVLOCK_TYPE_REMOTE 0x03
+
+STAILQ_HEAD(advlock_list, advlock);
+
+__BEGIN_DECLS
+void procstat_close(struct procstat *procstat);
+void procstat_freeadvlock(struct procstat *procstat,
+ struct advlock_list *advlocks);
+void procstat_freeargv(struct procstat *procstat);
+#ifndef ZFS
+void procstat_freeauxv(struct procstat *procstat, Elf_Auxinfo *auxv);
+#endif
+void procstat_freeenvv(struct procstat *procstat);
+void procstat_freegroups(struct procstat *procstat, gid_t *groups);
+void procstat_freekqinfo(struct procstat *procstat, struct kinfo_knote *kni);
+void procstat_freekstack(struct procstat *procstat,
+ struct kinfo_kstack *kkstp);
+void procstat_freeprocs(struct procstat *procstat, struct kinfo_proc *p);
+void procstat_freefiles(struct procstat *procstat,
+ struct filestat_list *head);
+void procstat_freeptlwpinfo(struct procstat *procstat,
+ struct ptrace_lwpinfo *pl);
+void procstat_freerlimitusage(struct procstat *procstat, rlim_t *resusage);
+void procstat_freevmmap(struct procstat *procstat,
+ struct kinfo_vmentry *vmmap);
+struct advlock_list *procstat_getadvlock(struct procstat *procstat);
+struct filestat_list *procstat_getfiles(struct procstat *procstat,
+ struct kinfo_proc *kp, int mmapped);
+struct kinfo_proc *procstat_getprocs(struct procstat *procstat,
+ int what, int arg, unsigned int *count);
+struct kinfo_knote *procstat_get_kqueue_info(struct procstat *procstat,
+ struct kinfo_proc *kp, int kqfd, unsigned int *count, char *errbuf);
+int procstat_get_pipe_info(struct procstat *procstat, struct filestat *fst,
+ struct pipestat *pipe, char *errbuf);
+int procstat_get_pts_info(struct procstat *procstat, struct filestat *fst,
+ struct ptsstat *pts, char *errbuf);
+int procstat_get_sem_info(struct procstat *procstat, struct filestat *fst,
+ struct semstat *sem, char *errbuf);
+int procstat_get_shm_info(struct procstat *procstat, struct filestat *fst,
+ struct shmstat *shm, char *errbuf);
+int procstat_get_socket_info(struct procstat *procstat, struct filestat *fst,
+ struct sockstat *sock, char *errbuf);
+int procstat_get_vnode_info(struct procstat *procstat, struct filestat *fst,
+ struct vnstat *vn, char *errbuf);
+char **procstat_getargv(struct procstat *procstat, struct kinfo_proc *p,
+ size_t nchr);
+#ifndef ZFS
+Elf_Auxinfo *procstat_getauxv(struct procstat *procstat,
+ struct kinfo_proc *kp, unsigned int *cntp);
+#endif
+struct ptrace_lwpinfo *procstat_getptlwpinfo(struct procstat *procstat,
+ unsigned int *cntp);
+char **procstat_getenvv(struct procstat *procstat, struct kinfo_proc *p,
+ size_t nchr);
+gid_t *procstat_getgroups(struct procstat *procstat, struct kinfo_proc *kp,
+ unsigned int *count);
+struct kinfo_kstack *procstat_getkstack(struct procstat *procstat,
+ struct kinfo_proc *kp, unsigned int *count);
+int procstat_getosrel(struct procstat *procstat, struct kinfo_proc *kp,
+ int *osrelp);
+int procstat_getpathname(struct procstat *procstat, struct kinfo_proc *kp,
+ char *pathname, size_t maxlen);
+int procstat_getrlimit(struct procstat *procstat, struct kinfo_proc *kp,
+ int which, struct rlimit* rlimit);
+rlim_t *procstat_getrlimitusage(struct procstat *procstat,
+ struct kinfo_proc *kp, unsigned int *cntp);
+int procstat_getumask(struct procstat *procstat, struct kinfo_proc *kp,
+ unsigned short* umask);
+struct kinfo_vmentry *procstat_getvmmap(struct procstat *procstat,
+ struct kinfo_proc *kp, unsigned int *count);
+struct procstat *procstat_open_core(const char *filename);
+struct procstat *procstat_open_sysctl(void);
+struct procstat *procstat_open_kvm(const char *nlistf, const char *memf);
+__END_DECLS
+
+#endif /* !_LIBPROCSTAT_H_ */
diff --git a/lib/libprocstat/libprocstat_compat.c b/lib/libprocstat/libprocstat_compat.c
new file mode 100644
index 000000000000..63eb35678752
--- /dev/null
+++ b/lib/libprocstat/libprocstat_compat.c
@@ -0,0 +1,259 @@
+/*-
+ * Copyright (c) 2014 Gleb Kurtsou <gleb@FreeBSD.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/param.h>
+#include <sys/user.h>
+#include <sys/socket.h>
+#include <string.h>
+
+#include "libprocstat.h"
+
+#define SPECNAMELEN_COMPAT12 63
+
+struct freebsd11_ptsstat {
+ uint32_t dev;
+ char devname[SPECNAMELEN_COMPAT12 + 1];
+};
+
+struct freebsd11_vnstat {
+ uint64_t vn_fileid;
+ uint64_t vn_size;
+ char *vn_mntdir;
+ uint32_t vn_dev;
+ uint32_t vn_fsid;
+ int vn_type;
+ uint16_t vn_mode;
+ char vn_devname[SPECNAMELEN_COMPAT12 + 1];
+};
+struct freebsd11_semstat {
+ uint32_t value;
+ uint16_t mode;
+};
+struct freebsd11_shmstat {
+ uint64_t size;
+ uint16_t mode;
+};
+
+struct freebsd11_sockstat {
+ uint64_t inp_ppcb;
+ uint64_t so_addr;
+ uint64_t so_pcb;
+ uint64_t unp_conn;
+ int dom_family;
+ int proto;
+ int so_rcv_sb_state;
+ int so_snd_sb_state;
+ struct sockaddr_storage sa_local; /* Socket address. */
+ struct sockaddr_storage sa_peer; /* Peer address. */
+ int type;
+ char dname[32];
+};
+
+struct freebsd12_vnstat {
+ uint64_t vn_fileid;
+ uint64_t vn_size;
+ uint64_t vn_dev;
+ uint64_t vn_fsid;
+ char *vn_mntdir;
+ int vn_type;
+ uint16_t vn_mode;
+ char vn_devname[SPECNAMELEN_COMPAT12 + 1];
+};
+struct freebsd12_ptsstat {
+ uint64_t dev;
+ char devname[SPECNAMELEN_COMPAT12 + 1];
+};
+
+int freebsd11_procstat_get_pts_info(struct procstat *procstat,
+ struct filestat *fst, struct freebsd11_ptsstat *pts, char *errbuf);
+int freebsd12_procstat_get_pts_info(struct procstat *procstat,
+ struct filestat *fst, struct freebsd12_ptsstat *pts_compat, char *errbuf);
+int freebsd11_procstat_get_sem_info(struct procstat *procstat,
+ struct filestat *fst, struct freebsd11_semstat *sem, char *errbuf);
+int freebsd11_procstat_get_shm_info(struct procstat *procstat,
+ struct filestat *fst, struct freebsd11_shmstat *shm, char *errbuf);
+int freebsd11_procstat_get_socket_info(struct procstat *procstat,
+ struct filestat *fst, struct freebsd11_sockstat *sock, char *errbuf);
+int freebsd11_procstat_get_vnode_info(struct procstat *procstat,
+ struct filestat *fst, struct freebsd11_vnstat *vn, char *errbuf);
+int freebsd12_procstat_get_vnode_info(struct procstat *procstat,
+ struct filestat *fst, struct freebsd12_vnstat *vn_compat, char *errbuf);
+
+static const char trunc_name[] = "<TRUNCATED>";
+
+int
+freebsd11_procstat_get_pts_info(struct procstat *procstat,
+ struct filestat *fst, struct freebsd11_ptsstat *pts_compat, char *errbuf)
+{
+ struct ptsstat pts;
+ int r;
+
+ r = procstat_get_pts_info(procstat, fst, &pts, errbuf);
+ if (r != 0)
+ return (r);
+ pts_compat->dev = pts.dev;
+ if (strlen(pts.devname) >= sizeof(pts_compat->devname))
+ strcpy(pts_compat->devname, trunc_name);
+ else
+ memcpy(pts_compat->devname, pts.devname,
+ sizeof(pts_compat->devname));
+ return (0);
+}
+
+int
+freebsd12_procstat_get_pts_info(struct procstat *procstat,
+ struct filestat *fst, struct freebsd12_ptsstat *pts_compat, char *errbuf)
+{
+ struct ptsstat pts;
+ int r;
+
+ r = procstat_get_pts_info(procstat, fst, &pts, errbuf);
+ if (r != 0)
+ return (r);
+ pts_compat->dev = pts.dev;
+ if (strlen(pts.devname) >= sizeof(pts_compat->devname))
+ strcpy(pts_compat->devname, trunc_name);
+ else
+ memcpy(pts_compat->devname, pts.devname,
+ sizeof(pts_compat->devname));
+ return (0);
+}
+
+int
+freebsd11_procstat_get_sem_info(struct procstat *procstat,
+ struct filestat *fst, struct freebsd11_semstat *sem_compat, char *errbuf)
+{
+ struct semstat sem;
+ int r;
+
+ r = procstat_get_sem_info(procstat, fst, &sem, errbuf);
+ if (r != 0)
+ return (r);
+ sem_compat->value = sem.value;
+ sem_compat->mode = sem.mode;
+ return (0);
+}
+
+int
+freebsd11_procstat_get_shm_info(struct procstat *procstat,
+ struct filestat *fst, struct freebsd11_shmstat *shm_compat, char *errbuf)
+{
+ struct shmstat shm;
+ int r;
+
+ r = procstat_get_shm_info(procstat, fst, &shm, errbuf);
+ if (r != 0)
+ return (r);
+ shm_compat->size = shm.size;
+ shm_compat->mode = shm.mode;
+ return (0);
+}
+
+int
+freebsd11_procstat_get_socket_info(struct procstat *procstat, struct filestat *fst,
+ struct freebsd11_sockstat *sock_compat, char *errbuf)
+{
+ struct sockstat sock;
+ int r;
+
+ r = procstat_get_socket_info(procstat, fst, &sock, errbuf);
+ if (r != 0)
+ return (r);
+ sock_compat->inp_ppcb = sock.so_pcb;
+ sock_compat->so_addr = sock.so_addr;
+ sock_compat->so_pcb = sock.so_pcb;
+ sock_compat->unp_conn = sock.unp_conn;
+ sock_compat->dom_family = sock.dom_family;
+ sock_compat->proto = sock.proto;
+ sock_compat->so_rcv_sb_state = sock.so_rcv_sb_state;
+ sock_compat->so_snd_sb_state = sock.so_snd_sb_state;
+ sock_compat->sa_local = sock.sa_local;
+ sock_compat->sa_peer = sock.sa_peer;
+ sock_compat->type = sock.type;
+ memcpy(sock_compat->dname, sock.dname, sizeof(sock.dname));
+ return (0);
+}
+
+int
+freebsd11_procstat_get_vnode_info(struct procstat *procstat,
+ struct filestat *fst, struct freebsd11_vnstat *vn_compat, char *errbuf)
+{
+ struct vnstat vn;
+ int r;
+
+ r = procstat_get_vnode_info(procstat, fst, &vn, errbuf);
+ if (r != 0)
+ return (r);
+ vn_compat->vn_fileid = vn.vn_fileid;
+ vn_compat->vn_size = vn.vn_size;
+ vn_compat->vn_mntdir = vn.vn_mntdir;
+ vn_compat->vn_dev = vn.vn_dev;
+ vn_compat->vn_fsid = vn.vn_fsid;
+ vn_compat->vn_type = vn.vn_type;
+ vn_compat->vn_mode = vn.vn_mode;
+ if (strlen(vn.vn_devname) >= sizeof(vn_compat->vn_devname))
+ strcpy(vn_compat->vn_devname, trunc_name);
+ else
+ memcpy(vn_compat->vn_devname, vn.vn_devname,
+ sizeof(vn_compat->vn_devname));
+ return (0);
+}
+
+int
+freebsd12_procstat_get_vnode_info(struct procstat *procstat,
+ struct filestat *fst, struct freebsd12_vnstat *vn_compat, char *errbuf)
+{
+ struct vnstat vn;
+ int r;
+
+ r = procstat_get_vnode_info(procstat, fst, &vn, errbuf);
+ if (r != 0)
+ return (r);
+ vn_compat->vn_fileid = vn.vn_fileid;
+ vn_compat->vn_size = vn.vn_size;
+ vn_compat->vn_mntdir = vn.vn_mntdir;
+ vn_compat->vn_dev = vn.vn_dev;
+ vn_compat->vn_fsid = vn.vn_fsid;
+ vn_compat->vn_type = vn.vn_type;
+ vn_compat->vn_mode = vn.vn_mode;
+ if (strlen(vn.vn_devname) >= sizeof(vn_compat->vn_devname))
+ strcpy(vn_compat->vn_devname, trunc_name);
+ else
+ memcpy(vn_compat->vn_devname, vn.vn_devname,
+ sizeof(vn_compat->vn_devname));
+ return (0);
+}
+
+__sym_compat(procstat_get_pts_info, freebsd11_procstat_get_pts_info, FBSD_1.2);
+__sym_compat(procstat_get_socket_info, freebsd11_procstat_get_socket_info,
+ FBSD_1.2);
+__sym_compat(procstat_get_vnode_info, freebsd11_procstat_get_vnode_info,
+ FBSD_1.2);
+__sym_compat(procstat_get_sem_info, freebsd11_procstat_get_sem_info, FBSD_1.3);
+__sym_compat(procstat_get_shm_info, freebsd11_procstat_get_shm_info, FBSD_1.3);
+__sym_compat(procstat_get_pts_info, freebsd12_procstat_get_pts_info, FBSD_1.5);
+__sym_compat(procstat_get_vnode_info, freebsd12_procstat_get_vnode_info,
+ FBSD_1.5);
diff --git a/lib/libprocstat/libprocstat_internal.h b/lib/libprocstat/libprocstat_internal.h
new file mode 100644
index 000000000000..df3f8dd99334
--- /dev/null
+++ b/lib/libprocstat/libprocstat_internal.h
@@ -0,0 +1,42 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2009 Stanislav Sedov <stas@FreeBSD.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef _LIBPROCSTAT_INTERNAL_H_
+#define _LIBPROCSTAT_INTERNAL_H_
+
+struct procstat {
+ int type;
+ kvm_t *kd;
+ void *vmentries;
+ void *files;
+ void *argv;
+ void *envv;
+ struct procstat_core *core;
+};
+
+#endif /* !_LIBPROCSTAT_INTERNAL_H_ */
diff --git a/lib/libprocstat/msdosfs.c b/lib/libprocstat/msdosfs.c
new file mode 100644
index 000000000000..01345bb8d334
--- /dev/null
+++ b/lib/libprocstat/msdosfs.c
@@ -0,0 +1,147 @@
+/*-
+ * SPDX-License-Identifier: BSD-4-Clause
+ *
+ * Copyright (c) 2000 Peter Edwards
+ * Copyright (c) 1988, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * This code is derived from software contributed to Berkeley by Peter Edwards
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by the University of
+ * California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+#include <stdbool.h>
+
+#include <sys/param.h>
+#include <sys/buf.h>
+#include <sys/time.h>
+#include <sys/stat.h>
+#include <sys/vnode.h>
+#include <sys/mount.h>
+
+#include <netinet/in.h>
+
+#define _WANT_MSDOSFS_INTERNALS
+#include <fs/msdosfs/bpb.h>
+#include <fs/msdosfs/msdosfsmount.h>
+#include <fs/msdosfs/direntry.h>
+#include <fs/msdosfs/denode.h>
+#include <fs/msdosfs/fat.h>
+
+#include <err.h>
+#include <kvm.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "libprocstat.h"
+#include "common_kvm.h"
+
+struct dosmount {
+ struct dosmount *next;
+ struct msdosfsmount *kptr; /* Pointer in kernel space */
+ struct msdosfsmount data; /* User space copy of structure */
+};
+
+int
+msdosfs_filestat(kvm_t *kd, struct vnode *vp, struct vnstat *vn)
+{
+ struct denode denode;
+ static struct dosmount *mounts;
+ struct dosmount *mnt;
+ u_long dirsperblk;
+ int fileid;
+
+ if (!kvm_read_all(kd, (unsigned long)VTODE(vp), &denode,
+ sizeof(denode))) {
+ warnx("can't read denode at %p", (void *)VTODE(vp));
+ return (1);
+ }
+
+ /*
+ * Find msdosfsmount structure for the vnode's filesystem. Needed
+ * for some filesystem parameters
+ */
+ for (mnt = mounts; mnt; mnt = mnt->next)
+ if (mnt->kptr == denode.de_pmp)
+ break;
+
+ if (!mnt) {
+ if ((mnt = malloc(sizeof(struct dosmount))) == NULL) {
+ warn("malloc()");
+ return (1);
+ }
+ if (!kvm_read_all(kd, (unsigned long)denode.de_pmp,
+ &mnt->data, sizeof(mnt->data))) {
+ free(mnt);
+ warnx("can't read mount info at %p",
+ (void *)denode.de_pmp);
+ return (1);
+ }
+ mnt->next = mounts;
+ mounts = mnt;
+ mnt->kptr = denode.de_pmp;
+ }
+
+ vn->vn_fsid = dev2udev(kd, mnt->data.pm_dev);
+ vn->vn_mode = 0555;
+ vn->vn_mode |= denode.de_Attributes & ATTR_READONLY ? 0 : 0222;
+ vn->vn_mode &= mnt->data.pm_mask;
+
+ /* Distinguish directories and files. No "special" files in FAT. */
+ vn->vn_mode |= denode.de_Attributes & ATTR_DIRECTORY ? S_IFDIR : S_IFREG;
+ vn->vn_size = denode.de_FileSize;
+
+ /*
+ * XXX -
+ * Culled from msdosfs_vnops.c. There appears to be a problem
+ * here, in that a directory has the same inode number as the first
+ * file in the directory. stat(2) suffers from this problem also, so
+ * I won't try to fix it here.
+ *
+ * The following computation of the fileid must be the same as that
+ * used in msdosfs_readdir() to compute d_fileno. If not, pwd
+ * doesn't work.
+ */
+ dirsperblk = mnt->data.pm_BytesPerSec / sizeof(struct direntry);
+ if (denode.de_Attributes & ATTR_DIRECTORY) {
+ fileid = cntobn(&mnt->data, denode.de_StartCluster)
+ * dirsperblk;
+ if (denode.de_StartCluster == MSDOSFSROOT)
+ fileid = 1;
+ } else {
+ fileid = cntobn(&mnt->data, denode.de_dirclust) * dirsperblk;
+ if (denode.de_dirclust == MSDOSFSROOT)
+ fileid = roottobn(&mnt->data, 0) * dirsperblk;
+ fileid += denode.de_diroffset / sizeof(struct direntry);
+ }
+
+ vn->vn_fileid = fileid;
+ return (0);
+}
diff --git a/lib/libprocstat/smbfs.c b/lib/libprocstat/smbfs.c
new file mode 100644
index 000000000000..cbd32cf8f597
--- /dev/null
+++ b/lib/libprocstat/smbfs.c
@@ -0,0 +1,78 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2005-2009 Stanislav Sedov <stas@FreeBSD.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+#include <sys/cdefs.h>
+#include <stdbool.h>
+
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <sys/vnode.h>
+#define _WANT_MOUNT
+#include <sys/mount.h>
+
+#include <netinet/in.h>
+
+#include <assert.h>
+#include <err.h>
+#include <kvm.h>
+#include <stdlib.h>
+
+#include <fs/smbfs/smbfs.h>
+#include <fs/smbfs/smbfs_node.h>
+
+#include "libprocstat.h"
+#include "common_kvm.h"
+
+int
+smbfs_filestat(kvm_t *kd, struct vnode *vp, struct vnstat *vn)
+{
+ struct smbnode node;
+ struct mount mnt;
+ int error;
+
+ assert(kd);
+ assert(vn);
+ error = kvm_read_all(kd, (unsigned long)VTOSMB(vp), &node,
+ sizeof(node));
+ if (error != 0) {
+ warnx("can't read smbfs fnode at %p", (void *)VTOSMB(vp));
+ return (1);
+ }
+ error = kvm_read_all(kd, (unsigned long)getvnodemount(vp), &mnt,
+ sizeof(mnt));
+ if (error != 0) {
+ warnx("can't read mount at %p for vnode %p",
+ (void *)getvnodemount(vp), vp);
+ return (1);
+ }
+ vn->vn_fileid = node.n_ino;
+ if (vn->vn_fileid == 0)
+ vn->vn_fileid = 2;
+ vn->vn_fsid = mnt.mnt_stat.f_fsid.val[0];
+ return (0);
+}
diff --git a/lib/libprocstat/udf.c b/lib/libprocstat/udf.c
new file mode 100644
index 000000000000..3298af6dec5f
--- /dev/null
+++ b/lib/libprocstat/udf.c
@@ -0,0 +1,101 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2005-2009 Stanislav Sedov <stas@FreeBSD.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+#include <sys/cdefs.h>
+#include <stdbool.h>
+
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <sys/vnode.h>
+#include <sys/mount.h>
+
+#include <netinet/in.h>
+
+#include <assert.h>
+#include <err.h>
+#include <kvm.h>
+#include <stdlib.h>
+
+#include <fs/udf/ecma167-udf.h>
+
+#include "libprocstat.h"
+#include "common_kvm.h"
+
+/* XXX */
+struct udf_mnt {
+ int im_flags;
+ struct mount *im_mountp;
+ struct g_consumer *im_cp;
+ struct bufobj *im_bo;
+ struct cdev *im_dev;
+ struct vnode *im_devvp;
+ int bsize;
+ int bshift;
+ int bmask;
+ uint32_t part_start;
+ uint32_t part_len;
+ uint64_t root_id;
+ struct long_ad root_icb;
+ int p_sectors;
+ int s_table_entries;
+ void *s_table;
+ void *im_d2l;
+};
+struct udf_node {
+ struct vnode *i_vnode;
+ struct udf_mnt *udfmp;
+ ino_t hash_id;
+ long diroff;
+ struct file_entry *fentry;
+};
+#define VTON(vp) ((struct udf_node *)((vp)->v_data))
+
+int
+udf_filestat(kvm_t *kd, struct vnode *vp, struct vnstat *vn)
+{
+ struct udf_node node;
+ struct udf_mnt mnt;
+ int error;
+
+ assert(kd);
+ assert(vn);
+ error = kvm_read_all(kd, (unsigned long)VTON(vp), &node, sizeof(node));
+ if (error != 0) {
+ warnx("can't read udf fnode at %p", (void *)VTON(vp));
+ return (1);
+ }
+ error = kvm_read_all(kd, (unsigned long)node.udfmp, &mnt, sizeof(mnt));
+ if (error != 0) {
+ warnx("can't read udf_mnt at %p for vnode %p",
+ (void *)node.udfmp, vp);
+ return (1);
+ }
+ vn->vn_fileid = node.hash_id;
+ vn->vn_fsid = dev2udev(kd, mnt.im_dev);
+ return (0);
+}
diff --git a/lib/libprocstat/zfs.c b/lib/libprocstat/zfs.c
new file mode 100644
index 000000000000..6dc23d7573fd
--- /dev/null
+++ b/lib/libprocstat/zfs.c
@@ -0,0 +1,112 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2007 Ulf Lilleengen
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/param.h>
+#define _WANT_MOUNT
+#include <sys/mount.h>
+#include <sys/queue.h>
+#include <sys/stat.h>
+#include <sys/sysctl.h>
+#include <sys/time.h>
+#include <sys/vnode.h>
+#define _WANT_ZNODE
+#include <sys/zfs_context.h>
+#include <sys/zfs_znode.h>
+
+#include <netinet/in.h>
+
+#include <err.h>
+#include <kvm.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#define ZFS
+#include "libprocstat.h"
+#include "common_kvm.h"
+
+int
+zfs_filestat(kvm_t *kd, struct vnode *vp, struct vnstat *vn)
+{
+
+ struct mount mount, *mountptr;
+ znode_t *kznodeptr, *znode;
+ size_t len;
+ int size;
+
+ len = sizeof(size);
+ if (sysctlbyname("debug.sizeof.znode", &size, &len, NULL, 0) == -1) {
+ warnx("error getting sysctl");
+ return (1);
+ }
+ znode = malloc(size);
+ if (znode == NULL) {
+ warnx("error allocating memory for znode storage");
+ return (1);
+ }
+
+ if ((size_t)size != sizeof(znode_t))
+ warnx("znode_t size mismatch, data could be wrong");
+
+ if ((size_t)size < offsetof(znode_t, z_id) + sizeof(znode->z_id) ||
+ (size_t)size < offsetof(znode_t, z_mode) + sizeof(znode->z_mode) ||
+ (size_t)size < offsetof(znode_t, z_size) + sizeof(znode->z_size)) {
+ warnx("znode_t size is too small");
+ goto bad;
+ }
+
+ /*
+ * OpenZFS's libspl provides a dummy sys/vnode.h that shadows ours so
+ * struct vnode is an incomplete type. Use the wrapper until that is
+ * resolved.
+ */
+ kznodeptr = getvnodedata(vp);
+ if (!kvm_read_all(kd, (unsigned long)kznodeptr, znode, (size_t)size)) {
+ warnx("can't read znode at %p", (void *)kznodeptr);
+ goto bad;
+ }
+
+ /* Get the mount pointer, and read from the address. */
+ mountptr = getvnodemount(vp);
+ if (!kvm_read_all(kd, (unsigned long)mountptr, &mount, sizeof(mount))) {
+ warnx("can't read mount at %p", (void *)mountptr);
+ goto bad;
+ }
+
+ /*
+ * XXX Assume that this is a znode, but it can be a special node
+ * under .zfs/.
+ */
+ vn->vn_fsid = mount.mnt_stat.f_fsid.val[0];
+ vn->vn_fileid = znode->z_id;
+ vn->vn_mode = znode->z_mode;
+ vn->vn_size = znode->z_size;
+ return (0);
+bad:
+ return (1);
+}