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