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