Add prototype for ciss_print0 in place.
[dragonfly.git] / usr.sbin / pstat / pstat.c
1 /*-
2  * Copyright (c) 1980, 1991, 1993, 1994
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * @(#) Copyright (c) 1980, 1991, 1993, 1994 The Regents of the University of California.  All rights reserved.
34  * @(#)pstat.c  8.16 (Berkeley) 5/9/95
35  * $FreeBSD: src/usr.sbin/pstat/pstat.c,v 1.49.2.5 2002/07/12 09:12:49 des Exp $
36  * $DragonFly: src/usr.sbin/pstat/pstat.c,v 1.13 2004/12/18 22:48:04 swildner Exp $
37  */
38
39 #define _KERNEL_STRUCTURES
40
41 #include <sys/param.h>
42 #include <sys/time.h>
43 #include <sys/vnode.h>
44 #include <sys/ucred.h>
45 #include <sys/file.h>
46 #include <vfs/ufs/quota.h>
47 #include <vfs/ufs/inode.h>
48 #include <sys/mount.h>
49 #include <sys/uio.h>
50 #include <sys/namei.h>
51 #include <vfs/union/union.h>
52 #include <sys/stat.h>
53 #include <nfs/rpcv2.h>
54 #include <nfs/nfsproto.h>
55 #include <nfs/nfs.h>
56 #include <nfs/nfsnode.h>
57 #include <sys/ioctl.h>
58 #include <sys/ioctl_compat.h>   /* XXX NTTYDISC is too well hidden */
59 #include <sys/tty.h>
60 #include <sys/conf.h>
61 #include <sys/blist.h>
62
63 #include <sys/kinfo.h>
64 #include <sys/user.h>
65 #include <sys/sysctl.h>
66
67 #include <err.h>
68 #include <fcntl.h>
69 #include <kvm.h>
70 #include <limits.h>
71 #include <nlist.h>
72 #include <stdio.h>
73 #include <stdlib.h>
74 #include <string.h>
75 #include <unistd.h>
76
77 #ifdef USE_KCORE
78 #  define KCORE_KINFO_WRAPPER
79 #  include <kcore.h>
80
81 struct kcore_data *KCORE_KVM_GLOBAL;
82
83 #else
84 #  include <kinfo.h>
85 #endif
86
87 enum {
88         NL_MOUNTLIST,
89         NL_NUMVNODES,
90         NL_RC_TTY,
91         NL_NRC_TTY,
92         NL_CY_TTY,
93         NL_NCY_TTY,
94         NL_SI_TTY,
95         NL_SI_NPORTS,
96 };
97
98 const size_t NL_LAST_MANDATORY = NL_NUMVNODES;
99
100 struct nlist nl[] = {
101         { "_mountlist", 0, 0, 0, 0 },   /* address of head of mount list. */
102         { "_numvnodes", 0, 0, 0, 0 },
103         { "_rc_tty", 0, 0, 0, 0 },
104         { "_nrc_tty", 0, 0, 0, 0 },
105         { "_cy_tty", 0, 0, 0, 0 },
106         { "_ncy_tty", 0, 0, 0, 0 },
107         { "_si__tty", 0, 0, 0, 0 },
108         { "_si_Nports", 0, 0, 0, 0 },
109         { "", 0, 0, 0, 0 }
110 };
111
112 int     usenumflag;
113 int     totalflag;
114 int     swapflag;
115 char    *nlistf = NULL;
116 char    *memf   = NULL;
117 kvm_t   *kd;
118
119 const char      *usagestr;
120
121 struct {
122         int m_flag;
123         const char *m_name;
124 } mnt_flags[] = {
125         { MNT_RDONLY, "rdonly" },
126         { MNT_SYNCHRONOUS, "sync" },
127         { MNT_NOEXEC, "noexec" },
128         { MNT_NOSUID, "nosuid" },
129         { MNT_NODEV, "nodev" },
130         { MNT_UNION, "union" },
131         { MNT_ASYNC, "async" },
132         { MNT_SUIDDIR, "suiddir" },
133         { MNT_SOFTDEP, "softdep" },
134         { MNT_NOSYMFOLLOW, "nosymfollow" },
135         { MNT_NOATIME, "noatime" },
136         { MNT_NOCLUSTERR, "noclusterread" },
137         { MNT_NOCLUSTERW, "noclusterwrite" },
138         { MNT_EXRDONLY, "exrdonly" },
139         { MNT_EXPORTED, "exported" },
140         { MNT_DEFEXPORTED, "defexported" },
141         { MNT_EXPORTANON, "exportanon" },
142         { MNT_EXKERB, "exkerb" },
143         { MNT_EXPUBLIC, "public" },
144         { MNT_LOCAL, "local" },
145         { MNT_QUOTA, "quota" },
146         { MNT_ROOTFS, "rootfs" },
147         { MNT_USER, "user" },
148         { MNT_IGNORE, "ignore" },
149         { MNT_UPDATE, "update" },
150         { MNT_DELEXPORT, "delexport" },
151         { MNT_RELOAD, "reload" },
152         { MNT_FORCE, "force" },
153         { 0, NULL }
154 };
155
156
157 #define SVAR(var) __STRING(var) /* to force expansion */
158 #define KGET(idx, var)                                                  \
159         KGET1(idx, &var, sizeof(var), SVAR(var))
160 #define KGET1(idx, p, s, msg)                                           \
161         KGET2(nl[idx].n_value, p, s, msg)
162 #define KGET2(addr, p, s, msg)                                          \
163         if (kvm_read(kd, (u_long)(addr), p, s) != s)                    \
164                 warnx("cannot read %s: %s", msg, kvm_geterr(kd))
165 #define KGETN(idx, var)                                                 \
166         KGET1N(idx, &var, sizeof(var), SVAR(var))
167 #define KGET1N(idx, p, s, msg)                                          \
168         KGET2N(nl[idx].n_value, p, s, msg)
169 #define KGET2N(addr, p, s, msg)                                         \
170         ((kvm_read(kd, (u_long)(addr), p, s) == s) ? 1 : 0)
171 #define KGETRET(addr, p, s, msg)                                        \
172         if (kvm_read(kd, (u_long)(addr), p, s) != s) {                  \
173                 warnx("cannot read %s: %s", msg, kvm_geterr(kd));       \
174                 return (0);                                             \
175         }
176
177 void    filemode(void);
178 int     getfiles(char **, int *);
179 struct mount *
180         getmnt(struct mount *);
181 struct e_vnode *
182         kinfo_vnodes(int *);
183 struct e_vnode *
184         loadvnodes(int *);
185 void    mount_print(struct mount *);
186 void    nfs_header(void);
187 int     nfs_print(struct vnode *);
188 void    swapmode(void);
189 void    ttymode(void);
190 void    ttyprt(struct tty *, int);
191 void    ttytype(struct tty *, const char *, int, int, int);
192 void    ufs_header(void);
193 int     ufs_print(struct vnode *);
194 void    union_header(void);
195 int     union_print(struct vnode *);
196 static void usage(void);
197 void    vnode_header(void);
198 void    vnode_print(struct vnode *, struct vnode *);
199 void    vnodemode(void);
200
201 int
202 main(int argc, char **argv)
203 {
204         int ch, ret;
205         int fileflag, ttyflag, vnodeflag;
206         char buf[_POSIX2_LINE_MAX];
207         const char *opts;
208
209         fileflag = swapflag = ttyflag = vnodeflag = 0;
210
211         /* We will behave like good old swapinfo if thus invoked */
212         opts = strrchr(argv[0],'/');
213         if (opts)
214                 opts++;
215         else
216                 opts = argv[0];
217         if (!strcmp(opts,"swapinfo")) {
218                 swapflag = 1;
219                 opts = "kM:N:";
220                 usagestr = "swapinfo [-k] [-M core] [-N system]";
221         } else {
222                 opts = "TM:N:fiknstv";
223                 usagestr = "pstat [-Tfknst] [-M core] [-N system]";
224         }
225
226         while ((ch = getopt(argc, argv, opts)) != -1)
227                 switch (ch) {
228                 case 'f':
229                         fileflag = 1;
230                         break;
231                 case 'k':
232                         putenv("BLOCKSIZE=1K");
233                         break;
234                 case 'M':
235                         memf = optarg;
236                         break;
237                 case 'N':
238                         nlistf = optarg;
239                         break;
240                 case 'n':
241                         usenumflag = 1;
242                         break;
243                 case 's':
244                         ++swapflag;
245                         break;
246                 case 'T':
247                         totalflag = 1;
248                         break;
249                 case 't':
250                         ttyflag = 1;
251                         break;
252                 case 'v':
253                 case 'i':               /* Backward compatibility. */
254                         errx(1, "vnode mode not supported");
255 #if 0
256                         vnodeflag = 1;
257                         break;
258 #endif
259                 default:
260                         usage();
261                 }
262         argc -= optind;
263         argv += optind;
264
265         /*
266          * Discard setgid privileges if not the running kernel so that bad
267          * guys can't print interesting stuff from kernel memory.
268          */
269         if (nlistf != NULL || memf != NULL)
270                 setgid(getgid());
271
272         if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == 0)
273                 errx(1, "kvm_openfiles: %s", buf);
274 #ifdef USE_KCORE
275         if ((KCORE_KVM_GLOBAL = kcore_open(nlistf, memf, buf)) == NULL)
276                 errx(1, "kcore_open: %s", buf);
277 #endif
278         if ((ret = kvm_nlist(kd, nl)) != 0) {
279                 size_t i;
280                 int quit = 0;
281
282                 if (ret == -1)
283                         errx(1, "kvm_nlist: %s", kvm_geterr(kd));
284                 for (i = 0; i < NL_LAST_MANDATORY; i++) {
285                         if (!nl[i].n_value) {
286                                 quit = 1;
287                                 warnx("undefined symbol: %s", nl[i].n_name);
288                         }
289                 }
290                 if (quit)
291                         exit(1);
292         }
293         if (!(fileflag | vnodeflag | ttyflag | swapflag | totalflag))
294                 usage();
295         if (fileflag || totalflag)
296                 filemode();
297         if (vnodeflag)
298                 vnodemode();
299         if (ttyflag)
300                 ttymode();
301         if (swapflag || totalflag)
302                 swapmode();
303         exit (0);
304 }
305
306 static void
307 usage(void)
308 {
309         fprintf(stderr, "usage: %s\n", usagestr);
310         exit (1);
311 }
312
313 struct e_vnode {
314         struct vnode *avnode;
315         struct vnode vnode;
316 };
317
318 void
319 vnodemode(void)
320 {
321         struct e_vnode *e_vnodebase, *endvnode, *evp;
322         struct vnode *vp;
323         struct mount *maddr, *mp = NULL;
324         int numvnodes;
325
326         e_vnodebase = loadvnodes(&numvnodes);
327         if (totalflag) {
328                 printf("%7d vnodes\n", numvnodes);
329                 return;
330         }
331         endvnode = e_vnodebase + numvnodes;
332         printf("%d active vnodes\n", numvnodes);
333
334
335 #define ST      mp->mnt_stat
336         maddr = NULL;
337         for (evp = e_vnodebase; evp < endvnode; evp++) {
338                 vp = &evp->vnode;
339                 if (vp->v_mount != maddr) {
340                         /*
341                          * New filesystem
342                          */
343                         if ((mp = getmnt(vp->v_mount)) == NULL)
344                                 continue;
345                         maddr = vp->v_mount;
346                         mount_print(mp);
347                         vnode_header();
348                         if (!strcmp(ST.f_fstypename, "ufs") ||
349                             !strcmp(ST.f_fstypename, "mfs"))
350                                 ufs_header();
351                         else if (!strcmp(ST.f_fstypename, "nfs"))
352                                 nfs_header();
353                         else if (!strcmp(ST.f_fstypename, "union"))
354                                 union_header();
355                         printf("\n");
356                 }
357                 vnode_print(evp->avnode, vp);
358                 if (!strcmp(ST.f_fstypename, "ufs") ||
359                     !strcmp(ST.f_fstypename, "mfs"))
360                         ufs_print(vp);
361                 else if (!strcmp(ST.f_fstypename, "nfs"))
362                         nfs_print(vp);
363                 else if (!strcmp(ST.f_fstypename, "union"))
364                         union_print(vp);
365                 printf("\n");
366         }
367         free(e_vnodebase);
368 }
369
370 void
371 vnode_header(void)
372 {
373         printf("ADDR     TYP VFLAG  USE HOLD");
374 }
375
376 void
377 vnode_print(struct vnode *avnode, struct vnode *vp)
378 {
379         const char *type;
380         char flags[32];
381         char *fp = flags;
382         int flag;
383
384         /*
385          * set type
386          */
387         switch (vp->v_type) {
388         case VNON:
389                 type = "non"; break;
390         case VREG:
391                 type = "reg"; break;
392         case VDIR:
393                 type = "dir"; break;
394         case VBLK:
395                 type = "blk"; break;
396         case VCHR:
397                 type = "chr"; break;
398         case VLNK:
399                 type = "lnk"; break;
400         case VSOCK:
401                 type = "soc"; break;
402         case VFIFO:
403                 type = "fif"; break;
404         case VBAD:
405                 type = "bad"; break;
406         default:
407                 type = "unk"; break;
408         }
409         /*
410          * gather flags
411          */
412         flag = vp->v_flag;
413         if (flag & VROOT)
414                 *fp++ = 'R';
415         if (flag & VTEXT)
416                 *fp++ = 'T';
417         if (flag & VSYSTEM)
418                 *fp++ = 'S';
419         if (flag & VISTTY)
420                 *fp++ = 't';
421 #ifdef VXLOCK
422         if (flag & VXLOCK)
423                 *fp++ = 'L';
424         if (flag & VXWANT)
425                 *fp++ = 'W';
426 #endif
427         if (flag & VBWAIT)
428                 *fp++ = 'B';
429         if (flag & VOBJBUF)
430                 *fp++ = 'V';
431         if (flag & VAGE)
432                 *fp++ = 'a';
433         if (flag & VOLOCK)
434                 *fp++ = 'l';
435         if (flag & VOWANT)
436                 *fp++ = 'w';
437 #ifdef VDOOMED
438         if (flag & VDOOMED)
439                 *fp++ = 'D';
440 #endif
441         if (flag & VFREE)
442                 *fp++ = 'F';
443         if (flag & VONWORKLST)
444                 *fp++ = 'O';
445         if (flag & VMOUNT)
446                 *fp++ = 'M';
447 #ifdef VRECLAIMED
448         if (flag & VINACTIVE)
449                 *fp++ = 'I';
450         if (flag & VRECLAIMED)
451                 *fp++ = 'X';
452 #endif
453
454         if (flag == 0)
455                 *fp++ = '-';
456         *fp = '\0';
457         printf("%8lx %s %5s %4d %4d",
458             (u_long)(void *)avnode, type, flags, vp->v_usecount, vp->v_holdcnt);
459 }
460
461 void
462 ufs_header(void)
463 {
464         printf(" FILEID IFLAG RDEV|SZ");
465 }
466
467 int
468 ufs_print(struct vnode *vp)
469 {
470         int flag;
471         struct inode inode, *ip = &inode;
472         char flagbuf[16], *flags = flagbuf;
473         char *name;
474         mode_t type;
475
476         KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode");
477         flag = ip->i_flag;
478         if (flag & IN_ACCESS)
479                 *flags++ = 'A';
480         if (flag & IN_CHANGE)
481                 *flags++ = 'C';
482         if (flag & IN_UPDATE)
483                 *flags++ = 'U';
484         if (flag & IN_MODIFIED)
485                 *flags++ = 'M';
486         if (flag & IN_RENAME)
487                 *flags++ = 'R';
488         if (flag & IN_SHLOCK)
489                 *flags++ = 'S';
490         if (flag & IN_EXLOCK)
491                 *flags++ = 'E';
492         if (flag & IN_HASHED)
493                 *flags++ = 'H';
494         if (flag & IN_LAZYMOD)
495                 *flags++ = 'L';
496         if (flag == 0)
497                 *flags++ = '-';
498         *flags = '\0';
499
500         printf(" %6d %5s", ip->i_number, flagbuf);
501         type = ip->i_mode & S_IFMT;
502         if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode))
503                 if (usenumflag || ((name = devname(ip->i_rdev, type)) == NULL))
504                         printf("   %2d,%-2d",
505                             major(ip->i_rdev), minor(ip->i_rdev));
506                 else
507                         printf(" %7s", name);
508         else
509                 printf(" %7qd", ip->i_size);
510         return (0);
511 }
512
513 void
514 nfs_header(void)
515 {
516         printf(" FILEID NFLAG RDEV|SZ");
517 }
518
519 int
520 nfs_print(struct vnode *vp)
521 {
522         struct nfsnode nfsnode, *np = &nfsnode;
523         char flagbuf[16], *flags = flagbuf;
524         int flag;
525         char *name;
526         mode_t type;
527
528         KGETRET(VTONFS(vp), &nfsnode, sizeof(nfsnode), "vnode's nfsnode");
529         flag = np->n_flag;
530         if (flag & NFLUSHWANT)
531                 *flags++ = 'W';
532         if (flag & NFLUSHINPROG)
533                 *flags++ = 'P';
534         if (flag & NMODIFIED)
535                 *flags++ = 'M';
536         if (flag & NWRITEERR)
537                 *flags++ = 'E';
538         if (flag & NQNFSNONCACHE)
539                 *flags++ = 'X';
540         if (flag & NQNFSWRITE)
541                 *flags++ = 'O';
542         if (flag & NQNFSEVICTED)
543                 *flags++ = 'G';
544         if (flag & NACC)
545                 *flags++ = 'A';
546         if (flag & NUPD)
547                 *flags++ = 'U';
548         if (flag & NCHG)
549                 *flags++ = 'C';
550         if (flag & NLOCKED)
551                 *flags++ = 'L';
552         if (flag & NWANTED)
553                 *flags++ = 'w';
554         if (flag == 0)
555                 *flags++ = '-';
556         *flags = '\0';
557
558 #define VT      np->n_vattr
559         printf(" %6ld %5s", VT.va_fileid, flagbuf);
560         type = VT.va_mode & S_IFMT;
561         if (S_ISCHR(VT.va_mode) || S_ISBLK(VT.va_mode))
562                 if (usenumflag || ((name = devname(VT.va_rdev, type)) == NULL))
563                         printf("   %2d,%-2d",
564                             major(VT.va_rdev), minor(VT.va_rdev));
565                 else
566                         printf(" %7s", name);
567         else
568                 printf(" %7qd", np->n_size);
569         return (0);
570 }
571
572 void
573 union_header(void)
574 {
575         printf("    UPPER    LOWER");
576 }
577
578 int
579 union_print(struct vnode *vp)
580 {
581         struct union_node unode, *up = &unode;
582
583         KGETRET(VTOUNION(vp), &unode, sizeof(unode), "vnode's unode");
584
585         printf(" %8lx %8lx", (u_long)(void *)up->un_uppervp,
586             (u_long)(void *)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(struct mount *maddr)
596 {
597         static struct mtab {
598                 struct mtab *next;
599                 struct mount *maddr;
600                 struct mount mount;
601         } *mhead = NULL;
602         struct mtab *mt;
603
604         for (mt = mhead; mt != NULL; mt = mt->next)
605                 if (maddr == mt->maddr)
606                         return (&mt->mount);
607         if ((mt = malloc(sizeof(struct mtab))) == NULL)
608                 errx(1, "malloc");
609         KGETRET(maddr, &mt->mount, sizeof(struct mount), "mount table");
610         mt->maddr = maddr;
611         mt->next = mhead;
612         mhead = mt;
613         return (&mt->mount);
614 }
615
616 void
617 mount_print(struct mount *mp)
618 {
619         int flags;
620
621 #define ST      mp->mnt_stat
622         printf("*** MOUNT %s %s on %s", ST.f_fstypename,
623             ST.f_mntfromname, ST.f_mntonname);
624         if ((flags = mp->mnt_flag)) {
625                 int i;
626                 const char *sep = " (";
627
628                 for (i = 0; mnt_flags[i].m_flag; i++) {
629                         if (flags & mnt_flags[i].m_flag) {
630                                 printf("%s%s", sep, mnt_flags[i].m_name);
631                                 flags &= ~mnt_flags[i].m_flag;
632                                 sep = ",";
633                         }
634                 }
635                 if (flags)
636                         printf("%sunknown_flags:%x", sep, flags);
637                 printf(")");
638         }
639         printf("\n");
640 #undef ST
641 }
642
643 struct e_vnode *
644 loadvnodes(int *avnodes)
645 {
646         int mib[2];
647         size_t copysize;
648         struct e_vnode *vnodebase;
649
650         if (memf != NULL) {
651                 /*
652                  * do it by hand
653                  */
654                 return (kinfo_vnodes(avnodes));
655         }
656         mib[0] = CTL_KERN;
657         mib[1] = KERN_VNODE;
658         if (sysctl(mib, 2, NULL, &copysize, NULL, 0) == -1)
659                 err(1, "sysctl: KERN_VNODE");
660         if ((vnodebase = malloc(copysize)) == NULL)
661                 errx(1, "malloc");
662         if (sysctl(mib, 2, vnodebase, &copysize, NULL, 0) == -1)
663                 err(1, "sysctl: KERN_VNODE");
664         if (copysize % sizeof(struct e_vnode))
665                 errx(1, "vnode size mismatch");
666         *avnodes = copysize / sizeof(struct e_vnode);
667
668         return (vnodebase);
669 }
670
671 /*
672  * simulate what a running kernel does in in kinfo_vnode
673  */
674 struct e_vnode *
675 kinfo_vnodes(int *avnodes)
676 {
677         struct mntlist mountlist;
678         struct mount *mp, mounth, *mp_next;
679         struct vnode *vp, vnode, *vp_next;
680         char *vbuf, *evbuf, *bp;
681         int num, numvnodes;
682
683 #define VPTRSZ  sizeof(struct vnode *)
684 #define VNODESZ sizeof(struct vnode)
685
686         KGET(NL_NUMVNODES, numvnodes);
687         if ((vbuf = malloc((numvnodes + 20) * (VPTRSZ + VNODESZ))) == NULL)
688                 errx(1, "malloc");
689         bp = vbuf;
690         evbuf = vbuf + (numvnodes + 20) * (VPTRSZ + VNODESZ);
691         KGET(NL_MOUNTLIST, mountlist);
692         for (num = 0, mp = TAILQ_FIRST(&mountlist); ; mp = mp_next) {
693                 KGET2(mp, &mounth, sizeof(mounth), "mount entry");
694                 mp_next = TAILQ_NEXT(&mounth, mnt_list);
695                 for (vp = TAILQ_FIRST(&mounth.mnt_nvnodelist);
696                     vp != NULL; vp = vp_next) {
697                         KGET2(vp, &vnode, sizeof(vnode), "vnode");
698                         vp_next = TAILQ_NEXT(&vnode, v_nmntvnodes);
699                         if ((bp + VPTRSZ + VNODESZ) > evbuf)
700                                 /* XXX - should realloc */
701                                 errx(1, "no more room for vnodes");
702                         memmove(bp, &vp, VPTRSZ);
703                         bp += VPTRSZ;
704                         memmove(bp, &vnode, VNODESZ);
705                         bp += VNODESZ;
706                         num++;
707                 }
708                 if (mp == TAILQ_LAST(&mountlist, mntlist))
709                         break;
710         }
711         *avnodes = num;
712         return ((struct e_vnode *)vbuf);
713 }
714
715 char hdr[] =
716 "  LINE RAW CAN OUT IHIWT ILOWT OHWT LWT     COL STATE  SESS      PGID DISC\n";
717 int ttyspace = 128;
718
719 void
720 ttymode(void)
721 {
722         struct tty *tty;
723         struct tty ttyb[1000];
724         int error;
725         size_t len, i;
726
727         printf(hdr);
728         len = sizeof(ttyb);
729         error = sysctlbyname("kern.ttys", &ttyb, &len, 0, 0);
730         if (!error) {
731                 len /= sizeof(ttyb[0]);
732                 for (i = 0; i < len; i++) {
733                         ttyprt(&ttyb[i], 0);
734                 }
735         }
736         if ((tty = malloc(ttyspace * sizeof(*tty))) == NULL)
737                 errx(1, "malloc");
738         if (nl[NL_NRC_TTY].n_type != 0)
739                 ttytype(tty, "rc", NL_RC_TTY, NL_NRC_TTY, 0);
740         if (nl[NL_NCY_TTY].n_type != 0)
741                 ttytype(tty, "cy", NL_CY_TTY, NL_NCY_TTY, 0);
742         if (nl[NL_SI_NPORTS].n_type != 0)
743                 ttytype(tty, "si", NL_SI_TTY, NL_SI_NPORTS, 1);
744         free(tty);
745 }
746
747 void
748 ttytype(struct tty *tty, const char *name, int type, int number, int indir)
749 {
750         struct tty *tp;
751         int ntty;
752         struct tty **ttyaddr;
753
754         if (tty == NULL)
755                 return;
756         KGET(number, ntty);
757         printf("%d %s %s\n", ntty, name, (ntty == 1) ? "line" : "lines");
758         if (ntty > ttyspace) {
759                 ttyspace = ntty;
760                 if ((tty = realloc(tty, ttyspace * sizeof(*tty))) == 0)
761                         errx(1, "realloc");
762         }
763         if (indir) {
764                 KGET(type, ttyaddr);
765                 KGET2(ttyaddr, tty, (ssize_t)(ntty * sizeof(struct tty)),
766                       "tty structs");
767         } else {
768                 KGET1(type, tty, (ssize_t)(ntty * sizeof(struct tty)),
769                       "tty structs");
770         }
771         printf(hdr);
772         for (tp = tty; tp < &tty[ntty]; tp++)
773                 ttyprt(tp, tp - tty);
774 }
775
776 struct {
777         int flag;
778         char val;
779 } ttystates[] = {
780 #ifdef TS_WOPEN
781         { TS_WOPEN,     'W'},
782 #endif
783         { TS_ISOPEN,    'O'},
784         { TS_CARR_ON,   'C'},
785 #ifdef TS_CONNECTED
786         { TS_CONNECTED, 'c'},
787 #endif
788         { TS_TIMEOUT,   'T'},
789         { TS_FLUSH,     'F'},
790         { TS_BUSY,      'B'},
791 #ifdef TS_ASLEEP
792         { TS_ASLEEP,    'A'},
793 #endif
794 #ifdef TS_SO_OLOWAT
795         { TS_SO_OLOWAT, 'A'},
796 #endif
797 #ifdef TS_SO_OCOMPLETE
798         { TS_SO_OCOMPLETE, 'a'},
799 #endif
800         { TS_XCLUDE,    'X'},
801         { TS_TTSTOP,    'S'},
802 #ifdef TS_CAR_OFLOW
803         { TS_CAR_OFLOW, 'm'},
804 #endif
805 #ifdef TS_CTS_OFLOW
806         { TS_CTS_OFLOW, 'o'},
807 #endif
808 #ifdef TS_DSR_OFLOW
809         { TS_DSR_OFLOW, 'd'},
810 #endif
811         { TS_TBLOCK,    'K'},
812         { TS_ASYNC,     'Y'},
813         { TS_BKSL,      'D'},
814         { TS_ERASE,     'E'},
815         { TS_LNCH,      'L'},
816         { TS_TYPEN,     'P'},
817         { TS_CNTTB,     'N'},
818 #ifdef TS_CAN_BYPASS_L_RINT
819         { TS_CAN_BYPASS_L_RINT, 'l'},
820 #endif
821 #ifdef TS_SNOOP
822         { TS_SNOOP,     's'},
823 #endif
824 #ifdef TS_ZOMBIE
825         { TS_ZOMBIE,    'Z'},
826 #endif
827         { 0,           '\0'},
828 };
829
830 void
831 ttyprt(struct tty *tp, int line)
832 {
833         int i, j;
834         pid_t pgid;
835         char *name, state[20];
836
837         if (usenumflag || tp->t_dev == 0 ||
838            (name = devname(tp->t_dev, S_IFCHR)) == NULL)
839                 printf("%7d ", line);
840         else
841                 printf("%7s ", name);
842         printf("%2d %3d ", tp->t_rawq.c_cc, tp->t_canq.c_cc);
843         printf("%3d %5d %5d %4d %3d %7d ", tp->t_outq.c_cc,
844                 tp->t_ihiwat, tp->t_ilowat, tp->t_ohiwat, tp->t_olowat,
845                 tp->t_column);
846         for (i = j = 0; ttystates[i].flag; i++)
847                 if (tp->t_state&ttystates[i].flag)
848                         state[j++] = ttystates[i].val;
849         if (j == 0)
850                 state[j++] = '-';
851         state[j] = '\0';
852         printf("%-6s %8lx", state, (u_long)(void *)tp->t_session);
853         pgid = 0;
854         if (tp->t_pgrp != NULL)
855                 KGET2(&tp->t_pgrp->pg_id, &pgid, sizeof(pid_t), "pgid");
856         printf("%6d ", pgid);
857         switch (tp->t_line) {
858         case TTYDISC:
859                 printf("term\n");
860                 break;
861         case NTTYDISC:
862                 printf("ntty\n");
863                 break;
864         case SLIPDISC:
865                 printf("slip\n");
866                 break;
867         case PPPDISC:
868                 printf("ppp\n");
869                 break;
870         default:
871                 printf("%d\n", tp->t_line);
872                 break;
873         }
874 }
875
876 void
877 filemode(void)
878 {
879         struct kinfo_file *fp, *ofp;
880         size_t len;
881         char flagbuf[16], *fbp;
882         int maxfile, nfile;
883         static const char *dtypes[] = { "???", "inode", "socket" };
884
885         if (kinfo_get_maxfiles(&maxfile))
886                 err(1, "kinfo_get_maxfiles");
887         if (totalflag) {
888                 if (kinfo_get_openfiles(&nfile))
889                         err(1, "kinfo_get_openfiles");
890                 printf("%3d/%3d files\n", nfile, maxfile);
891                 return;
892         }
893         if (kinfo_get_files(&fp, &len))
894                 err(1, "kinfo_get_files");
895         ofp = fp;
896         
897         printf("%d/%d open files\n", len, maxfile);
898         printf("   LOC   TYPE    FLG     CNT  MSG    DATA    OFFSET\n");
899         for (; len-- > 0; fp++) {
900                 if ((unsigned)fp->f_type > DTYPE_SOCKET)
901                         continue;
902                 printf("%p ", fp->f_file);
903                 printf("%-8.8s", dtypes[fp->f_type]);
904                 fbp = flagbuf;
905                 if (fp->f_flag & FREAD)
906                         *fbp++ = 'R';
907                 if (fp->f_flag & FWRITE)
908                         *fbp++ = 'W';
909                 if (fp->f_flag & FAPPEND)
910                         *fbp++ = 'A';
911 #ifdef FSHLOCK  /* currently gone */
912                 if (fp->f_flag & FSHLOCK)
913                         *fbp++ = 'S';
914                 if (fp->f_flag & FEXLOCK)
915                         *fbp++ = 'X';
916 #endif
917                 if (fp->f_flag & FASYNC)
918                         *fbp++ = 'I';
919                 *fbp = '\0';
920                 printf("%6s  %3d", flagbuf, fp->f_count);
921                 printf("  %3d", fp->f_msgcount);
922                 printf("  %8lx", (u_long)(void *)fp->f_data);
923                 if (fp->f_offset < 0)
924                         printf("  %qx\n", fp->f_offset);
925                 else
926                         printf("  %qd\n", fp->f_offset);
927         }
928         free(ofp);
929 }
930
931 /*
932  * swapmode is based on a program called swapinfo written
933  * by Kevin Lahey <kml@rokkaku.atl.ga.us>.
934  */
935 void
936 swapmode(void)
937 {
938         struct kvm_swap kswap[16];
939         int i;
940         int n;
941         int pagesize = getpagesize();
942         const char *header;
943         int hlen;
944         long blocksize;
945
946         n = kvm_getswapinfo(
947             kd, 
948             kswap,
949             sizeof(kswap)/sizeof(kswap[0]),
950             ((swapflag > 1) ? SWIF_DUMP_TREE : 0) | SWIF_DEV_PREFIX
951         );
952
953 #define CONVERT(v)      ((int)((quad_t)(v) * pagesize / blocksize))
954
955         header = getbsize(&hlen, &blocksize);
956         if (totalflag == 0) {
957                 printf("%-15s %*s %8s %8s %8s  %s\n",
958                     "Device", hlen, header,
959                     "Used", "Avail", "Capacity", "Type");
960
961                 for (i = 0; i < n; ++i) {
962                         printf(
963                             "%-15s %*d ",
964                             kswap[i].ksw_devname,
965                             hlen,
966                             CONVERT(kswap[i].ksw_total)
967                         );
968                         printf(
969                             "%8d %8d %5.0f%%    %s\n",
970                             CONVERT(kswap[i].ksw_used),
971                             CONVERT(kswap[i].ksw_total - kswap[i].ksw_used),
972                             (double)kswap[i].ksw_used * 100.0 /
973                                 (double)kswap[i].ksw_total,
974                             (kswap[i].ksw_flags & SW_SEQUENTIAL) ?
975                                 "Sequential" : "Interleaved"
976                         );
977                 }
978         }
979
980         if (totalflag) {
981                 blocksize = 1024 * 1024;
982
983                 printf(
984                     "%dM/%dM swap space\n", 
985                     CONVERT(kswap[n].ksw_used),
986                     CONVERT(kswap[n].ksw_total)
987                 );
988         } else if (n > 1) {
989                 printf(
990                     "%-15s %*d %8d %8d %5.0f%%\n",
991                     "Total",
992                     hlen, 
993                     CONVERT(kswap[n].ksw_total),
994                     CONVERT(kswap[n].ksw_used),
995                     CONVERT(kswap[n].ksw_total - kswap[n].ksw_used),
996                     (double)kswap[n].ksw_used * 100.0 /
997                         (double)kswap[n].ksw_total
998                 );
999         }
1000 }