main.c revision 1.41 1 /* $NetBSD: main.c,v 1.41 2007/12/31 00:22:15 christos 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.41 2007/12/31 00:22:15 christos 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 { .n_name = "_hz" },
63 #define X_STATHZ 1
64 { .n_name = "_stathz" },
65 #define X_MAXSLP 2
66 { .n_name = "_maxslp" },
67 { .n_name = NULL }
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 sig_atomic_t needsredraw = 0;
89
90 static WINDOW *wload; /* one line window for load average */
91
92 static void (*sv_stop_handler)(int);
93
94 static void stop(int);
95 static void usage(void);
96 int main(int, char **);
97
98 gid_t egid; /* XXX needed by initiostat() and initkre() */
99
100 int
101 main(int argc, char **argv)
102 {
103 int ch;
104 char errbuf[_POSIX2_LINE_MAX];
105
106 egid = getegid();
107 (void)setegid(getgid());
108
109 while ((ch = getopt(argc, argv, "M:N:nw:t:")) != -1)
110 switch(ch) {
111 case 'M':
112 memf = optarg;
113 break;
114 case 'N':
115 nlistf = optarg;
116 break;
117 case 'n':
118 nflag = !nflag;
119 break;
120 case 'w':
121 if ((naptime = atoi(optarg)) <= 0)
122 errx(1, "interval <= 0.");
123 break;
124 case 't':
125 if ((turns = atoi(optarg)) <= 0)
126 errx(1, "turns <= 0.");
127 break;
128 case '?':
129 default:
130 usage();
131 }
132 argc -= optind;
133 argv += optind;
134
135
136 for ( ; argc > 0; argc--, argv++) {
137 struct mode *p;
138 int modefound = 0;
139
140 if (isdigit((unsigned char)argv[0][0])) {
141 naptime = atoi(argv[0]);
142 if (naptime <= 0)
143 naptime = 5;
144 continue;
145 }
146
147 for (p = modes; p->c_name ; p++) {
148 if (strstr(p->c_name, argv[0]) == p->c_name) {
149 curmode = p;
150 modefound++;
151 break;
152 }
153
154 if(strstr("all",argv[0]) == "all"){
155 allcounter=0;
156 allflag=1;
157 }
158 }
159
160
161 if (!modefound && !allflag)
162 error("%s: Unknown command.", argv[0]);
163 }
164
165 /*
166 * Discard setgid privileges. If not the running kernel, we toss
167 * them away totally so that bad guys can't print interesting stuff
168 * from kernel memory, otherwise switch back to kmem for the
169 * duration of the kvm_openfiles() call.
170 */
171 if (nlistf != NULL || memf != NULL)
172 (void)setgid(getgid());
173 else
174 (void)setegid(egid);
175
176 kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
177 if (kd == NULL) {
178 error("%s", errbuf);
179 exit(1);
180 }
181
182 /* Get rid of privs for now. */
183 if (nlistf == NULL && memf == NULL)
184 (void)setegid(getgid());
185
186 if (kvm_nlist(kd, namelist)) {
187 if (nlistf)
188 errx(1, "%s: no namelist", nlistf);
189 else
190 errx(1, "no namelist");
191 }
192
193 signal(SIGINT, die);
194 signal(SIGQUIT, die);
195 signal(SIGTERM, die);
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 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 struct mode *p;
268
269 /* Get the load average over the last minute. */
270 (void)getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0]));
271 (*curmode->c_fetch)();
272 if (curmode->c_flags & CF_LOADAV) {
273 j = 5.0*avenrun[0] + 0.5;
274 dellave -= avenrun[0];
275 if (dellave >= 0.0)
276 c = '<';
277 else {
278 c = '>';
279 dellave = -dellave;
280 }
281 if (dellave < 0.1)
282 c = '|';
283 dellave = avenrun[0];
284 wmove(wload, 0, 0);
285 wclrtoeol(wload);
286 whline(wload, c, (j > 50) ? 50 : j);
287 if (j > 50)
288 wprintw(wload, " %4.1f", avenrun[0]);
289 }
290 (*curmode->c_refresh)();
291 if (curmode->c_flags & CF_LOADAV)
292 wrefresh(wload);
293 wrefresh(wnd);
294 move(CMDLINE, col);
295 refresh();
296
297 if (allflag && signo==SIGALRM) {
298 if (allcounter >= turns){
299 p = curmode;
300 p++;
301 if (p->c_name == NULL)
302 p = modes;
303 switch_mode(p);
304 allcounter=0;
305 } else
306 allcounter++;
307 }
308
309 timeout(naptime * 1000);
310 }
311
312 void
313 redraw(void)
314 {
315 resizeterm(LINES, COLS);
316 CMDLINE = LINES - 1;
317 labels();
318
319 display(0);
320 needsredraw = 0;
321 }
322
323 static void
324 stop(int signo)
325 {
326 sigset_t set;
327
328 signal(SIGTSTP, sv_stop_handler);
329 /* unblock SIGTSTP */
330 sigemptyset(&set);
331 sigaddset(&set, SIGTSTP);
332 sigprocmask(SIG_UNBLOCK, &set, NULL);
333 /* stop ourselves */
334 kill(0, SIGTSTP);
335 /* must have been restarted */
336 signal(SIGTSTP, stop);
337 needsredraw = signo;
338 }
339
340 void
341 die(int signo)
342 {
343 move(CMDLINE, 0);
344 clrtoeol();
345 refresh();
346 endwin();
347 exit(0);
348 }
349
350 void
351 error(const char *fmt, ...)
352 {
353 va_list ap;
354 char buf[255];
355 int oy, ox;
356
357 va_start(ap, fmt);
358
359 if (wnd) {
360 getyx(stdscr, oy, ox);
361 (void) vsnprintf(buf, sizeof(buf), fmt, ap);
362 clrtoeol();
363 standout();
364 mvaddstr(CMDLINE, 0, buf);
365 standend();
366 move(oy, ox);
367 refresh();
368 } else {
369 (void) vfprintf(stderr, fmt, ap);
370 fprintf(stderr, "\n");
371 }
372 va_end(ap);
373 }
374
375 void
376 nlisterr(struct nlist name_list[])
377 {
378 int i, n;
379
380 n = 0;
381 clear();
382 mvprintw(2, 10, "systat: nlist: can't find following symbols:");
383 for (i = 0;
384 name_list[i].n_name != NULL && *name_list[i].n_name != '\0'; i++)
385 if (name_list[i].n_value == 0)
386 mvprintw(2 + ++n, 10, "%s", name_list[i].n_name);
387 move(CMDLINE, 0);
388 clrtoeol();
389 refresh();
390 sleep(5);
391 endwin();
392 exit(1);
393 }
394