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