[BACK]Return to main.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / systat

Annotation of src/usr.bin/systat/main.c, Revision 1.12

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