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