diff options
author | Michael Klein <michael.klein@puffin.lb.shuttle.de> | 2015-12-03 22:16:10 +0100 |
---|---|---|
committer | Michael Klein <michael.klein@puffin.lb.shuttle.de> | 2015-12-03 22:23:40 +0100 |
commit | cc23d13f87e74d4dc2cc781da28ddc07f3860b63 (patch) | |
tree | efba2e92a9fc328def3025ce52c707d4d5260b15 /linux | |
parent | 0919ea32f91bb2ddf349c1108f6f0f67b511cca6 (diff) |
Add Platform_getProcessEnv
- currently implemented for darwin and linux
Diffstat (limited to 'linux')
-rw-r--r-- | linux/Platform.c | 28 | ||||
-rw-r--r-- | linux/Platform.h | 2 |
2 files changed, 30 insertions, 0 deletions
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 |