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