fstat.c revision 1.73 1 /* $NetBSD: fstat.c,v 1.73 2006/02/10 16:01:45 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.73 2006/02/10 16:01:45 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_TMPFS:
444 if (!tmpfs_filestat(vp, fsp))
445 badtype = "error";
446 break;
447 case VT_NULL:
448 case VT_OVERLAY:
449 case VT_UMAP:
450 badtype = layer_filestat(vp, fsp);
451 break;
452 default: {
453 static char unknown[10];
454 (void)snprintf(unknown, sizeof unknown,
455 "?(%x)", vp->v_tag);
456 badtype = unknown;
457 break;
458 }
459 }
460 return (badtype);
461 }
462
463 static void
464 vtrans(struct vnode *vp, int i, int flag)
465 {
466 struct vnode vn;
467 struct filestat fst;
468 char mode[15], rw[3];
469 const char *badtype, *filename;
470
471 filename = NULL;
472 if (!KVM_READ(vp, &vn, sizeof(struct vnode))) {
473 dprintf("can't read vnode at %p for pid %d", vp, Pid);
474 return;
475 }
476 badtype = vfilestat(&vn, &fst);
477 if (checkfile) {
478 int fsmatch = 0;
479 DEVS *d;
480
481 if (badtype)
482 return;
483 for (d = devs; d != NULL; d = d->next)
484 if (d->fsid == fst.fsid) {
485 fsmatch = 1;
486 if (d->ino == fst.fileid) {
487 filename = d->name;
488 break;
489 }
490 }
491 if (fsmatch == 0 || (filename == NULL && fsflg == 0))
492 return;
493 }
494 PREFIX(i);
495 if (badtype) {
496 (void)printf(" - - %10s -\n", badtype);
497 return;
498 }
499 if (nflg)
500 (void)printf(" %2d,%-2d", major(fst.fsid), minor(fst.fsid));
501 else
502 (void)printf(" %-8s", getmnton(vn.v_mount));
503 if (nflg)
504 (void)snprintf(mode, sizeof mode, "%o", fst.mode);
505 else
506 strmode(fst.mode, mode);
507 (void)printf(" %7lu %*s", (unsigned long)fst.fileid, nflg ? 5 : 10, mode);
508 switch (vn.v_type) {
509 case VBLK:
510 case VCHR: {
511 char *name;
512
513 if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ?
514 S_IFCHR : S_IFBLK)) == NULL))
515 (void)printf(" %2d,%-2d", major(fst.rdev),
516 minor(fst.rdev));
517 else
518 (void)printf(" %6s", name);
519 break;
520 }
521 default:
522 (void)printf(" %6lld", (long long)fst.size);
523 }
524 rw[0] = '\0';
525 if (flag & FREAD)
526 (void)strlcat(rw, "r", sizeof(rw));
527 if (flag & FWRITE)
528 (void)strlcat(rw, "w", sizeof(rw));
529 (void)printf(" %-2s", rw);
530 if (filename && !fsflg)
531 (void)printf(" %s", filename);
532 (void)putchar('\n');
533 }
534
535 static int
536 ufs_filestat(struct vnode *vp, struct filestat *fsp)
537 {
538 struct inode inode;
539 union dinode {
540 struct ufs1_dinode dp1;
541 struct ufs2_dinode dp2;
542 } dip;
543
544 if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
545 dprintf("can't read inode at %p for pid %d", VTOI(vp), Pid);
546 return 0;
547 }
548
549 if (!KVM_READ(inode.i_din.ffs1_din, &dip, sizeof(struct ufs1_dinode))) {
550 dprintf("can't read dinode at %p for pid %d",
551 inode.i_din.ffs1_din, Pid);
552 return 0;
553 }
554 if (inode.i_size == dip.dp1.di_size)
555 fsp->rdev = dip.dp1.di_rdev;
556 else {
557 if (!KVM_READ(inode.i_din.ffs1_din, &dip,
558 sizeof(struct ufs2_dinode))) {
559 dprintf("can't read dinode at %p for pid %d",
560 inode.i_din.ffs1_din, Pid);
561 return 0;
562 }
563 fsp->rdev = dip.dp2.di_rdev;
564 }
565
566 fsp->fsid = inode.i_dev & 0xffff;
567 fsp->fileid = (long)inode.i_number;
568 fsp->mode = (mode_t)inode.i_mode;
569 fsp->size = inode.i_size;
570
571 return 1;
572 }
573
574 static int
575 ext2fs_filestat(struct vnode *vp, struct filestat *fsp)
576 {
577 struct inode inode;
578 u_int16_t mode;
579 u_int32_t size;
580
581 if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
582 dprintf("can't read inode at %p for pid %d", VTOI(vp), Pid);
583 return 0;
584 }
585 fsp->fsid = inode.i_dev & 0xffff;
586 fsp->fileid = (long)inode.i_number;
587
588 if (!KVM_READ(&inode.i_e2fs_mode, &mode, sizeof mode)) {
589 dprintf("can't read inode %p's mode at %p for pid %d", VTOI(vp),
590 &inode.i_e2fs_mode, Pid);
591 return 0;
592 }
593 fsp->mode = mode;
594
595 if (!KVM_READ(&inode.i_e2fs_size, &size, sizeof size)) {
596 dprintf("can't read inode %p's size at %p for pid %d", VTOI(vp),
597 &inode.i_e2fs_size, Pid);
598 return 0;
599 }
600 fsp->size = size;
601 fsp->rdev = 0; /* XXX */
602 return 1;
603 }
604
605 static int
606 nfs_filestat(struct vnode *vp, struct filestat *fsp)
607 {
608 struct nfsnode nfsnode;
609 struct vattr va;
610
611 if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
612 dprintf("can't read nfsnode at %p for pid %d", VTONFS(vp),
613 Pid);
614 return 0;
615 }
616 if (!KVM_READ(nfsnode.n_vattr, &va, sizeof(va))) {
617 dprintf("can't read vnode attributes at %p for pid %d",
618 nfsnode.n_vattr, Pid);
619 return 0;
620 }
621 fsp->fsid = va.va_fsid;
622 fsp->fileid = va.va_fileid;
623 fsp->size = nfsnode.n_size;
624 fsp->rdev = va.va_rdev;
625 fsp->mode = (mode_t)va.va_mode | getftype(vp->v_type);
626
627 return 1;
628 }
629
630 static int
631 msdosfs_filestat(struct vnode *vp, struct filestat *fsp)
632 {
633 struct denode de;
634 struct msdosfsmount mp;
635
636 if (!KVM_READ(VTONFS(vp), &de, sizeof(de))) {
637 dprintf("can't read denode at %p for pid %d", VTONFS(vp),
638 Pid);
639 return 0;
640 }
641 if (!KVM_READ(de.de_pmp, &mp, sizeof(mp))) {
642 dprintf("can't read mount struct at %p for pid %d", de.de_pmp,
643 Pid);
644 return 0;
645 }
646
647 fsp->fsid = de.de_dev & 0xffff;
648 fsp->fileid = 0; /* XXX see msdosfs_vptofh() for more info */
649 fsp->size = de.de_FileSize;
650 fsp->rdev = 0; /* msdosfs doesn't support device files */
651 fsp->mode = (0777 & mp.pm_mask) | getftype(vp->v_type);
652 return 1;
653 }
654
655 static const char *
656 layer_filestat(struct vnode *vp, struct filestat *fsp)
657 {
658 struct layer_node layer_node;
659 struct mount mount;
660 struct vnode vn;
661 const char *badtype;
662
663 if (!KVM_READ(VTOLAYER(vp), &layer_node, sizeof(layer_node))) {
664 dprintf("can't read layer_node at %p for pid %d",
665 VTOLAYER(vp), Pid);
666 return ("error");
667 }
668 if (!KVM_READ(vp->v_mount, &mount, sizeof(struct mount))) {
669 dprintf("can't read mount struct at %p for pid %d",
670 vp->v_mount, Pid);
671 return ("error");
672 }
673 vp = layer_node.layer_lowervp;
674 if (!KVM_READ(vp, &vn, sizeof(struct vnode))) {
675 dprintf("can't read vnode at %p for pid %d", vp, Pid);
676 return ("error");
677 }
678 if ((badtype = vfilestat(&vn, fsp)) == NULL)
679 fsp->fsid = mount.mnt_stat.f_fsidx.__fsid_val[0];
680 return (badtype);
681 }
682
683 static char *
684 getmnton(struct mount *m)
685 {
686 static struct mount mount;
687 static struct mtab {
688 struct mtab *next;
689 struct mount *m;
690 char mntonname[MNAMELEN];
691 } *mhead = NULL;
692 struct mtab *mt;
693
694 for (mt = mhead; mt != NULL; mt = mt->next)
695 if (m == mt->m)
696 return (mt->mntonname);
697 if (!KVM_READ(m, &mount, sizeof(struct mount))) {
698 warnx("can't read mount table at %p", m);
699 return (NULL);
700 }
701 if ((mt = malloc(sizeof (struct mtab))) == NULL) {
702 err(1, "malloc(%u)", (unsigned int)sizeof(struct mtab));
703 }
704 mt->m = m;
705 (void)memmove(&mt->mntonname[0], &mount.mnt_stat.f_mntonname[0],
706 MNAMELEN);
707 mt->next = mhead;
708 mhead = mt;
709 return (mt->mntonname);
710 }
711
712 #ifdef INET6
713 static const char *
714 inet6_addrstr(struct in6_addr *p)
715 {
716 struct sockaddr_in6 sin6;
717 static char hbuf[NI_MAXHOST];
718 const int niflags = NI_NUMERICHOST;
719
720 (void)memset(&sin6, 0, sizeof(sin6));
721 sin6.sin6_family = AF_INET6;
722 sin6.sin6_len = sizeof(struct sockaddr_in6);
723 sin6.sin6_addr = *p;
724 if (IN6_IS_ADDR_LINKLOCAL(p) &&
725 *(u_int16_t *)&sin6.sin6_addr.s6_addr[2] != 0) {
726 sin6.sin6_scope_id =
727 ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);
728 sin6.sin6_addr.s6_addr[2] = sin6.sin6_addr.s6_addr[3] = 0;
729 }
730
731 if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
732 hbuf, sizeof(hbuf), NULL, 0, niflags))
733 return "invalid";
734
735 return hbuf;
736 }
737 #endif
738
739 static void
740 socktrans(struct socket *sock, int i)
741 {
742 static const char *stypename[] = {
743 "unused", /* 0 */
744 "stream", /* 1 */
745 "dgram", /* 2 */
746 "raw", /* 3 */
747 "rdm", /* 4 */
748 "seqpak" /* 5 */
749 };
750 #define STYPEMAX 5
751 struct socket so;
752 struct protosw proto;
753 struct domain dom;
754 struct inpcb inpcb;
755 #ifdef INET6
756 struct in6pcb in6pcb;
757 #endif
758 struct unpcb unpcb;
759 int len;
760 char dname[32];
761 #ifdef INET6
762 char xaddrbuf[NI_MAXHOST + 2];
763 #endif
764
765 PREFIX(i);
766
767 /* fill in socket */
768 if (!KVM_READ(sock, &so, sizeof(struct socket))) {
769 dprintf("can't read sock at %p", sock);
770 goto bad;
771 }
772
773 /* fill in protosw entry */
774 if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
775 dprintf("can't read protosw at %p", so.so_proto);
776 goto bad;
777 }
778
779 /* fill in domain */
780 if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
781 dprintf("can't read domain at %p", proto.pr_domain);
782 goto bad;
783 }
784
785 if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
786 sizeof(dname) - 1)) != sizeof(dname) -1) {
787 dprintf("can't read domain name at %p", dom.dom_name);
788 dname[0] = '\0';
789 }
790 else
791 dname[len] = '\0';
792
793 if ((u_short)so.so_type > STYPEMAX)
794 (void)printf("* %s ?%d", dname, so.so_type);
795 else
796 (void)printf("* %s %s", dname, stypename[so.so_type]);
797
798 /*
799 * protocol specific formatting
800 *
801 * Try to find interesting things to print. For TCP, the interesting
802 * thing is the address of the tcpcb, for UDP and others, just the
803 * inpcb (socket pcb). For UNIX domain, its the address of the socket
804 * pcb and the address of the connected pcb (if connected). Otherwise
805 * just print the protocol number and address of the socket itself.
806 * The idea is not to duplicate netstat, but to make available enough
807 * information for further analysis.
808 */
809 switch(dom.dom_family) {
810 case AF_INET:
811 getinetproto(proto.pr_protocol);
812 if (proto.pr_protocol == IPPROTO_TCP) {
813 if (so.so_pcb == NULL)
814 break;
815 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
816 sizeof(struct inpcb)) != sizeof(struct inpcb)) {
817 dprintf("can't read inpcb at %p", so.so_pcb);
818 goto bad;
819 }
820 (void)printf(" %lx", (long)inpcb.inp_ppcb);
821 (void)printf(" %s:%d",
822 inpcb.inp_laddr.s_addr == INADDR_ANY ? "*" :
823 inet_ntoa(inpcb.inp_laddr), ntohs(inpcb.inp_lport));
824 if (inpcb.inp_fport) {
825 (void)printf(" <-> %s:%d",
826 inpcb.inp_faddr.s_addr == INADDR_ANY ? "*" :
827 inet_ntoa(inpcb.inp_faddr),
828 ntohs(inpcb.inp_fport));
829 }
830 } else if (proto.pr_protocol == IPPROTO_UDP) {
831 if (so.so_pcb == NULL)
832 break;
833 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
834 sizeof(struct inpcb)) != sizeof(struct inpcb)) {
835 dprintf("can't read inpcb at %p", so.so_pcb);
836 goto bad;
837 }
838 (void)printf(" %lx", (long)so.so_pcb);
839 (void)printf(" %s:%d",
840 inpcb.inp_laddr.s_addr == INADDR_ANY ? "*" :
841 inet_ntoa(inpcb.inp_laddr), ntohs(inpcb.inp_lport));
842 if (inpcb.inp_fport)
843 (void)printf(" <-> %s:%d",
844 inpcb.inp_faddr.s_addr == INADDR_ANY ? "*" :
845 inet_ntoa(inpcb.inp_faddr),
846 ntohs(inpcb.inp_fport));
847 } else if (so.so_pcb)
848 (void)printf(" %lx", (long)so.so_pcb);
849 break;
850 #ifdef INET6
851 case AF_INET6:
852 getinetproto(proto.pr_protocol);
853 if (proto.pr_protocol == IPPROTO_TCP) {
854 if (so.so_pcb == NULL)
855 break;
856 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&in6pcb,
857 sizeof(struct in6pcb)) != sizeof(struct in6pcb)) {
858 dprintf("can't read in6pcb at %p", so.so_pcb);
859 goto bad;
860 }
861 (void)printf(" %lx", (long)in6pcb.in6p_ppcb);
862 (void)snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]",
863 inet6_addrstr(&in6pcb.in6p_laddr));
864 (void)printf(" %s:%d",
865 IN6_IS_ADDR_UNSPECIFIED(&in6pcb.in6p_laddr) ? "*" :
866 xaddrbuf,
867 ntohs(in6pcb.in6p_lport));
868 if (in6pcb.in6p_fport) {
869 (void)snprintf(xaddrbuf, sizeof(xaddrbuf),
870 "[%s]", inet6_addrstr(&in6pcb.in6p_faddr));
871 (void)printf(" <-> %s:%d",
872 IN6_IS_ADDR_UNSPECIFIED(&in6pcb.in6p_faddr) ? "*" :
873 xaddrbuf,
874 ntohs(in6pcb.in6p_fport));
875 }
876 } else if (proto.pr_protocol == IPPROTO_UDP) {
877 if (so.so_pcb == NULL)
878 break;
879 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&in6pcb,
880 sizeof(struct in6pcb)) != sizeof(struct in6pcb)) {
881 dprintf("can't read inpcb at %p", so.so_pcb);
882 goto bad;
883 }
884 (void)printf(" %lx", (long)so.so_pcb);
885 (void)snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]",
886 inet6_addrstr(&in6pcb.in6p_laddr));
887 (void)printf(" %s:%d",
888 IN6_IS_ADDR_UNSPECIFIED(&in6pcb.in6p_laddr) ? "*" :
889 xaddrbuf,
890 ntohs(in6pcb.in6p_lport));
891 if (in6pcb.in6p_fport) {
892 (void)snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]",
893 inet6_addrstr(&in6pcb.in6p_faddr));
894 (void)printf(" <-> %s:%d",
895 IN6_IS_ADDR_UNSPECIFIED(&in6pcb.in6p_faddr) ? "*" :
896 xaddrbuf,
897 ntohs(in6pcb.in6p_fport));
898 }
899 } else if (so.so_pcb)
900 (void)printf(" %lx", (long)so.so_pcb);
901 break;
902 #endif
903 case AF_LOCAL:
904 /* print address of pcb and connected pcb */
905 if (so.so_pcb) {
906 (void)printf(" %lx", (long)so.so_pcb);
907 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb,
908 sizeof(struct unpcb)) != sizeof(struct unpcb)){
909 dprintf("can't read unpcb at %p", so.so_pcb);
910 goto bad;
911 }
912 if (unpcb.unp_conn) {
913 char shoconn[4], *cp;
914
915 cp = shoconn;
916 if (!(so.so_state & SS_CANTRCVMORE))
917 *cp++ = '<';
918 *cp++ = '-';
919 if (!(so.so_state & SS_CANTSENDMORE))
920 *cp++ = '>';
921 *cp = '\0';
922 (void)printf(" %s %lx", shoconn,
923 (long)unpcb.unp_conn);
924 }
925 }
926 break;
927 default:
928 /* print protocol number and socket address */
929 (void)printf(" %d %lx", proto.pr_protocol, (long)sock);
930 }
931 (void)printf("\n");
932 return;
933 bad:
934 (void)printf("* error\n");
935 }
936
937 static void
938 ptrans(struct file *fp, struct pipe *cpipe, int i)
939 {
940 struct pipe cp;
941
942 PREFIX(i);
943
944 /* fill in pipe */
945 if (!KVM_READ(cpipe, &cp, sizeof(struct pipe))) {
946 dprintf("can't read pipe at %p", cpipe);
947 goto bad;
948 }
949
950 /* pipe descriptor is either read or write, never both */
951 (void)printf("* pipe %p %s %p %s%s%s", cpipe,
952 (fp->f_flag & FWRITE) ? "->" : "<-",
953 cp.pipe_peer,
954 (fp->f_flag & FWRITE) ? "w" : "r",
955 (fp->f_flag & FNONBLOCK) ? "n" : "",
956 (cp.pipe_state & PIPE_ASYNC) ? "a" : "");
957 (void)printf("\n");
958 return;
959 bad:
960 (void)printf("* error\n");
961 }
962
963 static void
964 kqueuetrans(void *kq, int i)
965 {
966
967 PREFIX(i);
968 (void)printf("* kqueue %lx", (long)kq);
969 (void)printf("\n");
970 }
971
972 /*
973 * getinetproto --
974 * print name of protocol number
975 */
976 static void
977 getinetproto(int number)
978 {
979 const char *cp;
980
981 switch (number) {
982 case IPPROTO_IP:
983 cp = "ip"; break;
984 case IPPROTO_ICMP:
985 cp ="icmp"; break;
986 case IPPROTO_GGP:
987 cp ="ggp"; break;
988 case IPPROTO_TCP:
989 cp ="tcp"; break;
990 case IPPROTO_EGP:
991 cp ="egp"; break;
992 case IPPROTO_PUP:
993 cp ="pup"; break;
994 case IPPROTO_UDP:
995 cp ="udp"; break;
996 case IPPROTO_IDP:
997 cp ="idp"; break;
998 case IPPROTO_RAW:
999 cp ="raw"; break;
1000 case IPPROTO_ICMPV6:
1001 cp ="icmp6"; break;
1002 default:
1003 (void)printf(" %d", number);
1004 return;
1005 }
1006 (void)printf(" %s", cp);
1007 }
1008
1009 static int
1010 getfname(const char *filename)
1011 {
1012 struct stat statbuf;
1013 DEVS *cur;
1014
1015 if (stat(filename, &statbuf)) {
1016 warn("stat(%s)", filename);
1017 return 0;
1018 }
1019 if ((cur = malloc(sizeof(DEVS))) == NULL) {
1020 err(1, "malloc(%u)", (unsigned int)sizeof(DEVS));
1021 }
1022 cur->next = devs;
1023 devs = cur;
1024
1025 cur->ino = statbuf.st_ino;
1026 cur->fsid = statbuf.st_dev & 0xffff;
1027 cur->name = filename;
1028 return 1;
1029 }
1030
1031 mode_t
1032 getftype(enum vtype v_type)
1033 {
1034 mode_t ftype;
1035
1036 switch (v_type) {
1037 case VREG:
1038 ftype = S_IFREG;
1039 break;
1040 case VDIR:
1041 ftype = S_IFDIR;
1042 break;
1043 case VBLK:
1044 ftype = S_IFBLK;
1045 break;
1046 case VCHR:
1047 ftype = S_IFCHR;
1048 break;
1049 case VLNK:
1050 ftype = S_IFLNK;
1051 break;
1052 case VSOCK:
1053 ftype = S_IFSOCK;
1054 break;
1055 case VFIFO:
1056 ftype = S_IFIFO;
1057 break;
1058 default:
1059 ftype = 0;
1060 break;
1061 };
1062
1063 return ftype;
1064 }
1065
1066 static void
1067 usage(void)
1068 {
1069 (void)fprintf(stderr, "Usage: %s [-fnv] [-p pid] [-u user] "
1070 "[-N system] [-M core] [file ...]\n", getprogname());
1071 exit(1);
1072 }
1073