fstat.c revision 1.95.2.2 1 /* $NetBSD: fstat.c,v 1.95.2.2 2012/10/30 19:00:17 yamt 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\
35 The Regents of the University of California. All rights reserved.");
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.95.2.2 2012/10/30 19:00:17 yamt 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/mount.h>
62 #undef _KERNEL
63 #define _KERNEL
64 #include <sys/file.h>
65 #include <ufs/ufs/inode.h>
66 #include <ufs/ufs/ufsmount.h>
67 #undef _KERNEL
68 #define NFS
69 #include <nfs/nfsproto.h>
70 #include <nfs/rpcv2.h>
71 #include <nfs/nfs.h>
72 #include <nfs/nfsnode.h>
73 #undef NFS
74 #include <msdosfs/denode.h>
75 #include <msdosfs/bpb.h>
76 #define _KERNEL
77 #include <msdosfs/msdosfsmount.h>
78 #undef _KERNEL
79 #define _KERNEL
80 #include <miscfs/genfs/layer.h>
81 #undef _KERNEL
82
83 #include <net/route.h>
84 #include <netinet/in.h>
85 #include <netinet/in_systm.h>
86 #include <netinet/ip.h>
87 #include <netinet/in_pcb.h>
88
89 #ifdef INET6
90 #include <netinet/ip6.h>
91 #include <netinet6/ip6_var.h>
92 #include <netinet6/in6_pcb.h>
93 #endif
94
95 #include <netatalk/at.h>
96 #include <netatalk/ddp_var.h>
97
98 #include <netdb.h>
99 #include <arpa/inet.h>
100
101 #include <ctype.h>
102 #include <errno.h>
103 #include <kvm.h>
104 #include <limits.h>
105 #include <nlist.h>
106 #include <paths.h>
107 #include <pwd.h>
108 #include <stdio.h>
109 #include <stdlib.h>
110 #include <string.h>
111 #include <unistd.h>
112 #include <err.h>
113 #include <util.h>
114
115 #include "fstat.h"
116
117 #define TEXT -1
118 #define CDIR -2
119 #define RDIR -3
120 #define TRACE -4
121
122 typedef struct devs {
123 struct devs *next;
124 long fsid;
125 ino_t ino;
126 const char *name;
127 } DEVS;
128 static DEVS *devs;
129
130 static int fsflg, /* show files on same filesystem as file(s) argument */
131 pflg, /* show files open by a particular pid */
132 uflg; /* show files open by a particular (effective) user */
133 static int checkfile; /* true if restricting to particular files or filesystems */
134 static int nflg; /* (numerical) display f.s. and rdev as dev_t */
135 int vflg; /* display errors in locating kernel data objects etc... */
136
137 static fdfile_t **ofiles; /* buffer of pointers to file structures */
138 static int fstat_maxfiles;
139 #define ALLOC_OFILES(d) \
140 if ((d) > fstat_maxfiles) { \
141 size_t len = (d) * sizeof(fdfile_t *); \
142 free(ofiles); \
143 ofiles = malloc(len); \
144 if (ofiles == NULL) { \
145 err(1, "malloc(%zu)", len); \
146 } \
147 fstat_maxfiles = (d); \
148 }
149
150 kvm_t *kd;
151
152 static const char *const dtypes[] = {
153 DTYPE_NAMES
154 };
155
156 static void dofiles(struct kinfo_proc2 *);
157 static int ext2fs_filestat(struct vnode *, struct filestat *);
158 static int getfname(const char *);
159 static void getinetproto(int);
160 static void getatproto(int);
161 static char *getmnton(struct mount *);
162 static const char *layer_filestat(struct vnode *, struct filestat *);
163 static int msdosfs_filestat(struct vnode *, struct filestat *);
164 static int nfs_filestat(struct vnode *, struct filestat *);
165 static const char *inet_addrstr(char *, size_t, const struct in_addr *,
166 uint16_t);
167 #ifdef INET6
168 static const char *inet6_addrstr(char *, size_t, const struct in6_addr *,
169 uint16_t);
170 #endif
171 static const char *at_addrstr(char *, size_t, const struct sockaddr_at *);
172 static void socktrans(struct socket *, int);
173 static void misctrans(struct file *);
174 static int ufs_filestat(struct vnode *, struct filestat *);
175 static void usage(void) __dead;
176 static const char *vfilestat(struct vnode *, struct filestat *);
177 static void vtrans(struct vnode *, int, int);
178 static void ftrans(fdfile_t *, int);
179 static void ptrans(struct file *, struct pipe *, int);
180 static void kdriver_init(void);
181
182 int
183 main(int argc, char **argv)
184 {
185 struct passwd *passwd;
186 struct kinfo_proc2 *p, *plast;
187 int arg, ch, what;
188 char *memf, *nlistf;
189 char buf[_POSIX2_LINE_MAX];
190 int cnt;
191 gid_t egid = getegid();
192
193 (void)setegid(getgid());
194 arg = 0;
195 what = KERN_PROC_ALL;
196 nlistf = memf = NULL;
197 while ((ch = getopt(argc, argv, "fnp:u:vN:M:")) != -1)
198 switch((char)ch) {
199 case 'f':
200 fsflg = 1;
201 break;
202 case 'M':
203 memf = optarg;
204 break;
205 case 'N':
206 nlistf = optarg;
207 break;
208 case 'n':
209 nflg = 1;
210 break;
211 case 'p':
212 if (pflg++)
213 usage();
214 if (!isdigit((unsigned char)*optarg)) {
215 warnx("-p requires a process id");
216 usage();
217 }
218 what = KERN_PROC_PID;
219 arg = atoi(optarg);
220 break;
221 case 'u':
222 if (uflg++)
223 usage();
224 if (!(passwd = getpwnam(optarg))) {
225 errx(1, "%s: unknown uid", optarg);
226 }
227 what = KERN_PROC_UID;
228 arg = passwd->pw_uid;
229 break;
230 case 'v':
231 vflg = 1;
232 break;
233 case '?':
234 default:
235 usage();
236 }
237
238 kdriver_init();
239
240 if (*(argv += optind)) {
241 for (; *argv; ++argv) {
242 if (getfname(*argv))
243 checkfile = 1;
244 }
245 if (!checkfile) /* file(s) specified, but none accessible */
246 exit(1);
247 }
248
249 ALLOC_OFILES(256); /* reserve space for file pointers */
250
251 if (fsflg && !checkfile) {
252 /* -f with no files means use wd */
253 if (getfname(".") == 0)
254 exit(1);
255 checkfile = 1;
256 }
257
258 /*
259 * Discard setgid privileges. If not the running kernel, we toss
260 * them away totally so that bad guys can't print interesting stuff
261 * from kernel memory, otherwise switch back to kmem for the
262 * duration of the kvm_openfiles() call.
263 */
264 if (nlistf != NULL || memf != NULL)
265 (void)setgid(getgid());
266 else
267 (void)setegid(egid);
268
269 if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL)
270 errx(1, "%s", buf);
271
272 /* get rid of it now anyway */
273 if (nlistf == NULL && memf == NULL)
274 (void)setgid(getgid());
275
276 if ((p = kvm_getproc2(kd, what, arg, sizeof *p, &cnt)) == NULL) {
277 errx(1, "%s", kvm_geterr(kd));
278 }
279 if (nflg)
280 (void)printf("%s",
281 "USER CMD PID FD DEV INUM MODE SZ|DV R/W");
282 else
283 (void)printf("%s",
284 "USER CMD PID FD MOUNT INUM MODE SZ|DV R/W");
285 if (checkfile && fsflg == 0)
286 (void)printf(" NAME\n");
287 else
288 (void)putchar('\n');
289
290 for (plast = &p[cnt]; p < plast; ++p) {
291 if (p->p_stat == SZOMB)
292 continue;
293 dofiles(p);
294 }
295 return 0;
296 }
297
298 static const char *Uname, *Comm;
299 pid_t Pid;
300
301 #define PREFIX(i) (void)printf("%-8.8s %-10s %5d", Uname, Comm, Pid); \
302 switch(i) { \
303 case TEXT: \
304 (void)printf(" text"); \
305 break; \
306 case CDIR: \
307 (void)printf(" wd"); \
308 break; \
309 case RDIR: \
310 (void)printf(" root"); \
311 break; \
312 case TRACE: \
313 (void)printf(" tr"); \
314 break; \
315 default: \
316 (void)printf(" %4d", i); \
317 break; \
318 }
319
320 static struct kinfo_drivers *kdriver;
321 static size_t kdriverlen;
322
323 static int
324 kdriver_comp(const void *a, const void *b)
325 {
326 const struct kinfo_drivers *ka = a;
327 const struct kinfo_drivers *kb = b;
328 int kac = ka->d_cmajor == -1 ? 0 : ka->d_cmajor;
329 int kbc = kb->d_cmajor == -1 ? 0 : kb->d_cmajor;
330 int kab = ka->d_bmajor == -1 ? 0 : ka->d_bmajor;
331 int kbb = kb->d_bmajor == -1 ? 0 : kb->d_bmajor;
332 int c = kac - kbc;
333 if (c == 0)
334 return kab - kbb;
335 else
336 return c;
337 }
338
339 static const char *
340 kdriver_search(int type, dev_t num)
341 {
342 struct kinfo_drivers k, *kp;
343 static char buf[64];
344
345 if (nflg)
346 goto out;
347
348 if (type == VBLK) {
349 k.d_bmajor = num;
350 k.d_cmajor = -1;
351 } else {
352 k.d_bmajor = -1;
353 k.d_cmajor = num;
354 }
355 kp = bsearch(&k, kdriver, kdriverlen, sizeof(*kdriver), kdriver_comp);
356 if (kp)
357 return kp->d_name;
358 out:
359 snprintf(buf, sizeof(buf), "%llu", (unsigned long long)num);
360 return buf;
361 }
362
363
364 static void
365 kdriver_init(void)
366 {
367 size_t sz;
368 int error;
369 static const int name[2] = { CTL_KERN, KERN_DRIVERS };
370
371 error = sysctl(name, __arraycount(name), NULL, &sz, NULL, 0);
372 if (error == -1) {
373 warn("sysctl kern.drivers");
374 return;
375 }
376
377 if (sz % sizeof(*kdriver)) {
378 warnx("bad size %zu for kern.drivers", sz);
379 return;
380 }
381
382 kdriver = malloc(sz);
383 if (kdriver == NULL) {
384 warn("malloc");
385 return;
386 }
387
388 error = sysctl(name, __arraycount(name), kdriver, &sz, NULL, 0);
389 if (error == -1) {
390 warn("sysctl kern.drivers");
391 return;
392 }
393
394 kdriverlen = sz / sizeof(*kdriver);
395 qsort(kdriver, kdriverlen, sizeof(*kdriver), kdriver_comp);
396 #ifdef DEBUG
397 for (size_t i = 0; i < kdriverlen; i++)
398 printf("%d %d %s\n", kdriver[i].d_cmajor, kdriver[i].d_bmajor,
399 kdriver[i].d_name);
400 #endif
401 }
402
403 /*
404 * print open files attributed to this process
405 */
406 static void
407 dofiles(struct kinfo_proc2 *p)
408 {
409 int i;
410 struct filedesc filed;
411 struct cwdinfo cwdi;
412 struct fdtab dt;
413
414 Uname = user_from_uid(p->p_uid, 0);
415 Pid = p->p_pid;
416 Comm = p->p_comm;
417
418 if (p->p_fd == 0 || p->p_cwdi == 0)
419 return;
420 if (!KVM_READ(p->p_fd, &filed, sizeof (filed))) {
421 warnx("can't read filedesc at %p for pid %d",
422 (void *)(uintptr_t)p->p_fd, Pid);
423 return;
424 }
425 if (filed.fd_lastfile == -1)
426 return;
427 if (!KVM_READ(p->p_cwdi, &cwdi, sizeof(cwdi))) {
428 warnx("can't read cwdinfo at %p for pid %d",
429 (void *)(uintptr_t)p->p_cwdi, Pid);
430 return;
431 }
432 if (!KVM_READ(filed.fd_dt, &dt, sizeof(dt))) {
433 warnx("can't read dtab at %p for pid %d", filed.fd_dt, Pid);
434 return;
435 }
436 if ((unsigned)filed.fd_lastfile >= dt.dt_nfiles ||
437 filed.fd_freefile > filed.fd_lastfile + 1) {
438 dprintf("filedesc corrupted at %p for pid %d",
439 (void *)(uintptr_t)p->p_fd, Pid);
440 return;
441 }
442 /*
443 * root directory vnode, if one
444 */
445 if (cwdi.cwdi_rdir)
446 vtrans(cwdi.cwdi_rdir, RDIR, FREAD);
447 /*
448 * current working directory vnode
449 */
450 vtrans(cwdi.cwdi_cdir, CDIR, FREAD);
451 #if 0
452 /*
453 * Disable for now, since p->p_tracep appears to point to a ktr_desc *
454 * ktrace vnode, if one
455 */
456 if (p->p_tracep)
457 ftrans(p->p_tracep, TRACE);
458 #endif
459 /*
460 * open files
461 */
462 #define FPSIZE (sizeof (fdfile_t *))
463 ALLOC_OFILES(filed.fd_lastfile+1);
464 if (!KVM_READ(&filed.fd_dt->dt_ff, ofiles,
465 (filed.fd_lastfile+1) * FPSIZE)) {
466 dprintf("can't read file structures at %p for pid %d",
467 &filed.fd_dt->dt_ff, Pid);
468 return;
469 }
470 for (i = 0; i <= filed.fd_lastfile; i++) {
471 if (ofiles[i] == NULL)
472 continue;
473 ftrans(ofiles[i], i);
474 }
475 }
476
477 static void
478 ftrans(fdfile_t *fp, int i)
479 {
480 struct file file;
481 fdfile_t fdfile;
482
483 if (!KVM_READ(fp, &fdfile, sizeof(fdfile))) {
484 dprintf("can't read file %d at %p for pid %d",
485 i, fp, Pid);
486 return;
487 }
488 if (fdfile.ff_file == NULL)
489 return;
490 if (!KVM_READ(fdfile.ff_file, &file, sizeof(file))) {
491 dprintf("can't read file %d at %p for pid %d",
492 i, fdfile.ff_file, Pid);
493 return;
494 }
495 switch (file.f_type) {
496 case DTYPE_VNODE:
497 vtrans(file.f_data, i, file.f_flag);
498 break;
499 case DTYPE_SOCKET:
500 if (checkfile == 0)
501 socktrans(file.f_data, i);
502 break;
503 case DTYPE_PIPE:
504 if (checkfile == 0)
505 ptrans(&file, file.f_data, i);
506 break;
507 case DTYPE_MISC:
508 case DTYPE_KQUEUE:
509 case DTYPE_CRYPTO:
510 case DTYPE_MQUEUE:
511 case DTYPE_SEM:
512 if (checkfile == 0)
513 misctrans(&file);
514 break;
515 default:
516 dprintf("unknown file type %d for file %d of pid %d",
517 file.f_type, i, Pid);
518 break;
519 }
520 }
521
522 static const char dead[] = "dead";
523
524 static const char *
525 vfilestat(struct vnode *vp, struct filestat *fsp)
526 {
527 const char *badtype = NULL;
528
529 if (vp->v_type == VNON)
530 badtype = "none";
531 else if (vp->v_type == VBAD)
532 badtype = "bad";
533 else
534 switch (vp->v_tag) {
535 case VT_NON:
536 badtype = dead;
537 break;
538 case VT_UFS:
539 case VT_LFS:
540 case VT_MFS:
541 if (!ufs_filestat(vp, fsp))
542 badtype = "error";
543 break;
544 case VT_MSDOSFS:
545 if (!msdosfs_filestat(vp, fsp))
546 badtype = "error";
547 break;
548 case VT_NFS:
549 if (!nfs_filestat(vp, fsp))
550 badtype = "error";
551 break;
552 case VT_EXT2FS:
553 if (!ext2fs_filestat(vp, fsp))
554 badtype = "error";
555 break;
556 case VT_ISOFS:
557 if (!isofs_filestat(vp, fsp))
558 badtype = "error";
559 break;
560 case VT_NTFS:
561 if (!ntfs_filestat(vp, fsp))
562 badtype = "error";
563 break;
564 case VT_PTYFS:
565 if (!ptyfs_filestat(vp, fsp))
566 badtype = "error";
567 break;
568 case VT_TMPFS:
569 if (!tmpfs_filestat(vp, fsp))
570 badtype = "error";
571 break;
572 case VT_NULL:
573 case VT_OVERLAY:
574 case VT_UMAP:
575 badtype = layer_filestat(vp, fsp);
576 break;
577 default: {
578 static char unknown[10];
579 (void)snprintf(unknown, sizeof unknown,
580 "?(%x)", vp->v_tag);
581 badtype = unknown;
582 break;
583 }
584 }
585 return badtype;
586 }
587
588 static void
589 vtrans(struct vnode *vp, int i, int flag)
590 {
591 struct vnode vn;
592 struct filestat fst;
593 char mode[15], rw[3];
594 const char *badtype, *filename;
595
596 filename = NULL;
597 if (!KVM_READ(vp, &vn, sizeof(struct vnode))) {
598 dprintf("can't read vnode at %p for pid %d", vp, Pid);
599 return;
600 }
601 badtype = vfilestat(&vn, &fst);
602 if (checkfile) {
603 int fsmatch = 0;
604 DEVS *d;
605
606 if (badtype && badtype != dead)
607 return;
608 for (d = devs; d != NULL; d = d->next)
609 if (d->fsid == fst.fsid) {
610 fsmatch = 1;
611 if (d->ino == fst.fileid) {
612 filename = d->name;
613 break;
614 }
615 }
616 if (fsmatch == 0 || (filename == NULL && fsflg == 0))
617 return;
618 }
619 PREFIX(i);
620 if (badtype == dead) {
621 char buf[1024];
622 (void)snprintb(buf, sizeof(buf), VNODE_FLAGBITS,
623 vn.v_iflag | vn.v_vflag | vn.v_uflag);
624 (void)printf(" flags %s\n", buf);
625 return;
626 } else if (badtype) {
627 (void)printf(" - - %10s -\n", badtype);
628 return;
629 }
630 if (nflg)
631 (void)printf(" %2llu,%-2llu",
632 (unsigned long long)major(fst.fsid),
633 (unsigned long long)minor(fst.fsid));
634 else
635 (void)printf(" %-8s", getmnton(vn.v_mount));
636 if (nflg)
637 (void)snprintf(mode, sizeof mode, "%o", fst.mode);
638 else
639 strmode(fst.mode, mode);
640 (void)printf(" %7"PRIu64" %*s", fst.fileid, nflg ? 5 : 10, mode);
641 switch (vn.v_type) {
642 case VBLK:
643 case VCHR: {
644 char *name;
645
646 if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ?
647 S_IFCHR : S_IFBLK)) == NULL))
648 (void)printf(" %s,%-2llu",
649 kdriver_search(vn.v_type, major(fst.rdev)),
650 (unsigned long long)minor(fst.rdev));
651 else
652 (void)printf(" %6s", name);
653 break;
654 }
655 default:
656 (void)printf(" %6lld", (long long)fst.size);
657 }
658 rw[0] = '\0';
659 if (flag & FREAD)
660 (void)strlcat(rw, "r", sizeof(rw));
661 if (flag & FWRITE)
662 (void)strlcat(rw, "w", sizeof(rw));
663 (void)printf(" %-2s", rw);
664 if (filename && !fsflg)
665 (void)printf(" %s", filename);
666 (void)putchar('\n');
667 }
668
669 static int
670 ufs_filestat(struct vnode *vp, struct filestat *fsp)
671 {
672 struct inode inode;
673 struct ufsmount ufsmount;
674 union dinode {
675 struct ufs1_dinode dp1;
676 struct ufs2_dinode dp2;
677 } dip;
678
679 if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
680 dprintf("can't read inode at %p for pid %d", VTOI(vp), Pid);
681 return 0;
682 }
683
684 if (!KVM_READ(inode.i_ump, &ufsmount, sizeof (struct ufsmount))) {
685 dprintf("can't read ufsmount at %p for pid %d", inode.i_ump, Pid);
686 return 0;
687 }
688
689 switch (ufsmount.um_fstype) {
690 case UFS1:
691 if (!KVM_READ(inode.i_din.ffs1_din, &dip,
692 sizeof(struct ufs1_dinode))) {
693 dprintf("can't read dinode at %p for pid %d",
694 inode.i_din.ffs1_din, Pid);
695 return 0;
696 }
697 fsp->rdev = dip.dp1.di_rdev;
698 break;
699 case UFS2:
700 if (!KVM_READ(inode.i_din.ffs2_din, &dip,
701 sizeof(struct ufs2_dinode))) {
702 dprintf("can't read dinode at %p for pid %d",
703 inode.i_din.ffs2_din, Pid);
704 return 0;
705 }
706 fsp->rdev = dip.dp2.di_rdev;
707 break;
708 default:
709 dprintf("unknown ufs type %ld for pid %d",
710 ufsmount.um_fstype, Pid);
711 break;
712 }
713 fsp->fsid = inode.i_dev & 0xffff;
714 fsp->fileid = inode.i_number;
715 fsp->mode = (mode_t)inode.i_mode;
716 fsp->size = inode.i_size;
717
718 return 1;
719 }
720
721 static int
722 ext2fs_filestat(struct vnode *vp, struct filestat *fsp)
723 {
724 struct inode inode;
725 struct ext2fs_dinode dinode;
726
727 if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
728 dprintf("can't read inode at %p for pid %d", VTOI(vp), Pid);
729 return 0;
730 }
731 fsp->fsid = inode.i_dev & 0xffff;
732 fsp->fileid = inode.i_number;
733
734 if (!KVM_READ(inode.i_din.e2fs_din, &dinode, sizeof dinode)) {
735 dprintf("can't read ext2fs_dinode at %p for pid %d",
736 inode.i_din.e2fs_din, Pid);
737 return 0;
738 }
739 fsp->mode = dinode.e2di_mode;
740 fsp->size = dinode.e2di_size;
741 fsp->rdev = dinode.e2di_rdev;
742
743 return 1;
744 }
745
746 static int
747 nfs_filestat(struct vnode *vp, struct filestat *fsp)
748 {
749 struct nfsnode nfsnode;
750 struct vattr va;
751
752 if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
753 dprintf("can't read nfsnode at %p for pid %d", VTONFS(vp),
754 Pid);
755 return 0;
756 }
757 if (!KVM_READ(nfsnode.n_vattr, &va, sizeof(va))) {
758 dprintf("can't read vnode attributes at %p for pid %d",
759 nfsnode.n_vattr, Pid);
760 return 0;
761 }
762 fsp->fsid = va.va_fsid;
763 fsp->fileid = va.va_fileid;
764 fsp->size = nfsnode.n_size;
765 fsp->rdev = va.va_rdev;
766 fsp->mode = (mode_t)va.va_mode | getftype(vp->v_type);
767
768 return 1;
769 }
770
771 static int
772 msdosfs_filestat(struct vnode *vp, struct filestat *fsp)
773 {
774 struct denode de;
775 struct msdosfsmount mp;
776
777 if (!KVM_READ(VTONFS(vp), &de, sizeof(de))) {
778 dprintf("can't read denode at %p for pid %d", VTONFS(vp),
779 Pid);
780 return 0;
781 }
782 if (!KVM_READ(de.de_pmp, &mp, sizeof(mp))) {
783 dprintf("can't read mount struct at %p for pid %d", de.de_pmp,
784 Pid);
785 return 0;
786 }
787
788 fsp->fsid = de.de_dev & 0xffff;
789 fsp->fileid = 0; /* XXX see msdosfs_vptofh() for more info */
790 fsp->size = de.de_FileSize;
791 fsp->rdev = 0; /* msdosfs doesn't support device files */
792 fsp->mode = (0777 & mp.pm_mask) | getftype(vp->v_type);
793 return 1;
794 }
795
796 static const char *
797 layer_filestat(struct vnode *vp, struct filestat *fsp)
798 {
799 struct layer_node layer_node;
800 struct mount mount;
801 struct vnode vn;
802 const char *badtype;
803
804 if (!KVM_READ(VTOLAYER(vp), &layer_node, sizeof(layer_node))) {
805 dprintf("can't read layer_node at %p for pid %d",
806 VTOLAYER(vp), Pid);
807 return "error";
808 }
809 if (!KVM_READ(vp->v_mount, &mount, sizeof(struct mount))) {
810 dprintf("can't read mount struct at %p for pid %d",
811 vp->v_mount, Pid);
812 return "error";
813 }
814 vp = layer_node.layer_lowervp;
815 if (!KVM_READ(vp, &vn, sizeof(struct vnode))) {
816 dprintf("can't read vnode at %p for pid %d", vp, Pid);
817 return "error";
818 }
819 if ((badtype = vfilestat(&vn, fsp)) == NULL)
820 fsp->fsid = mount.mnt_stat.f_fsidx.__fsid_val[0];
821 return badtype;
822 }
823
824 static char *
825 getmnton(struct mount *m)
826 {
827 static struct mount mount;
828 static struct mtab {
829 struct mtab *next;
830 struct mount *m;
831 char mntonname[MNAMELEN];
832 } *mhead = NULL;
833 struct mtab *mt;
834
835 for (mt = mhead; mt != NULL; mt = mt->next)
836 if (m == mt->m)
837 return mt->mntonname;
838 if (!KVM_READ(m, &mount, sizeof(struct mount))) {
839 warnx("can't read mount table at %p", m);
840 return NULL;
841 }
842 if ((mt = malloc(sizeof (struct mtab))) == NULL) {
843 err(1, "malloc(%u)", (unsigned int)sizeof(struct mtab));
844 }
845 mt->m = m;
846 (void)memmove(&mt->mntonname[0], &mount.mnt_stat.f_mntonname[0],
847 MNAMELEN);
848 mt->next = mhead;
849 mhead = mt;
850 return mt->mntonname;
851 }
852
853 static const char *
854 inet_addrstr(char *buf, size_t len, const struct in_addr *a, uint16_t p)
855 {
856 char addr[256], serv[256];
857 struct sockaddr_in sin;
858 const int niflags = nflg ? (NI_NUMERICHOST|NI_NUMERICSERV) : 0;
859
860 (void)memset(&sin, 0, sizeof(sin));
861 sin.sin_family = AF_INET;
862 sin.sin_len = sizeof(sin);
863 sin.sin_addr = *a;
864 sin.sin_port = htons(p);
865
866 serv[0] = '\0';
867
868 if (getnameinfo((struct sockaddr *)&sin, sin.sin_len,
869 addr, sizeof(addr), serv, sizeof(serv), niflags)) {
870 if (inet_ntop(AF_INET, a, addr, sizeof(addr)) == NULL)
871 strlcpy(addr, "invalid", sizeof(addr));
872 }
873
874 if (serv[0] == '\0')
875 snprintf(serv, sizeof(serv), "%u", p);
876
877 if (a->s_addr == INADDR_ANY) {
878 if (p == 0)
879 buf[0] = '\0';
880 else
881 snprintf(buf, len, "*:%s", serv);
882 return buf;
883 }
884
885 snprintf(buf, len, "%s:%s", addr, serv);
886 return buf;
887 }
888
889 #ifdef INET6
890 static const char *
891 inet6_addrstr(char *buf, size_t len, const struct in6_addr *a, uint16_t p)
892 {
893 char addr[256], serv[256];
894 struct sockaddr_in6 sin6;
895 const int niflags = nflg ? (NI_NUMERICHOST|NI_NUMERICSERV) : 0;
896
897 (void)memset(&sin6, 0, sizeof(sin6));
898 sin6.sin6_family = AF_INET6;
899 sin6.sin6_len = sizeof(sin6);
900 sin6.sin6_addr = *a;
901 sin6.sin6_port = htons(p);
902
903 if (IN6_IS_ADDR_LINKLOCAL(a) &&
904 *(u_int16_t *)&sin6.sin6_addr.s6_addr[2] != 0) {
905 sin6.sin6_scope_id =
906 ntohs(*(uint16_t *)&sin6.sin6_addr.s6_addr[2]);
907 sin6.sin6_addr.s6_addr[2] = 0;
908 sin6.sin6_addr.s6_addr[3] = 0;
909 }
910
911 serv[0] = '\0';
912
913 if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
914 addr, sizeof(addr), serv, sizeof(serv), niflags)) {
915 if (inet_ntop(AF_INET6, a, addr, sizeof(addr)) == NULL)
916 strlcpy(addr, "invalid", sizeof(addr));
917 }
918
919 if (serv[0] == '\0')
920 snprintf(serv, sizeof(serv), "%u", p);
921
922 if (IN6_IS_ADDR_UNSPECIFIED(a)) {
923 if (p == 0)
924 buf[0] = '\0';
925 else
926 snprintf(buf, len, "*:%s", serv);
927 return buf;
928 }
929
930 if (strchr(addr, ':') == NULL)
931 snprintf(buf, len, "%s:%s", addr, serv);
932 else
933 snprintf(buf, len, "[%s]:%s", addr, serv);
934
935 return buf;
936 }
937 #endif
938
939 static const char *
940 at_addrstr(char *buf, size_t len, const struct sockaddr_at *sat)
941 {
942 const struct netrange *nr = &sat->sat_range.r_netrange;
943 const struct at_addr *at = &sat->sat_addr;
944 char addr[64], phase[64], range[64];
945
946 if (sat->sat_port || at->s_net || at->s_node) {
947 if (at->s_net || at->s_node)
948 snprintf(addr, sizeof(addr), "%u.%u:%u",
949 ntohs(at->s_net), at->s_node, sat->sat_port);
950 else
951 snprintf(addr, sizeof(addr), "*:%u", sat->sat_port);
952 } else
953 addr[0] = '\0';
954
955 if (nr->nr_phase)
956 snprintf(phase, sizeof(phase), " phase %u", nr->nr_phase);
957 else
958 phase[0] = '\0';
959
960 if (nr->nr_firstnet || nr->nr_lastnet)
961 snprintf(range, sizeof(range), " range [%u-%u]",
962 ntohs(nr->nr_firstnet), ntohs(nr->nr_lastnet));
963 else
964 range[0] = '\0';
965
966 snprintf(buf, len, "%s%s%s", addr, phase, range);
967 return buf;
968 }
969
970 static void
971 socktrans(struct socket *sock, int i)
972 {
973 static const char *stypename[] = {
974 "unused", /* 0 */
975 "stream", /* 1 */
976 "dgram", /* 2 */
977 "raw", /* 3 */
978 "rdm", /* 4 */
979 "seqpak" /* 5 */
980 };
981 #define STYPEMAX 5
982 struct socket so;
983 struct protosw proto;
984 struct domain dom;
985 struct inpcb inpcb;
986 #ifdef INET6
987 struct in6pcb in6pcb;
988 #endif
989 struct unpcb unpcb;
990 struct ddpcb ddpcb;
991 int len;
992 char dname[32];
993 char lbuf[512], fbuf[512];
994 PREFIX(i);
995
996 /* fill in socket */
997 if (!KVM_READ(sock, &so, sizeof(struct socket))) {
998 dprintf("can't read sock at %p", sock);
999 goto bad;
1000 }
1001
1002 /* fill in protosw entry */
1003 if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
1004 dprintf("can't read protosw at %p", so.so_proto);
1005 goto bad;
1006 }
1007
1008 /* fill in domain */
1009 if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
1010 dprintf("can't read domain at %p", proto.pr_domain);
1011 goto bad;
1012 }
1013
1014 if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
1015 sizeof(dname) - 1)) != sizeof(dname) -1) {
1016 dprintf("can't read domain name at %p", dom.dom_name);
1017 dname[0] = '\0';
1018 }
1019 else
1020 dname[len] = '\0';
1021
1022 if ((u_short)so.so_type > STYPEMAX)
1023 (void)printf("* %s ?%d", dname, so.so_type);
1024 else
1025 (void)printf("* %s %s", dname, stypename[so.so_type]);
1026
1027 /*
1028 * protocol specific formatting
1029 *
1030 * Try to find interesting things to print. For TCP, the interesting
1031 * thing is the address of the tcpcb, for UDP and others, just the
1032 * inpcb (socket pcb). For UNIX domain, its the address of the socket
1033 * pcb and the address of the connected pcb (if connected). Otherwise
1034 * just print the protocol number and address of the socket itself.
1035 * The idea is not to duplicate netstat, but to make available enough
1036 * information for further analysis.
1037 */
1038 fbuf[0] = '\0';
1039 lbuf[0] = '\0';
1040 switch(dom.dom_family) {
1041 case AF_INET:
1042 getinetproto(proto.pr_protocol);
1043 switch (proto.pr_protocol) {
1044 case IPPROTO_TCP:
1045 case IPPROTO_UDP:
1046 if (so.so_pcb == NULL)
1047 break;
1048 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
1049 sizeof(inpcb)) != sizeof(inpcb)) {
1050 dprintf("can't read inpcb at %p", so.so_pcb);
1051 goto bad;
1052 }
1053 inet_addrstr(lbuf, sizeof(lbuf), &inpcb.inp_laddr,
1054 ntohs(inpcb.inp_lport));
1055 inet_addrstr(fbuf, sizeof(fbuf), &inpcb.inp_faddr,
1056 ntohs(inpcb.inp_fport));
1057 break;
1058 default:
1059 break;
1060 }
1061 break;
1062 #ifdef INET6
1063 case AF_INET6:
1064 getinetproto(proto.pr_protocol);
1065 switch (proto.pr_protocol) {
1066 case IPPROTO_TCP:
1067 case IPPROTO_UDP:
1068 if (so.so_pcb == NULL)
1069 break;
1070 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&in6pcb,
1071 sizeof(in6pcb)) != sizeof(in6pcb)) {
1072 dprintf("can't read in6pcb at %p", so.so_pcb);
1073 goto bad;
1074 }
1075 inet6_addrstr(lbuf, sizeof(lbuf), &in6pcb.in6p_laddr,
1076 ntohs(in6pcb.in6p_lport));
1077 inet6_addrstr(fbuf, sizeof(fbuf), &in6pcb.in6p_faddr,
1078 ntohs(in6pcb.in6p_fport));
1079 break;
1080 default:
1081 break;
1082 }
1083 break;
1084 #endif
1085 case AF_LOCAL:
1086 /* print address of pcb and connected pcb */
1087 if (so.so_pcb) {
1088 char shoconn[4], *cp;
1089 void *pcb[2];
1090 size_t p = 0;
1091
1092 pcb[0] = so.so_pcb;
1093
1094 cp = shoconn;
1095 if (!(so.so_state & SS_CANTRCVMORE))
1096 *cp++ = '<';
1097 *cp++ = '-';
1098 if (!(so.so_state & SS_CANTSENDMORE))
1099 *cp++ = '>';
1100 *cp = '\0';
1101 again:
1102 if (kvm_read(kd, (u_long)pcb[p], (char *)&unpcb,
1103 sizeof(struct unpcb)) != sizeof(struct unpcb)){
1104 dprintf("can't read unpcb at %p", so.so_pcb);
1105 goto bad;
1106 }
1107
1108 if (unpcb.unp_addr) {
1109 struct sockaddr_un *sun =
1110 malloc(unpcb.unp_addrlen);
1111 if (sun == NULL)
1112 err(1, "malloc(%zu)",
1113 unpcb.unp_addrlen);
1114 if (kvm_read(kd, (u_long)unpcb.unp_addr,
1115 sun, unpcb.unp_addrlen) !=
1116 (ssize_t)unpcb.unp_addrlen) {
1117 dprintf("can't read sun at %p",
1118 unpcb.unp_addr);
1119 free(sun);
1120 } else {
1121 snprintf(fbuf, sizeof(fbuf), " %s %s %s",
1122 shoconn, sun->sun_path,
1123 p == 0 ? "[creat]" : "[using]");
1124 free(sun);
1125 break;
1126 }
1127 }
1128 if (unpcb.unp_conn) {
1129 if (p == 0) {
1130 pcb[++p] = unpcb.unp_conn;
1131 goto again;
1132 } else
1133 snprintf(fbuf, sizeof(fbuf),
1134 " %p %s %p", pcb[0], shoconn,
1135 pcb[1]);
1136 }
1137 }
1138 break;
1139 case AF_APPLETALK:
1140 getatproto(proto.pr_protocol);
1141 if (so.so_pcb) {
1142 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&ddpcb,
1143 sizeof(ddpcb)) != sizeof(ddpcb)){
1144 dprintf("can't read ddpcb at %p", so.so_pcb);
1145 goto bad;
1146 }
1147 at_addrstr(fbuf, sizeof(fbuf), &ddpcb.ddp_fsat);
1148 at_addrstr(lbuf, sizeof(lbuf), &ddpcb.ddp_lsat);
1149 }
1150 break;
1151 default:
1152 /* print protocol number and socket address */
1153 snprintf(fbuf, sizeof(fbuf), " %d %jx", proto.pr_protocol,
1154 (uintmax_t)(uintptr_t)sock);
1155 break;
1156 }
1157 if (fbuf[0] || lbuf[0])
1158 printf(" %s%s%s", fbuf, (fbuf[0] && lbuf[0]) ? " <-> " : "",
1159 lbuf);
1160 else if (so.so_pcb)
1161 printf(" %jx", (uintmax_t)(uintptr_t)so.so_pcb);
1162 (void)printf("\n");
1163 return;
1164 bad:
1165 (void)printf("* error\n");
1166 }
1167
1168 static void
1169 ptrans(struct file *fp, struct pipe *cpipe, int i)
1170 {
1171 struct pipe cp;
1172
1173 PREFIX(i);
1174
1175 /* fill in pipe */
1176 if (!KVM_READ(cpipe, &cp, sizeof(struct pipe))) {
1177 dprintf("can't read pipe at %p", cpipe);
1178 goto bad;
1179 }
1180
1181 /* pipe descriptor is either read or write, never both */
1182 (void)printf("* pipe %p %s %p %s%s%s", cpipe,
1183 (fp->f_flag & FWRITE) ? "->" : "<-",
1184 cp.pipe_peer,
1185 (fp->f_flag & FWRITE) ? "w" : "r",
1186 (fp->f_flag & FNONBLOCK) ? "n" : "",
1187 (cp.pipe_state & PIPE_ASYNC) ? "a" : "");
1188 (void)printf("\n");
1189 return;
1190 bad:
1191 (void)printf("* error\n");
1192 }
1193
1194 static void
1195 misctrans(struct file *file)
1196 {
1197
1198 PREFIX((int)file->f_type);
1199 pmisc(file, dtypes[file->f_type]);
1200 }
1201
1202 /*
1203 * getinetproto --
1204 * print name of protocol number
1205 */
1206 static void
1207 getinetproto(int number)
1208 {
1209 const char *cp;
1210
1211 switch (number) {
1212 case IPPROTO_IP:
1213 cp = "ip"; break;
1214 case IPPROTO_ICMP:
1215 cp ="icmp"; break;
1216 case IPPROTO_GGP:
1217 cp ="ggp"; break;
1218 case IPPROTO_TCP:
1219 cp ="tcp"; break;
1220 case IPPROTO_EGP:
1221 cp ="egp"; break;
1222 case IPPROTO_PUP:
1223 cp ="pup"; break;
1224 case IPPROTO_UDP:
1225 cp ="udp"; break;
1226 case IPPROTO_IDP:
1227 cp ="idp"; break;
1228 case IPPROTO_RAW:
1229 cp ="raw"; break;
1230 case IPPROTO_ICMPV6:
1231 cp ="icmp6"; break;
1232 default:
1233 (void)printf(" %d", number);
1234 return;
1235 }
1236 (void)printf(" %s", cp);
1237 }
1238
1239 /*
1240 * getatproto --
1241 * print name of protocol number
1242 */
1243 static void
1244 getatproto(int number)
1245 {
1246 const char *cp;
1247
1248 switch (number) {
1249 case ATPROTO_DDP:
1250 cp = "ddp"; break;
1251 case ATPROTO_AARP:
1252 cp ="aarp"; break;
1253 default:
1254 (void)printf(" %d", number);
1255 return;
1256 }
1257 (void)printf(" %s", cp);
1258 }
1259
1260 static int
1261 getfname(const char *filename)
1262 {
1263 struct stat statbuf;
1264 DEVS *cur;
1265
1266 if (stat(filename, &statbuf)) {
1267 warn("stat(%s)", filename);
1268 return 0;
1269 }
1270 if ((cur = malloc(sizeof(*cur))) == NULL) {
1271 err(1, "malloc(%zu)", sizeof(*cur));
1272 }
1273 cur->next = devs;
1274 devs = cur;
1275
1276 cur->ino = statbuf.st_ino;
1277 cur->fsid = statbuf.st_dev & 0xffff;
1278 cur->name = filename;
1279 return 1;
1280 }
1281
1282 mode_t
1283 getftype(enum vtype v_type)
1284 {
1285 mode_t ftype;
1286
1287 switch (v_type) {
1288 case VREG:
1289 ftype = S_IFREG;
1290 break;
1291 case VDIR:
1292 ftype = S_IFDIR;
1293 break;
1294 case VBLK:
1295 ftype = S_IFBLK;
1296 break;
1297 case VCHR:
1298 ftype = S_IFCHR;
1299 break;
1300 case VLNK:
1301 ftype = S_IFLNK;
1302 break;
1303 case VSOCK:
1304 ftype = S_IFSOCK;
1305 break;
1306 case VFIFO:
1307 ftype = S_IFIFO;
1308 break;
1309 default:
1310 ftype = 0;
1311 break;
1312 };
1313
1314 return ftype;
1315 }
1316
1317 static void
1318 usage(void)
1319 {
1320 (void)fprintf(stderr, "Usage: %s [-fnv] [-p pid] [-u user] "
1321 "[-N system] [-M core] [file ...]\n", getprogname());
1322 exit(1);
1323 }
1324