[mp2] [bug?] %ENV changes not propagated to qx() and to C-library calls.

[mp2] [bug?] %ENV changes not propagated to qx() and to C-library calls.

am 30.01.2008 15:58:09 von bwooster47

Looks like mod_perl scripts execute the line that changes $ENV{PATH},
but when a qx() back-tick command is executed, that command is executed
under the value of the original path.

The docs seem to suggest that that changes to %ENV should work fine:
http://perl.apache.org/docs/2.0/user/coding/coding.html
>mod_perl passes (exports) the following shell environment variables
(if they are set) :
> * PATH - Executables search path.
> * TZ - Time Zone.
>Any of these environment variables can be accessed via %ENV

Maybe the docs need to be changed to say that while mod_perl passes these
values to the script, it does not pass them on to any qx() executions.
Would be best if mod_perl code can be fixed to allow this, though.

Related threads in mod_perl mailing list:
How to change $ENV{PATH} for a specific script?
http://mail-archives.apache.org/mod_mbox/perl-modperl/200801 .mbox/%3c204abd770801161416n6a6511cam88d5035c0744a9ec@mail.g mail.com%3e

mod_perl, ENV{'TZ'}, and localtime
http://mail-archives.apache.org/mod_mbox/perl-modperl/200712 .mbox/%3c4775D752.4030703@gkg.net%3e

---
Here's a simple test on Fedora FC7 system (more version info at end):
#!/usr/bin/perl -Tw
print "Content-type: text/plain\n\n";

my $command = "basename";
foreach ( "/bin:", "/invalid/dir:" ) {
$ENV{PATH} = $_;
print "------ Testing command '$command', path $ENV{PATH}\n";
my $string = `$command testing` || '';
print " failed to execute '$command', \$? is $?/" . ($? >> 8) . " : $!\n"
if ($? != 0);
print " output \$string is: $string\n";
}

exit(0);
------------------------------------------------
When run from the command line, I get the correct output:
Content-type: text/plain

------ Testing command 'basename', path /bin:
output $string is: testing

------ Testing command 'basename', path /invalid/dir:
[Wed Jan 16 17:13:39 2008] script.pl: Can't exec "basename": No such
file or directory at /home/cgi/script.pl line 12.
failed to execute 'basename', $? is -1/16777215 : No such file or directory
output $string is:
-------------------------------------------------------
But when I run this under Apache and mod_perl, I get this output -
/bin is still in the PATH even after removal:
------ Testing command 'basename', path /bin:
output $string is: testing

------ Testing command 'basename', path /invalid/dir:
output $string is: testing

-------------------------------------------------------
/perl-status output snippet:
Embedded Perl version v5.8.8 for Apache/2.2.6
Under the "perl-script" handler, the environment is:
MOD_PERL = mod_perl/2.0.3
MOD_PERL_API_VERSION = 2
PATH = /sbin:/usr/sbin:/bin:/usr/bin

Re: [mp2] [bug?] %ENV changes not propagated to qx() and to C-librarycalls.

am 31.01.2008 16:31:23 von Michael Schout

> The docs seem to suggest that that changes to %ENV should work fine:
> http://perl.apache.org/docs/2.0/user/coding/coding.html
>> mod_perl passes (exports) the following shell environment variables
> (if they are set) :
>> * PATH - Executables search path.
>> * TZ - Time Zone.
>> Any of these environment variables can be accessed via %ENV

You can *access* them, but if you change them, they will not be
synchronized with the C environment. You have to use Env::C to work
around that. MP2 unties the perl %ENV hash from the C environment
during the request, so any changes you make to %ENV during the request
will never be seen by anything written in C unless you manually copy it
over with something like Env::C.

IMO, it would be a lot easier if we could make the "untie %ENV"
behavior optional with a PerlSwitches configuration directive or
something. If you are running a non-threaded perl on a prefork MPM, I
really do not see the harm in leaving %ENV tied to the C environment.
Maybe I'll try to whip up a patch if I can figure it out :).

Regards,
Michael Schout