Retrieving per-process environment block?

Erik Bray
Tue Nov 29 17:42:00 GMT 2016

On Tue, Nov 29, 2016 at 3:28 PM, Eliot Moss <> wrote:
> On 11/29/2016 8:26 AM, Erik Bray wrote:
>> On Thu, Nov 17, 2016 at 3:00 PM, Corinna Vinschen
>> <> wrote:
>>> On Nov 17 14:30, Erik Bray wrote:
>>>> Hi all,
>>>> For a quick bit of background, I'm working on porting the highly
>>>> useful psutil [1] Python library to Cygwin.  This has proved an
>>>> interesting exercise, as much of the functionality of psutil works on
>>>> Cygwin through existing POSIX interfaces, and a handful of
>>>> Linux-specific interfaces as well.  But there are some bits that
>>>> simply don't map at all.
>>>> The one I'm struggling with right now is retrieving Cygwin environment
>>>> variables for a process (under inspection--i.e. not listing a
>>>> process's environment from within that process which is obviously
>>>> trivial).
>>>> I've looked at every route I could conceive of but as far as I can
>>>> tell this is currently impossible.  That's fine for now--I simply
>>>> disable that functionality in psutil.  But it is unfortunate, though,
>>>> since the information is there.
>>>> There are a couple avenues I could see to this.  The most "obvious"
>>>> (to me) being to implement /proc/<pid>/environ.
>>>> I would be willing to provide a patch for this if it would be
>>>> accepted.  Is there some particular non-obvious hurdle to this that it
>>>> hasn't been implemented?  Obviously there are security
>>>> implications--the /proc/<pid>/environ should only be readable to the
>>>> process's owner, but that is already within Cygwin's capabilities, and
>>>> works for other /proc files.
>>> Patch welcome.  Implementing this should be fairly straightforward.
>>> The only hurdle is winsup/CONTRIBUTORS ;)
>> Thanks--I went to go work on this finally but it turns out not to be
>> straightforward after all, as the process's environment is not shared
>> in any way between processes.
>> I could do this, if each process kept a copy of its environment block
>> in shared memory, which would in turn have to be updated every time
>> the process's environment is updated.  But I don't know what the
>> impact of that would be performance-wise.
> Sorry, no advice getting around this, but a thought about why it may have
> been hard -- it can be a security hole.  Given Windows' complex access
> rights,
> how would one determine which processes should reasonably have access to
> another process's environment variables?  Maybe Windows decided to punt the
> issue.  (I don't know anything about this at all - as I said, just a
> thought.)
> A question: Are there any Windows tools that can do this?  If so, you might
> be able to trace them to find what they do.


Thanks for the reply.  The issue here isn't getting the Windows
process environment--that can be done, albeit trickily [1].  While
it's true there are security implications, in this case that is
handled at the OpenProcess call required to obtain a handle to the
process.  It's true you can't just read the environment from a process
you don't have permission to.

The issue here though is reading the Cygwin process's environment
which is separate from its Windows environment, and the above
technique is inapplicable.

The easiest way, as I suggested, would be to keep a copy of each
process's Cygwin environment in shared memory as is the case with the
pinfo struct.  I just don't know if it's worth the effort for this
relatively narrow case (it's no so much effort to implement, I just
worry what performance impacts that could have, though I suppose I
could just try it and see...)


Problem reports:
Unsubscribe info:

More information about the Cygwin mailing list