Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / bin / PerlACE / ProcessVX_Win32.pm
blob3b506b605042ce69ad2421789e9df8c658bd238e
1 #!/usr/bin/env perl
3 package PerlACE::ProcessVX;
5 use strict;
6 use Win32::Process;
7 use File::Basename;
8 use File::Spec;
9 use FileHandle;
10 use Cwd;
12 eval { require Net::Telnet; };
14 ###############################################################################
16 # This is what GetExitCode will return if the process is still running.
17 my $STILL_ACTIVE = 259;
19 ###############################################################################
21 ### Constructor and Destructor
23 sub new
25 my $proto = shift;
26 my $class = ref ($proto) || $proto;
27 my $self = {};
29 $self->{RUNNING} = 0;
30 $self->{IGNOREEXESUBDIR} = 1;
31 $self->{IGNOREHOSTROOT} = 0;
32 $self->{PROCESS} = undef;
33 $self->{EXECUTABLE} = shift;
34 $self->{ARGUMENTS} = shift;
35 if (!defined $PerlACE::ProcessVX::WAIT_DELAY_FACTOR) {
36 $PerlACE::ProcessVX::WAIT_DELAY_FACTOR = 3;
38 if (!defined $PerlACE::ProcessVX::RebootCmd) {
39 $PerlACE::ProcessVX::RebootCmd = "reboot";
41 bless ($self, $class);
42 return $self;
45 sub DESTROY
47 my $self = shift;
49 if ($self->{RUNNING} == 1) {
50 print STDERR "ERROR: <", $self->{EXECUTABLE},
51 "> still running upon object destruction\n";
52 $self->Kill ();
55 if (!defined $ENV{'ACE_TEST_VERBOSE'}) {
56 unlink "run_vx.pl";
59 if (defined $ENV{'ACE_RUN_VX_IBOOT'} && !defined $ENV{'ACE_RUN_VX_NO_SHUTDOWN'}) {
60 # Shutdown the target to save power
61 $self->iboot_cycle_power(1);
65 ###############################################################################
67 ### Spawning processes
70 # Spawn the process and continue.
72 sub Spawn ()
74 my $self = shift;
76 if ($self->{RUNNING} == 1) {
77 print STDERR "ERROR: Cannot Spawn: <", $self->Executable (),
78 "> already running\n";
79 return -1;
82 if (!defined $self->{EXECUTABLE}) {
83 print STDERR "ERROR: Cannot Spawn: No executable specified\n";
84 return -1;
87 if ($self->{IGNOREEXESUBDIR} == 0) {
88 if (!-f $self->Executable ()) {
89 print STDERR "ERROR: Cannot Spawn: <", $self->Executable (),
90 "> not found\n";
91 return -1;
95 my $status = 0;
97 my $cmdline;
99 # Reboot the target if necessery
100 $self->reboot();
102 my $program = $self->Executable ();
103 my $cwdrel = dirname ($program);
104 my $prjroot = defined $ENV{'ACE_RUN_VX_PRJ_ROOT'} ? $ENV{'ACE_RUN_VX_PRJ_ROOT'} : $ENV{'ACE_ROOT'};
105 if (length ($cwdrel) > 0) {
106 $cwdrel = File::Spec->abs2rel( cwd(), $prjroot );
108 else {
109 $cwdrel = File::Spec->abs2rel( $cwdrel, $prjroot );
111 $cwdrel =~ s/\\/\//g;
112 $program = basename($program, $PerlACE::ProcessVX::ExeExt);
114 unlink "run_vx.pl";
115 my $oh = new FileHandle();
116 if (!open($oh, ">run_vx.pl")) {
117 print STDERR "ERROR: Unable to write to run_vx.pl\n";
118 exit -1;
121 my @cmds;
122 my $cmdnr = 0;
123 my $arguments = "";
124 my $prompt = '';
125 my $exesubdir = defined $ENV{'ACE_RUN_VX_EXE_SUBDIR'} ? $ENV{'ACE_RUN_VX_EXE_SUBDIR'} : "";
127 if (defined $ENV{'ACE_RUN_VX_STARTUP_SCRIPT'}) {
128 if (defined $ENV{'ACE_RUN_VX_STARTUP_SCRIPT_ROOT'}) {
129 @cmds[$cmdnr++] = 'cd "' . $ENV{'ACE_RUN_VX_STARTUP_SCRIPT_ROOT'} . '"';
131 @cmds[$cmdnr++] = '< ' . $ENV{'ACE_RUN_VX_STARTUP_SCRIPT'};
134 if (defined $ENV{'ACE_RUN_VX_STARTUP_COMMAND'}) {
135 @cmds[$cmdnr++] = $ENV{'ACE_RUN_VX_STARTUP_COMMAND'};
138 if ($PerlACE::VxWorks_RTP_Test) {
139 @cmds[$cmdnr++] = 'cmd';
140 if ( defined $ENV{'ACE_RUN_VX_TGTSVR_DEFGW'} && $PerlACE::ProcessVX::VxDefGw) {
141 @cmds[$cmdnr++] = "C mRouteAdd(\"0.0.0.0\", \"" . $ENV{'ACE_RUN_VX_TGTSVR_DEFGW'} . "\", 0,0,0)";
142 $PerlACE::ProcessVX::VxDefGw = 0;
145 if (defined $ENV{'ACE_RUN_VX_TGT_STARTUP_SCRIPT'}) {
146 my(@start_commands);
147 if (handle_startup_script ($ENV{'ACE_RUN_VX_TGT_STARTUP_SCRIPT'}, \@start_commands)) {
148 push @cmds, @start_commands;
149 $cmdnr += scalar @start_commands;
153 @cmds[$cmdnr++] = 'cd "' . $ENV{'ACE_RUN_VX_TGTSVR_ROOT'} . "/" . $cwdrel . '"';
154 @cmds[$cmdnr++] = 'C putenv("TMPDIR=' . $ENV{'ACE_RUN_VX_TGTSVR_ROOT'} . "/" . $cwdrel . '")';
156 if (defined $ENV{'ACE_RUN_ACE_DEBUG'}) {
157 @cmds[$cmdnr++] = 'C putenv("ACE_DEBUG=' . $ENV{'ACE_RUN_ACE_DEBUG'} . '")';
160 if (defined $ENV{'ACE_RUN_TAO_ORB_DEBUG'}) {
161 @cmds[$cmdnr++] = 'C putenv("TAO_ORB_DEBUG=' . $ENV{'ACE_RUN_TAO_ORB_DEBUG'} . '")';
164 if (defined $ENV{'ACE_RUN_ACE_LD_SEARCH_PATH'}) {
165 @cmds[$cmdnr++] = 'C putenv("ACE_LD_SEARCH_PATH=' . $ENV{'ACE_RUN_ACE_LD_SEARCH_PATH'} . '")';
167 if (defined $self->{TARGET}) {
168 my $x_env_ref = $self->{TARGET}->{EXTRA_ENV};
169 while ( my ($env_key, $env_value) = each(%$x_env_ref) ) {
170 if (defined $ENV{'ACE_TEST_VERBOSE'}) {
171 print "INFO: adding target environment $env_key=$env_value\n";
173 @cmds[$cmdnr++] = 'C putenv("' . $env_key. '=' . $env_value . '")';
177 if (defined $ENV{'ACE_RUN_VX_CHECK_RESOURCES'}) {
178 @cmds[$cmdnr++] = 'C memShow()';
181 $cmdline = $program . $PerlACE::ProcessVX::ExeExt . ' ' . $self->{ARGUMENTS};
182 @cmds[$cmdnr++] = $cmdline;
183 $prompt = '\[vxWorks \*\]\# $';
185 if ($PerlACE::VxWorks_Test) {
186 if ( defined $ENV{'ACE_RUN_VX_TGTSVR_DEFGW'} && $PerlACE::ProcessVX::VxDefGw) {
187 @cmds[$cmdnr++] = "mRouteAdd(\"0.0.0.0\", \"" . $ENV{'ACE_RUN_VX_TGTSVR_DEFGW'} . "\", 0,0,0)";
188 $PerlACE::ProcessVX::VxDefGw = 0;
191 if (defined $ENV{'ACE_RUN_VX_TGT_STARTUP_SCRIPT'}) {
192 my(@start_commands);
193 if (handle_startup_script ($ENV{'ACE_RUN_VX_TGT_STARTUP_SCRIPT'}, \@start_commands)) {
194 push @cmds, @start_commands;
195 $cmdnr += scalar @start_commands;
199 my(@load_commands);
200 my(@unload_commands);
201 if (!$PerlACE::Static && !$PerlACE::VxWorks_RTP_Test) {
202 my $vxtest_file = $program . '.vxtest';
203 if (handle_vxtest_file($self, $vxtest_file, \@load_commands, \@unload_commands)) {
204 @cmds[$cmdnr++] = "cd \"$ENV{'ACE_RUN_VX_TGTSVR_ROOT'}/lib\"";
205 push @cmds, @load_commands;
206 $cmdnr += scalar @load_commands;
207 } else {
208 print STDERR "ERROR: Cannot find <", $vxtest_file, ">\n";
209 return -1;
213 @cmds[$cmdnr++] = 'cd "' . $ENV{'ACE_RUN_VX_TGTSVR_ROOT'} . "/" . $cwdrel . "/" . $exesubdir . '"';
214 @cmds[$cmdnr++] = 'putenv("TMPDIR=' . $ENV{'ACE_RUN_VX_TGTSVR_ROOT'} . "/" . $cwdrel . '")';
216 if (defined $ENV{'ACE_RUN_VX_CHECK_RESOURCES'}) {
217 @cmds[$cmdnr++] = 'memShow()';
220 if (defined $ENV{'ACE_RUN_ACE_DEBUG'}) {
221 @cmds[$cmdnr++] = 'putenv("ACE_DEBUG=' . $ENV{'ACE_RUN_ACE_DEBUG'} . '")';
224 if (defined $ENV{'ACE_RUN_TAO_ORB_DEBUG'}) {
225 @cmds[$cmdnr++] = 'putenv("TAO_ORB_DEBUG=' . $ENV{'ACE_RUN_TAO_ORB_DEBUG'} . '")';
228 if (defined $ENV{'ACE_RUN_ACE_LD_SEARCH_PATH'}) {
229 @cmds[$cmdnr++] = 'putenv("ACE_LD_SEARCH_PATH=' . $ENV{'ACE_RUN_ACE_LD_SEARCH_PATH'} . '")';
231 if (defined $self->{TARGET}) {
232 my $x_env_ref = $self->{TARGET}->{EXTRA_ENV};
233 while ( my ($env_key, $env_value) = each(%$x_env_ref) ) {
234 if (defined $ENV{'ACE_TEST_VERBOSE'}) {
235 print "INFO: adding target environment $env_key=$env_value\n";
237 @cmds[$cmdnr++] = 'putenv("' . $env_key. '=' . $env_value . '")';
241 @cmds[$cmdnr++] = 'ld <'. $program . $PerlACE::ProcessVX::ExeExt;
242 if (defined $self->{ARGUMENTS}) {
243 ($arguments = $self->{ARGUMENTS})=~ s/\"/\\\"/g;
244 ($arguments = $self->{ARGUMENTS})=~ s/\'/\\\'/g;
245 $arguments = ",\"" . $arguments . "\"";
247 if (defined $ENV{'ACE_RUN_VX_TGTSRV_WORKINGDIR'}) {
248 @cmds[$cmdnr++] = 'cd "' . $ENV{'ACE_RUN_VX_TGTSRV_WORKINGDIR'} . '"';
249 } else {
250 @cmds[$cmdnr++] = 'cd "' . $ENV{'ACE_RUN_VX_TGTSVR_ROOT'} . "/" . $cwdrel . '"';
252 @cmds[$cmdnr++] = 'ace_vx_rc = vx_execae(ace_main' . $arguments . ')';
253 @cmds[$cmdnr++] = 'unld "'. $program . $PerlACE::ProcessVX::ExeExt . '"';
254 push @cmds, @unload_commands;
255 $cmdnr += scalar @unload_commands;
256 $prompt = '-> $';
259 print $oh "require Net::Telnet;\n";
260 print $oh "my \@cmds;\n";
261 print $oh "my \$prompt = '$prompt';\n";
262 my $i = 0;
263 while($i < $cmdnr) {
264 print $oh "\@cmds[$i] = '" . @cmds[$i++] . "';\n";
266 print $oh "my \$cmdnr = $cmdnr;\n\n";
268 print $oh <<'__END__';
270 my $ok;
271 my $telnet_port = $ENV{'ACE_RUN_VX_TGT_TELNET_PORT'};
272 my $telnet_host = $ENV{'ACE_RUN_VX_TGT_TELNET_HOST'};
273 if (!defined $telnet_host) {
274 $telnet_host = $ENV{'ACE_RUN_VX_TGTHOST'};
276 if (!defined $telnet_port) {
277 $telnet_port = 23;
279 my $t = new Net::Telnet(Timeout => 600, Errmode => 'return', Host => $telnet_host, Port => $telnet_port);
280 if (!defined $t) {
281 die "ERROR: Telnet failed to <" . $telnet_host . ":". $telnet_port . ">";
283 if (!$t->open()) {
284 die "ERROR: Telnet open to <" . $telnet_host . ":". $telnet_port . "> " . $t->errmsg;
286 __END__
288 if (defined $ENV{'ACE_RUN_VX_TERMSERV_DEV'}) {
289 print $oh <<'__END__';
291 $t->prompt('/' . $ENV{'ACE_RUN_VX_IBOOT_USER'} . '>$/');
292 $t->login($ENV{'ACE_RUN_VX_IBOOT_USER'}, $ENV{'ACE_RUN_VX_IBOOT_PASSWORD'});
293 $t->print('direct ' . $ENV{'ACE_RUN_VX_TERMSERV_DEV'});
294 $t->waitfor('/Entering Direct mode/');
295 $t->print('');
297 __END__
300 print $oh <<'__END__';
301 my $target_login = $ENV{'ACE_RUN_VX_LOGIN'};
302 my $target_password = $ENV{'ACE_RUN_VX_PASSWORD'};
304 if (defined $target_login) {
305 $t->waitfor('/VxWorks login: $/');
306 $t->print("$target_login");
309 if (defined $target_password) {
310 $t->waitfor('/Password: $/');
311 $t->print("$target_password");
314 # wait for the prompt
315 my $buf = '';
316 my $prompt1 = '-> $';
317 while (1) {
318 my $blk = $t->get;
319 print $blk;
320 $buf .= $blk;
321 if ($buf =~ /$prompt1/) {
322 last;
325 if ($buf !~ /$prompt1/) {
326 die "ERROR: Didn't got prompt but got <$buf>";
328 my $i = 0;
329 my @lines;
330 while($i < $cmdnr) {
331 if (defined $ENV{'ACE_TEST_VERBOSE'}) {
332 print @cmds[$i]."\n";
334 if ($t->print (@cmds[$i++])) {
335 my $buf = '';
336 while (1) {
337 my $blk = $t->get;
338 print $blk;
339 $buf .= $blk;
340 if ($buf =~ /$prompt/) {
341 last;
344 } else {
345 print $t->errmsg;
348 $t->close();
349 sleep(2);
350 exit;
351 __END__
353 close($oh);
355 Win32::Process::Create ($self->{PROCESS},
356 "$^X",
357 "$^X run_vx.pl",
360 '.');
362 Win32::Process::GetExitCode ($self->{PROCESS}, $status);
364 if ($status != $STILL_ACTIVE) {
365 print STDERR "ERROR: Spawn failed for <", "$^X run_vx.pl", ">\n";
366 exit $status;
369 $self->{RUNNING} = 1;
370 return 0;
373 # Wait for a process to exit with a timeout
375 sub TimedWait ($)
377 my($self) = shift;
378 my($timeout) = shift;
379 return $self->Wait($timeout);
383 # Terminate the process and wait for it to finish
385 sub TerminateWaitKill ($)
387 my $self = shift;
388 my $timeout = shift;
390 if ($self->{RUNNING}) {
391 print STDERR "INFO: $self->{EXECUTABLE} being killed.\n";
392 Win32::Process::Kill ($self->{PROCESS}, 0);
393 $PerlACE::ProcessVX::DoVxInit = 1; # force reboot on next run
396 return $self->WaitKill ($timeout);
399 # Wait until a process exits.
400 # return -1 if the process is still alive.
401 sub Wait ($)
403 my $self = shift;
404 my $timeout = shift;
405 if (!defined $timeout || $timeout < 0) {
406 $timeout = INFINITE;
407 } else {
408 $timeout = $timeout * 1000 * $PerlACE::ProcessVX::WAIT_DELAY_FACTOR;
411 my $result = 0;
413 if ($self->{RUNNING}) {
414 $result = Win32::Process::Wait ($self->{PROCESS}, $timeout);
415 if ($result == 0) {
416 return -1;
419 Win32::Process::GetExitCode ($self->{PROCESS}, $result);
420 $self->{RUNNING} = 0;
421 if ($result != 0) {
422 $PerlACE::ProcessVX::DoVxInit = 1; # force reboot on next run
424 return $result;
429 # Kill the process
431 sub Kill ()
433 my $self = shift;
435 if ($self->{RUNNING}) {
436 Win32::Process::Kill ($self->{PROCESS}, -1);
439 $self->{RUNNING} = 0;