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

Annotation of src/usr.bin/top/top.c, Revision 1.26

1.26    ! millert     1: /*     $OpenBSD: top.c,v 1.25 2003/06/18 08:42:17 deraadt Exp $        */
1.1       downsj      2:
                      3: /*
                      4:  *  Top users/processes display for Unix
                      5:  *  Version 3
                      6:  *
1.18      deraadt     7:  * Copyright (c) 1984, 1989, William LeFebvre, Rice University
                      8:  * Copyright (c) 1989, 1990, 1992, William LeFebvre, Northwestern University
1.1       downsj      9:  *
1.18      deraadt    10:  * Redistribution and use in source and binary forms, with or without
                     11:  * modification, are permitted provided that the following conditions
                     12:  * are met:
                     13:  * 1. Redistributions of source code must retain the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer.
                     15:  * 2. Redistributions in binary form must reproduce the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer in the
                     17:  *    documentation and/or other materials provided with the distribution.
                     18:  *
                     19:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     20:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     21:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     22:  * IN NO EVENT SHALL THE AUTHOR OR HIS EMPLOYER BE LIABLE FOR ANY DIRECT, INDIRECT,
                     23:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     24:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     25:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     26:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     27:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     28:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.1       downsj     29:  */
                     30:
1.20      deraadt    31: const char      copyright[] = "Copyright (c) 1984 through 1996, William LeFebvre";
1.1       downsj     32:
1.2       downsj     33: #include <sys/types.h>
1.23      millert    34: #include <sys/time.h>
                     35: #include <err.h>
                     36: #include <errno.h>
1.2       downsj     37: #include <stdio.h>
                     38: #include <ctype.h>
1.1       downsj     39: #include <signal.h>
1.2       downsj     40: #include <string.h>
                     41: #include <stdlib.h>
                     42: #include <unistd.h>
1.1       downsj     43:
                     44: /* includes specific to top */
                     45: #include "display.h"           /* interface to display package */
                     46: #include "screen.h"            /* interface to screen package */
                     47: #include "top.h"
                     48: #include "top.local.h"
                     49: #include "boolean.h"
                     50: #include "machine.h"
                     51: #include "utils.h"
                     52:
                     53: /* Size of the stdio buffer given to stdout */
1.20      deraadt    54: #define BUFFERSIZE     2048
1.1       downsj     55:
                     56: /* The buffer that stdio will use */
1.20      deraadt    57: char            stdoutbuf[BUFFERSIZE];
1.1       downsj     58:
1.20      deraadt    59: extern int      overstrike;
1.1       downsj     60:
                     61: /* signal handling routines */
1.20      deraadt    62: static void     leave(int);
                     63: static void     onalrm(int);
                     64: static void     tstop(int);
                     65: static void     winch(int);
1.1       downsj     66:
1.25      deraadt    67: volatile sig_atomic_t leaveflag, tstopflag, winchflag;
1.7       deraadt    68:
1.20      deraadt    69: static void     reset_display(void);
                     70: int            rundisplay(void);
1.1       downsj     71:
1.20      deraadt    72: static int      max_topn;      /* maximum displayable processes */
1.1       downsj     73:
1.20      deraadt    74: extern int      (*proc_compares[])(const void *, const void *);
                     75: int order_index;
1.1       downsj     76:
                     77: /* pointers to display routines */
1.20      deraadt    78: void            (*d_loadave) () = i_loadave;
                     79: void            (*d_procstates) () = i_procstates;
                     80: void            (*d_cpustates) () = i_cpustates;
                     81: void            (*d_memory) () = i_memory;
                     82: void            (*d_message) () = i_message;
                     83: void            (*d_header) () = i_header;
                     84: void            (*d_process) () = i_process;
                     85:
                     86: int displays = 0;      /* indicates unspecified */
                     87: char do_unames = Yes;
                     88: struct process_select ps;
                     89: char dostates = No;
                     90: char interactive = Maybe;
                     91: char warnings = 0;
                     92: double delay = Default_DELAY;
                     93: char *order_name = NULL;
                     94: int topn = Default_TOPN;
1.24      deraadt    95: int no_command = Yes;
1.1       downsj     96:
                     97: #if Default_TOPN == Infinity
1.20      deraadt    98: char topn_specified = No;
1.1       downsj     99: #endif
                    100:
1.20      deraadt   101: /*
                    102:  * these defines enumerate the "strchr"s of the commands in
                    103:  * command_chars
                    104:  */
1.1       downsj    105: #define CMD_redraw     0
                    106: #define CMD_update     1
                    107: #define CMD_quit       2
                    108: #define CMD_help1      3
                    109: #define CMD_help2      4
1.20      deraadt   110: #define CMD_OSLIMIT    4       /* terminals with OS can only handle commands */
                    111: #define CMD_errors     5       /* less than or equal to CMD_OSLIMIT       */
1.1       downsj    112: #define CMD_number1    6
                    113: #define CMD_number2    7
                    114: #define CMD_delay      8
                    115: #define CMD_displays   9
                    116: #define CMD_kill       10
                    117: #define CMD_renice     11
                    118: #define CMD_idletog     12
                    119: #define CMD_idletog2    13
                    120: #define CMD_user       14
1.12      fgsch     121: #define CMD_system     15
                    122: #define CMD_order       16
1.1       downsj    123:
1.20      deraadt   124: void
                    125: usage(void)
                    126: {
1.22      deraadt   127:        extern char *__progname;
                    128:
1.20      deraadt   129:        fprintf(stderr,
1.22      deraadt   130:            "usage: %s [-ISbinqu] [-d x] [-s x] [-o field] [-U username] [number]\n",
                    131:            __progname);
1.20      deraadt   132: }
                    133:
                    134: void
                    135: parseargs(int ac, char **av)
                    136: {
                    137:        char *endp;
                    138:        int i;
                    139:
                    140:        while ((i = getopt(ac, av, "SIbinqus:d:U:o:")) != -1) {
                    141:                switch (i) {
                    142:                case 'u':       /* toggle uid/username display */
                    143:                        do_unames = !do_unames;
                    144:                        break;
                    145:
                    146:                case 'U':       /* display only username's processes */
1.21      millert   147:                        if ((ps.uid = userid(optarg)) == (uid_t)-1) {
1.20      deraadt   148:                                fprintf(stderr, "%s: unknown user\n", optarg);
                    149:                                exit(1);
                    150:                        }
                    151:                        break;
1.1       downsj    152:
1.20      deraadt   153:                case 'S':       /* show system processes */
                    154:                        ps.system = !ps.system;
                    155:                        break;
                    156:
                    157:                case 'I':       /* show idle processes */
                    158:                        ps.idle = !ps.idle;
                    159:                        break;
                    160:
                    161:                case 'i':       /* go interactive regardless */
                    162:                        interactive = Yes;
                    163:                        break;
                    164:
                    165:                case 'n':       /* batch, or non-interactive */
                    166:                case 'b':
                    167:                        interactive = No;
                    168:                        break;
                    169:
                    170:                case 'd':       /* number of displays to show */
                    171:                        if ((i = atoiwi(optarg)) != Invalid && i != 0) {
                    172:                                displays = i;
                    173:                                break;
                    174:                        }
1.22      deraadt   175:                        warnx("warning: display count should be positive "
                    176:                            "-- option ignored");
1.20      deraadt   177:                        warnings++;
                    178:                        break;
                    179:
                    180:                case 's':
                    181:                        delay = strtod(optarg, &endp);
                    182:
                    183:                        if (delay > 0 && delay <= 1000000 && *endp == '\0')
                    184:                                break;
                    185:
1.22      deraadt   186:                        warnx("warning: delay should be a non-negative number"
                    187:                            " -- using default");
1.20      deraadt   188:                        delay = Default_DELAY;
                    189:                        warnings++;
                    190:                        break;
                    191:
                    192:                case 'q':       /* be quick about it */
                    193:                        /* only allow this if user is really root */
                    194:                        if (getuid() == 0) {
                    195:                                /* be very un-nice! */
                    196:                                (void) nice(-20);
                    197:                                break;
                    198:                        }
1.22      deraadt   199:                        warnx("warning: `-q' option can only be used by root");
1.20      deraadt   200:                        warnings++;
                    201:                        break;
                    202:
                    203:                case 'o':       /* select sort order */
                    204:                        order_name = optarg;
                    205:                        break;
                    206:
                    207:                default:
                    208:                        usage();
                    209:                        exit(1);
1.1       downsj    210:                }
1.20      deraadt   211:        }
1.1       downsj    212:
1.20      deraadt   213:        /* get count of top processes to display (if any) */
                    214:        if (optind < ac) {
                    215:                if ((topn = atoiwi(av[optind])) == Invalid) {
1.22      deraadt   216:                        warnx("warning: process display count should "
                    217:                            "be non-negative -- using default");
1.20      deraadt   218:                        warnings++;
1.1       downsj    219:                }
1.20      deraadt   220: #if Default_TOPN == Infinity
1.1       downsj    221:                else
1.20      deraadt   222:                        topn_specified = Yes;
                    223: #endif
                    224:        }
                    225: }
1.1       downsj    226:
1.20      deraadt   227: struct system_info system_info;
                    228: struct statics  statics;
1.1       downsj    229:
1.20      deraadt   230: int
                    231: main(int argc, char *argv[])
                    232: {
                    233:        char *uname_field = "USERNAME", *header_text, *env_top;
                    234:        char *(*get_userid)() = username, **preset_argv, **av;
                    235:        int preset_argc = 0, ac, active_procs, i;
                    236:        sigset_t mask, oldmask;
                    237:        time_t curr_time;
                    238:        caddr_t processes;
1.1       downsj    239:
1.20      deraadt   240:        /* set the buffer for stdout */
                    241: #ifdef DEBUG
                    242:        setbuffer(stdout, NULL, 0);
1.1       downsj    243: #else
1.20      deraadt   244:        setbuffer(stdout, stdoutbuf, sizeof stdoutbuf);
1.1       downsj    245: #endif
                    246:
1.20      deraadt   247:        /* initialize some selection options */
                    248:        ps.idle = Yes;
                    249:        ps.system = No;
1.21      millert   250:        ps.uid = (uid_t)-1;
1.20      deraadt   251:        ps.command = NULL;
                    252:
                    253:        /* get preset options from the environment */
                    254:        if ((env_top = getenv("TOP")) != NULL) {
                    255:                av = preset_argv = argparse(env_top, &preset_argc);
                    256:                ac = preset_argc;
                    257:
                    258:                /*
                    259:                 * set the dummy argument to an explanatory message, in case
                    260:                 * getopt encounters a bad argument
                    261:                 */
                    262:                preset_argv[0] = "while processing environment";
                    263:        }
                    264:        /* process options */
                    265:        do {
                    266:                /*
                    267:                 * if we're done doing the presets, then process the real
                    268:                 * arguments
                    269:                 */
                    270:                if (preset_argc == 0) {
                    271:                        ac = argc;
                    272:                        av = argv;
                    273:                        optind = 1;
                    274:                }
                    275:                parseargs(ac, av);
                    276:                i = preset_argc;
                    277:                preset_argc = 0;
                    278:        } while (i != 0);
                    279:
                    280:        /* set constants for username/uid display correctly */
                    281:        if (!do_unames) {
                    282:                uname_field = "   UID  ";
1.26    ! millert   283:                get_userid = format_uid;
1.20      deraadt   284:        }
                    285:        /* initialize the kernel memory interface */
                    286:        if (machine_init(&statics) == -1)
1.1       downsj    287:                exit(1);
1.20      deraadt   288:
                    289:        /* determine sorting order index, if necessary */
                    290:        if (order_name != NULL) {
                    291:                if ((order_index = string_index(order_name,
                    292:                    statics.order_names)) == -1) {
                    293:                        char **pp;
                    294:
1.22      deraadt   295:                        warnx("'%s' is not a recognized sorting order",
                    296:                            order_name);
1.20      deraadt   297:                        fprintf(stderr, "\tTry one of these:");
                    298:                        pp = statics.order_names;
                    299:                        while (*pp != NULL)
                    300:                                fprintf(stderr, " %s", *pp++);
                    301:                        fputc('\n', stderr);
                    302:                        exit(1);
                    303:                }
1.1       downsj    304:        }
                    305:
1.20      deraadt   306:        /* initialize termcap */
                    307:        init_termcap(interactive);
                    308:
                    309:        /* get the string to use for the process area header */
                    310:        header_text = format_header(uname_field);
                    311:
                    312:        /* initialize display interface */
                    313:        if ((max_topn = display_init(&statics)) == -1) {
1.22      deraadt   314:                warnx("can't allocate sufficient memory");
1.20      deraadt   315:                exit(4);
                    316:        }
                    317:        /* print warning if user requested more processes than we can display */
                    318:        if (topn > max_topn) {
1.22      deraadt   319:                warnx("warning: this terminal can only display %d processes",
                    320:                    max_topn);
1.1       downsj    321:                warnings++;
1.20      deraadt   322:        }
                    323:        /* adjust for topn == Infinity */
                    324:        if (topn == Infinity) {
                    325:                /*
                    326:                 *  For smart terminals, infinity really means everything that can
                    327:                 *  be displayed, or Largest.
                    328:                 *  On dumb terminals, infinity means every process in the system!
                    329:                 *  We only really want to do that if it was explicitly specified.
                    330:                 *  This is always the case when "Default_TOPN != Infinity".  But if
                    331:                 *  topn wasn't explicitly specified and we are on a dumb terminal
                    332:                 *  and the default is Infinity, then (and only then) we use
                    333:                 *  "Nominal_TOPN" instead.
                    334:                 */
1.1       downsj    335: #if Default_TOPN == Infinity
1.20      deraadt   336:                topn = smart_terminal ? Largest :
                    337:                    (topn_specified ? Largest : Nominal_TOPN);
                    338: #else
                    339:                topn = Largest;
1.1       downsj    340: #endif
                    341:        }
1.20      deraadt   342:        /* set header display accordingly */
                    343:        display_header(topn > 0);
1.1       downsj    344:
1.20      deraadt   345:        /* determine interactive state */
                    346:        if (interactive == Maybe)
                    347:                interactive = smart_terminal;
1.1       downsj    348:
1.20      deraadt   349:        /* if # of displays not specified, fill it in */
                    350:        if (displays == 0)
                    351:                displays = smart_terminal ? Infinity : 1;
1.1       downsj    352:
                    353:        /*
1.20      deraadt   354:         * block interrupt signals while setting up the screen and the
                    355:         * handlers
1.1       downsj    356:         */
1.20      deraadt   357:        sigemptyset(&mask);
                    358:        sigaddset(&mask, SIGINT);
                    359:        sigaddset(&mask, SIGQUIT);
                    360:        sigaddset(&mask, SIGTSTP);
                    361:        sigprocmask(SIG_BLOCK, &mask, &oldmask);
                    362:        init_screen();
                    363:        (void) signal(SIGINT, leave);
                    364:        (void) signal(SIGQUIT, leave);
                    365:        (void) signal(SIGTSTP, tstop);
                    366:        (void) signal(SIGWINCH, winch);
                    367:        sigprocmask(SIG_SETMASK, &oldmask, NULL);
                    368:        if (warnings) {
                    369:                fputs("....", stderr);
                    370:                fflush(stderr); /* why must I do this? */
                    371:                sleep((unsigned) (3 * warnings));
                    372:                fputc('\n', stderr);
                    373:        }
1.7       deraadt   374: restart:
1.1       downsj    375:
1.20      deraadt   376:        /*
                    377:         *  main loop -- repeat while display count is positive or while it
                    378:         *              indicates infinity (by being -1)
                    379:         */
                    380:        while ((displays == -1) || (displays-- > 0)) {
                    381:                /* get the current stats */
                    382:                get_system_info(&system_info);
                    383:
                    384:                /* get the current set of processes */
                    385:                processes = get_process_info(&system_info, &ps,
                    386:                    proc_compares[order_index]);
                    387:
                    388:                /* display the load averages */
                    389:                (*d_loadave)(system_info.last_pid, system_info.load_avg);
                    390:
                    391:                /* display the current time */
                    392:                /* this method of getting the time SHOULD be fairly portable */
                    393:                time(&curr_time);
                    394:                i_timeofday(&curr_time);
                    395:
                    396:                /* display process state breakdown */
                    397:                (*d_procstates)(system_info.p_total, system_info.procstates);
                    398:
                    399:                /* display the cpu state percentage breakdown */
                    400:                if (dostates) { /* but not the first time */
                    401:                        (*d_cpustates) (system_info.cpustates);
                    402:                } else {
                    403:                        /* we'll do it next time */
                    404:                        if (smart_terminal)
                    405:                                z_cpustates();
                    406:                        else {
                    407:                                if (putchar('\n') == EOF)
                    408:                                        exit(1);
                    409:                        }
                    410:                        dostates = Yes;
                    411:                }
1.1       downsj    412:
1.20      deraadt   413:                /* display memory stats */
                    414:                (*d_memory) (system_info.memory);
1.1       downsj    415:
1.20      deraadt   416:                /* handle message area */
                    417:                (*d_message) ();
1.1       downsj    418:
1.20      deraadt   419:                /* update the header area */
                    420:                (*d_header) (header_text);
                    421:
                    422:                if (topn > 0) {
                    423:                        /* determine number of processes to actually display */
                    424:                        /*
                    425:                         * this number will be the smallest of:  active
                    426:                         * processes, number user requested, number current
                    427:                         * screen accommodates
                    428:                         */
                    429:                        active_procs = system_info.p_active;
                    430:                        if (active_procs > topn)
                    431:                                active_procs = topn;
                    432:                        if (active_procs > max_topn)
                    433:                                active_procs = max_topn;
                    434:                        /* now show the top "n" processes. */
                    435:                        for (i = 0; i < active_procs; i++)
                    436:                                (*d_process)(i, format_next_process(processes,
                    437:                                    get_userid));
                    438:                } else
                    439:                        i = 0;
                    440:
                    441:                /* do end-screen processing */
                    442:                u_endscreen(i);
                    443:
                    444:                /* now, flush the output buffer */
                    445:                fflush(stdout);
                    446:
                    447:                /* only do the rest if we have more displays to show */
                    448:                if (displays) {
                    449:                        /* switch out for new display on smart terminals */
                    450:                        if (smart_terminal) {
                    451:                                if (overstrike) {
                    452:                                        reset_display();
                    453:                                } else {
                    454:                                        d_loadave = u_loadave;
                    455:                                        d_procstates = u_procstates;
                    456:                                        d_cpustates = u_cpustates;
                    457:                                        d_memory = u_memory;
                    458:                                        d_message = u_message;
                    459:                                        d_header = u_header;
                    460:                                        d_process = u_process;
                    461:                                }
                    462:                        }
                    463:                        no_command = Yes;
                    464:                        if (!interactive) {
                    465:                                /* set up alarm */
                    466:                                (void) signal(SIGALRM, onalrm);
                    467:                                (void) alarm((unsigned) delay);
                    468:
                    469:                                /* wait for the rest of it .... */
                    470:                                pause();
                    471:                        } else {
                    472:                                while (no_command)
                    473:                                        if (rundisplay())
                    474:                                                goto restart;
                    475:                        }
                    476:                }
                    477:        }
1.1       downsj    478:
1.20      deraadt   479:        quit(0);
                    480:        /* NOTREACHED */
                    481:        return (0);
                    482: }
1.7       deraadt   483:
1.20      deraadt   484: int
                    485: rundisplay(void)
                    486: {
                    487:        static char tempbuf1[50], tempbuf2[50];
                    488:        struct timeval timeout;
                    489:        fd_set readfds;
                    490:        sigset_t mask;
                    491:        char ch, *iptr;
                    492:        int change, i;
1.21      millert   493:        uid_t uid;
1.20      deraadt   494:        static char command_chars[] = "\f qh?en#sdkriIuSo";
1.7       deraadt   495:
1.20      deraadt   496:        /*
                    497:         * assume valid command unless told
                    498:         * otherwise
                    499:         */
                    500:        no_command = No;
1.7       deraadt   501:
1.20      deraadt   502:        /*
                    503:         * set up arguments for select with
                    504:         * timeout
                    505:         */
                    506:        FD_ZERO(&readfds);
                    507:        FD_SET(STDIN_FILENO, &readfds);
                    508:        timeout.tv_sec = (long) delay;
                    509:        timeout.tv_usec = (long) ((delay - timeout.tv_sec) * 1000000);
                    510:
                    511:        if (leaveflag) {
                    512:                end_screen();
                    513:                exit(0);
                    514:        }
                    515:        if (tstopflag) {
                    516:                /* move to the lower left */
                    517:                end_screen();
                    518:                fflush(stdout);
                    519:
                    520:                /*
                    521:                 * default the signal handler
                    522:                 * action
                    523:                 */
                    524:                (void) signal(SIGTSTP, SIG_DFL);
                    525:
                    526:                /*
                    527:                 * unblock the signal and
                    528:                 * send ourselves one
                    529:                 */
                    530:                sigemptyset(&mask);
                    531:                sigaddset(&mask, SIGTSTP);
                    532:                sigprocmask(SIG_UNBLOCK, &mask, NULL);
                    533:                (void) kill(0, SIGTSTP);
                    534:
                    535:                /* reset the signal handler */
                    536:                (void) signal(SIGTSTP, tstop);
                    537:
                    538:                /* reinit screen */
                    539:                reinit_screen();
                    540:                reset_display();
                    541:                tstopflag = 0;
                    542:                return 1;
                    543:        }
                    544:        if (winchflag) {
                    545:                /*
                    546:                 * reascertain the screen
                    547:                 * dimensions
                    548:                 */
                    549:                get_screensize();
                    550:
                    551:                /* tell display to resize */
                    552:                max_topn = display_resize();
                    553:
                    554:                /* reset the signal handler */
                    555:                (void) signal(SIGWINCH, winch);
                    556:
                    557:                reset_display();
                    558:                winchflag = 0;
                    559:                return 1;
                    560:        }
                    561:        /*
                    562:         * wait for either input or the end
                    563:         * of the delay period
                    564:         */
                    565:        if (select(STDIN_FILENO + 1, &readfds, (fd_set *) NULL,
                    566:            (fd_set *) NULL, &timeout) > 0) {
                    567:                char *errmsg;
                    568:                int newval;
                    569:
                    570:                clear_message();
                    571:
                    572:                /*
                    573:                 * now read it and convert to
                    574:                 * command strchr
                    575:                 */
                    576:                (void) read(0, &ch, 1);
                    577:                if ((iptr = strchr(command_chars, ch)) == NULL) {
1.1       downsj    578:                        /* illegal command */
                    579:                        new_message(MT_standout, " Command not understood");
1.9       deraadt   580:                        if (putchar('\r') == EOF)
1.20      deraadt   581:                                exit(1);
1.1       downsj    582:                        no_command = Yes;
1.20      deraadt   583:                        fflush(stdout);
                    584:                        return (0);
                    585:                }
                    586:
                    587:                change = iptr - command_chars;
                    588:                if (overstrike && change > CMD_OSLIMIT) {
                    589:                        /* error */
                    590:                        new_message(MT_standout,
1.1       downsj    591:                            " Command cannot be handled by this terminal");
1.20      deraadt   592:                        if (putchar('\r') == EOF)
                    593:                                exit(1);
                    594:                        no_command = Yes;
                    595:                        fflush(stdout);
                    596:                        return (0);
                    597:                }
                    598:
                    599:                switch (change) {
                    600:                case CMD_redraw:        /* redraw screen */
                    601:                        reset_display();
                    602:                        break;
                    603:
                    604:                case CMD_update:        /* merely update display */
                    605:                        /*
                    606:                         * is the load average high?
                    607:                         */
                    608:                        if (system_info.load_avg[0] > LoadMax) {
                    609:                                /* yes, go home for visual feedback */
                    610:                                go_home();
                    611:                                fflush(stdout);
1.1       downsj    612:                        }
1.20      deraadt   613:                        break;
                    614:
                    615:                case CMD_quit:  /* quit */
                    616:                        quit(0);
                    617:                        break;
                    618:
                    619:                case CMD_help1: /* help */
                    620:                case CMD_help2:
                    621:                        reset_display();
                    622:                        clear();
                    623:                        show_help();
                    624:                        standout("Hit any key to continue: ");
                    625:                        fflush(stdout);
                    626:                        (void) read(0, &ch, 1);
                    627:                        break;
                    628:
                    629:                case CMD_errors:        /* show errors */
                    630:                        if (error_count() == 0) {
                    631:                                new_message(MT_standout,
                    632:                                    " Currently no errors to report.");
                    633:                                if (putchar('\r') == EOF)
                    634:                                        exit(1);
                    635:                                no_command = Yes;
                    636:                        } else {
1.1       downsj    637:                                reset_display();
                    638:                                clear();
1.20      deraadt   639:                                show_errors();
1.1       downsj    640:                                standout("Hit any key to continue: ");
                    641:                                fflush(stdout);
                    642:                                (void) read(0, &ch, 1);
1.20      deraadt   643:                        }
                    644:                        break;
                    645:
                    646:                case CMD_number1:       /* new number */
                    647:                case CMD_number2:
                    648:                        new_message(MT_standout,
                    649:                            "Number of processes to show: ");
                    650:                        newval = readline(tempbuf1, 8, Yes);
                    651:                        if (newval > -1) {
                    652:                                if (newval > max_topn) {
1.1       downsj    653:                                        new_message(MT_standout | MT_delayed,
1.20      deraadt   654:                                            " This terminal can only "
                    655:                                            "display %d processes.",
                    656:                                            max_topn);
1.9       deraadt   657:                                        if (putchar('\r') == EOF)
1.20      deraadt   658:                                                exit(1);
                    659:                                }
                    660:                                if (newval == 0)
1.1       downsj    661:                                        display_header(No);
1.20      deraadt   662:                                else if (newval > topn && topn == 0) {
1.1       downsj    663:                                        /* redraw the header */
                    664:                                        display_header(Yes);
                    665:                                        d_header = i_header;
                    666:                                }
1.20      deraadt   667:                                topn = newval;
                    668:                        }
                    669:                        break;
                    670:
                    671:                case CMD_delay: /* new seconds delay */
                    672:                        new_message(MT_standout, "Seconds to delay: ");
                    673:                        if (readline(tempbuf2, sizeof(tempbuf2), No) > 0) {
                    674:                                char *endp;
                    675:                                double newdelay = strtod(tempbuf2, &endp);
                    676:
                    677:                                if (newdelay >= 0 && newdelay < 1000000 &&
                    678:                                    *endp == '\0')
1.16      hugh      679:                                        delay = newdelay;
1.20      deraadt   680:                        }
                    681:                        clear_message();
                    682:                        break;
                    683:
                    684:                case CMD_displays:      /* change display count */
                    685:                        new_message(MT_standout,
                    686:                            "Displays to show (currently %s): ",
                    687:                            displays == -1 ? "infinite" :
                    688:                            itoa(displays));
                    689:                        if ((i = readline(tempbuf1, 10, Yes)) > 0)
                    690:                                displays = i;
                    691:                        else if (i == 0)
                    692:                                quit(0);
                    693:
                    694:                        clear_message();
                    695:                        break;
                    696:
                    697:                case CMD_kill:  /* kill program */
                    698:                        new_message(0, "kill ");
                    699:                        if (readline(tempbuf2, sizeof(tempbuf2), No) > 0) {
                    700:                                if ((errmsg = kill_procs(tempbuf2)) != NULL) {
1.4       millert   701:                                        new_message(MT_standout, "%s", errmsg);
1.9       deraadt   702:                                        if (putchar('\r') == EOF)
1.20      deraadt   703:                                                exit(1);
1.1       downsj    704:                                        no_command = Yes;
                    705:                                }
1.20      deraadt   706:                        } else
                    707:                                clear_message();
                    708:                        break;
                    709:
                    710:                case CMD_renice:        /* renice program */
                    711:                        new_message(0, "renice ");
                    712:                        if (readline(tempbuf2, sizeof(tempbuf2), No) > 0) {
                    713:                                if ((errmsg = renice_procs(tempbuf2)) != NULL) {
1.4       millert   714:                                        new_message(MT_standout, "%s", errmsg);
1.9       deraadt   715:                                        if (putchar('\r') == EOF)
1.20      deraadt   716:                                                exit(1);
1.1       downsj    717:                                        no_command = Yes;
                    718:                                }
1.20      deraadt   719:                        } else
                    720:                                clear_message();
                    721:                        break;
1.1       downsj    722:
1.20      deraadt   723:                case CMD_idletog:
                    724:                case CMD_idletog2:
                    725:                        ps.idle = !ps.idle;
                    726:                        new_message(MT_standout | MT_delayed,
                    727:                            " %sisplaying idle processes.",
                    728:                            ps.idle ? "D" : "Not d");
                    729:                        if (putchar('\r') == EOF)
                    730:                                exit(1);
                    731:                        break;
1.1       downsj    732:
1.20      deraadt   733:                case CMD_user:
                    734:                        new_message(MT_standout,
                    735:                            "Username to show: ");
                    736:                        if (readline(tempbuf2, sizeof(tempbuf2), No) > 0) {
                    737:                                if (tempbuf2[0] == '+' &&
                    738:                                    tempbuf2[1] == '\0') {
1.21      millert   739:                                        ps.uid = (uid_t)-1;
                    740:                                } else if ((uid = userid(tempbuf2)) == (uid_t)-1) {
1.1       downsj    741:                                        new_message(MT_standout,
                    742:                                            " %s: unknown user", tempbuf2);
                    743:                                        no_command = Yes;
1.20      deraadt   744:                                } else
1.21      millert   745:                                        ps.uid = uid;
1.20      deraadt   746:                                if (putchar('\r') == EOF)
                    747:                                        exit(1);
                    748:                        } else
                    749:                                clear_message();
                    750:                        break;
1.12      fgsch     751:
1.20      deraadt   752:                case CMD_system:
                    753:                        ps.system = !ps.system;
                    754:                        new_message(MT_standout | MT_delayed,
                    755:                            " %sisplaying system processes.",
                    756:                            ps.system ? "D" : "Not d");
                    757:                        break;
                    758:
                    759:                case CMD_order:
                    760:                        new_message(MT_standout,
                    761:                            "Order to sort: ");
                    762:                        if (readline(tempbuf2,
                    763:                            sizeof(tempbuf2), No) > 0) {
                    764:                                if ((i = string_index(tempbuf2,
                    765:                                    statics.order_names)) == -1) {
                    766:                                        new_message(MT_standout,
                    767:                                            " %s: unrecognized sorting order",
                    768:                                            tempbuf2);
                    769:                                        no_command = Yes;
                    770:                                } else
1.1       downsj    771:                                        order_index = i;
1.9       deraadt   772:                                if (putchar('\r') == EOF)
1.20      deraadt   773:                                        exit(1);
                    774:                        } else
                    775:                                clear_message();
                    776:                        break;
1.1       downsj    777:
1.20      deraadt   778:                default:
                    779:                        new_message(MT_standout, " BAD CASE IN SWITCH!");
                    780:                        if (putchar('\r') == EOF)
                    781:                                exit(1);
1.1       downsj    782:                }
                    783:        }
                    784:
1.20      deraadt   785:        /* flush out stuff that may have been written */
                    786:        fflush(stdout);
                    787:        return 0;
1.1       downsj    788: }
                    789:
1.20      deraadt   790:
1.1       downsj    791: /*
                    792:  *  reset_display() - reset all the display routine pointers so that entire
                    793:  *     screen will get redrawn.
                    794:  */
1.19      pvalchev  795: static void
                    796: reset_display(void)
1.1       downsj    797: {
1.20      deraadt   798:        d_loadave = i_loadave;
                    799:        d_procstates = i_procstates;
                    800:        d_cpustates = i_cpustates;
                    801:        d_memory = i_memory;
                    802:        d_message = i_message;
                    803:        d_header = i_header;
                    804:        d_process = i_process;
1.1       downsj    805: }
                    806:
1.19      pvalchev  807: void
1.20      deraadt   808: leave(int signo)
1.1       downsj    809: {
1.20      deraadt   810:        leaveflag = 1;
1.1       downsj    811: }
                    812:
1.19      pvalchev  813: void
1.20      deraadt   814: tstop(int signo)
1.1       downsj    815: {
1.20      deraadt   816:        tstopflag = 1;
1.1       downsj    817: }
                    818:
1.19      pvalchev  819: void
1.20      deraadt   820: winch(int signo)
1.1       downsj    821: {
1.20      deraadt   822:        winchflag = 1;
1.1       downsj    823: }
                    824:
1.19      pvalchev  825: void
1.20      deraadt   826: onalrm(int signo)
1.1       downsj    827: {
                    828: }
                    829:
1.19      pvalchev  830: void
1.20      deraadt   831: quit(int ret)
1.1       downsj    832: {
1.20      deraadt   833:        end_screen();
                    834:        exit(ret);
1.1       downsj    835: }