system() with timeout

Abigail abigail at abigail.be
Tue Apr 21 11:29:39 BST 2009


On Thu, Apr 16, 2009 at 08:34:16PM +0100, David Cantrell wrote:
> On Thu, Apr 16, 2009 at 07:44:39PM +0100, David Cantrell wrote:
> > On Thu, Apr 16, 2009 at 06:32:08PM +0100, Paul LeoNerd Evans wrote:
> > > On Thu, Apr 16, 2009 at 06:18:45PM +0100, David Cantrell wrote:
> > > > Anyone recommend a module to give me something like system(@list) but
> > > > with a timeout?
> > > Does IPC::Run have something suitable? I seem to recall it has quite a
> > > lot of interesting things to fiddle with...
> > Yes, that looks like exactly what I wanted.  Thanks!
> 
> But its documentation is utterly impenetrable, so 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 wonder whether that has a race condition. Could it be possible that
the following happens:

  - child finishes.
  - waitpid reaps the child.
  - another process is started, with PID equal to $pid.
  - 5 seconds are up and $SIG{ALRM} kicks in.


Abigail


More information about the london.pm mailing list