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