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

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