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