This commit is contained in:
Hisham Muhammad 2016-01-11 20:39:08 -02:00
commit d850803eb8
6 changed files with 38 additions and 19 deletions

View File

@ -404,7 +404,7 @@ static struct { const char* key; const char* info; } helpRight[] = {
#if (HAVE_LIBHWLOC || HAVE_NATIVE_AFFINITY) #if (HAVE_LIBHWLOC || HAVE_NATIVE_AFFINITY)
{ .key = " a: ", .info = "set CPU affinity" }, { .key = " a: ", .info = "set CPU affinity" },
#endif #endif
{ .key = " i: ", .info = "set IO prority" }, { .key = " i: ", .info = "set IO priority" },
{ .key = " l: ", .info = "list open files with lsof" }, { .key = " l: ", .info = "list open files with lsof" },
{ .key = " s: ", .info = "trace syscalls with strace" }, { .key = " s: ", .info = "trace syscalls with strace" },
{ .key = " ", .info = "" }, { .key = " ", .info = "" },

View File

@ -513,8 +513,11 @@ void Process_toggleTag(Process* this) {
} }
bool Process_setPriority(Process* this, int priority) { bool Process_setPriority(Process* this, int priority) {
uid_t euid = geteuid();
seteuid(getuid());
int old_prio = getpriority(PRIO_PROCESS, this->pid); int old_prio = getpriority(PRIO_PROCESS, this->pid);
int err = setpriority(PRIO_PROCESS, this->pid, priority); int err = setpriority(PRIO_PROCESS, this->pid, priority);
seteuid(euid);
if (err == 0 && old_prio != getpriority(PRIO_PROCESS, this->pid)) { if (err == 0 && old_prio != getpriority(PRIO_PROCESS, this->pid)) {
this->nice = priority; this->nice = priority;
} }
@ -526,7 +529,10 @@ bool Process_changePriorityBy(Process* this, size_t delta) {
} }
void Process_sendSignal(Process* this, size_t sgn) { void Process_sendSignal(Process* this, size_t sgn) {
uid_t euid = geteuid();
seteuid(getuid());
kill(this->pid, (int) sgn); kill(this->pid, (int) sgn);
seteuid(euid);
} }
long Process_pidCompare(const void* v1, const void* v2) { long Process_pidCompare(const void* v1, const void* v2) {

View File

@ -154,7 +154,12 @@ static void readFields(ProcessField* fields, int* flags, const char* line) {
} }
static bool Settings_read(Settings* this, const char* fileName) { static bool Settings_read(Settings* this, const char* fileName) {
FILE* fd = fopen(fileName, "r"); FILE* fd;
uid_t euid = geteuid();
seteuid(getuid());
fd = fopen(fileName, "r");
seteuid(euid);
if (!fd) if (!fd)
return false; return false;
@ -260,7 +265,11 @@ static void writeMeterModes(Settings* this, FILE* fd, int column) {
bool Settings_write(Settings* this) { bool Settings_write(Settings* this) {
FILE* fd; FILE* fd;
uid_t euid = geteuid();
seteuid(getuid());
fd = fopen(this->filename, "w"); fd = fopen(this->filename, "w");
seteuid(euid);
if (fd == NULL) { if (fd == NULL) {
return false; return false;
} }
@ -345,6 +354,8 @@ Settings* Settings_new(int cpuCount) {
htopDir = String_cat(home, "/.config/htop"); htopDir = String_cat(home, "/.config/htop");
} }
legacyDotfile = String_cat(home, "/.htoprc"); legacyDotfile = String_cat(home, "/.htoprc");
uid_t euid = geteuid();
seteuid(getuid());
(void) mkdir(configDir, 0700); (void) mkdir(configDir, 0700);
(void) mkdir(htopDir, 0700); (void) mkdir(htopDir, 0700);
free(htopDir); free(htopDir);
@ -357,6 +368,7 @@ Settings* Settings_new(int cpuCount) {
free(legacyDotfile); free(legacyDotfile);
legacyDotfile = NULL; legacyDotfile = NULL;
} }
seteuid(euid);
} }
this->colorScheme = 0; this->colorScheme = 0;
this->changed = false; this->changed = false;

View File

@ -86,6 +86,7 @@ void TraceScreen_run(TraceScreen* this) {
int child = fork(); int child = fork();
if (child == -1) return; if (child == -1) return;
if (child == 0) { if (child == 0) {
seteuid(getuid());
dup2(fdpair[1], STDERR_FILENO); dup2(fdpair[1], STDERR_FILENO);
int ok = fcntl(fdpair[1], F_SETFL, O_NONBLOCK); int ok = fcntl(fdpair[1], F_SETFL, O_NONBLOCK);
if (ok != -1) { if (ok != -1) {

View File

@ -103,7 +103,7 @@ if test "x$enable_proc" = xyes; then
AC_DEFINE(HAVE_PROC, 1, [Define if using a Linux-compatible proc filesystem.]) AC_DEFINE(HAVE_PROC, 1, [Define if using a Linux-compatible proc filesystem.])
fi fi
AC_ARG_WITH(proc, [ --with-proc=DIR Location of a Linux-compatible proc filesystem (default=/proc).], AC_ARG_WITH(proc, [AC_HELP_STRING([--with-proc=DIR], [Location of a Linux-compatible proc filesystem (default=/proc).])],
if test -n "$withval"; then if test -n "$withval"; then
AC_DEFINE_UNQUOTED(PROCDIR, "$withval", [Path of proc filesystem]) AC_DEFINE_UNQUOTED(PROCDIR, "$withval", [Path of proc filesystem])

View File

@ -49,8 +49,8 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, ui
int mib[] = { CTL_HW, HW_NCPU }; int mib[] = { CTL_HW, HW_NCPU };
int fmib[] = { CTL_KERN, KERN_FSCALE }; int fmib[] = { CTL_KERN, KERN_FSCALE };
int i, e; int i, e;
OpenBSDProcessList* fpl = calloc(1, sizeof(OpenBSDProcessList)); OpenBSDProcessList* opl = calloc(1, sizeof(OpenBSDProcessList));
ProcessList* pl = (ProcessList*) fpl; ProcessList* pl = (ProcessList*) opl;
size_t size = sizeof(pl->cpuCount); size_t size = sizeof(pl->cpuCount);
ProcessList_init(pl, Class(OpenBSDProcess), usersTable, pidWhiteList, userId); ProcessList_init(pl, Class(OpenBSDProcess), usersTable, pidWhiteList, userId);
@ -58,31 +58,31 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, ui
if (e == -1 || pl->cpuCount < 1) { if (e == -1 || pl->cpuCount < 1) {
pl->cpuCount = 1; pl->cpuCount = 1;
} }
fpl->cpus = realloc(fpl->cpus, pl->cpuCount * sizeof(CPUData)); opl->cpus = realloc(opl->cpus, pl->cpuCount * sizeof(CPUData));
size = sizeof(fscale); size = sizeof(fscale);
if (sysctl(fmib, 2, &fscale, &size, NULL, 0) < 0) if (sysctl(fmib, 2, &fscale, &size, NULL, 0) < 0)
err(1, "fscale sysctl call failed"); err(1, "fscale sysctl call failed");
for (i = 0; i < pl->cpuCount; i++) { for (i = 0; i < pl->cpuCount; i++) {
fpl->cpus[i].totalTime = 1; opl->cpus[i].totalTime = 1;
fpl->cpus[i].totalPeriod = 1; opl->cpus[i].totalPeriod = 1;
} }
pageSizeKb = PAGE_SIZE_KB; pageSizeKb = PAGE_SIZE_KB;
// XXX: last arg should eventually be an errbuf // XXX: last arg should eventually be an errbuf
fpl->kd = kvm_open(NULL, NULL, NULL, KVM_NO_FILES, NULL); opl->kd = kvm_open(NULL, NULL, NULL, KVM_NO_FILES, NULL);
assert(fpl->kd); assert(opl->kd);
return pl; return pl;
} }
void ProcessList_delete(ProcessList* this) { void ProcessList_delete(ProcessList* this) {
const OpenBSDProcessList* fpl = (OpenBSDProcessList*) this; const OpenBSDProcessList* opl = (OpenBSDProcessList*) this;
if (fpl->kd) kvm_close(fpl->kd); if (opl->kd) kvm_close(opl->kd);
free(fpl->cpus); free(opl->cpus);
ProcessList_done(this); ProcessList_done(this);
free(this); free(this);
@ -102,7 +102,7 @@ static inline void OpenBSDProcessList_scanMemoryInfo(ProcessList* pl) {
pl->totalMem = uvmexp.npages * pageSizeKb; pl->totalMem = uvmexp.npages * pageSizeKb;
/* /*
const OpenBSDProcessList* fpl = (OpenBSDProcessList*) pl; const OpenBSDProcessList* opl = (OpenBSDProcessList*) pl;
size_t len = sizeof(pl->totalMem); size_t len = sizeof(pl->totalMem);
sysctl(MIB_hw_physmem, 2, &(pl->totalMem), &len, NULL, 0); sysctl(MIB_hw_physmem, 2, &(pl->totalMem), &len, NULL, 0);
@ -114,7 +114,7 @@ static inline void OpenBSDProcessList_scanMemoryInfo(ProcessList* pl) {
pl->cachedMem *= pageSizeKb; pl->cachedMem *= pageSizeKb;
struct kvm_swap swap[16]; struct kvm_swap swap[16];
int nswap = kvm_getswapinfo(fpl->kd, swap, sizeof(swap)/sizeof(swap[0]), 0); int nswap = kvm_getswapinfo(opl->kd, swap, sizeof(swap)/sizeof(swap[0]), 0);
pl->totalSwap = 0; pl->totalSwap = 0;
pl->usedSwap = 0; pl->usedSwap = 0;
for (int i = 0; i < nswap; i++) { for (int i = 0; i < nswap; i++) {
@ -180,7 +180,7 @@ double getpcpu(const struct kinfo_proc *kp) {
} }
void ProcessList_goThroughEntries(ProcessList* this) { void ProcessList_goThroughEntries(ProcessList* this) {
OpenBSDProcessList* fpl = (OpenBSDProcessList*) this; OpenBSDProcessList* opl = (OpenBSDProcessList*) this;
Settings* settings = this->settings; Settings* settings = this->settings;
bool hideKernelThreads = settings->hideKernelThreads; bool hideKernelThreads = settings->hideKernelThreads;
bool hideUserlandThreads = settings->hideUserlandThreads; bool hideUserlandThreads = settings->hideUserlandThreads;
@ -194,7 +194,7 @@ void ProcessList_goThroughEntries(ProcessList* this) {
OpenBSDProcessList_scanMemoryInfo(this); OpenBSDProcessList_scanMemoryInfo(this);
// use KERN_PROC_KTHREAD to also include kernel threads // use KERN_PROC_KTHREAD to also include kernel threads
struct kinfo_proc* kprocs = kvm_getprocs(fpl->kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc), &count); struct kinfo_proc* kprocs = kvm_getprocs(opl->kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc), &count);
//struct kinfo_proc* kprocs = getprocs(KERN_PROC_ALL, 0, &count); //struct kinfo_proc* kprocs = getprocs(KERN_PROC_ALL, 0, &count);
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
@ -218,11 +218,11 @@ void ProcessList_goThroughEntries(ProcessList* this) {
proc->starttime_ctime = kproc->p_ustart_sec; proc->starttime_ctime = kproc->p_ustart_sec;
proc->user = UsersTable_getRef(this->usersTable, proc->st_uid); proc->user = UsersTable_getRef(this->usersTable, proc->st_uid);
ProcessList_add((ProcessList*)this, proc); ProcessList_add((ProcessList*)this, proc);
proc->comm = OpenBSDProcessList_readProcessName(fpl->kd, kproc, &proc->basenameOffset); proc->comm = OpenBSDProcessList_readProcessName(opl->kd, kproc, &proc->basenameOffset);
} else { } else {
if (settings->updateProcessNames) { if (settings->updateProcessNames) {
free(proc->comm); free(proc->comm);
proc->comm = OpenBSDProcessList_readProcessName(fpl->kd, kproc, &proc->basenameOffset); proc->comm = OpenBSDProcessList_readProcessName(opl->kd, kproc, &proc->basenameOffset);
} }
} }