Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

NetBSD 1.4.1

  • Loading branch information...
commit c016e0d216f2175ddc867def32dfa6c8a8ced4a7 1 parent 3445633
itojun authored
View
16 netbsd/usr.bin/fstat/Makefile
@@ -0,0 +1,16 @@
+# $NetBSD: Makefile,v 1.12 1999/03/24 05:51:31 mrg Exp $
+# from: @(#)Makefile 8.1 (Berkeley) 6/6/93
+
+PROG= fstat
+SRCS= fstat.c isofs.c
+DPADD= ${LIBKVM}
+LDADD= -lkvm
+BINGRP= kmem
+BINMODE=2555
+
+.include <bsd.prog.mk>
+
+# XXX
+.if defined(PMAP_NEW)
+CPPFLAGS+= -DPMAP_NEW
+.endif
View
260 netbsd/usr.bin/fstat/fstat.1
@@ -0,0 +1,260 @@
+.\" $NetBSD: fstat.1,v 1.14 1999/03/22 18:16:37 garbled Exp $
+.\"
+.\" Copyright (c) 1987, 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.
+.\" 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.
+.\"
+.\" from: @(#)fstat.1 8.3 (Berkeley) 2/25/94
+.\" $NetBSD: fstat.1,v 1.14 1999/03/22 18:16:37 garbled Exp $
+.\"
+.Dd February 18, 1999
+.Dt FSTAT 1
+.Os
+.Sh NAME
+.Nm fstat
+.Nd file status
+.Sh SYNOPSIS
+.Nm
+.Op Fl fnv
+.Op Fl M Ar core
+.Op Fl N Ar system
+.Op Fl p Ar pid
+.Op Fl u Ar user
+.Op Ar filename...
+.Sh DESCRIPTION
+.Nm
+identifies open files.
+A file is considered open by a process if it was explicitly opened,
+is the working directory, root directory, active pure text, or kernel
+trace file for that process.
+If no options are specified,
+.Nm
+reports on all open files in the system.
+.Pp
+Options:
+.Bl -tag -width Ds
+.It Fl f
+Restrict examination to files open in the same filesystems as
+the named file arguments, or to the filesystem containing the
+current directory if there are no additional filename arguments.
+For example, to find all files open in the filesystem where the
+directory
+.Pa /usr/src
+resides, type
+.Dq Li fstat -f /usr/src .
+.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
+.Pa /netbsd .
+.It Fl n
+Numerical format. Print the device number (maj,min) of the filesystem
+the file resides in rather than the mount point name; for special
+files, print the
+device number that the special device refers to rather than the filename
+in
+.Pa /dev ;
+and print the mode of the file in octal instead of symbolic form.
+.It Fl p
+Report all files open by the specified process.
+.It Fl u
+Report all files open by the specified user.
+.It Fl v
+Verbose mode. Print error messages upon failures to locate particular
+system data structures rather than silently ignoring them. Most of
+these data structures are dynamically created or deleted and it is
+possible for them to disappear while
+.Nm
+is running. This
+is normal and unavoidable since the rest of the system is running while
+.Nm
+itself is running.
+.It Ar filename ...
+Restrict reports to the specified files.
+.El
+.Pp
+The following fields are printed:
+.Bl -tag -width MOUNT
+.It Li USER
+The username of the owner of the process (effective UID).
+.It Li CMD
+The command name of the process.
+.It Li PID
+The process ID.
+.It Li FD
+The file number in the per-process open file table or one of the following
+special names:
+.Pp
+.Bl -tag -width MOUNT -offset -indent -compact
+.It Li text
+pure text inode
+.It Li wd
+current working directory
+.It Li root
+root inode
+.It Li tr
+kernel trace file
+.El
+.Pp
+If the file number is followed by an asterisk
+.Pq Dq * ,
+the file is not an inode, but rather a socket,
+.Tn FIFO ,
+or there is an error.
+In this case the remainder of the line doesn't
+correspond to the remaining headers -- the format of the line
+is described later under
+.Sx Sockets .
+.It Li MOUNT
+If the
+.Fl n
+flag wasn't specified, this header is present and is the
+pathname that the filesystem the file resides in is mounted on.
+.It Li DEV
+If the
+.Fl n
+flag is specified, this header is present and is the
+major/minor number of the device that this file resides in.
+.It Li INUM
+The inode number of the file.
+.It Li MODE
+The mode of the file. If the
+.Fl n
+flag isn't specified, the mode is printed
+using a symbolic format (see
+.Xr strmode 3 ) ;
+otherwise, the mode is printed
+as an octal number.
+.It Li SZ\&|DV
+If the file is not a character or block special file, prints the size of
+the file in bytes. Otherwise, if the
+.Fl n
+flag is not specified, prints
+the name of the special file as located in
+.Pa /dev .
+If that cannot be
+located, or the
+.Fl n
+flag is specified, prints the major/minor device
+number that the special device refers to.
+.It Li R/W
+This column describes the access mode that the file allows.
+The letter
+.Dq r
+indicates open for reading;
+the letter
+.Dq
+indicates open for writing.
+This field is useful when trying to find the processes that are
+preventing a filesystem from being downgraded to read-only.
+.It Li NAME
+If filename arguments are specified and the
+.Fl f
+flag is not, then
+this field is present and is the name associated with the given file.
+Normally the name cannot be determined since there is no mapping
+from an open file back to the directory entry that was used to open
+that file. Also, since different directory entries may reference
+the same file (via
+.Xr ln 1 ) ,
+the name printed may not be the actual
+name that the process originally used to open that file.
+.El
+.Sh SOCKETS
+The formating of open sockets depends on the protocol domain.
+In all cases the first field is the domain name, the second field
+is the socket type (stream, dgram, etc), and the third is the socket
+flags field (in hex).
+The remaining fields are protocol dependent.
+For TCP, it is the address of the tcpcb, and for UDP, the inpcb (socket pcb).
+For
+.Ux
+domain sockets, its the address of the socket pcb and the address
+of the connected pcb (if connected).
+Otherwise the protocol number and address of the socket itself are printed.
+The attempt is to make enough information available to
+permit further analysis without duplicating
+.Xr netstat 1 .
+.Pp
+For example, the addresses mentioned above are the addresses which the
+.Dq Li netstat -A
+command would print for TCP, UDP, and
+.Ux
+domain.
+Note that since pipes are implemented using sockets, a pipe appears as a
+connected
+.Ux
+domain stream socket.
+A unidirectional
+.Ux
+domain socket indicates the direction of flow with an arrow
+.Po
+.Dq <-
+or
+.Dq ->
+.Pc ,
+and a full duplex socket shows a double arrow
+.Pq Dq <-> .
+.Pp
+For internet sockets
+.Nm fstat
+also attempts to print the internet address and port for the
+local end of a connection.
+If the socket is connected, it also prints the remote internet address
+and port.
+A
+.Pq Dq *
+is used to indicate an INADDR_ANY binding.
+.Sh BUGS
+Since
+.Nm
+takes a snapshot of the system, it is only correct for a very short period
+of time.
+.Pp
+Moreover, because DNS resolution and YP lookups cause many file
+descriptor changes,
+.Nm
+does not attempt to translate the internet address and port numbers into
+symbolic names.
+.Sh SEE ALSO
+.Xr netstat 1 ,
+.Xr nfsstat 1 ,
+.Xr ps 1 ,
+.Xr systat 1 ,
+.Xr iostat 8 ,
+.Xr pstat 8 ,
+.Xr vmstat 8
+.Sh HISTORY
+The
+.Nm
+command appeared in
+.Bx 4.3 tahoe .
View
798 netbsd/usr.bin/fstat/fstat.c
@@ -0,0 +1,798 @@
+/* $NetBSD: fstat.c,v 1.34 1999/02/18 06:09:25 lukem Exp $ */
+
+/*-
+ * Copyright (c) 1988, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by the University of
+ * California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+#ifndef lint
+__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";
+#else
+__RCSID("$NetBSD: fstat.c,v 1.34 1999/02/18 06:09:25 lukem Exp $");
+#endif
+#endif /* not lint */
+
+#include <sys/param.h>
+#include <sys/time.h>
+#include <sys/proc.h>
+#include <sys/user.h>
+#include <sys/stat.h>
+#include <sys/vnode.h>
+#include <sys/socket.h>
+#include <sys/socketvar.h>
+#include <sys/domain.h>
+#include <sys/protosw.h>
+#include <sys/unpcb.h>
+#include <sys/sysctl.h>
+#include <sys/filedesc.h>
+#define _KERNEL
+#include <sys/file.h>
+#include <ufs/ufs/quota.h>
+#include <ufs/ufs/inode.h>
+#undef _KERNEL
+#define NFS
+#include <sys/mount.h>
+#include <nfs/nfsproto.h>
+#include <nfs/rpcv2.h>
+#include <nfs/nfs.h>
+#include <nfs/nfsnode.h>
+#undef NFS
+
+#include <net/route.h>
+#include <netinet/in.h>
+#include <netinet/in_systm.h>
+#include <netinet/ip.h>
+#include <netinet/in_pcb.h>
+
+#include <arpa/inet.h>
+
+#include <ctype.h>
+#include <errno.h>
+#include <kvm.h>
+#include <limits.h>
+#include <nlist.h>
+#include <paths.h>
+#include <pwd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <err.h>
+
+#include "fstat.h"
+
+#define TEXT -1
+#define CDIR -2
+#define RDIR -3
+#define TRACE -4
+
+typedef struct devs {
+ struct devs *next;
+ long fsid;
+ ino_t ino;
+ char *name;
+} DEVS;
+DEVS *devs;
+
+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... */
+
+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, "malloc(%u)", (d) * \
+ (unsigned int)sizeof(struct file *)); \
+ } \
+ maxfiles = (d); \
+ }
+
+kvm_t *kd;
+
+void dofiles __P((struct kinfo_proc *));
+int ext2fs_filestat __P((struct vnode *, struct filestat *));
+int getfname __P((char *));
+void getinetproto __P((int));
+char *getmnton __P((struct mount *));
+int main __P((int, char **));
+int nfs_filestat __P((struct vnode *, struct filestat *));
+void socktrans __P((struct socket *, int));
+int ufs_filestat __P((struct vnode *, struct filestat *));
+void usage __P((void));
+void vtrans __P((struct vnode *, int, int));
+
+int
+main(argc, argv)
+ int argc;
+ char **argv;
+{
+ struct passwd *passwd;
+ struct kinfo_proc *p, *plast;
+ int arg, ch, what;
+ char *memf, *nlistf;
+ char buf[_POSIX2_LINE_MAX];
+ int cnt;
+ gid_t egid = getegid();
+
+ (void)setegid(getgid());
+ arg = 0;
+ what = KERN_PROC_ALL;
+ nlistf = memf = NULL;
+ while ((ch = getopt(argc, argv, "fnp:u:vN:M:")) != -1)
+ switch((char)ch) {
+ case 'f':
+ fsflg = 1;
+ break;
+ case 'M':
+ memf = optarg;
+ break;
+ case 'N':
+ nlistf = optarg;
+ 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);
+ }
+
+ ALLOC_OFILES(256); /* reserve space for file pointers */
+
+ if (fsflg && !checkfile) {
+ /* -f with no files means use wd */
+ if (getfname(".") == 0)
+ exit(1);
+ checkfile = 1;
+ }
+
+ /*
+ * Discard setgid privileges. If not the running kernel, we toss
+ * them away totally so that bad guys can't print interesting stuff
+ * from kernel memory, otherwise switch back to kmem for the
+ * duration of the kvm_openfiles() call.
+ */
+ if (nlistf != NULL || memf != NULL)
+ (void)setgid(getgid());
+ else
+ (void)setegid(egid);
+
+ if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL)
+ errx(1, "%s", buf);
+
+ /* get rid of it now anyway */
+ if (nlistf == NULL && memf == NULL)
+ (void)setgid(getgid());
+
+ if ((p = kvm_getprocs(kd, what, arg, &cnt)) == NULL) {
+ errx(1, "%s", kvm_geterr(kd));
+ }
+ 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');
+
+ for (plast = &p[cnt]; p < plast; ++p) {
+ if (p->kp_proc.p_stat == SZOMB)
+ continue;
+ dofiles(p);
+ }
+ exit(0);
+}
+
+const char *Uname, *Comm;
+pid_t 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; \
+ default: \
+ printf(" %4d", i); \
+ break; \
+ }
+
+/*
+ * print open files attributed to this process
+ */
+void
+dofiles(kp)
+ struct kinfo_proc *kp;
+{
+ int i;
+ struct file file;
+ struct filedesc0 filed0;
+#define filed filed0.fd_fd
+ struct proc *p = &kp->kp_proc;
+ struct eproc *ep = &kp->kp_eproc;
+
+ Uname = user_from_uid(ep->e_ucred.cr_uid, 0);
+ Pid = p->p_pid;
+ Comm = p->p_comm;
+
+ if (p->p_fd == NULL)
+ return;
+ if (!KVM_READ(p->p_fd, &filed0, sizeof (filed0))) {
+ warnx("can't read filedesc at %p for pid %d", p->p_fd, Pid);
+ return;
+ }
+ if (filed.fd_nfiles < 0 || filed.fd_lastfile >= filed.fd_nfiles ||
+ filed.fd_freefile > filed.fd_lastfile + 1) {
+ dprintf("filedesc corrupted at %p for pid %d", p->p_fd, Pid);
+ return;
+ }
+ /*
+ * root directory vnode, if one
+ */
+ if (filed.fd_rdir)
+ vtrans(filed.fd_rdir, RDIR, FREAD);
+ /*
+ * current working directory vnode
+ */
+ vtrans(filed.fd_cdir, CDIR, FREAD);
+ /*
+ * ktrace vnode, if one
+ */
+ if (p->p_tracep)
+ vtrans(p->p_tracep, TRACE, FREAD|FWRITE);
+ /*
+ * open files
+ */
+#define FPSIZE (sizeof (struct file *))
+ ALLOC_OFILES(filed.fd_lastfile+1);
+ if (filed.fd_nfiles > NDFILE) {
+ if (!KVM_READ(filed.fd_ofiles, ofiles,
+ (filed.fd_lastfile+1) * FPSIZE)) {
+ dprintf("can't read file structures at %p for pid %d",
+ filed.fd_ofiles, Pid);
+ return;
+ }
+ } else
+ memmove(ofiles, filed0.fd_dfiles,
+ (filed.fd_lastfile+1) * FPSIZE);
+ for (i = 0; i <= filed.fd_lastfile; i++) {
+ if (ofiles[i] == NULL)
+ continue;
+ if (!KVM_READ(ofiles[i], &file, sizeof (struct file))) {
+ dprintf("can't read file %d at %p for pid %d",
+ i, ofiles[i], Pid);
+ continue;
+ }
+ if (file.f_type == DTYPE_VNODE)
+ vtrans((struct vnode *)file.f_data, i, file.f_flag);
+ else if (file.f_type == DTYPE_SOCKET) {
+ if (checkfile == 0)
+ socktrans((struct socket *)file.f_data, i);
+ }
+ else {
+ dprintf("unknown file type %d for file %d of pid %d",
+ file.f_type, i, Pid);
+ }
+ }
+}
+
+void
+vtrans(vp, i, flag)
+ struct vnode *vp;
+ int i;
+ int flag;
+{
+ struct vnode vn;
+ struct filestat fst;
+ char mode[15], rw[3];
+ char *badtype = NULL, *filename;
+
+ filename = badtype = NULL;
+ if (!KVM_READ(vp, &vn, sizeof (struct vnode))) {
+ dprintf("can't read vnode at %p for pid %d", vp, Pid);
+ return;
+ }
+ if (vn.v_type == VNON || vn.v_tag == VT_NON)
+ badtype = "none";
+ else if (vn.v_type == VBAD)
+ badtype = "bad";
+ else
+ switch (vn.v_tag) {
+ case VT_UFS:
+ if (!ufs_filestat(&vn, &fst))
+ badtype = "error";
+ break;
+ case VT_MFS:
+ if (!ufs_filestat(&vn, &fst))
+ badtype = "error";
+ break;
+ case VT_NFS:
+ if (!nfs_filestat(&vn, &fst))
+ badtype = "error";
+ break;
+ case VT_EXT2FS:
+ if (!ext2fs_filestat(&vn, &fst))
+ badtype = "error";
+ break;
+ case VT_ISOFS:
+ if (!isofs_filestat(&vn, &fst))
+ badtype = "error";
+ break;
+ default: {
+ static char unknown[10];
+ (void)snprintf(badtype = unknown, sizeof unknown,
+ "?(%x)", vn.v_tag);
+ break;;
+ }
+ }
+ if (checkfile) {
+ int fsmatch = 0;
+ DEVS *d;
+
+ if (badtype)
+ return;
+ for (d = devs; d != NULL; d = d->next)
+ if (d->fsid == fst.fsid) {
+ fsmatch = 1;
+ if (d->ino == fst.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)snprintf(mode, sizeof mode, "%o", fst.mode);
+ else
+ strmode(fst.mode, mode);
+ (void)printf(" %6ld %10s", (long)fst.fileid, mode);
+ switch (vn.v_type) {
+ case VBLK:
+ case VCHR: {
+ char *name;
+
+ if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ?
+ S_IFCHR : S_IFBLK)) == NULL))
+ printf(" %2d,%-2d", major(fst.rdev), minor(fst.rdev));
+ else
+ printf(" %6s", name);
+ break;
+ }
+ default:
+ printf(" %6qd", (long long)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(vp, fsp)
+ struct vnode *vp;
+ struct filestat *fsp;
+{
+ struct inode inode;
+
+ if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
+ dprintf("can't read inode at %p for pid %d", VTOI(vp), Pid);
+ return 0;
+ }
+ fsp->fsid = inode.i_dev & 0xffff;
+ fsp->fileid = (long)inode.i_number;
+ fsp->mode = (mode_t)inode.i_ffs_mode;
+ fsp->size = inode.i_ffs_size;
+ fsp->rdev = inode.i_ffs_rdev;
+
+ return 1;
+}
+
+int
+ext2fs_filestat(vp, fsp)
+ struct vnode *vp;
+ struct filestat *fsp;
+{
+ struct inode inode;
+
+ if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
+ dprintf("can't read inode at %p for pid %d", VTOI(vp), Pid);
+ return 0;
+ }
+ fsp->fsid = inode.i_dev & 0xffff;
+ fsp->fileid = (long)inode.i_number;
+ fsp->mode = (mode_t)inode.i_e2fs_mode;
+ fsp->size = inode.i_e2fs_size;
+ fsp->rdev = 0; /* XXX */
+ return 1;
+}
+
+int
+nfs_filestat(vp, fsp)
+ struct vnode *vp;
+ struct filestat *fsp;
+{
+ struct nfsnode nfsnode;
+ struct vattr va;
+ mode_t mode;
+
+ if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
+ dprintf("can't read nfsnode at %p for pid %d", VTONFS(vp),
+ Pid);
+ return 0;
+ }
+ if (!KVM_READ(nfsnode.n_vattr, &va, sizeof(va))) {
+ dprintf("can't read vnode attributes at %p for pid %d",
+ nfsnode.n_vattr, Pid);
+ return 0;
+ }
+ fsp->fsid = va.va_fsid;
+ fsp->fileid = va.va_fileid;
+ fsp->size = nfsnode.n_size;
+ fsp->rdev = va.va_rdev;
+ mode = (mode_t)va.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;
+ };
+ fsp->mode = mode;
+
+ return 1;
+}
+
+
+char *
+getmnton(m)
+ 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", m);
+ return (NULL);
+ }
+ if ((mt = malloc(sizeof (struct mtab))) == NULL) {
+ err(1, "malloc(%u)", (unsigned int)sizeof(struct mtab));
+ }
+ mt->m = m;
+ memmove(&mt->mntonname[0], &mount.mnt_stat.f_mntonname[0], MNAMELEN);
+ mt->next = mhead;
+ mhead = mt;
+ return (mt->mntonname);
+}
+
+void
+socktrans(sock, i)
+ struct socket *sock;
+ int i;
+{
+ static char *stypename[] = {
+ "unused", /* 0 */
+ "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];
+
+ PREFIX(i);
+
+ /* fill in socket */
+ if (!KVM_READ(sock, &so, sizeof(struct socket))) {
+ dprintf("can't read sock at %p", sock);
+ goto bad;
+ }
+
+ /* fill in protosw entry */
+ if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
+ dprintf("can't read protosw at %p", so.so_proto);
+ goto bad;
+ }
+
+ /* fill in domain */
+ if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
+ dprintf("can't read domain at %p", proto.pr_domain);
+ goto bad;
+ }
+
+ if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
+ sizeof(dname) - 1)) != sizeof(dname) -1) {
+ dprintf("can't read domain name at %p", dom.dom_name);
+ dname[0] = '\0';
+ }
+ else
+ dname[len] = '\0';
+
+ 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) {
+ case AF_INET:
+ getinetproto(proto.pr_protocol);
+ if (proto.pr_protocol == IPPROTO_TCP) {
+ if (so.so_pcb == NULL)
+ break;
+ if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
+ sizeof(struct inpcb)) != sizeof(struct inpcb)) {
+ dprintf("can't read inpcb at %p", so.so_pcb);
+ goto bad;
+ }
+ printf(" %lx", (long)inpcb.inp_ppcb);
+ printf(" %s:%d",
+ inpcb.inp_laddr.s_addr == INADDR_ANY ? "*" :
+ inet_ntoa(inpcb.inp_laddr), ntohs(inpcb.inp_lport));
+ if (inpcb.inp_fport) {
+ printf(" <-> ");
+ printf("%s:%d",
+ inpcb.inp_faddr.s_addr == INADDR_ANY ? "*" :
+ inet_ntoa(inpcb.inp_faddr),
+ ntohs(inpcb.inp_fport));
+ }
+ } else if (proto.pr_protocol == IPPROTO_UDP) {
+ if (so.so_pcb == NULL)
+ break;
+ if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
+ sizeof(struct inpcb)) != sizeof(struct inpcb)) {
+ dprintf("can't read inpcb at %p", so.so_pcb);
+ goto bad;
+ }
+ printf(" %lx", (long)so.so_pcb);
+ printf(" %s:%d",
+ inpcb.inp_laddr.s_addr == INADDR_ANY ? "*" :
+ inet_ntoa(inpcb.inp_laddr), ntohs(inpcb.inp_lport));
+ if (inpcb.inp_fport)
+ printf(" <-> %s:%d",
+ inpcb.inp_faddr.s_addr == INADDR_ANY ? "*" :
+ inet_ntoa(inpcb.inp_faddr),
+ ntohs(inpcb.inp_fport));
+ } else if (so.so_pcb)
+ printf(" %lx", (long)so.so_pcb);
+ break;
+ case AF_LOCAL:
+ /* print address of pcb and connected pcb */
+ if (so.so_pcb) {
+ printf(" %lx", (long)so.so_pcb);
+ if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb,
+ sizeof(struct unpcb)) != sizeof(struct unpcb)){
+ dprintf("can't read unpcb at %p", so.so_pcb);
+ goto bad;
+ }
+ if (unpcb.unp_conn) {
+ char shoconn[4], *cp;
+
+ cp = shoconn;
+ if (!(so.so_state & SS_CANTRCVMORE))
+ *cp++ = '<';
+ *cp++ = '-';
+ if (!(so.so_state & SS_CANTSENDMORE))
+ *cp++ = '>';
+ *cp = '\0';
+ printf(" %s %lx", shoconn,
+ (long)unpcb.unp_conn);
+ }
+ }
+ break;
+ default:
+ /* print protocol number and socket address */
+ printf(" %d %lx", proto.pr_protocol, (long)sock);
+ }
+ printf("\n");
+ return;
+bad:
+ printf("* error\n");
+}
+
+/*
+ * getinetproto --
+ * print name of protocol number
+ */
+void
+getinetproto(number)
+ int number;
+{
+ char *cp;
+
+ switch (number) {
+ case IPPROTO_IP:
+ cp = "ip"; break;
+ case IPPROTO_ICMP:
+ cp ="icmp"; break;
+ case IPPROTO_GGP:
+ cp ="ggp"; break;
+ case IPPROTO_TCP:
+ cp ="tcp"; break;
+ case IPPROTO_EGP:
+ cp ="egp"; break;
+ case IPPROTO_PUP:
+ cp ="pup"; break;
+ case IPPROTO_UDP:
+ cp ="udp"; break;
+ case IPPROTO_IDP:
+ cp ="idp"; break;
+ case IPPROTO_RAW:
+ cp ="raw"; break;
+ default:
+ printf(" %d", number);
+ return;
+ }
+ printf(" %s", cp);
+}
+
+int
+getfname(filename)
+ char *filename;
+{
+ struct stat statbuf;
+ DEVS *cur;
+
+ if (stat(filename, &statbuf)) {
+ warn("stat(%s)", filename);
+ return(0);
+ }
+ if ((cur = malloc(sizeof(DEVS))) == NULL) {
+ err(1, "malloc(%u)", (unsigned int)sizeof(DEVS));
+ }
+ cur->next = devs;
+ devs = cur;
+
+ cur->ino = statbuf.st_ino;
+ cur->fsid = statbuf.st_dev & 0xffff;
+ cur->name = filename;
+ return(1);
+}
+
+void
+usage()
+{
+ errx(1,
+ "usage: fstat [-fnv] [-p pid] [-u user] [-N system] [-M core] [file ...]\n");
+}
View
55 netbsd/usr.bin/fstat/fstat.h
@@ -0,0 +1,55 @@
+/* $NetBSD: fstat.h,v 1.1 1999/02/18 06:09:25 lukem Exp $ */
+/*-
+ * 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.
+ */
+
+struct filestat {
+ long fsid;
+ long fileid;
+ mode_t mode;
+ off_t size;
+ dev_t rdev;
+};
+
+/*
+ * a kvm_read that returns true if everything is read
+ */
+#define KVM_READ(kaddr, paddr, len) \
+ (kvm_read(kd, (u_long)(kaddr), (void *)(paddr), (len)) == (len))
+
+extern kvm_t *kd;
+extern int vflg;
+extern pid_t Pid;
+
+#define dprintf if (vflg) warnx
+
+int isofs_filestat __P((struct vnode *, struct filestat *));
View
97 netbsd/usr.bin/fstat/isofs.c
@@ -0,0 +1,97 @@
+/* $NetBSD: isofs.c,v 1.1 1999/02/18 06:09:25 lukem Exp $ */
+/*-
+ * Copyright (c) 1988, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by the University of
+ * California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__RCSID("$NetBSD: isofs.c,v 1.1 1999/02/18 06:09:25 lukem Exp $");
+
+#include <sys/param.h>
+#include <sys/time.h>
+#include <sys/proc.h>
+#include <sys/user.h>
+#include <sys/stat.h>
+#include <sys/vnode.h>
+#include <sys/socket.h>
+#include <sys/socketvar.h>
+#include <sys/domain.h>
+#include <sys/protosw.h>
+#include <sys/unpcb.h>
+#include <sys/sysctl.h>
+#include <sys/filedesc.h>
+#define _KERNEL
+#include <sys/file.h>
+#include <sys/mount.h>
+#undef _KERNEL
+#define NFS
+#include <nfs/nfsproto.h>
+#include <nfs/rpcv2.h>
+#include <nfs/nfs.h>
+#include <nfs/nfsnode.h>
+#undef NFS
+
+#include <isofs/cd9660/iso.h>
+#include <isofs/cd9660/cd9660_extern.h>
+#include <isofs/cd9660/cd9660_node.h>
+
+#include <ctype.h>
+#include <errno.h>
+#include <kvm.h>
+#include <limits.h>
+#include <nlist.h>
+#include <paths.h>
+#include <pwd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <err.h>
+#include "fstat.h"
+
+int
+isofs_filestat(vp, fsp)
+ struct vnode *vp;
+ struct filestat *fsp;
+{
+ struct iso_node inode;
+
+ if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
+ dprintf("can't read inode at %p for pid %d", VTOI(vp), Pid);
+ return 0;
+ }
+ fsp->fsid = inode.i_dev & 0xffff;
+ fsp->fileid = (long)inode.i_number;
+ fsp->mode = inode.inode.iso_mode;
+ fsp->size = inode.i_size;
+ fsp->rdev = inode.i_dev;
+ return 1;
+}
Please sign in to comment.
Something went wrong with that request. Please try again.