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