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

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