LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   apache includes the STDOUT of the programs called by my perl scripts! (http://www.linuxquestions.org/questions/programming-9/apache-includes-the-stdout-of-the-programs-called-by-my-perl-scripts-4175417920/)

MisterBark 07-21-2012 03:15 AM

apache includes the STDOUT of the programs called by my perl scripts!
 
Hi!

I just moved to a new server.
http 2.2.2
perl 5.16.0

When I call a binary from a perl cgi using system(), apache receives its STDOUT output!
So obviously it complains about "malformed header" and returns 500

It never happened before, I don't understand.
I checked the commands with "command > file" and there is nothing as STDERR, everything goes into "file", it is pure STDOUT.

How can this happen?
THANKS!

NevemTeve 07-21-2012 11:28 PM

Don't worry, it is perfectly okay; this is how CGI works: the standard output of the CGI program is sent to the client.
When you use system, you can redirect stdout with >file and/or stderr with 2>file.

MisterBark 07-22-2012 09:17 AM

Quote:

Originally Posted by NevemTeve (Post 4734898)
Don't worry, it is perfectly okay; this is how CGI works: the standard output of the CGI program is sent to the client.
When you use system, you can redirect stdout with >file and/or stderr with 2>file.

All right, but I didn't change my programs and the previous server didn't have that problem :-/
It happens only now that I installed a new httpd and new perl on a new server.
Do I really have to search in all my programs to add >/dev/null 2>&1 ????

NevemTeve 07-22-2012 11:34 AM

Yes, but you shouldn't use system too much: it is extremely slow, and may cause security problems as well.

MisterBark 07-22-2012 11:39 AM

Quote:

Originally Posted by NevemTeve (Post 4735215)
Yes, but you shouldn't use system too much: it is extremely slow, and may cause security problems as well.

oh!
What would you use? open with "|" ?

NevemTeve 07-22-2012 11:45 AM

Perl has many built-in functions and pluggable modules; using them is always better than calling external programs.

MisterBark 07-22-2012 11:49 AM

Quote:

Originally Posted by NevemTeve (Post 4735221)
Perl has many built-in functions and pluggable modules; using them is always better than calling external programs.

Right, but sometimes you cannot.
Ok so, to execute a binary, you wouldn't use something else than system() ?

I don't think security can be an issue because I never put any user variable in the string. (indeed)

CTM 07-23-2012 08:55 PM

This is happening because the shell invoked by the call to system() is inheriting its parent's (i.e. the httpd's) STDOUT, and (as NevemTeve said) CGI scripts invoked by httpd use STDOUT as the file descriptor used for their output. You have a couple of options:
  • Redirect the STDOUT of subshells created by system() somewhere else (e.g. /dev/null - take care, because this isn't portable):

    Code:

    system("command > /dev/null");
  • Use the command operator to capture the subshell's STDOUT (and assign its return value to a scalar if you want to inspect it):

    Code:

    `command`;          # discard output
    my $out = `command`; # keep output

  • Use a module for handling external processes, like Proc::Reliable

My preferred approach would be #2 - #3 is fine, but it introduces a new non-standard dependency when Perl's built-in behaviour is good enough for most purposes (and it's probably slower). Also make sure that what you're trying to do by executing the external command hasn't already been done by someone else more thoroughly on CPAN. Don't make life harder than it needs to be. ;)

MisterBark 07-23-2012 09:29 PM

Thanks CTM :)
I never thought of using `command` - I thought it was only for bash scripting, didn't know it worked for perl as well! :)

I'm still surprised that I have to make that change because it never happened on my previous server.


All times are GMT -5. The time now is 02:50 PM.