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