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