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

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