[BACK]Return to fstat.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / fstat

Annotation of src/usr.bin/fstat/fstat.c, Revision 1.61

1.61    ! thib        1: /*     $OpenBSD: fstat.c,v 1.60 2007/10/01 22:06:02 sobrado Exp $      */
1.2       niklas      2:
1.1       deraadt     3: /*-
                      4:  * Copyright (c) 1988, 1993
                      5:  *     The Regents of the University of California.  All rights reserved.
                      6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
1.43      millert    15:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    16:  *    may be used to endorse or promote products derived from this software
                     17:  *    without specific prior written permission.
                     18:  *
                     19:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     20:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     21:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     22:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     23:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     24:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     25:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     26:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     27:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     28:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     29:  * SUCH DAMAGE.
                     30:  */
                     31:
                     32: #ifndef lint
                     33: static char copyright[] =
                     34: "@(#) Copyright (c) 1988, 1993\n\
                     35:        The Regents of the University of California.  All rights reserved.\n";
                     36: #endif /* not lint */
                     37:
                     38: #ifndef lint
                     39: /*static char sccsid[] = "from: @(#)fstat.c    8.1 (Berkeley) 6/6/93";*/
1.61    ! thib       40: static char *rcsid = "$OpenBSD: fstat.c,v 1.60 2007/10/01 22:06:02 sobrado Exp $";
1.1       deraadt    41: #endif /* not lint */
                     42:
                     43: #include <sys/param.h>
                     44: #include <sys/time.h>
                     45: #include <sys/proc.h>
                     46: #include <sys/user.h>
                     47: #include <sys/stat.h>
                     48: #include <sys/vnode.h>
                     49: #include <sys/socket.h>
                     50: #include <sys/socketvar.h>
                     51: #include <sys/domain.h>
                     52: #include <sys/protosw.h>
1.37      deraadt    53: #include <sys/event.h>
                     54: #include <sys/eventvar.h>
1.1       deraadt    55: #include <sys/unpcb.h>
                     56: #include <sys/sysctl.h>
                     57: #include <sys/filedesc.h>
1.42      tedu       58: #define        _KERNEL
1.23      deraadt    59: #include <sys/mount.h>
1.37      deraadt    60: #include <crypto/cryptodev.h>
1.38      deraadt    61: #include <dev/systrace.h>
1.1       deraadt    62: #include <sys/file.h>
                     63: #include <ufs/ufs/quota.h>
                     64: #include <ufs/ufs/inode.h>
                     65: #undef _KERNEL
                     66: #define NFS
1.2       niklas     67: #include <nfs/nfsproto.h>
1.1       deraadt    68: #include <nfs/rpcv2.h>
                     69: #include <nfs/nfs.h>
                     70: #include <nfs/nfsnode.h>
                     71: #undef NFS
                     72:
1.29      deraadt    73: #include <xfs/xfs_config.h>
1.18      art        74: #include <xfs/xfs_node.h>
                     75:
1.61    ! thib       76: #include <miscfs/specfs/specdev.h>
        !            77:
1.1       deraadt    78: #include <net/route.h>
                     79: #include <netinet/in.h>
                     80: #include <netinet/in_systm.h>
                     81: #include <netinet/ip.h>
                     82: #include <netinet/in_pcb.h>
                     83:
1.26      itojun     84: #ifdef INET6
                     85: #include <netinet/ip6.h>
                     86: #include <netinet6/ip6_var.h>
                     87: #endif
                     88:
                     89: #include <netdb.h>
1.5       deraadt    90: #include <arpa/inet.h>
                     91:
1.22      art        92: #include <sys/pipe.h>
                     93:
1.1       deraadt    94: #include <ctype.h>
                     95: #include <errno.h>
                     96: #include <kvm.h>
1.2       niklas     97: #include <limits.h>
1.1       deraadt    98: #include <nlist.h>
                     99: #include <paths.h>
                    100: #include <pwd.h>
                    101: #include <stdio.h>
                    102: #include <stdlib.h>
                    103: #include <string.h>
1.11      millert   104: #include <unistd.h>
1.24      deraadt   105: #include <netdb.h>
1.17      mickey    106: #include <err.h>
1.14      deraadt   107: #include "fstat.h"
1.1       deraadt   108:
                    109: #define        TEXT    -1
                    110: #define        CDIR    -2
                    111: #define        RDIR    -3
                    112: #define        TRACE   -4
                    113:
                    114: typedef struct devs {
                    115:        struct  devs *next;
                    116:        long    fsid;
                    117:        ino_t   ino;
                    118:        char    *name;
                    119: } DEVS;
                    120: DEVS *devs;
                    121:
1.46      deraadt   122: int    fsflg;  /* show files on same filesystem as file(s) argument */
                    123: int    pflg;   /* show files open by a particular pid */
                    124: int    uflg;   /* show files open by a particular (effective) user */
1.38      deraadt   125: int    checkfile; /* true if restricting to particular files or filesystems */
1.1       deraadt   126: int    nflg;   /* (numerical) display f.s. and rdev as dev_t */
1.28      hugh      127: int    oflg;   /* display file offset */
1.56      mickey    128: int    sflg;   /* display file xfer/bytes counters */
1.1       deraadt   129: int    vflg;   /* display errors in locating kernel data objects etc... */
                    130:
                    131: struct file **ofiles;  /* buffer of pointers to file structures */
                    132: int maxfiles;
                    133: #define ALLOC_OFILES(d)        \
                    134:        if ((d) > maxfiles) { \
                    135:                free(ofiles); \
1.59      deraadt   136:                ofiles = calloc((d), sizeof(struct file *)); \
1.17      mickey    137:                if (ofiles == NULL) \
                    138:                        err(1, "malloc"); \
1.1       deraadt   139:                maxfiles = (d); \
                    140:        }
                    141:
                    142: /*
1.34      deraadt   143:  * a kvm_read that returns true if everything is read
1.1       deraadt   144:  */
                    145: #define KVM_READ(kaddr, paddr, len) \
1.11      millert   146:        (kvm_read(kd, (u_long)(kaddr), (void *)(paddr), (len)) == (len))
1.1       deraadt   147:
                    148: kvm_t *kd;
                    149:
1.35      millert   150: int ufs_filestat(struct vnode *, struct filestat *);
                    151: int ext2fs_filestat(struct vnode *, struct filestat *);
                    152: int isofs_filestat(struct vnode *, struct filestat *);
                    153: int msdos_filestat(struct vnode *, struct filestat *);
                    154: int nfs_filestat(struct vnode *, struct filestat *);
                    155: int xfs_filestat(struct vnode *, struct filestat *);
1.61    ! thib      156: int spec_filestat(struct vnode *, struct filestat *);
1.49      millert   157: void dofiles(struct kinfo_proc2 *);
1.35      millert   158: void getinetproto(int);
                    159: void usage(void);
                    160: int getfname(char *);
1.56      mickey    161: void socktrans(struct socket *, int, struct file *);
                    162: void vtrans(struct vnode *, int, int, struct file *);
                    163: void pipetrans(struct pipe *, int, struct file *);
                    164: void kqueuetrans(struct kqueue *, int, struct file *);
                    165: void cryptotrans(void *, int, struct file *);
                    166: void systracetrans(struct fsystrace *, int, struct file *);
1.47      deraadt   167: char *getmnton(struct mount *);
                    168: const char *inet6_addrstr(struct in6_addr *);
1.1       deraadt   169:
1.11      millert   170: int
1.40      deraadt   171: main(int argc, char *argv[])
1.1       deraadt   172: {
                    173:        extern char *optarg;
                    174:        extern int optind;
1.32      mpech     175:        struct passwd *passwd;
1.49      millert   176:        struct kinfo_proc2 *p, *plast;
1.1       deraadt   177:        int arg, ch, what;
                    178:        char *memf, *nlistf;
1.2       niklas    179:        char buf[_POSIX2_LINE_MAX];
1.58      tedu      180:        const char *errstr;
1.1       deraadt   181:        int cnt;
1.52      djm       182:        gid_t gid;
1.1       deraadt   183:
                    184:        arg = 0;
                    185:        what = KERN_PROC_ALL;
                    186:        nlistf = memf = NULL;
1.28      hugh      187:        oflg = 0;
1.56      mickey    188:        while ((ch = getopt(argc, argv, "fnop:su:vN:M:")) != -1)
1.60      sobrado   189:                switch ((char)ch) {
1.1       deraadt   190:                case 'f':
                    191:                        fsflg = 1;
                    192:                        break;
                    193:                case 'M':
                    194:                        memf = optarg;
                    195:                        break;
                    196:                case 'N':
                    197:                        nlistf = optarg;
                    198:                        break;
                    199:                case 'n':
                    200:                        nflg = 1;
                    201:                        break;
1.28      hugh      202:                case 'o':
                    203:                        oflg = 1;
                    204:                        break;
1.1       deraadt   205:                case 'p':
                    206:                        if (pflg++)
                    207:                                usage();
1.58      tedu      208:                        arg = strtonum(optarg, 0, INT_MAX, &errstr);
                    209:                        if (errstr != NULL) {
                    210:                                warnx("-p requires a process id, %s: %s",
                    211:                                        errstr, optarg);
1.1       deraadt   212:                                usage();
                    213:                        }
                    214:                        what = KERN_PROC_PID;
                    215:                        break;
1.56      mickey    216:                case 's':
                    217:                        sflg = 1;
                    218:                        break;
1.1       deraadt   219:                case 'u':
                    220:                        if (uflg++)
                    221:                                usage();
1.17      mickey    222:                        if (!(passwd = getpwnam(optarg)))
1.21      deraadt   223:                                errx(1, "%s: unknown uid", optarg);
1.1       deraadt   224:                        what = KERN_PROC_UID;
                    225:                        arg = passwd->pw_uid;
                    226:                        break;
                    227:                case 'v':
                    228:                        vflg = 1;
                    229:                        break;
                    230:                default:
                    231:                        usage();
                    232:                }
                    233:
1.41      deraadt   234:        /*
                    235:         * Discard setgid privileges if not the running kernel so that bad
                    236:         * guys can't print interesting stuff from kernel memory.
                    237:         */
1.52      djm       238:        gid = getgid();
                    239:        if (nlistf != NULL || memf != NULL)
                    240:                if (setresgid(gid, gid, gid) == -1)
                    241:                        err(1, "setresgid");
1.41      deraadt   242:
                    243:        if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL)
                    244:                errx(1, "%s", buf);
                    245:
1.52      djm       246:        if (nlistf == NULL && memf == NULL)
                    247:                if (setresgid(gid, gid, gid) == -1)
                    248:                        err(1, "setresgid");
1.41      deraadt   249:
1.1       deraadt   250:        if (*(argv += optind)) {
                    251:                for (; *argv; ++argv) {
                    252:                        if (getfname(*argv))
                    253:                                checkfile = 1;
                    254:                }
1.30      pvalchev  255:                if (!checkfile) /* file(s) specified, but none accessible */
1.1       deraadt   256:                        exit(1);
                    257:        }
                    258:
                    259:        ALLOC_OFILES(256);      /* reserve space for file pointers */
                    260:
1.34      deraadt   261:        if (fsflg && !checkfile) {
1.1       deraadt   262:                /* -f with no files means use wd */
                    263:                if (getfname(".") == 0)
                    264:                        exit(1);
                    265:                checkfile = 1;
                    266:        }
1.16      deraadt   267:
1.49      millert   268:        if ((p = kvm_getproc2(kd, what, arg, sizeof(*p), &cnt)) == NULL)
1.21      deraadt   269:                errx(1, "%s", kvm_geterr(kd));
1.1       deraadt   270:        if (nflg)
                    271:                printf("%s",
1.60      sobrado   272: "USER     CMD          PID   FD  DEV      INUM       MODE R/W    SZ|DV");
1.1       deraadt   273:        else
                    274:                printf("%s",
1.60      sobrado   275: "USER     CMD          PID   FD MOUNT        INUM MODE       R/W    SZ|DV");
1.28      hugh      276:        if (oflg)
                    277:                printf("%s", ":OFFSET  ");
1.1       deraadt   278:        if (checkfile && fsflg == 0)
1.56      mickey    279:                printf(" NAME");
                    280:        if (sflg)
                    281:                printf("    XFERS   KBYTES");
                    282:        putchar('\n');
1.1       deraadt   283:
                    284:        for (plast = &p[cnt]; p < plast; ++p) {
1.49      millert   285:                if (p->p_stat == SZOMB)
1.1       deraadt   286:                        continue;
                    287:                dofiles(p);
                    288:        }
                    289:        exit(0);
                    290: }
                    291:
                    292: char   *Uname, *Comm;
1.14      deraadt   293: pid_t  Pid;
1.1       deraadt   294:
1.34      deraadt   295: #define PREFIX(i) do { \
1.39      mpech     296:        printf("%-8.8s %-10s %5ld", Uname, Comm, (long)Pid); \
1.60      sobrado   297:        switch (i) { \
1.1       deraadt   298:        case TEXT: \
                    299:                printf(" text"); \
                    300:                break; \
                    301:        case CDIR: \
                    302:                printf("   wd"); \
                    303:                break; \
                    304:        case RDIR: \
                    305:                printf(" root"); \
                    306:                break; \
                    307:        case TRACE: \
                    308:                printf("   tr"); \
                    309:                break; \
                    310:        default: \
                    311:                printf(" %4d", i); \
                    312:                break; \
1.34      deraadt   313:        } \
                    314: } while (0)
1.1       deraadt   315:
                    316: /*
                    317:  * print open files attributed to this process
                    318:  */
                    319: void
1.49      millert   320: dofiles(struct kinfo_proc2 *kp)
1.1       deraadt   321: {
1.11      millert   322:        int i;
1.1       deraadt   323:        struct file file;
                    324:        struct filedesc0 filed0;
                    325: #define        filed   filed0.fd_fd
                    326:
1.49      millert   327:        Uname = user_from_uid(kp->p_uid, 0);
                    328:        Pid = kp->p_pid;
                    329:        Comm = kp->p_comm;
1.1       deraadt   330:
1.49      millert   331:        if (kp->p_fd == 0)
1.1       deraadt   332:                return;
1.49      millert   333:        if (!KVM_READ(kp->p_fd, &filed0, sizeof (filed0))) {
1.39      mpech     334:                dprintf("can't read filedesc at %p for pid %ld",
1.49      millert   335:                    (void *)(u_long)kp->p_fd, (long)Pid);
1.1       deraadt   336:                return;
                    337:        }
                    338:        if (filed.fd_nfiles < 0 || filed.fd_lastfile >= filed.fd_nfiles ||
                    339:            filed.fd_freefile > filed.fd_lastfile + 1) {
1.39      mpech     340:                dprintf("filedesc corrupted at %p for pid %ld",
1.49      millert   341:                    (void *)(u_long)kp->p_fd, (long)Pid);
1.1       deraadt   342:                return;
                    343:        }
                    344:        /*
                    345:         * root directory vnode, if one
                    346:         */
                    347:        if (filed.fd_rdir)
1.56      mickey    348:                vtrans(filed.fd_rdir, RDIR, FREAD, NULL);
1.1       deraadt   349:        /*
                    350:         * current working directory vnode
                    351:         */
1.56      mickey    352:        vtrans(filed.fd_cdir, CDIR, FREAD, NULL);
                    353:
1.1       deraadt   354:        /*
                    355:         * ktrace vnode, if one
                    356:         */
1.49      millert   357:        if (kp->p_tracep)
1.56      mickey    358:                vtrans((struct vnode *)(u_long)kp->p_tracep, TRACE, FREAD|FWRITE, NULL);
1.1       deraadt   359:        /*
                    360:         * open files
                    361:         */
                    362: #define FPSIZE (sizeof (struct file *))
                    363:        ALLOC_OFILES(filed.fd_lastfile+1);
                    364:        if (filed.fd_nfiles > NDFILE) {
                    365:                if (!KVM_READ(filed.fd_ofiles, ofiles,
                    366:                    (filed.fd_lastfile+1) * FPSIZE)) {
1.39      mpech     367:                        dprintf("can't read file structures at %p for pid %ld",
                    368:                            filed.fd_ofiles, (long)Pid);
1.1       deraadt   369:                        return;
                    370:                }
                    371:        } else
                    372:                bcopy(filed0.fd_dfiles, ofiles, (filed.fd_lastfile+1) * FPSIZE);
                    373:        for (i = 0; i <= filed.fd_lastfile; i++) {
                    374:                if (ofiles[i] == NULL)
                    375:                        continue;
                    376:                if (!KVM_READ(ofiles[i], &file, sizeof (struct file))) {
1.39      mpech     377:                        dprintf("can't read file %d at %p for pid %ld",
                    378:                                i, ofiles[i], (long)Pid);
1.1       deraadt   379:                        continue;
                    380:                }
                    381:                if (file.f_type == DTYPE_VNODE)
1.38      deraadt   382:                        vtrans((struct vnode *)file.f_data, i, file.f_flag,
1.56      mickey    383:                            &file);
1.1       deraadt   384:                else if (file.f_type == DTYPE_SOCKET) {
                    385:                        if (checkfile == 0)
1.56      mickey    386:                                socktrans((struct socket *)file.f_data, i,
                    387:                                    &file);
1.22      art       388:                } else if (file.f_type == DTYPE_PIPE) {
                    389:                        if (checkfile == 0)
1.56      mickey    390:                                pipetrans((struct pipe *)file.f_data, i, &file);
1.37      deraadt   391:                } else if (file.f_type == DTYPE_KQUEUE) {
                    392:                        if (checkfile == 0)
1.56      mickey    393:                                kqueuetrans((struct kqueue *)file.f_data, i,
                    394:                                    &file);
1.37      deraadt   395:                } else if (file.f_type == DTYPE_CRYPTO) {
                    396:                        if (checkfile == 0)
1.56      mickey    397:                                cryptotrans(file.f_data, i, &file);
1.37      deraadt   398:                } else if (file.f_type == DTYPE_SYSTRACE) {
                    399:                        if (checkfile == 0)
1.56      mickey    400:                                systracetrans((struct fsystrace *)file.f_data,
                    401:                                    i, &file);
1.22      art       402:                } else {
1.39      mpech     403:                        dprintf("unknown file type %d for file %d of pid %ld",
                    404:                                file.f_type, i, (long)Pid);
1.1       deraadt   405:                }
                    406:        }
                    407: }
                    408:
                    409: void
1.56      mickey    410: vtrans(struct vnode *vp, int i, int flag, struct file *fp)
1.1       deraadt   411: {
                    412:        struct vnode vn;
                    413:        struct filestat fst;
1.12      deraadt   414:        char rw[3], mode[17];
1.47      deraadt   415:        char *badtype = NULL, *filename;
1.1       deraadt   416:
                    417:        filename = badtype = NULL;
                    418:        if (!KVM_READ(vp, &vn, sizeof (struct vnode))) {
1.39      mpech     419:                dprintf("can't read vnode at %p for pid %ld", vp, (long)Pid);
1.1       deraadt   420:                return;
                    421:        }
1.61    ! thib      422:        if (vn.v_type == VNON)
1.1       deraadt   423:                badtype = "none";
                    424:        else if (vn.v_type == VBAD)
                    425:                badtype = "bad";
                    426:        else
                    427:                switch (vn.v_tag) {
                    428:                case VT_UFS:
                    429:                case VT_MFS:
                    430:                        if (!ufs_filestat(&vn, &fst))
                    431:                                badtype = "error";
                    432:                        break;
                    433:                case VT_NFS:
                    434:                        if (!nfs_filestat(&vn, &fst))
                    435:                                badtype = "error";
                    436:                        break;
1.9       downsj    437:                case VT_EXT2FS:
                    438:                        if (!ext2fs_filestat(&vn, &fst))
                    439:                                badtype = "error";
                    440:                        break;
1.14      deraadt   441:                case VT_ISOFS:
                    442:                        if (!isofs_filestat(&vn, &fst))
                    443:                                badtype = "error";
                    444:                        break;
                    445:                case VT_MSDOSFS:
                    446:                        if (!msdos_filestat(&vn, &fst))
                    447:                                badtype = "error";
                    448:                        break;
1.18      art       449:                case VT_XFS:
                    450:                        if (!xfs_filestat(&vn, &fst))
                    451:                                badtype = "error";
                    452:                        break;
1.61    ! thib      453:                case VT_NON:
        !           454:                        if (vn.v_flag & VCLONE) {
        !           455:                                if (!spec_filestat(&vn, &fst))
        !           456:                                        badtype = "error";
        !           457:                        } else {
        !           458:                                badtype = "none";       /* not a clone */
        !           459:                        }
        !           460:                        break;
1.1       deraadt   461:                default: {
1.12      deraadt   462:                        static char unknown[30];
1.34      deraadt   463:                        snprintf(badtype = unknown, sizeof unknown,
                    464:                            "?(%x)", vn.v_tag);
1.10      deraadt   465:                        break;
1.1       deraadt   466:                }
                    467:        }
                    468:        if (checkfile) {
                    469:                int fsmatch = 0;
1.32      mpech     470:                DEVS *d;
1.1       deraadt   471:
                    472:                if (badtype)
                    473:                        return;
                    474:                for (d = devs; d != NULL; d = d->next)
                    475:                        if (d->fsid == fst.fsid) {
                    476:                                fsmatch = 1;
                    477:                                if (d->ino == fst.fileid) {
                    478:                                        filename = d->name;
                    479:                                        break;
                    480:                                }
                    481:                        }
                    482:                if (fsmatch == 0 || (filename == NULL && fsflg == 0))
                    483:                        return;
                    484:        }
                    485:        PREFIX(i);
                    486:        if (badtype) {
1.53      mickey    487:                (void)printf(" -           -  %10s    -\n", badtype);
1.1       deraadt   488:                return;
                    489:        }
1.61    ! thib      490:
1.1       deraadt   491:        if (nflg)
1.31      deraadt   492:                (void)printf(" %2ld,%-2ld", (long)major(fst.fsid),
                    493:                    (long)minor(fst.fsid));
1.61    ! thib      494:        else if (!(vn.v_flag & VCLONE))
        !           495:                (void)printf(" %-8s", getmnton(vn.v_mount));
1.1       deraadt   496:        else
1.61    ! thib      497:                (void)printf(" clone");
1.1       deraadt   498:        if (nflg)
1.34      deraadt   499:                (void)snprintf(mode, sizeof mode, "%o", fst.mode);
1.1       deraadt   500:        else
                    501:                strmode(fst.mode, mode);
1.53      mickey    502:        (void)printf(" %8ld %11s", fst.fileid, mode);
1.28      hugh      503:        rw[0] = '\0';
                    504:        if (flag & FREAD)
1.34      deraadt   505:                strlcat(rw, "r", sizeof rw);
1.28      hugh      506:        if (flag & FWRITE)
1.34      deraadt   507:                strlcat(rw, "w", sizeof rw);
1.28      hugh      508:        printf(" %2s", rw);
1.1       deraadt   509:        switch (vn.v_type) {
                    510:        case VBLK:
                    511:        case VCHR: {
                    512:                char *name;
                    513:
1.34      deraadt   514:                if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ?
1.1       deraadt   515:                    S_IFCHR : S_IFBLK)) == NULL))
1.28      hugh      516:                        printf("   %2d,%-3d", major(fst.rdev), minor(fst.rdev));
1.1       deraadt   517:                else
1.28      hugh      518:                        printf("  %7s", name);
                    519:                if (oflg)
                    520:                        printf("         ");
1.1       deraadt   521:                break;
                    522:        }
                    523:        default:
1.31      deraadt   524:                printf(" %8lld", (long long)fst.size);
1.28      hugh      525:                if (oflg)
1.56      mickey    526:                        printf(":%-8lld", (long long)(fp? fp->f_offset : 0));
1.1       deraadt   527:        }
1.56      mickey    528:        if (sflg)
                    529:                printf(" %8lld %8lld",
                    530:                    (long long)(fp? fp->f_rxfer + fp->f_wxfer : 0),
                    531:                    (long long)(fp? fp->f_rbytes + fp->f_wbytes : 0) / 1024);
1.1       deraadt   532:        if (filename && !fsflg)
1.28      hugh      533:                printf(" %s", filename);
1.1       deraadt   534:        putchar('\n');
                    535: }
                    536:
                    537: int
1.40      deraadt   538: ufs_filestat(struct vnode *vp, struct filestat *fsp)
1.1       deraadt   539: {
                    540:        struct inode inode;
1.54      pedro     541:        struct ufs1_dinode di1;
1.1       deraadt   542:
                    543:        if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
1.39      mpech     544:                dprintf("can't read inode at %p for pid %ld",
                    545:                    VTOI(vp), (long)Pid);
1.1       deraadt   546:                return 0;
                    547:        }
1.54      pedro     548:
                    549:        if (!KVM_READ(inode.i_din1, &di1, sizeof(struct ufs1_dinode))) {
                    550:                dprintf("can't read dinode at %p for pid %ld",
                    551:                    inode.i_din1, (long)Pid);
                    552:                return (0);
                    553:        }
                    554:
                    555:        inode.i_din1 = &di1;
                    556:
1.1       deraadt   557:        fsp->fsid = inode.i_dev & 0xffff;
                    558:        fsp->fileid = (long)inode.i_number;
1.55      pedro     559:        fsp->mode = inode.i_ffs1_mode;
                    560:        fsp->size = inode.i_ffs1_size;
                    561:        fsp->rdev = inode.i_ffs1_rdev;
1.9       downsj    562:
                    563:        return 1;
                    564: }
                    565:
                    566: int
1.40      deraadt   567: ext2fs_filestat(struct vnode *vp, struct filestat *fsp)
1.9       downsj    568: {
                    569:        struct inode inode;
1.54      pedro     570:        struct ext2fs_dinode e2di;
1.9       downsj    571:
                    572:        if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
1.39      mpech     573:                dprintf("can't read inode at %p for pid %ld",
                    574:                    VTOI(vp), (long)Pid);
1.9       downsj    575:                return 0;
                    576:        }
1.54      pedro     577:
                    578:        if (!KVM_READ(inode.i_e2din, &e2di, sizeof(struct ext2fs_dinode))) {
                    579:                dprintf("can't read dinode at %p for pid %ld",
                    580:                    inode.i_e2din, (long)Pid);
                    581:                return (0);
                    582:        }
                    583:
                    584:        inode.i_e2din = &e2di;
                    585:
1.9       downsj    586:        fsp->fsid = inode.i_dev & 0xffff;
                    587:        fsp->fileid = (long)inode.i_number;
1.11      millert   588:        fsp->mode = inode.i_e2fs_mode;
                    589:        fsp->size = inode.i_e2fs_size;
1.9       downsj    590:        fsp->rdev = 0;  /* XXX */
1.14      deraadt   591:
                    592:        return 1;
                    593: }
                    594:
                    595: int
1.40      deraadt   596: msdos_filestat(struct vnode *vp, struct filestat *fsp)
1.14      deraadt   597: {
                    598: #if 0
                    599:        struct inode inode;
                    600:
                    601:        if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
1.39      mpech     602:                dprintf("can't read inode at %p for pid %ld",
                    603:                    VTOI(vp), (long)Pid);
1.14      deraadt   604:                return 0;
                    605:        }
                    606:        fsp->fsid = inode.i_dev & 0xffff;
                    607:        fsp->fileid = (long)inode.i_number;
                    608:        fsp->mode = inode.i_e2fs_mode;
                    609:        fsp->size = inode.i_e2fs_size;
                    610:        fsp->rdev = 0;  /* XXX */
                    611: #endif
1.1       deraadt   612:
                    613:        return 1;
                    614: }
                    615:
                    616: int
1.40      deraadt   617: nfs_filestat(struct vnode *vp, struct filestat *fsp)
1.1       deraadt   618: {
                    619:        struct nfsnode nfsnode;
1.32      mpech     620:        mode_t mode;
1.1       deraadt   621:
                    622:        if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
1.39      mpech     623:                dprintf("can't read nfsnode at %p for pid %ld",
                    624:                    VTONFS(vp), (long)Pid);
1.1       deraadt   625:                return 0;
                    626:        }
                    627:        fsp->fsid = nfsnode.n_vattr.va_fsid;
                    628:        fsp->fileid = nfsnode.n_vattr.va_fileid;
                    629:        fsp->size = nfsnode.n_size;
                    630:        fsp->rdev = nfsnode.n_vattr.va_rdev;
                    631:        mode = (mode_t)nfsnode.n_vattr.va_mode;
                    632:        switch (vp->v_type) {
                    633:        case VREG:
                    634:                mode |= S_IFREG;
                    635:                break;
                    636:        case VDIR:
                    637:                mode |= S_IFDIR;
                    638:                break;
                    639:        case VBLK:
                    640:                mode |= S_IFBLK;
                    641:                break;
                    642:        case VCHR:
                    643:                mode |= S_IFCHR;
                    644:                break;
                    645:        case VLNK:
                    646:                mode |= S_IFLNK;
                    647:                break;
                    648:        case VSOCK:
                    649:                mode |= S_IFSOCK;
                    650:                break;
                    651:        case VFIFO:
                    652:                mode |= S_IFIFO;
                    653:                break;
1.17      mickey    654:        default:
                    655:                break;
1.34      deraadt   656:        }
1.1       deraadt   657:        fsp->mode = mode;
                    658:
                    659:        return 1;
                    660: }
                    661:
1.18      art       662: int
1.44      deraadt   663: xfs_filestat(struct vnode *vp, struct filestat *fsp)
1.18      art       664: {
                    665:        struct xfs_node xfs_node;
                    666:
                    667:        if (!KVM_READ(VNODE_TO_XNODE(vp), &xfs_node, sizeof (xfs_node))) {
1.39      mpech     668:                dprintf("can't read xfs_node at %p for pid %ld",
                    669:                    VTOI(vp), (long)Pid);
1.18      art       670:                return 0;
                    671:        }
                    672:        fsp->fsid = xfs_node.attr.va_fsid;
                    673:        fsp->fileid = (long)xfs_node.attr.va_fileid;
                    674:        fsp->mode = xfs_node.attr.va_mode;
                    675:        fsp->size = xfs_node.attr.va_size;
                    676:        fsp->rdev = xfs_node.attr.va_rdev;
                    677:
                    678:        return 1;
1.61    ! thib      679: }
        !           680:
        !           681: int
        !           682: spec_filestat(struct vnode *vp, struct filestat *fsp)
        !           683: {
        !           684:        struct specinfo         specinfo;
        !           685:        struct vnode            parent;
        !           686:
        !           687:        if (!KVM_READ(vp->v_specinfo, &specinfo, sizeof(struct specinfo))) {
        !           688:                dprintf("can't read specinfo at %p for pid %ld",
        !           689:                     vp->v_specinfo, (long) Pid);
        !           690:                return (0);
        !           691:        }
        !           692:
        !           693:        vp->v_specinfo = &specinfo;
        !           694:
        !           695:        if (!KVM_READ(vp->v_specparent, &parent, sizeof(struct vnode))) {
        !           696:                dprintf("can't read parent vnode at %p for pid %ld",
        !           697:                     vp->v_specparent, (long) Pid);
        !           698:                return (0);
        !           699:        }
        !           700:
        !           701:        if (!ufs_filestat(&parent, fsp))
        !           702:                return (0);
        !           703:
        !           704:        if (nflg)
        !           705:                fsp->rdev = vp->v_rdev;
        !           706:
        !           707:        return (1);
1.18      art       708: }
1.1       deraadt   709:
                    710: char *
1.40      deraadt   711: getmnton(struct mount *m)
1.1       deraadt   712: {
                    713:        static struct mount mount;
                    714:        static struct mtab {
                    715:                struct mtab *next;
                    716:                struct mount *m;
                    717:                char mntonname[MNAMELEN];
                    718:        } *mhead = NULL;
1.32      mpech     719:        struct mtab *mt;
1.1       deraadt   720:
                    721:        for (mt = mhead; mt != NULL; mt = mt->next)
                    722:                if (m == mt->m)
                    723:                        return (mt->mntonname);
                    724:        if (!KVM_READ(m, &mount, sizeof(struct mount))) {
1.17      mickey    725:                warn("can't read mount table at %p", m);
1.1       deraadt   726:                return (NULL);
                    727:        }
1.17      mickey    728:        if ((mt = malloc(sizeof (struct mtab))) == NULL)
                    729:                err(1, "malloc");
1.1       deraadt   730:        mt->m = m;
                    731:        bcopy(&mount.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN);
                    732:        mt->next = mhead;
                    733:        mhead = mt;
                    734:        return (mt->mntonname);
1.22      art       735: }
                    736:
                    737: void
1.56      mickey    738: pipetrans(struct pipe *pipe, int i, struct file *fp)
1.22      art       739: {
                    740:        struct pipe pi;
                    741:        void *maxaddr;
                    742:
                    743:        PREFIX(i);
                    744:
                    745:        printf(" ");
                    746:
                    747:        /* fill in socket */
                    748:        if (!KVM_READ(pipe, &pi, sizeof(struct pipe))) {
                    749:                dprintf("can't read pipe at %p", pipe);
                    750:                goto bad;
                    751:        }
                    752:
                    753:        /*
                    754:         * We don't have enough space to fit both peer and own address, so
                    755:         * we select the higher address so both ends of the pipe have the
                    756:         * same visible addr. (it's the higher address because when the other
                    757:         * end closes, it becomes 0)
                    758:         */
                    759:        maxaddr = MAX(pipe, pi.pipe_peer);
                    760:
1.56      mickey    761:        printf("pipe %p state: %s%s%s", maxaddr,
1.34      deraadt   762:            (pi.pipe_state & PIPE_WANTR) ? "R" : "",
                    763:            (pi.pipe_state & PIPE_WANTW) ? "W" : "",
                    764:            (pi.pipe_state & PIPE_EOF) ? "E" : "");
1.56      mickey    765:        if (sflg)
                    766:                printf("\t%8lld %8lld",
                    767:                    (long long)(fp? fp->f_rxfer + fp->f_wxfer : 0),
                    768:                    (long long)(fp? fp->f_rbytes + fp->f_wbytes : 0) / 1024);
                    769:        printf("\n");
1.37      deraadt   770:        return;
                    771: bad:
                    772:        printf("* error\n");
                    773: }
                    774:
                    775: void
1.56      mickey    776: kqueuetrans(struct kqueue *kq, int i, struct file *fp)
1.37      deraadt   777: {
                    778:        struct kqueue kqi;
                    779:
                    780:        PREFIX(i);
                    781:
                    782:        printf(" ");
                    783:
                    784:        /* fill it in */
                    785:        if (!KVM_READ(kq, &kqi, sizeof(struct kqueue))) {
                    786:                dprintf("can't read kqueue at %p", kq);
                    787:                goto bad;
                    788:        }
                    789:
1.57      deraadt   790:        printf("kqueue %p %d state: %s%s\n", kq, kqi.kq_count,
1.37      deraadt   791:            (kqi.kq_state & KQ_SEL) ? "S" : "",
                    792:            (kqi.kq_state & KQ_SLEEP) ? "W" : "");
                    793:        return;
                    794: bad:
                    795:        printf("* error\n");
                    796: }
                    797:
                    798: void
1.56      mickey    799: cryptotrans(void *f, int i, struct file *fp)
1.37      deraadt   800: {
                    801:        PREFIX(i);
                    802:
                    803:        printf(" ");
                    804:
1.38      deraadt   805:        printf("crypto %p\n", f);
1.37      deraadt   806: }
                    807:
                    808: void
1.56      mickey    809: systracetrans(struct fsystrace *f, int i, struct file *fp)
1.37      deraadt   810: {
1.38      deraadt   811:        struct fsystrace fi;
                    812:
1.37      deraadt   813:        PREFIX(i);
                    814:
                    815:        printf(" ");
                    816:
1.38      deraadt   817:        /* fill it in */
                    818:        if (!KVM_READ(f, &fi, sizeof(fi))) {
                    819:                dprintf("can't read fsystrace at %p", f);
                    820:                goto bad;
                    821:        }
1.34      deraadt   822:
1.38      deraadt   823:        printf("systrace %p npol %d\n", f, fi.npolicies);
1.22      art       824:        return;
                    825: bad:
                    826:        printf("* error\n");
1.1       deraadt   827: }
                    828:
1.26      itojun    829: #ifdef INET6
                    830: const char *
1.40      deraadt   831: inet6_addrstr(struct in6_addr *p)
1.26      itojun    832: {
                    833:        struct sockaddr_in6 sin6;
                    834:        static char hbuf[NI_MAXHOST];
1.50      itojun    835:        const int niflags = NI_NUMERICHOST;
1.26      itojun    836:
                    837:        memset(&sin6, 0, sizeof(sin6));
                    838:        sin6.sin6_family = AF_INET6;
                    839:        sin6.sin6_len = sizeof(struct sockaddr_in6);
                    840:        sin6.sin6_addr = *p;
                    841:        if (IN6_IS_ADDR_LINKLOCAL(p) &&
                    842:            *(u_int16_t *)&sin6.sin6_addr.s6_addr[2] != 0) {
                    843:                sin6.sin6_scope_id =
1.38      deraadt   844:                    ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);
1.26      itojun    845:                sin6.sin6_addr.s6_addr[2] = sin6.sin6_addr.s6_addr[3] = 0;
                    846:        }
                    847:
                    848:        if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
1.38      deraadt   849:            hbuf, sizeof(hbuf), NULL, 0, niflags))
1.26      itojun    850:                return "invalid";
                    851:
                    852:        return hbuf;
                    853: }
                    854: #endif
                    855:
1.1       deraadt   856: void
1.56      mickey    857: socktrans(struct socket *sock, int i, struct file *fp)
1.1       deraadt   858: {
                    859:        static char *stypename[] = {
                    860:                "unused",       /* 0 */
1.38      deraadt   861:                "stream",       /* 1 */
1.1       deraadt   862:                "dgram",        /* 2 */
                    863:                "raw",          /* 3 */
                    864:                "rdm",          /* 4 */
                    865:                "seqpak"        /* 5 */
                    866:        };
                    867: #define        STYPEMAX 5
                    868:        struct socket   so;
                    869:        struct protosw  proto;
                    870:        struct domain   dom;
                    871:        struct inpcb    inpcb;
                    872:        struct unpcb    unpcb;
                    873:        int len;
1.4       deraadt   874:        char dname[32];
1.26      itojun    875: #ifdef INET6
                    876:        char xaddrbuf[NI_MAXHOST + 2];
                    877: #endif
1.1       deraadt   878:
                    879:        PREFIX(i);
                    880:
                    881:        /* fill in socket */
                    882:        if (!KVM_READ(sock, &so, sizeof(struct socket))) {
1.17      mickey    883:                dprintf("can't read sock at %p", sock);
1.1       deraadt   884:                goto bad;
                    885:        }
                    886:
                    887:        /* fill in protosw entry */
                    888:        if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
1.17      mickey    889:                dprintf("can't read protosw at %p", so.so_proto);
1.1       deraadt   890:                goto bad;
                    891:        }
                    892:
                    893:        /* fill in domain */
                    894:        if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
1.17      mickey    895:                dprintf("can't read domain at %p", proto.pr_domain);
1.1       deraadt   896:                goto bad;
                    897:        }
                    898:
                    899:        if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
1.15      deraadt   900:            sizeof(dname) - 1)) != sizeof(dname) -1) {
1.17      mickey    901:                dprintf("can't read domain name at %p", dom.dom_name);
1.1       deraadt   902:                dname[0] = '\0';
1.38      deraadt   903:        } else
1.1       deraadt   904:                dname[len] = '\0';
                    905:
                    906:        if ((u_short)so.so_type > STYPEMAX)
                    907:                printf("* %s ?%d", dname, so.so_type);
                    908:        else
                    909:                printf("* %s %s", dname, stypename[so.so_type]);
                    910:
1.34      deraadt   911:        /*
1.1       deraadt   912:         * protocol specific formatting
                    913:         *
                    914:         * Try to find interesting things to print.  For tcp, the interesting
                    915:         * thing is the address of the tcpcb, for udp and others, just the
                    916:         * inpcb (socket pcb).  For unix domain, its the address of the socket
                    917:         * pcb and the address of the connected pcb (if connected).  Otherwise
                    918:         * just print the protocol number and address of the socket itself.
                    919:         * The idea is not to duplicate netstat, but to make available enough
                    920:         * information for further analysis.
                    921:         */
1.60      sobrado   922:        switch (dom.dom_family) {
1.1       deraadt   923:        case AF_INET:
                    924:                getinetproto(proto.pr_protocol);
1.5       deraadt   925:                if (proto.pr_protocol == IPPROTO_TCP) {
                    926:                        if (so.so_pcb == NULL)
                    927:                                break;
                    928:                        if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
                    929:                            sizeof(struct inpcb)) != sizeof(struct inpcb)) {
1.17      mickey    930:                                dprintf("can't read inpcb at %p", so.so_pcb);
1.5       deraadt   931:                                goto bad;
                    932:                        }
1.11      millert   933:                        printf(" %p", inpcb.inp_ppcb);
1.5       deraadt   934:                        printf(" %s:%d",
                    935:                            inpcb.inp_laddr.s_addr == INADDR_ANY ? "*" :
                    936:                            inet_ntoa(inpcb.inp_laddr),
                    937:                            ntohs(inpcb.inp_lport));
1.17      mickey    938:                        if (inpcb.inp_fport) {
1.13      deraadt   939:                                if (so.so_state & SS_CONNECTOUT)
                    940:                                        printf(" --> ");
                    941:                                else
                    942:                                        printf(" <-- ");
                    943:                                printf("%s:%d",
1.5       deraadt   944:                                    inpcb.inp_faddr.s_addr == INADDR_ANY ? "*" :
                    945:                                    inet_ntoa(inpcb.inp_faddr),
                    946:                                    ntohs(inpcb.inp_fport));
1.17      mickey    947:                        }
1.5       deraadt   948:                } else if (proto.pr_protocol == IPPROTO_UDP) {
                    949:                        if (so.so_pcb == NULL)
                    950:                                break;
                    951:                        if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
                    952:                            sizeof(struct inpcb)) != sizeof(struct inpcb)) {
1.17      mickey    953:                                dprintf("can't read inpcb at %p", so.so_pcb);
1.5       deraadt   954:                                goto bad;
1.1       deraadt   955:                        }
1.5       deraadt   956:                        printf(" %s:%d",
                    957:                            inpcb.inp_laddr.s_addr == INADDR_ANY ? "*" :
                    958:                            inet_ntoa(inpcb.inp_laddr),
                    959:                            ntohs(inpcb.inp_lport));
                    960:                        if (inpcb.inp_fport)
1.6       deraadt   961:                                printf(" <-> %s:%d",
1.5       deraadt   962:                                    inpcb.inp_faddr.s_addr == INADDR_ANY ? "*" :
                    963:                                    inet_ntoa(inpcb.inp_faddr),
                    964:                                    ntohs(inpcb.inp_fport));
                    965:                } else if (so.so_pcb)
1.11      millert   966:                        printf(" %p", so.so_pcb);
1.1       deraadt   967:                break;
1.26      itojun    968: #ifdef INET6
                    969:        case AF_INET6:
                    970:                getinetproto(proto.pr_protocol);
                    971:                if (proto.pr_protocol == IPPROTO_TCP) {
                    972:                        if (so.so_pcb == NULL)
                    973:                                break;
                    974:                        if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
                    975:                            sizeof(struct inpcb)) != sizeof(struct inpcb)) {
                    976:                                dprintf("can't read inpcb at %p", so.so_pcb);
                    977:                                goto bad;
                    978:                        }
                    979:                        printf(" %p", inpcb.inp_ppcb);
                    980:                        snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]",
                    981:                            inet6_addrstr(&inpcb.inp_laddr6));
                    982:                        printf(" %s:%d",
                    983:                            IN6_IS_ADDR_UNSPECIFIED(&inpcb.inp_laddr6) ? "*" :
                    984:                            xaddrbuf,
                    985:                            ntohs(inpcb.inp_lport));
                    986:                        if (inpcb.inp_fport) {
                    987:                                if (so.so_state & SS_CONNECTOUT)
                    988:                                        printf(" --> ");
                    989:                                else
                    990:                                        printf(" <-- ");
                    991:                                snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]",
                    992:                                    inet6_addrstr(&inpcb.inp_faddr6));
                    993:                                printf("%s:%d",
                    994:                                    IN6_IS_ADDR_UNSPECIFIED(&inpcb.inp_faddr6) ? "*" :
                    995:                                    xaddrbuf,
                    996:                                    ntohs(inpcb.inp_fport));
                    997:                        }
                    998:                } else if (proto.pr_protocol == IPPROTO_UDP) {
                    999:                        if (so.so_pcb == NULL)
                   1000:                                break;
                   1001:                        if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
                   1002:                            sizeof(struct inpcb)) != sizeof(struct inpcb)) {
                   1003:                                dprintf("can't read inpcb at %p", so.so_pcb);
                   1004:                                goto bad;
                   1005:                        }
                   1006:                        snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]",
                   1007:                            inet6_addrstr(&inpcb.inp_laddr6));
                   1008:                        printf(" %s:%d",
                   1009:                            IN6_IS_ADDR_UNSPECIFIED(&inpcb.inp_laddr6) ? "*" :
                   1010:                            xaddrbuf,
                   1011:                            ntohs(inpcb.inp_lport));
1.33      itojun   1012:                        if (inpcb.inp_fport) {
1.26      itojun   1013:                                snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]",
                   1014:                                    inet6_addrstr(&inpcb.inp_faddr6));
                   1015:                                printf(" <-> %s:%d",
                   1016:                                    IN6_IS_ADDR_UNSPECIFIED(&inpcb.inp_faddr6) ? "*" :
                   1017:                                    xaddrbuf,
                   1018:                                    ntohs(inpcb.inp_fport));
1.33      itojun   1019:                        }
1.26      itojun   1020:                } else if (so.so_pcb)
                   1021:                        printf(" %p", so.so_pcb);
                   1022:                break;
                   1023: #endif
1.1       deraadt  1024:        case AF_UNIX:
                   1025:                /* print address of pcb and connected pcb */
                   1026:                if (so.so_pcb) {
1.11      millert  1027:                        printf(" %p", so.so_pcb);
1.1       deraadt  1028:                        if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb,
                   1029:                            sizeof(struct unpcb)) != sizeof(struct unpcb)){
1.17      mickey   1030:                                dprintf("can't read unpcb at %p", so.so_pcb);
1.1       deraadt  1031:                                goto bad;
                   1032:                        }
                   1033:                        if (unpcb.unp_conn) {
                   1034:                                char shoconn[4], *cp;
                   1035:
                   1036:                                cp = shoconn;
                   1037:                                if (!(so.so_state & SS_CANTRCVMORE))
                   1038:                                        *cp++ = '<';
                   1039:                                *cp++ = '-';
                   1040:                                if (!(so.so_state & SS_CANTSENDMORE))
                   1041:                                        *cp++ = '>';
                   1042:                                *cp = '\0';
1.11      millert  1043:                                printf(" %s %p", shoconn,
                   1044:                                    unpcb.unp_conn);
1.1       deraadt  1045:                        }
                   1046:                }
                   1047:                break;
                   1048:        default:
                   1049:                /* print protocol number and socket address */
1.11      millert  1050:                printf(" %d %p", proto.pr_protocol, sock);
1.1       deraadt  1051:        }
1.56      mickey   1052:        if (sflg)
                   1053:                printf("\t%8lld %8lld",
                   1054:                    (long long)(fp? fp->f_rxfer + fp->f_wxfer : 0),
                   1055:                    (long long)(fp? fp->f_rbytes + fp->f_wbytes : 0) / 1024);
1.1       deraadt  1056:        printf("\n");
                   1057:        return;
                   1058: bad:
                   1059:        printf("* error\n");
                   1060: }
                   1061:
                   1062: /*
                   1063:  * getinetproto --
                   1064:  *     print name of protocol number
                   1065:  */
                   1066: void
                   1067: getinetproto(number)
                   1068:        int number;
                   1069: {
1.24      deraadt  1070:        static int isopen;
1.32      mpech    1071:        struct protoent *pe;
1.1       deraadt  1072:
1.24      deraadt  1073:        if (!isopen)
                   1074:                setprotoent(++isopen);
                   1075:        if ((pe = getprotobynumber(number)) != NULL)
                   1076:                printf(" %s", pe->p_name);
                   1077:        else
1.1       deraadt  1078:                printf(" %d", number);
                   1079: }
                   1080:
1.11      millert  1081: int
1.40      deraadt  1082: getfname(char *filename)
1.1       deraadt  1083: {
                   1084:        struct stat statbuf;
                   1085:        DEVS *cur;
                   1086:
                   1087:        if (stat(filename, &statbuf)) {
1.27      millert  1088:                warn("%s", filename);
1.1       deraadt  1089:                return(0);
                   1090:        }
1.17      mickey   1091:        if ((cur = malloc(sizeof(DEVS))) == NULL)
                   1092:                err(1, "malloc");
1.1       deraadt  1093:        cur->next = devs;
                   1094:        devs = cur;
                   1095:
                   1096:        cur->ino = statbuf.st_ino;
                   1097:        cur->fsid = statbuf.st_dev & 0xffff;
                   1098:        cur->name = filename;
                   1099:        return(1);
                   1100: }
                   1101:
                   1102: void
1.40      deraadt  1103: usage(void)
1.1       deraadt  1104: {
1.56      mickey   1105:        fprintf(stderr, "usage: fstat [-fnosv] [-M core] [-N system] "
1.48      jmc      1106:            "[-p pid] [-u user] [file ...]\n");
1.1       deraadt  1107:        exit(1);
                   1108: }