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