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