Annotation of src/usr.bin/fstat/fstat.c, Revision 1.24
1.24 ! deraadt 1: /* $OpenBSD: fstat.c,v 1.23 1999/07/01 21:41:58 deraadt Exp $ */
1.2 niklas 2:
1.1 deraadt 3: /*-
4: * Copyright (c) 1988, 1993
5: * The Regents of the University of California. All rights reserved.
6: *
7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in the
14: * documentation and/or other materials provided with the distribution.
15: * 3. All advertising materials mentioning features or use of this software
16: * must display the following acknowledgement:
17: * This product includes software developed by the University of
18: * California, Berkeley and its contributors.
19: * 4. Neither the name of the University nor the names of its contributors
20: * may be used to endorse or promote products derived from this software
21: * without specific prior written permission.
22: *
23: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33: * SUCH DAMAGE.
34: */
35:
36: #ifndef lint
37: static char copyright[] =
38: "@(#) Copyright (c) 1988, 1993\n\
39: The Regents of the University of California. All rights reserved.\n";
40: #endif /* not lint */
41:
42: #ifndef lint
43: /*static char sccsid[] = "from: @(#)fstat.c 8.1 (Berkeley) 6/6/93";*/
1.24 ! deraadt 44: static char *rcsid = "$OpenBSD: fstat.c,v 1.23 1999/07/01 21:41:58 deraadt Exp $";
1.1 deraadt 45: #endif /* not lint */
46:
47: #include <sys/param.h>
48: #include <sys/time.h>
49: #include <sys/proc.h>
50: #include <sys/user.h>
51: #include <sys/stat.h>
52: #include <sys/vnode.h>
53: #include <sys/socket.h>
54: #include <sys/socketvar.h>
55: #include <sys/domain.h>
56: #include <sys/protosw.h>
57: #include <sys/unpcb.h>
58: #include <sys/sysctl.h>
59: #include <sys/filedesc.h>
1.23 deraadt 60: #include <sys/mount.h>
1.1 deraadt 61: #define _KERNEL
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.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.5 deraadt 82: #include <arpa/inet.h>
83:
1.22 art 84: #define PIPE_NODIRECT /* XXX - define here, since it's not defined
85: outside _KERNEL */
86: #include <sys/pipe.h>
87:
1.1 deraadt 88: #include <ctype.h>
89: #include <errno.h>
90: #include <kvm.h>
1.2 niklas 91: #include <limits.h>
1.1 deraadt 92: #include <nlist.h>
93: #include <paths.h>
94: #include <pwd.h>
95: #include <stdio.h>
96: #include <stdlib.h>
97: #include <string.h>
1.11 millert 98: #include <unistd.h>
1.24 ! deraadt 99: #include <netdb.h>
1.17 mickey 100: #include <err.h>
1.14 deraadt 101: #include "fstat.h"
1.1 deraadt 102:
103: #define TEXT -1
104: #define CDIR -2
105: #define RDIR -3
106: #define TRACE -4
107:
108: typedef struct devs {
109: struct devs *next;
110: long fsid;
111: ino_t ino;
112: char *name;
113: } DEVS;
114: DEVS *devs;
115:
116: int fsflg, /* show files on same filesystem as file(s) argument */
117: pflg, /* show files open by a particular pid */
118: uflg; /* show files open by a particular (effective) user */
119: int checkfile; /* true if restricting to particular files or filesystems */
120: int nflg; /* (numerical) display f.s. and rdev as dev_t */
121: int vflg; /* display errors in locating kernel data objects etc... */
122:
123: struct file **ofiles; /* buffer of pointers to file structures */
124: int maxfiles;
125: #define ALLOC_OFILES(d) \
126: if ((d) > maxfiles) { \
127: free(ofiles); \
128: ofiles = malloc((d) * sizeof(struct file *)); \
1.17 mickey 129: if (ofiles == NULL) \
130: err(1, "malloc"); \
1.1 deraadt 131: maxfiles = (d); \
132: }
133:
134: /*
135: * a kvm_read that returns true if everything is read
136: */
137: #define KVM_READ(kaddr, paddr, len) \
1.11 millert 138: (kvm_read(kd, (u_long)(kaddr), (void *)(paddr), (len)) == (len))
1.1 deraadt 139:
140: kvm_t *kd;
141:
1.11 millert 142: int ufs_filestat __P((struct vnode *, struct filestat *));
143: int ext2fs_filestat __P((struct vnode *, struct filestat *));
1.14 deraadt 144: int isofs_filestat __P((struct vnode *, struct filestat *));
145: int msdos_filestat __P((struct vnode *, struct filestat *));
1.11 millert 146: int nfs_filestat __P((struct vnode *, struct filestat *));
147: void dofiles __P((struct kinfo_proc *));
148: void getinetproto __P((int));
149: void socktrans __P((struct socket *, int));
150: void usage __P((void));
151: void vtrans __P((struct vnode *, int, int));
152: int getfname __P((char *));
1.22 art 153: void pipetrans __P((struct pipe *, int));
1.1 deraadt 154:
1.11 millert 155: int
1.1 deraadt 156: main(argc, argv)
157: int argc;
158: char **argv;
159: {
160: extern char *optarg;
161: extern int optind;
162: register struct passwd *passwd;
163: struct kinfo_proc *p, *plast;
164: int arg, ch, what;
165: char *memf, *nlistf;
1.2 niklas 166: char buf[_POSIX2_LINE_MAX];
1.1 deraadt 167: int cnt;
168:
169: arg = 0;
170: what = KERN_PROC_ALL;
171: nlistf = memf = NULL;
1.8 millert 172: while ((ch = getopt(argc, argv, "fnp:u:vN:M:")) != -1)
1.1 deraadt 173: switch((char)ch) {
174: case 'f':
175: fsflg = 1;
176: break;
177: case 'M':
178: memf = optarg;
179: break;
180: case 'N':
181: nlistf = optarg;
182: break;
183: case 'n':
184: nflg = 1;
185: break;
186: case 'p':
187: if (pflg++)
188: usage();
189: if (!isdigit(*optarg)) {
1.17 mickey 190: warnx( "-p requires a process id\n");
1.1 deraadt 191: usage();
192: }
193: what = KERN_PROC_PID;
194: arg = atoi(optarg);
195: break;
196: case 'u':
197: if (uflg++)
198: usage();
1.17 mickey 199: if (!(passwd = getpwnam(optarg)))
1.21 deraadt 200: errx(1, "%s: unknown uid", optarg);
1.1 deraadt 201: what = KERN_PROC_UID;
202: arg = passwd->pw_uid;
203: break;
204: case 'v':
205: vflg = 1;
206: break;
207: case '?':
208: default:
209: usage();
210: }
211:
212: if (*(argv += optind)) {
213: for (; *argv; ++argv) {
214: if (getfname(*argv))
215: checkfile = 1;
216: }
217: if (!checkfile) /* file(s) specified, but none accessable */
218: exit(1);
219: }
220:
221: ALLOC_OFILES(256); /* reserve space for file pointers */
222:
223: if (fsflg && !checkfile) {
224: /* -f with no files means use wd */
225: if (getfname(".") == 0)
226: exit(1);
227: checkfile = 1;
228: }
229:
230: /*
231: * Discard setgid privileges if not the running kernel so that bad
232: * guys can't print interesting stuff from kernel memory.
233: */
1.7 tholo 234: if (nlistf != NULL || memf != NULL) {
235: setegid(getgid());
1.1 deraadt 236: setgid(getgid());
1.7 tholo 237: }
1.1 deraadt 238:
1.17 mickey 239: if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL)
1.21 deraadt 240: errx(1, "%s", buf);
1.16 deraadt 241:
242: setegid(getgid());
243: setgid(getgid());
244:
1.17 mickey 245: if ((p = kvm_getprocs(kd, what, arg, &cnt)) == NULL)
1.21 deraadt 246: errx(1, "%s", kvm_geterr(kd));
1.1 deraadt 247: if (nflg)
248: printf("%s",
249: "USER CMD PID FD DEV INUM MODE SZ|DV R/W");
250: else
251: printf("%s",
252: "USER CMD PID FD MOUNT INUM MODE SZ|DV R/W");
253: if (checkfile && fsflg == 0)
254: printf(" NAME\n");
255: else
256: putchar('\n');
257:
258: for (plast = &p[cnt]; p < plast; ++p) {
259: if (p->kp_proc.p_stat == SZOMB)
260: continue;
261: dofiles(p);
262: }
263: exit(0);
264: }
265:
266: char *Uname, *Comm;
1.14 deraadt 267: pid_t Pid;
1.1 deraadt 268:
269: #define PREFIX(i) printf("%-8.8s %-10s %5d", Uname, Comm, Pid); \
270: switch(i) { \
271: case TEXT: \
272: printf(" text"); \
273: break; \
274: case CDIR: \
275: printf(" wd"); \
276: break; \
277: case RDIR: \
278: printf(" root"); \
279: break; \
280: case TRACE: \
281: printf(" tr"); \
282: break; \
283: default: \
284: printf(" %4d", i); \
285: break; \
286: }
287:
288: /*
289: * print open files attributed to this process
290: */
291: void
292: dofiles(kp)
293: struct kinfo_proc *kp;
294: {
1.11 millert 295: int i;
1.1 deraadt 296: struct file file;
297: struct filedesc0 filed0;
298: #define filed filed0.fd_fd
299: struct proc *p = &kp->kp_proc;
300: struct eproc *ep = &kp->kp_eproc;
301:
302: extern char *user_from_uid();
303:
304: Uname = user_from_uid(ep->e_ucred.cr_uid, 0);
305: Pid = p->p_pid;
306: Comm = p->p_comm;
307:
308: if (p->p_fd == NULL)
309: return;
310: if (!KVM_READ(p->p_fd, &filed0, sizeof (filed0))) {
1.17 mickey 311: dprintf("can't read filedesc at %p for pid %d", p->p_fd, Pid);
1.1 deraadt 312: return;
313: }
314: if (filed.fd_nfiles < 0 || filed.fd_lastfile >= filed.fd_nfiles ||
315: filed.fd_freefile > filed.fd_lastfile + 1) {
1.17 mickey 316: dprintf("filedesc corrupted at %p for pid %d", p->p_fd, Pid);
1.1 deraadt 317: return;
318: }
319: /*
320: * root directory vnode, if one
321: */
322: if (filed.fd_rdir)
323: vtrans(filed.fd_rdir, RDIR, FREAD);
324: /*
325: * current working directory vnode
326: */
327: vtrans(filed.fd_cdir, CDIR, FREAD);
328: /*
329: * ktrace vnode, if one
330: */
331: if (p->p_tracep)
332: vtrans(p->p_tracep, TRACE, FREAD|FWRITE);
333: /*
334: * open files
335: */
336: #define FPSIZE (sizeof (struct file *))
337: ALLOC_OFILES(filed.fd_lastfile+1);
338: if (filed.fd_nfiles > NDFILE) {
339: if (!KVM_READ(filed.fd_ofiles, ofiles,
340: (filed.fd_lastfile+1) * FPSIZE)) {
1.17 mickey 341: dprintf("can't read file structures at %p for pid %d",
1.1 deraadt 342: filed.fd_ofiles, Pid);
343: return;
344: }
345: } else
346: bcopy(filed0.fd_dfiles, ofiles, (filed.fd_lastfile+1) * FPSIZE);
347: for (i = 0; i <= filed.fd_lastfile; i++) {
348: if (ofiles[i] == NULL)
349: continue;
350: if (!KVM_READ(ofiles[i], &file, sizeof (struct file))) {
1.17 mickey 351: dprintf("can't read file %d at %p for pid %d",
1.1 deraadt 352: i, ofiles[i], Pid);
353: continue;
354: }
355: if (file.f_type == DTYPE_VNODE)
356: vtrans((struct vnode *)file.f_data, i, file.f_flag);
357: else if (file.f_type == DTYPE_SOCKET) {
358: if (checkfile == 0)
359: socktrans((struct socket *)file.f_data, i);
1.22 art 360: } else if (file.f_type == DTYPE_PIPE) {
361: if (checkfile == 0)
362: pipetrans((struct pipe *)file.f_data, i);
363: } else {
1.17 mickey 364: dprintf("unknown file type %d for file %d of pid %d",
1.1 deraadt 365: file.f_type, i, Pid);
366: }
367: }
368: }
369:
370: void
371: vtrans(vp, i, flag)
372: struct vnode *vp;
373: int i;
374: int flag;
375: {
376: struct vnode vn;
377: struct filestat fst;
1.12 deraadt 378: char rw[3], mode[17];
1.1 deraadt 379: char *badtype = NULL, *filename, *getmnton();
380:
381: filename = badtype = NULL;
382: if (!KVM_READ(vp, &vn, sizeof (struct vnode))) {
1.17 mickey 383: dprintf("can't read vnode at %p for pid %d", vp, Pid);
1.1 deraadt 384: return;
385: }
386: if (vn.v_type == VNON || vn.v_tag == VT_NON)
387: badtype = "none";
388: else if (vn.v_type == VBAD)
389: badtype = "bad";
390: else
391: switch (vn.v_tag) {
392: case VT_UFS:
393: case VT_MFS:
394: if (!ufs_filestat(&vn, &fst))
395: badtype = "error";
396: break;
397: case VT_NFS:
398: if (!nfs_filestat(&vn, &fst))
399: badtype = "error";
400: break;
1.9 downsj 401: case VT_EXT2FS:
402: if (!ext2fs_filestat(&vn, &fst))
403: badtype = "error";
404: break;
1.14 deraadt 405: case VT_ISOFS:
406: if (!isofs_filestat(&vn, &fst))
407: badtype = "error";
408: break;
409: case VT_MSDOSFS:
410: if (!msdos_filestat(&vn, &fst))
411: badtype = "error";
412: break;
1.18 art 413: case VT_XFS:
414: if (!xfs_filestat(&vn, &fst))
415: badtype = "error";
416: break;
1.23 deraadt 417: case VT_NULL:
418: if (!null_filestat(&vn, &fst))
419: badtype = "error";
420: break;
1.1 deraadt 421: default: {
1.12 deraadt 422: static char unknown[30];
1.1 deraadt 423: sprintf(badtype = unknown, "?(%x)", vn.v_tag);
1.10 deraadt 424: break;
1.1 deraadt 425: }
426: }
427: if (checkfile) {
428: int fsmatch = 0;
429: register DEVS *d;
430:
431: if (badtype)
432: return;
433: for (d = devs; d != NULL; d = d->next)
434: if (d->fsid == fst.fsid) {
435: fsmatch = 1;
436: if (d->ino == fst.fileid) {
437: filename = d->name;
438: break;
439: }
440: }
441: if (fsmatch == 0 || (filename == NULL && fsflg == 0))
442: return;
443: }
444: PREFIX(i);
445: if (badtype) {
446: (void)printf(" - - %10s -\n", badtype);
447: return;
448: }
449: if (nflg)
450: (void)printf(" %2d,%-2d", major(fst.fsid), minor(fst.fsid));
451: else
452: (void)printf(" %-8s", getmnton(vn.v_mount));
453: if (nflg)
454: (void)sprintf(mode, "%o", fst.mode);
455: else
456: strmode(fst.mode, mode);
1.11 millert 457: (void)printf(" %6ld %10s", fst.fileid, mode);
1.1 deraadt 458: switch (vn.v_type) {
459: case VBLK:
460: case VCHR: {
461: char *name;
462:
463: if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ?
464: S_IFCHR : S_IFBLK)) == NULL))
465: printf(" %2d,%-2d", major(fst.rdev), minor(fst.rdev));
466: else
467: printf(" %6s", name);
468: break;
469: }
470: default:
1.11 millert 471: printf(" %6qd", fst.size);
1.1 deraadt 472: }
473: rw[0] = '\0';
474: if (flag & FREAD)
475: strcat(rw, "r");
476: if (flag & FWRITE)
477: strcat(rw, "w");
478: printf(" %2s", rw);
479: if (filename && !fsflg)
480: printf(" %s", filename);
481: putchar('\n');
482: }
483:
484: int
485: ufs_filestat(vp, fsp)
486: struct vnode *vp;
487: struct filestat *fsp;
488: {
489: struct inode inode;
490:
491: if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
1.17 mickey 492: dprintf("can't read inode at %p for pid %d", VTOI(vp), Pid);
1.1 deraadt 493: return 0;
494: }
495: fsp->fsid = inode.i_dev & 0xffff;
496: fsp->fileid = (long)inode.i_number;
1.11 millert 497: fsp->mode = inode.i_ffs_mode;
498: fsp->size = inode.i_ffs_size;
1.9 downsj 499: fsp->rdev = inode.i_ffs_rdev;
500:
501: return 1;
502: }
503:
504: int
505: ext2fs_filestat(vp, fsp)
506: struct vnode *vp;
507: struct filestat *fsp;
508: {
509: struct inode inode;
510:
511: if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
1.17 mickey 512: dprintf("can't read inode at %p for pid %d", VTOI(vp), Pid);
1.9 downsj 513: return 0;
514: }
515: fsp->fsid = inode.i_dev & 0xffff;
516: fsp->fileid = (long)inode.i_number;
1.11 millert 517: fsp->mode = inode.i_e2fs_mode;
518: fsp->size = inode.i_e2fs_size;
1.9 downsj 519: fsp->rdev = 0; /* XXX */
1.14 deraadt 520:
521: return 1;
522: }
523:
524: int
525: msdos_filestat(vp, fsp)
526: struct vnode *vp;
527: struct filestat *fsp;
528: {
529: #if 0
530: struct inode inode;
531:
532: if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
1.17 mickey 533: dprintf("can't read inode at %p for pid %d", VTOI(vp), Pid);
1.14 deraadt 534: return 0;
535: }
536: fsp->fsid = inode.i_dev & 0xffff;
537: fsp->fileid = (long)inode.i_number;
538: fsp->mode = inode.i_e2fs_mode;
539: fsp->size = inode.i_e2fs_size;
540: fsp->rdev = 0; /* XXX */
541: #endif
1.1 deraadt 542:
543: return 1;
544: }
545:
546: int
547: nfs_filestat(vp, fsp)
548: struct vnode *vp;
549: struct filestat *fsp;
550: {
551: struct nfsnode nfsnode;
552: register mode_t mode;
553:
554: if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
1.17 mickey 555: dprintf("can't read nfsnode at %p for pid %d", VTONFS(vp), Pid);
1.1 deraadt 556: return 0;
557: }
558: fsp->fsid = nfsnode.n_vattr.va_fsid;
559: fsp->fileid = nfsnode.n_vattr.va_fileid;
560: fsp->size = nfsnode.n_size;
561: fsp->rdev = nfsnode.n_vattr.va_rdev;
562: mode = (mode_t)nfsnode.n_vattr.va_mode;
563: switch (vp->v_type) {
564: case VREG:
565: mode |= S_IFREG;
566: break;
567: case VDIR:
568: mode |= S_IFDIR;
569: break;
570: case VBLK:
571: mode |= S_IFBLK;
572: break;
573: case VCHR:
574: mode |= S_IFCHR;
575: break;
576: case VLNK:
577: mode |= S_IFLNK;
578: break;
579: case VSOCK:
580: mode |= S_IFSOCK;
581: break;
582: case VFIFO:
583: mode |= S_IFIFO;
584: break;
1.17 mickey 585: default:
586: break;
1.1 deraadt 587: };
588: fsp->mode = mode;
589:
590: return 1;
591: }
592:
1.18 art 593: int
594: xfs_filestat(vp, fsp)
595: struct vnode *vp;
596: struct filestat *fsp;
597: {
598: struct xfs_node xfs_node;
599:
600: if (!KVM_READ(VNODE_TO_XNODE(vp), &xfs_node, sizeof (xfs_node))) {
601: dprintf("can't read xfs_node at %p for pid %d", VTOI(vp), Pid);
602: return 0;
603: }
604: fsp->fsid = xfs_node.attr.va_fsid;
605: fsp->fileid = (long)xfs_node.attr.va_fileid;
606: fsp->mode = xfs_node.attr.va_mode;
607: fsp->size = xfs_node.attr.va_size;
608: fsp->rdev = xfs_node.attr.va_rdev;
1.23 deraadt 609:
610: return 1;
611: }
612:
613: int
614: null_filestat(vp, fsp)
615: struct vnode *vp;
616: struct filestat *fsp;
617: {
618: struct null_node node;
619: struct filestat fst;
620: struct vnode vn;
621: int fail = 1;
622:
623: memset(&fst, 0, sizeof fst);
624:
625: if (!KVM_READ(VTONULL(vp), &node, sizeof (node))) {
626: dprintf("can't read node at %p for pid %d", VTONULL(vp), Pid);
627: return 0;
628: }
629:
630: /*
631: * Attempt to find information that might be useful.
632: */
633: if (node.null_lowervp) {
634: if (!KVM_READ(node.null_lowervp, &vn, sizeof (vn))) {
635: dprintf("can't read vnode at %p for pid %d",
636: node.null_lowervp, Pid);
637: return 0;
638: }
639:
640: fail = 0;
641: if (vn.v_type == VNON || vn.v_tag == VT_NON)
642: fail = 1;
643: else if (vn.v_type == VBAD)
644: fail = 1;
645: else
646: switch (vn.v_tag) {
647: case VT_UFS:
648: case VT_MFS:
649: if (!ufs_filestat(&vn, &fst))
650: fail = 1;
651: break;
652: case VT_NFS:
653: if (!nfs_filestat(&vn, &fst))
654: fail = 1;
655: break;
656: case VT_EXT2FS:
657: if (!ext2fs_filestat(&vn, &fst))
658: fail = 1;
659: break;
660: case VT_ISOFS:
661: if (!isofs_filestat(&vn, &fst))
662: fail = 1;
663: break;
664: case VT_MSDOSFS:
665: if (!msdos_filestat(&vn, &fst))
666: fail = 1;
667: break;
668: case VT_XFS:
669: if (!xfs_filestat(&vn, &fst))
670: fail = 1;
671: break;
672: default:
673: break;
674: }
675: }
676:
677: fsp->fsid = (long)node.null_vnode;
678: if (fail)
679: fsp->fileid = (long)node.null_lowervp;
680: else
681: fsp->fileid = fst.fileid;
682: fsp->mode = fst.mode;
683: fsp->size = fst.mode;
684: fsp->rdev = fst.mode;
1.18 art 685:
686: return 1;
687: }
1.1 deraadt 688:
689: char *
690: getmnton(m)
691: struct mount *m;
692: {
693: static struct mount mount;
694: static struct mtab {
695: struct mtab *next;
696: struct mount *m;
697: char mntonname[MNAMELEN];
698: } *mhead = NULL;
699: register struct mtab *mt;
700:
701: for (mt = mhead; mt != NULL; mt = mt->next)
702: if (m == mt->m)
703: return (mt->mntonname);
704: if (!KVM_READ(m, &mount, sizeof(struct mount))) {
1.17 mickey 705: warn("can't read mount table at %p", m);
1.1 deraadt 706: return (NULL);
707: }
1.17 mickey 708: if ((mt = malloc(sizeof (struct mtab))) == NULL)
709: err(1, "malloc");
1.1 deraadt 710: mt->m = m;
711: bcopy(&mount.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN);
712: mt->next = mhead;
713: mhead = mt;
714: return (mt->mntonname);
1.22 art 715: }
716:
717: void
718: pipetrans(pipe, i)
719: struct pipe *pipe;
720: int i;
721: {
722: struct pipe pi;
723: void *maxaddr;
724:
725: PREFIX(i);
726:
727: printf(" ");
728:
729: /* fill in socket */
730: if (!KVM_READ(pipe, &pi, sizeof(struct pipe))) {
731: dprintf("can't read pipe at %p", pipe);
732: goto bad;
733: }
734:
735: /*
736: * We don't have enough space to fit both peer and own address, so
737: * we select the higher address so both ends of the pipe have the
738: * same visible addr. (it's the higher address because when the other
739: * end closes, it becomes 0)
740: */
741: maxaddr = MAX(pipe, pi.pipe_peer);
742:
743: printf("pipe %p state: %s%s%s", maxaddr,
744: (pi.pipe_state & PIPE_WANTR) ? "R" : "",
745: (pi.pipe_state & PIPE_WANTW) ? "W" : "",
746: (pi.pipe_state & PIPE_EOF) ? "E" : "");
747:
748: printf("\n");
749: return;
750: bad:
751: printf("* error\n");
1.1 deraadt 752: }
753:
754: void
755: socktrans(sock, i)
756: struct socket *sock;
757: int i;
758: {
759: static char *stypename[] = {
760: "unused", /* 0 */
761: "stream", /* 1 */
762: "dgram", /* 2 */
763: "raw", /* 3 */
764: "rdm", /* 4 */
765: "seqpak" /* 5 */
766: };
767: #define STYPEMAX 5
768: struct socket so;
769: struct protosw proto;
770: struct domain dom;
771: struct inpcb inpcb;
772: struct unpcb unpcb;
773: int len;
1.4 deraadt 774: char dname[32];
1.1 deraadt 775:
776: PREFIX(i);
777:
778: /* fill in socket */
779: if (!KVM_READ(sock, &so, sizeof(struct socket))) {
1.17 mickey 780: dprintf("can't read sock at %p", sock);
1.1 deraadt 781: goto bad;
782: }
783:
784: /* fill in protosw entry */
785: if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
1.17 mickey 786: dprintf("can't read protosw at %p", so.so_proto);
1.1 deraadt 787: goto bad;
788: }
789:
790: /* fill in domain */
791: if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
1.17 mickey 792: dprintf("can't read domain at %p", proto.pr_domain);
1.1 deraadt 793: goto bad;
794: }
795:
796: if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
1.15 deraadt 797: sizeof(dname) - 1)) != sizeof(dname) -1) {
1.17 mickey 798: dprintf("can't read domain name at %p", dom.dom_name);
1.1 deraadt 799: dname[0] = '\0';
800: }
801: else
802: dname[len] = '\0';
803:
804: if ((u_short)so.so_type > STYPEMAX)
805: printf("* %s ?%d", dname, so.so_type);
806: else
807: printf("* %s %s", dname, stypename[so.so_type]);
808:
809: /*
810: * protocol specific formatting
811: *
812: * Try to find interesting things to print. For tcp, the interesting
813: * thing is the address of the tcpcb, for udp and others, just the
814: * inpcb (socket pcb). For unix domain, its the address of the socket
815: * pcb and the address of the connected pcb (if connected). Otherwise
816: * just print the protocol number and address of the socket itself.
817: * The idea is not to duplicate netstat, but to make available enough
818: * information for further analysis.
819: */
820: switch(dom.dom_family) {
821: case AF_INET:
822: getinetproto(proto.pr_protocol);
1.5 deraadt 823: if (proto.pr_protocol == IPPROTO_TCP) {
824: if (so.so_pcb == NULL)
825: break;
826: if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
827: sizeof(struct inpcb)) != sizeof(struct inpcb)) {
1.17 mickey 828: dprintf("can't read inpcb at %p", so.so_pcb);
1.5 deraadt 829: goto bad;
830: }
1.11 millert 831: printf(" %p", inpcb.inp_ppcb);
1.5 deraadt 832: printf(" %s:%d",
833: inpcb.inp_laddr.s_addr == INADDR_ANY ? "*" :
834: inet_ntoa(inpcb.inp_laddr),
835: ntohs(inpcb.inp_lport));
1.17 mickey 836: if (inpcb.inp_fport) {
1.13 deraadt 837: if (so.so_state & SS_CONNECTOUT)
838: printf(" --> ");
839: else
840: printf(" <-- ");
841: printf("%s:%d",
1.5 deraadt 842: inpcb.inp_faddr.s_addr == INADDR_ANY ? "*" :
843: inet_ntoa(inpcb.inp_faddr),
844: ntohs(inpcb.inp_fport));
1.17 mickey 845: }
1.5 deraadt 846: } else if (proto.pr_protocol == IPPROTO_UDP) {
847: if (so.so_pcb == NULL)
848: break;
849: if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
850: sizeof(struct inpcb)) != sizeof(struct inpcb)) {
1.17 mickey 851: dprintf("can't read inpcb at %p", so.so_pcb);
1.5 deraadt 852: goto bad;
1.1 deraadt 853: }
1.5 deraadt 854: printf(" %s:%d",
855: inpcb.inp_laddr.s_addr == INADDR_ANY ? "*" :
856: inet_ntoa(inpcb.inp_laddr),
857: ntohs(inpcb.inp_lport));
858: if (inpcb.inp_fport)
1.6 deraadt 859: printf(" <-> %s:%d",
1.5 deraadt 860: inpcb.inp_faddr.s_addr == INADDR_ANY ? "*" :
861: inet_ntoa(inpcb.inp_faddr),
862: ntohs(inpcb.inp_fport));
863: } else if (so.so_pcb)
1.11 millert 864: printf(" %p", so.so_pcb);
1.1 deraadt 865: break;
866: case AF_UNIX:
867: /* print address of pcb and connected pcb */
868: if (so.so_pcb) {
1.11 millert 869: printf(" %p", so.so_pcb);
1.1 deraadt 870: if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb,
871: sizeof(struct unpcb)) != sizeof(struct unpcb)){
1.17 mickey 872: dprintf("can't read unpcb at %p", so.so_pcb);
1.1 deraadt 873: goto bad;
874: }
875: if (unpcb.unp_conn) {
876: char shoconn[4], *cp;
877:
878: cp = shoconn;
879: if (!(so.so_state & SS_CANTRCVMORE))
880: *cp++ = '<';
881: *cp++ = '-';
882: if (!(so.so_state & SS_CANTSENDMORE))
883: *cp++ = '>';
884: *cp = '\0';
1.11 millert 885: printf(" %s %p", shoconn,
886: unpcb.unp_conn);
1.1 deraadt 887: }
888: }
889: break;
890: default:
891: /* print protocol number and socket address */
1.11 millert 892: printf(" %d %p", proto.pr_protocol, sock);
1.1 deraadt 893: }
894: printf("\n");
895: return;
896: bad:
897: printf("* error\n");
898: }
899:
900: /*
901: * getinetproto --
902: * print name of protocol number
903: */
904: void
905: getinetproto(number)
906: int number;
907: {
1.24 ! deraadt 908: static int isopen;
! 909: register struct protoent *pe;
1.1 deraadt 910:
1.24 ! deraadt 911: if (!isopen)
! 912: setprotoent(++isopen);
! 913: if ((pe = getprotobynumber(number)) != NULL)
! 914: printf(" %s", pe->p_name);
! 915: else
1.1 deraadt 916: printf(" %d", number);
917: }
918:
1.11 millert 919: int
1.1 deraadt 920: getfname(filename)
921: char *filename;
922: {
923: struct stat statbuf;
924: DEVS *cur;
925:
926: if (stat(filename, &statbuf)) {
1.17 mickey 927: warn(filename);
1.1 deraadt 928: return(0);
929: }
1.17 mickey 930: if ((cur = malloc(sizeof(DEVS))) == NULL)
931: err(1, "malloc");
1.1 deraadt 932: cur->next = devs;
933: devs = cur;
934:
935: cur->ino = statbuf.st_ino;
936: cur->fsid = statbuf.st_dev & 0xffff;
937: cur->name = filename;
938: return(1);
939: }
940:
941: void
942: usage()
943: {
944: (void)fprintf(stderr,
945: "usage: fstat [-fnv] [-p pid] [-u user] [-N system] [-M core] [file ...]\n");
946: exit(1);
947: }