Annotation of src/usr.bin/top/top.c, Revision 1.103
1.103 ! kn 1: /* $OpenBSD: top.c,v 1.102 2020/01/06 20:05:10 zhuk 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;
1.103 ! kn 572: skip_processes(processes, skip);
1.20 deraadt 573: /* now show the top "n" processes. */
1.51 otto 574: for (i = 0; i < active_procs; i++) {
575: pid_t pid;
576: char * s;
577:
1.101 kn 578: s = format_next_process(processes,
579: ps.threads ? NULL : get_userid, &pid);
1.51 otto 580: i_process(i, s, pid == hlpid);
581: }
1.54 otto 582: }
1.20 deraadt 583:
584: /* do end-screen processing */
1.52 deraadt 585: u_endscreen();
1.20 deraadt 586:
587: /* now, flush the output buffer */
588: fflush(stdout);
589:
1.51 otto 590: if (smart_terminal)
591: refresh();
592:
1.20 deraadt 593: /* only do the rest if we have more displays to show */
594: if (displays) {
595: /* switch out for new display on smart terminals */
1.100 kn 596: no_command = true;
1.20 deraadt 597: if (!interactive) {
598: /* set up alarm */
599: (void) signal(SIGALRM, onalrm);
600: (void) alarm((unsigned) delay);
601:
602: /* wait for the rest of it .... */
603: pause();
1.42 otto 604: if (leaveflag)
605: exit(0);
606: if (tstopflag) {
607: (void) signal(SIGTSTP, SIG_DFL);
608: (void) kill(0, SIGTSTP);
609: /* reset the signal handler */
610: (void) signal(SIGTSTP, tstop);
611: tstopflag = 0;
612: }
1.20 deraadt 613: } else {
614: while (no_command)
615: if (rundisplay())
616: goto restart;
617: }
618: }
619: }
1.1 downsj 620:
1.20 deraadt 621: quit(0);
622: /* NOTREACHED */
623: return (0);
624: }
1.7 deraadt 625:
1.20 deraadt 626: int
627: rundisplay(void)
628: {
1.72 lum 629: static char tempbuf[TEMPBUFSIZE];
1.20 deraadt 630: sigset_t mask;
631: char ch, *iptr;
632: int change, i;
1.28 deraadt 633: struct pollfd pfd[1];
1.102 zhuk 634: static char command_chars[] = "\f qh?en#sdkriIuSopCHg+P109)(";
1.7 deraadt 635:
1.20 deraadt 636: /*
637: * assume valid command unless told
638: * otherwise
639: */
1.100 kn 640: no_command = false;
1.7 deraadt 641:
1.20 deraadt 642: /*
643: * set up arguments for select with
644: * timeout
645: */
1.28 deraadt 646: pfd[0].fd = STDIN_FILENO;
647: pfd[0].events = POLLIN;
1.20 deraadt 648:
1.42 otto 649: if (leaveflag)
650: quit(0);
1.20 deraadt 651: if (tstopflag) {
652: /* move to the lower left */
653: end_screen();
654: fflush(stdout);
655:
656: /*
657: * default the signal handler
658: * action
659: */
660: (void) signal(SIGTSTP, SIG_DFL);
661:
662: /*
663: * unblock the signal and
664: * send ourselves one
665: */
666: sigemptyset(&mask);
667: sigaddset(&mask, SIGTSTP);
668: sigprocmask(SIG_UNBLOCK, &mask, NULL);
669: (void) kill(0, SIGTSTP);
670:
671: /* reset the signal handler */
672: (void) signal(SIGTSTP, tstop);
673:
674: /* reinit screen */
675: reinit_screen();
676: reset_display();
677: tstopflag = 0;
678: return 1;
679: }
680: /*
681: * wait for either input or the end
682: * of the delay period
683: */
1.81 millert 684: if (poll(pfd, 1, (int)(delay * 1000)) > 0) {
1.20 deraadt 685: char *errmsg;
1.30 deraadt 686: ssize_t len;
1.81 millert 687:
688: if ((pfd[0].revents & (POLLERR|POLLHUP|POLLNVAL)))
689: exit(1);
1.20 deraadt 690:
691: clear_message();
692:
693: /*
694: * now read it and convert to
695: * command strchr
696: */
1.30 deraadt 697: while (1) {
1.36 deraadt 698: len = read(STDIN_FILENO, &ch, 1);
1.30 deraadt 699: if (len == -1 && errno == EINTR)
700: continue;
701: if (len == 0)
702: exit(1);
703: break;
704: }
1.20 deraadt 705: if ((iptr = strchr(command_chars, ch)) == NULL) {
1.1 downsj 706: /* illegal command */
707: new_message(MT_standout, " Command not understood");
1.51 otto 708: putr();
1.100 kn 709: no_command = true;
1.20 deraadt 710: fflush(stdout);
711: return (0);
712: }
713:
714: change = iptr - command_chars;
715:
716: switch (change) {
717: case CMD_redraw: /* redraw screen */
718: reset_display();
719: break;
720:
721: case CMD_update: /* merely update display */
722: /*
723: * is the load average high?
724: */
725: if (system_info.load_avg[0] > LoadMax) {
726: /* yes, go home for visual feedback */
727: go_home();
728: fflush(stdout);
1.1 downsj 729: }
1.20 deraadt 730: break;
731:
732: case CMD_quit: /* quit */
733: quit(0);
734: break;
735:
736: case CMD_help1: /* help */
737: case CMD_help2:
738: clear();
739: show_help();
1.51 otto 740: anykey();
741: clear();
1.20 deraadt 742: break;
743:
744: case CMD_errors: /* show errors */
745: if (error_count() == 0) {
746: new_message(MT_standout,
747: " Currently no errors to report.");
1.51 otto 748: putr();
1.100 kn 749: no_command = true;
1.20 deraadt 750: } else {
1.1 downsj 751: clear();
1.20 deraadt 752: show_errors();
1.51 otto 753: anykey();
754: clear();
1.20 deraadt 755: }
756: break;
757:
758: case CMD_number1: /* new number */
759: case CMD_number2:
760: new_message(MT_standout,
761: "Number of processes to show: ");
1.63 otto 762:
763: if (readline(tempbuf, 8) > 0) {
1.73 lum 764: if ((i = atoiwi(tempbuf)) != Invalid) {
1.63 otto 765: if (i > max_topn) {
1.65 otto 766: new_message(MT_standout |
767: MT_delayed,
1.63 otto 768: " This terminal can only "
769: "display %d processes.",
770: max_topn);
771: putr();
1.65 otto 772: }
773: if ((i > topn || i == Infinity)
1.64 otto 774: && topn == 0) {
1.63 otto 775: /* redraw the header */
1.100 kn 776: display_header(true);
1.65 otto 777: } else if (i == 0)
1.100 kn 778: display_header(false);
1.63 otto 779: topn = i;
780: } else {
781: new_message(MT_standout,
1.65 otto 782: "Processes should be a "
783: "non-negative number");
1.89 deraadt 784: putr();
1.100 kn 785: no_command = true;
1.20 deraadt 786: }
1.63 otto 787: } else
788: clear_message();
1.20 deraadt 789: break;
790:
791: case CMD_delay: /* new seconds delay */
792: new_message(MT_standout, "Seconds to delay: ");
1.63 otto 793: if (readline(tempbuf, sizeof(tempbuf)) > 0) {
1.20 deraadt 794: char *endp;
1.56 otto 795: double newdelay = strtod(tempbuf, &endp);
1.20 deraadt 796:
1.71 lum 797: if (newdelay >= 0 && newdelay <= 1000000 &&
1.48 otto 798: *endp == '\0') {
1.16 hugh 799: delay = newdelay;
1.48 otto 800: } else {
801: new_message(MT_standout,
802: "Delay should be a non-negative number");
1.51 otto 803: putr();
1.100 kn 804: no_command = true;
1.48 otto 805: }
806:
807: } else
808: clear_message();
1.20 deraadt 809: break;
810:
811: case CMD_displays: /* change display count */
812: new_message(MT_standout,
813: "Displays to show (currently %s): ",
814: displays == -1 ? "infinite" :
815: itoa(displays));
816:
1.63 otto 817: if (readline(tempbuf, 10) > 0) {
1.73 lum 818: if ((i = atoiwi(tempbuf)) != Invalid) {
1.63 otto 819: if (i == 0)
820: quit(0);
821: displays = i;
822: } else {
823: new_message(MT_standout,
824: "Displays should be a non-negative number");
825: putr();
1.100 kn 826: no_command = true;
1.63 otto 827: }
828: } else
829: clear_message();
1.20 deraadt 830: break;
831:
832: case CMD_kill: /* kill program */
833: new_message(0, "kill ");
1.63 otto 834: if (readline(tempbuf, sizeof(tempbuf)) > 0) {
1.56 otto 835: if ((errmsg = kill_procs(tempbuf)) != NULL) {
1.4 millert 836: new_message(MT_standout, "%s", errmsg);
1.51 otto 837: putr();
1.100 kn 838: no_command = true;
1.1 downsj 839: }
1.20 deraadt 840: } else
841: clear_message();
842: break;
843:
844: case CMD_renice: /* renice program */
845: new_message(0, "renice ");
1.63 otto 846: if (readline(tempbuf, sizeof(tempbuf)) > 0) {
1.56 otto 847: if ((errmsg = renice_procs(tempbuf)) != NULL) {
1.4 millert 848: new_message(MT_standout, "%s", errmsg);
1.51 otto 849: putr();
1.100 kn 850: no_command = true;
1.1 downsj 851: }
1.20 deraadt 852: } else
853: clear_message();
854: break;
1.1 downsj 855:
1.20 deraadt 856: case CMD_idletog:
857: case CMD_idletog2:
858: ps.idle = !ps.idle;
859: new_message(MT_standout | MT_delayed,
860: " %sisplaying idle processes.",
861: ps.idle ? "D" : "Not d");
1.51 otto 862: putr();
1.20 deraadt 863: break;
1.1 downsj 864:
1.20 deraadt 865: case CMD_user:
866: new_message(MT_standout,
867: "Username to show: ");
1.63 otto 868: if (readline(tempbuf, sizeof(tempbuf)) > 0) {
1.78 brynet 869: if ((tempbuf[0] == '+' || tempbuf[0] == '-') &&
1.56 otto 870: tempbuf[1] == '\0') {
1.21 millert 871: ps.uid = (uid_t)-1;
1.78 brynet 872: ps.huid = (uid_t)-1;
1.95 kn 873: } else if (filteruser(tempbuf) == -1) {
874: new_message(MT_standout,
875: " %s: unknown user",
876: tempbuf[0] == '-' ? tempbuf + 1 :
877: tempbuf);
1.100 kn 878: no_command = true;
1.78 brynet 879: }
1.51 otto 880: putr();
1.20 deraadt 881: } else
882: clear_message();
883: break;
1.12 fgsch 884:
1.20 deraadt 885: case CMD_system:
886: ps.system = !ps.system;
1.35 otto 887: old_system = ps.system;
1.20 deraadt 888: new_message(MT_standout | MT_delayed,
889: " %sisplaying system processes.",
890: ps.system ? "D" : "Not d");
891: break;
892:
893: case CMD_order:
894: new_message(MT_standout,
895: "Order to sort: ");
1.63 otto 896: if (readline(tempbuf, sizeof(tempbuf)) > 0) {
1.98 kn 897: if ((i = getorder(tempbuf)) == -1) {
1.20 deraadt 898: new_message(MT_standout,
899: " %s: unrecognized sorting order",
1.98 kn 900: tempbuf[0] == '-' ? tempbuf + 1 :
1.56 otto 901: tempbuf);
1.100 kn 902: no_command = true;
1.20 deraadt 903: } else
1.1 downsj 904: order_index = i;
1.51 otto 905: putr();
1.35 otto 906: } else
907: clear_message();
908: break;
909:
910: case CMD_pid:
1.37 jaredy 911: new_message(MT_standout, "Process ID to show: ");
1.63 otto 912: if (readline(tempbuf, sizeof(tempbuf)) > 0) {
1.56 otto 913: if (tempbuf[0] == '+' &&
914: tempbuf[1] == '\0') {
1.35 otto 915: ps.pid = (pid_t)-1;
916: ps.system = old_system;
1.95 kn 917: } else if (filterpid(tempbuf, 0) == -1) {
918: new_message(MT_standout,
919: " %s: unknown pid", tempbuf);
1.100 kn 920: no_command = true;
1.35 otto 921: }
1.51 otto 922: putr();
1.20 deraadt 923: } else
924: clear_message();
1.40 markus 925: break;
926:
927: case CMD_command:
1.100 kn 928: show_args = !show_args;
1.41 tedu 929: break;
1.89 deraadt 930:
1.41 tedu 931: case CMD_threads:
932: ps.threads = !ps.threads;
933: old_threads = ps.threads;
934: new_message(MT_standout | MT_delayed,
935: " %sisplaying threads.",
936: ps.threads ? "D" : "Not d");
1.45 otto 937: break;
938:
939: case CMD_grep:
940: new_message(MT_standout,
941: "Grep command name: ");
1.63 otto 942: if (readline(tempbuf, sizeof(tempbuf)) > 0) {
1.45 otto 943: free(ps.command);
1.56 otto 944: if (tempbuf[0] == '+' &&
945: tempbuf[1] == '\0')
1.45 otto 946: ps.command = NULL;
1.95 kn 947: else if ((ps.command = strdup(tempbuf)) == NULL)
948: err(1, NULL);
1.51 otto 949: putr();
950: } else
951: clear_message();
952: break;
953:
954: case CMD_hl:
1.53 otto 955: new_message(MT_standout, "Process ID to highlight: ");
1.63 otto 956: if (readline(tempbuf, sizeof(tempbuf)) > 0) {
1.56 otto 957: if (tempbuf[0] == '+' &&
958: tempbuf[1] == '\0') {
1.95 kn 959: hlpid = (pid_t)-1;
1.100 kn 960: } else if (filterpid(tempbuf, true) == -1) {
1.95 kn 961: new_message(MT_standout,
962: " %s: unknown pid", tempbuf);
1.100 kn 963: no_command = true;
1.51 otto 964: }
965: putr();
1.45 otto 966: } else
967: clear_message();
1.20 deraadt 968: break;
1.1 downsj 969:
1.47 otto 970: case CMD_add:
971: ps.uid = (uid_t)-1; /* uid */
1.78 brynet 972: ps.huid = (uid_t)-1;
1.89 deraadt 973: ps.pid = (pid_t)-1; /* pid */
1.47 otto 974: ps.system = old_system;
975: ps.command = NULL; /* grep */
1.95 kn 976: hlpid = (pid_t)-1;
1.47 otto 977: break;
1.68 tedu 978: case CMD_cpus:
979: combine_cpus = !combine_cpus;
980: max_topn = display_resize();
981: reset_display();
1.102 zhuk 982: break;
983: case CMD_down:
984: skip++;
985: break;
986: case CMD_up:
987: if (skip > 0)
988: skip--;
989: break;
990: case CMD_pagedown:
991: skip += max_topn / 2;
992: break;
993: case CMD_pageup:
994: skip -= max_topn / 2;
995: if (skip < 0)
996: skip = 0;
1.68 tedu 997: break;
1.20 deraadt 998: default:
999: new_message(MT_standout, " BAD CASE IN SWITCH!");
1.51 otto 1000: putr();
1.1 downsj 1001: }
1002: }
1003:
1.20 deraadt 1004: /* flush out stuff that may have been written */
1005: fflush(stdout);
1006: return 0;
1.1 downsj 1007: }
1008:
1.20 deraadt 1009:
1.1 downsj 1010: /*
1011: * reset_display() - reset all the display routine pointers so that entire
1012: * screen will get redrawn.
1013: */
1.19 pvalchev 1014: static void
1015: reset_display(void)
1.1 downsj 1016: {
1.51 otto 1017: if (smart_terminal) {
1018: clear();
1019: refresh();
1020: }
1.1 downsj 1021: }
1022:
1.34 deraadt 1023: /* ARGSUSED */
1.19 pvalchev 1024: void
1.20 deraadt 1025: leave(int signo)
1.1 downsj 1026: {
1.20 deraadt 1027: leaveflag = 1;
1.1 downsj 1028: }
1029:
1.34 deraadt 1030: /* ARGSUSED */
1.19 pvalchev 1031: void
1.20 deraadt 1032: tstop(int signo)
1.1 downsj 1033: {
1.20 deraadt 1034: tstopflag = 1;
1.1 downsj 1035: }
1036:
1.34 deraadt 1037: /* ARGSUSED */
1.19 pvalchev 1038: void
1.51 otto 1039: sigwinch(int signo)
1.1 downsj 1040: {
1.20 deraadt 1041: winchflag = 1;
1.1 downsj 1042: }
1043:
1.34 deraadt 1044: /* ARGSUSED */
1.19 pvalchev 1045: void
1.20 deraadt 1046: onalrm(int signo)
1.1 downsj 1047: {
1048: }
1049:
1.19 pvalchev 1050: void
1.20 deraadt 1051: quit(int ret)
1.1 downsj 1052: {
1.20 deraadt 1053: end_screen();
1054: exit(ret);
1.1 downsj 1055: }