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

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