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