pstat.c revision 1.18 1 /* $NetBSD: pstat.c,v 1.18 1996/04/30 00:41:51 pk Exp $ */
2
3 /*-
4 * Copyright (c) 1980, 1991, 1993
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 #ifndef lint
37 static char copyright[] =
38 "@(#) Copyright (c) 1980, 1991, 1993\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 from: static char sccsid[] = "@(#)pstat.c 8.9 (Berkeley) 2/16/94";
45 #else
46 static char *rcsid = "$NetBSD: pstat.c,v 1.18 1996/04/30 00:41:51 pk 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 #include <sys/file.h>
57 #include <ufs/ufs/quota.h>
58 #include <ufs/ufs/inode.h>
59 #define NFS
60 #include <sys/mount.h>
61 #undef NFS
62 #undef _KERNEL
63 #include <sys/stat.h>
64 #include <nfs/nfsproto.h>
65 #include <nfs/rpcv2.h>
66 #include <nfs/nfsnode.h>
67 #include <sys/ioctl.h>
68 #include <sys/tty.h>
69 #include <sys/conf.h>
70 #include <sys/device.h>
71
72 #include <sys/sysctl.h>
73
74 #include <err.h>
75 #include <kvm.h>
76 #include <limits.h>
77 #include <nlist.h>
78 #include <stdio.h>
79 #include <stdlib.h>
80 #include <string.h>
81 #include <unistd.h>
82
83 struct nlist nl[] = {
84 #define VM_SWAPMAP 0
85 { "_swapmap" }, /* list of free swap areas */
86 #define VM_NSWAPMAP 1
87 { "_nswapmap" },/* size of the swap map */
88 #define VM_SWDEVT 2
89 { "_swdevt" }, /* list of swap devices and sizes */
90 #define VM_NSWAP 3
91 { "_nswap" }, /* size of largest swap device */
92 #define VM_NSWDEV 4
93 { "_nswdev" }, /* number of swap devices */
94 #define VM_DMMAX 5
95 { "_dmmax" }, /* maximum size of a swap block */
96 #define V_MOUNTLIST 6
97 { "_mountlist" }, /* address of head of mount list. */
98 #define V_NUMV 7
99 { "_numvnodes" },
100 #define FNL_NFILE 8
101 {"_nfiles"},
102 #define FNL_MAXFILE 9
103 {"_maxfiles"},
104 #define NLMANDATORY FNL_MAXFILE /* names up to here are mandatory */
105 #define VM_NISWAP NLMANDATORY + 1
106 { "_niswap" },
107 #define VM_NISWDEV NLMANDATORY + 2
108 { "_niswdev" },
109 #define SCONS NLMANDATORY + 3
110 { "_constty" },
111 #define SPTY NLMANDATORY + 4
112 { "_pt_tty" },
113 #define SNPTY NLMANDATORY + 5
114 { "_npty" },
115
116 #ifdef sparc
117 #define SZS (SNPTY+1)
118 { "_zs_tty" },
119 #define SCZS (SNPTY+2)
120 { "_zscd" },
121 #endif
122
123 #ifdef hp300
124 #define SDCA (SNPTY+1)
125 { "_dca_tty" },
126 #define SNDCA (SNPTY+2)
127 { "_ndca" },
128 #define SDCM (SNPTY+3)
129 { "_dcm_tty" },
130 #define SNDCM (SNPTY+4)
131 { "_ndcm" },
132 #define SDCL (SNPTY+5)
133 { "_dcl_tty" },
134 #define SNDCL (SNPTY+6)
135 { "_ndcl" },
136 #define SITE (SNPTY+7)
137 { "_ite_tty" },
138 #define SNITE (SNPTY+8)
139 { "_nite" },
140 #endif
141
142 #ifdef mips
143 #define SDC (SNPTY+1)
144 { "_dc_tty" },
145 #define SNDC (SNPTY+2)
146 { "_dc_cnt" },
147 #endif
148
149 #ifdef i386
150 #define SPC (SNPTY+1)
151 { "_pc_tty" },
152 #define SCPC (SNPTY+2)
153 { "_pccd" },
154 #define SCOM (SNPTY+3)
155 { "_com_tty" },
156 #define SCCOM (SNPTY+4)
157 { "_comcd" },
158 #endif
159 #ifdef amiga
160 #define SSER (SNPTY + 1)
161 { "_ser_tty" },
162 #define SCSER (SNPTY + 2)
163 { "_sercd" },
164 #define SITE (SNPTY + 3)
165 { "_ite_tty" },
166 #define SCITE (SNPTY + 4)
167 { "_itecd" },
168 #define SMFCS (SNPTY + 5)
169 { "_mfcs_tty" },
170 #define SCMFCS (SNPTY + 6)
171 { "_mfcscd" },
172 #endif
173
174 { "" }
175 };
176
177 int usenumflag;
178 int totalflag;
179 int kflag;
180 char *nlistf = NULL;
181 char *memf = NULL;
182 kvm_t *kd;
183
184 #define SVAR(var) __STRING(var) /* to force expansion */
185 #define KGET(idx, var) \
186 KGET1(idx, &var, sizeof(var), SVAR(var))
187 #define KGET1(idx, p, s, msg) \
188 KGET2(nl[idx].n_value, p, s, msg)
189 #define KGET2(addr, p, s, msg) \
190 if (kvm_read(kd, (u_long)(addr), p, s) != s) \
191 warnx("cannot read %s: %s", msg, kvm_geterr(kd))
192 #define KGETRET(addr, p, s, msg) \
193 if (kvm_read(kd, (u_long)(addr), p, s) != s) { \
194 warnx("cannot read %s: %s", msg, kvm_geterr(kd)); \
195 return (0); \
196 }
197
198 void filemode __P((void));
199 int getfiles __P((char **, int *));
200 struct mount *
201 getmnt __P((struct mount *));
202 struct e_vnode *
203 kinfo_vnodes __P((int *));
204 struct e_vnode *
205 loadvnodes __P((int *));
206 void mount_print __P((struct mount *));
207 void nfs_header __P((void));
208 int nfs_print __P((struct vnode *));
209 void swapmode __P((void));
210 void ttymode __P((void));
211 void ttyprt __P((struct tty *, int));
212 void ttytype __P((char *, int, int));
213 void ttytype_newcf __P((char *, int, int));
214 void ttytype_oldcf __P((char *, int, int));
215 void ufs_header __P((void));
216 int ufs_print __P((struct vnode *));
217 void usage __P((void));
218 void vnode_header __P((void));
219 void vnode_print __P((struct vnode *, struct vnode *));
220 void vnodemode __P((void));
221
222 int
223 main(argc, argv)
224 int argc;
225 char *argv[];
226 {
227 extern char *optarg;
228 extern int optind;
229 int ch, i, quit, ret;
230 int fileflag, swapflag, ttyflag, vnodeflag;
231 char buf[_POSIX2_LINE_MAX];
232
233 fileflag = swapflag = ttyflag = vnodeflag = 0;
234 while ((ch = getopt(argc, argv, "TM:N:fiknstv")) != -1)
235 switch (ch) {
236 case 'f':
237 fileflag = 1;
238 break;
239 case 'M':
240 memf = optarg;
241 break;
242 case 'N':
243 nlistf = optarg;
244 break;
245 case 'n':
246 usenumflag = 1;
247 break;
248 case 's':
249 swapflag = 1;
250 break;
251 case 'T':
252 totalflag = 1;
253 break;
254 case 't':
255 ttyflag = 1;
256 break;
257 case 'k':
258 kflag = 1;
259 break;
260 case 'v':
261 case 'i': /* Backward compatibility. */
262 vnodeflag = 1;
263 break;
264 default:
265 usage();
266 }
267 argc -= optind;
268 argv += optind;
269
270 /*
271 * Discard setgid privileges if not the running kernel so that bad
272 * guys can't print interesting stuff from kernel memory.
273 */
274 if (nlistf != NULL || memf != NULL)
275 (void)setgid(getgid());
276
277 if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == 0)
278 errx(1, "kvm_openfiles: %s", buf);
279 if ((ret = kvm_nlist(kd, nl)) != 0) {
280 if (ret == -1)
281 errx(1, "kvm_nlist: %s", kvm_geterr(kd));
282 for (i = quit = 0; i <= NLMANDATORY; i++)
283 if (!nl[i].n_value) {
284 quit = 1;
285 warnx("undefined symbol: %s\n", nl[i].n_name);
286 }
287 if (quit)
288 exit(1);
289 }
290 if (!(fileflag | vnodeflag | ttyflag | swapflag | totalflag))
291 usage();
292 if (fileflag || totalflag)
293 filemode();
294 if (vnodeflag || totalflag)
295 vnodemode();
296 if (ttyflag)
297 ttymode();
298 if (swapflag || totalflag)
299 swapmode();
300 exit (0);
301 }
302
303 struct e_vnode {
304 struct vnode *avnode;
305 struct vnode vnode;
306 };
307
308 void
309 vnodemode()
310 {
311 register struct e_vnode *e_vnodebase, *endvnode, *evp;
312 register struct vnode *vp;
313 register struct mount *maddr, *mp;
314 int numvnodes;
315
316 e_vnodebase = loadvnodes(&numvnodes);
317 if (totalflag) {
318 (void)printf("%7d vnodes\n", numvnodes);
319 return;
320 }
321 endvnode = e_vnodebase + numvnodes;
322 (void)printf("%d active vnodes\n", numvnodes);
323
324
325 #define ST mp->mnt_stat
326 maddr = NULL;
327 for (evp = e_vnodebase; evp < endvnode; evp++) {
328 vp = &evp->vnode;
329 if (vp->v_mount != maddr) {
330 /*
331 * New filesystem
332 */
333 if ((mp = getmnt(vp->v_mount)) == NULL)
334 continue;
335 maddr = vp->v_mount;
336 mount_print(mp);
337 vnode_header();
338 if (!strncmp(ST.f_fstypename, MOUNT_FFS, MFSNAMELEN) ||
339 !strncmp(ST.f_fstypename, MOUNT_MFS, MFSNAMELEN)) {
340 ufs_header();
341 } else if (!strncmp(ST.f_fstypename, MOUNT_NFS,
342 MFSNAMELEN)) {
343 nfs_header();
344 }
345 (void)printf("\n");
346 }
347 vnode_print(evp->avnode, vp);
348 if (!strncmp(ST.f_fstypename, MOUNT_FFS, MFSNAMELEN) ||
349 !strncmp(ST.f_fstypename, MOUNT_MFS, MFSNAMELEN)) {
350 ufs_print(vp);
351 } else if (!strncmp(ST.f_fstypename, MOUNT_NFS, MFSNAMELEN)) {
352 nfs_print(vp);
353 }
354 (void)printf("\n");
355 }
356 free(e_vnodebase);
357 }
358
359 void
360 vnode_header()
361 {
362 (void)printf("ADDR TYP VFLAG USE HOLD");
363 }
364
365 void
366 vnode_print(avnode, vp)
367 struct vnode *avnode;
368 struct vnode *vp;
369 {
370 char *type, flags[16];
371 char *fp = flags;
372 register int flag;
373
374 /*
375 * set type
376 */
377 switch(vp->v_type) {
378 case VNON:
379 type = "non"; break;
380 case VREG:
381 type = "reg"; break;
382 case VDIR:
383 type = "dir"; break;
384 case VBLK:
385 type = "blk"; break;
386 case VCHR:
387 type = "chr"; break;
388 case VLNK:
389 type = "lnk"; break;
390 case VSOCK:
391 type = "soc"; break;
392 case VFIFO:
393 type = "fif"; break;
394 case VBAD:
395 type = "bad"; break;
396 default:
397 type = "unk"; break;
398 }
399 /*
400 * gather flags
401 */
402 flag = vp->v_flag;
403 if (flag & VROOT)
404 *fp++ = 'R';
405 if (flag & VTEXT)
406 *fp++ = 'T';
407 if (flag & VSYSTEM)
408 *fp++ = 'S';
409 if (flag & VXLOCK)
410 *fp++ = 'L';
411 if (flag & VXWANT)
412 *fp++ = 'W';
413 if (flag & VBWAIT)
414 *fp++ = 'B';
415 if (flag & VALIASED)
416 *fp++ = 'A';
417 if (flag == 0)
418 *fp++ = '-';
419 *fp = '\0';
420 (void)printf("%8x %s %5s %4d %4d",
421 avnode, type, flags, vp->v_usecount, vp->v_holdcnt);
422 }
423
424 void
425 ufs_header()
426 {
427 (void)printf(" FILEID IFLAG RDEV|SZ");
428 }
429
430 int
431 ufs_print(vp)
432 struct vnode *vp;
433 {
434 register int flag;
435 struct inode inode, *ip = &inode;
436 char flagbuf[16], *flags = flagbuf;
437 char *name;
438 mode_t type;
439
440 KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode");
441 flag = ip->i_flag;
442 if (flag & IN_LOCKED)
443 *flags++ = 'L';
444 if (flag & IN_WANTED)
445 *flags++ = 'W';
446 if (flag & IN_RENAME)
447 *flags++ = 'R';
448 if (flag & IN_UPDATE)
449 *flags++ = 'U';
450 if (flag & IN_ACCESS)
451 *flags++ = 'A';
452 if (flag & IN_CHANGE)
453 *flags++ = 'C';
454 if (flag & IN_MODIFIED)
455 *flags++ = 'M';
456 if (flag & IN_SHLOCK)
457 *flags++ = 'S';
458 if (flag & IN_EXLOCK)
459 *flags++ = 'E';
460 if (flag & IN_LWAIT)
461 *flags++ = 'Z';
462 if (flag == 0)
463 *flags++ = '-';
464 *flags = '\0';
465
466 (void)printf(" %6d %5s", ip->i_number, flagbuf);
467 type = ip->i_mode & S_IFMT;
468 if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode))
469 if (usenumflag || ((name = devname(ip->i_rdev, type)) == NULL))
470 (void)printf(" %2d,%-2d",
471 major(ip->i_rdev), minor(ip->i_rdev));
472 else
473 (void)printf(" %7s", name);
474 else
475 (void)printf(" %7qd", ip->i_size);
476 return (0);
477 }
478
479 void
480 nfs_header()
481 {
482 (void)printf(" FILEID NFLAG RDEV|SZ");
483 }
484
485 int
486 nfs_print(vp)
487 struct vnode *vp;
488 {
489 struct nfsnode nfsnode, *np = &nfsnode;
490 char flagbuf[16], *flags = flagbuf;
491 register int flag;
492 char *name;
493 mode_t type;
494
495 KGETRET(VTONFS(vp), &nfsnode, sizeof(nfsnode), "vnode's nfsnode");
496 flag = np->n_flag;
497 if (flag & NFLUSHWANT)
498 *flags++ = 'W';
499 if (flag & NFLUSHINPROG)
500 *flags++ = 'P';
501 if (flag & NMODIFIED)
502 *flags++ = 'M';
503 if (flag & NWRITEERR)
504 *flags++ = 'E';
505 if (flag & NQNFSNONCACHE)
506 *flags++ = 'X';
507 if (flag & NQNFSWRITE)
508 *flags++ = 'O';
509 if (flag & NQNFSEVICTED)
510 *flags++ = 'G';
511 if (flag == 0)
512 *flags++ = '-';
513 *flags = '\0';
514
515 #define VT np->n_vattr
516 (void)printf(" %6d %5s", VT.va_fileid, flagbuf);
517 type = VT.va_mode & S_IFMT;
518 if (S_ISCHR(VT.va_mode) || S_ISBLK(VT.va_mode))
519 if (usenumflag || ((name = devname(VT.va_rdev, type)) == NULL))
520 (void)printf(" %2d,%-2d",
521 major(VT.va_rdev), minor(VT.va_rdev));
522 else
523 (void)printf(" %7s", name);
524 else
525 (void)printf(" %7qd", np->n_size);
526 return (0);
527 }
528
529 /*
530 * Given a pointer to a mount structure in kernel space,
531 * read it in and return a usable pointer to it.
532 */
533 struct mount *
534 getmnt(maddr)
535 struct mount *maddr;
536 {
537 static struct mtab {
538 struct mtab *next;
539 struct mount *maddr;
540 struct mount mount;
541 } *mhead = NULL;
542 register struct mtab *mt;
543
544 for (mt = mhead; mt != NULL; mt = mt->next)
545 if (maddr == mt->maddr)
546 return (&mt->mount);
547 if ((mt = malloc(sizeof(struct mtab))) == NULL)
548 err(1, NULL);
549 KGETRET(maddr, &mt->mount, sizeof(struct mount), "mount table");
550 mt->maddr = maddr;
551 mt->next = mhead;
552 mhead = mt;
553 return (&mt->mount);
554 }
555
556 void
557 mount_print(mp)
558 struct mount *mp;
559 {
560 register int flags;
561 char *type;
562
563 #define ST mp->mnt_stat
564 (void)printf("*** MOUNT ");
565 (void)printf("%.*s %s on %s", MFSNAMELEN, ST.f_fstypename,
566 ST.f_mntfromname, ST.f_mntonname);
567 if (flags = mp->mnt_flag) {
568 char *comma = "(";
569
570 putchar(' ');
571 /* user visable flags */
572 if (flags & MNT_RDONLY) {
573 (void)printf("%srdonly", comma);
574 flags &= ~MNT_RDONLY;
575 comma = ",";
576 }
577 if (flags & MNT_SYNCHRONOUS) {
578 (void)printf("%ssynchronous", comma);
579 flags &= ~MNT_SYNCHRONOUS;
580 comma = ",";
581 }
582 if (flags & MNT_NOEXEC) {
583 (void)printf("%snoexec", comma);
584 flags &= ~MNT_NOEXEC;
585 comma = ",";
586 }
587 if (flags & MNT_NOSUID) {
588 (void)printf("%snosuid", comma);
589 flags &= ~MNT_NOSUID;
590 comma = ",";
591 }
592 if (flags & MNT_NODEV) {
593 (void)printf("%snodev", comma);
594 flags &= ~MNT_NODEV;
595 comma = ",";
596 }
597 if (flags & MNT_EXPORTED) {
598 (void)printf("%sexport", comma);
599 flags &= ~MNT_EXPORTED;
600 comma = ",";
601 }
602 if (flags & MNT_EXRDONLY) {
603 (void)printf("%sexrdonly", comma);
604 flags &= ~MNT_EXRDONLY;
605 comma = ",";
606 }
607 if (flags & MNT_LOCAL) {
608 (void)printf("%slocal", comma);
609 flags &= ~MNT_LOCAL;
610 comma = ",";
611 }
612 if (flags & MNT_QUOTA) {
613 (void)printf("%squota", comma);
614 flags &= ~MNT_QUOTA;
615 comma = ",";
616 }
617 if (flags & MNT_ROOTFS) {
618 (void)printf("%srootfs", comma);
619 flags &= ~MNT_ROOTFS;
620 comma = ",";
621 }
622 /* filesystem control flags */
623 if (flags & MNT_UPDATE) {
624 (void)printf("%supdate", comma);
625 flags &= ~MNT_UPDATE;
626 comma = ",";
627 }
628 if (flags & MNT_MLOCK) {
629 (void)printf("%slock", comma);
630 flags &= ~MNT_MLOCK;
631 comma = ",";
632 }
633 if (flags & MNT_MWAIT) {
634 (void)printf("%swait", comma);
635 flags &= ~MNT_MWAIT;
636 comma = ",";
637 }
638 if (flags & MNT_MPBUSY) {
639 (void)printf("%sbusy", comma);
640 flags &= ~MNT_MPBUSY;
641 comma = ",";
642 }
643 if (flags & MNT_MPWANT) {
644 (void)printf("%swant", comma);
645 flags &= ~MNT_MPWANT;
646 comma = ",";
647 }
648 if (flags & MNT_UNMOUNT) {
649 (void)printf("%sunmount", comma);
650 flags &= ~MNT_UNMOUNT;
651 comma = ",";
652 }
653 if (flags)
654 (void)printf("%sunknown_flags:%x", comma, flags);
655 (void)printf(")");
656 }
657 (void)printf("\n");
658 #undef ST
659 }
660
661 struct e_vnode *
662 loadvnodes(avnodes)
663 int *avnodes;
664 {
665 int mib[2];
666 size_t copysize;
667 struct e_vnode *vnodebase;
668
669 if (memf != NULL) {
670 /*
671 * do it by hand
672 */
673 return (kinfo_vnodes(avnodes));
674 }
675 mib[0] = CTL_KERN;
676 mib[1] = KERN_VNODE;
677 if (sysctl(mib, 2, NULL, ©size, NULL, 0) == -1)
678 err(1, "sysctl: KERN_VNODE");
679 if ((vnodebase = malloc(copysize)) == NULL)
680 err(1, NULL);
681 if (sysctl(mib, 2, vnodebase, ©size, NULL, 0) == -1)
682 err(1, "sysctl: KERN_VNODE");
683 if (copysize % sizeof(struct e_vnode))
684 errx(1, "vnode size mismatch");
685 *avnodes = copysize / sizeof(struct e_vnode);
686
687 return (vnodebase);
688 }
689
690 /*
691 * simulate what a running kernel does in in kinfo_vnode
692 */
693 struct e_vnode *
694 kinfo_vnodes(avnodes)
695 int *avnodes;
696 {
697 struct mntlist mountlist;
698 struct mount *mp, mount;
699 struct vnode *vp, vnode;
700 char *vbuf, *evbuf, *bp;
701 int num, numvnodes;
702
703 #define VPTRSZ sizeof(struct vnode *)
704 #define VNODESZ sizeof(struct vnode)
705
706 KGET(V_NUMV, numvnodes);
707 if ((vbuf = malloc((numvnodes + 20) * (VPTRSZ + VNODESZ))) == NULL)
708 err(1, NULL);
709 bp = vbuf;
710 evbuf = vbuf + (numvnodes + 20) * (VPTRSZ + VNODESZ);
711 KGET(V_MOUNTLIST, mountlist);
712 for (num = 0, mp = mountlist.cqh_first; ; mp = mount.mnt_list.cqe_next) {
713 KGET2(mp, &mount, sizeof(mount), "mount entry");
714 for (vp = mount.mnt_vnodelist.lh_first;
715 vp != NULL; vp = vnode.v_mntvnodes.le_next) {
716 KGET2(vp, &vnode, sizeof(vnode), "vnode");
717 if ((bp + VPTRSZ + VNODESZ) > evbuf)
718 /* XXX - should realloc */
719 errx(1, "no more room for vnodes");
720 memmove(bp, &vp, VPTRSZ);
721 bp += VPTRSZ;
722 memmove(bp, &vnode, VNODESZ);
723 bp += VNODESZ;
724 num++;
725 }
726 if (mp == mountlist.cqh_last)
727 break;
728 }
729 *avnodes = num;
730 return ((struct e_vnode *)vbuf);
731 }
732
733 char hdr[]=" LINE RAW CAN OUT HWT LWT COL STATE SESS PGID DISC\n";
734
735 void
736 ttymode()
737 {
738
739 #ifdef sparc
740 ttytype("console", SCONS, 1);
741 ttytype_newcf("zs", SZS, SCZS);
742 #endif
743
744 #ifdef vax
745 /* May fill in this later */
746 #endif
747 #ifdef tahoe
748 if (nl[SNVX].n_type != 0)
749 ttytype_oldcf("vx", SVX, SNVX);
750 if (nl[SNMP].n_type != 0)
751 ttytype_oldcf("mp", SMP, SNMP);
752 #endif
753 #ifdef hp300
754 if (nl[SNITE].n_type != 0)
755 ttytype_oldcf("ite", SITE, SNITE);
756 if (nl[SNDCA].n_type != 0)
757 ttytype_oldcf("dca", SDCA, SNDCA);
758 if (nl[SNDCM].n_type != 0)
759 ttytype_oldcf("dcm", SDCM, SNDCM);
760 if (nl[SNDCL].n_type != 0)
761 ttytype_oldcf("dcl", SDCL, SNDCL);
762 #endif
763 #ifdef mips
764 if (nl[SNDC].n_type != 0)
765 ttytype_oldcf("dc", SDC, SNDC);
766 #endif
767 #ifdef i386
768 if (nl[SCPC].n_type != 0)
769 ttytype_newcf("pc", SPC, SCPC);
770 if (nl[SCCOM].n_type != 0)
771 ttytype_newcf("com", SCOM, SCCOM);
772 #endif
773 #ifdef amiga
774 if (nl[SCSER].n_type != 0)
775 ttytype_newcf("ser", SSER, SCSER);
776 if (nl[SCITE].n_type != 0)
777 ttytype_newcf("ite", SITE, SCITE);
778 if (nl[SCMFCS].n_type != 0)
779 ttytype_newcf("mfcs", SMFCS, SCMFCS);
780 #endif
781 if (nl[SNPTY].n_type != 0)
782 ttytype_oldcf("pty", SPTY, SNPTY);
783 }
784
785 void
786 ttytype_oldcf(name, type, number)
787 char *name;
788 int type, number;
789 {
790 int ntty;
791
792 KGET(number, ntty);
793 ttytype(name, type, ntty);
794 }
795
796 void
797 ttytype_newcf(name, type, config)
798 char *name;
799 int type, config;
800 {
801 struct cfdriver cf;
802 void **cd;
803 int i;
804
805 KGET(config, cf);
806 cd = malloc(cf.cd_ndevs * sizeof(void *));
807 if (!cd)
808 return;
809 KGET2(cf.cd_devs, cd, cf.cd_ndevs * sizeof(void *), "cfdevicep");
810 for (i = cf.cd_ndevs - 1; i >= 0; --i)
811 if (cd[i])
812 break;
813 free(cd);
814 ttytype(name, type, i + 1);
815 }
816
817 void
818 ttytype(name, type, number)
819 char *name;
820 int type, number;
821 {
822 static struct tty **ttyp;
823 static int nttyp;
824 static struct tty tty;
825 int ntty = number, i;
826
827 (void)printf("%d %s %s\n", ntty, name, (ntty == 1) ? "line" : "lines");
828 if (ntty > nttyp) {
829 nttyp = ntty;
830 if ((ttyp = realloc(ttyp, nttyp * sizeof(*ttyp))) == 0)
831 err(1, NULL);
832 }
833 KGET1(type, ttyp, nttyp * sizeof(*ttyp), "tty pointers");
834 (void)printf(hdr);
835 for (i = 0; i < ntty; i++) {
836 if (ttyp[i] == NULL)
837 continue;
838 KGET2(ttyp[i], &tty, sizeof(struct tty), "tty struct");
839 ttyprt(&tty, i);
840 }
841 }
842
843 struct {
844 int flag;
845 char val;
846 } ttystates[] = {
847 { TS_WOPEN, 'W'},
848 { TS_ISOPEN, 'O'},
849 { TS_CARR_ON, 'C'},
850 { TS_TIMEOUT, 'T'},
851 { TS_FLUSH, 'F'},
852 { TS_BUSY, 'B'},
853 { TS_ASLEEP, 'A'},
854 { TS_XCLUDE, 'X'},
855 { TS_TTSTOP, 'S'},
856 { TS_TBLOCK, 'K'},
857 { TS_ASYNC, 'Y'},
858 { TS_BKSL, 'D'},
859 { TS_ERASE, 'E'},
860 { TS_LNCH, 'L'},
861 { TS_TYPEN, 'P'},
862 { TS_CNTTB, 'N'},
863 { 0, '\0'},
864 };
865
866 void
867 ttyprt(tp, line)
868 register struct tty *tp;
869 int line;
870 {
871 register int i, j;
872 pid_t pgid;
873 char *name, state[20];
874
875 if (usenumflag || tp->t_dev == 0 ||
876 (name = devname(tp->t_dev, S_IFCHR)) == NULL)
877 (void)printf("%7d ", line);
878 else
879 (void)printf("%-7s ", name);
880 (void)printf("%3d %4d ", tp->t_rawq.c_cc, tp->t_canq.c_cc);
881 (void)printf("%4d %4d %3d %6d ", tp->t_outq.c_cc,
882 tp->t_hiwat, tp->t_lowat, tp->t_column);
883 for (i = j = 0; ttystates[i].flag; i++)
884 if (tp->t_state&ttystates[i].flag)
885 state[j++] = ttystates[i].val;
886 if (j == 0)
887 state[j++] = '-';
888 state[j] = '\0';
889 (void)printf("%-6s %6x", state, (u_long)tp->t_session & ~KERNBASE);
890 pgid = 0;
891 if (tp->t_pgrp != NULL)
892 KGET2(&tp->t_pgrp->pg_id, &pgid, sizeof(pid_t), "pgid");
893 (void)printf("%6d ", pgid);
894 switch (tp->t_line) {
895 case TTYDISC:
896 (void)printf("term\n");
897 break;
898 case TABLDISC:
899 (void)printf("tab\n");
900 break;
901 case SLIPDISC:
902 (void)printf("slip\n");
903 break;
904 case PPPDISC:
905 (void)printf("ppp\n");
906 break;
907 default:
908 (void)printf("%d\n", tp->t_line);
909 break;
910 }
911 }
912
913 void
914 filemode()
915 {
916 register struct file *fp;
917 struct file *addr;
918 char *buf, flagbuf[16], *fbp;
919 int len, maxfile, nfile;
920 static char *dtypes[] = { "???", "inode", "socket" };
921
922 KGET(FNL_MAXFILE, maxfile);
923 if (totalflag) {
924 KGET(FNL_NFILE, nfile);
925 (void)printf("%3d/%3d files\n", nfile, maxfile);
926 return;
927 }
928 if (getfiles(&buf, &len) == -1)
929 return;
930 /*
931 * Getfiles returns in malloc'd memory a pointer to the first file
932 * structure, and then an array of file structs (whose addresses are
933 * derivable from the previous entry).
934 */
935 addr = ((struct filelist *)buf)->lh_first;
936 fp = (struct file *)(buf + sizeof(struct filelist));
937 nfile = (len - sizeof(struct filelist)) / sizeof(struct file);
938
939 (void)printf("%d/%d open files\n", nfile, maxfile);
940 (void)printf(" LOC TYPE FLG CNT MSG DATA OFFSET\n");
941 for (; (char *)fp < buf + len; addr = fp->f_list.le_next, fp++) {
942 if ((unsigned)fp->f_type > DTYPE_SOCKET)
943 continue;
944 (void)printf("%x ", addr);
945 (void)printf("%-8.8s", dtypes[fp->f_type]);
946 fbp = flagbuf;
947 if (fp->f_flag & FREAD)
948 *fbp++ = 'R';
949 if (fp->f_flag & FWRITE)
950 *fbp++ = 'W';
951 if (fp->f_flag & FAPPEND)
952 *fbp++ = 'A';
953 #ifdef FSHLOCK /* currently gone */
954 if (fp->f_flag & FSHLOCK)
955 *fbp++ = 'S';
956 if (fp->f_flag & FEXLOCK)
957 *fbp++ = 'X';
958 #endif
959 if (fp->f_flag & FASYNC)
960 *fbp++ = 'I';
961 *fbp = '\0';
962 (void)printf("%6s %3d", flagbuf, fp->f_count);
963 (void)printf(" %3d", fp->f_msgcount);
964 (void)printf(" %8.1x", fp->f_data);
965 if (fp->f_offset < 0)
966 (void)printf(" %qx\n", fp->f_offset);
967 else
968 (void)printf(" %qd\n", fp->f_offset);
969 }
970 free(buf);
971 }
972
973 int
974 getfiles(abuf, alen)
975 char **abuf;
976 int *alen;
977 {
978 size_t len;
979 int mib[2];
980 char *buf;
981
982 /*
983 * XXX
984 * Add emulation of KINFO_FILE here.
985 */
986 if (memf != NULL)
987 errx(1, "files on dead kernel, not implemented\n");
988
989 mib[0] = CTL_KERN;
990 mib[1] = KERN_FILE;
991 if (sysctl(mib, 2, NULL, &len, NULL, 0) == -1) {
992 warn("sysctl: KERN_FILE");
993 return (-1);
994 }
995 if ((buf = malloc(len)) == NULL)
996 err(1, NULL);
997 if (sysctl(mib, 2, buf, &len, NULL, 0) == -1) {
998 warn("sysctl: KERN_FILE");
999 return (-1);
1000 }
1001 *abuf = buf;
1002 *alen = len;
1003 return (0);
1004 }
1005
1006 /*
1007 * swapmode is based on a program called swapinfo written
1008 * by Kevin Lahey <kml (at) rokkaku.atl.ga.us>.
1009 */
1010 void
1011 swapmode()
1012 {
1013 char *header;
1014 int hlen, nswap, nswdev, dmmax, nswapmap, niswap, niswdev;
1015 int s, e, div, i, l, avail, nfree, npfree, used;
1016 struct swdevt *sw;
1017 long blocksize, *perdev;
1018 struct map *swapmap, *kswapmap;
1019 struct mapent *mp;
1020
1021 KGET(VM_NSWAP, nswap);
1022 KGET(VM_NSWDEV, nswdev);
1023 KGET(VM_DMMAX, dmmax);
1024 KGET(VM_NSWAPMAP, nswapmap);
1025 KGET(VM_SWAPMAP, kswapmap); /* kernel `swapmap' is a pointer */
1026 if ((sw = malloc(nswdev * sizeof(*sw))) == NULL ||
1027 (perdev = malloc(nswdev * sizeof(*perdev))) == NULL ||
1028 (mp = malloc(nswapmap * sizeof(*mp))) == NULL)
1029 err(1, "malloc");
1030 KGET1(VM_SWDEVT, sw, nswdev * sizeof(*sw), "swdevt");
1031 KGET2((long)kswapmap, mp, nswapmap * sizeof(*mp), "swapmap");
1032
1033 /* Supports sequential swap */
1034 if (nl[VM_NISWAP].n_value != 0) {
1035 KGET(VM_NISWAP, niswap);
1036 KGET(VM_NISWDEV, niswdev);
1037 } else {
1038 niswap = nswap;
1039 niswdev = nswdev;
1040 }
1041
1042 /* First entry in map is `struct map'; rest are mapent's. */
1043 swapmap = (struct map *)mp;
1044 if (nswapmap != swapmap->m_limit - (struct mapent *)kswapmap)
1045 errx(1, "panic: nswapmap goof");
1046
1047 /* Count up swap space. */
1048 nfree = 0;
1049 memset(perdev, 0, nswdev * sizeof(*perdev));
1050 for (mp++; mp->m_addr != 0; mp++) {
1051 s = mp->m_addr; /* start of swap region */
1052 e = mp->m_addr + mp->m_size; /* end of region */
1053 nfree += mp->m_size;
1054
1055 /*
1056 * Swap space is split up among the configured disks.
1057 *
1058 * For interleaved swap devices, the first dmmax blocks
1059 * of swap space some from the first disk, the next dmmax
1060 * blocks from the next, and so on up to niswap blocks.
1061 *
1062 * Sequential swap devices follow the interleaved devices
1063 * (i.e. blocks starting at niswap) in the order in which
1064 * they appear in the swdev table. The size of each device
1065 * will be a multiple of dmmax.
1066 *
1067 * The list of free space joins adjacent free blocks,
1068 * ignoring device boundries. If we want to keep track
1069 * of this information per device, we'll just have to
1070 * extract it ourselves. We know that dmmax-sized chunks
1071 * cannot span device boundaries (interleaved or sequential)
1072 * so we loop over such chunks assigning them to devices.
1073 */
1074 i = -1;
1075 while (s < e) { /* XXX this is inefficient */
1076 int bound = roundup(s+1, dmmax);
1077
1078 if (bound > e)
1079 bound = e;
1080 if (bound <= niswap) {
1081 /* Interleaved swap chunk. */
1082 if (i == -1)
1083 i = (s / dmmax) % niswdev;
1084 perdev[i] += bound - s;
1085 if (++i >= niswdev)
1086 i = 0;
1087 } else {
1088 /* Sequential swap chunk. */
1089 if (i < niswdev) {
1090 i = niswdev;
1091 l = niswap + sw[i].sw_nblks;
1092 }
1093 while (s >= l) {
1094 /* XXX don't die on bogus blocks */
1095 if (i == nswdev-1)
1096 break;
1097 l += sw[++i].sw_nblks;
1098 }
1099 perdev[i] += bound - s;
1100 }
1101 s = bound;
1102 }
1103 }
1104
1105 if (kflag) {
1106 header = "1K-blocks";
1107 blocksize = 1024;
1108 hlen = strlen(header);
1109 } else
1110 header = getbsize(&hlen, &blocksize);
1111 if (!totalflag)
1112 (void)printf("%-11s %*s %8s %8s %8s %s\n",
1113 "Device", hlen, header,
1114 "Used", "Avail", "Capacity", "Type");
1115 div = blocksize / 512;
1116 avail = npfree = 0;
1117 for (i = 0; i < nswdev; i++) {
1118 int xsize, xfree;
1119
1120 if (!totalflag)
1121 (void)printf("/dev/%-6s %*d ",
1122 devname(sw[i].sw_dev, S_IFBLK),
1123 hlen, sw[i].sw_nblks / div);
1124
1125 /*
1126 * Don't report statistics for partitions which have not
1127 * yet been activated via swapon(8).
1128 */
1129 if (!(sw[i].sw_flags & SW_FREED)) {
1130 if (totalflag)
1131 continue;
1132 (void)printf(" *** not available for swapping ***\n");
1133 continue;
1134 }
1135 xsize = sw[i].sw_nblks;
1136 xfree = perdev[i];
1137 used = xsize - xfree;
1138 npfree++;
1139 avail += xsize;
1140 if (totalflag)
1141 continue;
1142 (void)printf("%8d %8d %5.0f%% %s\n",
1143 used / div, xfree / div,
1144 (double)used / (double)xsize * 100.0,
1145 (sw[i].sw_flags & SW_SEQUENTIAL) ?
1146 "Sequential" : "Interleaved");
1147 }
1148
1149 /*
1150 * If only one partition has been set up via swapon(8), we don't
1151 * need to bother with totals.
1152 */
1153 used = avail - nfree;
1154 if (totalflag) {
1155 (void)printf("%dM/%dM swap space\n", used / 2048, avail / 2048);
1156 return;
1157 }
1158 if (npfree > 1) {
1159 (void)printf("%-11s %*d %8d %8d %5.0f%%\n",
1160 "Total", hlen, avail / div, used / div, nfree / div,
1161 (double)used / (double)avail * 100.0);
1162 }
1163 }
1164
1165 void
1166 usage()
1167 {
1168 (void)fprintf(stderr,
1169 "usage: pstat [-Tfknstv] [-M core] [-N system]\n");
1170 exit(1);
1171 }
1172