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