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