system() with timeout

David Cantrell david at cantrell.org.uk
Fri Apr 17 11:25:56 BST 2009


On Fri, Apr 17, 2009 at 09:24:01AM +0100, Matt Lawrence wrote:
> David Cantrell wrote:
> > I ended up doing ...
> >    eval {
> >        if(my $pid = fork()) { # parent
> >            local $SIG{ALRM} = sub {
> >                kill 9, $pid; # quit RIGHT FUCKING NOW
> >                die("Child process timed out\n");
> >            };
> >            alarm(5);
> >            waitpid($pid, 0);
> >            alarm(0);
> >        } else {
> >            exec(@blah);
> >        }
> >    };
> I recently discovered that die() inside a signal handler causes a memory 
> leak. I don't know if that would be a problem for you in this case.

Not one I care much about at the moment :-)

> I guess that might be related to the problem you were having before 
> timing out system().

No.  The problem is that the code I'm exec()ing might do something like ...
  while(1) {}

or (rather more likely) ...
  print "would you like Fried::Fake::Potato::Mush with that?";$fries = <STDIN>;

but it'll never get any input.  Here's the code in context:
  http://www.cantrell.org.uk/cgit/cgit.cgi/perlmodules/tree/CPAN-FindDependencies/lib/CPAN/FindDependencies/MakeMaker.pm

-- 
David Cantrell | Cake Smuggler Extraordinaire

   The voices said it's a good day to clean my weapons


More information about the london.pm mailing list