Annotation of src/usr.bin/fstat/fstat.c, Revision 1.50
1.50 ! itojun 1: /* $OpenBSD: fstat.c,v 1.49 2004/01/08 19:28:56 millert 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.50 ! itojun 40: static char *rcsid = "$OpenBSD: fstat.c,v 1.49 2004/01/08 19:28:56 millert Exp $";
1.1 deraadt 41: #endif /* not lint */
42:
43: #include <sys/param.h>
44: #include <sys/time.h>
45: #include <sys/proc.h>
46: #include <sys/user.h>
47: #include <sys/stat.h>
48: #include <sys/vnode.h>
49: #include <sys/socket.h>
50: #include <sys/socketvar.h>
51: #include <sys/domain.h>
52: #include <sys/protosw.h>
1.37 deraadt 53: #include <sys/event.h>
54: #include <sys/eventvar.h>
1.1 deraadt 55: #include <sys/unpcb.h>
56: #include <sys/sysctl.h>
57: #include <sys/filedesc.h>
1.42 tedu 58: #define _KERNEL
1.23 deraadt 59: #include <sys/mount.h>
1.37 deraadt 60: #include <crypto/cryptodev.h>
1.38 deraadt 61: #include <dev/systrace.h>
1.1 deraadt 62: #include <sys/file.h>
63: #include <ufs/ufs/quota.h>
64: #include <ufs/ufs/inode.h>
1.23 deraadt 65: #include <miscfs/nullfs/null.h>
1.1 deraadt 66: #undef _KERNEL
67: #define NFS
1.2 niklas 68: #include <nfs/nfsproto.h>
1.1 deraadt 69: #include <nfs/rpcv2.h>
70: #include <nfs/nfs.h>
71: #include <nfs/nfsnode.h>
72: #undef NFS
73:
1.29 deraadt 74: #include <xfs/xfs_config.h>
1.18 art 75: #include <xfs/xfs_node.h>
76:
1.1 deraadt 77: #include <net/route.h>
78: #include <netinet/in.h>
79: #include <netinet/in_systm.h>
80: #include <netinet/ip.h>
81: #include <netinet/in_pcb.h>
82:
1.26 itojun 83: #ifdef INET6
84: #include <netinet/ip6.h>
85: #include <netinet6/ip6_var.h>
86: #endif
87:
88: #include <netdb.h>
1.5 deraadt 89: #include <arpa/inet.h>
90:
1.22 art 91: #include <sys/pipe.h>
92:
1.1 deraadt 93: #include <ctype.h>
94: #include <errno.h>
95: #include <kvm.h>
1.2 niklas 96: #include <limits.h>
1.1 deraadt 97: #include <nlist.h>
98: #include <paths.h>
99: #include <pwd.h>
100: #include <stdio.h>
101: #include <stdlib.h>
102: #include <string.h>
1.11 millert 103: #include <unistd.h>
1.24 deraadt 104: #include <netdb.h>
1.17 mickey 105: #include <err.h>
1.14 deraadt 106: #include "fstat.h"
1.1 deraadt 107:
108: #define TEXT -1
109: #define CDIR -2
110: #define RDIR -3
111: #define TRACE -4
112:
113: typedef struct devs {
114: struct devs *next;
115: long fsid;
116: ino_t ino;
117: char *name;
118: } DEVS;
119: DEVS *devs;
120:
1.46 deraadt 121: int fsflg; /* show files on same filesystem as file(s) argument */
122: int pflg; /* show files open by a particular pid */
123: int uflg; /* show files open by a particular (effective) user */
1.38 deraadt 124: int checkfile; /* true if restricting to particular files or filesystems */
1.1 deraadt 125: int nflg; /* (numerical) display f.s. and rdev as dev_t */
1.28 hugh 126: int oflg; /* display file offset */
1.1 deraadt 127: int vflg; /* display errors in locating kernel data objects etc... */
128:
129: struct file **ofiles; /* buffer of pointers to file structures */
130: int maxfiles;
131: #define ALLOC_OFILES(d) \
132: if ((d) > maxfiles) { \
133: free(ofiles); \
134: ofiles = malloc((d) * sizeof(struct file *)); \
1.17 mickey 135: if (ofiles == NULL) \
136: err(1, "malloc"); \
1.1 deraadt 137: maxfiles = (d); \
138: }
139:
140: /*
1.34 deraadt 141: * a kvm_read that returns true if everything is read
1.1 deraadt 142: */
143: #define KVM_READ(kaddr, paddr, len) \
1.11 millert 144: (kvm_read(kd, (u_long)(kaddr), (void *)(paddr), (len)) == (len))
1.1 deraadt 145:
146: kvm_t *kd;
147:
1.35 millert 148: int ufs_filestat(struct vnode *, struct filestat *);
149: int ext2fs_filestat(struct vnode *, struct filestat *);
150: int isofs_filestat(struct vnode *, struct filestat *);
151: int msdos_filestat(struct vnode *, struct filestat *);
152: int nfs_filestat(struct vnode *, struct filestat *);
153: int xfs_filestat(struct vnode *, struct filestat *);
154: int null_filestat(struct vnode *, struct filestat *);
1.49 millert 155: void dofiles(struct kinfo_proc2 *);
1.35 millert 156: void getinetproto(int);
157: void socktrans(struct socket *, int);
158: void usage(void);
159: void vtrans(struct vnode *, int, int, off_t);
160: int getfname(char *);
161: void pipetrans(struct pipe *, int);
1.37 deraadt 162: void kqueuetrans(struct kqueue *, int);
1.47 deraadt 163: void cryptotrans(void *, int);
164: void systracetrans(struct fsystrace *, int);
165: char *getmnton(struct mount *);
166: const char *inet6_addrstr(struct in6_addr *);
1.1 deraadt 167:
1.11 millert 168: int
1.40 deraadt 169: main(int argc, char *argv[])
1.1 deraadt 170: {
171: extern char *optarg;
172: extern int optind;
1.32 mpech 173: struct passwd *passwd;
1.49 millert 174: struct kinfo_proc2 *p, *plast;
1.1 deraadt 175: int arg, ch, what;
176: char *memf, *nlistf;
1.2 niklas 177: char buf[_POSIX2_LINE_MAX];
1.1 deraadt 178: int cnt;
179:
180: arg = 0;
181: what = KERN_PROC_ALL;
182: nlistf = memf = NULL;
1.28 hugh 183: oflg = 0;
184: while ((ch = getopt(argc, argv, "fnop:u:vN:M:")) != -1)
1.1 deraadt 185: switch((char)ch) {
186: case 'f':
187: fsflg = 1;
188: break;
189: case 'M':
190: memf = optarg;
191: break;
192: case 'N':
193: nlistf = optarg;
194: break;
195: case 'n':
196: nflg = 1;
197: break;
1.28 hugh 198: case 'o':
199: oflg = 1;
200: break;
1.1 deraadt 201: case 'p':
202: if (pflg++)
203: usage();
204: if (!isdigit(*optarg)) {
1.36 mpech 205: warnx( "-p requires a process id");
1.1 deraadt 206: usage();
207: }
208: what = KERN_PROC_PID;
209: arg = atoi(optarg);
210: break;
211: case 'u':
212: if (uflg++)
213: usage();
1.17 mickey 214: if (!(passwd = getpwnam(optarg)))
1.21 deraadt 215: errx(1, "%s: unknown uid", optarg);
1.1 deraadt 216: what = KERN_PROC_UID;
217: arg = passwd->pw_uid;
218: break;
219: case 'v':
220: vflg = 1;
221: break;
222: default:
223: usage();
224: }
225:
1.41 deraadt 226: /*
227: * Discard setgid privileges if not the running kernel so that bad
228: * guys can't print interesting stuff from kernel memory.
229: */
230: if (nlistf != NULL || memf != NULL) {
231: setegid(getgid());
232: setgid(getgid());
233: }
234:
235: if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL)
236: errx(1, "%s", buf);
237:
238: setegid(getgid());
239: setgid(getgid());
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.28 hugh 263: "USER CMD PID FD DEV INUM MODE R/W DV|SZ");
1.1 deraadt 264: else
265: printf("%s",
1.28 hugh 266: "USER CMD PID FD MOUNT INUM MODE R/W DV|SZ");
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.23 deraadt 439: case VT_NULL:
440: if (!null_filestat(&vn, &fst))
441: badtype = "error";
1.34 deraadt 442: break;
1.1 deraadt 443: default: {
1.12 deraadt 444: static char unknown[30];
1.34 deraadt 445: snprintf(badtype = unknown, sizeof unknown,
446: "?(%x)", vn.v_tag);
1.10 deraadt 447: break;
1.1 deraadt 448: }
449: }
450: if (checkfile) {
451: int fsmatch = 0;
1.32 mpech 452: DEVS *d;
1.1 deraadt 453:
454: if (badtype)
455: return;
456: for (d = devs; d != NULL; d = d->next)
457: if (d->fsid == fst.fsid) {
458: fsmatch = 1;
459: if (d->ino == fst.fileid) {
460: filename = d->name;
461: break;
462: }
463: }
464: if (fsmatch == 0 || (filename == NULL && fsflg == 0))
465: return;
466: }
467: PREFIX(i);
468: if (badtype) {
469: (void)printf(" - - %10s -\n", badtype);
470: return;
471: }
472: if (nflg)
1.31 deraadt 473: (void)printf(" %2ld,%-2ld", (long)major(fst.fsid),
474: (long)minor(fst.fsid));
1.1 deraadt 475: else
476: (void)printf(" %-8s", getmnton(vn.v_mount));
477: if (nflg)
1.34 deraadt 478: (void)snprintf(mode, sizeof mode, "%o", fst.mode);
1.1 deraadt 479: else
480: strmode(fst.mode, mode);
1.28 hugh 481: (void)printf(" %6ld %11s", fst.fileid, mode);
482: rw[0] = '\0';
483: if (flag & FREAD)
1.34 deraadt 484: strlcat(rw, "r", sizeof rw);
1.28 hugh 485: if (flag & FWRITE)
1.34 deraadt 486: strlcat(rw, "w", sizeof rw);
1.28 hugh 487: printf(" %2s", rw);
1.1 deraadt 488: switch (vn.v_type) {
489: case VBLK:
490: case VCHR: {
491: char *name;
492:
1.34 deraadt 493: if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ?
1.1 deraadt 494: S_IFCHR : S_IFBLK)) == NULL))
1.28 hugh 495: printf(" %2d,%-3d", major(fst.rdev), minor(fst.rdev));
1.1 deraadt 496: else
1.28 hugh 497: printf(" %7s", name);
498: if (oflg)
499: printf(" ");
1.1 deraadt 500: break;
501: }
502: default:
1.31 deraadt 503: printf(" %8lld", (long long)fst.size);
1.28 hugh 504: if (oflg)
1.31 deraadt 505: printf(":%-8lld", (long long)offset);
1.1 deraadt 506: }
507: if (filename && !fsflg)
1.28 hugh 508: printf(" %s", filename);
1.1 deraadt 509: putchar('\n');
510: }
511:
512: int
1.40 deraadt 513: ufs_filestat(struct vnode *vp, struct filestat *fsp)
1.1 deraadt 514: {
515: struct inode inode;
516:
517: if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
1.39 mpech 518: dprintf("can't read inode at %p for pid %ld",
519: VTOI(vp), (long)Pid);
1.1 deraadt 520: return 0;
521: }
522: fsp->fsid = inode.i_dev & 0xffff;
523: fsp->fileid = (long)inode.i_number;
1.11 millert 524: fsp->mode = inode.i_ffs_mode;
525: fsp->size = inode.i_ffs_size;
1.9 downsj 526: fsp->rdev = inode.i_ffs_rdev;
527:
528: return 1;
529: }
530:
531: int
1.40 deraadt 532: ext2fs_filestat(struct vnode *vp, struct filestat *fsp)
1.9 downsj 533: {
534: struct inode inode;
535:
536: if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
1.39 mpech 537: dprintf("can't read inode at %p for pid %ld",
538: VTOI(vp), (long)Pid);
1.9 downsj 539: return 0;
540: }
541: fsp->fsid = inode.i_dev & 0xffff;
542: fsp->fileid = (long)inode.i_number;
1.11 millert 543: fsp->mode = inode.i_e2fs_mode;
544: fsp->size = inode.i_e2fs_size;
1.9 downsj 545: fsp->rdev = 0; /* XXX */
1.14 deraadt 546:
547: return 1;
548: }
549:
550: int
1.40 deraadt 551: msdos_filestat(struct vnode *vp, struct filestat *fsp)
1.14 deraadt 552: {
553: #if 0
554: struct inode inode;
555:
556: if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
1.39 mpech 557: dprintf("can't read inode at %p for pid %ld",
558: VTOI(vp), (long)Pid);
1.14 deraadt 559: return 0;
560: }
561: fsp->fsid = inode.i_dev & 0xffff;
562: fsp->fileid = (long)inode.i_number;
563: fsp->mode = inode.i_e2fs_mode;
564: fsp->size = inode.i_e2fs_size;
565: fsp->rdev = 0; /* XXX */
566: #endif
1.1 deraadt 567:
568: return 1;
569: }
570:
571: int
1.40 deraadt 572: nfs_filestat(struct vnode *vp, struct filestat *fsp)
1.1 deraadt 573: {
574: struct nfsnode nfsnode;
1.32 mpech 575: mode_t mode;
1.1 deraadt 576:
577: if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
1.39 mpech 578: dprintf("can't read nfsnode at %p for pid %ld",
579: VTONFS(vp), (long)Pid);
1.1 deraadt 580: return 0;
581: }
582: fsp->fsid = nfsnode.n_vattr.va_fsid;
583: fsp->fileid = nfsnode.n_vattr.va_fileid;
584: fsp->size = nfsnode.n_size;
585: fsp->rdev = nfsnode.n_vattr.va_rdev;
586: mode = (mode_t)nfsnode.n_vattr.va_mode;
587: switch (vp->v_type) {
588: case VREG:
589: mode |= S_IFREG;
590: break;
591: case VDIR:
592: mode |= S_IFDIR;
593: break;
594: case VBLK:
595: mode |= S_IFBLK;
596: break;
597: case VCHR:
598: mode |= S_IFCHR;
599: break;
600: case VLNK:
601: mode |= S_IFLNK;
602: break;
603: case VSOCK:
604: mode |= S_IFSOCK;
605: break;
606: case VFIFO:
607: mode |= S_IFIFO;
608: break;
1.17 mickey 609: default:
610: break;
1.34 deraadt 611: }
1.1 deraadt 612: fsp->mode = mode;
613:
614: return 1;
615: }
616:
1.18 art 617: int
1.44 deraadt 618: xfs_filestat(struct vnode *vp, struct filestat *fsp)
1.18 art 619: {
620: struct xfs_node xfs_node;
621:
622: if (!KVM_READ(VNODE_TO_XNODE(vp), &xfs_node, sizeof (xfs_node))) {
1.39 mpech 623: dprintf("can't read xfs_node at %p for pid %ld",
624: VTOI(vp), (long)Pid);
1.18 art 625: return 0;
626: }
627: fsp->fsid = xfs_node.attr.va_fsid;
628: fsp->fileid = (long)xfs_node.attr.va_fileid;
629: fsp->mode = xfs_node.attr.va_mode;
630: fsp->size = xfs_node.attr.va_size;
631: fsp->rdev = xfs_node.attr.va_rdev;
1.23 deraadt 632:
633: return 1;
634: }
635:
636: int
1.40 deraadt 637: null_filestat(struct vnode *vp, struct filestat *fsp)
1.23 deraadt 638: {
639: struct null_node node;
640: struct filestat fst;
641: struct vnode vn;
642: int fail = 1;
643:
644: memset(&fst, 0, sizeof fst);
645:
646: if (!KVM_READ(VTONULL(vp), &node, sizeof (node))) {
1.39 mpech 647: dprintf("can't read node at %p for pid %ld",
648: VTONULL(vp), (long)Pid);
1.23 deraadt 649: return 0;
650: }
651:
652: /*
653: * Attempt to find information that might be useful.
654: */
655: if (node.null_lowervp) {
656: if (!KVM_READ(node.null_lowervp, &vn, sizeof (vn))) {
1.39 mpech 657: dprintf("can't read vnode at %p for pid %ld",
658: node.null_lowervp, (long)Pid);
1.23 deraadt 659: return 0;
660: }
661:
662: fail = 0;
663: if (vn.v_type == VNON || vn.v_tag == VT_NON)
664: fail = 1;
665: else if (vn.v_type == VBAD)
666: fail = 1;
667: else
668: switch (vn.v_tag) {
669: case VT_UFS:
670: case VT_MFS:
671: if (!ufs_filestat(&vn, &fst))
672: fail = 1;
673: break;
674: case VT_NFS:
675: if (!nfs_filestat(&vn, &fst))
676: fail = 1;
677: break;
678: case VT_EXT2FS:
679: if (!ext2fs_filestat(&vn, &fst))
680: fail = 1;
681: break;
682: case VT_ISOFS:
683: if (!isofs_filestat(&vn, &fst))
684: fail = 1;
685: break;
686: case VT_MSDOSFS:
687: if (!msdos_filestat(&vn, &fst))
688: fail = 1;
689: break;
690: case VT_XFS:
691: if (!xfs_filestat(&vn, &fst))
692: fail = 1;
693: break;
694: default:
695: break;
696: }
697: }
698:
699: fsp->fsid = (long)node.null_vnode;
700: if (fail)
701: fsp->fileid = (long)node.null_lowervp;
702: else
1.34 deraadt 703: fsp->fileid = fst.fileid;
1.23 deraadt 704: fsp->mode = fst.mode;
705: fsp->size = fst.mode;
706: fsp->rdev = fst.mode;
1.18 art 707:
708: return 1;
709: }
1.1 deraadt 710:
711: char *
1.40 deraadt 712: getmnton(struct mount *m)
1.1 deraadt 713: {
714: static struct mount mount;
715: static struct mtab {
716: struct mtab *next;
717: struct mount *m;
718: char mntonname[MNAMELEN];
719: } *mhead = NULL;
1.32 mpech 720: struct mtab *mt;
1.1 deraadt 721:
722: for (mt = mhead; mt != NULL; mt = mt->next)
723: if (m == mt->m)
724: return (mt->mntonname);
725: if (!KVM_READ(m, &mount, sizeof(struct mount))) {
1.17 mickey 726: warn("can't read mount table at %p", m);
1.1 deraadt 727: return (NULL);
728: }
1.17 mickey 729: if ((mt = malloc(sizeof (struct mtab))) == NULL)
730: err(1, "malloc");
1.1 deraadt 731: mt->m = m;
732: bcopy(&mount.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN);
733: mt->next = mhead;
734: mhead = mt;
735: return (mt->mntonname);
1.22 art 736: }
737:
738: void
1.40 deraadt 739: pipetrans(struct pipe *pipe, int i)
1.22 art 740: {
741: struct pipe pi;
742: void *maxaddr;
743:
744: PREFIX(i);
745:
746: printf(" ");
747:
748: /* fill in socket */
749: if (!KVM_READ(pipe, &pi, sizeof(struct pipe))) {
750: dprintf("can't read pipe at %p", pipe);
751: goto bad;
752: }
753:
754: /*
755: * We don't have enough space to fit both peer and own address, so
756: * we select the higher address so both ends of the pipe have the
757: * same visible addr. (it's the higher address because when the other
758: * end closes, it becomes 0)
759: */
760: maxaddr = MAX(pipe, pi.pipe_peer);
761:
1.38 deraadt 762: printf("pipe %p state: %s%s%s\n", maxaddr,
1.34 deraadt 763: (pi.pipe_state & PIPE_WANTR) ? "R" : "",
764: (pi.pipe_state & PIPE_WANTW) ? "W" : "",
765: (pi.pipe_state & PIPE_EOF) ? "E" : "");
1.37 deraadt 766: return;
767: bad:
768: printf("* error\n");
769: }
770:
771: void
1.40 deraadt 772: kqueuetrans(struct kqueue *kq, int i)
1.37 deraadt 773: {
774: struct kqueue kqi;
775:
776: PREFIX(i);
777:
778: printf(" ");
779:
780: /* fill it in */
781: if (!KVM_READ(kq, &kqi, sizeof(struct kqueue))) {
782: dprintf("can't read kqueue at %p", kq);
783: goto bad;
784: }
785:
1.38 deraadt 786: printf("kqueue %p %d state: %s%s\n", kq, kqi.kq_count,
1.37 deraadt 787: (kqi.kq_state & KQ_SEL) ? "S" : "",
788: (kqi.kq_state & KQ_SLEEP) ? "W" : "");
789: return;
790: bad:
791: printf("* error\n");
792: }
793:
794: void
1.40 deraadt 795: cryptotrans(void *f, int i)
1.37 deraadt 796: {
797: PREFIX(i);
798:
799: printf(" ");
800:
1.38 deraadt 801: printf("crypto %p\n", f);
1.37 deraadt 802: }
803:
804: void
1.40 deraadt 805: systracetrans(struct fsystrace *f, int i)
1.37 deraadt 806: {
1.38 deraadt 807: struct fsystrace fi;
808:
1.37 deraadt 809: PREFIX(i);
810:
811: printf(" ");
812:
1.38 deraadt 813: /* fill it in */
814: if (!KVM_READ(f, &fi, sizeof(fi))) {
815: dprintf("can't read fsystrace at %p", f);
816: goto bad;
817: }
1.34 deraadt 818:
1.38 deraadt 819: printf("systrace %p npol %d\n", f, fi.npolicies);
1.22 art 820: return;
821: bad:
822: printf("* error\n");
1.1 deraadt 823: }
824:
1.26 itojun 825: #ifdef INET6
826: const char *
1.40 deraadt 827: inet6_addrstr(struct in6_addr *p)
1.26 itojun 828: {
829: struct sockaddr_in6 sin6;
830: static char hbuf[NI_MAXHOST];
1.50 ! itojun 831: const int niflags = NI_NUMERICHOST;
1.26 itojun 832:
833: memset(&sin6, 0, sizeof(sin6));
834: sin6.sin6_family = AF_INET6;
835: sin6.sin6_len = sizeof(struct sockaddr_in6);
836: sin6.sin6_addr = *p;
837: if (IN6_IS_ADDR_LINKLOCAL(p) &&
838: *(u_int16_t *)&sin6.sin6_addr.s6_addr[2] != 0) {
839: sin6.sin6_scope_id =
1.38 deraadt 840: ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);
1.26 itojun 841: sin6.sin6_addr.s6_addr[2] = sin6.sin6_addr.s6_addr[3] = 0;
842: }
843:
844: if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
1.38 deraadt 845: hbuf, sizeof(hbuf), NULL, 0, niflags))
1.26 itojun 846: return "invalid";
847:
848: return hbuf;
849: }
850: #endif
851:
1.1 deraadt 852: void
1.40 deraadt 853: socktrans(struct socket *sock, int i)
1.1 deraadt 854: {
855: static char *stypename[] = {
856: "unused", /* 0 */
1.38 deraadt 857: "stream", /* 1 */
1.1 deraadt 858: "dgram", /* 2 */
859: "raw", /* 3 */
860: "rdm", /* 4 */
861: "seqpak" /* 5 */
862: };
863: #define STYPEMAX 5
864: struct socket so;
865: struct protosw proto;
866: struct domain dom;
867: struct inpcb inpcb;
868: struct unpcb unpcb;
869: int len;
1.4 deraadt 870: char dname[32];
1.26 itojun 871: #ifdef INET6
872: char xaddrbuf[NI_MAXHOST + 2];
873: #endif
1.1 deraadt 874:
875: PREFIX(i);
876:
877: /* fill in socket */
878: if (!KVM_READ(sock, &so, sizeof(struct socket))) {
1.17 mickey 879: dprintf("can't read sock at %p", sock);
1.1 deraadt 880: goto bad;
881: }
882:
883: /* fill in protosw entry */
884: if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
1.17 mickey 885: dprintf("can't read protosw at %p", so.so_proto);
1.1 deraadt 886: goto bad;
887: }
888:
889: /* fill in domain */
890: if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
1.17 mickey 891: dprintf("can't read domain at %p", proto.pr_domain);
1.1 deraadt 892: goto bad;
893: }
894:
895: if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
1.15 deraadt 896: sizeof(dname) - 1)) != sizeof(dname) -1) {
1.17 mickey 897: dprintf("can't read domain name at %p", dom.dom_name);
1.1 deraadt 898: dname[0] = '\0';
1.38 deraadt 899: } else
1.1 deraadt 900: dname[len] = '\0';
901:
902: if ((u_short)so.so_type > STYPEMAX)
903: printf("* %s ?%d", dname, so.so_type);
904: else
905: printf("* %s %s", dname, stypename[so.so_type]);
906:
1.34 deraadt 907: /*
1.1 deraadt 908: * protocol specific formatting
909: *
910: * Try to find interesting things to print. For tcp, the interesting
911: * thing is the address of the tcpcb, for udp and others, just the
912: * inpcb (socket pcb). For unix domain, its the address of the socket
913: * pcb and the address of the connected pcb (if connected). Otherwise
914: * just print the protocol number and address of the socket itself.
915: * The idea is not to duplicate netstat, but to make available enough
916: * information for further analysis.
917: */
918: switch(dom.dom_family) {
919: case AF_INET:
920: getinetproto(proto.pr_protocol);
1.5 deraadt 921: if (proto.pr_protocol == IPPROTO_TCP) {
922: if (so.so_pcb == NULL)
923: break;
924: if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
925: sizeof(struct inpcb)) != sizeof(struct inpcb)) {
1.17 mickey 926: dprintf("can't read inpcb at %p", so.so_pcb);
1.5 deraadt 927: goto bad;
928: }
1.11 millert 929: printf(" %p", inpcb.inp_ppcb);
1.5 deraadt 930: printf(" %s:%d",
931: inpcb.inp_laddr.s_addr == INADDR_ANY ? "*" :
932: inet_ntoa(inpcb.inp_laddr),
933: ntohs(inpcb.inp_lport));
1.17 mickey 934: if (inpcb.inp_fport) {
1.13 deraadt 935: if (so.so_state & SS_CONNECTOUT)
936: printf(" --> ");
937: else
938: printf(" <-- ");
939: printf("%s:%d",
1.5 deraadt 940: inpcb.inp_faddr.s_addr == INADDR_ANY ? "*" :
941: inet_ntoa(inpcb.inp_faddr),
942: ntohs(inpcb.inp_fport));
1.17 mickey 943: }
1.5 deraadt 944: } else if (proto.pr_protocol == IPPROTO_UDP) {
945: if (so.so_pcb == NULL)
946: break;
947: if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
948: sizeof(struct inpcb)) != sizeof(struct inpcb)) {
1.17 mickey 949: dprintf("can't read inpcb at %p", so.so_pcb);
1.5 deraadt 950: goto bad;
1.1 deraadt 951: }
1.5 deraadt 952: printf(" %s:%d",
953: inpcb.inp_laddr.s_addr == INADDR_ANY ? "*" :
954: inet_ntoa(inpcb.inp_laddr),
955: ntohs(inpcb.inp_lport));
956: if (inpcb.inp_fport)
1.6 deraadt 957: printf(" <-> %s:%d",
1.5 deraadt 958: inpcb.inp_faddr.s_addr == INADDR_ANY ? "*" :
959: inet_ntoa(inpcb.inp_faddr),
960: ntohs(inpcb.inp_fport));
961: } else if (so.so_pcb)
1.11 millert 962: printf(" %p", so.so_pcb);
1.1 deraadt 963: break;
1.26 itojun 964: #ifdef INET6
965: case AF_INET6:
966: getinetproto(proto.pr_protocol);
967: if (proto.pr_protocol == IPPROTO_TCP) {
968: if (so.so_pcb == NULL)
969: break;
970: if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
971: sizeof(struct inpcb)) != sizeof(struct inpcb)) {
972: dprintf("can't read inpcb at %p", so.so_pcb);
973: goto bad;
974: }
975: printf(" %p", inpcb.inp_ppcb);
976: snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]",
977: inet6_addrstr(&inpcb.inp_laddr6));
978: printf(" %s:%d",
979: IN6_IS_ADDR_UNSPECIFIED(&inpcb.inp_laddr6) ? "*" :
980: xaddrbuf,
981: ntohs(inpcb.inp_lport));
982: if (inpcb.inp_fport) {
983: if (so.so_state & SS_CONNECTOUT)
984: printf(" --> ");
985: else
986: printf(" <-- ");
987: snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]",
988: inet6_addrstr(&inpcb.inp_faddr6));
989: printf("%s:%d",
990: IN6_IS_ADDR_UNSPECIFIED(&inpcb.inp_faddr6) ? "*" :
991: xaddrbuf,
992: ntohs(inpcb.inp_fport));
993: }
994: } else if (proto.pr_protocol == IPPROTO_UDP) {
995: if (so.so_pcb == NULL)
996: break;
997: if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
998: sizeof(struct inpcb)) != sizeof(struct inpcb)) {
999: dprintf("can't read inpcb at %p", so.so_pcb);
1000: goto bad;
1001: }
1002: snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]",
1003: inet6_addrstr(&inpcb.inp_laddr6));
1004: printf(" %s:%d",
1005: IN6_IS_ADDR_UNSPECIFIED(&inpcb.inp_laddr6) ? "*" :
1006: xaddrbuf,
1007: ntohs(inpcb.inp_lport));
1.33 itojun 1008: if (inpcb.inp_fport) {
1.26 itojun 1009: snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]",
1010: inet6_addrstr(&inpcb.inp_faddr6));
1011: printf(" <-> %s:%d",
1012: IN6_IS_ADDR_UNSPECIFIED(&inpcb.inp_faddr6) ? "*" :
1013: xaddrbuf,
1014: ntohs(inpcb.inp_fport));
1.33 itojun 1015: }
1.26 itojun 1016: } else if (so.so_pcb)
1017: printf(" %p", so.so_pcb);
1018: break;
1019: #endif
1.1 deraadt 1020: case AF_UNIX:
1021: /* print address of pcb and connected pcb */
1022: if (so.so_pcb) {
1.11 millert 1023: printf(" %p", so.so_pcb);
1.1 deraadt 1024: if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb,
1025: sizeof(struct unpcb)) != sizeof(struct unpcb)){
1.17 mickey 1026: dprintf("can't read unpcb at %p", so.so_pcb);
1.1 deraadt 1027: goto bad;
1028: }
1029: if (unpcb.unp_conn) {
1030: char shoconn[4], *cp;
1031:
1032: cp = shoconn;
1033: if (!(so.so_state & SS_CANTRCVMORE))
1034: *cp++ = '<';
1035: *cp++ = '-';
1036: if (!(so.so_state & SS_CANTSENDMORE))
1037: *cp++ = '>';
1038: *cp = '\0';
1.11 millert 1039: printf(" %s %p", shoconn,
1040: unpcb.unp_conn);
1.1 deraadt 1041: }
1042: }
1043: break;
1044: default:
1045: /* print protocol number and socket address */
1.11 millert 1046: printf(" %d %p", proto.pr_protocol, sock);
1.1 deraadt 1047: }
1048: printf("\n");
1049: return;
1050: bad:
1051: printf("* error\n");
1052: }
1053:
1054: /*
1055: * getinetproto --
1056: * print name of protocol number
1057: */
1058: void
1059: getinetproto(number)
1060: int number;
1061: {
1.24 deraadt 1062: static int isopen;
1.32 mpech 1063: struct protoent *pe;
1.1 deraadt 1064:
1.24 deraadt 1065: if (!isopen)
1066: setprotoent(++isopen);
1067: if ((pe = getprotobynumber(number)) != NULL)
1068: printf(" %s", pe->p_name);
1069: else
1.1 deraadt 1070: printf(" %d", number);
1071: }
1072:
1.11 millert 1073: int
1.40 deraadt 1074: getfname(char *filename)
1.1 deraadt 1075: {
1076: struct stat statbuf;
1077: DEVS *cur;
1078:
1079: if (stat(filename, &statbuf)) {
1.27 millert 1080: warn("%s", filename);
1.1 deraadt 1081: return(0);
1082: }
1.17 mickey 1083: if ((cur = malloc(sizeof(DEVS))) == NULL)
1084: err(1, "malloc");
1.1 deraadt 1085: cur->next = devs;
1086: devs = cur;
1087:
1088: cur->ino = statbuf.st_ino;
1089: cur->fsid = statbuf.st_dev & 0xffff;
1090: cur->name = filename;
1091: return(1);
1092: }
1093:
1094: void
1.40 deraadt 1095: usage(void)
1.1 deraadt 1096: {
1.48 jmc 1097: fprintf(stderr, "usage: fstat [-fnov] [-M core] [-N system] "
1098: "[-p pid] [-u user] [file ...]\n");
1.1 deraadt 1099: exit(1);
1100: }