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