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

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