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