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