Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Who modifies $ENV{PATH}?

by Anonymous Monk
on Dec 06, 2012 at 10:11 UTC ( [id://1007509]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monks,

I have a script which runs on several computers. On one computer today I find out that the script to execute a program, complaining that the program is not in PATH. File::Which::which("progname") returns empty and File::Spec->path gives ("/bin", "/usr/bin"), as well as $ENV{PATH} being set to "/bin:/usr/bin". This script is not run setuid.

How do I find out who modifies $ENV{PATH}? I remember a module in CPAN which monitors %ENV for modification, but couldn't find which module. I also tried to grep for "/usr/bin" in the source code; that didn't return any clues.

Replies are listed 'Best First'.
Re: Who modifies $ENV{PATH}?
by tobyink (Canon) on Dec 06, 2012 at 10:35 UTC

    You could try adding the following right at the top of your script. (Even add it before use strict - before everything else except perhaps the #! line.)

    BEGIN { package Tie::Env::Warn; use Carp qw(cluck); use Tie::Hash; our @ISA = 'Tie::StdHash'; sub STORE { my ($self, $key, $value) = @_; cluck "Modification of \$ENV{$key}!"; $self->SUPER::STORE($key, $value); } tie %ENV, __PACKAGE__; };

    That will print out a warning including a stack trace (which shows line numbers, etc) whenever %ENV values are assigned. If that doesn't help, then you could also try hooking the DELETE and CLEAR methods (the example above just hooks STORE).

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

      Thank you. Found the culprit: Date/Manip/TZ.pm version 6.34 line 96. Looks like it's fixed in 6.37 (the latest on CPAN).

      Relieved!

Re: Who modifies $ENV{PATH}?
by jethro (Monsignor) on Dec 06, 2012 at 10:40 UTC

    Are you sure your script isn't called with modified ENV already? cron-jobs usually start with a modified aka purified ENV

    If not you might add debug code that checks ENV and warns about it. If your script is not multithreaded this should enable you to pinpoint the culprit in a few runs with ever narrower "bisection" steps.

Re: Who modifies $ENV{PATH}?
by kcott (Archbishop) on Dec 06, 2012 at 11:50 UTC

    perlsec provides information about modifying %ENV (including $ENV{PATH}). As already mentioned by jethro, it's standard practice for cron.

    I wouldn't assume something bad or malicious has occurred. Consulting System Administrators is probably your best bet in the first instance.

    -- Ken

Re: Who modifies $ENV{PATH}?
by Anonymous Monk on Dec 06, 2012 at 13:52 UTC

    child process inherit %ENV from parent process, and they can modify it, so their children inherit a totally different %ENV

    perl -V ought to list any perl specific customers (like PERLLIB, PERL5OPT ...) that might modify your env

    ps ps axjf ought to list any parent procs

Re: Who modifies $ENV{PATH}?
by Anonymous Monk on Dec 06, 2012 at 10:12 UTC

    I find out that the script to execute a program

    Sorry. That should have read:

    I find out that the script fails to execute a program

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1007509]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2024-03-19 11:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found