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

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