pstat.c revision 1.9 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.9 1995/01/18 06:27:32 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 int kflag;
169 char *nlistf = NULL;
170 char *memf = NULL;
171 kvm_t *kd;
172
173 #define SVAR(var) __STRING(var) /* to force expansion */
174 #define KGET(idx, var) \
175 KGET1(idx, &var, sizeof(var), SVAR(var))
176 #define KGET1(idx, p, s, msg) \
177 KGET2(nl[idx].n_value, p, s, msg)
178 #define KGET2(addr, p, s, msg) \
179 if (kvm_read(kd, (u_long)(addr), p, s) != s) \
180 warnx("cannot read %s: %s", msg, kvm_geterr(kd))
181 #define KGETRET(addr, p, s, msg) \
182 if (kvm_read(kd, (u_long)(addr), p, s) != s) { \
183 warnx("cannot read %s: %s", msg, kvm_geterr(kd)); \
184 return (0); \
185 }
186
187 void filemode __P((void));
188 int getfiles __P((char **, int *));
189 struct mount *
190 getmnt __P((struct mount *));
191 struct e_vnode *
192 kinfo_vnodes __P((int *));
193 struct e_vnode *
194 loadvnodes __P((int *));
195 void mount_print __P((struct mount *));
196 void nfs_header __P((void));
197 int nfs_print __P((struct vnode *));
198 void swapmode __P((void));
199 void ttymode __P((void));
200 void ttyprt __P((struct tty *, int));
201 void ttytype __P((char *, int, int));
202 void ttytype_newcf __P((char *, int, int));
203 void ttytype_oldcf __P((char *, int, int));
204 void ufs_header __P((void));
205 int ufs_print __P((struct vnode *));
206 void usage __P((void));
207 void vnode_header __P((void));
208 void vnode_print __P((struct vnode *, struct vnode *));
209 void vnodemode __P((void));
210
211 int
212 main(argc, argv)
213 int argc;
214 char *argv[];
215 {
216 extern char *optarg;
217 extern int optind;
218 int ch, i, quit, ret;
219 int fileflag, swapflag, ttyflag, vnodeflag;
220 char buf[_POSIX2_LINE_MAX];
221
222 fileflag = swapflag = ttyflag = vnodeflag = 0;
223 while ((ch = getopt(argc, argv, "TM:N:fiknstv")) != -1)
224 switch (ch) {
225 case 'f':
226 fileflag = 1;
227 break;
228 case 'M':
229 memf = optarg;
230 break;
231 case 'N':
232 nlistf = optarg;
233 break;
234 case 'n':
235 usenumflag = 1;
236 break;
237 case 's':
238 swapflag = 1;
239 break;
240 case 'T':
241 totalflag = 1;
242 break;
243 case 't':
244 ttyflag = 1;
245 break;
246 case 'k':
247 kflag = 1;
248 break;
249 case 'v':
250 case 'i': /* Backward compatibility. */
251 vnodeflag = 1;
252 break;
253 default:
254 usage();
255 }
256 argc -= optind;
257 argv += optind;
258
259 /*
260 * Discard setgid privileges if not the running kernel so that bad
261 * guys can't print interesting stuff from kernel memory.
262 */
263 if (nlistf != NULL || memf != NULL)
264 (void)setgid(getgid());
265
266 if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == 0)
267 errx(1, "kvm_openfiles: %s", buf);
268 if ((ret = kvm_nlist(kd, nl)) != 0) {
269 if (ret == -1)
270 errx(1, "kvm_nlist: %s", kvm_geterr(kd));
271 for (i = quit = 0; i <= NLMANDATORY; i++)
272 if (!nl[i].n_value) {
273 quit = 1;
274 warnx("undefined symbol: %s\n", nl[i].n_name);
275 }
276 if (quit)
277 exit(1);
278 }
279 if (!(fileflag | vnodeflag | ttyflag | swapflag | totalflag))
280 usage();
281 if (fileflag || totalflag)
282 filemode();
283 if (vnodeflag || totalflag)
284 vnodemode();
285 if (ttyflag)
286 ttymode();
287 if (swapflag || totalflag)
288 swapmode();
289 exit (0);
290 }
291
292 struct e_vnode {
293 struct vnode *avnode;
294 struct vnode vnode;
295 };
296
297 void
298 vnodemode()
299 {
300 register struct e_vnode *e_vnodebase, *endvnode, *evp;
301 register struct vnode *vp;
302 register struct mount *maddr, *mp;
303 int numvnodes;
304
305 e_vnodebase = loadvnodes(&numvnodes);
306 if (totalflag) {
307 (void)printf("%7d vnodes\n", numvnodes);
308 return;
309 }
310 endvnode = e_vnodebase + numvnodes;
311 (void)printf("%d active vnodes\n", numvnodes);
312
313
314 #define ST mp->mnt_stat
315 maddr = NULL;
316 for (evp = e_vnodebase; evp < endvnode; evp++) {
317 vp = &evp->vnode;
318 if (vp->v_mount != maddr) {
319 /*
320 * New filesystem
321 */
322 if ((mp = getmnt(vp->v_mount)) == NULL)
323 continue;
324 maddr = vp->v_mount;
325 mount_print(mp);
326 vnode_header();
327 if (!strncmp(ST.f_fstypename, MOUNT_UFS, MFSNAMELEN) ||
328 !strncmp(ST.f_fstypename, MOUNT_MFS, MFSNAMELEN)) {
329 ufs_header();
330 } else if (!strncmp(ST.f_fstypename, MOUNT_NFS,
331 MFSNAMELEN)) {
332 nfs_header();
333 }
334 (void)printf("\n");
335 }
336 vnode_print(evp->avnode, vp);
337 if (!strncmp(ST.f_fstypename, MOUNT_UFS, MFSNAMELEN) ||
338 !strncmp(ST.f_fstypename, MOUNT_MFS, MFSNAMELEN)) {
339 ufs_print(vp);
340 } else if (!strncmp(ST.f_fstypename, MOUNT_NFS, MFSNAMELEN)) {
341 nfs_print(vp);
342 }
343 (void)printf("\n");
344 }
345 free(e_vnodebase);
346 }
347
348 void
349 vnode_header()
350 {
351 (void)printf("ADDR TYP VFLAG USE HOLD");
352 }
353
354 void
355 vnode_print(avnode, vp)
356 struct vnode *avnode;
357 struct vnode *vp;
358 {
359 char *type, flags[16];
360 char *fp = flags;
361 register int flag;
362
363 /*
364 * set type
365 */
366 switch(vp->v_type) {
367 case VNON:
368 type = "non"; break;
369 case VREG:
370 type = "reg"; break;
371 case VDIR:
372 type = "dir"; break;
373 case VBLK:
374 type = "blk"; break;
375 case VCHR:
376 type = "chr"; break;
377 case VLNK:
378 type = "lnk"; break;
379 case VSOCK:
380 type = "soc"; break;
381 case VFIFO:
382 type = "fif"; break;
383 case VBAD:
384 type = "bad"; break;
385 default:
386 type = "unk"; break;
387 }
388 /*
389 * gather flags
390 */
391 flag = vp->v_flag;
392 if (flag & VROOT)
393 *fp++ = 'R';
394 if (flag & VTEXT)
395 *fp++ = 'T';
396 if (flag & VSYSTEM)
397 *fp++ = 'S';
398 if (flag & VXLOCK)
399 *fp++ = 'L';
400 if (flag & VXWANT)
401 *fp++ = 'W';
402 if (flag & VBWAIT)
403 *fp++ = 'B';
404 if (flag & VALIASED)
405 *fp++ = 'A';
406 if (flag == 0)
407 *fp++ = '-';
408 *fp = '\0';
409 (void)printf("%8x %s %5s %4d %4d",
410 avnode, type, flags, vp->v_usecount, vp->v_holdcnt);
411 }
412
413 void
414 ufs_header()
415 {
416 (void)printf(" FILEID IFLAG RDEV|SZ");
417 }
418
419 int
420 ufs_print(vp)
421 struct vnode *vp;
422 {
423 register int flag;
424 struct inode inode, *ip = &inode;
425 char flagbuf[16], *flags = flagbuf;
426 char *name;
427 mode_t type;
428
429 KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode");
430 flag = ip->i_flag;
431 if (flag & IN_LOCKED)
432 *flags++ = 'L';
433 if (flag & IN_WANTED)
434 *flags++ = 'W';
435 if (flag & IN_RENAME)
436 *flags++ = 'R';
437 if (flag & IN_UPDATE)
438 *flags++ = 'U';
439 if (flag & IN_ACCESS)
440 *flags++ = 'A';
441 if (flag & IN_CHANGE)
442 *flags++ = 'C';
443 if (flag & IN_MODIFIED)
444 *flags++ = 'M';
445 if (flag & IN_SHLOCK)
446 *flags++ = 'S';
447 if (flag & IN_EXLOCK)
448 *flags++ = 'E';
449 if (flag & IN_LWAIT)
450 *flags++ = 'Z';
451 if (flag == 0)
452 *flags++ = '-';
453 *flags = '\0';
454
455 (void)printf(" %6d %5s", ip->i_number, flagbuf);
456 type = ip->i_mode & S_IFMT;
457 if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode))
458 if (usenumflag || ((name = devname(ip->i_rdev, type)) == NULL))
459 (void)printf(" %2d,%-2d",
460 major(ip->i_rdev), minor(ip->i_rdev));
461 else
462 (void)printf(" %7s", name);
463 else
464 (void)printf(" %7qd", ip->i_size);
465 return (0);
466 }
467
468 void
469 nfs_header()
470 {
471 (void)printf(" FILEID NFLAG RDEV|SZ");
472 }
473
474 int
475 nfs_print(vp)
476 struct vnode *vp;
477 {
478 struct nfsnode nfsnode, *np = &nfsnode;
479 char flagbuf[16], *flags = flagbuf;
480 register int flag;
481 char *name;
482 mode_t type;
483
484 KGETRET(VTONFS(vp), &nfsnode, sizeof(nfsnode), "vnode's nfsnode");
485 flag = np->n_flag;
486 if (flag & NFLUSHWANT)
487 *flags++ = 'W';
488 if (flag & NFLUSHINPROG)
489 *flags++ = 'P';
490 if (flag & NMODIFIED)
491 *flags++ = 'M';
492 if (flag & NWRITEERR)
493 *flags++ = 'E';
494 if (flag & NQNFSNONCACHE)
495 *flags++ = 'X';
496 if (flag & NQNFSWRITE)
497 *flags++ = 'O';
498 if (flag & NQNFSEVICTED)
499 *flags++ = 'G';
500 if (flag == 0)
501 *flags++ = '-';
502 *flags = '\0';
503
504 #define VT np->n_vattr
505 (void)printf(" %6d %5s", VT.va_fileid, flagbuf);
506 type = VT.va_mode & S_IFMT;
507 if (S_ISCHR(VT.va_mode) || S_ISBLK(VT.va_mode))
508 if (usenumflag || ((name = devname(VT.va_rdev, type)) == NULL))
509 (void)printf(" %2d,%-2d",
510 major(VT.va_rdev), minor(VT.va_rdev));
511 else
512 (void)printf(" %7s", name);
513 else
514 (void)printf(" %7qd", np->n_size);
515 return (0);
516 }
517
518 /*
519 * Given a pointer to a mount structure in kernel space,
520 * read it in and return a usable pointer to it.
521 */
522 struct mount *
523 getmnt(maddr)
524 struct mount *maddr;
525 {
526 static struct mtab {
527 struct mtab *next;
528 struct mount *maddr;
529 struct mount mount;
530 } *mhead = NULL;
531 register struct mtab *mt;
532
533 for (mt = mhead; mt != NULL; mt = mt->next)
534 if (maddr == mt->maddr)
535 return (&mt->mount);
536 if ((mt = malloc(sizeof(struct mtab))) == NULL)
537 err(1, NULL);
538 KGETRET(maddr, &mt->mount, sizeof(struct mount), "mount table");
539 mt->maddr = maddr;
540 mt->next = mhead;
541 mhead = mt;
542 return (&mt->mount);
543 }
544
545 void
546 mount_print(mp)
547 struct mount *mp;
548 {
549 register int flags;
550 char *type;
551
552 #define ST mp->mnt_stat
553 (void)printf("*** MOUNT ");
554 (void)printf("%s %s on %s", ST.f_fstypename,
555 ST.f_mntfromname, ST.f_mntonname);
556 if (flags = mp->mnt_flag) {
557 char *comma = "(";
558
559 putchar(' ');
560 /* user visable flags */
561 if (flags & MNT_RDONLY) {
562 (void)printf("%srdonly", comma);
563 flags &= ~MNT_RDONLY;
564 comma = ",";
565 }
566 if (flags & MNT_SYNCHRONOUS) {
567 (void)printf("%ssynchronous", comma);
568 flags &= ~MNT_SYNCHRONOUS;
569 comma = ",";
570 }
571 if (flags & MNT_NOEXEC) {
572 (void)printf("%snoexec", comma);
573 flags &= ~MNT_NOEXEC;
574 comma = ",";
575 }
576 if (flags & MNT_NOSUID) {
577 (void)printf("%snosuid", comma);
578 flags &= ~MNT_NOSUID;
579 comma = ",";
580 }
581 if (flags & MNT_NODEV) {
582 (void)printf("%snodev", comma);
583 flags &= ~MNT_NODEV;
584 comma = ",";
585 }
586 if (flags & MNT_EXPORTED) {
587 (void)printf("%sexport", comma);
588 flags &= ~MNT_EXPORTED;
589 comma = ",";
590 }
591 if (flags & MNT_EXRDONLY) {
592 (void)printf("%sexrdonly", comma);
593 flags &= ~MNT_EXRDONLY;
594 comma = ",";
595 }
596 if (flags & MNT_LOCAL) {
597 (void)printf("%slocal", comma);
598 flags &= ~MNT_LOCAL;
599 comma = ",";
600 }
601 if (flags & MNT_QUOTA) {
602 (void)printf("%squota", comma);
603 flags &= ~MNT_QUOTA;
604 comma = ",";
605 }
606 /* filesystem control flags */
607 if (flags & MNT_UPDATE) {
608 (void)printf("%supdate", comma);
609 flags &= ~MNT_UPDATE;
610 comma = ",";
611 }
612 if (flags & MNT_MLOCK) {
613 (void)printf("%slock", comma);
614 flags &= ~MNT_MLOCK;
615 comma = ",";
616 }
617 if (flags & MNT_MWAIT) {
618 (void)printf("%swait", comma);
619 flags &= ~MNT_MWAIT;
620 comma = ",";
621 }
622 if (flags & MNT_MPBUSY) {
623 (void)printf("%sbusy", comma);
624 flags &= ~MNT_MPBUSY;
625 comma = ",";
626 }
627 if (flags & MNT_MPWANT) {
628 (void)printf("%swant", comma);
629 flags &= ~MNT_MPWANT;
630 comma = ",";
631 }
632 if (flags & MNT_UNMOUNT) {
633 (void)printf("%sunmount", comma);
634 flags &= ~MNT_UNMOUNT;
635 comma = ",";
636 }
637 if (flags)
638 (void)printf("%sunknown_flags:%x", comma, flags);
639 (void)printf(")");
640 }
641 (void)printf("\n");
642 #undef ST
643 }
644
645 struct e_vnode *
646 loadvnodes(avnodes)
647 int *avnodes;
648 {
649 int mib[2];
650 size_t copysize;
651 struct e_vnode *vnodebase;
652
653 if (memf != NULL) {
654 /*
655 * do it by hand
656 */
657 return (kinfo_vnodes(avnodes));
658 }
659 mib[0] = CTL_KERN;
660 mib[1] = KERN_VNODE;
661 if (sysctl(mib, 2, NULL, ©size, NULL, 0) == -1)
662 err(1, "sysctl: KERN_VNODE");
663 if ((vnodebase = malloc(copysize)) == NULL)
664 err(1, NULL);
665 if (sysctl(mib, 2, vnodebase, ©size, NULL, 0) == -1)
666 err(1, "sysctl: KERN_VNODE");
667 if (copysize % sizeof(struct e_vnode))
668 errx(1, "vnode size mismatch");
669 *avnodes = copysize / sizeof(struct e_vnode);
670
671 return (vnodebase);
672 }
673
674 /*
675 * simulate what a running kernel does in in kinfo_vnode
676 */
677 struct e_vnode *
678 kinfo_vnodes(avnodes)
679 int *avnodes;
680 {
681 struct mntlist mountlist;
682 struct mount *mp, mount;
683 struct vnode *vp, vnode;
684 char *vbuf, *evbuf, *bp;
685 int num, numvnodes;
686
687 #define VPTRSZ sizeof(struct vnode *)
688 #define VNODESZ sizeof(struct vnode)
689
690 KGET(V_NUMV, numvnodes);
691 if ((vbuf = malloc((numvnodes + 20) * (VPTRSZ + VNODESZ))) == NULL)
692 err(1, NULL);
693 bp = vbuf;
694 evbuf = vbuf + (numvnodes + 20) * (VPTRSZ + VNODESZ);
695 KGET(V_MOUNTLIST, mountlist);
696 for (num = 0, mp = mountlist.cqh_first; ; mp = mp->mnt_list.cqe_next) {
697 KGET2(mp, &mount, sizeof(mount), "mount entry");
698 for (vp = mount.mnt_vnodelist.lh_first;
699 vp != NULL; vp = vp->v_mntvnodes.le_next) {
700 KGET2(vp, &vnode, sizeof(vnode), "vnode");
701 if ((bp + VPTRSZ + VNODESZ) > evbuf)
702 /* XXX - should realloc */
703 errx(1, "no more room for vnodes");
704 memmove(bp, &vp, VPTRSZ);
705 bp += VPTRSZ;
706 memmove(bp, &vnode, VNODESZ);
707 bp += VNODESZ;
708 num++;
709 }
710 if (mp == mountlist.cqh_last)
711 break;
712 }
713 *avnodes = num;
714 return ((struct e_vnode *)vbuf);
715 }
716
717 char hdr[]=" LINE RAW CAN OUT HWT LWT COL STATE SESS PGID DISC\n";
718
719 void
720 ttymode()
721 {
722
723 #ifdef sparc
724 ttytype("console", SCONS, 1);
725 ttytype_newcf("zs", SZS, SCZS);
726 #endif
727
728 #ifdef vax
729 if (nl[SNQD].n_type != 0)
730 qdss();
731 if (nl[SNDZ].n_type != 0)
732 ttytype_oldcf("dz", SDZ, SNDZ);
733 if (nl[SNDH].n_type != 0)
734 ttytype_oldcf("dh", SDH, SNDH);
735 if (nl[SNDMF].n_type != 0)
736 ttytype_oldcf("dmf", SDMF, SNDMF);
737 if (nl[SNDHU].n_type != 0)
738 ttytype_oldcf("dhu", SDHU, SNDHU);
739 if (nl[SNDMZ].n_type != 0)
740 ttytype_oldcf("dmz", SDMZ, SNDMZ);
741 #endif
742 #ifdef tahoe
743 if (nl[SNVX].n_type != 0)
744 ttytype_oldcf("vx", SVX, SNVX);
745 if (nl[SNMP].n_type != 0)
746 ttytype_oldcf("mp", SMP, SNMP);
747 #endif
748 #ifdef hp300
749 if (nl[SNITE].n_type != 0)
750 ttytype_oldcf("ite", SITE, SNITE);
751 if (nl[SNDCA].n_type != 0)
752 ttytype_oldcf("dca", SDCA, SNDCA);
753 if (nl[SNDCM].n_type != 0)
754 ttytype_oldcf("dcm", SDCM, SNDCM);
755 if (nl[SNDCL].n_type != 0)
756 ttytype_oldcf("dcl", SDCL, SNDCL);
757 #endif
758 #ifdef mips
759 if (nl[SNDC].n_type != 0)
760 ttytype_oldcf("dc", SDC, SNDC);
761 #endif
762 #ifdef i386
763 if (nl[SCPC].n_type != 0)
764 ttytype_newcf("pc", SPC, SCPC);
765 if (nl[SCCOM].n_type != 0)
766 ttytype_newcf("com", SCOM, SCCOM);
767 #endif
768 #ifdef amiga
769 if (nl[SCSER].n_type != 0)
770 ttytype_newcf("ser", SSER, SCSER);
771 if (nl[SCITE].n_type != 0)
772 ttytype_newcf("ite", SITE, SCITE);
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