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