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