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