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

1.93      cheloha     1: /*     $OpenBSD: top.c,v 1.92 2018/09/22 16:50:35 millert 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.51      otto       86: pid_t hlpid = -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.27      deraadt   134: static void
1.20      deraadt   135: parseargs(int ac, char **av)
                    136: {
                    137:        char *endp;
                    138:        int i;
                    139:
1.76      jsing     140:        while ((i = getopt(ac, av, "1SHICbinqus:d:p:U:o:g:")) != -1) {
1.20      deraadt   141:                switch (i) {
1.68      tedu      142:                case '1':
                    143:                        combine_cpus = 1;
                    144:                        break;
1.44      otto      145:                case 'C':
                    146:                        show_args = Yes;
                    147:                        break;
1.20      deraadt   148:                case 'u':       /* toggle uid/username display */
                    149:                        do_unames = !do_unames;
                    150:                        break;
                    151:
                    152:                case 'U':       /* display only username's processes */
1.78      brynet    153:                        if (optarg[0] == '-') {
1.92      millert   154:                                if (uid_from_user(optarg+1, &ps.huid) == -1)
                    155:                                        new_message(MT_delayed,
                    156:                                            "%s: unknown user", optarg);
1.79      lum       157:                                else
                    158:                                        ps.uid = (uid_t)-1;
1.92      millert   159:                        } else if (uid_from_user(optarg, &ps.uid) == -1)
1.61      otto      160:                                new_message(MT_delayed, "%s: unknown user",
                    161:                                    optarg);
1.79      lum       162:                        else
                    163:                                ps.huid = (uid_t)-1;
1.20      deraadt   164:                        break;
1.1       downsj    165:
1.35      otto      166:                case 'p': {     /* display only process id */
                    167:                        const char *errstr;
                    168:
1.55      otto      169:                        i = strtonum(optarg, 0, INT_MAX, &errstr);
1.61      otto      170:                        if (errstr != NULL || !find_pid(i))
                    171:                                new_message(MT_delayed, "%s: unknown pid",
                    172:                                    optarg);
                    173:                        else {
                    174:                                ps.pid = (pid_t)i;
                    175:                                ps.system = Yes;
1.35      otto      176:                        }
                    177:                        break;
                    178:                }
                    179:
1.20      deraadt   180:                case 'S':       /* show system processes */
1.67      jmc       181:                        ps.system = !ps.system;
                    182:                        old_system = !old_system;
1.20      deraadt   183:                        break;
                    184:
1.76      jsing     185:                case 'H':       /* show threads */
1.41      tedu      186:                        ps.threads = Yes;
                    187:                        old_threads = Yes;
                    188:                        break;
                    189:
1.20      deraadt   190:                case 'I':       /* show idle processes */
                    191:                        ps.idle = !ps.idle;
                    192:                        break;
                    193:
                    194:                case 'i':       /* go interactive regardless */
                    195:                        interactive = Yes;
                    196:                        break;
                    197:
                    198:                case 'n':       /* batch, or non-interactive */
                    199:                case 'b':
                    200:                        interactive = No;
                    201:                        break;
                    202:
                    203:                case 'd':       /* number of displays to show */
                    204:                        if ((i = atoiwi(optarg)) != Invalid && i != 0) {
                    205:                                displays = i;
1.66      sthen     206:                                if (displays == 1)
                    207:                                        interactive = No;
1.20      deraadt   208:                                break;
1.32      deraadt   209:                        }
1.60      otto      210:                        new_message(MT_delayed,
                    211:                            "warning: display count should be positive "
1.22      deraadt   212:                            "-- option ignored");
1.20      deraadt   213:                        break;
                    214:
                    215:                case 's':
                    216:                        delay = strtod(optarg, &endp);
                    217:
1.59      otto      218:                        if (delay >= 0 && delay <= 1000000 && *endp == '\0')
1.20      deraadt   219:                                break;
                    220:
1.60      otto      221:                        new_message(MT_delayed,
                    222:                            "warning: delay should be a non-negative number"
1.22      deraadt   223:                            " -- using default");
1.20      deraadt   224:                        delay = Default_DELAY;
                    225:                        break;
                    226:
                    227:                case 'q':       /* be quick about it */
                    228:                        /* only allow this if user is really root */
                    229:                        if (getuid() == 0) {
                    230:                                /* be very un-nice! */
                    231:                                (void) nice(-20);
                    232:                                break;
                    233:                        }
1.60      otto      234:                        new_message(MT_delayed,
                    235:                            "warning: `-q' option can only be used by root");
1.20      deraadt   236:                        break;
                    237:
                    238:                case 'o':       /* select sort order */
                    239:                        order_name = optarg;
                    240:                        break;
                    241:
1.45      otto      242:                case 'g':       /* grep command name */
1.74      lum       243:                        free(ps.command);
1.75      deraadt   244:                        if ((ps.command = strdup(optarg)) == NULL)
1.74      lum       245:                                err(1, NULL);
1.45      otto      246:                        break;
                    247:
1.20      deraadt   248:                default:
                    249:                        usage();
                    250:                        exit(1);
1.1       downsj    251:                }
1.20      deraadt   252:        }
1.82      dlg       253:
1.94    ! cheloha   254:        i = getncpu();
1.82      dlg       255:        if (i == -1)
                    256:                err(1, NULL);
                    257:
                    258:        if (i > 8)
                    259:                combine_cpus = 1;
1.1       downsj    260:
1.20      deraadt   261:        /* get count of top processes to display (if any) */
                    262:        if (optind < ac) {
                    263:                if ((topn = atoiwi(av[optind])) == Invalid) {
1.60      otto      264:                        new_message(MT_delayed,
                    265:                            "warning: process count should "
1.49      otto      266:                            "be a non-negative number -- using default");
1.46      otto      267:                        topn = Infinity;
1.1       downsj    268:                }
1.20      deraadt   269: #if Default_TOPN == Infinity
1.1       downsj    270:                else
1.20      deraadt   271:                        topn_specified = Yes;
                    272: #endif
                    273:        }
                    274: }
1.1       downsj    275:
1.20      deraadt   276: struct system_info system_info;
                    277: struct statics  statics;
1.1       downsj    278:
1.20      deraadt   279: int
                    280: main(int argc, char *argv[])
                    281: {
                    282:        char *uname_field = "USERNAME", *header_text, *env_top;
1.92      millert   283:        const char *(*get_userid)(uid_t, int) = user_from_uid;
1.77      pirofti   284:        char **preset_argv = NULL, **av = argv;
                    285:        int preset_argc = 0, ac = argc, active_procs, i;
1.20      deraadt   286:        sigset_t mask, oldmask;
                    287:        time_t curr_time;
                    288:        caddr_t processes;
1.1       downsj    289:
1.20      deraadt   290:        /* set the buffer for stdout */
                    291: #ifdef DEBUG
1.87      tedu      292:        setvbuf(stdout, NULL, _IONBUF, 0);
1.1       downsj    293: #else
1.87      tedu      294:        setvbuf(stdout, stdoutbuf, _IOFBF, sizeof stdoutbuf);
1.1       downsj    295: #endif
                    296:
1.20      deraadt   297:        /* initialize some selection options */
                    298:        ps.idle = Yes;
                    299:        ps.system = No;
1.21      millert   300:        ps.uid = (uid_t)-1;
1.78      brynet    301:        ps.huid = (uid_t)-1;
1.35      otto      302:        ps.pid = (pid_t)-1;
1.20      deraadt   303:        ps.command = NULL;
                    304:
                    305:        /* get preset options from the environment */
                    306:        if ((env_top = getenv("TOP")) != NULL) {
                    307:                av = preset_argv = argparse(env_top, &preset_argc);
                    308:                ac = preset_argc;
                    309:
                    310:                /*
                    311:                 * set the dummy argument to an explanatory message, in case
                    312:                 * getopt encounters a bad argument
                    313:                 */
                    314:                preset_argv[0] = "while processing environment";
                    315:        }
                    316:        /* process options */
                    317:        do {
                    318:                /*
                    319:                 * if we're done doing the presets, then process the real
                    320:                 * arguments
                    321:                 */
                    322:                if (preset_argc == 0) {
                    323:                        ac = argc;
                    324:                        av = argv;
                    325:                        optind = 1;
                    326:                }
                    327:                parseargs(ac, av);
                    328:                i = preset_argc;
                    329:                preset_argc = 0;
                    330:        } while (i != 0);
1.84      deraadt   331:
1.88      espie     332:        if (pledge("stdio rpath getpw tty proc ps vminfo", NULL) == -1)
1.84      deraadt   333:                err(1, "pledge");
1.20      deraadt   334:
                    335:        /* set constants for username/uid display correctly */
                    336:        if (!do_unames) {
                    337:                uname_field = "   UID  ";
1.26      millert   338:                get_userid = format_uid;
1.20      deraadt   339:        }
                    340:        /* initialize the kernel memory interface */
                    341:        if (machine_init(&statics) == -1)
1.1       downsj    342:                exit(1);
1.20      deraadt   343:
                    344:        /* determine sorting order index, if necessary */
                    345:        if (order_name != NULL) {
                    346:                if ((order_index = string_index(order_name,
                    347:                    statics.order_names)) == -1) {
1.69      tedu      348:                        char **pp, msg[512];
1.20      deraadt   349:
1.60      otto      350:                        snprintf(msg, sizeof(msg),
                    351:                            "'%s' is not a recognized sorting order",
1.22      deraadt   352:                            order_name);
1.60      otto      353:                        strlcat(msg, ". Valid are:", sizeof(msg));
1.20      deraadt   354:                        pp = statics.order_names;
1.60      otto      355:                        while (*pp != NULL) {
                    356:                                strlcat(msg, " ", sizeof(msg));
                    357:                                strlcat(msg, *pp++, sizeof(msg));
                    358:                        }
                    359:                        new_message(MT_delayed, msg);
1.62      otto      360:                        order_index = 0;
1.20      deraadt   361:                }
1.1       downsj    362:        }
                    363:
1.20      deraadt   364:        /* initialize termcap */
                    365:        init_termcap(interactive);
                    366:
                    367:        /* initialize display interface */
1.58      otto      368:        max_topn = display_init(&statics);
                    369:
1.20      deraadt   370:        /* print warning if user requested more processes than we can display */
1.60      otto      371:        if (topn > max_topn)
                    372:                new_message(MT_delayed,
                    373:                    "warning: this terminal can only display %d processes",
1.22      deraadt   374:                    max_topn);
1.20      deraadt   375:        /* adjust for topn == Infinity */
                    376:        if (topn == Infinity) {
                    377:                /*
                    378:                 *  For smart terminals, infinity really means everything that can
                    379:                 *  be displayed, or Largest.
                    380:                 *  On dumb terminals, infinity means every process in the system!
                    381:                 *  We only really want to do that if it was explicitly specified.
                    382:                 *  This is always the case when "Default_TOPN != Infinity".  But if
                    383:                 *  topn wasn't explicitly specified and we are on a dumb terminal
                    384:                 *  and the default is Infinity, then (and only then) we use
                    385:                 *  "Nominal_TOPN" instead.
                    386:                 */
1.1       downsj    387: #if Default_TOPN == Infinity
1.20      deraadt   388:                topn = smart_terminal ? Largest :
                    389:                    (topn_specified ? Largest : Nominal_TOPN);
                    390: #else
                    391:                topn = Largest;
1.1       downsj    392: #endif
                    393:        }
1.20      deraadt   394:        /* set header display accordingly */
                    395:        display_header(topn > 0);
1.1       downsj    396:
1.20      deraadt   397:        /* determine interactive state */
                    398:        if (interactive == Maybe)
                    399:                interactive = smart_terminal;
1.1       downsj    400:
1.20      deraadt   401:        /* if # of displays not specified, fill it in */
                    402:        if (displays == 0)
                    403:                displays = smart_terminal ? Infinity : 1;
1.1       downsj    404:
                    405:        /*
1.20      deraadt   406:         * block interrupt signals while setting up the screen and the
                    407:         * handlers
1.1       downsj    408:         */
1.20      deraadt   409:        sigemptyset(&mask);
                    410:        sigaddset(&mask, SIGINT);
                    411:        sigaddset(&mask, SIGQUIT);
                    412:        sigaddset(&mask, SIGTSTP);
                    413:        sigprocmask(SIG_BLOCK, &mask, &oldmask);
1.57      otto      414:        if (interactive)
                    415:                init_screen();
1.90      deraadt   416:        if (pledge("stdio getpw tty proc ps vminfo", NULL) == -1)
                    417:                err(1, "pledge");
1.20      deraadt   418:        (void) signal(SIGINT, leave);
1.30      deraadt   419:        siginterrupt(SIGINT, 1);
1.20      deraadt   420:        (void) signal(SIGQUIT, leave);
                    421:        (void) signal(SIGTSTP, tstop);
1.51      otto      422:        if (smart_terminal)
                    423:                (void) signal(SIGWINCH, sigwinch);
1.20      deraadt   424:        sigprocmask(SIG_SETMASK, &oldmask, NULL);
1.7       deraadt   425: restart:
1.1       downsj    426:
1.20      deraadt   427:        /*
                    428:         *  main loop -- repeat while display count is positive or while it
                    429:         *              indicates infinity (by being -1)
                    430:         */
                    431:        while ((displays == -1) || (displays-- > 0)) {
1.70      otto      432:                if (winchflag) {
                    433:                        /*
                    434:                         * reascertain the screen
                    435:                         * dimensions
                    436:                         */
                    437:                        get_screensize();
                    438:                        resizeterm(screen_length, screen_width + 1);
                    439:
                    440:                        /* tell display to resize */
                    441:                        max_topn = display_resize();
                    442:
                    443:                        /* reset the signal handler */
                    444:                        (void) signal(SIGWINCH, sigwinch);
                    445:
                    446:                        reset_display();
                    447:                        winchflag = 0;
                    448:                }
                    449:
1.20      deraadt   450:                /* get the current stats */
                    451:                get_system_info(&system_info);
                    452:
                    453:                /* get the current set of processes */
                    454:                processes = get_process_info(&system_info, &ps,
                    455:                    proc_compares[order_index]);
                    456:
                    457:                /* display the load averages */
1.51      otto      458:                i_loadave(system_info.last_pid, system_info.load_avg);
1.20      deraadt   459:
                    460:                /* display the current time */
                    461:                /* this method of getting the time SHOULD be fairly portable */
                    462:                time(&curr_time);
                    463:                i_timeofday(&curr_time);
                    464:
1.80      guenther  465:                /* display process/threads state breakdown */
                    466:                i_procstates(system_info.p_total, system_info.procstates,
                    467:                    ps.threads);
1.20      deraadt   468:
                    469:                /* display the cpu state percentage breakdown */
1.94    ! cheloha   470:                i_cpustates(system_info.cpustates);
1.1       downsj    471:
1.20      deraadt   472:                /* display memory stats */
1.51      otto      473:                i_memory(system_info.memory);
1.1       downsj    474:
1.20      deraadt   475:                /* handle message area */
1.51      otto      476:                i_message();
1.1       downsj    477:
1.83      mpi       478:                /* get the string to use for the process area header */
                    479:                header_text = format_header(uname_field, ps.threads);
                    480:
1.20      deraadt   481:                /* update the header area */
1.51      otto      482:                i_header(header_text);
1.20      deraadt   483:
1.63      otto      484:                if (topn == Infinity) {
                    485: #if Default_TOPN == Infinity
                    486:                        topn = smart_terminal ? Largest :
                    487:                            (topn_specified ? Largest : Nominal_TOPN);
                    488: #else
                    489:                        topn = Largest;
                    490: #endif
                    491:                }
                    492:
1.20      deraadt   493:                if (topn > 0) {
                    494:                        /* determine number of processes to actually display */
                    495:                        /*
                    496:                         * this number will be the smallest of:  active
                    497:                         * processes, number user requested, number current
                    498:                         * screen accommodates
                    499:                         */
                    500:                        active_procs = system_info.p_active;
                    501:                        if (active_procs > topn)
                    502:                                active_procs = topn;
                    503:                        if (active_procs > max_topn)
                    504:                                active_procs = max_topn;
                    505:                        /* now show the top "n" processes. */
1.51      otto      506:                        for (i = 0; i < active_procs; i++) {
                    507:                                pid_t pid;
                    508:                                char * s;
                    509:
                    510:                                s = format_next_process(processes, get_userid,
1.83      mpi       511:                                    &pid, ps.threads);
1.51      otto      512:                                i_process(i, s, pid == hlpid);
                    513:                        }
1.54      otto      514:                }
1.20      deraadt   515:
                    516:                /* do end-screen processing */
1.52      deraadt   517:                u_endscreen();
1.20      deraadt   518:
                    519:                /* now, flush the output buffer */
                    520:                fflush(stdout);
                    521:
1.51      otto      522:                if (smart_terminal)
                    523:                        refresh();
                    524:
1.20      deraadt   525:                /* only do the rest if we have more displays to show */
                    526:                if (displays) {
                    527:                        /* switch out for new display on smart terminals */
                    528:                        no_command = Yes;
                    529:                        if (!interactive) {
                    530:                                /* set up alarm */
                    531:                                (void) signal(SIGALRM, onalrm);
                    532:                                (void) alarm((unsigned) delay);
                    533:
                    534:                                /* wait for the rest of it .... */
                    535:                                pause();
1.42      otto      536:                                if (leaveflag)
                    537:                                        exit(0);
                    538:                                if (tstopflag) {
                    539:                                        (void) signal(SIGTSTP, SIG_DFL);
                    540:                                        (void) kill(0, SIGTSTP);
                    541:                                        /* reset the signal handler */
                    542:                                        (void) signal(SIGTSTP, tstop);
                    543:                                        tstopflag = 0;
                    544:                                }
1.20      deraadt   545:                        } else {
                    546:                                while (no_command)
                    547:                                        if (rundisplay())
                    548:                                                goto restart;
                    549:                        }
                    550:                }
                    551:        }
1.1       downsj    552:
1.20      deraadt   553:        quit(0);
                    554:        /* NOTREACHED */
                    555:        return (0);
                    556: }
1.7       deraadt   557:
1.20      deraadt   558: int
                    559: rundisplay(void)
                    560: {
1.72      lum       561:        static char tempbuf[TEMPBUFSIZE];
1.20      deraadt   562:        sigset_t mask;
                    563:        char ch, *iptr;
                    564:        int change, i;
1.28      deraadt   565:        struct pollfd pfd[1];
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] == '-') {
1.92      millert   806:                                        if (uid_from_user(tempbuf+1, &ps.huid) == -1) {
1.78      brynet    807:                                                new_message(MT_standout,
                    808:                                                    " %s: unknown user", tempbuf+1);
                    809:                                                no_command = Yes;
                    810:                                        } else {
                    811:                                                ps.uid = (uid_t)-1;
                    812:                                        }
1.92      millert   813:                                } else if (uid_from_user(tempbuf, &ps.uid) == -1) {
1.78      brynet    814:                                                new_message(MT_standout,
1.89      deraadt   815:                                                    " %s: unknown user", tempbuf);
1.78      brynet    816:                                                no_command = Yes;
                    817:                                } else {
                    818:                                        ps.huid = (uid_t)-1;
                    819:                                }
1.51      otto      820:                                putr();
1.20      deraadt   821:                        } else
                    822:                                clear_message();
                    823:                        break;
1.12      fgsch     824:
1.20      deraadt   825:                case CMD_system:
                    826:                        ps.system = !ps.system;
1.35      otto      827:                        old_system = ps.system;
1.20      deraadt   828:                        new_message(MT_standout | MT_delayed,
                    829:                            " %sisplaying system processes.",
                    830:                            ps.system ? "D" : "Not d");
                    831:                        break;
                    832:
                    833:                case CMD_order:
                    834:                        new_message(MT_standout,
                    835:                            "Order to sort: ");
1.63      otto      836:                        if (readline(tempbuf, sizeof(tempbuf)) > 0) {
1.56      otto      837:                                if ((i = string_index(tempbuf,
1.20      deraadt   838:                                    statics.order_names)) == -1) {
                    839:                                        new_message(MT_standout,
                    840:                                            " %s: unrecognized sorting order",
1.56      otto      841:                                            tempbuf);
1.20      deraadt   842:                                        no_command = Yes;
                    843:                                } else
1.1       downsj    844:                                        order_index = i;
1.51      otto      845:                                putr();
1.35      otto      846:                        } else
                    847:                                clear_message();
                    848:                        break;
                    849:
                    850:                case CMD_pid:
1.37      jaredy    851:                        new_message(MT_standout, "Process ID to show: ");
1.63      otto      852:                        if (readline(tempbuf, sizeof(tempbuf)) > 0) {
1.56      otto      853:                                if (tempbuf[0] == '+' &&
                    854:                                    tempbuf[1] == '\0') {
1.35      otto      855:                                        ps.pid = (pid_t)-1;
                    856:                                        ps.system = old_system;
                    857:                                } else {
                    858:                                        unsigned long long num;
                    859:                                        const char *errstr;
                    860:
1.56      otto      861:                                        num = strtonum(tempbuf, 0, INT_MAX,
1.35      otto      862:                                            &errstr);
                    863:                                        if (errstr != NULL || !find_pid(num)) {
                    864:                                                new_message(MT_standout,
                    865:                                                    " %s: unknown pid",
1.56      otto      866:                                                    tempbuf);
1.35      otto      867:                                                no_command = Yes;
                    868:                                        } else {
                    869:                                                if (ps.system == No)
                    870:                                                        old_system = No;
                    871:                                                ps.pid = (pid_t)num;
                    872:                                                ps.system = Yes;
                    873:                                        }
                    874:                                }
1.51      otto      875:                                putr();
1.20      deraadt   876:                        } else
                    877:                                clear_message();
1.40      markus    878:                        break;
                    879:
                    880:                case CMD_command:
                    881:                        show_args = (show_args == No) ? Yes : No;
1.41      tedu      882:                        break;
1.89      deraadt   883:
1.41      tedu      884:                case CMD_threads:
                    885:                        ps.threads = !ps.threads;
                    886:                        old_threads = ps.threads;
                    887:                        new_message(MT_standout | MT_delayed,
                    888:                            " %sisplaying threads.",
                    889:                            ps.threads ? "D" : "Not d");
1.45      otto      890:                        break;
                    891:
                    892:                case CMD_grep:
                    893:                        new_message(MT_standout,
                    894:                            "Grep command name: ");
1.63      otto      895:                        if (readline(tempbuf, sizeof(tempbuf)) > 0) {
1.45      otto      896:                                free(ps.command);
1.56      otto      897:                                if (tempbuf[0] == '+' &&
                    898:                                    tempbuf[1] == '\0')
1.45      otto      899:                                        ps.command = NULL;
                    900:                                else
1.75      deraadt   901:                                        if ((ps.command = strdup(tempbuf)) ==
                    902:                                            NULL)
1.74      lum       903:                                                err(1, NULL);
1.51      otto      904:                                putr();
                    905:                        } else
                    906:                                clear_message();
                    907:                        break;
                    908:
                    909:                case CMD_hl:
1.53      otto      910:                        new_message(MT_standout, "Process ID to highlight: ");
1.63      otto      911:                        if (readline(tempbuf, sizeof(tempbuf)) > 0) {
1.56      otto      912:                                if (tempbuf[0] == '+' &&
                    913:                                    tempbuf[1] == '\0') {
1.51      otto      914:                                        hlpid = -1;
                    915:                                } else {
                    916:                                        unsigned long long num;
                    917:                                        const char *errstr;
                    918:
1.56      otto      919:                                        num = strtonum(tempbuf, 0, INT_MAX,
1.51      otto      920:                                            &errstr);
                    921:                                        if (errstr != NULL || !find_pid(num)) {
                    922:                                                new_message(MT_standout,
                    923:                                                    " %s: unknown pid",
1.56      otto      924:                                                    tempbuf);
1.51      otto      925:                                                no_command = Yes;
                    926:                                        } else
                    927:                                                hlpid = (pid_t)num;
                    928:                                }
                    929:                                putr();
1.45      otto      930:                        } else
                    931:                                clear_message();
1.20      deraadt   932:                        break;
1.1       downsj    933:
1.47      otto      934:                case CMD_add:
                    935:                        ps.uid = (uid_t)-1;     /* uid */
1.78      brynet    936:                        ps.huid = (uid_t)-1;
1.89      deraadt   937:                        ps.pid = (pid_t)-1;     /* pid */
1.47      otto      938:                        ps.system = old_system;
                    939:                        ps.command = NULL;      /* grep */
1.51      otto      940:                        hlpid = -1;
1.47      otto      941:                        break;
1.68      tedu      942:                case CMD_cpus:
                    943:                        combine_cpus = !combine_cpus;
                    944:                        max_topn = display_resize();
                    945:                        reset_display();
                    946:                        break;
1.20      deraadt   947:                default:
                    948:                        new_message(MT_standout, " BAD CASE IN SWITCH!");
1.51      otto      949:                        putr();
1.1       downsj    950:                }
                    951:        }
                    952:
1.20      deraadt   953:        /* flush out stuff that may have been written */
                    954:        fflush(stdout);
                    955:        return 0;
1.1       downsj    956: }
                    957:
1.20      deraadt   958:
1.1       downsj    959: /*
                    960:  *  reset_display() - reset all the display routine pointers so that entire
                    961:  *     screen will get redrawn.
                    962:  */
1.19      pvalchev  963: static void
                    964: reset_display(void)
1.1       downsj    965: {
1.51      otto      966:        if (smart_terminal) {
                    967:                clear();
                    968:                refresh();
                    969:        }
1.1       downsj    970: }
                    971:
1.34      deraadt   972: /* ARGSUSED */
1.19      pvalchev  973: void
1.20      deraadt   974: leave(int signo)
1.1       downsj    975: {
1.20      deraadt   976:        leaveflag = 1;
1.1       downsj    977: }
                    978:
1.34      deraadt   979: /* ARGSUSED */
1.19      pvalchev  980: void
1.20      deraadt   981: tstop(int signo)
1.1       downsj    982: {
1.20      deraadt   983:        tstopflag = 1;
1.1       downsj    984: }
                    985:
1.34      deraadt   986: /* ARGSUSED */
1.19      pvalchev  987: void
1.51      otto      988: sigwinch(int signo)
1.1       downsj    989: {
1.20      deraadt   990:        winchflag = 1;
1.1       downsj    991: }
                    992:
1.34      deraadt   993: /* ARGSUSED */
1.19      pvalchev  994: void
1.20      deraadt   995: onalrm(int signo)
1.1       downsj    996: {
                    997: }
                    998:
1.19      pvalchev  999: void
1.20      deraadt  1000: quit(int ret)
1.1       downsj   1001: {
1.20      deraadt  1002:        end_screen();
                   1003:        exit(ret);
1.1       downsj   1004: }