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 /darwin | |
parent | 0919ea32f91bb2ddf349c1108f6f0f67b511cca6 (diff) |
Add Platform_getProcessEnv
- currently implemented for darwin and linux
Diffstat (limited to 'darwin')
-rw-r--r-- | darwin/Platform.c | 53 | ||||
-rw-r--r-- | darwin/Platform.h | 2 |
2 files changed, 55 insertions, 0 deletions
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 |