2000-07-05 15:03:20 +04:00
|
|
|
/* $NetBSD: main.c,v 1.26 2000/07/05 11:03:22 ad Exp $ */
|
1995-01-20 11:51:48 +03:00
|
|
|
|
1995-01-20 11:30:50 +03:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1980, 1992, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-07-21 11:04:56 +04:00
|
|
|
#include <sys/cdefs.h>
|
1995-01-20 11:30:50 +03:00
|
|
|
#ifndef lint
|
1997-07-21 11:04:56 +04:00
|
|
|
__COPYRIGHT("@(#) Copyright (c) 1980, 1992, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n");
|
1995-01-20 11:51:48 +03:00
|
|
|
#if 0
|
1995-01-20 11:30:50 +03:00
|
|
|
static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/6/93";
|
1995-01-20 11:51:48 +03:00
|
|
|
#endif
|
2000-07-05 15:03:20 +04:00
|
|
|
__RCSID("$NetBSD: main.c,v 1.26 2000/07/05 11:03:22 ad Exp $");
|
1995-01-20 11:30:50 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
|
1995-03-22 18:25:56 +03:00
|
|
|
#include <err.h>
|
1997-03-04 01:12:19 +03:00
|
|
|
#include <limits.h>
|
1995-01-20 11:30:50 +03:00
|
|
|
#include <nlist.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
1995-04-29 09:54:42 +04:00
|
|
|
#include <string.h>
|
1996-05-11 03:16:30 +04:00
|
|
|
#include <unistd.h>
|
1997-07-21 11:04:56 +04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <ctype.h>
|
2000-07-05 15:03:20 +04:00
|
|
|
#include <stdarg.h>
|
1995-03-22 18:25:56 +03:00
|
|
|
|
1995-01-20 11:30:50 +03:00
|
|
|
#include "systat.h"
|
|
|
|
#include "extern.h"
|
|
|
|
|
|
|
|
static struct nlist namelist[] = {
|
|
|
|
#define X_FIRST 0
|
|
|
|
#define X_HZ 0
|
|
|
|
{ "_hz" },
|
|
|
|
#define X_STATHZ 1
|
|
|
|
{ "_stathz" },
|
|
|
|
{ "" }
|
|
|
|
};
|
|
|
|
static int dellave;
|
|
|
|
|
|
|
|
kvm_t *kd;
|
1996-05-11 03:16:30 +04:00
|
|
|
char *memf = NULL;
|
|
|
|
char *nlistf = NULL;
|
1995-01-20 11:30:50 +03:00
|
|
|
sig_t sigtstpdfl;
|
|
|
|
double avenrun[3];
|
|
|
|
int col;
|
|
|
|
int naptime = 5;
|
|
|
|
int verbose = 1; /* to report kvm read errs */
|
|
|
|
int hz, stathz;
|
|
|
|
char c;
|
|
|
|
char *namp;
|
1998-07-06 10:45:41 +04:00
|
|
|
char hostname[MAXHOSTNAMELEN + 1];
|
1995-01-20 11:30:50 +03:00
|
|
|
WINDOW *wnd;
|
|
|
|
int CMDLINE;
|
|
|
|
|
|
|
|
static WINDOW *wload; /* one line window for load average */
|
|
|
|
|
2000-07-05 15:03:20 +04:00
|
|
|
static void usage(void);
|
|
|
|
int main(int, char **);
|
1996-05-11 03:16:30 +04:00
|
|
|
|
1998-07-19 21:47:07 +04:00
|
|
|
gid_t egid; /* XXX needed by initiostat() and initkre() */
|
|
|
|
|
1996-03-21 20:56:15 +03:00
|
|
|
int
|
2000-07-05 15:03:20 +04:00
|
|
|
main(int argc, char **argv)
|
1995-01-20 11:30:50 +03:00
|
|
|
{
|
1996-05-11 03:16:30 +04:00
|
|
|
int ch;
|
1997-03-04 01:12:19 +03:00
|
|
|
char errbuf[_POSIX2_LINE_MAX];
|
1995-01-20 11:30:50 +03:00
|
|
|
|
1998-07-19 21:47:07 +04:00
|
|
|
egid = getegid();
|
1998-07-06 10:45:41 +04:00
|
|
|
(void)setegid(getgid());
|
1999-12-20 20:08:52 +03:00
|
|
|
|
2000-01-07 07:47:23 +03:00
|
|
|
while ((ch = getopt(argc, argv, "M:N:nw:")) != -1)
|
1996-05-11 03:16:30 +04:00
|
|
|
switch(ch) {
|
1996-12-13 22:26:18 +03:00
|
|
|
case 'M':
|
|
|
|
memf = optarg;
|
|
|
|
break;
|
|
|
|
case 'N':
|
|
|
|
nlistf = optarg;
|
|
|
|
break;
|
2000-01-07 07:47:23 +03:00
|
|
|
case 'n':
|
|
|
|
nflag = !nflag;
|
|
|
|
break;
|
1996-12-13 22:26:18 +03:00
|
|
|
case 'w':
|
|
|
|
if ((naptime = atoi(optarg)) <= 0)
|
|
|
|
errx(1, "interval <= 0.");
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
1996-05-11 03:16:30 +04:00
|
|
|
|
1999-12-16 20:15:48 +03:00
|
|
|
|
|
|
|
for ( ; argc > 0; argc--, argv++) {
|
|
|
|
struct mode *p;
|
1999-12-20 06:45:01 +03:00
|
|
|
int modefound = 0;
|
1999-12-16 20:15:48 +03:00
|
|
|
|
1996-05-11 03:16:30 +04:00
|
|
|
if (isdigit(argv[0][0])) {
|
|
|
|
naptime = atoi(argv[0]);
|
|
|
|
if (naptime <= 0)
|
|
|
|
naptime = 5;
|
1999-12-16 20:15:48 +03:00
|
|
|
continue;
|
1995-01-20 11:30:50 +03:00
|
|
|
}
|
1999-12-16 20:15:48 +03:00
|
|
|
|
|
|
|
for (p = modes; p->c_name ; p++) {
|
2000-04-11 05:01:26 +04:00
|
|
|
if (strstr(p->c_name, argv[0]) == p->c_name) {
|
1999-12-16 20:15:48 +03:00
|
|
|
curmode = p;
|
1999-12-20 06:45:01 +03:00
|
|
|
modefound++;
|
1999-12-16 20:15:48 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-12-20 06:45:01 +03:00
|
|
|
if (!modefound)
|
1999-12-16 20:15:48 +03:00
|
|
|
error("%s: Unknown command.", argv[0]);
|
1995-01-20 11:30:50 +03:00
|
|
|
}
|
1998-07-06 10:45:41 +04:00
|
|
|
|
1998-07-19 21:47:07 +04:00
|
|
|
/*
|
|
|
|
* Discard setgid privileges. If not the running kernel, we toss
|
|
|
|
* them away totally so that bad guys can't print interesting stuff
|
|
|
|
* from kernel memory, otherwise switch back to kmem for the
|
|
|
|
* duration of the kvm_openfiles() call.
|
|
|
|
*/
|
|
|
|
if (nlistf != NULL || memf != NULL)
|
|
|
|
(void)setgid(getgid());
|
|
|
|
else
|
|
|
|
(void)setegid(egid);
|
|
|
|
|
1996-05-11 03:16:30 +04:00
|
|
|
kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
|
1995-01-20 11:30:50 +03:00
|
|
|
if (kd == NULL) {
|
|
|
|
error("%s", errbuf);
|
|
|
|
exit(1);
|
|
|
|
}
|
1998-07-19 21:47:07 +04:00
|
|
|
|
|
|
|
/* Get rid of privs for now. */
|
1998-07-06 10:45:41 +04:00
|
|
|
if (nlistf == NULL && memf == NULL)
|
1998-07-19 21:47:07 +04:00
|
|
|
(void)setegid(getgid());
|
|
|
|
|
1995-01-20 11:30:50 +03:00
|
|
|
if (kvm_nlist(kd, namelist)) {
|
|
|
|
nlisterr(namelist);
|
|
|
|
exit(1);
|
|
|
|
}
|
1995-03-22 18:25:56 +03:00
|
|
|
if (namelist[X_FIRST].n_type == 0)
|
|
|
|
errx(1, "couldn't read namelist");
|
1995-01-20 11:30:50 +03:00
|
|
|
signal(SIGINT, die);
|
|
|
|
signal(SIGQUIT, die);
|
|
|
|
signal(SIGTERM, die);
|
1996-12-13 22:26:18 +03:00
|
|
|
signal(SIGWINCH, redraw);
|
1995-01-20 11:30:50 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize display. Load average appears in a one line
|
|
|
|
* window of its own. Current command's display appears in
|
|
|
|
* an overlapping sub-window of stdscr configured by the display
|
|
|
|
* routines to minimize update work by curses.
|
|
|
|
*/
|
1995-05-06 10:25:07 +04:00
|
|
|
if (initscr() == NULL)
|
|
|
|
{
|
|
|
|
warnx("couldn't initialize screen");
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
1995-01-20 11:30:50 +03:00
|
|
|
CMDLINE = LINES - 1;
|
1999-12-16 07:02:22 +03:00
|
|
|
wnd = (*curmode->c_open)();
|
1995-01-20 11:30:50 +03:00
|
|
|
if (wnd == NULL) {
|
1995-03-22 18:25:56 +03:00
|
|
|
warnx("couldn't initialize display");
|
1995-01-20 11:30:50 +03:00
|
|
|
die(0);
|
|
|
|
}
|
|
|
|
wload = newwin(1, 0, 3, 20);
|
|
|
|
if (wload == NULL) {
|
1995-03-22 18:25:56 +03:00
|
|
|
warnx("couldn't set up load average window");
|
1995-01-20 11:30:50 +03:00
|
|
|
die(0);
|
|
|
|
}
|
|
|
|
gethostname(hostname, sizeof (hostname));
|
1998-07-06 10:45:41 +04:00
|
|
|
hostname[sizeof(hostname) - 1] = '\0';
|
1999-04-25 03:36:36 +04:00
|
|
|
NREAD(X_HZ, &hz, sizeof hz);
|
|
|
|
NREAD(X_STATHZ, &stathz, sizeof stathz);
|
1999-12-16 07:02:22 +03:00
|
|
|
(*curmode->c_init)();
|
|
|
|
curmode->c_flags |= CF_INIT;
|
1995-01-20 11:30:50 +03:00
|
|
|
labels();
|
|
|
|
|
|
|
|
dellave = 0.0;
|
|
|
|
|
|
|
|
signal(SIGALRM, display);
|
|
|
|
display(0);
|
|
|
|
noecho();
|
|
|
|
crmode();
|
|
|
|
keyboard();
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
|
1996-05-11 03:16:30 +04:00
|
|
|
static void
|
2000-07-05 15:03:20 +04:00
|
|
|
usage(void)
|
1996-05-11 03:16:30 +04:00
|
|
|
{
|
2000-01-07 07:47:23 +03:00
|
|
|
fprintf(stderr, "usage: systat [-n] [-M core] [-N system] [-w wait] "
|
1999-11-11 06:06:04 +03:00
|
|
|
"[display] [refresh-interval]\n");
|
1996-05-11 03:16:30 +04:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1995-01-20 11:30:50 +03:00
|
|
|
void
|
2000-07-05 15:03:20 +04:00
|
|
|
labels(void)
|
1995-01-20 11:30:50 +03:00
|
|
|
{
|
1999-12-16 07:02:22 +03:00
|
|
|
if (curmode->c_flags & CF_LOADAV) {
|
1995-01-20 11:30:50 +03:00
|
|
|
mvaddstr(2, 20,
|
|
|
|
"/0 /1 /2 /3 /4 /5 /6 /7 /8 /9 /10");
|
|
|
|
mvaddstr(3, 5, "Load Average");
|
|
|
|
}
|
1999-12-16 07:02:22 +03:00
|
|
|
(*curmode->c_label)();
|
1995-01-20 11:30:50 +03:00
|
|
|
#ifdef notdef
|
|
|
|
mvprintw(21, 25, "CPU usage on %s", hostname);
|
|
|
|
#endif
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-07-05 15:03:20 +04:00
|
|
|
display(int signo)
|
1995-01-20 11:30:50 +03:00
|
|
|
{
|
2000-06-04 22:29:13 +04:00
|
|
|
int j;
|
1999-12-21 00:04:41 +03:00
|
|
|
sigset_t set;
|
|
|
|
|
|
|
|
sigemptyset(&set);
|
|
|
|
sigaddset(&set, SIGALRM);
|
|
|
|
sigprocmask(SIG_BLOCK, &set, NULL);
|
1995-01-20 11:30:50 +03:00
|
|
|
|
|
|
|
/* Get the load average over the last minute. */
|
1998-07-12 09:59:00 +04:00
|
|
|
(void)getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0]));
|
1999-12-16 07:02:22 +03:00
|
|
|
(*curmode->c_fetch)();
|
|
|
|
if (curmode->c_flags & CF_LOADAV) {
|
1995-01-20 11:30:50 +03:00
|
|
|
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];
|
2000-06-04 22:29:13 +04:00
|
|
|
wmove(wload, 0, 0);
|
|
|
|
wclrtoeol(wload);
|
|
|
|
whline(wload, c, (j > 50) ? 50 : j);
|
1995-01-20 11:30:50 +03:00
|
|
|
if (j > 50)
|
|
|
|
wprintw(wload, " %4.1f", avenrun[0]);
|
|
|
|
}
|
1999-12-16 07:02:22 +03:00
|
|
|
(*curmode->c_refresh)();
|
|
|
|
if (curmode->c_flags & CF_LOADAV)
|
1995-01-20 11:30:50 +03:00
|
|
|
wrefresh(wload);
|
|
|
|
wrefresh(wnd);
|
|
|
|
move(CMDLINE, col);
|
|
|
|
refresh();
|
1999-12-21 00:04:41 +03:00
|
|
|
sigprocmask(SIG_UNBLOCK, &set, NULL);
|
1995-01-20 11:30:50 +03:00
|
|
|
alarm(naptime);
|
|
|
|
}
|
|
|
|
|
1996-12-13 22:26:18 +03:00
|
|
|
void
|
2000-07-05 15:03:20 +04:00
|
|
|
redraw(int signo)
|
1996-12-13 22:26:18 +03:00
|
|
|
{
|
|
|
|
sigset_t set;
|
|
|
|
|
|
|
|
sigemptyset(&set);
|
|
|
|
sigaddset(&set, SIGALRM);
|
|
|
|
sigprocmask(SIG_BLOCK, &set, NULL);
|
|
|
|
wrefresh(curscr);
|
|
|
|
refresh();
|
|
|
|
sigprocmask(SIG_UNBLOCK, &set, NULL);
|
1995-01-20 11:30:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-07-05 15:03:20 +04:00
|
|
|
die(int signo)
|
1995-01-20 11:30:50 +03:00
|
|
|
{
|
|
|
|
move(CMDLINE, 0);
|
|
|
|
clrtoeol();
|
|
|
|
refresh();
|
|
|
|
endwin();
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
error(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
char buf[255];
|
|
|
|
int oy, ox;
|
2000-07-05 15:03:20 +04:00
|
|
|
|
1995-01-20 11:30:50 +03:00
|
|
|
va_start(ap, fmt);
|
|
|
|
|
|
|
|
if (wnd) {
|
|
|
|
getyx(stdscr, oy, ox);
|
1999-12-20 20:08:52 +03:00
|
|
|
(void) vsnprintf(buf, sizeof(buf), fmt, ap);
|
1995-01-20 11:30:50 +03:00
|
|
|
clrtoeol();
|
|
|
|
standout();
|
|
|
|
mvaddstr(CMDLINE, 0, buf);
|
|
|
|
standend();
|
|
|
|
move(oy, ox);
|
|
|
|
refresh();
|
|
|
|
} else {
|
|
|
|
(void) vfprintf(stderr, fmt, ap);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-07-05 15:03:20 +04:00
|
|
|
nlisterr(struct nlist namelist[])
|
1995-01-20 11:30:50 +03:00
|
|
|
{
|
|
|
|
int i, n;
|
|
|
|
|
|
|
|
n = 0;
|
|
|
|
clear();
|
|
|
|
mvprintw(2, 10, "systat: nlist: can't find following symbols:");
|
|
|
|
for (i = 0;
|
|
|
|
namelist[i].n_name != NULL && *namelist[i].n_name != '\0'; i++)
|
|
|
|
if (namelist[i].n_value == 0)
|
|
|
|
mvprintw(2 + ++n, 10, "%s", namelist[i].n_name);
|
|
|
|
move(CMDLINE, 0);
|
|
|
|
clrtoeol();
|
|
|
|
refresh();
|
|
|
|
endwin();
|
|
|
|
exit(1);
|
|
|
|
}
|