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