main.c revision 1.21 1 /* $NetBSD: main.c,v 1.21 1999/12/20 17:08:52 jwise Exp $ */
2
3 /*-
4 * Copyright (c) 1980, 1992, 1993
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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) Copyright (c) 1980, 1992, 1993\n\
39 The Regents of the University of California. All rights reserved.\n");
40 #if 0
41 static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/6/93";
42 #endif
43 __RCSID("$NetBSD: main.c,v 1.21 1999/12/20 17:08:52 jwise Exp $");
44 #endif /* not lint */
45
46 #include <sys/param.h>
47
48 #include <err.h>
49 #include <limits.h>
50 #include <nlist.h>
51 #include <signal.h>
52 #include <stdio.h>
53 #include <string.h>
54 #include <unistd.h>
55 #include <stdlib.h>
56 #include <ctype.h>
57
58 #include "systat.h"
59 #include "extern.h"
60
61 static struct nlist namelist[] = {
62 #define X_FIRST 0
63 #define X_HZ 0
64 { "_hz" },
65 #define X_STATHZ 1
66 { "_stathz" },
67 { "" }
68 };
69 static int dellave;
70
71 kvm_t *kd;
72 char *memf = NULL;
73 char *nlistf = NULL;
74 sig_t sigtstpdfl;
75 double avenrun[3];
76 int col;
77 int naptime = 5;
78 int verbose = 1; /* to report kvm read errs */
79 int hz, stathz;
80 char c;
81 char *namp;
82 char hostname[MAXHOSTNAMELEN + 1];
83 WINDOW *wnd;
84 int CMDLINE;
85
86 static WINDOW *wload; /* one line window for load average */
87
88 static void usage __P((void));
89 int main __P((int, char **));
90
91 gid_t egid; /* XXX needed by initiostat() and initkre() */
92
93 int
94 main(argc, argv)
95 int argc;
96 char **argv;
97 {
98 int ch;
99 char errbuf[_POSIX2_LINE_MAX];
100
101 egid = getegid();
102 (void)setegid(getgid());
103
104 while ((ch = getopt(argc, argv, "M:N:w:")) != -1)
105 switch(ch) {
106 case 'M':
107 memf = optarg;
108 break;
109 case 'N':
110 nlistf = optarg;
111 break;
112 case 'w':
113 if ((naptime = atoi(optarg)) <= 0)
114 errx(1, "interval <= 0.");
115 break;
116 case '?':
117 default:
118 usage();
119 }
120 argc -= optind;
121 argv += optind;
122
123
124 for ( ; argc > 0; argc--, argv++) {
125 struct mode *p;
126 int modefound = 0;
127
128 if (isdigit(argv[0][0])) {
129 naptime = atoi(argv[0]);
130 if (naptime <= 0)
131 naptime = 5;
132 continue;
133 }
134
135 for (p = modes; p->c_name ; p++) {
136 if (strcmp(argv[0], p->c_name) == 0) {
137 curmode = p;
138 modefound++;
139 break;
140 }
141 }
142
143 if (!modefound)
144 error("%s: Unknown command.", argv[0]);
145 }
146
147 /*
148 * Discard setgid privileges. If not the running kernel, we toss
149 * them away totally so that bad guys can't print interesting stuff
150 * from kernel memory, otherwise switch back to kmem for the
151 * duration of the kvm_openfiles() call.
152 */
153 if (nlistf != NULL || memf != NULL)
154 (void)setgid(getgid());
155 else
156 (void)setegid(egid);
157
158 kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
159 if (kd == NULL) {
160 error("%s", errbuf);
161 exit(1);
162 }
163
164 /* Get rid of privs for now. */
165 if (nlistf == NULL && memf == NULL)
166 (void)setegid(getgid());
167
168 if (kvm_nlist(kd, namelist)) {
169 nlisterr(namelist);
170 exit(1);
171 }
172 if (namelist[X_FIRST].n_type == 0)
173 errx(1, "couldn't read namelist");
174 signal(SIGINT, die);
175 signal(SIGQUIT, die);
176 signal(SIGTERM, die);
177 signal(SIGWINCH, redraw);
178
179 /*
180 * Initialize display. Load average appears in a one line
181 * window of its own. Current command's display appears in
182 * an overlapping sub-window of stdscr configured by the display
183 * routines to minimize update work by curses.
184 */
185 if (initscr() == NULL)
186 {
187 warnx("couldn't initialize screen");
188 exit(0);
189 }
190
191 CMDLINE = LINES - 1;
192 wnd = (*curmode->c_open)();
193 if (wnd == NULL) {
194 warnx("couldn't initialize display");
195 die(0);
196 }
197 wload = newwin(1, 0, 3, 20);
198 if (wload == NULL) {
199 warnx("couldn't set up load average window");
200 die(0);
201 }
202 gethostname(hostname, sizeof (hostname));
203 hostname[sizeof(hostname) - 1] = '\0';
204 NREAD(X_HZ, &hz, sizeof hz);
205 NREAD(X_STATHZ, &stathz, sizeof stathz);
206 (*curmode->c_init)();
207 curmode->c_flags |= CF_INIT;
208 labels();
209
210 dellave = 0.0;
211
212 signal(SIGALRM, display);
213 display(0);
214 noecho();
215 crmode();
216 keyboard();
217 /*NOTREACHED*/
218 }
219
220 static void
221 usage()
222 {
223 fprintf(stderr, "usage: systat [-M core] [-N system] [-w wait] "
224 "[display] [refresh-interval]\n");
225 exit(1);
226 }
227
228
229 void
230 labels()
231 {
232 if (curmode->c_flags & CF_LOADAV) {
233 mvaddstr(2, 20,
234 "/0 /1 /2 /3 /4 /5 /6 /7 /8 /9 /10");
235 mvaddstr(3, 5, "Load Average");
236 }
237 (*curmode->c_label)();
238 #ifdef notdef
239 mvprintw(21, 25, "CPU usage on %s", hostname);
240 #endif
241 refresh();
242 }
243
244 void
245 display(signo)
246 int signo;
247 {
248 int i, j;
249
250 /* Get the load average over the last minute. */
251 (void)getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0]));
252 (*curmode->c_fetch)();
253 if (curmode->c_flags & CF_LOADAV) {
254 j = 5.0*avenrun[0] + 0.5;
255 dellave -= avenrun[0];
256 if (dellave >= 0.0)
257 c = '<';
258 else {
259 c = '>';
260 dellave = -dellave;
261 }
262 if (dellave < 0.1)
263 c = '|';
264 dellave = avenrun[0];
265 wmove(wload, 0, 0); wclrtoeol(wload);
266 for (i = (j > 50) ? 50 : j; i > 0; i--)
267 waddch(wload, c);
268 if (j > 50)
269 wprintw(wload, " %4.1f", avenrun[0]);
270 }
271 (*curmode->c_refresh)();
272 if (curmode->c_flags & CF_LOADAV)
273 wrefresh(wload);
274 wrefresh(wnd);
275 move(CMDLINE, col);
276 refresh();
277 alarm(naptime);
278 }
279
280 void
281 redraw(signo)
282 int signo;
283 {
284 sigset_t set;
285
286 sigemptyset(&set);
287 sigaddset(&set, SIGALRM);
288 sigprocmask(SIG_BLOCK, &set, NULL);
289 wrefresh(curscr);
290 refresh();
291 sigprocmask(SIG_UNBLOCK, &set, NULL);
292 }
293
294 void
295 die(signo)
296 int signo;
297 {
298 move(CMDLINE, 0);
299 clrtoeol();
300 refresh();
301 endwin();
302 exit(0);
303 }
304
305 #if __STDC__
306 #include <stdarg.h>
307 #else
308 #include <varargs.h>
309 #endif
310
311 #if __STDC__
312 void
313 error(const char *fmt, ...)
314 #else
315 void
316 error(fmt, va_alist)
317 char *fmt;
318 va_dcl
319 #endif
320 {
321 va_list ap;
322 char buf[255];
323 int oy, ox;
324 #if __STDC__
325 va_start(ap, fmt);
326 #else
327 va_start(ap);
328 #endif
329
330 if (wnd) {
331 getyx(stdscr, oy, ox);
332 (void) vsnprintf(buf, sizeof(buf), fmt, ap);
333 clrtoeol();
334 standout();
335 mvaddstr(CMDLINE, 0, buf);
336 standend();
337 move(oy, ox);
338 refresh();
339 } else {
340 (void) vfprintf(stderr, fmt, ap);
341 fprintf(stderr, "\n");
342 }
343 va_end(ap);
344 }
345
346 void
347 nlisterr(namelist)
348 struct nlist namelist[];
349 {
350 int i, n;
351
352 n = 0;
353 clear();
354 mvprintw(2, 10, "systat: nlist: can't find following symbols:");
355 for (i = 0;
356 namelist[i].n_name != NULL && *namelist[i].n_name != '\0'; i++)
357 if (namelist[i].n_value == 0)
358 mvprintw(2 + ++n, 10, "%s", namelist[i].n_name);
359 move(CMDLINE, 0);
360 clrtoeol();
361 refresh();
362 endwin();
363 exit(1);
364 }
365