int main(int argc, char **argv) { /* For select(2). */ struct timeval tv; fd_set in; /* For parsing arguments. */ char *cp; /* The key read in. */ char c; struct sigaction sact; setlocale(LC_ALL, ""); get_options(); /* set to PCPU sorting */ register_sort_function( -1, (cmp_t)pcpu_sort); #ifdef HZ Hertz = HZ; #endif /* * Parse arguments. */ argv++; while (*argv) { cp = *argv++; while (*cp) { switch (*cp) { case 'd': if (cp[1]) { if (sscanf(++cp, "%f", &Sleeptime) != 1) { fprintf(stderr, PROGNAME ": Bad delay time `%s'\n", cp); exit(1); } goto breakargv; } else if (*argv) { /* last char in an argv, use next as arg */ if (sscanf(cp = *argv++, "%f", &Sleeptime) != 1) { fprintf(stderr, PROGNAME ": Bad delay time `%s'\n", cp); exit(1); } goto breakargv; } else { fprintf(stderr, "-d requires an argument\n"); exit(1); } break; case 'n': if (cp[1]) { if (sscanf(++cp, "%d", &Loops) != 1) { fprintf(stderr, PROGNAME ": Bad value `%s'\n", cp); exit(1); } goto breakargv; } else if (*argv) { /* last char in an argv, use next as arg */ if (sscanf(cp = *argv++, "%d", &Loops) != 1) { fprintf(stderr, PROGNAME ": Bad value `%s'\n", cp); exit(1); } goto breakargv; } break; case 'q': if (!getuid()) /* set priority to -10 in order to stay above kswapd */ if (setpriority(PRIO_PROCESS, getpid(), -10)) { /* We check this just for paranoia. It's not fatal, and shouldn't happen. */ perror(PROGNAME ": setpriority() failed"); } Sleeptime = 0; break; case 'p': if (monpids_index >= monpids_max) { fprintf(stderr, PROGNAME ": More than %u process ids specified\n", monpids_max); exit(1); } if (cp[1]) { if (sscanf(++cp, "%d", &monpids[monpids_index]) != 1 || monpids[monpids_index] < 0 || monpids[monpids_index] > 65535) { fprintf(stderr, PROGNAME ": Bad process id `%s'\n", cp); exit(1); } } else if (*argv) { /* last char in an argv, use next as arg */ if (sscanf(cp = *argv++, "%d", &monpids[monpids_index]) != 1 || monpids[monpids_index] < 0 || monpids[monpids_index] > 65535) { fprintf(stderr, PROGNAME ": Bad process id `%s'\n", cp); exit(1); } } else { fprintf(stderr, "-p requires an argument\n"); exit(1); } if (!monpids[monpids_index]) monpids[monpids_index] = getpid(); /* default to no sorting when monitoring process ids */ if (!monpids_index++) { sort_type = S_NONE; reset_sort_options(); } cp = "_"; break; case 'b': Batch = 1; break; case 'c': show_cmd = !show_cmd; break; case 'S': Cumulative = 1; break; case 'i': Noidle = 1; break; case 's': Secure = 1; break; case 'C': CPU_states = 1; break; case '-': break; /* Just ignore it */ case 'v': case 'V': fprintf(stdout, "top (%s)\n", procps_version); exit(0); case 'h': fprintf(stdout, "usage: " PROGNAME " -hvbcisqS -d delay -p pid -n iterations\n"); exit(0); default: fprintf(stderr, PROGNAME ": Unknown argument `%c'\n", *cp); fprintf(stdout, "usage: " PROGNAME " -hvbcisqS -d delay -p pid -n iterations\n"); exit(1); } cp++; } breakargv:; } if (nr_cpu > 1 && CPU_states) header_lines++; setup_terminal(); window_size(0); /* * Set up signal handlers. */ sact.sa_handler = sig_end; sact.sa_flags = 0; sigemptyset(&sact.sa_mask); sigaction(SIGHUP, &sact, NULL); sigaction(SIGINT, &sact, NULL); sigaction(SIGQUIT, &sact, NULL); sact.sa_handler = sig_stop; sact.sa_flags = SA_RESTART; sigaction(SIGTSTP, &sact, NULL); sact.sa_handler = window_size; sigaction(SIGWINCH, &sact, NULL); sigaction(SIGCONT, &sact, NULL); /* loop, collecting process info and sleeping */ while (1) { if (Loops > 0) Loops--; /* display the tasks */ show_procs(); /* sleep & wait for keyboard input */ if (Loops == 0) sig_end(0); if (!Batch) { tv.tv_sec = Sleeptime; tv.tv_usec = (Sleeptime - (int) Sleeptime) * 1000000; FD_ZERO(&in); FD_SET(0, &in); if (select(1, &in, 0, 0, &tv) > 0 && read(0, &c, 1) == 1) do_key(c); } else { sleep(Sleeptime); } } }
/* * parse the options string as read from the config file(s). * if top is in secure mode, disallow changing of the delay time between * screen updates. */ void parse_options(char *Options, int secure) { int i; for (i = 0; i < strlen(Options); i++) { switch (Options[i]) { case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': if (!secure) Sleeptime = (float) Options[i] - '0'; break; case 'S': Cumulative = 1; headers[22][1] = 'C'; break; case 's': Secure = 1; break; case 'i': Noidle = 1; break; case 'm': show_memory = 0; header_lines -= 2; break; case 'M': sort_type = S_MEM; reset_sort_options(); register_sort_function( -1, (cmp_t)mem_sort); break; case 'l': show_loadav = 0; header_lines -= 1; break; case 'P': sort_type = S_PCPU; reset_sort_options(); register_sort_function( -1, (cmp_t)pcpu_sort); break; case 'N': sort_type = S_NONE; reset_sort_options(); break; case 'A': sort_type = S_AGE; reset_sort_options(); register_sort_function( -1, (cmp_t)age_sort); break; case 't': show_stats = 0; header_lines -= 2; break; case 'T': sort_type = S_TIME; reset_sort_options(); register_sort_function( -1, (cmp_t)time_sort); break; case 'c': show_cmd = 0; break; case '\n': break; case 'I': Irixmode = 0; break; default: fprintf(stderr, "Wrong configuration option %c\n", i); exit(1); break; } } }
/* * Process keyboard input during the main loop */ void do_key(char c) { int numinput, i; char rcfile[MAXNAMELEN]; FILE *fp; /* * First the commands which don't require a terminal mode switch. */ if (c == 'q') sig_end(0); else if (c == ' ') return; else if (c == 12) { clear_screen(); return; } else if (c == 'I') { Irixmode=(Irixmode) ? 0 : 1; return; } /* * Switch the terminal to normal mode. (Will the original * attributes always be normal? Does it matter? I suppose the * shell will be set up the way the user wants it.) */ if (!Batch) tcsetattr(0, TCSANOW, &Savetty); /* * Handle the rest of the commands. */ switch (c) { case '?': case 'h': PUTP(cl); PUTP(ho); putchar('\n'); PUTP(mr); printf("Proc-Top Revision 1.2"); PUTP(me); putchar('\n'); printf("Secure mode "); PUTP(md); fputs(Secure ? "on" : "off", stdout); PUTP(me); fputs("; cumulative mode ", stdout); PUTP(md); fputs(Cumulative ? "on" : "off", stdout); PUTP(me); fputs("; noidle mode ", stdout); PUTP(md); fputs(Noidle ? "on" : "off", stdout); PUTP(me); fputs("\n\n", stdout); printf("%s\n\nPress any key to continue", Secure ? SECURE_HELP_SCREEN : HELP_SCREEN); if (!Batch) tcsetattr(0, TCSANOW, &Rawtty); (void) getchar(); break; case 'i': Noidle = !Noidle; SHOWMESSAGE(("No-idle mode %s", Noidle ? "on" : "off")); break; case 'u': SHOWMESSAGE(("Which User (Blank for All): ")); strcpy(CurrUser,getstr()); break; case 'k': if (Secure) SHOWMESSAGE(("\aCan't kill in secure mode")); else { int pid, signal; PUTP(md); SHOWMESSAGE(("PID to kill: ")); pid = getint(); if (pid == BAD_INPUT) break; PUTP(top_clrtoeol); SHOWMESSAGE(("Kill PID %d with signal [15]: ", pid)); PUTP(me); signal = getsig(); if (signal == -1) signal = SIGTERM; if (kill(pid, signal)) SHOWMESSAGE(("\aKill of PID %d with %d failed: %s", pid, signal, strerror(errno))); } break; case 'l': SHOWMESSAGE(("Display load average %s", !show_loadav ? "on" : "off")); if (show_loadav) { show_loadav = 0; header_lines--; } else { show_loadav = 1; header_lines++; } Numfields = make_header(); break; case 'm': SHOWMESSAGE(("Display memory information %s", !show_memory ? "on" : "off")); if (show_memory) { show_memory = 0; header_lines -= 2; } else { show_memory = 1; header_lines += 2; } Numfields = make_header(); break; case 'M': SHOWMESSAGE(("Sort by memory usage")); sort_type = S_MEM; reset_sort_options(); register_sort_function(-1, (cmp_t)mem_sort); break; case 'n': case '#': printf("Processes to display (0 for unlimited): "); numinput = getint(); if (numinput != BAD_INPUT) { Display_procs = numinput; window_size(0); } break; case 'r': if (Secure) SHOWMESSAGE(("\aCan't renice in secure mode")); else { int pid, val; printf("PID to renice: "); pid = getint(); if (pid == BAD_INPUT) break; PUTP(tgoto(cm, 0, header_lines - 2)); PUTP(top_clrtoeol); printf("Renice PID %d to value: ", pid); val = getint(); if (val == BAD_INPUT) val = 10; if (setpriority(PRIO_PROCESS, pid, val)) SHOWMESSAGE(("\aRenice of PID %d to %d failed: %s", pid, val, strerror(errno))); } break; case 'P': SHOWMESSAGE(("Sort by CPU usage")); sort_type = S_PCPU; reset_sort_options(); register_sort_function(-1, (cmp_t)pcpu_sort); break; case 'A': SHOWMESSAGE(("Sort by age")); sort_type = S_AGE; reset_sort_options(); register_sort_function(-1, (cmp_t)age_sort); break; case 'N': SHOWMESSAGE(("Sort numerically by pid")); sort_type = S_NONE; reset_sort_options(); break; case 'c': show_cmd = !show_cmd; SHOWMESSAGE(("Show %s", show_cmd ? "command names" : "command line")); break; case 'S': Cumulative = !Cumulative; SHOWMESSAGE(("Cumulative mode %s", Cumulative ? "on" : "off")); if (Cumulative) headers[22][1] = 'C'; else headers[22][1] = ' '; Numfields = make_header(); break; case 's': if (Secure) SHOWMESSAGE(("\aCan't change delay in secure mode")); else { double tmp; printf("Delay between updates: "); tmp = getfloat(); if (!(tmp < 0)) Sleeptime = tmp; } break; case 't': SHOWMESSAGE(("Display summary information %s", !show_stats ? "on" : "off")); if (show_stats) { show_stats = 0; header_lines -= 2; } else { show_stats = 1; header_lines += 2; } Numfields = make_header(); break; case 'T': SHOWMESSAGE(("Sort by %stime", Cumulative ? "cumulative " : "")); sort_type = S_TIME; reset_sort_options(); register_sort_function( -1, (cmp_t)time_sort); break; case 'f': case 'F': change_fields(); break; case 'o': case 'O': change_order(); break; case 'W': if (getenv("HOME")) { strcpy(rcfile, getenv("HOME")); strcat(rcfile, "/"); strcat(rcfile, RCFILE); fp = fopen(rcfile, "w"); if (fp != NULL) { fprintf(fp, "%s\n", Fields); i = (int) Sleeptime; if (i < 2) i = 2; if (i > 9) i = 9; fprintf(fp, "%d", i); if (Secure) fprintf(fp, "%c", 's'); if (Cumulative) fprintf(fp, "%c", 'S'); if (!show_cmd) fprintf(fp, "%c", 'c'); if (Noidle) fprintf(fp, "%c", 'i'); if (!show_memory) fprintf(fp, "%c", 'm'); if (!show_loadav) fprintf(fp, "%c", 'l'); if (!show_stats) fprintf(fp, "%c", 't'); if (!Irixmode) fprintf(fp, "%c", 'I'); fprintf(fp, "\n"); fclose(fp); SHOWMESSAGE(("Wrote configuration to %s", rcfile)); } else { SHOWMESSAGE(("Couldn't open %s", rcfile)); } } else { SHOWMESSAGE(("Couldn't get $HOME -- not saving")); } break; default: SHOWMESSAGE(("\aUnknown command `%c' -- hit `h' for help", c)); } /* * Return to raw mode. */ if (!Batch) tcsetattr(0, TCSANOW, &Rawtty); return; }
int main(int argc, char **argv) { /* For select(2). */ struct timeval tv; fd_set in; /* For parsing arguments. */ char *cp; /* The key read in. */ char c; get_options(); /* * Parse arguments. */ argv++; while (*argv) { cp = *argv++; while (*cp) { switch (*cp) { case 'd': if (cp[1]) { if (sscanf(++cp, "%f", &Sleeptime) != 1) { fprintf(stderr, PROGNAME ": Bad delay time `%s'\n", cp); exit(1); } goto breakargv; } else if (*argv) { /* last char in an argv, use next as arg */ if (sscanf(cp = *argv++, "%f", &Sleeptime) != 1) { fprintf(stderr, PROGNAME ": Bad delay time `%s'\n", cp); exit(1); } goto breakargv; } else { fprintf(stderr, "-d requires an argument\n"); exit(1); } break; case 'q': if (!getuid()) /* set priority to -10 in order to stay above kswapd */ if (setpriority(PRIO_PROCESS, getpid(), -10)) { /* We check this just for paranoia. It's not fatal, and shouldn't happen. */ perror(PROGNAME ": setpriority() failed"); } Sleeptime = 0; break; case 'c': show_cmd = !show_cmd; break; case 'S': Cumulative = 1; break; case 'i': Noidle = 1; break; case 's': Secure = 1; break; case '-': break; /* Just ignore it */ #if defined (SHOWFLAG) case '/': showall++; #endif default: fprintf(stderr, PROGNAME ": Unknown argument `%c'\n", *cp); exit(1); } cp++; } breakargv: } /* set to PCPU sorting */ register_sort_function( -1, (cmp_t)pcpu_sort); /* for correct handling of some fields, we have to do distinguish * between kernel versions */ set_linux_version(); /* get kernel symbol table, if needed */ if (!CL_wchan_nout) { if (open_psdb()) { CL_wchan_nout = 1; } else { psdbsucc = 1; } } setup_terminal(); window_size(); /* * calculate header size, length of cmdline field ... */ Numfields = make_header(); /* * Set up signal handlers. */ signal(SIGHUP, (void *) (int) end); signal(SIGINT, (void *) (int) end); signal(SIGQUIT, (void *) (int) end); signal(SIGTSTP, (void *) (int) stop); signal(SIGWINCH, (void *) (int) window_size); /* loop, collecting process info and sleeping */ while (1) { if (setjmp(redraw_jmp)) clear_screen(); /* display the tasks */ show_procs(); /* sleep & wait for keyboard input */ tv.tv_sec = Sleeptime; tv.tv_usec = (Sleeptime - (int) Sleeptime) * 1000000; FD_ZERO(&in); FD_SET(0, &in); if (select(16, &in, 0, 0, &tv) > 0 && read(0, &c, 1) == 1) do_key(c); } } /*####################################################################### *#### Signal handled routines: error_end, end, stop, window_size ### *#### Small utilities: make_header, getstr, getint, getfloat, getsig ### *####################################################################### */ /* * end when exiting with an error. */ void error_end(int rno) { if (psdbsucc) close_psdb(); ioctl(0, TCSETAF, &Savetty); PUTP(tgoto(cm, 0, Lines - 1)); fputs("\r\n", stdout); exit(rno); } /* * Normal end of execution. */ void end(void) { if (psdbsucc) close_psdb(); ioctl(0, TCSETAF, &Savetty); PUTP(tgoto(cm, 0, Lines - 1)); fputs("\r\n", stdout); exit(0); } /* * SIGTSTP catcher. */ void stop(void) { /* Reset terminal. */ if (psdbsucc) close_psdb(); ioctl(0, TCSETAF, &Savetty); PUTP(tgoto(cm, 0, Lines - 3)); fflush(stdout); raise(SIGTSTP); /* Later... */ ioctl(0, TCSETAF, &Rawtty); signal(SIGTSTP, (void *) (int) stop); longjmp(redraw_jmp, 1); } /* * Reads the window size and clear the window. This is called on setup, * and also catches SIGWINCHs, and adjusts Maxlines. Basically, this is * the central place for window size stuff. */ void window_size(void) { struct winsize ws; if (ioctl(1, TIOCGWINSZ, &ws) != -1) { Cols = ws.ws_col; Lines = ws.ws_row; } else { Cols = tgetnum("co"); Lines = tgetnum("li"); } clear_screen(); } /* * this adjusts the lines needed for the header to the current value */ int make_header(void) { int i, j; j = 0; for (i = 0; i < strlen(Fields); i++) { if (isupper(Fields[i])) { pflags[j++] = Fields[i] - 'A'; } } strcpy(Header, ""); for (i = 0; i < j; i++) strcat(Header, headers[pflags[i]]); /* readjust window size ... */ Maxcmd = Cols - strlen(Header) + 7; Maxlines = Display_procs ? Display_procs : Lines - header_lines; if (Maxlines > Lines - header_lines) Maxlines = Lines - header_lines; return (j); }