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

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