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

1.48    ! otto        1: /*     $OpenBSD: top.c,v 1.47 2007/02/04 19:17:14 otto 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.32      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>
1.28      deraadt    41: #include <poll.h>
1.2       downsj     42: #include <stdlib.h>
1.35      otto       43: #include <limits.h>
1.2       downsj     44: #include <unistd.h>
1.39      millert    45: #include <sys/stat.h>
1.1       downsj     46:
                     47: /* includes specific to top */
                     48: #include "display.h"           /* interface to display package */
                     49: #include "screen.h"            /* interface to screen package */
                     50: #include "top.h"
                     51: #include "top.local.h"
                     52: #include "boolean.h"
                     53: #include "machine.h"
                     54: #include "utils.h"
                     55:
                     56: /* Size of the stdio buffer given to stdout */
1.20      deraadt    57: #define BUFFERSIZE     2048
1.1       downsj     58:
                     59: /* The buffer that stdio will use */
1.32      deraadt    60: char           stdoutbuf[BUFFERSIZE];
1.1       downsj     61:
1.32      deraadt    62: extern int     overstrike;
1.1       downsj     63:
                     64: /* signal handling routines */
1.32      deraadt    65: static void    leave(int);
                     66: static void    onalrm(int);
                     67: static void    tstop(int);
                     68: static void    winch(int);
1.1       downsj     69:
1.25      deraadt    70: volatile sig_atomic_t leaveflag, tstopflag, winchflag;
1.7       deraadt    71:
1.32      deraadt    72: static void    reset_display(void);
1.20      deraadt    73: int            rundisplay(void);
1.1       downsj     74:
1.32      deraadt    75: static int     max_topn;       /* maximum displayable processes */
1.1       downsj     76:
1.32      deraadt    77: extern int     (*proc_compares[])(const void *, const void *);
1.20      deraadt    78: int order_index;
1.1       downsj     79:
                     80: /* pointers to display routines */
1.32      deraadt    81: void           (*d_loadave)(int, double *) = i_loadave;
                     82: void           (*d_procstates)(int, int *) = i_procstates;
1.39      millert    83: void           (*d_cpustates)(int64_t *) = i_cpustates;
1.32      deraadt    84: void           (*d_memory)(int *) = i_memory;
                     85: void           (*d_message)(void) = i_message;
                     86: void           (*d_header)(char *) = i_header;
                     87: void           (*d_process)(int, char *) = i_process;
1.20      deraadt    88:
                     89: int displays = 0;      /* indicates unspecified */
                     90: char do_unames = Yes;
                     91: struct process_select ps;
                     92: char interactive = Maybe;
                     93: char warnings = 0;
                     94: double delay = Default_DELAY;
                     95: char *order_name = NULL;
                     96: int topn = Default_TOPN;
1.24      deraadt    97: int no_command = Yes;
1.35      otto       98: int old_system = No;
1.41      tedu       99: int old_threads = No;
1.40      markus    100: int show_args = No;
1.1       downsj    101:
                    102: #if Default_TOPN == Infinity
1.20      deraadt   103: char topn_specified = No;
1.1       downsj    104: #endif
                    105:
1.20      deraadt   106: /*
                    107:  * these defines enumerate the "strchr"s of the commands in
                    108:  * command_chars
                    109:  */
1.1       downsj    110: #define CMD_redraw     0
                    111: #define CMD_update     1
                    112: #define CMD_quit       2
                    113: #define CMD_help1      3
                    114: #define CMD_help2      4
1.20      deraadt   115: #define CMD_OSLIMIT    4       /* terminals with OS can only handle commands */
                    116: #define CMD_errors     5       /* less than or equal to CMD_OSLIMIT       */
1.1       downsj    117: #define CMD_number1    6
                    118: #define CMD_number2    7
                    119: #define CMD_delay      8
                    120: #define CMD_displays   9
                    121: #define CMD_kill       10
                    122: #define CMD_renice     11
1.32      deraadt   123: #define CMD_idletog    12
                    124: #define CMD_idletog2   13
1.1       downsj    125: #define CMD_user       14
1.12      fgsch     126: #define CMD_system     15
1.32      deraadt   127: #define CMD_order      16
1.35      otto      128: #define CMD_pid                17
1.40      markus    129: #define CMD_command    18
1.41      tedu      130: #define CMD_threads    19
1.45      otto      131: #define CMD_grep       20
1.47      otto      132: #define CMD_add                21
1.1       downsj    133:
1.27      deraadt   134: static void
1.20      deraadt   135: usage(void)
                    136: {
1.22      deraadt   137:        extern char *__progname;
                    138:
1.20      deraadt   139:        fprintf(stderr,
1.45      otto      140:            "usage: %s [-bCIinqSTu] [-d count] [-g command] [-o field] [-p pid] [-s time]\n\t[-U username] [number]\n",
1.22      deraadt   141:            __progname);
1.20      deraadt   142: }
                    143:
1.27      deraadt   144: static void
1.20      deraadt   145: parseargs(int ac, char **av)
                    146: {
                    147:        char *endp;
                    148:        int i;
                    149:
1.45      otto      150:        while ((i = getopt(ac, av, "STICbinqus:d:p:U:o:g:")) != -1) {
1.20      deraadt   151:                switch (i) {
1.44      otto      152:                case 'C':
                    153:                        show_args = Yes;
                    154:                        break;
                    155:
1.20      deraadt   156:                case 'u':       /* toggle uid/username display */
                    157:                        do_unames = !do_unames;
                    158:                        break;
                    159:
                    160:                case 'U':       /* display only username's processes */
1.21      millert   161:                        if ((ps.uid = userid(optarg)) == (uid_t)-1) {
1.20      deraadt   162:                                fprintf(stderr, "%s: unknown user\n", optarg);
                    163:                                exit(1);
                    164:                        }
                    165:                        break;
1.1       downsj    166:
1.35      otto      167:                case 'p': {     /* display only process id */
                    168:                        unsigned long long num;
                    169:                        const char *errstr;
                    170:
                    171:                        num = strtonum(optarg, 0, INT_MAX, &errstr);
                    172:                        if (errstr != NULL || !find_pid(num)) {
                    173:                                fprintf(stderr, "%s: unknown pid\n", optarg);
                    174:                                exit(1);
                    175:                        }
                    176:                        ps.pid = (pid_t)num;
                    177:                        ps.system = Yes;
                    178:                        break;
                    179:                }
                    180:
1.20      deraadt   181:                case 'S':       /* show system processes */
1.35      otto      182:                        ps.system = Yes;
                    183:                        old_system = Yes;
1.20      deraadt   184:                        break;
                    185:
1.41      tedu      186:                case 'T':       /* show threads */
                    187:                        ps.threads = Yes;
                    188:                        old_threads = Yes;
                    189:                        break;
                    190:
1.20      deraadt   191:                case 'I':       /* show idle processes */
                    192:                        ps.idle = !ps.idle;
                    193:                        break;
                    194:
                    195:                case 'i':       /* go interactive regardless */
                    196:                        interactive = Yes;
                    197:                        break;
                    198:
                    199:                case 'n':       /* batch, or non-interactive */
                    200:                case 'b':
                    201:                        interactive = No;
                    202:                        break;
                    203:
                    204:                case 'd':       /* number of displays to show */
                    205:                        if ((i = atoiwi(optarg)) != Invalid && i != 0) {
                    206:                                displays = i;
                    207:                                break;
1.32      deraadt   208:                        }
1.22      deraadt   209:                        warnx("warning: display count should be positive "
                    210:                            "-- option ignored");
1.20      deraadt   211:                        warnings++;
                    212:                        break;
                    213:
                    214:                case 's':
                    215:                        delay = strtod(optarg, &endp);
                    216:
                    217:                        if (delay > 0 && delay <= 1000000 && *endp == '\0')
                    218:                                break;
                    219:
1.22      deraadt   220:                        warnx("warning: delay should be a non-negative number"
                    221:                            " -- using default");
1.20      deraadt   222:                        delay = Default_DELAY;
                    223:                        warnings++;
                    224:                        break;
                    225:
                    226:                case 'q':       /* be quick about it */
                    227:                        /* only allow this if user is really root */
                    228:                        if (getuid() == 0) {
                    229:                                /* be very un-nice! */
                    230:                                (void) nice(-20);
                    231:                                break;
                    232:                        }
1.22      deraadt   233:                        warnx("warning: `-q' option can only be used by root");
1.20      deraadt   234:                        warnings++;
                    235:                        break;
                    236:
                    237:                case 'o':       /* select sort order */
                    238:                        order_name = optarg;
                    239:                        break;
                    240:
1.45      otto      241:                case 'g':       /* grep command name */
                    242:                        ps.command = strdup(optarg);
                    243:                        break;
                    244:
1.20      deraadt   245:                default:
                    246:                        usage();
                    247:                        exit(1);
1.1       downsj    248:                }
1.20      deraadt   249:        }
1.1       downsj    250:
1.20      deraadt   251:        /* get count of top processes to display (if any) */
                    252:        if (optind < ac) {
                    253:                if ((topn = atoiwi(av[optind])) == Invalid) {
1.22      deraadt   254:                        warnx("warning: process display count should "
                    255:                            "be non-negative -- using default");
1.20      deraadt   256:                        warnings++;
1.46      otto      257:                        topn = Infinity;
1.1       downsj    258:                }
1.20      deraadt   259: #if Default_TOPN == Infinity
1.1       downsj    260:                else
1.20      deraadt   261:                        topn_specified = Yes;
                    262: #endif
                    263:        }
                    264: }
1.1       downsj    265:
1.20      deraadt   266: struct system_info system_info;
                    267: struct statics  statics;
1.1       downsj    268:
1.20      deraadt   269: int
                    270: main(int argc, char *argv[])
                    271: {
                    272:        char *uname_field = "USERNAME", *header_text, *env_top;
1.27      deraadt   273:        char *(*get_userid)(uid_t) = username, **preset_argv, **av;
1.20      deraadt   274:        int preset_argc = 0, ac, active_procs, i;
                    275:        sigset_t mask, oldmask;
                    276:        time_t curr_time;
                    277:        caddr_t processes;
1.1       downsj    278:
1.20      deraadt   279:        /* set the buffer for stdout */
                    280: #ifdef DEBUG
                    281:        setbuffer(stdout, NULL, 0);
1.1       downsj    282: #else
1.20      deraadt   283:        setbuffer(stdout, stdoutbuf, sizeof stdoutbuf);
1.1       downsj    284: #endif
                    285:
1.20      deraadt   286:        /* initialize some selection options */
                    287:        ps.idle = Yes;
                    288:        ps.system = No;
1.21      millert   289:        ps.uid = (uid_t)-1;
1.35      otto      290:        ps.pid = (pid_t)-1;
1.20      deraadt   291:        ps.command = NULL;
                    292:
                    293:        /* get preset options from the environment */
                    294:        if ((env_top = getenv("TOP")) != NULL) {
                    295:                av = preset_argv = argparse(env_top, &preset_argc);
                    296:                ac = preset_argc;
                    297:
                    298:                /*
                    299:                 * set the dummy argument to an explanatory message, in case
                    300:                 * getopt encounters a bad argument
                    301:                 */
                    302:                preset_argv[0] = "while processing environment";
                    303:        }
                    304:        /* process options */
                    305:        do {
                    306:                /*
                    307:                 * if we're done doing the presets, then process the real
                    308:                 * arguments
                    309:                 */
                    310:                if (preset_argc == 0) {
                    311:                        ac = argc;
                    312:                        av = argv;
                    313:                        optind = 1;
                    314:                }
                    315:                parseargs(ac, av);
                    316:                i = preset_argc;
                    317:                preset_argc = 0;
                    318:        } while (i != 0);
                    319:
                    320:        /* set constants for username/uid display correctly */
                    321:        if (!do_unames) {
                    322:                uname_field = "   UID  ";
1.26      millert   323:                get_userid = format_uid;
1.20      deraadt   324:        }
                    325:        /* initialize the kernel memory interface */
                    326:        if (machine_init(&statics) == -1)
1.1       downsj    327:                exit(1);
1.20      deraadt   328:
                    329:        /* determine sorting order index, if necessary */
                    330:        if (order_name != NULL) {
                    331:                if ((order_index = string_index(order_name,
                    332:                    statics.order_names)) == -1) {
                    333:                        char **pp;
                    334:
1.22      deraadt   335:                        warnx("'%s' is not a recognized sorting order",
                    336:                            order_name);
1.20      deraadt   337:                        fprintf(stderr, "\tTry one of these:");
                    338:                        pp = statics.order_names;
                    339:                        while (*pp != NULL)
                    340:                                fprintf(stderr, " %s", *pp++);
                    341:                        fputc('\n', stderr);
                    342:                        exit(1);
                    343:                }
1.1       downsj    344:        }
                    345:
1.20      deraadt   346:        /* initialize termcap */
                    347:        init_termcap(interactive);
                    348:
                    349:        /* get the string to use for the process area header */
                    350:        header_text = format_header(uname_field);
                    351:
                    352:        /* initialize display interface */
                    353:        if ((max_topn = display_init(&statics)) == -1) {
1.22      deraadt   354:                warnx("can't allocate sufficient memory");
1.20      deraadt   355:                exit(4);
                    356:        }
                    357:        /* print warning if user requested more processes than we can display */
                    358:        if (topn > max_topn) {
1.22      deraadt   359:                warnx("warning: this terminal can only display %d processes",
                    360:                    max_topn);
1.1       downsj    361:                warnings++;
1.20      deraadt   362:        }
                    363:        /* adjust for topn == Infinity */
                    364:        if (topn == Infinity) {
                    365:                /*
                    366:                 *  For smart terminals, infinity really means everything that can
                    367:                 *  be displayed, or Largest.
                    368:                 *  On dumb terminals, infinity means every process in the system!
                    369:                 *  We only really want to do that if it was explicitly specified.
                    370:                 *  This is always the case when "Default_TOPN != Infinity".  But if
                    371:                 *  topn wasn't explicitly specified and we are on a dumb terminal
                    372:                 *  and the default is Infinity, then (and only then) we use
                    373:                 *  "Nominal_TOPN" instead.
                    374:                 */
1.1       downsj    375: #if Default_TOPN == Infinity
1.20      deraadt   376:                topn = smart_terminal ? Largest :
                    377:                    (topn_specified ? Largest : Nominal_TOPN);
                    378: #else
                    379:                topn = Largest;
1.1       downsj    380: #endif
                    381:        }
1.20      deraadt   382:        /* set header display accordingly */
                    383:        display_header(topn > 0);
1.1       downsj    384:
1.20      deraadt   385:        /* determine interactive state */
                    386:        if (interactive == Maybe)
                    387:                interactive = smart_terminal;
1.1       downsj    388:
1.20      deraadt   389:        /* if # of displays not specified, fill it in */
                    390:        if (displays == 0)
                    391:                displays = smart_terminal ? Infinity : 1;
1.1       downsj    392:
                    393:        /*
1.20      deraadt   394:         * block interrupt signals while setting up the screen and the
                    395:         * handlers
1.1       downsj    396:         */
1.20      deraadt   397:        sigemptyset(&mask);
                    398:        sigaddset(&mask, SIGINT);
                    399:        sigaddset(&mask, SIGQUIT);
                    400:        sigaddset(&mask, SIGTSTP);
                    401:        sigprocmask(SIG_BLOCK, &mask, &oldmask);
                    402:        init_screen();
                    403:        (void) signal(SIGINT, leave);
1.30      deraadt   404:        siginterrupt(SIGINT, 1);
1.20      deraadt   405:        (void) signal(SIGQUIT, leave);
                    406:        (void) signal(SIGTSTP, tstop);
                    407:        (void) signal(SIGWINCH, winch);
                    408:        sigprocmask(SIG_SETMASK, &oldmask, NULL);
                    409:        if (warnings) {
                    410:                fputs("....", stderr);
                    411:                fflush(stderr); /* why must I do this? */
1.32      deraadt   412:                sleep((unsigned)(3 * warnings));
1.20      deraadt   413:                fputc('\n', stderr);
                    414:        }
1.7       deraadt   415: restart:
1.1       downsj    416:
1.20      deraadt   417:        /*
                    418:         *  main loop -- repeat while display count is positive or while it
                    419:         *              indicates infinity (by being -1)
                    420:         */
                    421:        while ((displays == -1) || (displays-- > 0)) {
                    422:                /* get the current stats */
                    423:                get_system_info(&system_info);
                    424:
                    425:                /* get the current set of processes */
                    426:                processes = get_process_info(&system_info, &ps,
                    427:                    proc_compares[order_index]);
                    428:
                    429:                /* display the load averages */
                    430:                (*d_loadave)(system_info.last_pid, system_info.load_avg);
                    431:
                    432:                /* display the current time */
                    433:                /* this method of getting the time SHOULD be fairly portable */
                    434:                time(&curr_time);
                    435:                i_timeofday(&curr_time);
                    436:
                    437:                /* display process state breakdown */
                    438:                (*d_procstates)(system_info.p_total, system_info.procstates);
                    439:
                    440:                /* display the cpu state percentage breakdown */
1.43      otto      441:                (*d_cpustates)(system_info.cpustates);
1.1       downsj    442:
1.20      deraadt   443:                /* display memory stats */
1.32      deraadt   444:                (*d_memory)(system_info.memory);
1.1       downsj    445:
1.20      deraadt   446:                /* handle message area */
1.32      deraadt   447:                (*d_message)();
1.1       downsj    448:
1.20      deraadt   449:                /* update the header area */
1.32      deraadt   450:                (*d_header)(header_text);
1.20      deraadt   451:
                    452:                if (topn > 0) {
                    453:                        /* determine number of processes to actually display */
                    454:                        /*
                    455:                         * this number will be the smallest of:  active
                    456:                         * processes, number user requested, number current
                    457:                         * screen accommodates
                    458:                         */
                    459:                        active_procs = system_info.p_active;
                    460:                        if (active_procs > topn)
                    461:                                active_procs = topn;
                    462:                        if (active_procs > max_topn)
                    463:                                active_procs = max_topn;
                    464:                        /* now show the top "n" processes. */
                    465:                        for (i = 0; i < active_procs; i++)
                    466:                                (*d_process)(i, format_next_process(processes,
                    467:                                    get_userid));
                    468:                } else
                    469:                        i = 0;
                    470:
                    471:                /* do end-screen processing */
                    472:                u_endscreen(i);
                    473:
                    474:                /* now, flush the output buffer */
                    475:                fflush(stdout);
                    476:
                    477:                /* only do the rest if we have more displays to show */
                    478:                if (displays) {
                    479:                        /* switch out for new display on smart terminals */
                    480:                        if (smart_terminal) {
                    481:                                if (overstrike) {
                    482:                                        reset_display();
                    483:                                } else {
                    484:                                        d_loadave = u_loadave;
                    485:                                        d_procstates = u_procstates;
                    486:                                        d_cpustates = u_cpustates;
                    487:                                        d_memory = u_memory;
                    488:                                        d_message = u_message;
                    489:                                        d_header = u_header;
                    490:                                        d_process = u_process;
                    491:                                }
                    492:                        }
                    493:                        no_command = Yes;
                    494:                        if (!interactive) {
                    495:                                /* set up alarm */
                    496:                                (void) signal(SIGALRM, onalrm);
                    497:                                (void) alarm((unsigned) delay);
                    498:
                    499:                                /* wait for the rest of it .... */
                    500:                                pause();
1.42      otto      501:                                if (leaveflag)
                    502:                                        exit(0);
                    503:                                if (tstopflag) {
                    504:                                        (void) signal(SIGTSTP, SIG_DFL);
                    505:                                        (void) kill(0, SIGTSTP);
                    506:                                        /* reset the signal handler */
                    507:                                        (void) signal(SIGTSTP, tstop);
                    508:                                        tstopflag = 0;
                    509:                                }
1.20      deraadt   510:                        } else {
                    511:                                while (no_command)
                    512:                                        if (rundisplay())
                    513:                                                goto restart;
                    514:                        }
                    515:                }
                    516:        }
1.1       downsj    517:
1.20      deraadt   518:        quit(0);
                    519:        /* NOTREACHED */
                    520:        return (0);
                    521: }
1.7       deraadt   522:
1.20      deraadt   523: int
                    524: rundisplay(void)
                    525: {
                    526:        static char tempbuf1[50], tempbuf2[50];
                    527:        sigset_t mask;
                    528:        char ch, *iptr;
                    529:        int change, i;
1.28      deraadt   530:        struct pollfd pfd[1];
1.21      millert   531:        uid_t uid;
1.47      otto      532:        static char command_chars[] = "\f qh?en#sdkriIuSopCTg+";
1.7       deraadt   533:
1.20      deraadt   534:        /*
                    535:         * assume valid command unless told
                    536:         * otherwise
                    537:         */
                    538:        no_command = No;
1.7       deraadt   539:
1.20      deraadt   540:        /*
                    541:         * set up arguments for select with
                    542:         * timeout
                    543:         */
1.28      deraadt   544:        pfd[0].fd = STDIN_FILENO;
                    545:        pfd[0].events = POLLIN;
1.20      deraadt   546:
1.42      otto      547:        if (leaveflag)
                    548:                quit(0);
1.20      deraadt   549:        if (tstopflag) {
                    550:                /* move to the lower left */
                    551:                end_screen();
                    552:                fflush(stdout);
                    553:
                    554:                /*
                    555:                 * default the signal handler
                    556:                 * action
                    557:                 */
                    558:                (void) signal(SIGTSTP, SIG_DFL);
                    559:
                    560:                /*
                    561:                 * unblock the signal and
                    562:                 * send ourselves one
                    563:                 */
                    564:                sigemptyset(&mask);
                    565:                sigaddset(&mask, SIGTSTP);
                    566:                sigprocmask(SIG_UNBLOCK, &mask, NULL);
                    567:                (void) kill(0, SIGTSTP);
                    568:
                    569:                /* reset the signal handler */
                    570:                (void) signal(SIGTSTP, tstop);
                    571:
                    572:                /* reinit screen */
                    573:                reinit_screen();
                    574:                reset_display();
                    575:                tstopflag = 0;
                    576:                return 1;
                    577:        }
                    578:        if (winchflag) {
                    579:                /*
                    580:                 * reascertain the screen
                    581:                 * dimensions
                    582:                 */
                    583:                get_screensize();
                    584:
                    585:                /* tell display to resize */
                    586:                max_topn = display_resize();
                    587:
                    588:                /* reset the signal handler */
                    589:                (void) signal(SIGWINCH, winch);
                    590:
                    591:                reset_display();
                    592:                winchflag = 0;
                    593:                return 1;
                    594:        }
                    595:        /*
                    596:         * wait for either input or the end
                    597:         * of the delay period
                    598:         */
1.33      deraadt   599:        if (poll(pfd, 1, (int)(delay * 1000)) > 0 &&
1.31      millert   600:            !(pfd[0].revents & (POLLERR|POLLHUP|POLLNVAL))) {
1.20      deraadt   601:                char *errmsg;
1.30      deraadt   602:                ssize_t len;
1.20      deraadt   603:                int newval;
                    604:
                    605:                clear_message();
                    606:
                    607:                /*
                    608:                 * now read it and convert to
                    609:                 * command strchr
                    610:                 */
1.30      deraadt   611:                while (1) {
1.36      deraadt   612:                        len = read(STDIN_FILENO, &ch, 1);
1.30      deraadt   613:                        if (len == -1 && errno == EINTR)
                    614:                                continue;
                    615:                        if (len == 0)
                    616:                                exit(1);
                    617:                        break;
                    618:                }
1.20      deraadt   619:                if ((iptr = strchr(command_chars, ch)) == NULL) {
1.1       downsj    620:                        /* illegal command */
                    621:                        new_message(MT_standout, " Command not understood");
1.9       deraadt   622:                        if (putchar('\r') == EOF)
1.20      deraadt   623:                                exit(1);
1.1       downsj    624:                        no_command = Yes;
1.20      deraadt   625:                        fflush(stdout);
                    626:                        return (0);
                    627:                }
                    628:
                    629:                change = iptr - command_chars;
                    630:                if (overstrike && change > CMD_OSLIMIT) {
                    631:                        /* error */
                    632:                        new_message(MT_standout,
1.1       downsj    633:                            " Command cannot be handled by this terminal");
1.20      deraadt   634:                        if (putchar('\r') == EOF)
                    635:                                exit(1);
                    636:                        no_command = Yes;
                    637:                        fflush(stdout);
                    638:                        return (0);
                    639:                }
                    640:
                    641:                switch (change) {
                    642:                case CMD_redraw:        /* redraw screen */
                    643:                        reset_display();
                    644:                        break;
                    645:
                    646:                case CMD_update:        /* merely update display */
                    647:                        /*
                    648:                         * is the load average high?
                    649:                         */
                    650:                        if (system_info.load_avg[0] > LoadMax) {
                    651:                                /* yes, go home for visual feedback */
                    652:                                go_home();
                    653:                                fflush(stdout);
1.1       downsj    654:                        }
1.20      deraadt   655:                        break;
                    656:
                    657:                case CMD_quit:  /* quit */
                    658:                        quit(0);
                    659:                        break;
                    660:
                    661:                case CMD_help1: /* help */
                    662:                case CMD_help2:
                    663:                        reset_display();
                    664:                        clear();
                    665:                        show_help();
                    666:                        standout("Hit any key to continue: ");
                    667:                        fflush(stdout);
1.30      deraadt   668:                        while (1) {
1.36      deraadt   669:                                len = read(STDIN_FILENO, &ch, 1);
1.30      deraadt   670:                                if (len == -1 && errno == EINTR)
                    671:                                        continue;
                    672:                                if (len == 0)
                    673:                                        exit(1);
                    674:                                break;
                    675:                        }
1.20      deraadt   676:                        break;
                    677:
                    678:                case CMD_errors:        /* show errors */
                    679:                        if (error_count() == 0) {
                    680:                                new_message(MT_standout,
                    681:                                    " Currently no errors to report.");
                    682:                                if (putchar('\r') == EOF)
                    683:                                        exit(1);
                    684:                                no_command = Yes;
                    685:                        } else {
1.1       downsj    686:                                reset_display();
                    687:                                clear();
1.20      deraadt   688:                                show_errors();
1.1       downsj    689:                                standout("Hit any key to continue: ");
                    690:                                fflush(stdout);
1.30      deraadt   691:                                while (1) {
1.36      deraadt   692:                                        len = read(STDIN_FILENO, &ch, 1);
1.30      deraadt   693:                                        if (len == -1 && errno == EINTR)
                    694:                                                continue;
                    695:                                        if (len == 0)
                    696:                                                exit(1);
                    697:                                        break;
                    698:                                }
1.20      deraadt   699:                        }
                    700:                        break;
                    701:
                    702:                case CMD_number1:       /* new number */
                    703:                case CMD_number2:
                    704:                        new_message(MT_standout,
                    705:                            "Number of processes to show: ");
                    706:                        newval = readline(tempbuf1, 8, Yes);
                    707:                        if (newval > -1) {
                    708:                                if (newval > max_topn) {
1.1       downsj    709:                                        new_message(MT_standout | MT_delayed,
1.20      deraadt   710:                                            " This terminal can only "
                    711:                                            "display %d processes.",
                    712:                                            max_topn);
1.9       deraadt   713:                                        if (putchar('\r') == EOF)
1.20      deraadt   714:                                                exit(1);
                    715:                                }
                    716:                                if (newval == 0)
1.1       downsj    717:                                        display_header(No);
1.20      deraadt   718:                                else if (newval > topn && topn == 0) {
1.1       downsj    719:                                        /* redraw the header */
                    720:                                        display_header(Yes);
                    721:                                        d_header = i_header;
                    722:                                }
1.20      deraadt   723:                                topn = newval;
                    724:                        }
                    725:                        break;
                    726:
                    727:                case CMD_delay: /* new seconds delay */
                    728:                        new_message(MT_standout, "Seconds to delay: ");
                    729:                        if (readline(tempbuf2, sizeof(tempbuf2), No) > 0) {
                    730:                                char *endp;
                    731:                                double newdelay = strtod(tempbuf2, &endp);
                    732:
                    733:                                if (newdelay >= 0 && newdelay < 1000000 &&
1.48    ! otto      734:                                    *endp == '\0') {
1.16      hugh      735:                                        delay = newdelay;
1.48    ! otto      736:                                } else {
        !           737:                                        new_message(MT_standout,
        !           738:                                            "Delay should be a non-negative number");
        !           739:                                        if (putchar('\r') == EOF)
        !           740:                                                exit(1);
        !           741:                                        no_command = Yes;
        !           742:                                }
        !           743:
        !           744:                        } else
        !           745:                                clear_message();
1.20      deraadt   746:                        break;
                    747:
                    748:                case CMD_displays:      /* change display count */
                    749:                        new_message(MT_standout,
                    750:                            "Displays to show (currently %s): ",
                    751:                            displays == -1 ? "infinite" :
                    752:                            itoa(displays));
                    753:                        if ((i = readline(tempbuf1, 10, Yes)) > 0)
                    754:                                displays = i;
                    755:                        else if (i == 0)
                    756:                                quit(0);
                    757:
                    758:                        clear_message();
                    759:                        break;
                    760:
                    761:                case CMD_kill:  /* kill program */
                    762:                        new_message(0, "kill ");
                    763:                        if (readline(tempbuf2, sizeof(tempbuf2), No) > 0) {
                    764:                                if ((errmsg = kill_procs(tempbuf2)) != NULL) {
1.4       millert   765:                                        new_message(MT_standout, "%s", errmsg);
1.9       deraadt   766:                                        if (putchar('\r') == EOF)
1.20      deraadt   767:                                                exit(1);
1.1       downsj    768:                                        no_command = Yes;
                    769:                                }
1.20      deraadt   770:                        } else
                    771:                                clear_message();
                    772:                        break;
                    773:
                    774:                case CMD_renice:        /* renice program */
                    775:                        new_message(0, "renice ");
                    776:                        if (readline(tempbuf2, sizeof(tempbuf2), No) > 0) {
                    777:                                if ((errmsg = renice_procs(tempbuf2)) != NULL) {
1.4       millert   778:                                        new_message(MT_standout, "%s", errmsg);
1.9       deraadt   779:                                        if (putchar('\r') == EOF)
1.20      deraadt   780:                                                exit(1);
1.1       downsj    781:                                        no_command = Yes;
                    782:                                }
1.20      deraadt   783:                        } else
                    784:                                clear_message();
                    785:                        break;
1.1       downsj    786:
1.20      deraadt   787:                case CMD_idletog:
                    788:                case CMD_idletog2:
                    789:                        ps.idle = !ps.idle;
                    790:                        new_message(MT_standout | MT_delayed,
                    791:                            " %sisplaying idle processes.",
                    792:                            ps.idle ? "D" : "Not d");
                    793:                        if (putchar('\r') == EOF)
                    794:                                exit(1);
                    795:                        break;
1.1       downsj    796:
1.20      deraadt   797:                case CMD_user:
                    798:                        new_message(MT_standout,
                    799:                            "Username to show: ");
                    800:                        if (readline(tempbuf2, sizeof(tempbuf2), No) > 0) {
                    801:                                if (tempbuf2[0] == '+' &&
                    802:                                    tempbuf2[1] == '\0') {
1.21      millert   803:                                        ps.uid = (uid_t)-1;
                    804:                                } else if ((uid = userid(tempbuf2)) == (uid_t)-1) {
1.1       downsj    805:                                        new_message(MT_standout,
                    806:                                            " %s: unknown user", tempbuf2);
                    807:                                        no_command = Yes;
1.20      deraadt   808:                                } else
1.21      millert   809:                                        ps.uid = uid;
1.20      deraadt   810:                                if (putchar('\r') == EOF)
                    811:                                        exit(1);
                    812:                        } else
                    813:                                clear_message();
                    814:                        break;
1.12      fgsch     815:
1.20      deraadt   816:                case CMD_system:
                    817:                        ps.system = !ps.system;
1.35      otto      818:                        old_system = ps.system;
1.20      deraadt   819:                        new_message(MT_standout | MT_delayed,
                    820:                            " %sisplaying system processes.",
                    821:                            ps.system ? "D" : "Not d");
                    822:                        break;
                    823:
                    824:                case CMD_order:
                    825:                        new_message(MT_standout,
                    826:                            "Order to sort: ");
                    827:                        if (readline(tempbuf2,
                    828:                            sizeof(tempbuf2), No) > 0) {
                    829:                                if ((i = string_index(tempbuf2,
                    830:                                    statics.order_names)) == -1) {
                    831:                                        new_message(MT_standout,
                    832:                                            " %s: unrecognized sorting order",
                    833:                                            tempbuf2);
                    834:                                        no_command = Yes;
                    835:                                } else
1.1       downsj    836:                                        order_index = i;
1.35      otto      837:                                if (putchar('\r') == EOF)
                    838:                                        exit(1);
                    839:                        } else
                    840:                                clear_message();
                    841:                        break;
                    842:
                    843:                case CMD_pid:
1.37      jaredy    844:                        new_message(MT_standout, "Process ID to show: ");
1.35      otto      845:                        if (readline(tempbuf2, sizeof(tempbuf2), No) > 0) {
                    846:                                if (tempbuf2[0] == '+' &&
                    847:                                    tempbuf2[1] == '\0') {
                    848:                                        ps.pid = (pid_t)-1;
                    849:                                        ps.system = old_system;
                    850:                                } else {
                    851:                                        unsigned long long num;
                    852:                                        const char *errstr;
                    853:
                    854:                                        num = strtonum(tempbuf2, 0, INT_MAX,
                    855:                                            &errstr);
                    856:                                        if (errstr != NULL || !find_pid(num)) {
                    857:                                                new_message(MT_standout,
                    858:                                                    " %s: unknown pid",
                    859:                                                    tempbuf2);
                    860:                                                no_command = Yes;
                    861:                                        } else {
                    862:                                                if (ps.system == No)
                    863:                                                        old_system = No;
                    864:                                                ps.pid = (pid_t)num;
                    865:                                                ps.system = Yes;
                    866:                                        }
                    867:                                }
1.9       deraadt   868:                                if (putchar('\r') == EOF)
1.20      deraadt   869:                                        exit(1);
                    870:                        } else
                    871:                                clear_message();
1.40      markus    872:                        break;
                    873:
                    874:                case CMD_command:
                    875:                        show_args = (show_args == No) ? Yes : No;
1.41      tedu      876:                        break;
                    877:
                    878:                case CMD_threads:
                    879:                        ps.threads = !ps.threads;
                    880:                        old_threads = ps.threads;
                    881:                        new_message(MT_standout | MT_delayed,
                    882:                            " %sisplaying threads.",
                    883:                            ps.threads ? "D" : "Not d");
1.45      otto      884:                        break;
                    885:
                    886:                case CMD_grep:
                    887:                        new_message(MT_standout,
                    888:                            "Grep command name: ");
                    889:                        if (readline(tempbuf2, sizeof(tempbuf2), No) > 0) {
                    890:                                free(ps.command);
                    891:                                if (tempbuf2[0] == '+' &&
                    892:                                    tempbuf2[1] == '\0')
                    893:                                        ps.command = NULL;
                    894:                                else
                    895:                                        ps.command = strdup(tempbuf2);
                    896:                                if (putchar('\r') == EOF)
                    897:                                        exit(1);
                    898:                        } else
                    899:                                clear_message();
1.20      deraadt   900:                        break;
1.1       downsj    901:
1.47      otto      902:                case CMD_add:
                    903:                        ps.uid = (uid_t)-1;     /* uid */
                    904:                        ps.pid = (pid_t)-1;     /* pid */
                    905:                        ps.system = old_system;
                    906:                        ps.command = NULL;      /* grep */
                    907:                        break;
                    908:
1.20      deraadt   909:                default:
                    910:                        new_message(MT_standout, " BAD CASE IN SWITCH!");
                    911:                        if (putchar('\r') == EOF)
                    912:                                exit(1);
1.1       downsj    913:                }
                    914:        }
                    915:
1.20      deraadt   916:        /* flush out stuff that may have been written */
                    917:        fflush(stdout);
                    918:        return 0;
1.1       downsj    919: }
                    920:
1.20      deraadt   921:
1.1       downsj    922: /*
                    923:  *  reset_display() - reset all the display routine pointers so that entire
                    924:  *     screen will get redrawn.
                    925:  */
1.19      pvalchev  926: static void
                    927: reset_display(void)
1.1       downsj    928: {
1.20      deraadt   929:        d_loadave = i_loadave;
                    930:        d_procstates = i_procstates;
                    931:        d_cpustates = i_cpustates;
                    932:        d_memory = i_memory;
                    933:        d_message = i_message;
                    934:        d_header = i_header;
                    935:        d_process = i_process;
1.1       downsj    936: }
                    937:
1.34      deraadt   938: /* ARGSUSED */
1.19      pvalchev  939: void
1.20      deraadt   940: leave(int signo)
1.1       downsj    941: {
1.20      deraadt   942:        leaveflag = 1;
1.1       downsj    943: }
                    944:
1.34      deraadt   945: /* ARGSUSED */
1.19      pvalchev  946: void
1.20      deraadt   947: tstop(int signo)
1.1       downsj    948: {
1.20      deraadt   949:        tstopflag = 1;
1.1       downsj    950: }
                    951:
1.34      deraadt   952: /* ARGSUSED */
1.19      pvalchev  953: void
1.20      deraadt   954: winch(int signo)
1.1       downsj    955: {
1.20      deraadt   956:        winchflag = 1;
1.1       downsj    957: }
                    958:
1.34      deraadt   959: /* ARGSUSED */
1.19      pvalchev  960: void
1.20      deraadt   961: onalrm(int signo)
1.1       downsj    962: {
                    963: }
                    964:
1.19      pvalchev  965: void
1.20      deraadt   966: quit(int ret)
1.1       downsj    967: {
1.20      deraadt   968:        end_screen();
                    969:        exit(ret);
1.1       downsj    970: }