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