[forwarded submission from a non-member address -- rjk]


From: "Evan A. Zacks" <[EMAIL PROTECTED]>
Date: Fri, 5 Oct 2001 15:04:37 -0400
Subject: Re: [Boston.pm] Safe execution of 1 perl prog from another?
To: [EMAIL PROTECTED]

On Fri, Oct 05, 2001 at 11:27:02 AM, Paul Lussier wrote:

> I have a situation where I need to exec a command-line based perl 
> program from a CGI wrapper.  I want to do this safely, but I also 
> want to see the same output from the command line program in the
> browser that I would normally.
> 
> I'm not quite sure how I should go about doing this, and have it safe 
> as well.  I'm assuming the best option is using the list form of 
> system()?  If so, how do I get STDERR to the browser as well?

Hello Paul,

One option is to open a pipe from 'minus' to fork a child whose STDOUT
will be available to the parent through the pipe. Then have the child
exec() the command line program. You'll want to redirect the child's
STDERR to STDOUT before exec'ing, though.

my $program = '/usr/games/fortune';
my @options = qw(-l); # something from user input here

defined( my $pid = open (FORTUNE, '-|') ) or die "can't fork: $!\n";

# parent (reader)
if ( $pid ) {
  while ( <FORTUNE> ) {
    print "$. $_"; # add line numbers, for example
  }
  close (FORTUNE) or warn "error closing fortune: $?";
}
# child (command line program)
else {
    # child process gets replaced with $program
    open ( STDERR, '>&STDOUT' ) or die "can't redirect STDERR: $!\n";
    exec ( $program, @options ) or die "exec error: $!";
}

perldoc perlipc and read the section under 'Using open() for IPC' for
more details. 


-E

Reply via email to