Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: LWP::UserAgent connection problem as CGI

by tachyon (Chancellor)
on Mar 03, 2004 at 18:47 UTC ( #333650=note: print w/ replies, xml ) Need Help??


in reply to LWP::UserAgent connection problem as CGI

The error message you see is generated initially by IO::Socket::INET's configure method when it has called its _get_addr() method which has in turn called gethostbyname() which is what will have choked in the first place. (Probably)

To debug write a CGI like this:

#!/usr/bin/perl print "Content-Type: text/html\n\n"; my $host = gethostbyname($ENV{QUERY_STRING}); print $host ? "Got host $host\n" : "Choked!"; call it like: http://yourdomain.com/cgi-bin/gethost?perlmonks.org

Assuming you call it gethost you should see it print 4 binary bytes out as the host name. If it says choked you have an issue with gethostbyname under CGI. If so it will be perms as your CGI will not be running as you.

An alternative way to prove it is perms is

su nobody ./script.pl

ie change to the user nobody/apache or whatever webserver runs as and see if it chokes. I am making the assumption that you are testing it off the command line *on the same box* that is running the CGI? If not all bets are off!

If gethostbyname is working your IO::Socket::INET has issues. The most probable reason for this is that you have 2 perls on your system. The webserver will probably do

./script.pl

To exec the script and thus use the shebang. Probably. It could be running mod_perl or lots of other stuff but that is the high probability. If you are executing it as perl script.pl then you could quite easily be running it with a different perl to the web server. We have 5.6.1, 5.6.2, 5.8.2 and 5.8.3 on our devel box. perl will currently give you 5.6.2 but...... Let us know your results. div class="pmsig">

cheers

tachyon


Comment on Re: LWP::UserAgent connection problem as CGI
Select or Download Code
Re: Re: LWP::UserAgent connection problem as CGI
by bassplayer (Monsignor) on Mar 03, 2004 at 20:09 UTC
    Thanks for the lengthy response. I tried your script and it failed on the problem server, and succeeded on the working server. Given that, you are saying that if the issue is likely permissions. As stated in my original post, my script works at command line whether as root, or as the user that apache runs as. For the record, I am indeed testing on the same machine as the CGI is running. Same script, actually, with a use lib statement added to lead to our modules (a line usually provided by mod_perl).

    I also tried running your test script with the shebang line pointing to any perl install I could find, as well as without a shebang line, using all of the different perl installs before the script name. Is there another way that permissions could be affecting this that I am not seeing, or can you suggest further tests to help me narrow this down? I also added the following line to your script:

    print "\nVERSION:  ".`perl -v`."\n";

    The version was always displayed as 5.8.0, regardless of what I tried to make it use (via shebang or command line). The perl installs appear to be different (not just symbolic links to one). The @INC array did change with each different shebang line:
    #!/usr/bin/perl #!/usr/local/bin/perl #!/usr/local/bin/perl5.8.0
    produce:
    /usr/local/lib/perl5/5.8.0/i386-freebsd /usr/local/lib/perl5/5.8.0 /usr/local/lib/perl5/site_perl/5.8.0/i386-freebsd /usr/local/lib/perl5/site_perl/5.8.0 /usr/local/lib/perl5/site_perl

    #!/usr/bin/perl5 #!/usr/bin/perl5.00503
    produce:
    /usr/local/lib/perl5/site_perl/5.005/i386-freebsd /usr/local/lib/perl5/site_perl/5.005 /usr/libdata/perl/5.00503/mach /usr/libdata/perl/5.00503

    Update: Fixed shebang typos (second group).

    Not sure if any of that helps at all...

    bassplayer

      When you say failed I presume you mean it printed choked. Here is the same script as a command line utility. Run it on the server that choked. As root, and as nobody/apache.

      #!/usr/bin/perl my $host = gethostbyname($ARGV[0]); print $host ? "Got host $host\n" : "Choked!";

      It fails because gethostbyname() is failing. Some systems do have a crap gethostbyname(). In fact freebsd (which I see is what you are using) is one such OS. Do a google for 'gethostbyname freebsd perl issue' or some such. From memory the gethostbyname function is not thread safe up until the most recent of the freebsd releases.

      So.....

      • Freebsd has known issues with gethostbyname. I use Linux so don't know the details but there may be a patch for the core function.
      • You have now effectively isolated the bug to gethostbyname() failing under some circumstances and have a test case.
      • You are using 5.8.0 which was by all reports less than optimal. I would strongly consider recomiling perl using 5.8.3. I seem to remember there is the option to not use the systems native gethostbyname() - something like 'system has broken gethostbyname()' so let perl know.
      • Getting 5.8.3, compiling, installing it into the default location (/usr/local/lib/perl5/5.8.3) will not overwrite anything and will probably be the quickest fix if it works. It will take about 15 minutes or so to do.

      You may need to install libwwwperl (if you do not include the old 5.8.0 lib and site/lib into @INC during the configure. Anyway if you do it is just /usr/local/bin/perl5.8.3 Makefile.PL && make && make test && make install. This forces linking to 5.8.3 and installation into the 5.8.3 tree. Once it is in all you need to do to the test case is put #!/usr/local/bin/perl5.8.3 to run the test case against the new perl. If it works you have a cure. I would then just remove/rename the /usr/bin/perl (probably a symlink) and link it to the new 5.8.3 binary. You can always undo the link if random stuff happens (like none of the 5.8.0 modules can get found as the lib paths were not compiled into your new perl)

      cheers

      tachyon

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://333650]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (11)
As of 2014-09-17 13:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (80 votes), past polls