summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHisham Muhammad <hisham@gobolinux.org>2016-01-12 02:57:58 -0200
committerHisham Muhammad <hisham@gobolinux.org>2016-01-12 02:57:58 -0200
commitfaf28606696ad023c786e8b63372bfa498b0974a (patch)
tree3288a07433d988062afc4f0be4335e07760c33fa
parentd850803eb8401cb63e452e4a247be3b4a6d92313 (diff)
parent032af1577c4bd58cc7c76f3455afe3e1b22a4b9a (diff)
Merge branch 'mklein-de-envscreen'
-rw-r--r--Action.c17
-rw-r--r--Action.h1
-rw-r--r--EnvScreen.c157
-rw-r--r--EnvScreen.h22
-rw-r--r--Makefile.am5
-rw-r--r--darwin/Platform.c53
-rw-r--r--darwin/Platform.h2
-rw-r--r--freebsd/Platform.c5
-rw-r--r--linux/Platform.c28
-rw-r--r--linux/Platform.h2
-rw-r--r--openbsd/Platform.c5
-rw-r--r--unsupported/Platform.c3
12 files changed, 297 insertions, 3 deletions
diff --git a/Action.c b/Action.c
index 97fe55ac..e60a7f23 100644
--- a/Action.c
+++ b/Action.c
@@ -12,6 +12,7 @@ in the source distribution for its full text.
#include "AffinityPanel.h"
#include "CategoriesPanel.h"
#include "CRT.h"
+#include "EnvScreen.h"
#include "MainPanel.h"
#include "OpenFilesScreen.h"
#include "Process.h"
@@ -404,7 +405,8 @@ static struct { const char* key; const char* info; } helpRight[] = {
#if (HAVE_LIBHWLOC || HAVE_NATIVE_AFFINITY)
{ .key = " a: ", .info = "set CPU affinity" },
#endif
- { .key = " i: ", .info = "set IO priority" },
+ { .key = " e: ", .info = "show process environment" },
+ { .key = " i: ", .info = "set IO prority" },
{ .key = " l: ", .info = "list open files with lsof" },
{ .key = " s: ", .info = "trace syscalls with strace" },
{ .key = " ", .info = "" },
@@ -499,6 +501,18 @@ static Htop_Reaction actionTagAllChildren(State* st) {
return HTOP_OK;
}
+static Htop_Reaction actionShowEnvScreen(State* st) {
+ Process* p = (Process*) Panel_getSelected(st->panel);
+ if (!p) return HTOP_OK;
+ EnvScreen* ts = EnvScreen_new(p);
+ EnvScreen_run(ts);
+ EnvScreen_delete(ts);
+ clear();
+ CRT_enableDelay();
+ return HTOP_REFRESH | HTOP_REDRAW_BAR;
+}
+
+
void Action_setBindings(Htop_Action* keys) {
keys[KEY_RESIZE] = actionResize;
keys['M'] = actionSortByMemory;
@@ -548,5 +562,6 @@ void Action_setBindings(Htop_Action* keys) {
keys['?'] = actionHelp;
keys['U'] = actionUntagAll;
keys['c'] = actionTagAllChildren;
+ keys['e'] = actionShowEnvScreen;
}
diff --git a/Action.h b/Action.h
index 8927ce2f..34fc298f 100644
--- a/Action.h
+++ b/Action.h
@@ -49,6 +49,7 @@ Htop_Reaction Action_setSortKey(Settings* settings, ProcessField sortKey);
// ----------------------------------------
+
void Action_setBindings(Htop_Action* keys);
diff --git a/EnvScreen.c b/EnvScreen.c
new file mode 100644
index 00000000..1881b1ff
--- /dev/null
+++ b/EnvScreen.c
@@ -0,0 +1,157 @@
+#include "EnvScreen.h"
+
+#include "config.h"
+#include "CRT.h"
+#include "IncSet.h"
+#include "ListItem.h"
+#include "Platform.h"
+#include "StringUtils.h"
+
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+/*{
+#include "ProcessList.h"
+#include "Panel.h"
+#include "FunctionBar.h"
+
+typedef struct EnvScreen_ {
+ Process* process;
+ Panel* display;
+ FunctionBar* bar;
+} EnvScreen;
+}*/
+
+static const char* EnvScreenFunctions[] = {"Search ", "Filter ", "Refresh", "Done ", NULL};
+
+static const char* EnvScreenKeys[] = {"F3", "F4", "F5", "Esc"};
+
+static int EnvScreenEvents[] = {KEY_F(3), KEY_F(4), KEY_F(5), 27};
+
+EnvScreen* EnvScreen_new(Process* process) {
+ EnvScreen* this = malloc(sizeof(EnvScreen));
+ this->process = process;
+ FunctionBar* bar = FunctionBar_new(EnvScreenFunctions, EnvScreenKeys, EnvScreenEvents);
+ this->display = Panel_new(0, 1, COLS, LINES-3, false, Class(ListItem), bar);
+ return this;
+}
+
+void EnvScreen_delete(EnvScreen* this) {
+ Panel_delete((Object*)this->display);
+ free(this);
+}
+
+static void EnvScreen_draw(EnvScreen* this, IncSet* inc) {
+ attrset(CRT_colors[METER_TEXT]);
+ mvhline(0, 0, ' ', COLS);
+ mvprintw(0, 0, "environment of process %d - %s", this->process->pid, this->process->comm);
+ attrset(CRT_colors[DEFAULT_COLOR]);
+ Panel_draw(this->display, true);
+ IncSet_drawBar(inc);
+}
+
+static inline void addLine(const char* line, Vector* lines, Panel* panel, const char* incFilter) {
+ Vector_add(lines, (Object*) ListItem_new(line, 0));
+ if (!incFilter || String_contains_i(line, incFilter))
+ Panel_add(panel, (Object*)Vector_get(lines, Vector_size(lines)-1));
+}
+
+static void EnvScreen_scan(EnvScreen* this, Vector* lines, IncSet* inc) {
+ Panel* panel = this->display;
+ int idx = MAX(Panel_getSelectedIndex(panel), 0);
+
+ Panel_prune(panel);
+
+ uid_t euid = geteuid();
+ seteuid(getuid());
+ char *env = Platform_getProcessEnv(this->process->pid);
+ seteuid(euid);
+ if (env) {
+ for (char *p = env; *p; p = strrchr(p, 0)+1)
+ addLine(p, lines, panel, IncSet_filter(inc));
+ free(env);
+ }
+ else {
+ addLine("Could not read process environment.", lines, panel, IncSet_filter(inc));
+ }
+
+ Vector_insertionSort(lines);
+ Vector_insertionSort(panel->items);
+ Panel_setSelected(panel, idx);
+}
+
+void EnvScreen_run(EnvScreen* this) {
+ Panel* panel = this->display;
+ Panel_setHeader(panel, " ");
+
+ FunctionBar* bar = panel->defaultBar;
+ IncSet* inc = IncSet_new(bar);
+
+ Vector* lines = Vector_new(panel->items->type, true, DEFAULT_SIZE);
+
+ EnvScreen_scan(this, lines, inc);
+ EnvScreen_draw(this, inc);
+
+ bool looping = true;
+ while (looping) {
+
+ Panel_draw(panel, true);
+
+ if (inc->active)
+ move(LINES-1, CRT_cursorX);
+ int ch = getch();
+
+ if (ch == KEY_MOUSE) {
+ MEVENT mevent;
+ int ok = getmouse(&mevent);
+ if (ok == OK)
+ if (mevent.y >= panel->y && mevent.y < LINES - 1) {
+ Panel_setSelected(panel, mevent.y - panel->y + panel->scrollV);
+ ch = 0;
+ } if (mevent.y == LINES - 1)
+ ch = IncSet_synthesizeEvent(inc, mevent.x);
+ }
+
+ if (inc->active) {
+ IncSet_handleKey(inc, ch, panel, IncSet_getListItemValue, lines);
+ continue;
+ }
+
+ switch(ch) {
+ case ERR:
+ continue;
+ case KEY_F(3):
+ case '/':
+ IncSet_activate(inc, INC_SEARCH, panel);
+ break;
+ case KEY_F(4):
+ case '\\':
+ IncSet_activate(inc, INC_FILTER, panel);
+ break;
+ case KEY_F(5):
+ clear();
+ EnvScreen_scan(this, lines, inc);
+ EnvScreen_draw(this, inc);
+ break;
+ case '\014': // Ctrl+L
+ clear();
+ EnvScreen_draw(this, inc);
+ break;
+ case 'q':
+ case 27:
+ case KEY_F(10):
+ looping = false;
+ break;
+ case KEY_RESIZE:
+ Panel_resize(panel, COLS, LINES-2);
+ EnvScreen_draw(this, inc);
+ break;
+ default:
+ Panel_onKey(panel, ch);
+ }
+ }
+
+ Vector_delete(lines);
+ IncSet_delete(inc);
+}
diff --git a/EnvScreen.h b/EnvScreen.h
new file mode 100644
index 00000000..f33f0591
--- /dev/null
+++ b/EnvScreen.h
@@ -0,0 +1,22 @@
+/* Do not edit this file. It was automatically generated. */
+
+#ifndef HEADER_EnvScreen
+#define HEADER_EnvScreen
+
+#include "ProcessList.h"
+#include "Panel.h"
+#include "FunctionBar.h"
+
+typedef struct EnvScreen_ {
+ Process* process;
+ Panel* display;
+ FunctionBar* bar;
+} EnvScreen;
+
+EnvScreen* EnvScreen_new(Process* process);
+
+void EnvScreen_delete(EnvScreen* this);
+
+void EnvScreen_run(EnvScreen* this);
+
+#endif
diff --git a/Makefile.am b/Makefile.am
index b54b9a91..2861e10b 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -23,7 +23,7 @@ LoadAverageMeter.c MemoryMeter.c Meter.c MetersPanel.c Object.c Panel.c \
BatteryMeter.c Process.c ProcessList.c RichString.c ScreenManager.c Settings.c \
SignalsPanel.c StringUtils.c SwapMeter.c TasksMeter.c UptimeMeter.c \
TraceScreen.c UsersTable.c Vector.c AvailableColumnsPanel.c AffinityPanel.c \
-HostnameMeter.c OpenFilesScreen.c Affinity.c IncSet.c Action.c
+HostnameMeter.c OpenFilesScreen.c Affinity.c IncSet.c Action.c EnvScreen.c
myhtopheaders = AvailableColumnsPanel.h AvailableMetersPanel.h \
CategoriesPanel.h CheckItem.h ClockMeter.h ColorsPanel.h ColumnsPanel.h \
@@ -32,7 +32,8 @@ Hashtable.h Header.h htop.h ListItem.h LoadAverageMeter.h MemoryMeter.h \
BatteryMeter.h Meter.h MetersPanel.h Object.h Panel.h ProcessList.h RichString.h \
ScreenManager.h Settings.h SignalsPanel.h StringUtils.h SwapMeter.h \
TasksMeter.h UptimeMeter.h TraceScreen.h UsersTable.h Vector.h Process.h \
-AffinityPanel.h HostnameMeter.h OpenFilesScreen.h Affinity.h IncSet.h Action.h
+AffinityPanel.h HostnameMeter.h OpenFilesScreen.h Affinity.h IncSet.h Action.h \
+EnvScreen.h
if HTOP_LINUX
htop_CFLAGS += -rdynamic
diff --git a/darwin/Platform.c b/darwin/Platform.c
index 73b47c4c..c025509f 100644
--- a/darwin/Platform.c
+++ b/darwin/Platform.c
@@ -236,3 +236,56 @@ void Platform_setSwapValues(Meter* mtr) {
mtr->total = swapused.xsu_total / 1024;
mtr->values[0] = swapused.xsu_used / 1024;
}
+
+char* Platform_getProcessEnv(pid_t pid) {
+ char* env = NULL;
+
+ int argmax;
+ size_t bufsz = sizeof(argmax);
+
+ int mib[3];
+ mib[0] = CTL_KERN;
+ mib[1] = KERN_ARGMAX;
+ if (sysctl(mib, 2, &argmax, &bufsz, 0, 0) == 0) {
+ char* buf = malloc(argmax);
+ if (buf) {
+ mib[0] = CTL_KERN;
+ mib[1] = KERN_PROCARGS2;
+ mib[2] = pid;
+ size_t bufsz = argmax;
+ if (sysctl(mib, 3, buf, &bufsz, 0, 0) == 0) {
+ if (bufsz > sizeof(int)) {
+ char *p = buf, *endp = buf + bufsz;
+ int argc = *(int*)p;
+ p += sizeof(int);
+
+ // skip exe
+ p = strchr(p, 0)+1;
+
+ // skip padding
+ while(!*p && p < endp)
+ ++p;
+
+ // skip argv
+ for (; argc-- && p < endp; p = strrchr(p, 0)+1)
+ ;
+
+ // skip padding
+ while(!*p && p < endp)
+ ++p;
+
+ size_t size = endp - p;
+ env = malloc(size+2);
+ if (env) {
+ memcpy(env, p, size);
+ env[size] = 0;
+ env[size+1] = 0;
+ }
+ }
+ }
+ free(buf);
+ }
+ }
+
+ return env;
+}
diff --git a/darwin/Platform.h b/darwin/Platform.h
index d1b9ea5e..0a873bf5 100644
--- a/darwin/Platform.h
+++ b/darwin/Platform.h
@@ -44,4 +44,6 @@ void Platform_setMemoryValues(Meter* mtr);
void Platform_setSwapValues(Meter* mtr);
+char* Platform_getProcessEnv(pid_t pid);
+
#endif
diff --git a/freebsd/Platform.c b/freebsd/Platform.c
index a51b12bb..a5e0e0b3 100644
--- a/freebsd/Platform.c
+++ b/freebsd/Platform.c
@@ -195,3 +195,8 @@ void Platform_setSwapValues(Meter* this) {
void Platform_setTasksValues(Meter* this) {
// TODO
}
+
+char* Platform_getProcessEnv(pid_t pid) {
+ // TODO
+ return NULL;
+}
diff --git a/linux/Platform.c b/linux/Platform.c
index 3a919bb1..81c96e4c 100644
--- a/linux/Platform.c
+++ b/linux/Platform.c
@@ -25,6 +25,9 @@ in the source distribution for its full text.
#include <math.h>
#include <assert.h>
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
/*{
#include "Action.h"
@@ -205,3 +208,28 @@ void Platform_setSwapValues(Meter* this) {
this->total = pl->totalSwap;
this->values[0] = pl->usedSwap;
}
+
+char* Platform_getProcessEnv(pid_t pid) {
+ char procname[32+1];
+ snprintf(procname, 32, "/proc/%d/environ", pid);
+ FILE* fd = fopen(procname, "r");
+ char *env = NULL;
+ if (fd) {
+ size_t capacity = 4096, size = 0, bytes;
+ env = malloc(capacity);
+ while (env && (bytes = fread(env+size, 1, capacity-size, fd)) > 0) {
+ size += bytes;
+ capacity *= 2;
+ env = realloc(env, capacity);
+ }
+ fclose(fd);
+ if (size < 2 || env[size-1] || env[size-2]) {
+ if (size + 2 < capacity) {
+ env = realloc(env, capacity+2);
+ }
+ env[size] = 0;
+ env[size+1] = 0;
+ }
+ }
+ return env;
+}
diff --git a/linux/Platform.h b/linux/Platform.h
index 2c79b299..114b9a20 100644
--- a/linux/Platform.h
+++ b/linux/Platform.h
@@ -39,4 +39,6 @@ void Platform_setMemoryValues(Meter* this);
void Platform_setSwapValues(Meter* this);
+char* Platform_getProcessEnv(pid_t pid);
+
#endif
diff --git a/openbsd/Platform.c b/openbsd/Platform.c
index 7114d941..90fbd2bc 100644
--- a/openbsd/Platform.c
+++ b/openbsd/Platform.c
@@ -295,3 +295,8 @@ void Platform_setSwapValues(Meter* this) {
void Platform_setTasksValues(Meter* this) {
// TODO
}
+
+char* Platform_getProcessEnv(pid_t pid) {
+ // TODO
+ return NULL;
+}
diff --git a/unsupported/Platform.c b/unsupported/Platform.c
index 5dd2e8f8..0f1ba97d 100644
--- a/unsupported/Platform.c
+++ b/unsupported/Platform.c
@@ -131,3 +131,6 @@ bool Process_isThread(Process* this) {
return false;
}
+char* Platform_getProcessEnv(pid_t pid) {
+ return NULL;
+}

© 2014-2024 Faster IT GmbH | imprint | privacy policy