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