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