#include <sys/cdefs.h>
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1980, 1992, 1993\
The Regents of the University of California. All rights reserved.");
#if 0
static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/6/93";
#endif
__RCSID("$NetBSD: main.c,v 1.58 2024/06/16 22:44:01 kre Exp $");
#endif
#include <sys/param.h>
#include <sys/sysctl.h>
#include <sys/ioctl.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <limits.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <termios.h>
#include "systat.h"
#include "extern.h"
#include "drvstats.h"
static double dellave;
kvm_t *kd;
char *memf = NULL;
char *nlistf = NULL;
sig_t sigtstpdfl;
double avenrun[3];
int col;
double naptime = 1;
int verbose = 1;
int hz, stathz, maxslp;
char c;
char *namp;
char hostname[MAXHOSTNAMELEN + 1];
WINDOW *wnd;
int CMDLINE;
int turns = 2;
int allflag;
int allcounter;
sig_atomic_t needsredraw = 0;
float hertz;
double etime;
bool showzero = false;
static WINDOW *wload;
static void (*sv_stop_handler)(int);
static void stop(int);
__dead static void usage(void);
gid_t egid;
int
main(int argc, char **argv)
{
int ch;
char errbuf[_POSIX2_LINE_MAX];
const char *all;
struct clockinfo clk;
size_t len;
int bflag = 0;
all = "all";
egid = getegid();
(void)setegid(getgid());
while ((ch = getopt(argc, argv, "M:N:bnw:t:z")) != -1)
switch(ch) {
case 'M':
memf = optarg;
break;
case 'N':
nlistf = optarg;
break;
case 'b':
bflag = !bflag;
break;
case 'n':
nflag = !nflag;
break;
case 't':
if ((turns = atoi(optarg)) <= 0)
errx(EXIT_FAILURE, "turns <= 0.");
break;
case 'w':
if ((naptime = strtod(optarg, NULL)) <= 0)
errx(EXIT_FAILURE, "interval <= 0.");
break;
case 'z':
showzero = true;
break;
case '?':
default:
usage();
}
argc -= optind;
argv += optind;
for ( ; argc > 0; argc--, argv++) {
struct mode *p;
int modefound = 0;
if (isdigit((unsigned char)argv[0][0])) {
naptime = strtod(argv[0], NULL);
if (naptime <= 0)
naptime = 5;
continue;
}
for (p = modes; p->c_name ; p++) {
if (strstr(p->c_name, argv[0]) == p->c_name) {
curmode = p;
modefound++;
break;
}
if (strstr(all, argv[0]) == all) {
allcounter=0;
allflag=1;
}
}
if (!modefound && !allflag)
error("%s: Unknown command.", argv[0]);
}
if (nlistf != NULL || memf != NULL)
(void)setgid(getgid());
else
(void)setegid(egid);
kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
if (kd == NULL)
errx(EXIT_FAILURE, "%s", errbuf);
if (nlistf == NULL && memf == NULL)
(void)setegid(getgid());
signal(SIGINT, die);
signal(SIGQUIT, die);
signal(SIGTERM, die);
sv_stop_handler = signal(SIGTSTP, stop);
if (initscr() == NULL)
errx(EXIT_FAILURE, "couldn't initialize screen");
CMDLINE = LINES - 1;
wnd = (*curmode->c_open)();
if (wnd == NULL) {
move(CMDLINE, 0);
clrtoeol();
refresh();
endwin();
warnx("couldn't initialize display");
die(0);
}
wload = newwin(1, 0, 3, 20);
if (wload == NULL) {
move(CMDLINE, 0);
clrtoeol();
refresh();
endwin();
warnx("couldn't set up load average window");
die(0);
}
gethostname(hostname, sizeof (hostname));
hostname[sizeof(hostname) - 1] = '\0';
len = sizeof(clk);
if (sysctlbyname("kern.clockrate", &clk, &len, NULL, 0))
error("can't get \"kern.clockrate\": %s", strerror(errno));
hz = clk.hz;
stathz = clk.stathz;
len = sizeof(maxslp);
if (sysctlbyname("vm.maxslp", &maxslp, &len, NULL, 0))
error("can't get \"vm.maxslp\": %s", strerror(errno));
(*curmode->c_init)();
curmode->c_flags |= CF_INIT;
labels();
dellave = 0.0;
display(0);
if (!bflag) {
noecho();
cbreak();
keyboard();
} else
die(0);
}
static void
usage(void)
{
fprintf(stderr, "usage: %s [-bnz] [-M core] [-N system] [-w wait] "
"[-t turns]\n\t\t[display] [refresh-interval]\n", getprogname());
exit(EXIT_FAILURE);
}
void
labels(void)
{
if (curmode->c_flags & CF_LOADAV) {
mvaddstr(2, 20,
"/0 /1 /2 /3 /4 /5 /6 /7 /8 /9 /10");
mvaddstr(3, 5, "Load Average");
}
(*curmode->c_label)();
#ifdef notdef
mvprintw(21, 25, "CPU usage on %s", hostname);
#endif
refresh();
}
void
display(int signo)
{
static int skip;
int j;
struct mode *p;
int ms_delay;
if (signo == SIGALRM && skip-- > 0)
return;
(void)getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0]));
(*curmode->c_fetch)();
if (curmode->c_flags & CF_LOADAV) {
j = 5.0*avenrun[0] + 0.5;
dellave -= avenrun[0];
if (dellave >= 0.0)
c = '<';
else {
c = '>';
dellave = -dellave;
}
if (dellave < 0.1)
c = '|';
dellave = avenrun[0];
wmove(wload, 0, 0);
wclrtoeol(wload);
whline(wload, c, (j > 50) ? 50 : j);
if (j > 50)
wprintw(wload, " %4.1f", avenrun[0]);
}
(*curmode->c_refresh)();
if (curmode->c_flags & CF_LOADAV)
wrefresh(wload);
wrefresh(wnd);
move(CMDLINE, col);
refresh();
if (allflag && signo==SIGALRM) {
if (allcounter >= turns){
p = curmode;
p++;
if (p->c_name == NULL)
p = modes;
switch_mode(p);
allcounter=0;
} else
allcounter++;
}
ms_delay = naptime * 1000;
if (ms_delay < 25500) {
timeout(ms_delay);
skip = 0;
} else {
skip = ms_delay / 25500;
timeout(ms_delay / (skip + 1));
}
}
void
redraw(void)
{
CMDLINE = LINES - 1;
labels();
display(0);
needsredraw = 0;
}
static void
stop(int signo)
{
sigset_t set;
signal(SIGTSTP, sv_stop_handler);
sigemptyset(&set);
sigaddset(&set, SIGTSTP);
sigprocmask(SIG_UNBLOCK, &set, NULL);
kill(0, SIGTSTP);
signal(SIGTSTP, stop);
needsredraw = signo;
}
void
die(int signo)
{
move(CMDLINE, 0);
clrtoeol();
refresh();
endwin();
exit(EXIT_SUCCESS);
}
void
error(const char *fmt, ...)
{
va_list ap;
char buf[255];
int oy, ox;
va_start(ap, fmt);
if (wnd) {
getyx(stdscr, oy, ox);
(void) vsnprintf(buf, sizeof(buf), fmt, ap);
clrtoeol();
standout();
mvaddstr(CMDLINE, 0, buf);
standend();
move(oy, ox);
refresh();
} else {
(void) vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
}
va_end(ap);
}
void
clearerror(void)
{
error("%s", "");
}
void
nlisterr(struct nlist name_list[])
{
int i, n;
n = 0;
clear();
mvprintw(2, 10, "systat: nlist: can't find following symbols:");
for (i = 0;
name_list[i].n_name != NULL && *name_list[i].n_name != '\0'; i++)
if (name_list[i].n_value == 0)
mvprintw(2 + ++n, 10, "%s", name_list[i].n_name);
move(CMDLINE, 0);
clrtoeol();
refresh();
endwin();
exit(EXIT_FAILURE);
}
bool
toofast(int *failcnt)
{
static char pigs[] = "pigs";
etime = cur.cp_etime;
if ((etime * hertz) >= 1.0)
return false;
if ((*failcnt)++ <= MAXFAIL) {
struct timespec interval = { 0, 1000000000L / hertz };
while (nanosleep(&interval, &interval) == -1)
continue;
return true;
}
clear();
mvprintw(2, 10, "The alternate system clock has died!");
mvprintw(3, 10, "Reverting to ``pigs'' display.");
move(CMDLINE, 0);
refresh();
failcnt = 0;
sleep(5);
command(pigs);
return true;
}