#! /bin/sh # This is the LHEA perl script: /cvmfs/extras-fp7.egi.eu/extras/heasoft/attitude/x86_64-unknown-linux-gnu-libc2.19-0/bin/rewaiter # The purpose of this special block is to make this script work with # the user's local perl, regardless of where that perl is installed. # The variable LHEAPERL is set by the initialization script to # point to the local perl installation. #------------------------------------------------------------------------------- eval ' if [ "x$LHEAPERL" = x ]; then echo "Please run standard LHEA initialization before attempting to run /cvmfs/extras-fp7.egi.eu/extras/heasoft/attitude/x86_64-unknown-linux-gnu-libc2.19-0/bin/rewaiter." exit 3 elif [ "$LHEAPERL" = noperl ]; then echo "During LHEA initialization, no acceptable version of Perl was found." echo "Cannot execute script /cvmfs/extras-fp7.egi.eu/extras/heasoft/attitude/x86_64-unknown-linux-gnu-libc2.19-0/bin/rewaiter." exit 3 elif [ `$LHEAPERL -v < /dev/null 2> /dev/null | grep -ic "perl"` -eq 0 ]; then echo "LHEAPERL variable does not point to a usable perl." exit 3 else # Force Perl into 32-bit mode (to match the binaries) if necessary: if [ "x$HD_BUILD_ARCH_32_BIT" = xyes ]; then if [ `$LHEAPERL -V 2> /dev/null | grep -ic "USE_64_BIT"` -ne 0 ]; then VERSIONER_PERL_PREFER_32_BIT=yes export VERSIONER_PERL_PREFER_32_BIT fi fi exec $LHEAPERL -x $0 ${1+"$@"} fi ' if(0); # Do not delete anything above this comment from an installed LHEA script! #------------------------------------------------------------------------------- #!/usr/bin/perl # # $Source: /headas/headas/attitude/lib/rew/rewaiter,v $ # $Revision: 1.3 $ # $Date: 2013/11/20 14:58:13 $ # # $Log: rewaiter,v $ # Revision 1.3 2013/11/20 14:58:13 rwiegand # Terminate sub-processes with SIGKILL. # # Revision 1.2 2011/11/07 15:55:48 rwiegand # Fixed shutdown when secondary is successful. # # Revision 1.1 2004/06/17 21:44:48 rwiegand # Promoted waiter so it is available outside GCN. use strict; use Getopt::Long; use IO::Select; use constant COMPLETED => 0; use constant BAD_USAGE => 1; use constant TIMED_OUT => 2; use constant FAILURE => 3; { my $primary = ''; my $secondary = ''; my $delta = 3; my $limit = 100; my @kids; my $finish = sub { my ($code, $text) = @_; print "debug: interrupting @kids\n"; if (@kids) { my $count = kill('KILL', @kids); print "debug: signalled $count sub-processes\n"; print "result: $text\n"; } exit($code); }; if (not Getopt::Long::GetOptions( 'primary=s' => \$primary, 'secondary=s' => \$secondary, 'delta=i' => \$delta, 'limit=i' => \$limit, )) { $finish->(BAD_USAGE, 'unable to parse options'); } if (not $primary or not $secondary) { $finish->(BAD_USAGE, 'missing primary/secondary command(s)'); } my $start = time; if (my $pid = open(PRIMARY, "$primary|")) { # in parent print "started $primary [pid=$pid]\n"; push(@kids, $pid); } else { $finish->(FAILURE, "unable to start primary [$!]"); } if (my $pid = open(SECONDARY, "$secondary|")) { # in parent print "started $secondary [pid=$pid]\n"; push(@kids, $pid); } else { $finish->(FAILURE, "unable to start secondary [$!]"); } my @handles = (\*PRIMARY, \*SECONDARY); my $selector = IO::Select->new(@handles); my $secondaryGood = 0; while ($selector->count > 0) { my $elapsed = time - $start; if ($elapsed > $limit) { if ($secondaryGood) { $finish->(COMPLETED, 'secondary completed'); } else { $finish->(TIMED_OUT, 'timed out'); } } # this could be tightened my @ready = $selector->can_read($delta); foreach my $handle (@ready) { my $tag = 'invalid'; if ($handle == \*PRIMARY) { $tag = 'primary'; } elsif ($handle == \*SECONDARY) { $tag = 'secondary'; } else { print "uhoh: bogus handle $handle\n"; } my $text = <$handle>; if (not defined($text)) { $selector->remove($handle); my $close = close($handle) ? 0 : $!; my $os = $?; my $bad = $os || $close; my $word = $bad ? 'bad' : 'good'; my $code = $os >> 8; my $signal = $? & 0xff; print "$tag: $word: code=$code signal=$signal os=$os close=$close\n"; if (not $close) { if (not $os) { if ($tag eq 'primary') { $finish->(COMPLETED, 'primary completed'); } else { $secondaryGood = 1; } } } else { print "uhoh: unable to close $tag [$!]\n"; } } else { $text =~ s/\s+$//; print "$tag: $text\n"; } } } if ($secondaryGood) { $finish->(COMPLETED, 'secondary completed'); } $finish->(FAILURE, 'tasks failed'); }