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