Perl shebang portability changes
[ACE_TAO.git] / ACE / bin / PerlACE / Process_VMS.pm
blob6c89c3f9e9bdd23251abbeac32116e3090472da3
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 sub WaitKill ($)
208 my $self = shift;
209 my $timeout = shift;
210 my $status = $self->TimedWait ($timeout);
212 if ($status == -1) {
213 print STDERR "ERROR: $self->{EXECUTABLE} timedout\n";
214 $self->Kill ();
217 $self->{RUNNING} = 0;
219 return $status;
223 # Do a Spawn and immediately WaitKill
225 sub SpawnWaitKill ($)
227 my $self = shift;
228 my $timeout = shift;
230 if ($self->Spawn () == -1) {
231 return -1;
234 return $self->WaitKill ($timeout);
237 sub TerminateWaitKill ($)
239 my $self = shift;
240 my $timeout = shift;
242 if ($self->{RUNNING}) {
243 print STDERR "INFO: $self->{EXECUTABLE} being killed.\n";
244 kill ('TERM', $self->{PROCESS});
247 return $self->WaitKill ($timeout);
250 # really only for internal use
251 sub check_return_value ($)
253 my $self = shift;
254 my $rc = shift;
256 if ($rc == 0) {
257 return 0;
259 elsif ($rc == 0xff00) {
260 print STDERR "ERROR: <", $self->{EXECUTABLE},
261 "> failed: $!\n";
262 return ($rc >> 8);
264 elsif (($rc & 0xff) == 0) {
265 $rc >>= 8;
266 return $rc;
269 my $dump = 0;
271 if ($rc & 0x80) {
272 $rc &= ~0x80;
273 $dump = 1;
276 # check for ABRT, KILL or TERM
277 if ($rc == 6 || $rc == 9 || $rc == 15) {
278 return 0;
281 print STDERR "ERROR: <", $self->{EXECUTABLE},
282 "> exited with ";
284 print STDERR "coredump from " if ($dump == 1);
286 print STDERR "signal $rc : ", $signame[$rc], "\n";
288 return 0;
291 sub Kill ()
293 my $self = shift;
295 if ($self->{RUNNING}) {
296 kill ('KILL', $self->{PROCESS});
297 waitpid ($self->{PROCESS}, 0);
298 $self->check_return_value ($?);
301 $self->{RUNNING} = 0;
304 # Wait until a process exits.
305 # return -1 if the process is still alive.
306 sub Wait ($)
308 my $self = shift;
309 my $timeout = shift;
310 if (!defined $timeout || $timeout < 0) {
311 waitpid ($self->{PROCESS}, 0);
312 } else {
313 return TimedWait($self, $timeout);
318 sub TimedWait ($)
320 my $self = shift;
321 my $timeout = shift;
323 $timeout *= $PerlACE::Process::WAIT_DELAY_FACTOR;
325 my $status;
326 my $pid = VmsProcess::TimedWaitPid ($self->{PROCESS}, $timeout, $status);
327 if ($pid > 0) {
328 return $self->check_return_value ($status);
330 return -1;
335 sub kill_all
337 my $procmask = shift;
338 my $target = shift;
339 ## NOT IMPLEMENTED YET