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