#!/usr/bin/perl

#  These scripts fail..but normal harness doesn't deal well with
#  failing scripts..so run them here.

use strict;

# These commands are bad to hang and otherwise mis-behave.
# So, fork before calling them..and kill them if they don't
# die in time.

my $timeout = 120;

sub REAPER {
  $SIG{CHLD} = \&REAPER;
  wait;
}
$SIG{CHLD} = \&REAPER;

sub fork_sys {
  my $cmd = shift;
  my $timeout = shift;

  my $pid = fork();
  if ($pid == 0) {
    # child
    system($cmd);
    exit(0);
  }
  elsif ($pid > 0) {
    sleep(10); # Let child get started
    # Parent.
    my $i;
    for ($i = 0; $i<$timeout; $i++) {
      if (-f "/proc/$pid/cmdline") {
	sleep(1);
      }
      else {
        print "Seems child has exited properly.\n";
	last;
      }
    }
    # There should be no test_peer processes running now, kill them if they
    # exist.
    print "Killing any existing test_peer processes, child pid: $pid";
    print `ps auxwww|grep test_peer`;
    print "\n";
    system("killall -s 9 -r runit");
    system("killall -s 9 -r test_peer");
    system("killall -s 9 -r runit");
    system("killall -s 9 -r test_peer");
  }
  else {
    print "ERROR:  fork failed in test_busted script!\n";
  }
}

fork_sys("./test_peering1.sh -l -t test26", $timeout);

fork_sys("./test_peering1.sh -l -t test28_ipv6", $timeout);

fork_sys("./test_peering1.sh -l -t test28_ipv6_ok", $timeout);

exit(0);
