Index: head/lib/Makefile =================================================================== --- head/lib/Makefile (revision 221806) +++ head/lib/Makefile (revision 221807) @@ -1,237 +1,238 @@ # @(#)Makefile 8.1 (Berkeley) 6/4/93 # $FreeBSD$ .include # To satisfy shared library or ELF linkage when only the libraries being # built are visible: # # csu must be built before all shared libaries for ELF. # libc must be built before all other shared libraries. # libbsm must be built before ibauditd. # libcom_err must be built before libpam. # libcrypt must be built before libpam. # libkvm must be built before libdevstat. # msun must be built before libg++ and libstdc++. # libmd must be built before libatm, libopie, libradius, and libtacplus. # ncurses must be built before libdialog, libedit and libreadline. # libnetgraph must be built before libbsnmp/modules/snmp_netgraph. # libopie must be built before libpam. # libradius must be built before libpam. # librpcsvc must be built before libpam. # libsbuf must be built before libcam. # libtacplus must be built before libpam. # libutil must be built before libpam. # libypclnt must be built before libpam. # libgssapi must be built before librpcsec_gss # # Otherwise, the SUBDIR list should be in alphabetical order. # # Except it appears bind needs to be compiled last SUBDIR_ORDERED= ${_csu} \ libc \ libbsm \ libauditd \ libcom_err \ libcompiler_rt \ libcrypt \ libelf \ ${_libiconv_modules} \ libkvm \ msun \ libmd \ ncurses \ ${_libnetgraph} \ libradius \ librpcsvc \ libsbuf \ libtacplus \ libutil \ ${_libypclnt} SUBDIR= ${SUBDIR_ORDERED} \ libalias \ libarchive \ ${_libatm} \ libbegemot \ libblocksruntime \ ${_libbluetooth} \ ${_libbsnmp} \ libbz2 \ libcalendar \ libcam \ libcompat \ libdevinfo \ libdevstat \ libdisk \ libdwarf \ libedit \ ${_libefi} \ libexpat \ libfetch \ libftpio \ libgeom \ ${_libgpib} \ ${_libgssapi} \ ${_librpcsec_gss} \ libipsec \ ${_libipx} \ libjail \ libkiconv \ liblzma \ libmagic \ libmemstat \ ${_libmilter} \ ${_libmp} \ ${_libncp} \ ${_libngatm} \ libopie \ libpam \ libpcap \ ${_libpkg} \ ${_libpmc} \ ${_libproc} \ + libprocstat \ librt \ ${_librtld_db} \ ${_libsdp} \ ${_libsm} \ ${_libsmb} \ ${_libsmdb} \ ${_libsmutil} \ libstand \ ${_libtelnet} \ ${_libthr} \ libthread_db \ libufs \ libugidfw \ libulog \ ${_libusbhid} \ ${_libusb} \ ${_libvgl} \ libwrap \ liby \ libz \ ${_bind} \ ${_clang} .if exists(${.CURDIR}/csu/${MACHINE_ARCH}-elf) _csu=csu/${MACHINE_ARCH}-elf .elif exists(${.CURDIR}/csu/${MACHINE_ARCH}) _csu=csu/${MACHINE_ARCH} .elif exists(${.CURDIR}/csu/${MACHINE_CPUARCH}/Makefile) _csu=csu/${MACHINE_CPUARCH} .else _csu=csu .endif # NB: keep these sorted by MK_* knobs .if ${MK_ATM} != "no" _libngatm= libngatm .endif .if ${MK_BIND} != "no" _bind= bind .endif .if ${MK_BLUETOOTH} != "no" _libbluetooth= libbluetooth _libsdp= libsdp .endif .if ${MK_BSNMP} != "no" _libbsnmp= libbsnmp .endif .if ${MK_CLANG} != "no" && !defined(COMPAT_32BIT) _clang= clang .endif .if ${MK_GPIB} != "no" _libgpib= libgpib .endif .if ${MK_GSSAPI} != "no" _libgssapi= libgssapi _librpcsec_gss= librpcsec_gss .endif .if ${MK_ICONV} != "no" _libiconv_modules= libiconv_modules .endif .if ${MK_IPX} != "no" _libipx= libipx .endif .if ${MK_LIBTHR} != "no" _libthr= libthr .endif .if ${MK_NETGRAPH} != "no" _libnetgraph= libnetgraph .endif .if ${MK_NIS} != "no" _libypclnt= libypclnt .endif .if ${MACHINE_CPUARCH} == "i386" || ${MACHINE_CPUARCH} == "amd64" .if ${MK_NCP} != "no" _libncp= libncp .endif _libsmb= libsmb _libvgl= libvgl _libproc= libproc _librtld_db= librtld_db .endif .if ${MACHINE_CPUARCH} == "ia64" _libefi= libefi _libsmb= libsmb .endif .if ${MACHINE_CPUARCH} == "amd64" .if ${MK_NCP} != "no" _libncp= libncp .endif .endif .if ${MACHINE_CPUARCH} == "powerpc" _libsmb= libsmb .endif .if ${MACHINE_CPUARCH} == "sparc64" _libsmb= libsmb .endif .if ${MK_OPENSSL} != "no" _libmp= libmp .endif .if ${MK_PMC} != "no" _libpmc= libpmc .endif .if ${MK_PKGTOOLS} != "no" _libpkg= libpkg .endif .if ${MK_SENDMAIL} != "no" _libmilter= libmilter _libsm= libsm _libsmdb= libsmdb _libsmutil= libsmutil .endif .if ${MK_TELNET} != "no" _libtelnet= libtelnet .endif .if ${MK_USB} != "no" _libusbhid= libusbhid _libusb= libusb .endif .include Index: head/lib/libprocstat/Makefile =================================================================== --- head/lib/libprocstat/Makefile (nonexistent) +++ head/lib/libprocstat/Makefile (revision 221807) @@ -0,0 +1,36 @@ +# $FreeBSD$ + +.include + +LIB= procstat + +SRCS= cd9660.c \ + common_kvm.c \ + libprocstat.c \ + msdosfs.c \ + ntfs.c \ + nwfs.c \ + smbfs.c \ + udf.c + +INCS= libprocstat.h +CFLAGS+= -I. -I${.CURDIR} -D_KVM_VNODE +SHLIB_MAJOR= 1 +WITHOUT_MAN= yes + +# XXX This is a hack. +.if ${MK_CDDL} != "no" +CFLAGS+= -DZFS +OBJS+= zfs/zfs.o +SOBJS+= zfs/zfs.So +POBJS+= zfs/zfs.po +SUBDIR= zfs +zfs/zfs.o: .PHONY + @cd ${.CURDIR}/zfs && ${MAKE} zfs.o +zfs/zfs.So: .PHONY + @cd ${.CURDIR}/zfs && ${MAKE} zfs.So +zfs/zfs.po: .PHONY + @cd ${.CURDIR}/zfs && ${MAKE} zfs.po +.endif + +.include Property changes on: head/lib/libprocstat/Makefile ___________________________________________________________________ Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Index: head/lib/libprocstat/cd9660.c =================================================================== --- head/lib/libprocstat/cd9660.c (nonexistent) +++ head/lib/libprocstat/cd9660.c (revision 221807) @@ -0,0 +1,90 @@ +/* + * 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 +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include +#include + +#include + +#include + +#include +#define _KERNEL +#include +#undef _KERNEL + +#include +#include + +#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 = (long)isonode.i_number; + vn->vn_size = (u_long)isonode.i_size; + return (0); +} Property changes on: head/lib/libprocstat/cd9660.c ___________________________________________________________________ Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Index: head/lib/libprocstat/common_kvm.c =================================================================== --- head/lib/libprocstat/common_kvm.c (nonexistent) +++ head/lib/libprocstat/common_kvm.c (revision 221807) @@ -0,0 +1,207 @@ +/*- + * Copyright (c) 2009 Stanislav Sedov + * 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 +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include +#include +#define _KERNEL +#include +#include +#include +#include +#include +#include +#undef _KERNEL +#include +#include +#include + +#include +#include +#include +#include +#include + +#include +#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_namebuf, SPECNAMELEN + 1); + return (0); +} + +int +ufs_filestat(kvm_t *kd, struct vnode *vp, struct vnstat *vn) +{ + struct inode inode; + + if (!kvm_read_all(kd, (unsigned long)VTOI(vp), &inode, sizeof(inode))) { + warnx("can't read inode at %p", (void *)VTOI(vp)); + 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, inode.i_dev); + vn->vn_fileid = (long)inode.i_number; + vn->vn_mode = (mode_t)inode.i_mode; + vn->vn_size = (u_long)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); +} Property changes on: head/lib/libprocstat/common_kvm.c ___________________________________________________________________ Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Index: head/lib/libprocstat/common_kvm.h =================================================================== --- head/lib/libprocstat/common_kvm.h (nonexistent) +++ head/lib/libprocstat/common_kvm.h (revision 221807) @@ -0,0 +1,53 @@ +/*- + * Copyright (c) 2009 Stanislav Sedov + * 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. + * + * $FreeBSD$ + */ + +#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 ntfs_filestat(kvm_t *kd, struct vnode *vp, struct vnstat *vn); +int nwfs_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_ */ Property changes on: head/lib/libprocstat/common_kvm.h ___________________________________________________________________ Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Index: head/lib/libprocstat/libprocstat.c =================================================================== --- head/lib/libprocstat/libprocstat.c (nonexistent) +++ head/lib/libprocstat/libprocstat.c (revision 221807) @@ -0,0 +1,1306 @@ +/*- + * Copyright (c) 2009 Stanislav Sedov + * 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 +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define _WANT_FILE +#include +#include +#define _KERNEL +#include +#include +#include +#include +#include +#include +#undef _KERNEL +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include "libprocstat_internal.h" +#include "common_kvm.h" + +int statfs(const char *, struct statfs *); /* XXX */ + +#define PROCSTAT_KVM 1 +#define PROCSTAT_SYSCTL 2 + +static char *getmnton(kvm_t *kd, struct mount *m); +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_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 int vntype2psfsttype(int type); + +void +procstat_close(struct procstat *procstat) +{ + + assert(procstat); + if (procstat->type == PROCSTAT_KVM) + kvm_close(procstat->kd); +} + +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 kinfo_proc * +procstat_getprocs(struct procstat *procstat, int what, int arg, + unsigned int *count) +{ + struct kinfo_proc *p0, *p; + size_t len; + int name[4]; + int error; + + assert(procstat); + assert(count); + p = NULL; + if (procstat->type == PROCSTAT_KVM) { + p0 = kvm_getprocs(procstat->kd, what, arg, count); + if (p0 == NULL || count == 0) + return (NULL); + len = *count * sizeof(*p); + p = malloc(len); + if (p == NULL) { + warnx("malloc(%zd)", 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, 4, NULL, &len, NULL, 0); + if (error < 0 && errno != EPERM) { + warn("sysctl(kern.proc)"); + goto fail; + } + if (len == 0) { + warnx("no processes?"); + goto fail; + } + p = malloc(len); + if (p == NULL) { + warnx("malloc(%zd)", len); + goto fail; + } + error = sysctl(name, 4, p, &len, NULL, 0); + 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"); + goto fail; + } + *count = len / sizeof(*p); + return (p); + } else { + warnx("unknown access method"); + 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) +{ + + if (procstat->type == PROCSTAT_SYSCTL) + return (procstat_getfiles_sysctl(procstat, kp, mmapped)); + else if (procstat->type == PROCSTAT_KVM) + return (procstat_getfiles_kvm(procstat, kp, mmapped)); + else + 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) +{ + 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; + 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 struct filestat_list * +procstat_getfiles_kvm(struct procstat *procstat, struct kinfo_proc *kp, int mmapped) +{ + struct file file; + struct filedesc filed; + struct vm_map_entry vmentry; + struct vm_object object; + struct vmspace vmspace; + vm_map_entry_t entryp; + vm_map_t map; + vm_object_t objp; + struct vnode *vp; + struct file **ofiles; + struct filestat *entry; + struct filestat_list *head; + kvm_t *kd; + void *data; + int i, fflags; + int prot, type; + unsigned int nfiles; + + assert(procstat); + kd = procstat->kd; + if (kd == NULL) + return (NULL); + if (kp->ki_fd == 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); + } + + /* + * Allocate list head. + */ + head = malloc(sizeof(*head)); + if (head == NULL) + return (NULL); + STAILQ_INIT(head); + + /* root directory vnode, if one. */ + if (filed.fd_rdir) { + entry = filestat_new_entry(filed.fd_rdir, PS_FST_TYPE_VNODE, -1, + PS_FST_FFLAG_READ, PS_FST_UFLAG_RDIR, 0, 0, NULL); + if (entry != NULL) + STAILQ_INSERT_TAIL(head, entry, next); + } + /* current working directory vnode. */ + if (filed.fd_cdir) { + entry = filestat_new_entry(filed.fd_cdir, PS_FST_TYPE_VNODE, -1, + PS_FST_FFLAG_READ, PS_FST_UFLAG_CDIR, 0, 0, NULL); + if (entry != NULL) + STAILQ_INSERT_TAIL(head, entry, next); + } + /* jail root, if any. */ + if (filed.fd_jdir) { + entry = filestat_new_entry(filed.fd_jdir, PS_FST_TYPE_VNODE, -1, + PS_FST_FFLAG_READ, PS_FST_UFLAG_JAIL, 0, 0, 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); + 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); + 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); + if (entry != NULL) + STAILQ_INSERT_TAIL(head, entry, next); + } + + nfiles = filed.fd_lastfile + 1; + ofiles = malloc(nfiles * sizeof(struct file *)); + if (ofiles == NULL) { + warn("malloc(%zd)", nfiles * sizeof(struct file *)); + goto do_mmapped; + } + if (!kvm_read_all(kd, (unsigned long)filed.fd_ofiles, ofiles, + nfiles * sizeof(struct file *))) { + warnx("cannot read file structures at %p", + (void *)filed.fd_ofiles); + free(ofiles); + goto do_mmapped; + } + for (i = 0; i <= filed.fd_lastfile; i++) { + if (ofiles[i] == NULL) + continue; + if (!kvm_read_all(kd, (unsigned long)ofiles[i], &file, + sizeof(struct file))) { + warnx("can't read file %d at %p", i, + (void *)ofiles[i]); + 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 + default: + continue; + } + entry = filestat_new_entry(data, type, i, + to_filestat_flags(file.f_flag), 0, 0, 0, NULL); + if (entry != NULL) + STAILQ_INSERT_TAIL(head, entry, next); + } + free(ofiles); + +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; + } + map = &vmspace.vm_map; + + for (entryp = map->header.next; + entryp != &kp->ki_vmspace->vm_map.header; + entryp = vmentry.next) { + if (!kvm_read_all(kd, (unsigned long)entryp, &vmentry, + sizeof(vmentry))) { + warnx("can't read vm_map_entry at %p", + (void *)entryp); + continue; + } + 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 (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); + if (entry != NULL) + STAILQ_INSERT_TAIL(head, entry, next); + } + } +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_CRYPTO, PS_FST_TYPE_CRYPTO }, + { 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_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 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; + + assert(kp); + if (kp->ki_fd == NULL) + return (NULL); + + files = kinfo_getfile(kp->ki_pid, &cnt); + 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; + + /* + * Create filestat entry. + */ + entry = filestat_new_entry(kif, type, fd, fflags, uflags, + refcount, offset, path); + if (entry != NULL) + STAILQ_INSERT_TAIL(head, entry, next); + } + if (mmapped != 0) { + vmentries = kinfo_getvmmap(kp->ki_pid, &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_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); + 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) { + return (procstat_get_pipe_info_sysctl(fst, ps, errbuf)); + } else { + warnx("unknow access method: %d", procstat->type); + 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: + 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) { + return (procstat_get_pts_info_sysctl(fst, pts, errbuf)); + } else { + warnx("unknow access method: %d", procstat->type); + 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: + 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_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) { + return (procstat_get_vnode_info_sysctl(fst, vn, errbuf)); + } else { + warnx("unknow access method: %d", procstat->type); + 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(ntfs), + FSTYPE(nwfs), + FSTYPE(smbfs), + FSTYPE(udf), + FSTYPE(ufs), +#ifdef ZFS + FSTYPE(zfs), +#endif + }; +#define NTYPES (sizeof(fstypes) / sizeof(*fstypes)) + struct vnode vnode; + char tagstr[12]; + void *vp; + int error, found; + 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_tag, tagstr, + sizeof(tagstr)); + if (error == 0) { + warnx("can't read v_tag at %p", (void *)vp); + goto fail; + } + tagstr[sizeof(tagstr) - 1] = '\0'; + + /* + * Find appropriate handler. + */ + for (i = 0, found = 0; i < NTYPES; i++) + if (!strcmp(fstypes[i].tag, tagstr)) { + if (fstypes[i].handler(kd, &vnode, vn) != 0) { + goto fail; + } + break; + } + if (i == NTYPES) { + snprintf(errbuf, _POSIX2_LINE_MAX, "?(%s)", tagstr); + return (1); + } + vn->vn_mntdir = getmnton(kd, vnode.v_mount); + if ((vnode.v_type == VBLK || vnode.v_type == VCHR) && + 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: + 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; + uint64_t fileid; + uint64_t size; + char *name, *path; + uint32_t fsid; + uint16_t mode; + uint32_t rdev; + 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) { + 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) { + return (procstat_get_socket_info_sysctl(fst, sock, errbuf)); + } else { + warnx("unknow access method: %d", procstat->type); + 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 inpcb inpcb; + 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; + + /* + * Protocol specific data. + */ + switch(dom.dom_family) { + case AF_INET: + case AF_INET6: + if (proto.pr_protocol == IPPROTO_TCP) { + if (s.so_pcb) { + if (kvm_read(kd, (u_long)s.so_pcb, + (char *)&inpcb, sizeof(struct inpcb)) + != sizeof(struct inpcb)) { + warnx("can't read inpcb at %p", + (void *)s.so_pcb); + } else + sock->inp_ppcb = + (uintptr_t)inpcb.inp_ppcb; + } + } + break; + 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->so_rcv_sb_state = s.so_rcv.sb_state; + sock->so_snd_sb_state = s.so_snd.sb_state; + sock->unp_conn = (uintptr_t)unpcb.unp_conn; + } + } + break; + default: + break; + } + return (0); + +fail: + 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_sa_local, &sock->sa_local, kif->kf_sa_local.ss_len); + bcopy(&kif->kf_sa_peer, &sock->sa_peer, kif->kf_sa_peer.ss_len); + + /* + * Protocol specific data. + */ + switch(sock->dom_family) { + case AF_INET: + case AF_INET6: + if (sock->proto == IPPROTO_TCP) + sock->inp_ppcb = kif->kf_un.kf_sock.kf_sock_inpcb; + 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; + } + 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) +{ + static 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); + mnt.mnt_stat.f_mntonname[MNAMELEN] = '\0'; + mt->next = mhead; + mhead = mt; + return (mt->mntonname); +} Property changes on: head/lib/libprocstat/libprocstat.c ___________________________________________________________________ Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Index: head/lib/libprocstat/libprocstat.h =================================================================== --- head/lib/libprocstat/libprocstat.h (nonexistent) +++ head/lib/libprocstat/libprocstat.h (revision 221807) @@ -0,0 +1,160 @@ +/*- + * Copyright (c) 2009 Stanislav Sedov + * 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. + * + * $FreeBSD$ + */ + +#ifndef _LIBPROCSTAT_H_ +#define _LIBPROCSTAT_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 +#define 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 + +/* + * 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 + +struct procstat; +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; +}; +struct 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 + 1]; +}; +struct ptsstat { + uint32_t dev; + char devname[SPECNAMELEN + 1]; +}; +struct pipestat { + size_t buffer_cnt; + uint64_t addr; + uint64_t peer; +}; +struct 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]; +}; + +STAILQ_HEAD(filestat_list, filestat); + +void procstat_close(struct procstat *procstat); +void procstat_freeprocs(struct procstat *procstat, struct kinfo_proc *p); +void procstat_freefiles(struct procstat *procstat, + struct filestat_list *head); +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); +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_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); +struct procstat *procstat_open_sysctl(void); +struct procstat *procstat_open_kvm(const char *nlistf, const char *memf); + +#endif /* !_LIBPROCSTAT_H_ */ Property changes on: head/lib/libprocstat/libprocstat.h ___________________________________________________________________ Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Index: head/lib/libprocstat/libprocstat_internal.h =================================================================== --- head/lib/libprocstat/libprocstat_internal.h (nonexistent) +++ head/lib/libprocstat/libprocstat_internal.h (revision 221807) @@ -0,0 +1,39 @@ +/*- + * Copyright (c) 2009 Stanislav Sedov + * 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. + * + * $FreeBSD$ + */ + +#ifndef _LIBPROCSTAT_INTERNAL_H_ +#define _LIBPROCSTAT_INTERNAL_H_ + +struct procstat { + int type; + kvm_t *kd; + void *vmentries; + void *files; +}; + +#endif /* !_LIBPROCSTAT_INTERNAL_H_ */ Property changes on: head/lib/libprocstat/libprocstat_internal.h ___________________________________________________________________ Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Index: head/lib/libprocstat/msdosfs.c =================================================================== --- head/lib/libprocstat/msdosfs.c (nonexistent) +++ head/lib/libprocstat/msdosfs.c (revision 221807) @@ -0,0 +1,153 @@ +/* + * 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 +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include + +#include + +#define _KERNEL +#include +#include +#include +#undef _KERNEL + +#include +#include +#include + +#include +#include +#include +#include + +/* + * XXX - + * VTODE is defined in denode.h only if _KERNEL is defined, but that leads to + * header explosion + */ +#define VTODE(vp) ((struct denode *)getvnodedata(vp)) + +#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); +} Property changes on: head/lib/libprocstat/msdosfs.c ___________________________________________________________________ Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Index: head/lib/libprocstat/ntfs.c =================================================================== --- head/lib/libprocstat/ntfs.c (nonexistent) +++ head/lib/libprocstat/ntfs.c (revision 221807) @@ -0,0 +1,71 @@ +/*- + * Copyright (c) 2005-2009 Stanislav Sedov + * 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 +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include + +#include +#include + +#include "libprocstat.h" +#include "common_kvm.h" + +int +ntfs_filestat(kvm_t *kd, struct vnode *vp, struct vnstat *vn) +{ + struct fnode fnod; + struct ntnode node; + int error; + + assert(kd); + assert(vn); + error = kvm_read_all(kd, (unsigned long)VTOF(vp), &fnod, sizeof(fnod)); + if (error != 0) { + warnx("can't read ntfs fnode at %p", (void *)VTOF(vp)); + return (1); + } + error = kvm_read_all(kd, (unsigned long)FTONT(&fnod), &node, + sizeof(node)); + if (error != 0) { + warnx("can't read ntfs node at %p", (void *)FTONT(&fnod)); + return (1); + } + vn->vn_fileid = node.i_number; + vn->vn_fsid = dev2udev(kd, node.i_dev); + return (0); +} Property changes on: head/lib/libprocstat/ntfs.c ___________________________________________________________________ Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Index: head/lib/libprocstat/nwfs.c =================================================================== --- head/lib/libprocstat/nwfs.c (nonexistent) +++ head/lib/libprocstat/nwfs.c (revision 221807) @@ -0,0 +1,76 @@ +/*- + * Copyright (c) 2005-2009 Stanislav Sedov + * 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 +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include +#define _KERNEL +#include +#undef _KERNEL + +#include + +#include +#include +#include +#include + +#include +#include + +#include "libprocstat.h" +#include "common_kvm.h" + +int +nwfs_filestat(kvm_t *kd, struct vnode *vp, struct vnstat *vn) +{ + struct mount mnt; + struct nwnode node; + int error; + + assert(kd); + assert(vn); + error = kvm_read_all(kd, (unsigned long)VTONW(vp), &node, sizeof(node)); + if (error != 0) { + warnx("can't read nwfs fnode at %p", (void *)VTONW(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_fid.f_id; + if (vn->vn_fileid == 0) + vn->vn_fileid = NWFS_ROOT_INO; + vn->vn_fsid = mnt.mnt_stat.f_fsid.val[0]; + return (0); +} Property changes on: head/lib/libprocstat/nwfs.c ___________________________________________________________________ Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Index: head/lib/libprocstat/smbfs.c =================================================================== --- head/lib/libprocstat/smbfs.c (nonexistent) +++ head/lib/libprocstat/smbfs.c (revision 221807) @@ -0,0 +1,77 @@ +/*- + * Copyright (c) 2005-2009 Stanislav Sedov + * 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 +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include +#define _KERNEL +#include +#undef _KERNEL + +#include + +#include +#include +#include +#include + +#include +#include + +#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); +} Property changes on: head/lib/libprocstat/smbfs.c ___________________________________________________________________ Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Index: head/lib/libprocstat/udf.c =================================================================== --- head/lib/libprocstat/udf.c (nonexistent) +++ head/lib/libprocstat/udf.c (revision 221807) @@ -0,0 +1,102 @@ +/*- + * Copyright (c) 2005-2009 Stanislav Sedov + * 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 +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include +#include +#define _KERNEL +#include +#undef _KERNEL + +#include + +#include +#include +#include +#include + +#include + +#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); +} Property changes on: head/lib/libprocstat/udf.c ___________________________________________________________________ Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Index: head/lib/libprocstat/zfs/Makefile =================================================================== --- head/lib/libprocstat/zfs/Makefile (nonexistent) +++ head/lib/libprocstat/zfs/Makefile (revision 221807) @@ -0,0 +1,23 @@ +# $FreeBSD$ + +.PATH: ${.CURDIR}/.. + +SRCS= zfs.c +OBJS= zfs.o +WARNS?= 1 + +CFLAGS+= -I${.CURDIR}/../../../sys/cddl/compat/opensolaris +CFLAGS+= -I${.CURDIR}/../../../cddl/compat/opensolaris/include +CFLAGS+= -I${.CURDIR}/../../../cddl/compat/opensolaris/lib/libumem +CFLAGS+= -I${.CURDIR}/../../../cddl/contrib/opensolaris/lib/libzpool/common +CFLAGS+= -I${.CURDIR}/../../../sys/cddl/contrib/opensolaris/uts/common/fs/zfs +CFLAGS+= -I${.CURDIR}/../../../sys/cddl/contrib/opensolaris/uts/common +CFLAGS+= -I${.CURDIR}/../../../sys/cddl/contrib/opensolaris/uts/common/sys +CFLAGS+= -I${.CURDIR}/../../../cddl/contrib/opensolaris/head +CFLAGS+= -I${.CURDIR}/.. +CFLAGS+= -DNEED_SOLARIS_BOOLEAN + +all: ${OBJS} +CLEANFILES= ${OBJS} + +.include Property changes on: head/lib/libprocstat/zfs/Makefile ___________________________________________________________________ Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Index: head/lib/libprocstat/zfs.c =================================================================== --- head/lib/libprocstat/zfs.c (nonexistent) +++ head/lib/libprocstat/zfs.c (revision 221807) @@ -0,0 +1,134 @@ +/*- + * 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. + * + * $FreeBSD$ + */ + +#include +#define _KERNEL +#include +#include +#undef _KERNEL +#include + +#undef lbolt +#undef lbolt64 +#undef gethrestime_sec +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include + +#define ZFS +#include "libprocstat.h" +#include "common_kvm.h" + +/* + * Offset calculations that are used to get data from znode without having the + * definition. + */ +#define LOCATION_ZID (2 * sizeof(void *)) +#define LOCATION_ZPHYS(zsize) ((zsize) - (2 * sizeof(void *) + sizeof(struct task))) + +int +zfs_filestat(kvm_t *kd, struct vnode *vp, struct vnstat *vn) +{ + + znode_phys_t zphys; + struct mount mount, *mountptr; + uint64_t *zid; + void *znodeptr, *vnodeptr; + char *dataptr; + void *zphys_addr; + size_t len; + int size; + + len = sizeof(size); + if (sysctlbyname("debug.sizeof.znode", &size, &len, NULL, 0) == -1) { + warnx("error getting sysctl"); + return (1); + } + znodeptr = malloc(size); + if (znodeptr == NULL) { + warnx("error allocating memory for znode storage"); + return (1); + } + /* Since we have problems including vnode.h, we'll use the wrappers. */ + vnodeptr = getvnodedata(vp); + if (!kvm_read_all(kd, (unsigned long)vnodeptr, znodeptr, + (size_t)size)) { + warnx("can't read znode at %p", (void *)vnodeptr); + goto bad; + } + + /* + * z_id field is stored in the third pointer. We therefore skip the two + * first bytes. + * + * Pointer to the z_phys structure is the next last pointer. Therefore + * go back two bytes from the end. + */ + dataptr = znodeptr; + zid = (uint64_t *)(dataptr + LOCATION_ZID); + zphys_addr = *(void **)(dataptr + LOCATION_ZPHYS(size)); + + if (!kvm_read_all(kd, (unsigned long)zphys_addr, &zphys, + sizeof(zphys))) { + warnx("can't read znode_phys at %p", zphys_addr); + 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; + } + vn->vn_fsid = mount.mnt_stat.f_fsid.val[0]; + vn->vn_fileid = *zid; + /* + * XXX: Shows up wrong in output, but UFS has this error too. Could + * be that we're casting mode-variables from 64-bit to 8-bit or simply + * error in the mode-to-string function. + */ + vn->vn_mode = (mode_t)zphys.zp_mode; + vn->vn_size = (u_long)zphys.zp_size; + free(znodeptr); + return (0); +bad: + free(znodeptr); + return (1); +} Property changes on: head/lib/libprocstat/zfs.c ___________________________________________________________________ Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Index: head/lib/libutil/Makefile =================================================================== --- head/lib/libutil/Makefile (revision 221806) +++ head/lib/libutil/Makefile (revision 221807) @@ -1,69 +1,71 @@ # @(#)Makefile 8.1 (Berkeley) 6/4/93 # $FreeBSD$ SHLIBDIR?= /lib .include LIB= util SHLIB_MAJOR= 9 SRCS= _secure_path.c auth.c expand_number.c flopen.c fparseln.c gr_util.c \ - hexdump.c humanize_number.c kinfo_getfile.c kinfo_getvmmap.c kld.c \ + hexdump.c humanize_number.c kinfo_getfile.c kinfo_getfile.c \ + kinfo_getallproc.c kinfo_getproc.c kinfo_getvmmap.c kld.c \ login_auth.c login_cap.c \ login_class.c login_crypt.c login_ok.c login_times.c login_tty.c \ pidfile.c property.c pty.c pw_util.c quotafile.c realhostname.c \ stub.c trimdomain.c uucplock.c INCS= libutil.h login_cap.h CFLAGS+= -DLIBC_SCCS .if ${MK_INET6_SUPPORT} != "no" CFLAGS+= -DINET6 .endif CFLAGS+= -I${.CURDIR} -I${.CURDIR}/../libc/gen/ MAN+= kld.3 login_auth.3 login_tty.3 pty.3 \ login_cap.3 login_class.3 login_times.3 login_ok.3 \ _secure_path.3 uucplock.3 property.3 auth.3 realhostname.3 \ realhostname_sa.3 trimdomain.3 fparseln.3 humanize_number.3 \ pidfile.3 flopen.3 expand_number.3 hexdump.3 \ - kinfo_getfile.3 kinfo_getvmmap.3 quotafile.3 + kinfo_getfile.3 kinfo_getallproc.3 kinfo_getproc.3 \ + kinfo_getvmmap.3 quotafile.3 MAN+= login.conf.5 auth.conf.5 MLINKS+= kld.3 kld_isloaded.3 kld.3 kld_load.3 MLINKS+= property.3 properties_read.3 property.3 properties_free.3 MLINKS+= property.3 property_find.3 MLINKS+= auth.3 auth_getval.3 MLINKS+= pty.3 openpty.3 pty.3 forkpty.3 MLINKS+=login_cap.3 login_getclassbyname.3 login_cap.3 login_close.3 \ login_cap.3 login_getclass.3 login_cap.3 login_getuserclass.3 \ login_cap.3 login_getcapstr.3 login_cap.3 login_getcaplist.3 \ login_cap.3 login_getstyle.3 login_cap.3 login_getcaptime.3 \ login_cap.3 login_getcapnum.3 login_cap.3 login_getcapsize.3 \ login_cap.3 login_getcapbool.3 login_cap.3 login_getpath.3 \ login_cap.3 login_getpwclass.3 login_cap.3 login_setcryptfmt.3 MLINKS+=login_class.3 setusercontext.3 login_class.3 setclasscontext.3 \ login_class.3 setclassenvironment.3 login_class.3 setclassresources.3 MLINKS+=login_times.3 parse_lt.3 login_times.3 in_ltm.3 \ login_times.3 in_lt.3 login_times.3 in_ltms.3 \ login_times.3 in_lts.3 MLINKS+=login_ok.3 auth_ttyok.3 login_ok.3 auth_hostok.3 \ login_ok.3 auth_timeok.3 MLINKS+=login_auth.3 auth_checknologin.3 login_auth.3 auth_cat.3 MLINKS+=uucplock.3 uu_lock.3 uucplock.3 uu_lock_txfr.3 \ uucplock.3 uu_unlock.3 uucplock.3 uu_lockerr.3 MLINKS+=pidfile.3 pidfile_open.3 \ pidfile.3 pidfile_write.3 \ pidfile.3 pidfile_close.3 \ pidfile.3 pidfile_remove.3 MLINKS+=quotafile.3 quota_open.3 \ quotafile.3 quota_fsname.3 \ quotafile.3 quota_qfname.3 \ quotafile.3 quota_statfs.3 \ quotafile.3 quota_read.3 \ quotafile.3 quota_write_limits.3 \ quotafile.3 quota_write_usage.3 \ quotafile.3 quota_close.3 .include Index: head/lib/libutil/kinfo_getallproc.3 =================================================================== --- head/lib/libutil/kinfo_getallproc.3 (nonexistent) +++ head/lib/libutil/kinfo_getallproc.3 (revision 221807) @@ -0,0 +1,74 @@ +.\" +.\" Copyright (c) 2009 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. +.\" +.\" $FreeBSD$ +.\" +.Dd July 9, 2009 +.Os +.Dt KINFO_GETALLPROC 3 +.Sh NAME +.Nm kinfo_getallproc +.Nd function for getting process information of all processes from kernel +.Sh LIBRARY +.Lb libutil +.Sh SYNOPSIS +.In sys/types.h +.In libutil.h +.Ft struct kinfo_proc * +.Fn kinfo_getallproc "int *cntp" +.Sh DESCRIPTION +This function is used for obtaining process information of all processes from +the kernel. +.Pp +The +.Ar cntp +field is a pointer containing the number of process structures returned. +This function is a wrapper around +.Xr sysctl 3 +with the +.Dv KERN_PROC_PROC +mib. +While the kernel returns a packed structure, this function expands the +data into a fixed record format. +.Sh RETURN VALUES +On success the +.Fn kinfo_getallproc +function returns a pointer to +.Ar cntp +.Vt struct kinfo_proc +structures as defined by +.In sys/user.h . +The pointer was obtained by an internal call to +.Xr malloc 3 +and must be freed by the caller with a call to +.Xr free 3 . +On failure the +.Fn kinfo_getallproc +function returns +.Dv NULL . +.Sh SEE ALSO +.Xr free 3 , +.Xr malloc 3 , +.Xr sysctl 3 Property changes on: head/lib/libutil/kinfo_getallproc.3 ___________________________________________________________________ Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Index: head/lib/libutil/kinfo_getallproc.c =================================================================== --- head/lib/libutil/kinfo_getallproc.c (nonexistent) +++ head/lib/libutil/kinfo_getallproc.c (revision 221807) @@ -0,0 +1,98 @@ +/*- + * Copyright (c) 2007 Robert N. M. Watson + * Copyright (c) 2009 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. + * + * $FreeBSD$ + */ + +#include +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include +#include + +#include "libutil.h" + + +/* + * Sort processes first by pid and then tid. + */ +static int +kinfo_proc_compare(const void *a, const void *b) +{ + int i; + + i = ((const struct kinfo_proc *)a)->ki_pid - + ((const struct kinfo_proc *)b)->ki_pid; + if (i != 0) + return (i); + i = ((const struct kinfo_proc *)a)->ki_tid - + ((const struct kinfo_proc *)b)->ki_tid; + return (i); +} + +static void +kinfo_proc_sort(struct kinfo_proc *kipp, int count) +{ + + qsort(kipp, count, sizeof(*kipp), kinfo_proc_compare); +} + +struct kinfo_proc * +kinfo_getallproc(int *cntp) +{ + struct kinfo_proc *kipp; + size_t len; + int mib[3]; + + mib[0] = CTL_KERN; + mib[1] = KERN_PROC; + mib[2] = KERN_PROC_PROC; + + len = 0; + if (sysctl(mib, 3, NULL, &len, NULL, 0) < 0) + return (NULL); + + kipp = malloc(len); + if (kipp == NULL) + return (NULL); + + if (sysctl(mib, 3, kipp, &len, NULL, 0) < 0) + goto bad; + if (len % sizeof(*kipp) != 0) + goto bad; + if (kipp->ki_structsize != sizeof(*kipp)) + goto bad; + *cntp = len / sizeof(*kipp); + kinfo_proc_sort(kipp, len / sizeof(*kipp)); + return (kipp); +bad: + *cntp = 0; + free(kipp); + return (NULL); +} Property changes on: head/lib/libutil/kinfo_getallproc.c ___________________________________________________________________ Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Index: head/lib/libutil/kinfo_getproc.3 =================================================================== --- head/lib/libutil/kinfo_getproc.3 (nonexistent) +++ head/lib/libutil/kinfo_getproc.3 (revision 221807) @@ -0,0 +1,73 @@ +.\" +.\" Copyright (c) 2009 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. +.\" +.\" $FreeBSD$ +.\" +.Dd July 9, 2009 +.Os +.Dt KINFO_GETPROC 3 +.Sh NAME +.Nm kinfo_getproc +.Nd function for getting process information from kernel +.Sh LIBRARY +.Lb libutil +.Sh SYNOPSIS +.In sys/types.h +.In libutil.h +.Ft struct kinfo_proc * +.Fn kinfo_getproc "pid_t pid" "int *cntp" +.Sh DESCRIPTION +This function is used for obtaining process information from the kernel. +.Pp +The +.Ar pid +field contains the process identifier. +This should be the a process that you have privilige to access. +This function is a wrapper around +.Xr sysctl 3 +with the +.Dv KERN_PROC_PID +mib. +While the kernel returns a packed structure, this function expands the +data into a fixed record format. +.Sh RETURN VALUES +On success the +.Fn kinfo_getproc +function returns a pointer to a +.Vt struct kinfo_proc +structure as defined by +.In sys/user.h . +The pointer was obtained by an internal call to +.Xr malloc 3 +and must be freed by the caller with a call to +.Xr free 3 . +On failure the +.Fn kinfo_getproc +function returns +.Dv NULL . +.Sh SEE ALSO +.Xr free 3 , +.Xr malloc 3 , +.Xr sysctl 3 Property changes on: head/lib/libutil/kinfo_getproc.3 ___________________________________________________________________ Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Index: head/lib/libutil/kinfo_getproc.c =================================================================== --- head/lib/libutil/kinfo_getproc.c (nonexistent) +++ head/lib/libutil/kinfo_getproc.c (revision 221807) @@ -0,0 +1,71 @@ +/*- + * Copyright (c) 2009 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. + * + * $FreeBSD$ + */ + +#include +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include +#include + +#include "libutil.h" + +struct kinfo_proc * +kinfo_getproc(pid_t pid) +{ + struct kinfo_proc *kipp; + int mib[4]; + size_t len; + + len = 0; + mib[0] = CTL_KERN; + mib[1] = KERN_PROC; + mib[2] = KERN_PROC_PID; + mib[3] = pid; + if (sysctl(mib, 4, NULL, &len, NULL, 0) < 0) + return (NULL); + + kipp = malloc(len); + if (kipp == NULL) + return (NULL); + + if (sysctl(mib, 4, kipp, &len, NULL, 0) < 0) + goto bad; + if (len != sizeof(*kipp)) + goto bad; + if (kipp->ki_structsize != sizeof(*kipp)) + goto bad; + if (kipp->ki_pid != pid) + goto bad; + return (kipp); +bad: + free(kipp); + return (NULL); +} Property changes on: head/lib/libutil/kinfo_getproc.c ___________________________________________________________________ Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Index: head/lib/libutil/libutil.h =================================================================== --- head/lib/libutil/libutil.h (revision 221806) +++ head/lib/libutil/libutil.h (revision 221807) @@ -1,227 +1,232 @@ /* * Copyright (c) 1996 Peter Wemm . * All rights reserved. * Copyright (c) 2002 Networks Associates Technology, Inc. * All rights reserved. * * Portions of this software were developed for the FreeBSD Project by * ThinkSec AS and NAI Labs, the Security Research Division of Network * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 * ("CBOSS"), as part of the DARPA CHATS research program. * * Redistribution and use in source and binary forms, with or without * modification, is 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. The name of the author may not be used to endorse or promote * products derived from this software without specific prior written * permission. * * 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. * * $FreeBSD$ */ #ifndef _LIBUTIL_H_ #define _LIBUTIL_H_ #include #include #include #ifndef _GID_T_DECLARED typedef __gid_t gid_t; #define _GID_T_DECLARED #endif #ifndef _PID_T_DECLARED typedef __pid_t pid_t; #define _PID_T_DECLARED #endif #ifndef _SIZE_T_DECLARED typedef __size_t size_t; #define _SIZE_T_DECLARED #endif #ifndef _UID_T_DECLARED typedef __uid_t uid_t; #define _UID_T_DECLARED #endif #define PROPERTY_MAX_NAME 64 #define PROPERTY_MAX_VALUE 512 /* for properties.c */ typedef struct _property { struct _property *next; char *name; char *value; } *properties; #ifdef _SYS_PARAM_H_ /* for pidfile.c */ struct pidfh { int pf_fd; char pf_path[MAXPATHLEN + 1]; __dev_t pf_dev; ino_t pf_ino; }; #endif /* Avoid pulling in all the include files for no need */ struct termios; struct winsize; struct in_addr; struct kinfo_file; +struct kinfo_proc; struct kinfo_vmentry; __BEGIN_DECLS void clean_environment(const char * const *_white, const char * const *_more_white); int extattr_namespace_to_string(int _attrnamespace, char **_string); int extattr_string_to_namespace(const char *_string, int *_attrnamespace); int flopen(const char *_path, int _flags, ...); void hexdump(const void *ptr, int length, const char *hdr, int flags); int login_tty(int _fd); void trimdomain(char *_fullhost, int _hostsize); int openpty(int *_amaster, int *_aslave, char *_name, struct termios *_termp, struct winsize *_winp); int forkpty(int *_amaster, char *_name, struct termios *_termp, struct winsize *_winp); int humanize_number(char *_buf, size_t _len, int64_t _number, const char *_suffix, int _scale, int _flags); int expand_number(const char *_buf, uint64_t *_num); const char *uu_lockerr(int _uu_lockresult); int uu_lock(const char *_ttyname); int uu_unlock(const char *_ttyname); int uu_lock_txfr(const char *_ttyname, pid_t _pid); int _secure_path(const char *_path, uid_t _uid, gid_t _gid); properties properties_read(int fd); void properties_free(properties list); char *property_find(properties list, const char *name); char *auth_getval(const char *name); int realhostname(char *host, size_t hsize, const struct in_addr *ip); struct sockaddr; int realhostname_sa(char *host, size_t hsize, struct sockaddr *addr, int addrlen); int kld_isloaded(const char *name); int kld_load(const char *name); struct kinfo_file * kinfo_getfile(pid_t _pid, int *_cntp); struct kinfo_vmentry * kinfo_getvmmap(pid_t _pid, int *_cntp); +struct kinfo_proc * + kinfo_getallproc(int *_cntp); +struct kinfo_proc * + kinfo_getproc(pid_t _pid); #ifdef _STDIO_H_ /* avoid adding new includes */ char *fparseln(FILE *, size_t *, size_t *, const char[3], int); #endif #ifdef _PWD_H_ int pw_copy(int _ffd, int _tfd, const struct passwd *_pw, struct passwd *_old_pw); struct passwd *pw_dup(const struct passwd *_pw); int pw_edit(int _notsetuid); int pw_equal(const struct passwd *_pw1, const struct passwd *_pw2); void pw_fini(void); int pw_init(const char *_dir, const char *_master); char *pw_make(const struct passwd *_pw); int pw_mkdb(const char *_user); int pw_lock(void); struct passwd *pw_scan(const char *_line, int _flags); const char *pw_tempname(void); int pw_tmp(int _mfd); #endif #ifdef _GRP_H_ int gr_equal(const struct group *gr1, const struct group *gr2); char *gr_make(const struct group *gr); struct group *gr_dup(const struct group *gr); struct group *gr_scan(const char *line); #endif #ifdef _SYS_PARAM_H_ struct pidfh *pidfile_open(const char *path, mode_t mode, pid_t *pidptr); int pidfile_write(struct pidfh *pfh); int pidfile_close(struct pidfh *pfh); int pidfile_remove(struct pidfh *pfh); #endif #ifdef _UFS_UFS_QUOTA_H_ struct quotafile; struct fstab; struct quotafile *quota_open(struct fstab *, int, int); void quota_close(struct quotafile *); int quota_on(struct quotafile *); int quota_off(struct quotafile *); const char *quota_fsname(const struct quotafile *); const char *quota_qfname(const struct quotafile *); int quota_maxid(struct quotafile *); int quota_check_path(const struct quotafile *, const char *path); int quota_read(struct quotafile *, struct dqblk *, int); int quota_write_limits(struct quotafile *, struct dqblk *, int); int quota_write_usage(struct quotafile *, struct dqblk *, int); int quota_convert(struct quotafile *, int); #endif __END_DECLS #define UU_LOCK_INUSE (1) #define UU_LOCK_OK (0) #define UU_LOCK_OPEN_ERR (-1) #define UU_LOCK_READ_ERR (-2) #define UU_LOCK_CREAT_ERR (-3) #define UU_LOCK_WRITE_ERR (-4) #define UU_LOCK_LINK_ERR (-5) #define UU_LOCK_TRY_ERR (-6) #define UU_LOCK_OWNER_ERR (-7) /* return values from realhostname() */ #define HOSTNAME_FOUND (0) #define HOSTNAME_INCORRECTNAME (1) #define HOSTNAME_INVALIDADDR (2) #define HOSTNAME_INVALIDNAME (3) /* fparseln(3) */ #define FPARSELN_UNESCESC 0x01 #define FPARSELN_UNESCCONT 0x02 #define FPARSELN_UNESCCOMM 0x04 #define FPARSELN_UNESCREST 0x08 #define FPARSELN_UNESCALL 0x0f /* pw_scan() */ #define PWSCAN_MASTER 0x01 #define PWSCAN_WARN 0x02 /* humanize_number(3) */ #define HN_DECIMAL 0x01 #define HN_NOSPACE 0x02 #define HN_B 0x04 #define HN_DIVISOR_1000 0x08 #define HN_IEC_PREFIXES 0x10 /* maxscale = 0x07 */ #define HN_GETSCALE 0x10 #define HN_AUTOSCALE 0x20 /* hexdump(3) */ #define HD_COLUMN_MASK 0xff #define HD_DELIM_MASK 0xff00 #define HD_OMIT_COUNT (1 << 16) #define HD_OMIT_HEX (1 << 17) #define HD_OMIT_CHARS (1 << 18) #endif /* !_LIBUTIL_H_ */ Index: head/sys/kern/kern_descrip.c =================================================================== --- head/sys/kern/kern_descrip.c (revision 221806) +++ head/sys/kern/kern_descrip.c (revision 221807) @@ -1,3488 +1,3636 @@ /*- * Copyright (c) 1982, 1986, 1989, 1991, 1993 * The Regents of the University of California. All rights reserved. * (c) UNIX System Laboratories, Inc. * All or some portions of this file are derived from material licensed * to the University of California by American Telephone and Telegraph * Co. or Unix System Laboratories, Inc. and are reproduced herein with * the permission of UNIX System Laboratories, Inc. * * 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. * 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. * * @(#)kern_descrip.c 8.6 (Berkeley) 4/19/94 */ #include __FBSDID("$FreeBSD$"); #include "opt_compat.h" #include "opt_ddb.h" #include "opt_ktrace.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include +#include +#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include +#include +#include #include #include #ifdef KTRACE #include #endif #include +#include +#include + #include #include #include static MALLOC_DEFINE(M_FILEDESC, "filedesc", "Open file descriptor table"); static MALLOC_DEFINE(M_FILEDESC_TO_LEADER, "filedesc_to_leader", "file desc to leader structures"); static MALLOC_DEFINE(M_SIGIO, "sigio", "sigio structures"); static uma_zone_t file_zone; /* Flags for do_dup() */ #define DUP_FIXED 0x1 /* Force fixed allocation */ #define DUP_FCNTL 0x2 /* fcntl()-style errors */ static int do_dup(struct thread *td, int flags, int old, int new, register_t *retval); static int fd_first_free(struct filedesc *, int, int); static int fd_last_used(struct filedesc *, int, int); static void fdgrowtable(struct filedesc *, int); static void fdunused(struct filedesc *fdp, int fd); static void fdused(struct filedesc *fdp, int fd); +static int fill_vnode_info(struct vnode *vp, struct kinfo_file *kif); +static int fill_socket_info(struct socket *so, struct kinfo_file *kif); +static int fill_pts_info(struct tty *tp, struct kinfo_file *kif); +static int fill_pipe_info(struct pipe *pi, struct kinfo_file *kif); /* * A process is initially started out with NDFILE descriptors stored within * this structure, selected to be enough for typical applications based on * the historical limit of 20 open files (and the usage of descriptors by * shells). If these descriptors are exhausted, a larger descriptor table * may be allocated, up to a process' resource limit; the internal arrays * are then unused. */ #define NDFILE 20 #define NDSLOTSIZE sizeof(NDSLOTTYPE) #define NDENTRIES (NDSLOTSIZE * __CHAR_BIT) #define NDSLOT(x) ((x) / NDENTRIES) #define NDBIT(x) ((NDSLOTTYPE)1 << ((x) % NDENTRIES)) #define NDSLOTS(x) (((x) + NDENTRIES - 1) / NDENTRIES) /* * Storage required per open file descriptor. */ #define OFILESIZE (sizeof(struct file *) + sizeof(char)) /* * Storage to hold unused ofiles that need to be reclaimed. */ struct freetable { struct file **ft_table; SLIST_ENTRY(freetable) ft_next; }; /* * Basic allocation of descriptors: * one of the above, plus arrays for NDFILE descriptors. */ struct filedesc0 { struct filedesc fd_fd; /* * ofiles which need to be reclaimed on free. */ SLIST_HEAD(,freetable) fd_free; /* * These arrays are used when the number of open files is * <= NDFILE, and are then pointed to by the pointers above. */ struct file *fd_dfiles[NDFILE]; char fd_dfileflags[NDFILE]; NDSLOTTYPE fd_dmap[NDSLOTS(NDFILE)]; }; /* * Descriptor management. */ volatile int openfiles; /* actual number of open files */ struct mtx sigio_lock; /* mtx to protect pointers to sigio */ void (*mq_fdclose)(struct thread *td, int fd, struct file *fp); /* A mutex to protect the association between a proc and filedesc. */ static struct mtx fdesc_mtx; /* * Find the first zero bit in the given bitmap, starting at low and not * exceeding size - 1. */ static int fd_first_free(struct filedesc *fdp, int low, int size) { NDSLOTTYPE *map = fdp->fd_map; NDSLOTTYPE mask; int off, maxoff; if (low >= size) return (low); off = NDSLOT(low); if (low % NDENTRIES) { mask = ~(~(NDSLOTTYPE)0 >> (NDENTRIES - (low % NDENTRIES))); if ((mask &= ~map[off]) != 0UL) return (off * NDENTRIES + ffsl(mask) - 1); ++off; } for (maxoff = NDSLOTS(size); off < maxoff; ++off) if (map[off] != ~0UL) return (off * NDENTRIES + ffsl(~map[off]) - 1); return (size); } /* * Find the highest non-zero bit in the given bitmap, starting at low and * not exceeding size - 1. */ static int fd_last_used(struct filedesc *fdp, int low, int size) { NDSLOTTYPE *map = fdp->fd_map; NDSLOTTYPE mask; int off, minoff; if (low >= size) return (-1); off = NDSLOT(size); if (size % NDENTRIES) { mask = ~(~(NDSLOTTYPE)0 << (size % NDENTRIES)); if ((mask &= map[off]) != 0) return (off * NDENTRIES + flsl(mask) - 1); --off; } for (minoff = NDSLOT(low); off >= minoff; --off) if (map[off] != 0) return (off * NDENTRIES + flsl(map[off]) - 1); return (low - 1); } static int fdisused(struct filedesc *fdp, int fd) { KASSERT(fd >= 0 && fd < fdp->fd_nfiles, ("file descriptor %d out of range (0, %d)", fd, fdp->fd_nfiles)); return ((fdp->fd_map[NDSLOT(fd)] & NDBIT(fd)) != 0); } /* * Mark a file descriptor as used. */ static void fdused(struct filedesc *fdp, int fd) { FILEDESC_XLOCK_ASSERT(fdp); KASSERT(!fdisused(fdp, fd), ("fd already used")); fdp->fd_map[NDSLOT(fd)] |= NDBIT(fd); if (fd > fdp->fd_lastfile) fdp->fd_lastfile = fd; if (fd == fdp->fd_freefile) fdp->fd_freefile = fd_first_free(fdp, fd, fdp->fd_nfiles); } /* * Mark a file descriptor as unused. */ static void fdunused(struct filedesc *fdp, int fd) { FILEDESC_XLOCK_ASSERT(fdp); KASSERT(fdisused(fdp, fd), ("fd is already unused")); KASSERT(fdp->fd_ofiles[fd] == NULL, ("fd is still in use")); fdp->fd_map[NDSLOT(fd)] &= ~NDBIT(fd); if (fd < fdp->fd_freefile) fdp->fd_freefile = fd; if (fd == fdp->fd_lastfile) fdp->fd_lastfile = fd_last_used(fdp, 0, fd); } /* * System calls on descriptors. */ #ifndef _SYS_SYSPROTO_H_ struct getdtablesize_args { int dummy; }; #endif /* ARGSUSED */ int getdtablesize(struct thread *td, struct getdtablesize_args *uap) { struct proc *p = td->td_proc; uint64_t lim; PROC_LOCK(p); td->td_retval[0] = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc); lim = racct_get_limit(td->td_proc, RACCT_NOFILE); PROC_UNLOCK(p); if (lim < td->td_retval[0]) td->td_retval[0] = lim; return (0); } /* * Duplicate a file descriptor to a particular value. * * Note: keep in mind that a potential race condition exists when closing * descriptors from a shared descriptor table (via rfork). */ #ifndef _SYS_SYSPROTO_H_ struct dup2_args { u_int from; u_int to; }; #endif /* ARGSUSED */ int dup2(struct thread *td, struct dup2_args *uap) { return (do_dup(td, DUP_FIXED, (int)uap->from, (int)uap->to, td->td_retval)); } /* * Duplicate a file descriptor. */ #ifndef _SYS_SYSPROTO_H_ struct dup_args { u_int fd; }; #endif /* ARGSUSED */ int dup(struct thread *td, struct dup_args *uap) { return (do_dup(td, 0, (int)uap->fd, 0, td->td_retval)); } /* * The file control system call. */ #ifndef _SYS_SYSPROTO_H_ struct fcntl_args { int fd; int cmd; long arg; }; #endif /* ARGSUSED */ int fcntl(struct thread *td, struct fcntl_args *uap) { struct flock fl; struct oflock ofl; intptr_t arg; int error; int cmd; error = 0; cmd = uap->cmd; switch (uap->cmd) { case F_OGETLK: case F_OSETLK: case F_OSETLKW: /* * Convert old flock structure to new. */ error = copyin((void *)(intptr_t)uap->arg, &ofl, sizeof(ofl)); fl.l_start = ofl.l_start; fl.l_len = ofl.l_len; fl.l_pid = ofl.l_pid; fl.l_type = ofl.l_type; fl.l_whence = ofl.l_whence; fl.l_sysid = 0; switch (uap->cmd) { case F_OGETLK: cmd = F_GETLK; break; case F_OSETLK: cmd = F_SETLK; break; case F_OSETLKW: cmd = F_SETLKW; break; } arg = (intptr_t)&fl; break; case F_GETLK: case F_SETLK: case F_SETLKW: case F_SETLK_REMOTE: error = copyin((void *)(intptr_t)uap->arg, &fl, sizeof(fl)); arg = (intptr_t)&fl; break; default: arg = uap->arg; break; } if (error) return (error); error = kern_fcntl(td, uap->fd, cmd, arg); if (error) return (error); if (uap->cmd == F_OGETLK) { ofl.l_start = fl.l_start; ofl.l_len = fl.l_len; ofl.l_pid = fl.l_pid; ofl.l_type = fl.l_type; ofl.l_whence = fl.l_whence; error = copyout(&ofl, (void *)(intptr_t)uap->arg, sizeof(ofl)); } else if (uap->cmd == F_GETLK) { error = copyout(&fl, (void *)(intptr_t)uap->arg, sizeof(fl)); } return (error); } static inline struct file * fdtofp(int fd, struct filedesc *fdp) { struct file *fp; FILEDESC_LOCK_ASSERT(fdp); if ((unsigned)fd >= fdp->fd_nfiles || (fp = fdp->fd_ofiles[fd]) == NULL) return (NULL); return (fp); } int kern_fcntl(struct thread *td, int fd, int cmd, intptr_t arg) { struct filedesc *fdp; struct flock *flp; struct file *fp; struct proc *p; char *pop; struct vnode *vp; int error, flg, tmp; int vfslocked; u_int old, new; uint64_t bsize; vfslocked = 0; error = 0; flg = F_POSIX; p = td->td_proc; fdp = p->p_fd; switch (cmd) { case F_DUPFD: tmp = arg; error = do_dup(td, DUP_FCNTL, fd, tmp, td->td_retval); break; case F_DUP2FD: tmp = arg; error = do_dup(td, DUP_FIXED, fd, tmp, td->td_retval); break; case F_GETFD: FILEDESC_SLOCK(fdp); if ((fp = fdtofp(fd, fdp)) == NULL) { FILEDESC_SUNLOCK(fdp); error = EBADF; break; } pop = &fdp->fd_ofileflags[fd]; td->td_retval[0] = (*pop & UF_EXCLOSE) ? FD_CLOEXEC : 0; FILEDESC_SUNLOCK(fdp); break; case F_SETFD: FILEDESC_XLOCK(fdp); if ((fp = fdtofp(fd, fdp)) == NULL) { FILEDESC_XUNLOCK(fdp); error = EBADF; break; } pop = &fdp->fd_ofileflags[fd]; *pop = (*pop &~ UF_EXCLOSE) | (arg & FD_CLOEXEC ? UF_EXCLOSE : 0); FILEDESC_XUNLOCK(fdp); break; case F_GETFL: FILEDESC_SLOCK(fdp); if ((fp = fdtofp(fd, fdp)) == NULL) { FILEDESC_SUNLOCK(fdp); error = EBADF; break; } td->td_retval[0] = OFLAGS(fp->f_flag); FILEDESC_SUNLOCK(fdp); break; case F_SETFL: FILEDESC_SLOCK(fdp); if ((fp = fdtofp(fd, fdp)) == NULL) { FILEDESC_SUNLOCK(fdp); error = EBADF; break; } fhold(fp); FILEDESC_SUNLOCK(fdp); do { tmp = flg = fp->f_flag; tmp &= ~FCNTLFLAGS; tmp |= FFLAGS(arg & ~O_ACCMODE) & FCNTLFLAGS; } while(atomic_cmpset_int(&fp->f_flag, flg, tmp) == 0); tmp = fp->f_flag & FNONBLOCK; error = fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td); if (error) { fdrop(fp, td); break; } tmp = fp->f_flag & FASYNC; error = fo_ioctl(fp, FIOASYNC, &tmp, td->td_ucred, td); if (error == 0) { fdrop(fp, td); break; } atomic_clear_int(&fp->f_flag, FNONBLOCK); tmp = 0; (void)fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td); fdrop(fp, td); break; case F_GETOWN: FILEDESC_SLOCK(fdp); if ((fp = fdtofp(fd, fdp)) == NULL) { FILEDESC_SUNLOCK(fdp); error = EBADF; break; } fhold(fp); FILEDESC_SUNLOCK(fdp); error = fo_ioctl(fp, FIOGETOWN, &tmp, td->td_ucred, td); if (error == 0) td->td_retval[0] = tmp; fdrop(fp, td); break; case F_SETOWN: FILEDESC_SLOCK(fdp); if ((fp = fdtofp(fd, fdp)) == NULL) { FILEDESC_SUNLOCK(fdp); error = EBADF; break; } fhold(fp); FILEDESC_SUNLOCK(fdp); tmp = arg; error = fo_ioctl(fp, FIOSETOWN, &tmp, td->td_ucred, td); fdrop(fp, td); break; case F_SETLK_REMOTE: error = priv_check(td, PRIV_NFS_LOCKD); if (error) return (error); flg = F_REMOTE; goto do_setlk; case F_SETLKW: flg |= F_WAIT; /* FALLTHROUGH F_SETLK */ case F_SETLK: do_setlk: FILEDESC_SLOCK(fdp); if ((fp = fdtofp(fd, fdp)) == NULL) { FILEDESC_SUNLOCK(fdp); error = EBADF; break; } if (fp->f_type != DTYPE_VNODE) { FILEDESC_SUNLOCK(fdp); error = EBADF; break; } flp = (struct flock *)arg; if (flp->l_whence == SEEK_CUR) { if (fp->f_offset < 0 || (flp->l_start > 0 && fp->f_offset > OFF_MAX - flp->l_start)) { FILEDESC_SUNLOCK(fdp); error = EOVERFLOW; break; } flp->l_start += fp->f_offset; } /* * VOP_ADVLOCK() may block. */ fhold(fp); FILEDESC_SUNLOCK(fdp); vp = fp->f_vnode; vfslocked = VFS_LOCK_GIANT(vp->v_mount); switch (flp->l_type) { case F_RDLCK: if ((fp->f_flag & FREAD) == 0) { error = EBADF; break; } PROC_LOCK(p->p_leader); p->p_leader->p_flag |= P_ADVLOCK; PROC_UNLOCK(p->p_leader); error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK, flp, flg); break; case F_WRLCK: if ((fp->f_flag & FWRITE) == 0) { error = EBADF; break; } PROC_LOCK(p->p_leader); p->p_leader->p_flag |= P_ADVLOCK; PROC_UNLOCK(p->p_leader); error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK, flp, flg); break; case F_UNLCK: error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_UNLCK, flp, flg); break; case F_UNLCKSYS: /* * Temporary api for testing remote lock * infrastructure. */ if (flg != F_REMOTE) { error = EINVAL; break; } error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_UNLCKSYS, flp, flg); break; default: error = EINVAL; break; } VFS_UNLOCK_GIANT(vfslocked); vfslocked = 0; /* Check for race with close */ FILEDESC_SLOCK(fdp); if ((unsigned) fd >= fdp->fd_nfiles || fp != fdp->fd_ofiles[fd]) { FILEDESC_SUNLOCK(fdp); flp->l_whence = SEEK_SET; flp->l_start = 0; flp->l_len = 0; flp->l_type = F_UNLCK; vfslocked = VFS_LOCK_GIANT(vp->v_mount); (void) VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_UNLCK, flp, F_POSIX); VFS_UNLOCK_GIANT(vfslocked); vfslocked = 0; } else FILEDESC_SUNLOCK(fdp); fdrop(fp, td); break; case F_GETLK: FILEDESC_SLOCK(fdp); if ((fp = fdtofp(fd, fdp)) == NULL) { FILEDESC_SUNLOCK(fdp); error = EBADF; break; } if (fp->f_type != DTYPE_VNODE) { FILEDESC_SUNLOCK(fdp); error = EBADF; break; } flp = (struct flock *)arg; if (flp->l_type != F_RDLCK && flp->l_type != F_WRLCK && flp->l_type != F_UNLCK) { FILEDESC_SUNLOCK(fdp); error = EINVAL; break; } if (flp->l_whence == SEEK_CUR) { if ((flp->l_start > 0 && fp->f_offset > OFF_MAX - flp->l_start) || (flp->l_start < 0 && fp->f_offset < OFF_MIN - flp->l_start)) { FILEDESC_SUNLOCK(fdp); error = EOVERFLOW; break; } flp->l_start += fp->f_offset; } /* * VOP_ADVLOCK() may block. */ fhold(fp); FILEDESC_SUNLOCK(fdp); vp = fp->f_vnode; vfslocked = VFS_LOCK_GIANT(vp->v_mount); error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_GETLK, flp, F_POSIX); VFS_UNLOCK_GIANT(vfslocked); vfslocked = 0; fdrop(fp, td); break; case F_RDAHEAD: arg = arg ? 128 * 1024: 0; /* FALLTHROUGH */ case F_READAHEAD: FILEDESC_SLOCK(fdp); if ((fp = fdtofp(fd, fdp)) == NULL) { FILEDESC_SUNLOCK(fdp); error = EBADF; break; } if (fp->f_type != DTYPE_VNODE) { FILEDESC_SUNLOCK(fdp); error = EBADF; break; } fhold(fp); FILEDESC_SUNLOCK(fdp); if (arg != 0) { vp = fp->f_vnode; vfslocked = VFS_LOCK_GIANT(vp->v_mount); error = vn_lock(vp, LK_SHARED); if (error != 0) goto readahead_vnlock_fail; bsize = fp->f_vnode->v_mount->mnt_stat.f_iosize; VOP_UNLOCK(vp, 0); fp->f_seqcount = (arg + bsize - 1) / bsize; do { new = old = fp->f_flag; new |= FRDAHEAD; } while (!atomic_cmpset_rel_int(&fp->f_flag, old, new)); readahead_vnlock_fail: VFS_UNLOCK_GIANT(vfslocked); vfslocked = 0; } else { do { new = old = fp->f_flag; new &= ~FRDAHEAD; } while (!atomic_cmpset_rel_int(&fp->f_flag, old, new)); } fdrop(fp, td); break; default: error = EINVAL; break; } VFS_UNLOCK_GIANT(vfslocked); return (error); } /* * Common code for dup, dup2, fcntl(F_DUPFD) and fcntl(F_DUP2FD). */ static int do_dup(struct thread *td, int flags, int old, int new, register_t *retval) { struct filedesc *fdp; struct proc *p; struct file *fp; struct file *delfp; int error, holdleaders, maxfd; p = td->td_proc; fdp = p->p_fd; /* * Verify we have a valid descriptor to dup from and possibly to * dup to. Unlike dup() and dup2(), fcntl()'s F_DUPFD should * return EINVAL when the new descriptor is out of bounds. */ if (old < 0) return (EBADF); if (new < 0) return (flags & DUP_FCNTL ? EINVAL : EBADF); PROC_LOCK(p); maxfd = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc); PROC_UNLOCK(p); if (new >= maxfd) return (flags & DUP_FCNTL ? EINVAL : EMFILE); FILEDESC_XLOCK(fdp); if (old >= fdp->fd_nfiles || fdp->fd_ofiles[old] == NULL) { FILEDESC_XUNLOCK(fdp); return (EBADF); } if (flags & DUP_FIXED && old == new) { *retval = new; FILEDESC_XUNLOCK(fdp); return (0); } fp = fdp->fd_ofiles[old]; fhold(fp); /* * If the caller specified a file descriptor, make sure the file * table is large enough to hold it, and grab it. Otherwise, just * allocate a new descriptor the usual way. Since the filedesc * lock may be temporarily dropped in the process, we have to look * out for a race. */ if (flags & DUP_FIXED) { if (new >= fdp->fd_nfiles) { /* * The resource limits are here instead of e.g. fdalloc(), * because the file descriptor table may be shared between * processes, so we can't really use racct_add()/racct_sub(). * Instead of counting the number of actually allocated * descriptors, just put the limit on the size of the file * descriptor table. */ PROC_LOCK(p); error = racct_set(p, RACCT_NOFILE, new + 1); PROC_UNLOCK(p); if (error != 0) { FILEDESC_XUNLOCK(fdp); fdrop(fp, td); return (EMFILE); } fdgrowtable(fdp, new + 1); } if (fdp->fd_ofiles[new] == NULL) fdused(fdp, new); } else { if ((error = fdalloc(td, new, &new)) != 0) { FILEDESC_XUNLOCK(fdp); fdrop(fp, td); return (error); } } /* * If the old file changed out from under us then treat it as a * bad file descriptor. Userland should do its own locking to * avoid this case. */ if (fdp->fd_ofiles[old] != fp) { /* we've allocated a descriptor which we won't use */ if (fdp->fd_ofiles[new] == NULL) fdunused(fdp, new); FILEDESC_XUNLOCK(fdp); fdrop(fp, td); return (EBADF); } KASSERT(old != new, ("new fd is same as old")); /* * Save info on the descriptor being overwritten. We cannot close * it without introducing an ownership race for the slot, since we * need to drop the filedesc lock to call closef(). * * XXX this duplicates parts of close(). */ delfp = fdp->fd_ofiles[new]; holdleaders = 0; if (delfp != NULL) { if (td->td_proc->p_fdtol != NULL) { /* * Ask fdfree() to sleep to ensure that all relevant * process leaders can be traversed in closef(). */ fdp->fd_holdleaderscount++; holdleaders = 1; } } /* * Duplicate the source descriptor */ fdp->fd_ofiles[new] = fp; fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] &~ UF_EXCLOSE; if (new > fdp->fd_lastfile) fdp->fd_lastfile = new; *retval = new; /* * If we dup'd over a valid file, we now own the reference to it * and must dispose of it using closef() semantics (as if a * close() were performed on it). * * XXX this duplicates parts of close(). */ if (delfp != NULL) { knote_fdclose(td, new); if (delfp->f_type == DTYPE_MQUEUE) mq_fdclose(td, new, delfp); FILEDESC_XUNLOCK(fdp); (void) closef(delfp, td); if (holdleaders) { FILEDESC_XLOCK(fdp); fdp->fd_holdleaderscount--; if (fdp->fd_holdleaderscount == 0 && fdp->fd_holdleaderswakeup != 0) { fdp->fd_holdleaderswakeup = 0; wakeup(&fdp->fd_holdleaderscount); } FILEDESC_XUNLOCK(fdp); } } else { FILEDESC_XUNLOCK(fdp); } return (0); } /* * If sigio is on the list associated with a process or process group, * disable signalling from the device, remove sigio from the list and * free sigio. */ void funsetown(struct sigio **sigiop) { struct sigio *sigio; SIGIO_LOCK(); sigio = *sigiop; if (sigio == NULL) { SIGIO_UNLOCK(); return; } *(sigio->sio_myref) = NULL; if ((sigio)->sio_pgid < 0) { struct pgrp *pg = (sigio)->sio_pgrp; PGRP_LOCK(pg); SLIST_REMOVE(&sigio->sio_pgrp->pg_sigiolst, sigio, sigio, sio_pgsigio); PGRP_UNLOCK(pg); } else { struct proc *p = (sigio)->sio_proc; PROC_LOCK(p); SLIST_REMOVE(&sigio->sio_proc->p_sigiolst, sigio, sigio, sio_pgsigio); PROC_UNLOCK(p); } SIGIO_UNLOCK(); crfree(sigio->sio_ucred); free(sigio, M_SIGIO); } /* * Free a list of sigio structures. * We only need to lock the SIGIO_LOCK because we have made ourselves * inaccessible to callers of fsetown and therefore do not need to lock * the proc or pgrp struct for the list manipulation. */ void funsetownlst(struct sigiolst *sigiolst) { struct proc *p; struct pgrp *pg; struct sigio *sigio; sigio = SLIST_FIRST(sigiolst); if (sigio == NULL) return; p = NULL; pg = NULL; /* * Every entry of the list should belong * to a single proc or pgrp. */ if (sigio->sio_pgid < 0) { pg = sigio->sio_pgrp; PGRP_LOCK_ASSERT(pg, MA_NOTOWNED); } else /* if (sigio->sio_pgid > 0) */ { p = sigio->sio_proc; PROC_LOCK_ASSERT(p, MA_NOTOWNED); } SIGIO_LOCK(); while ((sigio = SLIST_FIRST(sigiolst)) != NULL) { *(sigio->sio_myref) = NULL; if (pg != NULL) { KASSERT(sigio->sio_pgid < 0, ("Proc sigio in pgrp sigio list")); KASSERT(sigio->sio_pgrp == pg, ("Bogus pgrp in sigio list")); PGRP_LOCK(pg); SLIST_REMOVE(&pg->pg_sigiolst, sigio, sigio, sio_pgsigio); PGRP_UNLOCK(pg); } else /* if (p != NULL) */ { KASSERT(sigio->sio_pgid > 0, ("Pgrp sigio in proc sigio list")); KASSERT(sigio->sio_proc == p, ("Bogus proc in sigio list")); PROC_LOCK(p); SLIST_REMOVE(&p->p_sigiolst, sigio, sigio, sio_pgsigio); PROC_UNLOCK(p); } SIGIO_UNLOCK(); crfree(sigio->sio_ucred); free(sigio, M_SIGIO); SIGIO_LOCK(); } SIGIO_UNLOCK(); } /* * This is common code for FIOSETOWN ioctl called by fcntl(fd, F_SETOWN, arg). * * After permission checking, add a sigio structure to the sigio list for * the process or process group. */ int fsetown(pid_t pgid, struct sigio **sigiop) { struct proc *proc; struct pgrp *pgrp; struct sigio *sigio; int ret; if (pgid == 0) { funsetown(sigiop); return (0); } ret = 0; /* Allocate and fill in the new sigio out of locks. */ sigio = malloc(sizeof(struct sigio), M_SIGIO, M_WAITOK); sigio->sio_pgid = pgid; sigio->sio_ucred = crhold(curthread->td_ucred); sigio->sio_myref = sigiop; sx_slock(&proctree_lock); if (pgid > 0) { proc = pfind(pgid); if (proc == NULL) { ret = ESRCH; goto fail; } /* * Policy - Don't allow a process to FSETOWN a process * in another session. * * Remove this test to allow maximum flexibility or * restrict FSETOWN to the current process or process * group for maximum safety. */ PROC_UNLOCK(proc); if (proc->p_session != curthread->td_proc->p_session) { ret = EPERM; goto fail; } pgrp = NULL; } else /* if (pgid < 0) */ { pgrp = pgfind(-pgid); if (pgrp == NULL) { ret = ESRCH; goto fail; } PGRP_UNLOCK(pgrp); /* * Policy - Don't allow a process to FSETOWN a process * in another session. * * Remove this test to allow maximum flexibility or * restrict FSETOWN to the current process or process * group for maximum safety. */ if (pgrp->pg_session != curthread->td_proc->p_session) { ret = EPERM; goto fail; } proc = NULL; } funsetown(sigiop); if (pgid > 0) { PROC_LOCK(proc); /* * Since funsetownlst() is called without the proctree * locked, we need to check for P_WEXIT. * XXX: is ESRCH correct? */ if ((proc->p_flag & P_WEXIT) != 0) { PROC_UNLOCK(proc); ret = ESRCH; goto fail; } SLIST_INSERT_HEAD(&proc->p_sigiolst, sigio, sio_pgsigio); sigio->sio_proc = proc; PROC_UNLOCK(proc); } else { PGRP_LOCK(pgrp); SLIST_INSERT_HEAD(&pgrp->pg_sigiolst, sigio, sio_pgsigio); sigio->sio_pgrp = pgrp; PGRP_UNLOCK(pgrp); } sx_sunlock(&proctree_lock); SIGIO_LOCK(); *sigiop = sigio; SIGIO_UNLOCK(); return (0); fail: sx_sunlock(&proctree_lock); crfree(sigio->sio_ucred); free(sigio, M_SIGIO); return (ret); } /* * This is common code for FIOGETOWN ioctl called by fcntl(fd, F_GETOWN, arg). */ pid_t fgetown(sigiop) struct sigio **sigiop; { pid_t pgid; SIGIO_LOCK(); pgid = (*sigiop != NULL) ? (*sigiop)->sio_pgid : 0; SIGIO_UNLOCK(); return (pgid); } /* * Close a file descriptor. */ #ifndef _SYS_SYSPROTO_H_ struct close_args { int fd; }; #endif /* ARGSUSED */ int close(td, uap) struct thread *td; struct close_args *uap; { return (kern_close(td, uap->fd)); } int kern_close(td, fd) struct thread *td; int fd; { struct filedesc *fdp; struct file *fp; int error; int holdleaders; error = 0; holdleaders = 0; fdp = td->td_proc->p_fd; AUDIT_SYSCLOSE(td, fd); FILEDESC_XLOCK(fdp); if ((unsigned)fd >= fdp->fd_nfiles || (fp = fdp->fd_ofiles[fd]) == NULL) { FILEDESC_XUNLOCK(fdp); return (EBADF); } fdp->fd_ofiles[fd] = NULL; fdp->fd_ofileflags[fd] = 0; fdunused(fdp, fd); if (td->td_proc->p_fdtol != NULL) { /* * Ask fdfree() to sleep to ensure that all relevant * process leaders can be traversed in closef(). */ fdp->fd_holdleaderscount++; holdleaders = 1; } /* * We now hold the fp reference that used to be owned by the * descriptor array. We have to unlock the FILEDESC *AFTER* * knote_fdclose to prevent a race of the fd getting opened, a knote * added, and deleteing a knote for the new fd. */ knote_fdclose(td, fd); if (fp->f_type == DTYPE_MQUEUE) mq_fdclose(td, fd, fp); FILEDESC_XUNLOCK(fdp); error = closef(fp, td); if (holdleaders) { FILEDESC_XLOCK(fdp); fdp->fd_holdleaderscount--; if (fdp->fd_holdleaderscount == 0 && fdp->fd_holdleaderswakeup != 0) { fdp->fd_holdleaderswakeup = 0; wakeup(&fdp->fd_holdleaderscount); } FILEDESC_XUNLOCK(fdp); } return (error); } /* * Close open file descriptors. */ #ifndef _SYS_SYSPROTO_H_ struct closefrom_args { int lowfd; }; #endif /* ARGSUSED */ int closefrom(struct thread *td, struct closefrom_args *uap) { struct filedesc *fdp; int fd; fdp = td->td_proc->p_fd; AUDIT_ARG_FD(uap->lowfd); /* * Treat negative starting file descriptor values identical to * closefrom(0) which closes all files. */ if (uap->lowfd < 0) uap->lowfd = 0; FILEDESC_SLOCK(fdp); for (fd = uap->lowfd; fd < fdp->fd_nfiles; fd++) { if (fdp->fd_ofiles[fd] != NULL) { FILEDESC_SUNLOCK(fdp); (void)kern_close(td, fd); FILEDESC_SLOCK(fdp); } } FILEDESC_SUNLOCK(fdp); return (0); } #if defined(COMPAT_43) /* * Return status information about a file descriptor. */ #ifndef _SYS_SYSPROTO_H_ struct ofstat_args { int fd; struct ostat *sb; }; #endif /* ARGSUSED */ int ofstat(struct thread *td, struct ofstat_args *uap) { struct ostat oub; struct stat ub; int error; error = kern_fstat(td, uap->fd, &ub); if (error == 0) { cvtstat(&ub, &oub); error = copyout(&oub, uap->sb, sizeof(oub)); } return (error); } #endif /* COMPAT_43 */ /* * Return status information about a file descriptor. */ #ifndef _SYS_SYSPROTO_H_ struct fstat_args { int fd; struct stat *sb; }; #endif /* ARGSUSED */ int fstat(struct thread *td, struct fstat_args *uap) { struct stat ub; int error; error = kern_fstat(td, uap->fd, &ub); if (error == 0) error = copyout(&ub, uap->sb, sizeof(ub)); return (error); } int kern_fstat(struct thread *td, int fd, struct stat *sbp) { struct file *fp; int error; AUDIT_ARG_FD(fd); if ((error = fget(td, fd, &fp)) != 0) return (error); AUDIT_ARG_FILE(td->td_proc, fp); error = fo_stat(fp, sbp, td->td_ucred, td); fdrop(fp, td); #ifdef KTRACE if (error == 0 && KTRPOINT(td, KTR_STRUCT)) ktrstat(sbp); #endif return (error); } /* * Return status information about a file descriptor. */ #ifndef _SYS_SYSPROTO_H_ struct nfstat_args { int fd; struct nstat *sb; }; #endif /* ARGSUSED */ int nfstat(struct thread *td, struct nfstat_args *uap) { struct nstat nub; struct stat ub; int error; error = kern_fstat(td, uap->fd, &ub); if (error == 0) { cvtnstat(&ub, &nub); error = copyout(&nub, uap->sb, sizeof(nub)); } return (error); } /* * Return pathconf information about a file descriptor. */ #ifndef _SYS_SYSPROTO_H_ struct fpathconf_args { int fd; int name; }; #endif /* ARGSUSED */ int fpathconf(struct thread *td, struct fpathconf_args *uap) { struct file *fp; struct vnode *vp; int error; if ((error = fget(td, uap->fd, &fp)) != 0) return (error); /* If asynchronous I/O is available, it works for all descriptors. */ if (uap->name == _PC_ASYNC_IO) { td->td_retval[0] = async_io_version; goto out; } vp = fp->f_vnode; if (vp != NULL) { int vfslocked; vfslocked = VFS_LOCK_GIANT(vp->v_mount); vn_lock(vp, LK_SHARED | LK_RETRY); error = VOP_PATHCONF(vp, uap->name, td->td_retval); VOP_UNLOCK(vp, 0); VFS_UNLOCK_GIANT(vfslocked); } else if (fp->f_type == DTYPE_PIPE || fp->f_type == DTYPE_SOCKET) { if (uap->name != _PC_PIPE_BUF) { error = EINVAL; } else { td->td_retval[0] = PIPE_BUF; error = 0; } } else { error = EOPNOTSUPP; } out: fdrop(fp, td); return (error); } /* * Grow the file table to accomodate (at least) nfd descriptors. This may * block and drop the filedesc lock, but it will reacquire it before * returning. */ static void fdgrowtable(struct filedesc *fdp, int nfd) { struct filedesc0 *fdp0; struct freetable *fo; struct file **ntable; struct file **otable; char *nfileflags; int nnfiles, onfiles; NDSLOTTYPE *nmap; FILEDESC_XLOCK_ASSERT(fdp); KASSERT(fdp->fd_nfiles > 0, ("zero-length file table")); /* compute the size of the new table */ onfiles = fdp->fd_nfiles; nnfiles = NDSLOTS(nfd) * NDENTRIES; /* round up */ if (nnfiles <= onfiles) /* the table is already large enough */ return; /* allocate a new table and (if required) new bitmaps */ FILEDESC_XUNLOCK(fdp); ntable = malloc((nnfiles * OFILESIZE) + sizeof(struct freetable), M_FILEDESC, M_ZERO | M_WAITOK); nfileflags = (char *)&ntable[nnfiles]; if (NDSLOTS(nnfiles) > NDSLOTS(onfiles)) nmap = malloc(NDSLOTS(nnfiles) * NDSLOTSIZE, M_FILEDESC, M_ZERO | M_WAITOK); else nmap = NULL; FILEDESC_XLOCK(fdp); /* * We now have new tables ready to go. Since we dropped the * filedesc lock to call malloc(), watch out for a race. */ onfiles = fdp->fd_nfiles; if (onfiles >= nnfiles) { /* we lost the race, but that's OK */ free(ntable, M_FILEDESC); if (nmap != NULL) free(nmap, M_FILEDESC); return; } bcopy(fdp->fd_ofiles, ntable, onfiles * sizeof(*ntable)); bcopy(fdp->fd_ofileflags, nfileflags, onfiles); otable = fdp->fd_ofiles; fdp->fd_ofileflags = nfileflags; fdp->fd_ofiles = ntable; /* * We must preserve ofiles until the process exits because we can't * be certain that no threads have references to the old table via * _fget(). */ if (onfiles > NDFILE) { fo = (struct freetable *)&otable[onfiles]; fdp0 = (struct filedesc0 *)fdp; fo->ft_table = otable; SLIST_INSERT_HEAD(&fdp0->fd_free, fo, ft_next); } if (NDSLOTS(nnfiles) > NDSLOTS(onfiles)) { bcopy(fdp->fd_map, nmap, NDSLOTS(onfiles) * sizeof(*nmap)); if (NDSLOTS(onfiles) > NDSLOTS(NDFILE)) free(fdp->fd_map, M_FILEDESC); fdp->fd_map = nmap; } fdp->fd_nfiles = nnfiles; } /* * Allocate a file descriptor for the process. */ int fdalloc(struct thread *td, int minfd, int *result) { struct proc *p = td->td_proc; struct filedesc *fdp = p->p_fd; int fd = -1, maxfd, error; FILEDESC_XLOCK_ASSERT(fdp); if (fdp->fd_freefile > minfd) minfd = fdp->fd_freefile; PROC_LOCK(p); maxfd = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc); PROC_UNLOCK(p); /* * Search the bitmap for a free descriptor. If none is found, try * to grow the file table. Keep at it until we either get a file * descriptor or run into process or system limits; fdgrowtable() * may drop the filedesc lock, so we're in a race. */ for (;;) { fd = fd_first_free(fdp, minfd, fdp->fd_nfiles); if (fd >= maxfd) return (EMFILE); if (fd < fdp->fd_nfiles) break; PROC_LOCK(p); error = racct_set(p, RACCT_NOFILE, min(fdp->fd_nfiles * 2, maxfd)); PROC_UNLOCK(p); if (error != 0) return (EMFILE); fdgrowtable(fdp, min(fdp->fd_nfiles * 2, maxfd)); } /* * Perform some sanity checks, then mark the file descriptor as * used and return it to the caller. */ KASSERT(!fdisused(fdp, fd), ("fd_first_free() returned non-free descriptor")); KASSERT(fdp->fd_ofiles[fd] == NULL, ("free descriptor isn't")); fdp->fd_ofileflags[fd] = 0; /* XXX needed? */ fdused(fdp, fd); *result = fd; return (0); } /* * Check to see whether n user file descriptors are available to the process * p. */ int fdavail(struct thread *td, int n) { struct proc *p = td->td_proc; struct filedesc *fdp = td->td_proc->p_fd; struct file **fpp; int i, lim, last; FILEDESC_LOCK_ASSERT(fdp); /* * XXX: This is only called from uipc_usrreq.c:unp_externalize(); * call racct_add() from there instead of dealing with containers * here. */ PROC_LOCK(p); lim = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc); PROC_UNLOCK(p); if ((i = lim - fdp->fd_nfiles) > 0 && (n -= i) <= 0) return (1); last = min(fdp->fd_nfiles, lim); fpp = &fdp->fd_ofiles[fdp->fd_freefile]; for (i = last - fdp->fd_freefile; --i >= 0; fpp++) { if (*fpp == NULL && --n <= 0) return (1); } return (0); } /* * Create a new open file structure and allocate a file decriptor for the * process that refers to it. We add one reference to the file for the * descriptor table and one reference for resultfp. This is to prevent us * being preempted and the entry in the descriptor table closed after we * release the FILEDESC lock. */ int falloc(struct thread *td, struct file **resultfp, int *resultfd, int flags) { struct proc *p = td->td_proc; struct file *fp; int error, i; int maxuserfiles = maxfiles - (maxfiles / 20); static struct timeval lastfail; static int curfail; fp = uma_zalloc(file_zone, M_WAITOK | M_ZERO); if ((openfiles >= maxuserfiles && priv_check(td, PRIV_MAXFILES) != 0) || openfiles >= maxfiles) { if (ppsratecheck(&lastfail, &curfail, 1)) { printf("kern.maxfiles limit exceeded by uid %i, please see tuning(7).\n", td->td_ucred->cr_ruid); } uma_zfree(file_zone, fp); return (ENFILE); } atomic_add_int(&openfiles, 1); /* * If the process has file descriptor zero open, add the new file * descriptor to the list of open files at that point, otherwise * put it at the front of the list of open files. */ refcount_init(&fp->f_count, 1); if (resultfp) fhold(fp); fp->f_cred = crhold(td->td_ucred); fp->f_ops = &badfileops; fp->f_data = NULL; fp->f_vnode = NULL; FILEDESC_XLOCK(p->p_fd); if ((error = fdalloc(td, 0, &i))) { FILEDESC_XUNLOCK(p->p_fd); fdrop(fp, td); if (resultfp) fdrop(fp, td); return (error); } p->p_fd->fd_ofiles[i] = fp; if ((flags & O_CLOEXEC) != 0) p->p_fd->fd_ofileflags[i] |= UF_EXCLOSE; FILEDESC_XUNLOCK(p->p_fd); if (resultfp) *resultfp = fp; if (resultfd) *resultfd = i; return (0); } /* * Build a new filedesc structure from another. * Copy the current, root, and jail root vnode references. */ struct filedesc * fdinit(struct filedesc *fdp) { struct filedesc0 *newfdp; newfdp = malloc(sizeof *newfdp, M_FILEDESC, M_WAITOK | M_ZERO); FILEDESC_LOCK_INIT(&newfdp->fd_fd); if (fdp != NULL) { FILEDESC_XLOCK(fdp); newfdp->fd_fd.fd_cdir = fdp->fd_cdir; if (newfdp->fd_fd.fd_cdir) VREF(newfdp->fd_fd.fd_cdir); newfdp->fd_fd.fd_rdir = fdp->fd_rdir; if (newfdp->fd_fd.fd_rdir) VREF(newfdp->fd_fd.fd_rdir); newfdp->fd_fd.fd_jdir = fdp->fd_jdir; if (newfdp->fd_fd.fd_jdir) VREF(newfdp->fd_fd.fd_jdir); FILEDESC_XUNLOCK(fdp); } /* Create the file descriptor table. */ newfdp->fd_fd.fd_refcnt = 1; newfdp->fd_fd.fd_holdcnt = 1; newfdp->fd_fd.fd_cmask = CMASK; newfdp->fd_fd.fd_ofiles = newfdp->fd_dfiles; newfdp->fd_fd.fd_ofileflags = newfdp->fd_dfileflags; newfdp->fd_fd.fd_nfiles = NDFILE; newfdp->fd_fd.fd_map = newfdp->fd_dmap; newfdp->fd_fd.fd_lastfile = -1; return (&newfdp->fd_fd); } static struct filedesc * fdhold(struct proc *p) { struct filedesc *fdp; mtx_lock(&fdesc_mtx); fdp = p->p_fd; if (fdp != NULL) fdp->fd_holdcnt++; mtx_unlock(&fdesc_mtx); return (fdp); } static void fddrop(struct filedesc *fdp) { struct filedesc0 *fdp0; struct freetable *ft; int i; mtx_lock(&fdesc_mtx); i = --fdp->fd_holdcnt; mtx_unlock(&fdesc_mtx); if (i > 0) return; FILEDESC_LOCK_DESTROY(fdp); fdp0 = (struct filedesc0 *)fdp; while ((ft = SLIST_FIRST(&fdp0->fd_free)) != NULL) { SLIST_REMOVE_HEAD(&fdp0->fd_free, ft_next); free(ft->ft_table, M_FILEDESC); } free(fdp, M_FILEDESC); } /* * Share a filedesc structure. */ struct filedesc * fdshare(struct filedesc *fdp) { FILEDESC_XLOCK(fdp); fdp->fd_refcnt++; FILEDESC_XUNLOCK(fdp); return (fdp); } /* * Unshare a filedesc structure, if necessary by making a copy */ void fdunshare(struct proc *p, struct thread *td) { FILEDESC_XLOCK(p->p_fd); if (p->p_fd->fd_refcnt > 1) { struct filedesc *tmp; FILEDESC_XUNLOCK(p->p_fd); tmp = fdcopy(p->p_fd); fdfree(td); p->p_fd = tmp; } else FILEDESC_XUNLOCK(p->p_fd); } /* * Copy a filedesc structure. A NULL pointer in returns a NULL reference, * this is to ease callers, not catch errors. */ struct filedesc * fdcopy(struct filedesc *fdp) { struct filedesc *newfdp; int i; /* Certain daemons might not have file descriptors. */ if (fdp == NULL) return (NULL); newfdp = fdinit(fdp); FILEDESC_SLOCK(fdp); while (fdp->fd_lastfile >= newfdp->fd_nfiles) { FILEDESC_SUNLOCK(fdp); FILEDESC_XLOCK(newfdp); fdgrowtable(newfdp, fdp->fd_lastfile + 1); FILEDESC_XUNLOCK(newfdp); FILEDESC_SLOCK(fdp); } /* copy everything except kqueue descriptors */ newfdp->fd_freefile = -1; for (i = 0; i <= fdp->fd_lastfile; ++i) { if (fdisused(fdp, i) && fdp->fd_ofiles[i]->f_type != DTYPE_KQUEUE && fdp->fd_ofiles[i]->f_ops != &badfileops) { newfdp->fd_ofiles[i] = fdp->fd_ofiles[i]; newfdp->fd_ofileflags[i] = fdp->fd_ofileflags[i]; fhold(newfdp->fd_ofiles[i]); newfdp->fd_lastfile = i; } else { if (newfdp->fd_freefile == -1) newfdp->fd_freefile = i; } } newfdp->fd_cmask = fdp->fd_cmask; FILEDESC_SUNLOCK(fdp); FILEDESC_XLOCK(newfdp); for (i = 0; i <= newfdp->fd_lastfile; ++i) if (newfdp->fd_ofiles[i] != NULL) fdused(newfdp, i); if (newfdp->fd_freefile == -1) newfdp->fd_freefile = i; FILEDESC_XUNLOCK(newfdp); return (newfdp); } /* * Release a filedesc structure. */ void fdfree(struct thread *td) { struct filedesc *fdp; struct file **fpp; int i, locked; struct filedesc_to_leader *fdtol; struct file *fp; struct vnode *cdir, *jdir, *rdir, *vp; struct flock lf; /* Certain daemons might not have file descriptors. */ fdp = td->td_proc->p_fd; if (fdp == NULL) return; PROC_LOCK(td->td_proc); racct_set(td->td_proc, RACCT_NOFILE, 0); PROC_UNLOCK(td->td_proc); /* Check for special need to clear POSIX style locks */ fdtol = td->td_proc->p_fdtol; if (fdtol != NULL) { FILEDESC_XLOCK(fdp); KASSERT(fdtol->fdl_refcount > 0, ("filedesc_to_refcount botch: fdl_refcount=%d", fdtol->fdl_refcount)); if (fdtol->fdl_refcount == 1 && (td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) { for (i = 0, fpp = fdp->fd_ofiles; i <= fdp->fd_lastfile; i++, fpp++) { if (*fpp == NULL || (*fpp)->f_type != DTYPE_VNODE) continue; fp = *fpp; fhold(fp); FILEDESC_XUNLOCK(fdp); lf.l_whence = SEEK_SET; lf.l_start = 0; lf.l_len = 0; lf.l_type = F_UNLCK; vp = fp->f_vnode; locked = VFS_LOCK_GIANT(vp->v_mount); (void) VOP_ADVLOCK(vp, (caddr_t)td->td_proc-> p_leader, F_UNLCK, &lf, F_POSIX); VFS_UNLOCK_GIANT(locked); FILEDESC_XLOCK(fdp); fdrop(fp, td); fpp = fdp->fd_ofiles + i; } } retry: if (fdtol->fdl_refcount == 1) { if (fdp->fd_holdleaderscount > 0 && (td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) { /* * close() or do_dup() has cleared a reference * in a shared file descriptor table. */ fdp->fd_holdleaderswakeup = 1; sx_sleep(&fdp->fd_holdleaderscount, FILEDESC_LOCK(fdp), PLOCK, "fdlhold", 0); goto retry; } if (fdtol->fdl_holdcount > 0) { /* * Ensure that fdtol->fdl_leader remains * valid in closef(). */ fdtol->fdl_wakeup = 1; sx_sleep(fdtol, FILEDESC_LOCK(fdp), PLOCK, "fdlhold", 0); goto retry; } } fdtol->fdl_refcount--; if (fdtol->fdl_refcount == 0 && fdtol->fdl_holdcount == 0) { fdtol->fdl_next->fdl_prev = fdtol->fdl_prev; fdtol->fdl_prev->fdl_next = fdtol->fdl_next; } else fdtol = NULL; td->td_proc->p_fdtol = NULL; FILEDESC_XUNLOCK(fdp); if (fdtol != NULL) free(fdtol, M_FILEDESC_TO_LEADER); } FILEDESC_XLOCK(fdp); i = --fdp->fd_refcnt; FILEDESC_XUNLOCK(fdp); if (i > 0) return; fpp = fdp->fd_ofiles; for (i = fdp->fd_lastfile; i-- >= 0; fpp++) { if (*fpp) { FILEDESC_XLOCK(fdp); fp = *fpp; *fpp = NULL; FILEDESC_XUNLOCK(fdp); (void) closef(fp, td); } } FILEDESC_XLOCK(fdp); /* XXX This should happen earlier. */ mtx_lock(&fdesc_mtx); td->td_proc->p_fd = NULL; mtx_unlock(&fdesc_mtx); if (fdp->fd_nfiles > NDFILE) free(fdp->fd_ofiles, M_FILEDESC); if (NDSLOTS(fdp->fd_nfiles) > NDSLOTS(NDFILE)) free(fdp->fd_map, M_FILEDESC); fdp->fd_nfiles = 0; cdir = fdp->fd_cdir; fdp->fd_cdir = NULL; rdir = fdp->fd_rdir; fdp->fd_rdir = NULL; jdir = fdp->fd_jdir; fdp->fd_jdir = NULL; FILEDESC_XUNLOCK(fdp); if (cdir) { locked = VFS_LOCK_GIANT(cdir->v_mount); vrele(cdir); VFS_UNLOCK_GIANT(locked); } if (rdir) { locked = VFS_LOCK_GIANT(rdir->v_mount); vrele(rdir); VFS_UNLOCK_GIANT(locked); } if (jdir) { locked = VFS_LOCK_GIANT(jdir->v_mount); vrele(jdir); VFS_UNLOCK_GIANT(locked); } fddrop(fdp); } /* * For setugid programs, we don't want to people to use that setugidness * to generate error messages which write to a file which otherwise would * otherwise be off-limits to the process. We check for filesystems where * the vnode can change out from under us after execve (like [lin]procfs). * * Since setugidsafety calls this only for fd 0, 1 and 2, this check is * sufficient. We also don't check for setugidness since we know we are. */ static int is_unsafe(struct file *fp) { if (fp->f_type == DTYPE_VNODE) { struct vnode *vp = fp->f_vnode; if ((vp->v_vflag & VV_PROCDEP) != 0) return (1); } return (0); } /* * Make this setguid thing safe, if at all possible. */ void setugidsafety(struct thread *td) { struct filedesc *fdp; int i; /* Certain daemons might not have file descriptors. */ fdp = td->td_proc->p_fd; if (fdp == NULL) return; /* * Note: fdp->fd_ofiles may be reallocated out from under us while * we are blocked in a close. Be careful! */ FILEDESC_XLOCK(fdp); for (i = 0; i <= fdp->fd_lastfile; i++) { if (i > 2) break; if (fdp->fd_ofiles[i] && is_unsafe(fdp->fd_ofiles[i])) { struct file *fp; knote_fdclose(td, i); /* * NULL-out descriptor prior to close to avoid * a race while close blocks. */ fp = fdp->fd_ofiles[i]; fdp->fd_ofiles[i] = NULL; fdp->fd_ofileflags[i] = 0; fdunused(fdp, i); FILEDESC_XUNLOCK(fdp); (void) closef(fp, td); FILEDESC_XLOCK(fdp); } } FILEDESC_XUNLOCK(fdp); } /* * If a specific file object occupies a specific file descriptor, close the * file descriptor entry and drop a reference on the file object. This is a * convenience function to handle a subsequent error in a function that calls * falloc() that handles the race that another thread might have closed the * file descriptor out from under the thread creating the file object. */ void fdclose(struct filedesc *fdp, struct file *fp, int idx, struct thread *td) { FILEDESC_XLOCK(fdp); if (fdp->fd_ofiles[idx] == fp) { fdp->fd_ofiles[idx] = NULL; fdunused(fdp, idx); FILEDESC_XUNLOCK(fdp); fdrop(fp, td); } else FILEDESC_XUNLOCK(fdp); } /* * Close any files on exec? */ void fdcloseexec(struct thread *td) { struct filedesc *fdp; int i; /* Certain daemons might not have file descriptors. */ fdp = td->td_proc->p_fd; if (fdp == NULL) return; FILEDESC_XLOCK(fdp); /* * We cannot cache fd_ofiles or fd_ofileflags since operations * may block and rip them out from under us. */ for (i = 0; i <= fdp->fd_lastfile; i++) { if (fdp->fd_ofiles[i] != NULL && (fdp->fd_ofiles[i]->f_type == DTYPE_MQUEUE || (fdp->fd_ofileflags[i] & UF_EXCLOSE))) { struct file *fp; knote_fdclose(td, i); /* * NULL-out descriptor prior to close to avoid * a race while close blocks. */ fp = fdp->fd_ofiles[i]; fdp->fd_ofiles[i] = NULL; fdp->fd_ofileflags[i] = 0; fdunused(fdp, i); if (fp->f_type == DTYPE_MQUEUE) mq_fdclose(td, i, fp); FILEDESC_XUNLOCK(fdp); (void) closef(fp, td); FILEDESC_XLOCK(fdp); } } FILEDESC_XUNLOCK(fdp); } /* * It is unsafe for set[ug]id processes to be started with file * descriptors 0..2 closed, as these descriptors are given implicit * significance in the Standard C library. fdcheckstd() will create a * descriptor referencing /dev/null for each of stdin, stdout, and * stderr that is not already open. */ int fdcheckstd(struct thread *td) { struct filedesc *fdp; register_t retval, save; int i, error, devnull; fdp = td->td_proc->p_fd; if (fdp == NULL) return (0); KASSERT(fdp->fd_refcnt == 1, ("the fdtable should not be shared")); devnull = -1; error = 0; for (i = 0; i < 3; i++) { if (fdp->fd_ofiles[i] != NULL) continue; if (devnull < 0) { save = td->td_retval[0]; error = kern_open(td, "/dev/null", UIO_SYSSPACE, O_RDWR, 0); devnull = td->td_retval[0]; td->td_retval[0] = save; if (error) break; KASSERT(devnull == i, ("oof, we didn't get our fd")); } else { error = do_dup(td, DUP_FIXED, devnull, i, &retval); if (error != 0) break; } } return (error); } /* * Internal form of close. Decrement reference count on file structure. * Note: td may be NULL when closing a file that was being passed in a * message. * * XXXRW: Giant is not required for the caller, but often will be held; this * makes it moderately likely the Giant will be recursed in the VFS case. */ int closef(struct file *fp, struct thread *td) { struct vnode *vp; struct flock lf; struct filedesc_to_leader *fdtol; struct filedesc *fdp; /* * POSIX record locking dictates that any close releases ALL * locks owned by this process. This is handled by setting * a flag in the unlock to free ONLY locks obeying POSIX * semantics, and not to free BSD-style file locks. * If the descriptor was in a message, POSIX-style locks * aren't passed with the descriptor, and the thread pointer * will be NULL. Callers should be careful only to pass a * NULL thread pointer when there really is no owning * context that might have locks, or the locks will be * leaked. */ if (fp->f_type == DTYPE_VNODE && td != NULL) { int vfslocked; vp = fp->f_vnode; vfslocked = VFS_LOCK_GIANT(vp->v_mount); if ((td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) { lf.l_whence = SEEK_SET; lf.l_start = 0; lf.l_len = 0; lf.l_type = F_UNLCK; (void) VOP_ADVLOCK(vp, (caddr_t)td->td_proc->p_leader, F_UNLCK, &lf, F_POSIX); } fdtol = td->td_proc->p_fdtol; if (fdtol != NULL) { /* * Handle special case where file descriptor table is * shared between multiple process leaders. */ fdp = td->td_proc->p_fd; FILEDESC_XLOCK(fdp); for (fdtol = fdtol->fdl_next; fdtol != td->td_proc->p_fdtol; fdtol = fdtol->fdl_next) { if ((fdtol->fdl_leader->p_flag & P_ADVLOCK) == 0) continue; fdtol->fdl_holdcount++; FILEDESC_XUNLOCK(fdp); lf.l_whence = SEEK_SET; lf.l_start = 0; lf.l_len = 0; lf.l_type = F_UNLCK; vp = fp->f_vnode; (void) VOP_ADVLOCK(vp, (caddr_t)fdtol->fdl_leader, F_UNLCK, &lf, F_POSIX); FILEDESC_XLOCK(fdp); fdtol->fdl_holdcount--; if (fdtol->fdl_holdcount == 0 && fdtol->fdl_wakeup != 0) { fdtol->fdl_wakeup = 0; wakeup(fdtol); } } FILEDESC_XUNLOCK(fdp); } VFS_UNLOCK_GIANT(vfslocked); } return (fdrop(fp, td)); } /* * Initialize the file pointer with the specified properties. * * The ops are set with release semantics to be certain that the flags, type, * and data are visible when ops is. This is to prevent ops methods from being * called with bad data. */ void finit(struct file *fp, u_int flag, short type, void *data, struct fileops *ops) { fp->f_data = data; fp->f_flag = flag; fp->f_type = type; atomic_store_rel_ptr((volatile uintptr_t *)&fp->f_ops, (uintptr_t)ops); } struct file * fget_unlocked(struct filedesc *fdp, int fd) { struct file *fp; u_int count; if (fd < 0 || fd >= fdp->fd_nfiles) return (NULL); /* * Fetch the descriptor locklessly. We avoid fdrop() races by * never raising a refcount above 0. To accomplish this we have * to use a cmpset loop rather than an atomic_add. The descriptor * must be re-verified once we acquire a reference to be certain * that the identity is still correct and we did not lose a race * due to preemption. */ for (;;) { fp = fdp->fd_ofiles[fd]; if (fp == NULL) break; count = fp->f_count; if (count == 0) continue; /* * Use an acquire barrier to prevent caching of fd_ofiles * so it is refreshed for verification. */ if (atomic_cmpset_acq_int(&fp->f_count, count, count + 1) != 1) continue; if (fp == fdp->fd_ofiles[fd]) break; fdrop(fp, curthread); } return (fp); } /* * Extract the file pointer associated with the specified descriptor for the * current user process. * * If the descriptor doesn't exist or doesn't match 'flags', EBADF is * returned. * * If an error occured the non-zero error is returned and *fpp is set to * NULL. Otherwise *fpp is held and set and zero is returned. Caller is * responsible for fdrop(). */ static __inline int _fget(struct thread *td, int fd, struct file **fpp, int flags) { struct filedesc *fdp; struct file *fp; *fpp = NULL; if (td == NULL || (fdp = td->td_proc->p_fd) == NULL) return (EBADF); if ((fp = fget_unlocked(fdp, fd)) == NULL) return (EBADF); if (fp->f_ops == &badfileops) { fdrop(fp, td); return (EBADF); } /* * FREAD and FWRITE failure return EBADF as per POSIX. * * Only one flag, or 0, may be specified. */ if ((flags == FREAD && (fp->f_flag & FREAD) == 0) || (flags == FWRITE && (fp->f_flag & FWRITE) == 0)) { fdrop(fp, td); return (EBADF); } *fpp = fp; return (0); } int fget(struct thread *td, int fd, struct file **fpp) { return(_fget(td, fd, fpp, 0)); } int fget_read(struct thread *td, int fd, struct file **fpp) { return(_fget(td, fd, fpp, FREAD)); } int fget_write(struct thread *td, int fd, struct file **fpp) { return(_fget(td, fd, fpp, FWRITE)); } /* * Like fget() but loads the underlying vnode, or returns an error if the * descriptor does not represent a vnode. Note that pipes use vnodes but * never have VM objects. The returned vnode will be vref()'d. * * XXX: what about the unused flags ? */ static __inline int _fgetvp(struct thread *td, int fd, struct vnode **vpp, int flags) { struct file *fp; int error; *vpp = NULL; if ((error = _fget(td, fd, &fp, flags)) != 0) return (error); if (fp->f_vnode == NULL) { error = EINVAL; } else { *vpp = fp->f_vnode; vref(*vpp); } fdrop(fp, td); return (error); } int fgetvp(struct thread *td, int fd, struct vnode **vpp) { return (_fgetvp(td, fd, vpp, 0)); } int fgetvp_read(struct thread *td, int fd, struct vnode **vpp) { return (_fgetvp(td, fd, vpp, FREAD)); } #ifdef notyet int fgetvp_write(struct thread *td, int fd, struct vnode **vpp) { return (_fgetvp(td, fd, vpp, FWRITE)); } #endif /* * Like fget() but loads the underlying socket, or returns an error if the * descriptor does not represent a socket. * * We bump the ref count on the returned socket. XXX Also obtain the SX lock * in the future. * * Note: fgetsock() and fputsock() are deprecated, as consumers should rely * on their file descriptor reference to prevent the socket from being free'd * during use. */ int fgetsock(struct thread *td, int fd, struct socket **spp, u_int *fflagp) { struct file *fp; int error; *spp = NULL; if (fflagp != NULL) *fflagp = 0; if ((error = _fget(td, fd, &fp, 0)) != 0) return (error); if (fp->f_type != DTYPE_SOCKET) { error = ENOTSOCK; } else { *spp = fp->f_data; if (fflagp) *fflagp = fp->f_flag; SOCK_LOCK(*spp); soref(*spp); SOCK_UNLOCK(*spp); } fdrop(fp, td); return (error); } /* * Drop the reference count on the socket and XXX release the SX lock in the * future. The last reference closes the socket. * * Note: fputsock() is deprecated, see comment for fgetsock(). */ void fputsock(struct socket *so) { ACCEPT_LOCK(); SOCK_LOCK(so); CURVNET_SET(so->so_vnet); sorele(so); CURVNET_RESTORE(); } /* * Handle the last reference to a file being closed. */ int _fdrop(struct file *fp, struct thread *td) { int error; error = 0; if (fp->f_count != 0) panic("fdrop: count %d", fp->f_count); if (fp->f_ops != &badfileops) error = fo_close(fp, td); /* * The f_cdevpriv cannot be assigned non-NULL value while we * are destroying the file. */ if (fp->f_cdevpriv != NULL) devfs_fpdrop(fp); atomic_subtract_int(&openfiles, 1); crfree(fp->f_cred); uma_zfree(file_zone, fp); return (error); } /* * Apply an advisory lock on a file descriptor. * * Just attempt to get a record lock of the requested type on the entire file * (l_whence = SEEK_SET, l_start = 0, l_len = 0). */ #ifndef _SYS_SYSPROTO_H_ struct flock_args { int fd; int how; }; #endif /* ARGSUSED */ int flock(struct thread *td, struct flock_args *uap) { struct file *fp; struct vnode *vp; struct flock lf; int vfslocked; int error; if ((error = fget(td, uap->fd, &fp)) != 0) return (error); if (fp->f_type != DTYPE_VNODE) { fdrop(fp, td); return (EOPNOTSUPP); } vp = fp->f_vnode; vfslocked = VFS_LOCK_GIANT(vp->v_mount); lf.l_whence = SEEK_SET; lf.l_start = 0; lf.l_len = 0; if (uap->how & LOCK_UN) { lf.l_type = F_UNLCK; atomic_clear_int(&fp->f_flag, FHASLOCK); error = VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK); goto done2; } if (uap->how & LOCK_EX) lf.l_type = F_WRLCK; else if (uap->how & LOCK_SH) lf.l_type = F_RDLCK; else { error = EBADF; goto done2; } atomic_set_int(&fp->f_flag, FHASLOCK); error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, (uap->how & LOCK_NB) ? F_FLOCK : F_FLOCK | F_WAIT); done2: fdrop(fp, td); VFS_UNLOCK_GIANT(vfslocked); return (error); } /* * Duplicate the specified descriptor to a free descriptor. */ int dupfdopen(struct thread *td, struct filedesc *fdp, int indx, int dfd, int mode, int error) { struct file *wfp; struct file *fp; /* * If the to-be-dup'd fd number is greater than the allowed number * of file descriptors, or the fd to be dup'd has already been * closed, then reject. */ FILEDESC_XLOCK(fdp); if (dfd < 0 || dfd >= fdp->fd_nfiles || (wfp = fdp->fd_ofiles[dfd]) == NULL) { FILEDESC_XUNLOCK(fdp); return (EBADF); } /* * There are two cases of interest here. * * For ENODEV simply dup (dfd) to file descriptor (indx) and return. * * For ENXIO steal away the file structure from (dfd) and store it in * (indx). (dfd) is effectively closed by this operation. * * Any other error code is just returned. */ switch (error) { case ENODEV: /* * Check that the mode the file is being opened for is a * subset of the mode of the existing descriptor. */ if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag) { FILEDESC_XUNLOCK(fdp); return (EACCES); } fp = fdp->fd_ofiles[indx]; fdp->fd_ofiles[indx] = wfp; fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd]; if (fp == NULL) fdused(fdp, indx); fhold(wfp); FILEDESC_XUNLOCK(fdp); if (fp != NULL) /* * We now own the reference to fp that the ofiles[] * array used to own. Release it. */ fdrop(fp, td); return (0); case ENXIO: /* * Steal away the file pointer from dfd and stuff it into indx. */ fp = fdp->fd_ofiles[indx]; fdp->fd_ofiles[indx] = fdp->fd_ofiles[dfd]; fdp->fd_ofiles[dfd] = NULL; fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd]; fdp->fd_ofileflags[dfd] = 0; fdunused(fdp, dfd); if (fp == NULL) fdused(fdp, indx); FILEDESC_XUNLOCK(fdp); /* * We now own the reference to fp that the ofiles[] array * used to own. Release it. */ if (fp != NULL) fdrop(fp, td); return (0); default: FILEDESC_XUNLOCK(fdp); return (error); } /* NOTREACHED */ } /* * Scan all active processes and prisons to see if any of them have a current * or root directory of `olddp'. If so, replace them with the new mount point. */ void mountcheckdirs(struct vnode *olddp, struct vnode *newdp) { struct filedesc *fdp; struct prison *pr; struct proc *p; int nrele; if (vrefcnt(olddp) == 1) return; nrele = 0; sx_slock(&allproc_lock); FOREACH_PROC_IN_SYSTEM(p) { fdp = fdhold(p); if (fdp == NULL) continue; FILEDESC_XLOCK(fdp); if (fdp->fd_cdir == olddp) { vref(newdp); fdp->fd_cdir = newdp; nrele++; } if (fdp->fd_rdir == olddp) { vref(newdp); fdp->fd_rdir = newdp; nrele++; } if (fdp->fd_jdir == olddp) { vref(newdp); fdp->fd_jdir = newdp; nrele++; } FILEDESC_XUNLOCK(fdp); fddrop(fdp); } sx_sunlock(&allproc_lock); if (rootvnode == olddp) { vref(newdp); rootvnode = newdp; nrele++; } mtx_lock(&prison0.pr_mtx); if (prison0.pr_root == olddp) { vref(newdp); prison0.pr_root = newdp; nrele++; } mtx_unlock(&prison0.pr_mtx); sx_slock(&allprison_lock); TAILQ_FOREACH(pr, &allprison, pr_list) { mtx_lock(&pr->pr_mtx); if (pr->pr_root == olddp) { vref(newdp); pr->pr_root = newdp; nrele++; } mtx_unlock(&pr->pr_mtx); } sx_sunlock(&allprison_lock); while (nrele--) vrele(olddp); } struct filedesc_to_leader * filedesc_to_leader_alloc(struct filedesc_to_leader *old, struct filedesc *fdp, struct proc *leader) { struct filedesc_to_leader *fdtol; fdtol = malloc(sizeof(struct filedesc_to_leader), M_FILEDESC_TO_LEADER, M_WAITOK); fdtol->fdl_refcount = 1; fdtol->fdl_holdcount = 0; fdtol->fdl_wakeup = 0; fdtol->fdl_leader = leader; if (old != NULL) { FILEDESC_XLOCK(fdp); fdtol->fdl_next = old->fdl_next; fdtol->fdl_prev = old; old->fdl_next = fdtol; fdtol->fdl_next->fdl_prev = fdtol; FILEDESC_XUNLOCK(fdp); } else { fdtol->fdl_next = fdtol; fdtol->fdl_prev = fdtol; } return (fdtol); } /* * Get file structures globally. */ static int sysctl_kern_file(SYSCTL_HANDLER_ARGS) { struct xfile xf; struct filedesc *fdp; struct file *fp; struct proc *p; int error, n; error = sysctl_wire_old_buffer(req, 0); if (error != 0) return (error); if (req->oldptr == NULL) { n = 0; sx_slock(&allproc_lock); FOREACH_PROC_IN_SYSTEM(p) { if (p->p_state == PRS_NEW) continue; fdp = fdhold(p); if (fdp == NULL) continue; /* overestimates sparse tables. */ if (fdp->fd_lastfile > 0) n += fdp->fd_lastfile; fddrop(fdp); } sx_sunlock(&allproc_lock); return (SYSCTL_OUT(req, 0, n * sizeof(xf))); } error = 0; bzero(&xf, sizeof(xf)); xf.xf_size = sizeof(xf); sx_slock(&allproc_lock); FOREACH_PROC_IN_SYSTEM(p) { PROC_LOCK(p); if (p->p_state == PRS_NEW) { PROC_UNLOCK(p); continue; } if (p_cansee(req->td, p) != 0) { PROC_UNLOCK(p); continue; } xf.xf_pid = p->p_pid; xf.xf_uid = p->p_ucred->cr_uid; PROC_UNLOCK(p); fdp = fdhold(p); if (fdp == NULL) continue; FILEDESC_SLOCK(fdp); for (n = 0; fdp->fd_refcnt > 0 && n < fdp->fd_nfiles; ++n) { if ((fp = fdp->fd_ofiles[n]) == NULL) continue; xf.xf_fd = n; xf.xf_file = fp; xf.xf_data = fp->f_data; xf.xf_vnode = fp->f_vnode; xf.xf_type = fp->f_type; xf.xf_count = fp->f_count; xf.xf_msgcount = 0; xf.xf_offset = fp->f_offset; xf.xf_flag = fp->f_flag; error = SYSCTL_OUT(req, &xf, sizeof(xf)); if (error) break; } FILEDESC_SUNLOCK(fdp); fddrop(fdp); if (error) break; } sx_sunlock(&allproc_lock); return (error); } SYSCTL_PROC(_kern, KERN_FILE, file, CTLTYPE_OPAQUE|CTLFLAG_RD, 0, 0, sysctl_kern_file, "S,xfile", "Entire file table"); #ifdef KINFO_OFILE_SIZE CTASSERT(sizeof(struct kinfo_ofile) == KINFO_OFILE_SIZE); #endif #ifdef COMPAT_FREEBSD7 static int export_vnode_for_osysctl(struct vnode *vp, int type, struct kinfo_ofile *kif, struct filedesc *fdp, struct sysctl_req *req) { int error; char *fullpath, *freepath; int vfslocked; bzero(kif, sizeof(*kif)); kif->kf_structsize = sizeof(*kif); vref(vp); kif->kf_fd = type; kif->kf_type = KF_TYPE_VNODE; /* This function only handles directories. */ if (vp->v_type != VDIR) { vrele(vp); return (ENOTDIR); } kif->kf_vnode_type = KF_VTYPE_VDIR; /* * This is not a true file descriptor, so we set a bogus refcount * and offset to indicate these fields should be ignored. */ kif->kf_ref_count = -1; kif->kf_offset = -1; freepath = NULL; fullpath = "-"; FILEDESC_SUNLOCK(fdp); vn_fullpath(curthread, vp, &fullpath, &freepath); vfslocked = VFS_LOCK_GIANT(vp->v_mount); vrele(vp); VFS_UNLOCK_GIANT(vfslocked); strlcpy(kif->kf_path, fullpath, sizeof(kif->kf_path)); if (freepath != NULL) free(freepath, M_TEMP); error = SYSCTL_OUT(req, kif, sizeof(*kif)); FILEDESC_SLOCK(fdp); return (error); } /* * Get per-process file descriptors for use by procstat(1), et al. */ static int sysctl_kern_proc_ofiledesc(SYSCTL_HANDLER_ARGS) { char *fullpath, *freepath; struct kinfo_ofile *kif; struct filedesc *fdp; int error, i, *name; struct socket *so; struct vnode *vp; struct file *fp; struct proc *p; struct tty *tp; int vfslocked; name = (int *)arg1; if ((p = pfind((pid_t)name[0])) == NULL) return (ESRCH); if ((error = p_candebug(curthread, p))) { PROC_UNLOCK(p); return (error); } fdp = fdhold(p); PROC_UNLOCK(p); if (fdp == NULL) return (ENOENT); kif = malloc(sizeof(*kif), M_TEMP, M_WAITOK); FILEDESC_SLOCK(fdp); if (fdp->fd_cdir != NULL) export_vnode_for_osysctl(fdp->fd_cdir, KF_FD_TYPE_CWD, kif, fdp, req); if (fdp->fd_rdir != NULL) export_vnode_for_osysctl(fdp->fd_rdir, KF_FD_TYPE_ROOT, kif, fdp, req); if (fdp->fd_jdir != NULL) export_vnode_for_osysctl(fdp->fd_jdir, KF_FD_TYPE_JAIL, kif, fdp, req); for (i = 0; i < fdp->fd_nfiles; i++) { if ((fp = fdp->fd_ofiles[i]) == NULL) continue; bzero(kif, sizeof(*kif)); kif->kf_structsize = sizeof(*kif); vp = NULL; so = NULL; tp = NULL; kif->kf_fd = i; switch (fp->f_type) { case DTYPE_VNODE: kif->kf_type = KF_TYPE_VNODE; vp = fp->f_vnode; break; case DTYPE_SOCKET: kif->kf_type = KF_TYPE_SOCKET; so = fp->f_data; break; case DTYPE_PIPE: kif->kf_type = KF_TYPE_PIPE; break; case DTYPE_FIFO: kif->kf_type = KF_TYPE_FIFO; vp = fp->f_vnode; break; case DTYPE_KQUEUE: kif->kf_type = KF_TYPE_KQUEUE; break; case DTYPE_CRYPTO: kif->kf_type = KF_TYPE_CRYPTO; break; case DTYPE_MQUEUE: kif->kf_type = KF_TYPE_MQUEUE; break; case DTYPE_SHM: kif->kf_type = KF_TYPE_SHM; break; case DTYPE_SEM: kif->kf_type = KF_TYPE_SEM; break; case DTYPE_PTS: kif->kf_type = KF_TYPE_PTS; tp = fp->f_data; break; default: kif->kf_type = KF_TYPE_UNKNOWN; break; } kif->kf_ref_count = fp->f_count; if (fp->f_flag & FREAD) kif->kf_flags |= KF_FLAG_READ; if (fp->f_flag & FWRITE) kif->kf_flags |= KF_FLAG_WRITE; if (fp->f_flag & FAPPEND) kif->kf_flags |= KF_FLAG_APPEND; if (fp->f_flag & FASYNC) kif->kf_flags |= KF_FLAG_ASYNC; if (fp->f_flag & FFSYNC) kif->kf_flags |= KF_FLAG_FSYNC; if (fp->f_flag & FNONBLOCK) kif->kf_flags |= KF_FLAG_NONBLOCK; if (fp->f_flag & O_DIRECT) kif->kf_flags |= KF_FLAG_DIRECT; if (fp->f_flag & FHASLOCK) kif->kf_flags |= KF_FLAG_HASLOCK; kif->kf_offset = fp->f_offset; if (vp != NULL) { vref(vp); switch (vp->v_type) { case VNON: kif->kf_vnode_type = KF_VTYPE_VNON; break; case VREG: kif->kf_vnode_type = KF_VTYPE_VREG; break; case VDIR: kif->kf_vnode_type = KF_VTYPE_VDIR; break; case VBLK: kif->kf_vnode_type = KF_VTYPE_VBLK; break; case VCHR: kif->kf_vnode_type = KF_VTYPE_VCHR; break; case VLNK: kif->kf_vnode_type = KF_VTYPE_VLNK; break; case VSOCK: kif->kf_vnode_type = KF_VTYPE_VSOCK; break; case VFIFO: kif->kf_vnode_type = KF_VTYPE_VFIFO; break; case VBAD: kif->kf_vnode_type = KF_VTYPE_VBAD; break; default: kif->kf_vnode_type = KF_VTYPE_UNKNOWN; break; } /* * It is OK to drop the filedesc lock here as we will * re-validate and re-evaluate its properties when * the loop continues. */ freepath = NULL; fullpath = "-"; FILEDESC_SUNLOCK(fdp); vn_fullpath(curthread, vp, &fullpath, &freepath); vfslocked = VFS_LOCK_GIANT(vp->v_mount); vrele(vp); VFS_UNLOCK_GIANT(vfslocked); strlcpy(kif->kf_path, fullpath, sizeof(kif->kf_path)); if (freepath != NULL) free(freepath, M_TEMP); FILEDESC_SLOCK(fdp); } if (so != NULL) { struct sockaddr *sa; if (so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa) == 0 && sa->sa_len <= sizeof(kif->kf_sa_local)) { bcopy(sa, &kif->kf_sa_local, sa->sa_len); free(sa, M_SONAME); } if (so->so_proto->pr_usrreqs->pru_peeraddr(so, &sa) == 0 && sa->sa_len <= sizeof(kif->kf_sa_peer)) { bcopy(sa, &kif->kf_sa_peer, sa->sa_len); free(sa, M_SONAME); } kif->kf_sock_domain = so->so_proto->pr_domain->dom_family; kif->kf_sock_type = so->so_type; kif->kf_sock_protocol = so->so_proto->pr_protocol; } if (tp != NULL) { strlcpy(kif->kf_path, tty_devname(tp), sizeof(kif->kf_path)); } error = SYSCTL_OUT(req, kif, sizeof(*kif)); if (error) break; } FILEDESC_SUNLOCK(fdp); fddrop(fdp); free(kif, M_TEMP); return (0); } static SYSCTL_NODE(_kern_proc, KERN_PROC_OFILEDESC, ofiledesc, CTLFLAG_RD, sysctl_kern_proc_ofiledesc, "Process ofiledesc entries"); #endif /* COMPAT_FREEBSD7 */ #ifdef KINFO_FILE_SIZE CTASSERT(sizeof(struct kinfo_file) == KINFO_FILE_SIZE); #endif static int -export_vnode_for_sysctl(struct vnode *vp, int type, - struct kinfo_file *kif, struct filedesc *fdp, struct sysctl_req *req) +export_fd_for_sysctl(void *data, int type, int fd, int fflags, int refcnt, + int64_t offset, struct kinfo_file *kif, struct sysctl_req *req) { - int error; - char *fullpath, *freepath; - int vfslocked; + struct { + int fflag; + int kf_fflag; + } fflags_table[] = { + { FAPPEND, KF_FLAG_APPEND }, + { FASYNC, KF_FLAG_ASYNC }, + { FFSYNC, KF_FLAG_FSYNC }, + { FHASLOCK, KF_FLAG_HASLOCK }, + { FNONBLOCK, KF_FLAG_NONBLOCK }, + { FREAD, KF_FLAG_READ }, + { FWRITE, KF_FLAG_WRITE }, + { O_CREAT, KF_FLAG_CREAT }, + { O_DIRECT, KF_FLAG_DIRECT }, + { O_EXCL, KF_FLAG_EXCL }, + { O_EXEC, KF_FLAG_EXEC }, + { O_EXLOCK, KF_FLAG_EXLOCK }, + { O_NOFOLLOW, KF_FLAG_NOFOLLOW }, + { O_SHLOCK, KF_FLAG_SHLOCK }, + { O_TRUNC, KF_FLAG_TRUNC } + }; +#define NFFLAGS (sizeof(fflags_table) / sizeof(*fflags_table)) + struct vnode *vp; + int error, vfslocked; + unsigned int i; bzero(kif, sizeof(*kif)); - - vref(vp); - kif->kf_fd = type; - kif->kf_type = KF_TYPE_VNODE; - /* This function only handles directories. */ - if (vp->v_type != VDIR) { + switch (type) { + case KF_TYPE_FIFO: + case KF_TYPE_VNODE: + vp = (struct vnode *)data; + error = fill_vnode_info(vp, kif); + vfslocked = VFS_LOCK_GIANT(vp->v_mount); vrele(vp); - return (ENOTDIR); + VFS_UNLOCK_GIANT(vfslocked); + break; + case KF_TYPE_SOCKET: + error = fill_socket_info((struct socket *)data, kif); + break; + case KF_TYPE_PIPE: + error = fill_pipe_info((struct pipe *)data, kif); + break; + case KF_TYPE_PTS: + error = fill_pts_info((struct tty *)data, kif); + break; + default: + error = 0; } - kif->kf_vnode_type = KF_VTYPE_VDIR; + if (error == 0) + kif->kf_status |= KF_ATTR_VALID; /* - * This is not a true file descriptor, so we set a bogus refcount - * and offset to indicate these fields should be ignored. + * Translate file access flags. */ - kif->kf_ref_count = -1; - kif->kf_offset = -1; - - freepath = NULL; - fullpath = "-"; - FILEDESC_SUNLOCK(fdp); - vn_fullpath(curthread, vp, &fullpath, &freepath); - vfslocked = VFS_LOCK_GIANT(vp->v_mount); - vrele(vp); - VFS_UNLOCK_GIANT(vfslocked); - strlcpy(kif->kf_path, fullpath, sizeof(kif->kf_path)); - if (freepath != NULL) - free(freepath, M_TEMP); + for (i = 0; i < NFFLAGS; i++) + if (fflags & fflags_table[i].fflag) + kif->kf_flags |= fflags_table[i].kf_fflag; + kif->kf_fd = fd; + kif->kf_type = type; + kif->kf_ref_count = refcnt; + kif->kf_offset = offset; /* Pack record size down */ kif->kf_structsize = offsetof(struct kinfo_file, kf_path) + strlen(kif->kf_path) + 1; kif->kf_structsize = roundup(kif->kf_structsize, sizeof(uint64_t)); error = SYSCTL_OUT(req, kif, kif->kf_structsize); - FILEDESC_SLOCK(fdp); return (error); } /* * Get per-process file descriptors for use by procstat(1), et al. */ static int sysctl_kern_proc_filedesc(SYSCTL_HANDLER_ARGS) { - char *fullpath, *freepath; - struct kinfo_file *kif; - struct filedesc *fdp; - int error, i, *name; - struct socket *so; - struct vnode *vp; struct file *fp; + struct filedesc *fdp; + struct kinfo_file *kif; struct proc *p; - struct tty *tp; - int vfslocked; + struct vnode *cttyvp, *textvp, *tracevp; size_t oldidx; + int64_t offset; + void *data; + int error, i, *name; + int type, refcnt, fflags; name = (int *)arg1; if ((p = pfind((pid_t)name[0])) == NULL) return (ESRCH); if ((error = p_candebug(curthread, p))) { PROC_UNLOCK(p); return (error); } + /* ktrace vnode */ + tracevp = p->p_tracevp; + if (tracevp != NULL) + vref(tracevp); + /* text vnode */ + textvp = p->p_textvp; + if (textvp != NULL) + vref(textvp); + /* Controlling tty. */ + cttyvp = NULL; + if (p->p_pgrp != NULL && p->p_pgrp->pg_session != NULL) { + cttyvp = p->p_pgrp->pg_session->s_ttyvp; + if (cttyvp != NULL) + vref(cttyvp); + } fdp = fdhold(p); PROC_UNLOCK(p); - if (fdp == NULL) - return (ENOENT); kif = malloc(sizeof(*kif), M_TEMP, M_WAITOK); + if (tracevp != NULL) + export_fd_for_sysctl(tracevp, KF_TYPE_VNODE, KF_FD_TYPE_TRACE, + FREAD | FWRITE, -1, -1, kif, req); + if (textvp != NULL) + export_fd_for_sysctl(textvp, KF_TYPE_VNODE, KF_FD_TYPE_TEXT, + FREAD, -1, -1, kif, req); + if (cttyvp != NULL) + export_fd_for_sysctl(cttyvp, KF_TYPE_VNODE, KF_FD_TYPE_CTTY, + FREAD | FWRITE, -1, -1, kif, req); + if (fdp == NULL) + goto fail; FILEDESC_SLOCK(fdp); - if (fdp->fd_cdir != NULL) - export_vnode_for_sysctl(fdp->fd_cdir, KF_FD_TYPE_CWD, kif, - fdp, req); - if (fdp->fd_rdir != NULL) - export_vnode_for_sysctl(fdp->fd_rdir, KF_FD_TYPE_ROOT, kif, - fdp, req); - if (fdp->fd_jdir != NULL) - export_vnode_for_sysctl(fdp->fd_jdir, KF_FD_TYPE_JAIL, kif, - fdp, req); + /* working directory */ + if (fdp->fd_cdir != NULL) { + vref(fdp->fd_cdir); + data = fdp->fd_cdir; + FILEDESC_SUNLOCK(fdp); + export_fd_for_sysctl(data, KF_TYPE_VNODE, KF_FD_TYPE_CWD, + FREAD, -1, -1, kif, req); + FILEDESC_SLOCK(fdp); + } + /* root directory */ + if (fdp->fd_rdir != NULL) { + vref(fdp->fd_rdir); + data = fdp->fd_rdir; + FILEDESC_SUNLOCK(fdp); + export_fd_for_sysctl(data, KF_TYPE_VNODE, KF_FD_TYPE_ROOT, + FREAD, -1, -1, kif, req); + FILEDESC_SLOCK(fdp); + } + /* jail directory */ + if (fdp->fd_jdir != NULL) { + vref(fdp->fd_jdir); + data = fdp->fd_jdir; + FILEDESC_SUNLOCK(fdp); + export_fd_for_sysctl(data, KF_TYPE_VNODE, KF_FD_TYPE_JAIL, + FREAD, -1, -1, kif, req); + FILEDESC_SLOCK(fdp); + } for (i = 0; i < fdp->fd_nfiles; i++) { if ((fp = fdp->fd_ofiles[i]) == NULL) continue; - bzero(kif, sizeof(*kif)); - vp = NULL; - so = NULL; - tp = NULL; - kif->kf_fd = i; + data = NULL; switch (fp->f_type) { case DTYPE_VNODE: - kif->kf_type = KF_TYPE_VNODE; - vp = fp->f_vnode; + type = KF_TYPE_VNODE; + vref(fp->f_vnode); + data = fp->f_vnode; break; case DTYPE_SOCKET: - kif->kf_type = KF_TYPE_SOCKET; - so = fp->f_data; + type = KF_TYPE_SOCKET; + data = fp->f_data; break; case DTYPE_PIPE: - kif->kf_type = KF_TYPE_PIPE; + type = KF_TYPE_PIPE; + data = fp->f_data; break; case DTYPE_FIFO: - kif->kf_type = KF_TYPE_FIFO; - vp = fp->f_vnode; + type = KF_TYPE_FIFO; + vref(fp->f_vnode); + data = fp->f_vnode; break; case DTYPE_KQUEUE: - kif->kf_type = KF_TYPE_KQUEUE; + type = KF_TYPE_KQUEUE; break; case DTYPE_CRYPTO: - kif->kf_type = KF_TYPE_CRYPTO; + type = KF_TYPE_CRYPTO; break; case DTYPE_MQUEUE: - kif->kf_type = KF_TYPE_MQUEUE; + type = KF_TYPE_MQUEUE; break; case DTYPE_SHM: - kif->kf_type = KF_TYPE_SHM; + type = KF_TYPE_SHM; break; case DTYPE_SEM: - kif->kf_type = KF_TYPE_SEM; + type = KF_TYPE_SEM; break; case DTYPE_PTS: - kif->kf_type = KF_TYPE_PTS; - tp = fp->f_data; + type = KF_TYPE_PTS; + data = fp->f_data; break; default: - kif->kf_type = KF_TYPE_UNKNOWN; + type = KF_TYPE_UNKNOWN; break; } - kif->kf_ref_count = fp->f_count; - if (fp->f_flag & FREAD) - kif->kf_flags |= KF_FLAG_READ; - if (fp->f_flag & FWRITE) - kif->kf_flags |= KF_FLAG_WRITE; - if (fp->f_flag & FAPPEND) - kif->kf_flags |= KF_FLAG_APPEND; - if (fp->f_flag & FASYNC) - kif->kf_flags |= KF_FLAG_ASYNC; - if (fp->f_flag & FFSYNC) - kif->kf_flags |= KF_FLAG_FSYNC; - if (fp->f_flag & FNONBLOCK) - kif->kf_flags |= KF_FLAG_NONBLOCK; - if (fp->f_flag & O_DIRECT) - kif->kf_flags |= KF_FLAG_DIRECT; - if (fp->f_flag & FHASLOCK) - kif->kf_flags |= KF_FLAG_HASLOCK; - kif->kf_offset = fp->f_offset; - if (vp != NULL) { - vref(vp); - switch (vp->v_type) { - case VNON: - kif->kf_vnode_type = KF_VTYPE_VNON; - break; - case VREG: - kif->kf_vnode_type = KF_VTYPE_VREG; - break; - case VDIR: - kif->kf_vnode_type = KF_VTYPE_VDIR; - break; - case VBLK: - kif->kf_vnode_type = KF_VTYPE_VBLK; - break; - case VCHR: - kif->kf_vnode_type = KF_VTYPE_VCHR; - break; - case VLNK: - kif->kf_vnode_type = KF_VTYPE_VLNK; - break; - case VSOCK: - kif->kf_vnode_type = KF_VTYPE_VSOCK; - break; - case VFIFO: - kif->kf_vnode_type = KF_VTYPE_VFIFO; - break; - case VBAD: - kif->kf_vnode_type = KF_VTYPE_VBAD; - break; - default: - kif->kf_vnode_type = KF_VTYPE_UNKNOWN; - break; - } - /* - * It is OK to drop the filedesc lock here as we will - * re-validate and re-evaluate its properties when - * the loop continues. - */ - freepath = NULL; - fullpath = "-"; - FILEDESC_SUNLOCK(fdp); - vn_fullpath(curthread, vp, &fullpath, &freepath); - vfslocked = VFS_LOCK_GIANT(vp->v_mount); - vrele(vp); - VFS_UNLOCK_GIANT(vfslocked); - strlcpy(kif->kf_path, fullpath, - sizeof(kif->kf_path)); - if (freepath != NULL) - free(freepath, M_TEMP); - FILEDESC_SLOCK(fdp); - } - if (so != NULL) { - struct sockaddr *sa; + refcnt = fp->f_count; + fflags = fp->f_flag; + offset = fp->f_offset; - if (so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa) - == 0 && sa->sa_len <= sizeof(kif->kf_sa_local)) { - bcopy(sa, &kif->kf_sa_local, sa->sa_len); - free(sa, M_SONAME); - } - if (so->so_proto->pr_usrreqs->pru_peeraddr(so, &sa) - == 0 && sa->sa_len <= sizeof(kif->kf_sa_peer)) { - bcopy(sa, &kif->kf_sa_peer, sa->sa_len); - free(sa, M_SONAME); - } - kif->kf_sock_domain = - so->so_proto->pr_domain->dom_family; - kif->kf_sock_type = so->so_type; - kif->kf_sock_protocol = so->so_proto->pr_protocol; - } - if (tp != NULL) { - strlcpy(kif->kf_path, tty_devname(tp), - sizeof(kif->kf_path)); - } - /* Pack record size down */ - kif->kf_structsize = offsetof(struct kinfo_file, kf_path) + - strlen(kif->kf_path) + 1; - kif->kf_structsize = roundup(kif->kf_structsize, - sizeof(uint64_t)); + /* + * Create sysctl entry. + * It is OK to drop the filedesc lock here as we will + * re-validate and re-evaluate its properties when + * the loop continues. + */ oldidx = req->oldidx; - error = SYSCTL_OUT(req, kif, kif->kf_structsize); + if (type == KF_TYPE_VNODE || type == KF_TYPE_FIFO) + FILEDESC_SUNLOCK(fdp); + error = export_fd_for_sysctl(data, type, i, + fflags, refcnt, offset, kif, req); + if (type == KF_TYPE_VNODE || type == KF_TYPE_FIFO) + FILEDESC_SLOCK(fdp); if (error) { if (error == ENOMEM) { /* * The hack to keep the ABI of sysctl * kern.proc.filedesc intact, but not * to account a partially copied * kinfo_file into the oldidx. */ req->oldidx = oldidx; error = 0; } break; } } FILEDESC_SUNLOCK(fdp); +fail: fddrop(fdp); free(kif, M_TEMP); return (error); +} + +int +vntype_to_kinfo(int vtype) +{ + struct { + int vtype; + int kf_vtype; + } vtypes_table[] = { + { VBAD, KF_VTYPE_VBAD }, + { VBLK, KF_VTYPE_VBLK }, + { VCHR, KF_VTYPE_VCHR }, + { VDIR, KF_VTYPE_VDIR }, + { VFIFO, KF_VTYPE_VFIFO }, + { VLNK, KF_VTYPE_VLNK }, + { VNON, KF_VTYPE_VNON }, + { VREG, KF_VTYPE_VREG }, + { VSOCK, KF_VTYPE_VSOCK } + }; +#define NVTYPES (sizeof(vtypes_table) / sizeof(*vtypes_table)) + unsigned int i; + + /* + * Perform vtype translation. + */ + for (i = 0; i < NVTYPES; i++) + if (vtypes_table[i].vtype == vtype) + break; + if (i < NVTYPES) + return (vtypes_table[i].kf_vtype); + + return (KF_VTYPE_UNKNOWN); +} + +static int +fill_vnode_info(struct vnode *vp, struct kinfo_file *kif) +{ + struct vattr va; + char *fullpath, *freepath; + int error, vfslocked; + + if (vp == NULL) + return (1); + kif->kf_vnode_type = vntype_to_kinfo(vp->v_type); + freepath = NULL; + fullpath = "-"; + error = vn_fullpath(curthread, vp, &fullpath, &freepath); + if (error == 0) { + strlcpy(kif->kf_path, fullpath, sizeof(kif->kf_path)); + } + if (freepath != NULL) + free(freepath, M_TEMP); + + /* + * Retrieve vnode attributes. + */ + va.va_fsid = VNOVAL; + va.va_rdev = NODEV; + vfslocked = VFS_LOCK_GIANT(vp->v_mount); + vn_lock(vp, LK_SHARED | LK_RETRY); + error = VOP_GETATTR(vp, &va, curthread->td_ucred); + VOP_UNLOCK(vp, 0); + VFS_UNLOCK_GIANT(vfslocked); + if (error != 0) + return (error); + if (va.va_fsid != VNOVAL) + kif->kf_un.kf_file.kf_file_fsid = va.va_fsid; + else + kif->kf_un.kf_file.kf_file_fsid = + vp->v_mount->mnt_stat.f_fsid.val[0]; + kif->kf_un.kf_file.kf_file_fileid = va.va_fileid; + kif->kf_un.kf_file.kf_file_mode = MAKEIMODE(va.va_type, va.va_mode); + kif->kf_un.kf_file.kf_file_size = va.va_size; + kif->kf_un.kf_file.kf_file_rdev = va.va_rdev; + return (0); +} + +static int +fill_socket_info(struct socket *so, struct kinfo_file *kif) +{ + struct sockaddr *sa; + struct inpcb *inpcb; + struct unpcb *unpcb; + int error; + + if (so == NULL) + return (1); + kif->kf_sock_domain = so->so_proto->pr_domain->dom_family; + kif->kf_sock_type = so->so_type; + kif->kf_sock_protocol = so->so_proto->pr_protocol; + kif->kf_un.kf_sock.kf_sock_pcb = (uintptr_t)so->so_pcb; + switch(kif->kf_sock_domain) { + case AF_INET: + case AF_INET6: + if (kif->kf_sock_protocol == IPPROTO_TCP) { + if (so->so_pcb != NULL) { + inpcb = (struct inpcb *)(so->so_pcb); + kif->kf_un.kf_sock.kf_sock_inpcb = + (uintptr_t)inpcb->inp_ppcb; + } + } + break; + case AF_UNIX: + if (so->so_pcb != NULL) { + unpcb = (struct unpcb *)(so->so_pcb); + if (unpcb->unp_conn) { + kif->kf_un.kf_sock.kf_sock_unpconn = + (uintptr_t)unpcb->unp_conn; + kif->kf_un.kf_sock.kf_sock_rcv_sb_state = + so->so_rcv.sb_state; + kif->kf_un.kf_sock.kf_sock_snd_sb_state = + so->so_snd.sb_state; + } + } + break; + } + error = so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa); + if (error == 0 && sa->sa_len <= sizeof(kif->kf_sa_local)) { + bcopy(sa, &kif->kf_sa_local, sa->sa_len); + free(sa, M_SONAME); + } + error = so->so_proto->pr_usrreqs->pru_peeraddr(so, &sa); + if (error == 0 && sa->sa_len <= sizeof(kif->kf_sa_peer)) { + bcopy(sa, &kif->kf_sa_peer, sa->sa_len); + free(sa, M_SONAME); + } + strncpy(kif->kf_path, so->so_proto->pr_domain->dom_name, + sizeof(kif->kf_path)); + return (0); +} + +static int +fill_pts_info(struct tty *tp, struct kinfo_file *kif) +{ + + if (tp == NULL) + return (1); + kif->kf_un.kf_pts.kf_pts_dev = tty_udev(tp); + strlcpy(kif->kf_path, tty_devname(tp), sizeof(kif->kf_path)); + return (0); +} + +static int +fill_pipe_info(struct pipe *pi, struct kinfo_file *kif) +{ + + if (pi == NULL) + return (1); + kif->kf_un.kf_pipe.kf_pipe_addr = (uintptr_t)pi; + kif->kf_un.kf_pipe.kf_pipe_peer = (uintptr_t)pi->pipe_peer; + kif->kf_un.kf_pipe.kf_pipe_buffer_cnt = pi->pipe_buffer.cnt; + return (0); } static SYSCTL_NODE(_kern_proc, KERN_PROC_FILEDESC, filedesc, CTLFLAG_RD, sysctl_kern_proc_filedesc, "Process filedesc entries"); #ifdef DDB /* * For the purposes of debugging, generate a human-readable string for the * file type. */ static const char * file_type_to_name(short type) { switch (type) { case 0: return ("zero"); case DTYPE_VNODE: return ("vnod"); case DTYPE_SOCKET: return ("sock"); case DTYPE_PIPE: return ("pipe"); case DTYPE_FIFO: return ("fifo"); case DTYPE_KQUEUE: return ("kque"); case DTYPE_CRYPTO: return ("crpt"); case DTYPE_MQUEUE: return ("mque"); case DTYPE_SHM: return ("shm"); case DTYPE_SEM: return ("ksem"); default: return ("unkn"); } } /* * For the purposes of debugging, identify a process (if any, perhaps one of * many) that references the passed file in its file descriptor array. Return * NULL if none. */ static struct proc * file_to_first_proc(struct file *fp) { struct filedesc *fdp; struct proc *p; int n; FOREACH_PROC_IN_SYSTEM(p) { if (p->p_state == PRS_NEW) continue; fdp = p->p_fd; if (fdp == NULL) continue; for (n = 0; n < fdp->fd_nfiles; n++) { if (fp == fdp->fd_ofiles[n]) return (p); } } return (NULL); } static void db_print_file(struct file *fp, int header) { struct proc *p; if (header) db_printf("%8s %4s %8s %8s %4s %5s %6s %8s %5s %12s\n", "File", "Type", "Data", "Flag", "GCFl", "Count", "MCount", "Vnode", "FPID", "FCmd"); p = file_to_first_proc(fp); db_printf("%8p %4s %8p %08x %04x %5d %6d %8p %5d %12s\n", fp, file_type_to_name(fp->f_type), fp->f_data, fp->f_flag, 0, fp->f_count, 0, fp->f_vnode, p != NULL ? p->p_pid : -1, p != NULL ? p->p_comm : "-"); } DB_SHOW_COMMAND(file, db_show_file) { struct file *fp; if (!have_addr) { db_printf("usage: show file \n"); return; } fp = (struct file *)addr; db_print_file(fp, 1); } DB_SHOW_COMMAND(files, db_show_files) { struct filedesc *fdp; struct file *fp; struct proc *p; int header; int n; header = 1; FOREACH_PROC_IN_SYSTEM(p) { if (p->p_state == PRS_NEW) continue; if ((fdp = p->p_fd) == NULL) continue; for (n = 0; n < fdp->fd_nfiles; ++n) { if ((fp = fdp->fd_ofiles[n]) == NULL) continue; db_print_file(fp, header); header = 0; } } } #endif SYSCTL_INT(_kern, KERN_MAXFILESPERPROC, maxfilesperproc, CTLFLAG_RW, &maxfilesperproc, 0, "Maximum files allowed open per process"); SYSCTL_INT(_kern, KERN_MAXFILES, maxfiles, CTLFLAG_RW, &maxfiles, 0, "Maximum number of files"); SYSCTL_INT(_kern, OID_AUTO, openfiles, CTLFLAG_RD, __DEVOLATILE(int *, &openfiles), 0, "System-wide number of open files"); /* ARGSUSED*/ static void filelistinit(void *dummy) { file_zone = uma_zcreate("Files", sizeof(struct file), NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); mtx_init(&sigio_lock, "sigio lock", NULL, MTX_DEF); mtx_init(&fdesc_mtx, "fdesc", NULL, MTX_DEF); } SYSINIT(select, SI_SUB_LOCK, SI_ORDER_FIRST, filelistinit, NULL); /*-------------------------------------------------------------------*/ static int badfo_readwrite(struct file *fp, struct uio *uio, struct ucred *active_cred, int flags, struct thread *td) { return (EBADF); } static int badfo_truncate(struct file *fp, off_t length, struct ucred *active_cred, struct thread *td) { return (EINVAL); } static int badfo_ioctl(struct file *fp, u_long com, void *data, struct ucred *active_cred, struct thread *td) { return (EBADF); } static int badfo_poll(struct file *fp, int events, struct ucred *active_cred, struct thread *td) { return (0); } static int badfo_kqfilter(struct file *fp, struct knote *kn) { return (EBADF); } static int badfo_stat(struct file *fp, struct stat *sb, struct ucred *active_cred, struct thread *td) { return (EBADF); } static int badfo_close(struct file *fp, struct thread *td) { return (EBADF); } struct fileops badfileops = { .fo_read = badfo_readwrite, .fo_write = badfo_readwrite, .fo_truncate = badfo_truncate, .fo_ioctl = badfo_ioctl, .fo_poll = badfo_poll, .fo_kqfilter = badfo_kqfilter, .fo_stat = badfo_stat, .fo_close = badfo_close, }; /*-------------------------------------------------------------------*/ /* * File Descriptor pseudo-device driver (/dev/fd/). * * Opening minor device N dup()s the file (if any) connected to file * descriptor N belonging to the calling process. Note that this driver * consists of only the ``open()'' routine, because all subsequent * references to this file will be direct to the other driver. * * XXX: we could give this one a cloning event handler if necessary. */ /* ARGSUSED */ static int fdopen(struct cdev *dev, int mode, int type, struct thread *td) { /* * XXX Kludge: set curthread->td_dupfd to contain the value of the * the file descriptor being sought for duplication. The error * return ensures that the vnode for this device will be released * by vn_open. Open will detect this special error and take the * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN * will simply report the error. */ td->td_dupfd = dev2unit(dev); return (ENODEV); } static struct cdevsw fildesc_cdevsw = { .d_version = D_VERSION, .d_open = fdopen, .d_name = "FD", }; static void fildesc_drvinit(void *unused) { struct cdev *dev; dev = make_dev_credf(MAKEDEV_ETERNAL, &fildesc_cdevsw, 0, NULL, UID_ROOT, GID_WHEEL, 0666, "fd/0"); make_dev_alias(dev, "stdin"); dev = make_dev_credf(MAKEDEV_ETERNAL, &fildesc_cdevsw, 1, NULL, UID_ROOT, GID_WHEEL, 0666, "fd/1"); make_dev_alias(dev, "stdout"); dev = make_dev_credf(MAKEDEV_ETERNAL, &fildesc_cdevsw, 2, NULL, UID_ROOT, GID_WHEEL, 0666, "fd/2"); make_dev_alias(dev, "stderr"); } SYSINIT(fildescdev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, fildesc_drvinit, NULL); Index: head/sys/kern/kern_proc.c =================================================================== --- head/sys/kern/kern_proc.c (revision 221806) +++ head/sys/kern/kern_proc.c (revision 221807) @@ -1,2069 +1,2073 @@ /*- * Copyright (c) 1982, 1986, 1989, 1991, 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. * 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. * * @(#)kern_proc.c 8.7 (Berkeley) 2/14/95 */ #include __FBSDID("$FreeBSD$"); #include "opt_compat.h" #include "opt_ddb.h" #include "opt_kdtrace.h" #include "opt_ktrace.h" #include "opt_kstack_pages.h" #include "opt_stack.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef DDB #include #endif #include #include #include #include #include #include #ifdef COMPAT_FREEBSD32 #include #include #endif SDT_PROVIDER_DEFINE(proc); SDT_PROBE_DEFINE(proc, kernel, ctor, entry, entry); SDT_PROBE_ARGTYPE(proc, kernel, ctor, entry, 0, "struct proc *"); SDT_PROBE_ARGTYPE(proc, kernel, ctor, entry, 1, "int"); SDT_PROBE_ARGTYPE(proc, kernel, ctor, entry, 2, "void *"); SDT_PROBE_ARGTYPE(proc, kernel, ctor, entry, 3, "int"); SDT_PROBE_DEFINE(proc, kernel, ctor, return, return); SDT_PROBE_ARGTYPE(proc, kernel, ctor, return, 0, "struct proc *"); SDT_PROBE_ARGTYPE(proc, kernel, ctor, return, 1, "int"); SDT_PROBE_ARGTYPE(proc, kernel, ctor, return, 2, "void *"); SDT_PROBE_ARGTYPE(proc, kernel, ctor, return, 3, "int"); SDT_PROBE_DEFINE(proc, kernel, dtor, entry, entry); SDT_PROBE_ARGTYPE(proc, kernel, dtor, entry, 0, "struct proc *"); SDT_PROBE_ARGTYPE(proc, kernel, dtor, entry, 1, "int"); SDT_PROBE_ARGTYPE(proc, kernel, dtor, entry, 2, "void *"); SDT_PROBE_ARGTYPE(proc, kernel, dtor, entry, 3, "struct thread *"); SDT_PROBE_DEFINE(proc, kernel, dtor, return, return); SDT_PROBE_ARGTYPE(proc, kernel, dtor, return, 0, "struct proc *"); SDT_PROBE_ARGTYPE(proc, kernel, dtor, return, 1, "int"); SDT_PROBE_ARGTYPE(proc, kernel, dtor, return, 2, "void *"); SDT_PROBE_DEFINE(proc, kernel, init, entry, entry); SDT_PROBE_ARGTYPE(proc, kernel, init, entry, 0, "struct proc *"); SDT_PROBE_ARGTYPE(proc, kernel, init, entry, 1, "int"); SDT_PROBE_ARGTYPE(proc, kernel, init, entry, 2, "int"); SDT_PROBE_DEFINE(proc, kernel, init, return, return); SDT_PROBE_ARGTYPE(proc, kernel, init, return, 0, "struct proc *"); SDT_PROBE_ARGTYPE(proc, kernel, init, return, 1, "int"); SDT_PROBE_ARGTYPE(proc, kernel, init, return, 2, "int"); MALLOC_DEFINE(M_PGRP, "pgrp", "process group header"); MALLOC_DEFINE(M_SESSION, "session", "session header"); static MALLOC_DEFINE(M_PROC, "proc", "Proc structures"); MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures"); static void doenterpgrp(struct proc *, struct pgrp *); static void orphanpg(struct pgrp *pg); static void fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp); static void fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp); static void fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp, int preferthread); static void pgadjustjobc(struct pgrp *pgrp, int entering); static void pgdelete(struct pgrp *); static int proc_ctor(void *mem, int size, void *arg, int flags); static void proc_dtor(void *mem, int size, void *arg); static int proc_init(void *mem, int size, int flags); static void proc_fini(void *mem, int size); static void pargs_free(struct pargs *pa); /* * Other process lists */ struct pidhashhead *pidhashtbl; u_long pidhash; struct pgrphashhead *pgrphashtbl; u_long pgrphash; struct proclist allproc; struct proclist zombproc; struct sx allproc_lock; struct sx proctree_lock; struct mtx ppeers_lock; uma_zone_t proc_zone; int kstack_pages = KSTACK_PAGES; SYSCTL_INT(_kern, OID_AUTO, kstack_pages, CTLFLAG_RD, &kstack_pages, 0, "Kernel stack size in pages"); CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE); #ifdef COMPAT_FREEBSD32 CTASSERT(sizeof(struct kinfo_proc32) == KINFO_PROC32_SIZE); #endif /* * Initialize global process hashing structures. */ void procinit() { sx_init(&allproc_lock, "allproc"); sx_init(&proctree_lock, "proctree"); mtx_init(&ppeers_lock, "p_peers", NULL, MTX_DEF); LIST_INIT(&allproc); LIST_INIT(&zombproc); pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash); pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash); proc_zone = uma_zcreate("PROC", sched_sizeof_proc(), proc_ctor, proc_dtor, proc_init, proc_fini, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); uihashinit(); } /* * Prepare a proc for use. */ static int proc_ctor(void *mem, int size, void *arg, int flags) { struct proc *p; p = (struct proc *)mem; SDT_PROBE(proc, kernel, ctor , entry, p, size, arg, flags, 0); EVENTHANDLER_INVOKE(process_ctor, p); SDT_PROBE(proc, kernel, ctor , return, p, size, arg, flags, 0); return (0); } /* * Reclaim a proc after use. */ static void proc_dtor(void *mem, int size, void *arg) { struct proc *p; struct thread *td; /* INVARIANTS checks go here */ p = (struct proc *)mem; td = FIRST_THREAD_IN_PROC(p); SDT_PROBE(proc, kernel, dtor, entry, p, size, arg, td, 0); if (td != NULL) { #ifdef INVARIANTS KASSERT((p->p_numthreads == 1), ("bad number of threads in exiting process")); KASSERT(STAILQ_EMPTY(&p->p_ktr), ("proc_dtor: non-empty p_ktr")); #endif /* Free all OSD associated to this thread. */ osd_thread_exit(td); } EVENTHANDLER_INVOKE(process_dtor, p); if (p->p_ksi != NULL) KASSERT(! KSI_ONQ(p->p_ksi), ("SIGCHLD queue")); SDT_PROBE(proc, kernel, dtor, return, p, size, arg, 0, 0); } /* * Initialize type-stable parts of a proc (when newly created). */ static int proc_init(void *mem, int size, int flags) { struct proc *p; p = (struct proc *)mem; SDT_PROBE(proc, kernel, init, entry, p, size, flags, 0, 0); p->p_sched = (struct p_sched *)&p[1]; bzero(&p->p_mtx, sizeof(struct mtx)); mtx_init(&p->p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK); mtx_init(&p->p_slock, "process slock", NULL, MTX_SPIN | MTX_RECURSE); cv_init(&p->p_pwait, "ppwait"); cv_init(&p->p_dbgwait, "dbgwait"); TAILQ_INIT(&p->p_threads); /* all threads in proc */ EVENTHANDLER_INVOKE(process_init, p); p->p_stats = pstats_alloc(); SDT_PROBE(proc, kernel, init, return, p, size, flags, 0, 0); return (0); } /* * UMA should ensure that this function is never called. * Freeing a proc structure would violate type stability. */ static void proc_fini(void *mem, int size) { #ifdef notnow struct proc *p; p = (struct proc *)mem; EVENTHANDLER_INVOKE(process_fini, p); pstats_free(p->p_stats); thread_free(FIRST_THREAD_IN_PROC(p)); mtx_destroy(&p->p_mtx); if (p->p_ksi != NULL) ksiginfo_free(p->p_ksi); #else panic("proc reclaimed"); #endif } /* * Is p an inferior of the current process? */ int inferior(p) register struct proc *p; { sx_assert(&proctree_lock, SX_LOCKED); for (; p != curproc; p = p->p_pptr) if (p->p_pid == 0) return (0); return (1); } /* * Locate a process by number; return only "live" processes -- i.e., neither * zombies nor newly born but incompletely initialized processes. By not * returning processes in the PRS_NEW state, we allow callers to avoid * testing for that condition to avoid dereferencing p_ucred, et al. */ struct proc * pfind(pid) register pid_t pid; { register struct proc *p; sx_slock(&allproc_lock); LIST_FOREACH(p, PIDHASH(pid), p_hash) if (p->p_pid == pid) { PROC_LOCK(p); if (p->p_state == PRS_NEW) { PROC_UNLOCK(p); p = NULL; } break; } sx_sunlock(&allproc_lock); return (p); } /* * Locate a process group by number. * The caller must hold proctree_lock. */ struct pgrp * pgfind(pgid) register pid_t pgid; { register struct pgrp *pgrp; sx_assert(&proctree_lock, SX_LOCKED); LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) { if (pgrp->pg_id == pgid) { PGRP_LOCK(pgrp); return (pgrp); } } return (NULL); } /* * Create a new process group. * pgid must be equal to the pid of p. * Begin a new session if required. */ int enterpgrp(p, pgid, pgrp, sess) register struct proc *p; pid_t pgid; struct pgrp *pgrp; struct session *sess; { struct pgrp *pgrp2; sx_assert(&proctree_lock, SX_XLOCKED); KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL")); KASSERT(p->p_pid == pgid, ("enterpgrp: new pgrp and pid != pgid")); pgrp2 = pgfind(pgid); KASSERT(pgrp2 == NULL, ("enterpgrp: pgrp with pgid exists")); KASSERT(!SESS_LEADER(p), ("enterpgrp: session leader attempted setpgrp")); mtx_init(&pgrp->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK); if (sess != NULL) { /* * new session */ mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF); PROC_LOCK(p); p->p_flag &= ~P_CONTROLT; PROC_UNLOCK(p); PGRP_LOCK(pgrp); sess->s_leader = p; sess->s_sid = p->p_pid; refcount_init(&sess->s_count, 1); sess->s_ttyvp = NULL; sess->s_ttydp = NULL; sess->s_ttyp = NULL; bcopy(p->p_session->s_login, sess->s_login, sizeof(sess->s_login)); pgrp->pg_session = sess; KASSERT(p == curproc, ("enterpgrp: mksession and p != curproc")); } else { pgrp->pg_session = p->p_session; sess_hold(pgrp->pg_session); PGRP_LOCK(pgrp); } pgrp->pg_id = pgid; LIST_INIT(&pgrp->pg_members); /* * As we have an exclusive lock of proctree_lock, * this should not deadlock. */ LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash); pgrp->pg_jobc = 0; SLIST_INIT(&pgrp->pg_sigiolst); PGRP_UNLOCK(pgrp); doenterpgrp(p, pgrp); return (0); } /* * Move p to an existing process group */ int enterthispgrp(p, pgrp) register struct proc *p; struct pgrp *pgrp; { sx_assert(&proctree_lock, SX_XLOCKED); PROC_LOCK_ASSERT(p, MA_NOTOWNED); PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED); SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED); KASSERT(pgrp->pg_session == p->p_session, ("%s: pgrp's session %p, p->p_session %p.\n", __func__, pgrp->pg_session, p->p_session)); KASSERT(pgrp != p->p_pgrp, ("%s: p belongs to pgrp.", __func__)); doenterpgrp(p, pgrp); return (0); } /* * Move p to a process group */ static void doenterpgrp(p, pgrp) struct proc *p; struct pgrp *pgrp; { struct pgrp *savepgrp; sx_assert(&proctree_lock, SX_XLOCKED); PROC_LOCK_ASSERT(p, MA_NOTOWNED); PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED); SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED); savepgrp = p->p_pgrp; /* * Adjust eligibility of affected pgrps to participate in job control. * Increment eligibility counts before decrementing, otherwise we * could reach 0 spuriously during the first call. */ fixjobc(p, pgrp, 1); fixjobc(p, p->p_pgrp, 0); PGRP_LOCK(pgrp); PGRP_LOCK(savepgrp); PROC_LOCK(p); LIST_REMOVE(p, p_pglist); p->p_pgrp = pgrp; PROC_UNLOCK(p); LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist); PGRP_UNLOCK(savepgrp); PGRP_UNLOCK(pgrp); if (LIST_EMPTY(&savepgrp->pg_members)) pgdelete(savepgrp); } /* * remove process from process group */ int leavepgrp(p) register struct proc *p; { struct pgrp *savepgrp; sx_assert(&proctree_lock, SX_XLOCKED); savepgrp = p->p_pgrp; PGRP_LOCK(savepgrp); PROC_LOCK(p); LIST_REMOVE(p, p_pglist); p->p_pgrp = NULL; PROC_UNLOCK(p); PGRP_UNLOCK(savepgrp); if (LIST_EMPTY(&savepgrp->pg_members)) pgdelete(savepgrp); return (0); } /* * delete a process group */ static void pgdelete(pgrp) register struct pgrp *pgrp; { struct session *savesess; struct tty *tp; sx_assert(&proctree_lock, SX_XLOCKED); PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED); /* * Reset any sigio structures pointing to us as a result of * F_SETOWN with our pgid. */ funsetownlst(&pgrp->pg_sigiolst); PGRP_LOCK(pgrp); tp = pgrp->pg_session->s_ttyp; LIST_REMOVE(pgrp, pg_hash); savesess = pgrp->pg_session; PGRP_UNLOCK(pgrp); /* Remove the reference to the pgrp before deallocating it. */ if (tp != NULL) { tty_lock(tp); tty_rel_pgrp(tp, pgrp); } mtx_destroy(&pgrp->pg_mtx); free(pgrp, M_PGRP); sess_release(savesess); } static void pgadjustjobc(pgrp, entering) struct pgrp *pgrp; int entering; { PGRP_LOCK(pgrp); if (entering) pgrp->pg_jobc++; else { --pgrp->pg_jobc; if (pgrp->pg_jobc == 0) orphanpg(pgrp); } PGRP_UNLOCK(pgrp); } /* * Adjust pgrp jobc counters when specified process changes process group. * We count the number of processes in each process group that "qualify" * the group for terminal job control (those with a parent in a different * process group of the same session). If that count reaches zero, the * process group becomes orphaned. Check both the specified process' * process group and that of its children. * entering == 0 => p is leaving specified group. * entering == 1 => p is entering specified group. */ void fixjobc(p, pgrp, entering) register struct proc *p; register struct pgrp *pgrp; int entering; { register struct pgrp *hispgrp; register struct session *mysession; sx_assert(&proctree_lock, SX_LOCKED); PROC_LOCK_ASSERT(p, MA_NOTOWNED); PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED); /* * Check p's parent to see whether p qualifies its own process * group; if so, adjust count for p's process group. */ mysession = pgrp->pg_session; if ((hispgrp = p->p_pptr->p_pgrp) != pgrp && hispgrp->pg_session == mysession) pgadjustjobc(pgrp, entering); /* * Check this process' children to see whether they qualify * their process groups; if so, adjust counts for children's * process groups. */ LIST_FOREACH(p, &p->p_children, p_sibling) { hispgrp = p->p_pgrp; if (hispgrp == pgrp || hispgrp->pg_session != mysession) continue; PROC_LOCK(p); if (p->p_state == PRS_ZOMBIE) { PROC_UNLOCK(p); continue; } PROC_UNLOCK(p); pgadjustjobc(hispgrp, entering); } } /* * A process group has become orphaned; * if there are any stopped processes in the group, * hang-up all process in that group. */ static void orphanpg(pg) struct pgrp *pg; { register struct proc *p; PGRP_LOCK_ASSERT(pg, MA_OWNED); LIST_FOREACH(p, &pg->pg_members, p_pglist) { PROC_LOCK(p); if (P_SHOULDSTOP(p)) { PROC_UNLOCK(p); LIST_FOREACH(p, &pg->pg_members, p_pglist) { PROC_LOCK(p); psignal(p, SIGHUP); psignal(p, SIGCONT); PROC_UNLOCK(p); } return; } PROC_UNLOCK(p); } } void sess_hold(struct session *s) { refcount_acquire(&s->s_count); } void sess_release(struct session *s) { if (refcount_release(&s->s_count)) { if (s->s_ttyp != NULL) { tty_lock(s->s_ttyp); tty_rel_sess(s->s_ttyp, s); } mtx_destroy(&s->s_mtx); free(s, M_SESSION); } } #include "opt_ddb.h" #ifdef DDB #include DB_SHOW_COMMAND(pgrpdump, pgrpdump) { register struct pgrp *pgrp; register struct proc *p; register int i; for (i = 0; i <= pgrphash; i++) { if (!LIST_EMPTY(&pgrphashtbl[i])) { printf("\tindx %d\n", i); LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) { printf( "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n", (void *)pgrp, (long)pgrp->pg_id, (void *)pgrp->pg_session, pgrp->pg_session->s_count, (void *)LIST_FIRST(&pgrp->pg_members)); LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { printf("\t\tpid %ld addr %p pgrp %p\n", (long)p->p_pid, (void *)p, (void *)p->p_pgrp); } } } } } #endif /* DDB */ /* * Calculate the kinfo_proc members which contain process-wide * informations. * Must be called with the target process locked. */ static void fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp) { struct thread *td; PROC_LOCK_ASSERT(p, MA_OWNED); kp->ki_estcpu = 0; kp->ki_pctcpu = 0; FOREACH_THREAD_IN_PROC(p, td) { thread_lock(td); kp->ki_pctcpu += sched_pctcpu(td); kp->ki_estcpu += td->td_estcpu; thread_unlock(td); } } /* * Clear kinfo_proc and fill in any information that is common * to all threads in the process. * Must be called with the target process locked. */ static void fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp) { struct thread *td0; struct tty *tp; struct session *sp; struct ucred *cred; struct sigacts *ps; PROC_LOCK_ASSERT(p, MA_OWNED); bzero(kp, sizeof(*kp)); kp->ki_structsize = sizeof(*kp); kp->ki_paddr = p; kp->ki_addr =/* p->p_addr; */0; /* XXX */ kp->ki_args = p->p_args; kp->ki_textvp = p->p_textvp; #ifdef KTRACE kp->ki_tracep = p->p_tracevp; kp->ki_traceflag = p->p_traceflag; #endif kp->ki_fd = p->p_fd; kp->ki_vmspace = p->p_vmspace; kp->ki_flag = p->p_flag; cred = p->p_ucred; if (cred) { kp->ki_uid = cred->cr_uid; kp->ki_ruid = cred->cr_ruid; kp->ki_svuid = cred->cr_svuid; kp->ki_cr_flags = 0; if (cred->cr_flags & CRED_FLAG_CAPMODE) kp->ki_cr_flags |= KI_CRF_CAPABILITY_MODE; /* XXX bde doesn't like KI_NGROUPS */ if (cred->cr_ngroups > KI_NGROUPS) { kp->ki_ngroups = KI_NGROUPS; kp->ki_cr_flags |= KI_CRF_GRP_OVERFLOW; } else kp->ki_ngroups = cred->cr_ngroups; bcopy(cred->cr_groups, kp->ki_groups, kp->ki_ngroups * sizeof(gid_t)); kp->ki_rgid = cred->cr_rgid; kp->ki_svgid = cred->cr_svgid; /* If jailed(cred), emulate the old P_JAILED flag. */ if (jailed(cred)) { kp->ki_flag |= P_JAILED; /* If inside the jail, use 0 as a jail ID. */ if (cred->cr_prison != curthread->td_ucred->cr_prison) kp->ki_jid = cred->cr_prison->pr_id; } strlcpy(kp->ki_loginclass, cred->cr_loginclass->lc_name, sizeof(kp->ki_loginclass)); } ps = p->p_sigacts; if (ps) { mtx_lock(&ps->ps_mtx); kp->ki_sigignore = ps->ps_sigignore; kp->ki_sigcatch = ps->ps_sigcatch; mtx_unlock(&ps->ps_mtx); } if (p->p_state != PRS_NEW && p->p_state != PRS_ZOMBIE && p->p_vmspace != NULL) { struct vmspace *vm = p->p_vmspace; kp->ki_size = vm->vm_map.size; kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/ FOREACH_THREAD_IN_PROC(p, td0) { if (!TD_IS_SWAPPED(td0)) kp->ki_rssize += td0->td_kstack_pages; } kp->ki_swrss = vm->vm_swrss; kp->ki_tsize = vm->vm_tsize; kp->ki_dsize = vm->vm_dsize; kp->ki_ssize = vm->vm_ssize; } else if (p->p_state == PRS_ZOMBIE) kp->ki_stat = SZOMB; if (kp->ki_flag & P_INMEM) kp->ki_sflag = PS_INMEM; else kp->ki_sflag = 0; /* Calculate legacy swtime as seconds since 'swtick'. */ kp->ki_swtime = (ticks - p->p_swtick) / hz; kp->ki_pid = p->p_pid; kp->ki_nice = p->p_nice; kp->ki_start = p->p_stats->p_start; timevaladd(&kp->ki_start, &boottime); PROC_SLOCK(p); rufetch(p, &kp->ki_rusage); kp->ki_runtime = cputick2usec(p->p_rux.rux_runtime); calcru(p, &kp->ki_rusage.ru_utime, &kp->ki_rusage.ru_stime); PROC_SUNLOCK(p); calccru(p, &kp->ki_childutime, &kp->ki_childstime); /* Some callers want child times in a single value. */ kp->ki_childtime = kp->ki_childstime; timevaladd(&kp->ki_childtime, &kp->ki_childutime); tp = NULL; if (p->p_pgrp) { kp->ki_pgid = p->p_pgrp->pg_id; kp->ki_jobc = p->p_pgrp->pg_jobc; sp = p->p_pgrp->pg_session; if (sp != NULL) { kp->ki_sid = sp->s_sid; SESS_LOCK(sp); strlcpy(kp->ki_login, sp->s_login, sizeof(kp->ki_login)); if (sp->s_ttyvp) kp->ki_kiflag |= KI_CTTY; if (SESS_LEADER(p)) kp->ki_kiflag |= KI_SLEADER; /* XXX proctree_lock */ tp = sp->s_ttyp; SESS_UNLOCK(sp); } } if ((p->p_flag & P_CONTROLT) && tp != NULL) { kp->ki_tdev = tty_udev(tp); kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID; if (tp->t_session) kp->ki_tsid = tp->t_session->s_sid; } else kp->ki_tdev = NODEV; if (p->p_comm[0] != '\0') strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm)); if (p->p_sysent && p->p_sysent->sv_name != NULL && p->p_sysent->sv_name[0] != '\0') strlcpy(kp->ki_emul, p->p_sysent->sv_name, sizeof(kp->ki_emul)); kp->ki_siglist = p->p_siglist; kp->ki_xstat = p->p_xstat; kp->ki_acflag = p->p_acflag; kp->ki_lock = p->p_lock; if (p->p_pptr) kp->ki_ppid = p->p_pptr->p_pid; } /* * Fill in information that is thread specific. Must be called with * target process locked. If 'preferthread' is set, overwrite certain * process-related fields that are maintained for both threads and * processes. */ static void fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp, int preferthread) { struct proc *p; p = td->td_proc; kp->ki_tdaddr = td; PROC_LOCK_ASSERT(p, MA_OWNED); thread_lock(td); if (td->td_wmesg != NULL) strlcpy(kp->ki_wmesg, td->td_wmesg, sizeof(kp->ki_wmesg)); else bzero(kp->ki_wmesg, sizeof(kp->ki_wmesg)); strlcpy(kp->ki_ocomm, td->td_name, sizeof(kp->ki_ocomm)); if (TD_ON_LOCK(td)) { kp->ki_kiflag |= KI_LOCKBLOCK; strlcpy(kp->ki_lockname, td->td_lockname, sizeof(kp->ki_lockname)); } else { kp->ki_kiflag &= ~KI_LOCKBLOCK; bzero(kp->ki_lockname, sizeof(kp->ki_lockname)); } if (p->p_state == PRS_NORMAL) { /* approximate. */ if (TD_ON_RUNQ(td) || TD_CAN_RUN(td) || TD_IS_RUNNING(td)) { kp->ki_stat = SRUN; } else if (P_SHOULDSTOP(p)) { kp->ki_stat = SSTOP; } else if (TD_IS_SLEEPING(td)) { kp->ki_stat = SSLEEP; } else if (TD_ON_LOCK(td)) { kp->ki_stat = SLOCK; } else { kp->ki_stat = SWAIT; } } else if (p->p_state == PRS_ZOMBIE) { kp->ki_stat = SZOMB; } else { kp->ki_stat = SIDL; } /* Things in the thread */ kp->ki_wchan = td->td_wchan; kp->ki_pri.pri_level = td->td_priority; kp->ki_pri.pri_native = td->td_base_pri; kp->ki_lastcpu = td->td_lastcpu; kp->ki_oncpu = td->td_oncpu; kp->ki_tdflags = td->td_flags; kp->ki_tid = td->td_tid; kp->ki_numthreads = p->p_numthreads; kp->ki_pcb = td->td_pcb; kp->ki_kstack = (void *)td->td_kstack; kp->ki_slptime = (ticks - td->td_slptick) / hz; kp->ki_pri.pri_class = td->td_pri_class; kp->ki_pri.pri_user = td->td_user_pri; if (preferthread) { kp->ki_runtime = cputick2usec(td->td_rux.rux_runtime); kp->ki_pctcpu = sched_pctcpu(td); kp->ki_estcpu = td->td_estcpu; } /* We can't get this anymore but ps etc never used it anyway. */ kp->ki_rqindex = 0; if (preferthread) kp->ki_siglist = td->td_siglist; kp->ki_sigmask = td->td_sigmask; thread_unlock(td); } /* * Fill in a kinfo_proc structure for the specified process. * Must be called with the target process locked. */ void fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp) { MPASS(FIRST_THREAD_IN_PROC(p) != NULL); fill_kinfo_proc_only(p, kp); fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp, 0); fill_kinfo_aggregate(p, kp); } struct pstats * pstats_alloc(void) { return (malloc(sizeof(struct pstats), M_SUBPROC, M_ZERO|M_WAITOK)); } /* * Copy parts of p_stats; zero the rest of p_stats (statistics). */ void pstats_fork(struct pstats *src, struct pstats *dst) { bzero(&dst->pstat_startzero, __rangeof(struct pstats, pstat_startzero, pstat_endzero)); bcopy(&src->pstat_startcopy, &dst->pstat_startcopy, __rangeof(struct pstats, pstat_startcopy, pstat_endcopy)); } void pstats_free(struct pstats *ps) { free(ps, M_SUBPROC); } /* * Locate a zombie process by number */ struct proc * zpfind(pid_t pid) { struct proc *p; sx_slock(&allproc_lock); LIST_FOREACH(p, &zombproc, p_list) if (p->p_pid == pid) { PROC_LOCK(p); break; } sx_sunlock(&allproc_lock); return (p); } #define KERN_PROC_ZOMBMASK 0x3 #define KERN_PROC_NOTHREADS 0x4 #ifdef COMPAT_FREEBSD32 /* * This function is typically used to copy out the kernel address, so * it can be replaced by assignment of zero. */ static inline uint32_t ptr32_trim(void *ptr) { uintptr_t uptr; uptr = (uintptr_t)ptr; return ((uptr > UINT_MAX) ? 0 : uptr); } #define PTRTRIM_CP(src,dst,fld) \ do { (dst).fld = ptr32_trim((src).fld); } while (0) static void freebsd32_kinfo_proc_out(const struct kinfo_proc *ki, struct kinfo_proc32 *ki32) { int i; bzero(ki32, sizeof(struct kinfo_proc32)); ki32->ki_structsize = sizeof(struct kinfo_proc32); CP(*ki, *ki32, ki_layout); PTRTRIM_CP(*ki, *ki32, ki_args); PTRTRIM_CP(*ki, *ki32, ki_paddr); PTRTRIM_CP(*ki, *ki32, ki_addr); PTRTRIM_CP(*ki, *ki32, ki_tracep); PTRTRIM_CP(*ki, *ki32, ki_textvp); PTRTRIM_CP(*ki, *ki32, ki_fd); PTRTRIM_CP(*ki, *ki32, ki_vmspace); PTRTRIM_CP(*ki, *ki32, ki_wchan); CP(*ki, *ki32, ki_pid); CP(*ki, *ki32, ki_ppid); CP(*ki, *ki32, ki_pgid); CP(*ki, *ki32, ki_tpgid); CP(*ki, *ki32, ki_sid); CP(*ki, *ki32, ki_tsid); CP(*ki, *ki32, ki_jobc); CP(*ki, *ki32, ki_tdev); CP(*ki, *ki32, ki_siglist); CP(*ki, *ki32, ki_sigmask); CP(*ki, *ki32, ki_sigignore); CP(*ki, *ki32, ki_sigcatch); CP(*ki, *ki32, ki_uid); CP(*ki, *ki32, ki_ruid); CP(*ki, *ki32, ki_svuid); CP(*ki, *ki32, ki_rgid); CP(*ki, *ki32, ki_svgid); CP(*ki, *ki32, ki_ngroups); for (i = 0; i < KI_NGROUPS; i++) CP(*ki, *ki32, ki_groups[i]); CP(*ki, *ki32, ki_size); CP(*ki, *ki32, ki_rssize); CP(*ki, *ki32, ki_swrss); CP(*ki, *ki32, ki_tsize); CP(*ki, *ki32, ki_dsize); CP(*ki, *ki32, ki_ssize); CP(*ki, *ki32, ki_xstat); CP(*ki, *ki32, ki_acflag); CP(*ki, *ki32, ki_pctcpu); CP(*ki, *ki32, ki_estcpu); CP(*ki, *ki32, ki_slptime); CP(*ki, *ki32, ki_swtime); CP(*ki, *ki32, ki_runtime); TV_CP(*ki, *ki32, ki_start); TV_CP(*ki, *ki32, ki_childtime); CP(*ki, *ki32, ki_flag); CP(*ki, *ki32, ki_kiflag); CP(*ki, *ki32, ki_traceflag); CP(*ki, *ki32, ki_stat); CP(*ki, *ki32, ki_nice); CP(*ki, *ki32, ki_lock); CP(*ki, *ki32, ki_rqindex); CP(*ki, *ki32, ki_oncpu); CP(*ki, *ki32, ki_lastcpu); bcopy(ki->ki_ocomm, ki32->ki_ocomm, OCOMMLEN + 1); bcopy(ki->ki_wmesg, ki32->ki_wmesg, WMESGLEN + 1); bcopy(ki->ki_login, ki32->ki_login, LOGNAMELEN + 1); bcopy(ki->ki_lockname, ki32->ki_lockname, LOCKNAMELEN + 1); bcopy(ki->ki_comm, ki32->ki_comm, COMMLEN + 1); bcopy(ki->ki_emul, ki32->ki_emul, KI_EMULNAMELEN + 1); bcopy(ki->ki_loginclass, ki32->ki_loginclass, LOGINCLASSLEN + 1); CP(*ki, *ki32, ki_cr_flags); CP(*ki, *ki32, ki_jid); CP(*ki, *ki32, ki_numthreads); CP(*ki, *ki32, ki_tid); CP(*ki, *ki32, ki_pri); freebsd32_rusage_out(&ki->ki_rusage, &ki32->ki_rusage); freebsd32_rusage_out(&ki->ki_rusage_ch, &ki32->ki_rusage_ch); PTRTRIM_CP(*ki, *ki32, ki_pcb); PTRTRIM_CP(*ki, *ki32, ki_kstack); PTRTRIM_CP(*ki, *ki32, ki_udata); CP(*ki, *ki32, ki_sflag); CP(*ki, *ki32, ki_tdflags); } static int sysctl_out_proc_copyout(struct kinfo_proc *ki, struct sysctl_req *req) { struct kinfo_proc32 ki32; int error; if (req->flags & SCTL_MASK32) { freebsd32_kinfo_proc_out(ki, &ki32); error = SYSCTL_OUT(req, &ki32, sizeof(struct kinfo_proc32)); } else error = SYSCTL_OUT(req, ki, sizeof(struct kinfo_proc)); return (error); } #else static int sysctl_out_proc_copyout(struct kinfo_proc *ki, struct sysctl_req *req) { return (SYSCTL_OUT(req, ki, sizeof(struct kinfo_proc))); } #endif /* * Must be called with the process locked and will return with it unlocked. */ static int sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags) { struct thread *td; struct kinfo_proc kinfo_proc; int error = 0; struct proc *np; pid_t pid = p->p_pid; PROC_LOCK_ASSERT(p, MA_OWNED); MPASS(FIRST_THREAD_IN_PROC(p) != NULL); fill_kinfo_proc(p, &kinfo_proc); if (flags & KERN_PROC_NOTHREADS) error = sysctl_out_proc_copyout(&kinfo_proc, req); else { FOREACH_THREAD_IN_PROC(p, td) { fill_kinfo_thread(td, &kinfo_proc, 1); error = sysctl_out_proc_copyout(&kinfo_proc, req); if (error) break; } } PROC_UNLOCK(p); if (error) return (error); if (flags & KERN_PROC_ZOMBMASK) np = zpfind(pid); else { if (pid == 0) return (0); np = pfind(pid); } if (np == NULL) return (ESRCH); if (np != p) { PROC_UNLOCK(np); return (ESRCH); } PROC_UNLOCK(np); return (0); } static int sysctl_kern_proc(SYSCTL_HANDLER_ARGS) { int *name = (int*) arg1; u_int namelen = arg2; struct proc *p; int flags, doingzomb, oid_number; int error = 0; oid_number = oidp->oid_number; if (oid_number != KERN_PROC_ALL && (oid_number & KERN_PROC_INC_THREAD) == 0) flags = KERN_PROC_NOTHREADS; else { flags = 0; oid_number &= ~KERN_PROC_INC_THREAD; } if (oid_number == KERN_PROC_PID) { if (namelen != 1) return (EINVAL); error = sysctl_wire_old_buffer(req, 0); if (error) return (error); p = pfind((pid_t)name[0]); if (!p) return (ESRCH); if ((error = p_cansee(curthread, p))) { PROC_UNLOCK(p); return (error); } error = sysctl_out_proc(p, req, flags); return (error); } switch (oid_number) { case KERN_PROC_ALL: if (namelen != 0) return (EINVAL); break; case KERN_PROC_PROC: if (namelen != 0 && namelen != 1) return (EINVAL); break; default: if (namelen != 1) return (EINVAL); break; } if (!req->oldptr) { /* overestimate by 5 procs */ error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5); if (error) return (error); } error = sysctl_wire_old_buffer(req, 0); if (error != 0) return (error); sx_slock(&allproc_lock); for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) { if (!doingzomb) p = LIST_FIRST(&allproc); else p = LIST_FIRST(&zombproc); for (; p != 0; p = LIST_NEXT(p, p_list)) { /* * Skip embryonic processes. */ PROC_LOCK(p); if (p->p_state == PRS_NEW) { PROC_UNLOCK(p); continue; } KASSERT(p->p_ucred != NULL, ("process credential is NULL for non-NEW proc")); /* * Show a user only appropriate processes. */ if (p_cansee(curthread, p)) { PROC_UNLOCK(p); continue; } /* * TODO - make more efficient (see notes below). * do by session. */ switch (oid_number) { case KERN_PROC_GID: if (p->p_ucred->cr_gid != (gid_t)name[0]) { PROC_UNLOCK(p); continue; } break; case KERN_PROC_PGRP: /* could do this by traversing pgrp */ if (p->p_pgrp == NULL || p->p_pgrp->pg_id != (pid_t)name[0]) { PROC_UNLOCK(p); continue; } break; case KERN_PROC_RGID: if (p->p_ucred->cr_rgid != (gid_t)name[0]) { PROC_UNLOCK(p); continue; } break; case KERN_PROC_SESSION: if (p->p_session == NULL || p->p_session->s_sid != (pid_t)name[0]) { PROC_UNLOCK(p); continue; } break; case KERN_PROC_TTY: if ((p->p_flag & P_CONTROLT) == 0 || p->p_session == NULL) { PROC_UNLOCK(p); continue; } /* XXX proctree_lock */ SESS_LOCK(p->p_session); if (p->p_session->s_ttyp == NULL || tty_udev(p->p_session->s_ttyp) != (dev_t)name[0]) { SESS_UNLOCK(p->p_session); PROC_UNLOCK(p); continue; } SESS_UNLOCK(p->p_session); break; case KERN_PROC_UID: if (p->p_ucred->cr_uid != (uid_t)name[0]) { PROC_UNLOCK(p); continue; } break; case KERN_PROC_RUID: if (p->p_ucred->cr_ruid != (uid_t)name[0]) { PROC_UNLOCK(p); continue; } break; case KERN_PROC_PROC: break; default: break; } error = sysctl_out_proc(p, req, flags | doingzomb); if (error) { sx_sunlock(&allproc_lock); return (error); } } } sx_sunlock(&allproc_lock); return (0); } struct pargs * pargs_alloc(int len) { struct pargs *pa; pa = malloc(sizeof(struct pargs) + len, M_PARGS, M_WAITOK); refcount_init(&pa->ar_ref, 1); pa->ar_length = len; return (pa); } static void pargs_free(struct pargs *pa) { free(pa, M_PARGS); } void pargs_hold(struct pargs *pa) { if (pa == NULL) return; refcount_acquire(&pa->ar_ref); } void pargs_drop(struct pargs *pa) { if (pa == NULL) return; if (refcount_release(&pa->ar_ref)) pargs_free(pa); } /* * This sysctl allows a process to retrieve the argument list or process * title for another process without groping around in the address space * of the other process. It also allow a process to set its own "process * title to a string of its own choice. */ static int sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS) { int *name = (int*) arg1; u_int namelen = arg2; struct pargs *newpa, *pa; struct proc *p; int error = 0; if (namelen != 1) return (EINVAL); p = pfind((pid_t)name[0]); if (!p) return (ESRCH); if ((error = p_cansee(curthread, p)) != 0) { PROC_UNLOCK(p); return (error); } if (req->newptr && curproc != p) { PROC_UNLOCK(p); return (EPERM); } pa = p->p_args; pargs_hold(pa); PROC_UNLOCK(p); if (req->oldptr != NULL && pa != NULL) error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length); pargs_drop(pa); if (error != 0 || req->newptr == NULL) return (error); if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit) return (ENOMEM); newpa = pargs_alloc(req->newlen); error = SYSCTL_IN(req, newpa->ar_args, req->newlen); if (error != 0) { pargs_free(newpa); return (error); } PROC_LOCK(p); pa = p->p_args; p->p_args = newpa; PROC_UNLOCK(p); pargs_drop(pa); return (0); } /* * This sysctl allows a process to retrieve the path of the executable for * itself or another process. */ static int sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS) { pid_t *pidp = (pid_t *)arg1; unsigned int arglen = arg2; struct proc *p; struct vnode *vp; char *retbuf, *freebuf; int error, vfslocked; if (arglen != 1) return (EINVAL); if (*pidp == -1) { /* -1 means this process */ p = req->td->td_proc; } else { p = pfind(*pidp); if (p == NULL) return (ESRCH); if ((error = p_cansee(curthread, p)) != 0) { PROC_UNLOCK(p); return (error); } } vp = p->p_textvp; if (vp == NULL) { if (*pidp != -1) PROC_UNLOCK(p); return (0); } vref(vp); if (*pidp != -1) PROC_UNLOCK(p); error = vn_fullpath(req->td, vp, &retbuf, &freebuf); vfslocked = VFS_LOCK_GIANT(vp->v_mount); vrele(vp); VFS_UNLOCK_GIANT(vfslocked); if (error) return (error); error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1); free(freebuf, M_TEMP); return (error); } static int sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS) { struct proc *p; char *sv_name; int *name; int namelen; int error; namelen = arg2; if (namelen != 1) return (EINVAL); name = (int *)arg1; if ((p = pfind((pid_t)name[0])) == NULL) return (ESRCH); if ((error = p_cansee(curthread, p))) { PROC_UNLOCK(p); return (error); } sv_name = p->p_sysent->sv_name; PROC_UNLOCK(p); return (sysctl_handle_string(oidp, sv_name, 0, req)); } #ifdef KINFO_OVMENTRY_SIZE CTASSERT(sizeof(struct kinfo_ovmentry) == KINFO_OVMENTRY_SIZE); #endif #ifdef COMPAT_FREEBSD7 static int sysctl_kern_proc_ovmmap(SYSCTL_HANDLER_ARGS) { vm_map_entry_t entry, tmp_entry; unsigned int last_timestamp; char *fullpath, *freepath; struct kinfo_ovmentry *kve; struct vattr va; struct ucred *cred; int error, *name; struct vnode *vp; struct proc *p; vm_map_t map; struct vmspace *vm; name = (int *)arg1; if ((p = pfind((pid_t)name[0])) == NULL) return (ESRCH); if (p->p_flag & P_WEXIT) { PROC_UNLOCK(p); return (ESRCH); } if ((error = p_candebug(curthread, p))) { PROC_UNLOCK(p); return (error); } _PHOLD(p); PROC_UNLOCK(p); vm = vmspace_acquire_ref(p); if (vm == NULL) { PRELE(p); return (ESRCH); } kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK); map = &p->p_vmspace->vm_map; /* XXXRW: More locking required? */ vm_map_lock_read(map); for (entry = map->header.next; entry != &map->header; entry = entry->next) { vm_object_t obj, tobj, lobj; vm_offset_t addr; int vfslocked; if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) continue; bzero(kve, sizeof(*kve)); kve->kve_structsize = sizeof(*kve); kve->kve_private_resident = 0; obj = entry->object.vm_object; if (obj != NULL) { VM_OBJECT_LOCK(obj); if (obj->shadow_count == 1) kve->kve_private_resident = obj->resident_page_count; } kve->kve_resident = 0; addr = entry->start; while (addr < entry->end) { if (pmap_extract(map->pmap, addr)) kve->kve_resident++; addr += PAGE_SIZE; } for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) { if (tobj != obj) VM_OBJECT_LOCK(tobj); if (lobj != obj) VM_OBJECT_UNLOCK(lobj); lobj = tobj; } kve->kve_start = (void*)entry->start; kve->kve_end = (void*)entry->end; kve->kve_offset = (off_t)entry->offset; if (entry->protection & VM_PROT_READ) kve->kve_protection |= KVME_PROT_READ; if (entry->protection & VM_PROT_WRITE) kve->kve_protection |= KVME_PROT_WRITE; if (entry->protection & VM_PROT_EXECUTE) kve->kve_protection |= KVME_PROT_EXEC; if (entry->eflags & MAP_ENTRY_COW) kve->kve_flags |= KVME_FLAG_COW; if (entry->eflags & MAP_ENTRY_NEEDS_COPY) kve->kve_flags |= KVME_FLAG_NEEDS_COPY; if (entry->eflags & MAP_ENTRY_NOCOREDUMP) kve->kve_flags |= KVME_FLAG_NOCOREDUMP; last_timestamp = map->timestamp; vm_map_unlock_read(map); kve->kve_fileid = 0; kve->kve_fsid = 0; freepath = NULL; fullpath = ""; if (lobj) { vp = NULL; switch (lobj->type) { case OBJT_DEFAULT: kve->kve_type = KVME_TYPE_DEFAULT; break; case OBJT_VNODE: kve->kve_type = KVME_TYPE_VNODE; vp = lobj->handle; vref(vp); break; case OBJT_SWAP: kve->kve_type = KVME_TYPE_SWAP; break; case OBJT_DEVICE: kve->kve_type = KVME_TYPE_DEVICE; break; case OBJT_PHYS: kve->kve_type = KVME_TYPE_PHYS; break; case OBJT_DEAD: kve->kve_type = KVME_TYPE_DEAD; break; case OBJT_SG: kve->kve_type = KVME_TYPE_SG; break; default: kve->kve_type = KVME_TYPE_UNKNOWN; break; } if (lobj != obj) VM_OBJECT_UNLOCK(lobj); kve->kve_ref_count = obj->ref_count; kve->kve_shadow_count = obj->shadow_count; VM_OBJECT_UNLOCK(obj); if (vp != NULL) { vn_fullpath(curthread, vp, &fullpath, &freepath); cred = curthread->td_ucred; vfslocked = VFS_LOCK_GIANT(vp->v_mount); vn_lock(vp, LK_SHARED | LK_RETRY); if (VOP_GETATTR(vp, &va, cred) == 0) { kve->kve_fileid = va.va_fileid; kve->kve_fsid = va.va_fsid; } vput(vp); VFS_UNLOCK_GIANT(vfslocked); } } else { kve->kve_type = KVME_TYPE_NONE; kve->kve_ref_count = 0; kve->kve_shadow_count = 0; } strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path)); if (freepath != NULL) free(freepath, M_TEMP); error = SYSCTL_OUT(req, kve, sizeof(*kve)); vm_map_lock_read(map); if (error) break; if (last_timestamp != map->timestamp) { vm_map_lookup_entry(map, addr - 1, &tmp_entry); entry = tmp_entry; } } vm_map_unlock_read(map); vmspace_free(vm); PRELE(p); free(kve, M_TEMP); return (error); } #endif /* COMPAT_FREEBSD7 */ #ifdef KINFO_VMENTRY_SIZE CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE); #endif static int sysctl_kern_proc_vmmap(SYSCTL_HANDLER_ARGS) { vm_map_entry_t entry, tmp_entry; unsigned int last_timestamp; char *fullpath, *freepath; struct kinfo_vmentry *kve; struct vattr va; struct ucred *cred; int error, *name; struct vnode *vp; struct proc *p; struct vmspace *vm; vm_map_t map; name = (int *)arg1; if ((p = pfind((pid_t)name[0])) == NULL) return (ESRCH); if (p->p_flag & P_WEXIT) { PROC_UNLOCK(p); return (ESRCH); } if ((error = p_candebug(curthread, p))) { PROC_UNLOCK(p); return (error); } _PHOLD(p); PROC_UNLOCK(p); vm = vmspace_acquire_ref(p); if (vm == NULL) { PRELE(p); return (ESRCH); } kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK); map = &vm->vm_map; /* XXXRW: More locking required? */ vm_map_lock_read(map); for (entry = map->header.next; entry != &map->header; entry = entry->next) { vm_object_t obj, tobj, lobj; vm_offset_t addr; int vfslocked; if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) continue; bzero(kve, sizeof(*kve)); kve->kve_private_resident = 0; obj = entry->object.vm_object; if (obj != NULL) { VM_OBJECT_LOCK(obj); if (obj->shadow_count == 1) kve->kve_private_resident = obj->resident_page_count; } kve->kve_resident = 0; addr = entry->start; while (addr < entry->end) { if (pmap_extract(map->pmap, addr)) kve->kve_resident++; addr += PAGE_SIZE; } for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) { if (tobj != obj) VM_OBJECT_LOCK(tobj); if (lobj != obj) VM_OBJECT_UNLOCK(lobj); lobj = tobj; } kve->kve_start = entry->start; kve->kve_end = entry->end; kve->kve_offset = entry->offset; if (entry->protection & VM_PROT_READ) kve->kve_protection |= KVME_PROT_READ; if (entry->protection & VM_PROT_WRITE) kve->kve_protection |= KVME_PROT_WRITE; if (entry->protection & VM_PROT_EXECUTE) kve->kve_protection |= KVME_PROT_EXEC; if (entry->eflags & MAP_ENTRY_COW) kve->kve_flags |= KVME_FLAG_COW; if (entry->eflags & MAP_ENTRY_NEEDS_COPY) kve->kve_flags |= KVME_FLAG_NEEDS_COPY; if (entry->eflags & MAP_ENTRY_NOCOREDUMP) kve->kve_flags |= KVME_FLAG_NOCOREDUMP; last_timestamp = map->timestamp; vm_map_unlock_read(map); - kve->kve_fileid = 0; - kve->kve_fsid = 0; freepath = NULL; fullpath = ""; if (lobj) { vp = NULL; switch (lobj->type) { case OBJT_DEFAULT: kve->kve_type = KVME_TYPE_DEFAULT; break; case OBJT_VNODE: kve->kve_type = KVME_TYPE_VNODE; vp = lobj->handle; vref(vp); break; case OBJT_SWAP: kve->kve_type = KVME_TYPE_SWAP; break; case OBJT_DEVICE: kve->kve_type = KVME_TYPE_DEVICE; break; case OBJT_PHYS: kve->kve_type = KVME_TYPE_PHYS; break; case OBJT_DEAD: kve->kve_type = KVME_TYPE_DEAD; break; case OBJT_SG: kve->kve_type = KVME_TYPE_SG; break; default: kve->kve_type = KVME_TYPE_UNKNOWN; break; } if (lobj != obj) VM_OBJECT_UNLOCK(lobj); kve->kve_ref_count = obj->ref_count; kve->kve_shadow_count = obj->shadow_count; VM_OBJECT_UNLOCK(obj); if (vp != NULL) { vn_fullpath(curthread, vp, &fullpath, &freepath); + kve->kve_vn_type = vntype_to_kinfo(vp->v_type); cred = curthread->td_ucred; vfslocked = VFS_LOCK_GIANT(vp->v_mount); vn_lock(vp, LK_SHARED | LK_RETRY); if (VOP_GETATTR(vp, &va, cred) == 0) { - kve->kve_fileid = va.va_fileid; - kve->kve_fsid = va.va_fsid; + kve->kve_vn_fileid = va.va_fileid; + kve->kve_vn_fsid = va.va_fsid; + kve->kve_vn_mode = + MAKEIMODE(va.va_type, va.va_mode); + kve->kve_vn_size = va.va_size; + kve->kve_vn_rdev = va.va_rdev; + kve->kve_status = KF_ATTR_VALID; } vput(vp); VFS_UNLOCK_GIANT(vfslocked); } } else { kve->kve_type = KVME_TYPE_NONE; kve->kve_ref_count = 0; kve->kve_shadow_count = 0; } strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path)); if (freepath != NULL) free(freepath, M_TEMP); /* Pack record size down */ kve->kve_structsize = offsetof(struct kinfo_vmentry, kve_path) + strlen(kve->kve_path) + 1; kve->kve_structsize = roundup(kve->kve_structsize, sizeof(uint64_t)); error = SYSCTL_OUT(req, kve, kve->kve_structsize); vm_map_lock_read(map); if (error) break; if (last_timestamp != map->timestamp) { vm_map_lookup_entry(map, addr - 1, &tmp_entry); entry = tmp_entry; } } vm_map_unlock_read(map); vmspace_free(vm); PRELE(p); free(kve, M_TEMP); return (error); } #if defined(STACK) || defined(DDB) static int sysctl_kern_proc_kstack(SYSCTL_HANDLER_ARGS) { struct kinfo_kstack *kkstp; int error, i, *name, numthreads; lwpid_t *lwpidarray; struct thread *td; struct stack *st; struct sbuf sb; struct proc *p; name = (int *)arg1; if ((p = pfind((pid_t)name[0])) == NULL) return (ESRCH); /* XXXRW: Not clear ESRCH is the right error during proc execve(). */ if (p->p_flag & P_WEXIT || p->p_flag & P_INEXEC) { PROC_UNLOCK(p); return (ESRCH); } if ((error = p_candebug(curthread, p))) { PROC_UNLOCK(p); return (error); } _PHOLD(p); PROC_UNLOCK(p); kkstp = malloc(sizeof(*kkstp), M_TEMP, M_WAITOK); st = stack_create(); lwpidarray = NULL; numthreads = 0; PROC_LOCK(p); repeat: if (numthreads < p->p_numthreads) { if (lwpidarray != NULL) { free(lwpidarray, M_TEMP); lwpidarray = NULL; } numthreads = p->p_numthreads; PROC_UNLOCK(p); lwpidarray = malloc(sizeof(*lwpidarray) * numthreads, M_TEMP, M_WAITOK | M_ZERO); PROC_LOCK(p); goto repeat; } i = 0; /* * XXXRW: During the below loop, execve(2) and countless other sorts * of changes could have taken place. Should we check to see if the * vmspace has been replaced, or the like, in order to prevent * giving a snapshot that spans, say, execve(2), with some threads * before and some after? Among other things, the credentials could * have changed, in which case the right to extract debug info might * no longer be assured. */ FOREACH_THREAD_IN_PROC(p, td) { KASSERT(i < numthreads, ("sysctl_kern_proc_kstack: numthreads")); lwpidarray[i] = td->td_tid; i++; } numthreads = i; for (i = 0; i < numthreads; i++) { td = thread_find(p, lwpidarray[i]); if (td == NULL) { continue; } bzero(kkstp, sizeof(*kkstp)); (void)sbuf_new(&sb, kkstp->kkst_trace, sizeof(kkstp->kkst_trace), SBUF_FIXEDLEN); thread_lock(td); kkstp->kkst_tid = td->td_tid; if (TD_IS_SWAPPED(td)) kkstp->kkst_state = KKST_STATE_SWAPPED; else if (TD_IS_RUNNING(td)) kkstp->kkst_state = KKST_STATE_RUNNING; else { kkstp->kkst_state = KKST_STATE_STACKOK; stack_save_td(st, td); } thread_unlock(td); PROC_UNLOCK(p); stack_sbuf_print(&sb, st); sbuf_finish(&sb); sbuf_delete(&sb); error = SYSCTL_OUT(req, kkstp, sizeof(*kkstp)); PROC_LOCK(p); if (error) break; } _PRELE(p); PROC_UNLOCK(p); if (lwpidarray != NULL) free(lwpidarray, M_TEMP); stack_destroy(st); free(kkstp, M_TEMP); return (error); } #endif /* * This sysctl allows a process to retrieve the full list of groups from * itself or another process. */ static int sysctl_kern_proc_groups(SYSCTL_HANDLER_ARGS) { pid_t *pidp = (pid_t *)arg1; unsigned int arglen = arg2; struct proc *p; struct ucred *cred; int error; if (arglen != 1) return (EINVAL); if (*pidp == -1) { /* -1 means this process */ p = req->td->td_proc; } else { p = pfind(*pidp); if (p == NULL) return (ESRCH); if ((error = p_cansee(curthread, p)) != 0) { PROC_UNLOCK(p); return (error); } } cred = crhold(p->p_ucred); if (*pidp != -1) PROC_UNLOCK(p); error = SYSCTL_OUT(req, cred->cr_groups, cred->cr_ngroups * sizeof(gid_t)); crfree(cred); return (error); } SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 0, "Process table"); SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT| CTLFLAG_MPSAFE, 0, 0, sysctl_kern_proc, "S,proc", "Return entire process table"); static SYSCTL_NODE(_kern_proc, KERN_PROC_GID, gid, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); static SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); static SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); static SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); static SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); static SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); static SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); static SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); static SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Return process table, no threads"); static SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args, CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_args, "Process argument list"); static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc_pathname, "Process executable path"); static SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc_sv_name, "Process syscall vector name (ABI type)"); static SYSCTL_NODE(_kern_proc, (KERN_PROC_GID | KERN_PROC_INC_THREAD), gid_td, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); static SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); static SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); static SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD), sid_td, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); static SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); static SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); static SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); static SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); static SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Return process table, no threads"); #ifdef COMPAT_FREEBSD7 static SYSCTL_NODE(_kern_proc, KERN_PROC_OVMMAP, ovmmap, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc_ovmmap, "Old Process vm map entries"); #endif static SYSCTL_NODE(_kern_proc, KERN_PROC_VMMAP, vmmap, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc_vmmap, "Process vm map entries"); #if defined(STACK) || defined(DDB) static SYSCTL_NODE(_kern_proc, KERN_PROC_KSTACK, kstack, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc_kstack, "Process kernel stacks"); #endif static SYSCTL_NODE(_kern_proc, KERN_PROC_GROUPS, groups, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc_groups, "Process groups"); Index: head/sys/sys/user.h =================================================================== --- head/sys/sys/user.h (revision 221806) +++ head/sys/sys/user.h (revision 221807) @@ -1,418 +1,483 @@ /*- * Copyright (c) 1982, 1986, 1989, 1991, 1993 * The Regents of the University of California. * Copyright (c) 2007 Robert N. M. Watson * 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. * 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. * * @(#)user.h 8.2 (Berkeley) 9/23/93 * $FreeBSD$ */ #ifndef _SYS_USER_H_ #define _SYS_USER_H_ #include #ifndef _KERNEL /* stuff that *used* to be included by user.h, or is now needed */ #include #include #include #include #include #include #include #include #include #include /* XXX */ #include /* XXX */ #include /* XXX */ #include /* XXX */ #endif /* !_KERNEL */ #ifndef _SYS_RESOURCEVAR_H_ #include #endif #ifndef _SYS_SIGNALVAR_H_ #include #endif #ifndef _SYS_SOCKET_VAR_H_ #include #endif /* * KERN_PROC subtype ops return arrays of selected proc structure entries: * * This struct includes several arrays of spare space, with different arrays * for different standard C-types. When adding new variables to this struct, * the space for byte-aligned data should be taken from the ki_sparestring, * pointers from ki_spareptrs, word-aligned data from ki_spareints, and * doubleword-aligned data from ki_sparelongs. Make sure the space for new * variables come from the array which matches the size and alignment of * those variables on ALL hardware platforms, and then adjust the appropriate * KI_NSPARE_* value(s) to match. * * Always verify that sizeof(struct kinfo_proc) == KINFO_PROC_SIZE on all * platforms after you have added new variables. Note that if you change * the value of KINFO_PROC_SIZE, then many userland programs will stop * working until they are recompiled! * * Once you have added the new field, you will need to add code to initialize * it in two places: function fill_kinfo_proc in sys/kern/kern_proc.c and * function kvm_proclist in lib/libkvm/kvm_proc.c . */ #define KI_NSPARE_INT 9 #define KI_NSPARE_LONG 12 #define KI_NSPARE_PTR 6 #ifndef _KERNEL #ifndef KINFO_PROC_SIZE #error "Unknown architecture" #endif #endif /* !_KERNEL */ #define WMESGLEN 8 /* size of returned wchan message */ #define LOCKNAMELEN 8 /* size of returned lock name */ #define OCOMMLEN 16 /* size of returned thread name */ #define COMMLEN 19 /* size of returned ki_comm name */ #define KI_EMULNAMELEN 16 /* size of returned ki_emul */ #define KI_NGROUPS 16 /* number of groups in ki_groups */ #define LOGNAMELEN 17 /* size of returned ki_login */ #define LOGINCLASSLEN 17 /* size of returned ki_loginclass */ /* Flags for the process credential. */ #define KI_CRF_CAPABILITY_MODE 0x00000001 /* * Steal a bit from ki_cr_flags to indicate that the cred had more than * KI_NGROUPS groups. */ #define KI_CRF_GRP_OVERFLOW 0x80000000 struct kinfo_proc { int ki_structsize; /* size of this structure */ int ki_layout; /* reserved: layout identifier */ struct pargs *ki_args; /* address of command arguments */ struct proc *ki_paddr; /* address of proc */ struct user *ki_addr; /* kernel virtual addr of u-area */ struct vnode *ki_tracep; /* pointer to trace file */ struct vnode *ki_textvp; /* pointer to executable file */ struct filedesc *ki_fd; /* pointer to open file info */ struct vmspace *ki_vmspace; /* pointer to kernel vmspace struct */ void *ki_wchan; /* sleep address */ pid_t ki_pid; /* Process identifier */ pid_t ki_ppid; /* parent process id */ pid_t ki_pgid; /* process group id */ pid_t ki_tpgid; /* tty process group id */ pid_t ki_sid; /* Process session ID */ pid_t ki_tsid; /* Terminal session ID */ short ki_jobc; /* job control counter */ short ki_spare_short1; /* unused (just here for alignment) */ dev_t ki_tdev; /* controlling tty dev */ sigset_t ki_siglist; /* Signals arrived but not delivered */ sigset_t ki_sigmask; /* Current signal mask */ sigset_t ki_sigignore; /* Signals being ignored */ sigset_t ki_sigcatch; /* Signals being caught by user */ uid_t ki_uid; /* effective user id */ uid_t ki_ruid; /* Real user id */ uid_t ki_svuid; /* Saved effective user id */ gid_t ki_rgid; /* Real group id */ gid_t ki_svgid; /* Saved effective group id */ short ki_ngroups; /* number of groups */ short ki_spare_short2; /* unused (just here for alignment) */ gid_t ki_groups[KI_NGROUPS]; /* groups */ vm_size_t ki_size; /* virtual size */ segsz_t ki_rssize; /* current resident set size in pages */ segsz_t ki_swrss; /* resident set size before last swap */ segsz_t ki_tsize; /* text size (pages) XXX */ segsz_t ki_dsize; /* data size (pages) XXX */ segsz_t ki_ssize; /* stack size (pages) */ u_short ki_xstat; /* Exit status for wait & stop signal */ u_short ki_acflag; /* Accounting flags */ fixpt_t ki_pctcpu; /* %cpu for process during ki_swtime */ u_int ki_estcpu; /* Time averaged value of ki_cpticks */ u_int ki_slptime; /* Time since last blocked */ u_int ki_swtime; /* Time swapped in or out */ int ki_spareint1; /* unused (just here for alignment) */ u_int64_t ki_runtime; /* Real time in microsec */ struct timeval ki_start; /* starting time */ struct timeval ki_childtime; /* time used by process children */ long ki_flag; /* P_* flags */ long ki_kiflag; /* KI_* flags (below) */ int ki_traceflag; /* Kernel trace points */ char ki_stat; /* S* process status */ signed char ki_nice; /* Process "nice" value */ char ki_lock; /* Process lock (prevent swap) count */ char ki_rqindex; /* Run queue index */ u_char ki_oncpu; /* Which cpu we are on */ u_char ki_lastcpu; /* Last cpu we were on */ char ki_ocomm[OCOMMLEN+1]; /* thread name */ char ki_wmesg[WMESGLEN+1]; /* wchan message */ char ki_login[LOGNAMELEN+1]; /* setlogin name */ char ki_lockname[LOCKNAMELEN+1]; /* lock name */ char ki_comm[COMMLEN+1]; /* command name */ char ki_emul[KI_EMULNAMELEN+1]; /* emulation name */ char ki_loginclass[LOGINCLASSLEN+1]; /* login class */ /* * When adding new variables, take space for char-strings from the * front of ki_sparestrings, and ints from the end of ki_spareints. * That way the spare room from both arrays will remain contiguous. */ char ki_sparestrings[50]; /* spare string space */ int ki_spareints[KI_NSPARE_INT]; /* spare room for growth */ u_int ki_cr_flags; /* Credential flags */ int ki_jid; /* Process jail ID */ int ki_numthreads; /* XXXKSE number of threads in total */ lwpid_t ki_tid; /* XXXKSE thread id */ struct priority ki_pri; /* process priority */ struct rusage ki_rusage; /* process rusage statistics */ /* XXX - most fields in ki_rusage_ch are not (yet) filled in */ struct rusage ki_rusage_ch; /* rusage of children processes */ struct pcb *ki_pcb; /* kernel virtual addr of pcb */ void *ki_kstack; /* kernel virtual addr of stack */ void *ki_udata; /* User convenience pointer */ struct thread *ki_tdaddr; /* address of thread */ /* * When adding new variables, take space for pointers from the * front of ki_spareptrs, and longs from the end of ki_sparelongs. * That way the spare room from both arrays will remain contiguous. */ void *ki_spareptrs[KI_NSPARE_PTR]; /* spare room for growth */ long ki_sparelongs[KI_NSPARE_LONG]; /* spare room for growth */ long ki_sflag; /* PS_* flags */ long ki_tdflags; /* XXXKSE kthread flag */ }; void fill_kinfo_proc(struct proc *, struct kinfo_proc *); /* XXX - the following two defines are temporary */ #define ki_childstime ki_rusage_ch.ru_stime #define ki_childutime ki_rusage_ch.ru_utime /* * Legacy PS_ flag. This moved to p_flag but is maintained for * compatibility. */ #define PS_INMEM 0x00001 /* Loaded into memory. */ /* ki_sessflag values */ #define KI_CTTY 0x00000001 /* controlling tty vnode active */ #define KI_SLEADER 0x00000002 /* session leader */ #define KI_LOCKBLOCK 0x00000004 /* proc blocked on lock ki_lockname */ /* * This used to be the per-process structure containing data that * isn't needed in core when the process is swapped out, but now it * remains only for the benefit of a.out core dumps. */ struct user { struct pstats u_stats; /* *p_stats */ struct kinfo_proc u_kproc; /* eproc */ }; /* * The KERN_PROC_FILE sysctl allows a process to dump the file descriptor * array of another process. */ +#define KF_ATTR_VALID 0x0001 + #define KF_TYPE_NONE 0 #define KF_TYPE_VNODE 1 #define KF_TYPE_SOCKET 2 #define KF_TYPE_PIPE 3 #define KF_TYPE_FIFO 4 #define KF_TYPE_KQUEUE 5 #define KF_TYPE_CRYPTO 6 #define KF_TYPE_MQUEUE 7 #define KF_TYPE_SHM 8 #define KF_TYPE_SEM 9 #define KF_TYPE_PTS 10 #define KF_TYPE_UNKNOWN 255 #define KF_VTYPE_VNON 0 #define KF_VTYPE_VREG 1 #define KF_VTYPE_VDIR 2 #define KF_VTYPE_VBLK 3 #define KF_VTYPE_VCHR 4 #define KF_VTYPE_VLNK 5 #define KF_VTYPE_VSOCK 6 #define KF_VTYPE_VFIFO 7 #define KF_VTYPE_VBAD 8 #define KF_VTYPE_UNKNOWN 255 #define KF_FD_TYPE_CWD -1 /* Current working directory */ #define KF_FD_TYPE_ROOT -2 /* Root directory */ #define KF_FD_TYPE_JAIL -3 /* Jail directory */ +#define KF_FD_TYPE_TRACE -4 /* ptrace vnode */ +#define KF_FD_TYPE_TEXT -5 /* Text vnode */ +#define KF_FD_TYPE_CTTY -6 /* Controlling terminal */ #define KF_FLAG_READ 0x00000001 #define KF_FLAG_WRITE 0x00000002 #define KF_FLAG_APPEND 0x00000004 #define KF_FLAG_ASYNC 0x00000008 #define KF_FLAG_FSYNC 0x00000010 #define KF_FLAG_NONBLOCK 0x00000020 #define KF_FLAG_DIRECT 0x00000040 #define KF_FLAG_HASLOCK 0x00000080 +#define KF_FLAG_SHLOCK 0x00000100 +#define KF_FLAG_EXLOCK 0x00000200 +#define KF_FLAG_NOFOLLOW 0x00000400 +#define KF_FLAG_CREAT 0x00000800 +#define KF_FLAG_TRUNC 0x00001000 +#define KF_FLAG_EXCL 0x00002000 +#define KF_FLAG_EXEC 0x00004000 /* * Old format. Has variable hidden padding due to alignment. * This is a compatability hack for pre-build 7.1 packages. */ #if defined(__amd64__) #define KINFO_OFILE_SIZE 1328 #endif #if defined(__i386__) #define KINFO_OFILE_SIZE 1324 #endif struct kinfo_ofile { int kf_structsize; /* Size of kinfo_file. */ int kf_type; /* Descriptor type. */ int kf_fd; /* Array index. */ int kf_ref_count; /* Reference count. */ int kf_flags; /* Flags. */ /* XXX Hidden alignment padding here on amd64 */ off_t kf_offset; /* Seek location. */ int kf_vnode_type; /* Vnode type. */ int kf_sock_domain; /* Socket domain. */ int kf_sock_type; /* Socket type. */ int kf_sock_protocol; /* Socket protocol. */ char kf_path[PATH_MAX]; /* Path to file, if any. */ struct sockaddr_storage kf_sa_local; /* Socket address. */ struct sockaddr_storage kf_sa_peer; /* Peer address. */ }; #if defined(__amd64__) || defined(__i386__) #define KINFO_FILE_SIZE 1392 #endif struct kinfo_file { - int kf_structsize; /* Variable size of record. */ - int kf_type; /* Descriptor type. */ - int kf_fd; /* Array index. */ - int kf_ref_count; /* Reference count. */ - int kf_flags; /* Flags. */ - int _kf_pad0; /* Round to 64 bit alignment */ - int64_t kf_offset; /* Seek location. */ - int kf_vnode_type; /* Vnode type. */ - int kf_sock_domain; /* Socket domain. */ - int kf_sock_type; /* Socket type. */ - int kf_sock_protocol; /* Socket protocol. */ + int kf_structsize; /* Variable size of record. */ + int kf_type; /* Descriptor type. */ + int kf_fd; /* Array index. */ + int kf_ref_count; /* Reference count. */ + int kf_flags; /* Flags. */ + int kf_pad0; /* Round to 64 bit alignment. */ + int64_t kf_offset; /* Seek location. */ + int kf_vnode_type; /* Vnode type. */ + int kf_sock_domain; /* Socket domain. */ + int kf_sock_type; /* Socket type. */ + int kf_sock_protocol; /* Socket protocol. */ struct sockaddr_storage kf_sa_local; /* Socket address. */ struct sockaddr_storage kf_sa_peer; /* Peer address. */ - int _kf_ispare[16]; /* Space for more stuff. */ + union { + struct { + /* Address of so_pcb. */ + uint64_t kf_sock_pcb; + /* Address of inp_ppcb. */ + uint64_t kf_sock_inpcb; + /* Address of unp_conn. */ + uint64_t kf_sock_unpconn; + /* Send buffer state. */ + uint16_t kf_sock_snd_sb_state; + /* Receive buffer state. */ + uint16_t kf_sock_rcv_sb_state; + /* Round to 64 bit alignment. */ + uint32_t kf_sock_pad0; + } kf_sock; + struct { + /* Global file id. */ + uint64_t kf_file_fileid; + /* File size. */ + uint64_t kf_file_size; + /* Vnode filesystem id. */ + uint32_t kf_file_fsid; + /* File device. */ + uint32_t kf_file_rdev; + /* File mode. */ + uint16_t kf_file_mode; + /* Round to 64 bit alignment. */ + uint16_t kf_file_pad0; + uint32_t kf_file_pad1; + } kf_file; + struct { + uint64_t kf_pipe_addr; + uint64_t kf_pipe_peer; + uint32_t kf_pipe_buffer_cnt; + /* Round to 64 bit alignment. */ + uint32_t kf_pipe_pad0[3]; + } kf_pipe; + struct { + uint32_t kf_pts_dev; + /* Round to 64 bit alignment. */ + uint32_t kf_pts_pad0[7]; + } kf_pts; + } kf_un; + uint16_t kf_status; /* Status flags. */ + uint16_t kf_pad1; /* Round to 32 bit alignment. */ + int _kf_ispare[7]; /* Space for more stuff. */ /* Truncated before copyout in sysctl */ - char kf_path[PATH_MAX]; /* Path to file, if any. */ + char kf_path[PATH_MAX]; /* Path to file, if any. */ }; /* * The KERN_PROC_VMMAP sysctl allows a process to dump the VM layout of * another process as a series of entries. */ #define KVME_TYPE_NONE 0 #define KVME_TYPE_DEFAULT 1 #define KVME_TYPE_VNODE 2 #define KVME_TYPE_SWAP 3 #define KVME_TYPE_DEVICE 4 #define KVME_TYPE_PHYS 5 #define KVME_TYPE_DEAD 6 #define KVME_TYPE_SG 7 #define KVME_TYPE_UNKNOWN 255 #define KVME_PROT_READ 0x00000001 #define KVME_PROT_WRITE 0x00000002 #define KVME_PROT_EXEC 0x00000004 #define KVME_FLAG_COW 0x00000001 #define KVME_FLAG_NEEDS_COPY 0x00000002 #define KVME_FLAG_NOCOREDUMP 0x00000004 #if defined(__amd64__) #define KINFO_OVMENTRY_SIZE 1168 #endif #if defined(__i386__) #define KINFO_OVMENTRY_SIZE 1128 #endif struct kinfo_ovmentry { int kve_structsize; /* Size of kinfo_vmmapentry. */ int kve_type; /* Type of map entry. */ void *kve_start; /* Starting address. */ void *kve_end; /* Finishing address. */ int kve_flags; /* Flags on map entry. */ int kve_resident; /* Number of resident pages. */ int kve_private_resident; /* Number of private pages. */ int kve_protection; /* Protection bitmask. */ int kve_ref_count; /* VM obj ref count. */ int kve_shadow_count; /* VM obj shadow count. */ char kve_path[PATH_MAX]; /* Path to VM obj, if any. */ void *_kve_pspare[8]; /* Space for more stuff. */ off_t kve_offset; /* Mapping offset in object */ uint64_t kve_fileid; /* inode number if vnode */ dev_t kve_fsid; /* dev_t of vnode location */ int _kve_ispare[3]; /* Space for more stuff. */ }; #if defined(__amd64__) || defined(__i386__) #define KINFO_VMENTRY_SIZE 1160 #endif struct kinfo_vmentry { int kve_structsize; /* Variable size of record. */ int kve_type; /* Type of map entry. */ uint64_t kve_start; /* Starting address. */ uint64_t kve_end; /* Finishing address. */ uint64_t kve_offset; /* Mapping offset in object */ - uint64_t kve_fileid; /* inode number if vnode */ - uint32_t kve_fsid; /* dev_t of vnode location */ + uint64_t kve_vn_fileid; /* inode number if vnode */ + uint32_t kve_vn_fsid; /* dev_t of vnode location */ int kve_flags; /* Flags on map entry. */ int kve_resident; /* Number of resident pages. */ int kve_private_resident; /* Number of private pages. */ int kve_protection; /* Protection bitmask. */ int kve_ref_count; /* VM obj ref count. */ int kve_shadow_count; /* VM obj shadow count. */ - int _kve_pad0; /* 64bit align next field */ - int _kve_ispare[16]; /* Space for more stuff. */ + int kve_vn_type; /* Vnode type. */ + uint64_t kve_vn_size; /* File size. */ + uint32_t kve_vn_rdev; /* Device id if device. */ + uint16_t kve_vn_mode; /* File mode. */ + uint16_t kve_status; /* Status flags. */ + int _kve_ispare[12]; /* Space for more stuff. */ /* Truncated before copyout in sysctl */ char kve_path[PATH_MAX]; /* Path to VM obj, if any. */ }; /* * The KERN_PROC_KSTACK sysctl allows a process to dump the kernel stacks of * another process as a series of entries. Each stack is represented by a * series of symbol names and offsets as generated by stack_sbuf_print(9). */ #define KKST_MAXLEN 1024 #define KKST_STATE_STACKOK 0 /* Stack is valid. */ #define KKST_STATE_SWAPPED 1 /* Stack swapped out. */ #define KKST_STATE_RUNNING 2 /* Stack ephemeral. */ #if defined(__amd64__) || defined(__i386__) #define KINFO_KSTACK_SIZE 1096 #endif struct kinfo_kstack { lwpid_t kkst_tid; /* ID of thread. */ int kkst_state; /* Validity of stack. */ char kkst_trace[KKST_MAXLEN]; /* String representing stack. */ int _kkst_ispare[16]; /* Space for more stuff. */ }; + +#ifdef _KERNEL +int vntype_to_kinfo(int vtype); +#endif /* !_KERNEL */ #endif Index: head/usr.bin/fstat/zfs/Makefile =================================================================== --- head/usr.bin/fstat/zfs/Makefile (revision 221806) +++ head/usr.bin/fstat/zfs/Makefile (nonexistent) @@ -1,23 +0,0 @@ -# $FreeBSD$ - -.PATH: ${.CURDIR}/.. - -SRCS= zfs.c -OBJS= zfs.o -WARNS?= 1 - -CFLAGS+= -I${.CURDIR}/../../../sys/cddl/compat/opensolaris -CFLAGS+= -I${.CURDIR}/../../../cddl/compat/opensolaris/include -CFLAGS+= -I${.CURDIR}/../../../cddl/compat/opensolaris/lib/libumem -CFLAGS+= -I${.CURDIR}/../../../cddl/contrib/opensolaris/lib/libzpool/common -CFLAGS+= -I${.CURDIR}/../../../sys/cddl/contrib/opensolaris/uts/common/fs/zfs -CFLAGS+= -I${.CURDIR}/../../../sys/cddl/contrib/opensolaris/uts/common -CFLAGS+= -I${.CURDIR}/../../../sys/cddl/contrib/opensolaris/uts/common/sys -CFLAGS+= -I${.CURDIR}/../../../cddl/contrib/opensolaris/head -CFLAGS+= -I${.CURDIR}/.. -CFLAGS+= -DNEED_SOLARIS_BOOLEAN - -all: ${OBJS} -CLEANFILES= ${OBJS} - -.include Property changes on: head/usr.bin/fstat/zfs/Makefile ___________________________________________________________________ Deleted: svn:keywords ## -1 +0,0 ## -FreeBSD=%H \ No newline at end of property Index: head/usr.bin/fstat/zfs.c =================================================================== --- head/usr.bin/fstat/zfs.c (revision 221806) +++ head/usr.bin/fstat/zfs.c (nonexistent) @@ -1,136 +0,0 @@ -/*- - * 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. - * - * $FreeBSD$ - */ - -#include -#define _KERNEL -#include -#include -#undef _KERNEL -#include - -#undef lbolt -#undef lbolt64 -#undef gethrestime_sec -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include - -#define ZFS -#undef dprintf -#include - -/* - * Offset calculations that are used to get data from znode without having the - * definition. - */ -#define LOCATION_ZID (2 * sizeof(void *)) -#define LOCATION_ZPHYS(zsize) ((zsize) - (2 * sizeof(void *) + sizeof(struct task))) - -int -zfs_filestat(struct vnode *vp, struct filestat *fsp) -{ - - znode_phys_t zphys; - struct mount mount, *mountptr; - uint64_t *zid; - void *znodeptr, *vnodeptr; - char *dataptr; - void *zphys_addr; - size_t len; - int size; - - len = sizeof(size); - if (sysctlbyname("debug.sizeof.znode", &size, &len, NULL, 0) == -1) { - dprintf(stderr, "error getting sysctl\n"); - return (0); - } - znodeptr = malloc(size); - if (znodeptr == NULL) { - dprintf(stderr, "error allocating memory for znode storage\n"); - return (0); - } - - /* Since we have problems including vnode.h, we'll use the wrappers. */ - vnodeptr = getvnodedata(vp); - if (!KVM_READ(vnodeptr, znodeptr, (size_t)size)) { - dprintf(stderr, "can't read znode at %p for pid %d\n", - (void *)vnodeptr, Pid); - goto bad; - } - - /* - * z_id field is stored in the third pointer. We therefore skip the two - * first bytes. - * - * Pointer to the z_phys structure is the next last pointer. Therefore - * go back two bytes from the end. - */ - dataptr = znodeptr; - zid = (uint64_t *)(dataptr + LOCATION_ZID); - zphys_addr = *(void **)(dataptr + LOCATION_ZPHYS(size)); - - if (!KVM_READ(zphys_addr, &zphys, sizeof(zphys))) { - dprintf(stderr, "can't read znode_phys at %p for pid %d\n", - zphys_addr, Pid); - goto bad; - } - - /* Get the mount pointer, and read from the address. */ - mountptr = getvnodemount(vp); - if (!KVM_READ(mountptr, &mount, sizeof(mount))) { - dprintf(stderr, "can't read mount at %p for pid %d\n", - (void *)mountptr, Pid); - goto bad; - } - - fsp->fsid = (long)(uint32_t)mount.mnt_stat.f_fsid.val[0]; - fsp->fileid = *zid; - /* - * XXX: Shows up wrong in output, but UFS has this error too. Could - * be that we're casting mode-variables from 64-bit to 8-bit or simply - * error in the mode-to-string function. - */ - fsp->mode = (mode_t)zphys.zp_mode; - fsp->size = (u_long)zphys.zp_size; - fsp->rdev = (dev_t)zphys.zp_rdev; - free(znodeptr); - return (1); -bad: - free(znodeptr); - return (0); -} Property changes on: head/usr.bin/fstat/zfs.c ___________________________________________________________________ Deleted: svn:keywords ## -1 +0,0 ## -FreeBSD=%H \ No newline at end of property Index: head/usr.bin/fstat/fstat.h =================================================================== --- head/usr.bin/fstat/fstat.h (revision 221806) +++ head/usr.bin/fstat/fstat.h (nonexistent) @@ -1,76 +0,0 @@ -/*- - * 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. - * 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. - * - * $FreeBSD$ - */ - -#ifndef __FSTAT_H__ -#define __FSTAT_H__ - -/* - * a kvm_read that returns true if everything is read - */ -#define KVM_READ(kaddr, paddr, len) \ - ((len) < SSIZE_MAX && \ - kvm_read(kd, (u_long)(kaddr), (char *)(paddr), (len)) == (ssize_t)(len)) - -#define dprintf if (vflg) fprintf - -typedef struct devs { - struct devs *next; - long fsid; - long ino; - const char *name; -} DEVS; - -struct filestat { - long fsid; - long fileid; - mode_t mode; - u_long size; - dev_t rdev; -}; - -/* Ugh */ -extern kvm_t *kd; -extern int vflg; -extern int Pid; - -dev_t dev2udev(struct cdev *dev); - -/* Additional filesystem types */ -int isofs_filestat(struct vnode *vp, struct filestat *fsp); -int msdosfs_filestat(struct vnode *vp, struct filestat *fsp); - -#ifdef ZFS -int zfs_filestat(struct vnode *vp, struct filestat *fsp); -void *getvnodedata(struct vnode *vp); -struct mount *getvnodemount(struct vnode *vp); -#endif - -#endif /* __FSTAT_H__ */ Property changes on: head/usr.bin/fstat/fstat.h ___________________________________________________________________ Deleted: svn:keywords ## -1 +0,0 ## -FreeBSD=%H \ No newline at end of property Index: head/usr.bin/fstat/cd9660.c =================================================================== --- head/usr.bin/fstat/cd9660.c (revision 221806) +++ head/usr.bin/fstat/cd9660.c (nonexistent) @@ -1,79 +0,0 @@ -/* - * 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 -__FBSDID("$FreeBSD$"); - -#include -#include -#include -#include -#include - -#include - -#include -#include - -#include "fstat.h" - -int -isofs_filestat(struct vnode *vp, struct filestat *fsp) -{ - struct iso_node isonode; - - if (!KVM_READ(VTOI(vp), &isonode, sizeof (isonode))) { - dprintf(stderr, "can't read iso_node at %p for pid %d\n", - (void *)VTOI(vp), Pid); - return 0; - } -#if 0 - fsp->fsid = dev2udev(isonode.i_dev); -#endif - fsp->mode = (mode_t)isonode.inode.iso_mode; - fsp->rdev = isonode.inode.iso_rdev; - - fsp->fileid = (long)isonode.i_number; - fsp->size = (u_long)isonode.i_size; - return 1; -} - Property changes on: head/usr.bin/fstat/cd9660.c ___________________________________________________________________ Deleted: svn:keywords ## -1 +0,0 ## -FreeBSD=%H \ No newline at end of property Index: head/usr.bin/fstat/msdosfs.c =================================================================== --- head/usr.bin/fstat/msdosfs.c (revision 221806) +++ head/usr.bin/fstat/msdosfs.c (nonexistent) @@ -1,150 +0,0 @@ -/* - * 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 -__FBSDID("$FreeBSD$"); - -#include -#include -#include -#include - -#define _KERNEL -#include -#include -#include -#undef _KERNEL - -#include -#include -#include - -#include -#include -#include -#include - -/* - * XXX - - * VTODE is defined in denode.h only if _KERNEL is defined, but that leads to - * header explosion - */ -#define VTODE(vp) ((struct denode *)(vp)->v_data) - -#include "fstat.h" - -struct dosmount { - struct dosmount *next; - struct msdosfsmount *kptr; /* Pointer in kernel space */ - struct msdosfsmount data; /* User space copy of structure */ -}; - -int -msdosfs_filestat(struct vnode *vp, struct filestat *fsp) -{ - struct denode denode; - static struct dosmount *mounts; - struct dosmount *mnt; - u_long dirsperblk; - int fileid; - - if (!KVM_READ(VTODE(vp), &denode, sizeof (denode))) { - dprintf(stderr, "can't read denode at %p for pid %d\n", - (void *)VTODE(vp), Pid); - return 0; - } - - /* - * 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) - err(1, NULL); - if (!KVM_READ(denode.de_pmp, &mnt->data, sizeof mnt->data)) { - free(mnt); - dprintf(stderr, - "can't read mount info at %p for pid %d\n", - (void *)denode.de_pmp, Pid); - return 0; - } - mnt->next = mounts; - mounts = mnt; - mnt->kptr = denode.de_pmp; - } - - fsp->fsid = dev2udev(mnt->data.pm_dev); - fsp->mode = 0555; - fsp->mode |= denode.de_Attributes & ATTR_READONLY ? 0 : 0222; - fsp->mode &= mnt->data.pm_mask; - - /* Distinguish directories and files. No "special" files in FAT. */ - fsp->mode |= denode.de_Attributes & ATTR_DIRECTORY ? S_IFDIR : S_IFREG; - - fsp->size = denode.de_FileSize; - fsp->rdev = 0; - - /* - * 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); - } - - fsp->fileid = fileid; - return 1; -} Property changes on: head/usr.bin/fstat/msdosfs.c ___________________________________________________________________ Deleted: svn:keywords ## -1 +0,0 ## -FreeBSD=%H \ No newline at end of property Index: head/usr.bin/fstat/Makefile =================================================================== --- head/usr.bin/fstat/Makefile (revision 221806) +++ head/usr.bin/fstat/Makefile (revision 221807) @@ -1,24 +1,12 @@ # @(#)Makefile 8.1 (Berkeley) 6/6/93 # $FreeBSD$ -.include - PROG= fstat -SRCS= cd9660.c fstat.c msdosfs.c +SRCS= fstat.c fuser.c main.c +LINKS= ${BINDIR}/fstat ${BINDIR}/fuser DPADD= ${LIBKVM} -LDADD= -lkvm -BINGRP= kmem -BINMODE=2555 +LDADD= -lkvm -lutil -lprocstat -CFLAGS+=-D_KVM_VNODE - -# XXX This is a hack. -.if ${MK_CDDL} != "no" -CFLAGS+= -DZFS -OBJS+= zfs/zfs.o -SUBDIR= zfs -zfs/zfs.o: .PHONY - @cd ${.CURDIR}/zfs && ${MAKE} zfs.o -.endif +MAN1= fuser.1 fstat.1 .include Index: head/usr.bin/fstat/fstat.c =================================================================== --- head/usr.bin/fstat/fstat.c (revision 221806) +++ head/usr.bin/fstat/fstat.c (revision 221807) @@ -1,1014 +1,508 @@ /*- + * Copyright (c) 2009 Stanislav Sedov * 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. * 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. */ -#ifndef lint -static const char copyright[] = -"@(#) Copyright (c) 1988, 1993\n\ - The Regents of the University of California. All rights reserved.\n"; -#endif /* not lint */ - -#ifndef lint -#if 0 -static char sccsid[] = "@(#)fstat.c 8.3 (Berkeley) 5/2/95"; -#endif -#endif /* not lint */ #include __FBSDID("$FreeBSD$"); #include -#include -#include #include #include -#include #include #include -#include -#include -#include -#include #include -#include -#include #include -#define _WANT_FILE -#include -#include -#define _KERNEL -#include -#include -#include -#include -#include -#include -#undef _KERNEL -#include -#include -#include - -#include -#include -#include - -#include #include -#include -#include -#include +#include #include #include -#include -#include +#include #include -#include -#include #include #include #include #include #include #include #include -#include "fstat.h" +#include "functions.h" -#define TEXT -1 -#define CDIR -2 -#define RDIR -3 -#define TRACE -4 -#define MMAP -5 -#define JDIR -6 - -DEVS *devs; - -#ifdef notdef -struct nlist nl[] = { - { "" }, -}; -#endif - int fsflg, /* show files on same filesystem as file(s) argument */ pflg, /* show files open by a particular pid */ uflg; /* show files open by a particular (effective) user */ int checkfile; /* true if restricting to particular files or filesystems */ int nflg; /* (numerical) display f.s. and rdev as dev_t */ -int vflg; /* display errors in locating kernel data objects etc... */ int mflg; /* include memory-mapped files */ +int vflg; /* be verbose */ +typedef struct devs { + struct devs *next; + uint32_t fsid; + uint64_t ino; + const char *name; +} DEVS; -struct file **ofiles; /* buffer of pointers to file structures */ -int maxfiles; -#define ALLOC_OFILES(d) \ - if ((d) > maxfiles) { \ - free(ofiles); \ - ofiles = malloc((d) * sizeof(struct file *)); \ - if (ofiles == NULL) { \ - err(1, NULL); \ - } \ - maxfiles = (d); \ - } - +DEVS *devs; char *memf, *nlistf; -kvm_t *kd; -static void fstat_kvm(int, int); -static void fstat_sysctl(int, int); -void dofiles(struct kinfo_proc *kp); -void dommap(struct kinfo_proc *kp); -void vtrans(struct vnode *vp, int i, int flag); -int ufs_filestat(struct vnode *vp, struct filestat *fsp); -int nfs_filestat(struct vnode *vp, struct filestat *fsp); -int devfs_filestat(struct vnode *vp, struct filestat *fsp); -char *getmnton(struct mount *m); -void pipetrans(struct pipe *pi, int i, int flag); -void socktrans(struct socket *sock, int i); -void ptstrans(struct tty *tp, int i, int flag); -void getinetproto(int number); -int getfname(const char *filename); -void usage(void); -char *kdevtoname(struct cdev *dev); +static int getfname(const char *filename); +static void dofiles(struct procstat *procstat, struct kinfo_proc *p); +static void print_access_flags(int flags); +static void print_file_info(struct procstat *procstat, + struct filestat *fst, const char *uname, const char *cmd, int pid); +static void print_pipe_info(struct procstat *procstat, + struct filestat *fst); +static void print_pts_info(struct procstat *procstat, + struct filestat *fst); +static void print_socket_info(struct procstat *procstat, + struct filestat *fst); +static void print_vnode_info(struct procstat *procstat, + struct filestat *fst); +static void usage(void) __dead2; int -main(int argc, char **argv) +do_fstat(int argc, char **argv) { + struct kinfo_proc *p; struct passwd *passwd; + struct procstat *procstat; int arg, ch, what; + int cnt, i; arg = 0; what = KERN_PROC_PROC; nlistf = memf = NULL; while ((ch = getopt(argc, argv, "fmnp:u:vN:M:")) != -1) switch((char)ch) { case 'f': fsflg = 1; break; case 'M': memf = optarg; break; case 'N': nlistf = optarg; break; case 'm': mflg = 1; break; case 'n': nflg = 1; break; case 'p': if (pflg++) usage(); if (!isdigit(*optarg)) { warnx("-p requires a process id"); usage(); } what = KERN_PROC_PID; arg = atoi(optarg); break; case 'u': if (uflg++) usage(); if (!(passwd = getpwnam(optarg))) errx(1, "%s: unknown uid", optarg); what = KERN_PROC_UID; arg = passwd->pw_uid; break; case 'v': vflg = 1; break; case '?': default: usage(); } if (*(argv += optind)) { for (; *argv; ++argv) { if (getfname(*argv)) checkfile = 1; } if (!checkfile) /* file(s) specified, but none accessable */ exit(1); } if (fsflg && !checkfile) { /* -f with no files means use wd */ if (getfname(".") == 0) exit(1); checkfile = 1; } if (memf != NULL) - fstat_kvm(what, arg); + procstat = procstat_open_kvm(nlistf, memf); else - fstat_sysctl(what, arg); - exit(0); -} + procstat = procstat_open_sysctl(); + if (procstat == NULL) + errx(1, "procstat_open()"); + p = procstat_getprocs(procstat, what, arg, &cnt); + if (p == NULL) + errx(1, "procstat_getprocs()"); -static void -print_header(void) -{ - + /* + * Print header. + */ if (nflg) printf("%s", "USER CMD PID FD DEV INUM MODE SZ|DV R/W"); else printf("%s", "USER CMD PID FD MOUNT INUM MODE SZ|DV R/W"); if (checkfile && fsflg == 0) printf(" NAME\n"); else putchar('\n'); -} -static void -fstat_kvm(int what, int arg) -{ - struct kinfo_proc *p, *plast; - char buf[_POSIX2_LINE_MAX]; - int cnt; - - ALLOC_OFILES(256); /* reserve space for file pointers */ - /* - * Discard setgid privileges if not the running kernel so that bad - * guys can't print interesting stuff from kernel memory. + * Go through the process list. */ - if (nlistf != NULL || memf != NULL) - setgid(getgid()); - - if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL) - errx(1, "%s", buf); - setgid(getgid()); -#ifdef notdef - if (kvm_nlist(kd, nl) != 0) - errx(1, "no namelist: %s", kvm_geterr(kd)); -#endif - if ((p = kvm_getprocs(kd, what, arg, &cnt)) == NULL) - errx(1, "%s", kvm_geterr(kd)); - print_header(); - for (plast = &p[cnt]; p < plast; ++p) { - if (p->ki_stat == SZOMB) + for (i = 0; i < cnt; i++) { + if (p[i].ki_stat == SZOMB) continue; - dofiles(p); - if (mflg) - dommap(p); + dofiles(procstat, &p[i]); } + procstat_freeprocs(procstat, p); + procstat_close(procstat); + return (0); } static void -fstat_sysctl(int what, int arg) +dofiles(struct procstat *procstat, struct kinfo_proc *kp) { + const char *cmd; + const char *uname; + struct filestat *fst; + struct filestat_list *head; + int pid; - /* not yet implemented */ - fstat_kvm(what, arg); -} + uname = user_from_uid(kp->ki_uid, 0); + pid = kp->ki_pid; + cmd = kp->ki_comm; -const char *Uname, *Comm; -int Pid; - -#define PREFIX(i) printf("%-8.8s %-10s %5d", Uname, Comm, Pid); \ - switch(i) { \ - case TEXT: \ - printf(" text"); \ - break; \ - case CDIR: \ - printf(" wd"); \ - break; \ - case RDIR: \ - printf(" root"); \ - break; \ - case TRACE: \ - printf(" tr"); \ - break; \ - case MMAP: \ - printf(" mmap"); \ - break; \ - case JDIR: \ - printf(" jail"); \ - break; \ - default: \ - printf(" %4d", i); \ - break; \ - } - -/* - * print open files attributed to this process - */ -void -dofiles(struct kinfo_proc *kp) -{ - int i; - struct file file; - struct filedesc filed; - - Uname = user_from_uid(kp->ki_uid, 0); - Pid = kp->ki_pid; - Comm = kp->ki_comm; - - if (kp->ki_fd == NULL) + head = procstat_getfiles(procstat, kp, mflg); + if (head == NULL) return; - if (!KVM_READ(kp->ki_fd, &filed, sizeof (filed))) { - dprintf(stderr, "can't read filedesc at %p for pid %d\n", - (void *)kp->ki_fd, Pid); - return; - } - /* - * root directory vnode, if one - */ - if (filed.fd_rdir) - vtrans(filed.fd_rdir, RDIR, FREAD); - /* - * current working directory vnode - */ - if (filed.fd_cdir) - vtrans(filed.fd_cdir, CDIR, FREAD); - /* - * jail root, if any. - */ - if (filed.fd_jdir) - vtrans(filed.fd_jdir, JDIR, FREAD); - /* - * ktrace vnode, if one - */ - if (kp->ki_tracep) - vtrans(kp->ki_tracep, TRACE, FREAD|FWRITE); - /* - * text vnode, if one - */ - if (kp->ki_textvp) - vtrans(kp->ki_textvp, TEXT, FREAD); - /* - * open files - */ -#define FPSIZE (sizeof (struct file *)) -#define MAX_LASTFILE (0x1000000) - - /* Sanity check on filed.fd_lastfile */ - if (filed.fd_lastfile <= -1 || filed.fd_lastfile > MAX_LASTFILE) - return; - - ALLOC_OFILES(filed.fd_lastfile+1); - if (!KVM_READ(filed.fd_ofiles, ofiles, - (filed.fd_lastfile+1) * FPSIZE)) { - dprintf(stderr, - "can't read file structures at %p for pid %d\n", - (void *)filed.fd_ofiles, Pid); - return; - } - for (i = 0; i <= filed.fd_lastfile; i++) { - if (ofiles[i] == NULL) - continue; - if (!KVM_READ(ofiles[i], &file, sizeof (struct file))) { - dprintf(stderr, "can't read file %d at %p for pid %d\n", - i, (void *)ofiles[i], Pid); - continue; - } - if (file.f_type == DTYPE_VNODE) - vtrans(file.f_vnode, i, file.f_flag); - else if (file.f_type == DTYPE_SOCKET) { - if (checkfile == 0) - socktrans(file.f_data, i); - } -#ifdef DTYPE_PIPE - else if (file.f_type == DTYPE_PIPE) { - if (checkfile == 0) - pipetrans(file.f_data, i, file.f_flag); - } -#endif -#ifdef DTYPE_FIFO - else if (file.f_type == DTYPE_FIFO) { - if (checkfile == 0) - vtrans(file.f_vnode, i, file.f_flag); - } -#endif -#ifdef DTYPE_PTS - else if (file.f_type == DTYPE_PTS) { - if (checkfile == 0) - ptstrans(file.f_data, i, file.f_flag); - } -#endif - else { - dprintf(stderr, - "unknown file type %d for file %d of pid %d\n", - file.f_type, i, Pid); - } - } + STAILQ_FOREACH(fst, head, next) + print_file_info(procstat, fst, uname, cmd, pid); + procstat_freefiles(procstat, head); } -void -dommap(struct kinfo_proc *kp) -{ - vm_map_t map; - struct vmspace vmspace; - struct vm_map_entry entry; - vm_map_entry_t entryp; - struct vm_object object; - vm_object_t objp; - int prot, fflags; - if (!KVM_READ(kp->ki_vmspace, &vmspace, sizeof(vmspace))) { - dprintf(stderr, - "can't read vmspace at %p for pid %d\n", - (void *)kp->ki_vmspace, Pid); - return; - } - map = &vmspace.vm_map; - - for (entryp = map->header.next; - entryp != &kp->ki_vmspace->vm_map.header; entryp = entry.next) { - if (!KVM_READ(entryp, &entry, sizeof(entry))) { - dprintf(stderr, - "can't read vm_map_entry at %p for pid %d\n", - (void *)entryp, Pid); - return; - } - - if (entry.eflags & MAP_ENTRY_IS_SUB_MAP) - continue; - - if ((objp = entry.object.vm_object) == NULL) - continue; - - for (; objp; objp = object.backing_object) { - if (!KVM_READ(objp, &object, sizeof(object))) { - dprintf(stderr, - "can't read vm_object at %p for pid %d\n", - (void *)objp, Pid); - return; - } - } - - prot = entry.protection; - fflags = (prot & VM_PROT_READ ? FREAD : 0) | - (prot & VM_PROT_WRITE ? FWRITE : 0); - - switch (object.type) { - case OBJT_VNODE: - vtrans((struct vnode *)object.handle, MMAP, fflags); - break; - default: - break; - } - } -} - -char * -kdevtoname(struct cdev *dev) +static void +print_file_info(struct procstat *procstat, struct filestat *fst, + const char *uname, const char *cmd, int pid) { - struct cdev si; + struct vnstat vn; + DEVS *d; + const char *filename; + int error, fsmatch = 0; + char errbuf[_POSIX2_LINE_MAX]; - if (!KVM_READ(dev, &si, sizeof si)) - return (NULL); - return (strdup(si.__si_namebuf)); -} - -void -vtrans(struct vnode *vp, int i, int flag) -{ - struct vnode vn; - struct filestat fst; - char rw[3], mode[15], tagstr[12], *tagptr; - const char *badtype, *filename; - - filename = badtype = NULL; - if (!KVM_READ(vp, &vn, sizeof (struct vnode))) { - dprintf(stderr, "can't read vnode at %p for pid %d\n", - (void *)vp, Pid); - return; - } - if (!KVM_READ(&vp->v_tag, &tagptr, sizeof tagptr) || - !KVM_READ(tagptr, tagstr, sizeof tagstr)) { - dprintf(stderr, "can't read v_tag at %p for pid %d\n", - (void *)vp, Pid); - return; - } - tagstr[sizeof(tagstr) - 1] = '\0'; - if (vn.v_type == VNON) - badtype = "none"; - else if (vn.v_type == VBAD) - badtype = "bad"; - else { - if (!strcmp("ufs", tagstr)) { - if (!ufs_filestat(&vn, &fst)) - badtype = "error"; - } else if (!strcmp("devfs", tagstr)) { - if (!devfs_filestat(&vn, &fst)) - badtype = "error"; - } else if (!strcmp("nfs", tagstr)) { - if (!nfs_filestat(&vn, &fst)) - badtype = "error"; - } else if (!strcmp("msdosfs", tagstr)) { - if (!msdosfs_filestat(&vn, &fst)) - badtype = "error"; - } else if (!strcmp("isofs", tagstr)) { - if (!isofs_filestat(&vn, &fst)) - badtype = "error"; -#ifdef ZFS - } else if (!strcmp("zfs", tagstr)) { - if (!zfs_filestat(&vn, &fst)) - badtype = "error"; -#endif - } else { - static char unknown[32]; - snprintf(unknown, sizeof unknown, "?(%s)", tagstr); - badtype = unknown; - } - } - if (checkfile) { - int fsmatch = 0; - DEVS *d; - - if (badtype) + filename = NULL; + if (checkfile != 0) { + if (fst->fs_type != PS_FST_TYPE_VNODE && + fst->fs_type != PS_FST_TYPE_FIFO) return; + error = procstat_get_vnode_info(procstat, fst, &vn, errbuf); + if (error != 0) + return; + for (d = devs; d != NULL; d = d->next) - if (d->fsid == fst.fsid) { + if (d->fsid == vn.vn_fsid) { fsmatch = 1; - if (d->ino == fst.fileid) { + if ((unsigned)d->ino == vn.vn_fileid) { filename = d->name; break; } } if (fsmatch == 0 || (filename == NULL && fsflg == 0)) return; } - PREFIX(i); - if (badtype) { - (void)printf(" - - %10s -\n", badtype); - return; - } - if (nflg) - (void)printf(" %2d,%-2d", major(fst.fsid), minor(fst.fsid)); - else - (void)printf(" %-8s", getmnton(vn.v_mount)); - if (nflg) - (void)sprintf(mode, "%o", fst.mode); - else - strmode(fst.mode, mode); - (void)printf(" %6ld %10s", fst.fileid, mode); - switch (vn.v_type) { - case VBLK: - case VCHR: { - char *name; - name = kdevtoname(vn.v_rdev); - if (nflg || !name) - printf(" %2d,%-2d", major(fst.rdev), minor(fst.rdev)); - else { - printf(" %6s", name); - free(name); - } - break; - } - default: - printf(" %6lu", fst.size); - } - rw[0] = '\0'; - if (flag & FREAD) - strcat(rw, "r"); - if (flag & FWRITE) - strcat(rw, "w"); - printf(" %2s", rw); - if (filename && !fsflg) - printf(" %s", filename); - putchar('\n'); -} - -int -ufs_filestat(struct vnode *vp, struct filestat *fsp) -{ - struct inode inode; - - if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) { - dprintf(stderr, "can't read inode at %p for pid %d\n", - (void *)VTOI(vp), Pid); - return 0; - } /* - * 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 + * Print entry prefix. */ - fsp->fsid = dev2udev(inode.i_dev); - fsp->fileid = (long)inode.i_number; - fsp->mode = (mode_t)inode.i_mode; - fsp->size = (u_long)inode.i_size; -#if should_be_but_is_hard - /* XXX - need to load i_ump and i_din[12] from kernel memory */ - if (inode.i_ump->um_fstype == UFS1) - fsp->rdev = inode.i_din1->di_rdev; + printf("%-8.8s %-10s %5d", uname, cmd, pid); + if (fst->fs_uflags & PS_FST_UFLAG_TEXT) + printf(" text"); + else if (fst->fs_uflags & PS_FST_UFLAG_CDIR) + printf(" wd"); + else if (fst->fs_uflags & PS_FST_UFLAG_RDIR) + printf(" root"); + else if (fst->fs_uflags & PS_FST_UFLAG_TRACE) + printf(" tr"); + else if (fst->fs_uflags & PS_FST_UFLAG_MMAP) + printf(" mmap"); + else if (fst->fs_uflags & PS_FST_UFLAG_JAIL) + printf(" jail"); + else if (fst->fs_uflags & PS_FST_UFLAG_CTTY) + printf(" ctty"); else - fsp->rdev = inode.i_din2->di_rdev; -#else - fsp->rdev = 0; -#endif + printf(" %4d", fst->fs_fd); - return 1; -} - -int -devfs_filestat(struct vnode *vp, struct filestat *fsp) -{ - struct devfs_dirent devfs_dirent; - struct mount mount; - struct vnode vnode; - - if (!KVM_READ(vp->v_data, &devfs_dirent, sizeof (devfs_dirent))) { - dprintf(stderr, "can't read devfs_dirent at %p for pid %d\n", - (void *)vp->v_data, Pid); - return 0; - } - if (!KVM_READ(vp->v_mount, &mount, sizeof (mount))) { - dprintf(stderr, "can't read mount at %p for pid %d\n", - (void *)vp->v_mount, Pid); - return 0; - } - if (!KVM_READ(devfs_dirent.de_vnode, &vnode, sizeof (vnode))) { - dprintf(stderr, "can't read vnode at %p for pid %d\n", - (void *)devfs_dirent.de_vnode, Pid); - return 0; - } - fsp->fsid = (long)(uint32_t)mount.mnt_stat.f_fsid.val[0]; - fsp->fileid = devfs_dirent.de_inode; - fsp->mode = (devfs_dirent.de_mode & ~S_IFMT) | S_IFCHR; - fsp->size = 0; - fsp->rdev = dev2udev(vnode.v_rdev); - - return 1; -} - -int -nfs_filestat(struct vnode *vp, struct filestat *fsp) -{ - struct nfsnode nfsnode; - mode_t mode; - - if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) { - dprintf(stderr, "can't read nfsnode at %p for pid %d\n", - (void *)VTONFS(vp), Pid); - return 0; - } - fsp->fsid = nfsnode.n_vattr.va_fsid; - fsp->fileid = nfsnode.n_vattr.va_fileid; - fsp->size = nfsnode.n_size; - fsp->rdev = nfsnode.n_vattr.va_rdev; - mode = (mode_t)nfsnode.n_vattr.va_mode; - switch (vp->v_type) { - case VREG: - mode |= S_IFREG; + /* + * Print type-specific data. + */ + switch (fst->fs_type) { + case PS_FST_TYPE_FIFO: + case PS_FST_TYPE_VNODE: + print_vnode_info(procstat, fst); break; - case VDIR: - mode |= S_IFDIR; + case PS_FST_TYPE_SOCKET: + print_socket_info(procstat, fst); break; - case VBLK: - mode |= S_IFBLK; + case PS_FST_TYPE_PIPE: + print_pipe_info(procstat, fst); break; - case VCHR: - mode |= S_IFCHR; + case PS_FST_TYPE_PTS: + print_pts_info(procstat, fst); break; - case VLNK: - mode |= S_IFLNK; - break; - case VSOCK: - mode |= S_IFSOCK; - break; - case VFIFO: - mode |= S_IFIFO; - break; - case VNON: - case VBAD: - case VMARKER: - return 0; - }; - fsp->mode = mode; - - return 1; -} - - -char * -getmnton(struct mount *m) -{ - static struct mount mount; - static struct mtab { - struct mtab *next; - struct mount *m; - char mntonname[MNAMELEN]; - } *mhead = NULL; - struct mtab *mt; - - for (mt = mhead; mt != NULL; mt = mt->next) - if (m == mt->m) - return (mt->mntonname); - if (!KVM_READ(m, &mount, sizeof(struct mount))) { - warnx("can't read mount table at %p", (void *)m); - return (NULL); + default: + if (vflg) + fprintf(stderr, + "unknown file type %d for file %d of pid %d\n", + fst->fs_type, fst->fs_fd, pid); } - if ((mt = malloc(sizeof (struct mtab))) == NULL) - err(1, NULL); - mt->m = m; - bcopy(&mount.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN); - mt->next = mhead; - mhead = mt; - return (mt->mntonname); -} - -void -pipetrans(struct pipe *pi, int i, int flag) -{ - struct pipe pip; - char rw[3]; - - PREFIX(i); - - /* fill in socket */ - if (!KVM_READ(pi, &pip, sizeof(struct pipe))) { - dprintf(stderr, "can't read pipe at %p\n", (void *)pi); - goto bad; - } - - printf("* pipe %8lx <-> %8lx", (u_long)pi, (u_long)pip.pipe_peer); - printf(" %6d", (int)pip.pipe_buffer.cnt); - rw[0] = '\0'; - if (flag & FREAD) - strcat(rw, "r"); - if (flag & FWRITE) - strcat(rw, "w"); - printf(" %2s", rw); + if (filename && !fsflg) + printf(" %s", filename); putchar('\n'); - return; - -bad: - printf("* error\n"); } -void -socktrans(struct socket *sock, int i) +static void +print_socket_info(struct procstat *procstat, struct filestat *fst) { static const char *stypename[] = { "unused", /* 0 */ - "stream", /* 1 */ + "stream", /* 1 */ "dgram", /* 2 */ "raw", /* 3 */ "rdm", /* 4 */ "seqpak" /* 5 */ }; -#define STYPEMAX 5 - struct socket so; - struct protosw proto; - struct domain dom; - struct inpcb inpcb; - struct unpcb unpcb; - int len; - char dname[32]; +#define STYPEMAX 5 + struct sockstat sock; + struct protoent *pe; + char errbuf[_POSIX2_LINE_MAX]; + int error; + static int isopen; - PREFIX(i); - - /* fill in socket */ - if (!KVM_READ(sock, &so, sizeof(struct socket))) { - dprintf(stderr, "can't read sock at %p\n", (void *)sock); - goto bad; + error = procstat_get_socket_info(procstat, fst, &sock, errbuf); + if (error != 0) { + printf("* error"); + return; } - - /* fill in protosw entry */ - if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) { - dprintf(stderr, "can't read protosw at %p", - (void *)so.so_proto); - goto bad; - } - - /* fill in domain */ - if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) { - dprintf(stderr, "can't read domain at %p\n", - (void *)proto.pr_domain); - goto bad; - } - - if ((len = kvm_read(kd, (u_long)dom.dom_name, dname, - sizeof(dname) - 1)) < 0) { - dprintf(stderr, "can't read domain name at %p\n", - (void *)dom.dom_name); - dname[0] = '\0'; - } + if (sock.type > STYPEMAX) + printf("* %s ?%d", sock.dname, sock.type); else - dname[len] = '\0'; + printf("* %s %s", sock.dname, stypename[sock.type]); - if ((u_short)so.so_type > STYPEMAX) - printf("* %s ?%d", dname, so.so_type); - else - printf("* %s %s", dname, stypename[so.so_type]); - /* * protocol specific formatting * * Try to find interesting things to print. For tcp, the interesting * thing is the address of the tcpcb, for udp and others, just the * inpcb (socket pcb). For unix domain, its the address of the socket * pcb and the address of the connected pcb (if connected). Otherwise * just print the protocol number and address of the socket itself. * The idea is not to duplicate netstat, but to make available enough * information for further analysis. */ - switch(dom.dom_family) { + switch (sock.dom_family) { case AF_INET: case AF_INET6: - getinetproto(proto.pr_protocol); - if (proto.pr_protocol == IPPROTO_TCP ) { - if (so.so_pcb) { - if (kvm_read(kd, (u_long)so.so_pcb, - (char *)&inpcb, sizeof(struct inpcb)) - != sizeof(struct inpcb)) { - dprintf(stderr, - "can't read inpcb at %p\n", - (void *)so.so_pcb); - goto bad; - } - printf(" %lx", (u_long)inpcb.inp_ppcb); - } + if (!isopen) + setprotoent(++isopen); + if ((pe = getprotobynumber(sock.proto)) != NULL) + printf(" %s", pe->p_name); + else + printf(" %d", sock.proto); + if (sock.proto == IPPROTO_TCP ) { + if (sock.inp_ppcb != 0) + printf(" %lx", (u_long)sock.inp_ppcb); } - else if (so.so_pcb) - printf(" %lx", (u_long)so.so_pcb); + else if (sock.so_pcb != 0) + printf(" %lx", (u_long)sock.so_pcb); break; case AF_UNIX: /* print address of pcb and connected pcb */ - if (so.so_pcb) { - printf(" %lx", (u_long)so.so_pcb); - if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb, - sizeof(struct unpcb)) != sizeof(struct unpcb)){ - dprintf(stderr, "can't read unpcb at %p\n", - (void *)so.so_pcb); - goto bad; - } - if (unpcb.unp_conn) { + if (sock.so_pcb != 0) { + printf(" %lx", (u_long)sock.so_pcb); + if (sock.unp_conn) { char shoconn[4], *cp; cp = shoconn; - if (!(so.so_rcv.sb_state & SBS_CANTRCVMORE)) + if (!(sock.so_rcv_sb_state & SBS_CANTRCVMORE)) *cp++ = '<'; *cp++ = '-'; - if (!(so.so_snd.sb_state & SBS_CANTSENDMORE)) + if (!(sock.so_snd_sb_state & SBS_CANTSENDMORE)) *cp++ = '>'; *cp = '\0'; printf(" %s %lx", shoconn, - (u_long)unpcb.unp_conn); - } + (u_long)sock.unp_conn); + } } break; default: /* print protocol number and socket address */ - printf(" %d %lx", proto.pr_protocol, (u_long)sock); + printf(" %d %lx", sock.proto, (u_long)sock.so_addr); } - printf("\n"); - return; -bad: - printf("* error\n"); } -void -ptstrans(struct tty *tp, int i, int flag) +static void +print_pipe_info(struct procstat *procstat, struct filestat *fst) { - struct tty tty; - char *name; - char rw[3]; - dev_t rdev; + struct pipestat ps; + char errbuf[_POSIX2_LINE_MAX]; + int error; - PREFIX(i); - - /* Obtain struct tty. */ - if (!KVM_READ(tp, &tty, sizeof(struct tty))) { - dprintf(stderr, "can't read tty at %p\n", (void *)tp); - goto bad; + error = procstat_get_pipe_info(procstat, fst, &ps, errbuf); + if (error != 0) { + printf("* error"); + return; } + printf("* pipe %8lx <-> %8lx", (u_long)ps.addr, (u_long)ps.peer); + printf(" %6zd", ps.buffer_cnt); + print_access_flags(fst->fs_fflags); +} - /* Figure out the device name. */ - name = kdevtoname(tty.t_dev); - if (name == NULL) { - dprintf(stderr, "can't determine tty name at %p\n", (void *)tp); - goto bad; - } +static void +print_pts_info(struct procstat *procstat, struct filestat *fst) +{ + struct ptsstat pts; + char errbuf[_POSIX2_LINE_MAX]; + int error; - rw[0] = '\0'; - if (flag & FREAD) - strcat(rw, "r"); - if (flag & FWRITE) - strcat(rw, "w"); - + error = procstat_get_pts_info(procstat, fst, &pts, errbuf); + if (error != 0) { + printf("* error"); + return; + } printf("* pseudo-terminal master "); - if (nflg || !name) { - rdev = dev2udev(tty.t_dev); - printf("%10d,%-2d", major(rdev), minor(rdev)); + if (nflg || !*pts.devname) { + printf("%10d,%-2d", major(pts.dev), minor(pts.dev)); } else { - printf("%10s", name); + printf("%10s", pts.devname); } - printf(" %2s\n", rw); - - free(name); - - return; -bad: - printf("* error\n"); + print_access_flags(fst->fs_fflags); } -/* - * Read the cdev structure in the kernel in order to work out the - * associated dev_t - */ -dev_t -dev2udev(struct cdev *dev) +static void +print_vnode_info(struct procstat *procstat, struct filestat *fst) { - struct cdev_priv priv; + struct vnstat vn; + char errbuf[_POSIX2_LINE_MAX]; + char mode[15]; + const char *badtype; + int error; - if (KVM_READ(cdev2priv(dev), &priv, sizeof priv)) { - return ((dev_t)priv.cdp_inode); - } else { - dprintf(stderr, "can't convert cdev *%p to a dev_t\n", dev); - return -1; + badtype = NULL; + error = procstat_get_vnode_info(procstat, fst, &vn, errbuf); + if (error != 0) + badtype = errbuf; + else if (vn.vn_type == PS_FST_VTYPE_VBAD) + badtype = "bad"; + else if (vn.vn_type == PS_FST_VTYPE_VNON) + badtype = "none"; + if (badtype != NULL) { + printf(" - - %10s -", badtype); + return; } + + if (nflg) + printf(" %2d,%-2d", major(vn.vn_fsid), minor(vn.vn_fsid)); + else if (vn.vn_mntdir != NULL) + (void)printf(" %-8s", vn.vn_mntdir); + + /* + * Print access mode. + */ + if (nflg) + (void)snprintf(mode, sizeof(mode), "%o", vn.vn_mode); + else { + strmode(vn.vn_mode, mode); + } + (void)printf(" %6ld %10s", vn.vn_fileid, mode); + + if (vn.vn_type == PS_FST_VTYPE_VBLK || vn.vn_type == PS_FST_VTYPE_VCHR) { + if (nflg || !*vn.vn_devname) + printf(" %2d,%-2d", major(vn.vn_dev), minor(vn.vn_dev)); + else { + printf(" %6s", vn.vn_devname); + } + } else + printf(" %6lu", vn.vn_size); + print_access_flags(fst->fs_fflags); } -/* - * getinetproto -- - * print name of protocol number - */ -void -getinetproto(int number) +static void +print_access_flags(int flags) { - static int isopen; - struct protoent *pe; + char rw[3]; - if (!isopen) - setprotoent(++isopen); - if ((pe = getprotobynumber(number)) != NULL) - printf(" %s", pe->p_name); - else - printf(" %d", number); + rw[0] = '\0'; + if (flags & PS_FST_FFLAG_READ) + strcat(rw, "r"); + if (flags & PS_FST_FFLAG_WRITE) + strcat(rw, "w"); + printf(" %2s", rw); } int getfname(const char *filename) { struct stat statbuf; DEVS *cur; if (stat(filename, &statbuf)) { warn("%s", filename); - return(0); + return (0); } if ((cur = malloc(sizeof(DEVS))) == NULL) err(1, NULL); cur->next = devs; devs = cur; cur->ino = statbuf.st_ino; cur->fsid = statbuf.st_dev; cur->name = filename; - return(1); + return (1); } -#ifdef ZFS -void * -getvnodedata(struct vnode *vp) -{ - return (vp->v_data); -} - -struct mount * -getvnodemount(struct vnode *vp) -{ - return (vp->v_mount); -} -#endif - -void +static void usage(void) { (void)fprintf(stderr, "usage: fstat [-fmnv] [-M core] [-N system] [-p pid] [-u user] [file ...]\n"); exit(1); } Index: head/usr.bin/fstat/functions.h =================================================================== --- head/usr.bin/fstat/functions.h (nonexistent) +++ head/usr.bin/fstat/functions.h (revision 221807) @@ -0,0 +1,34 @@ +/*- + * Copyright (c) 2009 Stanislav Sedov + * 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. + * + * $FreeBSD$ + */ +#ifndef __FUNCTIONS_H__ +#define __FUNCTIONS_H__ + +int do_fstat(int argc, char *argv[]); +int do_fuser(int argc, char *argv[]); + +#endif /* !__FUNCTIONS_H__ */ Property changes on: head/usr.bin/fstat/functions.h ___________________________________________________________________ Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Index: head/usr.bin/fstat/fuser.1 =================================================================== --- head/usr.bin/fstat/fuser.1 (nonexistent) +++ head/usr.bin/fstat/fuser.1 (revision 221807) @@ -0,0 +1,148 @@ +.\" Copyright (c) 2005-2009 Stanislav Sedov +.\" 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. +.\" +.\" $FreeBSD$ +.\" +.Dd July 31, 2009 +.Dt FUSER 1 +.Os +.Sh NAME +.Nm fuser +.Nd list IDs of all processes that have one or more files open +.Sh SYNOPSIS +.Nm +.Op Fl cfkmu +.Op Fl M Ar core +.Op Fl N Ar system +.Op Fl s Ar signal +.Op Ar +.Sh DESCRIPTION +The +.Nm +utility writes to stdout the PIDs of processes that have one or +more named files open. +For block and character special devices, all processes using files +on that device are listed. +A file is considered open by a process if it was explicitly opened, +is the working directory, root directory, jail root directory, +active executable text, kernel trace file or the controlling terminal +of the process. +If +.Fl m +option is specified, the +.Nm +utility will also look through mmapped files. +.Pp +The following options are available: +.Bl -tag -width indent +.It Fl c +Treat files as mount point and report on any files open in the file system. +.It Fl f +The report must be only for named files. +.It Fl k +Send signal to reported processes +.Pq SIGKILL by default . +.It Fl m +Search through mmapped files too. +.It Fl u +Write the user name associated with each process to stdout. +.It Fl M +Extract values associated with the name list from the specified core +instead of the default +.Pa /dev/kmem . +.It Fl N +Extract the name list from the specified system instead of the default, +which is the kernel image the system has booted from. +.It Fl s +Use given signal name instead of default SIGKILL. +.El +.Pp +The following symbols, written to stderr will indicate how files is used: +.Bl -tag -width MOUNT +.It Cm r +The file is the root directory of the process. +.It Cm c +The file is the current workdir directory of the process. +.It Cm j +The file is the jail-root of the process. +.It Cm t +The file is the kernel tracing file for the process. +.It Cm x +The file is executable text of the process. +.It Cm y +The process use this file as its controlling tty. +.It Cm m +The file is mmapped. +.It Cm w +The file is open for writing. +.It Cm a +The file is open as append only +.Pq O_APPEND was specified . +.It Cm d +The process bypasses fs cache while writing to this file +.Pq O_DIRECT was specified . +.It Cm s +Shared lock is hold. +.It Cm e +Exclusive lock is hold. +.El +.Sh EXIT STATUS +The +.Nm +utility returns 0 on successful completion and >0 otherwise. +.Sh EXAMPLES +The command: +.Dq Li "fuser -fu ." +writes to standart output the process IDs of processes that are using the +current directory and writes to stderr an indication of how those processes are +using the direcory and user names associated with the processes that are using +this directory. +.Sh SEE ALSO +.Xr fstat 1 , +.Xr ps 1 , +.Xr systat 1 , +.Xr iostat 8 , +.Xr pstat 8 , +.Xr vmstat 8 +.Sh STANDARTS +The +.Nm +utility is expected to conform to +.St -p1003.1-2004 . +.Sh BUGS +Since +.Nm +takes a snapshot of the system, it is only correct for a very short period +of time. +When working via +.Xr kvm 3 +interface the report will be limited to filesystems the +.Nm +utility knows about (currently only cd9660, devfs, nfs, ntfs, nwfs, udf, +ufs and zfs). +.Sh AUTHORS +The +.Nm +utility and this manual page was written by +.An Stanislav Sedov Aq stas@FreeBSD.org . Property changes on: head/usr.bin/fstat/fuser.1 ___________________________________________________________________ Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Index: head/usr.bin/fstat/fuser.c =================================================================== --- head/usr.bin/fstat/fuser.c (nonexistent) +++ head/usr.bin/fstat/fuser.c (revision 221807) @@ -0,0 +1,369 @@ +/*- + * Copyright (c) 2005-2009 Stanislav Sedov + * 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 +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "functions.h" + +/* + * File access mode flags table. + */ +struct { + int flag; + char ch; +} fflags[] = { + {PS_FST_FFLAG_WRITE, 'w'}, + {PS_FST_FFLAG_APPEND, 'a'}, + {PS_FST_FFLAG_DIRECT, 'd'}, + {PS_FST_FFLAG_SHLOCK, 's'}, + {PS_FST_FFLAG_EXLOCK, 'e'} +}; +#define NFFLAGS (sizeof(fflags) / sizeof(*fflags)) + +/* + * Usage flags translation table. + */ +struct { + int flag; + char ch; +} uflags[] = { + {PS_FST_UFLAG_RDIR, 'r'}, + {PS_FST_UFLAG_CDIR, 'c'}, + {PS_FST_UFLAG_JAIL, 'j'}, + {PS_FST_UFLAG_TRACE, 't'}, + {PS_FST_UFLAG_TEXT, 'x'}, + {PS_FST_UFLAG_MMAP, 'm'}, + {PS_FST_UFLAG_CTTY, 'y'} +}; +#define NUFLAGS (sizeof(uflags) / sizeof(*uflags)) + +struct consumer { + pid_t pid; + uid_t uid; + int fd; + int flags; + int uflags; + STAILQ_ENTRY(consumer) next; +}; +struct reqfile { + uint32_t fsid; + uint64_t fileid; + const char *name; + STAILQ_HEAD(, consumer) consumers; +}; + +/* + * Option flags. + */ +#define UFLAG 0x01 /* -u flag: show users */ +#define FFLAG 0x02 /* -f flag: specified files only */ +#define CFLAG 0x04 /* -c flag: treat as mpoints */ +#define MFLAG 0x10 /* -m flag: mmapped files too */ +#define KFLAG 0x20 /* -k flag: send signal (SIGKILL by default) */ + +static int flags = 0; /* Option flags. */ + +static void printflags(struct consumer *consumer); +static int str2sig(const char *str); +static void usage(void) __dead2; +static int addfile(const char *path, struct reqfile *reqfile); +static void dofiles(struct procstat *procstat, struct kinfo_proc *kp, + struct reqfile *reqfiles, size_t nfiles); + +static void +usage(void) +{ + + fprintf(stderr, +"usage: fuser [-cfhkmu] [-M core] [-N system] [-s signal] file ...\n"); + exit(EX_USAGE); +} + +static void +printflags(struct consumer *cons) +{ + unsigned int i; + + assert(cons); + for (i = 0; i < NUFLAGS; i++) + if ((cons->uflags & uflags[i].flag) != 0) + fputc(uflags[i].ch, stderr); + for (i = 0; i < NFFLAGS; i++) + if ((cons->flags & fflags[i].flag) != 0) + fputc(fflags[i].ch, stderr); +} + +/* + * Add file to the list. + */ +static int +addfile(const char *path, struct reqfile *reqfile) +{ + struct stat sb; + + assert(path); + if (stat(path, &sb) != 0) { + warn("%s", path); + return (1); + } + reqfile->fileid = sb.st_ino; + reqfile->fsid = sb.st_dev; + reqfile->name = path; + STAILQ_INIT(&reqfile->consumers); + return (0); +} + +int +do_fuser(int argc, char *argv[]) +{ + struct consumer *consumer; + struct kinfo_proc *p, *procs; + struct procstat *procstat; + struct reqfile *reqfiles; + char *ep, *nlistf, *memf; + int ch, cnt, sig; + unsigned int i, nfiles; + + sig = SIGKILL; /* Default to kill. */ + nlistf = NULL; + memf = NULL; + while ((ch = getopt(argc, argv, "M:N:cfhkms:u")) != -1) + switch(ch) { + case 'f': + if ((flags & CFLAG) != 0) + usage(); + flags |= FFLAG; + break; + case 'c': + if ((flags & FFLAG) != 0) + usage(); + flags |= CFLAG; + break; + case 'N': + nlistf = optarg; + break; + case 'M': + memf = optarg; + break; + case 'u': + flags |= UFLAG; + break; + case 'm': + flags |= MFLAG; + break; + case 'k': + flags |= KFLAG; + break; + case 's': + if (isdigit(*optarg)) { + sig = strtol(optarg, &ep, 10); + if (*ep != '\0' || sig < 0 || sig >= sys_nsig) + errx(EX_USAGE, "illegal signal number" ": %s", + optarg); + } else { + sig = str2sig(optarg); + if (sig < 0) + errx(EX_USAGE, "illegal signal name: " + "%s", optarg); + } + break; + case 'h': + /* PASSTHROUGH */ + default: + usage(); + /* NORETURN */ + } + argv += optind; + argc -= optind; + + assert(argc >= 0); + if (argc == 0) + usage(); + /* NORETURN */ + + /* + * Process named files. + */ + reqfiles = malloc(argc * sizeof(struct reqfile)); + if (reqfiles == NULL) + err(EX_OSERR, "malloc()"); + nfiles = 0; + while (argc--) + if (!addfile(*(argv++), &reqfiles[nfiles])) + nfiles++; + if (nfiles == 0) + errx(EX_IOERR, "files not accessible"); + + if (memf != NULL) + procstat = procstat_open_kvm(nlistf, memf); + else + procstat = procstat_open_sysctl(); + if (procstat == NULL) + errx(1, "procstat_open()"); + procs = procstat_getprocs(procstat, KERN_PROC_PROC, 0, &cnt); + if (procs == NULL) + errx(1, "procstat_getprocs()"); + + /* + * Walk through process table and look for matching files. + */ + p = procs; + while(cnt--) + if (p->ki_stat != SZOMB) + dofiles(procstat, p++, reqfiles, nfiles); + + for (i = 0; i < nfiles; i++) { + fprintf(stderr, "%s:", reqfiles[i].name); + fflush(stderr); + STAILQ_FOREACH(consumer, &reqfiles[i].consumers, next) { + if (consumer->flags != 0) { + fprintf(stdout, "%6d", consumer->pid); + fflush(stdout); + printflags(consumer); + if ((flags & UFLAG) != 0) + fprintf(stderr, "(%s)", + user_from_uid(consumer->uid, 0)); + if ((flags & KFLAG) != 0) + kill(consumer->pid, sig); + fflush(stderr); + } + } + (void)fprintf(stderr, "\n"); + } + procstat_freeprocs(procstat, procs); + procstat_close(procstat); + free(reqfiles); + return (0); +} + +static void +dofiles(struct procstat *procstat, struct kinfo_proc *kp, + struct reqfile *reqfiles, size_t nfiles) +{ + struct vnstat vn; + struct consumer *cons; + struct filestat *fst; + struct filestat_list *head; + int error, match; + unsigned int i; + char errbuf[_POSIX2_LINE_MAX]; + + head = procstat_getfiles(procstat, kp, flags & MFLAG); + if (head == NULL) + return; + STAILQ_FOREACH(fst, head, next) { + if (fst->fs_type != PS_FST_TYPE_VNODE) + continue; + error = procstat_get_vnode_info(procstat, fst, &vn, errbuf); + if (error != 0) + continue; + for (i = 0; i < nfiles; i++) { + if (flags & CFLAG && reqfiles[i].fsid == vn.vn_fsid) { + break; + } + else if (reqfiles[i].fsid == vn.vn_fsid && + reqfiles[i].fileid == vn.vn_fileid) { + break; + } + else if (!(flags & FFLAG) && + (vn.vn_type == PS_FST_VTYPE_VCHR || + vn.vn_type == PS_FST_VTYPE_VBLK) && + vn.vn_fsid == reqfiles[i].fileid) { + break; + } + } + if (i == nfiles) + continue; /* No match. */ + + /* + * Look for existing entries. + */ + match = 0; + STAILQ_FOREACH(cons, &reqfiles[i].consumers, next) + if (cons->pid == kp->ki_pid) { + match = 1; + break; + } + if (match == 1) { /* Use old entry. */ + cons->flags |= fst->fs_fflags; + cons->uflags |= fst->fs_uflags; + } else { + /* + * Create new entry in the consumer chain. + */ + cons = calloc(1, sizeof(struct consumer)); + if (cons == NULL) { + warn("malloc()"); + continue; + } + cons->uid = kp->ki_uid; + cons->pid = kp->ki_pid; + cons->uflags = fst->fs_uflags; + cons->flags = fst->fs_fflags; + STAILQ_INSERT_TAIL(&reqfiles[i].consumers, cons, next); + } + } + procstat_freefiles(procstat, head); +} + +/* + * Returns signal number for it's string representation. + */ +static int +str2sig(const char *str) +{ + int i; + +#define SIGPREFIX "sig" + if (!strncasecmp(str, SIGPREFIX, sizeof(SIGPREFIX))) + str += sizeof(SIGPREFIX); + for (i = 1; i < sys_nsig; i++) { + if (!strcasecmp(sys_signame[i], str)) + return (i); + } + return (-1); +} Property changes on: head/usr.bin/fstat/fuser.c ___________________________________________________________________ Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Index: head/usr.bin/fstat/main.c =================================================================== --- head/usr.bin/fstat/main.c (nonexistent) +++ head/usr.bin/fstat/main.c (revision 221807) @@ -0,0 +1,49 @@ +/*- + * Copyright (c) 2009 Stanislav Sedov . + * 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 ``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 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 +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include + +#include "functions.h" + +int +main(int argc, char *argv[]) +{ + char *p; + + p = basename(argv[0]); + if (p == NULL) + err(1, "basename(%s)", argv[0]); + if (!strcmp(p, "fuser")) + return (do_fuser(argc, argv)); + else + return (do_fstat(argc, argv)); +} Property changes on: head/usr.bin/fstat/main.c ___________________________________________________________________ Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Index: head/usr.bin/procstat/Makefile =================================================================== --- head/usr.bin/procstat/Makefile (revision 221806) +++ head/usr.bin/procstat/Makefile (revision 221807) @@ -1,19 +1,19 @@ # $FreeBSD$ PROG= procstat MAN= procstat.1 SRCS= procstat.c \ procstat_args.c \ procstat_basic.c \ procstat_bin.c \ procstat_cred.c \ procstat_files.c \ procstat_kstack.c \ procstat_sigs.c \ procstat_threads.c \ procstat_vm.c -LDADD+= -lutil +LDADD+= -lutil -lprocstat -lkvm DPADD+= ${LIBUTIL} .include Index: head/usr.bin/procstat/procstat.c =================================================================== --- head/usr.bin/procstat/procstat.c (revision 221806) +++ head/usr.bin/procstat/procstat.c (revision 221807) @@ -1,269 +1,250 @@ /*- * Copyright (c) 2007 Robert N. M. Watson * 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. * * $FreeBSD$ */ #include #include #include #include +#include #include #include #include #include #include "procstat.h" static int aflag, bflag, cflag, fflag, iflag, jflag, kflag, sflag, tflag, vflag; int hflag, nflag; static void usage(void) { - fprintf(stderr, "usage: procstat [-h] [-n] [-w interval] [-b | -c | -f | " - "-i | -j | -k | -s | -t | -v]\n"); + fprintf(stderr, "usage: procstat [-h] [-M core] [-N system] " + "[-w interval] [-b | -c | -f | -i | -j | -k | -s | -t | -v]\n"); fprintf(stderr, " [-a | pid ...]\n"); exit(EX_USAGE); } static void -procstat(pid_t pid, struct kinfo_proc *kipp) +procstat(struct procstat *prstat, struct kinfo_proc *kipp) { if (bflag) - procstat_bin(pid, kipp); + procstat_bin(kipp); else if (cflag) - procstat_args(pid, kipp); + procstat_args(kipp); else if (fflag) - procstat_files(pid, kipp); + procstat_files(prstat, kipp); else if (iflag) - procstat_sigs(pid, kipp); + procstat_sigs(prstat, kipp); else if (jflag) - procstat_threads_sigs(pid, kipp); + procstat_threads_sigs(prstat, kipp); else if (kflag) - procstat_kstack(pid, kipp, kflag); + procstat_kstack(kipp, kflag); else if (sflag) - procstat_cred(pid, kipp); + procstat_cred(kipp); else if (tflag) - procstat_threads(pid, kipp); + procstat_threads(kipp); else if (vflag) - procstat_vm(pid, kipp); + procstat_vm(kipp); else - procstat_basic(pid, kipp); + procstat_basic(kipp); } /* * Sort processes first by pid and then tid. */ static int kinfo_proc_compare(const void *a, const void *b) { int i; i = ((const struct kinfo_proc *)a)->ki_pid - ((const struct kinfo_proc *)b)->ki_pid; if (i != 0) return (i); i = ((const struct kinfo_proc *)a)->ki_tid - ((const struct kinfo_proc *)b)->ki_tid; return (i); } void kinfo_proc_sort(struct kinfo_proc *kipp, int count) { qsort(kipp, count, sizeof(*kipp), kinfo_proc_compare); } int main(int argc, char *argv[]) { - int ch, interval, name[4], tmp; - unsigned int i; - struct kinfo_proc *kipp; - size_t len; + int ch, interval, tmp; + int i; + struct kinfo_proc *p; + struct procstat *prstat; long l; pid_t pid; char *dummy; + char *nlistf, *memf; + int cnt; interval = 0; - while ((ch = getopt(argc, argv, "abcfijknhstvw:")) != -1) { + memf = nlistf = NULL; + while ((ch = getopt(argc, argv, "N:M:abcfijkhstvw:")) != -1) { switch (ch) { + case 'M': + memf = optarg; + break; + case 'N': + nlistf = optarg; + break; case 'a': aflag++; break; case 'b': bflag++; break; case 'c': cflag++; break; case 'f': fflag++; break; case 'i': iflag++; break; case 'j': jflag++; break; case 'k': kflag++; break; case 'n': nflag++; break; case 'h': hflag++; break; case 's': sflag++; break; case 't': tflag++; break; case 'v': vflag++; break; case 'w': l = strtol(optarg, &dummy, 10); if (*dummy != '\0') usage(); if (l < 1 || l > INT_MAX) usage(); interval = l; break; case '?': default: usage(); } } argc -= optind; argv += optind; /* We require that either 0 or 1 mode flags be set. */ tmp = bflag + cflag + fflag + (kflag ? 1 : 0) + sflag + tflag + vflag; if (!(tmp == 0 || tmp == 1)) usage(); /* We allow -k to be specified up to twice, but not more. */ if (kflag > 2) usage(); /* Must specify either the -a flag or a list of pids. */ if (!(aflag == 1 && argc == 0) && !(aflag == 0 && argc > 0)) usage(); + if (memf != NULL) + prstat = procstat_open_kvm(nlistf, memf); + else + prstat = procstat_open_sysctl(); + if (prstat == NULL) + errx(1, "procstat_open()"); do { if (aflag) { - name[0] = CTL_KERN; - name[1] = KERN_PROC; - name[2] = KERN_PROC_PROC; + p = procstat_getprocs(prstat, KERN_PROC_PROC, 0, &cnt); + if (p == NULL) + errx(1, "procstat_getprocs()"); + kinfo_proc_sort(p, cnt); + for (i = 0; i < cnt; i++) { + procstat(prstat, &p[i]); - len = 0; - if (sysctl(name, 3, NULL, &len, NULL, 0) < 0) - err(-1, "sysctl: kern.proc.all"); - - kipp = malloc(len); - if (kipp == NULL) - err(-1, "malloc"); - - if (sysctl(name, 3, kipp, &len, NULL, 0) < 0) { - free(kipp); - err(-1, "sysctl: kern.proc.all"); - } - if (len % sizeof(*kipp) != 0) - err(-1, "kinfo_proc mismatch"); - if (kipp->ki_structsize != sizeof(*kipp)) - err(-1, "kinfo_proc structure mismatch"); - kinfo_proc_sort(kipp, len / sizeof(*kipp)); - for (i = 0; i < len / sizeof(*kipp); i++) { - procstat(kipp[i].ki_pid, &kipp[i]); - /* Suppress header after first process. */ hflag = 1; } - free(kipp); + procstat_freeprocs(prstat, p); } - for (i = 0; i < (unsigned int)argc; i++) { + for (i = 0; i < argc; i++) { l = strtol(argv[i], &dummy, 10); if (*dummy != '\0') usage(); if (l < 0) usage(); pid = l; - name[0] = CTL_KERN; - name[1] = KERN_PROC; - name[2] = KERN_PROC_PID; - name[3] = pid; + p = procstat_getprocs(prstat, KERN_PROC_PID, pid, &cnt); + if (p == NULL) + errx(1, "procstat_getprocs()"); + if (cnt != 0) + procstat(prstat, p); + procstat_freeprocs(prstat, p); - len = 0; - if (sysctl(name, 4, NULL, &len, NULL, 0) < 0) - err(-1, "sysctl: kern.proc.pid: %d", pid); - - kipp = malloc(len); - if (kipp == NULL) - err(-1, "malloc"); - - if (sysctl(name, 4, kipp, &len, NULL, 0) < 0) { - free(kipp); - err(-1, "sysctl: kern.proc.pid: %d", pid); - } - if (len != sizeof(*kipp)) - err(-1, "kinfo_proc mismatch"); - if (kipp->ki_structsize != sizeof(*kipp)) - errx(-1, "kinfo_proc structure mismatch"); - if (kipp->ki_pid != pid) - errx(-1, "kinfo_proc pid mismatch"); - procstat(pid, kipp); - free(kipp); - /* Suppress header after first process. */ hflag = 1; } if (interval) sleep(interval); } while (interval); + procstat_close(prstat); exit(0); } Index: head/usr.bin/procstat/procstat.h =================================================================== --- head/usr.bin/procstat/procstat.h (revision 221806) +++ head/usr.bin/procstat/procstat.h (revision 221807) @@ -1,48 +1,48 @@ /*- * Copyright (c) 2007 Robert N. M. Watson * 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. * * $FreeBSD$ */ #ifndef PROCSTAT_H #define PROCSTAT_H extern int hflag, nflag; struct kinfo_proc; void kinfo_proc_sort(struct kinfo_proc *kipp, int count); -void procstat_args(pid_t pid, struct kinfo_proc *kipp); -void procstat_basic(pid_t pid, struct kinfo_proc *kipp); -void procstat_bin(pid_t pid, struct kinfo_proc *kipp); -void procstat_cred(pid_t pid, struct kinfo_proc *kipp); -void procstat_files(pid_t pid, struct kinfo_proc *kipp); -void procstat_kstack(pid_t pid, struct kinfo_proc *kipp, int kflag); -void procstat_sigs(pid_t pid, struct kinfo_proc *kipp); -void procstat_threads(pid_t pid, struct kinfo_proc *kipp); -void procstat_threads_sigs(pid_t pid, struct kinfo_proc *kipp); -void procstat_vm(pid_t pid, struct kinfo_proc *kipp); +void procstat_args(struct kinfo_proc *kipp); +void procstat_basic(struct kinfo_proc *kipp); +void procstat_bin(struct kinfo_proc *kipp); +void procstat_cred(struct kinfo_proc *kipp); +void procstat_files(struct procstat *prstat, struct kinfo_proc *kipp); +void procstat_kstack(struct kinfo_proc *kipp, int kflag); +void procstat_sigs(struct procstat *prstat, struct kinfo_proc *kipp); +void procstat_threads(struct kinfo_proc *kipp); +void procstat_threads_sigs(struct procstat *prstat, struct kinfo_proc *kipp); +void procstat_vm(struct kinfo_proc *kipp); #endif /* !PROCSTAT_H */ Index: head/usr.bin/procstat/procstat_args.c =================================================================== --- head/usr.bin/procstat/procstat_args.c (revision 221806) +++ head/usr.bin/procstat/procstat_args.c (revision 221807) @@ -1,76 +1,77 @@ /*- * Copyright (c) 2007 Robert N. M. Watson * 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. * * $FreeBSD$ */ #include #include #include #include #include +#include #include #include #include #include #include "procstat.h" static char args[ARG_MAX]; void -procstat_args(pid_t pid, struct kinfo_proc *kipp) +procstat_args(struct kinfo_proc *kipp) { int error, name[4]; size_t len; char *cp; if (!hflag) printf("%5s %-16s %-53s\n", "PID", "COMM", "ARGS"); name[0] = CTL_KERN; name[1] = KERN_PROC; name[2] = KERN_PROC_ARGS; - name[3] = pid; + name[3] = kipp->ki_pid; len = sizeof(args); error = sysctl(name, 4, args, &len, NULL, 0); if (error < 0 && errno != ESRCH) { - warn("sysctl: kern.proc.args: %d", pid); + warn("sysctl: kern.proc.args: %d", kipp->ki_pid); return; } if (error < 0) return; if (len == 0 || strlen(args) == 0) { strcpy(args, "-"); len = strlen(args) + 1; } - printf("%5d ", pid); + printf("%5d ", kipp->ki_pid); printf("%-16s ", kipp->ki_comm); for (cp = args; cp < args + len; cp += strlen(cp) + 1) printf("%s%s", cp != args ? " " : "", cp); printf("\n"); } Index: head/usr.bin/procstat/procstat_basic.c =================================================================== --- head/usr.bin/procstat/procstat_basic.c (revision 221806) +++ head/usr.bin/procstat/procstat_basic.c (revision 221807) @@ -1,64 +1,65 @@ /*- * Copyright (c) 2007 Robert N. M. Watson * 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. * * $FreeBSD$ */ #include #include #include #include +#include #include #include #include "procstat.h" void -procstat_basic(pid_t pid __unused, struct kinfo_proc *kipp) +procstat_basic(struct kinfo_proc *kipp) { if (!hflag) printf("%5s %5s %5s %5s %5s %3s %-8s %-9s %-13s %-12s\n", "PID", "PPID", "PGID", "SID", "TSID", "THR", "LOGIN", "WCHAN", "EMUL", "COMM"); printf("%5d ", kipp->ki_pid); printf("%5d ", kipp->ki_ppid); printf("%5d ", kipp->ki_pgid); printf("%5d ", kipp->ki_sid); printf("%5d ", kipp->ki_tsid); printf("%3d ", kipp->ki_numthreads); printf("%-8s ", strlen(kipp->ki_login) ? kipp->ki_login : "-"); if (kipp->ki_kiflag & KI_LOCKBLOCK) { printf("*%-8s ", strlen(kipp->ki_lockname) ? kipp->ki_lockname : "-"); } else { printf("%-9s ", strlen(kipp->ki_wmesg) ? kipp->ki_wmesg : "-"); } printf("%-13s ", strcmp(kipp->ki_emul, "null") ? kipp->ki_emul : "-"); printf("%-12s\n", kipp->ki_comm); } Index: head/usr.bin/procstat/procstat_bin.c =================================================================== --- head/usr.bin/procstat/procstat_bin.c (revision 221806) +++ head/usr.bin/procstat/procstat_bin.c (revision 221807) @@ -1,70 +1,71 @@ /*- * Copyright (c) 2007 Robert N. M. Watson * 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. * * $FreeBSD$ */ #include #include #include #include #include +#include #include #include #include #include "procstat.h" void -procstat_bin(pid_t pid, struct kinfo_proc *kipp) +procstat_bin(struct kinfo_proc *kipp) { char pathname[PATH_MAX]; int error, name[4]; size_t len; if (!hflag) printf("%5s %-16s %-53s\n", "PID", "COMM", "PATH"); name[0] = CTL_KERN; name[1] = KERN_PROC; name[2] = KERN_PROC_PATHNAME; - name[3] = pid; + name[3] = kipp->ki_pid; len = sizeof(pathname); error = sysctl(name, 4, pathname, &len, NULL, 0); if (error < 0 && errno != ESRCH) { - warn("sysctl: kern.proc.pathname: %d", pid); + warn("sysctl: kern.proc.pathname: %d", kipp->ki_pid); return; } if (error < 0) return; if (len == 0 || strlen(pathname) == 0) strcpy(pathname, "-"); - printf("%5d ", pid); + printf("%5d ", kipp->ki_pid); printf("%-16s ", kipp->ki_comm); printf("%s\n", pathname); } Index: head/usr.bin/procstat/procstat_cred.c =================================================================== --- head/usr.bin/procstat/procstat_cred.c (revision 221806) +++ head/usr.bin/procstat/procstat_cred.c (revision 221807) @@ -1,97 +1,98 @@ /*- * Copyright (c) 2007 Robert N. M. Watson * 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. * * $FreeBSD$ */ #include #include #include #include +#include #include #include #include #include "procstat.h" void -procstat_cred(pid_t pid, struct kinfo_proc *kipp) +procstat_cred(struct kinfo_proc *kipp) { int i; int mib[4]; int ngroups; size_t len; gid_t *groups = NULL; if (!hflag) printf("%5s %-16s %5s %5s %5s %5s %5s %5s %-20s\n", "PID", "COMM", "EUID", "RUID", "SVUID", "EGID", "RGID", "SVGID", "GROUPS"); - printf("%5d ", pid); + printf("%5d ", kipp->ki_pid); printf("%-16s ", kipp->ki_comm); printf("%5d ", kipp->ki_uid); printf("%5d ", kipp->ki_ruid); printf("%5d ", kipp->ki_svuid); printf("%5d ", kipp->ki_groups[0]); printf("%5d ", kipp->ki_rgid); printf("%5d ", kipp->ki_svgid); /* * We may have too many groups to fit in kinfo_proc's statically * sized storage. If that occurs, attempt to retrieve them via * sysctl. */ if (kipp->ki_cr_flags & KI_CRF_GRP_OVERFLOW) { mib[0] = CTL_KERN; mib[1] = KERN_PROC; mib[2] = KERN_PROC_GROUPS; - mib[3] = pid; + mib[3] = kipp->ki_pid; ngroups = sysconf(_SC_NGROUPS_MAX) + 1; len = ngroups * sizeof(gid_t); if((groups = malloc(len)) == NULL) err(-1, "malloc"); if (sysctl(mib, 4, groups, &len, NULL, 0) == -1) { warn("sysctl: kern.proc.groups: %d " - "group list truncated", pid); + "group list truncated", kipp->ki_pid); free(groups); groups = NULL; } ngroups = len / sizeof(gid_t); } if (groups == NULL) { ngroups = kipp->ki_ngroups; groups = kipp->ki_groups; } for (i = 0; i < ngroups; i++) printf("%s%d", (i > 0) ? "," : "", groups[i]); if (groups != kipp->ki_groups) free(groups); printf("\n"); } Index: head/usr.bin/procstat/procstat_files.c =================================================================== --- head/usr.bin/procstat/procstat_files.c (revision 221806) +++ head/usr.bin/procstat/procstat_files.c (revision 221807) @@ -1,321 +1,323 @@ /*- * Copyright (c) 2007 Robert N. M. Watson * 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. * * $FreeBSD$ */ #include #include #include #include #include #include #include #include +#include #include #include #include #include -#include #include "procstat.h" static const char * protocol_to_string(int domain, int type, int protocol) { switch (domain) { case AF_INET: case AF_INET6: switch (protocol) { case IPPROTO_TCP: return ("TCP"); case IPPROTO_UDP: return ("UDP"); case IPPROTO_ICMP: return ("ICM"); case IPPROTO_RAW: return ("RAW"); case IPPROTO_SCTP: return ("SCT"); case IPPROTO_DIVERT: return ("IPD"); default: return ("IP?"); } case AF_LOCAL: switch (type) { case SOCK_STREAM: return ("UDS"); case SOCK_DGRAM: return ("UDD"); default: return ("UD?"); } default: return ("?"); } } static void addr_to_string(struct sockaddr_storage *ss, char *buffer, int buflen) { char buffer2[INET6_ADDRSTRLEN]; struct sockaddr_in6 *sin6; struct sockaddr_in *sin; struct sockaddr_un *sun; switch (ss->ss_family) { case AF_LOCAL: sun = (struct sockaddr_un *)ss; if (strlen(sun->sun_path) == 0) strlcpy(buffer, "-", buflen); else strlcpy(buffer, sun->sun_path, buflen); break; case AF_INET: sin = (struct sockaddr_in *)ss; snprintf(buffer, buflen, "%s:%d", inet_ntoa(sin->sin_addr), ntohs(sin->sin_port)); break; case AF_INET6: sin6 = (struct sockaddr_in6 *)ss; if (inet_ntop(AF_INET6, &sin6->sin6_addr, buffer2, sizeof(buffer2)) != NULL) snprintf(buffer, buflen, "%s.%d", buffer2, ntohs(sin6->sin6_port)); else strlcpy(buffer, "-", sizeof(buffer)); break; default: strlcpy(buffer, "", buflen); break; } } static void print_address(struct sockaddr_storage *ss) { char addr[PATH_MAX]; addr_to_string(ss, addr, sizeof(addr)); printf("%s", addr); } void -procstat_files(pid_t pid, struct kinfo_proc *kipp) -{ - struct kinfo_file *freep, *kif; - int i, cnt; +procstat_files(struct procstat *procstat, struct kinfo_proc *kipp) +{ + struct sockstat sock; + struct filestat_list *head; + struct filestat *fst; const char *str; + struct vnstat vn; + int error; if (!hflag) printf("%5s %-16s %4s %1s %1s %-8s %3s %7s %-3s %-12s\n", "PID", "COMM", "FD", "T", "V", "FLAGS", "REF", "OFFSET", "PRO", "NAME"); - freep = kinfo_getfile(pid, &cnt); - if (freep == NULL) + head = procstat_getfiles(procstat, kipp, 0); + if (head == NULL) return; - for (i = 0; i < cnt; i++) { - kif = &freep[i]; - - printf("%5d ", pid); + STAILQ_FOREACH(fst, head, next) { + printf("%5d ", kipp->ki_pid); printf("%-16s ", kipp->ki_comm); - switch (kif->kf_fd) { - case KF_FD_TYPE_CWD: + if (fst->fs_uflags & PS_FST_UFLAG_CTTY) + printf("ctty "); + else if (fst->fs_uflags & PS_FST_UFLAG_CDIR) printf(" cwd "); - break; - - case KF_FD_TYPE_ROOT: + else if (fst->fs_uflags & PS_FST_UFLAG_JAIL) + printf("jail "); + else if (fst->fs_uflags & PS_FST_UFLAG_RDIR) printf("root "); - break; + else if (fst->fs_uflags & PS_FST_UFLAG_TEXT) + printf("text "); + else if (fst->fs_uflags & PS_FST_UFLAG_TRACE) + printf("trace "); + else + printf("%4d ", fst->fs_fd); - case KF_FD_TYPE_JAIL: - printf("jail "); - break; - - default: - printf("%4d ", kif->kf_fd); - break; - } - switch (kif->kf_type) { - case KF_TYPE_VNODE: + switch (fst->fs_type) { + case PS_FST_TYPE_VNODE: str = "v"; break; - case KF_TYPE_SOCKET: + case PS_FST_TYPE_SOCKET: str = "s"; break; - case KF_TYPE_PIPE: + case PS_FST_TYPE_PIPE: str = "p"; break; - case KF_TYPE_FIFO: + case PS_FST_TYPE_FIFO: str = "f"; break; - case KF_TYPE_KQUEUE: + case PS_FST_TYPE_KQUEUE: str = "k"; break; - case KF_TYPE_CRYPTO: + case PS_FST_TYPE_CRYPTO: str = "c"; break; - case KF_TYPE_MQUEUE: + case PS_FST_TYPE_MQUEUE: str = "m"; break; - case KF_TYPE_SHM: + case PS_FST_TYPE_SHM: str = "h"; break; - case KF_TYPE_PTS: + case PS_FST_TYPE_PTS: str = "t"; break; - case KF_TYPE_SEM: + case PS_FST_TYPE_SEM: str = "e"; break; - case KF_TYPE_NONE: - case KF_TYPE_UNKNOWN: + case PS_FST_TYPE_NONE: + case PS_FST_TYPE_UNKNOWN: default: str = "?"; break; } printf("%1s ", str); str = "-"; - if (kif->kf_type == KF_TYPE_VNODE) { - switch (kif->kf_vnode_type) { - case KF_VTYPE_VREG: + if (fst->fs_type == PS_FST_TYPE_VNODE) { + error = procstat_get_vnode_info(procstat, fst, &vn, NULL); + switch (vn.vn_type) { + case PS_FST_VTYPE_VREG: str = "r"; break; - case KF_VTYPE_VDIR: + case PS_FST_VTYPE_VDIR: str = "d"; break; - case KF_VTYPE_VBLK: + case PS_FST_VTYPE_VBLK: str = "b"; break; - case KF_VTYPE_VCHR: + case PS_FST_VTYPE_VCHR: str = "c"; break; - case KF_VTYPE_VLNK: + case PS_FST_VTYPE_VLNK: str = "l"; break; - case KF_VTYPE_VSOCK: + case PS_FST_VTYPE_VSOCK: str = "s"; break; - case KF_VTYPE_VFIFO: + case PS_FST_VTYPE_VFIFO: str = "f"; break; - case KF_VTYPE_VBAD: + case PS_FST_VTYPE_VBAD: str = "x"; break; - case KF_VTYPE_VNON: - case KF_VTYPE_UNKNOWN: + case PS_FST_VTYPE_VNON: + case PS_FST_VTYPE_UNKNOWN: default: str = "?"; break; } } printf("%1s ", str); - printf("%s", kif->kf_flags & KF_FLAG_READ ? "r" : "-"); - printf("%s", kif->kf_flags & KF_FLAG_WRITE ? "w" : "-"); - printf("%s", kif->kf_flags & KF_FLAG_APPEND ? "a" : "-"); - printf("%s", kif->kf_flags & KF_FLAG_ASYNC ? "s" : "-"); - printf("%s", kif->kf_flags & KF_FLAG_FSYNC ? "f" : "-"); - printf("%s", kif->kf_flags & KF_FLAG_NONBLOCK ? "n" : "-"); - printf("%s", kif->kf_flags & KF_FLAG_DIRECT ? "d" : "-"); - printf("%s ", kif->kf_flags & KF_FLAG_HASLOCK ? "l" : "-"); - if (kif->kf_ref_count > -1) - printf("%3d ", kif->kf_ref_count); + printf("%s", fst->fs_fflags & PS_FST_FFLAG_READ ? "r" : "-"); + printf("%s", fst->fs_fflags & PS_FST_FFLAG_WRITE ? "w" : "-"); + printf("%s", fst->fs_fflags & PS_FST_FFLAG_APPEND ? "a" : "-"); + printf("%s", fst->fs_fflags & PS_FST_FFLAG_ASYNC ? "s" : "-"); + printf("%s", fst->fs_fflags & PS_FST_FFLAG_SYNC ? "f" : "-"); + printf("%s", fst->fs_fflags & PS_FST_FFLAG_NONBLOCK ? "n" : "-"); + printf("%s", fst->fs_fflags & PS_FST_FFLAG_DIRECT ? "d" : "-"); + printf("%s ", fst->fs_fflags & PS_FST_FFLAG_HASLOCK ? "l" : "-"); + if (fst->fs_ref_count > -1) + printf("%3d ", fst->fs_ref_count); else printf("%3c ", '-'); - if (kif->kf_offset > -1) - printf("%7jd ", (intmax_t)kif->kf_offset); + if (fst->fs_offset > -1) + printf("%7jd ", (intmax_t)fst->fs_offset); else printf("%7c ", '-'); - switch (kif->kf_type) { - case KF_TYPE_VNODE: - case KF_TYPE_FIFO: - case KF_TYPE_PTS: + switch (fst->fs_type) { + case PS_FST_TYPE_VNODE: + case PS_FST_TYPE_FIFO: + case PS_FST_TYPE_PTS: printf("%-3s ", "-"); - printf("%-18s", kif->kf_path); + printf("%-18s", fst->fs_path != NULL ? fst->fs_path : "-"); break; - case KF_TYPE_SOCKET: + case PS_FST_TYPE_SOCKET: + error = procstat_get_socket_info(procstat, fst, &sock, NULL); + if (error != 0) + break; printf("%-3s ", - protocol_to_string(kif->kf_sock_domain, - kif->kf_sock_type, kif->kf_sock_protocol)); + protocol_to_string(sock.dom_family, + sock.type, sock.proto)); /* * While generally we like to print two addresses, * local and peer, for sockets, it turns out to be * more useful to print the first non-nul address for * local sockets, as typically they aren't bound and * connected, and the path strings can get long. */ - if (kif->kf_sock_domain == AF_LOCAL) { + if (sock.dom_family == AF_LOCAL) { struct sockaddr_un *sun = - (struct sockaddr_un *)&kif->kf_sa_local; + (struct sockaddr_un *)&sock.sa_local; if (sun->sun_path[0] != 0) - print_address(&kif->kf_sa_local); + print_address(&sock.sa_local); else - print_address(&kif->kf_sa_peer); + print_address(&sock.sa_peer); } else { - print_address(&kif->kf_sa_local); + print_address(&sock.sa_local); printf(" "); - print_address(&kif->kf_sa_peer); + print_address(&sock.sa_peer); } break; default: printf("%-3s ", "-"); printf("%-18s", "-"); } printf("\n"); } - free(freep); } Index: head/usr.bin/procstat/procstat_kstack.c =================================================================== --- head/usr.bin/procstat/procstat_kstack.c (revision 221806) +++ head/usr.bin/procstat/procstat_kstack.c (revision 221807) @@ -1,246 +1,247 @@ /*- * Copyright (c) 2007 Robert N. M. Watson * 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. * * $FreeBSD$ */ #include #include #include #include #include +#include #include #include #include #include "procstat.h" /* * Walk the stack trace provided by the kernel and reduce it to what we * actually want to print. This involves stripping true instruction pointers, * frame numbers, and carriage returns as generated by stack(9). If -kk is * specified, print the function and offset, otherwise just the function. */ enum trace_state { TS_FRAMENUM, TS_PC, TS_AT, TS_FUNC, TS_OFF }; static enum trace_state kstack_nextstate(enum trace_state ts) { switch (ts) { case TS_FRAMENUM: return (TS_PC); case TS_PC: return (TS_AT); case TS_AT: return (TS_FUNC); case TS_FUNC: return (TS_OFF); case TS_OFF: return TS_FRAMENUM; default: errx(-1, "kstack_nextstate"); } } static void kstack_cleanup(const char *old, char *new, int kflag) { enum trace_state old_ts, ts; const char *cp_old; char *cp_new; ts = TS_FRAMENUM; for (cp_old = old, cp_new = new; *cp_old != '\0'; cp_old++) { switch (*cp_old) { case ' ': case '\n': case '+': old_ts = ts; ts = kstack_nextstate(old_ts); if (old_ts == TS_OFF) { *cp_new = ' '; cp_new++; } if (kflag > 1 && old_ts == TS_FUNC) { *cp_new = '+'; cp_new++; } continue; } if (ts == TS_FUNC || (kflag > 1 && ts == TS_OFF)) { *cp_new = *cp_old; cp_new++; } } *cp_new = '\0'; } /* * Sort threads by tid. */ static int kinfo_kstack_compare(const void *a, const void *b) { return ((const struct kinfo_kstack *)a)->kkst_tid - ((const struct kinfo_kstack *)b)->kkst_tid; } static void kinfo_kstack_sort(struct kinfo_kstack *kkstp, int count) { qsort(kkstp, count, sizeof(*kkstp), kinfo_kstack_compare); } void -procstat_kstack(pid_t pid, struct kinfo_proc *kipp, int kflag) +procstat_kstack(struct kinfo_proc *kipp, int kflag) { struct kinfo_kstack *kkstp, *kkstp_free; struct kinfo_proc *kip, *kip_free; char trace[KKST_MAXLEN]; int error, name[4]; unsigned int i, j; size_t kip_len, kstk_len; if (!hflag) printf("%5s %6s %-16s %-16s %-29s\n", "PID", "TID", "COMM", "TDNAME", "KSTACK"); name[0] = CTL_KERN; name[1] = KERN_PROC; name[2] = KERN_PROC_KSTACK; - name[3] = pid; + name[3] = kipp->ki_pid; kstk_len = 0; error = sysctl(name, 4, NULL, &kstk_len, NULL, 0); if (error < 0 && errno != ESRCH && errno != EPERM && errno != ENOENT) { - warn("sysctl: kern.proc.kstack: %d", pid); + warn("sysctl: kern.proc.kstack: %d", kipp->ki_pid); return; } if (error < 0 && errno == ENOENT) { warnx("sysctl: kern.proc.kstack unavailable"); errx(-1, "options DDB or options STACK required in kernel"); } if (error < 0) return; kkstp = kkstp_free = malloc(kstk_len); if (kkstp == NULL) err(-1, "malloc"); if (sysctl(name, 4, kkstp, &kstk_len, NULL, 0) < 0) { - warn("sysctl: kern.proc.pid: %d", pid); + warn("sysctl: kern.proc.pid: %d", kipp->ki_pid); free(kkstp); return; } /* * We need to re-query for thread information, so don't use *kipp. */ name[0] = CTL_KERN; name[1] = KERN_PROC; name[2] = KERN_PROC_PID | KERN_PROC_INC_THREAD; - name[3] = pid; + name[3] = kipp->ki_pid; kip_len = 0; error = sysctl(name, 4, NULL, &kip_len, NULL, 0); if (error < 0 && errno != ESRCH) { - warn("sysctl: kern.proc.pid: %d", pid); + warn("sysctl: kern.proc.pid: %d", kipp->ki_pid); return; } if (error < 0) return; kip = kip_free = malloc(kip_len); if (kip == NULL) err(-1, "malloc"); if (sysctl(name, 4, kip, &kip_len, NULL, 0) < 0) { - warn("sysctl: kern.proc.pid: %d", pid); + warn("sysctl: kern.proc.pid: %d", kipp->ki_pid); free(kip); return; } kinfo_kstack_sort(kkstp, kstk_len / sizeof(*kkstp)); for (i = 0; i < kstk_len / sizeof(*kkstp); i++) { kkstp = &kkstp_free[i]; /* * Look up the specific thread using its tid so we can * display the per-thread command line. */ kipp = NULL; for (j = 0; j < kip_len / sizeof(*kipp); j++) { kipp = &kip_free[j]; if (kkstp->kkst_tid == kipp->ki_tid) break; } if (kipp == NULL) continue; - printf("%5d ", pid); + printf("%5d ", kipp->ki_pid); printf("%6d ", kkstp->kkst_tid); printf("%-16s ", kipp->ki_comm); printf("%-16s ", (strlen(kipp->ki_ocomm) && (strcmp(kipp->ki_comm, kipp->ki_ocomm) != 0)) ? kipp->ki_ocomm : "-"); switch (kkstp->kkst_state) { case KKST_STATE_RUNNING: printf("%-29s\n", ""); continue; case KKST_STATE_SWAPPED: printf("%-29s\n", ""); continue; case KKST_STATE_STACKOK: break; default: printf("%-29s\n", ""); continue; } /* * The kernel generates a trace with carriage returns between * entries, but for a more compact view, we convert carriage * returns to spaces. */ kstack_cleanup(kkstp->kkst_trace, trace, kflag); printf("%-29s\n", trace); } free(kip_free); free(kkstp_free); } Index: head/usr.bin/procstat/procstat_sigs.c =================================================================== --- head/usr.bin/procstat/procstat_sigs.c (revision 221806) +++ head/usr.bin/procstat/procstat_sigs.c (revision 221807) @@ -1,139 +1,144 @@ /*- * Copyright (c) 2010 Konstantin Belousov * 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. * * $FreeBSD$ */ #include #include #include #include #include #include #include #include #include #include +#include #include "procstat.h" static void procstat_print_signame(int sig) { char name[12]; int i; if (!nflag && sig < sys_nsig) { strlcpy(name, sys_signame[sig], sizeof(name)); for (i = 0; name[i] != 0; i++) name[i] = toupper(name[i]); printf("%-7s ", name); } else printf("%-7d ", sig); } static void procstat_print_sig(const sigset_t *set, int sig, char flag) { printf("%c", sigismember(set, sig) ? flag : '-'); } void -procstat_sigs(pid_t pid, struct kinfo_proc *kipp) +procstat_sigs(struct procstat *prstat __unused, struct kinfo_proc *kipp) { int j; + pid_t pid; + pid = kipp->ki_pid; if (!hflag) printf("%5s %-16s %-7s %4s\n", "PID", "COMM", "SIG", "FLAGS"); for (j = 1; j <= _SIG_MAXSIG; j++) { printf("%5d ", pid); printf("%-16s ", kipp->ki_comm); procstat_print_signame(j); printf(" "); procstat_print_sig(&kipp->ki_siglist, j, 'P'); procstat_print_sig(&kipp->ki_sigignore, j, 'I'); procstat_print_sig(&kipp->ki_sigcatch, j, 'C'); printf("\n"); } } void -procstat_threads_sigs(pid_t pid, struct kinfo_proc *kipp) +procstat_threads_sigs(struct procstat *prstat __unused, struct kinfo_proc *kipp) { struct kinfo_proc *kip; + pid_t pid; int error, name[4], j; unsigned int i; size_t len; + pid = kipp->ki_pid; if (!hflag) printf("%5s %6s %-16s %-7s %4s\n", "PID", "TID", "COMM", "SIG", "FLAGS"); /* * We need to re-query for thread information, so don't use *kipp. */ name[0] = CTL_KERN; name[1] = KERN_PROC; name[2] = KERN_PROC_PID | KERN_PROC_INC_THREAD; name[3] = pid; len = 0; error = sysctl(name, 4, NULL, &len, NULL, 0); if (error < 0 && errno != ESRCH) { warn("sysctl: kern.proc.pid: %d", pid); return; } if (error < 0) return; kip = malloc(len); if (kip == NULL) err(-1, "malloc"); if (sysctl(name, 4, kip, &len, NULL, 0) < 0) { warn("sysctl: kern.proc.pid: %d", pid); free(kip); return; } kinfo_proc_sort(kip, len / sizeof(*kipp)); for (i = 0; i < len / sizeof(*kipp); i++) { kipp = &kip[i]; for (j = 1; j <= _SIG_MAXSIG; j++) { printf("%5d ", pid); printf("%6d ", kipp->ki_tid); printf("%-16s ", kipp->ki_comm); procstat_print_signame(j); printf(" "); procstat_print_sig(&kipp->ki_siglist, j, 'P'); procstat_print_sig(&kipp->ki_sigmask, j, 'B'); printf("\n"); } } free(kip); } Index: head/usr.bin/procstat/procstat_threads.c =================================================================== --- head/usr.bin/procstat/procstat_threads.c (revision 221806) +++ head/usr.bin/procstat/procstat_threads.c (revision 221807) @@ -1,142 +1,143 @@ /*- * Copyright (c) 2007 Robert N. M. Watson * 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. * * $FreeBSD$ */ #include #include #include #include #include +#include #include #include #include #include "procstat.h" void -procstat_threads(pid_t pid, struct kinfo_proc *kipp) +procstat_threads(struct kinfo_proc *kipp) { struct kinfo_proc *kip; int error, name[4]; unsigned int i; const char *str; size_t len; if (!hflag) printf("%5s %6s %-16s %-16s %2s %4s %-7s %-9s\n", "PID", "TID", "COMM", "TDNAME", "CPU", "PRI", "STATE", "WCHAN"); /* * We need to re-query for thread information, so don't use *kipp. */ name[0] = CTL_KERN; name[1] = KERN_PROC; name[2] = KERN_PROC_PID | KERN_PROC_INC_THREAD; - name[3] = pid; + name[3] = kipp->ki_pid; len = 0; error = sysctl(name, 4, NULL, &len, NULL, 0); if (error < 0 && errno != ESRCH) { - warn("sysctl: kern.proc.pid: %d", pid); + warn("sysctl: kern.proc.pid: %d", kipp->ki_pid); return; } if (error < 0) return; kip = malloc(len); if (kip == NULL) err(-1, "malloc"); if (sysctl(name, 4, kip, &len, NULL, 0) < 0) { - warn("sysctl: kern.proc.pid: %d", pid); + warn("sysctl: kern.proc.pid: %d", kipp->ki_pid); free(kip); return; } kinfo_proc_sort(kip, len / sizeof(*kipp)); for (i = 0; i < len / sizeof(*kipp); i++) { kipp = &kip[i]; - printf("%5d ", pid); + printf("%5d ", kipp->ki_pid); printf("%6d ", kipp->ki_tid); printf("%-16s ", strlen(kipp->ki_comm) ? kipp->ki_comm : "-"); printf("%-16s ", (strlen(kipp->ki_ocomm) && (strcmp(kipp->ki_comm, kipp->ki_ocomm) != 0)) ? kipp->ki_ocomm : "-"); if (kipp->ki_oncpu != 255) printf("%3d ", kipp->ki_oncpu); else if (kipp->ki_lastcpu != 255) printf("%3d ", kipp->ki_lastcpu); else printf("%3s ", "-"); printf("%4d ", kipp->ki_pri.pri_level); switch (kipp->ki_stat) { case SRUN: str = "run"; break; case SSTOP: str = "stop"; break; case SSLEEP: str = "sleep"; break; case SLOCK: str = "lock"; break; case SWAIT: str = "wait"; break; case SZOMB: str = "zomb"; break; case SIDL: str = "idle"; break; default: str = "??"; break; } printf("%-7s ", str); if (kipp->ki_kiflag & KI_LOCKBLOCK) { printf("*%-8s ", strlen(kipp->ki_lockname) ? kipp->ki_lockname : "-"); } else { printf("%-9s ", strlen(kipp->ki_wmesg) ? kipp->ki_wmesg : "-"); } printf("\n"); } free(kip); } Index: head/usr.bin/procstat/procstat_vm.c =================================================================== --- head/usr.bin/procstat/procstat_vm.c (revision 221806) +++ head/usr.bin/procstat/procstat_vm.c (revision 221807) @@ -1,108 +1,109 @@ /*- * Copyright (c) 2007 Robert N. M. Watson * 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. * * $FreeBSD$ */ #include #include #include #include #include +#include #include #include #include #include #include "procstat.h" void -procstat_vm(pid_t pid, struct kinfo_proc *kipp __unused) +procstat_vm(struct kinfo_proc *kipp) { struct kinfo_vmentry *freep, *kve; int ptrwidth; int i, cnt; const char *str; ptrwidth = 2*sizeof(void *) + 2; if (!hflag) printf("%5s %*s %*s %3s %4s %4s %3s %3s %2s %-2s %-s\n", "PID", ptrwidth, "START", ptrwidth, "END", "PRT", "RES", "PRES", "REF", "SHD", "FL", "TP", "PATH"); - freep = kinfo_getvmmap(pid, &cnt); + freep = kinfo_getvmmap(kipp->ki_pid, &cnt); if (freep == NULL) return; for (i = 0; i < cnt; i++) { kve = &freep[i]; - printf("%5d ", pid); + printf("%5d ", kipp->ki_pid); printf("%#*jx ", ptrwidth, (uintmax_t)kve->kve_start); printf("%#*jx ", ptrwidth, (uintmax_t)kve->kve_end); printf("%s", kve->kve_protection & KVME_PROT_READ ? "r" : "-"); printf("%s", kve->kve_protection & KVME_PROT_WRITE ? "w" : "-"); printf("%s ", kve->kve_protection & KVME_PROT_EXEC ? "x" : "-"); printf("%4d ", kve->kve_resident); printf("%4d ", kve->kve_private_resident); printf("%3d ", kve->kve_ref_count); printf("%3d ", kve->kve_shadow_count); printf("%-1s", kve->kve_flags & KVME_FLAG_COW ? "C" : "-"); printf("%-1s ", kve->kve_flags & KVME_FLAG_NEEDS_COPY ? "N" : "-"); switch (kve->kve_type) { case KVME_TYPE_NONE: str = "--"; break; case KVME_TYPE_DEFAULT: str = "df"; break; case KVME_TYPE_VNODE: str = "vn"; break; case KVME_TYPE_SWAP: str = "sw"; break; case KVME_TYPE_DEVICE: str = "dv"; break; case KVME_TYPE_PHYS: str = "ph"; break; case KVME_TYPE_DEAD: str = "dd"; break; case KVME_TYPE_SG: str = "sg"; break; case KVME_TYPE_UNKNOWN: default: str = "??"; break; } printf("%-2s ", str); printf("%-s\n", kve->kve_path); } free(freep); }