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