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