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