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