3 package PerlACE
::ProcessVX
;
6 use POSIX
"sys_wait_h";
13 eval { require Net
::Telnet
; };
15 ###############################################################################
21 if (defined $Config{sig_name
}) {
23 foreach my $name (split (' ', $Config{sig_name
})) {
30 for ($i = 0; $i < 255; ++$i) {
35 ###############################################################################
37 # This is what GetExitCode will return if the process is still running.
38 my $STILL_ACTIVE = 259;
40 ###############################################################################
42 ### Constructor and Destructor
47 my $class = ref ($proto) || $proto;
51 $self->{IGNOREEXESUBDIR
} = 1;
52 $self->{IGNOREHOSTROOT
} = 0;
53 $self->{PROCESS
} = undef;
54 $self->{EXECUTABLE
} = shift;
55 $self->{ARGUMENTS
} = shift;
56 if (!defined $PerlACE::ProcessVX
::WAIT_DELAY_FACTOR
) {
57 $PerlACE::ProcessVX
::WAIT_DELAY_FACTOR
= 2;
59 if (!defined $PerlACE::ProcessVX
::RebootCmd
) {
60 $PerlACE::ProcessVX
::RebootCmd
= "reboot 0x02";
62 bless ($self, $class);
70 if ($self->{RUNNING
} == 1) {
71 print STDERR
"ERROR: <", $self->{EXECUTABLE
},
72 "> still running upon object destruction\n";
76 if (defined $ENV{'ACE_RUN_VX_IBOOT'} && !defined $ENV{'ACE_RUN_VX_NO_SHUTDOWN'}) {
77 # Shutdown the target to save power
78 $self->iboot_cycle_power(1);
82 ###############################################################################
84 # Use the "expect" program to invoke telnet, doesn't need Perl's Net::Telnet.
85 # This is run by the child process which was forked from Spawn().
88 my($host, $port, $prompt, $cmdsRef) = @_;
89 my $pid = open(EXP
, "|expect -f -") or die "ERROR: Could not run 'expect'";
90 $SIG{'TERM'} = sub { # If the parent wants to Kill() this process,
91 kill 'TERM', $pid; # send a SIGTERM to the expect process and
92 $SIG{'TERM'} = 'DEFAULT'; # then go back to the normal handler for TERM
93 kill 'TERM', $$; # and invoke it.
97 spawn telnet $host $port
100 # target login and password are not currently implemented
101 for my $cmd (@
$cmdsRef) {
103 $cmdEsc =~ s/\"/\\\"/g; # escape quotes
111 expect -re "Au revoir!"
119 # Spawn the process and continue.
125 if ($self->{RUNNING
} == 1) {
126 print STDERR
"ERROR: Cannot Spawn: <", $self->Executable (),
127 "> already running\n";
131 if (!defined $self->{EXECUTABLE
}) {
132 print STDERR
"ERROR: Cannot Spawn: No executable specified\n";
136 if ($self->{IGNOREEXESUBDIR
} == 0) {
137 if (!-f
$self->Executable ()) {
138 print STDERR
"ERROR: Cannot Spawn: <", $self->Executable (),
148 # Reboot the target if necessery
151 my $program = $self->Executable ();
152 my $exe_cwdrel = dirname
($program);
153 my $prjroot = defined $ENV{'ACE_RUN_VX_PRJ_ROOT'} ?
$ENV{'ACE_RUN_VX_PRJ_ROOT'} : $ENV{'ACE_ROOT'};
154 $exe_cwdrel = cwd
() if length ($exe_cwdrel) == 0;
155 $exe_cwdrel = File
::Spec
->abs2rel($exe_cwdrel, $prjroot);
156 my $cwdrel = File
::Spec
->abs2rel(cwd
(), $prjroot);
157 $program = basename
($program, $PerlACE::ProcessVX
::ExeExt
);
163 my $exesubdir = defined $ENV{'ACE_RUN_VX_EXE_SUBDIR'} ?
$ENV{'ACE_RUN_VX_EXE_SUBDIR'} : "";
165 if (defined $ENV{'ACE_RUN_VX_STARTUP_SCRIPT'}) {
166 if (defined $ENV{'ACE_RUN_VX_STARTUP_SCRIPT_ROOT'}) {
167 $cmds[$cmdnr++] = 'cd "' . $ENV{'ACE_RUN_VX_STARTUP_SCRIPT_ROOT'} . '"';
169 $cmds[$cmdnr++] = '< ' . $ENV{'ACE_RUN_VX_STARTUP_SCRIPT'};
172 if (defined $ENV{'ACE_RUN_VX_STARTUP_COMMAND'}) {
173 $cmds[$cmdnr++] = $ENV{'ACE_RUN_VX_STARTUP_COMMAND'};
176 if ($PerlACE::VxWorks_RTP_Test
) {
177 $cmds[$cmdnr++] = 'cmd';
178 if ( defined $ENV{'ACE_RUN_VX_TGTSVR_DEFGW'} && $self->{SET_VX_DEFGW
}) {
179 $cmds[$cmdnr++] = "C mRouteAdd(\"0.0.0.0\", \"" . $ENV{'ACE_RUN_VX_TGTSVR_DEFGW'} . "\", 0,0,0)";
180 $PerlACE::ProcessVX
::VxDefGw
= 0;
183 if (defined $ENV{'ACE_RUN_VX_TGT_STARTUP_SCRIPT'}) {
185 if (handle_startup_script
($ENV{'ACE_RUN_VX_TGT_STARTUP_SCRIPT'}, \
@start_commands)) {
186 push @cmds, @start_commands;
187 $cmdnr += scalar @start_commands;
191 $cmds[$cmdnr++] = 'cd "' . $ENV{'ACE_RUN_VX_TGTSVR_ROOT'} . "/" . $cwdrel . '"';
192 $cmds[$cmdnr++] = 'C putenv("TMPDIR=' . $ENV{'ACE_RUN_VX_TGTSVR_ROOT'} . "/" . $cwdrel . '")';
194 if (defined $ENV{'ACE_RUN_ACE_DEBUG'}) {
195 $cmds[$cmdnr++] = 'C putenv("ACE_DEBUG=' . $ENV{'ACE_RUN_ACE_DEBUG'} . '")';
198 if (defined $ENV{'ACE_RUN_TAO_ORB_DEBUG'}) {
199 $cmds[$cmdnr++] = 'C putenv("TAO_ORB_DEBUG=' . $ENV{'ACE_RUN_TAO_ORB_DEBUG'} . '")';
202 if (defined $ENV{'ACE_RUN_ACE_LD_SEARCH_PATH'}) {
203 $cmds[$cmdnr++] = 'C putenv("ACE_LD_SEARCH_PATH=' . $ENV{'ACE_RUN_ACE_LD_SEARCH_PATH'} . '")';
205 if (defined $self->{TARGET
}) {
206 my $x_env_ref = $self->{TARGET
}->{EXTRA_ENV
};
207 while ( my ($env_key, $env_value) = each(%$x_env_ref) ) {
208 if (defined $ENV{'ACE_TEST_VERBOSE'}) {
209 print "INFO: adding target environment $env_key=$env_value\n";
211 $cmds[$cmdnr++] = 'C putenv("' . $env_key. '=' . $env_value . '")';
215 if (defined $ENV{'ACE_RUN_VX_CHECK_RESOURCES'}) {
216 $cmds[$cmdnr++] = 'C memShow()';
219 $cmdline = $program . $PerlACE::ProcessVX
::ExeExt
. ' ' . $self->{ARGUMENTS
};
220 $cmds[$cmdnr++] = $cmdline;
221 $prompt = '\[vxWorks \*\]\# $';
223 if ($PerlACE::VxWorks_Test
) {
224 if ( defined $ENV{'ACE_RUN_VX_TGTSVR_DEFGW'} && $PerlACE::ProcessVX
::VxDefGw
) {
225 $cmds[$cmdnr++] = "mRouteAdd(\"0.0.0.0\", \"" . $ENV{'ACE_RUN_VX_TGTSVR_DEFGW'} . "\", 0,0,0)";
226 $PerlACE::ProcessVX
::VxDefGw
= 0;
229 if (defined $ENV{'ACE_RUN_VX_TGT_STARTUP_SCRIPT'}) {
231 if (handle_startup_script
($ENV{'ACE_RUN_VX_TGT_STARTUP_SCRIPT'}, \
@start_commands)) {
232 push @cmds, @start_commands;
233 $cmdnr += scalar @start_commands;
238 my(@unload_commands);
239 if (!$PerlACE::Static
&& !$PerlACE::VxWorks_RTP_Test
) {
240 my $vxtest_file = $program . '.vxtest';
241 if (handle_vxtest_file
($self, $vxtest_file, \
@load_commands, \
@unload_commands)) {
242 $cmds[$cmdnr++] = "cd \"$ENV{'ACE_RUN_VX_TGTSVR_ROOT'}/lib\"";
243 push @cmds, @load_commands;
244 $cmdnr += scalar @load_commands;
246 print STDERR
"ERROR: Cannot find <", $vxtest_file, ">\n";
251 $cmds[$cmdnr++] = 'cd "' . $ENV{'ACE_RUN_VX_TGTSVR_ROOT'} . "/" . $exe_cwdrel . "/" . $exesubdir . '"';
252 $cmds[$cmdnr++] = 'putenv("TMPDIR=' . $ENV{'ACE_RUN_VX_TGTSVR_ROOT'} . "/" . $cwdrel . '")';
254 if (defined $ENV{'ACE_RUN_VX_CHECK_RESOURCES'}) {
255 $cmds[$cmdnr++] = 'memShow()';
258 if (defined $ENV{'ACE_RUN_ACE_DEBUG'}) {
259 $cmds[$cmdnr++] = 'putenv("ACE_DEBUG=' . $ENV{'ACE_RUN_ACE_DEBUG'} . '")';
262 if (defined $ENV{'ACE_RUN_TAO_ORB_DEBUG'}) {
263 $cmds[$cmdnr++] = 'putenv("TAO_ORB_DEBUG=' . $ENV{'ACE_RUN_TAO_ORB_DEBUG'} . '")';
266 if (defined $ENV{'ACE_RUN_ACE_LD_SEARCH_PATH'}) {
267 $cmds[$cmdnr++] = 'putenv("ACE_LD_SEARCH_PATH=' . $ENV{'ACE_RUN_ACE_LD_SEARCH_PATH'} . '")';
269 if (defined $self->{TARGET
}) {
270 my $x_env_ref = $self->{TARGET
}->{EXTRA_ENV
};
271 while ( my ($env_key, $env_value) = each(%$x_env_ref) ) {
272 if (defined $ENV{'ACE_TEST_VERBOSE'}) {
273 print "INFO: adding target environment $env_key=$env_value\n";
275 $cmds[$cmdnr++] = 'putenv("' . $env_key. '=' . $env_value . '")';
279 $cmds[$cmdnr++] = 'ld <'. $program . $PerlACE::ProcessVX
::ExeExt
;
280 $cmdline = $program . $PerlACE::ProcessVX
::ExeExt
. ' ' . $self->{ARGUMENTS
};
281 if (defined $self->{ARGUMENTS
}) {
282 ($arguments = $self->{ARGUMENTS
})=~ s/\"/\\\"/g;
283 ($arguments = $self->{ARGUMENTS
})=~ s/\'/\\\'/g;
284 $arguments = ",\"" . $arguments . "\"";
286 if (defined $ENV{'ACE_RUN_VX_TGTSRV_WORKINGDIR'}) {
287 $cmds[$cmdnr++] = 'cd "' . $ENV{'ACE_RUN_VX_TGTSRV_WORKINGDIR'} . '"';
289 $cmds[$cmdnr++] = 'cd "' . $ENV{'ACE_RUN_VX_TGTSVR_ROOT'} . "/" . $cwdrel . '"';
291 $cmds[$cmdnr++] = 'ace_vx_rc = vx_execae(ace_main' . $arguments . ')';
292 $cmds[$cmdnr++] = 'unld "'. $program . $PerlACE::ProcessVX
::ExeExt
. '"';
293 push @cmds, @unload_commands;
294 $cmdnr += scalar @unload_commands;
299 if ($self->{PROCESS
} = fork) {
303 elsif (defined $self->{PROCESS
}) {
305 my $telnet_port = $ENV{'ACE_RUN_VX_TGT_TELNET_PORT'};
306 my $telnet_host = $ENV{'ACE_RUN_VX_TGT_TELNET_HOST'};
307 if (!defined $telnet_host) {
308 $telnet_host = $ENV{'ACE_RUN_VX_TGTHOST'};
310 if (!defined $telnet_port) {
313 if (defined $ENV{'ACE_RUN_VX_USE_EXPECT'}) {
314 expect_telnet
($telnet_host, $telnet_port, $prompt, \
@cmds);
318 if (defined $ENV{'ACE_TEST_VERBOSE'}) {
319 print "Opening telnet connection <" . $telnet_host . ":". $telnet_port . ">\n";
321 my $t = new Net
::Telnet
(Timeout
=> 600, Errmode
=> 'return', Host
=> $telnet_host, Port
=> $telnet_port);
323 die "ERROR: Telnet failed to <" . $telnet_host . ":". $telnet_port . ">";
328 if (defined $ENV{'ACE_TEST_VERBOSE'}) {
329 print "Couldn't open telnet connection; sleeping then retrying\n";
332 die "ERROR: Telnet open to <" . $telnet_host . ":". $telnet_port . "> " . $t->errmsg;
340 my $target_login = $ENV{'ACE_RUN_VX_LOGIN'};
341 my $target_password = $ENV{'ACE_RUN_VX_PASSWORD'};
343 if (defined $target_login) {
344 $t->waitfor('/VxWorks login: $/');
345 $t->print("$target_login");
348 if (defined $target_password) {
349 $t->waitfor('/Password: $/');
350 $t->print("$target_password");
354 # wait for the prompt
355 my $prompt1 = '->[\ ]$';
360 if ($buf =~ /$prompt1/) {
364 if ($buf !~ /$prompt1/) {
365 die "ERROR: Didn't got prompt but got <$buf>";
370 if (defined $ENV{'ACE_TEST_VERBOSE'}) {
371 print $cmds[$i]."\n";
373 if ($t->print ($cmds[$i++])) {
374 # After each command wait for the prompt
380 if ($buf =~ /$prompt/) {
392 elsif ($! =~ /No more process/) {
393 #EAGAIN, supposedly recoverable fork error
399 print STDERR
"ERROR: Can't fork <" . $cmdline . ">: $!\n";
402 $self->{RUNNING
} = 1;
407 # Terminate the process and wait for it to finish
409 sub TerminateWaitKill
($)
414 if ($self->{RUNNING
}) {
415 print STDERR
"INFO: $self->{EXECUTABLE} being killed.\n";
416 kill ('TERM', $self->{PROCESS
});
418 $PerlACE::ProcessVX
::DoVxInit
= 1; # force reboot on next run
421 return $self->WaitKill ($timeout);
424 # really only for internal use
425 sub check_return_value
($)
430 my $CC_MASK = 0xff00;
432 # Exit code processing
436 elsif ($rc == $CC_MASK) {
437 print STDERR
"ERROR: <", $self->{EXECUTABLE
},
440 $PerlACE::ProcessVX
::DoVxInit
= 1; # force reboot on next run
444 elsif (($rc & 0xff) == 0) {
449 # Remember Core dump flag
457 # check for ABRT, KILL or TERM
458 if ($rc == 6 || $rc == 9 || $rc == 15) {
462 print STDERR
"ERROR: <", $self->{EXECUTABLE
},
465 print STDERR
"coredump from " if ($dump == 1);
467 print STDERR
"signal $rc : ", $signame[$rc], "\n";
469 $PerlACE::ProcessVX
::DoVxInit
= 1; # force reboot on next run
478 if ($self->{RUNNING
} && !defined $ENV{'ACE_TEST_WINDOW'}) {
479 kill ((defined $ENV{'ACE_RUN_VX_USE_EXPECT'}) ?
'TERM' : 'KILL',
481 waitpid ($self->{PROCESS
}, 0);
482 $self->check_return_value ($?
);
485 $self->{RUNNING
} = 0;
488 # Wait until a process exits.
489 # return -1 if the process is still alive.
494 if (!defined $timeout || $timeout < 0) {
495 waitpid ($self->{PROCESS
}, 0);
497 return TimedWait
($self, $timeout);
507 if ($PerlACE::Process
::WAIT_DELAY_FACTOR
> 0) {
508 $timeout *= $PerlACE::Process
::WAIT_DELAY_FACTOR
;
511 # Multiply with 10 because we wait a tenth of a second each time
514 while ($timeout-- != 0) {
515 my $pid = waitpid ($self->{PROCESS
}, &WNOHANG
);
516 if ($pid != 0 && $?
!= -1) {
517 return $self->check_return_value ($?
);
519 select(undef, undef, undef, 0.1);
522 $PerlACE::ProcessVX
::DoVxInit
= 1; # force reboot on next run