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

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