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