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