Annotation of src/usr.bin/top/top.c, Revision 1.73
1.73 ! lum 1: /* $OpenBSD: top.c,v 1.72 2010/03/23 16:16:09 lum 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.72 lum 535: static char tempbuf[TEMPBUFSIZE];
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) {
1.73 ! lum 669: if ((i = atoiwi(tempbuf)) != Invalid) {
1.63 otto 670: if (i > max_topn) {
1.65 otto 671: new_message(MT_standout |
672: MT_delayed,
1.63 otto 673: " This terminal can only "
674: "display %d processes.",
675: max_topn);
676: putr();
1.65 otto 677: }
678: if ((i > topn || i == Infinity)
1.64 otto 679: && topn == 0) {
1.63 otto 680: /* redraw the header */
681: display_header(Yes);
1.65 otto 682: } else if (i == 0)
683: display_header(No);
1.63 otto 684: topn = i;
685: } else {
686: new_message(MT_standout,
1.65 otto 687: "Processes should be a "
688: "non-negative number");
1.63 otto 689: putr();
690: no_command = Yes;
1.20 deraadt 691: }
1.63 otto 692: } else
693: clear_message();
1.20 deraadt 694: break;
695:
696: case CMD_delay: /* new seconds delay */
697: new_message(MT_standout, "Seconds to delay: ");
1.63 otto 698: if (readline(tempbuf, sizeof(tempbuf)) > 0) {
1.20 deraadt 699: char *endp;
1.56 otto 700: double newdelay = strtod(tempbuf, &endp);
1.20 deraadt 701:
1.71 lum 702: if (newdelay >= 0 && newdelay <= 1000000 &&
1.48 otto 703: *endp == '\0') {
1.16 hugh 704: delay = newdelay;
1.48 otto 705: } else {
706: new_message(MT_standout,
707: "Delay should be a non-negative number");
1.51 otto 708: putr();
1.48 otto 709: no_command = Yes;
710: }
711:
712: } else
713: clear_message();
1.20 deraadt 714: break;
715:
716: case CMD_displays: /* change display count */
717: new_message(MT_standout,
718: "Displays to show (currently %s): ",
719: displays == -1 ? "infinite" :
720: itoa(displays));
721:
1.63 otto 722: if (readline(tempbuf, 10) > 0) {
1.73 ! lum 723: if ((i = atoiwi(tempbuf)) != Invalid) {
1.63 otto 724: if (i == 0)
725: quit(0);
726: displays = i;
727: } else {
728: new_message(MT_standout,
729: "Displays should be a non-negative number");
730: putr();
731: no_command = Yes;
732: }
733: } else
734: clear_message();
1.20 deraadt 735: break;
736:
737: case CMD_kill: /* kill program */
738: new_message(0, "kill ");
1.63 otto 739: if (readline(tempbuf, sizeof(tempbuf)) > 0) {
1.56 otto 740: if ((errmsg = kill_procs(tempbuf)) != NULL) {
1.4 millert 741: new_message(MT_standout, "%s", errmsg);
1.51 otto 742: putr();
1.1 downsj 743: no_command = Yes;
744: }
1.20 deraadt 745: } else
746: clear_message();
747: break;
748:
749: case CMD_renice: /* renice program */
750: new_message(0, "renice ");
1.63 otto 751: if (readline(tempbuf, sizeof(tempbuf)) > 0) {
1.56 otto 752: if ((errmsg = renice_procs(tempbuf)) != NULL) {
1.4 millert 753: new_message(MT_standout, "%s", errmsg);
1.51 otto 754: putr();
1.1 downsj 755: no_command = Yes;
756: }
1.20 deraadt 757: } else
758: clear_message();
759: break;
1.1 downsj 760:
1.20 deraadt 761: case CMD_idletog:
762: case CMD_idletog2:
763: ps.idle = !ps.idle;
764: new_message(MT_standout | MT_delayed,
765: " %sisplaying idle processes.",
766: ps.idle ? "D" : "Not d");
1.51 otto 767: putr();
1.20 deraadt 768: break;
1.1 downsj 769:
1.20 deraadt 770: case CMD_user:
771: new_message(MT_standout,
772: "Username to show: ");
1.63 otto 773: if (readline(tempbuf, sizeof(tempbuf)) > 0) {
1.56 otto 774: if (tempbuf[0] == '+' &&
775: tempbuf[1] == '\0') {
1.21 millert 776: ps.uid = (uid_t)-1;
1.56 otto 777: } else if ((uid = userid(tempbuf)) == (uid_t)-1) {
1.1 downsj 778: new_message(MT_standout,
1.56 otto 779: " %s: unknown user", tempbuf);
1.1 downsj 780: no_command = Yes;
1.20 deraadt 781: } else
1.21 millert 782: ps.uid = uid;
1.51 otto 783: putr();
1.20 deraadt 784: } else
785: clear_message();
786: break;
1.12 fgsch 787:
1.20 deraadt 788: case CMD_system:
789: ps.system = !ps.system;
1.35 otto 790: old_system = ps.system;
1.20 deraadt 791: new_message(MT_standout | MT_delayed,
792: " %sisplaying system processes.",
793: ps.system ? "D" : "Not d");
794: break;
795:
796: case CMD_order:
797: new_message(MT_standout,
798: "Order to sort: ");
1.63 otto 799: if (readline(tempbuf, sizeof(tempbuf)) > 0) {
1.56 otto 800: if ((i = string_index(tempbuf,
1.20 deraadt 801: statics.order_names)) == -1) {
802: new_message(MT_standout,
803: " %s: unrecognized sorting order",
1.56 otto 804: tempbuf);
1.20 deraadt 805: no_command = Yes;
806: } else
1.1 downsj 807: order_index = i;
1.51 otto 808: putr();
1.35 otto 809: } else
810: clear_message();
811: break;
812:
813: case CMD_pid:
1.37 jaredy 814: new_message(MT_standout, "Process ID to show: ");
1.63 otto 815: if (readline(tempbuf, sizeof(tempbuf)) > 0) {
1.56 otto 816: if (tempbuf[0] == '+' &&
817: tempbuf[1] == '\0') {
1.35 otto 818: ps.pid = (pid_t)-1;
819: ps.system = old_system;
820: } else {
821: unsigned long long num;
822: const char *errstr;
823:
1.56 otto 824: num = strtonum(tempbuf, 0, INT_MAX,
1.35 otto 825: &errstr);
826: if (errstr != NULL || !find_pid(num)) {
827: new_message(MT_standout,
828: " %s: unknown pid",
1.56 otto 829: tempbuf);
1.35 otto 830: no_command = Yes;
831: } else {
832: if (ps.system == No)
833: old_system = No;
834: ps.pid = (pid_t)num;
835: ps.system = Yes;
836: }
837: }
1.51 otto 838: putr();
1.20 deraadt 839: } else
840: clear_message();
1.40 markus 841: break;
842:
843: case CMD_command:
844: show_args = (show_args == No) ? Yes : No;
1.41 tedu 845: break;
846:
847: case CMD_threads:
848: ps.threads = !ps.threads;
849: old_threads = ps.threads;
850: new_message(MT_standout | MT_delayed,
851: " %sisplaying threads.",
852: ps.threads ? "D" : "Not d");
1.45 otto 853: break;
854:
855: case CMD_grep:
856: new_message(MT_standout,
857: "Grep command name: ");
1.63 otto 858: if (readline(tempbuf, sizeof(tempbuf)) > 0) {
1.45 otto 859: free(ps.command);
1.56 otto 860: if (tempbuf[0] == '+' &&
861: tempbuf[1] == '\0')
1.45 otto 862: ps.command = NULL;
863: else
1.56 otto 864: ps.command = strdup(tempbuf);
1.51 otto 865: putr();
866: } else
867: clear_message();
868: break;
869:
870: case CMD_hl:
1.53 otto 871: new_message(MT_standout, "Process ID to highlight: ");
1.63 otto 872: if (readline(tempbuf, sizeof(tempbuf)) > 0) {
1.56 otto 873: if (tempbuf[0] == '+' &&
874: tempbuf[1] == '\0') {
1.51 otto 875: hlpid = -1;
876: } else {
877: unsigned long long num;
878: const char *errstr;
879:
1.56 otto 880: num = strtonum(tempbuf, 0, INT_MAX,
1.51 otto 881: &errstr);
882: if (errstr != NULL || !find_pid(num)) {
883: new_message(MT_standout,
884: " %s: unknown pid",
1.56 otto 885: tempbuf);
1.51 otto 886: no_command = Yes;
887: } else
888: hlpid = (pid_t)num;
889: }
890: putr();
1.45 otto 891: } else
892: clear_message();
1.20 deraadt 893: break;
1.1 downsj 894:
1.47 otto 895: case CMD_add:
896: ps.uid = (uid_t)-1; /* uid */
897: ps.pid = (pid_t)-1; /* pid */
898: ps.system = old_system;
899: ps.command = NULL; /* grep */
1.51 otto 900: hlpid = -1;
1.47 otto 901: break;
1.68 tedu 902: case CMD_cpus:
903: combine_cpus = !combine_cpus;
904: max_topn = display_resize();
905: reset_display();
906: break;
1.20 deraadt 907: default:
908: new_message(MT_standout, " BAD CASE IN SWITCH!");
1.51 otto 909: putr();
1.1 downsj 910: }
911: }
912:
1.20 deraadt 913: /* flush out stuff that may have been written */
914: fflush(stdout);
915: return 0;
1.1 downsj 916: }
917:
1.20 deraadt 918:
1.1 downsj 919: /*
920: * reset_display() - reset all the display routine pointers so that entire
921: * screen will get redrawn.
922: */
1.19 pvalchev 923: static void
924: reset_display(void)
1.1 downsj 925: {
1.51 otto 926: if (smart_terminal) {
927: clear();
928: refresh();
929: }
1.1 downsj 930: }
931:
1.34 deraadt 932: /* ARGSUSED */
1.19 pvalchev 933: void
1.20 deraadt 934: leave(int signo)
1.1 downsj 935: {
1.20 deraadt 936: leaveflag = 1;
1.1 downsj 937: }
938:
1.34 deraadt 939: /* ARGSUSED */
1.19 pvalchev 940: void
1.20 deraadt 941: tstop(int signo)
1.1 downsj 942: {
1.20 deraadt 943: tstopflag = 1;
1.1 downsj 944: }
945:
1.34 deraadt 946: /* ARGSUSED */
1.19 pvalchev 947: void
1.51 otto 948: sigwinch(int signo)
1.1 downsj 949: {
1.20 deraadt 950: winchflag = 1;
1.1 downsj 951: }
952:
1.34 deraadt 953: /* ARGSUSED */
1.19 pvalchev 954: void
1.20 deraadt 955: onalrm(int signo)
1.1 downsj 956: {
957: }
958:
1.19 pvalchev 959: void
1.20 deraadt 960: quit(int ret)
1.1 downsj 961: {
1.20 deraadt 962: end_screen();
963: exit(ret);
1.1 downsj 964: }