Annotation of src/usr.bin/top/top.c, Revision 1.104
1.104 ! kn 1: /* $OpenBSD: top.c,v 1.103 2020/06/25 20:38:41 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.104 ! kn 134: #define CMD_grep2 28
1.1 downsj 135:
1.27 deraadt 136: static void
1.20 deraadt 137: usage(void)
138: {
1.22 deraadt 139: extern char *__progname;
140:
1.20 deraadt 141: fprintf(stderr,
1.98 kn 142: "usage: %s [-1bCHIinqSu] [-d count] [-g string] [-o [-]field] "
1.78 brynet 143: "[-p pid] [-s time]\n\t[-U [-]user] [number]\n",
1.22 deraadt 144: __progname);
1.20 deraadt 145: }
146:
1.95 kn 147: static int
1.98 kn 148: getorder(char *field)
149: {
150: int i, r = field[0] == '-';
151:
152: i = string_index(r ? field + 1 : field, statics.order_names);
153: if (i != -1)
154: rev_order = r;
155:
156: return i;
157: }
158:
159: static int
1.95 kn 160: filteruser(char buf[])
161: {
1.96 kn 162: const char *errstr;
1.95 kn 163: char *bufp = buf;
164: uid_t *uidp;
1.96 kn 165: uid_t uid;
1.95 kn 166:
167: if (bufp[0] == '-') {
168: bufp++;
169: uidp = &ps.huid;
170: ps.uid = (pid_t)-1;
171: } else {
172: uidp = &ps.uid;
173: ps.huid = (pid_t)-1;
174: }
175:
1.96 kn 176: if (uid_from_user(bufp, uidp) == 0)
177: return 0;
178:
179: uid = strtonum(bufp, 0, UID_MAX, &errstr);
180: if (errstr == NULL && user_from_uid(uid, 1) != NULL) {
181: *uidp = uid;
182: return 0;
183: }
184:
185: return -1;
1.95 kn 186: }
187:
188: static int
189: filterpid(char buf[], int hl)
190: {
191: const char *errstr;
192: int pid;
193:
194: pid = strtonum(buf, 0, INT_MAX, &errstr);
195: if (errstr != NULL || !find_pid(pid))
196: return -1;
197:
1.100 kn 198: if (hl)
1.95 kn 199: hlpid = (pid_t)pid;
200: else {
1.100 kn 201: if (!ps.system)
202: old_system = false;
1.95 kn 203: ps.pid = (pid_t)pid;
1.100 kn 204: ps.system = true;
1.95 kn 205: }
206:
207: return 0;
208: }
209:
1.27 deraadt 210: static void
1.20 deraadt 211: parseargs(int ac, char **av)
212: {
213: char *endp;
214: int i;
215:
1.76 jsing 216: while ((i = getopt(ac, av, "1SHICbinqus:d:p:U:o:g:")) != -1) {
1.20 deraadt 217: switch (i) {
1.68 tedu 218: case '1':
219: combine_cpus = 1;
220: break;
1.44 otto 221: case 'C':
1.100 kn 222: show_args = true;
1.44 otto 223: break;
1.20 deraadt 224: case 'u': /* toggle uid/username display */
225: do_unames = !do_unames;
226: break;
227:
228: case 'U': /* display only username's processes */
1.95 kn 229: if (filteruser(optarg) == -1)
1.61 otto 230: new_message(MT_delayed, "%s: unknown user",
231: optarg);
1.20 deraadt 232: break;
1.1 downsj 233:
1.95 kn 234: case 'p': /* display only process id */
1.100 kn 235: if (filterpid(optarg, false) == -1)
1.61 otto 236: new_message(MT_delayed, "%s: unknown pid",
237: optarg);
1.35 otto 238: break;
239:
1.20 deraadt 240: case 'S': /* show system processes */
1.67 jmc 241: ps.system = !ps.system;
242: old_system = !old_system;
1.20 deraadt 243: break;
244:
1.76 jsing 245: case 'H': /* show threads */
1.100 kn 246: ps.threads = true;
247: old_threads = true;
1.41 tedu 248: break;
249:
1.20 deraadt 250: case 'I': /* show idle processes */
251: ps.idle = !ps.idle;
252: break;
253:
254: case 'i': /* go interactive regardless */
1.100 kn 255: interactive = true;
1.20 deraadt 256: break;
257:
258: case 'n': /* batch, or non-interactive */
259: case 'b':
1.100 kn 260: interactive = false;
1.20 deraadt 261: break;
262:
263: case 'd': /* number of displays to show */
264: if ((i = atoiwi(optarg)) != Invalid && i != 0) {
265: displays = i;
1.66 sthen 266: if (displays == 1)
1.100 kn 267: interactive = false;
1.20 deraadt 268: break;
1.32 deraadt 269: }
1.60 otto 270: new_message(MT_delayed,
271: "warning: display count should be positive "
1.22 deraadt 272: "-- option ignored");
1.20 deraadt 273: break;
274:
275: case 's':
276: delay = strtod(optarg, &endp);
277:
1.59 otto 278: if (delay >= 0 && delay <= 1000000 && *endp == '\0')
1.20 deraadt 279: break;
280:
1.60 otto 281: new_message(MT_delayed,
282: "warning: delay should be a non-negative number"
1.22 deraadt 283: " -- using default");
1.20 deraadt 284: delay = Default_DELAY;
285: break;
286:
287: case 'q': /* be quick about it */
288: /* only allow this if user is really root */
289: if (getuid() == 0) {
290: /* be very un-nice! */
291: (void) nice(-20);
292: break;
293: }
1.60 otto 294: new_message(MT_delayed,
295: "warning: `-q' option can only be used by root");
1.20 deraadt 296: break;
297:
298: case 'o': /* select sort order */
299: order_name = optarg;
300: break;
301:
1.45 otto 302: case 'g': /* grep command name */
1.74 lum 303: free(ps.command);
1.75 deraadt 304: if ((ps.command = strdup(optarg)) == NULL)
1.74 lum 305: err(1, NULL);
1.45 otto 306: break;
307:
1.20 deraadt 308: default:
309: usage();
310: exit(1);
1.1 downsj 311: }
1.20 deraadt 312: }
1.82 dlg 313:
1.97 cheloha 314: i = getncpuonline();
1.82 dlg 315: if (i == -1)
316: err(1, NULL);
317:
318: if (i > 8)
319: combine_cpus = 1;
1.1 downsj 320:
1.20 deraadt 321: /* get count of top processes to display (if any) */
322: if (optind < ac) {
323: if ((topn = atoiwi(av[optind])) == Invalid) {
1.60 otto 324: new_message(MT_delayed,
325: "warning: process count should "
1.49 otto 326: "be a non-negative number -- using default");
1.46 otto 327: topn = Infinity;
1.1 downsj 328: }
1.20 deraadt 329: #if Default_TOPN == Infinity
1.1 downsj 330: else
1.100 kn 331: topn_specified = true;
1.20 deraadt 332: #endif
333: }
334: }
1.1 downsj 335:
1.20 deraadt 336: int
337: main(int argc, char *argv[])
338: {
339: char *uname_field = "USERNAME", *header_text, *env_top;
1.92 millert 340: const char *(*get_userid)(uid_t, int) = user_from_uid;
1.77 pirofti 341: char **preset_argv = NULL, **av = argv;
1.97 cheloha 342: int preset_argc = 0, ac = argc, active_procs, i, ncpuonline_now;
1.20 deraadt 343: sigset_t mask, oldmask;
344: time_t curr_time;
1.99 kn 345: struct handle *processes;
1.1 downsj 346:
1.20 deraadt 347: /* set the buffer for stdout */
348: #ifdef DEBUG
1.87 tedu 349: setvbuf(stdout, NULL, _IONBUF, 0);
1.1 downsj 350: #else
1.87 tedu 351: setvbuf(stdout, stdoutbuf, _IOFBF, sizeof stdoutbuf);
1.1 downsj 352: #endif
353:
1.20 deraadt 354: /* initialize some selection options */
1.100 kn 355: ps.idle = true;
356: ps.system = false;
1.21 millert 357: ps.uid = (uid_t)-1;
1.78 brynet 358: ps.huid = (uid_t)-1;
1.35 otto 359: ps.pid = (pid_t)-1;
1.20 deraadt 360: ps.command = NULL;
361:
362: /* get preset options from the environment */
363: if ((env_top = getenv("TOP")) != NULL) {
364: av = preset_argv = argparse(env_top, &preset_argc);
365: ac = preset_argc;
366:
367: /*
368: * set the dummy argument to an explanatory message, in case
369: * getopt encounters a bad argument
370: */
371: preset_argv[0] = "while processing environment";
372: }
373: /* process options */
374: do {
375: /*
376: * if we're done doing the presets, then process the real
377: * arguments
378: */
379: if (preset_argc == 0) {
380: ac = argc;
381: av = argv;
382: optind = 1;
383: }
384: parseargs(ac, av);
385: i = preset_argc;
386: preset_argc = 0;
387: } while (i != 0);
1.84 deraadt 388:
1.88 espie 389: if (pledge("stdio rpath getpw tty proc ps vminfo", NULL) == -1)
1.84 deraadt 390: err(1, "pledge");
1.20 deraadt 391:
392: /* set constants for username/uid display correctly */
393: if (!do_unames) {
394: uname_field = " UID ";
1.26 millert 395: get_userid = format_uid;
1.20 deraadt 396: }
397: /* initialize the kernel memory interface */
398: if (machine_init(&statics) == -1)
1.1 downsj 399: exit(1);
1.20 deraadt 400:
401: /* determine sorting order index, if necessary */
402: if (order_name != NULL) {
1.98 kn 403: if ((order_index = getorder(order_name)) == -1) {
404: new_message(MT_delayed,
405: " %s: unrecognized sorting order", order_name);
1.62 otto 406: order_index = 0;
1.20 deraadt 407: }
1.1 downsj 408: }
409:
1.20 deraadt 410: /* initialize termcap */
411: init_termcap(interactive);
412:
413: /* initialize display interface */
1.58 otto 414: max_topn = display_init(&statics);
415:
1.20 deraadt 416: /* print warning if user requested more processes than we can display */
1.60 otto 417: if (topn > max_topn)
418: new_message(MT_delayed,
419: "warning: this terminal can only display %d processes",
1.22 deraadt 420: max_topn);
1.20 deraadt 421: /* adjust for topn == Infinity */
422: if (topn == Infinity) {
423: /*
424: * For smart terminals, infinity really means everything that can
425: * be displayed, or Largest.
426: * On dumb terminals, infinity means every process in the system!
427: * We only really want to do that if it was explicitly specified.
428: * This is always the case when "Default_TOPN != Infinity". But if
429: * topn wasn't explicitly specified and we are on a dumb terminal
430: * and the default is Infinity, then (and only then) we use
431: * "Nominal_TOPN" instead.
432: */
1.1 downsj 433: #if Default_TOPN == Infinity
1.20 deraadt 434: topn = smart_terminal ? Largest :
435: (topn_specified ? Largest : Nominal_TOPN);
436: #else
437: topn = Largest;
1.1 downsj 438: #endif
439: }
1.20 deraadt 440: /* set header display accordingly */
441: display_header(topn > 0);
1.1 downsj 442:
1.20 deraadt 443: /* determine interactive state */
1.100 kn 444: if (interactive == -1)
1.20 deraadt 445: interactive = smart_terminal;
1.1 downsj 446:
1.20 deraadt 447: /* if # of displays not specified, fill it in */
448: if (displays == 0)
449: displays = smart_terminal ? Infinity : 1;
1.1 downsj 450:
451: /*
1.20 deraadt 452: * block interrupt signals while setting up the screen and the
453: * handlers
1.1 downsj 454: */
1.20 deraadt 455: sigemptyset(&mask);
456: sigaddset(&mask, SIGINT);
457: sigaddset(&mask, SIGQUIT);
458: sigaddset(&mask, SIGTSTP);
459: sigprocmask(SIG_BLOCK, &mask, &oldmask);
1.57 otto 460: if (interactive)
461: init_screen();
1.90 deraadt 462: if (pledge("stdio getpw tty proc ps vminfo", NULL) == -1)
463: err(1, "pledge");
1.20 deraadt 464: (void) signal(SIGINT, leave);
1.30 deraadt 465: siginterrupt(SIGINT, 1);
1.20 deraadt 466: (void) signal(SIGQUIT, leave);
467: (void) signal(SIGTSTP, tstop);
1.51 otto 468: if (smart_terminal)
469: (void) signal(SIGWINCH, sigwinch);
1.20 deraadt 470: sigprocmask(SIG_SETMASK, &oldmask, NULL);
1.7 deraadt 471: restart:
1.1 downsj 472:
1.20 deraadt 473: /*
474: * main loop -- repeat while display count is positive or while it
475: * indicates infinity (by being -1)
476: */
477: while ((displays == -1) || (displays-- > 0)) {
1.70 otto 478: if (winchflag) {
479: /*
480: * reascertain the screen
481: * dimensions
482: */
483: get_screensize();
484: resizeterm(screen_length, screen_width + 1);
485:
486: /* tell display to resize */
487: max_topn = display_resize();
488:
489: /* reset the signal handler */
490: (void) signal(SIGWINCH, sigwinch);
491:
492: reset_display();
493: winchflag = 0;
494: }
495:
1.20 deraadt 496: /* get the current stats */
497: get_system_info(&system_info);
498:
1.97 cheloha 499: /*
500: * don't display stats for offline CPUs: resize if we're
501: * interactive and CPUs have toggled on or offline
502: */
503: if (interactive && !combine_cpus) {
504: for (i = ncpuonline_now = 0; i < ncpu; i++)
505: if (system_info.cpuonline[i])
506: ncpuonline_now++;
507: if (ncpuonline_now != ncpuonline) {
508: max_topn = display_resize();
509: reset_display();
510: continue;
511: }
512: }
513:
1.20 deraadt 514: /* get the current set of processes */
515: processes = get_process_info(&system_info, &ps,
516: proc_compares[order_index]);
517:
518: /* display the load averages */
1.51 otto 519: i_loadave(system_info.last_pid, system_info.load_avg);
1.20 deraadt 520:
521: /* display the current time */
522: /* this method of getting the time SHOULD be fairly portable */
523: time(&curr_time);
524: i_timeofday(&curr_time);
525:
1.80 guenther 526: /* display process/threads state breakdown */
527: i_procstates(system_info.p_total, system_info.procstates,
528: ps.threads);
1.20 deraadt 529:
530: /* display the cpu state percentage breakdown */
1.97 cheloha 531: i_cpustates(system_info.cpustates, system_info.cpuonline);
1.1 downsj 532:
1.20 deraadt 533: /* display memory stats */
1.51 otto 534: i_memory(system_info.memory);
1.1 downsj 535:
1.20 deraadt 536: /* handle message area */
1.51 otto 537: i_message();
1.1 downsj 538:
1.83 mpi 539: /* get the string to use for the process area header */
1.101 kn 540: header_text = format_header(ps.threads ? NULL : uname_field);
1.83 mpi 541:
1.20 deraadt 542: /* update the header area */
1.51 otto 543: i_header(header_text);
1.20 deraadt 544:
1.63 otto 545: if (topn == Infinity) {
546: #if Default_TOPN == Infinity
547: topn = smart_terminal ? Largest :
548: (topn_specified ? Largest : Nominal_TOPN);
549: #else
550: topn = Largest;
551: #endif
552: }
553:
1.20 deraadt 554: if (topn > 0) {
555: /* determine number of processes to actually display */
556: /*
557: * this number will be the smallest of: active
558: * processes, number user requested, number current
559: * screen accommodates
560: */
561: active_procs = system_info.p_active;
562: if (active_procs > topn)
563: active_procs = topn;
564: if (active_procs > max_topn)
565: active_procs = max_topn;
1.102 zhuk 566: /* determine how many process to skip, if asked to */
567: /*
568: * this number is tweaked by user, but gets shrinked
569: * when number of active processes lowers too much
570: */
571: if (skip + active_procs > system_info.p_active)
572: skip = system_info.p_active - active_procs;
1.103 kn 573: skip_processes(processes, skip);
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.104 ! kn 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:
1.104 ! kn 941: case CMD_grep2:
1.45 otto 942: new_message(MT_standout,
943: "Grep command name: ");
1.63 otto 944: if (readline(tempbuf, sizeof(tempbuf)) > 0) {
1.45 otto 945: free(ps.command);
1.56 otto 946: if (tempbuf[0] == '+' &&
947: tempbuf[1] == '\0')
1.45 otto 948: ps.command = NULL;
1.95 kn 949: else if ((ps.command = strdup(tempbuf)) == NULL)
950: err(1, NULL);
1.51 otto 951: putr();
952: } else
953: clear_message();
954: break;
955:
956: case CMD_hl:
1.53 otto 957: new_message(MT_standout, "Process ID to highlight: ");
1.63 otto 958: if (readline(tempbuf, sizeof(tempbuf)) > 0) {
1.56 otto 959: if (tempbuf[0] == '+' &&
960: tempbuf[1] == '\0') {
1.95 kn 961: hlpid = (pid_t)-1;
1.100 kn 962: } else if (filterpid(tempbuf, true) == -1) {
1.95 kn 963: new_message(MT_standout,
964: " %s: unknown pid", tempbuf);
1.100 kn 965: no_command = true;
1.51 otto 966: }
967: putr();
1.45 otto 968: } else
969: clear_message();
1.20 deraadt 970: break;
1.1 downsj 971:
1.47 otto 972: case CMD_add:
973: ps.uid = (uid_t)-1; /* uid */
1.78 brynet 974: ps.huid = (uid_t)-1;
1.89 deraadt 975: ps.pid = (pid_t)-1; /* pid */
1.47 otto 976: ps.system = old_system;
977: ps.command = NULL; /* grep */
1.95 kn 978: hlpid = (pid_t)-1;
1.47 otto 979: break;
1.68 tedu 980: case CMD_cpus:
981: combine_cpus = !combine_cpus;
982: max_topn = display_resize();
983: reset_display();
1.102 zhuk 984: break;
985: case CMD_down:
986: skip++;
987: break;
988: case CMD_up:
989: if (skip > 0)
990: skip--;
991: break;
992: case CMD_pagedown:
993: skip += max_topn / 2;
994: break;
995: case CMD_pageup:
996: skip -= max_topn / 2;
997: if (skip < 0)
998: skip = 0;
1.68 tedu 999: break;
1.20 deraadt 1000: default:
1001: new_message(MT_standout, " BAD CASE IN SWITCH!");
1.51 otto 1002: putr();
1.1 downsj 1003: }
1004: }
1005:
1.20 deraadt 1006: /* flush out stuff that may have been written */
1007: fflush(stdout);
1008: return 0;
1.1 downsj 1009: }
1010:
1.20 deraadt 1011:
1.1 downsj 1012: /*
1013: * reset_display() - reset all the display routine pointers so that entire
1014: * screen will get redrawn.
1015: */
1.19 pvalchev 1016: static void
1017: reset_display(void)
1.1 downsj 1018: {
1.51 otto 1019: if (smart_terminal) {
1020: clear();
1021: refresh();
1022: }
1.1 downsj 1023: }
1024:
1.34 deraadt 1025: /* ARGSUSED */
1.19 pvalchev 1026: void
1.20 deraadt 1027: leave(int signo)
1.1 downsj 1028: {
1.20 deraadt 1029: leaveflag = 1;
1.1 downsj 1030: }
1031:
1.34 deraadt 1032: /* ARGSUSED */
1.19 pvalchev 1033: void
1.20 deraadt 1034: tstop(int signo)
1.1 downsj 1035: {
1.20 deraadt 1036: tstopflag = 1;
1.1 downsj 1037: }
1038:
1.34 deraadt 1039: /* ARGSUSED */
1.19 pvalchev 1040: void
1.51 otto 1041: sigwinch(int signo)
1.1 downsj 1042: {
1.20 deraadt 1043: winchflag = 1;
1.1 downsj 1044: }
1045:
1.34 deraadt 1046: /* ARGSUSED */
1.19 pvalchev 1047: void
1.20 deraadt 1048: onalrm(int signo)
1.1 downsj 1049: {
1050: }
1051:
1.19 pvalchev 1052: void
1.20 deraadt 1053: quit(int ret)
1.1 downsj 1054: {
1.20 deraadt 1055: end_screen();
1056: exit(ret);
1.1 downsj 1057: }