Annotation of src/usr.bin/systat/main.c, Revision 1.51
1.51 ! canacar 1: /* $Id: main.c,v 1.50 2008/11/05 15:48:44 canacar Exp $ */
1.38 canacar 2: /*
3: * Copyright (c) 2001, 2007 Can Erkin Acar
4: * Copyright (c) 2001 Daniel Hartmeier
5: * All rights reserved.
1.1 deraadt 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.38 canacar 11: * - Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * - Redistributions in binary form must reproduce the above
14: * copyright notice, this list of conditions and the following
15: * disclaimer in the documentation and/or other materials provided
16: * with the distribution.
17: *
18: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21: * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22: * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24: * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28: * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29: * POSSIBILITY OF SUCH DAMAGE.
30: *
1.1 deraadt 31: */
32:
1.38 canacar 33: #include <sys/types.h>
1.1 deraadt 34: #include <sys/param.h>
1.21 deraadt 35: #include <sys/sysctl.h>
1.1 deraadt 36:
1.38 canacar 37:
38: #include <ctype.h>
39: #include <curses.h>
1.1 deraadt 40: #include <err.h>
1.38 canacar 41: #include <errno.h>
42: #include <fcntl.h>
43: #include <limits.h>
44: #include <netdb.h>
1.1 deraadt 45: #include <signal.h>
46: #include <stdio.h>
1.38 canacar 47: #include <stdlib.h>
1.1 deraadt 48: #include <string.h>
1.38 canacar 49: #include <stdarg.h>
1.3 deraadt 50: #include <unistd.h>
1.35 deraadt 51: #include <utmp.h>
1.1 deraadt 52:
1.38 canacar 53: #include "engine.h"
1.1 deraadt 54: #include "systat.h"
55:
1.21 deraadt 56: double dellave;
1.1 deraadt 57:
1.21 deraadt 58: kvm_t *kd;
59: char *nlistf = NULL;
1.20 pvalchev 60: char *memf = NULL;
1.21 deraadt 61: double avenrun[3];
1.36 tedu 62: double naptime = 5.0;
1.21 deraadt 63: int verbose = 1; /* to report kvm read errs */
1.38 canacar 64: int nflag = 1;
1.35 deraadt 65: int ut, hz, stathz;
1.1 deraadt 66: char hostname[MAXHOSTNAMELEN];
67: WINDOW *wnd;
1.32 deraadt 68: int CMDLINE;
1.1 deraadt 69:
1.38 canacar 70: #define TIMEPOS 55
71:
1.50 canacar 72: int ucount(void);
73: void usage(void);
74:
1.38 canacar 75: /* command prompt */
76:
1.46 canacar 77: void cmd_delay(const char *);
78: void cmd_count(const char *);
79: void cmd_compat(const char *);
1.38 canacar 80:
81: struct command cm_compat = {"Command", cmd_compat};
82: struct command cm_delay = {"Seconds to delay", cmd_delay};
83: struct command cm_count = {"Number of lines to display", cmd_count};
1.1 deraadt 84:
1.38 canacar 85:
86: /* display functions */
1.3 deraadt 87:
1.2 deraadt 88: int
1.38 canacar 89: print_header(void)
1.1 deraadt 90: {
1.50 canacar 91: time_t now;
1.40 deraadt 92: int start = dispstart + 1, end = dispstart + maxprint;
93: char tbuf[26];
1.38 canacar 94:
95: if (end > num_disp)
96: end = num_disp;
97:
98: tb_start();
1.35 deraadt 99:
1.38 canacar 100: getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0]));
101:
102: time(&now);
103: strlcpy(tbuf, ctime(&now), sizeof tbuf);
104: tbprintf(" %d users", ucount());
105: tbprintf(" Load %.2f %.2f %.2f", avenrun[0], avenrun[1], avenrun[2]);
106: if (num_disp && (start > 1 || end != num_disp))
107: tbprintf(" (%u-%u of %u)", start, end, num_disp);
1.25 deraadt 108:
1.38 canacar 109: if (paused)
110: tbprintf(" PAUSED");
1.25 deraadt 111:
1.38 canacar 112: if (rawmode)
113: printf("\n\n%s\n", tmp_buf);
114: else
115: mvprintw(0, 0, "%s", tmp_buf);
1.36 tedu 116:
1.38 canacar 117: mvprintw(0, TIMEPOS, "%s", tbuf);
1.3 deraadt 118:
1.24 deraadt 119:
1.38 canacar 120: return (1);
1.1 deraadt 121: }
1.3 deraadt 122:
1.38 canacar 123: /* compatibility functions, rearrange later */
1.21 deraadt 124: void
1.38 canacar 125: error(const char *fmt, ...)
1.21 deraadt 126: {
1.38 canacar 127: va_list ap;
128: char buf[MAX_LINE_BUF];
129:
130: va_start(ap, fmt);
131: vsnprintf(buf, sizeof buf, fmt, ap);
132: va_end(ap);
1.21 deraadt 133:
1.38 canacar 134: message_set(buf);
1.21 deraadt 135: }
136:
1.38 canacar 137: void
138: nlisterr(struct nlist namelist[])
1.3 deraadt 139: {
1.38 canacar 140: int i, n;
141:
142: n = 0;
143: clear();
144: mvprintw(2, 10, "systat: nlist: can't find following symbols:");
145: for (i = 0;
146: namelist[i].n_name != NULL && *namelist[i].n_name != '\0'; i++)
147: if (namelist[i].n_value == 0)
148: mvprintw(2 + ++n, 10, "%s", namelist[i].n_name);
149: move(CMDLINE, 0);
150: clrtoeol();
151: refresh();
152: endwin();
1.3 deraadt 153: exit(1);
154: }
155:
1.1 deraadt 156: void
1.38 canacar 157: die(void)
1.1 deraadt 158: {
1.38 canacar 159: if (!rawmode)
160: endwin();
161: exit(0);
1.1 deraadt 162: }
163:
1.38 canacar 164:
165: int
166: prefix(char *s1, char *s2)
1.1 deraadt 167: {
1.38 canacar 168:
169: while (*s1 == *s2) {
170: if (*s1 == '\0')
171: return (1);
172: s1++, s2++;
173: }
174: return (*s1 == '\0');
1.21 deraadt 175: }
176:
1.38 canacar 177: /* calculate number of users on the system */
178: int
179: ucount(void)
1.21 deraadt 180: {
1.38 canacar 181: int nusers = 0;
182: struct utmp utmp;
1.35 deraadt 183:
1.38 canacar 184: if (ut < 0)
185: return (0);
186: lseek(ut, (off_t)0, SEEK_SET);
187: while (read(ut, &utmp, sizeof(utmp)))
188: if (utmp.ut_name[0] != '\0')
189: nusers++;
1.35 deraadt 190:
1.38 canacar 191: return (nusers);
1.1 deraadt 192: }
193:
1.38 canacar 194: /* main program functions */
195:
1.1 deraadt 196: void
1.50 canacar 197: usage(void)
1.1 deraadt 198: {
1.38 canacar 199: extern char *__progname;
1.44 jmc 200: fprintf(stderr, "usage: %s [-abin] [-d count] "
1.43 matthieu 201: "[-s delay] [-w width] [view] [delay]\n", __progname);
1.38 canacar 202: exit(1);
1.1 deraadt 203: }
204:
1.45 canacar 205: void
206: show_view(void)
207: {
208: if (rawmode)
209: return;
210:
211: tb_start();
212: tbprintf("%s %g", curr_view->name, naptime);
213: tb_end();
214: message_set(tmp_buf);
215: }
1.21 deraadt 216:
1.1 deraadt 217: void
1.38 canacar 218: add_view_tb(field_view *v)
1.1 deraadt 219: {
1.38 canacar 220: if (curr_view == v)
221: tbprintf("[%s] ", v->name);
222: else
223: tbprintf("%s ", v->name);
1.29 deraadt 224: }
225:
226: void
1.38 canacar 227: show_help(void)
1.29 deraadt 228: {
1.38 canacar 229: if (rawmode)
230: return;
231:
232: tb_start();
233: foreach_view(add_view_tb);
234: tb_end();
235: message_set(tmp_buf);
1.21 deraadt 236: }
237:
238: void
1.46 canacar 239: cmd_compat(const char *buf)
1.21 deraadt 240: {
1.46 canacar 241: const char *s;
1.38 canacar 242:
1.46 canacar 243: if (strcasecmp(buf, "help") == 0) {
1.38 canacar 244: show_help();
245: need_update = 1;
246: return;
247: }
1.46 canacar 248: if (strcasecmp(buf, "quit") == 0 || strcasecmp(buf, "q") == 0) {
1.38 canacar 249: gotsig_close = 1;
250: return;
251: }
1.46 canacar 252: if (strcasecmp(buf, "stop") == 0) {
253: paused = 1;
254: gotsig_alarm = 1;
255: return;
256: }
257: if (strncasecmp(buf, "start", 5) == 0) {
258: paused = 0;
259: gotsig_alarm = 1;
1.47 canacar 260: cmd_delay(buf + 5);
1.46 canacar 261: return;
262: }
1.38 canacar 263:
1.46 canacar 264: for (s = buf; *s && strchr("0123456789+-.eE", *s) != NULL; s++)
1.38 canacar 265: ;
266: if (*s) {
1.46 canacar 267: if (set_view(buf))
1.49 espie 268: error("Invalid/ambiguous view: %s", buf);
1.38 canacar 269: } else
1.46 canacar 270: cmd_delay(buf);
1.38 canacar 271: }
272:
273: void
1.46 canacar 274: cmd_delay(const char *buf)
1.38 canacar 275: {
276: double del;
1.46 canacar 277: del = atof(buf);
1.39 canacar 278:
1.38 canacar 279: if (del > 0) {
280: udelay = (useconds_t)(del * 1000000);
281: gotsig_alarm = 1;
1.39 canacar 282: naptime = del;
1.13 deraadt 283: }
1.1 deraadt 284: }
1.14 kstailey 285:
286: void
1.46 canacar 287: cmd_count(const char *buf)
1.14 kstailey 288: {
1.38 canacar 289: int ms;
1.46 canacar 290: ms = atoi(buf);
1.38 canacar 291:
292: if (ms <= 0 || ms > lines - HEADER_LINES)
293: maxprint = lines - HEADER_LINES;
294: else
295: maxprint = ms;
1.14 kstailey 296: }
297:
1.38 canacar 298:
299: int
300: keyboard_callback(int ch)
1.1 deraadt 301: {
1.38 canacar 302: switch (ch) {
303: case '?':
304: /* FALLTHROUGH */
305: case 'h':
306: show_help();
1.45 canacar 307: need_update = 1;
308: break;
309: case CTRL_G:
310: show_view();
1.38 canacar 311: need_update = 1;
312: break;
313: case 'l':
314: command_set(&cm_count, NULL);
315: break;
316: case 's':
317: command_set(&cm_delay, NULL);
318: break;
319: case ':':
320: command_set(&cm_compat, NULL);
321: break;
322: default:
323: return 0;
324: };
325:
326: return 1;
327: }
1.23 millert 328:
1.38 canacar 329: void
330: initialize(void)
331: {
332: engine_initialize();
333:
334: initvmstat();
335: initpigs();
336: initifstat();
337: initiostat();
338: initsensors();
339: initmembufs();
340: initnetstat();
341: initswap();
342: initpftop();
343: initpf();
1.48 canacar 344: initpool();
1.51 ! canacar 345: initmalloc();
1.1 deraadt 346: }
347:
348: void
1.38 canacar 349: gethz(void)
1.1 deraadt 350: {
1.38 canacar 351: struct clockinfo cinf;
352: size_t size = sizeof(cinf);
353: int mib[2];
1.1 deraadt 354:
1.38 canacar 355: mib[0] = CTL_KERN;
356: mib[1] = KERN_CLOCKRATE;
357: if (sysctl(mib, 2, &cinf, &size, NULL, 0) == -1)
358: return;
359: stathz = cinf.stathz;
360: hz = cinf.hz;
1.35 deraadt 361: }
362:
363: int
1.38 canacar 364: main(int argc, char *argv[])
1.35 deraadt 365: {
1.38 canacar 366: char errbuf[_POSIX2_LINE_MAX];
367: extern char *optarg;
368: extern int optind;
369: double delay = 5;
370:
371: char *viewstr = NULL;
372:
373: gid_t gid;
374: int countmax = 0;
375: int maxlines = 0;
376:
377: int ch;
378:
379: ut = open(_PATH_UTMP, O_RDONLY);
380: if (ut < 0) {
381: warn("No utmp");
382: }
383:
384: kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
385:
386: gid = getgid();
387: if (setresgid(gid, gid, gid) == -1)
388: err(1, "setresgid");
389:
1.47 canacar 390: while ((ch = getopt(argc, argv, "abd:ins:w:")) != -1) {
1.38 canacar 391: switch (ch) {
392: case 'a':
393: maxlines = -1;
394: break;
395: case 'b':
396: rawmode = 1;
397: interactive = 0;
398: break;
399: case 'd':
400: countmax = atoi(optarg);
401: if (countmax < 0)
402: countmax = 0;
403: break;
404: case 'i':
405: interactive = 1;
406: break;
407: case 'n':
408: nflag = 1;
409: break;
410: case 's':
411: delay = atof(optarg);
1.39 canacar 412: if (delay <= 0)
1.38 canacar 413: delay = 5;
414: break;
415: case 'w':
416: rawwidth = atoi(optarg);
417: if (rawwidth < 1)
418: rawwidth = DEFAULT_WIDTH;
419: if (rawwidth >= MAX_LINE_BUF)
420: rawwidth = MAX_LINE_BUF - 1;
421: break;
422: default:
423: usage();
424: /* NOTREACHED */
425: }
426: }
1.43 matthieu 427:
428: if (kd == NULL)
429: warnx("kvm_openfiles: %s", errbuf);
1.38 canacar 430:
431: argc -= optind;
432: argv += optind;
433:
434: if (argc == 1) {
435: double del = atof(argv[0]);
436: if (del == 0)
437: viewstr = argv[0];
438: else
439: delay = del;
440: } else if (argc == 2) {
441: viewstr = argv[0];
442: delay = atof(argv[1]);
1.42 canacar 443: if (delay <= 0)
444: delay = 5;
1.38 canacar 445: }
446:
447: udelay = (useconds_t)(delay * 1000000.0);
448: if (udelay < 1)
449: udelay = 1;
1.39 canacar 450:
451: naptime = (double)udelay / 1000000.0;
1.38 canacar 452:
453: gethostname(hostname, sizeof (hostname));
454: gethz();
455:
456: initialize();
457:
458: set_order(NULL);
459: if (viewstr && set_view(viewstr)) {
1.49 espie 460: fprintf(stderr, "Unknown/ambiguous view name: %s\n", viewstr);
1.38 canacar 461: return 1;
462: }
463:
464: if (!isatty(STDOUT_FILENO)) {
465: rawmode = 1;
466: interactive = 0;
467: }
468:
469: setup_term(maxlines);
470:
471: if (rawmode && countmax == 0)
472: countmax = 1;
473:
474: gotsig_alarm = 1;
1.35 deraadt 475:
1.38 canacar 476: engine_loop(countmax);
1.35 deraadt 477:
1.38 canacar 478: return 0;
1.1 deraadt 479: }