pstat.c revision 1.84 1 /* $NetBSD: pstat.c,v 1.84 2003/12/20 11:22:25 mrg Exp $ */
2
3 /*-
4 * Copyright (c) 1980, 1991, 1993, 1994
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) 1980, 1991, 1993, 1994\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[] = "@(#)pstat.c 8.16 (Berkeley) 5/9/95";
41 #else
42 __RCSID("$NetBSD: pstat.c,v 1.84 2003/12/20 11:22:25 mrg Exp $");
43 #endif
44 #endif /* not lint */
45
46 #include <sys/param.h>
47 #include <sys/time.h>
48 #include <sys/vnode.h>
49 #include <sys/ucred.h>
50 #define _KERNEL
51 #include <sys/file.h>
52 #include <ufs/ufs/inode.h>
53 #define NFS
54 #include <sys/mount.h>
55 #undef NFS
56 #include <sys/uio.h>
57 #include <sys/namei.h>
58 #include <miscfs/genfs/layer.h>
59 #include <miscfs/union/union.h>
60 #undef _KERNEL
61 #include <sys/stat.h>
62 #include <nfs/nfsproto.h>
63 #include <nfs/rpcv2.h>
64 #include <nfs/nfs.h>
65 #include <nfs/nfsnode.h>
66 #include <sys/ioctl.h>
67 #include <sys/tty.h>
68 #include <sys/conf.h>
69 #include <sys/device.h>
70
71 #include <sys/sysctl.h>
72
73 #include <err.h>
74 #include <kvm.h>
75 #include <limits.h>
76 #include <nlist.h>
77 #include <stdio.h>
78 #include <stdlib.h>
79 #include <string.h>
80 #include <unistd.h>
81
82 #include "swapctl.h"
83
84 struct nlist nl[] = {
85 #define V_MOUNTLIST 0
86 { "_mountlist" }, /* address of head of mount list. */
87 #define V_NUMV 1
88 { "_numvnodes" },
89 #define FNL_NFILE 2
90 { "_nfiles" },
91 #define FNL_MAXFILE 3
92 { "_maxfiles" },
93 #define TTY_NTTY 4
94 { "_tty_count" },
95 #define TTY_TTYLIST 5
96 { "_ttylist" },
97 #define NLMANDATORY TTY_TTYLIST /* names up to here are mandatory */
98 { "" }
99 };
100
101 int usenumflag;
102 int totalflag;
103 int kflag;
104 int hflag;
105 char *nlistf = NULL;
106 char *memf = NULL;
107 kvm_t *kd;
108
109 static const struct {
110 u_int m_flag;
111 u_int m_visible;
112 const char *m_name;
113 } mnt_flags[] = {
114 __MNT_FLAGS
115 };
116
117 struct flagbit_desc {
118 u_int fd_flags;
119 char fd_mark;
120 };
121
122 #define SVAR(var) __STRING(var) /* to force expansion */
123 #define KGET(idx, var) \
124 KGET1(idx, &var, sizeof(var), SVAR(var))
125 #define KGET1(idx, p, s, msg) \
126 KGET2(nl[idx].n_value, p, s, msg)
127 #define KGET2(addr, p, s, msg) do { \
128 if (kvm_read(kd, (u_long)(addr), p, s) != s) \
129 warnx("cannot read %s: %s", msg, kvm_geterr(kd)); \
130 } while (/* CONSTCOND */0)
131 #define KGETRET(addr, p, s, msg) do { \
132 if (kvm_read(kd, (u_long)(addr), p, s) != s) { \
133 warnx("cannot read %s: %s", msg, kvm_geterr(kd)); \
134 return (0); \
135 } \
136 } while (/* CONSTCOND */0)
137
138 #if 1 /* This is copied from vmstat/vmstat.c */
139 /*
140 * Print single word. `ovflow' is number of characters didn't fit
141 * on the last word. `fmt' is a format string to print this word.
142 * It must contain asterisk for field width. `width' is a width
143 * occupied by this word. `fixed' is a number of constant chars in
144 * `fmt'. `val' is a value to be printed using format string `fmt'.
145 */
146 #define PRWORD(ovflw, fmt, width, fixed, val) do { \
147 (ovflw) += printf((fmt), \
148 (width) - (fixed) - (ovflw) > 0 ? \
149 (width) - (fixed) - (ovflw) : 0, \
150 (val)) - (width); \
151 if ((ovflw) < 0) \
152 (ovflw) = 0; \
153 } while (/* CONSTCOND */0)
154 #endif
155
156 void filemode __P((void));
157 int getfiles __P((char **, int *));
158 int getflags __P((const struct flagbit_desc *, char *, u_int));
159 struct mount *
160 getmnt __P((struct mount *));
161 char * kinfo_vnodes __P((int *));
162 void layer_header __P((void));
163 int layer_print __P((struct vnode *, int));
164 char * loadvnodes __P((int *));
165 int main __P((int, char **));
166 void mount_print __P((struct mount *));
167 void nfs_header __P((void));
168 int nfs_print __P((struct vnode *, int));
169 void ttymode __P((void));
170 void ttyprt __P((struct tty *));
171 void ufs_header __P((void));
172 int ufs_print __P((struct vnode *, int));
173 int ext2fs_print __P((struct vnode *, int));
174 void union_header __P((void));
175 int union_print __P((struct vnode *, int));
176 void usage __P((void));
177 void vnode_header __P((void));
178 int vnode_print __P((struct vnode *, struct vnode *));
179 void vnodemode __P((void));
180
181 int
182 main(argc, argv)
183 int argc;
184 char *argv[];
185 {
186 int ch, i, quit, ret;
187 int fileflag, swapflag, ttyflag, vnodeflag;
188 gid_t egid = getegid();
189 char buf[_POSIX2_LINE_MAX];
190
191 setegid(getgid());
192 fileflag = swapflag = ttyflag = vnodeflag = 0;
193 while ((ch = getopt(argc, argv, "TM:N:fhiknstv")) != -1)
194 switch (ch) {
195 case 'f':
196 fileflag = 1;
197 break;
198 case 'M':
199 memf = optarg;
200 break;
201 case 'N':
202 nlistf = optarg;
203 break;
204 case 'n':
205 usenumflag = 1;
206 break;
207 case 's':
208 swapflag = 1;
209 break;
210 case 'T':
211 totalflag = 1;
212 break;
213 case 't':
214 ttyflag = 1;
215 break;
216 case 'k':
217 kflag = 1;
218 break;
219 case 'h':
220 hflag = 1;
221 break;
222 case 'v':
223 case 'i': /* Backward compatibility. */
224 vnodeflag = 1;
225 break;
226 default:
227 usage();
228 }
229 argc -= optind;
230 argv += optind;
231
232 /*
233 * Discard setgid privileges. If not the running kernel, we toss
234 * them away totally so that bad guys can't print interesting stuff
235 * from kernel memory, otherwise switch back to kmem for the
236 * duration of the kvm_openfiles() call.
237 */
238 if (nlistf != NULL || memf != NULL)
239 (void)setgid(getgid());
240 else
241 (void)setegid(egid);
242
243 if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == 0)
244 errx(1, "kvm_openfiles: %s", buf);
245
246 /* get rid of it now anyway */
247 if (nlistf == NULL && memf == NULL)
248 (void)setgid(getgid());
249 if ((ret = kvm_nlist(kd, nl)) != 0) {
250 if (ret == -1)
251 errx(1, "kvm_nlist: %s", kvm_geterr(kd));
252 for (i = quit = 0; i <= NLMANDATORY; i++)
253 if (!nl[i].n_value) {
254 quit = 1;
255 warnx("undefined symbol: %s", nl[i].n_name);
256 }
257 if (quit)
258 exit(1);
259 }
260 if (!(fileflag | vnodeflag | ttyflag | swapflag | totalflag))
261 usage();
262 if (fileflag || totalflag)
263 filemode();
264 if (vnodeflag || totalflag)
265 vnodemode();
266 if (ttyflag)
267 ttymode();
268 if (swapflag || totalflag)
269 list_swap(0, kflag, 0, totalflag, 1, hflag);
270 exit(0);
271 }
272
273 #define VPTRSZ sizeof(struct vnode *)
274 #define VNODESZ sizeof(struct vnode)
275 #define PTRSTRWIDTH ((int)sizeof(void *) * 2) /* Width of resulting string
276 when pointer is printed
277 in hexadecimal. */
278
279 void
280 vnodemode()
281 {
282 char *e_vnodebase, *endvnode, *evp;
283 struct vnode *vp;
284 struct mount *maddr, *mp;
285 int numvnodes, ovflw;
286 int (*vnode_fsprint)
287 __P((struct vnode *, int)); /* per-fs data printer */
288
289 mp = NULL;
290 e_vnodebase = loadvnodes(&numvnodes);
291 if (totalflag) {
292 (void)printf("%7d vnodes\n", numvnodes);
293 goto out;
294 }
295 endvnode = e_vnodebase + numvnodes * (VPTRSZ + VNODESZ);
296 (void)printf("%d active vnodes\n", numvnodes);
297
298 #define ST mp->mnt_stat
299 #define FSTYPE_IS(mp, name) \
300 (strncmp((mp)->mnt_stat.f_fstypename, (name), MFSNAMELEN) == 0)
301 maddr = NULL;
302 vnode_fsprint = NULL;
303 for (evp = e_vnodebase; evp < endvnode; evp += VPTRSZ + VNODESZ) {
304 vp = (struct vnode *)(evp + VPTRSZ);
305 if (vp->v_mount != maddr) {
306 /*
307 * New filesystem
308 */
309 if ((mp = getmnt(vp->v_mount)) == NULL)
310 continue;
311 maddr = vp->v_mount;
312 mount_print(mp);
313 vnode_header();
314 if (FSTYPE_IS(mp, MOUNT_FFS) ||
315 FSTYPE_IS(mp, MOUNT_MFS)) {
316 ufs_header();
317 vnode_fsprint = ufs_print;
318 } else if (FSTYPE_IS(mp, MOUNT_NFS)) {
319 nfs_header();
320 vnode_fsprint = nfs_print;
321 } else if (FSTYPE_IS(mp, MOUNT_EXT2FS)) {
322 ufs_header();
323 vnode_fsprint = ext2fs_print;
324 } else if (FSTYPE_IS(mp, MOUNT_NULL) ||
325 FSTYPE_IS(mp, MOUNT_OVERLAY) ||
326 FSTYPE_IS(mp, MOUNT_UMAP)) {
327 layer_header();
328 vnode_fsprint = layer_print;
329 } else if (FSTYPE_IS(mp, MOUNT_UNION)) {
330 union_header();
331 vnode_fsprint = union_print;
332 } else
333 vnode_fsprint = NULL;
334 (void)printf("\n");
335 }
336 ovflw = vnode_print(*(struct vnode **)evp, vp);
337 if (VTOI(vp) != NULL && vnode_fsprint != NULL)
338 (*vnode_fsprint)(vp, ovflw);
339 (void)printf("\n");
340 }
341
342 out:
343 free(e_vnodebase);
344 }
345
346 int
347 getflags(fd, p, flags)
348 const struct flagbit_desc *fd;
349 char *p;
350 u_int flags;
351 {
352 char *q = p;
353
354 if (flags == 0) {
355 *p++ = '-';
356 *p = '\0';
357 return (0);
358 }
359
360 for (; fd->fd_flags != 0; fd++)
361 if ((flags & fd->fd_flags) != 0)
362 *p++ = fd->fd_mark;
363 *p = '\0';
364 return (p - q);
365 }
366
367 const struct flagbit_desc vnode_flags[] = {
368 { VROOT, 'R' },
369 { VTEXT, 'T' },
370 { VSYSTEM, 'S' },
371 { VISTTY, 'I' },
372 { VEXECMAP, 'E' },
373 { VXLOCK, 'L' },
374 { VXWANT, 'W' },
375 { VBWAIT, 'B' },
376 { VALIASED, 'A' },
377 { VDIROP, 'D' },
378 { VLAYER, 'Y' },
379 { VONWORKLST, 'O' },
380 { 0, '\0' },
381 };
382
383 void
384 vnode_header()
385 {
386
387 (void)printf("%-*s TYP VFLAG USE HOLD TAG NPAGE",
388 PTRSTRWIDTH, "ADDR");
389 }
390
391 int
392 vnode_print(avnode, vp)
393 struct vnode *avnode;
394 struct vnode *vp;
395 {
396 char *type, flags[sizeof(vnode_flags) / sizeof(vnode_flags[0])];
397 int ovflw;
398
399 /*
400 * set type
401 */
402 switch (vp->v_type) {
403 case VNON:
404 type = "non"; break;
405 case VREG:
406 type = "reg"; break;
407 case VDIR:
408 type = "dir"; break;
409 case VBLK:
410 type = "blk"; break;
411 case VCHR:
412 type = "chr"; break;
413 case VLNK:
414 type = "lnk"; break;
415 case VSOCK:
416 type = "soc"; break;
417 case VFIFO:
418 type = "fif"; break;
419 case VBAD:
420 type = "bad"; break;
421 default:
422 type = "unk"; break;
423 }
424 /*
425 * gather flags
426 */
427 (void)getflags(vnode_flags, flags, vp->v_flag);
428
429 ovflw = 0;
430 PRWORD(ovflw, "%*lx", PTRSTRWIDTH, 0, (long)avnode);
431 PRWORD(ovflw, " %*s", 4, 1, type);
432 PRWORD(ovflw, " %*s", 6, 1, flags);
433 PRWORD(ovflw, " %*ld", 5, 1, (long)vp->v_usecount);
434 PRWORD(ovflw, " %*ld", 5, 1, (long)vp->v_holdcnt);
435 PRWORD(ovflw, " %*d", 4, 1, vp->v_tag);
436 PRWORD(ovflw, " %*d", 6, 1, vp->v_uobj.uo_npages);
437 return (ovflw);
438 }
439
440 const struct flagbit_desc ufs_flags[] = {
441 { IN_ACCESS, 'A' },
442 { IN_CHANGE, 'C' },
443 { IN_UPDATE, 'U' },
444 { IN_MODIFIED, 'M' },
445 { IN_ACCESSED, 'a' },
446 { IN_RENAME, 'R' },
447 { IN_SHLOCK, 'S' },
448 { IN_EXLOCK, 'E' },
449 { IN_CLEANING, 'c' },
450 { IN_ADIROP, 'D' },
451 { IN_SPACECOUNTED, 's' },
452 { 0, '\0' },
453 };
454
455 void
456 ufs_header()
457 {
458
459 (void)printf(" FILEID IFLAG RDEV|SZ");
460 }
461
462 int
463 ufs_print(vp, ovflw)
464 struct vnode *vp;
465 int ovflw;
466 {
467 struct inode inode, *ip = &inode;
468 union dinode {
469 struct ufs1_dinode dp1;
470 struct ufs2_dinode dp2;
471 } dip;
472 char flags[sizeof(ufs_flags) / sizeof(ufs_flags[0])];
473 char dev[4 + 1 + 7 + 1]; /* 12bit marjor + 20bit minor */
474 char *name;
475 mode_t type;
476 dev_t rdev;
477
478 KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode");
479 KGETRET(ip->i_din.ffs1_din, &dip, sizeof (struct ufs1_dinode),
480 "inode's dinode");
481
482 if (ip->i_size == dip.dp1.di_size)
483 rdev = dip.dp1.di_rdev;
484 else {
485 KGETRET(ip->i_din.ffs1_din, &dip, sizeof (struct ufs2_dinode),
486 "inode's UFS2 dinode");
487 rdev = dip.dp2.di_rdev;
488 }
489
490 /*
491 * XXX need to to locking state.
492 */
493
494 (void)getflags(ufs_flags, flags, ip->i_flag);
495 PRWORD(ovflw, " %*d", 7, 1, ip->i_number);
496 PRWORD(ovflw, " %*s", 6, 1, flags);
497 type = ip->i_mode & S_IFMT;
498 if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode)) {
499 if (usenumflag ||
500 (name = devname(rdev, type)) == NULL) {
501 snprintf(dev, sizeof(dev), "%d,%d",
502 major(rdev), minor(rdev));
503 name = dev;
504 }
505 PRWORD(ovflw, " %*s", 8, 1, name);
506 } else
507 PRWORD(ovflw, " %*lld", 8, 1, (long long)ip->i_size);
508 return 0;
509 }
510
511 int
512 ext2fs_print(vp, ovflw)
513 struct vnode *vp;
514 int ovflw;
515 {
516 struct inode inode, *ip = &inode;
517 char flags[sizeof(ufs_flags) / sizeof(ufs_flags[0])];
518 char dev[4 + 1 + 7 + 1]; /* 12bit marjor + 20bit minor */
519 char *name;
520 mode_t type;
521
522 KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode");
523
524 /*
525 * XXX need to to locking state.
526 */
527
528 (void)getflags(ufs_flags, flags, ip->i_flag);
529 PRWORD(ovflw, " %*d", 7, 1, ip->i_number);
530 PRWORD(ovflw, " %*s", 6, 1, flags);
531 type = ip->i_e2fs_mode & S_IFMT;
532 if (S_ISCHR(ip->i_e2fs_mode) || S_ISBLK(ip->i_e2fs_mode)) {
533 if (usenumflag ||
534 (name = devname(ip->i_e2fs_rdev, type)) == NULL) {
535 snprintf(dev, sizeof(dev), "%d,%d",
536 major(ip->i_e2fs_rdev), minor(ip->i_e2fs_rdev));
537 name = dev;
538 }
539 PRWORD(ovflw, " %*s", 8, 1, name);
540 } else
541 PRWORD(ovflw, " %*u", 8, 1, (u_int)ip->i_e2fs_size);
542 return (0);
543 }
544
545 const struct flagbit_desc nfs_flags[] = {
546 { NFLUSHWANT, 'W' },
547 { NFLUSHINPROG, 'P' },
548 { NMODIFIED, 'M' },
549 { NWRITEERR, 'E' },
550 { NQNFSNONCACHE, 'X' },
551 { NQNFSWRITE, 'O' },
552 { NQNFSEVICTED, 'G' },
553 { NACC, 'A' },
554 { NUPD, 'U' },
555 { NCHG, 'C' },
556 { 0, '\0' },
557 };
558
559 void
560 nfs_header()
561 {
562
563 (void)printf(" FILEID NFLAG RDEV|SZ");
564 }
565
566 int
567 nfs_print(vp, ovflw)
568 struct vnode *vp;
569 int ovflw;
570 {
571 struct nfsnode nfsnode, *np = &nfsnode;
572 char flags[sizeof(nfs_flags) / sizeof(nfs_flags[0])];
573 char dev[4 + 1 + 7 + 1]; /* 12bit marjor + 20bit minor */
574 struct vattr va;
575 char *name;
576 mode_t type;
577
578 KGETRET(VTONFS(vp), &nfsnode, sizeof(nfsnode), "vnode's nfsnode");
579 (void)getflags(nfs_flags, flags, np->n_flag);
580
581 KGETRET(np->n_vattr, &va, sizeof(va), "vnode attr");
582 PRWORD(ovflw, " %*ld", 7, 1, (long)va.va_fileid);
583 PRWORD(ovflw, " %*s", 6, 1, flags);
584 switch (va.va_type) {
585 case VCHR:
586 type = S_IFCHR;
587 goto device;
588
589 case VBLK:
590 type = S_IFBLK;
591 device:
592 if (usenumflag || (name = devname(va.va_rdev, type)) == NULL) {
593 (void)snprintf(dev, sizeof(dev), "%d,%d",
594 major(va.va_rdev), minor(va.va_rdev));
595 name = dev;
596 }
597 PRWORD(ovflw, " %*s", 8, 1, name);
598 break;
599 default:
600 PRWORD(ovflw, " %*lld", 8, 1, (long long)np->n_size);
601 break;
602 }
603 return (0);
604 }
605
606 void
607 layer_header()
608 {
609
610 (void)printf(" %*s", PTRSTRWIDTH, "LOWER");
611 }
612
613 int
614 layer_print(vp, ovflw)
615 struct vnode *vp;
616 int ovflw;
617 {
618 struct layer_node lnode, *lp = &lnode;
619
620 KGETRET(VTOLAYER(vp), &lnode, sizeof(lnode), "layer vnode");
621
622 PRWORD(ovflw, " %*lx", PTRSTRWIDTH + 1, 1, (long)lp->layer_lowervp);
623 return (0);
624 }
625
626 void
627 union_header()
628 {
629
630 (void)printf(" %*s %*s", PTRSTRWIDTH, "UPPER", PTRSTRWIDTH, "LOWER");
631 }
632
633 int
634 union_print(vp, ovflw)
635 struct vnode *vp;
636 int ovflw;
637 {
638 struct union_node unode, *up = &unode;
639
640 KGETRET(VTOUNION(vp), &unode, sizeof(unode), "vnode's unode");
641
642 PRWORD(ovflw, " %*lx", PTRSTRWIDTH + 1, 1, (long)up->un_uppervp);
643 PRWORD(ovflw, " %*lx", PTRSTRWIDTH + 1, 1, (long)up->un_lowervp);
644 return (0);
645 }
646
647 /*
648 * Given a pointer to a mount structure in kernel space,
649 * read it in and return a usable pointer to it.
650 */
651 struct mount *
652 getmnt(maddr)
653 struct mount *maddr;
654 {
655 static struct mtab {
656 struct mtab *next;
657 struct mount *maddr;
658 struct mount mount;
659 } *mhead = NULL;
660 struct mtab *mt;
661
662 for (mt = mhead; mt != NULL; mt = mt->next)
663 if (maddr == mt->maddr)
664 return (&mt->mount);
665 if ((mt = malloc(sizeof(struct mtab))) == NULL)
666 err(1, "malloc");
667 KGETRET(maddr, &mt->mount, sizeof(struct mount), "mount table");
668 mt->maddr = maddr;
669 mt->next = mhead;
670 mhead = mt;
671 return (&mt->mount);
672 }
673
674 void
675 mount_print(mp)
676 struct mount *mp;
677 {
678 int flags;
679
680 (void)printf("*** MOUNT %s %s on %s", ST.f_fstypename,
681 ST.f_mntfromname, ST.f_mntonname);
682 if ((flags = mp->mnt_flag) != 0) {
683 int i;
684 const char *sep = " (";
685
686 for (i = 0; i <= sizeof mnt_flags / sizeof mnt_flags[0]; i++) {
687 if (flags & mnt_flags[i].m_flag) {
688 (void)printf("%s%s", sep, mnt_flags[i].m_name);
689 flags &= ~mnt_flags[i].m_flag;
690 sep = ",";
691 }
692 }
693 if (flags)
694 (void)printf("%sunknown_flags:%x", sep, flags);
695 (void)printf(")");
696 }
697 (void)printf("\n");
698 }
699
700 char *
701 loadvnodes(avnodes)
702 int *avnodes;
703 {
704 int mib[2];
705 size_t copysize;
706 char *vnodebase;
707
708 if (memf != NULL) {
709 /*
710 * do it by hand
711 */
712 return (kinfo_vnodes(avnodes));
713 }
714 mib[0] = CTL_KERN;
715 mib[1] = KERN_VNODE;
716 if (sysctl(mib, 2, NULL, ©size, NULL, 0) == -1)
717 err(1, "sysctl: KERN_VNODE");
718 if ((vnodebase = malloc(copysize)) == NULL)
719 err(1, "malloc");
720 if (sysctl(mib, 2, vnodebase, ©size, NULL, 0) == -1)
721 err(1, "sysctl: KERN_VNODE");
722 if (copysize % (VPTRSZ + VNODESZ))
723 errx(1, "vnode size mismatch");
724 *avnodes = copysize / (VPTRSZ + VNODESZ);
725
726 return (vnodebase);
727 }
728
729 /*
730 * simulate what a running kernel does in in kinfo_vnode
731 */
732 char *
733 kinfo_vnodes(avnodes)
734 int *avnodes;
735 {
736 struct mntlist mountlist;
737 struct mount *mp, mount;
738 struct vnode *vp, vnode;
739 char *beg, *bp, *ep;
740 int numvnodes;
741
742 KGET(V_NUMV, numvnodes);
743 if ((bp = malloc((numvnodes + 20) * (VPTRSZ + VNODESZ))) == NULL)
744 err(1, "malloc");
745 beg = bp;
746 ep = bp + (numvnodes + 20) * (VPTRSZ + VNODESZ);
747 KGET(V_MOUNTLIST, mountlist);
748 for (mp = mountlist.cqh_first;;
749 mp = mount.mnt_list.cqe_next) {
750 KGET2(mp, &mount, sizeof(mount), "mount entry");
751 for (vp = mount.mnt_vnodelist.lh_first;
752 vp != NULL; vp = vnode.v_mntvnodes.le_next) {
753 KGET2(vp, &vnode, sizeof(vnode), "vnode");
754 if (bp + VPTRSZ + VNODESZ > ep)
755 /* XXX - should realloc */
756 errx(1, "no more room for vnodes");
757 memmove(bp, &vp, VPTRSZ);
758 bp += VPTRSZ;
759 memmove(bp, &vnode, VNODESZ);
760 bp += VNODESZ;
761 }
762 if (mp == mountlist.cqh_last)
763 break;
764 }
765 *avnodes = (bp - beg) / (VPTRSZ + VNODESZ);
766 return (beg);
767 }
768
769 void
770 ttymode()
771 {
772 int ntty;
773 struct ttylist_head tty_head;
774 struct tty *tp, tty;
775
776 KGET(TTY_NTTY, ntty);
777 (void)printf("%d terminal device%s\n", ntty, ntty == 1 ? "" : "s");
778 KGET(TTY_TTYLIST, tty_head);
779 (void)printf(
780 " LINE RAW CAN OUT HWT LWT COL STATE %-*s PGID DISC\n",
781 PTRSTRWIDTH, "SESS");
782 for (tp = tty_head.tqh_first; tp; tp = tty.tty_link.tqe_next) {
783 KGET2(tp, &tty, sizeof tty, "tty struct");
784 ttyprt(&tty);
785 }
786 }
787
788 static const struct flagbit_desc ttystates[] = {
789 { TS_ISOPEN, 'O'},
790 { TS_DIALOUT, '>'},
791 { TS_CARR_ON, 'C'},
792 { TS_TIMEOUT, 'T'},
793 { TS_FLUSH, 'F'},
794 { TS_BUSY, 'B'},
795 { TS_ASLEEP, 'A'},
796 { TS_XCLUDE, 'X'},
797 { TS_TTSTOP, 'S'},
798 { TS_TBLOCK, 'K'},
799 { TS_ASYNC, 'Y'},
800 { TS_BKSL, 'D'},
801 { TS_ERASE, 'E'},
802 { TS_LNCH, 'L'},
803 { TS_TYPEN, 'P'},
804 { TS_CNTTB, 'N'},
805 { 0, '\0'},
806 };
807
808 void
809 ttyprt(tp)
810 struct tty *tp;
811 {
812 char state[sizeof(ttystates) / sizeof(ttystates[0]) + 1];
813 char dev[2 + 3 + 1 + 5 + 1]; /* 12bit major + 20bit minor */
814 struct linesw t_linesw;
815 char *name, buffer;
816 pid_t pgid;
817 int n, ovflw;
818
819 if (usenumflag || (name = devname(tp->t_dev, S_IFCHR)) == NULL) {
820 (void)snprintf(dev, sizeof(dev), "0x%3x:%x",
821 major(tp->t_dev), minor(tp->t_dev));
822 name = dev;
823 }
824 ovflw = 0;
825 PRWORD(ovflw, "%-*s", 7, 0, name);
826 PRWORD(ovflw, " %*d", 3, 1, tp->t_rawq.c_cc);
827 PRWORD(ovflw, " %*d", 4, 1, tp->t_canq.c_cc);
828 PRWORD(ovflw, " %*d", 4, 1, tp->t_outq.c_cc);
829 PRWORD(ovflw, " %*d", 5, 1, tp->t_hiwat);
830 PRWORD(ovflw, " %*d", 4, 1, tp->t_lowat);
831 PRWORD(ovflw, " %*d", 8, 1, tp->t_column);
832 n = getflags(ttystates, state, tp->t_state);
833 if (tp->t_wopen) {
834 state[n++] = 'W';
835 state[n] = '\0';
836 }
837 PRWORD(ovflw, " %-*s", 7, 1, state);
838 PRWORD(ovflw, " %*lX", PTRSTRWIDTH + 1, 1, (u_long)tp->t_session);
839 pgid = 0;
840 if (tp->t_pgrp != NULL)
841 KGET2(&tp->t_pgrp->pg_id, &pgid, sizeof(pid_t), "pgid");
842 PRWORD(ovflw, " %*d", 6, 1, pgid);
843 KGET2(tp->t_linesw, &t_linesw, sizeof(t_linesw),
844 "line discipline switch table");
845 name = t_linesw.l_name;
846 (void)putchar(' ');
847 for (;;) {
848 KGET2(name, &buffer, sizeof(buffer), "line discipline name");
849 if (buffer == '\0')
850 break;
851 (void)putchar(buffer);
852 name++;
853 }
854 (void)putchar('\n');
855 }
856
857 static const struct flagbit_desc filemode_flags[] = {
858 { FREAD, 'R' },
859 { FWRITE, 'W' },
860 { FAPPEND, 'A' },
861 #ifdef FSHLOCK /* currently gone */
862 { FSHLOCK, 'S' },
863 { FEXLOCK, 'X' },
864 #endif
865 { FASYNC, 'I' },
866 { 0, '\0' },
867 };
868
869 void
870 filemode()
871 {
872 struct file *fp;
873 struct file *addr;
874 char flags[sizeof(filemode_flags) / sizeof(filemode_flags[0])];
875 char *buf;
876 int len, maxfile, nfile, ovflw;
877 static const char * const dtypes[] =
878 { "???", "inode", "socket", "pipe" };
879
880 KGET(FNL_MAXFILE, maxfile);
881 if (totalflag) {
882 KGET(FNL_NFILE, nfile);
883 (void)printf("%3d/%3d files\n", nfile, maxfile);
884 return;
885 }
886 if (getfiles(&buf, &len) == -1)
887 return;
888 /*
889 * Getfiles returns in malloc'd memory a pointer to the first file
890 * structure, and then an array of file structs (whose addresses are
891 * derivable from the previous entry).
892 */
893 addr = ((struct filelist *)buf)->lh_first;
894 fp = (struct file *)(buf + sizeof(struct filelist));
895 nfile = (len - sizeof(struct filelist)) / sizeof(struct file);
896
897 (void)printf("%d/%d open files\n", nfile, maxfile);
898 (void)printf("%*s%s%*s TYPE FLG CNT MSG %*s%s%*s OFFSET\n",
899 (PTRSTRWIDTH - 4) / 2, "", " LOC", (PTRSTRWIDTH - 4) / 2, "",
900 (PTRSTRWIDTH - 4) / 2, "", "DATA", (PTRSTRWIDTH - 4) / 2, "");
901 for (; (char *)fp < buf + len; addr = fp->f_list.le_next, fp++) {
902 if ((unsigned)fp->f_type > DTYPE_PIPE)
903 continue;
904 ovflw = 0;
905 (void)getflags(filemode_flags, flags, fp->f_flag);
906 PRWORD(ovflw, "%*lx", PTRSTRWIDTH, 0, (long)addr);
907 PRWORD(ovflw, " %-*s", 9, 1, dtypes[fp->f_type]);
908 PRWORD(ovflw, " %*s", 6, 1, flags);
909 PRWORD(ovflw, " %*d", 5, 1, fp->f_count);
910 PRWORD(ovflw, " %*d", 5, 1, fp->f_msgcount);
911 PRWORD(ovflw, " %*lx", PTRSTRWIDTH + 1, 2, (long)fp->f_data);
912 if (fp->f_offset < 0)
913 PRWORD(ovflw, " %-*llx\n", PTRSTRWIDTH + 1, 2,
914 (long long)fp->f_offset);
915 else
916 PRWORD(ovflw, " %-*lld\n", PTRSTRWIDTH + 1, 2,
917 (long long)fp->f_offset);
918 }
919 free(buf);
920 }
921
922 int
923 getfiles(abuf, alen)
924 char **abuf;
925 int *alen;
926 {
927 size_t len;
928 int mib[2];
929 char *buf;
930
931 /*
932 * XXX
933 * Add emulation of KINFO_FILE here.
934 */
935 if (memf != NULL)
936 errx(1, "files on dead kernel, not implemented");
937
938 mib[0] = CTL_KERN;
939 mib[1] = KERN_FILE;
940 if (sysctl(mib, 2, NULL, &len, NULL, 0) == -1) {
941 warn("sysctl: KERN_FILE");
942 return (-1);
943 }
944 if ((buf = malloc(len)) == NULL)
945 err(1, "malloc");
946 if (sysctl(mib, 2, buf, &len, NULL, 0) == -1) {
947 warn("sysctl: KERN_FILE");
948 return (-1);
949 }
950 *abuf = buf;
951 *alen = len;
952 return (0);
953 }
954
955 void
956 usage()
957 {
958
959 (void)fprintf(stderr,
960 "usage: %s [-T|-f|-s|-t|-v] [-knh] [-M core] [-N system]\n",
961 getprogname());
962 exit(1);
963 }
964