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

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