Home | History | Annotate | Line # | Download | only in systat
main.c revision 1.34
      1  1.34  jdolecek /*	$NetBSD: main.c,v 1.34 2003/08/03 12:14:58 jdolecek 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.34  jdolecek __RCSID("$NetBSD: main.c,v 1.34 2003/08/03 12:14:58 jdolecek 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.28    simonb #include <ctype.h>
     49   1.4   mycroft #include <err.h>
     50  1.10  explorer #include <limits.h>
     51   1.1       jtc #include <signal.h>
     52  1.28    simonb #include <stdarg.h>
     53   1.1       jtc #include <stdio.h>
     54  1.28    simonb #include <stdlib.h>
     55   1.5       cgd #include <string.h>
     56   1.8   thorpej #include <unistd.h>
     57  1.31       dsl #include <termios.h>
     58  1.31       dsl #include <sys/ioctl.h>
     59   1.4   mycroft 
     60   1.1       jtc #include "systat.h"
     61   1.1       jtc #include "extern.h"
     62   1.1       jtc 
     63   1.1       jtc static struct nlist namelist[] = {
     64   1.1       jtc #define X_FIRST		0
     65   1.1       jtc #define	X_HZ		0
     66   1.1       jtc 	{ "_hz" },
     67   1.1       jtc #define	X_STATHZ		1
     68   1.1       jtc 	{ "_stathz" },
     69  1.29      matt #define	X_MAXSLP		2
     70  1.29      matt 	{ "_maxslp" },
     71   1.1       jtc 	{ "" }
     72   1.1       jtc };
     73   1.1       jtc static int     dellave;
     74   1.1       jtc 
     75   1.1       jtc kvm_t *kd;
     76   1.8   thorpej char	*memf = NULL;
     77   1.8   thorpej char	*nlistf = NULL;
     78   1.1       jtc sig_t	sigtstpdfl;
     79   1.1       jtc double avenrun[3];
     80   1.1       jtc int     col;
     81   1.1       jtc int	naptime = 5;
     82   1.1       jtc int     verbose = 1;                    /* to report kvm read errs */
     83  1.29      matt int     hz, stathz, maxslp;
     84   1.1       jtc char    c;
     85   1.1       jtc char    *namp;
     86  1.13       mrg char    hostname[MAXHOSTNAMELEN + 1];
     87   1.1       jtc WINDOW  *wnd;
     88   1.1       jtc int     CMDLINE;
     89  1.27   hubertf int     turns = 2;	/* stay how many refresh-turns in 'all' mode? */
     90  1.27   hubertf int     allflag;
     91  1.27   hubertf int     allcounter;
     92   1.1       jtc 
     93   1.1       jtc static	WINDOW *wload;			/* one line window for load average */
     94   1.1       jtc 
     95  1.31       dsl static void (*sv_stop_handler)(int);
     96  1.31       dsl 
     97  1.31       dsl static void stop(int);
     98  1.26        ad static void usage(void);
     99  1.26        ad int main(int, char **);
    100   1.8   thorpej 
    101  1.15  drochner gid_t egid; /* XXX needed by initiostat() and initkre() */
    102  1.15  drochner 
    103   1.7       jtc int
    104  1.26        ad main(int argc, char **argv)
    105   1.1       jtc {
    106   1.8   thorpej 	int ch;
    107  1.10  explorer 	char errbuf[_POSIX2_LINE_MAX];
    108   1.1       jtc 
    109  1.15  drochner 	egid = getegid();
    110  1.13       mrg 	(void)setegid(getgid());
    111  1.21     jwise 
    112  1.27   hubertf 	while ((ch = getopt(argc, argv, "M:N:nw:t:")) != -1)
    113   1.8   thorpej 		switch(ch) {
    114   1.9    scottr 		case 'M':
    115   1.9    scottr 			memf = optarg;
    116   1.9    scottr 			break;
    117   1.9    scottr 		case 'N':
    118   1.9    scottr 			nlistf = optarg;
    119   1.9    scottr 			break;
    120  1.23    itojun 		case 'n':
    121  1.23    itojun 			nflag = !nflag;
    122  1.23    itojun 			break;
    123   1.9    scottr 		case 'w':
    124   1.9    scottr 			if ((naptime = atoi(optarg)) <= 0)
    125   1.9    scottr 				errx(1, "interval <= 0.");
    126   1.9    scottr 			break;
    127  1.27   hubertf 		case 't':
    128  1.27   hubertf 			if ((turns = atoi(optarg)) <= 0)
    129  1.27   hubertf 				errx(1, "turns <= 0.");
    130  1.27   hubertf 			break;
    131   1.9    scottr 		case '?':
    132   1.9    scottr 		default:
    133   1.9    scottr 			usage();
    134   1.9    scottr 		}
    135   1.9    scottr 	argc -= optind;
    136   1.9    scottr 	argv += optind;
    137   1.8   thorpej 
    138  1.19     jwise 
    139  1.19     jwise 	for ( ; argc > 0; argc--, argv++) {
    140  1.19     jwise 		struct mode *p;
    141  1.20     jwise 		int modefound = 0;
    142  1.19     jwise 
    143   1.8   thorpej 		if (isdigit(argv[0][0])) {
    144   1.8   thorpej 			naptime = atoi(argv[0]);
    145   1.8   thorpej 			if (naptime <= 0)
    146   1.8   thorpej 				naptime = 5;
    147  1.19     jwise 			continue;
    148  1.19     jwise 		}
    149   1.1       jtc 
    150  1.19     jwise 		for (p = modes; p->c_name ; p++) {
    151  1.24     jwise 			if (strstr(p->c_name, argv[0]) == p->c_name) {
    152  1.19     jwise 				curmode = p;
    153  1.20     jwise 				modefound++;
    154  1.19     jwise 				break;
    155  1.19     jwise 			}
    156  1.27   hubertf 
    157  1.27   hubertf 			if(strstr("all",argv[0]) == "all"){
    158  1.27   hubertf 				allcounter=0;
    159  1.27   hubertf 				allflag=1;
    160  1.27   hubertf 			}
    161   1.1       jtc 		}
    162  1.19     jwise 
    163  1.27   hubertf 
    164  1.27   hubertf 		if (!modefound && !allflag)
    165  1.19     jwise 			error("%s: Unknown command.", argv[0]);
    166   1.1       jtc 	}
    167  1.13       mrg 
    168  1.15  drochner 	/*
    169  1.15  drochner 	 * Discard setgid privileges.  If not the running kernel, we toss
    170  1.15  drochner 	 * them away totally so that bad guys can't print interesting stuff
    171  1.15  drochner 	 * from kernel memory, otherwise switch back to kmem for the
    172  1.15  drochner 	 * duration of the kvm_openfiles() call.
    173  1.15  drochner 	 */
    174  1.15  drochner 	if (nlistf != NULL || memf != NULL)
    175  1.15  drochner 		(void)setgid(getgid());
    176  1.15  drochner 	else
    177  1.15  drochner 		(void)setegid(egid);
    178  1.15  drochner 
    179   1.8   thorpej 	kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
    180   1.1       jtc 	if (kd == NULL) {
    181   1.1       jtc 		error("%s", errbuf);
    182   1.1       jtc 		exit(1);
    183   1.1       jtc 	}
    184  1.15  drochner 
    185  1.15  drochner 	/* Get rid of privs for now. */
    186  1.13       mrg 	if (nlistf == NULL && memf == NULL)
    187  1.15  drochner 		(void)setegid(getgid());
    188  1.15  drochner 
    189   1.1       jtc 	if (kvm_nlist(kd, namelist)) {
    190  1.34  jdolecek 		if (nlistf)
    191  1.34  jdolecek 			errx(1, "%s: no namelist", nlistf);
    192  1.34  jdolecek 		else
    193  1.34  jdolecek 			errx(1, "no namelist");
    194   1.1       jtc 	}
    195  1.34  jdolecek 
    196   1.1       jtc 	signal(SIGINT, die);
    197   1.1       jtc 	signal(SIGQUIT, die);
    198   1.1       jtc 	signal(SIGTERM, die);
    199   1.9    scottr 	signal(SIGWINCH, redraw);
    200  1.31       dsl 	sv_stop_handler = signal(SIGTSTP, stop);
    201   1.1       jtc 
    202   1.1       jtc 	/*
    203   1.1       jtc 	 * Initialize display.  Load average appears in a one line
    204   1.1       jtc 	 * window of its own.  Current command's display appears in
    205   1.1       jtc 	 * an overlapping sub-window of stdscr configured by the display
    206   1.1       jtc 	 * routines to minimize update work by curses.
    207   1.1       jtc 	 */
    208   1.6       jtc 	if (initscr() == NULL)
    209   1.6       jtc 	{
    210   1.6       jtc 		warnx("couldn't initialize screen");
    211   1.6       jtc 		exit(0);
    212   1.6       jtc 	}
    213   1.6       jtc 
    214   1.1       jtc 	CMDLINE = LINES - 1;
    215  1.18     jwise 	wnd = (*curmode->c_open)();
    216   1.1       jtc 	if (wnd == NULL) {
    217   1.4   mycroft 		warnx("couldn't initialize display");
    218   1.1       jtc 		die(0);
    219   1.1       jtc 	}
    220   1.1       jtc 	wload = newwin(1, 0, 3, 20);
    221   1.1       jtc 	if (wload == NULL) {
    222   1.4   mycroft 		warnx("couldn't set up load average window");
    223   1.1       jtc 		die(0);
    224   1.1       jtc 	}
    225   1.1       jtc 	gethostname(hostname, sizeof (hostname));
    226  1.13       mrg 	hostname[sizeof(hostname) - 1] = '\0';
    227  1.16      ross 	NREAD(X_HZ, &hz, sizeof hz);
    228  1.16      ross 	NREAD(X_STATHZ, &stathz, sizeof stathz);
    229  1.29      matt 	NREAD(X_MAXSLP, &maxslp, sizeof maxslp);
    230  1.18     jwise 	(*curmode->c_init)();
    231  1.18     jwise 	curmode->c_flags |= CF_INIT;
    232   1.1       jtc 	labels();
    233   1.1       jtc 
    234   1.1       jtc 	dellave = 0.0;
    235   1.1       jtc 
    236   1.1       jtc 	signal(SIGALRM, display);
    237   1.1       jtc 	display(0);
    238   1.1       jtc 	noecho();
    239  1.30     blymn 	cbreak();
    240   1.1       jtc 	keyboard();
    241   1.1       jtc 	/*NOTREACHED*/
    242   1.1       jtc }
    243   1.8   thorpej 
    244   1.8   thorpej static void
    245  1.26        ad usage(void)
    246   1.8   thorpej {
    247  1.23    itojun 	fprintf(stderr, "usage: systat [-n] [-M core] [-N system] [-w wait] "
    248  1.27   hubertf 		"[-t turns]\n\t\t[display] [refresh-interval]\n");
    249   1.8   thorpej 	exit(1);
    250   1.8   thorpej }
    251   1.8   thorpej 
    252   1.1       jtc 
    253   1.1       jtc void
    254  1.26        ad labels(void)
    255   1.1       jtc {
    256  1.18     jwise 	if (curmode->c_flags & CF_LOADAV) {
    257   1.1       jtc 		mvaddstr(2, 20,
    258   1.1       jtc 		    "/0   /1   /2   /3   /4   /5   /6   /7   /8   /9   /10");
    259   1.1       jtc 		mvaddstr(3, 5, "Load Average");
    260   1.1       jtc 	}
    261  1.18     jwise 	(*curmode->c_label)();
    262   1.1       jtc #ifdef notdef
    263   1.1       jtc 	mvprintw(21, 25, "CPU usage on %s", hostname);
    264   1.1       jtc #endif
    265   1.1       jtc 	refresh();
    266   1.1       jtc }
    267   1.1       jtc 
    268   1.1       jtc void
    269  1.26        ad display(int signo)
    270   1.1       jtc {
    271  1.25   mycroft 	int j;
    272  1.22     jwise 	sigset_t set;
    273  1.27   hubertf 	struct mode *p;
    274  1.22     jwise 
    275  1.22     jwise 	sigemptyset(&set);
    276  1.22     jwise 	sigaddset(&set, SIGALRM);
    277  1.22     jwise 	sigprocmask(SIG_BLOCK, &set, NULL);
    278   1.1       jtc 
    279   1.1       jtc 	/* Get the load average over the last minute. */
    280  1.14       mrg 	(void)getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0]));
    281  1.18     jwise 	(*curmode->c_fetch)();
    282  1.18     jwise 	if (curmode->c_flags & CF_LOADAV) {
    283   1.1       jtc 		j = 5.0*avenrun[0] + 0.5;
    284   1.1       jtc 		dellave -= avenrun[0];
    285   1.1       jtc 		if (dellave >= 0.0)
    286   1.1       jtc 			c = '<';
    287   1.1       jtc 		else {
    288   1.1       jtc 			c = '>';
    289   1.1       jtc 			dellave = -dellave;
    290   1.1       jtc 		}
    291   1.1       jtc 		if (dellave < 0.1)
    292   1.1       jtc 			c = '|';
    293   1.1       jtc 		dellave = avenrun[0];
    294  1.25   mycroft 		wmove(wload, 0, 0);
    295  1.25   mycroft 		wclrtoeol(wload);
    296  1.25   mycroft 		whline(wload, c, (j > 50) ? 50 : j);
    297   1.1       jtc 		if (j > 50)
    298   1.1       jtc 			wprintw(wload, " %4.1f", avenrun[0]);
    299   1.1       jtc 	}
    300  1.18     jwise 	(*curmode->c_refresh)();
    301  1.18     jwise 	if (curmode->c_flags & CF_LOADAV)
    302   1.1       jtc 		wrefresh(wload);
    303   1.1       jtc 	wrefresh(wnd);
    304   1.1       jtc 	move(CMDLINE, col);
    305   1.1       jtc 	refresh();
    306  1.27   hubertf 
    307  1.27   hubertf 	if (allflag && signo==SIGALRM) {
    308  1.27   hubertf 		if (allcounter >= turns){
    309  1.27   hubertf 			p = curmode;
    310  1.27   hubertf 			p++;
    311  1.27   hubertf 			if (p->c_name == NULL)
    312  1.27   hubertf 				p = modes;
    313  1.27   hubertf 			switch_mode(p);
    314  1.27   hubertf 			allcounter=0;
    315  1.27   hubertf 		} else
    316  1.27   hubertf 			allcounter++;
    317  1.27   hubertf        }
    318  1.27   hubertf 
    319  1.22     jwise 	sigprocmask(SIG_UNBLOCK, &set, NULL);
    320   1.1       jtc 	alarm(naptime);
    321   1.9    scottr }
    322   1.9    scottr 
    323   1.9    scottr void
    324  1.26        ad redraw(int signo)
    325   1.9    scottr {
    326   1.9    scottr 	sigset_t set;
    327  1.31       dsl 	struct winsize win;
    328   1.9    scottr 
    329   1.9    scottr 	sigemptyset(&set);
    330   1.9    scottr 	sigaddset(&set, SIGALRM);
    331   1.9    scottr 	sigprocmask(SIG_BLOCK, &set, NULL);
    332  1.31       dsl 
    333  1.33       dsl 	if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) != -1 &&
    334  1.33       dsl 	    (win.ws_row != LINES || win.ws_col != COLS)) {
    335  1.31       dsl 		resizeterm(win.ws_row, win.ws_col);
    336  1.31       dsl 		CMDLINE = LINES - 1;
    337  1.31       dsl 		labels();
    338  1.31       dsl 	}
    339  1.31       dsl 
    340  1.31       dsl 	sigprocmask(SIG_UNBLOCK, &set, NULL);
    341  1.31       dsl 	display(0);
    342  1.31       dsl }
    343  1.31       dsl 
    344  1.31       dsl static void
    345  1.31       dsl stop(int signo)
    346  1.31       dsl {
    347  1.31       dsl 	sigset_t set;
    348  1.31       dsl 
    349  1.31       dsl 	signal(SIGTSTP, sv_stop_handler);
    350  1.31       dsl 	/* unblock SIGTSTP */
    351  1.31       dsl 	sigemptyset(&set);
    352  1.31       dsl 	sigaddset(&set, SIGTSTP);
    353   1.9    scottr 	sigprocmask(SIG_UNBLOCK, &set, NULL);
    354  1.31       dsl 	/* stop ourselves */
    355  1.31       dsl 	kill(0, SIGTSTP);
    356  1.31       dsl 	/* must have been restarted */
    357  1.32       dsl 	signal(SIGTSTP, stop);
    358  1.31       dsl 	redraw(signo);
    359   1.1       jtc }
    360   1.1       jtc 
    361   1.1       jtc void
    362  1.26        ad die(int signo)
    363   1.1       jtc {
    364   1.1       jtc 	move(CMDLINE, 0);
    365   1.1       jtc 	clrtoeol();
    366   1.1       jtc 	refresh();
    367   1.1       jtc 	endwin();
    368   1.1       jtc 	exit(0);
    369   1.1       jtc }
    370   1.1       jtc 
    371   1.1       jtc void
    372   1.1       jtc error(const char *fmt, ...)
    373   1.1       jtc {
    374   1.1       jtc 	va_list ap;
    375   1.1       jtc 	char buf[255];
    376   1.1       jtc 	int oy, ox;
    377  1.26        ad 
    378   1.1       jtc 	va_start(ap, fmt);
    379   1.1       jtc 
    380   1.1       jtc 	if (wnd) {
    381   1.1       jtc 		getyx(stdscr, oy, ox);
    382  1.21     jwise 		(void) vsnprintf(buf, sizeof(buf), fmt, ap);
    383   1.1       jtc 		clrtoeol();
    384   1.1       jtc 		standout();
    385   1.1       jtc 		mvaddstr(CMDLINE, 0, buf);
    386   1.1       jtc 		standend();
    387   1.1       jtc 		move(oy, ox);
    388   1.1       jtc 		refresh();
    389   1.1       jtc 	} else {
    390   1.1       jtc 		(void) vfprintf(stderr, fmt, ap);
    391   1.1       jtc 		fprintf(stderr, "\n");
    392   1.1       jtc 	}
    393   1.1       jtc 	va_end(ap);
    394   1.1       jtc }
    395   1.1       jtc 
    396   1.1       jtc void
    397  1.26        ad nlisterr(struct nlist namelist[])
    398   1.1       jtc {
    399   1.1       jtc 	int i, n;
    400   1.1       jtc 
    401   1.1       jtc 	n = 0;
    402   1.1       jtc 	clear();
    403   1.1       jtc 	mvprintw(2, 10, "systat: nlist: can't find following symbols:");
    404   1.1       jtc 	for (i = 0;
    405   1.1       jtc 	    namelist[i].n_name != NULL && *namelist[i].n_name != '\0'; i++)
    406   1.1       jtc 		if (namelist[i].n_value == 0)
    407   1.1       jtc 			mvprintw(2 + ++n, 10, "%s", namelist[i].n_name);
    408   1.1       jtc 	move(CMDLINE, 0);
    409   1.1       jtc 	clrtoeol();
    410   1.1       jtc 	refresh();
    411  1.34  jdolecek 	sleep(5);
    412   1.1       jtc 	endwin();
    413   1.1       jtc 	exit(1);
    414   1.1       jtc }
    415