pstat.c revision 1.94 1 /* $NetBSD: pstat.c,v 1.94 2006/03/17 21:01:02 elad 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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __COPYRIGHT("@(#) Copyright (c) 1980, 1991, 1993, 1994\n\
35 The Regents of the University of California. All rights reserved.\n");
36 #endif /* not lint */
37
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)pstat.c 8.16 (Berkeley) 5/9/95";
41 #else
42 __RCSID("$NetBSD: pstat.c,v 1.94 2006/03/17 21:01:02 elad Exp $");
43 #endif
44 #endif /* not lint */
45
46 #include <sys/param.h>
47 #include <sys/time.h>
48 #include <sys/vnode.h>
49 #include <sys/ucred.h>
50 #define _KERNEL
51 #include <sys/file.h>
52 #include <ufs/ufs/inode.h>
53 #define NFS
54 #include <sys/mount.h>
55 #undef NFS
56 #include <sys/uio.h>
57 #include <sys/namei.h>
58 #include <miscfs/genfs/layer.h>
59 #include <miscfs/union/union.h>
60 #undef _KERNEL
61 #include <sys/stat.h>
62 #include <nfs/nfsproto.h>
63 #include <nfs/rpcv2.h>
64 #include <nfs/nfs.h>
65 #include <nfs/nfsnode.h>
66 #include <sys/ioctl.h>
67 #include <sys/tty.h>
68 #include <sys/conf.h>
69 #include <sys/device.h>
70
71 #include <sys/sysctl.h>
72
73 #include <err.h>
74 #include <kvm.h>
75 #include <limits.h>
76 #include <nlist.h>
77 #include <stdio.h>
78 #include <stdlib.h>
79 #include <string.h>
80 #include <unistd.h>
81
82 #include "swapctl.h"
83
84 struct nlist nl[] = {
85 #define V_MOUNTLIST 0
86 { "_mountlist" }, /* address of head of mount list. */
87 #define V_NUMV 1
88 { "_numvnodes" },
89 #define FNL_NFILE 2
90 { "_nfiles" },
91 #define FNL_MAXFILE 3
92 { "_maxfiles" },
93 #define TTY_NTTY 4
94 { "_tty_count" },
95 #define TTY_TTYLIST 5
96 { "_ttylist" },
97 #define NLMANDATORY TTY_TTYLIST /* names up to here are mandatory */
98 { "" }
99 };
100
101 int usenumflag;
102 int totalflag;
103 int kflag;
104 int hflag;
105 char *nlistf = NULL;
106 char *memf = NULL;
107 kvm_t *kd;
108
109 static const char * const dtypes[] = { DTYPE_NAMES };
110
111
112 static const struct {
113 u_int m_flag;
114 u_int m_visible;
115 const char *m_name;
116 } mnt_flags[] = {
117 __MNT_FLAGS
118 };
119
120 struct flagbit_desc {
121 u_int fd_flags;
122 char fd_mark;
123 };
124
125 #define SVAR(var) __STRING(var) /* to force expansion */
126 #define KGET(idx, var) \
127 KGET1(idx, &var, sizeof(var), SVAR(var))
128 #define KGET1(idx, p, s, msg) \
129 KGET2(nl[idx].n_value, p, s, msg)
130 #define KGET2(addr, p, s, msg) do { \
131 if (kvm_read(kd, (u_long)(addr), p, s) != s) \
132 warnx("cannot read %s: %s", msg, kvm_geterr(kd)); \
133 } while (/* CONSTCOND */0)
134 #define KGETRET(addr, p, s, msg) do { \
135 if (kvm_read(kd, (u_long)(addr), p, s) != s) { \
136 warnx("cannot read %s: %s", msg, kvm_geterr(kd)); \
137 return (0); \
138 } \
139 } while (/* CONSTCOND */0)
140
141 #if 1 /* This is copied from vmstat/vmstat.c */
142 /*
143 * Print single word. `ovflow' is number of characters didn't fit
144 * on the last word. `fmt' is a format string to print this word.
145 * It must contain asterisk for field width. `width' is a width
146 * occupied by this word. `fixed' is a number of constant chars in
147 * `fmt'. `val' is a value to be printed using format string `fmt'.
148 */
149 #define PRWORD(ovflw, fmt, width, fixed, val) do { \
150 (ovflw) += printf((fmt), \
151 (width) - (fixed) - (ovflw) > 0 ? \
152 (width) - (fixed) - (ovflw) : 0, \
153 (val)) - (width); \
154 if ((ovflw) < 0) \
155 (ovflw) = 0; \
156 } while (/* CONSTCOND */0)
157 #endif
158
159 void filemode(void);
160 int getfiles(char **, int *, char **);
161 int getflags(const struct flagbit_desc *, char *, u_int);
162 struct mount *
163 getmnt(struct mount *);
164 char * kinfo_vnodes(int *);
165 void layer_header(void);
166 int layer_print(struct vnode *, int);
167 char * loadvnodes(int *);
168 int main(int, char **);
169 void mount_print(struct mount *);
170 void nfs_header(void);
171 int nfs_print(struct vnode *, int);
172 void ttymode(void);
173 void ttyprt(struct tty *);
174 void ufs_header(void);
175 int ufs_print(struct vnode *, int);
176 int ext2fs_print(struct vnode *, int);
177 void union_header(void);
178 int union_print(struct vnode *, int);
179 void usage(void);
180 void vnode_header(void);
181 int vnode_print(struct vnode *, struct vnode *);
182 void vnodemode(void);
183
184 int
185 main(int argc, char *argv[])
186 {
187 int ch, i, quit, ret, use_sysctl;
188 int fileflag, swapflag, ttyflag, vnodeflag;
189 gid_t egid = getegid();
190 char buf[_POSIX2_LINE_MAX];
191
192 setegid(getgid());
193 fileflag = swapflag = ttyflag = vnodeflag = 0;
194 while ((ch = getopt(argc, argv, "TM:N:fghikmnstv")) != -1)
195 switch (ch) {
196 case 'f':
197 fileflag = 1;
198 break;
199 case 'M':
200 memf = optarg;
201 break;
202 case 'N':
203 nlistf = optarg;
204 break;
205 case 'n':
206 usenumflag = 1;
207 break;
208 case 's':
209 swapflag = 1;
210 break;
211 case 'T':
212 totalflag = 1;
213 break;
214 case 't':
215 ttyflag = 1;
216 break;
217 case 'k':
218 kflag = 1;
219 break;
220 case 'g':
221 kflag = 3; /* 1k ^ 3 */
222 break;
223 case 'h':
224 hflag = 1;
225 break;
226 case 'm':
227 kflag = 2; /* 1k ^ 2 */
228 break;
229 case 'v':
230 case 'i': /* Backward compatibility. */
231 vnodeflag = 1;
232 break;
233 default:
234 usage();
235 }
236 argc -= optind;
237 argv += optind;
238
239 /*
240 * Discard setgid privileges. If not the running kernel, we toss
241 * them away totally so that bad guys can't print interesting stuff
242 * from kernel memory, otherwise switch back to kmem for the
243 * duration of the kvm_openfiles() call.
244 */
245 if (nlistf != NULL || memf != NULL)
246 (void)setgid(getgid());
247 else
248 (void)setegid(egid);
249
250 use_sysctl = (nlistf == NULL && memf == NULL);
251
252 if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == 0)
253 errx(1, "kvm_openfiles: %s", buf);
254
255 /* get rid of it now anyway */
256 if (nlistf == NULL && memf == NULL)
257 (void)setgid(getgid());
258 if ((ret = kvm_nlist(kd, nl)) != 0) {
259 if (ret == -1)
260 errx(1, "kvm_nlist: %s", kvm_geterr(kd));
261 for (i = quit = 0; i <= NLMANDATORY; i++)
262 if (!nl[i].n_value) {
263 quit = 1;
264 warnx("undefined symbol: %s", nl[i].n_name);
265 }
266 if (quit)
267 exit(1);
268 }
269 if (!(fileflag | vnodeflag | ttyflag | swapflag | totalflag))
270 usage();
271 if (fileflag || totalflag)
272 filemode();
273 if (vnodeflag || totalflag)
274 vnodemode();
275 if (ttyflag)
276 ttymode();
277 if (swapflag || totalflag)
278 if (use_sysctl)
279 list_swap(0, kflag, 0, totalflag, 1, hflag);
280 exit(0);
281 }
282
283 #define VPTRSZ sizeof(struct vnode *)
284 #define VNODESZ sizeof(struct vnode)
285 #define PTRSTRWIDTH ((int)sizeof(void *) * 2) /* Width of resulting string
286 when pointer is printed
287 in hexadecimal. */
288
289 void
290 vnodemode(void)
291 {
292 char *e_vnodebase, *endvnode, *evp;
293 struct vnode *vp;
294 struct mount *maddr, *mp;
295 int numvnodes, ovflw;
296 int (*vnode_fsprint) (struct vnode *, int); /* per-fs data printer */
297
298 mp = NULL;
299 e_vnodebase = loadvnodes(&numvnodes);
300 if (totalflag) {
301 (void)printf("%7d vnodes\n", numvnodes);
302 goto out;
303 }
304 endvnode = e_vnodebase + numvnodes * (VPTRSZ + VNODESZ);
305 (void)printf("%d active vnodes\n", numvnodes);
306
307 #define ST mp->mnt_stat
308 #define FSTYPE_IS(mp, name) \
309 (strncmp((mp)->mnt_stat.f_fstypename, (name), MFSNAMELEN) == 0)
310 maddr = NULL;
311 vnode_fsprint = NULL;
312 for (evp = e_vnodebase; evp < endvnode; evp += VPTRSZ + VNODESZ) {
313 vp = (struct vnode *)(evp + VPTRSZ);
314 if (vp->v_mount != maddr) {
315 /*
316 * New filesystem
317 */
318 if ((mp = getmnt(vp->v_mount)) == NULL)
319 continue;
320 maddr = vp->v_mount;
321 mount_print(mp);
322 vnode_header();
323 if (FSTYPE_IS(mp, MOUNT_FFS) ||
324 FSTYPE_IS(mp, MOUNT_MFS)) {
325 ufs_header();
326 vnode_fsprint = ufs_print;
327 } else if (FSTYPE_IS(mp, MOUNT_NFS)) {
328 nfs_header();
329 vnode_fsprint = nfs_print;
330 } else if (FSTYPE_IS(mp, MOUNT_EXT2FS)) {
331 ufs_header();
332 vnode_fsprint = ext2fs_print;
333 } else if (FSTYPE_IS(mp, MOUNT_NULL) ||
334 FSTYPE_IS(mp, MOUNT_OVERLAY) ||
335 FSTYPE_IS(mp, MOUNT_UMAP)) {
336 layer_header();
337 vnode_fsprint = layer_print;
338 } else if (FSTYPE_IS(mp, MOUNT_UNION)) {
339 union_header();
340 vnode_fsprint = union_print;
341 } else
342 vnode_fsprint = NULL;
343 (void)printf("\n");
344 }
345 ovflw = vnode_print(*(struct vnode **)evp, vp);
346 if (VTOI(vp) != NULL && vnode_fsprint != NULL)
347 (*vnode_fsprint)(vp, ovflw);
348 (void)printf("\n");
349 }
350
351 out:
352 free(e_vnodebase);
353 }
354
355 int
356 getflags(const struct flagbit_desc *fd, char *p, u_int flags)
357 {
358 char *q = p;
359
360 if (flags == 0) {
361 *p++ = '-';
362 *p = '\0';
363 return (0);
364 }
365
366 for (; fd->fd_flags != 0; fd++)
367 if ((flags & fd->fd_flags) != 0)
368 *p++ = fd->fd_mark;
369 *p = '\0';
370 return (p - q);
371 }
372
373 const struct flagbit_desc vnode_flags[] = {
374 { VROOT, 'R' },
375 { VTEXT, 'T' },
376 { VSYSTEM, 'S' },
377 { VISTTY, 'I' },
378 { VEXECMAP, 'E' },
379 { VXLOCK, 'L' },
380 { VXWANT, 'W' },
381 { VBWAIT, 'B' },
382 { VALIASED, 'A' },
383 { VDIROP, 'D' },
384 { VLAYER, 'Y' },
385 { VONWORKLST, 'O' },
386 { 0, '\0' },
387 };
388
389 void
390 vnode_header(void)
391 {
392
393 (void)printf("%-*s TYP VFLAG USE HOLD TAG NPAGE",
394 PTRSTRWIDTH, "ADDR");
395 }
396
397 int
398 vnode_print(struct vnode *avnode, struct vnode *vp)
399 {
400 char *type, flags[sizeof(vnode_flags) / sizeof(vnode_flags[0])];
401 int ovflw;
402
403 /*
404 * set type
405 */
406 switch (vp->v_type) {
407 case VNON:
408 type = "non"; break;
409 case VREG:
410 type = "reg"; break;
411 case VDIR:
412 type = "dir"; break;
413 case VBLK:
414 type = "blk"; break;
415 case VCHR:
416 type = "chr"; break;
417 case VLNK:
418 type = "lnk"; break;
419 case VSOCK:
420 type = "soc"; break;
421 case VFIFO:
422 type = "fif"; break;
423 case VBAD:
424 type = "bad"; break;
425 default:
426 type = "unk"; break;
427 }
428 /*
429 * gather flags
430 */
431 (void)getflags(vnode_flags, flags, vp->v_flag);
432
433 ovflw = 0;
434 PRWORD(ovflw, "%*lx", PTRSTRWIDTH, 0, (long)avnode);
435 PRWORD(ovflw, " %*s", 4, 1, type);
436 PRWORD(ovflw, " %*s", 6, 1, flags);
437 PRWORD(ovflw, " %*ld", 5, 1, (long)vp->v_usecount);
438 PRWORD(ovflw, " %*ld", 5, 1, (long)vp->v_holdcnt);
439 PRWORD(ovflw, " %*d", 4, 1, vp->v_tag);
440 PRWORD(ovflw, " %*d", 6, 1, vp->v_uobj.uo_npages);
441 return (ovflw);
442 }
443
444 const struct flagbit_desc ufs_flags[] = {
445 { IN_ACCESS, 'A' },
446 { IN_CHANGE, 'C' },
447 { IN_UPDATE, 'U' },
448 { IN_MODIFIED, 'M' },
449 { IN_ACCESSED, 'a' },
450 { IN_RENAME, 'R' },
451 { IN_SHLOCK, 'S' },
452 { IN_EXLOCK, 'E' },
453 { IN_CLEANING, 'c' },
454 { IN_ADIROP, 'D' },
455 { IN_SPACECOUNTED, 's' },
456 { 0, '\0' },
457 };
458
459 void
460 ufs_header(void)
461 {
462
463 (void)printf(" FILEID IFLAG RDEV|SZ");
464 }
465
466 int
467 ufs_print(struct vnode *vp, int ovflw)
468 {
469 struct inode inode, *ip = &inode;
470 union dinode {
471 struct ufs1_dinode dp1;
472 struct ufs2_dinode dp2;
473 } dip;
474 char flags[sizeof(ufs_flags) / sizeof(ufs_flags[0])];
475 char dev[4 + 1 + 7 + 1]; /* 12bit marjor + 20bit minor */
476 char *name;
477 mode_t type;
478 dev_t rdev;
479
480 KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode");
481 KGETRET(ip->i_din.ffs1_din, &dip, sizeof (struct ufs1_dinode),
482 "inode's dinode");
483
484 if (ip->i_size == dip.dp1.di_size)
485 rdev = dip.dp1.di_rdev;
486 else {
487 KGETRET(ip->i_din.ffs1_din, &dip, sizeof (struct ufs2_dinode),
488 "inode's UFS2 dinode");
489 rdev = dip.dp2.di_rdev;
490 }
491
492 /*
493 * XXX need to to locking state.
494 */
495
496 (void)getflags(ufs_flags, flags, ip->i_flag);
497 PRWORD(ovflw, " %*llu", 7, 1, (unsigned long long)ip->i_number);
498 PRWORD(ovflw, " %*s", 6, 1, flags);
499 type = ip->i_mode & S_IFMT;
500 if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode)) {
501 if (usenumflag ||
502 (name = devname(rdev, type)) == NULL) {
503 snprintf(dev, sizeof(dev), "%d,%d",
504 major(rdev), minor(rdev));
505 name = dev;
506 }
507 PRWORD(ovflw, " %*s", 8, 1, name);
508 } else
509 PRWORD(ovflw, " %*lld", 8, 1, (long long)ip->i_size);
510 return 0;
511 }
512
513 int
514 ext2fs_print(struct vnode *vp, int ovflw)
515 {
516 struct inode inode, *ip = &inode;
517 char flags[sizeof(ufs_flags) / sizeof(ufs_flags[0])];
518 char dev[4 + 1 + 7 + 1]; /* 12bit marjor + 20bit minor */
519 char *name;
520 mode_t type;
521
522 KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode");
523
524 /*
525 * XXX need to to locking state.
526 */
527
528 (void)getflags(ufs_flags, flags, ip->i_flag);
529 PRWORD(ovflw, " %*llu", 7, 1, (unsigned long long)ip->i_number);
530 PRWORD(ovflw, " %*s", 6, 1, flags);
531 type = ip->i_e2fs_mode & S_IFMT;
532 if (S_ISCHR(ip->i_e2fs_mode) || S_ISBLK(ip->i_e2fs_mode)) {
533 if (usenumflag ||
534 (name = devname(ip->i_e2fs_rdev, type)) == NULL) {
535 snprintf(dev, sizeof(dev), "%d,%d",
536 major(ip->i_e2fs_rdev), minor(ip->i_e2fs_rdev));
537 name = dev;
538 }
539 PRWORD(ovflw, " %*s", 8, 1, name);
540 } else
541 PRWORD(ovflw, " %*u", 8, 1, (u_int)ip->i_e2fs_size);
542 return (0);
543 }
544
545 const struct flagbit_desc nfs_flags[] = {
546 { NFLUSHWANT, 'W' },
547 { NFLUSHINPROG, 'P' },
548 { NMODIFIED, 'M' },
549 { NWRITEERR, 'E' },
550 { NQNFSNONCACHE, 'X' },
551 { NQNFSWRITE, 'O' },
552 { NQNFSEVICTED, 'G' },
553 { NACC, 'A' },
554 { NUPD, 'U' },
555 { NCHG, 'C' },
556 { 0, '\0' },
557 };
558
559 void
560 nfs_header(void)
561 {
562
563 (void)printf(" FILEID NFLAG RDEV|SZ");
564 }
565
566 int
567 nfs_print(struct vnode *vp, int ovflw)
568 {
569 struct nfsnode nfsnode, *np = &nfsnode;
570 char flags[sizeof(nfs_flags) / sizeof(nfs_flags[0])];
571 char dev[4 + 1 + 7 + 1]; /* 12bit marjor + 20bit minor */
572 struct vattr va;
573 char *name;
574 mode_t type;
575
576 KGETRET(VTONFS(vp), &nfsnode, sizeof(nfsnode), "vnode's nfsnode");
577 (void)getflags(nfs_flags, flags, np->n_flag);
578
579 KGETRET(np->n_vattr, &va, sizeof(va), "vnode attr");
580 PRWORD(ovflw, " %*ld", 7, 1, (long)va.va_fileid);
581 PRWORD(ovflw, " %*s", 6, 1, flags);
582 switch (va.va_type) {
583 case VCHR:
584 type = S_IFCHR;
585 goto device;
586
587 case VBLK:
588 type = S_IFBLK;
589 device:
590 if (usenumflag || (name = devname(va.va_rdev, type)) == NULL) {
591 (void)snprintf(dev, sizeof(dev), "%d,%d",
592 major(va.va_rdev), minor(va.va_rdev));
593 name = dev;
594 }
595 PRWORD(ovflw, " %*s", 8, 1, name);
596 break;
597 default:
598 PRWORD(ovflw, " %*lld", 8, 1, (long long)np->n_size);
599 break;
600 }
601 return (0);
602 }
603
604 void
605 layer_header(void)
606 {
607
608 (void)printf(" %*s", PTRSTRWIDTH, "LOWER");
609 }
610
611 int
612 layer_print(struct vnode *vp, int ovflw)
613 {
614 struct layer_node lnode, *lp = &lnode;
615
616 KGETRET(VTOLAYER(vp), &lnode, sizeof(lnode), "layer vnode");
617
618 PRWORD(ovflw, " %*lx", PTRSTRWIDTH + 1, 1, (long)lp->layer_lowervp);
619 return (0);
620 }
621
622 void
623 union_header(void)
624 {
625
626 (void)printf(" %*s %*s", PTRSTRWIDTH, "UPPER", PTRSTRWIDTH, "LOWER");
627 }
628
629 int
630 union_print(struct vnode *vp, int ovflw)
631 {
632 struct union_node unode, *up = &unode;
633
634 KGETRET(VTOUNION(vp), &unode, sizeof(unode), "vnode's unode");
635
636 PRWORD(ovflw, " %*lx", PTRSTRWIDTH + 1, 1, (long)up->un_uppervp);
637 PRWORD(ovflw, " %*lx", PTRSTRWIDTH + 1, 1, (long)up->un_lowervp);
638 return (0);
639 }
640
641 /*
642 * Given a pointer to a mount structure in kernel space,
643 * read it in and return a usable pointer to it.
644 */
645 struct mount *
646 getmnt(struct mount *maddr)
647 {
648 static struct mtab {
649 struct mtab *next;
650 struct mount *maddr;
651 struct mount mount;
652 } *mhead = NULL;
653 struct mtab *mt;
654
655 for (mt = mhead; mt != NULL; mt = mt->next)
656 if (maddr == mt->maddr)
657 return (&mt->mount);
658 if ((mt = malloc(sizeof(struct mtab))) == NULL)
659 err(1, "malloc");
660 KGETRET(maddr, &mt->mount, sizeof(struct mount), "mount table");
661 mt->maddr = maddr;
662 mt->next = mhead;
663 mhead = mt;
664 return (&mt->mount);
665 }
666
667 void
668 mount_print(struct mount *mp)
669 {
670 int flags;
671
672 (void)printf("*** MOUNT %s %s on %s", ST.f_fstypename,
673 ST.f_mntfromname, ST.f_mntonname);
674 if ((flags = mp->mnt_flag) != 0) {
675 int i;
676 const char *sep = " (";
677
678 for (i = 0; i < sizeof mnt_flags / sizeof mnt_flags[0]; i++) {
679 if (flags & mnt_flags[i].m_flag) {
680 (void)printf("%s%s", sep, mnt_flags[i].m_name);
681 flags &= ~mnt_flags[i].m_flag;
682 sep = ",";
683 }
684 }
685 if (flags)
686 (void)printf("%sunknown_flags:%x", sep, flags);
687 (void)printf(")");
688 }
689 (void)printf("\n");
690 }
691
692 char *
693 loadvnodes(int *avnodes)
694 {
695 int mib[2];
696 size_t copysize;
697 char *vnodebase;
698
699 if (memf != NULL) {
700 /*
701 * do it by hand
702 */
703 return (kinfo_vnodes(avnodes));
704 }
705 mib[0] = CTL_KERN;
706 mib[1] = KERN_VNODE;
707 if (sysctl(mib, 2, NULL, ©size, NULL, 0) == -1)
708 err(1, "sysctl: KERN_VNODE");
709 if ((vnodebase = malloc(copysize)) == NULL)
710 err(1, "malloc");
711 if (sysctl(mib, 2, vnodebase, ©size, NULL, 0) == -1)
712 err(1, "sysctl: KERN_VNODE");
713 if (copysize % (VPTRSZ + VNODESZ))
714 errx(1, "vnode size mismatch");
715 *avnodes = copysize / (VPTRSZ + VNODESZ);
716
717 return (vnodebase);
718 }
719
720 /*
721 * simulate what a running kernel does in in kinfo_vnode
722 */
723 char *
724 kinfo_vnodes(int *avnodes)
725 {
726 struct mntlist mountlist;
727 struct mount *mp, mount;
728 struct vnode *vp, vnode;
729 char *beg, *bp, *ep;
730 int numvnodes;
731
732 KGET(V_NUMV, numvnodes);
733 if ((bp = malloc((numvnodes + 20) * (VPTRSZ + VNODESZ))) == NULL)
734 err(1, "malloc");
735 beg = bp;
736 ep = bp + (numvnodes + 20) * (VPTRSZ + VNODESZ);
737 KGET(V_MOUNTLIST, mountlist);
738 for (mp = mountlist.cqh_first;;
739 mp = mount.mnt_list.cqe_next) {
740 KGET2(mp, &mount, sizeof(mount), "mount entry");
741 for (vp = mount.mnt_vnodelist.lh_first;
742 vp != NULL; vp = vnode.v_mntvnodes.le_next) {
743 KGET2(vp, &vnode, sizeof(vnode), "vnode");
744 if (bp + VPTRSZ + VNODESZ > ep)
745 /* XXX - should realloc */
746 errx(1, "no more room for vnodes");
747 memmove(bp, &vp, VPTRSZ);
748 bp += VPTRSZ;
749 memmove(bp, &vnode, VNODESZ);
750 bp += VNODESZ;
751 }
752 if (mp == mountlist.cqh_last)
753 break;
754 }
755 *avnodes = (bp - beg) / (VPTRSZ + VNODESZ);
756 return (beg);
757 }
758
759 void
760 ttymode(void)
761 {
762 int ntty;
763 struct ttylist_head tty_head;
764 struct tty *tp, tty;
765
766 KGET(TTY_NTTY, ntty);
767 (void)printf("%d terminal device%s\n", ntty, ntty == 1 ? "" : "s");
768 KGET(TTY_TTYLIST, tty_head);
769 (void)printf(
770 " LINE RAW CAN OUT HWT LWT COL STATE %-*s PGID DISC\n",
771 PTRSTRWIDTH, "SESS");
772 for (tp = tty_head.tqh_first; tp; tp = tty.tty_link.tqe_next) {
773 KGET2(tp, &tty, sizeof tty, "tty struct");
774 ttyprt(&tty);
775 }
776 }
777
778 static const struct flagbit_desc ttystates[] = {
779 { TS_ISOPEN, 'O'},
780 { TS_DIALOUT, '>'},
781 { TS_CARR_ON, 'C'},
782 { TS_TIMEOUT, 'T'},
783 { TS_FLUSH, 'F'},
784 { TS_BUSY, 'B'},
785 { TS_ASLEEP, 'A'},
786 { TS_XCLUDE, 'X'},
787 { TS_TTSTOP, 'S'},
788 { TS_TBLOCK, 'K'},
789 { TS_ASYNC, 'Y'},
790 { TS_BKSL, 'D'},
791 { TS_ERASE, 'E'},
792 { TS_LNCH, 'L'},
793 { TS_TYPEN, 'P'},
794 { TS_CNTTB, 'N'},
795 { 0, '\0'},
796 };
797
798 void
799 ttyprt(struct tty *tp)
800 {
801 char state[sizeof(ttystates) / sizeof(ttystates[0]) + 1];
802 char dev[2 + 3 + 1 + 5 + 1]; /* 12bit major + 20bit minor */
803 struct linesw t_linesw;
804 const char *name;
805 char buffer;
806 pid_t pgid;
807 int n, ovflw;
808
809 if (usenumflag || (name = devname(tp->t_dev, S_IFCHR)) == NULL) {
810 (void)snprintf(dev, sizeof(dev), "0x%3x:%x",
811 major(tp->t_dev), minor(tp->t_dev));
812 name = dev;
813 }
814 ovflw = 0;
815 PRWORD(ovflw, "%-*s", 7, 0, name);
816 PRWORD(ovflw, " %*d", 3, 1, tp->t_rawq.c_cc);
817 PRWORD(ovflw, " %*d", 4, 1, tp->t_canq.c_cc);
818 PRWORD(ovflw, " %*d", 4, 1, tp->t_outq.c_cc);
819 PRWORD(ovflw, " %*d", 5, 1, tp->t_hiwat);
820 PRWORD(ovflw, " %*d", 4, 1, tp->t_lowat);
821 PRWORD(ovflw, " %*d", 8, 1, tp->t_column);
822 n = getflags(ttystates, state, tp->t_state);
823 if (tp->t_wopen) {
824 state[n++] = 'W';
825 state[n] = '\0';
826 }
827 PRWORD(ovflw, " %-*s", 7, 1, state);
828 PRWORD(ovflw, " %*lX", PTRSTRWIDTH + 1, 1, (u_long)tp->t_session);
829 pgid = 0;
830 if (tp->t_pgrp != NULL)
831 KGET2(&tp->t_pgrp->pg_id, &pgid, sizeof(pid_t), "pgid");
832 PRWORD(ovflw, " %*d", 6, 1, pgid);
833 KGET2(tp->t_linesw, &t_linesw, sizeof(t_linesw),
834 "line discipline switch table");
835 name = t_linesw.l_name;
836 (void)putchar(' ');
837 for (;;) {
838 KGET2(name, &buffer, sizeof(buffer), "line discipline name");
839 if (buffer == '\0')
840 break;
841 (void)putchar(buffer);
842 name++;
843 }
844 (void)putchar('\n');
845 }
846
847 static const struct flagbit_desc filemode_flags[] = {
848 { FREAD, 'R' },
849 { FWRITE, 'W' },
850 { FAPPEND, 'A' },
851 #ifdef FSHLOCK /* currently gone */
852 { FSHLOCK, 'S' },
853 { FEXLOCK, 'X' },
854 #endif
855 { FASYNC, 'I' },
856 { 0, '\0' },
857 };
858
859 void
860 filemode(void)
861 {
862 struct file *fp;
863 struct file *addr;
864 char flags[sizeof(filemode_flags) / sizeof(filemode_flags[0])];
865 char *buf, *offset;
866 int len, maxfile, nfile, ovflw;
867
868 KGET(FNL_MAXFILE, maxfile);
869 if (totalflag) {
870 KGET(FNL_NFILE, nfile);
871 (void)printf("%3d/%3d files\n", nfile, maxfile);
872 return;
873 }
874 if (getfiles(&buf, &len, &offset) == -1)
875 return;
876 /*
877 * Getfiles returns in malloc'd memory a pointer to the first file
878 * structure, and then an array of file structs (whose addresses are
879 * derivable from the previous entry).
880 */
881 addr = ((struct filelist *)offset)->lh_first;
882 fp = (struct file *)(offset + sizeof(struct filelist));
883 nfile = (len - sizeof(struct filelist)) / sizeof(struct file);
884
885 (void)printf("%d/%d open files\n", nfile, maxfile);
886 (void)printf("%*s%s%*s TYPE FLG CNT MSG %*s%s%*s USE IFLG OFFSET\n",
887 (PTRSTRWIDTH - 4) / 2, "", " LOC", (PTRSTRWIDTH - 4) / 2, "",
888 (PTRSTRWIDTH - 4) / 2, "", "DATA", (PTRSTRWIDTH - 4) / 2, "");
889 for (; (char *)fp < offset + len; addr = fp->f_list.le_next, fp++) {
890 if ((unsigned)fp->f_type >= sizeof(dtypes) / sizeof(dtypes[0]))
891 continue;
892 ovflw = 0;
893 (void)getflags(filemode_flags, flags, fp->f_flag);
894 PRWORD(ovflw, "%*lx", PTRSTRWIDTH, 0, (long)addr);
895 PRWORD(ovflw, " %-*s", 9, 1, dtypes[fp->f_type]);
896 PRWORD(ovflw, " %*s", 6, 1, flags);
897 PRWORD(ovflw, " %*d", 5, 1, fp->f_count);
898 PRWORD(ovflw, " %*d", 5, 1, fp->f_msgcount);
899 PRWORD(ovflw, " %*lx", PTRSTRWIDTH + 1, 2, (long)fp->f_data);
900 PRWORD(ovflw, " %*d", 5, 1, fp->f_usecount);
901 PRWORD(ovflw, " %*x", 5, 1, fp->f_iflags);
902 if (fp->f_offset < 0)
903 PRWORD(ovflw, " %-*lld\n", PTRSTRWIDTH + 1, 2,
904 (long long)fp->f_offset);
905 else
906 PRWORD(ovflw, " %-*lld\n", PTRSTRWIDTH + 1, 2,
907 (long long)fp->f_offset);
908 }
909 free(buf);
910 }
911
912 int
913 getfiles(char **abuf, int *alen, char **aoffset)
914 {
915 size_t len;
916 int mib[2];
917 char *buf;
918 size_t offset;
919
920 /*
921 * XXX
922 * Add emulation of KINFO_FILE here.
923 */
924 if (memf != NULL)
925 errx(1, "files on dead kernel, not implemented");
926
927 mib[0] = CTL_KERN;
928 mib[1] = KERN_FILE;
929 if (sysctl(mib, 2, NULL, &len, NULL, 0) == -1) {
930 warn("sysctl: KERN_FILE");
931 return (-1);
932 }
933 /* We need to align (struct file *) in the buffer. */
934 offset = len % sizeof(off_t);
935 if ((buf = malloc(len + offset)) == NULL)
936 err(1, "malloc");
937 if (sysctl(mib, 2, buf + offset, &len, NULL, 0) == -1) {
938 warn("sysctl: KERN_FILE");
939 return (-1);
940 }
941 *abuf = buf;
942 *alen = len;
943 *aoffset = (buf + offset);
944 return (0);
945 }
946
947 void
948 usage(void)
949 {
950
951 (void)fprintf(stderr,
952 "usage: %s [-T|-f|-s|-t|-v] [-ghkmn] [-M core] [-N system]\n",
953 getprogname());
954 exit(1);
955 }
956