main.c revision 1.35 1 /* $NetBSD: main.c,v 1.35 2003/08/07 11:15:59 agc 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. 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, 1992, 1993\n\
35 The Regents of the University of California. All rights reserved.\n");
36 #if 0
37 static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/6/93";
38 #endif
39 __RCSID("$NetBSD: main.c,v 1.35 2003/08/07 11:15:59 agc Exp $");
40 #endif /* not lint */
41
42 #include <sys/param.h>
43
44 #include <ctype.h>
45 #include <err.h>
46 #include <limits.h>
47 #include <signal.h>
48 #include <stdarg.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <unistd.h>
53 #include <termios.h>
54 #include <sys/ioctl.h>
55
56 #include "systat.h"
57 #include "extern.h"
58
59 static struct nlist namelist[] = {
60 #define X_FIRST 0
61 #define X_HZ 0
62 { "_hz" },
63 #define X_STATHZ 1
64 { "_stathz" },
65 #define X_MAXSLP 2
66 { "_maxslp" },
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, maxslp;
80 char c;
81 char *namp;
82 char hostname[MAXHOSTNAMELEN + 1];
83 WINDOW *wnd;
84 int CMDLINE;
85 int turns = 2; /* stay how many refresh-turns in 'all' mode? */
86 int allflag;
87 int allcounter;
88
89 static WINDOW *wload; /* one line window for load average */
90
91 static void (*sv_stop_handler)(int);
92
93 static void stop(int);
94 static void usage(void);
95 int main(int, char **);
96
97 gid_t egid; /* XXX needed by initiostat() and initkre() */
98
99 int
100 main(int argc, char **argv)
101 {
102 int ch;
103 char errbuf[_POSIX2_LINE_MAX];
104
105 egid = getegid();
106 (void)setegid(getgid());
107
108 while ((ch = getopt(argc, argv, "M:N:nw:t:")) != -1)
109 switch(ch) {
110 case 'M':
111 memf = optarg;
112 break;
113 case 'N':
114 nlistf = optarg;
115 break;
116 case 'n':
117 nflag = !nflag;
118 break;
119 case 'w':
120 if ((naptime = atoi(optarg)) <= 0)
121 errx(1, "interval <= 0.");
122 break;
123 case 't':
124 if ((turns = atoi(optarg)) <= 0)
125 errx(1, "turns <= 0.");
126 break;
127 case '?':
128 default:
129 usage();
130 }
131 argc -= optind;
132 argv += optind;
133
134
135 for ( ; argc > 0; argc--, argv++) {
136 struct mode *p;
137 int modefound = 0;
138
139 if (isdigit(argv[0][0])) {
140 naptime = atoi(argv[0]);
141 if (naptime <= 0)
142 naptime = 5;
143 continue;
144 }
145
146 for (p = modes; p->c_name ; p++) {
147 if (strstr(p->c_name, argv[0]) == p->c_name) {
148 curmode = p;
149 modefound++;
150 break;
151 }
152
153 if(strstr("all",argv[0]) == "all"){
154 allcounter=0;
155 allflag=1;
156 }
157 }
158
159
160 if (!modefound && !allflag)
161 error("%s: Unknown command.", argv[0]);
162 }
163
164 /*
165 * Discard setgid privileges. If not the running kernel, we toss
166 * them away totally so that bad guys can't print interesting stuff
167 * from kernel memory, otherwise switch back to kmem for the
168 * duration of the kvm_openfiles() call.
169 */
170 if (nlistf != NULL || memf != NULL)
171 (void)setgid(getgid());
172 else
173 (void)setegid(egid);
174
175 kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
176 if (kd == NULL) {
177 error("%s", errbuf);
178 exit(1);
179 }
180
181 /* Get rid of privs for now. */
182 if (nlistf == NULL && memf == NULL)
183 (void)setegid(getgid());
184
185 if (kvm_nlist(kd, namelist)) {
186 if (nlistf)
187 errx(1, "%s: no namelist", nlistf);
188 else
189 errx(1, "no namelist");
190 }
191
192 signal(SIGINT, die);
193 signal(SIGQUIT, die);
194 signal(SIGTERM, die);
195 signal(SIGWINCH, redraw);
196 sv_stop_handler = signal(SIGTSTP, stop);
197
198 /*
199 * Initialize display. Load average appears in a one line
200 * window of its own. Current command's display appears in
201 * an overlapping sub-window of stdscr configured by the display
202 * routines to minimize update work by curses.
203 */
204 if (initscr() == NULL)
205 {
206 warnx("couldn't initialize screen");
207 exit(0);
208 }
209
210 CMDLINE = LINES - 1;
211 wnd = (*curmode->c_open)();
212 if (wnd == NULL) {
213 warnx("couldn't initialize display");
214 die(0);
215 }
216 wload = newwin(1, 0, 3, 20);
217 if (wload == NULL) {
218 warnx("couldn't set up load average window");
219 die(0);
220 }
221 gethostname(hostname, sizeof (hostname));
222 hostname[sizeof(hostname) - 1] = '\0';
223 NREAD(X_HZ, &hz, sizeof hz);
224 NREAD(X_STATHZ, &stathz, sizeof stathz);
225 NREAD(X_MAXSLP, &maxslp, sizeof maxslp);
226 (*curmode->c_init)();
227 curmode->c_flags |= CF_INIT;
228 labels();
229
230 dellave = 0.0;
231
232 signal(SIGALRM, display);
233 display(0);
234 noecho();
235 cbreak();
236 keyboard();
237 /*NOTREACHED*/
238 }
239
240 static void
241 usage(void)
242 {
243 fprintf(stderr, "usage: systat [-n] [-M core] [-N system] [-w wait] "
244 "[-t turns]\n\t\t[display] [refresh-interval]\n");
245 exit(1);
246 }
247
248
249 void
250 labels(void)
251 {
252 if (curmode->c_flags & CF_LOADAV) {
253 mvaddstr(2, 20,
254 "/0 /1 /2 /3 /4 /5 /6 /7 /8 /9 /10");
255 mvaddstr(3, 5, "Load Average");
256 }
257 (*curmode->c_label)();
258 #ifdef notdef
259 mvprintw(21, 25, "CPU usage on %s", hostname);
260 #endif
261 refresh();
262 }
263
264 void
265 display(int signo)
266 {
267 int j;
268 sigset_t set;
269 struct mode *p;
270
271 sigemptyset(&set);
272 sigaddset(&set, SIGALRM);
273 sigprocmask(SIG_BLOCK, &set, NULL);
274
275 /* Get the load average over the last minute. */
276 (void)getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0]));
277 (*curmode->c_fetch)();
278 if (curmode->c_flags & CF_LOADAV) {
279 j = 5.0*avenrun[0] + 0.5;
280 dellave -= avenrun[0];
281 if (dellave >= 0.0)
282 c = '<';
283 else {
284 c = '>';
285 dellave = -dellave;
286 }
287 if (dellave < 0.1)
288 c = '|';
289 dellave = avenrun[0];
290 wmove(wload, 0, 0);
291 wclrtoeol(wload);
292 whline(wload, c, (j > 50) ? 50 : j);
293 if (j > 50)
294 wprintw(wload, " %4.1f", avenrun[0]);
295 }
296 (*curmode->c_refresh)();
297 if (curmode->c_flags & CF_LOADAV)
298 wrefresh(wload);
299 wrefresh(wnd);
300 move(CMDLINE, col);
301 refresh();
302
303 if (allflag && signo==SIGALRM) {
304 if (allcounter >= turns){
305 p = curmode;
306 p++;
307 if (p->c_name == NULL)
308 p = modes;
309 switch_mode(p);
310 allcounter=0;
311 } else
312 allcounter++;
313 }
314
315 sigprocmask(SIG_UNBLOCK, &set, NULL);
316 alarm(naptime);
317 }
318
319 void
320 redraw(int signo)
321 {
322 sigset_t set;
323 struct winsize win;
324
325 sigemptyset(&set);
326 sigaddset(&set, SIGALRM);
327 sigprocmask(SIG_BLOCK, &set, NULL);
328
329 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) != -1 &&
330 (win.ws_row != LINES || win.ws_col != COLS)) {
331 resizeterm(win.ws_row, win.ws_col);
332 CMDLINE = LINES - 1;
333 labels();
334 }
335
336 sigprocmask(SIG_UNBLOCK, &set, NULL);
337 display(0);
338 }
339
340 static void
341 stop(int signo)
342 {
343 sigset_t set;
344
345 signal(SIGTSTP, sv_stop_handler);
346 /* unblock SIGTSTP */
347 sigemptyset(&set);
348 sigaddset(&set, SIGTSTP);
349 sigprocmask(SIG_UNBLOCK, &set, NULL);
350 /* stop ourselves */
351 kill(0, SIGTSTP);
352 /* must have been restarted */
353 signal(SIGTSTP, stop);
354 redraw(signo);
355 }
356
357 void
358 die(int signo)
359 {
360 move(CMDLINE, 0);
361 clrtoeol();
362 refresh();
363 endwin();
364 exit(0);
365 }
366
367 void
368 error(const char *fmt, ...)
369 {
370 va_list ap;
371 char buf[255];
372 int oy, ox;
373
374 va_start(ap, fmt);
375
376 if (wnd) {
377 getyx(stdscr, oy, ox);
378 (void) vsnprintf(buf, sizeof(buf), fmt, ap);
379 clrtoeol();
380 standout();
381 mvaddstr(CMDLINE, 0, buf);
382 standend();
383 move(oy, ox);
384 refresh();
385 } else {
386 (void) vfprintf(stderr, fmt, ap);
387 fprintf(stderr, "\n");
388 }
389 va_end(ap);
390 }
391
392 void
393 nlisterr(struct nlist namelist[])
394 {
395 int i, n;
396
397 n = 0;
398 clear();
399 mvprintw(2, 10, "systat: nlist: can't find following symbols:");
400 for (i = 0;
401 namelist[i].n_name != NULL && *namelist[i].n_name != '\0'; i++)
402 if (namelist[i].n_value == 0)
403 mvprintw(2 + ++n, 10, "%s", namelist[i].n_name);
404 move(CMDLINE, 0);
405 clrtoeol();
406 refresh();
407 sleep(5);
408 endwin();
409 exit(1);
410 }
411