[mp2] [question] $ENV{PATH} changes not taking effect?

[mp2] [question] $ENV{PATH} changes not taking effect?

am 17.01.2008 13:03:20 von bwooster47

I have searched through all the web docs and Usenet, but not found
anything that suggests anything special has to be done in mod_perl
scripts that need to set $ENV{PATH}

My specific example is that I need to add /usr/local/bin to PATH, but
I''ve been unable to find a way to do this, 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. One lone posting on Usenet wondered
whether PerlSetEnv in Apache Config is the only way to fix this, but that
seems like a too big a hammer.

In case it helps, 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

Any help appreciated!
[Please ignore one duplicate email if it shows up - sent before subscribed]

Re: [mp2] [question] $ENV{PATH} changes not taking effect?

am 17.01.2008 15:00:27 von Jim Brandt

What happens if you try running it with the modperl handler instead of
the perl-script handler?


B Wooster wrote:
> I have searched through all the web docs and Usenet, but not found
> anything that suggests anything special has to be done in mod_perl
> scripts that need to set $ENV{PATH}
>
> My specific example is that I need to add /usr/local/bin to PATH, but
> I''ve been unable to find a way to do this, 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. One lone posting on Usenet wondered
> whether PerlSetEnv in Apache Config is the only way to fix this, but that
> seems like a too big a hammer.
>
> In case it helps, 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
>
> Any help appreciated!
> [Please ignore one duplicate email if it shows up - sent before subscribed]

--
Jim Brandt
Administrative Computing Services
University at Buffalo

Re: [mp2] [question] $ENV{PATH} changes not taking effect?

am 17.01.2008 19:05:10 von bwooster47

Under modperl, get zero bytes output, HTTP response code is 200, though.

Apache error_log shows:
[Thu Jan 17 12:47:15 2008] -e: Can't exec "basename": No such file or
directory at /var/www/perl/script.pl line 12.

So, under modperl, changes to $ENV{PATH} are not accepted (like
perl-script), but the default value for that is probably empty (or
does not contain /bin).

I also tried adding PerlSetEnv in this case, but still same output -
no output, and same error as above, for this Apache config:

SetHandler modperl
PerlSetEnv PATH /bin/:
PerlResponseHandler ModPerl::Registry
PerlOptions +ParseHeaders
Options +ExecCGI

perl-status shows:
Under the "modperl" handler, the environment is:
MOD_PERL = mod_perl/2.0.3
MOD_PERL_API_VERSION = 2
PATH = /sbin:/usr/sbin:/bin:/usr/bin

On Jan 17, 2008 9:00 AM, Jim Brandt wrote:
> What happens if you try running it with the modperl handler instead of
> the perl-script handler?

Re: [mp2] [question] $ENV{PATH} changes not taking effect?

am 17.01.2008 22:14:03 von Bart Schaefer

There's a thread from December that would seem to be related. From that thread:

---------- Forwarded message ----------
From: Michael Schout
Date: Dec 28, 2007 9:12 PM
Subject: Re: mod_perl, ENV{'TZ'}, and localtime
To: Kirk Noda
Cc: modperl@perl.apache.org


Kirk Noda wrote:
> The thread seemed to die off. Still, is there a way to use $ENV{TZ} to
> modify the behavior of localtime?

The reason this does not work under modperl version 2.0 is because under
handler "perl-script", %ENV is untied from the C environment. The
localtime() function is implemented in C, and as a result, it will never
see the changes you made to $ENV{TZ} from mod_perl.

The way I got around this was to use Env::C, and override
CORE::localtime() with something like:

package MyLocaltime;

use Env::C;

sub import {
my $class = shift;
$class->export('CORE::GLOBAL', 'localtime');
}

sub localtime {
my $time = shift;
$time = time unless defined $time;

my $orig_tz = Env::C::getenv('TZ');
Env::C::setenv('TZ', $ENV{TZ}, 1);

my @ret = CORE::localtime($time);

Env::C::setenv('TZ', $orig_tz, 1);

return @ret;
}

The real problem is that this is only safe under a prefork MPM because
it is not thread safe. There really ought to be an option (IMO) where
you can make the untie of %ENV under perl-script to be optional. Maybe
something like PerlOptions +NoUntieEnv or something so that if you are
running under a prefork MPM, you do not need to resort to tactics like
the above.

Regards,
Michael Schout

Re: [mp2] [question] $ENV{PATH} changes not taking effect?

am 18.01.2008 02:36:16 von wrowe

Bart Schaefer wrote:
> There's a thread from December that would seem to be related. From that thread:
>
> ---------- Forwarded message ----------
> From: Michael Schout
> Date: Dec 28, 2007 9:12 PM

> The real problem is that this is only safe under a prefork MPM because
> it is not thread safe. There really ought to be an option (IMO) where
> you can make the untie of %ENV under perl-script to be optional. Maybe
> something like PerlOptions +NoUntieEnv or something so that if you are
> running under a prefork MPM, you do not need to resort to tactics like
> the above.

If invoked as a section at startup, you are mislead. The fork
and threads creation hasn't happened at that phase.

Re: [mp2] [question] $ENV{PATH} changes not taking effect?

am 21.01.2008 17:37:53 von bwooster47

On Jan 17, 2008 4:14 PM, Bart Schaefer wrote:
> There's a thread from December that would seem to be related. From that thread:
>
> ---------- Forwarded message ----------
> From: Michael Schout
> Date: Dec 28, 2007 9:12 PM
> Subject: Re: mod_perl, ENV{'TZ'}, and localtime
> To: Kirk Noda
> Cc: modperl@perl.apache.org
>
>
> Kirk Noda wrote:
> The reason this does not work under modperl version 2.0 is because under
> handler "perl-script", %ENV is untied from the C environment. The
> localtime() function is implemented in C, and as a result, it will never
> see the changes you made to $ENV{TZ} from mod_perl.
>
> The way I got around this was to use Env::C, and override
> CORE::localtime() with something like:
>...
> Regards,
> Michael Schout

Hmm... this is a bit beyond what I wanted to do, not sure of all the
implications of the above. Does this mean I need to do a
Env::C::setenv('PATH', $ENV{PATH}, 1);
in my script?

In any case, given the results, looks like I should report a [bug],
not necessarily in mod_perl code, but in the docs, which seem to
suggest that the docs could be clarified that changes to %ENV won't
really do what may be expected?
>From: 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.

If anyone has other options to recommend, let me know, will try to
look into other (non-mod-perl) to get a speedier CGI execution of perl
scripts (for a small web site).