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

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