main.c revision 1.36 1 /* $NetBSD: main.c,v 1.36 2004/07/03 18:31:36 mycroft 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.36 2004/07/03 18:31:36 mycroft 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 sv_stop_handler = signal(SIGTSTP, stop);
196
197 /*
198 * Initialize display. Load average appears in a one line
199 * window of its own. Current command's display appears in
200 * an overlapping sub-window of stdscr configured by the display
201 * routines to minimize update work by curses.
202 */
203 if (initscr() == NULL)
204 {
205 warnx("couldn't initialize screen");
206 exit(0);
207 }
208
209 CMDLINE = LINES - 1;
210 wnd = (*curmode->c_open)();
211 if (wnd == NULL) {
212 warnx("couldn't initialize display");
213 die(0);
214 }
215 wload = newwin(1, 0, 3, 20);
216 if (wload == NULL) {
217 warnx("couldn't set up load average window");
218 die(0);
219 }
220 gethostname(hostname, sizeof (hostname));
221 hostname[sizeof(hostname) - 1] = '\0';
222 NREAD(X_HZ, &hz, sizeof hz);
223 NREAD(X_STATHZ, &stathz, sizeof stathz);
224 NREAD(X_MAXSLP, &maxslp, sizeof maxslp);
225 (*curmode->c_init)();
226 curmode->c_flags |= CF_INIT;
227 labels();
228
229 dellave = 0.0;
230
231 signal(SIGALRM, display);
232 display(0);
233 noecho();
234 cbreak();
235 keyboard();
236 /*NOTREACHED*/
237 }
238
239 static void
240 usage(void)
241 {
242 fprintf(stderr, "usage: systat [-n] [-M core] [-N system] [-w wait] "
243 "[-t turns]\n\t\t[display] [refresh-interval]\n");
244 exit(1);
245 }
246
247
248 void
249 labels(void)
250 {
251 if (curmode->c_flags & CF_LOADAV) {
252 mvaddstr(2, 20,
253 "/0 /1 /2 /3 /4 /5 /6 /7 /8 /9 /10");
254 mvaddstr(3, 5, "Load Average");
255 }
256 (*curmode->c_label)();
257 #ifdef notdef
258 mvprintw(21, 25, "CPU usage on %s", hostname);
259 #endif
260 refresh();
261 }
262
263 void
264 display(int signo)
265 {
266 int j;
267 sigset_t set;
268 struct mode *p;
269
270 sigemptyset(&set);
271 sigaddset(&set, SIGALRM);
272 sigprocmask(SIG_BLOCK, &set, NULL);
273
274 /* Get the load average over the last minute. */
275 (void)getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0]));
276 (*curmode->c_fetch)();
277 if (curmode->c_flags & CF_LOADAV) {
278 j = 5.0*avenrun[0] + 0.5;
279 dellave -= avenrun[0];
280 if (dellave >= 0.0)
281 c = '<';
282 else {
283 c = '>';
284 dellave = -dellave;
285 }
286 if (dellave < 0.1)
287 c = '|';
288 dellave = avenrun[0];
289 wmove(wload, 0, 0);
290 wclrtoeol(wload);
291 whline(wload, c, (j > 50) ? 50 : j);
292 if (j > 50)
293 wprintw(wload, " %4.1f", avenrun[0]);
294 }
295 (*curmode->c_refresh)();
296 if (curmode->c_flags & CF_LOADAV)
297 wrefresh(wload);
298 wrefresh(wnd);
299 move(CMDLINE, col);
300 refresh();
301
302 if (allflag && signo==SIGALRM) {
303 if (allcounter >= turns){
304 p = curmode;
305 p++;
306 if (p->c_name == NULL)
307 p = modes;
308 switch_mode(p);
309 allcounter=0;
310 } else
311 allcounter++;
312 }
313
314 sigprocmask(SIG_UNBLOCK, &set, NULL);
315 alarm(naptime);
316 }
317
318 void
319 redraw(int signo)
320 {
321 sigset_t set;
322
323 sigemptyset(&set);
324 sigaddset(&set, SIGALRM);
325 sigprocmask(SIG_BLOCK, &set, NULL);
326
327 resizeterm(LINES, COLS);
328 CMDLINE = LINES - 1;
329 labels();
330
331 sigprocmask(SIG_UNBLOCK, &set, NULL);
332 display(0);
333 }
334
335 static void
336 stop(int signo)
337 {
338 sigset_t set;
339
340 signal(SIGTSTP, sv_stop_handler);
341 /* unblock SIGTSTP */
342 sigemptyset(&set);
343 sigaddset(&set, SIGTSTP);
344 sigprocmask(SIG_UNBLOCK, &set, NULL);
345 /* stop ourselves */
346 kill(0, SIGTSTP);
347 /* must have been restarted */
348 signal(SIGTSTP, stop);
349 redraw(signo);
350 }
351
352 void
353 die(int signo)
354 {
355 move(CMDLINE, 0);
356 clrtoeol();
357 refresh();
358 endwin();
359 exit(0);
360 }
361
362 void
363 error(const char *fmt, ...)
364 {
365 va_list ap;
366 char buf[255];
367 int oy, ox;
368
369 va_start(ap, fmt);
370
371 if (wnd) {
372 getyx(stdscr, oy, ox);
373 (void) vsnprintf(buf, sizeof(buf), fmt, ap);
374 clrtoeol();
375 standout();
376 mvaddstr(CMDLINE, 0, buf);
377 standend();
378 move(oy, ox);
379 refresh();
380 } else {
381 (void) vfprintf(stderr, fmt, ap);
382 fprintf(stderr, "\n");
383 }
384 va_end(ap);
385 }
386
387 void
388 nlisterr(struct nlist namelist[])
389 {
390 int i, n;
391
392 n = 0;
393 clear();
394 mvprintw(2, 10, "systat: nlist: can't find following symbols:");
395 for (i = 0;
396 namelist[i].n_name != NULL && *namelist[i].n_name != '\0'; i++)
397 if (namelist[i].n_value == 0)
398 mvprintw(2 + ++n, 10, "%s", namelist[i].n_name);
399 move(CMDLINE, 0);
400 clrtoeol();
401 refresh();
402 sleep(5);
403 endwin();
404 exit(1);
405 }
406