public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* Cygwin app not receiving modified PATH env var
@ 2008-06-17  7:21 Mike Mueller
  2008-06-18  4:56 ` Mike Mueller
  0 siblings, 1 reply; 8+ messages in thread
From: Mike Mueller @ 2008-06-17  7:21 UTC (permalink / raw)
  To: cygwin

Hey all,

I have a program (native win32) that is capable of launching
sub-processes, using apr (apache portable runtime), which is essentially
a wrapper around CreateProcess in the Windows API.  My program creates a
modified environment (changes the PATH env var), and passes this new
environment to CreateProcess.

When I run a native Windows program, for example, a .bat script that
says "echo %PATH%", I see the correctly modified PATH env var.

When I run a Cygwin program, the PATH is the original unmodified path
(rephrased in cygwin format), maybe inherited from the parent process?
The modified PATH being passed to CreateProcess is not discovered by the
Cygwin app.

I would've expected the two to behave the same in this situation.  Has
anyone done anything like this before?  Is there a known issue here, or
is this a new bug?  Workarounds?

Thanks,
Mike

--
Mike Mueller
mike@subfocal.net

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Cygwin app not receiving modified PATH env var
  2008-06-17  7:21 Cygwin app not receiving modified PATH env var Mike Mueller
@ 2008-06-18  4:56 ` Mike Mueller
  2008-06-18  8:51   ` Mike Mueller
  2008-06-18  9:27   ` Corinna Vinschen
  0 siblings, 2 replies; 8+ messages in thread
From: Mike Mueller @ 2008-06-18  4:56 UTC (permalink / raw)
  To: cygwin

On Mon, Jun 16, 2008 at 11:41:52PM -0400, Mike Mueller wrote:
> I have a program (native win32) that is capable of launching
> sub-processes, using apr (apache portable runtime), which is essentially
> a wrapper around CreateProcess in the Windows API.  My program creates a
> modified environment (changes the PATH env var), and passes this new
> environment to CreateProcess.
> 
> When I run a native Windows program, for example, a .bat script that
> says "echo %PATH%", I see the correctly modified PATH env var.
> 
> When I run a Cygwin program, the PATH is the original unmodified path
> (rephrased in cygwin format), maybe inherited from the parent process?
> The modified PATH being passed to CreateProcess is not discovered by the
> Cygwin app.
> 
> I would've expected the two to behave the same in this situation.  Has
> anyone done anything like this before?  Is there a known issue here, or
> is this a new bug?  Workarounds?

No thoughts on this?  How & when does Cygwin take a snapshot of the
Windows environment?  I'm still trying to figure out how my modified
environment is not being picked up...

Mike

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Cygwin app not receiving modified PATH env var
  2008-06-18  4:56 ` Mike Mueller
@ 2008-06-18  8:51   ` Mike Mueller
  2008-06-18  9:30     ` Corinna Vinschen
  2008-06-18  9:27   ` Corinna Vinschen
  1 sibling, 1 reply; 8+ messages in thread
From: Mike Mueller @ 2008-06-18  8:51 UTC (permalink / raw)
  To: cygwin

On Tue, Jun 17, 2008 at 09:05:36PM -0400, Mike Mueller wrote:
> On Mon, Jun 16, 2008 at 11:41:52PM -0400, Mike Mueller wrote:
> > I have a program (native win32) that is capable of launching
> > sub-processes, using apr (apache portable runtime), which is essentially
> > a wrapper around CreateProcess in the Windows API.  My program creates a
> > modified environment (changes the PATH env var), and passes this new
> > environment to CreateProcess.
> > 
> > When I run a native Windows program, for example, a .bat script that
> > says "echo %PATH%", I see the correctly modified PATH env var.
> > 
> > When I run a Cygwin program, the PATH is the original unmodified path
> > (rephrased in cygwin format), maybe inherited from the parent process?
> > The modified PATH being passed to CreateProcess is not discovered by the
> > Cygwin app.
> > 
> > I would've expected the two to behave the same in this situation.  Has
> > anyone done anything like this before?  Is there a known issue here, or
> > is this a new bug?  Workarounds?
> 
> No thoughts on this?  How & when does Cygwin take a snapshot of the
> Windows environment?  I'm still trying to figure out how my modified
> environment is not being picked up...

A little further info.  It looks like apr detects a unicode environment,
so ultimately is calling CreateProcessW (not CreateProcessA).

Also, if I pass environment variables other than PATH, they are set
properly when the child executes.  I'm even able to override other
variables that are set already in the environment, and the new value is
seen by the child.  PATH is the only variable I can't get to work.

I also tried the 'noenvcache' option, with no effect.

Mike

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Cygwin app not receiving modified PATH env var
  2008-06-18  4:56 ` Mike Mueller
  2008-06-18  8:51   ` Mike Mueller
@ 2008-06-18  9:27   ` Corinna Vinschen
  1 sibling, 0 replies; 8+ messages in thread
From: Corinna Vinschen @ 2008-06-18  9:27 UTC (permalink / raw)
  To: cygwin

On Jun 17 21:05, Mike Mueller wrote:
> On Mon, Jun 16, 2008 at 11:41:52PM -0400, Mike Mueller wrote:
> > I have a program (native win32) that is capable of launching
> > sub-processes, using apr (apache portable runtime), which is essentially
> > a wrapper around CreateProcess in the Windows API.  My program creates a
> > modified environment (changes the PATH env var), and passes this new
> > environment to CreateProcess.
> > 
> > When I run a native Windows program, for example, a .bat script that
> > says "echo %PATH%", I see the correctly modified PATH env var.
> > 
> > When I run a Cygwin program, the PATH is the original unmodified path
> > (rephrased in cygwin format), maybe inherited from the parent process?
> > The modified PATH being passed to CreateProcess is not discovered by the
> > Cygwin app.
> 
> No thoughts on this?  How & when does Cygwin take a snapshot of the
> Windows environment?  I'm still trying to figure out how my modified
> environment is not being picked up...

Cygwin processes get their environment from the parent process, just
like any other process.  In case of having a Cygwin parent, it uses
another mechanism for environment inheritance, but if the parent is
a native process, it gets it env from a call to GetEnvironmentStrings.
There's no other way, really.


Corinna

-- 
Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Project Co-Leader          cygwin AT cygwin DOT com
Red Hat

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Cygwin app not receiving modified PATH env var
  2008-06-18  8:51   ` Mike Mueller
@ 2008-06-18  9:30     ` Corinna Vinschen
  2008-06-18 10:53       ` Mike Mueller
  0 siblings, 1 reply; 8+ messages in thread
From: Corinna Vinschen @ 2008-06-18  9:30 UTC (permalink / raw)
  To: cygwin

On Jun 18 04:00, Mike Mueller wrote:
> A little further info.  It looks like apr detects a unicode environment,
> so ultimately is calling CreateProcessW (not CreateProcessA).

How do you pass your unicode env?  Do you set the CREATE_UNICODE_ENVIRONMENT
flag?

> Also, if I pass environment variables other than PATH, they are set
> properly when the child executes.  I'm even able to override other
> variables that are set already in the environment, and the new value is
> seen by the child.  PATH is the only variable I can't get to work.

Is your environment big (> 32K)?  Up to 1.5.25 the environment is
fetched using GetEnvironmentStringsA, which has a bug in (at least)
Windows XP which results in a truncated environment in the child.  And
it's not truncated to 32K, but much less from my testing.  Something
along the lines of sizeof (child_env) == sizeof (parent_env) % 32K.

Other than that, I have no idea.  PATH is taken from the parent like
any other env var.  Are you *sure* there's not some bug in your app?


Corinna

-- 
Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Project Co-Leader          cygwin AT cygwin DOT com
Red Hat

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Cygwin app not receiving modified PATH env var
  2008-06-18  9:30     ` Corinna Vinschen
@ 2008-06-18 10:53       ` Mike Mueller
  2008-06-18 12:04         ` Corinna Vinschen
  0 siblings, 1 reply; 8+ messages in thread
From: Mike Mueller @ 2008-06-18 10:53 UTC (permalink / raw)
  To: cygwin

Thanks for the replies, Corinna.

> > A little further info.  It looks like apr detects a unicode environment,
> > so ultimately is calling CreateProcessW (not CreateProcessA).
> 
> How do you pass your unicode env?  Do you set the CREATE_UNICODE_ENVIRONMENT
> flag?

I get a snapshot of the current environment (GetEnvironmentStrings).
Then I create a new environment based on the current one, but adding
two additional variables and replacing PATH with a modified version of
the original PATH.

Then I hand it over to apr_proc_create, which ultimately calls
CreateProcessW.  If you're feeling bold, the implementation is here:
http://svn.apache.org/repos/asf/apr/apr/trunk/threadproc/win32/proc.c

I stepped through my program in the debugger, and yes, the
CREATE_UNICODE_ENVIRONMENT flag is set when apr_proc_create calls
CreateProcessW.  Also, I stepped through the apr code that constructs
the environment map that is passed to CreateProcessW, and my PATH
setting looks correct at that time.

> > Also, if I pass environment variables other than PATH, they are set
> > properly when the child executes.  I'm even able to override other
> > variables that are set already in the environment, and the new value is
> > seen by the child.  PATH is the only variable I can't get to work.
> 
> Is your environment big (> 32K)?  Up to 1.5.25 the environment is
> fetched using GetEnvironmentStringsA, which has a bug in (at least)
> Windows XP which results in a truncated environment in the child.  And
> it's not truncated to 32K, but much less from my testing.  Something
> along the lines of sizeof (child_env) == sizeof (parent_env) % 32K.

Nope, I just tallied up the size: 1486 bytes total.

> Other than that, I have no idea.  PATH is taken from the parent like
> any other env var.  Are you *sure* there's not some bug in your app?

Well, you never know, but I definitely try to see if it's my fault
before wasting anyone else's time.  To me, the key seems to be that
running a native windows app as the child works fine -- it receives the
PATH without any problem.  It's only running a cygwin child process where
the PATH is unchanged (even though 2 additional env vars other than PATH
get set properly).  This is the reason I'm asking on the cygwin list, and
not the apr list.

Obviously PATH is special to cygwin, because it has to transform the
Windows representation into a Unix-like representation.  I agree with
you that it *should* be pulled in like any other variable, but I'm not
familiar with cygwin's under-the-hood workings.  This problem has me
completely at a loss...

Thanks again,
Mike

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Cygwin app not receiving modified PATH env var
  2008-06-18 10:53       ` Mike Mueller
@ 2008-06-18 12:04         ` Corinna Vinschen
  2008-06-18 14:52           ` Mike Mueller
  0 siblings, 1 reply; 8+ messages in thread
From: Corinna Vinschen @ 2008-06-18 12:04 UTC (permalink / raw)
  To: cygwin

On Jun 18 05:30, Mike Mueller wrote:
> Obviously PATH is special to cygwin, because it has to transform the
> Windows representation into a Unix-like representation.  I agree with
> you that it *should* be pulled in like any other variable, but I'm not
> familiar with cygwin's under-the-hood workings.  This problem has me
> completely at a loss...

As I said, PATH is fetched with GetEnvironmentStrings from the env like
every other environment var, if the Cygwin app has been started by a
native parent.  It's just massaged into a POSIX path afterwards.
There's no magic involved like, say, some hidden mechanism to get the
environment from the grandparent or something.

Try this:

bash-3.2$ echo $PATH
/home/corinna/bin:/usr/bin:/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin:/cygdrive/c/WINDOWS/system32:/cygdrive/c/WINDOWS:/cygdrive/c/WINDOWS/System32/Wbem:/cygdrive/c/SFU/common/
$ cmd
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

c:\cygwin\home\corinna>echo %path%
C:\cygwin-1.5\mnt\c\cygwin\home\corinna\bin;C:\cygwin-1.5\bin;C:\cygwin-1.5\usr\local\bin;C:\cygwin-1.5\bin;C:\cygwin-1.5\bin;C:\cygwin-1.5\usr\X11R6\bin;c:\windows\system32;c:\windows;c:\windows\System32\Wbem;C:\cygwin-1.5\mnt\c\SFU\common\

c:\cygwin\home\corinna>set path=%path%;C:\

c:\cygwin\home\corinna>echo %path%
c:\cygwin\home\corinna\bin;C:\cygwin-1.5\bin;C:\cygwin-1.5\usr\local\bin;C:\cygw
in-1.5\bin;C:\cygwin-1.5\bin;C:\cygwin-1.5\usr\X11R6\bin;c:\WINDOWS\system32;c:\
WINDOWS;c:\WINDOWS\System32\Wbem;c:\SFU\common\;C:\

c:\cygwin\home\corinna>bash
bash-3.2$ echo $PATH
/cygdrive/c/cygwin/home/corinna/bin:/usr/bin:/usr/local/bin:/usr/bin:/usr/bin:/usr/X11R6/bin:/cygdrive/c/WINDOWS/system32:/cygdrive/c/WINDOWS:/cygdrive/c/WINDOWS/System32/Wbem:/cygdrive/c/SFU/common/:/cygdrive/c

Well, works for me, obviously.


Corinna

-- 
Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Project Co-Leader          cygwin AT cygwin DOT com
Red Hat

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Cygwin app not receiving modified PATH env var
  2008-06-18 12:04         ` Corinna Vinschen
@ 2008-06-18 14:52           ` Mike Mueller
  0 siblings, 0 replies; 8+ messages in thread
From: Mike Mueller @ 2008-06-18 14:52 UTC (permalink / raw)
  To: cygwin

On Wed, Jun 18, 2008 at 12:30:22PM +0200, Corinna Vinschen wrote:
> As I said, PATH is fetched with GetEnvironmentStrings from the env like
> every other environment var, if the Cygwin app has been started by a
> native parent.  It's just massaged into a POSIX path afterwards.
> There's no magic involved like, say, some hidden mechanism to get the
> environment from the grandparent or something.
> 
> Try this:
> [snip]

Yeah, the command line tricks work for me too.  If I modify the path in
cmd.exe, then invoke bash, its PATH looks correct.

This is slightly different than what my code is doing though.  I've been
trying to avoid modifying my application's (the parent that is) PATH, so
I create a new environment and pass it to CreateProcessW.  Your cmd.exe
scenario has the parent modify its own environment first.

In fact, I just modified my application to call SetEnvironmentVariableW
with the modified PATH, prior to invoking a child.  The other two vars
that I set are still set by grabbing a copy of the environment (after I
modified PATH) and inserting them into the map.  Result: It works.  The
cygwin child app sees all the variables I want it to see.

The downside of this is, well, I don't want to change my parent
process's PATH.  So I can save it, modify it, and revert it, but that's
not exactly thread-safe, since the environment is global to the
application.  It would be ideal if the CreateProcessW method worked.

Thanks for being patient with me; at least I have a workaround, for now.

Thanks,
Mike

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2008-06-18 12:04 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-06-17  7:21 Cygwin app not receiving modified PATH env var Mike Mueller
2008-06-18  4:56 ` Mike Mueller
2008-06-18  8:51   ` Mike Mueller
2008-06-18  9:30     ` Corinna Vinschen
2008-06-18 10:53       ` Mike Mueller
2008-06-18 12:04         ` Corinna Vinschen
2008-06-18 14:52           ` Mike Mueller
2008-06-18  9:27   ` Corinna Vinschen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).