fstat.c revision 1.72 1 /* $NetBSD: fstat.c,v 1.72 2005/07/17 07:36:26 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.72 2005/07/17 07:36:26 christos Exp $");
43 #endif
44 #endif /* not lint */
45
46 #include <sys/param.h>
47 #include <sys/time.h>
48 #include <sys/proc.h>
49 #include <sys/user.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 struct file **ofiles; /* buffer of pointers to file structures */
133 static int maxfiles;
134 #define ALLOC_OFILES(d) \
135 if ((d) > maxfiles) { \
136 free(ofiles); \
137 ofiles = malloc((d) * sizeof(struct file *)); \
138 if (ofiles == NULL) { \
139 err(1, "malloc(%u)", (d) * \
140 (unsigned int)sizeof(struct file *)); \
141 } \
142 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) __attribute__((__noreturn__));
162 static const char *vfilestat(struct vnode *, struct filestat *);
163 static void vtrans(struct vnode *, int, int);
164 static void ftrans(struct file *, 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 filedesc0 filed0;
311 #define filed filed0.fd_fd
312 struct cwdinfo cwdi;
313
314 Uname = user_from_uid(p->p_uid, 0);
315 Pid = p->p_pid;
316 Comm = p->p_comm;
317
318 if (p->p_fd == 0 || p->p_cwdi == 0)
319 return;
320 if (!KVM_READ(p->p_fd, &filed0, sizeof (filed0))) {
321 warnx("can't read filedesc at %#llx for pid %d", (unsigned long long)p->p_fd, Pid);
322 return;
323 }
324 if (!KVM_READ(p->p_cwdi, &cwdi, sizeof(cwdi))) {
325 warnx("can't read cwdinfo at %#llx for pid %d", (unsigned long long)p->p_cwdi, Pid);
326 return;
327 }
328 if (filed.fd_nfiles < 0 || filed.fd_lastfile >= filed.fd_nfiles ||
329 filed.fd_freefile > filed.fd_lastfile + 1) {
330 dprintf("filedesc corrupted at %#llx for pid %d", (unsigned long long)p->p_fd, Pid);
331 return;
332 }
333 /*
334 * root directory vnode, if one
335 */
336 if (cwdi.cwdi_rdir)
337 vtrans(cwdi.cwdi_rdir, RDIR, FREAD);
338 /*
339 * current working directory vnode
340 */
341 vtrans(cwdi.cwdi_cdir, CDIR, FREAD);
342 /*
343 * ktrace vnode, if one
344 */
345 if (p->p_tracep)
346 ftrans((struct file *)(intptr_t)p->p_tracep, TRACE);
347 /*
348 * open files
349 */
350 #define FPSIZE (sizeof (struct file *))
351 ALLOC_OFILES(filed.fd_lastfile+1);
352 if (filed.fd_nfiles > NDFILE) {
353 if (!KVM_READ(filed.fd_ofiles, ofiles,
354 (filed.fd_lastfile+1) * FPSIZE)) {
355 dprintf("can't read file structures at %p for pid %d",
356 filed.fd_ofiles, Pid);
357 return;
358 }
359 } else
360 (void)memmove(ofiles, filed0.fd_dfiles,
361 (filed.fd_lastfile+1) * FPSIZE);
362 for (i = 0; i <= filed.fd_lastfile; i++) {
363 if (ofiles[i] == NULL)
364 continue;
365 ftrans(ofiles[i], i);
366 }
367 }
368
369 static void
370 ftrans(struct file *fp, int i)
371 {
372 struct file file;
373
374 if (!KVM_READ(fp, &file, sizeof (struct file))) {
375 dprintf("can't read file %d at %p for pid %d",
376 i, fp, Pid);
377 return;
378 }
379 switch (file.f_type) {
380 case DTYPE_VNODE:
381 vtrans((struct vnode *)file.f_data, i, file.f_flag);
382 break;
383 case DTYPE_SOCKET:
384 if (checkfile == 0)
385 socktrans((struct socket *)file.f_data, i);
386 break;
387 case DTYPE_PIPE:
388 if (checkfile == 0)
389 ptrans(&file, (struct pipe *)file.f_data, i);
390 break;
391 case DTYPE_KQUEUE:
392 if (checkfile == 0)
393 kqueuetrans((void *)file.f_data, i);
394 break;
395 default:
396 dprintf("unknown file type %d for file %d of pid %d",
397 file.f_type, i, Pid);
398 break;
399 }
400 }
401
402 static const char *
403 vfilestat(struct vnode *vp, struct filestat *fsp)
404 {
405 const char *badtype = NULL;
406
407 if (vp->v_type == VNON || vp->v_tag == VT_NON)
408 badtype = "none";
409 else if (vp->v_type == VBAD)
410 badtype = "bad";
411 else
412 switch (vp->v_tag) {
413 case VT_UFS:
414 case VT_LFS:
415 case VT_MFS:
416 if (!ufs_filestat(vp, fsp))
417 badtype = "error";
418 break;
419 case VT_MSDOSFS:
420 if (!msdosfs_filestat(vp, fsp))
421 badtype = "error";
422 break;
423 case VT_NFS:
424 if (!nfs_filestat(vp, fsp))
425 badtype = "error";
426 break;
427 case VT_EXT2FS:
428 if (!ext2fs_filestat(vp, fsp))
429 badtype = "error";
430 break;
431 case VT_ISOFS:
432 if (!isofs_filestat(vp, fsp))
433 badtype = "error";
434 break;
435 case VT_NTFS:
436 if (!ntfs_filestat(vp, fsp))
437 badtype = "error";
438 break;
439 case VT_PTYFS:
440 if (!ptyfs_filestat(vp, fsp))
441 badtype = "error";
442 break;
443 case VT_NULL:
444 case VT_OVERLAY:
445 case VT_UMAP:
446 badtype = layer_filestat(vp, fsp);
447 break;
448 default: {
449 static char unknown[10];
450 (void)snprintf(unknown, sizeof unknown,
451 "?(%x)", vp->v_tag);
452 badtype = unknown;
453 break;
454 }
455 }
456 return (badtype);
457 }
458
459 static void
460 vtrans(struct vnode *vp, int i, int flag)
461 {
462 struct vnode vn;
463 struct filestat fst;
464 char mode[15], rw[3];
465 const char *badtype, *filename;
466
467 filename = NULL;
468 if (!KVM_READ(vp, &vn, sizeof(struct vnode))) {
469 dprintf("can't read vnode at %p for pid %d", vp, Pid);
470 return;
471 }
472 badtype = vfilestat(&vn, &fst);
473 if (checkfile) {
474 int fsmatch = 0;
475 DEVS *d;
476
477 if (badtype)
478 return;
479 for (d = devs; d != NULL; d = d->next)
480 if (d->fsid == fst.fsid) {
481 fsmatch = 1;
482 if (d->ino == fst.fileid) {
483 filename = d->name;
484 break;
485 }
486 }
487 if (fsmatch == 0 || (filename == NULL && fsflg == 0))
488 return;
489 }
490 PREFIX(i);
491 if (badtype) {
492 (void)printf(" - - %10s -\n", badtype);
493 return;
494 }
495 if (nflg)
496 (void)printf(" %2d,%-2d", major(fst.fsid), minor(fst.fsid));
497 else
498 (void)printf(" %-8s", getmnton(vn.v_mount));
499 if (nflg)
500 (void)snprintf(mode, sizeof mode, "%o", fst.mode);
501 else
502 strmode(fst.mode, mode);
503 (void)printf(" %7lu %*s", (unsigned long)fst.fileid, nflg ? 5 : 10, mode);
504 switch (vn.v_type) {
505 case VBLK:
506 case VCHR: {
507 char *name;
508
509 if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ?
510 S_IFCHR : S_IFBLK)) == NULL))
511 (void)printf(" %2d,%-2d", major(fst.rdev),
512 minor(fst.rdev));
513 else
514 (void)printf(" %6s", name);
515 break;
516 }
517 default:
518 (void)printf(" %6lld", (long long)fst.size);
519 }
520 rw[0] = '\0';
521 if (flag & FREAD)
522 (void)strlcat(rw, "r", sizeof(rw));
523 if (flag & FWRITE)
524 (void)strlcat(rw, "w", sizeof(rw));
525 (void)printf(" %-2s", rw);
526 if (filename && !fsflg)
527 (void)printf(" %s", filename);
528 (void)putchar('\n');
529 }
530
531 static int
532 ufs_filestat(struct vnode *vp, struct filestat *fsp)
533 {
534 struct inode inode;
535 union dinode {
536 struct ufs1_dinode dp1;
537 struct ufs2_dinode dp2;
538 } dip;
539
540 if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
541 dprintf("can't read inode at %p for pid %d", VTOI(vp), Pid);
542 return 0;
543 }
544
545 if (!KVM_READ(inode.i_din.ffs1_din, &dip, sizeof(struct ufs1_dinode))) {
546 dprintf("can't read dinode at %p for pid %d",
547 inode.i_din.ffs1_din, Pid);
548 return 0;
549 }
550 if (inode.i_size == dip.dp1.di_size)
551 fsp->rdev = dip.dp1.di_rdev;
552 else {
553 if (!KVM_READ(inode.i_din.ffs1_din, &dip,
554 sizeof(struct ufs2_dinode))) {
555 dprintf("can't read dinode at %p for pid %d",
556 inode.i_din.ffs1_din, Pid);
557 return 0;
558 }
559 fsp->rdev = dip.dp2.di_rdev;
560 }
561
562 fsp->fsid = inode.i_dev & 0xffff;
563 fsp->fileid = (long)inode.i_number;
564 fsp->mode = (mode_t)inode.i_mode;
565 fsp->size = inode.i_size;
566
567 return 1;
568 }
569
570 static int
571 ext2fs_filestat(struct vnode *vp, struct filestat *fsp)
572 {
573 struct inode inode;
574 u_int16_t mode;
575 u_int32_t size;
576
577 if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
578 dprintf("can't read inode at %p for pid %d", VTOI(vp), Pid);
579 return 0;
580 }
581 fsp->fsid = inode.i_dev & 0xffff;
582 fsp->fileid = (long)inode.i_number;
583
584 if (!KVM_READ(&inode.i_e2fs_mode, &mode, sizeof mode)) {
585 dprintf("can't read inode %p's mode at %p for pid %d", VTOI(vp),
586 &inode.i_e2fs_mode, Pid);
587 return 0;
588 }
589 fsp->mode = mode;
590
591 if (!KVM_READ(&inode.i_e2fs_size, &size, sizeof size)) {
592 dprintf("can't read inode %p's size at %p for pid %d", VTOI(vp),
593 &inode.i_e2fs_size, Pid);
594 return 0;
595 }
596 fsp->size = size;
597 fsp->rdev = 0; /* XXX */
598 return 1;
599 }
600
601 static int
602 nfs_filestat(struct vnode *vp, struct filestat *fsp)
603 {
604 struct nfsnode nfsnode;
605 struct vattr va;
606
607 if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
608 dprintf("can't read nfsnode at %p for pid %d", VTONFS(vp),
609 Pid);
610 return 0;
611 }
612 if (!KVM_READ(nfsnode.n_vattr, &va, sizeof(va))) {
613 dprintf("can't read vnode attributes at %p for pid %d",
614 nfsnode.n_vattr, Pid);
615 return 0;
616 }
617 fsp->fsid = va.va_fsid;
618 fsp->fileid = va.va_fileid;
619 fsp->size = nfsnode.n_size;
620 fsp->rdev = va.va_rdev;
621 fsp->mode = (mode_t)va.va_mode | getftype(vp->v_type);
622
623 return 1;
624 }
625
626 static int
627 msdosfs_filestat(struct vnode *vp, struct filestat *fsp)
628 {
629 struct denode de;
630 struct msdosfsmount mp;
631
632 if (!KVM_READ(VTONFS(vp), &de, sizeof(de))) {
633 dprintf("can't read denode at %p for pid %d", VTONFS(vp),
634 Pid);
635 return 0;
636 }
637 if (!KVM_READ(de.de_pmp, &mp, sizeof(mp))) {
638 dprintf("can't read mount struct at %p for pid %d", de.de_pmp,
639 Pid);
640 return 0;
641 }
642
643 fsp->fsid = de.de_dev & 0xffff;
644 fsp->fileid = 0; /* XXX see msdosfs_vptofh() for more info */
645 fsp->size = de.de_FileSize;
646 fsp->rdev = 0; /* msdosfs doesn't support device files */
647 fsp->mode = (0777 & mp.pm_mask) | getftype(vp->v_type);
648 return 1;
649 }
650
651 static const char *
652 layer_filestat(struct vnode *vp, struct filestat *fsp)
653 {
654 struct layer_node layer_node;
655 struct mount mount;
656 struct vnode vn;
657 const char *badtype;
658
659 if (!KVM_READ(VTOLAYER(vp), &layer_node, sizeof(layer_node))) {
660 dprintf("can't read layer_node at %p for pid %d",
661 VTOLAYER(vp), Pid);
662 return ("error");
663 }
664 if (!KVM_READ(vp->v_mount, &mount, sizeof(struct mount))) {
665 dprintf("can't read mount struct at %p for pid %d",
666 vp->v_mount, Pid);
667 return ("error");
668 }
669 vp = layer_node.layer_lowervp;
670 if (!KVM_READ(vp, &vn, sizeof(struct vnode))) {
671 dprintf("can't read vnode at %p for pid %d", vp, Pid);
672 return ("error");
673 }
674 if ((badtype = vfilestat(&vn, fsp)) == NULL)
675 fsp->fsid = mount.mnt_stat.f_fsidx.__fsid_val[0];
676 return (badtype);
677 }
678
679 static char *
680 getmnton(struct mount *m)
681 {
682 static struct mount mount;
683 static struct mtab {
684 struct mtab *next;
685 struct mount *m;
686 char mntonname[MNAMELEN];
687 } *mhead = NULL;
688 struct mtab *mt;
689
690 for (mt = mhead; mt != NULL; mt = mt->next)
691 if (m == mt->m)
692 return (mt->mntonname);
693 if (!KVM_READ(m, &mount, sizeof(struct mount))) {
694 warnx("can't read mount table at %p", m);
695 return (NULL);
696 }
697 if ((mt = malloc(sizeof (struct mtab))) == NULL) {
698 err(1, "malloc(%u)", (unsigned int)sizeof(struct mtab));
699 }
700 mt->m = m;
701 (void)memmove(&mt->mntonname[0], &mount.mnt_stat.f_mntonname[0],
702 MNAMELEN);
703 mt->next = mhead;
704 mhead = mt;
705 return (mt->mntonname);
706 }
707
708 #ifdef INET6
709 static const char *
710 inet6_addrstr(struct in6_addr *p)
711 {
712 struct sockaddr_in6 sin6;
713 static char hbuf[NI_MAXHOST];
714 const int niflags = NI_NUMERICHOST;
715
716 (void)memset(&sin6, 0, sizeof(sin6));
717 sin6.sin6_family = AF_INET6;
718 sin6.sin6_len = sizeof(struct sockaddr_in6);
719 sin6.sin6_addr = *p;
720 if (IN6_IS_ADDR_LINKLOCAL(p) &&
721 *(u_int16_t *)&sin6.sin6_addr.s6_addr[2] != 0) {
722 sin6.sin6_scope_id =
723 ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);
724 sin6.sin6_addr.s6_addr[2] = sin6.sin6_addr.s6_addr[3] = 0;
725 }
726
727 if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
728 hbuf, sizeof(hbuf), NULL, 0, niflags))
729 return "invalid";
730
731 return hbuf;
732 }
733 #endif
734
735 static void
736 socktrans(struct socket *sock, int i)
737 {
738 static const char *stypename[] = {
739 "unused", /* 0 */
740 "stream", /* 1 */
741 "dgram", /* 2 */
742 "raw", /* 3 */
743 "rdm", /* 4 */
744 "seqpak" /* 5 */
745 };
746 #define STYPEMAX 5
747 struct socket so;
748 struct protosw proto;
749 struct domain dom;
750 struct inpcb inpcb;
751 #ifdef INET6
752 struct in6pcb in6pcb;
753 #endif
754 struct unpcb unpcb;
755 int len;
756 char dname[32];
757 #ifdef INET6
758 char xaddrbuf[NI_MAXHOST + 2];
759 #endif
760
761 PREFIX(i);
762
763 /* fill in socket */
764 if (!KVM_READ(sock, &so, sizeof(struct socket))) {
765 dprintf("can't read sock at %p", sock);
766 goto bad;
767 }
768
769 /* fill in protosw entry */
770 if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
771 dprintf("can't read protosw at %p", so.so_proto);
772 goto bad;
773 }
774
775 /* fill in domain */
776 if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
777 dprintf("can't read domain at %p", proto.pr_domain);
778 goto bad;
779 }
780
781 if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
782 sizeof(dname) - 1)) != sizeof(dname) -1) {
783 dprintf("can't read domain name at %p", dom.dom_name);
784 dname[0] = '\0';
785 }
786 else
787 dname[len] = '\0';
788
789 if ((u_short)so.so_type > STYPEMAX)
790 (void)printf("* %s ?%d", dname, so.so_type);
791 else
792 (void)printf("* %s %s", dname, stypename[so.so_type]);
793
794 /*
795 * protocol specific formatting
796 *
797 * Try to find interesting things to print. For TCP, the interesting
798 * thing is the address of the tcpcb, for UDP and others, just the
799 * inpcb (socket pcb). For UNIX domain, its the address of the socket
800 * pcb and the address of the connected pcb (if connected). Otherwise
801 * just print the protocol number and address of the socket itself.
802 * The idea is not to duplicate netstat, but to make available enough
803 * information for further analysis.
804 */
805 switch(dom.dom_family) {
806 case AF_INET:
807 getinetproto(proto.pr_protocol);
808 if (proto.pr_protocol == IPPROTO_TCP) {
809 if (so.so_pcb == NULL)
810 break;
811 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
812 sizeof(struct inpcb)) != sizeof(struct inpcb)) {
813 dprintf("can't read inpcb at %p", so.so_pcb);
814 goto bad;
815 }
816 (void)printf(" %lx", (long)inpcb.inp_ppcb);
817 (void)printf(" %s:%d",
818 inpcb.inp_laddr.s_addr == INADDR_ANY ? "*" :
819 inet_ntoa(inpcb.inp_laddr), ntohs(inpcb.inp_lport));
820 if (inpcb.inp_fport) {
821 (void)printf(" <-> %s:%d",
822 inpcb.inp_faddr.s_addr == INADDR_ANY ? "*" :
823 inet_ntoa(inpcb.inp_faddr),
824 ntohs(inpcb.inp_fport));
825 }
826 } else if (proto.pr_protocol == IPPROTO_UDP) {
827 if (so.so_pcb == NULL)
828 break;
829 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
830 sizeof(struct inpcb)) != sizeof(struct inpcb)) {
831 dprintf("can't read inpcb at %p", so.so_pcb);
832 goto bad;
833 }
834 (void)printf(" %lx", (long)so.so_pcb);
835 (void)printf(" %s:%d",
836 inpcb.inp_laddr.s_addr == INADDR_ANY ? "*" :
837 inet_ntoa(inpcb.inp_laddr), ntohs(inpcb.inp_lport));
838 if (inpcb.inp_fport)
839 (void)printf(" <-> %s:%d",
840 inpcb.inp_faddr.s_addr == INADDR_ANY ? "*" :
841 inet_ntoa(inpcb.inp_faddr),
842 ntohs(inpcb.inp_fport));
843 } else if (so.so_pcb)
844 (void)printf(" %lx", (long)so.so_pcb);
845 break;
846 #ifdef INET6
847 case AF_INET6:
848 getinetproto(proto.pr_protocol);
849 if (proto.pr_protocol == IPPROTO_TCP) {
850 if (so.so_pcb == NULL)
851 break;
852 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&in6pcb,
853 sizeof(struct in6pcb)) != sizeof(struct in6pcb)) {
854 dprintf("can't read in6pcb at %p", so.so_pcb);
855 goto bad;
856 }
857 (void)printf(" %lx", (long)in6pcb.in6p_ppcb);
858 (void)snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]",
859 inet6_addrstr(&in6pcb.in6p_laddr));
860 (void)printf(" %s:%d",
861 IN6_IS_ADDR_UNSPECIFIED(&in6pcb.in6p_laddr) ? "*" :
862 xaddrbuf,
863 ntohs(in6pcb.in6p_lport));
864 if (in6pcb.in6p_fport) {
865 (void)snprintf(xaddrbuf, sizeof(xaddrbuf),
866 "[%s]", inet6_addrstr(&in6pcb.in6p_faddr));
867 (void)printf(" <-> %s:%d",
868 IN6_IS_ADDR_UNSPECIFIED(&in6pcb.in6p_faddr) ? "*" :
869 xaddrbuf,
870 ntohs(in6pcb.in6p_fport));
871 }
872 } else if (proto.pr_protocol == IPPROTO_UDP) {
873 if (so.so_pcb == NULL)
874 break;
875 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&in6pcb,
876 sizeof(struct in6pcb)) != sizeof(struct in6pcb)) {
877 dprintf("can't read inpcb at %p", so.so_pcb);
878 goto bad;
879 }
880 (void)printf(" %lx", (long)so.so_pcb);
881 (void)snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]",
882 inet6_addrstr(&in6pcb.in6p_laddr));
883 (void)printf(" %s:%d",
884 IN6_IS_ADDR_UNSPECIFIED(&in6pcb.in6p_laddr) ? "*" :
885 xaddrbuf,
886 ntohs(in6pcb.in6p_lport));
887 if (in6pcb.in6p_fport) {
888 (void)snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]",
889 inet6_addrstr(&in6pcb.in6p_faddr));
890 (void)printf(" <-> %s:%d",
891 IN6_IS_ADDR_UNSPECIFIED(&in6pcb.in6p_faddr) ? "*" :
892 xaddrbuf,
893 ntohs(in6pcb.in6p_fport));
894 }
895 } else if (so.so_pcb)
896 (void)printf(" %lx", (long)so.so_pcb);
897 break;
898 #endif
899 case AF_LOCAL:
900 /* print address of pcb and connected pcb */
901 if (so.so_pcb) {
902 (void)printf(" %lx", (long)so.so_pcb);
903 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb,
904 sizeof(struct unpcb)) != sizeof(struct unpcb)){
905 dprintf("can't read unpcb at %p", so.so_pcb);
906 goto bad;
907 }
908 if (unpcb.unp_conn) {
909 char shoconn[4], *cp;
910
911 cp = shoconn;
912 if (!(so.so_state & SS_CANTRCVMORE))
913 *cp++ = '<';
914 *cp++ = '-';
915 if (!(so.so_state & SS_CANTSENDMORE))
916 *cp++ = '>';
917 *cp = '\0';
918 (void)printf(" %s %lx", shoconn,
919 (long)unpcb.unp_conn);
920 }
921 }
922 break;
923 default:
924 /* print protocol number and socket address */
925 (void)printf(" %d %lx", proto.pr_protocol, (long)sock);
926 }
927 (void)printf("\n");
928 return;
929 bad:
930 (void)printf("* error\n");
931 }
932
933 static void
934 ptrans(struct file *fp, struct pipe *cpipe, int i)
935 {
936 struct pipe cp;
937
938 PREFIX(i);
939
940 /* fill in pipe */
941 if (!KVM_READ(cpipe, &cp, sizeof(struct pipe))) {
942 dprintf("can't read pipe at %p", cpipe);
943 goto bad;
944 }
945
946 /* pipe descriptor is either read or write, never both */
947 (void)printf("* pipe %p %s %p %s%s%s", cpipe,
948 (fp->f_flag & FWRITE) ? "->" : "<-",
949 cp.pipe_peer,
950 (fp->f_flag & FWRITE) ? "w" : "r",
951 (fp->f_flag & FNONBLOCK) ? "n" : "",
952 (cp.pipe_state & PIPE_ASYNC) ? "a" : "");
953 (void)printf("\n");
954 return;
955 bad:
956 (void)printf("* error\n");
957 }
958
959 static void
960 kqueuetrans(void *kq, int i)
961 {
962
963 PREFIX(i);
964 (void)printf("* kqueue %lx", (long)kq);
965 (void)printf("\n");
966 }
967
968 /*
969 * getinetproto --
970 * print name of protocol number
971 */
972 static void
973 getinetproto(int number)
974 {
975 const char *cp;
976
977 switch (number) {
978 case IPPROTO_IP:
979 cp = "ip"; break;
980 case IPPROTO_ICMP:
981 cp ="icmp"; break;
982 case IPPROTO_GGP:
983 cp ="ggp"; break;
984 case IPPROTO_TCP:
985 cp ="tcp"; break;
986 case IPPROTO_EGP:
987 cp ="egp"; break;
988 case IPPROTO_PUP:
989 cp ="pup"; break;
990 case IPPROTO_UDP:
991 cp ="udp"; break;
992 case IPPROTO_IDP:
993 cp ="idp"; break;
994 case IPPROTO_RAW:
995 cp ="raw"; break;
996 case IPPROTO_ICMPV6:
997 cp ="icmp6"; break;
998 default:
999 (void)printf(" %d", number);
1000 return;
1001 }
1002 (void)printf(" %s", cp);
1003 }
1004
1005 static int
1006 getfname(const char *filename)
1007 {
1008 struct stat statbuf;
1009 DEVS *cur;
1010
1011 if (stat(filename, &statbuf)) {
1012 warn("stat(%s)", filename);
1013 return 0;
1014 }
1015 if ((cur = malloc(sizeof(DEVS))) == NULL) {
1016 err(1, "malloc(%u)", (unsigned int)sizeof(DEVS));
1017 }
1018 cur->next = devs;
1019 devs = cur;
1020
1021 cur->ino = statbuf.st_ino;
1022 cur->fsid = statbuf.st_dev & 0xffff;
1023 cur->name = filename;
1024 return 1;
1025 }
1026
1027 mode_t
1028 getftype(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 static void
1063 usage(void)
1064 {
1065 (void)fprintf(stderr, "Usage: %s [-fnv] [-p pid] [-u user] "
1066 "[-N system] [-M core] [file ...]\n", getprogname());
1067 exit(1);
1068 }
1069