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

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