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