<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#! /usr/bin/perl
# $Id: ProcessVX.pm 89840 2010-04-12 09:36:32Z mcorino $

package PerlACE::ProcessVX;

use strict;
use English;
use POSIX qw(:time_h);

$PerlACE::ProcessVX::ExeSubDir = './';
$PerlACE::ProcessVX::DoVxInit = (defined $ENV{"ACE_RUN_VX_NO_INITIAL_REBOOT"}) ? 0 : 1;
$PerlACE::ProcessVX::VxDefGw = 1;
$PerlACE::ProcessVX::RebootTime = (defined $ENV{"ACE_RUN_VX_REBOOT_TIME"}) ? $ENV{"ACE_RUN_VX_REBOOT_TIME"} : 90;
$PerlACE::ProcessVX::ExeExt = ($PerlACE::VxWorks_RTP_Test) ? '.vxe' : '.out';
$PerlACE::ProcessVX::RebootCmd = $ENV{"ACE_RUN_VX_REBOOT_CMD"};

# Wait for the process to exit or kill after a time period

sub WaitKill ($)
{
    my $self = shift;
    my $timeout = shift;

    my $status = $self-&gt;TimedWait ($timeout);

    if ($status == -1) {
        print STDERR "ERROR: $self-&gt;{EXECUTABLE} timedout\n";
        $self-&gt;Kill ();
        # Don't need to Wait since we are on Win32

        $PerlACE::ProcessVX::DoVxInit = 1; # force reboot on next run
    }

    $self-&gt;{RUNNING} = 0;

    return $status;
}


# Do a Spawn and immediately WaitKill

sub SpawnWaitKill ($)
{
    my $self = shift;
    my $timeout = shift;

    if ($self-&gt;Spawn () == -1) {
        return -1;
    }

    return $self-&gt;WaitKill ($timeout);
}


###############################################################################

### Some Accessors

sub Normalize_Executable_Name
{
    my $self = shift;
    my $executable = shift;

    my $basename = basename ($executable);
    my $dirname = dirname ($executable). '/';

    $executable = $dirname.$PerlACE::ProcessVX::ExeSubDir.$basename.$PerlACE::ProcessVX::ExeExt;

    ## Installed executables do not conform to the ExeSubDir
    if (! -e $executable &amp;&amp; -e $dirname.$basename.$PerlACE::ProcessVX::ExeExt) {
      $executable = $dirname.$basename.$PerlACE::ProcessVX::ExeExt;
    }

    return $executable;
}


sub Executable
{
    my $self = shift;

    if (@_ != 0) {
        $self-&gt;{EXECUTABLE} = shift;
    }

    my $executable = $self-&gt;{EXECUTABLE};

    if ($self-&gt;{IGNOREEXESUBDIR} == 0) {
      $executable = $self-&gt;Normalize_Executable_Name ($executable);
    }
    else {
      $executable = $executable.$PerlACE::ProcessVX::ExeExt;
    }

    return $executable;
}

sub Arguments
{
    my $self = shift;

    if (@_ != 0) {
        $self-&gt;{ARGUMENTS} = shift;
    }

    return $self-&gt;{ARGUMENTS};
}

sub CommandLine ()
{
    my $self = shift;

    my $commandline = $self-&gt;Executable ();

    if (defined $self-&gt;{ARGUMENTS}) {
        $commandline .= ' '.$self-&gt;{ARGUMENTS};
    }

    return $commandline;
}

sub IgnoreExeSubDir
{
    my $self = shift;

    if (@_ != 0) {
        $self-&gt;{IGNOREEXESUBDIR} = shift;
    }

    return $self-&gt;{IGNOREEXESUBDIR};
}

sub IgnoreHostRoot
{
    my $self = shift;

    if (@_ != 0) {
        $self-&gt;{IGNOREHOSTROOT} = shift;
    }

    return $self-&gt;{IGNOREHOSTROOT};
}

sub delay_factor {
  my($lps)    = 128;
  my($factor) = 1;

  ## Keep increasing the loops per second until the amount of time
  ## exceeds the number of clocks per second.  The original code
  ## did not multiply $ticks by 8 but, for faster machines, it doesn't
  ## seem to return false values.  The multiplication is done to minimize
  ## the amount of time it takes to determine the correct factor.
  while(($lps &lt;&lt;= 1)) {
    my($ticks) = clock();
    for(my $i = $lps; $i &gt;= 0; $i--) {
    }
    $ticks = clock() - $ticks;
    if ($ticks * 8 &gt;= CLOCKS_PER_SEC) {
      $factor = 500000 / (($lps / $ticks) * CLOCKS_PER_SEC);
      last;
    }
  }

  return $factor;
}

sub iboot_cycle_power {
  my $self = shift;
  my $mode = shift;

  # mode 0 is reboot, mode 1 is just shutdown

  my ($iboot_host,
      $iboot_outlet,
      $iboot_user,
      $iboot_passwd ) = ($ENV{'ACE_RUN_VX_IBOOT'},
                         $ENV{'ACE_RUN_VX_IBOOT_OUTLET'},
                         $ENV{'ACE_RUN_VX_IBOOT_USER'},
                         $ENV{'ACE_RUN_VX_IBOOT_PASSWORD'});

  my $v = $ENV{'ACE_TEST_VERBOSE'};

  if ($v) {
    print "Using iBoot: $iboot_host\n";
    if (defined $iboot_outlet) {
      print "Using iBoot Outlet #: $iboot_outlet\n";
    }
  }

  # There are three cases to handle here:
  # 1. using a single-outlet iBoot
  # 2. using a multi-outlet iBootBar with custom firmware
  # 3. using a multi-outlet iBootBar with standard firmware
  #
  # In cases 1 &amp; 2, we use the iPAL protocol; in case 3 we
  # use a telnet connection and the command-line syntax.
  #
  # We determine that it's case #3 by the concurrent presence
  # of an outlet number, an iboot username, and an iboot password
  # in the environment.
  #

  if (defined($iboot_outlet) &amp;&amp; defined($iboot_user) &amp;&amp; defined($iboot_passwd)) {
    # We perform case #3
    # This case doesn't support shutdown
    return if $mode == 1;

    my $t = new Net::Telnet();

    $t-&gt;prompt('/iBootBar \&gt; /');
    my $savedmode = $t-&gt;errmode();
    $t-&gt;errmode("return");

    my $retries = 5;
    my $is_open = 0;

    while ($retries--) {
      my $r = $t-&gt;open($iboot_host);
      if ($r == 1) {
        $is_open = 1;
        last;
      }
    }
    continue {
      print "Couldn't open connection; sleeping then retrying\n" if ($v);
      sleep(5);
    }

    if (! $is_open) {
      print "Unable to open $iboot_host.\n" if ($v);
      return 0;
    }

    $t-&gt;errmode($savedmode);

    # Simple login b/c Net::Telnet::login hardcodes the prompts
    $t-&gt;waitfor('/User Name:\s*$/i');
    $t-&gt;print($iboot_user);
    $t-&gt;waitfor('/password:\s*/i');
    $t-&gt;print($iboot_passwd);

    $t-&gt;waitfor($t-&gt;prompt);

    print "successfully logged in to $iboot_host\n" if ($v);

    my $output = $t-&gt;cmd("set outlet $iboot_outlet cycle");

    print "successfully cycled power on outlet $iboot_outlet\n" if ($v);

    $t-&gt;close();
  }
  else {
    # Perform cases 1 &amp; 2
    my $iboot;
    my $text;
    if (!defined($iboot_passwd)) {
      $iboot_passwd = "PASS";
    }

    my $ipal_command_series;
    if (defined $iboot_outlet) {
      $ipal_command_series = ($mode == 0 ? ['E', 'D'] : ['E']);
    } else {
      $ipal_command_series = ($mode == 0 ? ['f', 'n'] : ['f']);
    }

    foreach my $ipal_cmd (@$ipal_command_series) {
      my $retries = 3;
      my $is_open = 0;
      while ($retries--) {
        $iboot = IO::Socket::INET-&gt;new ("$iboot_host");
        if ($iboot) {
          # if ACE_RUN_VX_IBOOT_OUTLET is defined, we're using
          # the iBootBar, and we're using the iPAL Protocol
          # to communicate with the iBootBar
          if (defined $iboot_outlet) {
            $iboot-&gt;send ("\e".$iboot_passwd."\e".$iboot_outlet.$ipal_cmd);
          }
          else  {
            $iboot-&gt;send ("\e".$iboot_passwd."\e$ipal_cmd\r");
          }
          $iboot-&gt;recv ($text,128);
          print "iBoot is currently: $text\n" if ($v);
          $iboot-&gt;close();
          if (defined $iboot_outlet) {
            $text = substr $text, $iboot_outlet - 1, 1;
            if ($text eq "0" || $text eq "1") {
              $is_open = 1;
              last;
            }
            else {
              print "iBoot is $text; sleeping then retrying\n" if ($v);
              sleep(5);
            }
          }
          else  {
            if ($text eq "OFF" || $text eq "ON") {
              $is_open = 1;
              last;
            }
            else {
              print "iBoot is $text; sleeping then retrying\n" if ($v);
              sleep(5);
            }
          }
        }
        else {
          print "ERROR: FAILED to execute 'reboot' command!\n";
        }
      }
      if (!$is_open) {
        print "Unable to reboot using $iboot_host.\n" if ($v);
        return 0;
      }
    }
  }
}

sub reboot {
  my $self = shift;
  my $iboot;
  my $text;
  my $t;
  my $ok;

  my $target_login = $ENV{'ACE_RUN_VX_LOGIN'};
  my $target_password = $ENV{'ACE_RUN_VX_PASSWORD'};

  ##
  ## initialize VxWorks kernel (reboot!) if needed
  if ($PerlACE::ProcessVX::DoVxInit || $ENV{'ACE_RUN_VX_TGT_REBOOT'}) {
      if (defined $ENV{'ACE_RUN_VX_REBOOT_TOOL'}) {
          if (defined $ENV{'ACE_TEST_VERBOSE'}) {
              print "Calling: $ENV{'ACE_RUN_VX_REBOOT_TOOL'}\n";
          }
          system ($ENV{'ACE_RUN_VX_REBOOT_TOOL'});
      }
      else {
        if (defined $ENV{'ACE_RUN_VX_IBOOT'}) {
          $self-&gt;iboot_cycle_power(0);
        }
        else {
          if (defined $ENV{'ACE_TEST_VERBOSE'}) {
            print "Executing 'reboot' command over Telnet to ".$ENV{'ACE_RUN_VX_TGTHOST'}.".\n";
          }
          $t = new Net::Telnet(Timeout =&gt; 10,
                               Prompt =&gt; '/-&gt; $/',
                               Errmode =&gt; 'return');
          $t-&gt;open($ENV{'ACE_RUN_VX_TGTHOST'});
          $t-&gt;print("");
          if (defined $target_login)  {
            $t-&gt;waitfor('/VxWorks login: $/');
            $t-&gt;print("$target_login");
          }
          if (defined $target_password)  {
            $t-&gt;waitfor('/Password: $/');
            $t-&gt;print("$target_password");
          }
          $ok = $t-&gt;waitfor('/-&gt; $/');
          if ($ok) {
            $t-&gt;print($PerlACE::ProcessVX::RebootCmd);
          }
          else {
            print "ERROR: FAILED to execute 'reboot' command!\n";
          }
          $t-&gt;close();
        }
      }
      $PerlACE::ProcessVX::VxDefGw = 1;
      $PerlACE::ProcessVX::DoVxInit = 0;
      sleep($PerlACE::ProcessVX::RebootTime);
  }
}

# Helper for spawning with list of kernel modules in a .vxtest file
sub handle_vxtest_file
{
  my $self = shift;
  my $vxtestfile = shift;
  my $vx_ref = shift;
  my $unld_ref = shift;
  my $fh = new FileHandle;
  if (open ($fh, $vxtestfile)) {
    my $line1 = &lt;$fh&gt;;
    chomp $line1;
    while(&lt;$fh&gt;) {
      $line1 = $_;
      chomp $line1;
      push @$vx_ref, "ld &lt; lib$line1" . ".so";
      unshift @$unld_ref, "unld \"lib$line1" . ".so\"";
    }
    close $fh;
  } else {
    return 0;
  }
  return 1;
}

# Load a file that is used as startup script. This script has to be
# located on the host system
sub handle_startup_script
{
  my $script = shift;
  my $cmds = shift;
  my $fh = new FileHandle;
  if (open ($fh, $script)) {
    while(&lt;$fh&gt;) {
      my $line1 = $_;
      chomp $line1;
      push @$cmds, "$line1";
    }
    close $fh;
  } else {
    return 0;
  }
  return 1;
}

### Check for -ExeSubDir commands, store the last one
my @new_argv = ();

for(my $i = 0; $i &lt;= $#ARGV; ++$i) {
    if ($ARGV[$i] eq '-ExeSubDir') {
        if (defined $ARGV[$i + 1]) {
            $PerlACE::ProcessVX::ExeSubDir = $ARGV[++$i].'/';
        }
        else {
            print STDERR "You must pass a directory with ExeSubDir\n";
            exit(1);
        }
    }
    else {
        push @new_argv, $ARGV[$i];
    }
}
@ARGV = @new_argv;

$PerlACE::ProcessVX::WAIT_DELAY_FACTOR = $ENV{"ACE_RUNTEST_DELAY"};

if (defined $ENV{'ACE_TEST_WINCE'}) {
  if ($OSNAME eq "MSWin32") {
      require PerlACE::ProcessWinCE;
  } else {
      require PerlACE::ProcessWinCE_Unix;
  }
} else {
  if ($OSNAME eq "MSWin32") {
      require PerlACE::ProcessVX_Win32;
  }
  else {
      require PerlACE::ProcessVX_Unix;
  }
}

###

sub kill_all
{
  my $procmask = shift;
  my $target = shift;
  ## NOT IMPLEMENTED YET
}

1;
</pre></body></html>