Won't get current environment variables values on Linux
Closed this issue · 3 comments
This is correct for getting the environment variables of the process when it was started, but not for getting the actual current state.
gdb is able to do this: start bash in one terminal, echo $$
to get its PID, then sudo gdb --pid=<PID>
to launch gdb. Then just do p environ[0]
and it will give you the first environment variable.
It may be possible to use libc's ptrace to coerce the environment variables from a running process, but there is a security restriction in that ptrace will fail on any other process unless you run it as root, or you started it with fork(). I don't know if Process.Start() uses fork() internally.
There is a fairly detailed discussion about it here:
https://unix.stackexchange.com/questions/29128/how-to-read-environment-variables-of-a-process
Don't feel compelled by the notion of shelling to gdb if it's available: sibling processes cannot access one another.
@HughPH, thanks a lot for spotting this behavior. Having a starting environment is at least something but it's a pity that it does not get updated on changes. And how to solve that... I have no idea as for now. Every proposed approach has its own set of pitfalls.
@hrumhurum Yes, I've tried tinkering a bit with ptrace and while I appear to be able to attach to a child process, attempting to read memory just results in -1 being returned. On top of which, it would significantly increase the complexity of this library and add complications to expressing its capabilities if you wanted to support "live" environment variables for child processes, but fall back to startup variables for all others.
I'm closing the issue because a better solution does not seem to exist yet. If anything changes then please feel free to reopen.