[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.31

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