Annotation of src/usr.bin/top/top.c, Revision 1.61
1.61 ! otto 1: /* $OpenBSD: top.c,v 1.60 2007/11/04 18:45: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.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.20 deraadt 331: }
1.1 downsj 332: }
333:
1.20 deraadt 334: /* initialize termcap */
335: init_termcap(interactive);
336:
337: /* get the string to use for the process area header */
338: header_text = format_header(uname_field);
339:
340: /* initialize display interface */
1.58 otto 341: max_topn = display_init(&statics);
342:
1.20 deraadt 343: /* print warning if user requested more processes than we can display */
1.60 otto 344: if (topn > max_topn)
345: new_message(MT_delayed,
346: "warning: this terminal can only display %d processes",
1.22 deraadt 347: max_topn);
1.20 deraadt 348: /* adjust for topn == Infinity */
349: if (topn == Infinity) {
350: /*
351: * For smart terminals, infinity really means everything that can
352: * be displayed, or Largest.
353: * On dumb terminals, infinity means every process in the system!
354: * We only really want to do that if it was explicitly specified.
355: * This is always the case when "Default_TOPN != Infinity". But if
356: * topn wasn't explicitly specified and we are on a dumb terminal
357: * and the default is Infinity, then (and only then) we use
358: * "Nominal_TOPN" instead.
359: */
1.1 downsj 360: #if Default_TOPN == Infinity
1.20 deraadt 361: topn = smart_terminal ? Largest :
362: (topn_specified ? Largest : Nominal_TOPN);
363: #else
364: topn = Largest;
1.1 downsj 365: #endif
366: }
1.20 deraadt 367: /* set header display accordingly */
368: display_header(topn > 0);
1.1 downsj 369:
1.20 deraadt 370: /* determine interactive state */
371: if (interactive == Maybe)
372: interactive = smart_terminal;
1.1 downsj 373:
1.20 deraadt 374: /* if # of displays not specified, fill it in */
375: if (displays == 0)
376: displays = smart_terminal ? Infinity : 1;
1.1 downsj 377:
378: /*
1.20 deraadt 379: * block interrupt signals while setting up the screen and the
380: * handlers
1.1 downsj 381: */
1.20 deraadt 382: sigemptyset(&mask);
383: sigaddset(&mask, SIGINT);
384: sigaddset(&mask, SIGQUIT);
385: sigaddset(&mask, SIGTSTP);
386: sigprocmask(SIG_BLOCK, &mask, &oldmask);
1.57 otto 387: if (interactive)
388: init_screen();
1.20 deraadt 389: (void) signal(SIGINT, leave);
1.30 deraadt 390: siginterrupt(SIGINT, 1);
1.20 deraadt 391: (void) signal(SIGQUIT, leave);
392: (void) signal(SIGTSTP, tstop);
1.51 otto 393: if (smart_terminal)
394: (void) signal(SIGWINCH, sigwinch);
1.20 deraadt 395: sigprocmask(SIG_SETMASK, &oldmask, NULL);
1.7 deraadt 396: restart:
1.1 downsj 397:
1.20 deraadt 398: /*
399: * main loop -- repeat while display count is positive or while it
400: * indicates infinity (by being -1)
401: */
402: while ((displays == -1) || (displays-- > 0)) {
403: /* get the current stats */
404: get_system_info(&system_info);
405:
406: /* get the current set of processes */
407: processes = get_process_info(&system_info, &ps,
408: proc_compares[order_index]);
409:
410: /* display the load averages */
1.51 otto 411: i_loadave(system_info.last_pid, system_info.load_avg);
1.20 deraadt 412:
413: /* display the current time */
414: /* this method of getting the time SHOULD be fairly portable */
415: time(&curr_time);
416: i_timeofday(&curr_time);
417:
418: /* display process state breakdown */
1.51 otto 419: i_procstates(system_info.p_total, system_info.procstates);
1.20 deraadt 420:
421: /* display the cpu state percentage breakdown */
1.51 otto 422: i_cpustates(system_info.cpustates);
1.1 downsj 423:
1.20 deraadt 424: /* display memory stats */
1.51 otto 425: i_memory(system_info.memory);
1.1 downsj 426:
1.20 deraadt 427: /* handle message area */
1.51 otto 428: i_message();
1.1 downsj 429:
1.20 deraadt 430: /* update the header area */
1.51 otto 431: i_header(header_text);
1.20 deraadt 432:
433: if (topn > 0) {
434: /* determine number of processes to actually display */
435: /*
436: * this number will be the smallest of: active
437: * processes, number user requested, number current
438: * screen accommodates
439: */
440: active_procs = system_info.p_active;
441: if (active_procs > topn)
442: active_procs = topn;
443: if (active_procs > max_topn)
444: active_procs = max_topn;
445: /* now show the top "n" processes. */
1.51 otto 446: for (i = 0; i < active_procs; i++) {
447: pid_t pid;
448: char * s;
449:
450: s = format_next_process(processes, get_userid,
451: &pid);
452: i_process(i, s, pid == hlpid);
453: }
1.54 otto 454: }
1.20 deraadt 455:
456: /* do end-screen processing */
1.52 deraadt 457: u_endscreen();
1.20 deraadt 458:
459: /* now, flush the output buffer */
460: fflush(stdout);
461:
1.51 otto 462: if (smart_terminal)
463: refresh();
464:
1.20 deraadt 465: /* only do the rest if we have more displays to show */
466: if (displays) {
467: /* switch out for new display on smart terminals */
468: no_command = Yes;
469: if (!interactive) {
470: /* set up alarm */
471: (void) signal(SIGALRM, onalrm);
472: (void) alarm((unsigned) delay);
473:
474: /* wait for the rest of it .... */
475: pause();
1.42 otto 476: if (leaveflag)
477: exit(0);
478: if (tstopflag) {
479: (void) signal(SIGTSTP, SIG_DFL);
480: (void) kill(0, SIGTSTP);
481: /* reset the signal handler */
482: (void) signal(SIGTSTP, tstop);
483: tstopflag = 0;
484: }
1.20 deraadt 485: } else {
486: while (no_command)
487: if (rundisplay())
488: goto restart;
489: }
490: }
491: }
1.1 downsj 492:
1.20 deraadt 493: quit(0);
494: /* NOTREACHED */
495: return (0);
496: }
1.7 deraadt 497:
1.20 deraadt 498: int
499: rundisplay(void)
500: {
1.56 otto 501: static char tempbuf[50];
1.20 deraadt 502: sigset_t mask;
503: char ch, *iptr;
504: int change, i;
1.28 deraadt 505: struct pollfd pfd[1];
1.21 millert 506: uid_t uid;
1.51 otto 507: static char command_chars[] = "\f qh?en#sdkriIuSopCTg+P";
1.7 deraadt 508:
1.20 deraadt 509: /*
510: * assume valid command unless told
511: * otherwise
512: */
513: no_command = No;
1.7 deraadt 514:
1.20 deraadt 515: /*
516: * set up arguments for select with
517: * timeout
518: */
1.28 deraadt 519: pfd[0].fd = STDIN_FILENO;
520: pfd[0].events = POLLIN;
1.20 deraadt 521:
1.42 otto 522: if (leaveflag)
523: quit(0);
1.20 deraadt 524: if (tstopflag) {
525: /* move to the lower left */
526: end_screen();
527: fflush(stdout);
528:
529: /*
530: * default the signal handler
531: * action
532: */
533: (void) signal(SIGTSTP, SIG_DFL);
534:
535: /*
536: * unblock the signal and
537: * send ourselves one
538: */
539: sigemptyset(&mask);
540: sigaddset(&mask, SIGTSTP);
541: sigprocmask(SIG_UNBLOCK, &mask, NULL);
542: (void) kill(0, SIGTSTP);
543:
544: /* reset the signal handler */
545: (void) signal(SIGTSTP, tstop);
546:
547: /* reinit screen */
548: reinit_screen();
549: reset_display();
550: tstopflag = 0;
551: return 1;
552: }
553: if (winchflag) {
554: /*
555: * reascertain the screen
556: * dimensions
557: */
558: get_screensize();
1.51 otto 559: resizeterm(screen_length, screen_width + 1);
1.20 deraadt 560:
561: /* tell display to resize */
562: max_topn = display_resize();
563:
564: /* reset the signal handler */
1.51 otto 565: (void) signal(SIGWINCH, sigwinch);
1.20 deraadt 566:
567: reset_display();
568: winchflag = 0;
569: return 1;
570: }
571: /*
572: * wait for either input or the end
573: * of the delay period
574: */
1.33 deraadt 575: if (poll(pfd, 1, (int)(delay * 1000)) > 0 &&
1.31 millert 576: !(pfd[0].revents & (POLLERR|POLLHUP|POLLNVAL))) {
1.20 deraadt 577: char *errmsg;
1.30 deraadt 578: ssize_t len;
1.20 deraadt 579: int newval;
580:
581: clear_message();
582:
583: /*
584: * now read it and convert to
585: * command strchr
586: */
1.30 deraadt 587: while (1) {
1.36 deraadt 588: len = read(STDIN_FILENO, &ch, 1);
1.30 deraadt 589: if (len == -1 && errno == EINTR)
590: continue;
591: if (len == 0)
592: exit(1);
593: break;
594: }
1.20 deraadt 595: if ((iptr = strchr(command_chars, ch)) == NULL) {
1.1 downsj 596: /* illegal command */
597: new_message(MT_standout, " Command not understood");
1.51 otto 598: putr();
1.1 downsj 599: no_command = Yes;
1.20 deraadt 600: fflush(stdout);
601: return (0);
602: }
603:
604: change = iptr - command_chars;
605:
606: switch (change) {
607: case CMD_redraw: /* redraw screen */
608: reset_display();
609: break;
610:
611: case CMD_update: /* merely update display */
612: /*
613: * is the load average high?
614: */
615: if (system_info.load_avg[0] > LoadMax) {
616: /* yes, go home for visual feedback */
617: go_home();
618: fflush(stdout);
1.1 downsj 619: }
1.20 deraadt 620: break;
621:
622: case CMD_quit: /* quit */
623: quit(0);
624: break;
625:
626: case CMD_help1: /* help */
627: case CMD_help2:
628: clear();
629: show_help();
1.51 otto 630: anykey();
631: clear();
1.20 deraadt 632: break;
633:
634: case CMD_errors: /* show errors */
635: if (error_count() == 0) {
636: new_message(MT_standout,
637: " Currently no errors to report.");
1.51 otto 638: putr();
1.20 deraadt 639: no_command = Yes;
640: } else {
1.1 downsj 641: clear();
1.20 deraadt 642: show_errors();
1.51 otto 643: anykey();
644: clear();
1.20 deraadt 645: }
646: break;
647:
648: case CMD_number1: /* new number */
649: case CMD_number2:
650: new_message(MT_standout,
651: "Number of processes to show: ");
1.56 otto 652: newval = readline(tempbuf, 8, Yes);
1.20 deraadt 653: if (newval > -1) {
654: if (newval > max_topn) {
1.1 downsj 655: new_message(MT_standout | MT_delayed,
1.20 deraadt 656: " This terminal can only "
657: "display %d processes.",
658: max_topn);
1.51 otto 659: putr();
1.20 deraadt 660: }
661: if (newval == 0)
1.1 downsj 662: display_header(No);
1.20 deraadt 663: else if (newval > topn && topn == 0) {
1.1 downsj 664: /* redraw the header */
665: display_header(Yes);
666: }
1.20 deraadt 667: topn = newval;
668: }
669: break;
670:
671: case CMD_delay: /* new seconds delay */
672: new_message(MT_standout, "Seconds to delay: ");
1.56 otto 673: if (readline(tempbuf, sizeof(tempbuf), No) > 0) {
1.20 deraadt 674: char *endp;
1.56 otto 675: double newdelay = strtod(tempbuf, &endp);
1.20 deraadt 676:
677: if (newdelay >= 0 && newdelay < 1000000 &&
1.48 otto 678: *endp == '\0') {
1.16 hugh 679: delay = newdelay;
1.48 otto 680: } else {
681: new_message(MT_standout,
682: "Delay should be a non-negative number");
1.51 otto 683: putr();
1.48 otto 684: no_command = Yes;
685: }
686:
687: } else
688: clear_message();
1.20 deraadt 689: break;
690:
691: case CMD_displays: /* change display count */
692: new_message(MT_standout,
693: "Displays to show (currently %s): ",
694: displays == -1 ? "infinite" :
695: itoa(displays));
1.56 otto 696: if ((i = readline(tempbuf, 10, Yes)) > 0)
1.20 deraadt 697: displays = i;
698: else if (i == 0)
699: quit(0);
700:
701: clear_message();
702: break;
703:
704: case CMD_kill: /* kill program */
705: new_message(0, "kill ");
1.56 otto 706: if (readline(tempbuf, sizeof(tempbuf), No) > 0) {
707: if ((errmsg = kill_procs(tempbuf)) != NULL) {
1.4 millert 708: new_message(MT_standout, "%s", errmsg);
1.51 otto 709: putr();
1.1 downsj 710: no_command = Yes;
711: }
1.20 deraadt 712: } else
713: clear_message();
714: break;
715:
716: case CMD_renice: /* renice program */
717: new_message(0, "renice ");
1.56 otto 718: if (readline(tempbuf, sizeof(tempbuf), No) > 0) {
719: if ((errmsg = renice_procs(tempbuf)) != NULL) {
1.4 millert 720: new_message(MT_standout, "%s", errmsg);
1.51 otto 721: putr();
1.1 downsj 722: no_command = Yes;
723: }
1.20 deraadt 724: } else
725: clear_message();
726: break;
1.1 downsj 727:
1.20 deraadt 728: case CMD_idletog:
729: case CMD_idletog2:
730: ps.idle = !ps.idle;
731: new_message(MT_standout | MT_delayed,
732: " %sisplaying idle processes.",
733: ps.idle ? "D" : "Not d");
1.51 otto 734: putr();
1.20 deraadt 735: break;
1.1 downsj 736:
1.20 deraadt 737: case CMD_user:
738: new_message(MT_standout,
739: "Username to show: ");
1.56 otto 740: if (readline(tempbuf, sizeof(tempbuf), No) > 0) {
741: if (tempbuf[0] == '+' &&
742: tempbuf[1] == '\0') {
1.21 millert 743: ps.uid = (uid_t)-1;
1.56 otto 744: } else if ((uid = userid(tempbuf)) == (uid_t)-1) {
1.1 downsj 745: new_message(MT_standout,
1.56 otto 746: " %s: unknown user", tempbuf);
1.1 downsj 747: no_command = Yes;
1.20 deraadt 748: } else
1.21 millert 749: ps.uid = uid;
1.51 otto 750: putr();
1.20 deraadt 751: } else
752: clear_message();
753: break;
1.12 fgsch 754:
1.20 deraadt 755: case CMD_system:
756: ps.system = !ps.system;
1.35 otto 757: old_system = ps.system;
1.20 deraadt 758: new_message(MT_standout | MT_delayed,
759: " %sisplaying system processes.",
760: ps.system ? "D" : "Not d");
761: break;
762:
763: case CMD_order:
764: new_message(MT_standout,
765: "Order to sort: ");
1.56 otto 766: if (readline(tempbuf, sizeof(tempbuf), No) > 0) {
767: if ((i = string_index(tempbuf,
1.20 deraadt 768: statics.order_names)) == -1) {
769: new_message(MT_standout,
770: " %s: unrecognized sorting order",
1.56 otto 771: tempbuf);
1.20 deraadt 772: no_command = Yes;
773: } else
1.1 downsj 774: order_index = i;
1.51 otto 775: putr();
1.35 otto 776: } else
777: clear_message();
778: break;
779:
780: case CMD_pid:
1.37 jaredy 781: new_message(MT_standout, "Process ID to show: ");
1.56 otto 782: if (readline(tempbuf, sizeof(tempbuf), No) > 0) {
783: if (tempbuf[0] == '+' &&
784: tempbuf[1] == '\0') {
1.35 otto 785: ps.pid = (pid_t)-1;
786: ps.system = old_system;
787: } else {
788: unsigned long long num;
789: const char *errstr;
790:
1.56 otto 791: num = strtonum(tempbuf, 0, INT_MAX,
1.35 otto 792: &errstr);
793: if (errstr != NULL || !find_pid(num)) {
794: new_message(MT_standout,
795: " %s: unknown pid",
1.56 otto 796: tempbuf);
1.35 otto 797: no_command = Yes;
798: } else {
799: if (ps.system == No)
800: old_system = No;
801: ps.pid = (pid_t)num;
802: ps.system = Yes;
803: }
804: }
1.51 otto 805: putr();
1.20 deraadt 806: } else
807: clear_message();
1.40 markus 808: break;
809:
810: case CMD_command:
811: show_args = (show_args == No) ? Yes : No;
1.41 tedu 812: break;
813:
814: case CMD_threads:
815: ps.threads = !ps.threads;
816: old_threads = ps.threads;
817: new_message(MT_standout | MT_delayed,
818: " %sisplaying threads.",
819: ps.threads ? "D" : "Not d");
1.45 otto 820: break;
821:
822: case CMD_grep:
823: new_message(MT_standout,
824: "Grep command name: ");
1.56 otto 825: if (readline(tempbuf, sizeof(tempbuf), No) > 0) {
1.45 otto 826: free(ps.command);
1.56 otto 827: if (tempbuf[0] == '+' &&
828: tempbuf[1] == '\0')
1.45 otto 829: ps.command = NULL;
830: else
1.56 otto 831: ps.command = strdup(tempbuf);
1.51 otto 832: putr();
833: } else
834: clear_message();
835: break;
836:
837: case CMD_hl:
1.53 otto 838: new_message(MT_standout, "Process ID to highlight: ");
1.56 otto 839: if (readline(tempbuf, sizeof(tempbuf), No) > 0) {
840: if (tempbuf[0] == '+' &&
841: tempbuf[1] == '\0') {
1.51 otto 842: hlpid = -1;
843: } else {
844: unsigned long long num;
845: const char *errstr;
846:
1.56 otto 847: num = strtonum(tempbuf, 0, INT_MAX,
1.51 otto 848: &errstr);
849: if (errstr != NULL || !find_pid(num)) {
850: new_message(MT_standout,
851: " %s: unknown pid",
1.56 otto 852: tempbuf);
1.51 otto 853: no_command = Yes;
854: } else
855: hlpid = (pid_t)num;
856: }
857: putr();
1.45 otto 858: } else
859: clear_message();
1.20 deraadt 860: break;
1.1 downsj 861:
1.47 otto 862: case CMD_add:
863: ps.uid = (uid_t)-1; /* uid */
864: ps.pid = (pid_t)-1; /* pid */
865: ps.system = old_system;
866: ps.command = NULL; /* grep */
1.51 otto 867: hlpid = -1;
1.47 otto 868: break;
869:
1.20 deraadt 870: default:
871: new_message(MT_standout, " BAD CASE IN SWITCH!");
1.51 otto 872: putr();
1.1 downsj 873: }
874: }
875:
1.20 deraadt 876: /* flush out stuff that may have been written */
877: fflush(stdout);
878: return 0;
1.1 downsj 879: }
880:
1.20 deraadt 881:
1.1 downsj 882: /*
883: * reset_display() - reset all the display routine pointers so that entire
884: * screen will get redrawn.
885: */
1.19 pvalchev 886: static void
887: reset_display(void)
1.1 downsj 888: {
1.51 otto 889: if (smart_terminal) {
890: clear();
891: refresh();
892: }
1.1 downsj 893: }
894:
1.34 deraadt 895: /* ARGSUSED */
1.19 pvalchev 896: void
1.20 deraadt 897: leave(int signo)
1.1 downsj 898: {
1.20 deraadt 899: leaveflag = 1;
1.1 downsj 900: }
901:
1.34 deraadt 902: /* ARGSUSED */
1.19 pvalchev 903: void
1.20 deraadt 904: tstop(int signo)
1.1 downsj 905: {
1.20 deraadt 906: tstopflag = 1;
1.1 downsj 907: }
908:
1.34 deraadt 909: /* ARGSUSED */
1.19 pvalchev 910: void
1.51 otto 911: sigwinch(int signo)
1.1 downsj 912: {
1.20 deraadt 913: winchflag = 1;
1.1 downsj 914: }
915:
1.34 deraadt 916: /* ARGSUSED */
1.19 pvalchev 917: void
1.20 deraadt 918: onalrm(int signo)
1.1 downsj 919: {
920: }
921:
1.19 pvalchev 922: void
1.20 deraadt 923: quit(int ret)
1.1 downsj 924: {
1.20 deraadt 925: end_screen();
926: exit(ret);
1.1 downsj 927: }