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