You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Igor Shevchenko <ig...@carcass.ath.cx> on 2005/03/11 00:29:34 UTC

an update to exec_helper.pl script

Hi all,

Some time ago we had a thread about starting long-running background processes 
from mp2. I've found a race condition in our last solution. The helper 
background script could be killed by apache cleanup handler before it had a 
chance to fork into background and do "setsid". Here's the script itself:

===start===
#!/usr/bin/perl -w
use strict;
use warnings;
use POSIX ();

chdir '/';

POSIX::setsid;

exit if fork() > 0; # fork once again

close STDIN;
open STDOUT, '+>>', '/path/to/apache/error_log'; # any way to get path to the 
current error_log and pass it in from the caller as an argument or as an 
opened filehandle ?
open STDERR, '>&STDOUT';

exec ( @ARGV ) or die "Failed to exec subprocess: $@\n";

===end===

Here's an updated helper subroutine:

sub safe_exec {
    my $in = $apr->spawn_proc_prog ( '/path/to/exec_helper.pl', \@_ );
    
    # makes us sleep until the helper is forked into background and it's 
handlers are untied from us
    eval { read $in, my($buffer), 1024 };
    if ( $@ ) {
	print STDERR "exec error=$@\n";
    }
    close $in;
}

I had to use this form of spawn_proc_prog to get something to wait on.

-- 
Best Regards,
Igor Shevchenko