<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: Process_Win32.pm 96060 2012-08-16 09:27:03Z sma $

use PerlACE::Run_Test;

package PerlACE::Process;

use strict;
use Win32::Process;
use File::Basename;
use Cwd;

# Make sure the File::Which module is installed on the machine
# before trying to use it. If the module is installed on this
# machine, then it will be included because of the eval ()
# statement below.
my $has_which = eval ("use File::Which; 1;") ? 1 : 0;

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

# This is what GetExitCode will return if the process is still running.
my $STILL_ACTIVE = 259;

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

### Constructor and Destructor

#
# Hack in purify support thru 2 environment variables:
#   ACE_RUN_PURIFY_CMD: complete path to purify executable
#   ACE_RUNTEST_DELAY: wait delay factor, default to 10 if
#                      ACE_RUN_PURIFY_CMD is defined, or 1 if
#                      ACE_RUN_PURIFY_CMD is not defined.
# ** Notice that when ACE_RUN_PURIFY_CMD is define, PerlACE::Process
#    reports the return status of *purify*, not the process being purified.
#
# Also hack in the ability to run the test on a WinCE device using the
# ACE_WINCE_TEST_CONTROLLER environment variable. If set, it specifies a
# controlling program to use for setting up and executing the test.
# Further setup can be specialized depending on the value of the variable.

sub new
{
    my $proto = shift;
    my $class = ref ($proto) || $proto;
    my $self = {};

    $self-&gt;{EXECUTABLE} = shift;
    $self-&gt;{ARGUMENTS} = shift;
    $self-&gt;{TARGET} = undef;
    $self-&gt;{RUNNING} = 0;
    $self-&gt;{IGNOREEXESUBDIR} = 0;
    $self-&gt;{IGNOREHOSTROOT} = 0;
    $self-&gt;{PROCESS} = undef;
    $self-&gt;{PURIFY_CMD} = $ENV{'ACE_RUN_PURIFY_CMD'};
    $self-&gt;{PURIFY_OPT} = $ENV{'ACE_RUN_PURIFY_OPT'};
    if (!defined $PerlACE::Process::WAIT_DELAY_FACTOR) {
        if (defined $self-&gt;{PURIFY_CMD}) {
            $PerlACE::Process::WAIT_DELAY_FACTOR = 10;
        }
        else {
            $PerlACE::Process::WAIT_DELAY_FACTOR = 1;
        }
    }
    $self-&gt;{WINCE_CTL} = $ENV{'ACE_WINCE_TEST_CONTROLLER'};

    bless ($self, $class);
    return $self;
}

sub DESTROY
{
    my $self = shift;

    if ($self-&gt;{RUNNING} == 1) {
        print STDERR "ERROR: &lt;", $self-&gt;{EXECUTABLE},
                     "&gt; still running upon object destruction\n";
        $self-&gt;Kill ();
    }
}


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

### Some Accessors

sub Normalize_Executable_Name($)
{
    my $self = shift;
    my $executable = shift;
    my $basename = basename ($executable);
    my $dirname = dirname ($executable). '/';
    my $subdir;
    if (defined $self-&gt;{TARGET}) {
        $subdir = $self-&gt;{TARGET}-&gt;ExeSubDir();
    }
    else {
        $subdir = $PerlACE::Process::ExeSubDir;
    }
    $executable = $dirname.$subdir.$basename.".EXE";

    ## Installed executables do not conform to the ExeSubDir
    if (! -x $executable &amp;&amp; -x $dirname.$basename.'.EXE') {
      $executable = $dirname.$basename.'.EXE';
    }

    $executable =~ s/\//\\/g; # / &lt;- # color coding issue in devenv

    return $executable;
}


sub Executable
{
    my $self = shift;

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

    my $executable = $self-&gt;{EXECUTABLE};
    # If the target's config has a different ACE_ROOT, rebase the executable
    # from $ACE_ROOT to the target's root.
    if (defined $self-&gt;{TARGET} &amp;&amp;
        $self-&gt;{TARGET}-&gt;ACE_ROOT() ne $ENV{'ACE_ROOT'}) {
        $executable = File::Spec-&gt;rel2abs($executable);
        $executable = File::Spec-&gt;abs2rel($executable, $ENV{'ACE_ROOT'});
        $executable = $self-&gt;{TARGET}-&gt;ACE_ROOT() . "/$executable";
    }

    # After VxWorks adopts the TARGET scheme, can do away with this block.
    if ($self-&gt;{IGNOREHOSTROOT} == 0) {
      if (PerlACE::is_vxworks_test()) {
          $executable = PerlACE::VX_HostFile ($executable);
      }
    }

    if ($self-&gt;{IGNOREEXESUBDIR} == 0) {
        $executable = $self-&gt;Normalize_Executable_Name ($executable);
    }
    else {
        if ($executable !~ m/\.(BAT|EXE|COM)$/i) {
            $executable = $executable.".EXE";
        }

        $executable =~ s/\//\\/g; # / &lt;- # color coding issue in devenv

        # If there is no directory in the executable name, then we are going
        # to search the PATH for the executable.
        if ($has_which &amp;&amp; $executable !~ m/\//) {
            my $which = File::Which::which ($executable);

            if ($which ne "") {
                $executable = $which;
            }
        }
    }

    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 ();
    $commandline = '"' . $commandline . '"' if $commandline =~ /\s/;

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

    return $commandline;
}

sub IgnoreExeSubDir
{
    my $self = shift;

    # If we have -Config ARCH, do not set IGNOREEXESUBDIR, since with ARCH
    # all executables (even those in $ACE_ROOT/bin, etc.) are located in the
    # architecture-specific subdirectory.
    if (@_ != 0 &amp;&amp; !grep(($_ eq 'ARCH'), @PerlACE::ConfigList::Configs)) {
        $self-&gt;{IGNOREEXESUBDIR} = shift;
    }
    elsif (@_ != 0 &amp;&amp; $self-&gt;{EXECUTABLE} =~ /perl$/) {
        print ("==== automatically ignoring...\n");
        $self-&gt;{IGNOREEXESUBDIR} = shift;
    }

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

sub IgnoreHostRoot
{
    my $self = shift;

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

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

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

### Spawning processes


# Spawn the process and continue.

sub Spawn ()
{
    my $self = shift;

    if ($self-&gt;{RUNNING} == 1) {
        print STDERR "ERROR: Cannot Spawn: &lt;", $self-&gt;Executable (),
                     "&gt; already running\n";
        return -1;
    }

    if (!defined $self-&gt;{EXECUTABLE}) {
        print STDERR "ERROR: Cannot Spawn: No executable specified\n";
        return -1;
    }

    if ($self-&gt;{IGNOREEXESUBDIR} == 0) {
        if (!-f $self-&gt;Executable ()) {
            print STDERR "ERROR: Cannot Spawn: &lt;", $self-&gt;Executable (),
                         "&gt; not found\n";
            return -1;
        }

        if (!-x $self-&gt;Executable ()) {
            print STDERR "ERROR: Cannot Spawn: &lt;", $self-&gt;Executable (),
                         "&gt; not executable\n";
            return -1;
        }
    }

    my $state = 0;
    my $cmdline = "";
    my $executable = "";

    my $chdir_needed = 0;
    my $curdir_bak;

    if (defined $self-&gt;{PURIFY_CMD}) {
        my $orig_cmdline = $self-&gt;CommandLine ();
        $executable = $self-&gt;{PURIFY_CMD};
        my $basename = basename ($self-&gt;{EXECUTABLE});

        my $PurifyOptions = $self-&gt;{PURIFY_OPT};
        if (!defined $PurifyOptions) {
            $PurifyOptions =
                "/run ".
#                "/save-data=$basename.pfy ".
                "/save-text-data=$basename.pfytxt ".
                "/AllocCallStackLength=20 ".
                "/ErrorCallStackLength=20 ".
                "/HandlesInUseAtExit ".
                "/InUseAtExit ".
                "/LeaksAtExit ";
        }
        $cmdline =
            "purify " .
            "$PurifyOptions ".
            "$orig_cmdline" ;
    }
    elsif (defined $self-&gt;{WINCE_CTL}) {
        $executable = $self-&gt;Executable ();
        $cmdline = $self-&gt;CommandLine ();

        # Generate a script to copy the test down to the device, run it,
        # copy the log file(s) back to the log directory, then delete the
        # program and log files on the remote device.
        unless (open (SCRIPT, "&gt;start_test.cmd")) {
            print STDERR "ERROR: Cannot Spawn: &lt;", $self-&gt;Executable (),
                         "&gt; failed to create start_test.cmd\n";
            return -1;
        }

        my $testname = basename($executable,'.EXE');
        my $here = getcwd();
        $here =~ s/\//\\/g;
        $executable =~ s/^\.//;      # Chop leading .
        $executable = $here . $executable;   # Fully qualified name
        # Take off the test name from the start of the command line.
        # The command name is preprended in the script below.
        my @tokens = split(' ', $cmdline);
        @tokens = splice(@tokens,1);
        $cmdline = join(' ', @tokens);
        print SCRIPT "copy $executable 1:\\Windows\n";
        print SCRIPT "start /wait $testname $cmdline\n";
        print SCRIPT "copy 1:\\log\\$testname*.txt $here\\log\n";
        print SCRIPT "del 1:\\Windows\\$testname.exe\n";
        print SCRIPT "del 1:\\log\\$testname*.txt\n";
        close SCRIPT;

        $executable = $ENV{'ComSpec'};
        my $pocket_device_opts = $ENV{'ACE_PCE_DEVICE'};
        $cmdline = "cmd /C start /B /WAIT $self-&gt;{WINCE_CTL} $pocket_device_opts -m NAME=start_test.cmd;WAIT=401000; -e"
    }
    elsif (defined $ENV{'ACE_TEST_WINDOW'}) {
        $state = ($ENV{'ACE_TEST_WINDOW'} =~ /\/k/i ? CREATE_NEW_CONSOLE : DETACHED_PROCESS);
        $executable = $ENV{'ComSpec'};
        $cmdline = $ENV{'ACE_TEST_WINDOW'} . ' ' . $self-&gt;CommandLine();
    }
    else {
        $executable = $self-&gt;Executable ();
        $cmdline = $self-&gt;CommandLine ();
        if ((defined $self-&gt;{TARGET}) &amp;&amp; ($ENV{'ACE_ROOT'} ne $self-&gt;{TARGET}-&gt;ACE_ROOT ())) {
          $chdir_needed = 1;
          $curdir_bak = cwd ();
          chdir (dirname ($executable));
        }
    }
    if (defined $ENV{'ACE_TEST_VERBOSE'}) {
      print "$executable $cmdline\n";
    }
    my %backup_ENV = %ENV;
    # update environment for target
    if (defined $self-&gt;{TARGET}) {
        if (!defined $self-&gt;{TARGET}-&gt;{REMOTE_SHELL}) {
            my $x_env_ref = $self-&gt;{TARGET}-&gt;{EXTRA_ENV};
            while ( my ($env_key, $env_value) = each(%$x_env_ref) ) {
                if (defined $ENV{'ACE_TEST_VERBOSE'}) {
                    print "INFO: adding target environment $env_key=$env_value\n";
                }
                $ENV{$env_key} = $env_value;
            }
        }
        if ($self-&gt;{TARGET}-&gt;{LIBPATH}) {
            if (defined $ENV{'ACE_TEST_VERBOSE'}) {
                print "INFO: adding target libpath ".$self-&gt;{TARGET}-&gt;{LIBPATH}."\n";
            }
            PerlACE::add_lib_path ($self-&gt;{TARGET}-&gt;{LIBPATH});
        }
    }
    my $status = Win32::Process::Create ($self-&gt;{PROCESS},
                                         $executable,
                                         $cmdline,
                                         ($state == 0 ? 1 : 0),
                                         $state,
                                         '.');
    %ENV = %backup_ENV;
    if ($chdir_needed) {
      chdir ($curdir_bak);
    }
    if ($status == 0) {
        print STDERR "ERROR: Spawn failed for &lt;", $self-&gt;CommandLine (), "&gt;\n";
        return -1;
    }

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


# 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
    }

    $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);
}


# Kill the process

sub Kill ($)
{
    my $self = shift;
    my $notused = shift; #Used in Process_Unix.pm

    if ($self-&gt;{RUNNING}  &amp;&amp; !defined $ENV{'ACE_TEST_WINDOW'}) {
        Win32::Process::Kill ($self-&gt;{PROCESS}, -1);
    }

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


# Terminate the process and wait for it to finish

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

    if ($self-&gt;{RUNNING}) {
        print STDERR "INFO: $self-&gt;{EXECUTABLE} being killed.\n";
        Win32::Process::Kill ($self-&gt;{PROCESS}, 0);
    }

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


# Wait until a process exits.
# return -1 if the process is still alive.

sub Wait ($)
{
    my $self = shift;
    my $timeout = shift;
    if (!defined $timeout || $timeout &lt; 0) {
      $timeout = INFINITE;
    } else {
      $timeout = $timeout * 1000 * $PerlACE::Process::WAIT_DELAY_FACTOR;
    }

    my $result = 0;

    if ($self-&gt;{RUNNING}) {
      $result = Win32::Process::Wait ($self-&gt;{PROCESS}, $timeout);
      if ($result == 0) {
        return -1;
      }
    }
    Win32::Process::GetExitCode ($self-&gt;{PROCESS}, $result);
    $self-&gt;{RUNNING} = 0;
    return $result;
}


# Wait for a process to exit with a timeout

sub TimedWait ($)
{
    my($self) = shift;
    my($timeout) = shift;
    return $self-&gt;Wait($timeout);
}

###

sub kill_all
{
    my $procmask = shift;
    my $target = shift;
    my $pid = -1;
    for my $line (`tasklist /nh /fo csv`) {
        # find matching process line
        if ($line =~ /$procmask/) {
            # find process PID
            if ($line =~ /^\"[^\"]+\",\"(\d+)\",/) {
                $pid = $1;
                Win32::Process::KillProcess ($pid, 0); # kill process
                if (defined $ENV{'ACE_TEST_VERBOSE'}) {
                    print STDERR "INFO: Killed process at [$line]\n"
                }
            }
        }
    }
}

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