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

1.16    ! deraadt     1: /*     $OpenBSD: fstat.c,v 1.15 1998/07/05 18:42:41 deraadt 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.16    ! deraadt    44: static char *rcsid = "$OpenBSD: fstat.c,v 1.15 1998/07/05 18:42:41 deraadt 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>
1.11      millert    91: #include <unistd.h>
1.14      deraadt    92: #include "fstat.h"
1.1       deraadt    93:
                     94: #define        TEXT    -1
                     95: #define        CDIR    -2
                     96: #define        RDIR    -3
                     97: #define        TRACE   -4
                     98:
                     99: typedef struct devs {
                    100:        struct  devs *next;
                    101:        long    fsid;
                    102:        ino_t   ino;
                    103:        char    *name;
                    104: } DEVS;
                    105: DEVS *devs;
                    106:
                    107: int    fsflg,  /* show files on same filesystem as file(s) argument */
                    108:        pflg,   /* show files open by a particular pid */
                    109:        uflg;   /* show files open by a particular (effective) user */
                    110: int    checkfile; /* true if restricting to particular files or filesystems */
                    111: int    nflg;   /* (numerical) display f.s. and rdev as dev_t */
                    112: int    vflg;   /* display errors in locating kernel data objects etc... */
                    113:
                    114: #define dprintf        if (vflg) fprintf
                    115:
                    116: struct file **ofiles;  /* buffer of pointers to file structures */
                    117: int maxfiles;
                    118: #define ALLOC_OFILES(d)        \
                    119:        if ((d) > maxfiles) { \
                    120:                free(ofiles); \
                    121:                ofiles = malloc((d) * sizeof(struct file *)); \
                    122:                if (ofiles == NULL) { \
                    123:                        fprintf(stderr, "fstat: %s\n", strerror(errno)); \
                    124:                        exit(1); \
                    125:                } \
                    126:                maxfiles = (d); \
                    127:        }
                    128:
                    129: /*
                    130:  * a kvm_read that returns true if everything is read
                    131:  */
                    132: #define KVM_READ(kaddr, paddr, len) \
1.11      millert   133:        (kvm_read(kd, (u_long)(kaddr), (void *)(paddr), (len)) == (len))
1.1       deraadt   134:
                    135: kvm_t *kd;
                    136:
1.11      millert   137: int ufs_filestat __P((struct vnode *, struct filestat *));
                    138: int ext2fs_filestat __P((struct vnode *, struct filestat *));
1.14      deraadt   139: int isofs_filestat __P((struct vnode *, struct filestat *));
                    140: int msdos_filestat __P((struct vnode *, struct filestat *));
1.11      millert   141: int nfs_filestat __P((struct vnode *, struct filestat *));
                    142: void dofiles __P((struct kinfo_proc *));
                    143: void getinetproto __P((int));
                    144: void socktrans __P((struct socket *, int));
                    145: void usage __P((void));
                    146: void vtrans __P((struct vnode *, int, int));
                    147: int getfname __P((char *));
1.1       deraadt   148:
1.11      millert   149: int
1.1       deraadt   150: main(argc, argv)
                    151:        int argc;
                    152:        char **argv;
                    153: {
                    154:        extern char *optarg;
                    155:        extern int optind;
                    156:        register struct passwd *passwd;
                    157:        struct kinfo_proc *p, *plast;
                    158:        int arg, ch, what;
                    159:        char *memf, *nlistf;
1.2       niklas    160:        char buf[_POSIX2_LINE_MAX];
1.1       deraadt   161:        int cnt;
                    162:
                    163:        arg = 0;
                    164:        what = KERN_PROC_ALL;
                    165:        nlistf = memf = NULL;
1.8       millert   166:        while ((ch = getopt(argc, argv, "fnp:u:vN:M:")) != -1)
1.1       deraadt   167:                switch((char)ch) {
                    168:                case 'f':
                    169:                        fsflg = 1;
                    170:                        break;
                    171:                case 'M':
                    172:                        memf = optarg;
                    173:                        break;
                    174:                case 'N':
                    175:                        nlistf = optarg;
                    176:                        break;
                    177:                case 'n':
                    178:                        nflg = 1;
                    179:                        break;
                    180:                case 'p':
                    181:                        if (pflg++)
                    182:                                usage();
                    183:                        if (!isdigit(*optarg)) {
                    184:                                fprintf(stderr,
                    185:                                    "fstat: -p requires a process id\n");
                    186:                                usage();
                    187:                        }
                    188:                        what = KERN_PROC_PID;
                    189:                        arg = atoi(optarg);
                    190:                        break;
                    191:                case 'u':
                    192:                        if (uflg++)
                    193:                                usage();
                    194:                        if (!(passwd = getpwnam(optarg))) {
                    195:                                fprintf(stderr, "%s: unknown uid\n",
                    196:                                    optarg);
                    197:                                exit(1);
                    198:                        }
                    199:                        what = KERN_PROC_UID;
                    200:                        arg = passwd->pw_uid;
                    201:                        break;
                    202:                case 'v':
                    203:                        vflg = 1;
                    204:                        break;
                    205:                case '?':
                    206:                default:
                    207:                        usage();
                    208:                }
                    209:
                    210:        if (*(argv += optind)) {
                    211:                for (; *argv; ++argv) {
                    212:                        if (getfname(*argv))
                    213:                                checkfile = 1;
                    214:                }
                    215:                if (!checkfile) /* file(s) specified, but none accessable */
                    216:                        exit(1);
                    217:        }
                    218:
                    219:        ALLOC_OFILES(256);      /* reserve space for file pointers */
                    220:
                    221:        if (fsflg && !checkfile) {
                    222:                /* -f with no files means use wd */
                    223:                if (getfname(".") == 0)
                    224:                        exit(1);
                    225:                checkfile = 1;
                    226:        }
                    227:
                    228:        /*
                    229:         * Discard setgid privileges if not the running kernel so that bad
                    230:         * guys can't print interesting stuff from kernel memory.
                    231:         */
1.7       tholo     232:        if (nlistf != NULL || memf != NULL) {
                    233:                setegid(getgid());
1.1       deraadt   234:                setgid(getgid());
1.7       tholo     235:        }
1.1       deraadt   236:
1.2       niklas    237:        if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL) {
                    238:                fprintf(stderr, "fstat: %s\n", buf);
1.1       deraadt   239:                exit(1);
                    240:        }
1.16    ! deraadt   241:
        !           242:        setegid(getgid());
        !           243:        setgid(getgid());
        !           244:
1.1       deraadt   245:        if ((p = kvm_getprocs(kd, what, arg, &cnt)) == NULL) {
                    246:                fprintf(stderr, "fstat: %s\n", kvm_geterr(kd));
                    247:                exit(1);
                    248:        }
                    249:        if (nflg)
                    250:                printf("%s",
                    251: "USER     CMD          PID   FD  DEV    INUM       MODE SZ|DV R/W");
                    252:        else
                    253:                printf("%s",
                    254: "USER     CMD          PID   FD MOUNT      INUM MODE         SZ|DV R/W");
                    255:        if (checkfile && fsflg == 0)
                    256:                printf(" NAME\n");
                    257:        else
                    258:                putchar('\n');
                    259:
                    260:        for (plast = &p[cnt]; p < plast; ++p) {
                    261:                if (p->kp_proc.p_stat == SZOMB)
                    262:                        continue;
                    263:                dofiles(p);
                    264:        }
                    265:        exit(0);
                    266: }
                    267:
                    268: char   *Uname, *Comm;
1.14      deraadt   269: pid_t  Pid;
1.1       deraadt   270:
                    271: #define PREFIX(i) printf("%-8.8s %-10s %5d", Uname, Comm, Pid); \
                    272:        switch(i) { \
                    273:        case TEXT: \
                    274:                printf(" text"); \
                    275:                break; \
                    276:        case CDIR: \
                    277:                printf("   wd"); \
                    278:                break; \
                    279:        case RDIR: \
                    280:                printf(" root"); \
                    281:                break; \
                    282:        case TRACE: \
                    283:                printf("   tr"); \
                    284:                break; \
                    285:        default: \
                    286:                printf(" %4d", i); \
                    287:                break; \
                    288:        }
                    289:
                    290: /*
                    291:  * print open files attributed to this process
                    292:  */
                    293: void
                    294: dofiles(kp)
                    295:        struct kinfo_proc *kp;
                    296: {
1.11      millert   297:        int i;
1.1       deraadt   298:        struct file file;
                    299:        struct filedesc0 filed0;
                    300: #define        filed   filed0.fd_fd
                    301:        struct proc *p = &kp->kp_proc;
                    302:        struct eproc *ep = &kp->kp_eproc;
                    303:
                    304:        extern char *user_from_uid();
                    305:
                    306:        Uname = user_from_uid(ep->e_ucred.cr_uid, 0);
                    307:        Pid = p->p_pid;
                    308:        Comm = p->p_comm;
                    309:
                    310:        if (p->p_fd == NULL)
                    311:                return;
                    312:        if (!KVM_READ(p->p_fd, &filed0, sizeof (filed0))) {
1.11      millert   313:                dprintf(stderr, "can't read filedesc at %p for pid %d\n",
1.1       deraadt   314:                        p->p_fd, Pid);
                    315:                return;
                    316:        }
                    317:        if (filed.fd_nfiles < 0 || filed.fd_lastfile >= filed.fd_nfiles ||
                    318:            filed.fd_freefile > filed.fd_lastfile + 1) {
1.11      millert   319:                dprintf(stderr, "filedesc corrupted at %p for pid %d\n",
1.1       deraadt   320:                        p->p_fd, Pid);
                    321:                return;
                    322:        }
                    323:        /*
                    324:         * root directory vnode, if one
                    325:         */
                    326:        if (filed.fd_rdir)
                    327:                vtrans(filed.fd_rdir, RDIR, FREAD);
                    328:        /*
                    329:         * current working directory vnode
                    330:         */
                    331:        vtrans(filed.fd_cdir, CDIR, FREAD);
                    332:        /*
                    333:         * ktrace vnode, if one
                    334:         */
                    335:        if (p->p_tracep)
                    336:                vtrans(p->p_tracep, TRACE, FREAD|FWRITE);
                    337:        /*
                    338:         * open files
                    339:         */
                    340: #define FPSIZE (sizeof (struct file *))
                    341:        ALLOC_OFILES(filed.fd_lastfile+1);
                    342:        if (filed.fd_nfiles > NDFILE) {
                    343:                if (!KVM_READ(filed.fd_ofiles, ofiles,
                    344:                    (filed.fd_lastfile+1) * FPSIZE)) {
                    345:                        dprintf(stderr,
1.11      millert   346:                            "can't read file structures at %p for pid %d\n",
1.1       deraadt   347:                            filed.fd_ofiles, Pid);
                    348:                        return;
                    349:                }
                    350:        } else
                    351:                bcopy(filed0.fd_dfiles, ofiles, (filed.fd_lastfile+1) * FPSIZE);
                    352:        for (i = 0; i <= filed.fd_lastfile; i++) {
                    353:                if (ofiles[i] == NULL)
                    354:                        continue;
                    355:                if (!KVM_READ(ofiles[i], &file, sizeof (struct file))) {
1.11      millert   356:                        dprintf(stderr, "can't read file %d at %p for pid %d\n",
1.1       deraadt   357:                                i, ofiles[i], Pid);
                    358:                        continue;
                    359:                }
                    360:                if (file.f_type == DTYPE_VNODE)
                    361:                        vtrans((struct vnode *)file.f_data, i, file.f_flag);
                    362:                else if (file.f_type == DTYPE_SOCKET) {
                    363:                        if (checkfile == 0)
                    364:                                socktrans((struct socket *)file.f_data, i);
                    365:                }
                    366:                else {
                    367:                        dprintf(stderr,
                    368:                                "unknown file type %d for file %d of pid %d\n",
                    369:                                file.f_type, i, Pid);
                    370:                }
                    371:        }
                    372: }
                    373:
                    374: void
                    375: vtrans(vp, i, flag)
                    376:        struct vnode *vp;
                    377:        int i;
                    378:        int flag;
                    379: {
                    380:        struct vnode vn;
                    381:        struct filestat fst;
1.12      deraadt   382:        char rw[3], mode[17];
1.1       deraadt   383:        char *badtype = NULL, *filename, *getmnton();
                    384:
                    385:        filename = badtype = NULL;
                    386:        if (!KVM_READ(vp, &vn, sizeof (struct vnode))) {
1.11      millert   387:                dprintf(stderr, "can't read vnode at %p for pid %d\n",
1.1       deraadt   388:                        vp, Pid);
                    389:                return;
                    390:        }
                    391:        if (vn.v_type == VNON || vn.v_tag == VT_NON)
                    392:                badtype = "none";
                    393:        else if (vn.v_type == VBAD)
                    394:                badtype = "bad";
                    395:        else
                    396:                switch (vn.v_tag) {
                    397:                case VT_UFS:
                    398:                case VT_MFS:
                    399:                        if (!ufs_filestat(&vn, &fst))
                    400:                                badtype = "error";
                    401:                        break;
                    402:                case VT_NFS:
                    403:                        if (!nfs_filestat(&vn, &fst))
                    404:                                badtype = "error";
                    405:                        break;
1.9       downsj    406:                case VT_EXT2FS:
                    407:                        if (!ext2fs_filestat(&vn, &fst))
                    408:                                badtype = "error";
                    409:                        break;
1.14      deraadt   410:                case VT_ISOFS:
                    411:                        if (!isofs_filestat(&vn, &fst))
                    412:                                badtype = "error";
                    413:                        break;
                    414:                case VT_MSDOSFS:
                    415:                        if (!msdos_filestat(&vn, &fst))
                    416:                                badtype = "error";
                    417:                        break;
1.1       deraadt   418:                default: {
1.12      deraadt   419:                        static char unknown[30];
1.1       deraadt   420:                        sprintf(badtype = unknown, "?(%x)", vn.v_tag);
1.10      deraadt   421:                        break;
1.1       deraadt   422:                }
                    423:        }
                    424:        if (checkfile) {
                    425:                int fsmatch = 0;
                    426:                register DEVS *d;
                    427:
                    428:                if (badtype)
                    429:                        return;
                    430:                for (d = devs; d != NULL; d = d->next)
                    431:                        if (d->fsid == fst.fsid) {
                    432:                                fsmatch = 1;
                    433:                                if (d->ino == fst.fileid) {
                    434:                                        filename = d->name;
                    435:                                        break;
                    436:                                }
                    437:                        }
                    438:                if (fsmatch == 0 || (filename == NULL && fsflg == 0))
                    439:                        return;
                    440:        }
                    441:        PREFIX(i);
                    442:        if (badtype) {
                    443:                (void)printf(" -         -  %10s    -\n", badtype);
                    444:                return;
                    445:        }
                    446:        if (nflg)
                    447:                (void)printf(" %2d,%-2d", major(fst.fsid), minor(fst.fsid));
                    448:        else
                    449:                (void)printf(" %-8s", getmnton(vn.v_mount));
                    450:        if (nflg)
                    451:                (void)sprintf(mode, "%o", fst.mode);
                    452:        else
                    453:                strmode(fst.mode, mode);
1.11      millert   454:        (void)printf(" %6ld %10s", fst.fileid, mode);
1.1       deraadt   455:        switch (vn.v_type) {
                    456:        case VBLK:
                    457:        case VCHR: {
                    458:                char *name;
                    459:
                    460:                if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ?
                    461:                    S_IFCHR : S_IFBLK)) == NULL))
                    462:                        printf("  %2d,%-2d", major(fst.rdev), minor(fst.rdev));
                    463:                else
                    464:                        printf(" %6s", name);
                    465:                break;
                    466:        }
                    467:        default:
1.11      millert   468:                printf(" %6qd", fst.size);
1.1       deraadt   469:        }
                    470:        rw[0] = '\0';
                    471:        if (flag & FREAD)
                    472:                strcat(rw, "r");
                    473:        if (flag & FWRITE)
                    474:                strcat(rw, "w");
                    475:        printf(" %2s", rw);
                    476:        if (filename && !fsflg)
                    477:                printf("  %s", filename);
                    478:        putchar('\n');
                    479: }
                    480:
                    481: int
                    482: ufs_filestat(vp, fsp)
                    483:        struct vnode *vp;
                    484:        struct filestat *fsp;
                    485: {
                    486:        struct inode inode;
                    487:
                    488:        if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
1.11      millert   489:                dprintf(stderr, "can't read inode at %p for pid %d\n",
1.1       deraadt   490:                        VTOI(vp), Pid);
                    491:                return 0;
                    492:        }
                    493:        fsp->fsid = inode.i_dev & 0xffff;
                    494:        fsp->fileid = (long)inode.i_number;
1.11      millert   495:        fsp->mode = inode.i_ffs_mode;
                    496:        fsp->size = inode.i_ffs_size;
1.9       downsj    497:        fsp->rdev = inode.i_ffs_rdev;
                    498:
                    499:        return 1;
                    500: }
                    501:
                    502: int
                    503: ext2fs_filestat(vp, fsp)
                    504:        struct vnode *vp;
                    505:        struct filestat *fsp;
                    506: {
                    507:        struct inode inode;
                    508:
                    509:        if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
1.11      millert   510:                dprintf(stderr, "can't read inode at %p for pid %d\n",
1.9       downsj    511:                        VTOI(vp), Pid);
                    512:                return 0;
                    513:        }
                    514:        fsp->fsid = inode.i_dev & 0xffff;
                    515:        fsp->fileid = (long)inode.i_number;
1.11      millert   516:        fsp->mode = inode.i_e2fs_mode;
                    517:        fsp->size = inode.i_e2fs_size;
1.9       downsj    518:        fsp->rdev = 0;  /* XXX */
1.14      deraadt   519:
                    520:        return 1;
                    521: }
                    522:
                    523: int
                    524: msdos_filestat(vp, fsp)
                    525:        struct vnode *vp;
                    526:        struct filestat *fsp;
                    527: {
                    528: #if 0
                    529:        struct inode inode;
                    530:
                    531:        if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
                    532:                dprintf(stderr, "can't read inode at %p for pid %d\n",
                    533:                        VTOI(vp), Pid);
                    534:                return 0;
                    535:        }
                    536:        fsp->fsid = inode.i_dev & 0xffff;
                    537:        fsp->fileid = (long)inode.i_number;
                    538:        fsp->mode = inode.i_e2fs_mode;
                    539:        fsp->size = inode.i_e2fs_size;
                    540:        fsp->rdev = 0;  /* XXX */
                    541: #endif
1.1       deraadt   542:
                    543:        return 1;
                    544: }
                    545:
                    546: int
                    547: nfs_filestat(vp, fsp)
                    548:        struct vnode *vp;
                    549:        struct filestat *fsp;
                    550: {
                    551:        struct nfsnode nfsnode;
                    552:        register mode_t mode;
                    553:
                    554:        if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
1.11      millert   555:                dprintf(stderr, "can't read nfsnode at %p for pid %d\n",
1.1       deraadt   556:                        VTONFS(vp), Pid);
                    557:                return 0;
                    558:        }
                    559:        fsp->fsid = nfsnode.n_vattr.va_fsid;
                    560:        fsp->fileid = nfsnode.n_vattr.va_fileid;
                    561:        fsp->size = nfsnode.n_size;
                    562:        fsp->rdev = nfsnode.n_vattr.va_rdev;
                    563:        mode = (mode_t)nfsnode.n_vattr.va_mode;
                    564:        switch (vp->v_type) {
                    565:        case VREG:
                    566:                mode |= S_IFREG;
                    567:                break;
                    568:        case VDIR:
                    569:                mode |= S_IFDIR;
                    570:                break;
                    571:        case VBLK:
                    572:                mode |= S_IFBLK;
                    573:                break;
                    574:        case VCHR:
                    575:                mode |= S_IFCHR;
                    576:                break;
                    577:        case VLNK:
                    578:                mode |= S_IFLNK;
                    579:                break;
                    580:        case VSOCK:
                    581:                mode |= S_IFSOCK;
                    582:                break;
                    583:        case VFIFO:
                    584:                mode |= S_IFIFO;
                    585:                break;
                    586:        };
                    587:        fsp->mode = mode;
                    588:
                    589:        return 1;
                    590: }
                    591:
                    592:
                    593: char *
                    594: getmnton(m)
                    595:        struct mount *m;
                    596: {
                    597:        static struct mount mount;
                    598:        static struct mtab {
                    599:                struct mtab *next;
                    600:                struct mount *m;
                    601:                char mntonname[MNAMELEN];
                    602:        } *mhead = NULL;
                    603:        register struct mtab *mt;
                    604:
                    605:        for (mt = mhead; mt != NULL; mt = mt->next)
                    606:                if (m == mt->m)
                    607:                        return (mt->mntonname);
                    608:        if (!KVM_READ(m, &mount, sizeof(struct mount))) {
1.11      millert   609:                fprintf(stderr, "can't read mount table at %p\n", m);
1.1       deraadt   610:                return (NULL);
                    611:        }
                    612:        if ((mt = malloc(sizeof (struct mtab))) == NULL) {
                    613:                fprintf(stderr, "fstat: %s\n", strerror(errno));
                    614:                exit(1);
                    615:        }
                    616:        mt->m = m;
                    617:        bcopy(&mount.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN);
                    618:        mt->next = mhead;
                    619:        mhead = mt;
                    620:        return (mt->mntonname);
                    621: }
                    622:
                    623: void
                    624: socktrans(sock, i)
                    625:        struct socket *sock;
                    626:        int i;
                    627: {
                    628:        static char *stypename[] = {
                    629:                "unused",       /* 0 */
                    630:                "stream",       /* 1 */
                    631:                "dgram",        /* 2 */
                    632:                "raw",          /* 3 */
                    633:                "rdm",          /* 4 */
                    634:                "seqpak"        /* 5 */
                    635:        };
                    636: #define        STYPEMAX 5
                    637:        struct socket   so;
                    638:        struct protosw  proto;
                    639:        struct domain   dom;
                    640:        struct inpcb    inpcb;
                    641:        struct unpcb    unpcb;
                    642:        int len;
1.4       deraadt   643:        char dname[32];
1.1       deraadt   644:
                    645:        PREFIX(i);
                    646:
                    647:        /* fill in socket */
                    648:        if (!KVM_READ(sock, &so, sizeof(struct socket))) {
1.11      millert   649:                dprintf(stderr, "can't read sock at %p\n", sock);
1.1       deraadt   650:                goto bad;
                    651:        }
                    652:
                    653:        /* fill in protosw entry */
                    654:        if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
1.11      millert   655:                dprintf(stderr, "can't read protosw at %p", so.so_proto);
1.1       deraadt   656:                goto bad;
                    657:        }
                    658:
                    659:        /* fill in domain */
                    660:        if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
1.11      millert   661:                dprintf(stderr, "can't read domain at %p\n", proto.pr_domain);
1.1       deraadt   662:                goto bad;
                    663:        }
                    664:
                    665:        if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
1.15      deraadt   666:            sizeof(dname) - 1)) != sizeof(dname) -1) {
1.11      millert   667:                dprintf(stderr, "can't read domain name at %p\n",
1.1       deraadt   668:                        dom.dom_name);
                    669:                dname[0] = '\0';
                    670:        }
                    671:        else
                    672:                dname[len] = '\0';
                    673:
                    674:        if ((u_short)so.so_type > STYPEMAX)
                    675:                printf("* %s ?%d", dname, so.so_type);
                    676:        else
                    677:                printf("* %s %s", dname, stypename[so.so_type]);
                    678:
                    679:        /*
                    680:         * protocol specific formatting
                    681:         *
                    682:         * Try to find interesting things to print.  For tcp, the interesting
                    683:         * thing is the address of the tcpcb, for udp and others, just the
                    684:         * inpcb (socket pcb).  For unix domain, its the address of the socket
                    685:         * pcb and the address of the connected pcb (if connected).  Otherwise
                    686:         * just print the protocol number and address of the socket itself.
                    687:         * The idea is not to duplicate netstat, but to make available enough
                    688:         * information for further analysis.
                    689:         */
                    690:        switch(dom.dom_family) {
                    691:        case AF_INET:
                    692:                getinetproto(proto.pr_protocol);
1.5       deraadt   693:                if (proto.pr_protocol == IPPROTO_TCP) {
                    694:                        if (so.so_pcb == NULL)
                    695:                                break;
                    696:                        if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
                    697:                            sizeof(struct inpcb)) != sizeof(struct inpcb)) {
1.11      millert   698:                                dprintf(stderr, "can't read inpcb at %p\n",
1.5       deraadt   699:                                    so.so_pcb);
                    700:                                goto bad;
                    701:                        }
1.11      millert   702:                        printf(" %p", inpcb.inp_ppcb);
1.5       deraadt   703:                        printf(" %s:%d",
                    704:                            inpcb.inp_laddr.s_addr == INADDR_ANY ? "*" :
                    705:                            inet_ntoa(inpcb.inp_laddr),
                    706:                            ntohs(inpcb.inp_lport));
                    707:                        if (inpcb.inp_fport)
1.13      deraadt   708:                                if (so.so_state & SS_CONNECTOUT)
                    709:                                        printf(" --> ");
                    710:                                else
                    711:                                        printf(" <-- ");
                    712:                                printf("%s:%d",
1.5       deraadt   713:                                    inpcb.inp_faddr.s_addr == INADDR_ANY ? "*" :
                    714:                                    inet_ntoa(inpcb.inp_faddr),
                    715:                                    ntohs(inpcb.inp_fport));
                    716:                } else if (proto.pr_protocol == IPPROTO_UDP) {
                    717:                        if (so.so_pcb == NULL)
                    718:                                break;
                    719:                        if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
                    720:                            sizeof(struct inpcb)) != sizeof(struct inpcb)) {
1.11      millert   721:                                dprintf(stderr, "can't read inpcb at %p\n",
1.5       deraadt   722:                                    so.so_pcb);
                    723:                                goto bad;
1.1       deraadt   724:                        }
1.5       deraadt   725:                        printf(" %s:%d",
                    726:                            inpcb.inp_laddr.s_addr == INADDR_ANY ? "*" :
                    727:                            inet_ntoa(inpcb.inp_laddr),
                    728:                            ntohs(inpcb.inp_lport));
                    729:                        if (inpcb.inp_fport)
1.6       deraadt   730:                                printf(" <-> %s:%d",
1.5       deraadt   731:                                    inpcb.inp_faddr.s_addr == INADDR_ANY ? "*" :
                    732:                                    inet_ntoa(inpcb.inp_faddr),
                    733:                                    ntohs(inpcb.inp_fport));
                    734:                } else if (so.so_pcb)
1.11      millert   735:                        printf(" %p", so.so_pcb);
1.1       deraadt   736:                break;
                    737:        case AF_UNIX:
                    738:                /* print address of pcb and connected pcb */
                    739:                if (so.so_pcb) {
1.11      millert   740:                        printf(" %p", so.so_pcb);
1.1       deraadt   741:                        if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb,
                    742:                            sizeof(struct unpcb)) != sizeof(struct unpcb)){
1.11      millert   743:                                dprintf(stderr, "can't read unpcb at %p\n",
1.1       deraadt   744:                                    so.so_pcb);
                    745:                                goto bad;
                    746:                        }
                    747:                        if (unpcb.unp_conn) {
                    748:                                char shoconn[4], *cp;
                    749:
                    750:                                cp = shoconn;
                    751:                                if (!(so.so_state & SS_CANTRCVMORE))
                    752:                                        *cp++ = '<';
                    753:                                *cp++ = '-';
                    754:                                if (!(so.so_state & SS_CANTSENDMORE))
                    755:                                        *cp++ = '>';
                    756:                                *cp = '\0';
1.11      millert   757:                                printf(" %s %p", shoconn,
                    758:                                    unpcb.unp_conn);
1.1       deraadt   759:                        }
                    760:                }
                    761:                break;
                    762:        default:
                    763:                /* print protocol number and socket address */
1.11      millert   764:                printf(" %d %p", proto.pr_protocol, sock);
1.1       deraadt   765:        }
                    766:        printf("\n");
                    767:        return;
                    768: bad:
                    769:        printf("* error\n");
                    770: }
                    771:
                    772: /*
                    773:  * getinetproto --
                    774:  *     print name of protocol number
                    775:  */
                    776: void
                    777: getinetproto(number)
                    778:        int number;
                    779: {
                    780:        char *cp;
                    781:
                    782:        switch(number) {
                    783:        case IPPROTO_IP:
                    784:                cp = "ip"; break;
                    785:        case IPPROTO_ICMP:
                    786:                cp ="icmp"; break;
                    787:        case IPPROTO_GGP:
                    788:                cp ="ggp"; break;
                    789:        case IPPROTO_TCP:
                    790:                cp ="tcp"; break;
                    791:        case IPPROTO_EGP:
                    792:                cp ="egp"; break;
                    793:        case IPPROTO_PUP:
                    794:                cp ="pup"; break;
                    795:        case IPPROTO_UDP:
                    796:                cp ="udp"; break;
                    797:        case IPPROTO_IDP:
                    798:                cp ="idp"; break;
                    799:        case IPPROTO_RAW:
                    800:                cp ="raw"; break;
                    801:        default:
                    802:                printf(" %d", number);
                    803:                return;
                    804:        }
                    805:        printf(" %s", cp);
                    806: }
                    807:
1.11      millert   808: int
1.1       deraadt   809: getfname(filename)
                    810:        char *filename;
                    811: {
                    812:        struct stat statbuf;
                    813:        DEVS *cur;
                    814:
                    815:        if (stat(filename, &statbuf)) {
                    816:                fprintf(stderr, "fstat: %s: %s\n", filename, strerror(errno));
                    817:                return(0);
                    818:        }
                    819:        if ((cur = malloc(sizeof(DEVS))) == NULL) {
                    820:                fprintf(stderr, "fstat: %s\n", strerror(errno));
                    821:                exit(1);
                    822:        }
                    823:        cur->next = devs;
                    824:        devs = cur;
                    825:
                    826:        cur->ino = statbuf.st_ino;
                    827:        cur->fsid = statbuf.st_dev & 0xffff;
                    828:        cur->name = filename;
                    829:        return(1);
                    830: }
                    831:
                    832: void
                    833: usage()
                    834: {
                    835:        (void)fprintf(stderr,
                    836:  "usage: fstat [-fnv] [-p pid] [-u user] [-N system] [-M core] [file ...]\n");
                    837:        exit(1);
                    838: }