main.c revision 1.19 1 1.19 jwise /* $NetBSD: main.c,v 1.19 1999/12/16 17:15:48 jwise Exp $ */
2 1.2 jtc
3 1.1 jtc /*-
4 1.1 jtc * Copyright (c) 1980, 1992, 1993
5 1.1 jtc * The Regents of the University of California. All rights reserved.
6 1.1 jtc *
7 1.1 jtc * Redistribution and use in source and binary forms, with or without
8 1.1 jtc * modification, are permitted provided that the following conditions
9 1.1 jtc * are met:
10 1.1 jtc * 1. Redistributions of source code must retain the above copyright
11 1.1 jtc * notice, this list of conditions and the following disclaimer.
12 1.1 jtc * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jtc * notice, this list of conditions and the following disclaimer in the
14 1.1 jtc * documentation and/or other materials provided with the distribution.
15 1.1 jtc * 3. All advertising materials mentioning features or use of this software
16 1.1 jtc * must display the following acknowledgement:
17 1.1 jtc * This product includes software developed by the University of
18 1.1 jtc * California, Berkeley and its contributors.
19 1.1 jtc * 4. Neither the name of the University nor the names of its contributors
20 1.1 jtc * may be used to endorse or promote products derived from this software
21 1.1 jtc * without specific prior written permission.
22 1.1 jtc *
23 1.1 jtc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 jtc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 jtc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 jtc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 jtc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 jtc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 jtc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 jtc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 jtc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 jtc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 jtc * SUCH DAMAGE.
34 1.1 jtc */
35 1.1 jtc
36 1.11 mrg #include <sys/cdefs.h>
37 1.1 jtc #ifndef lint
38 1.11 mrg __COPYRIGHT("@(#) Copyright (c) 1980, 1992, 1993\n\
39 1.11 mrg The Regents of the University of California. All rights reserved.\n");
40 1.2 jtc #if 0
41 1.1 jtc static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/6/93";
42 1.2 jtc #endif
43 1.19 jwise __RCSID("$NetBSD: main.c,v 1.19 1999/12/16 17:15:48 jwise Exp $");
44 1.1 jtc #endif /* not lint */
45 1.1 jtc
46 1.1 jtc #include <sys/param.h>
47 1.1 jtc
48 1.4 mycroft #include <err.h>
49 1.10 explorer #include <limits.h>
50 1.1 jtc #include <nlist.h>
51 1.1 jtc #include <signal.h>
52 1.1 jtc #include <stdio.h>
53 1.5 cgd #include <string.h>
54 1.8 thorpej #include <unistd.h>
55 1.11 mrg #include <stdlib.h>
56 1.11 mrg #include <ctype.h>
57 1.4 mycroft
58 1.1 jtc #include "systat.h"
59 1.1 jtc #include "extern.h"
60 1.1 jtc
61 1.1 jtc static struct nlist namelist[] = {
62 1.1 jtc #define X_FIRST 0
63 1.1 jtc #define X_HZ 0
64 1.1 jtc { "_hz" },
65 1.1 jtc #define X_STATHZ 1
66 1.1 jtc { "_stathz" },
67 1.1 jtc { "" }
68 1.1 jtc };
69 1.1 jtc static int dellave;
70 1.1 jtc
71 1.1 jtc kvm_t *kd;
72 1.8 thorpej char *memf = NULL;
73 1.8 thorpej char *nlistf = NULL;
74 1.1 jtc sig_t sigtstpdfl;
75 1.1 jtc double avenrun[3];
76 1.1 jtc int col;
77 1.1 jtc int naptime = 5;
78 1.1 jtc int verbose = 1; /* to report kvm read errs */
79 1.1 jtc int hz, stathz;
80 1.1 jtc char c;
81 1.1 jtc char *namp;
82 1.13 mrg char hostname[MAXHOSTNAMELEN + 1];
83 1.1 jtc WINDOW *wnd;
84 1.1 jtc int CMDLINE;
85 1.1 jtc
86 1.1 jtc static WINDOW *wload; /* one line window for load average */
87 1.1 jtc
88 1.11 mrg static void usage __P((void));
89 1.11 mrg int main __P((int, char **));
90 1.8 thorpej
91 1.15 drochner gid_t egid; /* XXX needed by initiostat() and initkre() */
92 1.15 drochner
93 1.7 jtc int
94 1.1 jtc main(argc, argv)
95 1.1 jtc int argc;
96 1.1 jtc char **argv;
97 1.1 jtc {
98 1.8 thorpej int ch;
99 1.10 explorer char errbuf[_POSIX2_LINE_MAX];
100 1.1 jtc
101 1.15 drochner egid = getegid();
102 1.13 mrg (void)setegid(getgid());
103 1.12 lukem while ((ch = getopt(argc, argv, "M:N:w:")) != -1)
104 1.8 thorpej switch(ch) {
105 1.9 scottr case 'M':
106 1.9 scottr memf = optarg;
107 1.9 scottr break;
108 1.9 scottr case 'N':
109 1.9 scottr nlistf = optarg;
110 1.9 scottr break;
111 1.9 scottr case 'w':
112 1.9 scottr if ((naptime = atoi(optarg)) <= 0)
113 1.9 scottr errx(1, "interval <= 0.");
114 1.9 scottr break;
115 1.9 scottr case '?':
116 1.9 scottr default:
117 1.9 scottr usage();
118 1.9 scottr }
119 1.9 scottr argc -= optind;
120 1.9 scottr argv += optind;
121 1.8 thorpej
122 1.19 jwise curmode = (struct mode *)NULL;
123 1.19 jwise
124 1.19 jwise for ( ; argc > 0; argc--, argv++) {
125 1.19 jwise struct mode *p;
126 1.19 jwise
127 1.8 thorpej if (isdigit(argv[0][0])) {
128 1.8 thorpej naptime = atoi(argv[0]);
129 1.8 thorpej if (naptime <= 0)
130 1.8 thorpej naptime = 5;
131 1.19 jwise continue;
132 1.19 jwise }
133 1.1 jtc
134 1.19 jwise for (p = modes; p->c_name ; p++) {
135 1.19 jwise if (strcmp(argv[0], p->c_name) == 0) {
136 1.19 jwise curmode = p;
137 1.19 jwise break;
138 1.19 jwise }
139 1.1 jtc }
140 1.19 jwise
141 1.19 jwise if (!curmode)
142 1.19 jwise error("%s: Unknown command.", argv[0]);
143 1.1 jtc }
144 1.13 mrg
145 1.15 drochner /*
146 1.15 drochner * Discard setgid privileges. If not the running kernel, we toss
147 1.15 drochner * them away totally so that bad guys can't print interesting stuff
148 1.15 drochner * from kernel memory, otherwise switch back to kmem for the
149 1.15 drochner * duration of the kvm_openfiles() call.
150 1.15 drochner */
151 1.15 drochner if (nlistf != NULL || memf != NULL)
152 1.15 drochner (void)setgid(getgid());
153 1.15 drochner else
154 1.15 drochner (void)setegid(egid);
155 1.15 drochner
156 1.8 thorpej kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
157 1.1 jtc if (kd == NULL) {
158 1.1 jtc error("%s", errbuf);
159 1.1 jtc exit(1);
160 1.1 jtc }
161 1.15 drochner
162 1.15 drochner /* Get rid of privs for now. */
163 1.13 mrg if (nlistf == NULL && memf == NULL)
164 1.15 drochner (void)setegid(getgid());
165 1.15 drochner
166 1.1 jtc if (kvm_nlist(kd, namelist)) {
167 1.1 jtc nlisterr(namelist);
168 1.1 jtc exit(1);
169 1.1 jtc }
170 1.4 mycroft if (namelist[X_FIRST].n_type == 0)
171 1.4 mycroft errx(1, "couldn't read namelist");
172 1.1 jtc signal(SIGINT, die);
173 1.1 jtc signal(SIGQUIT, die);
174 1.1 jtc signal(SIGTERM, die);
175 1.9 scottr signal(SIGWINCH, redraw);
176 1.1 jtc
177 1.1 jtc /*
178 1.1 jtc * Initialize display. Load average appears in a one line
179 1.1 jtc * window of its own. Current command's display appears in
180 1.1 jtc * an overlapping sub-window of stdscr configured by the display
181 1.1 jtc * routines to minimize update work by curses.
182 1.1 jtc */
183 1.6 jtc if (initscr() == NULL)
184 1.6 jtc {
185 1.6 jtc warnx("couldn't initialize screen");
186 1.6 jtc exit(0);
187 1.6 jtc }
188 1.6 jtc
189 1.1 jtc CMDLINE = LINES - 1;
190 1.18 jwise wnd = (*curmode->c_open)();
191 1.1 jtc if (wnd == NULL) {
192 1.4 mycroft warnx("couldn't initialize display");
193 1.1 jtc die(0);
194 1.1 jtc }
195 1.1 jtc wload = newwin(1, 0, 3, 20);
196 1.1 jtc if (wload == NULL) {
197 1.4 mycroft warnx("couldn't set up load average window");
198 1.1 jtc die(0);
199 1.1 jtc }
200 1.1 jtc gethostname(hostname, sizeof (hostname));
201 1.13 mrg hostname[sizeof(hostname) - 1] = '\0';
202 1.16 ross NREAD(X_HZ, &hz, sizeof hz);
203 1.16 ross NREAD(X_STATHZ, &stathz, sizeof stathz);
204 1.18 jwise (*curmode->c_init)();
205 1.18 jwise curmode->c_flags |= CF_INIT;
206 1.1 jtc labels();
207 1.1 jtc
208 1.1 jtc dellave = 0.0;
209 1.1 jtc
210 1.1 jtc signal(SIGALRM, display);
211 1.1 jtc display(0);
212 1.1 jtc noecho();
213 1.1 jtc crmode();
214 1.1 jtc keyboard();
215 1.1 jtc /*NOTREACHED*/
216 1.1 jtc }
217 1.8 thorpej
218 1.8 thorpej static void
219 1.8 thorpej usage()
220 1.8 thorpej {
221 1.17 soren fprintf(stderr, "usage: systat [-M core] [-N system] [-w wait] "
222 1.17 soren "[display] [refresh-interval]\n");
223 1.8 thorpej exit(1);
224 1.8 thorpej }
225 1.8 thorpej
226 1.1 jtc
227 1.1 jtc void
228 1.1 jtc labels()
229 1.1 jtc {
230 1.18 jwise if (curmode->c_flags & CF_LOADAV) {
231 1.1 jtc mvaddstr(2, 20,
232 1.1 jtc "/0 /1 /2 /3 /4 /5 /6 /7 /8 /9 /10");
233 1.1 jtc mvaddstr(3, 5, "Load Average");
234 1.1 jtc }
235 1.18 jwise (*curmode->c_label)();
236 1.1 jtc #ifdef notdef
237 1.1 jtc mvprintw(21, 25, "CPU usage on %s", hostname);
238 1.1 jtc #endif
239 1.1 jtc refresh();
240 1.1 jtc }
241 1.1 jtc
242 1.1 jtc void
243 1.1 jtc display(signo)
244 1.1 jtc int signo;
245 1.1 jtc {
246 1.12 lukem int i, j;
247 1.1 jtc
248 1.1 jtc /* Get the load average over the last minute. */
249 1.14 mrg (void)getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0]));
250 1.18 jwise (*curmode->c_fetch)();
251 1.18 jwise if (curmode->c_flags & CF_LOADAV) {
252 1.1 jtc j = 5.0*avenrun[0] + 0.5;
253 1.1 jtc dellave -= avenrun[0];
254 1.1 jtc if (dellave >= 0.0)
255 1.1 jtc c = '<';
256 1.1 jtc else {
257 1.1 jtc c = '>';
258 1.1 jtc dellave = -dellave;
259 1.1 jtc }
260 1.1 jtc if (dellave < 0.1)
261 1.1 jtc c = '|';
262 1.1 jtc dellave = avenrun[0];
263 1.1 jtc wmove(wload, 0, 0); wclrtoeol(wload);
264 1.1 jtc for (i = (j > 50) ? 50 : j; i > 0; i--)
265 1.1 jtc waddch(wload, c);
266 1.1 jtc if (j > 50)
267 1.1 jtc wprintw(wload, " %4.1f", avenrun[0]);
268 1.1 jtc }
269 1.18 jwise (*curmode->c_refresh)();
270 1.18 jwise if (curmode->c_flags & CF_LOADAV)
271 1.1 jtc wrefresh(wload);
272 1.1 jtc wrefresh(wnd);
273 1.1 jtc move(CMDLINE, col);
274 1.1 jtc refresh();
275 1.1 jtc alarm(naptime);
276 1.9 scottr }
277 1.9 scottr
278 1.9 scottr void
279 1.9 scottr redraw(signo)
280 1.9 scottr int signo;
281 1.9 scottr {
282 1.9 scottr sigset_t set;
283 1.9 scottr
284 1.9 scottr sigemptyset(&set);
285 1.9 scottr sigaddset(&set, SIGALRM);
286 1.9 scottr sigprocmask(SIG_BLOCK, &set, NULL);
287 1.9 scottr wrefresh(curscr);
288 1.9 scottr refresh();
289 1.9 scottr sigprocmask(SIG_UNBLOCK, &set, NULL);
290 1.1 jtc }
291 1.1 jtc
292 1.1 jtc void
293 1.1 jtc die(signo)
294 1.1 jtc int signo;
295 1.1 jtc {
296 1.1 jtc move(CMDLINE, 0);
297 1.1 jtc clrtoeol();
298 1.1 jtc refresh();
299 1.1 jtc endwin();
300 1.1 jtc exit(0);
301 1.1 jtc }
302 1.1 jtc
303 1.1 jtc #if __STDC__
304 1.1 jtc #include <stdarg.h>
305 1.1 jtc #else
306 1.1 jtc #include <varargs.h>
307 1.1 jtc #endif
308 1.1 jtc
309 1.1 jtc #if __STDC__
310 1.1 jtc void
311 1.1 jtc error(const char *fmt, ...)
312 1.1 jtc #else
313 1.1 jtc void
314 1.1 jtc error(fmt, va_alist)
315 1.1 jtc char *fmt;
316 1.1 jtc va_dcl
317 1.1 jtc #endif
318 1.1 jtc {
319 1.1 jtc va_list ap;
320 1.1 jtc char buf[255];
321 1.1 jtc int oy, ox;
322 1.1 jtc #if __STDC__
323 1.1 jtc va_start(ap, fmt);
324 1.1 jtc #else
325 1.1 jtc va_start(ap);
326 1.1 jtc #endif
327 1.1 jtc
328 1.1 jtc if (wnd) {
329 1.1 jtc getyx(stdscr, oy, ox);
330 1.1 jtc (void) vsprintf(buf, fmt, ap);
331 1.1 jtc clrtoeol();
332 1.1 jtc standout();
333 1.1 jtc mvaddstr(CMDLINE, 0, buf);
334 1.1 jtc standend();
335 1.1 jtc move(oy, ox);
336 1.1 jtc refresh();
337 1.1 jtc } else {
338 1.1 jtc (void) vfprintf(stderr, fmt, ap);
339 1.1 jtc fprintf(stderr, "\n");
340 1.1 jtc }
341 1.1 jtc va_end(ap);
342 1.1 jtc }
343 1.1 jtc
344 1.1 jtc void
345 1.1 jtc nlisterr(namelist)
346 1.1 jtc struct nlist namelist[];
347 1.1 jtc {
348 1.1 jtc int i, n;
349 1.1 jtc
350 1.1 jtc n = 0;
351 1.1 jtc clear();
352 1.1 jtc mvprintw(2, 10, "systat: nlist: can't find following symbols:");
353 1.1 jtc for (i = 0;
354 1.1 jtc namelist[i].n_name != NULL && *namelist[i].n_name != '\0'; i++)
355 1.1 jtc if (namelist[i].n_value == 0)
356 1.1 jtc mvprintw(2 + ++n, 10, "%s", namelist[i].n_name);
357 1.1 jtc move(CMDLINE, 0);
358 1.1 jtc clrtoeol();
359 1.1 jtc refresh();
360 1.1 jtc endwin();
361 1.1 jtc exit(1);
362 1.1 jtc }
363