1 /*	$OpenBSD: mbufs.c,v 1.15 2004/07/09 16:33:15 deraadt Exp $	*/
2 /*	$NetBSD: mbufs.c,v 1.2 1995/01/20 08:52:02 jtc Exp $	*/
3 
4 /*-
5  * Copyright (c) 1980, 1992, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)mbufs.c	8.1 (Berkeley) 6/6/93";
36 #endif
37 static char rcsid[] = "$OpenBSD: mbufs.c,v 1.15 2004/07/09 16:33:15 deraadt Exp $";
38 #endif /* not lint */
39 
40 #include <sys/param.h>
41 #include <sys/types.h>
42 #include <sys/mbuf.h>
43 #include <sys/sysctl.h>
44 
45 #include <stdlib.h>
46 #include <string.h>
47 #include <err.h>
48 #include <paths.h>
49 #include "systat.h"
50 #include "extern.h"
51 
52 static struct mbstat mb;
53 
54 char *mtnames[] = {
55 	"free",
56 	"data",
57 	"headers",
58 	"sockets",
59 	"pcbs",
60 	"routes",
61 	"hosts",
62 	"arps",
63 	"socknames",
64 	"zombies",
65 	"sockopts",
66 	"frags",
67 	"rights",
68 	"ifaddrs",
69 };
70 
71 #define	NNAMES	(sizeof (mtnames) / sizeof (mtnames[0]))
72 
73 WINDOW *
openmbufs(void)74 openmbufs(void)
75 {
76 	return (subwin(stdscr, LINES-5-1, 0, 5, 0));
77 }
78 
79 void
closembufs(WINDOW * w)80 closembufs(WINDOW *w)
81 {
82 	if (w == NULL)
83 		return;
84 	wclear(w);
85 	wrefresh(w);
86 	delwin(w);
87 }
88 
89 void
labelmbufs(void)90 labelmbufs(void)
91 {
92 	wmove(wnd, 0, 0); wclrtoeol(wnd);
93 	mvwaddstr(wnd, 0, 10,
94 	    "/0   /5   /10  /15  /20  /25  /30  /35  /40  /45  /50  /55  /60");
95 }
96 
97 void
showmbufs(void)98 showmbufs(void)
99 {
100 	int i, j, max, index;
101 	char buf[13];
102 
103 	for (j = 0; j < wnd->_maxy; j++) {
104 		max = 0, index = -1;
105 		for (i = 0; i < wnd->_maxy; i++)
106 			if (mb.m_mtypes[i] > max) {
107 				max = mb.m_mtypes[i];
108 				index = i;
109 			}
110 		if (max == 0)
111 			break;
112 		if (j > NNAMES)
113 			mvwprintw(wnd, 1+j, 0, "%10d", index);
114 		else
115 			mvwprintw(wnd, 1+j, 0, "%-10.10s", mtnames[index]);
116 		wmove(wnd, 1 + j, 10);
117 		if (max > 60) {
118 			snprintf(buf, sizeof buf, " %d", max);
119 			max = 60;
120 			while (max--)
121 				waddch(wnd, 'X');
122 			waddstr(wnd, buf);
123 		} else {
124 			while (max--)
125 				waddch(wnd, 'X');
126 			wclrtoeol(wnd);
127 		}
128 		mb.m_mtypes[index] = 0;
129 	}
130 	wmove(wnd, 1+j, 0); wclrtobot(wnd);
131 }
132 
133 
134 void
fetchmbufs(void)135 fetchmbufs(void)
136 {
137 	int mib[2];
138 	size_t size = sizeof (mb);
139 
140 	mib[0] = CTL_KERN;
141 	mib[1] = KERN_MBSTAT;
142 	if (sysctl(mib, 2, &mb, &size, NULL, 0) < 0)
143 		err(1, "sysctl(KERN_MBSTAT) failed");
144 }
145 
146 int
initmbufs(void)147 initmbufs(void)
148 {
149 	return (1);
150 }
151