add confSORT_QUEUE_BY_HOST and confBROKEN_SMTP_PEERS
[unix-history] / usr / src / usr.bin / fstat / fstat.c
index 0c21a3c..7d92a85 100644 (file)
@@ -1,48 +1,45 @@
 /*-
 /*-
- * Copyright (c) 1988 The Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1988, 1993
+ *     The Regents of the University of California.  All rights reserved.
  *
  * %sccs.include.redist.c%
  */
 
 #ifndef lint
  *
  * %sccs.include.redist.c%
  */
 
 #ifndef lint
-char copyright[] =
-"@(#) Copyright (c) 1988 The Regents of the University of California.\n\
- All rights reserved.\n";
+static char copyright[] =
+"@(#) Copyright (c) 1988, 1993\n\
      The Regents of the University of California.  All rights reserved.\n";
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)fstat.c    5.26 (Berkeley) %G%";
+static char sccsid[] = "@(#)fstat.c    8.1 (Berkeley) %G%";
 #endif /* not lint */
 
 #endif /* not lint */
 
-/*
- *  fstat 
- */
-#include <machine/pte.h>
-
 #include <sys/param.h>
 #include <sys/param.h>
-#include <sys/user.h>
+#include <sys/time.h>
 #include <sys/proc.h>
 #include <sys/proc.h>
-#include <sys/text.h>
+#include <sys/user.h>
 #include <sys/stat.h>
 #include <sys/stat.h>
-#include <sys/time.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/vnode.h>
 #include <sys/socket.h>
 #include <sys/socketvar.h>
 #include <sys/domain.h>
 #include <sys/protosw.h>
 #include <sys/unpcb.h>
-#include <sys/vmmac.h>
+#include <sys/sysctl.h>
+#include <sys/filedesc.h>
 #define        KERNEL
 #define        KERNEL
-#define NFS
 #include <sys/file.h>
 #include <sys/file.h>
+#include <ufs/ufs/quota.h>
+#include <ufs/ufs/inode.h>
+#undef KERNEL
+#define NFS
 #include <sys/mount.h>
 #include <sys/mount.h>
-#include <ufs/quota.h>
-#include <ufs/inode.h>
 #include <nfs/nfsv2.h>
 #include <nfs/nfsv2.h>
+#include <nfs/rpcv2.h>
 #include <nfs/nfs.h>
 #include <nfs/nfsnode.h>
 #include <nfs/nfs.h>
 #include <nfs/nfsnode.h>
-#undef KERNEL
+#undef NFS
 
 #include <net/route.h>
 #include <netinet/in.h>
 
 #include <net/route.h>
 #include <netinet/in.h>
@@ -50,13 +47,15 @@ static char sccsid[] = "@(#)fstat.c 5.26 (Berkeley) %G%";
 #include <netinet/ip.h>
 #include <netinet/in_pcb.h>
 
 #include <netinet/ip.h>
 #include <netinet/in_pcb.h>
 
-#include <kvm.h>
-#include <paths.h>
 #include <ctype.h>
 #include <ctype.h>
+#include <errno.h>
+#include <kvm.h>
 #include <nlist.h>
 #include <nlist.h>
+#include <paths.h>
 #include <pwd.h>
 #include <pwd.h>
-#include <string.h>
 #include <stdio.h>
 #include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 
 #define        TEXT    -1
 #define        CDIR    -2
 
 #define        TEXT    -1
 #define        CDIR    -2
@@ -94,33 +93,69 @@ int vflg;   /* display errors in locating kernel data objects etc... */
 
 #define dprintf        if (vflg) fprintf
 
 
 #define dprintf        if (vflg) fprintf
 
-extern int errno;
-off_t lseek();
+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) { \
+                       fprintf(stderr, "fstat: %s\n", strerror(errno)); \
+                       exit(1); \
+               } \
+               maxfiles = (d); \
+       }
+
+/*
+ * a kvm_read that returns true if everything is read 
+ */
+#define KVM_READ(kaddr, paddr, len) \
+       (kvm_read(kd, (u_long)(kaddr), (char *)(paddr), (len)) == (len))
+
+kvm_t *kd;
+
+int ufs_filestat(), nfs_filestat();
+void dofiles(), getinetproto(), socktrans();
+void usage(), vtrans();
 
 main(argc, argv)
        int argc;
        char **argv;
 {
 
 main(argc, argv)
        int argc;
        char **argv;
 {
-       register struct passwd *passwd;
-       int what = KINFO_PROC_ALL, arg = 0;
-       struct passwd *getpwnam(), *getpwuid();
-       struct proc *p;
        extern char *optarg;
        extern int optind;
        extern char *optarg;
        extern int optind;
-       int ch;
-       char *malloc();
-
-
-       while ((ch = getopt(argc, argv, "p:u:fnv")) != EOF)
+       register struct passwd *passwd;
+       struct kinfo_proc *p, *plast;
+       int arg, ch, what;
+       char *memf, *nlistf;
+       int cnt;
+
+       arg = 0;
+       what = KERN_PROC_ALL;
+       nlistf = memf = NULL;
+       while ((ch = getopt(argc, argv, "fnp:u:vNM")) != EOF)
                switch((char)ch) {
                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)) {
                case 'p':
                        if (pflg++)
                                usage();
                        if (!isdigit(*optarg)) {
-                               fputs("fstat: -p option requires a process id.\n", stderr);
+                               fprintf(stderr,
+                                   "fstat: -p requires a process id\n");
                                usage();
                        }
                                usage();
                        }
-                       what = KINFO_PROC_PID;
+                       what = KERN_PROC_PID;
                        arg = atoi(optarg);
                        break;
                case 'u':
                        arg = atoi(optarg);
                        break;
                case 'u':
@@ -131,17 +166,11 @@ main(argc, argv)
                                    optarg);
                                exit(1);
                        }
                                    optarg);
                                exit(1);
                        }
-                       what = KINFO_PROC_UID;
+                       what = KERN_PROC_UID;
                        arg = passwd->pw_uid;
                        break;
                        arg = passwd->pw_uid;
                        break;
-               case 'f':
-                       fsflg++;
-                       break;
-               case 'n':
-                       nflg++;
-                       break;
                case 'v':
                case 'v':
-                       vflg++;
+                       vflg = 1;
                        break;
                case '?':
                default:
                        break;
                case '?':
                default:
@@ -156,6 +185,9 @@ main(argc, argv)
                if (!checkfile) /* file(s) specified, but none accessable */
                        exit(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)
        if (fsflg && !checkfile) {      
                /* -f with no files means use wd */
                if (getfname(".") == 0)
@@ -163,32 +195,40 @@ main(argc, argv)
                checkfile = 1;
        }
 
                checkfile = 1;
        }
 
-       /* modify the following to make work on dead kernels */
-       if (kvm_openfiles(NULL, NULL, NULL) == -1) {
-               fprintf(stderr, "fstat: %s\n", kvm_geterr());
+       /*
+        * Discard setgid privileges if not the running kernel so that bad
+        * guys can't print interesting stuff from kernel memory.
+        */
+       if (nlistf != NULL || memf != NULL)
+               setgid(getgid());
+
+       if ((kd = kvm_open(nlistf, memf, NULL, O_RDONLY, NULL)) == NULL) {
+               fprintf(stderr, "fstat: %s\n", kvm_geterr(kd));
                exit(1);
        }
 #ifdef notdef
                exit(1);
        }
 #ifdef notdef
-       if (kvm_nlist(nl) != 0) {
-               fprintf(stderr, "fstat: no namelist: %s\n", kvm_geterr());
+       if (kvm_nlist(kd, nl) != 0) {
+               fprintf(stderr, "fstat: no namelist: %s\n", kvm_geterr(kd));
                exit(1);
        }
 #endif
                exit(1);
        }
 #endif
-       if (kvm_getprocs(what, arg) == -1) {
-               fprintf(stderr, "fstat: %s\n", kvm_geterr());
+       if ((p = kvm_getprocs(kd, what, arg, &cnt)) == NULL) {
+               fprintf(stderr, "fstat: %s\n", kvm_geterr(kd));
                exit(1);
        }
        if (nflg)
                exit(1);
        }
        if (nflg)
-fputs("USER     CMD        PID   FD  DEV    INUM       MODE SZ|DV", stdout);
+               printf("%s",
+"USER     CMD          PID   FD  DEV    INUM       MODE SZ|DV R/W");
        else
        else
-fputs("USER     CMD        PID   FD MOUNT      INUM MODE         SZ|DV", stdout);
+               printf("%s",
+"USER     CMD          PID   FD MOUNT      INUM MODE         SZ|DV R/W");
        if (checkfile && fsflg == 0)
        if (checkfile && fsflg == 0)
-               fputs(" NAME\n", stdout);       
+               printf(" NAME\n");
        else
                putchar('\n');
 
        else
                putchar('\n');
 
-       while ((p = kvm_nextproc()) != NULL) {
-               if (p->p_stat == SZOMB)
+       for (plast = &p[cnt]; p < plast; ++p) {
+               if (p->kp_proc.p_stat == SZOMB)
                        continue;
                dofiles(p);
        }
                        continue;
                dofiles(p);
        }
@@ -198,19 +238,19 @@ fputs("USER     CMD        PID   FD MOUNT      INUM MODE         SZ|DV", stdout)
 char   *Uname, *Comm;
 int    Pid;
 
 char   *Uname, *Comm;
 int    Pid;
 
-#define PREFIX(i) printf("%-8.8s %-8.8s %5d", Uname, Comm, Pid); \
+#define PREFIX(i) printf("%-8.8s %-10s %5d", Uname, Comm, Pid); \
        switch(i) { \
        case TEXT: \
        switch(i) { \
        case TEXT: \
-               fputs(" text", stdout); \
+               printf(" text"); \
                break; \
        case CDIR: \
                break; \
        case CDIR: \
-               fputs("   wd", stdout); \
+               printf("   wd"); \
                break; \
        case RDIR: \
                break; \
        case RDIR: \
-               fputs(" root", stdout); \
+               printf(" root"); \
                break; \
        case TRACE: \
                break; \
        case TRACE: \
-               fputs("   tr", stdout); \
+               printf("   tr"); \
                break; \
        default: \
                printf(" %4d", i); \
                break; \
        default: \
                printf(" %4d", i); \
@@ -220,61 +260,73 @@ int       Pid;
 /*
  * print open files attributed to this process
  */
 /*
  * print open files attributed to this process
  */
-dofiles(p)
-       struct proc *p;
+void
+dofiles(kp)
+       struct kinfo_proc *kp;
 {
 {
-       int i;
+       int i, last;
        struct file file;
        struct file file;
-       struct user *up = kvm_getu(p);
-       struct vnode *xvptr;
+       struct filedesc0 filed0;
+#define        filed   filed0.fd_fd
+       struct proc *p = &kp->kp_proc;
+       struct eproc *ep = &kp->kp_eproc;
+
        extern char *user_from_uid();
 
        extern char *user_from_uid();
 
-       Uname = user_from_uid(p->p_uid, 0);
+       Uname = user_from_uid(ep->e_ucred.cr_uid, 0);
        Pid = p->p_pid;
        Comm = p->p_comm;
 
        Pid = p->p_pid;
        Comm = p->p_comm;
 
-       if (up == NULL) {
-               dprintf(stderr, "can't read u for pid %d\n", Pid);
+       if (p->p_fd == NULL)
+               return;
+       if (!KVM_READ(p->p_fd, &filed0, sizeof (filed0))) {
+               dprintf(stderr, "can't read filedesc at %x for pid %d\n",
+                       p->p_fd, Pid);
                return;
        }
        /*
         * root directory vnode, if one
         */
                return;
        }
        /*
         * root directory vnode, if one
         */
-       if (up->u_rdir)
-               vtrans(up->u_rdir, RDIR);
-       /*
-        * text vnode
-        */
-       if (p->p_textp && 
-           kvm_read(&(p->p_textp->x_vptr), &xvptr,
-           sizeof (struct vnode *)) == sizeof (struct vnode *) &&
-           xvptr != NULL)
-               vtrans(xvptr, TEXT);
+       if (filed.fd_rdir)
+               vtrans(filed.fd_rdir, RDIR, FREAD);
        /*
         * current working directory vnode
         */
        /*
         * current working directory vnode
         */
-       vtrans(up->u_cdir, CDIR);
+       vtrans(filed.fd_cdir, CDIR, FREAD);
        /*
         * ktrace vnode, if one
         */
        if (p->p_tracep)
        /*
         * ktrace vnode, if one
         */
        if (p->p_tracep)
-               vtrans(p->p_tracep, TRACE);
+               vtrans(p->p_tracep, TRACE, FREAD|FWRITE);
        /*
         * open files
         */
        /*
         * open files
         */
-       for (i = 0; i <= up->u_lastfile; i++) {
-               if (up->u_ofile[i] == 0)
+#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(stderr,
+                           "can't read file structures at %x for pid %d\n",
+                           filed.fd_ofiles, Pid);
+                       return;
+               }
+       } else
+               bcopy(filed0.fd_dfiles, ofiles, (filed.fd_lastfile+1) * FPSIZE);
+       for (i = 0; i <= filed.fd_lastfile; i++) {
+               if (ofiles[i] == NULL)
                        continue;
                        continue;
-               if (kvm_read(up->u_ofile[i], &file, sizeof (struct file)) !=
-                   sizeof (struct file)) {
-                       dprintf(stderr, "can't read file %d for pid %d\n",
-                               i, Pid);
+               if (!KVM_READ(ofiles[i], &file, sizeof (struct file))) {
+                       dprintf(stderr, "can't read file %d at %x for pid %d\n",
+                               i, ofiles[i], Pid);
                        continue;
                }
                if (file.f_type == DTYPE_VNODE)
                        continue;
                }
                if (file.f_type == DTYPE_VNODE)
-                       vtrans((struct vnode *)file.f_data, i);
-               else if (file.f_type == DTYPE_SOCKET && checkfile == 0)
-                       socktrans((struct socket *)file.f_data, i);
+                       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(stderr, 
                                "unknown file type %d for file %d of pid %d\n",
                else {
                        dprintf(stderr, 
                                "unknown file type %d for file %d of pid %d\n",
@@ -283,19 +335,19 @@ dofiles(p)
        }
 }
 
        }
 }
 
-vtrans(vp, i)
+void
+vtrans(vp, i, flag)
        struct vnode *vp;
        struct vnode *vp;
+       int i;
+       int flag;
 {
        struct vnode vn;
        struct filestat fst;
 {
        struct vnode vn;
        struct filestat fst;
-       char *filename = NULL;
-       char *badtype = NULL;
-       char *getmnton();
-       extern char *devname();
-       char mode[15];
-
-       if (kvm_read((off_t)vp, &vn, sizeof (struct vnode)) != 
-           sizeof (struct vnode)) {
+       char rw[3], mode[15];
+       char *badtype = NULL, *filename, *getmnton();
+
+       filename = badtype = NULL;
+       if (!KVM_READ(vp, &vn, sizeof (struct vnode))) {
                dprintf(stderr, "can't read vnode at %x for pid %d\n",
                        vp, Pid);
                return;
                dprintf(stderr, "can't read vnode at %x for pid %d\n",
                        vp, Pid);
                return;
@@ -307,13 +359,16 @@ vtrans(vp, i)
        else
                switch (vn.v_tag) {
                case VT_UFS:
        else
                switch (vn.v_tag) {
                case VT_UFS:
-                       ufs_filestat(&vn, &fst);
+                       if (!ufs_filestat(&vn, &fst))
+                               badtype = "error";
                        break;
                case VT_MFS:
                        break;
                case VT_MFS:
-                       ufs_filestat(&vn, &fst);
+                       if (!ufs_filestat(&vn, &fst))
+                               badtype = "error";
                        break;
                case VT_NFS:
                        break;
                case VT_NFS:
-                       nfs_filestat(&vn, &fst);
+                       if (!nfs_filestat(&vn, &fst))
+                               badtype = "error";
                        break;
                default: {
                        static char unknown[10];
                        break;
                default: {
                        static char unknown[10];
@@ -367,37 +422,56 @@ vtrans(vp, i)
        default:
                printf(" %6d", fst.size);
        }
        default:
                printf(" %6d", fst.size);
        }
+       rw[0] = '\0';
+       if (flag & FREAD)
+               strcat(rw, "r");
+       if (flag & FWRITE)
+               strcat(rw, "w");
+       printf(" %2s", rw);
        if (filename && !fsflg)
        if (filename && !fsflg)
-               printf(" %s", filename);
-               
+               printf("  %s", filename);
        putchar('\n');
 }
 
        putchar('\n');
 }
 
+int
 ufs_filestat(vp, fsp)
        struct vnode *vp;
        struct filestat *fsp;
 {
 ufs_filestat(vp, fsp)
        struct vnode *vp;
        struct filestat *fsp;
 {
-       struct inode *ip = VTOI(vp);
+       struct inode inode;
+
+       if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
+               dprintf(stderr, "can't read inode at %x for pid %d\n",
+                       VTOI(vp), Pid);
+               return 0;
+       }
+       fsp->fsid = inode.i_dev & 0xffff;
+       fsp->fileid = (long)inode.i_number;
+       fsp->mode = (mode_t)inode.i_mode;
+       fsp->size = (u_long)inode.i_size;
+       fsp->rdev = inode.i_rdev;
 
 
-       fsp->fsid = ip->i_dev & 0xffff;
-       fsp->fileid = (long)ip->i_number;
-       fsp->mode = (mode_t)ip->i_mode;
-       fsp->size = (u_long)ip->i_size;
-       fsp->rdev = ip->i_rdev;
+       return 1;
 }
 
 }
 
+int
 nfs_filestat(vp, fsp)
        struct vnode *vp;
        struct filestat *fsp;
 {
 nfs_filestat(vp, fsp)
        struct vnode *vp;
        struct filestat *fsp;
 {
-       register struct nfsnode *np = VTONFS(vp);
+       struct nfsnode nfsnode;
        register mode_t mode;
 
        register mode_t mode;
 
-       fsp->fsid = np->n_vattr.va_fsid;
-       fsp->fileid = np->n_vattr.va_fileid;
-       fsp->size = np->n_size;
-       fsp->rdev = np->n_vattr.va_rdev;
-       mode = (mode_t)np->n_vattr.va_mode;
+       if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
+               dprintf(stderr, "can't read nfsnode at %x for pid %d\n",
+                       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;
        switch (vp->v_type) {
        case VREG:
                mode |= S_IFREG;
@@ -422,6 +496,8 @@ nfs_filestat(vp, fsp)
                break;
        };
        fsp->mode = mode;
                break;
        };
        fsp->mode = mode;
+
+       return 1;
 }
 
 
 }
 
 
@@ -440,13 +516,12 @@ getmnton(m)
        for (mt = mhead; mt != NULL; mt = mt->next)
                if (m == mt->m)
                        return (mt->mntonname);
        for (mt = mhead; mt != NULL; mt = mt->next)
                if (m == mt->m)
                        return (mt->mntonname);
-       if (kvm_read((off_t)m, &mount, sizeof(struct mount)) != 
-           sizeof(struct mount)) {
+       if (!KVM_READ(m, &mount, sizeof(struct mount))) {
                fprintf(stderr, "can't read mount table at %x\n", m);
                return (NULL);
        }
                fprintf(stderr, "can't read mount table at %x\n", m);
                return (NULL);
        }
-       if ((mt = (struct mtab *)malloc(sizeof (struct mtab))) == NULL) {
-               fprintf(stderr, "out of memory\n");
+       if ((mt = malloc(sizeof (struct mtab))) == NULL) {
+               fprintf(stderr, "fstat: %s\n", strerror(errno));
                exit(1);
        }
        mt->m = m;
                exit(1);
        }
        mt->m = m;
@@ -456,8 +531,10 @@ getmnton(m)
        return (mt->mntonname);
 }
 
        return (mt->mntonname);
 }
 
+void
 socktrans(sock, i)
        struct socket *sock;
 socktrans(sock, i)
        struct socket *sock;
+       int i;
 {
        static char *stypename[] = {
                "unused",       /* 0 */
 {
        static char *stypename[] = {
                "unused",       /* 0 */
@@ -479,41 +556,31 @@ socktrans(sock, i)
        PREFIX(i);
 
        /* fill in socket */
        PREFIX(i);
 
        /* fill in socket */
-       if (kvm_read((off_t)sock, (char *)&so, sizeof(struct socket))
-           != sizeof(struct socket)) {
+       if (!KVM_READ(sock, &so, sizeof(struct socket))) {
                dprintf(stderr, "can't read sock at %x\n", sock);
                goto bad;
        }
 
        /* fill in protosw entry */
                dprintf(stderr, "can't read sock at %x\n", sock);
                goto bad;
        }
 
        /* fill in protosw entry */
-       if (kvm_read((off_t)so.so_proto, (char *)&proto, sizeof(struct protosw))
-           != sizeof(struct protosw)) {
+       if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
                dprintf(stderr, "can't read protosw at %x", so.so_proto);
                goto bad;
        }
 
        /* fill in domain */
                dprintf(stderr, "can't read protosw at %x", so.so_proto);
                goto bad;
        }
 
        /* fill in domain */
-       if (kvm_read((off_t)proto.pr_domain, (char *)&dom, sizeof(struct domain))
-           != sizeof(struct domain)) {
+       if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
                dprintf(stderr, "can't read domain at %x\n", proto.pr_domain);
                goto bad;
        }
 
                dprintf(stderr, "can't read domain at %x\n", proto.pr_domain);
                goto bad;
        }
 
-       /*
-        * grab domain name
-        * kludge "internet" --> "inet" for brevity
-        */
-       if (dom.dom_family == AF_INET)
-               strcpy(dname, "inet");
-       else {
-               if ((len = kvm_read((off_t)dom.dom_name, dname, sizeof(dname) - 1)) < 0) {
-                       dprintf(stderr, "can't read domain name at %x\n",
-                               dom.dom_name);
-                       dname[0] = '\0';
-               }
-               else
-                       dname[len] = '\0';
+       if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
+           sizeof(dname) - 1)) < 0) {
+               dprintf(stderr, "can't read domain name at %x\n",
+                       dom.dom_name);
+               dname[0] = '\0';
        }
        }
+       else
+               dname[len] = '\0';
 
        if ((u_short)so.so_type > STYPEMAX)
                printf("* %s ?%d", dname, so.so_type);
 
        if ((u_short)so.so_type > STYPEMAX)
                printf("* %s ?%d", dname, so.so_type);
@@ -536,10 +603,12 @@ socktrans(sock, i)
                getinetproto(proto.pr_protocol);
                if (proto.pr_protocol == IPPROTO_TCP ) {
                        if (so.so_pcb) {
                getinetproto(proto.pr_protocol);
                if (proto.pr_protocol == IPPROTO_TCP ) {
                        if (so.so_pcb) {
-                               if (kvm_read((off_t)so.so_pcb, (char *)&inpcb, sizeof(struct inpcb))
-                                   != sizeof(struct inpcb)){
+                               if (kvm_read(kd, (u_long)so.so_pcb,
+                                   (char *)&inpcb, sizeof(struct inpcb))
+                                   != sizeof(struct inpcb)) {
                                        dprintf(stderr, 
                                        dprintf(stderr, 
-                                            "can't read inpcb at %x\n", so.so_pcb);
+                                           "can't read inpcb at %x\n",
+                                           so.so_pcb);
                                        goto bad;
                                }
                                printf(" %x", (int)inpcb.inp_ppcb);
                                        goto bad;
                                }
                                printf(" %x", (int)inpcb.inp_ppcb);
@@ -552,10 +621,10 @@ socktrans(sock, i)
                /* print address of pcb and connected pcb */
                if (so.so_pcb) {
                        printf(" %x", (int)so.so_pcb);
                /* print address of pcb and connected pcb */
                if (so.so_pcb) {
                        printf(" %x", (int)so.so_pcb);
-                       if (kvm_read((off_t)so.so_pcb, (char *)&unpcb, sizeof(struct unpcb))
-                           != sizeof(struct unpcb)){
+                       if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb,
+                           sizeof(struct unpcb)) != sizeof(struct unpcb)){
                                dprintf(stderr, "can't read unpcb at %x\n",
                                dprintf(stderr, "can't read unpcb at %x\n",
-                                       so.so_pcb);
+                                   so.so_pcb);
                                goto bad;
                        }
                        if (unpcb.unp_conn) {
                                goto bad;
                        }
                        if (unpcb.unp_conn) {
@@ -587,6 +656,7 @@ bad:
  * getinetproto --
  *     print name of protocol number
  */
  * getinetproto --
  *     print name of protocol number
  */
+void
 getinetproto(number)
        int number;
 {
 getinetproto(number)
        int number;
 {
@@ -623,15 +693,13 @@ getfname(filename)
 {
        struct stat statbuf;
        DEVS *cur;
 {
        struct stat statbuf;
        DEVS *cur;
-       char *malloc();
 
        if (stat(filename, &statbuf)) {
 
        if (stat(filename, &statbuf)) {
-               fprintf(stderr, "fstat: %s: %s\n", strerror(errno),
-                   filename);
+               fprintf(stderr, "fstat: %s: %s\n", filename, strerror(errno));
                return(0);
        }
                return(0);
        }
-       if ((cur = (DEVS *)malloc(sizeof(DEVS))) == NULL) {
-               fprintf(stderr, "fstat: out of space.\n");
+       if ((cur = malloc(sizeof(DEVS))) == NULL) {
+               fprintf(stderr, "fstat: %s\n", strerror(errno));
                exit(1);
        }
        cur->next = devs;
                exit(1);
        }
        cur->next = devs;
@@ -643,9 +711,10 @@ getfname(filename)
        return(1);
 }
 
        return(1);
 }
 
+void
 usage()
 {
        (void)fprintf(stderr,
 usage()
 {
        (void)fprintf(stderr,
          "usage: fstat [-fnv] [-p pid] [-u user] [filename ...]\n");
"usage: fstat [-fnv] [-p pid] [-u user] [-N system] [-M core] [file ...]\n");
        exit(1);
 }
        exit(1);
 }