pstat.c revision 1.127 1 /* $NetBSD: pstat.c,v 1.127 2017/04/17 08:29:58 hannken 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\
35 The Regents of the University of California. All rights reserved.");
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.127 2017/04/17 08:29:58 hannken Exp $");
43 #endif
44 #endif /* not lint */
45
46 #define _KERNEL
47 #include <sys/types.h>
48 #undef _KERNEL
49 #include <sys/param.h>
50 #include <sys/time.h>
51 #include <sys/vnode.h>
52 #include <sys/ucred.h>
53 #include <stdbool.h>
54 #define _KERNEL
55 #define NFS
56 #include <sys/mount.h>
57 #undef NFS
58 #include <sys/file.h>
59 #include <ufs/ufs/inode.h>
60 #include <ufs/ufs/ufsmount.h>
61 #include <sys/uio.h>
62 #include <miscfs/genfs/layer.h>
63 #undef _KERNEL
64 #include <sys/stat.h>
65 #include <nfs/nfsproto.h>
66 #include <nfs/rpcv2.h>
67 #include <nfs/nfs.h>
68 #include <nfs/nfsnode.h>
69 #include <sys/ioctl.h>
70 #include <sys/tty.h>
71 #include <sys/conf.h>
72
73 #include <sys/sysctl.h>
74
75 #include <err.h>
76 #include <errno.h>
77 #include <kvm.h>
78 #include <limits.h>
79 #include <nlist.h>
80 #include <stdio.h>
81 #include <stdlib.h>
82 #include <string.h>
83 #include <unistd.h>
84
85 #include "swapctl.h"
86
87 struct nlist nl[] = {
88 #define V_LRU_FREE_LIST 0
89 { "_lru_free_list", 0, 0, 0, 0 }, /* address of lru free list. */
90 #define V_LRU_HOLD_LIST 1
91 { "_lru_hold_list", 0, 0, 0, 0 }, /* address of lru hold list. */
92 #define V_LRU_VRELE_LIST 2
93 { "_lru_vrele_list", 0, 0, 0, 0 }, /* address of lru vrele list. */
94 #define V_NUMV 3
95 { "_numvnodes", 0, 0, 0, 0 },
96 #define V_NEXT_OFFSET 4
97 { "_vnode_offset_next_by_lru", 0, 0, 0, 0 },
98 #define FNL_NFILE 5
99 { "_nfiles", 0, 0, 0, 0 },
100 #define FNL_MAXFILE 6
101 { "_maxfiles", 0, 0, 0, 0 },
102 #define TTY_NTTY 7
103 { "_tty_count", 0, 0, 0, 0 },
104 #define TTY_TTYLIST 8
105 { "_ttylist", 0, 0, 0, 0 },
106 #define NLMANDATORY TTY_TTYLIST /* names up to here are mandatory */
107 { "", 0, 0, 0, 0 }
108 };
109
110 int usenumflag;
111 int totalflag;
112 int kflag;
113 int hflag;
114 char *nlistf = NULL;
115 char *memf = NULL;
116 kvm_t *kd;
117
118 static const char * const dtypes[] = { DTYPE_NAMES };
119
120
121 static const struct {
122 u_int m_flag;
123 u_int m_visible;
124 const char *m_name;
125 } mnt_flags[] = {
126 __MNT_FLAGS
127 };
128
129 struct flagbit_desc {
130 u_int fd_flags;
131 char fd_mark;
132 };
133
134 #define SVAR(var) __STRING(var) /* to force expansion */
135 #define KGET(idx, var) \
136 KGET1(idx, &var, sizeof(var), SVAR(var))
137 #define KGET1(idx, p, s, msg) \
138 KGET2(nl[idx].n_value, p, s, msg)
139 #define KGET2(addr, p, s, msg) do { \
140 if (kvm_read(kd, (u_long)(addr), p, s) != s) \
141 warnx("cannot read %s: %s", msg, kvm_geterr(kd)); \
142 } while (/* CONSTCOND */0)
143 #define KGETRET(addr, p, s, msg) do { \
144 if (kvm_read(kd, (u_long)(addr), p, s) != s) { \
145 warnx("cannot read %s: %s", msg, kvm_geterr(kd)); \
146 return (0); \
147 } \
148 } while (/* CONSTCOND */0)
149
150 #if 1 /* This is copied from vmstat/vmstat.c */
151 /*
152 * Print single word. `ovflow' is number of characters didn't fit
153 * on the last word. `fmt' is a format string to print this word.
154 * It must contain asterisk for field width. `width' is a width
155 * occupied by this word. `fixed' is a number of constant chars in
156 * `fmt'. `val' is a value to be printed using format string `fmt'.
157 */
158 #define PRWORD(ovflw, fmt, width, fixed, val) do { \
159 (ovflw) += printf((fmt), \
160 (width) - (fixed) - (ovflw) > 0 ? \
161 (width) - (fixed) - (ovflw) : 0, \
162 (val)) - (width); \
163 if ((ovflw) < 0) \
164 (ovflw) = 0; \
165 } while (/* CONSTCOND */0)
166 #endif
167
168 void filemode(void);
169 int getfiles(char **, int *, char **);
170 int getflags(const struct flagbit_desc *, char *, u_int);
171 struct mount *
172 getmnt(struct mount *);
173 char * kinfo_vnodes(int *);
174 void layer_header(void);
175 int layer_print(struct vnode *, int);
176 char * loadvnodes(int *);
177 int main(int, char **);
178 void mount_print(struct mount *);
179 void nfs_header(void);
180 int nfs_print(struct vnode *, int);
181 void ttymode(void);
182 void ttyprt(struct tty *);
183 void ufs_header(void);
184 int ufs_print(struct vnode *, int);
185 int ext2fs_print(struct vnode *, int);
186 __dead void usage(void);
187 void vnode_header(void);
188 int vnode_print(struct vnode *, struct vnode *);
189 void vnodemode(void);
190
191 int
192 main(int argc, char *argv[])
193 {
194 int ch, i, quit, ret, use_sysctl;
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:fghikmnstv")) != -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 'g':
228 kflag = 3; /* 1k ^ 3 */
229 break;
230 case 'h':
231 hflag = 1;
232 break;
233 case 'm':
234 kflag = 2; /* 1k ^ 2 */
235 break;
236 case 'v':
237 case 'i': /* Backward compatibility. */
238 vnodeflag = 1;
239 break;
240 default:
241 usage();
242 }
243 argc -= optind;
244 argv += optind;
245
246 /*
247 * Discard setgid privileges. If not the running kernel, we toss
248 * them away totally so that bad guys can't print interesting stuff
249 * from kernel memory, otherwise switch back to kmem for the
250 * duration of the kvm_openfiles() call.
251 */
252 if (nlistf != NULL || memf != NULL)
253 (void)setgid(getgid());
254 else
255 (void)setegid(egid);
256
257 use_sysctl = (nlistf == NULL && memf == NULL);
258
259 if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == 0)
260 errx(1, "kvm_openfiles: %s", buf);
261
262 /* get rid of it now anyway */
263 if (nlistf == NULL && memf == NULL)
264 (void)setgid(getgid());
265 if ((ret = kvm_nlist(kd, nl)) != 0) {
266 if (ret == -1)
267 errx(1, "kvm_nlist: %s", kvm_geterr(kd));
268 for (i = quit = 0; i <= NLMANDATORY; i++)
269 if (!nl[i].n_value) {
270 quit = 1;
271 warnx("undefined symbol: %s", nl[i].n_name);
272 }
273 if (quit)
274 exit(1);
275 }
276 if (!(fileflag | vnodeflag | ttyflag | swapflag | totalflag))
277 usage();
278 if (fileflag || totalflag)
279 filemode();
280 if (vnodeflag || totalflag)
281 vnodemode();
282 if (ttyflag)
283 ttymode();
284 if (swapflag || totalflag)
285 if (use_sysctl)
286 list_swap(0, kflag, 0, totalflag, 1, hflag);
287 exit(0);
288 }
289
290 #define VPTRSZ sizeof(struct vnode *)
291 #define VNODESZ sizeof(struct vnode)
292 #define PTRSTRWIDTH ((int)sizeof(void *) * 2) /* Width of resulting string
293 when pointer is printed
294 in hexadecimal. */
295
296 static void
297 devprintf(char *buf, size_t buflen, dev_t dev)
298 {
299 (void)snprintf(buf, buflen, "%llu,%llu",
300 (unsigned long long)major(dev),
301 (unsigned long long)minor(dev));
302 }
303
304 void
305 vnodemode(void)
306 {
307 char *e_vnodebase, *endvnode, *evp;
308 struct vnode *vp;
309 struct mount *maddr, *mp;
310 int numvnodes, ovflw;
311 int (*vnode_fsprint) (struct vnode *, int); /* per-fs data printer */
312
313 mp = NULL;
314 e_vnodebase = loadvnodes(&numvnodes);
315 if (totalflag) {
316 (void)printf("%7d vnodes\n", numvnodes);
317 goto out;
318 }
319 endvnode = e_vnodebase + numvnodes * (VPTRSZ + VNODESZ);
320 (void)printf("%d active vnodes\n", numvnodes);
321
322 #define ST mp->mnt_stat
323 #define FSTYPE_IS(mp, name) \
324 (strncmp((mp)->mnt_stat.f_fstypename, (name), \
325 sizeof((mp)->mnt_stat.f_fstypename)) == 0)
326 maddr = NULL;
327 vnode_fsprint = NULL;
328 for (evp = e_vnodebase; evp < endvnode; evp += VPTRSZ + VNODESZ) {
329 vp = (struct vnode *)(evp + VPTRSZ);
330 if (vp->v_mount != maddr) {
331 /*
332 * New filesystem
333 */
334 if ((mp = getmnt(vp->v_mount)) == NULL)
335 continue;
336 maddr = vp->v_mount;
337 mount_print(mp);
338 vnode_header();
339 /*
340 * XXX do this in a more fs-independent way
341 */
342 if (FSTYPE_IS(mp, MOUNT_FFS) ||
343 FSTYPE_IS(mp, MOUNT_MFS)) {
344 ufs_header();
345 vnode_fsprint = ufs_print;
346 } else if (FSTYPE_IS(mp, MOUNT_NFS)) {
347 nfs_header();
348 vnode_fsprint = nfs_print;
349 } else if (FSTYPE_IS(mp, MOUNT_EXT2FS)) {
350 ufs_header();
351 vnode_fsprint = ext2fs_print;
352 } else if (FSTYPE_IS(mp, MOUNT_NULL) ||
353 FSTYPE_IS(mp, MOUNT_OVERLAY) ||
354 FSTYPE_IS(mp, MOUNT_UMAP)) {
355 layer_header();
356 vnode_fsprint = layer_print;
357 } else
358 vnode_fsprint = NULL;
359 (void)printf("\n");
360 }
361 ovflw = vnode_print(*(struct vnode **)evp, vp);
362 if (VTOI(vp) != NULL && vnode_fsprint != NULL)
363 (*vnode_fsprint)(vp, ovflw);
364 (void)printf("\n");
365 }
366
367 out:
368 if (e_vnodebase)
369 free(e_vnodebase);
370 }
371
372 int
373 getflags(const struct flagbit_desc *fd, char *p, u_int flags)
374 {
375 char *q = p;
376
377 if (flags == 0) {
378 *p++ = '-';
379 *p = '\0';
380 return (0);
381 }
382
383 for (; fd->fd_flags != 0; fd++)
384 if ((flags & fd->fd_flags) != 0)
385 *p++ = fd->fd_mark;
386 *p = '\0';
387 return (p - q);
388 }
389
390 const struct flagbit_desc vnode_flags[] = {
391 { VV_ROOT, 'R' },
392 { VI_TEXT, 'T' },
393 { VV_SYSTEM, 'S' },
394 { VV_ISTTY, 'I' },
395 { VI_EXECMAP, 'E' },
396 { VU_DIROP, 'D' },
397 { VI_ONWORKLST, 'O' },
398 { 0, '\0' },
399 };
400
401 void
402 vnode_header(void)
403 {
404
405 (void)printf("%-*s TYP VFLAG USE HOLD TAG NPAGE",
406 PTRSTRWIDTH, "ADDR");
407 }
408
409 int
410 vnode_print(struct vnode *avnode, struct vnode *vp)
411 {
412 const char *type;
413 char flags[sizeof(vnode_flags) / sizeof(vnode_flags[0])];
414 int ovflw;
415
416 /*
417 * set type
418 */
419 switch (vp->v_type) {
420 case VNON:
421 type = "non"; break;
422 case VREG:
423 type = "reg"; break;
424 case VDIR:
425 type = "dir"; break;
426 case VBLK:
427 type = "blk"; break;
428 case VCHR:
429 type = "chr"; break;
430 case VLNK:
431 type = "lnk"; break;
432 case VSOCK:
433 type = "soc"; break;
434 case VFIFO:
435 type = "fif"; break;
436 case VBAD:
437 type = "bad"; break;
438 default:
439 type = "unk"; break;
440 }
441 /*
442 * gather flags
443 */
444 (void)getflags(vnode_flags, flags,
445 vp->v_uflag | vp->v_iflag | vp->v_vflag);
446
447 ovflw = 0;
448 PRWORD(ovflw, "%*lx", PTRSTRWIDTH, 0, (long)avnode);
449 PRWORD(ovflw, " %*s", 4, 1, type);
450 PRWORD(ovflw, " %*s", 6, 1, flags);
451 PRWORD(ovflw, " %*ld", 5, 1, (long)vp->v_usecount);
452 PRWORD(ovflw, " %*ld", 5, 1, (long)vp->v_holdcnt);
453 PRWORD(ovflw, " %*d", 4, 1, vp->v_tag);
454 PRWORD(ovflw, " %*d", 6, 1, vp->v_uobj.uo_npages);
455 return (ovflw);
456 }
457
458 const struct flagbit_desc ufs_flags[] = {
459 { IN_ACCESS, 'A' },
460 { IN_CHANGE, 'C' },
461 { IN_UPDATE, 'U' },
462 { IN_MODIFIED, 'M' },
463 { IN_ACCESSED, 'a' },
464 { IN_SHLOCK, 'S' },
465 { IN_EXLOCK, 'E' },
466 { IN_SPACECOUNTED, 's' },
467 { 0, '\0' },
468 };
469
470 void
471 ufs_header(void)
472 {
473
474 (void)printf(" FILEID IFLAG RDEV|SZ");
475 }
476
477 int
478 ufs_print(struct vnode *vp, int ovflw)
479 {
480 struct inode inode, *ip = &inode;
481 union dinode {
482 struct ufs1_dinode dp1;
483 struct ufs2_dinode dp2;
484 } dip;
485 struct ufsmount ump;
486 char flags[sizeof(ufs_flags) / sizeof(ufs_flags[0])];
487 char dev[4 + 1 + 7 + 1]; /* 12bit marjor + 20bit minor */
488 char *name;
489 mode_t type;
490 dev_t rdev;
491
492 KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode");
493 KGETRET(ip->i_ump, &ump, sizeof(struct ufsmount),
494 "vnode's mount point");
495
496 if (ump.um_fstype == UFS1) {
497 KGETRET(ip->i_din.ffs1_din, &dip, sizeof (struct ufs1_dinode),
498 "inode's dinode");
499 rdev = (uint32_t)dip.dp1.di_rdev;
500 } else {
501 KGETRET(ip->i_din.ffs2_din, &dip, sizeof (struct ufs2_dinode),
502 "inode's UFS2 dinode");
503 rdev = dip.dp2.di_rdev;
504 }
505
506 /*
507 * XXX need to to locking state.
508 */
509
510 (void)getflags(ufs_flags, flags, ip->i_flag);
511 PRWORD(ovflw, " %*llu", 7, 1, (unsigned long long)ip->i_number);
512 PRWORD(ovflw, " %*s", 6, 1, flags);
513 type = ip->i_mode & S_IFMT;
514 if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode)) {
515 if (usenumflag ||
516 (name = devname(rdev, type)) == NULL) {
517 devprintf(dev, sizeof(dev), rdev);
518 name = dev;
519 }
520 PRWORD(ovflw, " %*s", 8, 1, name);
521 } else
522 PRWORD(ovflw, " %*lld", 8, 1, (long long)ip->i_size);
523 return 0;
524 }
525
526 int
527 ext2fs_print(struct vnode *vp, int ovflw)
528 {
529 struct inode inode, *ip = &inode;
530 struct ext2fs_dinode dip;
531 char flags[sizeof(ufs_flags) / sizeof(ufs_flags[0])];
532 char dev[4 + 1 + 7 + 1]; /* 12bit marjor + 20bit minor */
533 char *name;
534 mode_t type;
535
536 KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode");
537 KGETRET(ip->i_din.e2fs_din, &dip, sizeof (struct ext2fs_dinode),
538 "inode's dinode");
539
540 /*
541 * XXX need to to locking state.
542 */
543
544 (void)getflags(ufs_flags, flags, ip->i_flag);
545 PRWORD(ovflw, " %*llu", 7, 1, (unsigned long long)ip->i_number);
546 PRWORD(ovflw, " %*s", 6, 1, flags);
547 type = dip.e2di_mode & S_IFMT;
548 if (S_ISCHR(dip.e2di_mode) || S_ISBLK(dip.e2di_mode)) {
549 if (usenumflag ||
550 (name = devname(dip.e2di_rdev, type)) == NULL) {
551 devprintf(dev, sizeof(dev), dip.e2di_rdev);
552 name = dev;
553 }
554 PRWORD(ovflw, " %*s", 8, 1, name);
555 } else
556 PRWORD(ovflw, " %*u", 8, 1, (u_int)dip.e2di_size);
557 return (0);
558 }
559
560 const struct flagbit_desc nfs_flags[] = {
561 { NFLUSHWANT, 'W' },
562 { NFLUSHINPROG, 'P' },
563 { NMODIFIED, 'M' },
564 { NWRITEERR, 'E' },
565 { NACC, 'A' },
566 { NUPD, 'U' },
567 { NCHG, 'C' },
568 { 0, '\0' },
569 };
570
571 void
572 nfs_header(void)
573 {
574
575 (void)printf(" FILEID NFLAG RDEV|SZ");
576 }
577
578 int
579 nfs_print(struct vnode *vp, int ovflw)
580 {
581 struct nfsnode nfsnode, *np = &nfsnode;
582 char flags[sizeof(nfs_flags) / sizeof(nfs_flags[0])];
583 char dev[4 + 1 + 7 + 1]; /* 12bit marjor + 20bit minor */
584 struct vattr va;
585 char *name;
586 mode_t type;
587
588 KGETRET(VTONFS(vp), &nfsnode, sizeof(nfsnode), "vnode's nfsnode");
589 (void)getflags(nfs_flags, flags, np->n_flag);
590
591 KGETRET(np->n_vattr, &va, sizeof(va), "vnode attr");
592 PRWORD(ovflw, " %*ld", 7, 1, (long)va.va_fileid);
593 PRWORD(ovflw, " %*s", 6, 1, flags);
594 switch (va.va_type) {
595 case VCHR:
596 type = S_IFCHR;
597 goto device;
598
599 case VBLK:
600 type = S_IFBLK;
601 device:
602 if (usenumflag || (name = devname(va.va_rdev, type)) == NULL) {
603 devprintf(dev, sizeof(dev), va.va_rdev);
604 name = dev;
605 }
606 PRWORD(ovflw, " %*s", 8, 1, name);
607 break;
608 default:
609 PRWORD(ovflw, " %*lld", 8, 1, (long long)np->n_size);
610 break;
611 }
612 return (0);
613 }
614
615 void
616 layer_header(void)
617 {
618
619 (void)printf(" %*s", PTRSTRWIDTH, "LOWER");
620 }
621
622 int
623 layer_print(struct vnode *vp, int ovflw)
624 {
625 struct layer_node lnode, *lp = &lnode;
626
627 KGETRET(VTOLAYER(vp), &lnode, sizeof(lnode), "layer vnode");
628
629 PRWORD(ovflw, " %*lx", PTRSTRWIDTH + 1, 1, (long)lp->layer_lowervp);
630 return (0);
631 }
632
633 /*
634 * Given a pointer to a mount structure in kernel space,
635 * read it in and return a usable pointer to it.
636 */
637 struct mount *
638 getmnt(struct mount *maddr)
639 {
640 static struct mtab {
641 struct mtab *next;
642 struct mount *maddr;
643 struct mount mount;
644 } *mhead = NULL;
645 struct mtab *mt;
646 struct mount mb;
647
648 for (mt = mhead; mt != NULL; mt = mt->next)
649 if (maddr == mt->maddr)
650 return (&mt->mount);
651 KGETRET(maddr, &mb, sizeof(struct mount), "mount table");
652 if ((mt = malloc(sizeof(struct mtab))) == NULL)
653 err(1, "malloc");
654 mt->mount = mb;
655 mt->maddr = maddr;
656 mt->next = mhead;
657 mhead = mt;
658 return (&mt->mount);
659 }
660
661 void
662 mount_print(struct mount *mp)
663 {
664 int flags;
665
666 (void)printf("*** MOUNT %s %s on %s", ST.f_fstypename,
667 ST.f_mntfromname, ST.f_mntonname);
668 if ((flags = mp->mnt_flag) != 0) {
669 size_t i;
670 const char *sep = " (";
671
672 for (i = 0; i < sizeof mnt_flags / sizeof mnt_flags[0]; i++) {
673 if (flags & mnt_flags[i].m_flag) {
674 (void)printf("%s%s", sep, mnt_flags[i].m_name);
675 flags &= ~mnt_flags[i].m_flag;
676 sep = ",";
677 }
678 }
679 if (flags)
680 (void)printf("%sunknown_flags:%x", sep, flags);
681 (void)printf(")");
682 }
683 (void)printf("\n");
684 }
685
686 char *
687 loadvnodes(int *avnodes)
688 {
689 int mib[2];
690 int status;
691 size_t copysize;
692 #if 0
693 size_t oldsize;
694 #endif
695 char *vnodebase;
696
697 if (totalflag) {
698 KGET(V_NUMV, *avnodes);
699 return NULL;
700 }
701 if (memf != NULL) {
702 /*
703 * do it by hand
704 */
705 return (kinfo_vnodes(avnodes));
706 }
707 mib[0] = CTL_KERN;
708 mib[1] = KERN_VNODE;
709 /*
710 * First sysctl call gets the necessary buffer size; second
711 * sysctl call gets the data. We allow for some growth in the
712 * data size between the two sysctl calls (increases of a few
713 * thousand vnodes in between the two calls have been observed).
714 * We ignore ENOMEM from the second sysctl call, which can
715 * happen if the kernel's data grew by even more than we allowed
716 * for.
717 */
718 if (sysctl(mib, 2, NULL, ©size, NULL, 0) == -1)
719 err(1, "sysctl: KERN_VNODE");
720 #if 0
721 oldsize = copysize;
722 #endif
723 copysize += 100 * sizeof(struct vnode) + copysize / 20;
724 if ((vnodebase = malloc(copysize)) == NULL)
725 err(1, "malloc");
726 status = sysctl(mib, 2, vnodebase, ©size, NULL, 0);
727 if (status == -1 && errno != ENOMEM)
728 err(1, "sysctl: KERN_VNODE");
729 #if 0 /* for debugging the amount of growth allowed for */
730 if (copysize != oldsize) {
731 warnx("count changed from %ld to %ld (%+ld)%s",
732 (long)(oldsize / sizeof(struct vnode)),
733 (long)(copysize / sizeof(struct vnode)),
734 (long)(copysize / sizeof(struct vnode)) -
735 (long)(oldsize / sizeof(struct vnode)),
736 (status == 0 ? "" : ", and errno = ENOMEM"));
737 }
738 #endif
739 if (copysize % (VPTRSZ + VNODESZ))
740 errx(1, "vnode size mismatch");
741 *avnodes = copysize / (VPTRSZ + VNODESZ);
742
743 return (vnodebase);
744 }
745
746 /*
747 * simulate what a running kernel does in in kinfo_vnode
748 */
749 static int
750 vnode_cmp(const void *p1, const void *p2)
751 {
752 const char *s1 = (const char *)p1;
753 const char *s2 = (const char *)p2;
754 const struct vnode *v1 = (const struct vnode *)(s1 + VPTRSZ);
755 const struct vnode *v2 = (const struct vnode *)(s2 + VPTRSZ);
756
757 return (v2->v_mount - v1->v_mount);
758 }
759
760 char *
761 kinfo_vnodes(int *avnodes)
762 {
763 int i;
764 char *beg, *bp, *ep;
765 int numvnodes, next_offset;
766
767 KGET(V_NUMV, numvnodes);
768 if ((bp = malloc((numvnodes + 20) * (VPTRSZ + VNODESZ))) == NULL)
769 err(1, "malloc");
770 beg = bp;
771 ep = bp + (numvnodes + 20) * (VPTRSZ + VNODESZ);
772 KGET(V_NEXT_OFFSET, next_offset);
773
774 for (i = V_LRU_FREE_LIST; i <= V_LRU_VRELE_LIST; i++) {
775 TAILQ_HEAD(vnodelst, vnode) lru_head;
776 struct vnode *vp, vnode;
777
778 KGET(i, lru_head);
779 vp = TAILQ_FIRST(&lru_head);
780 while (vp != NULL) {
781 KGET2(vp, &vnode, sizeof(vnode), "vnode");
782 if (bp + VPTRSZ + VNODESZ > ep)
783 /* XXX - should realloc */
784 errx(1, "no more room for vnodes");
785 memmove(bp, &vp, VPTRSZ);
786 bp += VPTRSZ;
787 memmove(bp, &vnode, VNODESZ);
788 bp += VNODESZ;
789 KGET2((char *)vp + next_offset, &vp, sizeof(vp), "nvp");
790 }
791 }
792 *avnodes = (bp - beg) / (VPTRSZ + VNODESZ);
793 /* Sort by mount like we get it from sysctl. */
794 qsort(beg, *avnodes, VPTRSZ + VNODESZ, vnode_cmp);
795 return (beg);
796 }
797
798 void
799 ttymode(void)
800 {
801 int ntty;
802 struct ttylist_head tty_head;
803 struct tty *tp, tty;
804
805 KGET(TTY_NTTY, ntty);
806 (void)printf("%d terminal device%s\n", ntty, ntty == 1 ? "" : "s");
807 KGET(TTY_TTYLIST, tty_head);
808 (void)printf(
809 " LINE RAW CAN OUT HWT LWT COL STATE %-*s PGID DISC\n",
810 PTRSTRWIDTH, "SESS");
811 for (tp = tty_head.tqh_first; tp; tp = tty.tty_link.tqe_next) {
812 KGET2(tp, &tty, sizeof tty, "tty struct");
813 ttyprt(&tty);
814 }
815 }
816
817 static const struct flagbit_desc ttystates[] = {
818 { TS_ISOPEN, 'O'},
819 { TS_DIALOUT, '>'},
820 { TS_CARR_ON, 'C'},
821 { TS_TIMEOUT, 'T'},
822 { TS_FLUSH, 'F'},
823 { TS_BUSY, 'B'},
824 { TS_XCLUDE, 'X'},
825 { TS_TTSTOP, 'S'},
826 { TS_TBLOCK, 'K'},
827 { TS_ASYNC, 'Y'},
828 { TS_BKSL, 'D'},
829 { TS_ERASE, 'E'},
830 { TS_LNCH, 'L'},
831 { TS_TYPEN, 'P'},
832 { TS_CNTTB, 'N'},
833 { 0, '\0'},
834 };
835
836 void
837 ttyprt(struct tty *tp)
838 {
839 char state[sizeof(ttystates) / sizeof(ttystates[0]) + 1];
840 char dev[2 + 3 + 1 + 5 + 1]; /* 12bit major + 20bit minor */
841 struct linesw t_linesw;
842 const char *name;
843 char buffer;
844 pid_t pgid;
845 int n, ovflw;
846
847 if (usenumflag || (name = devname(tp->t_dev, S_IFCHR)) == NULL) {
848 devprintf(dev, sizeof(dev), tp->t_dev);
849 name = dev;
850 }
851 ovflw = 0;
852 PRWORD(ovflw, "%-*s", 7, 0, name);
853 PRWORD(ovflw, " %*d", 3, 1, tp->t_rawq.c_cc);
854 PRWORD(ovflw, " %*d", 4, 1, tp->t_canq.c_cc);
855 PRWORD(ovflw, " %*d", 4, 1, tp->t_outq.c_cc);
856 PRWORD(ovflw, " %*d", 5, 1, tp->t_hiwat);
857 PRWORD(ovflw, " %*d", 4, 1, tp->t_lowat);
858 PRWORD(ovflw, " %*d", 8, 1, tp->t_column);
859 n = getflags(ttystates, state, tp->t_state);
860 if (tp->t_wopen) {
861 state[n++] = 'W';
862 state[n] = '\0';
863 }
864 PRWORD(ovflw, " %-*s", 7, 1, state);
865 PRWORD(ovflw, " %*lX", PTRSTRWIDTH + 1, 1, (u_long)tp->t_session);
866 pgid = 0;
867 if (tp->t_pgrp != NULL)
868 KGET2(&tp->t_pgrp->pg_id, &pgid, sizeof(pid_t), "pgid");
869 PRWORD(ovflw, " %*d", 6, 1, pgid);
870 KGET2(tp->t_linesw, &t_linesw, sizeof(t_linesw),
871 "line discipline switch table");
872 name = t_linesw.l_name;
873 (void)putchar(' ');
874 for (;;) {
875 KGET2(name, &buffer, sizeof(buffer), "line discipline name");
876 if (buffer == '\0')
877 break;
878 (void)putchar(buffer);
879 name++;
880 }
881 (void)putchar('\n');
882 }
883
884 static const struct flagbit_desc filemode_flags[] = {
885 { FREAD, 'R' },
886 { FWRITE, 'W' },
887 { FAPPEND, 'A' },
888 #ifdef FSHLOCK /* currently gone */
889 { FSHLOCK, 'S' },
890 { FEXLOCK, 'X' },
891 #endif
892 { FASYNC, 'I' },
893 { 0, '\0' },
894 };
895
896 void
897 filemode(void)
898 {
899 struct kinfo_file *ki;
900 char flags[sizeof(filemode_flags) / sizeof(filemode_flags[0])];
901 char *buf, *offset;
902 int len, maxfile, nfile, ovflw;
903
904 KGET(FNL_MAXFILE, maxfile);
905 if (totalflag) {
906 KGET(FNL_NFILE, nfile);
907 (void)printf("%3d/%3d files\n", nfile, maxfile);
908 return;
909 }
910 if (getfiles(&buf, &len, &offset) == -1)
911 return;
912 /*
913 * Getfiles returns in malloc'd memory to an array of kinfo_file2
914 * structures.
915 */
916 nfile = len / sizeof(struct kinfo_file);
917
918 (void)printf("%d/%d open files\n", nfile, maxfile);
919 (void)printf("%*s%s%*s TYPE FLG CNT MSG %*s%s%*s IFLG OFFSET\n",
920 (PTRSTRWIDTH - 4) / 2, "", " LOC", (PTRSTRWIDTH - 4) / 2, "",
921 (PTRSTRWIDTH - 4) / 2, "", "DATA", (PTRSTRWIDTH - 4) / 2, "");
922 for (ki = (struct kinfo_file *)offset; nfile--; ki++) {
923 if ((unsigned)ki->ki_ftype >= sizeof(dtypes) / sizeof(dtypes[0]))
924 continue;
925 ovflw = 0;
926 (void)getflags(filemode_flags, flags, ki->ki_flag);
927 PRWORD(ovflw, "%*lx", PTRSTRWIDTH, 0, (long)ki->ki_fileaddr);
928 PRWORD(ovflw, " %-*s", 9, 1, dtypes[ki->ki_ftype]);
929 PRWORD(ovflw, " %*s", 6, 1, flags);
930 PRWORD(ovflw, " %*d", 5, 1, ki->ki_count);
931 PRWORD(ovflw, " %*d", 5, 1, ki->ki_msgcount);
932 PRWORD(ovflw, " %*lx", PTRSTRWIDTH + 1, 2, (long)ki->ki_fdata);
933 PRWORD(ovflw, " %*x", 5, 1, 0);
934 if ((off_t)ki->ki_foffset < 0)
935 PRWORD(ovflw, " %-*lld\n", PTRSTRWIDTH + 1, 2,
936 (long long)ki->ki_foffset);
937 else
938 PRWORD(ovflw, " %-*lld\n", PTRSTRWIDTH + 1, 2,
939 (long long)ki->ki_foffset);
940 }
941 free(buf);
942 }
943
944 int
945 getfiles(char **abuf, int *alen, char **aoffset)
946 {
947 size_t len;
948 int mib[6];
949 char *buf;
950 size_t offset;
951
952 /*
953 * XXX
954 * Add emulation of KINFO_FILE here.
955 */
956 if (memf != NULL)
957 errx(1, "files on dead kernel, not implemented");
958
959 mib[0] = CTL_KERN;
960 mib[1] = KERN_FILE2;
961 mib[2] = KERN_FILE_BYFILE;
962 mib[3] = 0;
963 mib[4] = sizeof(struct kinfo_file);
964 mib[5] = 0;
965 if (sysctl(mib, 6, NULL, &len, NULL, 0) == -1) {
966 warn("sysctl: KERN_FILE2");
967 return (-1);
968 }
969 /* We need to align (struct kinfo_file *) in the buffer. */
970 offset = len % sizeof(off_t);
971 mib[5] = len / sizeof(struct kinfo_file);
972 if ((buf = malloc(len + offset)) == NULL)
973 err(1, "malloc");
974 if (sysctl(mib, 6, buf + offset, &len, NULL, 0) == -1) {
975 warn("sysctl: 2nd KERN_FILE2");
976 free(buf);
977 return (-1);
978 }
979 *abuf = buf;
980 *alen = len;
981 *aoffset = (buf + offset);
982 return (0);
983 }
984
985 void
986 usage(void)
987 {
988
989 (void)fprintf(stderr,
990 "usage: %s [-T|-f|-s|-t|-v] [-ghkmn] [-M core] [-N system]\n",
991 getprogname());
992 exit(1);
993 }
994