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