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