Home | History | Annotate | Line # | Download | only in systat
main.c revision 1.55
      1  1.55  christos /*	$NetBSD: main.c,v 1.55 2019/01/25 15:31:11 christos 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.35       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       jtc  *    may be used to endorse or promote products derived from this software
     17   1.1       jtc  *    without specific prior written permission.
     18   1.1       jtc  *
     19   1.1       jtc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       jtc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       jtc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       jtc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       jtc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       jtc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       jtc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       jtc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       jtc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       jtc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       jtc  * SUCH DAMAGE.
     30   1.1       jtc  */
     31   1.1       jtc 
     32  1.11       mrg #include <sys/cdefs.h>
     33   1.1       jtc #ifndef lint
     34  1.43     lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1992, 1993\
     35  1.43     lukem  The Regents of the University of California.  All rights reserved.");
     36   1.2       jtc #if 0
     37   1.1       jtc static char sccsid[] = "@(#)main.c	8.1 (Berkeley) 6/6/93";
     38   1.2       jtc #endif
     39  1.55  christos __RCSID("$NetBSD: main.c,v 1.55 2019/01/25 15:31:11 christos Exp $");
     40   1.1       jtc #endif /* not lint */
     41   1.1       jtc 
     42   1.1       jtc #include <sys/param.h>
     43  1.47  christos #include <sys/sysctl.h>
     44  1.47  christos #include <sys/ioctl.h>
     45   1.1       jtc 
     46  1.28    simonb #include <ctype.h>
     47   1.4   mycroft #include <err.h>
     48  1.47  christos #include <errno.h>
     49  1.10  explorer #include <limits.h>
     50   1.1       jtc #include <signal.h>
     51  1.28    simonb #include <stdarg.h>
     52   1.1       jtc #include <stdio.h>
     53  1.28    simonb #include <stdlib.h>
     54   1.5       cgd #include <string.h>
     55   1.8   thorpej #include <unistd.h>
     56  1.31       dsl #include <termios.h>
     57   1.4   mycroft 
     58   1.1       jtc #include "systat.h"
     59   1.1       jtc #include "extern.h"
     60  1.55  christos #include "drvstats.h"
     61   1.1       jtc 
     62   1.1       jtc static int     dellave;
     63   1.1       jtc 
     64   1.1       jtc kvm_t *kd;
     65   1.8   thorpej char	*memf = NULL;
     66   1.8   thorpej char	*nlistf = NULL;
     67   1.1       jtc sig_t	sigtstpdfl;
     68   1.1       jtc double avenrun[3];
     69   1.1       jtc int     col;
     70  1.54     sevan double	naptime = 1;
     71   1.1       jtc int     verbose = 1;                    /* to report kvm read errs */
     72  1.29      matt int     hz, stathz, maxslp;
     73   1.1       jtc char    c;
     74   1.1       jtc char    *namp;
     75  1.13       mrg char    hostname[MAXHOSTNAMELEN + 1];
     76   1.1       jtc WINDOW  *wnd;
     77   1.1       jtc int     CMDLINE;
     78  1.27   hubertf int     turns = 2;	/* stay how many refresh-turns in 'all' mode? */
     79  1.27   hubertf int     allflag;
     80  1.27   hubertf int     allcounter;
     81  1.41  christos sig_atomic_t needsredraw = 0;
     82  1.55  christos float	hertz;
     83  1.55  christos double	etime;
     84   1.1       jtc 
     85   1.1       jtc static	WINDOW *wload;			/* one line window for load average */
     86   1.1       jtc 
     87  1.31       dsl static void (*sv_stop_handler)(int);
     88  1.31       dsl 
     89  1.31       dsl static void stop(int);
     90  1.46     joerg __dead static void usage(void);
     91   1.8   thorpej 
     92  1.15  drochner gid_t egid; /* XXX needed by initiostat() and initkre() */
     93  1.15  drochner 
     94   1.7       jtc int
     95  1.26        ad main(int argc, char **argv)
     96   1.1       jtc {
     97   1.8   thorpej 	int ch;
     98  1.10  explorer 	char errbuf[_POSIX2_LINE_MAX];
     99  1.44       apb 	const char *all;
    100  1.47  christos 	struct clockinfo clk;
    101  1.47  christos 	size_t len;
    102  1.51       mrg 	int bflag = 0;
    103   1.1       jtc 
    104  1.44       apb 	all = "all";
    105  1.15  drochner 	egid = getegid();
    106  1.13       mrg 	(void)setegid(getgid());
    107  1.21     jwise 
    108  1.52       mrg 	while ((ch = getopt(argc, argv, "M:N:bnw:t:")) != -1)
    109   1.8   thorpej 		switch(ch) {
    110   1.9    scottr 		case 'M':
    111   1.9    scottr 			memf = optarg;
    112   1.9    scottr 			break;
    113   1.9    scottr 		case 'N':
    114   1.9    scottr 			nlistf = optarg;
    115   1.9    scottr 			break;
    116  1.52       mrg 		case 'b':
    117  1.52       mrg 			bflag = !bflag;
    118  1.52       mrg 			break;
    119  1.23    itojun 		case 'n':
    120  1.23    itojun 			nflag = !nflag;
    121  1.23    itojun 			break;
    122  1.27   hubertf 		case 't':
    123  1.27   hubertf 			if ((turns = atoi(optarg)) <= 0)
    124  1.27   hubertf 				errx(1, "turns <= 0.");
    125  1.27   hubertf 			break;
    126  1.52       mrg 		case 'w':
    127  1.52       mrg 			if ((naptime = strtod(optarg, NULL)) <= 0)
    128  1.52       mrg 				errx(1, "interval <= 0.");
    129  1.51       mrg 			break;
    130   1.9    scottr 		case '?':
    131   1.9    scottr 		default:
    132   1.9    scottr 			usage();
    133   1.9    scottr 		}
    134   1.9    scottr 	argc -= optind;
    135   1.9    scottr 	argv += optind;
    136   1.8   thorpej 
    137  1.19     jwise 
    138  1.19     jwise 	for ( ; argc > 0; argc--, argv++) {
    139  1.19     jwise 		struct mode *p;
    140  1.20     jwise 		int modefound = 0;
    141  1.19     jwise 
    142  1.38       dsl 		if (isdigit((unsigned char)argv[0][0])) {
    143  1.50  christos 			naptime = strtod(argv[0], NULL);
    144   1.8   thorpej 			if (naptime <= 0)
    145   1.8   thorpej 				naptime = 5;
    146  1.19     jwise 			continue;
    147  1.19     jwise 		}
    148   1.1       jtc 
    149  1.19     jwise 		for (p = modes; p->c_name ; p++) {
    150  1.24     jwise 			if (strstr(p->c_name, argv[0]) == p->c_name) {
    151  1.19     jwise 				curmode = p;
    152  1.20     jwise 				modefound++;
    153  1.19     jwise 				break;
    154  1.19     jwise 			}
    155  1.27   hubertf 
    156  1.44       apb 			if (strstr(all, argv[0]) == all) {
    157  1.27   hubertf 				allcounter=0;
    158  1.27   hubertf 				allflag=1;
    159  1.27   hubertf 			}
    160   1.1       jtc 		}
    161  1.19     jwise 
    162  1.27   hubertf 
    163  1.27   hubertf 		if (!modefound && !allflag)
    164  1.19     jwise 			error("%s: Unknown command.", argv[0]);
    165   1.1       jtc 	}
    166  1.13       mrg 
    167  1.15  drochner 	/*
    168  1.15  drochner 	 * Discard setgid privileges.  If not the running kernel, we toss
    169  1.15  drochner 	 * them away totally so that bad guys can't print interesting stuff
    170  1.15  drochner 	 * from kernel memory, otherwise switch back to kmem for the
    171  1.15  drochner 	 * duration of the kvm_openfiles() call.
    172  1.15  drochner 	 */
    173  1.15  drochner 	if (nlistf != NULL || memf != NULL)
    174  1.15  drochner 		(void)setgid(getgid());
    175  1.15  drochner 	else
    176  1.15  drochner 		(void)setegid(egid);
    177  1.15  drochner 
    178   1.8   thorpej 	kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
    179  1.54     sevan 	if (kd == NULL)
    180  1.54     sevan 		errx(1, "%s", errbuf);
    181  1.15  drochner 
    182  1.15  drochner 	/* Get rid of privs for now. */
    183  1.13       mrg 	if (nlistf == NULL && memf == NULL)
    184  1.15  drochner 		(void)setegid(getgid());
    185  1.15  drochner 
    186   1.1       jtc 	signal(SIGINT, die);
    187   1.1       jtc 	signal(SIGQUIT, die);
    188   1.1       jtc 	signal(SIGTERM, die);
    189  1.31       dsl 	sv_stop_handler = signal(SIGTSTP, stop);
    190   1.1       jtc 
    191   1.1       jtc 	/*
    192   1.1       jtc 	 * Initialize display.  Load average appears in a one line
    193   1.1       jtc 	 * window of its own.  Current command's display appears in
    194   1.1       jtc 	 * an overlapping sub-window of stdscr configured by the display
    195   1.1       jtc 	 * routines to minimize update work by curses.
    196   1.1       jtc 	 */
    197   1.6       jtc 	if (initscr() == NULL)
    198  1.54     sevan 		errx(1, "couldn't initialize screen");
    199   1.6       jtc 
    200   1.1       jtc 	CMDLINE = LINES - 1;
    201  1.18     jwise 	wnd = (*curmode->c_open)();
    202   1.1       jtc 	if (wnd == NULL) {
    203  1.54     sevan 		move(CMDLINE, 0);
    204  1.54     sevan 		clrtoeol();
    205  1.54     sevan 		refresh();
    206  1.54     sevan 		endwin();
    207   1.4   mycroft 		warnx("couldn't initialize display");
    208   1.1       jtc 		die(0);
    209   1.1       jtc 	}
    210   1.1       jtc 	wload = newwin(1, 0, 3, 20);
    211   1.1       jtc 	if (wload == NULL) {
    212  1.54     sevan 		move(CMDLINE, 0);
    213  1.54     sevan 		clrtoeol();
    214  1.54     sevan 		refresh();
    215  1.54     sevan 		endwin();
    216   1.4   mycroft 		warnx("couldn't set up load average window");
    217   1.1       jtc 		die(0);
    218   1.1       jtc 	}
    219   1.1       jtc 	gethostname(hostname, sizeof (hostname));
    220  1.13       mrg 	hostname[sizeof(hostname) - 1] = '\0';
    221  1.47  christos 
    222  1.47  christos 	len = sizeof(clk);
    223  1.47  christos 	if (sysctlbyname("kern.clockrate", &clk, &len, NULL, 0))
    224  1.47  christos 		error("can't get \"kern.clockrate\": %s", strerror(errno));
    225  1.47  christos 	hz = clk.hz;
    226  1.47  christos 	stathz = clk.stathz;
    227  1.47  christos 
    228  1.47  christos 	len = sizeof(maxslp);
    229  1.47  christos 	if (sysctlbyname("vm.maxslp", &maxslp, &len, NULL, 0))
    230  1.47  christos 		error("can't get \"vm.maxslp\": %s", strerror(errno));
    231  1.47  christos 
    232  1.18     jwise 	(*curmode->c_init)();
    233  1.18     jwise 	curmode->c_flags |= CF_INIT;
    234   1.1       jtc 	labels();
    235   1.1       jtc 
    236   1.1       jtc 	dellave = 0.0;
    237   1.1       jtc 
    238   1.1       jtc 	display(0);
    239  1.51       mrg 	if (!bflag) {
    240  1.51       mrg 		noecho();
    241  1.51       mrg 		cbreak();
    242  1.51       mrg 		keyboard();
    243  1.51       mrg 	} else
    244  1.51       mrg 		die(0);
    245   1.1       jtc 	/*NOTREACHED*/
    246   1.1       jtc }
    247   1.8   thorpej 
    248   1.8   thorpej static void
    249  1.26        ad usage(void)
    250   1.8   thorpej {
    251  1.53       snj 	fprintf(stderr, "usage: systat [-bn] [-M core] [-N system] [-w wait] "
    252  1.27   hubertf 		"[-t turns]\n\t\t[display] [refresh-interval]\n");
    253   1.8   thorpej 	exit(1);
    254   1.8   thorpej }
    255   1.8   thorpej 
    256   1.1       jtc 
    257   1.1       jtc void
    258  1.26        ad labels(void)
    259   1.1       jtc {
    260  1.18     jwise 	if (curmode->c_flags & CF_LOADAV) {
    261   1.1       jtc 		mvaddstr(2, 20,
    262   1.1       jtc 		    "/0   /1   /2   /3   /4   /5   /6   /7   /8   /9   /10");
    263   1.1       jtc 		mvaddstr(3, 5, "Load Average");
    264   1.1       jtc 	}
    265  1.18     jwise 	(*curmode->c_label)();
    266   1.1       jtc #ifdef notdef
    267   1.1       jtc 	mvprintw(21, 25, "CPU usage on %s", hostname);
    268   1.1       jtc #endif
    269   1.1       jtc 	refresh();
    270   1.1       jtc }
    271   1.1       jtc 
    272   1.1       jtc void
    273  1.26        ad display(int signo)
    274   1.1       jtc {
    275  1.45       dsl 	static int skip;
    276  1.25   mycroft 	int j;
    277  1.27   hubertf 	struct mode *p;
    278  1.45       dsl 	int ms_delay;
    279  1.45       dsl 
    280  1.45       dsl 	if (signo == SIGALRM && skip-- > 0)
    281  1.45       dsl 		/* Don't display on this timeout */
    282  1.45       dsl 		return;
    283  1.22     jwise 
    284   1.1       jtc 	/* Get the load average over the last minute. */
    285  1.14       mrg 	(void)getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0]));
    286  1.18     jwise 	(*curmode->c_fetch)();
    287  1.18     jwise 	if (curmode->c_flags & CF_LOADAV) {
    288   1.1       jtc 		j = 5.0*avenrun[0] + 0.5;
    289   1.1       jtc 		dellave -= avenrun[0];
    290   1.1       jtc 		if (dellave >= 0.0)
    291   1.1       jtc 			c = '<';
    292   1.1       jtc 		else {
    293   1.1       jtc 			c = '>';
    294   1.1       jtc 			dellave = -dellave;
    295   1.1       jtc 		}
    296   1.1       jtc 		if (dellave < 0.1)
    297   1.1       jtc 			c = '|';
    298   1.1       jtc 		dellave = avenrun[0];
    299  1.25   mycroft 		wmove(wload, 0, 0);
    300  1.25   mycroft 		wclrtoeol(wload);
    301  1.25   mycroft 		whline(wload, c, (j > 50) ? 50 : j);
    302   1.1       jtc 		if (j > 50)
    303   1.1       jtc 			wprintw(wload, " %4.1f", avenrun[0]);
    304   1.1       jtc 	}
    305  1.18     jwise 	(*curmode->c_refresh)();
    306  1.18     jwise 	if (curmode->c_flags & CF_LOADAV)
    307   1.1       jtc 		wrefresh(wload);
    308   1.1       jtc 	wrefresh(wnd);
    309   1.1       jtc 	move(CMDLINE, col);
    310   1.1       jtc 	refresh();
    311  1.27   hubertf 
    312  1.27   hubertf 	if (allflag && signo==SIGALRM) {
    313  1.27   hubertf 		if (allcounter >= turns){
    314  1.27   hubertf 			p = curmode;
    315  1.27   hubertf 			p++;
    316  1.27   hubertf 			if (p->c_name == NULL)
    317  1.27   hubertf 				p = modes;
    318  1.27   hubertf 			switch_mode(p);
    319  1.27   hubertf 			allcounter=0;
    320  1.27   hubertf 		} else
    321  1.27   hubertf 			allcounter++;
    322  1.45       dsl 	}
    323  1.27   hubertf 
    324  1.45       dsl 	/* curses timeout() uses VTIME, limited to 255 1/10th secs */
    325  1.45       dsl 	ms_delay = naptime * 1000;
    326  1.45       dsl 	if (ms_delay < 25500) {
    327  1.45       dsl 		timeout(ms_delay);
    328  1.45       dsl 		skip = 0;
    329  1.45       dsl 	} else {
    330  1.45       dsl 		skip = ms_delay / 25500;
    331  1.45       dsl 		timeout(ms_delay / (skip + 1));
    332  1.45       dsl 	}
    333   1.9    scottr }
    334   1.9    scottr 
    335   1.9    scottr void
    336  1.41  christos redraw(void)
    337   1.9    scottr {
    338  1.36   mycroft 	CMDLINE = LINES - 1;
    339  1.36   mycroft 	labels();
    340  1.31       dsl 
    341  1.31       dsl 	display(0);
    342  1.41  christos 	needsredraw = 0;
    343  1.31       dsl }
    344  1.31       dsl 
    345  1.31       dsl static void
    346  1.31       dsl stop(int signo)
    347  1.31       dsl {
    348  1.31       dsl 	sigset_t set;
    349  1.31       dsl 
    350  1.31       dsl 	signal(SIGTSTP, sv_stop_handler);
    351  1.31       dsl 	/* unblock SIGTSTP */
    352  1.31       dsl 	sigemptyset(&set);
    353  1.31       dsl 	sigaddset(&set, SIGTSTP);
    354   1.9    scottr 	sigprocmask(SIG_UNBLOCK, &set, NULL);
    355  1.31       dsl 	/* stop ourselves */
    356  1.31       dsl 	kill(0, SIGTSTP);
    357  1.31       dsl 	/* must have been restarted */
    358  1.32       dsl 	signal(SIGTSTP, stop);
    359  1.41  christos 	needsredraw = signo;
    360   1.1       jtc }
    361   1.1       jtc 
    362   1.1       jtc void
    363  1.26        ad die(int signo)
    364   1.1       jtc {
    365   1.1       jtc 	move(CMDLINE, 0);
    366   1.1       jtc 	clrtoeol();
    367   1.1       jtc 	refresh();
    368   1.1       jtc 	endwin();
    369   1.1       jtc 	exit(0);
    370   1.1       jtc }
    371   1.1       jtc 
    372   1.1       jtc void
    373   1.1       jtc error(const char *fmt, ...)
    374   1.1       jtc {
    375   1.1       jtc 	va_list ap;
    376   1.1       jtc 	char buf[255];
    377   1.1       jtc 	int oy, ox;
    378  1.26        ad 
    379   1.1       jtc 	va_start(ap, fmt);
    380   1.1       jtc 
    381   1.1       jtc 	if (wnd) {
    382   1.1       jtc 		getyx(stdscr, oy, ox);
    383  1.21     jwise 		(void) vsnprintf(buf, sizeof(buf), fmt, ap);
    384   1.1       jtc 		clrtoeol();
    385   1.1       jtc 		standout();
    386   1.1       jtc 		mvaddstr(CMDLINE, 0, buf);
    387   1.1       jtc 		standend();
    388   1.1       jtc 		move(oy, ox);
    389   1.1       jtc 		refresh();
    390   1.1       jtc 	} else {
    391   1.1       jtc 		(void) vfprintf(stderr, fmt, ap);
    392   1.1       jtc 		fprintf(stderr, "\n");
    393   1.1       jtc 	}
    394   1.1       jtc 	va_end(ap);
    395   1.1       jtc }
    396   1.1       jtc 
    397   1.1       jtc void
    398  1.49       mrg clearerror(void)
    399  1.49       mrg {
    400  1.49       mrg 
    401  1.49       mrg 	error("%s", "");
    402  1.49       mrg }
    403  1.49       mrg 
    404  1.49       mrg void
    405  1.39       dsl nlisterr(struct nlist name_list[])
    406   1.1       jtc {
    407   1.1       jtc 	int i, n;
    408   1.1       jtc 
    409   1.1       jtc 	n = 0;
    410   1.1       jtc 	clear();
    411   1.1       jtc 	mvprintw(2, 10, "systat: nlist: can't find following symbols:");
    412   1.1       jtc 	for (i = 0;
    413  1.39       dsl 	    name_list[i].n_name != NULL && *name_list[i].n_name != '\0'; i++)
    414  1.39       dsl 		if (name_list[i].n_value == 0)
    415  1.39       dsl 			mvprintw(2 + ++n, 10, "%s", name_list[i].n_name);
    416   1.1       jtc 	move(CMDLINE, 0);
    417   1.1       jtc 	clrtoeol();
    418   1.1       jtc 	refresh();
    419   1.1       jtc 	endwin();
    420   1.1       jtc 	exit(1);
    421   1.1       jtc }
    422  1.55  christos 
    423  1.55  christos bool
    424  1.55  christos toofast(int *failcnt)
    425  1.55  christos {
    426  1.55  christos 	static char pigs[] = "pigs";
    427  1.55  christos 	etime = cur.cp_etime;
    428  1.55  christos 	/* < 1 ticks - sleep for a tick */
    429  1.55  christos 	/* this is often triggered by repeated SIGWINCH */
    430  1.55  christos 	if ((etime * hertz) >= 1.0)
    431  1.55  christos 		return false;
    432  1.55  christos 
    433  1.55  christos 	if ((*failcnt)++ <= MAXFAIL) {
    434  1.55  christos 		struct timespec interval = { 0, 1000000000L / hertz };
    435  1.55  christos 		while (nanosleep(&interval, &interval) == -1)
    436  1.55  christos 			continue;
    437  1.55  christos 		return true;
    438  1.55  christos 	}
    439  1.55  christos 	clear();
    440  1.55  christos 	mvprintw(2, 10, "The alternate system clock has died!");
    441  1.55  christos 	mvprintw(3, 10, "Reverting to ``pigs'' display.");
    442  1.55  christos 	move(CMDLINE, 0);
    443  1.55  christos 	refresh();
    444  1.55  christos 	failcnt = 0;
    445  1.55  christos 	sleep(5);
    446  1.55  christos 	command(pigs);
    447  1.55  christos 	return true;
    448  1.55  christos }
    449