main.c revision 1.54 1 /* $NetBSD: main.c,v 1.54 2018/12/26 01:47:37 sevan 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\
35 The Regents of the University of California. All rights reserved.");
36 #if 0
37 static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/6/93";
38 #endif
39 __RCSID("$NetBSD: main.c,v 1.54 2018/12/26 01:47:37 sevan Exp $");
40 #endif /* not lint */
41
42 #include <sys/param.h>
43 #include <sys/sysctl.h>
44 #include <sys/ioctl.h>
45
46 #include <ctype.h>
47 #include <err.h>
48 #include <errno.h>
49 #include <limits.h>
50 #include <signal.h>
51 #include <stdarg.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <unistd.h>
56 #include <termios.h>
57
58 #include "systat.h"
59 #include "extern.h"
60
61 static int dellave;
62
63 kvm_t *kd;
64 char *memf = NULL;
65 char *nlistf = NULL;
66 sig_t sigtstpdfl;
67 double avenrun[3];
68 int col;
69 double naptime = 1;
70 int verbose = 1; /* to report kvm read errs */
71 int hz, stathz, maxslp;
72 char c;
73 char *namp;
74 char hostname[MAXHOSTNAMELEN + 1];
75 WINDOW *wnd;
76 int CMDLINE;
77 int turns = 2; /* stay how many refresh-turns in 'all' mode? */
78 int allflag;
79 int allcounter;
80 sig_atomic_t needsredraw = 0;
81
82 static WINDOW *wload; /* one line window for load average */
83
84 static void (*sv_stop_handler)(int);
85
86 static void stop(int);
87 __dead static void usage(void);
88
89 gid_t egid; /* XXX needed by initiostat() and initkre() */
90
91 int
92 main(int argc, char **argv)
93 {
94 int ch;
95 char errbuf[_POSIX2_LINE_MAX];
96 const char *all;
97 struct clockinfo clk;
98 size_t len;
99 int bflag = 0;
100
101 all = "all";
102 egid = getegid();
103 (void)setegid(getgid());
104
105 while ((ch = getopt(argc, argv, "M:N:bnw:t:")) != -1)
106 switch(ch) {
107 case 'M':
108 memf = optarg;
109 break;
110 case 'N':
111 nlistf = optarg;
112 break;
113 case 'b':
114 bflag = !bflag;
115 break;
116 case 'n':
117 nflag = !nflag;
118 break;
119 case 't':
120 if ((turns = atoi(optarg)) <= 0)
121 errx(1, "turns <= 0.");
122 break;
123 case 'w':
124 if ((naptime = strtod(optarg, NULL)) <= 0)
125 errx(1, "interval <= 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((unsigned char)argv[0][0])) {
140 naptime = strtod(argv[0], NULL);
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 errx(1, "%s", errbuf);
178
179 /* Get rid of privs for now. */
180 if (nlistf == NULL && memf == NULL)
181 (void)setegid(getgid());
182
183 signal(SIGINT, die);
184 signal(SIGQUIT, die);
185 signal(SIGTERM, die);
186 sv_stop_handler = signal(SIGTSTP, stop);
187
188 /*
189 * Initialize display. Load average appears in a one line
190 * window of its own. Current command's display appears in
191 * an overlapping sub-window of stdscr configured by the display
192 * routines to minimize update work by curses.
193 */
194 if (initscr() == NULL)
195 errx(1, "couldn't initialize screen");
196
197 CMDLINE = LINES - 1;
198 wnd = (*curmode->c_open)();
199 if (wnd == NULL) {
200 move(CMDLINE, 0);
201 clrtoeol();
202 refresh();
203 endwin();
204 warnx("couldn't initialize display");
205 die(0);
206 }
207 wload = newwin(1, 0, 3, 20);
208 if (wload == NULL) {
209 move(CMDLINE, 0);
210 clrtoeol();
211 refresh();
212 endwin();
213 warnx("couldn't set up load average window");
214 die(0);
215 }
216 gethostname(hostname, sizeof (hostname));
217 hostname[sizeof(hostname) - 1] = '\0';
218
219 len = sizeof(clk);
220 if (sysctlbyname("kern.clockrate", &clk, &len, NULL, 0))
221 error("can't get \"kern.clockrate\": %s", strerror(errno));
222 hz = clk.hz;
223 stathz = clk.stathz;
224
225 len = sizeof(maxslp);
226 if (sysctlbyname("vm.maxslp", &maxslp, &len, NULL, 0))
227 error("can't get \"vm.maxslp\": %s", strerror(errno));
228
229 (*curmode->c_init)();
230 curmode->c_flags |= CF_INIT;
231 labels();
232
233 dellave = 0.0;
234
235 display(0);
236 if (!bflag) {
237 noecho();
238 cbreak();
239 keyboard();
240 } else
241 die(0);
242 /*NOTREACHED*/
243 }
244
245 static void
246 usage(void)
247 {
248 fprintf(stderr, "usage: systat [-bn] [-M core] [-N system] [-w wait] "
249 "[-t turns]\n\t\t[display] [refresh-interval]\n");
250 exit(1);
251 }
252
253
254 void
255 labels(void)
256 {
257 if (curmode->c_flags & CF_LOADAV) {
258 mvaddstr(2, 20,
259 "/0 /1 /2 /3 /4 /5 /6 /7 /8 /9 /10");
260 mvaddstr(3, 5, "Load Average");
261 }
262 (*curmode->c_label)();
263 #ifdef notdef
264 mvprintw(21, 25, "CPU usage on %s", hostname);
265 #endif
266 refresh();
267 }
268
269 void
270 display(int signo)
271 {
272 static int skip;
273 int j;
274 struct mode *p;
275 int ms_delay;
276
277 if (signo == SIGALRM && skip-- > 0)
278 /* Don't display on this timeout */
279 return;
280
281 /* Get the load average over the last minute. */
282 (void)getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0]));
283 (*curmode->c_fetch)();
284 if (curmode->c_flags & CF_LOADAV) {
285 j = 5.0*avenrun[0] + 0.5;
286 dellave -= avenrun[0];
287 if (dellave >= 0.0)
288 c = '<';
289 else {
290 c = '>';
291 dellave = -dellave;
292 }
293 if (dellave < 0.1)
294 c = '|';
295 dellave = avenrun[0];
296 wmove(wload, 0, 0);
297 wclrtoeol(wload);
298 whline(wload, c, (j > 50) ? 50 : j);
299 if (j > 50)
300 wprintw(wload, " %4.1f", avenrun[0]);
301 }
302 (*curmode->c_refresh)();
303 if (curmode->c_flags & CF_LOADAV)
304 wrefresh(wload);
305 wrefresh(wnd);
306 move(CMDLINE, col);
307 refresh();
308
309 if (allflag && signo==SIGALRM) {
310 if (allcounter >= turns){
311 p = curmode;
312 p++;
313 if (p->c_name == NULL)
314 p = modes;
315 switch_mode(p);
316 allcounter=0;
317 } else
318 allcounter++;
319 }
320
321 /* curses timeout() uses VTIME, limited to 255 1/10th secs */
322 ms_delay = naptime * 1000;
323 if (ms_delay < 25500) {
324 timeout(ms_delay);
325 skip = 0;
326 } else {
327 skip = ms_delay / 25500;
328 timeout(ms_delay / (skip + 1));
329 }
330 }
331
332 void
333 redraw(void)
334 {
335 resizeterm(LINES, COLS);
336 CMDLINE = LINES - 1;
337 labels();
338
339 display(0);
340 needsredraw = 0;
341 }
342
343 static void
344 stop(int signo)
345 {
346 sigset_t set;
347
348 signal(SIGTSTP, sv_stop_handler);
349 /* unblock SIGTSTP */
350 sigemptyset(&set);
351 sigaddset(&set, SIGTSTP);
352 sigprocmask(SIG_UNBLOCK, &set, NULL);
353 /* stop ourselves */
354 kill(0, SIGTSTP);
355 /* must have been restarted */
356 signal(SIGTSTP, stop);
357 needsredraw = signo;
358 }
359
360 void
361 die(int signo)
362 {
363 move(CMDLINE, 0);
364 clrtoeol();
365 refresh();
366 endwin();
367 exit(0);
368 }
369
370 void
371 error(const char *fmt, ...)
372 {
373 va_list ap;
374 char buf[255];
375 int oy, ox;
376
377 va_start(ap, fmt);
378
379 if (wnd) {
380 getyx(stdscr, oy, ox);
381 (void) vsnprintf(buf, sizeof(buf), fmt, ap);
382 clrtoeol();
383 standout();
384 mvaddstr(CMDLINE, 0, buf);
385 standend();
386 move(oy, ox);
387 refresh();
388 } else {
389 (void) vfprintf(stderr, fmt, ap);
390 fprintf(stderr, "\n");
391 }
392 va_end(ap);
393 }
394
395 void
396 clearerror(void)
397 {
398
399 error("%s", "");
400 }
401
402 void
403 nlisterr(struct nlist name_list[])
404 {
405 int i, n;
406
407 n = 0;
408 clear();
409 mvprintw(2, 10, "systat: nlist: can't find following symbols:");
410 for (i = 0;
411 name_list[i].n_name != NULL && *name_list[i].n_name != '\0'; i++)
412 if (name_list[i].n_value == 0)
413 mvprintw(2 + ++n, 10, "%s", name_list[i].n_name);
414 move(CMDLINE, 0);
415 clrtoeol();
416 refresh();
417 endwin();
418 exit(1);
419 }
420