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