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