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