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

1.15    ! deraadt     1: /*     $OpenBSD: fstat.c,v 1.14 1998/06/25 06:21:34 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.15    ! deraadt    44: static char *rcsid = "$OpenBSD: fstat.c,v 1.14 1998/06/25 06:21:34 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:        }
                    241:        if ((p = kvm_getprocs(kd, what, arg, &cnt)) == NULL) {
                    242:                fprintf(stderr, "fstat: %s\n", kvm_geterr(kd));
                    243:                exit(1);
                    244:        }
                    245:        if (nflg)
                    246:                printf("%s",
                    247: "USER     CMD          PID   FD  DEV    INUM       MODE SZ|DV R/W");
                    248:        else
                    249:                printf("%s",
                    250: "USER     CMD          PID   FD MOUNT      INUM MODE         SZ|DV R/W");
                    251:        if (checkfile && fsflg == 0)
                    252:                printf(" NAME\n");
                    253:        else
                    254:                putchar('\n');
                    255:
                    256:        for (plast = &p[cnt]; p < plast; ++p) {
                    257:                if (p->kp_proc.p_stat == SZOMB)
                    258:                        continue;
                    259:                dofiles(p);
                    260:        }
                    261:        exit(0);
                    262: }
                    263:
                    264: char   *Uname, *Comm;
1.14      deraadt   265: pid_t  Pid;
1.1       deraadt   266:
                    267: #define PREFIX(i) printf("%-8.8s %-10s %5d", Uname, Comm, Pid); \
                    268:        switch(i) { \
                    269:        case TEXT: \
                    270:                printf(" text"); \
                    271:                break; \
                    272:        case CDIR: \
                    273:                printf("   wd"); \
                    274:                break; \
                    275:        case RDIR: \
                    276:                printf(" root"); \
                    277:                break; \
                    278:        case TRACE: \
                    279:                printf("   tr"); \
                    280:                break; \
                    281:        default: \
                    282:                printf(" %4d", i); \
                    283:                break; \
                    284:        }
                    285:
                    286: /*
                    287:  * print open files attributed to this process
                    288:  */
                    289: void
                    290: dofiles(kp)
                    291:        struct kinfo_proc *kp;
                    292: {
1.11      millert   293:        int i;
1.1       deraadt   294:        struct file file;
                    295:        struct filedesc0 filed0;
                    296: #define        filed   filed0.fd_fd
                    297:        struct proc *p = &kp->kp_proc;
                    298:        struct eproc *ep = &kp->kp_eproc;
                    299:
                    300:        extern char *user_from_uid();
                    301:
                    302:        Uname = user_from_uid(ep->e_ucred.cr_uid, 0);
                    303:        Pid = p->p_pid;
                    304:        Comm = p->p_comm;
                    305:
                    306:        if (p->p_fd == NULL)
                    307:                return;
                    308:        if (!KVM_READ(p->p_fd, &filed0, sizeof (filed0))) {
1.11      millert   309:                dprintf(stderr, "can't read filedesc at %p for pid %d\n",
1.1       deraadt   310:                        p->p_fd, Pid);
                    311:                return;
                    312:        }
                    313:        if (filed.fd_nfiles < 0 || filed.fd_lastfile >= filed.fd_nfiles ||
                    314:            filed.fd_freefile > filed.fd_lastfile + 1) {
1.11      millert   315:                dprintf(stderr, "filedesc corrupted at %p for pid %d\n",
1.1       deraadt   316:                        p->p_fd, Pid);
                    317:                return;
                    318:        }
                    319:        /*
                    320:         * root directory vnode, if one
                    321:         */
                    322:        if (filed.fd_rdir)
                    323:                vtrans(filed.fd_rdir, RDIR, FREAD);
                    324:        /*
                    325:         * current working directory vnode
                    326:         */
                    327:        vtrans(filed.fd_cdir, CDIR, FREAD);
                    328:        /*
                    329:         * ktrace vnode, if one
                    330:         */
                    331:        if (p->p_tracep)
                    332:                vtrans(p->p_tracep, TRACE, FREAD|FWRITE);
                    333:        /*
                    334:         * open files
                    335:         */
                    336: #define FPSIZE (sizeof (struct file *))
                    337:        ALLOC_OFILES(filed.fd_lastfile+1);
                    338:        if (filed.fd_nfiles > NDFILE) {
                    339:                if (!KVM_READ(filed.fd_ofiles, ofiles,
                    340:                    (filed.fd_lastfile+1) * FPSIZE)) {
                    341:                        dprintf(stderr,
1.11      millert   342:                            "can't read file structures at %p for pid %d\n",
1.1       deraadt   343:                            filed.fd_ofiles, Pid);
                    344:                        return;
                    345:                }
                    346:        } else
                    347:                bcopy(filed0.fd_dfiles, ofiles, (filed.fd_lastfile+1) * FPSIZE);
                    348:        for (i = 0; i <= filed.fd_lastfile; i++) {
                    349:                if (ofiles[i] == NULL)
                    350:                        continue;
                    351:                if (!KVM_READ(ofiles[i], &file, sizeof (struct file))) {
1.11      millert   352:                        dprintf(stderr, "can't read file %d at %p for pid %d\n",
1.1       deraadt   353:                                i, ofiles[i], Pid);
                    354:                        continue;
                    355:                }
                    356:                if (file.f_type == DTYPE_VNODE)
                    357:                        vtrans((struct vnode *)file.f_data, i, file.f_flag);
                    358:                else if (file.f_type == DTYPE_SOCKET) {
                    359:                        if (checkfile == 0)
                    360:                                socktrans((struct socket *)file.f_data, i);
                    361:                }
                    362:                else {
                    363:                        dprintf(stderr,
                    364:                                "unknown file type %d for file %d of pid %d\n",
                    365:                                file.f_type, i, Pid);
                    366:                }
                    367:        }
                    368: }
                    369:
                    370: void
                    371: vtrans(vp, i, flag)
                    372:        struct vnode *vp;
                    373:        int i;
                    374:        int flag;
                    375: {
                    376:        struct vnode vn;
                    377:        struct filestat fst;
1.12      deraadt   378:        char rw[3], mode[17];
1.1       deraadt   379:        char *badtype = NULL, *filename, *getmnton();
                    380:
                    381:        filename = badtype = NULL;
                    382:        if (!KVM_READ(vp, &vn, sizeof (struct vnode))) {
1.11      millert   383:                dprintf(stderr, "can't read vnode at %p for pid %d\n",
1.1       deraadt   384:                        vp, Pid);
                    385:                return;
                    386:        }
                    387:        if (vn.v_type == VNON || vn.v_tag == VT_NON)
                    388:                badtype = "none";
                    389:        else if (vn.v_type == VBAD)
                    390:                badtype = "bad";
                    391:        else
                    392:                switch (vn.v_tag) {
                    393:                case VT_UFS:
                    394:                case VT_MFS:
                    395:                        if (!ufs_filestat(&vn, &fst))
                    396:                                badtype = "error";
                    397:                        break;
                    398:                case VT_NFS:
                    399:                        if (!nfs_filestat(&vn, &fst))
                    400:                                badtype = "error";
                    401:                        break;
1.9       downsj    402:                case VT_EXT2FS:
                    403:                        if (!ext2fs_filestat(&vn, &fst))
                    404:                                badtype = "error";
                    405:                        break;
1.14      deraadt   406:                case VT_ISOFS:
                    407:                        if (!isofs_filestat(&vn, &fst))
                    408:                                badtype = "error";
                    409:                        break;
                    410:                case VT_MSDOSFS:
                    411:                        if (!msdos_filestat(&vn, &fst))
                    412:                                badtype = "error";
                    413:                        break;
1.1       deraadt   414:                default: {
1.12      deraadt   415:                        static char unknown[30];
1.1       deraadt   416:                        sprintf(badtype = unknown, "?(%x)", vn.v_tag);
1.10      deraadt   417:                        break;
1.1       deraadt   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);
1.11      millert   450:        (void)printf(" %6ld %10s", fst.fileid, mode);
1.1       deraadt   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:
1.11      millert   464:                printf(" %6qd", fst.size);
1.1       deraadt   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))) {
1.11      millert   485:                dprintf(stderr, "can't read inode at %p for pid %d\n",
1.1       deraadt   486:                        VTOI(vp), Pid);
                    487:                return 0;
                    488:        }
                    489:        fsp->fsid = inode.i_dev & 0xffff;
                    490:        fsp->fileid = (long)inode.i_number;
1.11      millert   491:        fsp->mode = inode.i_ffs_mode;
                    492:        fsp->size = inode.i_ffs_size;
1.9       downsj    493:        fsp->rdev = inode.i_ffs_rdev;
                    494:
                    495:        return 1;
                    496: }
                    497:
                    498: int
                    499: ext2fs_filestat(vp, fsp)
                    500:        struct vnode *vp;
                    501:        struct filestat *fsp;
                    502: {
                    503:        struct inode inode;
                    504:
                    505:        if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
1.11      millert   506:                dprintf(stderr, "can't read inode at %p for pid %d\n",
1.9       downsj    507:                        VTOI(vp), Pid);
                    508:                return 0;
                    509:        }
                    510:        fsp->fsid = inode.i_dev & 0xffff;
                    511:        fsp->fileid = (long)inode.i_number;
1.11      millert   512:        fsp->mode = inode.i_e2fs_mode;
                    513:        fsp->size = inode.i_e2fs_size;
1.9       downsj    514:        fsp->rdev = 0;  /* XXX */
1.14      deraadt   515:
                    516:        return 1;
                    517: }
                    518:
                    519: int
                    520: msdos_filestat(vp, fsp)
                    521:        struct vnode *vp;
                    522:        struct filestat *fsp;
                    523: {
                    524: #if 0
                    525:        struct inode inode;
                    526:
                    527:        if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
                    528:                dprintf(stderr, "can't read inode at %p for pid %d\n",
                    529:                        VTOI(vp), Pid);
                    530:                return 0;
                    531:        }
                    532:        fsp->fsid = inode.i_dev & 0xffff;
                    533:        fsp->fileid = (long)inode.i_number;
                    534:        fsp->mode = inode.i_e2fs_mode;
                    535:        fsp->size = inode.i_e2fs_size;
                    536:        fsp->rdev = 0;  /* XXX */
                    537: #endif
1.1       deraadt   538:
                    539:        return 1;
                    540: }
                    541:
                    542: int
                    543: nfs_filestat(vp, fsp)
                    544:        struct vnode *vp;
                    545:        struct filestat *fsp;
                    546: {
                    547:        struct nfsnode nfsnode;
                    548:        register mode_t mode;
                    549:
                    550:        if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
1.11      millert   551:                dprintf(stderr, "can't read nfsnode at %p for pid %d\n",
1.1       deraadt   552:                        VTONFS(vp), Pid);
                    553:                return 0;
                    554:        }
                    555:        fsp->fsid = nfsnode.n_vattr.va_fsid;
                    556:        fsp->fileid = nfsnode.n_vattr.va_fileid;
                    557:        fsp->size = nfsnode.n_size;
                    558:        fsp->rdev = nfsnode.n_vattr.va_rdev;
                    559:        mode = (mode_t)nfsnode.n_vattr.va_mode;
                    560:        switch (vp->v_type) {
                    561:        case VREG:
                    562:                mode |= S_IFREG;
                    563:                break;
                    564:        case VDIR:
                    565:                mode |= S_IFDIR;
                    566:                break;
                    567:        case VBLK:
                    568:                mode |= S_IFBLK;
                    569:                break;
                    570:        case VCHR:
                    571:                mode |= S_IFCHR;
                    572:                break;
                    573:        case VLNK:
                    574:                mode |= S_IFLNK;
                    575:                break;
                    576:        case VSOCK:
                    577:                mode |= S_IFSOCK;
                    578:                break;
                    579:        case VFIFO:
                    580:                mode |= S_IFIFO;
                    581:                break;
                    582:        };
                    583:        fsp->mode = mode;
                    584:
                    585:        return 1;
                    586: }
                    587:
                    588:
                    589: char *
                    590: getmnton(m)
                    591:        struct mount *m;
                    592: {
                    593:        static struct mount mount;
                    594:        static struct mtab {
                    595:                struct mtab *next;
                    596:                struct mount *m;
                    597:                char mntonname[MNAMELEN];
                    598:        } *mhead = NULL;
                    599:        register struct mtab *mt;
                    600:
                    601:        for (mt = mhead; mt != NULL; mt = mt->next)
                    602:                if (m == mt->m)
                    603:                        return (mt->mntonname);
                    604:        if (!KVM_READ(m, &mount, sizeof(struct mount))) {
1.11      millert   605:                fprintf(stderr, "can't read mount table at %p\n", m);
1.1       deraadt   606:                return (NULL);
                    607:        }
                    608:        if ((mt = malloc(sizeof (struct mtab))) == NULL) {
                    609:                fprintf(stderr, "fstat: %s\n", strerror(errno));
                    610:                exit(1);
                    611:        }
                    612:        mt->m = m;
                    613:        bcopy(&mount.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN);
                    614:        mt->next = mhead;
                    615:        mhead = mt;
                    616:        return (mt->mntonname);
                    617: }
                    618:
                    619: void
                    620: socktrans(sock, i)
                    621:        struct socket *sock;
                    622:        int i;
                    623: {
                    624:        static char *stypename[] = {
                    625:                "unused",       /* 0 */
                    626:                "stream",       /* 1 */
                    627:                "dgram",        /* 2 */
                    628:                "raw",          /* 3 */
                    629:                "rdm",          /* 4 */
                    630:                "seqpak"        /* 5 */
                    631:        };
                    632: #define        STYPEMAX 5
                    633:        struct socket   so;
                    634:        struct protosw  proto;
                    635:        struct domain   dom;
                    636:        struct inpcb    inpcb;
                    637:        struct unpcb    unpcb;
                    638:        int len;
1.4       deraadt   639:        char dname[32];
1.1       deraadt   640:
                    641:        PREFIX(i);
                    642:
                    643:        /* fill in socket */
                    644:        if (!KVM_READ(sock, &so, sizeof(struct socket))) {
1.11      millert   645:                dprintf(stderr, "can't read sock at %p\n", sock);
1.1       deraadt   646:                goto bad;
                    647:        }
                    648:
                    649:        /* fill in protosw entry */
                    650:        if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
1.11      millert   651:                dprintf(stderr, "can't read protosw at %p", so.so_proto);
1.1       deraadt   652:                goto bad;
                    653:        }
                    654:
                    655:        /* fill in domain */
                    656:        if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
1.11      millert   657:                dprintf(stderr, "can't read domain at %p\n", proto.pr_domain);
1.1       deraadt   658:                goto bad;
                    659:        }
                    660:
                    661:        if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
1.15    ! deraadt   662:            sizeof(dname) - 1)) != sizeof(dname) -1) {
1.11      millert   663:                dprintf(stderr, "can't read domain name at %p\n",
1.1       deraadt   664:                        dom.dom_name);
                    665:                dname[0] = '\0';
                    666:        }
                    667:        else
                    668:                dname[len] = '\0';
                    669:
                    670:        if ((u_short)so.so_type > STYPEMAX)
                    671:                printf("* %s ?%d", dname, so.so_type);
                    672:        else
                    673:                printf("* %s %s", dname, stypename[so.so_type]);
                    674:
                    675:        /*
                    676:         * protocol specific formatting
                    677:         *
                    678:         * Try to find interesting things to print.  For tcp, the interesting
                    679:         * thing is the address of the tcpcb, for udp and others, just the
                    680:         * inpcb (socket pcb).  For unix domain, its the address of the socket
                    681:         * pcb and the address of the connected pcb (if connected).  Otherwise
                    682:         * just print the protocol number and address of the socket itself.
                    683:         * The idea is not to duplicate netstat, but to make available enough
                    684:         * information for further analysis.
                    685:         */
                    686:        switch(dom.dom_family) {
                    687:        case AF_INET:
                    688:                getinetproto(proto.pr_protocol);
1.5       deraadt   689:                if (proto.pr_protocol == IPPROTO_TCP) {
                    690:                        if (so.so_pcb == NULL)
                    691:                                break;
                    692:                        if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
                    693:                            sizeof(struct inpcb)) != sizeof(struct inpcb)) {
1.11      millert   694:                                dprintf(stderr, "can't read inpcb at %p\n",
1.5       deraadt   695:                                    so.so_pcb);
                    696:                                goto bad;
                    697:                        }
1.11      millert   698:                        printf(" %p", inpcb.inp_ppcb);
1.5       deraadt   699:                        printf(" %s:%d",
                    700:                            inpcb.inp_laddr.s_addr == INADDR_ANY ? "*" :
                    701:                            inet_ntoa(inpcb.inp_laddr),
                    702:                            ntohs(inpcb.inp_lport));
                    703:                        if (inpcb.inp_fport)
1.13      deraadt   704:                                if (so.so_state & SS_CONNECTOUT)
                    705:                                        printf(" --> ");
                    706:                                else
                    707:                                        printf(" <-- ");
                    708:                                printf("%s:%d",
1.5       deraadt   709:                                    inpcb.inp_faddr.s_addr == INADDR_ANY ? "*" :
                    710:                                    inet_ntoa(inpcb.inp_faddr),
                    711:                                    ntohs(inpcb.inp_fport));
                    712:                } else if (proto.pr_protocol == IPPROTO_UDP) {
                    713:                        if (so.so_pcb == NULL)
                    714:                                break;
                    715:                        if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
                    716:                            sizeof(struct inpcb)) != sizeof(struct inpcb)) {
1.11      millert   717:                                dprintf(stderr, "can't read inpcb at %p\n",
1.5       deraadt   718:                                    so.so_pcb);
                    719:                                goto bad;
1.1       deraadt   720:                        }
1.5       deraadt   721:                        printf(" %s:%d",
                    722:                            inpcb.inp_laddr.s_addr == INADDR_ANY ? "*" :
                    723:                            inet_ntoa(inpcb.inp_laddr),
                    724:                            ntohs(inpcb.inp_lport));
                    725:                        if (inpcb.inp_fport)
1.6       deraadt   726:                                printf(" <-> %s:%d",
1.5       deraadt   727:                                    inpcb.inp_faddr.s_addr == INADDR_ANY ? "*" :
                    728:                                    inet_ntoa(inpcb.inp_faddr),
                    729:                                    ntohs(inpcb.inp_fport));
                    730:                } else if (so.so_pcb)
1.11      millert   731:                        printf(" %p", so.so_pcb);
1.1       deraadt   732:                break;
                    733:        case AF_UNIX:
                    734:                /* print address of pcb and connected pcb */
                    735:                if (so.so_pcb) {
1.11      millert   736:                        printf(" %p", so.so_pcb);
1.1       deraadt   737:                        if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb,
                    738:                            sizeof(struct unpcb)) != sizeof(struct unpcb)){
1.11      millert   739:                                dprintf(stderr, "can't read unpcb at %p\n",
1.1       deraadt   740:                                    so.so_pcb);
                    741:                                goto bad;
                    742:                        }
                    743:                        if (unpcb.unp_conn) {
                    744:                                char shoconn[4], *cp;
                    745:
                    746:                                cp = shoconn;
                    747:                                if (!(so.so_state & SS_CANTRCVMORE))
                    748:                                        *cp++ = '<';
                    749:                                *cp++ = '-';
                    750:                                if (!(so.so_state & SS_CANTSENDMORE))
                    751:                                        *cp++ = '>';
                    752:                                *cp = '\0';
1.11      millert   753:                                printf(" %s %p", shoconn,
                    754:                                    unpcb.unp_conn);
1.1       deraadt   755:                        }
                    756:                }
                    757:                break;
                    758:        default:
                    759:                /* print protocol number and socket address */
1.11      millert   760:                printf(" %d %p", proto.pr_protocol, sock);
1.1       deraadt   761:        }
                    762:        printf("\n");
                    763:        return;
                    764: bad:
                    765:        printf("* error\n");
                    766: }
                    767:
                    768: /*
                    769:  * getinetproto --
                    770:  *     print name of protocol number
                    771:  */
                    772: void
                    773: getinetproto(number)
                    774:        int number;
                    775: {
                    776:        char *cp;
                    777:
                    778:        switch(number) {
                    779:        case IPPROTO_IP:
                    780:                cp = "ip"; break;
                    781:        case IPPROTO_ICMP:
                    782:                cp ="icmp"; break;
                    783:        case IPPROTO_GGP:
                    784:                cp ="ggp"; break;
                    785:        case IPPROTO_TCP:
                    786:                cp ="tcp"; break;
                    787:        case IPPROTO_EGP:
                    788:                cp ="egp"; break;
                    789:        case IPPROTO_PUP:
                    790:                cp ="pup"; break;
                    791:        case IPPROTO_UDP:
                    792:                cp ="udp"; break;
                    793:        case IPPROTO_IDP:
                    794:                cp ="idp"; break;
                    795:        case IPPROTO_RAW:
                    796:                cp ="raw"; break;
                    797:        default:
                    798:                printf(" %d", number);
                    799:                return;
                    800:        }
                    801:        printf(" %s", cp);
                    802: }
                    803:
1.11      millert   804: int
1.1       deraadt   805: getfname(filename)
                    806:        char *filename;
                    807: {
                    808:        struct stat statbuf;
                    809:        DEVS *cur;
                    810:
                    811:        if (stat(filename, &statbuf)) {
                    812:                fprintf(stderr, "fstat: %s: %s\n", filename, strerror(errno));
                    813:                return(0);
                    814:        }
                    815:        if ((cur = malloc(sizeof(DEVS))) == NULL) {
                    816:                fprintf(stderr, "fstat: %s\n", strerror(errno));
                    817:                exit(1);
                    818:        }
                    819:        cur->next = devs;
                    820:        devs = cur;
                    821:
                    822:        cur->ino = statbuf.st_ino;
                    823:        cur->fsid = statbuf.st_dev & 0xffff;
                    824:        cur->name = filename;
                    825:        return(1);
                    826: }
                    827:
                    828: void
                    829: usage()
                    830: {
                    831:        (void)fprintf(stderr,
                    832:  "usage: fstat [-fnv] [-p pid] [-u user] [-N system] [-M core] [file ...]\n");
                    833:        exit(1);
                    834: }