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