[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.8

1.8     ! millert     1: /*     $OpenBSD: fstat.c,v 1.7 1996/12/22 03:25:52 tholo 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.
                     15:  * 3. All advertising materials mentioning features or use of this software
                     16:  *    must display the following acknowledgement:
                     17:  *     This product includes software developed by the University of
                     18:  *     California, Berkeley and its contributors.
                     19:  * 4. Neither the name of the University nor the names of its contributors
                     20:  *    may be used to endorse or promote products derived from this software
                     21:  *    without specific prior written permission.
                     22:  *
                     23:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     24:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     25:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     26:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     27:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     28:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     29:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     30:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     31:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     32:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     33:  * SUCH DAMAGE.
                     34:  */
                     35:
                     36: #ifndef lint
                     37: static char copyright[] =
                     38: "@(#) Copyright (c) 1988, 1993\n\
                     39:        The Regents of the University of California.  All rights reserved.\n";
                     40: #endif /* not lint */
                     41:
                     42: #ifndef lint
                     43: /*static char sccsid[] = "from: @(#)fstat.c    8.1 (Berkeley) 6/6/93";*/
1.8     ! millert    44: static char *rcsid = "$OpenBSD: fstat.c,v 1.7 1996/12/22 03:25:52 tholo Exp $";
1.1       deraadt    45: #endif /* not lint */
                     46:
                     47: #include <sys/param.h>
                     48: #include <sys/time.h>
                     49: #include <sys/proc.h>
                     50: #include <sys/user.h>
                     51: #include <sys/stat.h>
                     52: #include <sys/vnode.h>
                     53: #include <sys/socket.h>
                     54: #include <sys/socketvar.h>
                     55: #include <sys/domain.h>
                     56: #include <sys/protosw.h>
                     57: #include <sys/unpcb.h>
                     58: #include <sys/sysctl.h>
                     59: #include <sys/filedesc.h>
                     60: #define        _KERNEL
                     61: #include <sys/file.h>
                     62: #include <ufs/ufs/quota.h>
                     63: #include <ufs/ufs/inode.h>
                     64: #undef _KERNEL
                     65: #define NFS
                     66: #include <sys/mount.h>
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:
                     73: #include <net/route.h>
                     74: #include <netinet/in.h>
                     75: #include <netinet/in_systm.h>
                     76: #include <netinet/ip.h>
                     77: #include <netinet/in_pcb.h>
                     78:
1.5       deraadt    79: #include <arpa/inet.h>
                     80:
1.1       deraadt    81: #include <ctype.h>
                     82: #include <errno.h>
                     83: #include <kvm.h>
1.2       niklas     84: #include <limits.h>
1.1       deraadt    85: #include <nlist.h>
                     86: #include <paths.h>
                     87: #include <pwd.h>
                     88: #include <stdio.h>
                     89: #include <stdlib.h>
                     90: #include <string.h>
                     91:
                     92: #define        TEXT    -1
                     93: #define        CDIR    -2
                     94: #define        RDIR    -3
                     95: #define        TRACE   -4
                     96:
                     97: typedef struct devs {
                     98:        struct  devs *next;
                     99:        long    fsid;
                    100:        ino_t   ino;
                    101:        char    *name;
                    102: } DEVS;
                    103: DEVS *devs;
                    104:
                    105: struct  filestat {
                    106:        long    fsid;
                    107:        long    fileid;
                    108:        mode_t  mode;
                    109:        u_long  size;
                    110:        dev_t   rdev;
                    111: };
                    112:
                    113: #ifdef notdef
                    114: struct nlist nl[] = {
                    115:        { "" },
                    116: };
                    117: #endif
                    118:
                    119: int    fsflg,  /* show files on same filesystem as file(s) argument */
                    120:        pflg,   /* show files open by a particular pid */
                    121:        uflg;   /* show files open by a particular (effective) user */
                    122: int    checkfile; /* true if restricting to particular files or filesystems */
                    123: int    nflg;   /* (numerical) display f.s. and rdev as dev_t */
                    124: int    vflg;   /* display errors in locating kernel data objects etc... */
                    125:
                    126: #define dprintf        if (vflg) fprintf
                    127:
                    128: struct file **ofiles;  /* buffer of pointers to file structures */
                    129: int maxfiles;
                    130: #define ALLOC_OFILES(d)        \
                    131:        if ((d) > maxfiles) { \
                    132:                free(ofiles); \
                    133:                ofiles = malloc((d) * sizeof(struct file *)); \
                    134:                if (ofiles == NULL) { \
                    135:                        fprintf(stderr, "fstat: %s\n", strerror(errno)); \
                    136:                        exit(1); \
                    137:                } \
                    138:                maxfiles = (d); \
                    139:        }
                    140:
                    141: /*
                    142:  * a kvm_read that returns true if everything is read
                    143:  */
                    144: #define KVM_READ(kaddr, paddr, len) \
                    145:        (kvm_read(kd, (u_long)(kaddr), (char *)(paddr), (len)) == (len))
                    146:
                    147: kvm_t *kd;
                    148:
                    149: int ufs_filestat(), nfs_filestat();
                    150: void dofiles(), getinetproto(), socktrans();
                    151: void usage(), vtrans();
                    152:
                    153: main(argc, argv)
                    154:        int argc;
                    155:        char **argv;
                    156: {
                    157:        extern char *optarg;
                    158:        extern int optind;
                    159:        register struct passwd *passwd;
                    160:        struct kinfo_proc *p, *plast;
                    161:        int arg, ch, what;
                    162:        char *memf, *nlistf;
1.2       niklas    163:        char buf[_POSIX2_LINE_MAX];
1.1       deraadt   164:        int cnt;
                    165:
                    166:        arg = 0;
                    167:        what = KERN_PROC_ALL;
                    168:        nlistf = memf = NULL;
1.8     ! millert   169:        while ((ch = getopt(argc, argv, "fnp:u:vN:M:")) != -1)
1.1       deraadt   170:                switch((char)ch) {
                    171:                case 'f':
                    172:                        fsflg = 1;
                    173:                        break;
                    174:                case 'M':
                    175:                        memf = optarg;
                    176:                        break;
                    177:                case 'N':
                    178:                        nlistf = optarg;
                    179:                        break;
                    180:                case 'n':
                    181:                        nflg = 1;
                    182:                        break;
                    183:                case 'p':
                    184:                        if (pflg++)
                    185:                                usage();
                    186:                        if (!isdigit(*optarg)) {
                    187:                                fprintf(stderr,
                    188:                                    "fstat: -p requires a process id\n");
                    189:                                usage();
                    190:                        }
                    191:                        what = KERN_PROC_PID;
                    192:                        arg = atoi(optarg);
                    193:                        break;
                    194:                case 'u':
                    195:                        if (uflg++)
                    196:                                usage();
                    197:                        if (!(passwd = getpwnam(optarg))) {
                    198:                                fprintf(stderr, "%s: unknown uid\n",
                    199:                                    optarg);
                    200:                                exit(1);
                    201:                        }
                    202:                        what = KERN_PROC_UID;
                    203:                        arg = passwd->pw_uid;
                    204:                        break;
                    205:                case 'v':
                    206:                        vflg = 1;
                    207:                        break;
                    208:                case '?':
                    209:                default:
                    210:                        usage();
                    211:                }
                    212:
                    213:        if (*(argv += optind)) {
                    214:                for (; *argv; ++argv) {
                    215:                        if (getfname(*argv))
                    216:                                checkfile = 1;
                    217:                }
                    218:                if (!checkfile) /* file(s) specified, but none accessable */
                    219:                        exit(1);
                    220:        }
                    221:
                    222:        ALLOC_OFILES(256);      /* reserve space for file pointers */
                    223:
                    224:        if (fsflg && !checkfile) {
                    225:                /* -f with no files means use wd */
                    226:                if (getfname(".") == 0)
                    227:                        exit(1);
                    228:                checkfile = 1;
                    229:        }
                    230:
                    231:        /*
                    232:         * Discard setgid privileges if not the running kernel so that bad
                    233:         * guys can't print interesting stuff from kernel memory.
                    234:         */
1.7       tholo     235:        if (nlistf != NULL || memf != NULL) {
                    236:                setegid(getgid());
1.1       deraadt   237:                setgid(getgid());
1.7       tholo     238:        }
1.1       deraadt   239:
1.2       niklas    240:        if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL) {
                    241:                fprintf(stderr, "fstat: %s\n", buf);
1.1       deraadt   242:                exit(1);
                    243:        }
                    244: #ifdef notdef
                    245:        if (kvm_nlist(kd, nl) != 0) {
                    246:                fprintf(stderr, "fstat: no namelist: %s\n", kvm_geterr(kd));
                    247:                exit(1);
                    248:        }
                    249: #endif
                    250:        if ((p = kvm_getprocs(kd, what, arg, &cnt)) == NULL) {
                    251:                fprintf(stderr, "fstat: %s\n", kvm_geterr(kd));
                    252:                exit(1);
                    253:        }
                    254:        if (nflg)
                    255:                printf("%s",
                    256: "USER     CMD          PID   FD  DEV    INUM       MODE SZ|DV R/W");
                    257:        else
                    258:                printf("%s",
                    259: "USER     CMD          PID   FD MOUNT      INUM MODE         SZ|DV R/W");
                    260:        if (checkfile && fsflg == 0)
                    261:                printf(" NAME\n");
                    262:        else
                    263:                putchar('\n');
                    264:
                    265:        for (plast = &p[cnt]; p < plast; ++p) {
                    266:                if (p->kp_proc.p_stat == SZOMB)
                    267:                        continue;
                    268:                dofiles(p);
                    269:        }
                    270:        exit(0);
                    271: }
                    272:
                    273: char   *Uname, *Comm;
                    274: int    Pid;
                    275:
                    276: #define PREFIX(i) printf("%-8.8s %-10s %5d", Uname, Comm, Pid); \
                    277:        switch(i) { \
                    278:        case TEXT: \
                    279:                printf(" text"); \
                    280:                break; \
                    281:        case CDIR: \
                    282:                printf("   wd"); \
                    283:                break; \
                    284:        case RDIR: \
                    285:                printf(" root"); \
                    286:                break; \
                    287:        case TRACE: \
                    288:                printf("   tr"); \
                    289:                break; \
                    290:        default: \
                    291:                printf(" %4d", i); \
                    292:                break; \
                    293:        }
                    294:
                    295: /*
                    296:  * print open files attributed to this process
                    297:  */
                    298: void
                    299: dofiles(kp)
                    300:        struct kinfo_proc *kp;
                    301: {
                    302:        int i, last;
                    303:        struct file file;
                    304:        struct filedesc0 filed0;
                    305: #define        filed   filed0.fd_fd
                    306:        struct proc *p = &kp->kp_proc;
                    307:        struct eproc *ep = &kp->kp_eproc;
                    308:
                    309:        extern char *user_from_uid();
                    310:
                    311:        Uname = user_from_uid(ep->e_ucred.cr_uid, 0);
                    312:        Pid = p->p_pid;
                    313:        Comm = p->p_comm;
                    314:
                    315:        if (p->p_fd == NULL)
                    316:                return;
                    317:        if (!KVM_READ(p->p_fd, &filed0, sizeof (filed0))) {
                    318:                dprintf(stderr, "can't read filedesc at %x for pid %d\n",
                    319:                        p->p_fd, Pid);
                    320:                return;
                    321:        }
                    322:        if (filed.fd_nfiles < 0 || filed.fd_lastfile >= filed.fd_nfiles ||
                    323:            filed.fd_freefile > filed.fd_lastfile + 1) {
                    324:                dprintf(stderr, "filedesc corrupted at %x for pid %d\n",
                    325:                        p->p_fd, Pid);
                    326:                return;
                    327:        }
                    328:        /*
                    329:         * root directory vnode, if one
                    330:         */
                    331:        if (filed.fd_rdir)
                    332:                vtrans(filed.fd_rdir, RDIR, FREAD);
                    333:        /*
                    334:         * current working directory vnode
                    335:         */
                    336:        vtrans(filed.fd_cdir, CDIR, FREAD);
                    337:        /*
                    338:         * ktrace vnode, if one
                    339:         */
                    340:        if (p->p_tracep)
                    341:                vtrans(p->p_tracep, TRACE, FREAD|FWRITE);
                    342:        /*
                    343:         * open files
                    344:         */
                    345: #define FPSIZE (sizeof (struct file *))
                    346:        ALLOC_OFILES(filed.fd_lastfile+1);
                    347:        if (filed.fd_nfiles > NDFILE) {
                    348:                if (!KVM_READ(filed.fd_ofiles, ofiles,
                    349:                    (filed.fd_lastfile+1) * FPSIZE)) {
                    350:                        dprintf(stderr,
                    351:                            "can't read file structures at %x for pid %d\n",
                    352:                            filed.fd_ofiles, Pid);
                    353:                        return;
                    354:                }
                    355:        } else
                    356:                bcopy(filed0.fd_dfiles, ofiles, (filed.fd_lastfile+1) * FPSIZE);
                    357:        for (i = 0; i <= filed.fd_lastfile; i++) {
                    358:                if (ofiles[i] == NULL)
                    359:                        continue;
                    360:                if (!KVM_READ(ofiles[i], &file, sizeof (struct file))) {
                    361:                        dprintf(stderr, "can't read file %d at %x for pid %d\n",
                    362:                                i, ofiles[i], Pid);
                    363:                        continue;
                    364:                }
                    365:                if (file.f_type == DTYPE_VNODE)
                    366:                        vtrans((struct vnode *)file.f_data, i, file.f_flag);
                    367:                else if (file.f_type == DTYPE_SOCKET) {
                    368:                        if (checkfile == 0)
                    369:                                socktrans((struct socket *)file.f_data, i);
                    370:                }
                    371:                else {
                    372:                        dprintf(stderr,
                    373:                                "unknown file type %d for file %d of pid %d\n",
                    374:                                file.f_type, i, Pid);
                    375:                }
                    376:        }
                    377: }
                    378:
                    379: void
                    380: vtrans(vp, i, flag)
                    381:        struct vnode *vp;
                    382:        int i;
                    383:        int flag;
                    384: {
                    385:        struct vnode vn;
                    386:        struct filestat fst;
                    387:        char rw[3], mode[15];
                    388:        char *badtype = NULL, *filename, *getmnton();
                    389:
                    390:        filename = badtype = NULL;
                    391:        if (!KVM_READ(vp, &vn, sizeof (struct vnode))) {
                    392:                dprintf(stderr, "can't read vnode at %x for pid %d\n",
                    393:                        vp, Pid);
                    394:                return;
                    395:        }
                    396:        if (vn.v_type == VNON || vn.v_tag == VT_NON)
                    397:                badtype = "none";
                    398:        else if (vn.v_type == VBAD)
                    399:                badtype = "bad";
                    400:        else
                    401:                switch (vn.v_tag) {
                    402:                case VT_UFS:
                    403:                        if (!ufs_filestat(&vn, &fst))
                    404:                                badtype = "error";
                    405:                        break;
                    406:                case VT_MFS:
                    407:                        if (!ufs_filestat(&vn, &fst))
                    408:                                badtype = "error";
                    409:                        break;
                    410:                case VT_NFS:
                    411:                        if (!nfs_filestat(&vn, &fst))
                    412:                                badtype = "error";
                    413:                        break;
                    414:                default: {
1.4       deraadt   415:                        static char unknown[20];
1.1       deraadt   416:                        sprintf(badtype = unknown, "?(%x)", vn.v_tag);
                    417:                        break;;
                    418:                }
                    419:        }
                    420:        if (checkfile) {
                    421:                int fsmatch = 0;
                    422:                register DEVS *d;
                    423:
                    424:                if (badtype)
                    425:                        return;
                    426:                for (d = devs; d != NULL; d = d->next)
                    427:                        if (d->fsid == fst.fsid) {
                    428:                                fsmatch = 1;
                    429:                                if (d->ino == fst.fileid) {
                    430:                                        filename = d->name;
                    431:                                        break;
                    432:                                }
                    433:                        }
                    434:                if (fsmatch == 0 || (filename == NULL && fsflg == 0))
                    435:                        return;
                    436:        }
                    437:        PREFIX(i);
                    438:        if (badtype) {
                    439:                (void)printf(" -         -  %10s    -\n", badtype);
                    440:                return;
                    441:        }
                    442:        if (nflg)
                    443:                (void)printf(" %2d,%-2d", major(fst.fsid), minor(fst.fsid));
                    444:        else
                    445:                (void)printf(" %-8s", getmnton(vn.v_mount));
                    446:        if (nflg)
                    447:                (void)sprintf(mode, "%o", fst.mode);
                    448:        else
                    449:                strmode(fst.mode, mode);
                    450:        (void)printf(" %6d %10s", fst.fileid, mode);
                    451:        switch (vn.v_type) {
                    452:        case VBLK:
                    453:        case VCHR: {
                    454:                char *name;
                    455:
                    456:                if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ?
                    457:                    S_IFCHR : S_IFBLK)) == NULL))
                    458:                        printf("  %2d,%-2d", major(fst.rdev), minor(fst.rdev));
                    459:                else
                    460:                        printf(" %6s", name);
                    461:                break;
                    462:        }
                    463:        default:
                    464:                printf(" %6d", fst.size);
                    465:        }
                    466:        rw[0] = '\0';
                    467:        if (flag & FREAD)
                    468:                strcat(rw, "r");
                    469:        if (flag & FWRITE)
                    470:                strcat(rw, "w");
                    471:        printf(" %2s", rw);
                    472:        if (filename && !fsflg)
                    473:                printf("  %s", filename);
                    474:        putchar('\n');
                    475: }
                    476:
                    477: int
                    478: ufs_filestat(vp, fsp)
                    479:        struct vnode *vp;
                    480:        struct filestat *fsp;
                    481: {
                    482:        struct inode inode;
                    483:
                    484:        if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
                    485:                dprintf(stderr, "can't read inode at %x for pid %d\n",
                    486:                        VTOI(vp), Pid);
                    487:                return 0;
                    488:        }
                    489:        fsp->fsid = inode.i_dev & 0xffff;
                    490:        fsp->fileid = (long)inode.i_number;
                    491:        fsp->mode = (mode_t)inode.i_mode;
                    492:        fsp->size = (u_long)inode.i_size;
                    493:        fsp->rdev = inode.i_rdev;
                    494:
                    495:        return 1;
                    496: }
                    497:
                    498: int
                    499: nfs_filestat(vp, fsp)
                    500:        struct vnode *vp;
                    501:        struct filestat *fsp;
                    502: {
                    503:        struct nfsnode nfsnode;
                    504:        register mode_t mode;
                    505:
                    506:        if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
                    507:                dprintf(stderr, "can't read nfsnode at %x for pid %d\n",
                    508:                        VTONFS(vp), Pid);
                    509:                return 0;
                    510:        }
                    511:        fsp->fsid = nfsnode.n_vattr.va_fsid;
                    512:        fsp->fileid = nfsnode.n_vattr.va_fileid;
                    513:        fsp->size = nfsnode.n_size;
                    514:        fsp->rdev = nfsnode.n_vattr.va_rdev;
                    515:        mode = (mode_t)nfsnode.n_vattr.va_mode;
                    516:        switch (vp->v_type) {
                    517:        case VREG:
                    518:                mode |= S_IFREG;
                    519:                break;
                    520:        case VDIR:
                    521:                mode |= S_IFDIR;
                    522:                break;
                    523:        case VBLK:
                    524:                mode |= S_IFBLK;
                    525:                break;
                    526:        case VCHR:
                    527:                mode |= S_IFCHR;
                    528:                break;
                    529:        case VLNK:
                    530:                mode |= S_IFLNK;
                    531:                break;
                    532:        case VSOCK:
                    533:                mode |= S_IFSOCK;
                    534:                break;
                    535:        case VFIFO:
                    536:                mode |= S_IFIFO;
                    537:                break;
                    538:        };
                    539:        fsp->mode = mode;
                    540:
                    541:        return 1;
                    542: }
                    543:
                    544:
                    545: char *
                    546: getmnton(m)
                    547:        struct mount *m;
                    548: {
                    549:        static struct mount mount;
                    550:        static struct mtab {
                    551:                struct mtab *next;
                    552:                struct mount *m;
                    553:                char mntonname[MNAMELEN];
                    554:        } *mhead = NULL;
                    555:        register struct mtab *mt;
                    556:
                    557:        for (mt = mhead; mt != NULL; mt = mt->next)
                    558:                if (m == mt->m)
                    559:                        return (mt->mntonname);
                    560:        if (!KVM_READ(m, &mount, sizeof(struct mount))) {
                    561:                fprintf(stderr, "can't read mount table at %x\n", m);
                    562:                return (NULL);
                    563:        }
                    564:        if ((mt = malloc(sizeof (struct mtab))) == NULL) {
                    565:                fprintf(stderr, "fstat: %s\n", strerror(errno));
                    566:                exit(1);
                    567:        }
                    568:        mt->m = m;
                    569:        bcopy(&mount.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN);
                    570:        mt->next = mhead;
                    571:        mhead = mt;
                    572:        return (mt->mntonname);
                    573: }
                    574:
                    575: void
                    576: socktrans(sock, i)
                    577:        struct socket *sock;
                    578:        int i;
                    579: {
                    580:        static char *stypename[] = {
                    581:                "unused",       /* 0 */
                    582:                "stream",       /* 1 */
                    583:                "dgram",        /* 2 */
                    584:                "raw",          /* 3 */
                    585:                "rdm",          /* 4 */
                    586:                "seqpak"        /* 5 */
                    587:        };
                    588: #define        STYPEMAX 5
                    589:        struct socket   so;
                    590:        struct protosw  proto;
                    591:        struct domain   dom;
                    592:        struct inpcb    inpcb;
                    593:        struct unpcb    unpcb;
                    594:        int len;
1.4       deraadt   595:        char dname[32];
1.1       deraadt   596:
                    597:        PREFIX(i);
                    598:
                    599:        /* fill in socket */
                    600:        if (!KVM_READ(sock, &so, sizeof(struct socket))) {
                    601:                dprintf(stderr, "can't read sock at %x\n", sock);
                    602:                goto bad;
                    603:        }
                    604:
                    605:        /* fill in protosw entry */
                    606:        if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
                    607:                dprintf(stderr, "can't read protosw at %x", so.so_proto);
                    608:                goto bad;
                    609:        }
                    610:
                    611:        /* fill in domain */
                    612:        if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
                    613:                dprintf(stderr, "can't read domain at %x\n", proto.pr_domain);
                    614:                goto bad;
                    615:        }
                    616:
                    617:        if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
                    618:            sizeof(dname) - 1)) < 0) {
                    619:                dprintf(stderr, "can't read domain name at %x\n",
                    620:                        dom.dom_name);
                    621:                dname[0] = '\0';
                    622:        }
                    623:        else
                    624:                dname[len] = '\0';
                    625:
                    626:        if ((u_short)so.so_type > STYPEMAX)
                    627:                printf("* %s ?%d", dname, so.so_type);
                    628:        else
                    629:                printf("* %s %s", dname, stypename[so.so_type]);
                    630:
                    631:        /*
                    632:         * protocol specific formatting
                    633:         *
                    634:         * Try to find interesting things to print.  For tcp, the interesting
                    635:         * thing is the address of the tcpcb, for udp and others, just the
                    636:         * inpcb (socket pcb).  For unix domain, its the address of the socket
                    637:         * pcb and the address of the connected pcb (if connected).  Otherwise
                    638:         * just print the protocol number and address of the socket itself.
                    639:         * The idea is not to duplicate netstat, but to make available enough
                    640:         * information for further analysis.
                    641:         */
                    642:        switch(dom.dom_family) {
                    643:        case AF_INET:
                    644:                getinetproto(proto.pr_protocol);
1.5       deraadt   645:                if (proto.pr_protocol == IPPROTO_TCP) {
                    646:                        if (so.so_pcb == NULL)
                    647:                                break;
                    648:                        if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
                    649:                            sizeof(struct inpcb)) != sizeof(struct inpcb)) {
                    650:                                dprintf(stderr, "can't read inpcb at %x\n",
                    651:                                    so.so_pcb);
                    652:                                goto bad;
                    653:                        }
                    654:                        printf(" %lx", (long)inpcb.inp_ppcb);
                    655:                        printf(" %s:%d",
                    656:                            inpcb.inp_laddr.s_addr == INADDR_ANY ? "*" :
                    657:                            inet_ntoa(inpcb.inp_laddr),
                    658:                            ntohs(inpcb.inp_lport));
                    659:                        if (inpcb.inp_fport)
1.6       deraadt   660:                                printf(" <-> %s:%d",
1.5       deraadt   661:                                    inpcb.inp_faddr.s_addr == INADDR_ANY ? "*" :
                    662:                                    inet_ntoa(inpcb.inp_faddr),
                    663:                                    ntohs(inpcb.inp_fport));
                    664:                } else if (proto.pr_protocol == IPPROTO_UDP) {
                    665:                        if (so.so_pcb == NULL)
                    666:                                break;
                    667:                        if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
                    668:                            sizeof(struct inpcb)) != sizeof(struct inpcb)) {
                    669:                                dprintf(stderr, "can't read inpcb at %x\n",
                    670:                                    so.so_pcb);
                    671:                                goto bad;
1.1       deraadt   672:                        }
1.5       deraadt   673:                        printf(" %s:%d",
                    674:                            inpcb.inp_laddr.s_addr == INADDR_ANY ? "*" :
                    675:                            inet_ntoa(inpcb.inp_laddr),
                    676:                            ntohs(inpcb.inp_lport));
                    677:                        if (inpcb.inp_fport)
1.6       deraadt   678:                                printf(" <-> %s:%d",
1.5       deraadt   679:                                    inpcb.inp_faddr.s_addr == INADDR_ANY ? "*" :
                    680:                                    inet_ntoa(inpcb.inp_faddr),
                    681:                                    ntohs(inpcb.inp_fport));
                    682:                } else if (so.so_pcb)
1.1       deraadt   683:                        printf(" %lx", (long)so.so_pcb);
                    684:                break;
                    685:        case AF_UNIX:
                    686:                /* print address of pcb and connected pcb */
                    687:                if (so.so_pcb) {
                    688:                        printf(" %lx", (long)so.so_pcb);
                    689:                        if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb,
                    690:                            sizeof(struct unpcb)) != sizeof(struct unpcb)){
                    691:                                dprintf(stderr, "can't read unpcb at %x\n",
                    692:                                    so.so_pcb);
                    693:                                goto bad;
                    694:                        }
                    695:                        if (unpcb.unp_conn) {
                    696:                                char shoconn[4], *cp;
                    697:
                    698:                                cp = shoconn;
                    699:                                if (!(so.so_state & SS_CANTRCVMORE))
                    700:                                        *cp++ = '<';
                    701:                                *cp++ = '-';
                    702:                                if (!(so.so_state & SS_CANTSENDMORE))
                    703:                                        *cp++ = '>';
                    704:                                *cp = '\0';
                    705:                                printf(" %s %lx", shoconn,
                    706:                                    (long)unpcb.unp_conn);
                    707:                        }
                    708:                }
                    709:                break;
                    710:        default:
                    711:                /* print protocol number and socket address */
                    712:                printf(" %d %lx", proto.pr_protocol, (long)sock);
                    713:        }
                    714:        printf("\n");
                    715:        return;
                    716: bad:
                    717:        printf("* error\n");
                    718: }
                    719:
                    720: /*
                    721:  * getinetproto --
                    722:  *     print name of protocol number
                    723:  */
                    724: void
                    725: getinetproto(number)
                    726:        int number;
                    727: {
                    728:        char *cp;
                    729:
                    730:        switch(number) {
                    731:        case IPPROTO_IP:
                    732:                cp = "ip"; break;
                    733:        case IPPROTO_ICMP:
                    734:                cp ="icmp"; break;
                    735:        case IPPROTO_GGP:
                    736:                cp ="ggp"; break;
                    737:        case IPPROTO_TCP:
                    738:                cp ="tcp"; break;
                    739:        case IPPROTO_EGP:
                    740:                cp ="egp"; break;
                    741:        case IPPROTO_PUP:
                    742:                cp ="pup"; break;
                    743:        case IPPROTO_UDP:
                    744:                cp ="udp"; break;
                    745:        case IPPROTO_IDP:
                    746:                cp ="idp"; break;
                    747:        case IPPROTO_RAW:
                    748:                cp ="raw"; break;
                    749:        default:
                    750:                printf(" %d", number);
                    751:                return;
                    752:        }
                    753:        printf(" %s", cp);
                    754: }
                    755:
                    756: getfname(filename)
                    757:        char *filename;
                    758: {
                    759:        struct stat statbuf;
                    760:        DEVS *cur;
                    761:
                    762:        if (stat(filename, &statbuf)) {
                    763:                fprintf(stderr, "fstat: %s: %s\n", filename, strerror(errno));
                    764:                return(0);
                    765:        }
                    766:        if ((cur = malloc(sizeof(DEVS))) == NULL) {
                    767:                fprintf(stderr, "fstat: %s\n", strerror(errno));
                    768:                exit(1);
                    769:        }
                    770:        cur->next = devs;
                    771:        devs = cur;
                    772:
                    773:        cur->ino = statbuf.st_ino;
                    774:        cur->fsid = statbuf.st_dev & 0xffff;
                    775:        cur->name = filename;
                    776:        return(1);
                    777: }
                    778:
                    779: void
                    780: usage()
                    781: {
                    782:        (void)fprintf(stderr,
                    783:  "usage: fstat [-fnv] [-p pid] [-u user] [-N system] [-M core] [file ...]\n");
                    784:        exit(1);
                    785: }