pstat.c revision 1.43 1 /* $NetBSD: pstat.c,v 1.43 1998/06/26 19:52:29 tv Exp $ */
2
3 /*-
4 * Copyright (c) 1980, 1991, 1993, 1994
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 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) Copyright (c) 1980, 1991, 1993, 1994\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 static char sccsid[] = "@(#)pstat.c 8.16 (Berkeley) 5/9/95";
45 #else
46 __RCSID("$NetBSD: pstat.c,v 1.43 1998/06/26 19:52:29 tv 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 #define _LKM
57 #include <sys/file.h>
58 #include <ufs/ufs/quota.h>
59 #include <ufs/ufs/inode.h>
60 #define NFS
61 #include <sys/mount.h>
62 #undef NFS
63 #include <sys/uio.h>
64 #include <sys/namei.h>
65 #include <miscfs/union/union.h>
66 #undef _KERNEL
67 #include <sys/stat.h>
68 #include <nfs/nfsproto.h>
69 #include <nfs/rpcv2.h>
70 #include <nfs/nfs.h>
71 #include <nfs/nfsnode.h>
72 #include <sys/ioctl.h>
73 #include <sys/tty.h>
74 #include <sys/conf.h>
75 #include <sys/device.h>
76
77 #include <sys/sysctl.h>
78
79 #include <err.h>
80 #include <kvm.h>
81 #include <limits.h>
82 #include <nlist.h>
83 #include <stdio.h>
84 #include <stdlib.h>
85 #include <string.h>
86 #include <unistd.h>
87
88 #include "swapctl.h"
89
90 struct nlist nl[] = {
91 #define V_MOUNTLIST 0
92 { "_mountlist" }, /* address of head of mount list. */
93 #define V_NUMV 1
94 { "_numvnodes" },
95 #define FNL_NFILE 2
96 {"_nfiles"},
97 #define FNL_MAXFILE 3
98 {"_maxfiles"},
99 #define TTY_NTTY 4
100 {"_tty_count"},
101 #define TTY_TTYLIST 5
102 {"_ttylist"},
103 #define NLMANDATORY TTY_TTYLIST /* names up to here are mandatory */
104 { "" }
105 };
106
107 int usenumflag;
108 int totalflag;
109 int kflag;
110 char *nlistf = NULL;
111 char *memf = NULL;
112 kvm_t *kd;
113
114 struct {
115 int m_flag;
116 const char *m_name;
117 } mnt_flags[] = {
118 { MNT_RDONLY, "rdonly" },
119 { MNT_SYNCHRONOUS, "sync" },
120 { MNT_NOEXEC, "noexec" },
121 { MNT_NOSUID, "nosuid" },
122 { MNT_NODEV, "nodev" },
123 { MNT_UNION, "union" },
124 { MNT_ASYNC, "async" },
125 { MNT_NOCOREDUMP, "nocoredump" },
126 { MNT_EXRDONLY, "exrdonly" },
127 { MNT_EXPORTED, "exported" },
128 { MNT_DEFEXPORTED, "defexported" },
129 { MNT_EXPORTANON, "exportanon" },
130 { MNT_EXKERB, "exkerb" },
131 { MNT_LOCAL, "local" },
132 { MNT_QUOTA, "quota" },
133 { MNT_ROOTFS, "rootfs" },
134 { MNT_UPDATE, "update" },
135 { MNT_DELEXPORT, "delexport" },
136 { MNT_RELOAD, "reload" },
137 { MNT_FORCE, "force" },
138 { MNT_MWAIT, "wait" },
139 { MNT_UNMOUNT, "unmount" },
140 { MNT_WANTRDWR, "wantrdwr" },
141 { 0 }
142 };
143
144
145 #define SVAR(var) __STRING(var) /* to force expansion */
146 #define KGET(idx, var) \
147 KGET1(idx, &var, sizeof(var), SVAR(var))
148 #define KGET1(idx, p, s, msg) \
149 KGET2(nl[idx].n_value, p, s, msg)
150 #define KGET2(addr, p, s, msg) \
151 if (kvm_read(kd, (u_long)(addr), p, s) != s) \
152 warnx("cannot read %s: %s", msg, kvm_geterr(kd))
153 #define KGETRET(addr, p, s, msg) \
154 if (kvm_read(kd, (u_long)(addr), p, s) != s) { \
155 warnx("cannot read %s: %s", msg, kvm_geterr(kd)); \
156 return (0); \
157 }
158
159 void filemode __P((void));
160 int getfiles __P((char **, int *));
161 struct mount *
162 getmnt __P((struct mount *));
163 struct e_vnode *
164 kinfo_vnodes __P((int *));
165 struct e_vnode *
166 loadvnodes __P((int *));
167 int main __P((int, char **));
168 void mount_print __P((struct mount *));
169 void nfs_header __P((void));
170 int nfs_print __P((struct vnode *));
171 void ttymode __P((void));
172 void ttyprt __P((struct tty *));
173 void ufs_getflags __P((struct vnode *, struct inode *, char *));
174 void ufs_header __P((void));
175 int ufs_print __P((struct vnode *));
176 int ext2fs_print __P((struct vnode *));
177 void union_header __P((void));
178 int union_print __P((struct vnode *));
179 void usage __P((void));
180 void vnode_header __P((void));
181 void vnode_print __P((struct vnode *, struct vnode *));
182 void vnodemode __P((void));
183
184 int
185 main(argc, argv)
186 int argc;
187 char *argv[];
188 {
189 extern char *optarg;
190 extern int optind;
191 int ch, i, quit, ret;
192 int fileflag, swapflag, ttyflag, vnodeflag;
193 char buf[_POSIX2_LINE_MAX];
194
195 fileflag = swapflag = ttyflag = vnodeflag = 0;
196 while ((ch = getopt(argc, argv, "TM:N:fiknstv")) != -1)
197 switch (ch) {
198 case 'f':
199 fileflag = 1;
200 break;
201 case 'M':
202 memf = optarg;
203 break;
204 case 'N':
205 nlistf = optarg;
206 break;
207 case 'n':
208 usenumflag = 1;
209 break;
210 case 's':
211 swapflag = 1;
212 break;
213 case 'T':
214 totalflag = 1;
215 break;
216 case 't':
217 ttyflag = 1;
218 break;
219 case 'k':
220 kflag = 1;
221 break;
222 case 'v':
223 case 'i': /* Backward compatibility. */
224 vnodeflag = 1;
225 break;
226 default:
227 usage();
228 }
229 argc -= optind;
230 argv += optind;
231
232 /*
233 * Discard setgid privileges if not the running kernel so that bad
234 * guys can't print interesting stuff from kernel memory.
235 */
236 if (nlistf != NULL || memf != NULL)
237 (void)setgid(getgid());
238
239 if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == 0)
240 errx(1, "kvm_openfiles: %s", buf);
241 if ((ret = kvm_nlist(kd, nl)) != 0) {
242 if (ret == -1)
243 errx(1, "kvm_nlist: %s", kvm_geterr(kd));
244 for (i = quit = 0; i <= NLMANDATORY; i++)
245 if (!nl[i].n_value) {
246 quit = 1;
247 warnx("undefined symbol: %s", nl[i].n_name);
248 }
249 if (quit)
250 exit(1);
251 }
252 if (!(fileflag | vnodeflag | ttyflag | swapflag | totalflag))
253 usage();
254 if (fileflag || totalflag)
255 filemode();
256 if (vnodeflag || totalflag)
257 vnodemode();
258 if (ttyflag)
259 ttymode();
260 if (swapflag || totalflag)
261 list_swap(0, kflag, 0, totalflag, 1);
262 exit (0);
263 }
264
265 struct e_vnode {
266 struct vnode *avnode;
267 struct vnode vnode;
268 };
269
270 void
271 vnodemode()
272 {
273 struct e_vnode *e_vnodebase, *endvnode, *evp;
274 struct vnode *vp;
275 struct mount *maddr, *mp;
276 int numvnodes;
277
278 mp = NULL;
279 e_vnodebase = loadvnodes(&numvnodes);
280 if (totalflag) {
281 (void)printf("%7d vnodes\n", numvnodes);
282 return;
283 }
284 endvnode = e_vnodebase + numvnodes;
285 (void)printf("%d active vnodes\n", numvnodes);
286
287
288 #define ST mp->mnt_stat
289 maddr = NULL;
290 for (evp = e_vnodebase; evp < endvnode; evp++) {
291 vp = &evp->vnode;
292 if (vp->v_mount != maddr) {
293 /*
294 * New filesystem
295 */
296 if ((mp = getmnt(vp->v_mount)) == NULL)
297 continue;
298 maddr = vp->v_mount;
299 mount_print(mp);
300 vnode_header();
301 if (!strncmp(ST.f_fstypename, MOUNT_FFS, MFSNAMELEN) ||
302 !strncmp(ST.f_fstypename, MOUNT_MFS, MFSNAMELEN))
303 ufs_header();
304 else if (!strncmp(ST.f_fstypename, MOUNT_NFS,
305 MFSNAMELEN))
306 nfs_header();
307 else if (!strncmp(ST.f_fstypename, MOUNT_EXT2FS,
308 MFSNAMELEN))
309 ufs_header();
310 else if (!strcmp(ST.f_fstypename, "union"))
311 union_header();
312 (void)printf("\n");
313 }
314 vnode_print(evp->avnode, vp);
315 if (!strncmp(ST.f_fstypename, MOUNT_FFS, MFSNAMELEN) ||
316 !strncmp(ST.f_fstypename, MOUNT_MFS, MFSNAMELEN)) {
317 ufs_print(vp);
318 } else if (!strncmp(ST.f_fstypename, MOUNT_NFS, MFSNAMELEN)) {
319 nfs_print(vp);
320 } else if (!strncmp(ST.f_fstypename, MOUNT_EXT2FS, MFSNAMELEN)) {
321 ext2fs_print(vp);
322 }
323 (void)printf("\n");
324 }
325 free(e_vnodebase);
326 }
327
328 void
329 vnode_header()
330 {
331 (void)printf("ADDR TYP VFLAG USE HOLD TAG");
332 }
333
334 void
335 vnode_print(avnode, vp)
336 struct vnode *avnode;
337 struct vnode *vp;
338 {
339 char *type, flags[16];
340 char *fp = flags;
341 int flag;
342
343 /*
344 * set type
345 */
346 switch (vp->v_type) {
347 case VNON:
348 type = "non"; break;
349 case VREG:
350 type = "reg"; break;
351 case VDIR:
352 type = "dir"; break;
353 case VBLK:
354 type = "blk"; break;
355 case VCHR:
356 type = "chr"; break;
357 case VLNK:
358 type = "lnk"; break;
359 case VSOCK:
360 type = "soc"; break;
361 case VFIFO:
362 type = "fif"; break;
363 case VBAD:
364 type = "bad"; break;
365 default:
366 type = "unk"; break;
367 }
368 /*
369 * gather flags
370 */
371 flag = vp->v_flag;
372 if (flag & VROOT)
373 *fp++ = 'R';
374 if (flag & VTEXT)
375 *fp++ = 'T';
376 if (flag & VSYSTEM)
377 *fp++ = 'S';
378 if (flag & VISTTY)
379 *fp++ = 'I';
380 if (flag & VXLOCK)
381 *fp++ = 'L';
382 if (flag & VXWANT)
383 *fp++ = 'W';
384 if (flag & VBWAIT)
385 *fp++ = 'B';
386 if (flag & VALIASED)
387 *fp++ = 'A';
388 if (flag & VDIROP)
389 *fp++ = 'D';
390 if (flag == 0)
391 *fp++ = '-';
392 *fp = '\0';
393 (void)printf("%8lx %s %5s %4d %4ld %4d",
394 (long)avnode, type, flags, vp->v_usecount, (long)vp->v_holdcnt,
395 vp->v_tag);
396 }
397
398 void
399 ufs_getflags(vp, ip, flags)
400 struct vnode *vp;
401 struct inode *ip;
402 char *flags;
403 {
404 int flag;
405
406 /*
407 * XXX need to to locking state.
408 */
409
410 flag = ip->i_flag;
411 if (flag & IN_RENAME)
412 *flags++ = 'R';
413 if (flag & IN_UPDATE)
414 *flags++ = 'U';
415 if (flag & IN_ACCESS)
416 *flags++ = 'A';
417 if (flag & IN_CHANGE)
418 *flags++ = 'C';
419 if (flag & IN_MODIFIED)
420 *flags++ = 'M';
421 if (flag & IN_SHLOCK)
422 *flags++ = 'S';
423 if (flag & IN_EXLOCK)
424 *flags++ = 'E';
425 if (flag == 0)
426 *flags++ = '-';
427 *flags = '\0';
428
429 }
430
431 void
432 ufs_header()
433 {
434 (void)printf(" FILEID IFLAG RDEV|SZ");
435 }
436
437 int
438 ufs_print(vp)
439 struct vnode *vp;
440 {
441 struct inode inode, *ip = &inode;
442 char flagbuf[16];
443 char *name;
444 mode_t type;
445
446 KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode");
447 ufs_getflags(vp, ip, flagbuf);
448 (void)printf(" %6d %5s", ip->i_number, flagbuf);
449 type = ip->i_ffs_mode & S_IFMT;
450 if (S_ISCHR(ip->i_ffs_mode) || S_ISBLK(ip->i_ffs_mode))
451 if (usenumflag || ((name = devname(ip->i_ffs_rdev, type)) == NULL))
452 (void)printf(" %2d,%-2d",
453 major(ip->i_ffs_rdev), minor(ip->i_ffs_rdev));
454 else
455 (void)printf(" %7s", name);
456 else
457 (void)printf(" %7qd", (long long)ip->i_ffs_size);
458 return (0);
459 }
460
461 int
462 ext2fs_print(vp)
463 struct vnode *vp;
464 {
465 struct inode inode, *ip = &inode;
466 char flagbuf[16];
467 char *name;
468 mode_t type;
469
470 KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode");
471 ufs_getflags(vp, ip, flagbuf);
472 (void)printf(" %6d %5s", ip->i_number, flagbuf);
473 type = ip->i_e2fs_mode & S_IFMT;
474 if (S_ISCHR(ip->i_e2fs_mode) || S_ISBLK(ip->i_e2fs_mode))
475 if (usenumflag || ((name = devname(ip->i_din.e2fs_din.e2di_rdev,
476 type)) == NULL))
477 (void)printf(" %2d,%-2d",
478 major(ip->i_din.e2fs_din.e2di_rdev),
479 minor(ip->i_din.e2fs_din.e2di_rdev));
480 else
481 (void)printf(" %7s", name);
482 else
483 (void)printf(" %7u", (u_int)ip->i_e2fs_size);
484 return (0);
485 }
486
487 void
488 nfs_header()
489 {
490 (void)printf(" FILEID NFLAG RDEV|SZ");
491 }
492
493 int
494 nfs_print(vp)
495 struct vnode *vp;
496 {
497 struct nfsnode nfsnode, *np = &nfsnode;
498 char flagbuf[16], *flags = flagbuf;
499 int flag;
500 struct vattr va;
501 char *name;
502 mode_t type;
503
504 KGETRET(VTONFS(vp), &nfsnode, sizeof(nfsnode), "vnode's nfsnode");
505 flag = np->n_flag;
506 if (flag & NFLUSHWANT)
507 *flags++ = 'W';
508 if (flag & NFLUSHINPROG)
509 *flags++ = 'P';
510 if (flag & NMODIFIED)
511 *flags++ = 'M';
512 if (flag & NWRITEERR)
513 *flags++ = 'E';
514 if (flag & NQNFSNONCACHE)
515 *flags++ = 'X';
516 if (flag & NQNFSWRITE)
517 *flags++ = 'O';
518 if (flag & NQNFSEVICTED)
519 *flags++ = 'G';
520 if (flag == 0)
521 *flags++ = '-';
522 *flags = '\0';
523
524 KGETRET(np->n_vattr, &va, sizeof(va), "vnode attr");
525 (void)printf(" %6ld %5s", (long)va.va_fileid, flagbuf);
526 type = va.va_mode & S_IFMT;
527 if (S_ISCHR(va.va_mode) || S_ISBLK(va.va_mode))
528 if (usenumflag || ((name = devname(va.va_rdev, type)) == NULL))
529 (void)printf(" %2d,%-2d",
530 major(va.va_rdev), minor(va.va_rdev));
531 else
532 (void)printf(" %7s", name);
533 else
534 (void)printf(" %7qd", (long long)np->n_size);
535 return (0);
536 }
537
538 void
539 union_header()
540 {
541 (void)printf(" UPPER LOWER");
542 }
543
544 int
545 union_print(vp)
546 struct vnode *vp;
547 {
548 struct union_node unode, *up = &unode;
549
550 KGETRET(VTOUNION(vp), &unode, sizeof(unode), "vnode's unode");
551
552 (void)printf(" %8lx %8lx", (long)up->un_uppervp, (long)up->un_lowervp);
553 return (0);
554 }
555
556 /*
557 * Given a pointer to a mount structure in kernel space,
558 * read it in and return a usable pointer to it.
559 */
560 struct mount *
561 getmnt(maddr)
562 struct mount *maddr;
563 {
564 static struct mtab {
565 struct mtab *next;
566 struct mount *maddr;
567 struct mount mount;
568 } *mhead = NULL;
569 struct mtab *mt;
570
571 for (mt = mhead; mt != NULL; mt = mt->next)
572 if (maddr == mt->maddr)
573 return (&mt->mount);
574 if ((mt = malloc(sizeof(struct mtab))) == NULL)
575 err(1, "malloc");
576 KGETRET(maddr, &mt->mount, sizeof(struct mount), "mount table");
577 mt->maddr = maddr;
578 mt->next = mhead;
579 mhead = mt;
580 return (&mt->mount);
581 }
582
583 void
584 mount_print(mp)
585 struct mount *mp;
586 {
587 int flags;
588
589 (void)printf("*** MOUNT %s %s on %s", ST.f_fstypename,
590 ST.f_mntfromname, ST.f_mntonname);
591 if ((flags = mp->mnt_flag) != 0) {
592 int i;
593 const char *sep = " (";
594
595 for (i = 0; mnt_flags[i].m_flag; i++) {
596 if (flags & mnt_flags[i].m_flag) {
597 (void)printf("%s%s", sep, mnt_flags[i].m_name);
598 flags &= ~mnt_flags[i].m_flag;
599 sep = ",";
600 }
601 }
602 if (flags)
603 (void)printf("%sunknown_flags:%x", sep, flags);
604 (void)printf(")");
605 }
606 (void)printf("\n");
607 }
608
609 struct e_vnode *
610 loadvnodes(avnodes)
611 int *avnodes;
612 {
613 int mib[2];
614 size_t copysize;
615 struct e_vnode *vnodebase;
616
617 if (memf != NULL) {
618 /*
619 * do it by hand
620 */
621 return (kinfo_vnodes(avnodes));
622 }
623 mib[0] = CTL_KERN;
624 mib[1] = KERN_VNODE;
625 if (sysctl(mib, 2, NULL, ©size, NULL, 0) == -1)
626 err(1, "sysctl: KERN_VNODE");
627 if ((vnodebase = malloc(copysize)) == NULL)
628 err(1, "malloc");
629 if (sysctl(mib, 2, vnodebase, ©size, NULL, 0) == -1)
630 err(1, "sysctl: KERN_VNODE");
631 if (copysize % sizeof(struct e_vnode))
632 errx(1, "vnode size mismatch");
633 *avnodes = copysize / sizeof(struct e_vnode);
634
635 return (vnodebase);
636 }
637
638 /*
639 * simulate what a running kernel does in in kinfo_vnode
640 */
641 struct e_vnode *
642 kinfo_vnodes(avnodes)
643 int *avnodes;
644 {
645 struct mntlist mountlist;
646 struct mount *mp, mount;
647 struct vnode *vp, vnode;
648 char *vbuf, *evbuf, *bp;
649 int num, numvnodes;
650
651 #define VPTRSZ sizeof(struct vnode *)
652 #define VNODESZ sizeof(struct vnode)
653
654 KGET(V_NUMV, numvnodes);
655 if ((vbuf = malloc((numvnodes + 20) * (VPTRSZ + VNODESZ))) == NULL)
656 err(1, "malloc");
657 bp = vbuf;
658 evbuf = vbuf + (numvnodes + 20) * (VPTRSZ + VNODESZ);
659 KGET(V_MOUNTLIST, mountlist);
660 for (num = 0, mp = mountlist.cqh_first; ; mp = mount.mnt_list.cqe_next) {
661 KGET2(mp, &mount, sizeof(mount), "mount entry");
662 for (vp = mount.mnt_vnodelist.lh_first;
663 vp != NULL; vp = vnode.v_mntvnodes.le_next) {
664 KGET2(vp, &vnode, sizeof(vnode), "vnode");
665 if ((bp + VPTRSZ + VNODESZ) > evbuf)
666 /* XXX - should realloc */
667 errx(1, "no more room for vnodes");
668 memmove(bp, &vp, VPTRSZ);
669 bp += VPTRSZ;
670 memmove(bp, &vnode, VNODESZ);
671 bp += VNODESZ;
672 num++;
673 }
674 if (mp == mountlist.cqh_last)
675 break;
676 }
677 *avnodes = num;
678 return ((struct e_vnode *)vbuf);
679 }
680
681 char hdr[]=" LINE RAW CAN OUT HWT LWT COL STATE SESS PGID DISC\n";
682 int ttyspace = 128;
683
684 void
685 ttymode()
686 {
687 int ntty;
688 struct ttylist_head tty_head;
689 struct tty *tp, tty;
690
691 KGET(TTY_NTTY, ntty);
692 (void)printf("%d terminal device%s\n", ntty, ntty == 1 ? "" : "s");
693 KGET(TTY_TTYLIST, tty_head);
694 (void)printf(hdr);
695 for (tp = tty_head.tqh_first; tp; tp = tty.tty_link.tqe_next) {
696 KGET2(tp, &tty, sizeof tty, "tty struct");
697 ttyprt(&tty);
698 }
699 }
700
701 struct {
702 int flag;
703 char val;
704 } ttystates[] = {
705 { TS_ISOPEN, 'O'},
706 { TS_DIALOUT, '>'},
707 { TS_CARR_ON, 'C'},
708 { TS_TIMEOUT, 'T'},
709 { TS_FLUSH, 'F'},
710 { TS_BUSY, 'B'},
711 { TS_ASLEEP, 'A'},
712 { TS_XCLUDE, 'X'},
713 { TS_TTSTOP, 'S'},
714 { TS_TBLOCK, 'K'},
715 { TS_ASYNC, 'Y'},
716 { TS_BKSL, 'D'},
717 { TS_ERASE, 'E'},
718 { TS_LNCH, 'L'},
719 { TS_TYPEN, 'P'},
720 { TS_CNTTB, 'N'},
721 { 0, '\0'},
722 };
723
724 void
725 ttyprt(tp)
726 struct tty *tp;
727 {
728 int i, j;
729 pid_t pgid;
730 char *name, state[20];
731
732 if (usenumflag || (name = devname(tp->t_dev, S_IFCHR)) == NULL)
733 (void)printf("0x%3x:%1x ", major(tp->t_dev), minor(tp->t_dev));
734 else
735 (void)printf("%-7s ", name);
736 (void)printf("%2d %3d ", tp->t_rawq.c_cc, tp->t_canq.c_cc);
737 (void)printf("%3d %4d %3d %7d ", tp->t_outq.c_cc,
738 tp->t_hiwat, tp->t_lowat, tp->t_column);
739 for (i = j = 0; ttystates[i].flag; i++)
740 if (tp->t_state&ttystates[i].flag)
741 state[j++] = ttystates[i].val;
742 if (tp->t_wopen)
743 state[j++] = 'W';
744 if (j == 0)
745 state[j++] = '-';
746 state[j] = '\0';
747 (void)printf("%-6s %8lX", state, (u_long)tp->t_session);
748 pgid = 0;
749 if (tp->t_pgrp != NULL)
750 KGET2(&tp->t_pgrp->pg_id, &pgid, sizeof(pid_t), "pgid");
751 (void)printf("%6d ", pgid);
752 switch (tp->t_line) {
753 case TTYDISC:
754 (void)printf("term\n");
755 break;
756 case TABLDISC:
757 (void)printf("tab\n");
758 break;
759 case SLIPDISC:
760 (void)printf("slip\n");
761 break;
762 case PPPDISC:
763 (void)printf("ppp\n");
764 break;
765 case STRIPDISC:
766 (void)printf("strip\n");
767 break;
768 default:
769 (void)printf("%d\n", tp->t_line);
770 break;
771 }
772 }
773
774 void
775 filemode()
776 {
777 struct file *fp;
778 struct file *addr;
779 char *buf, flagbuf[16], *fbp;
780 int len, maxfile, nfile;
781 static char *dtypes[] = { "???", "inode", "socket" };
782
783 KGET(FNL_MAXFILE, maxfile);
784 if (totalflag) {
785 KGET(FNL_NFILE, nfile);
786 (void)printf("%3d/%3d files\n", nfile, maxfile);
787 return;
788 }
789 if (getfiles(&buf, &len) == -1)
790 return;
791 /*
792 * Getfiles returns in malloc'd memory a pointer to the first file
793 * structure, and then an array of file structs (whose addresses are
794 * derivable from the previous entry).
795 */
796 addr = ((struct filelist *)buf)->lh_first;
797 fp = (struct file *)(buf + sizeof(struct filelist));
798 nfile = (len - sizeof(struct filelist)) / sizeof(struct file);
799
800 (void)printf("%d/%d open files\n", nfile, maxfile);
801 (void)printf(" LOC TYPE FLG CNT MSG DATA OFFSET\n");
802 for (; (char *)fp < buf + len; addr = fp->f_list.le_next, fp++) {
803 if ((unsigned)fp->f_type > DTYPE_SOCKET)
804 continue;
805 (void)printf("%lx ", (long)addr);
806 (void)printf("%-8.8s", dtypes[fp->f_type]);
807 fbp = flagbuf;
808 if (fp->f_flag & FREAD)
809 *fbp++ = 'R';
810 if (fp->f_flag & FWRITE)
811 *fbp++ = 'W';
812 if (fp->f_flag & FAPPEND)
813 *fbp++ = 'A';
814 #ifdef FSHLOCK /* currently gone */
815 if (fp->f_flag & FSHLOCK)
816 *fbp++ = 'S';
817 if (fp->f_flag & FEXLOCK)
818 *fbp++ = 'X';
819 #endif
820 if (fp->f_flag & FASYNC)
821 *fbp++ = 'I';
822 *fbp = '\0';
823 (void)printf("%6s %3d", flagbuf, fp->f_count);
824 (void)printf(" %3d", fp->f_msgcount);
825 (void)printf(" %8.1lx", (long)fp->f_data);
826 if (fp->f_offset < 0)
827 (void)printf(" %qx\n", (long long)fp->f_offset);
828 else
829 (void)printf(" %qd\n", (long long)fp->f_offset);
830 }
831 free(buf);
832 }
833
834 int
835 getfiles(abuf, alen)
836 char **abuf;
837 int *alen;
838 {
839 size_t len;
840 int mib[2];
841 char *buf;
842
843 /*
844 * XXX
845 * Add emulation of KINFO_FILE here.
846 */
847 if (memf != NULL)
848 errx(1, "files on dead kernel, not implemented\n");
849
850 mib[0] = CTL_KERN;
851 mib[1] = KERN_FILE;
852 if (sysctl(mib, 2, NULL, &len, NULL, 0) == -1) {
853 warn("sysctl: KERN_FILE");
854 return (-1);
855 }
856 if ((buf = malloc(len)) == NULL)
857 err(1, "malloc");
858 if (sysctl(mib, 2, buf, &len, NULL, 0) == -1) {
859 warn("sysctl: KERN_FILE");
860 return (-1);
861 }
862 *abuf = buf;
863 *alen = len;
864 return (0);
865 }
866
867 void
868 usage()
869 {
870 (void)fprintf(stderr,
871 "usage: pstat [-T|-f|-s|-t|-v] [-kn] [-M core] [-N system]\n");
872 exit(1);
873 }
874