#include <sys/types.h>
#include <sys/socket.h>
#include <curses.h>
#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <poll.h>
#include <pwd.h>
#include <stdlib.h>
#include <limits.h>
#include <unistd.h>
#include <stdbool.h>
#include "display.h"
#include "screen.h"
#include "top.h"
#include "top.local.h"
#include "machine.h"
#include "utils.h"
#define BUFFERSIZE 2048
char stdoutbuf[BUFFERSIZE];
static void leave(int);
static void onalrm(int);
static void tstop(int);
static void sigwinch(int);
volatile sig_atomic_t leaveflag, tstopflag, winchflag;
static void reset_display(void);
int rundisplay(void);
static int max_topn;
static int skip;
extern int ncpu;
extern int ncpuonline;
extern int (*proc_compares[])(const void *, const void *);
int order_index;
int rev_order;
int displays = 0;
int do_unames = true;
struct process_select ps;
int interactive = -1;
double delay = Default_DELAY;
char *order_name = NULL;
int topn = Default_TOPN;
int no_command = true;
int old_system = false;
int old_threads = false;
int show_args = false;
pid_t hlpid = (pid_t)-1;
int combine_cpus = 0;
#if Default_TOPN == Infinity
int topn_specified = false;
#endif
struct system_info system_info;
struct statics statics;
#define CMD_redraw 0
#define CMD_update 1
#define CMD_quit 2
#define CMD_help1 3
#define CMD_help2 4
#define CMD_OSLIMIT 4
#define CMD_errors 5
#define CMD_number1 6
#define CMD_number2 7
#define CMD_delay 8
#define CMD_displays 9
#define CMD_kill 10
#define CMD_renice 11
#define CMD_idletog 12
#define CMD_idletog2 13
#define CMD_user 14
#define CMD_system 15
#define CMD_order 16
#define CMD_pid 17
#define CMD_command 18
#define CMD_threads 19
#define CMD_grep 20
#define CMD_add 21
#define CMD_hl 22
#define CMD_cpus 23
#define CMD_down 24
#define CMD_up 25
#define CMD_pagedown 26
#define CMD_pageup 27
#define CMD_grep2 28
#define CMD_rtableid 29
#define CMD_rtable 30
static void
usage(void)
{
extern char *__progname;
fprintf(stderr,
"usage: %s [-1bCHIinqStu] [-d count] [-g string] [-o [-]field] "
"[-p pid] [-s time]\n\t[-T [-]rtable] [-U [-]user] [number]\n",
__progname);
}
static int
getorder(char *field)
{
int i, r = field[0] == '-';
i = string_index(r ? field + 1 : field, statics.order_names);
if (i != -1)
rev_order = r;
return i;
}
static int
filteruser(char buf[])
{
const char *errstr;
char *bufp = buf;
uid_t *uidp;
uid_t uid;
if (bufp[0] == '-') {
bufp++;
uidp = &ps.huid;
ps.uid = (pid_t)-1;
} else {
uidp = &ps.uid;
ps.huid = (pid_t)-1;
}
if (uid_from_user(bufp, uidp) == 0)
return 0;
uid = strtonum(bufp, 0, UID_MAX, &errstr);
if (errstr == NULL && user_from_uid(uid, 1) != NULL) {
*uidp = uid;
return 0;
}
return -1;
}
static int
filterpid(char buf[], int hl)
{
const char *errstr;
int pid;
pid = strtonum(buf, 0, INT_MAX, &errstr);
if (errstr != NULL || !find_pid(pid))
return -1;
if (hl)
hlpid = (pid_t)pid;
else {
if (!ps.system)
old_system = false;
ps.pid = (pid_t)pid;
ps.system = true;
}
return 0;
}
static int
filterrtable(char buf[])
{
const char *errstr;
char *bufp = buf;
uint32_t *rtableidp;
uint32_t rtableid;
if (bufp[0] == '-') {
bufp++;
rtableidp = &ps.hrtableid;
ps.rtableid = -1;
} else {
rtableidp = &ps.rtableid;
ps.hrtableid = -1;
}
rtableid = strtonum(bufp, 0, RT_TABLEID_MAX, &errstr);
if (errstr == NULL) {
*rtableidp = rtableid;
return 0;
}
return -1;
}
static void
parseargs(int ac, char **av)
{
char *endp;
int i;
while ((i = getopt(ac, av, "1SHICbinqus:d:p:U:o:g:T:t")) != -1) {
switch (i) {
case '1':
combine_cpus = 1;
break;
case 'C':
show_args = true;
break;
case 'u':
do_unames = !do_unames;
break;
case 'U':
if (filteruser(optarg) == -1)
new_message(MT_delayed, "%s: unknown user",
optarg);
break;
case 'p':
if (filterpid(optarg, false) == -1)
new_message(MT_delayed, "%s: unknown pid",
optarg);
break;
case 'S':
ps.system = !ps.system;
old_system = !old_system;
break;
case 'H':
ps.threads = true;
old_threads = true;
break;
case 'I':
ps.idle = !ps.idle;
break;
case 'i':
interactive = true;
break;
case 'n':
case 'b':
interactive = false;
break;
case 'd':
if ((i = atoiwi(optarg)) != Invalid && i != 0) {
displays = i;
if (displays == 1)
interactive = false;
break;
}
new_message(MT_delayed,
"warning: display count should be positive "
"-- option ignored");
break;
case 's':
delay = strtod(optarg, &endp);
if (delay >= 0 && delay <= 1000000 && *endp == '\0')
break;
new_message(MT_delayed,
"warning: delay should be a non-negative number"
" -- using default");
delay = Default_DELAY;
break;
case 'q':
if (getuid() == 0) {
(void) nice(-20);
break;
}
new_message(MT_delayed,
"warning: `-q' option can only be used by root");
break;
case 'o':
order_name = optarg;
break;
case 'g':
free(ps.command);
if ((ps.command = strdup(optarg)) == NULL)
err(1, NULL);
break;
case 'T':
if (filterrtable(optarg) == -1)
new_message(MT_delayed,
"%s: invalid routing table", optarg);
break;
case 't':
ps.rtable = true;
break;
default:
usage();
exit(1);
}
}
i = getncpuonline();
if (i == -1)
err(1, NULL);
if (i > 8)
combine_cpus = 1;
if (optind < ac) {
if ((topn = atoiwi(av[optind])) == Invalid) {
new_message(MT_delayed,
"warning: process count should "
"be a non-negative number -- using default");
topn = Infinity;
}
#if Default_TOPN == Infinity
else
topn_specified = true;
#endif
}
}
int
main(int argc, char *argv[])
{
char *header_text, *env_top;
char *uname_field = "USERNAME", *thread_field = " TID";
char *wait_field = "WAIT ", *rtable_field = " RTABLE";
const char *(*get_userid)(uid_t, int) = user_from_uid;
char **preset_argv = NULL, **av = argv;
int preset_argc = 0, ac = argc, active_procs, i, ncpuonline_now;
sigset_t mask, oldmask;
time_t curr_time;
struct handle *processes;
#ifdef DEBUG
setvbuf(stdout, NULL, _IONBUF, 0);
#else
setvbuf(stdout, stdoutbuf, _IOFBF, sizeof stdoutbuf);
#endif
ps.idle = true;
ps.system = false;
ps.uid = (uid_t)-1;
ps.huid = (uid_t)-1;
ps.pid = (pid_t)-1;
ps.rtable = false;
ps.rtableid = -1;
ps.hrtableid = -1;
ps.command = NULL;
if ((env_top = getenv("TOP")) != NULL) {
av = preset_argv = argparse(env_top, &preset_argc);
ac = preset_argc;
preset_argv[0] = "while processing environment";
}
do {
if (preset_argc == 0) {
ac = argc;
av = argv;
optind = 1;
}
parseargs(ac, av);
i = preset_argc;
preset_argc = 0;
} while (i != 0);
if (pledge("stdio rpath getpw tty proc ps vminfo", NULL) == -1)
err(1, "pledge");
if (!do_unames) {
uname_field = " UID ";
get_userid = format_uid;
}
if (machine_init(&statics) == -1)
exit(1);
if (order_name != NULL) {
if ((order_index = getorder(order_name)) == -1) {
new_message(MT_delayed,
" %s: unrecognized sorting order", order_name);
order_index = 0;
}
}
init_termcap(interactive);
max_topn = display_init(&statics);
if (topn > max_topn)
new_message(MT_delayed,
"warning: this terminal can only display %d processes",
max_topn);
if (topn == Infinity) {
#if Default_TOPN == Infinity
topn = smart_terminal ? Largest :
(topn_specified ? Largest : Nominal_TOPN);
#else
topn = Largest;
#endif
}
display_header(topn > 0);
if (interactive == -1)
interactive = smart_terminal;
if (displays == 0)
displays = smart_terminal ? Infinity : 1;
sigemptyset(&mask);
sigaddset(&mask, SIGINT);
sigaddset(&mask, SIGQUIT);
sigaddset(&mask, SIGTSTP);
sigprocmask(SIG_BLOCK, &mask, &oldmask);
if (interactive)
init_screen();
if (pledge("stdio getpw tty proc ps vminfo", NULL) == -1)
err(1, "pledge");
(void) signal(SIGINT, leave);
siginterrupt(SIGINT, 1);
(void) signal(SIGQUIT, leave);
(void) signal(SIGTSTP, tstop);
if (smart_terminal)
(void) signal(SIGWINCH, sigwinch);
sigprocmask(SIG_SETMASK, &oldmask, NULL);
restart:
while ((displays == -1) || (displays-- > 0)) {
if (winchflag) {
get_screensize();
resizeterm(screen_length, screen_width + 1);
max_topn = display_resize();
(void) signal(SIGWINCH, sigwinch);
reset_display();
winchflag = 0;
}
get_system_info(&system_info);
if (interactive) {
for (i = ncpuonline_now = 0; i < ncpu; i++)
if (system_info.cpuonline[i])
ncpuonline_now++;
if (ncpuonline_now != ncpuonline) {
max_topn = display_resize();
reset_display();
continue;
}
}
processes = get_process_info(&system_info, &ps,
proc_compares[order_index]);
i_loadave(system_info.load_avg);
time(&curr_time);
i_timeofday(&curr_time);
i_procstates(system_info.p_total, system_info.procstates,
ps.threads);
i_cpustates(system_info.cpustates, system_info.cpuonline);
i_memory(system_info.memory);
i_message();
header_text = format_header(
ps.threads ? thread_field : uname_field,
ps.rtable ? rtable_field : wait_field);
i_header(header_text);
if (topn == Infinity) {
#if Default_TOPN == Infinity
topn = smart_terminal ? Largest :
(topn_specified ? Largest : Nominal_TOPN);
#else
topn = Largest;
#endif
}
if (topn > 0) {
active_procs = system_info.p_active;
if (active_procs > topn)
active_procs = topn;
if (active_procs > max_topn)
active_procs = max_topn;
if (skip + active_procs > system_info.p_active)
skip = system_info.p_active - active_procs;
skip_processes(processes, skip);
for (i = 0; i < active_procs; i++) {
pid_t pid;
char * s;
s = format_next_process(processes,
ps.threads ? NULL : get_userid, ps.rtable,
&pid);
i_process(i, s, pid == hlpid);
}
}
u_endscreen();
fflush(stdout);
if (smart_terminal)
refresh();
if (displays) {
no_command = true;
if (!interactive) {
(void) signal(SIGALRM, onalrm);
(void) alarm((unsigned) delay);
pause();
if (leaveflag)
exit(0);
if (tstopflag) {
(void) signal(SIGTSTP, SIG_DFL);
(void) kill(0, SIGTSTP);
(void) signal(SIGTSTP, tstop);
tstopflag = 0;
}
} else {
while (no_command)
if (rundisplay())
goto restart;
}
}
}
quit(0);
return (0);
}
int
rundisplay(void)
{
static char tempbuf[TEMPBUFSIZE];
sigset_t mask;
char ch, *iptr;
int change, i;
struct pollfd pfd[1];
static char command_chars[] = "\f qh?en#sdkriIuSopCHg+P109)(/Tt";
no_command = false;
pfd[0].fd = STDIN_FILENO;
pfd[0].events = POLLIN;
if (leaveflag)
quit(0);
if (tstopflag) {
end_screen();
fflush(stdout);
(void) signal(SIGTSTP, SIG_DFL);
sigemptyset(&mask);
sigaddset(&mask, SIGTSTP);
sigprocmask(SIG_UNBLOCK, &mask, NULL);
(void) kill(0, SIGTSTP);
(void) signal(SIGTSTP, tstop);
reinit_screen();
reset_display();
tstopflag = 0;
return 1;
}
if (poll(pfd, 1, (int)(delay * 1000)) > 0) {
char *errmsg;
ssize_t len;
if ((pfd[0].revents & (POLLERR|POLLHUP|POLLNVAL)))
exit(1);
clear_message();
while (1) {
len = read(STDIN_FILENO, &ch, 1);
if (len == -1 && errno == EINTR)
continue;
if (len == 0)
exit(1);
break;
}
if ((iptr = strchr(command_chars, ch)) == NULL) {
new_message(MT_standout, " Command not understood");
putr();
no_command = true;
fflush(stdout);
return (0);
}
change = iptr - command_chars;
switch (change) {
case CMD_redraw:
reset_display();
break;
case CMD_update:
if (system_info.load_avg[0] > LoadMax) {
go_home();
fflush(stdout);
}
break;
case CMD_quit:
quit(0);
break;
case CMD_help1:
case CMD_help2:
clear();
show_help();
anykey();
clear();
break;
case CMD_errors:
if (error_count() == 0) {
new_message(MT_standout,
" Currently no errors to report.");
putr();
no_command = true;
} else {
clear();
show_errors();
anykey();
clear();
}
break;
case CMD_number1:
case CMD_number2:
new_message(MT_standout,
"Number of processes to show: ");
if (readline(tempbuf, 8) > 0) {
if ((i = atoiwi(tempbuf)) != Invalid) {
if (i > max_topn) {
new_message(MT_standout |
MT_delayed,
" This terminal can only "
"display %d processes.",
max_topn);
putr();
}
if ((i > topn || i == Infinity)
&& topn == 0) {
display_header(true);
} else if (i == 0)
display_header(false);
topn = i;
} else {
new_message(MT_standout,
"Processes should be a "
"non-negative number");
putr();
no_command = true;
}
} else
clear_message();
break;
case CMD_delay:
new_message(MT_standout, "Seconds to delay: ");
if (readline(tempbuf, sizeof(tempbuf)) > 0) {
char *endp;
double newdelay = strtod(tempbuf, &endp);
if (newdelay >= 0 && newdelay <= 1000000 &&
*endp == '\0') {
delay = newdelay;
} else {
new_message(MT_standout,
"Delay should be a non-negative number");
putr();
no_command = true;
}
} else
clear_message();
break;
case CMD_displays:
new_message(MT_standout,
"Displays to show (currently %s): ",
displays == -1 ? "infinite" :
itoa(displays));
if (readline(tempbuf, 10) > 0) {
if ((i = atoiwi(tempbuf)) != Invalid) {
if (i == 0)
quit(0);
displays = i;
} else {
new_message(MT_standout,
"Displays should be a non-negative number");
putr();
no_command = true;
}
} else
clear_message();
break;
case CMD_kill:
new_message(0, "kill ");
if (readline(tempbuf, sizeof(tempbuf)) > 0) {
if ((errmsg = kill_procs(tempbuf)) != NULL) {
new_message(MT_standout, "%s", errmsg);
putr();
no_command = true;
}
} else
clear_message();
break;
case CMD_renice:
new_message(0, "renice ");
if (readline(tempbuf, sizeof(tempbuf)) > 0) {
if ((errmsg = renice_procs(tempbuf)) != NULL) {
new_message(MT_standout, "%s", errmsg);
putr();
no_command = true;
}
} else
clear_message();
break;
case CMD_idletog:
case CMD_idletog2:
ps.idle = !ps.idle;
new_message(MT_standout | MT_delayed,
" %sisplaying idle processes.",
ps.idle ? "D" : "Not d");
putr();
break;
case CMD_user:
new_message(MT_standout,
"Username to show: ");
if (readline(tempbuf, sizeof(tempbuf)) > 0) {
if ((tempbuf[0] == '+' || tempbuf[0] == '-') &&
tempbuf[1] == '\0') {
ps.uid = (uid_t)-1;
ps.huid = (uid_t)-1;
} else if (filteruser(tempbuf) == -1) {
new_message(MT_standout,
" %s: unknown user",
tempbuf[0] == '-' ? tempbuf + 1 :
tempbuf);
no_command = true;
}
putr();
} else
clear_message();
break;
case CMD_system:
ps.system = !ps.system;
old_system = ps.system;
new_message(MT_standout | MT_delayed,
" %sisplaying system processes.",
ps.system ? "D" : "Not d");
break;
case CMD_order:
new_message(MT_standout,
"Order to sort: ");
if (readline(tempbuf, sizeof(tempbuf)) > 0) {
if ((i = getorder(tempbuf)) == -1) {
new_message(MT_standout,
" %s: unrecognized sorting order",
tempbuf[0] == '-' ? tempbuf + 1 :
tempbuf);
no_command = true;
} else
order_index = i;
putr();
} else
clear_message();
break;
case CMD_pid:
new_message(MT_standout, "Process ID to show: ");
if (readline(tempbuf, sizeof(tempbuf)) > 0) {
if (tempbuf[0] == '+' &&
tempbuf[1] == '\0') {
ps.pid = (pid_t)-1;
ps.system = old_system;
} else if (filterpid(tempbuf, 0) == -1) {
new_message(MT_standout,
" %s: unknown pid", tempbuf);
no_command = true;
}
putr();
} else
clear_message();
break;
case CMD_command:
show_args = !show_args;
break;
case CMD_threads:
ps.threads = !ps.threads;
old_threads = ps.threads;
new_message(MT_standout | MT_delayed,
" %sisplaying threads.",
ps.threads ? "D" : "Not d");
break;
case CMD_grep:
case CMD_grep2:
new_message(MT_standout,
"Grep command name: ");
if (readline(tempbuf, sizeof(tempbuf)) > 0) {
free(ps.command);
if (tempbuf[0] == '+' &&
tempbuf[1] == '\0')
ps.command = NULL;
else if ((ps.command = strdup(tempbuf)) == NULL)
err(1, NULL);
putr();
} else
clear_message();
break;
case CMD_hl:
new_message(MT_standout, "Process ID to highlight: ");
if (readline(tempbuf, sizeof(tempbuf)) > 0) {
if (tempbuf[0] == '+' &&
tempbuf[1] == '\0') {
hlpid = (pid_t)-1;
} else if (filterpid(tempbuf, true) == -1) {
new_message(MT_standout,
" %s: unknown pid", tempbuf);
no_command = true;
}
putr();
} else
clear_message();
break;
case CMD_add:
ps.uid = (uid_t)-1;
ps.huid = (uid_t)-1;
ps.pid = (pid_t)-1;
ps.rtableid = -1;
ps.hrtableid = -1;
ps.system = old_system;
ps.command = NULL;
hlpid = (pid_t)-1;
break;
case CMD_cpus:
combine_cpus = !combine_cpus;
max_topn = display_resize();
reset_display();
break;
case CMD_down:
skip++;
break;
case CMD_up:
if (skip > 0)
skip--;
break;
case CMD_pagedown:
skip += max_topn / 2;
break;
case CMD_pageup:
skip -= max_topn / 2;
if (skip < 0)
skip = 0;
break;
case CMD_rtableid:
new_message(MT_standout,
"Routing table: ");
if (readline(tempbuf, sizeof(tempbuf)) > 0) {
if (tempbuf[0] == '+' && tempbuf[1] == '\0') {
ps.rtableid = -1;
ps.hrtableid = -1;
} else if (filterrtable(tempbuf) == -1) {
new_message(MT_standout,
" %s: invalid routing table",
tempbuf[0] == '-' ? tempbuf + 1 :
tempbuf);
no_command = true;
}
putr();
} else
clear_message();
break;
case CMD_rtable:
ps.rtable = !ps.rtable;
new_message(MT_standout | MT_delayed,
" %sisplaying routing tables.",
ps.rtable ? "D" : "Not d");
break;
default:
new_message(MT_standout, " BAD CASE IN SWITCH!");
putr();
}
}
fflush(stdout);
return 0;
}
static void
reset_display(void)
{
if (smart_terminal) {
clear();
refresh();
}
}
void
leave(int signo)
{
leaveflag = 1;
}
void
tstop(int signo)
{
tstopflag = 1;
}
void
sigwinch(int signo)
{
winchflag = 1;
}
void
onalrm(int signo)
{
}
void
quit(int ret)
{
end_screen();
exit(ret);
}