main.c revision 1.24 1 /* $NetBSD: main.c,v 1.24 2000/04/11 01:01:26 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.24 2000/04/11 01:01:26 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:nw:")) != -1)
105 switch(ch) {
106 case 'M':
107 memf = optarg;
108 break;
109 case 'N':
110 nlistf = optarg;
111 break;
112 case 'n':
113 nflag = !nflag;
114 break;
115 case 'w':
116 if ((naptime = atoi(optarg)) <= 0)
117 errx(1, "interval <= 0.");
118 break;
119 case '?':
120 default:
121 usage();
122 }
123 argc -= optind;
124 argv += optind;
125
126
127 for ( ; argc > 0; argc--, argv++) {
128 struct mode *p;
129 int modefound = 0;
130
131 if (isdigit(argv[0][0])) {
132 naptime = atoi(argv[0]);
133 if (naptime <= 0)
134 naptime = 5;
135 continue;
136 }
137
138 for (p = modes; p->c_name ; p++) {
139 if (strstr(p->c_name, argv[0]) == p->c_name) {
140 curmode = p;
141 modefound++;
142 break;
143 }
144 }
145
146 if (!modefound)
147 error("%s: Unknown command.", argv[0]);
148 }
149
150 /*
151 * Discard setgid privileges. If not the running kernel, we toss
152 * them away totally so that bad guys can't print interesting stuff
153 * from kernel memory, otherwise switch back to kmem for the
154 * duration of the kvm_openfiles() call.
155 */
156 if (nlistf != NULL || memf != NULL)
157 (void)setgid(getgid());
158 else
159 (void)setegid(egid);
160
161 kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
162 if (kd == NULL) {
163 error("%s", errbuf);
164 exit(1);
165 }
166
167 /* Get rid of privs for now. */
168 if (nlistf == NULL && memf == NULL)
169 (void)setegid(getgid());
170
171 if (kvm_nlist(kd, namelist)) {
172 nlisterr(namelist);
173 exit(1);
174 }
175 if (namelist[X_FIRST].n_type == 0)
176 errx(1, "couldn't read namelist");
177 signal(SIGINT, die);
178 signal(SIGQUIT, die);
179 signal(SIGTERM, die);
180 signal(SIGWINCH, redraw);
181
182 /*
183 * Initialize display. Load average appears in a one line
184 * window of its own. Current command's display appears in
185 * an overlapping sub-window of stdscr configured by the display
186 * routines to minimize update work by curses.
187 */
188 if (initscr() == NULL)
189 {
190 warnx("couldn't initialize screen");
191 exit(0);
192 }
193
194 CMDLINE = LINES - 1;
195 wnd = (*curmode->c_open)();
196 if (wnd == NULL) {
197 warnx("couldn't initialize display");
198 die(0);
199 }
200 wload = newwin(1, 0, 3, 20);
201 if (wload == NULL) {
202 warnx("couldn't set up load average window");
203 die(0);
204 }
205 gethostname(hostname, sizeof (hostname));
206 hostname[sizeof(hostname) - 1] = '\0';
207 NREAD(X_HZ, &hz, sizeof hz);
208 NREAD(X_STATHZ, &stathz, sizeof stathz);
209 (*curmode->c_init)();
210 curmode->c_flags |= CF_INIT;
211 labels();
212
213 dellave = 0.0;
214
215 signal(SIGALRM, display);
216 display(0);
217 noecho();
218 crmode();
219 keyboard();
220 /*NOTREACHED*/
221 }
222
223 static void
224 usage()
225 {
226 fprintf(stderr, "usage: systat [-n] [-M core] [-N system] [-w wait] "
227 "[display] [refresh-interval]\n");
228 exit(1);
229 }
230
231
232 void
233 labels()
234 {
235 if (curmode->c_flags & CF_LOADAV) {
236 mvaddstr(2, 20,
237 "/0 /1 /2 /3 /4 /5 /6 /7 /8 /9 /10");
238 mvaddstr(3, 5, "Load Average");
239 }
240 (*curmode->c_label)();
241 #ifdef notdef
242 mvprintw(21, 25, "CPU usage on %s", hostname);
243 #endif
244 refresh();
245 }
246
247 void
248 display(signo)
249 int signo;
250 {
251 int i, j;
252 sigset_t set;
253
254 sigemptyset(&set);
255 sigaddset(&set, SIGALRM);
256 sigprocmask(SIG_BLOCK, &set, NULL);
257
258 /* Get the load average over the last minute. */
259 (void)getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0]));
260 (*curmode->c_fetch)();
261 if (curmode->c_flags & CF_LOADAV) {
262 j = 5.0*avenrun[0] + 0.5;
263 dellave -= avenrun[0];
264 if (dellave >= 0.0)
265 c = '<';
266 else {
267 c = '>';
268 dellave = -dellave;
269 }
270 if (dellave < 0.1)
271 c = '|';
272 dellave = avenrun[0];
273 wmove(wload, 0, 0); wclrtoeol(wload);
274 for (i = (j > 50) ? 50 : j; i > 0; i--)
275 waddch(wload, c);
276 if (j > 50)
277 wprintw(wload, " %4.1f", avenrun[0]);
278 }
279 (*curmode->c_refresh)();
280 if (curmode->c_flags & CF_LOADAV)
281 wrefresh(wload);
282 wrefresh(wnd);
283 move(CMDLINE, col);
284 refresh();
285 sigprocmask(SIG_UNBLOCK, &set, NULL);
286 alarm(naptime);
287 }
288
289 void
290 redraw(signo)
291 int signo;
292 {
293 sigset_t set;
294
295 sigemptyset(&set);
296 sigaddset(&set, SIGALRM);
297 sigprocmask(SIG_BLOCK, &set, NULL);
298 wrefresh(curscr);
299 refresh();
300 sigprocmask(SIG_UNBLOCK, &set, NULL);
301 }
302
303 void
304 die(signo)
305 int signo;
306 {
307 move(CMDLINE, 0);
308 clrtoeol();
309 refresh();
310 endwin();
311 exit(0);
312 }
313
314 #if __STDC__
315 #include <stdarg.h>
316 #else
317 #include <varargs.h>
318 #endif
319
320 #if __STDC__
321 void
322 error(const char *fmt, ...)
323 #else
324 void
325 error(fmt, va_alist)
326 char *fmt;
327 va_dcl
328 #endif
329 {
330 va_list ap;
331 char buf[255];
332 int oy, ox;
333 #if __STDC__
334 va_start(ap, fmt);
335 #else
336 va_start(ap);
337 #endif
338
339 if (wnd) {
340 getyx(stdscr, oy, ox);
341 (void) vsnprintf(buf, sizeof(buf), fmt, ap);
342 clrtoeol();
343 standout();
344 mvaddstr(CMDLINE, 0, buf);
345 standend();
346 move(oy, ox);
347 refresh();
348 } else {
349 (void) vfprintf(stderr, fmt, ap);
350 fprintf(stderr, "\n");
351 }
352 va_end(ap);
353 }
354
355 void
356 nlisterr(namelist)
357 struct nlist namelist[];
358 {
359 int i, n;
360
361 n = 0;
362 clear();
363 mvprintw(2, 10, "systat: nlist: can't find following symbols:");
364 for (i = 0;
365 namelist[i].n_name != NULL && *namelist[i].n_name != '\0'; i++)
366 if (namelist[i].n_value == 0)
367 mvprintw(2 + ++n, 10, "%s", namelist[i].n_name);
368 move(CMDLINE, 0);
369 clrtoeol();
370 refresh();
371 endwin();
372 exit(1);
373 }
374