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