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