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