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