Annotation of src/usr.bin/top/top.c, Revision 1.84
1.84 ! deraadt 1: /* $OpenBSD: top.c,v 1.83 2015/05/06 07:53:29 mpi 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.82 dlg 252:
253: i = getncpu();
254: if (i == -1)
255: err(1, NULL);
256:
257: if (i > 8)
258: combine_cpus = 1;
1.1 downsj 259:
1.20 deraadt 260: /* get count of top processes to display (if any) */
261: if (optind < ac) {
262: if ((topn = atoiwi(av[optind])) == Invalid) {
1.60 otto 263: new_message(MT_delayed,
264: "warning: process count should "
1.49 otto 265: "be a non-negative number -- using default");
1.46 otto 266: topn = Infinity;
1.1 downsj 267: }
1.20 deraadt 268: #if Default_TOPN == Infinity
1.1 downsj 269: else
1.20 deraadt 270: topn_specified = Yes;
271: #endif
272: }
273: }
1.1 downsj 274:
1.20 deraadt 275: struct system_info system_info;
276: struct statics statics;
1.1 downsj 277:
1.20 deraadt 278: int
279: main(int argc, char *argv[])
280: {
281: char *uname_field = "USERNAME", *header_text, *env_top;
1.77 pirofti 282: char *(*get_userid)(uid_t) = username;
283: char **preset_argv = NULL, **av = argv;
284: int preset_argc = 0, ac = argc, active_procs, i;
1.20 deraadt 285: sigset_t mask, oldmask;
286: time_t curr_time;
287: caddr_t processes;
1.1 downsj 288:
1.20 deraadt 289: /* set the buffer for stdout */
290: #ifdef DEBUG
291: setbuffer(stdout, NULL, 0);
1.1 downsj 292: #else
1.20 deraadt 293: setbuffer(stdout, stdoutbuf, sizeof stdoutbuf);
1.1 downsj 294: #endif
295:
1.20 deraadt 296: /* initialize some selection options */
297: ps.idle = Yes;
298: ps.system = No;
1.21 millert 299: ps.uid = (uid_t)-1;
1.78 brynet 300: ps.huid = (uid_t)-1;
1.35 otto 301: ps.pid = (pid_t)-1;
1.20 deraadt 302: ps.command = NULL;
303:
304: /* get preset options from the environment */
305: if ((env_top = getenv("TOP")) != NULL) {
306: av = preset_argv = argparse(env_top, &preset_argc);
307: ac = preset_argc;
308:
309: /*
310: * set the dummy argument to an explanatory message, in case
311: * getopt encounters a bad argument
312: */
313: preset_argv[0] = "while processing environment";
314: }
315: /* process options */
316: do {
317: /*
318: * if we're done doing the presets, then process the real
319: * arguments
320: */
321: if (preset_argc == 0) {
322: ac = argc;
323: av = argv;
324: optind = 1;
325: }
326: parseargs(ac, av);
327: i = preset_argc;
328: preset_argc = 0;
329: } while (i != 0);
1.84 ! deraadt 330:
! 331: if (pledge("abort stdio rpath tty ps vminfo", NULL) == -1)
! 332: err(1, "pledge");
1.20 deraadt 333:
334: /* set constants for username/uid display correctly */
335: if (!do_unames) {
336: uname_field = " UID ";
1.26 millert 337: get_userid = format_uid;
1.20 deraadt 338: }
339: /* initialize the kernel memory interface */
340: if (machine_init(&statics) == -1)
1.1 downsj 341: exit(1);
1.20 deraadt 342:
343: /* determine sorting order index, if necessary */
344: if (order_name != NULL) {
345: if ((order_index = string_index(order_name,
346: statics.order_names)) == -1) {
1.69 tedu 347: char **pp, msg[512];
1.20 deraadt 348:
1.60 otto 349: snprintf(msg, sizeof(msg),
350: "'%s' is not a recognized sorting order",
1.22 deraadt 351: order_name);
1.60 otto 352: strlcat(msg, ". Valid are:", sizeof(msg));
1.20 deraadt 353: pp = statics.order_names;
1.60 otto 354: while (*pp != NULL) {
355: strlcat(msg, " ", sizeof(msg));
356: strlcat(msg, *pp++, sizeof(msg));
357: }
358: new_message(MT_delayed, msg);
1.62 otto 359: order_index = 0;
1.20 deraadt 360: }
1.1 downsj 361: }
362:
1.20 deraadt 363: /* initialize termcap */
364: init_termcap(interactive);
365:
366: /* initialize display interface */
1.58 otto 367: max_topn = display_init(&statics);
368:
1.20 deraadt 369: /* print warning if user requested more processes than we can display */
1.60 otto 370: if (topn > max_topn)
371: new_message(MT_delayed,
372: "warning: this terminal can only display %d processes",
1.22 deraadt 373: max_topn);
1.20 deraadt 374: /* adjust for topn == Infinity */
375: if (topn == Infinity) {
376: /*
377: * For smart terminals, infinity really means everything that can
378: * be displayed, or Largest.
379: * On dumb terminals, infinity means every process in the system!
380: * We only really want to do that if it was explicitly specified.
381: * This is always the case when "Default_TOPN != Infinity". But if
382: * topn wasn't explicitly specified and we are on a dumb terminal
383: * and the default is Infinity, then (and only then) we use
384: * "Nominal_TOPN" instead.
385: */
1.1 downsj 386: #if Default_TOPN == Infinity
1.20 deraadt 387: topn = smart_terminal ? Largest :
388: (topn_specified ? Largest : Nominal_TOPN);
389: #else
390: topn = Largest;
1.1 downsj 391: #endif
392: }
1.20 deraadt 393: /* set header display accordingly */
394: display_header(topn > 0);
1.1 downsj 395:
1.20 deraadt 396: /* determine interactive state */
397: if (interactive == Maybe)
398: interactive = smart_terminal;
1.1 downsj 399:
1.20 deraadt 400: /* if # of displays not specified, fill it in */
401: if (displays == 0)
402: displays = smart_terminal ? Infinity : 1;
1.1 downsj 403:
404: /*
1.20 deraadt 405: * block interrupt signals while setting up the screen and the
406: * handlers
1.1 downsj 407: */
1.20 deraadt 408: sigemptyset(&mask);
409: sigaddset(&mask, SIGINT);
410: sigaddset(&mask, SIGQUIT);
411: sigaddset(&mask, SIGTSTP);
412: sigprocmask(SIG_BLOCK, &mask, &oldmask);
1.57 otto 413: if (interactive)
414: init_screen();
1.20 deraadt 415: (void) signal(SIGINT, leave);
1.30 deraadt 416: siginterrupt(SIGINT, 1);
1.20 deraadt 417: (void) signal(SIGQUIT, leave);
418: (void) signal(SIGTSTP, tstop);
1.51 otto 419: if (smart_terminal)
420: (void) signal(SIGWINCH, sigwinch);
1.20 deraadt 421: sigprocmask(SIG_SETMASK, &oldmask, NULL);
1.7 deraadt 422: restart:
1.1 downsj 423:
1.20 deraadt 424: /*
425: * main loop -- repeat while display count is positive or while it
426: * indicates infinity (by being -1)
427: */
428: while ((displays == -1) || (displays-- > 0)) {
1.70 otto 429: if (winchflag) {
430: /*
431: * reascertain the screen
432: * dimensions
433: */
434: get_screensize();
435: resizeterm(screen_length, screen_width + 1);
436:
437: /* tell display to resize */
438: max_topn = display_resize();
439:
440: /* reset the signal handler */
441: (void) signal(SIGWINCH, sigwinch);
442:
443: reset_display();
444: winchflag = 0;
445: }
446:
1.20 deraadt 447: /* get the current stats */
448: get_system_info(&system_info);
449:
450: /* get the current set of processes */
451: processes = get_process_info(&system_info, &ps,
452: proc_compares[order_index]);
453:
454: /* display the load averages */
1.51 otto 455: i_loadave(system_info.last_pid, system_info.load_avg);
1.20 deraadt 456:
457: /* display the current time */
458: /* this method of getting the time SHOULD be fairly portable */
459: time(&curr_time);
460: i_timeofday(&curr_time);
461:
1.80 guenther 462: /* display process/threads state breakdown */
463: i_procstates(system_info.p_total, system_info.procstates,
464: ps.threads);
1.20 deraadt 465:
466: /* display the cpu state percentage breakdown */
1.51 otto 467: i_cpustates(system_info.cpustates);
1.1 downsj 468:
1.20 deraadt 469: /* display memory stats */
1.51 otto 470: i_memory(system_info.memory);
1.1 downsj 471:
1.20 deraadt 472: /* handle message area */
1.51 otto 473: i_message();
1.1 downsj 474:
1.83 mpi 475: /* get the string to use for the process area header */
476: header_text = format_header(uname_field, ps.threads);
477:
1.20 deraadt 478: /* update the header area */
1.51 otto 479: i_header(header_text);
1.20 deraadt 480:
1.63 otto 481: if (topn == Infinity) {
482: #if Default_TOPN == Infinity
483: topn = smart_terminal ? Largest :
484: (topn_specified ? Largest : Nominal_TOPN);
485: #else
486: topn = Largest;
487: #endif
488: }
489:
1.20 deraadt 490: if (topn > 0) {
491: /* determine number of processes to actually display */
492: /*
493: * this number will be the smallest of: active
494: * processes, number user requested, number current
495: * screen accommodates
496: */
497: active_procs = system_info.p_active;
498: if (active_procs > topn)
499: active_procs = topn;
500: if (active_procs > max_topn)
501: active_procs = max_topn;
502: /* now show the top "n" processes. */
1.51 otto 503: for (i = 0; i < active_procs; i++) {
504: pid_t pid;
505: char * s;
506:
507: s = format_next_process(processes, get_userid,
1.83 mpi 508: &pid, ps.threads);
1.51 otto 509: i_process(i, s, pid == hlpid);
510: }
1.54 otto 511: }
1.20 deraadt 512:
513: /* do end-screen processing */
1.52 deraadt 514: u_endscreen();
1.20 deraadt 515:
516: /* now, flush the output buffer */
517: fflush(stdout);
518:
1.51 otto 519: if (smart_terminal)
520: refresh();
521:
1.20 deraadt 522: /* only do the rest if we have more displays to show */
523: if (displays) {
524: /* switch out for new display on smart terminals */
525: no_command = Yes;
526: if (!interactive) {
527: /* set up alarm */
528: (void) signal(SIGALRM, onalrm);
529: (void) alarm((unsigned) delay);
530:
531: /* wait for the rest of it .... */
532: pause();
1.42 otto 533: if (leaveflag)
534: exit(0);
535: if (tstopflag) {
536: (void) signal(SIGTSTP, SIG_DFL);
537: (void) kill(0, SIGTSTP);
538: /* reset the signal handler */
539: (void) signal(SIGTSTP, tstop);
540: tstopflag = 0;
541: }
1.20 deraadt 542: } else {
543: while (no_command)
544: if (rundisplay())
545: goto restart;
546: }
547: }
548: }
1.1 downsj 549:
1.20 deraadt 550: quit(0);
551: /* NOTREACHED */
552: return (0);
553: }
1.7 deraadt 554:
1.20 deraadt 555: int
556: rundisplay(void)
557: {
1.72 lum 558: static char tempbuf[TEMPBUFSIZE];
1.20 deraadt 559: sigset_t mask;
560: char ch, *iptr;
561: int change, i;
1.28 deraadt 562: struct pollfd pfd[1];
1.78 brynet 563: uid_t uid, huid;
1.76 jsing 564: static char command_chars[] = "\f qh?en#sdkriIuSopCHg+P1";
1.7 deraadt 565:
1.20 deraadt 566: /*
567: * assume valid command unless told
568: * otherwise
569: */
570: no_command = No;
1.7 deraadt 571:
1.20 deraadt 572: /*
573: * set up arguments for select with
574: * timeout
575: */
1.28 deraadt 576: pfd[0].fd = STDIN_FILENO;
577: pfd[0].events = POLLIN;
1.20 deraadt 578:
1.42 otto 579: if (leaveflag)
580: quit(0);
1.20 deraadt 581: if (tstopflag) {
582: /* move to the lower left */
583: end_screen();
584: fflush(stdout);
585:
586: /*
587: * default the signal handler
588: * action
589: */
590: (void) signal(SIGTSTP, SIG_DFL);
591:
592: /*
593: * unblock the signal and
594: * send ourselves one
595: */
596: sigemptyset(&mask);
597: sigaddset(&mask, SIGTSTP);
598: sigprocmask(SIG_UNBLOCK, &mask, NULL);
599: (void) kill(0, SIGTSTP);
600:
601: /* reset the signal handler */
602: (void) signal(SIGTSTP, tstop);
603:
604: /* reinit screen */
605: reinit_screen();
606: reset_display();
607: tstopflag = 0;
608: return 1;
609: }
610: /*
611: * wait for either input or the end
612: * of the delay period
613: */
1.81 millert 614: if (poll(pfd, 1, (int)(delay * 1000)) > 0) {
1.20 deraadt 615: char *errmsg;
1.30 deraadt 616: ssize_t len;
1.81 millert 617:
618: if ((pfd[0].revents & (POLLERR|POLLHUP|POLLNVAL)))
619: exit(1);
1.20 deraadt 620:
621: clear_message();
622:
623: /*
624: * now read it and convert to
625: * command strchr
626: */
1.30 deraadt 627: while (1) {
1.36 deraadt 628: len = read(STDIN_FILENO, &ch, 1);
1.30 deraadt 629: if (len == -1 && errno == EINTR)
630: continue;
631: if (len == 0)
632: exit(1);
633: break;
634: }
1.20 deraadt 635: if ((iptr = strchr(command_chars, ch)) == NULL) {
1.1 downsj 636: /* illegal command */
637: new_message(MT_standout, " Command not understood");
1.51 otto 638: putr();
1.1 downsj 639: no_command = Yes;
1.20 deraadt 640: fflush(stdout);
641: return (0);
642: }
643:
644: change = iptr - command_chars;
645:
646: switch (change) {
647: case CMD_redraw: /* redraw screen */
648: reset_display();
649: break;
650:
651: case CMD_update: /* merely update display */
652: /*
653: * is the load average high?
654: */
655: if (system_info.load_avg[0] > LoadMax) {
656: /* yes, go home for visual feedback */
657: go_home();
658: fflush(stdout);
1.1 downsj 659: }
1.20 deraadt 660: break;
661:
662: case CMD_quit: /* quit */
663: quit(0);
664: break;
665:
666: case CMD_help1: /* help */
667: case CMD_help2:
668: clear();
669: show_help();
1.51 otto 670: anykey();
671: clear();
1.20 deraadt 672: break;
673:
674: case CMD_errors: /* show errors */
675: if (error_count() == 0) {
676: new_message(MT_standout,
677: " Currently no errors to report.");
1.51 otto 678: putr();
1.20 deraadt 679: no_command = Yes;
680: } else {
1.1 downsj 681: clear();
1.20 deraadt 682: show_errors();
1.51 otto 683: anykey();
684: clear();
1.20 deraadt 685: }
686: break;
687:
688: case CMD_number1: /* new number */
689: case CMD_number2:
690: new_message(MT_standout,
691: "Number of processes to show: ");
1.63 otto 692:
693: if (readline(tempbuf, 8) > 0) {
1.73 lum 694: if ((i = atoiwi(tempbuf)) != Invalid) {
1.63 otto 695: if (i > max_topn) {
1.65 otto 696: new_message(MT_standout |
697: MT_delayed,
1.63 otto 698: " This terminal can only "
699: "display %d processes.",
700: max_topn);
701: putr();
1.65 otto 702: }
703: if ((i > topn || i == Infinity)
1.64 otto 704: && topn == 0) {
1.63 otto 705: /* redraw the header */
706: display_header(Yes);
1.65 otto 707: } else if (i == 0)
708: display_header(No);
1.63 otto 709: topn = i;
710: } else {
711: new_message(MT_standout,
1.65 otto 712: "Processes should be a "
713: "non-negative number");
1.63 otto 714: putr();
715: no_command = Yes;
1.20 deraadt 716: }
1.63 otto 717: } else
718: clear_message();
1.20 deraadt 719: break;
720:
721: case CMD_delay: /* new seconds delay */
722: new_message(MT_standout, "Seconds to delay: ");
1.63 otto 723: if (readline(tempbuf, sizeof(tempbuf)) > 0) {
1.20 deraadt 724: char *endp;
1.56 otto 725: double newdelay = strtod(tempbuf, &endp);
1.20 deraadt 726:
1.71 lum 727: if (newdelay >= 0 && newdelay <= 1000000 &&
1.48 otto 728: *endp == '\0') {
1.16 hugh 729: delay = newdelay;
1.48 otto 730: } else {
731: new_message(MT_standout,
732: "Delay should be a non-negative number");
1.51 otto 733: putr();
1.48 otto 734: no_command = Yes;
735: }
736:
737: } else
738: clear_message();
1.20 deraadt 739: break;
740:
741: case CMD_displays: /* change display count */
742: new_message(MT_standout,
743: "Displays to show (currently %s): ",
744: displays == -1 ? "infinite" :
745: itoa(displays));
746:
1.63 otto 747: if (readline(tempbuf, 10) > 0) {
1.73 lum 748: if ((i = atoiwi(tempbuf)) != Invalid) {
1.63 otto 749: if (i == 0)
750: quit(0);
751: displays = i;
752: } else {
753: new_message(MT_standout,
754: "Displays should be a non-negative number");
755: putr();
756: no_command = Yes;
757: }
758: } else
759: clear_message();
1.20 deraadt 760: break;
761:
762: case CMD_kill: /* kill program */
763: new_message(0, "kill ");
1.63 otto 764: if (readline(tempbuf, sizeof(tempbuf)) > 0) {
1.56 otto 765: if ((errmsg = kill_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;
773:
774: case CMD_renice: /* renice program */
775: new_message(0, "renice ");
1.63 otto 776: if (readline(tempbuf, sizeof(tempbuf)) > 0) {
1.56 otto 777: if ((errmsg = renice_procs(tempbuf)) != NULL) {
1.4 millert 778: new_message(MT_standout, "%s", errmsg);
1.51 otto 779: putr();
1.1 downsj 780: no_command = Yes;
781: }
1.20 deraadt 782: } else
783: clear_message();
784: break;
1.1 downsj 785:
1.20 deraadt 786: case CMD_idletog:
787: case CMD_idletog2:
788: ps.idle = !ps.idle;
789: new_message(MT_standout | MT_delayed,
790: " %sisplaying idle processes.",
791: ps.idle ? "D" : "Not d");
1.51 otto 792: putr();
1.20 deraadt 793: break;
1.1 downsj 794:
1.20 deraadt 795: case CMD_user:
796: new_message(MT_standout,
797: "Username to show: ");
1.63 otto 798: if (readline(tempbuf, sizeof(tempbuf)) > 0) {
1.78 brynet 799: if ((tempbuf[0] == '+' || tempbuf[0] == '-') &&
1.56 otto 800: tempbuf[1] == '\0') {
1.21 millert 801: ps.uid = (uid_t)-1;
1.78 brynet 802: ps.huid = (uid_t)-1;
803: } else if (tempbuf[0] == '-') {
804: if ((huid = userid(tempbuf+1)) == (uid_t)-1) {
805: new_message(MT_standout,
806: " %s: unknown user", tempbuf+1);
807: no_command = Yes;
808: } else {
809: ps.huid = huid;
810: ps.uid = (uid_t)-1;
811: }
1.56 otto 812: } else if ((uid = userid(tempbuf)) == (uid_t)-1) {
1.78 brynet 813: new_message(MT_standout,
814: " %s: unknown user", tempbuf);
815: no_command = Yes;
816: } else {
1.21 millert 817: ps.uid = uid;
1.78 brynet 818: ps.huid = (uid_t)-1;
819: }
1.51 otto 820: putr();
1.20 deraadt 821: } else
822: clear_message();
823: break;
1.12 fgsch 824:
1.20 deraadt 825: case CMD_system:
826: ps.system = !ps.system;
1.35 otto 827: old_system = ps.system;
1.20 deraadt 828: new_message(MT_standout | MT_delayed,
829: " %sisplaying system processes.",
830: ps.system ? "D" : "Not d");
831: break;
832:
833: case CMD_order:
834: new_message(MT_standout,
835: "Order to sort: ");
1.63 otto 836: if (readline(tempbuf, sizeof(tempbuf)) > 0) {
1.56 otto 837: if ((i = string_index(tempbuf,
1.20 deraadt 838: statics.order_names)) == -1) {
839: new_message(MT_standout,
840: " %s: unrecognized sorting order",
1.56 otto 841: tempbuf);
1.20 deraadt 842: no_command = Yes;
843: } else
1.1 downsj 844: order_index = i;
1.51 otto 845: putr();
1.35 otto 846: } else
847: clear_message();
848: break;
849:
850: case CMD_pid:
1.37 jaredy 851: new_message(MT_standout, "Process ID to show: ");
1.63 otto 852: if (readline(tempbuf, sizeof(tempbuf)) > 0) {
1.56 otto 853: if (tempbuf[0] == '+' &&
854: tempbuf[1] == '\0') {
1.35 otto 855: ps.pid = (pid_t)-1;
856: ps.system = old_system;
857: } else {
858: unsigned long long num;
859: const char *errstr;
860:
1.56 otto 861: num = strtonum(tempbuf, 0, INT_MAX,
1.35 otto 862: &errstr);
863: if (errstr != NULL || !find_pid(num)) {
864: new_message(MT_standout,
865: " %s: unknown pid",
1.56 otto 866: tempbuf);
1.35 otto 867: no_command = Yes;
868: } else {
869: if (ps.system == No)
870: old_system = No;
871: ps.pid = (pid_t)num;
872: ps.system = Yes;
873: }
874: }
1.51 otto 875: putr();
1.20 deraadt 876: } else
877: clear_message();
1.40 markus 878: break;
879:
880: case CMD_command:
881: show_args = (show_args == No) ? Yes : No;
1.41 tedu 882: break;
883:
884: case CMD_threads:
885: ps.threads = !ps.threads;
886: old_threads = ps.threads;
887: new_message(MT_standout | MT_delayed,
888: " %sisplaying threads.",
889: ps.threads ? "D" : "Not d");
1.45 otto 890: break;
891:
892: case CMD_grep:
893: new_message(MT_standout,
894: "Grep command name: ");
1.63 otto 895: if (readline(tempbuf, sizeof(tempbuf)) > 0) {
1.45 otto 896: free(ps.command);
1.56 otto 897: if (tempbuf[0] == '+' &&
898: tempbuf[1] == '\0')
1.45 otto 899: ps.command = NULL;
900: else
1.75 deraadt 901: if ((ps.command = strdup(tempbuf)) ==
902: NULL)
1.74 lum 903: err(1, NULL);
1.51 otto 904: putr();
905: } else
906: clear_message();
907: break;
908:
909: case CMD_hl:
1.53 otto 910: new_message(MT_standout, "Process ID to highlight: ");
1.63 otto 911: if (readline(tempbuf, sizeof(tempbuf)) > 0) {
1.56 otto 912: if (tempbuf[0] == '+' &&
913: tempbuf[1] == '\0') {
1.51 otto 914: hlpid = -1;
915: } else {
916: unsigned long long num;
917: const char *errstr;
918:
1.56 otto 919: num = strtonum(tempbuf, 0, INT_MAX,
1.51 otto 920: &errstr);
921: if (errstr != NULL || !find_pid(num)) {
922: new_message(MT_standout,
923: " %s: unknown pid",
1.56 otto 924: tempbuf);
1.51 otto 925: no_command = Yes;
926: } else
927: hlpid = (pid_t)num;
928: }
929: putr();
1.45 otto 930: } else
931: clear_message();
1.20 deraadt 932: break;
1.1 downsj 933:
1.47 otto 934: case CMD_add:
935: ps.uid = (uid_t)-1; /* uid */
1.78 brynet 936: ps.huid = (uid_t)-1;
1.47 otto 937: ps.pid = (pid_t)-1; /* pid */
938: ps.system = old_system;
939: ps.command = NULL; /* grep */
1.51 otto 940: hlpid = -1;
1.47 otto 941: break;
1.68 tedu 942: case CMD_cpus:
943: combine_cpus = !combine_cpus;
944: max_topn = display_resize();
945: reset_display();
946: break;
1.20 deraadt 947: default:
948: new_message(MT_standout, " BAD CASE IN SWITCH!");
1.51 otto 949: putr();
1.1 downsj 950: }
951: }
952:
1.20 deraadt 953: /* flush out stuff that may have been written */
954: fflush(stdout);
955: return 0;
1.1 downsj 956: }
957:
1.20 deraadt 958:
1.1 downsj 959: /*
960: * reset_display() - reset all the display routine pointers so that entire
961: * screen will get redrawn.
962: */
1.19 pvalchev 963: static void
964: reset_display(void)
1.1 downsj 965: {
1.51 otto 966: if (smart_terminal) {
967: clear();
968: refresh();
969: }
1.1 downsj 970: }
971:
1.34 deraadt 972: /* ARGSUSED */
1.19 pvalchev 973: void
1.20 deraadt 974: leave(int signo)
1.1 downsj 975: {
1.20 deraadt 976: leaveflag = 1;
1.1 downsj 977: }
978:
1.34 deraadt 979: /* ARGSUSED */
1.19 pvalchev 980: void
1.20 deraadt 981: tstop(int signo)
1.1 downsj 982: {
1.20 deraadt 983: tstopflag = 1;
1.1 downsj 984: }
985:
1.34 deraadt 986: /* ARGSUSED */
1.19 pvalchev 987: void
1.51 otto 988: sigwinch(int signo)
1.1 downsj 989: {
1.20 deraadt 990: winchflag = 1;
1.1 downsj 991: }
992:
1.34 deraadt 993: /* ARGSUSED */
1.19 pvalchev 994: void
1.20 deraadt 995: onalrm(int signo)
1.1 downsj 996: {
997: }
998:
1.19 pvalchev 999: void
1.20 deraadt 1000: quit(int ret)
1.1 downsj 1001: {
1.20 deraadt 1002: end_screen();
1003: exit(ret);
1.1 downsj 1004: }