Merge pull request #2240 from DOCGroup/revert-2239-jwi-pi23
[ACE_TAO.git] / ACE / bin / PerlACE / Process_VMS.pm
blobd7ad0ac64ea87ac19ca31b626a8aef6664091884
1 #!/usr/bin/env perl
3 package PerlACE::Process;
5 use strict;
6 use POSIX "sys_wait_h";
7 use Cwd;
8 use File::Basename;
9 use Config;
10 use VmsProcess;
12 ###############################################################################
14 ###############################################################################
16 ### Grab signal names
18 my @signame;
20 if (defined $Config{sig_name}) {
21 my $i = 0;
22 foreach my $name (split (' ', $Config{sig_name})) {
23 $signame[$i] = $name;
24 $i++;
27 else {
28 my $i;
29 for ($i = 0; $i < 255; ++$i) {
30 $signame[$i] = $i;
34 ###############################################################################
36 ### Constructor and Destructor
38 sub new
40 my $proto = shift;
41 my $class = ref ($proto) || $proto;
42 my $self = {};
44 $self->{RUNNING} = 0;
45 $self->{IGNOREEXESUBDIR} = 0;
46 $self->{PROCESS} = undef;
47 $self->{EXECUTABLE} = shift;
48 $self->{ARGUMENTS} = shift;
49 $self->{VALGRIND_CMD} = $ENV{'ACE_RUN_VALGRIND_CMD'};
51 if (!defined $PerlACE::Process::WAIT_DELAY_FACTOR) {
52 if (defined $self->{PURIFY_CMD}) {
53 $PerlACE::Process::WAIT_DELAY_FACTOR = 10;
55 elsif (defined $self->{VALGRIND_CMD}) {
56 $PerlACE::Process::WAIT_DELAY_FACTOR = 5;
58 else {
59 $PerlACE::Process::WAIT_DELAY_FACTOR = 1;
63 bless ($self, $class);
64 return $self;
67 sub DESTROY
69 my $self = shift;
71 if ($self->{RUNNING} == 1) {
72 print STDERR "ERROR: <", $self->{EXECUTABLE},
73 "> still running upon object destruction\n";
74 $self->Kill ();
78 ###############################################################################
80 ### Some Accessors
82 sub Executable
84 my $self = shift;
86 if (@_ != 0) {
87 $self->{EXECUTABLE} = shift;
90 my $executable = $self->{EXECUTABLE};
92 if ($self->{IGNOREEXESUBDIR}) {
93 return $executable;
96 my $basename = basename ($executable);
97 my $dirname = dirname ($executable). '/';
98 if ($dirname != "") {
99 $executable = $dirname.$PerlACE::Process::ExeSubDir.$basename;
101 else {
102 $executable = $dirname.$basename;
105 if ( !-x $executable ) {
106 if ( -x $executable.'.exe' ) {
107 $executable = $executable.'.exe';
111 return $executable;
114 sub Arguments
116 my $self = shift;
118 if (@_ != 0) {
119 $self->{ARGUMENTS} = shift;
122 return $self->{ARGUMENTS};
125 sub CommandLine ()
127 my $self = shift;
129 my $commandline = $self->Executable ();
131 if (defined $self->{ARGUMENTS}) {
132 $commandline .= ' '.$self->{ARGUMENTS};
135 return $commandline;
138 sub IgnoreExeSubDir
140 my $self = shift;
142 if (@_ != 0) {
143 $self->{IGNOREEXESUBDIR} = shift;
146 return $self->{IGNOREEXESUBDIR};
149 sub IgnoreHostRoot
151 my $self = shift;
153 if (@_ != 0) {
154 $self->{IGNOREHOSTROOT} = shift;
157 return $self->{IGNOREHOSTROOT};
160 ###############################################################################
162 # Spawn the process and continue;
164 sub Spawn ()
166 my $self = shift;
168 if ($self->{RUNNING} == 1) {
169 print STDERR "ERROR: Cannot Spawn: <", $self->Executable (),
170 "> already running\n";
171 return -1;
174 if (!defined $self->{EXECUTABLE}) {
175 print STDERR "ERROR: Cannot Spawn: No executable specified\n";
176 return -1;
179 if ($self->{IGNOREEXESUBDIR} == 0) {
180 if (!-f $self->Executable ()) {
181 print STDERR "ERROR: Cannot Spawn: <", $self->Executable (),
182 "> not found\n";
183 return -1;
186 if (!-x $self->Executable ()) {
187 print STDERR "ERROR: Cannot Spawn: <", $self->Executable (),
188 "> not executable\n";
189 return -1;
193 $self->{PROCESS} = VmsProcess::Spawn $self->{EXECUTABLE}, $self->{ARGUMENTS};
194 if ($self->{PROCESS}) {
195 #parent here
196 bless $self;
198 else {
199 # weird fork error
200 print STDERR "ERROR: Can't spawn <" . $self->CommandLine () . ">: $!\n";
202 $self->{RUNNING} = 1;
203 return 0;
206 # The second argument is an optional output argument that, if present,
207 # will be passed to check_return_value function to get the signal number
208 # the process has received, if any, and/or whether there was a core dump.
209 sub WaitKill ($;$)
211 my $self = shift;
212 my $timeout = shift;
213 my $opts = shift;
215 my $status = $self->TimedWait ($timeout, $opts);
217 if ($status == -1) {
218 print STDERR "ERROR: $self->{EXECUTABLE} timedout\n";
219 $self->Kill ();
222 $self->{RUNNING} = 0;
224 return $status;
228 # Do a Spawn and immediately WaitKill
230 sub SpawnWaitKill ($)
232 my $self = shift;
233 my $timeout = shift;
235 if ($self->Spawn () == -1) {
236 return -1;
239 return $self->WaitKill ($timeout);
242 sub TerminateWaitKill ($)
244 my $self = shift;
245 my $timeout = shift;
247 if ($self->{RUNNING}) {
248 print STDERR "INFO: $self->{EXECUTABLE} being killed.\n";
249 kill ('TERM', $self->{PROCESS});
252 return $self->WaitKill ($timeout);
255 # Really only for internal use.
256 # The second optional argument indicates whether the corresponding process
257 # may deliberately send a signal to itself or not. It also contains output
258 # data indicating whether there was a core dump and/or the signal nubmer
259 # the process has died from, if any.
260 sub check_return_value ($)
262 my $self = shift;
263 my $rc = shift;
264 my $opts = shift // {};
266 if ($rc == 0) {
267 return 0;
269 elsif ($rc == 0xff00) {
270 print STDERR "ERROR: <", $self->{EXECUTABLE},
271 "> failed: $!\n";
272 return ($rc >> 8);
274 elsif (($rc & 0xff) == 0) {
275 return ($rc >> 8);
278 my $dump = 0;
280 if ($rc & 0x80) {
281 $rc &= ~0x80;
282 $dump = 1;
285 my $self_crash = $opts->{self_crash};
287 # ABRT, KILL or TERM can be sent deliberately
288 if ($self_crash && ($rc == 6 || $rc == 9 || $rc == 15)) {
289 return 0;
292 my $signal_ref = $opts->{signal_ref};
293 if (defined $signal_ref) {
294 ${$signal_ref} = $rc;
297 my $dump_ref = $opts->{dump_ref};
298 if (defined $dump_ref) {
299 ${$dump_ref} = $dump;
302 print STDERR "ERROR: <", $self->{EXECUTABLE},
303 "> exited with ";
305 print STDERR "coredump from " if ($dump == 1);
307 print STDERR "signal $rc : ", $signame[$rc], "\n";
309 return 255;
312 sub Kill ()
314 my $self = shift;
316 if ($self->{RUNNING}) {
317 kill ('KILL', $self->{PROCESS});
318 waitpid ($self->{PROCESS}, 0);
319 $self->check_return_value ($?, {self_crash => 1});
322 $self->{RUNNING} = 0;
325 # Wait until a process exits.
326 # return -1 if the process is still alive.
327 sub Wait ($)
329 my $self = shift;
330 my $timeout = shift;
331 if (!defined $timeout || $timeout < 0) {
332 waitpid ($self->{PROCESS}, 0);
333 } else {
334 return TimedWait($self, $timeout);
339 # The second argument is an optional output argument that, if present,
340 # will contain the signal number that the process has received, if any,
341 # and/or whether there was a core dump.
342 sub TimedWait ($;$)
344 my $self = shift;
345 my $timeout = shift;
346 my $opts = shift;
348 $timeout *= $PerlACE::Process::WAIT_DELAY_FACTOR;
350 my $status;
351 my $pid = VmsProcess::TimedWaitPid ($self->{PROCESS}, $timeout, $status);
352 if ($pid > 0) {
353 return $self->check_return_value ($status, $opts);
355 return -1;
360 sub kill_all
362 my $procmask = shift;
363 my $target = shift;
364 ## NOT IMPLEMENTED YET