Annotation of src/usr.bin/fstat/fstat.c, Revision 1.51
1.51 ! tedu 1: /* $OpenBSD: fstat.c,v 1.50 2004/11/17 01:47:20 itojun 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.51 ! tedu 40: static char *rcsid = "$OpenBSD: fstat.c,v 1.50 2004/11/17 01:47:20 itojun 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;
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.49 millert 257: if ((p = kvm_getproc2(kd, what, arg, sizeof(*p), &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) {
1.49 millert 273: if (p->p_stat == SZOMB)
1.1 deraadt 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.49 millert 308: dofiles(struct kinfo_proc2 *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:
1.49 millert 315: Uname = user_from_uid(kp->p_uid, 0);
316: Pid = kp->p_pid;
317: Comm = kp->p_comm;
1.1 deraadt 318:
1.49 millert 319: if (kp->p_fd == 0)
1.1 deraadt 320: return;
1.49 millert 321: if (!KVM_READ(kp->p_fd, &filed0, sizeof (filed0))) {
1.39 mpech 322: dprintf("can't read filedesc at %p for pid %ld",
1.49 millert 323: (void *)(u_long)kp->p_fd, (long)Pid);
1.1 deraadt 324: return;
325: }
326: if (filed.fd_nfiles < 0 || filed.fd_lastfile >= filed.fd_nfiles ||
327: filed.fd_freefile > filed.fd_lastfile + 1) {
1.39 mpech 328: dprintf("filedesc corrupted at %p for pid %ld",
1.49 millert 329: (void *)(u_long)kp->p_fd, (long)Pid);
1.1 deraadt 330: return;
331: }
332: /*
333: * root directory vnode, if one
334: */
335: if (filed.fd_rdir)
1.28 hugh 336: vtrans(filed.fd_rdir, RDIR, FREAD, 0);
1.1 deraadt 337: /*
338: * current working directory vnode
339: */
1.28 hugh 340: vtrans(filed.fd_cdir, CDIR, FREAD, 0);
1.1 deraadt 341: /*
342: * ktrace vnode, if one
343: */
1.49 millert 344: if (kp->p_tracep)
345: vtrans((struct vnode *)(u_long)kp->p_tracep, TRACE, FREAD|FWRITE, 0);
1.1 deraadt 346: /*
347: * open files
348: */
349: #define FPSIZE (sizeof (struct file *))
350: ALLOC_OFILES(filed.fd_lastfile+1);
351: if (filed.fd_nfiles > NDFILE) {
352: if (!KVM_READ(filed.fd_ofiles, ofiles,
353: (filed.fd_lastfile+1) * FPSIZE)) {
1.39 mpech 354: dprintf("can't read file structures at %p for pid %ld",
355: filed.fd_ofiles, (long)Pid);
1.1 deraadt 356: return;
357: }
358: } else
359: bcopy(filed0.fd_dfiles, ofiles, (filed.fd_lastfile+1) * FPSIZE);
360: for (i = 0; i <= filed.fd_lastfile; i++) {
361: if (ofiles[i] == NULL)
362: continue;
363: if (!KVM_READ(ofiles[i], &file, sizeof (struct file))) {
1.39 mpech 364: dprintf("can't read file %d at %p for pid %ld",
365: i, ofiles[i], (long)Pid);
1.1 deraadt 366: continue;
367: }
368: if (file.f_type == DTYPE_VNODE)
1.38 deraadt 369: vtrans((struct vnode *)file.f_data, i, file.f_flag,
370: file.f_offset);
1.1 deraadt 371: else if (file.f_type == DTYPE_SOCKET) {
372: if (checkfile == 0)
373: socktrans((struct socket *)file.f_data, i);
1.22 art 374: } else if (file.f_type == DTYPE_PIPE) {
375: if (checkfile == 0)
376: pipetrans((struct pipe *)file.f_data, i);
1.37 deraadt 377: } else if (file.f_type == DTYPE_KQUEUE) {
378: if (checkfile == 0)
379: kqueuetrans((struct kqueue *)file.f_data, i);
380: } else if (file.f_type == DTYPE_CRYPTO) {
381: if (checkfile == 0)
382: cryptotrans(file.f_data, i);
383: } else if (file.f_type == DTYPE_SYSTRACE) {
384: if (checkfile == 0)
1.38 deraadt 385: systracetrans((struct fsystrace *)file.f_data, i);
1.22 art 386: } else {
1.39 mpech 387: dprintf("unknown file type %d for file %d of pid %ld",
388: file.f_type, i, (long)Pid);
1.1 deraadt 389: }
390: }
391: }
392:
393: void
1.40 deraadt 394: vtrans(struct vnode *vp, int i, int flag, off_t offset)
1.1 deraadt 395: {
396: struct vnode vn;
397: struct filestat fst;
1.12 deraadt 398: char rw[3], mode[17];
1.47 deraadt 399: char *badtype = NULL, *filename;
1.1 deraadt 400:
401: filename = badtype = NULL;
402: if (!KVM_READ(vp, &vn, sizeof (struct vnode))) {
1.39 mpech 403: dprintf("can't read vnode at %p for pid %ld", vp, (long)Pid);
1.1 deraadt 404: return;
405: }
406: if (vn.v_type == VNON || vn.v_tag == VT_NON)
407: badtype = "none";
408: else if (vn.v_type == VBAD)
409: badtype = "bad";
410: else
411: switch (vn.v_tag) {
412: case VT_UFS:
413: case VT_MFS:
414: if (!ufs_filestat(&vn, &fst))
415: badtype = "error";
416: break;
417: case VT_NFS:
418: if (!nfs_filestat(&vn, &fst))
419: badtype = "error";
420: break;
1.9 downsj 421: case VT_EXT2FS:
422: if (!ext2fs_filestat(&vn, &fst))
423: badtype = "error";
424: break;
1.14 deraadt 425: case VT_ISOFS:
426: if (!isofs_filestat(&vn, &fst))
427: badtype = "error";
428: break;
429: case VT_MSDOSFS:
430: if (!msdos_filestat(&vn, &fst))
431: badtype = "error";
432: break;
1.18 art 433: case VT_XFS:
434: if (!xfs_filestat(&vn, &fst))
435: badtype = "error";
436: break;
1.1 deraadt 437: default: {
1.12 deraadt 438: static char unknown[30];
1.34 deraadt 439: snprintf(badtype = unknown, sizeof unknown,
440: "?(%x)", vn.v_tag);
1.10 deraadt 441: break;
1.1 deraadt 442: }
443: }
444: if (checkfile) {
445: int fsmatch = 0;
1.32 mpech 446: DEVS *d;
1.1 deraadt 447:
448: if (badtype)
449: return;
450: for (d = devs; d != NULL; d = d->next)
451: if (d->fsid == fst.fsid) {
452: fsmatch = 1;
453: if (d->ino == fst.fileid) {
454: filename = d->name;
455: break;
456: }
457: }
458: if (fsmatch == 0 || (filename == NULL && fsflg == 0))
459: return;
460: }
461: PREFIX(i);
462: if (badtype) {
463: (void)printf(" - - %10s -\n", badtype);
464: return;
465: }
466: if (nflg)
1.31 deraadt 467: (void)printf(" %2ld,%-2ld", (long)major(fst.fsid),
468: (long)minor(fst.fsid));
1.1 deraadt 469: else
470: (void)printf(" %-8s", getmnton(vn.v_mount));
471: if (nflg)
1.34 deraadt 472: (void)snprintf(mode, sizeof mode, "%o", fst.mode);
1.1 deraadt 473: else
474: strmode(fst.mode, mode);
1.28 hugh 475: (void)printf(" %6ld %11s", fst.fileid, mode);
476: rw[0] = '\0';
477: if (flag & FREAD)
1.34 deraadt 478: strlcat(rw, "r", sizeof rw);
1.28 hugh 479: if (flag & FWRITE)
1.34 deraadt 480: strlcat(rw, "w", sizeof rw);
1.28 hugh 481: printf(" %2s", rw);
1.1 deraadt 482: switch (vn.v_type) {
483: case VBLK:
484: case VCHR: {
485: char *name;
486:
1.34 deraadt 487: if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ?
1.1 deraadt 488: S_IFCHR : S_IFBLK)) == NULL))
1.28 hugh 489: printf(" %2d,%-3d", major(fst.rdev), minor(fst.rdev));
1.1 deraadt 490: else
1.28 hugh 491: printf(" %7s", name);
492: if (oflg)
493: printf(" ");
1.1 deraadt 494: break;
495: }
496: default:
1.31 deraadt 497: printf(" %8lld", (long long)fst.size);
1.28 hugh 498: if (oflg)
1.31 deraadt 499: printf(":%-8lld", (long long)offset);
1.1 deraadt 500: }
501: if (filename && !fsflg)
1.28 hugh 502: printf(" %s", filename);
1.1 deraadt 503: putchar('\n');
504: }
505:
506: int
1.40 deraadt 507: ufs_filestat(struct vnode *vp, struct filestat *fsp)
1.1 deraadt 508: {
509: struct inode inode;
510:
511: if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
1.39 mpech 512: dprintf("can't read inode at %p for pid %ld",
513: VTOI(vp), (long)Pid);
1.1 deraadt 514: return 0;
515: }
516: fsp->fsid = inode.i_dev & 0xffff;
517: fsp->fileid = (long)inode.i_number;
1.11 millert 518: fsp->mode = inode.i_ffs_mode;
519: fsp->size = inode.i_ffs_size;
1.9 downsj 520: fsp->rdev = inode.i_ffs_rdev;
521:
522: return 1;
523: }
524:
525: int
1.40 deraadt 526: ext2fs_filestat(struct vnode *vp, struct filestat *fsp)
1.9 downsj 527: {
528: struct inode inode;
529:
530: if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
1.39 mpech 531: dprintf("can't read inode at %p for pid %ld",
532: VTOI(vp), (long)Pid);
1.9 downsj 533: return 0;
534: }
535: fsp->fsid = inode.i_dev & 0xffff;
536: fsp->fileid = (long)inode.i_number;
1.11 millert 537: fsp->mode = inode.i_e2fs_mode;
538: fsp->size = inode.i_e2fs_size;
1.9 downsj 539: fsp->rdev = 0; /* XXX */
1.14 deraadt 540:
541: return 1;
542: }
543:
544: int
1.40 deraadt 545: msdos_filestat(struct vnode *vp, struct filestat *fsp)
1.14 deraadt 546: {
547: #if 0
548: struct inode inode;
549:
550: if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
1.39 mpech 551: dprintf("can't read inode at %p for pid %ld",
552: VTOI(vp), (long)Pid);
1.14 deraadt 553: return 0;
554: }
555: fsp->fsid = inode.i_dev & 0xffff;
556: fsp->fileid = (long)inode.i_number;
557: fsp->mode = inode.i_e2fs_mode;
558: fsp->size = inode.i_e2fs_size;
559: fsp->rdev = 0; /* XXX */
560: #endif
1.1 deraadt 561:
562: return 1;
563: }
564:
565: int
1.40 deraadt 566: nfs_filestat(struct vnode *vp, struct filestat *fsp)
1.1 deraadt 567: {
568: struct nfsnode nfsnode;
1.32 mpech 569: mode_t mode;
1.1 deraadt 570:
571: if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
1.39 mpech 572: dprintf("can't read nfsnode at %p for pid %ld",
573: VTONFS(vp), (long)Pid);
1.1 deraadt 574: return 0;
575: }
576: fsp->fsid = nfsnode.n_vattr.va_fsid;
577: fsp->fileid = nfsnode.n_vattr.va_fileid;
578: fsp->size = nfsnode.n_size;
579: fsp->rdev = nfsnode.n_vattr.va_rdev;
580: mode = (mode_t)nfsnode.n_vattr.va_mode;
581: switch (vp->v_type) {
582: case VREG:
583: mode |= S_IFREG;
584: break;
585: case VDIR:
586: mode |= S_IFDIR;
587: break;
588: case VBLK:
589: mode |= S_IFBLK;
590: break;
591: case VCHR:
592: mode |= S_IFCHR;
593: break;
594: case VLNK:
595: mode |= S_IFLNK;
596: break;
597: case VSOCK:
598: mode |= S_IFSOCK;
599: break;
600: case VFIFO:
601: mode |= S_IFIFO;
602: break;
1.17 mickey 603: default:
604: break;
1.34 deraadt 605: }
1.1 deraadt 606: fsp->mode = mode;
607:
608: return 1;
609: }
610:
1.18 art 611: int
1.44 deraadt 612: xfs_filestat(struct vnode *vp, struct filestat *fsp)
1.18 art 613: {
614: struct xfs_node xfs_node;
615:
616: if (!KVM_READ(VNODE_TO_XNODE(vp), &xfs_node, sizeof (xfs_node))) {
1.39 mpech 617: dprintf("can't read xfs_node at %p for pid %ld",
618: VTOI(vp), (long)Pid);
1.18 art 619: return 0;
620: }
621: fsp->fsid = xfs_node.attr.va_fsid;
622: fsp->fileid = (long)xfs_node.attr.va_fileid;
623: fsp->mode = xfs_node.attr.va_mode;
624: fsp->size = xfs_node.attr.va_size;
625: fsp->rdev = xfs_node.attr.va_rdev;
626:
627: return 1;
628: }
1.1 deraadt 629:
630: char *
1.40 deraadt 631: getmnton(struct mount *m)
1.1 deraadt 632: {
633: static struct mount mount;
634: static struct mtab {
635: struct mtab *next;
636: struct mount *m;
637: char mntonname[MNAMELEN];
638: } *mhead = NULL;
1.32 mpech 639: struct mtab *mt;
1.1 deraadt 640:
641: for (mt = mhead; mt != NULL; mt = mt->next)
642: if (m == mt->m)
643: return (mt->mntonname);
644: if (!KVM_READ(m, &mount, sizeof(struct mount))) {
1.17 mickey 645: warn("can't read mount table at %p", m);
1.1 deraadt 646: return (NULL);
647: }
1.17 mickey 648: if ((mt = malloc(sizeof (struct mtab))) == NULL)
649: err(1, "malloc");
1.1 deraadt 650: mt->m = m;
651: bcopy(&mount.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN);
652: mt->next = mhead;
653: mhead = mt;
654: return (mt->mntonname);
1.22 art 655: }
656:
657: void
1.40 deraadt 658: pipetrans(struct pipe *pipe, int i)
1.22 art 659: {
660: struct pipe pi;
661: void *maxaddr;
662:
663: PREFIX(i);
664:
665: printf(" ");
666:
667: /* fill in socket */
668: if (!KVM_READ(pipe, &pi, sizeof(struct pipe))) {
669: dprintf("can't read pipe at %p", pipe);
670: goto bad;
671: }
672:
673: /*
674: * We don't have enough space to fit both peer and own address, so
675: * we select the higher address so both ends of the pipe have the
676: * same visible addr. (it's the higher address because when the other
677: * end closes, it becomes 0)
678: */
679: maxaddr = MAX(pipe, pi.pipe_peer);
680:
1.38 deraadt 681: printf("pipe %p state: %s%s%s\n", maxaddr,
1.34 deraadt 682: (pi.pipe_state & PIPE_WANTR) ? "R" : "",
683: (pi.pipe_state & PIPE_WANTW) ? "W" : "",
684: (pi.pipe_state & PIPE_EOF) ? "E" : "");
1.37 deraadt 685: return;
686: bad:
687: printf("* error\n");
688: }
689:
690: void
1.40 deraadt 691: kqueuetrans(struct kqueue *kq, int i)
1.37 deraadt 692: {
693: struct kqueue kqi;
694:
695: PREFIX(i);
696:
697: printf(" ");
698:
699: /* fill it in */
700: if (!KVM_READ(kq, &kqi, sizeof(struct kqueue))) {
701: dprintf("can't read kqueue at %p", kq);
702: goto bad;
703: }
704:
1.38 deraadt 705: printf("kqueue %p %d state: %s%s\n", kq, kqi.kq_count,
1.37 deraadt 706: (kqi.kq_state & KQ_SEL) ? "S" : "",
707: (kqi.kq_state & KQ_SLEEP) ? "W" : "");
708: return;
709: bad:
710: printf("* error\n");
711: }
712:
713: void
1.40 deraadt 714: cryptotrans(void *f, int i)
1.37 deraadt 715: {
716: PREFIX(i);
717:
718: printf(" ");
719:
1.38 deraadt 720: printf("crypto %p\n", f);
1.37 deraadt 721: }
722:
723: void
1.40 deraadt 724: systracetrans(struct fsystrace *f, int i)
1.37 deraadt 725: {
1.38 deraadt 726: struct fsystrace fi;
727:
1.37 deraadt 728: PREFIX(i);
729:
730: printf(" ");
731:
1.38 deraadt 732: /* fill it in */
733: if (!KVM_READ(f, &fi, sizeof(fi))) {
734: dprintf("can't read fsystrace at %p", f);
735: goto bad;
736: }
1.34 deraadt 737:
1.38 deraadt 738: printf("systrace %p npol %d\n", f, fi.npolicies);
1.22 art 739: return;
740: bad:
741: printf("* error\n");
1.1 deraadt 742: }
743:
1.26 itojun 744: #ifdef INET6
745: const char *
1.40 deraadt 746: inet6_addrstr(struct in6_addr *p)
1.26 itojun 747: {
748: struct sockaddr_in6 sin6;
749: static char hbuf[NI_MAXHOST];
1.50 itojun 750: const int niflags = NI_NUMERICHOST;
1.26 itojun 751:
752: memset(&sin6, 0, sizeof(sin6));
753: sin6.sin6_family = AF_INET6;
754: sin6.sin6_len = sizeof(struct sockaddr_in6);
755: sin6.sin6_addr = *p;
756: if (IN6_IS_ADDR_LINKLOCAL(p) &&
757: *(u_int16_t *)&sin6.sin6_addr.s6_addr[2] != 0) {
758: sin6.sin6_scope_id =
1.38 deraadt 759: ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);
1.26 itojun 760: sin6.sin6_addr.s6_addr[2] = sin6.sin6_addr.s6_addr[3] = 0;
761: }
762:
763: if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
1.38 deraadt 764: hbuf, sizeof(hbuf), NULL, 0, niflags))
1.26 itojun 765: return "invalid";
766:
767: return hbuf;
768: }
769: #endif
770:
1.1 deraadt 771: void
1.40 deraadt 772: socktrans(struct socket *sock, int i)
1.1 deraadt 773: {
774: static char *stypename[] = {
775: "unused", /* 0 */
1.38 deraadt 776: "stream", /* 1 */
1.1 deraadt 777: "dgram", /* 2 */
778: "raw", /* 3 */
779: "rdm", /* 4 */
780: "seqpak" /* 5 */
781: };
782: #define STYPEMAX 5
783: struct socket so;
784: struct protosw proto;
785: struct domain dom;
786: struct inpcb inpcb;
787: struct unpcb unpcb;
788: int len;
1.4 deraadt 789: char dname[32];
1.26 itojun 790: #ifdef INET6
791: char xaddrbuf[NI_MAXHOST + 2];
792: #endif
1.1 deraadt 793:
794: PREFIX(i);
795:
796: /* fill in socket */
797: if (!KVM_READ(sock, &so, sizeof(struct socket))) {
1.17 mickey 798: dprintf("can't read sock at %p", sock);
1.1 deraadt 799: goto bad;
800: }
801:
802: /* fill in protosw entry */
803: if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
1.17 mickey 804: dprintf("can't read protosw at %p", so.so_proto);
1.1 deraadt 805: goto bad;
806: }
807:
808: /* fill in domain */
809: if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
1.17 mickey 810: dprintf("can't read domain at %p", proto.pr_domain);
1.1 deraadt 811: goto bad;
812: }
813:
814: if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
1.15 deraadt 815: sizeof(dname) - 1)) != sizeof(dname) -1) {
1.17 mickey 816: dprintf("can't read domain name at %p", dom.dom_name);
1.1 deraadt 817: dname[0] = '\0';
1.38 deraadt 818: } else
1.1 deraadt 819: dname[len] = '\0';
820:
821: if ((u_short)so.so_type > STYPEMAX)
822: printf("* %s ?%d", dname, so.so_type);
823: else
824: printf("* %s %s", dname, stypename[so.so_type]);
825:
1.34 deraadt 826: /*
1.1 deraadt 827: * protocol specific formatting
828: *
829: * Try to find interesting things to print. For tcp, the interesting
830: * thing is the address of the tcpcb, for udp and others, just the
831: * inpcb (socket pcb). For unix domain, its the address of the socket
832: * pcb and the address of the connected pcb (if connected). Otherwise
833: * just print the protocol number and address of the socket itself.
834: * The idea is not to duplicate netstat, but to make available enough
835: * information for further analysis.
836: */
837: switch(dom.dom_family) {
838: case AF_INET:
839: getinetproto(proto.pr_protocol);
1.5 deraadt 840: if (proto.pr_protocol == IPPROTO_TCP) {
841: if (so.so_pcb == NULL)
842: break;
843: if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
844: sizeof(struct inpcb)) != sizeof(struct inpcb)) {
1.17 mickey 845: dprintf("can't read inpcb at %p", so.so_pcb);
1.5 deraadt 846: goto bad;
847: }
1.11 millert 848: printf(" %p", inpcb.inp_ppcb);
1.5 deraadt 849: printf(" %s:%d",
850: inpcb.inp_laddr.s_addr == INADDR_ANY ? "*" :
851: inet_ntoa(inpcb.inp_laddr),
852: ntohs(inpcb.inp_lport));
1.17 mickey 853: if (inpcb.inp_fport) {
1.13 deraadt 854: if (so.so_state & SS_CONNECTOUT)
855: printf(" --> ");
856: else
857: printf(" <-- ");
858: printf("%s:%d",
1.5 deraadt 859: inpcb.inp_faddr.s_addr == INADDR_ANY ? "*" :
860: inet_ntoa(inpcb.inp_faddr),
861: ntohs(inpcb.inp_fport));
1.17 mickey 862: }
1.5 deraadt 863: } else if (proto.pr_protocol == IPPROTO_UDP) {
864: if (so.so_pcb == NULL)
865: break;
866: if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
867: sizeof(struct inpcb)) != sizeof(struct inpcb)) {
1.17 mickey 868: dprintf("can't read inpcb at %p", so.so_pcb);
1.5 deraadt 869: goto bad;
1.1 deraadt 870: }
1.5 deraadt 871: printf(" %s:%d",
872: inpcb.inp_laddr.s_addr == INADDR_ANY ? "*" :
873: inet_ntoa(inpcb.inp_laddr),
874: ntohs(inpcb.inp_lport));
875: if (inpcb.inp_fport)
1.6 deraadt 876: printf(" <-> %s:%d",
1.5 deraadt 877: inpcb.inp_faddr.s_addr == INADDR_ANY ? "*" :
878: inet_ntoa(inpcb.inp_faddr),
879: ntohs(inpcb.inp_fport));
880: } else if (so.so_pcb)
1.11 millert 881: printf(" %p", so.so_pcb);
1.1 deraadt 882: break;
1.26 itojun 883: #ifdef INET6
884: case AF_INET6:
885: getinetproto(proto.pr_protocol);
886: if (proto.pr_protocol == IPPROTO_TCP) {
887: if (so.so_pcb == NULL)
888: break;
889: if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
890: sizeof(struct inpcb)) != sizeof(struct inpcb)) {
891: dprintf("can't read inpcb at %p", so.so_pcb);
892: goto bad;
893: }
894: printf(" %p", inpcb.inp_ppcb);
895: snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]",
896: inet6_addrstr(&inpcb.inp_laddr6));
897: printf(" %s:%d",
898: IN6_IS_ADDR_UNSPECIFIED(&inpcb.inp_laddr6) ? "*" :
899: xaddrbuf,
900: ntohs(inpcb.inp_lport));
901: if (inpcb.inp_fport) {
902: if (so.so_state & SS_CONNECTOUT)
903: printf(" --> ");
904: else
905: printf(" <-- ");
906: snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]",
907: inet6_addrstr(&inpcb.inp_faddr6));
908: printf("%s:%d",
909: IN6_IS_ADDR_UNSPECIFIED(&inpcb.inp_faddr6) ? "*" :
910: xaddrbuf,
911: ntohs(inpcb.inp_fport));
912: }
913: } else if (proto.pr_protocol == IPPROTO_UDP) {
914: if (so.so_pcb == NULL)
915: break;
916: if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
917: sizeof(struct inpcb)) != sizeof(struct inpcb)) {
918: dprintf("can't read inpcb at %p", so.so_pcb);
919: goto bad;
920: }
921: snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]",
922: inet6_addrstr(&inpcb.inp_laddr6));
923: printf(" %s:%d",
924: IN6_IS_ADDR_UNSPECIFIED(&inpcb.inp_laddr6) ? "*" :
925: xaddrbuf,
926: ntohs(inpcb.inp_lport));
1.33 itojun 927: if (inpcb.inp_fport) {
1.26 itojun 928: snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]",
929: inet6_addrstr(&inpcb.inp_faddr6));
930: printf(" <-> %s:%d",
931: IN6_IS_ADDR_UNSPECIFIED(&inpcb.inp_faddr6) ? "*" :
932: xaddrbuf,
933: ntohs(inpcb.inp_fport));
1.33 itojun 934: }
1.26 itojun 935: } else if (so.so_pcb)
936: printf(" %p", so.so_pcb);
937: break;
938: #endif
1.1 deraadt 939: case AF_UNIX:
940: /* print address of pcb and connected pcb */
941: if (so.so_pcb) {
1.11 millert 942: printf(" %p", so.so_pcb);
1.1 deraadt 943: if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb,
944: sizeof(struct unpcb)) != sizeof(struct unpcb)){
1.17 mickey 945: dprintf("can't read unpcb at %p", so.so_pcb);
1.1 deraadt 946: goto bad;
947: }
948: if (unpcb.unp_conn) {
949: char shoconn[4], *cp;
950:
951: cp = shoconn;
952: if (!(so.so_state & SS_CANTRCVMORE))
953: *cp++ = '<';
954: *cp++ = '-';
955: if (!(so.so_state & SS_CANTSENDMORE))
956: *cp++ = '>';
957: *cp = '\0';
1.11 millert 958: printf(" %s %p", shoconn,
959: unpcb.unp_conn);
1.1 deraadt 960: }
961: }
962: break;
963: default:
964: /* print protocol number and socket address */
1.11 millert 965: printf(" %d %p", proto.pr_protocol, sock);
1.1 deraadt 966: }
967: printf("\n");
968: return;
969: bad:
970: printf("* error\n");
971: }
972:
973: /*
974: * getinetproto --
975: * print name of protocol number
976: */
977: void
978: getinetproto(number)
979: int number;
980: {
1.24 deraadt 981: static int isopen;
1.32 mpech 982: struct protoent *pe;
1.1 deraadt 983:
1.24 deraadt 984: if (!isopen)
985: setprotoent(++isopen);
986: if ((pe = getprotobynumber(number)) != NULL)
987: printf(" %s", pe->p_name);
988: else
1.1 deraadt 989: printf(" %d", number);
990: }
991:
1.11 millert 992: int
1.40 deraadt 993: getfname(char *filename)
1.1 deraadt 994: {
995: struct stat statbuf;
996: DEVS *cur;
997:
998: if (stat(filename, &statbuf)) {
1.27 millert 999: warn("%s", filename);
1.1 deraadt 1000: return(0);
1001: }
1.17 mickey 1002: if ((cur = malloc(sizeof(DEVS))) == NULL)
1003: err(1, "malloc");
1.1 deraadt 1004: cur->next = devs;
1005: devs = cur;
1006:
1007: cur->ino = statbuf.st_ino;
1008: cur->fsid = statbuf.st_dev & 0xffff;
1009: cur->name = filename;
1010: return(1);
1011: }
1012:
1013: void
1.40 deraadt 1014: usage(void)
1.1 deraadt 1015: {
1.48 jmc 1016: fprintf(stderr, "usage: fstat [-fnov] [-M core] [-N system] "
1017: "[-p pid] [-u user] [file ...]\n");
1.1 deraadt 1018: exit(1);
1019: }