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

1.55    ! pedro       1: /*     $OpenBSD: fstat.c,v 1.54 2005/12/17 13:56:02 pedro 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.
1.43      millert    15:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    16:  *    may be used to endorse or promote products derived from this software
                     17:  *    without specific prior written permission.
                     18:  *
                     19:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     20:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     21:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     22:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     23:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     24:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     25:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     26:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     27:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     28:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     29:  * SUCH DAMAGE.
                     30:  */
                     31:
                     32: #ifndef lint
                     33: static char copyright[] =
                     34: "@(#) Copyright (c) 1988, 1993\n\
                     35:        The Regents of the University of California.  All rights reserved.\n";
                     36: #endif /* not lint */
                     37:
                     38: #ifndef lint
                     39: /*static char sccsid[] = "from: @(#)fstat.c    8.1 (Berkeley) 6/6/93";*/
1.55    ! pedro      40: static char *rcsid = "$OpenBSD: fstat.c,v 1.54 2005/12/17 13:56:02 pedro Exp $";
1.1       deraadt    41: #endif /* not lint */
                     42:
                     43: #include <sys/param.h>
                     44: #include <sys/time.h>
                     45: #include <sys/proc.h>
                     46: #include <sys/user.h>
                     47: #include <sys/stat.h>
                     48: #include <sys/vnode.h>
                     49: #include <sys/socket.h>
                     50: #include <sys/socketvar.h>
                     51: #include <sys/domain.h>
                     52: #include <sys/protosw.h>
1.37      deraadt    53: #include <sys/event.h>
                     54: #include <sys/eventvar.h>
1.1       deraadt    55: #include <sys/unpcb.h>
                     56: #include <sys/sysctl.h>
                     57: #include <sys/filedesc.h>
1.42      tedu       58: #define        _KERNEL
1.23      deraadt    59: #include <sys/mount.h>
1.37      deraadt    60: #include <crypto/cryptodev.h>
1.38      deraadt    61: #include <dev/systrace.h>
1.1       deraadt    62: #include <sys/file.h>
                     63: #include <ufs/ufs/quota.h>
                     64: #include <ufs/ufs/inode.h>
                     65: #undef _KERNEL
                     66: #define NFS
1.2       niklas     67: #include <nfs/nfsproto.h>
1.1       deraadt    68: #include <nfs/rpcv2.h>
                     69: #include <nfs/nfs.h>
                     70: #include <nfs/nfsnode.h>
                     71: #undef NFS
                     72:
1.29      deraadt    73: #include <xfs/xfs_config.h>
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:
1.46      deraadt   120: int    fsflg;  /* show files on same filesystem as file(s) argument */
                    121: int    pflg;   /* show files open by a particular pid */
                    122: int    uflg;   /* show files open by a particular (effective) user */
1.38      deraadt   123: int    checkfile; /* true if restricting to particular files or filesystems */
1.1       deraadt   124: int    nflg;   /* (numerical) display f.s. and rdev as dev_t */
1.28      hugh      125: int    oflg;   /* display file offset */
1.1       deraadt   126: int    vflg;   /* display errors in locating kernel data objects etc... */
                    127:
                    128: struct file **ofiles;  /* buffer of pointers to file structures */
                    129: int maxfiles;
                    130: #define ALLOC_OFILES(d)        \
                    131:        if ((d) > maxfiles) { \
                    132:                free(ofiles); \
                    133:                ofiles = malloc((d) * sizeof(struct file *)); \
1.17      mickey    134:                if (ofiles == NULL) \
                    135:                        err(1, "malloc"); \
1.1       deraadt   136:                maxfiles = (d); \
                    137:        }
                    138:
                    139: /*
1.34      deraadt   140:  * a kvm_read that returns true if everything is read
1.1       deraadt   141:  */
                    142: #define KVM_READ(kaddr, paddr, len) \
1.11      millert   143:        (kvm_read(kd, (u_long)(kaddr), (void *)(paddr), (len)) == (len))
1.1       deraadt   144:
                    145: kvm_t *kd;
                    146:
1.35      millert   147: int ufs_filestat(struct vnode *, struct filestat *);
                    148: int ext2fs_filestat(struct vnode *, struct filestat *);
                    149: int isofs_filestat(struct vnode *, struct filestat *);
                    150: int msdos_filestat(struct vnode *, struct filestat *);
                    151: int nfs_filestat(struct vnode *, struct filestat *);
                    152: int xfs_filestat(struct vnode *, struct filestat *);
1.49      millert   153: void dofiles(struct kinfo_proc2 *);
1.35      millert   154: void getinetproto(int);
                    155: void socktrans(struct socket *, int);
                    156: void usage(void);
                    157: void vtrans(struct vnode *, int, int, off_t);
                    158: int getfname(char *);
                    159: void pipetrans(struct pipe *, int);
1.37      deraadt   160: void kqueuetrans(struct kqueue *, int);
1.47      deraadt   161: void cryptotrans(void *, int);
                    162: void systracetrans(struct fsystrace *, int);
                    163: char *getmnton(struct mount *);
                    164: const char *inet6_addrstr(struct in6_addr *);
1.1       deraadt   165:
1.11      millert   166: int
1.40      deraadt   167: main(int argc, char *argv[])
1.1       deraadt   168: {
                    169:        extern char *optarg;
                    170:        extern int optind;
1.32      mpech     171:        struct passwd *passwd;
1.49      millert   172:        struct kinfo_proc2 *p, *plast;
1.1       deraadt   173:        int arg, ch, what;
                    174:        char *memf, *nlistf;
1.2       niklas    175:        char buf[_POSIX2_LINE_MAX];
1.1       deraadt   176:        int cnt;
1.52      djm       177:        gid_t gid;
1.1       deraadt   178:
                    179:        arg = 0;
                    180:        what = KERN_PROC_ALL;
                    181:        nlistf = memf = NULL;
1.28      hugh      182:        oflg = 0;
                    183:        while ((ch = getopt(argc, argv, "fnop:u:vN:M:")) != -1)
1.1       deraadt   184:                switch((char)ch) {
                    185:                case 'f':
                    186:                        fsflg = 1;
                    187:                        break;
                    188:                case 'M':
                    189:                        memf = optarg;
                    190:                        break;
                    191:                case 'N':
                    192:                        nlistf = optarg;
                    193:                        break;
                    194:                case 'n':
                    195:                        nflg = 1;
                    196:                        break;
1.28      hugh      197:                case 'o':
                    198:                        oflg = 1;
                    199:                        break;
1.1       deraadt   200:                case 'p':
                    201:                        if (pflg++)
                    202:                                usage();
                    203:                        if (!isdigit(*optarg)) {
1.36      mpech     204:                                warnx( "-p requires a process id");
1.1       deraadt   205:                                usage();
                    206:                        }
                    207:                        what = KERN_PROC_PID;
                    208:                        arg = atoi(optarg);
                    209:                        break;
                    210:                case 'u':
                    211:                        if (uflg++)
                    212:                                usage();
1.17      mickey    213:                        if (!(passwd = getpwnam(optarg)))
1.21      deraadt   214:                                errx(1, "%s: unknown uid", optarg);
1.1       deraadt   215:                        what = KERN_PROC_UID;
                    216:                        arg = passwd->pw_uid;
                    217:                        break;
                    218:                case 'v':
                    219:                        vflg = 1;
                    220:                        break;
                    221:                default:
                    222:                        usage();
                    223:                }
                    224:
1.41      deraadt   225:        /*
                    226:         * Discard setgid privileges if not the running kernel so that bad
                    227:         * guys can't print interesting stuff from kernel memory.
                    228:         */
1.52      djm       229:        gid = getgid();
                    230:        if (nlistf != NULL || memf != NULL)
                    231:                if (setresgid(gid, gid, gid) == -1)
                    232:                        err(1, "setresgid");
1.41      deraadt   233:
                    234:        if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL)
                    235:                errx(1, "%s", buf);
                    236:
1.52      djm       237:        if (nlistf == NULL && memf == NULL)
                    238:                if (setresgid(gid, gid, gid) == -1)
                    239:                        err(1, "setresgid");
1.41      deraadt   240:
1.1       deraadt   241:        if (*(argv += optind)) {
                    242:                for (; *argv; ++argv) {
                    243:                        if (getfname(*argv))
                    244:                                checkfile = 1;
                    245:                }
1.30      pvalchev  246:                if (!checkfile) /* file(s) specified, but none accessible */
1.1       deraadt   247:                        exit(1);
                    248:        }
                    249:
                    250:        ALLOC_OFILES(256);      /* reserve space for file pointers */
                    251:
1.34      deraadt   252:        if (fsflg && !checkfile) {
1.1       deraadt   253:                /* -f with no files means use wd */
                    254:                if (getfname(".") == 0)
                    255:                        exit(1);
                    256:                checkfile = 1;
                    257:        }
1.16      deraadt   258:
1.49      millert   259:        if ((p = kvm_getproc2(kd, what, arg, sizeof(*p), &cnt)) == NULL)
1.21      deraadt   260:                errx(1, "%s", kvm_geterr(kd));
1.1       deraadt   261:        if (nflg)
                    262:                printf("%s",
1.53      mickey    263: "USER     CMD          PID   FD  DEV      INUM       MODE R/W    DV|SZ");
1.1       deraadt   264:        else
                    265:                printf("%s",
1.53      mickey    266: "USER     CMD          PID   FD MOUNT        INUM MODE       R/W    DV|SZ");
1.28      hugh      267:        if (oflg)
                    268:                printf("%s", ":OFFSET  ");
1.1       deraadt   269:        if (checkfile && fsflg == 0)
                    270:                printf(" NAME\n");
                    271:        else
                    272:                putchar('\n');
                    273:
                    274:        for (plast = &p[cnt]; p < plast; ++p) {
1.49      millert   275:                if (p->p_stat == SZOMB)
1.1       deraadt   276:                        continue;
                    277:                dofiles(p);
                    278:        }
                    279:        exit(0);
                    280: }
                    281:
                    282: char   *Uname, *Comm;
1.14      deraadt   283: pid_t  Pid;
1.1       deraadt   284:
1.34      deraadt   285: #define PREFIX(i) do { \
1.39      mpech     286:        printf("%-8.8s %-10s %5ld", Uname, Comm, (long)Pid); \
1.1       deraadt   287:        switch(i) { \
                    288:        case TEXT: \
                    289:                printf(" text"); \
                    290:                break; \
                    291:        case CDIR: \
                    292:                printf("   wd"); \
                    293:                break; \
                    294:        case RDIR: \
                    295:                printf(" root"); \
                    296:                break; \
                    297:        case TRACE: \
                    298:                printf("   tr"); \
                    299:                break; \
                    300:        default: \
                    301:                printf(" %4d", i); \
                    302:                break; \
1.34      deraadt   303:        } \
                    304: } while (0)
1.1       deraadt   305:
                    306: /*
                    307:  * print open files attributed to this process
                    308:  */
                    309: void
1.49      millert   310: dofiles(struct kinfo_proc2 *kp)
1.1       deraadt   311: {
1.11      millert   312:        int i;
1.1       deraadt   313:        struct file file;
                    314:        struct filedesc0 filed0;
                    315: #define        filed   filed0.fd_fd
                    316:
1.49      millert   317:        Uname = user_from_uid(kp->p_uid, 0);
                    318:        Pid = kp->p_pid;
                    319:        Comm = kp->p_comm;
1.1       deraadt   320:
1.49      millert   321:        if (kp->p_fd == 0)
1.1       deraadt   322:                return;
1.49      millert   323:        if (!KVM_READ(kp->p_fd, &filed0, sizeof (filed0))) {
1.39      mpech     324:                dprintf("can't read filedesc at %p for pid %ld",
1.49      millert   325:                    (void *)(u_long)kp->p_fd, (long)Pid);
1.1       deraadt   326:                return;
                    327:        }
                    328:        if (filed.fd_nfiles < 0 || filed.fd_lastfile >= filed.fd_nfiles ||
                    329:            filed.fd_freefile > filed.fd_lastfile + 1) {
1.39      mpech     330:                dprintf("filedesc corrupted at %p for pid %ld",
1.49      millert   331:                    (void *)(u_long)kp->p_fd, (long)Pid);
1.1       deraadt   332:                return;
                    333:        }
                    334:        /*
                    335:         * root directory vnode, if one
                    336:         */
                    337:        if (filed.fd_rdir)
1.28      hugh      338:                vtrans(filed.fd_rdir, RDIR, FREAD, 0);
1.1       deraadt   339:        /*
                    340:         * current working directory vnode
                    341:         */
1.28      hugh      342:        vtrans(filed.fd_cdir, CDIR, FREAD, 0);
1.1       deraadt   343:        /*
                    344:         * ktrace vnode, if one
                    345:         */
1.49      millert   346:        if (kp->p_tracep)
                    347:                vtrans((struct vnode *)(u_long)kp->p_tracep, TRACE, FREAD|FWRITE, 0);
1.1       deraadt   348:        /*
                    349:         * open files
                    350:         */
                    351: #define FPSIZE (sizeof (struct file *))
                    352:        ALLOC_OFILES(filed.fd_lastfile+1);
                    353:        if (filed.fd_nfiles > NDFILE) {
                    354:                if (!KVM_READ(filed.fd_ofiles, ofiles,
                    355:                    (filed.fd_lastfile+1) * FPSIZE)) {
1.39      mpech     356:                        dprintf("can't read file structures at %p for pid %ld",
                    357:                            filed.fd_ofiles, (long)Pid);
1.1       deraadt   358:                        return;
                    359:                }
                    360:        } else
                    361:                bcopy(filed0.fd_dfiles, ofiles, (filed.fd_lastfile+1) * FPSIZE);
                    362:        for (i = 0; i <= filed.fd_lastfile; i++) {
                    363:                if (ofiles[i] == NULL)
                    364:                        continue;
                    365:                if (!KVM_READ(ofiles[i], &file, sizeof (struct file))) {
1.39      mpech     366:                        dprintf("can't read file %d at %p for pid %ld",
                    367:                                i, ofiles[i], (long)Pid);
1.1       deraadt   368:                        continue;
                    369:                }
                    370:                if (file.f_type == DTYPE_VNODE)
1.38      deraadt   371:                        vtrans((struct vnode *)file.f_data, i, file.f_flag,
                    372:                            file.f_offset);
1.1       deraadt   373:                else if (file.f_type == DTYPE_SOCKET) {
                    374:                        if (checkfile == 0)
                    375:                                socktrans((struct socket *)file.f_data, i);
1.22      art       376:                } else if (file.f_type == DTYPE_PIPE) {
                    377:                        if (checkfile == 0)
                    378:                                pipetrans((struct pipe *)file.f_data, i);
1.37      deraadt   379:                } else if (file.f_type == DTYPE_KQUEUE) {
                    380:                        if (checkfile == 0)
                    381:                                kqueuetrans((struct kqueue *)file.f_data, i);
                    382:                } else if (file.f_type == DTYPE_CRYPTO) {
                    383:                        if (checkfile == 0)
                    384:                                cryptotrans(file.f_data, i);
                    385:                } else if (file.f_type == DTYPE_SYSTRACE) {
                    386:                        if (checkfile == 0)
1.38      deraadt   387:                                systracetrans((struct fsystrace *)file.f_data, i);
1.22      art       388:                } else {
1.39      mpech     389:                        dprintf("unknown file type %d for file %d of pid %ld",
                    390:                                file.f_type, i, (long)Pid);
1.1       deraadt   391:                }
                    392:        }
                    393: }
                    394:
                    395: void
1.40      deraadt   396: vtrans(struct vnode *vp, int i, int flag, off_t offset)
1.1       deraadt   397: {
                    398:        struct vnode vn;
                    399:        struct filestat fst;
1.12      deraadt   400:        char rw[3], mode[17];
1.47      deraadt   401:        char *badtype = NULL, *filename;
1.1       deraadt   402:
                    403:        filename = badtype = NULL;
                    404:        if (!KVM_READ(vp, &vn, sizeof (struct vnode))) {
1.39      mpech     405:                dprintf("can't read vnode at %p for pid %ld", vp, (long)Pid);
1.1       deraadt   406:                return;
                    407:        }
                    408:        if (vn.v_type == VNON || vn.v_tag == VT_NON)
                    409:                badtype = "none";
                    410:        else if (vn.v_type == VBAD)
                    411:                badtype = "bad";
                    412:        else
                    413:                switch (vn.v_tag) {
                    414:                case VT_UFS:
                    415:                case VT_MFS:
                    416:                        if (!ufs_filestat(&vn, &fst))
                    417:                                badtype = "error";
                    418:                        break;
                    419:                case VT_NFS:
                    420:                        if (!nfs_filestat(&vn, &fst))
                    421:                                badtype = "error";
                    422:                        break;
1.9       downsj    423:                case VT_EXT2FS:
                    424:                        if (!ext2fs_filestat(&vn, &fst))
                    425:                                badtype = "error";
                    426:                        break;
1.14      deraadt   427:                case VT_ISOFS:
                    428:                        if (!isofs_filestat(&vn, &fst))
                    429:                                badtype = "error";
                    430:                        break;
                    431:                case VT_MSDOSFS:
                    432:                        if (!msdos_filestat(&vn, &fst))
                    433:                                badtype = "error";
                    434:                        break;
1.18      art       435:                case VT_XFS:
                    436:                        if (!xfs_filestat(&vn, &fst))
                    437:                                badtype = "error";
                    438:                        break;
1.1       deraadt   439:                default: {
1.12      deraadt   440:                        static char unknown[30];
1.34      deraadt   441:                        snprintf(badtype = unknown, sizeof unknown,
                    442:                            "?(%x)", vn.v_tag);
1.10      deraadt   443:                        break;
1.1       deraadt   444:                }
                    445:        }
                    446:        if (checkfile) {
                    447:                int fsmatch = 0;
1.32      mpech     448:                DEVS *d;
1.1       deraadt   449:
                    450:                if (badtype)
                    451:                        return;
                    452:                for (d = devs; d != NULL; d = d->next)
                    453:                        if (d->fsid == fst.fsid) {
                    454:                                fsmatch = 1;
                    455:                                if (d->ino == fst.fileid) {
                    456:                                        filename = d->name;
                    457:                                        break;
                    458:                                }
                    459:                        }
                    460:                if (fsmatch == 0 || (filename == NULL && fsflg == 0))
                    461:                        return;
                    462:        }
                    463:        PREFIX(i);
                    464:        if (badtype) {
1.53      mickey    465:                (void)printf(" -           -  %10s    -\n", badtype);
1.1       deraadt   466:                return;
                    467:        }
                    468:        if (nflg)
1.31      deraadt   469:                (void)printf(" %2ld,%-2ld", (long)major(fst.fsid),
                    470:                    (long)minor(fst.fsid));
1.1       deraadt   471:        else
                    472:                (void)printf(" %-8s", getmnton(vn.v_mount));
                    473:        if (nflg)
1.34      deraadt   474:                (void)snprintf(mode, sizeof mode, "%o", fst.mode);
1.1       deraadt   475:        else
                    476:                strmode(fst.mode, mode);
1.53      mickey    477:        (void)printf(" %8ld %11s", fst.fileid, mode);
1.28      hugh      478:        rw[0] = '\0';
                    479:        if (flag & FREAD)
1.34      deraadt   480:                strlcat(rw, "r", sizeof rw);
1.28      hugh      481:        if (flag & FWRITE)
1.34      deraadt   482:                strlcat(rw, "w", sizeof rw);
1.28      hugh      483:        printf(" %2s", rw);
1.1       deraadt   484:        switch (vn.v_type) {
                    485:        case VBLK:
                    486:        case VCHR: {
                    487:                char *name;
                    488:
1.34      deraadt   489:                if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ?
1.1       deraadt   490:                    S_IFCHR : S_IFBLK)) == NULL))
1.28      hugh      491:                        printf("   %2d,%-3d", major(fst.rdev), minor(fst.rdev));
1.1       deraadt   492:                else
1.28      hugh      493:                        printf("  %7s", name);
                    494:                if (oflg)
                    495:                        printf("         ");
1.1       deraadt   496:                break;
                    497:        }
                    498:        default:
1.31      deraadt   499:                printf(" %8lld", (long long)fst.size);
1.28      hugh      500:                if (oflg)
1.31      deraadt   501:                        printf(":%-8lld", (long long)offset);
1.1       deraadt   502:        }
                    503:        if (filename && !fsflg)
1.28      hugh      504:                printf(" %s", filename);
1.1       deraadt   505:        putchar('\n');
                    506: }
                    507:
                    508: int
1.40      deraadt   509: ufs_filestat(struct vnode *vp, struct filestat *fsp)
1.1       deraadt   510: {
                    511:        struct inode inode;
1.54      pedro     512:        struct ufs1_dinode di1;
1.1       deraadt   513:
                    514:        if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
1.39      mpech     515:                dprintf("can't read inode at %p for pid %ld",
                    516:                    VTOI(vp), (long)Pid);
1.1       deraadt   517:                return 0;
                    518:        }
1.54      pedro     519:
                    520:        if (!KVM_READ(inode.i_din1, &di1, sizeof(struct ufs1_dinode))) {
                    521:                dprintf("can't read dinode at %p for pid %ld",
                    522:                    inode.i_din1, (long)Pid);
                    523:                return (0);
                    524:        }
                    525:
                    526:        inode.i_din1 = &di1;
                    527:
1.1       deraadt   528:        fsp->fsid = inode.i_dev & 0xffff;
                    529:        fsp->fileid = (long)inode.i_number;
1.55    ! pedro     530:        fsp->mode = inode.i_ffs1_mode;
        !           531:        fsp->size = inode.i_ffs1_size;
        !           532:        fsp->rdev = inode.i_ffs1_rdev;
1.9       downsj    533:
                    534:        return 1;
                    535: }
                    536:
                    537: int
1.40      deraadt   538: ext2fs_filestat(struct vnode *vp, struct filestat *fsp)
1.9       downsj    539: {
                    540:        struct inode inode;
1.54      pedro     541:        struct ext2fs_dinode e2di;
1.9       downsj    542:
                    543:        if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
1.39      mpech     544:                dprintf("can't read inode at %p for pid %ld",
                    545:                    VTOI(vp), (long)Pid);
1.9       downsj    546:                return 0;
                    547:        }
1.54      pedro     548:
                    549:        if (!KVM_READ(inode.i_e2din, &e2di, sizeof(struct ext2fs_dinode))) {
                    550:                dprintf("can't read dinode at %p for pid %ld",
                    551:                    inode.i_e2din, (long)Pid);
                    552:                return (0);
                    553:        }
                    554:
                    555:        inode.i_e2din = &e2di;
                    556:
1.9       downsj    557:        fsp->fsid = inode.i_dev & 0xffff;
                    558:        fsp->fileid = (long)inode.i_number;
1.11      millert   559:        fsp->mode = inode.i_e2fs_mode;
                    560:        fsp->size = inode.i_e2fs_size;
1.9       downsj    561:        fsp->rdev = 0;  /* XXX */
1.14      deraadt   562:
                    563:        return 1;
                    564: }
                    565:
                    566: int
1.40      deraadt   567: msdos_filestat(struct vnode *vp, struct filestat *fsp)
1.14      deraadt   568: {
                    569: #if 0
                    570:        struct inode inode;
                    571:
                    572:        if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
1.39      mpech     573:                dprintf("can't read inode at %p for pid %ld",
                    574:                    VTOI(vp), (long)Pid);
1.14      deraadt   575:                return 0;
                    576:        }
                    577:        fsp->fsid = inode.i_dev & 0xffff;
                    578:        fsp->fileid = (long)inode.i_number;
                    579:        fsp->mode = inode.i_e2fs_mode;
                    580:        fsp->size = inode.i_e2fs_size;
                    581:        fsp->rdev = 0;  /* XXX */
                    582: #endif
1.1       deraadt   583:
                    584:        return 1;
                    585: }
                    586:
                    587: int
1.40      deraadt   588: nfs_filestat(struct vnode *vp, struct filestat *fsp)
1.1       deraadt   589: {
                    590:        struct nfsnode nfsnode;
1.32      mpech     591:        mode_t mode;
1.1       deraadt   592:
                    593:        if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
1.39      mpech     594:                dprintf("can't read nfsnode at %p for pid %ld",
                    595:                    VTONFS(vp), (long)Pid);
1.1       deraadt   596:                return 0;
                    597:        }
                    598:        fsp->fsid = nfsnode.n_vattr.va_fsid;
                    599:        fsp->fileid = nfsnode.n_vattr.va_fileid;
                    600:        fsp->size = nfsnode.n_size;
                    601:        fsp->rdev = nfsnode.n_vattr.va_rdev;
                    602:        mode = (mode_t)nfsnode.n_vattr.va_mode;
                    603:        switch (vp->v_type) {
                    604:        case VREG:
                    605:                mode |= S_IFREG;
                    606:                break;
                    607:        case VDIR:
                    608:                mode |= S_IFDIR;
                    609:                break;
                    610:        case VBLK:
                    611:                mode |= S_IFBLK;
                    612:                break;
                    613:        case VCHR:
                    614:                mode |= S_IFCHR;
                    615:                break;
                    616:        case VLNK:
                    617:                mode |= S_IFLNK;
                    618:                break;
                    619:        case VSOCK:
                    620:                mode |= S_IFSOCK;
                    621:                break;
                    622:        case VFIFO:
                    623:                mode |= S_IFIFO;
                    624:                break;
1.17      mickey    625:        default:
                    626:                break;
1.34      deraadt   627:        }
1.1       deraadt   628:        fsp->mode = mode;
                    629:
                    630:        return 1;
                    631: }
                    632:
1.18      art       633: int
1.44      deraadt   634: xfs_filestat(struct vnode *vp, struct filestat *fsp)
1.18      art       635: {
                    636:        struct xfs_node xfs_node;
                    637:
                    638:        if (!KVM_READ(VNODE_TO_XNODE(vp), &xfs_node, sizeof (xfs_node))) {
1.39      mpech     639:                dprintf("can't read xfs_node at %p for pid %ld",
                    640:                    VTOI(vp), (long)Pid);
1.18      art       641:                return 0;
                    642:        }
                    643:        fsp->fsid = xfs_node.attr.va_fsid;
                    644:        fsp->fileid = (long)xfs_node.attr.va_fileid;
                    645:        fsp->mode = xfs_node.attr.va_mode;
                    646:        fsp->size = xfs_node.attr.va_size;
                    647:        fsp->rdev = xfs_node.attr.va_rdev;
                    648:
                    649:        return 1;
                    650: }
1.1       deraadt   651:
                    652: char *
1.40      deraadt   653: getmnton(struct mount *m)
1.1       deraadt   654: {
                    655:        static struct mount mount;
                    656:        static struct mtab {
                    657:                struct mtab *next;
                    658:                struct mount *m;
                    659:                char mntonname[MNAMELEN];
                    660:        } *mhead = NULL;
1.32      mpech     661:        struct mtab *mt;
1.1       deraadt   662:
                    663:        for (mt = mhead; mt != NULL; mt = mt->next)
                    664:                if (m == mt->m)
                    665:                        return (mt->mntonname);
                    666:        if (!KVM_READ(m, &mount, sizeof(struct mount))) {
1.17      mickey    667:                warn("can't read mount table at %p", m);
1.1       deraadt   668:                return (NULL);
                    669:        }
1.17      mickey    670:        if ((mt = malloc(sizeof (struct mtab))) == NULL)
                    671:                err(1, "malloc");
1.1       deraadt   672:        mt->m = m;
                    673:        bcopy(&mount.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN);
                    674:        mt->next = mhead;
                    675:        mhead = mt;
                    676:        return (mt->mntonname);
1.22      art       677: }
                    678:
                    679: void
1.40      deraadt   680: pipetrans(struct pipe *pipe, int i)
1.22      art       681: {
                    682:        struct pipe pi;
                    683:        void *maxaddr;
                    684:
                    685:        PREFIX(i);
                    686:
                    687:        printf(" ");
                    688:
                    689:        /* fill in socket */
                    690:        if (!KVM_READ(pipe, &pi, sizeof(struct pipe))) {
                    691:                dprintf("can't read pipe at %p", pipe);
                    692:                goto bad;
                    693:        }
                    694:
                    695:        /*
                    696:         * We don't have enough space to fit both peer and own address, so
                    697:         * we select the higher address so both ends of the pipe have the
                    698:         * same visible addr. (it's the higher address because when the other
                    699:         * end closes, it becomes 0)
                    700:         */
                    701:        maxaddr = MAX(pipe, pi.pipe_peer);
                    702:
1.38      deraadt   703:        printf("pipe %p state: %s%s%s\n", maxaddr,
1.34      deraadt   704:            (pi.pipe_state & PIPE_WANTR) ? "R" : "",
                    705:            (pi.pipe_state & PIPE_WANTW) ? "W" : "",
                    706:            (pi.pipe_state & PIPE_EOF) ? "E" : "");
1.37      deraadt   707:        return;
                    708: bad:
                    709:        printf("* error\n");
                    710: }
                    711:
                    712: void
1.40      deraadt   713: kqueuetrans(struct kqueue *kq, int i)
1.37      deraadt   714: {
                    715:        struct kqueue kqi;
                    716:
                    717:        PREFIX(i);
                    718:
                    719:        printf(" ");
                    720:
                    721:        /* fill it in */
                    722:        if (!KVM_READ(kq, &kqi, sizeof(struct kqueue))) {
                    723:                dprintf("can't read kqueue at %p", kq);
                    724:                goto bad;
                    725:        }
                    726:
1.38      deraadt   727:        printf("kqueue %p %d state: %s%s\n", kq, kqi.kq_count,
1.37      deraadt   728:            (kqi.kq_state & KQ_SEL) ? "S" : "",
                    729:            (kqi.kq_state & KQ_SLEEP) ? "W" : "");
                    730:        return;
                    731: bad:
                    732:        printf("* error\n");
                    733: }
                    734:
                    735: void
1.40      deraadt   736: cryptotrans(void *f, int i)
1.37      deraadt   737: {
                    738:        PREFIX(i);
                    739:
                    740:        printf(" ");
                    741:
1.38      deraadt   742:        printf("crypto %p\n", f);
1.37      deraadt   743: }
                    744:
                    745: void
1.40      deraadt   746: systracetrans(struct fsystrace *f, int i)
1.37      deraadt   747: {
1.38      deraadt   748:        struct fsystrace fi;
                    749:
1.37      deraadt   750:        PREFIX(i);
                    751:
                    752:        printf(" ");
                    753:
1.38      deraadt   754:        /* fill it in */
                    755:        if (!KVM_READ(f, &fi, sizeof(fi))) {
                    756:                dprintf("can't read fsystrace at %p", f);
                    757:                goto bad;
                    758:        }
1.34      deraadt   759:
1.38      deraadt   760:        printf("systrace %p npol %d\n", f, fi.npolicies);
1.22      art       761:        return;
                    762: bad:
                    763:        printf("* error\n");
1.1       deraadt   764: }
                    765:
1.26      itojun    766: #ifdef INET6
                    767: const char *
1.40      deraadt   768: inet6_addrstr(struct in6_addr *p)
1.26      itojun    769: {
                    770:        struct sockaddr_in6 sin6;
                    771:        static char hbuf[NI_MAXHOST];
1.50      itojun    772:        const int niflags = NI_NUMERICHOST;
1.26      itojun    773:
                    774:        memset(&sin6, 0, sizeof(sin6));
                    775:        sin6.sin6_family = AF_INET6;
                    776:        sin6.sin6_len = sizeof(struct sockaddr_in6);
                    777:        sin6.sin6_addr = *p;
                    778:        if (IN6_IS_ADDR_LINKLOCAL(p) &&
                    779:            *(u_int16_t *)&sin6.sin6_addr.s6_addr[2] != 0) {
                    780:                sin6.sin6_scope_id =
1.38      deraadt   781:                    ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);
1.26      itojun    782:                sin6.sin6_addr.s6_addr[2] = sin6.sin6_addr.s6_addr[3] = 0;
                    783:        }
                    784:
                    785:        if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
1.38      deraadt   786:            hbuf, sizeof(hbuf), NULL, 0, niflags))
1.26      itojun    787:                return "invalid";
                    788:
                    789:        return hbuf;
                    790: }
                    791: #endif
                    792:
1.1       deraadt   793: void
1.40      deraadt   794: socktrans(struct socket *sock, int i)
1.1       deraadt   795: {
                    796:        static char *stypename[] = {
                    797:                "unused",       /* 0 */
1.38      deraadt   798:                "stream",       /* 1 */
1.1       deraadt   799:                "dgram",        /* 2 */
                    800:                "raw",          /* 3 */
                    801:                "rdm",          /* 4 */
                    802:                "seqpak"        /* 5 */
                    803:        };
                    804: #define        STYPEMAX 5
                    805:        struct socket   so;
                    806:        struct protosw  proto;
                    807:        struct domain   dom;
                    808:        struct inpcb    inpcb;
                    809:        struct unpcb    unpcb;
                    810:        int len;
1.4       deraadt   811:        char dname[32];
1.26      itojun    812: #ifdef INET6
                    813:        char xaddrbuf[NI_MAXHOST + 2];
                    814: #endif
1.1       deraadt   815:
                    816:        PREFIX(i);
                    817:
                    818:        /* fill in socket */
                    819:        if (!KVM_READ(sock, &so, sizeof(struct socket))) {
1.17      mickey    820:                dprintf("can't read sock at %p", sock);
1.1       deraadt   821:                goto bad;
                    822:        }
                    823:
                    824:        /* fill in protosw entry */
                    825:        if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
1.17      mickey    826:                dprintf("can't read protosw at %p", so.so_proto);
1.1       deraadt   827:                goto bad;
                    828:        }
                    829:
                    830:        /* fill in domain */
                    831:        if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
1.17      mickey    832:                dprintf("can't read domain at %p", proto.pr_domain);
1.1       deraadt   833:                goto bad;
                    834:        }
                    835:
                    836:        if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
1.15      deraadt   837:            sizeof(dname) - 1)) != sizeof(dname) -1) {
1.17      mickey    838:                dprintf("can't read domain name at %p", dom.dom_name);
1.1       deraadt   839:                dname[0] = '\0';
1.38      deraadt   840:        } else
1.1       deraadt   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:
1.34      deraadt   848:        /*
1.1       deraadt   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));
1.33      itojun    949:                        if (inpcb.inp_fport) {
1.26      itojun    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));
1.33      itojun    956:                        }
1.26      itojun    957:                } else if (so.so_pcb)
                    958:                        printf(" %p", so.so_pcb);
                    959:                break;
                    960: #endif
1.1       deraadt   961:        case AF_UNIX:
                    962:                /* print address of pcb and connected pcb */
                    963:                if (so.so_pcb) {
1.11      millert   964:                        printf(" %p", so.so_pcb);
1.1       deraadt   965:                        if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb,
                    966:                            sizeof(struct unpcb)) != sizeof(struct unpcb)){
1.17      mickey    967:                                dprintf("can't read unpcb at %p", so.so_pcb);
1.1       deraadt   968:                                goto bad;
                    969:                        }
                    970:                        if (unpcb.unp_conn) {
                    971:                                char shoconn[4], *cp;
                    972:
                    973:                                cp = shoconn;
                    974:                                if (!(so.so_state & SS_CANTRCVMORE))
                    975:                                        *cp++ = '<';
                    976:                                *cp++ = '-';
                    977:                                if (!(so.so_state & SS_CANTSENDMORE))
                    978:                                        *cp++ = '>';
                    979:                                *cp = '\0';
1.11      millert   980:                                printf(" %s %p", shoconn,
                    981:                                    unpcb.unp_conn);
1.1       deraadt   982:                        }
                    983:                }
                    984:                break;
                    985:        default:
                    986:                /* print protocol number and socket address */
1.11      millert   987:                printf(" %d %p", proto.pr_protocol, sock);
1.1       deraadt   988:        }
                    989:        printf("\n");
                    990:        return;
                    991: bad:
                    992:        printf("* error\n");
                    993: }
                    994:
                    995: /*
                    996:  * getinetproto --
                    997:  *     print name of protocol number
                    998:  */
                    999: void
                   1000: getinetproto(number)
                   1001:        int number;
                   1002: {
1.24      deraadt  1003:        static int isopen;
1.32      mpech    1004:        struct protoent *pe;
1.1       deraadt  1005:
1.24      deraadt  1006:        if (!isopen)
                   1007:                setprotoent(++isopen);
                   1008:        if ((pe = getprotobynumber(number)) != NULL)
                   1009:                printf(" %s", pe->p_name);
                   1010:        else
1.1       deraadt  1011:                printf(" %d", number);
                   1012: }
                   1013:
1.11      millert  1014: int
1.40      deraadt  1015: getfname(char *filename)
1.1       deraadt  1016: {
                   1017:        struct stat statbuf;
                   1018:        DEVS *cur;
                   1019:
                   1020:        if (stat(filename, &statbuf)) {
1.27      millert  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
1.40      deraadt  1036: usage(void)
1.1       deraadt  1037: {
1.48      jmc      1038:        fprintf(stderr, "usage: fstat [-fnov] [-M core] [-N system] "
                   1039:            "[-p pid] [-u user] [file ...]\n");
1.1       deraadt  1040:        exit(1);
                   1041: }