LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-21-2012, 03:15 AM   #1
MisterBark
Member
 
Registered: Jul 2012
Location: Republic of Krakozhia
Distribution: Slackware & Zenwalk core + compile
Posts: 57

Rep: Reputation: 5
Question 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!
 
Old 07-21-2012, 11:28 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,771

Rep: Reputation: 495Reputation: 495Reputation: 495Reputation: 495Reputation: 495
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.

Last edited by NevemTeve; 07-21-2012 at 11:30 PM.
 
Old 07-22-2012, 09:17 AM   #3
MisterBark
Member
 
Registered: Jul 2012
Location: Republic of Krakozhia
Distribution: Slackware & Zenwalk core + compile
Posts: 57

Original Poster
Rep: Reputation: 5
Quote:
Originally Posted by NevemTeve View Post
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 ????
 
Old 07-22-2012, 11:34 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,771

Rep: Reputation: 495Reputation: 495Reputation: 495Reputation: 495Reputation: 495
Yes, but you shouldn't use system too much: it is extremely slow, and may cause security problems as well.
 
Old 07-22-2012, 11:39 AM   #5
MisterBark
Member
 
Registered: Jul 2012
Location: Republic of Krakozhia
Distribution: Slackware & Zenwalk core + compile
Posts: 57

Original Poster
Rep: Reputation: 5
Quote:
Originally Posted by NevemTeve View Post
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 "|" ?
 
Old 07-22-2012, 11:45 AM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,771

Rep: Reputation: 495Reputation: 495Reputation: 495Reputation: 495Reputation: 495
Perl has many built-in functions and pluggable modules; using them is always better than calling external programs.
 
Old 07-22-2012, 11:49 AM   #7
MisterBark
Member
 
Registered: Jul 2012
Location: Republic of Krakozhia
Distribution: Slackware & Zenwalk core + compile
Posts: 57

Original Poster
Rep: Reputation: 5
Quote:
Originally Posted by NevemTeve View Post
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)
 
Old 07-23-2012, 08:55 PM   #8
CTM
Member
 
Registered: Apr 2004
Distribution: Slackware64 14.0 / 14.1
Posts: 129

Rep: Reputation: 49
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.
 
1 members found this post helpful.
Old 07-23-2012, 09:29 PM   #9
MisterBark
Member
 
Registered: Jul 2012
Location: Republic of Krakozhia
Distribution: Slackware & Zenwalk core + compile
Posts: 57

Original Poster
Rep: Reputation: 5
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.
 
  


Reply

Tags
apache, cgi, httpd, perl, stdout


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Apache web site with perl scripts support aocferreira Linux - Networking 2 04-28-2011 06:59 AM
Perl scripts won't run on Apache -- they did before Tom Douglas Linux - Server 1 02-17-2008 06:26 AM
Executing perl scripts in apache kladizkov Linux - Server 2 10-04-2006 07:35 AM
How can I run a perl and cgi scripts in my apache.?. ethnicme Programming 6 07-21-2006 04:02 AM
running perl scripts une mdk9.l and apache clefler Linux - Software 4 11-15-2003 12:35 PM


All times are GMT -5. The time now is 11:26 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration