1 /** $MirOS: src/usr.bin/fstat/fstat.c,v 1.3 2005/11/23 17:36:13 tg Exp $ */
2 /* $OpenBSD: fstat.c,v 1.52 2005/07/04 01:54:09 djm Exp $ */
3
4 /*-
5 * Copyright (c) 1988, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 #ifndef lint
34 static char copyright[] =
35 "@(#) Copyright (c) 1988, 1993\n\
36 The Regents of the University of California. All rights reserved.\n";
37 #endif /* not lint */
38
39 #ifndef lint
40 /*static char sccsid[] = "from: @(#)fstat.c 8.1 (Berkeley) 6/6/93";*/
41 static char *rcsid = "$OpenBSD: fstat.c,v 1.52 2005/07/04 01:54:09 djm Exp $";
42 #endif /* not lint */
43
44 #include <sys/param.h>
45 #include <sys/time.h>
46 #include <sys/proc.h>
47 #include <sys/user.h>
48 #include <sys/stat.h>
49 #include <sys/vnode.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/domain.h>
53 #include <sys/protosw.h>
54 #include <sys/event.h>
55 #include <sys/eventvar.h>
56 #include <sys/unpcb.h>
57 #include <sys/sysctl.h>
58 #include <sys/filedesc.h>
59 #define _KERNEL
60 #include <sys/mount.h>
61 #include <crypto/cryptodev.h>
62 #include <dev/systrace.h>
63 #include <sys/file.h>
64 #include <ufs/ufs/quota.h>
65 #include <ufs/ufs/inode.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
74 #include <net/route.h>
75 #include <netinet/in.h>
76 #include <netinet/in_systm.h>
77 #include <netinet/ip.h>
78 #include <netinet/in_pcb.h>
79
80 #ifdef INET6
81 #include <netinet/ip6.h>
82 #include <netinet6/ip6_var.h>
83 #endif
84
85 #include <netdb.h>
86 #include <arpa/inet.h>
87
88 #include <sys/pipe.h>
89
90 #include <ctype.h>
91 #include <errno.h>
92 #include <kvm.h>
93 #include <limits.h>
94 #include <nlist.h>
95 #include <paths.h>
96 #include <pwd.h>
97 #include <stdio.h>
98 #include <stdlib.h>
99 #include <string.h>
100 #include <unistd.h>
101 #include <netdb.h>
102 #include <err.h>
103 #include "fstat.h"
104
105 #define TEXT -1
106 #define CDIR -2
107 #define RDIR -3
108 #define TRACE -4
109
110 typedef struct devs {
111 struct devs *next;
112 long fsid;
113 ino_t ino;
114 char *name;
115 } DEVS;
116 DEVS *devs;
117
118 int fsflg; /* show files on same filesystem as file(s) argument */
119 int pflg; /* show files open by a particular pid */
120 int uflg; /* show files open by a particular (effective) user */
121 int checkfile; /* true if restricting to particular files or filesystems */
122 int nflg; /* (numerical) display f.s. and rdev as dev_t */
123 int oflg; /* display file offset */
124 int vflg; /* display errors in locating kernel data objects etc... */
125
126 struct file **ofiles; /* buffer of pointers to file structures */
127 int maxfiles;
128 #define ALLOC_OFILES(d) \
129 if ((d) > maxfiles) { \
130 free(ofiles); \
131 ofiles = malloc((d) * sizeof(struct file *)); \
132 if (ofiles == NULL) \
133 err(1, "malloc"); \
134 maxfiles = (d); \
135 }
136
137 /*
138 * a kvm_read that returns true if everything is read
139 */
140 #define KVM_READ(kaddr, paddr, len) \
141 (kvm_read(kd, (u_long)(kaddr), (void *)(paddr), (len)) == (len))
142
143 kvm_t *kd;
144
145 int ufs_filestat(struct vnode *, struct filestat *);
146 int ext2fs_filestat(struct vnode *, struct filestat *);
147 int isofs_filestat(struct vnode *, struct filestat *);
148 int msdos_filestat(struct vnode *, struct filestat *);
149 int nfs_filestat(struct vnode *, struct filestat *);
150 void dofiles(struct kinfo_proc2 *);
151 void getinetproto(int);
152 void socktrans(struct socket *, int);
153 void usage(void);
154 void vtrans(struct vnode *, int, int, off_t);
155 int getfname(char *);
156 void pipetrans(struct pipe *, int);
157 void kqueuetrans(struct kqueue *, int);
158 void cryptotrans(void *, int);
159 void systracetrans(struct fsystrace *, int);
160 char *getmnton(struct mount *);
161 const char *inet6_addrstr(struct in6_addr *);
162
163 int
main(int argc,char * argv[])164 main(int argc, char *argv[])
165 {
166 extern char *optarg;
167 extern int optind;
168 struct passwd *passwd;
169 struct kinfo_proc2 *p, *plast;
170 int arg, ch, what;
171 char *memf, *nlistf;
172 char buf[_POSIX2_LINE_MAX];
173 int cnt;
174 gid_t gid;
175
176 arg = 0;
177 what = KERN_PROC_ALL;
178 nlistf = memf = NULL;
179 oflg = 0;
180 while ((ch = getopt(argc, argv, "fnop:u:vN:M:")) != -1)
181 switch((char)ch) {
182 case 'f':
183 fsflg = 1;
184 break;
185 case 'M':
186 memf = optarg;
187 break;
188 case 'N':
189 nlistf = optarg;
190 break;
191 case 'n':
192 nflg = 1;
193 break;
194 case 'o':
195 oflg = 1;
196 break;
197 case 'p':
198 if (pflg++)
199 usage();
200 if (!isdigit(*optarg)) {
201 warnx( "-p requires a process id");
202 usage();
203 }
204 what = KERN_PROC_PID;
205 arg = atoi(optarg);
206 break;
207 case 'u':
208 if (uflg++)
209 usage();
210 if (!(passwd = getpwnam(optarg)))
211 errx(1, "%s: unknown uid", optarg);
212 what = KERN_PROC_UID;
213 arg = passwd->pw_uid;
214 break;
215 case 'v':
216 vflg = 1;
217 break;
218 default:
219 usage();
220 }
221
222 /*
223 * Discard setgid privileges if not the running kernel so that bad
224 * guys can't print interesting stuff from kernel memory.
225 */
226 gid = getgid();
227 if (nlistf != NULL || memf != NULL)
228 if (setresgid(gid, gid, gid) == -1)
229 err(1, "setresgid");
230
231 if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL)
232 errx(1, "%s", buf);
233
234 if (nlistf == NULL && memf == NULL)
235 if (setresgid(gid, gid, gid) == -1)
236 err(1, "setresgid");
237
238 if (*(argv += optind)) {
239 for (; *argv; ++argv) {
240 if (getfname(*argv))
241 checkfile = 1;
242 }
243 if (!checkfile) /* file(s) specified, but none accessible */
244 exit(1);
245 }
246
247 ALLOC_OFILES(256); /* reserve space for file pointers */
248
249 if (fsflg && !checkfile) {
250 /* -f with no files means use wd */
251 if (getfname(".") == 0)
252 exit(1);
253 checkfile = 1;
254 }
255
256 if ((p = kvm_getproc2(kd, what, arg, sizeof(*p), &cnt)) == NULL)
257 errx(1, "%s", kvm_geterr(kd));
258 if (nflg)
259 printf("%s",
260 "USER CMD PID FD DEV INUM MODE R/W DV|SZ");
261 else
262 printf("%s",
263 "USER CMD PID FD MOUNT INUM MODE R/W DV|SZ");
264 if (oflg)
265 printf("%s", ":OFFSET ");
266 if (checkfile && fsflg == 0)
267 printf(" NAME\n");
268 else
269 putchar('\n');
270
271 for (plast = &p[cnt]; p < plast; ++p) {
272 if (p->p_stat == SZOMB)
273 continue;
274 dofiles(p);
275 }
276 exit(0);
277 }
278
279 char *Uname, *Comm;
280 pid_t Pid;
281
282 #define PREFIX(i) do { \
283 printf("%-8.8s %-10s %5ld", Uname, Comm, (long)Pid); \
284 switch(i) { \
285 case TEXT: \
286 printf(" text"); \
287 break; \
288 case CDIR: \
289 printf(" wd"); \
290 break; \
291 case RDIR: \
292 printf(" root"); \
293 break; \
294 case TRACE: \
295 printf(" tr"); \
296 break; \
297 default: \
298 printf(" %4d", i); \
299 break; \
300 } \
301 } while (0)
302
303 /*
304 * print open files attributed to this process
305 */
306 void
dofiles(struct kinfo_proc2 * kp)307 dofiles(struct kinfo_proc2 *kp)
308 {
309 int i;
310 struct file file;
311 struct filedesc0 filed0;
312 #define filed filed0.fd_fd
313
314 Uname = user_from_uid(kp->p_uid, 0);
315 Pid = kp->p_pid;
316 Comm = kp->p_comm;
317
318 if (kp->p_fd == 0)
319 return;
320 if (!KVM_READ(kp->p_fd, &filed0, sizeof (filed0))) {
321 dprintf("can't read filedesc at %p for pid %ld",
322 (void *)(u_long)kp->p_fd, (long)Pid);
323 return;
324 }
325 if (filed.fd_nfiles < 0 || filed.fd_lastfile >= filed.fd_nfiles ||
326 filed.fd_freefile > filed.fd_lastfile + 1) {
327 dprintf("filedesc corrupted at %p for pid %ld",
328 (void *)(u_long)kp->p_fd, (long)Pid);
329 return;
330 }
331 /*
332 * root directory vnode, if one
333 */
334 if (filed.fd_rdir)
335 vtrans(filed.fd_rdir, RDIR, FREAD, 0);
336 /*
337 * current working directory vnode
338 */
339 vtrans(filed.fd_cdir, CDIR, FREAD, 0);
340 /*
341 * ktrace vnode, if one
342 */
343 if (kp->p_tracep)
344 vtrans((struct vnode *)(u_long)kp->p_tracep, TRACE, FREAD|FWRITE, 0);
345 /*
346 * open files
347 */
348 #define FPSIZE (sizeof (struct file *))
349 ALLOC_OFILES(filed.fd_lastfile+1);
350 if (filed.fd_nfiles > NDFILE) {
351 if (!KVM_READ(filed.fd_ofiles, ofiles,
352 (filed.fd_lastfile+1) * FPSIZE)) {
353 dprintf("can't read file structures at %p for pid %ld",
354 filed.fd_ofiles, (long)Pid);
355 return;
356 }
357 } else
358 memmove(ofiles, filed0.fd_dfiles, (filed.fd_lastfile+1) * FPSIZE);
359 for (i = 0; i <= filed.fd_lastfile; i++) {
360 if (ofiles[i] == NULL)
361 continue;
362 if (!KVM_READ(ofiles[i], &file, sizeof (struct file))) {
363 dprintf("can't read file %d at %p for pid %ld",
364 i, ofiles[i], (long)Pid);
365 continue;
366 }
367 if (file.f_type == DTYPE_VNODE)
368 vtrans((struct vnode *)file.f_data, i, file.f_flag,
369 file.f_offset);
370 else if (file.f_type == DTYPE_SOCKET) {
371 if (checkfile == 0)
372 socktrans((struct socket *)file.f_data, i);
373 } else if (file.f_type == DTYPE_PIPE) {
374 if (checkfile == 0)
375 pipetrans((struct pipe *)file.f_data, i);
376 } else if (file.f_type == DTYPE_KQUEUE) {
377 if (checkfile == 0)
378 kqueuetrans((struct kqueue *)file.f_data, i);
379 } else if (file.f_type == DTYPE_CRYPTO) {
380 if (checkfile == 0)
381 cryptotrans(file.f_data, i);
382 } else if (file.f_type == DTYPE_SYSTRACE) {
383 if (checkfile == 0)
384 systracetrans((struct fsystrace *)file.f_data, i);
385 } else {
386 dprintf("unknown file type %d for file %d of pid %ld",
387 file.f_type, i, (long)Pid);
388 }
389 }
390 }
391
392 void
vtrans(struct vnode * vp,int i,int flag,off_t offset)393 vtrans(struct vnode *vp, int i, int flag, off_t offset)
394 {
395 struct vnode vn;
396 struct filestat fst;
397 char rw[3], mode[17];
398 char *badtype = NULL, *filename;
399
400 filename = badtype = NULL;
401 if (!KVM_READ(vp, &vn, sizeof (struct vnode))) {
402 dprintf("can't read vnode at %p for pid %ld", vp, (long)Pid);
403 return;
404 }
405 if (vn.v_type == VNON || vn.v_tag == VT_NON)
406 badtype = "none";
407 else if (vn.v_type == VBAD)
408 badtype = "bad";
409 else
410 switch (vn.v_tag) {
411 case VT_UFS:
412 case VT_MFS:
413 if (!ufs_filestat(&vn, &fst))
414 badtype = "error";
415 break;
416 case VT_NFS:
417 if (!nfs_filestat(&vn, &fst))
418 badtype = "error";
419 break;
420 case VT_EXT2FS:
421 if (!ext2fs_filestat(&vn, &fst))
422 badtype = "error";
423 break;
424 case VT_ISOFS:
425 if (!isofs_filestat(&vn, &fst))
426 badtype = "error";
427 break;
428 case VT_MSDOSFS:
429 if (!msdos_filestat(&vn, &fst))
430 badtype = "error";
431 break;
432 default: {
433 static char unknown[30];
434 snprintf(badtype = unknown, sizeof unknown,
435 "?(%x)", vn.v_tag);
436 break;
437 }
438 }
439 if (checkfile) {
440 int fsmatch = 0;
441 DEVS *d;
442
443 if (badtype)
444 return;
445 for (d = devs; d != NULL; d = d->next)
446 if (d->fsid == fst.fsid) {
447 fsmatch = 1;
448 if (d->ino == fst.fileid) {
449 filename = d->name;
450 break;
451 }
452 }
453 if (fsmatch == 0 || (filename == NULL && fsflg == 0))
454 return;
455 }
456 PREFIX(i);
457 if (badtype) {
458 (void)printf(" - - %10s -\n", badtype);
459 return;
460 }
461 if (nflg)
462 (void)printf(" %2ld,%-2ld", (long)major(fst.fsid),
463 (long)minor(fst.fsid));
464 else
465 (void)printf(" %-8s", getmnton(vn.v_mount));
466 if (nflg)
467 (void)snprintf(mode, sizeof mode, "%o", fst.mode);
468 else
469 strmode(fst.mode, mode);
470 (void)printf(" %6ld %11s", fst.fileid, mode);
471 rw[0] = '\0';
472 if (flag & FREAD)
473 strlcat(rw, "r", sizeof rw);
474 if (flag & FWRITE)
475 strlcat(rw, "w", sizeof rw);
476 printf(" %2s", rw);
477 switch (vn.v_type) {
478 case VBLK:
479 case VCHR: {
480 char *name;
481
482 if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ?
483 S_IFCHR : S_IFBLK)) == NULL))
484 printf(" %2d,%-3d", major(fst.rdev), minor(fst.rdev));
485 else
486 printf(" %7s", name);
487 if (oflg)
488 printf(" ");
489 break;
490 }
491 default:
492 printf(" %8lld", (long long)fst.size);
493 if (oflg)
494 printf(":%-8lld", (long long)offset);
495 }
496 if (filename && !fsflg)
497 printf(" %s", filename);
498 putchar('\n');
499 }
500
501 int
ufs_filestat(struct vnode * vp,struct filestat * fsp)502 ufs_filestat(struct vnode *vp, struct filestat *fsp)
503 {
504 struct inode inode;
505
506 if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
507 dprintf("can't read inode at %p for pid %ld",
508 VTOI(vp), (long)Pid);
509 return 0;
510 }
511 fsp->fsid = inode.i_dev & 0xffff;
512 fsp->fileid = (long)inode.i_number;
513 fsp->mode = inode.i_ffs_mode;
514 fsp->size = inode.i_ffs_size;
515 fsp->rdev = inode.i_ffs_rdev;
516
517 return 1;
518 }
519
520 int
ext2fs_filestat(struct vnode * vp,struct filestat * fsp)521 ext2fs_filestat(struct vnode *vp, struct filestat *fsp)
522 {
523 struct inode inode;
524
525 if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
526 dprintf("can't read inode at %p for pid %ld",
527 VTOI(vp), (long)Pid);
528 return 0;
529 }
530 fsp->fsid = inode.i_dev & 0xffff;
531 fsp->fileid = (long)inode.i_number;
532 fsp->mode = inode.i_e2fs_mode;
533 fsp->size = inode.i_e2fs_size;
534 fsp->rdev = 0; /* XXX */
535
536 return 1;
537 }
538
539 int
msdos_filestat(struct vnode * vp,struct filestat * fsp)540 msdos_filestat(struct vnode *vp, struct filestat *fsp)
541 {
542 #if 0
543 struct inode inode;
544
545 if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
546 dprintf("can't read inode at %p for pid %ld",
547 VTOI(vp), (long)Pid);
548 return 0;
549 }
550 fsp->fsid = inode.i_dev & 0xffff;
551 fsp->fileid = (long)inode.i_number;
552 fsp->mode = inode.i_e2fs_mode;
553 fsp->size = inode.i_e2fs_size;
554 fsp->rdev = 0; /* XXX */
555 #endif
556
557 return 1;
558 }
559
560 int
nfs_filestat(struct vnode * vp,struct filestat * fsp)561 nfs_filestat(struct vnode *vp, struct filestat *fsp)
562 {
563 struct nfsnode nfsnode;
564 mode_t mode;
565
566 if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
567 dprintf("can't read nfsnode at %p for pid %ld",
568 VTONFS(vp), (long)Pid);
569 return 0;
570 }
571 fsp->fsid = nfsnode.n_vattr.va_fsid;
572 fsp->fileid = nfsnode.n_vattr.va_fileid;
573 fsp->size = nfsnode.n_size;
574 fsp->rdev = nfsnode.n_vattr.va_rdev;
575 mode = (mode_t)nfsnode.n_vattr.va_mode;
576 switch (vp->v_type) {
577 case VREG:
578 mode |= S_IFREG;
579 break;
580 case VDIR:
581 mode |= S_IFDIR;
582 break;
583 case VBLK:
584 mode |= S_IFBLK;
585 break;
586 case VCHR:
587 mode |= S_IFCHR;
588 break;
589 case VLNK:
590 mode |= S_IFLNK;
591 break;
592 case VSOCK:
593 mode |= S_IFSOCK;
594 break;
595 case VFIFO:
596 mode |= S_IFIFO;
597 break;
598 default:
599 break;
600 }
601 fsp->mode = mode;
602
603 return 1;
604 }
605
606 char *
getmnton(struct mount * m)607 getmnton(struct mount *m)
608 {
609 static struct mount mount;
610 static struct mtab {
611 struct mtab *next;
612 struct mount *m;
613 char mntonname[MNAMELEN];
614 } *mhead = NULL;
615 struct mtab *mt;
616
617 for (mt = mhead; mt != NULL; mt = mt->next)
618 if (m == mt->m)
619 return (mt->mntonname);
620 if (!KVM_READ(m, &mount, sizeof(struct mount))) {
621 warn("can't read mount table at %p", m);
622 return (NULL);
623 }
624 if ((mt = malloc(sizeof (struct mtab))) == NULL)
625 err(1, "malloc");
626 mt->m = m;
627 memmove(&mt->mntonname[0], &mount.mnt_stat.f_mntonname[0], MNAMELEN);
628 mt->next = mhead;
629 mhead = mt;
630 return (mt->mntonname);
631 }
632
633 void
pipetrans(struct pipe * pipe,int i)634 pipetrans(struct pipe *pipe, int i)
635 {
636 struct pipe pi;
637 void *maxaddr;
638
639 PREFIX(i);
640
641 printf(" ");
642
643 /* fill in socket */
644 if (!KVM_READ(pipe, &pi, sizeof(struct pipe))) {
645 dprintf("can't read pipe at %p", pipe);
646 goto bad;
647 }
648
649 /*
650 * We don't have enough space to fit both peer and own address, so
651 * we select the higher address so both ends of the pipe have the
652 * same visible addr. (it's the higher address because when the other
653 * end closes, it becomes 0)
654 */
655 maxaddr = MAX(pipe, pi.pipe_peer);
656
657 printf("pipe %p state: %s%s%s\n", maxaddr,
658 (pi.pipe_state & PIPE_WANTR) ? "R" : "",
659 (pi.pipe_state & PIPE_WANTW) ? "W" : "",
660 (pi.pipe_state & PIPE_EOF) ? "E" : "");
661 return;
662 bad:
663 printf("* error\n");
664 }
665
666 void
kqueuetrans(struct kqueue * kq,int i)667 kqueuetrans(struct kqueue *kq, int i)
668 {
669 struct kqueue kqi;
670
671 PREFIX(i);
672
673 printf(" ");
674
675 /* fill it in */
676 if (!KVM_READ(kq, &kqi, sizeof(struct kqueue))) {
677 dprintf("can't read kqueue at %p", kq);
678 goto bad;
679 }
680
681 printf("kqueue %p %d state: %s%s\n", kq, kqi.kq_count,
682 (kqi.kq_state & KQ_SEL) ? "S" : "",
683 (kqi.kq_state & KQ_SLEEP) ? "W" : "");
684 return;
685 bad:
686 printf("* error\n");
687 }
688
689 void
cryptotrans(void * f,int i)690 cryptotrans(void *f, int i)
691 {
692 PREFIX(i);
693
694 printf(" ");
695
696 printf("crypto %p\n", f);
697 }
698
699 void
systracetrans(struct fsystrace * f,int i)700 systracetrans(struct fsystrace *f, int i)
701 {
702 struct fsystrace fi;
703
704 PREFIX(i);
705
706 printf(" ");
707
708 /* fill it in */
709 if (!KVM_READ(f, &fi, sizeof(fi))) {
710 dprintf("can't read fsystrace at %p", f);
711 goto bad;
712 }
713
714 printf("systrace %p npol %d\n", f, fi.npolicies);
715 return;
716 bad:
717 printf("* error\n");
718 }
719
720 #ifdef INET6
721 const char *
inet6_addrstr(struct in6_addr * p)722 inet6_addrstr(struct in6_addr *p)
723 {
724 struct sockaddr_in6 sin6;
725 static char hbuf[NI_MAXHOST];
726 const int niflags = NI_NUMERICHOST;
727
728 memset(&sin6, 0, sizeof(sin6));
729 sin6.sin6_family = AF_INET6;
730 sin6.sin6_len = sizeof(struct sockaddr_in6);
731 sin6.sin6_addr = *p;
732 if (IN6_IS_ADDR_LINKLOCAL(p) &&
733 *(u_int16_t *)&sin6.sin6_addr.s6_addr[2] != 0) {
734 sin6.sin6_scope_id =
735 ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);
736 sin6.sin6_addr.s6_addr[2] = sin6.sin6_addr.s6_addr[3] = 0;
737 }
738
739 if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
740 hbuf, sizeof(hbuf), NULL, 0, niflags))
741 return "invalid";
742
743 return hbuf;
744 }
745 #endif
746
747 void
socktrans(struct socket * sock,int i)748 socktrans(struct socket *sock, int i)
749 {
750 static char *stypename[] = {
751 "unused", /* 0 */
752 "stream", /* 1 */
753 "dgram", /* 2 */
754 "raw", /* 3 */
755 "rdm", /* 4 */
756 "seqpak" /* 5 */
757 };
758 #define STYPEMAX 5
759 struct socket so;
760 struct protosw proto;
761 struct domain dom;
762 struct inpcb inpcb;
763 struct unpcb unpcb;
764 int len;
765 char dname[32];
766 #ifdef INET6
767 char xaddrbuf[NI_MAXHOST + 2];
768 #endif
769
770 PREFIX(i);
771
772 /* fill in socket */
773 if (!KVM_READ(sock, &so, sizeof(struct socket))) {
774 dprintf("can't read sock at %p", sock);
775 goto bad;
776 }
777
778 /* fill in protosw entry */
779 if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
780 dprintf("can't read protosw at %p", so.so_proto);
781 goto bad;
782 }
783
784 /* fill in domain */
785 if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
786 dprintf("can't read domain at %p", proto.pr_domain);
787 goto bad;
788 }
789
790 if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
791 sizeof(dname) - 1)) != sizeof(dname) -1) {
792 dprintf("can't read domain name at %p", dom.dom_name);
793 dname[0] = '\0';
794 } else
795 dname[len] = '\0';
796
797 if ((u_short)so.so_type > STYPEMAX)
798 printf("* %s ?%d", dname, so.so_type);
799 else
800 printf("* %s %s", dname, stypename[so.so_type]);
801
802 /*
803 * protocol specific formatting
804 *
805 * Try to find interesting things to print. For tcp, the interesting
806 * thing is the address of the tcpcb, for udp and others, just the
807 * inpcb (socket pcb). For unix domain, its the address of the socket
808 * pcb and the address of the connected pcb (if connected). Otherwise
809 * just print the protocol number and address of the socket itself.
810 * The idea is not to duplicate netstat, but to make available enough
811 * information for further analysis.
812 */
813 switch(dom.dom_family) {
814 case AF_INET:
815 getinetproto(proto.pr_protocol);
816 if (proto.pr_protocol == IPPROTO_TCP) {
817 if (so.so_pcb == NULL)
818 break;
819 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
820 sizeof(struct inpcb)) != sizeof(struct inpcb)) {
821 dprintf("can't read inpcb at %p", so.so_pcb);
822 goto bad;
823 }
824 printf(" %p", inpcb.inp_ppcb);
825 printf(" %s:%d",
826 inpcb.inp_laddr.s_addr == INADDR_ANY ? "*" :
827 inet_ntoa(inpcb.inp_laddr),
828 ntohs(inpcb.inp_lport));
829 if (inpcb.inp_fport) {
830 if (so.so_state & SS_CONNECTOUT)
831 printf(" --> ");
832 else
833 printf(" <-- ");
834 printf("%s:%d",
835 inpcb.inp_faddr.s_addr == INADDR_ANY ? "*" :
836 inet_ntoa(inpcb.inp_faddr),
837 ntohs(inpcb.inp_fport));
838 }
839 } else if (proto.pr_protocol == IPPROTO_UDP) {
840 if (so.so_pcb == NULL)
841 break;
842 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
843 sizeof(struct inpcb)) != sizeof(struct inpcb)) {
844 dprintf("can't read inpcb at %p", so.so_pcb);
845 goto bad;
846 }
847 printf(" %s:%d",
848 inpcb.inp_laddr.s_addr == INADDR_ANY ? "*" :
849 inet_ntoa(inpcb.inp_laddr),
850 ntohs(inpcb.inp_lport));
851 if (inpcb.inp_fport)
852 printf(" <-> %s:%d",
853 inpcb.inp_faddr.s_addr == INADDR_ANY ? "*" :
854 inet_ntoa(inpcb.inp_faddr),
855 ntohs(inpcb.inp_fport));
856 } else if (so.so_pcb)
857 printf(" %p", so.so_pcb);
858 break;
859 #ifdef INET6
860 case AF_INET6:
861 getinetproto(proto.pr_protocol);
862 if (proto.pr_protocol == IPPROTO_TCP) {
863 if (so.so_pcb == NULL)
864 break;
865 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
866 sizeof(struct inpcb)) != sizeof(struct inpcb)) {
867 dprintf("can't read inpcb at %p", so.so_pcb);
868 goto bad;
869 }
870 printf(" %p", inpcb.inp_ppcb);
871 snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]",
872 inet6_addrstr(&inpcb.inp_laddr6));
873 printf(" %s:%d",
874 IN6_IS_ADDR_UNSPECIFIED(&inpcb.inp_laddr6) ? "*" :
875 xaddrbuf,
876 ntohs(inpcb.inp_lport));
877 if (inpcb.inp_fport) {
878 if (so.so_state & SS_CONNECTOUT)
879 printf(" --> ");
880 else
881 printf(" <-- ");
882 snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]",
883 inet6_addrstr(&inpcb.inp_faddr6));
884 printf("%s:%d",
885 IN6_IS_ADDR_UNSPECIFIED(&inpcb.inp_faddr6) ? "*" :
886 xaddrbuf,
887 ntohs(inpcb.inp_fport));
888 }
889 } else if (proto.pr_protocol == IPPROTO_UDP) {
890 if (so.so_pcb == NULL)
891 break;
892 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
893 sizeof(struct inpcb)) != sizeof(struct inpcb)) {
894 dprintf("can't read inpcb at %p", so.so_pcb);
895 goto bad;
896 }
897 snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]",
898 inet6_addrstr(&inpcb.inp_laddr6));
899 printf(" %s:%d",
900 IN6_IS_ADDR_UNSPECIFIED(&inpcb.inp_laddr6) ? "*" :
901 xaddrbuf,
902 ntohs(inpcb.inp_lport));
903 if (inpcb.inp_fport) {
904 snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]",
905 inet6_addrstr(&inpcb.inp_faddr6));
906 printf(" <-> %s:%d",
907 IN6_IS_ADDR_UNSPECIFIED(&inpcb.inp_faddr6) ? "*" :
908 xaddrbuf,
909 ntohs(inpcb.inp_fport));
910 }
911 } else if (so.so_pcb)
912 printf(" %p", so.so_pcb);
913 break;
914 #endif
915 case AF_UNIX:
916 /* print address of pcb and connected pcb */
917 if (so.so_pcb) {
918 printf(" %p", so.so_pcb);
919 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb,
920 sizeof(struct unpcb)) != sizeof(struct unpcb)){
921 dprintf("can't read unpcb at %p", so.so_pcb);
922 goto bad;
923 }
924 if (unpcb.unp_conn) {
925 char shoconn[4], *cp;
926
927 cp = shoconn;
928 if (!(so.so_state & SS_CANTRCVMORE))
929 *cp++ = '<';
930 *cp++ = '-';
931 if (!(so.so_state & SS_CANTSENDMORE))
932 *cp++ = '>';
933 *cp = '\0';
934 printf(" %s %p", shoconn,
935 unpcb.unp_conn);
936 }
937 }
938 break;
939 default:
940 /* print protocol number and socket address */
941 printf(" %d %p", proto.pr_protocol, sock);
942 }
943 printf("\n");
944 return;
945 bad:
946 printf("* error\n");
947 }
948
949 /*
950 * getinetproto --
951 * print name of protocol number
952 */
953 void
getinetproto(number)954 getinetproto(number)
955 int number;
956 {
957 static int isopen;
958 struct protoent *pe;
959
960 if (!isopen)
961 setprotoent(++isopen);
962 if ((pe = getprotobynumber(number)) != NULL)
963 printf(" %s", pe->p_name);
964 else
965 printf(" %d", number);
966 }
967
968 int
getfname(char * filename)969 getfname(char *filename)
970 {
971 struct stat statbuf;
972 DEVS *cur;
973
974 if (stat(filename, &statbuf)) {
975 warn("%s", filename);
976 return(0);
977 }
978 if ((cur = malloc(sizeof(DEVS))) == NULL)
979 err(1, "malloc");
980 cur->next = devs;
981 devs = cur;
982
983 cur->ino = statbuf.st_ino;
984 cur->fsid = statbuf.st_dev & 0xffff;
985 cur->name = filename;
986 return(1);
987 }
988
989 void
usage(void)990 usage(void)
991 {
992 fprintf(stderr, "usage: fstat [-fnov] [-M core] [-N system] "
993 "[-p pid] [-u user] [file ...]\n");
994 exit(1);
995 }
996