spewlogfile.in 2.34 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
#!/usr/bin/perl -wT
use English;
use Getopt::Std;

#
# Spew the current log file for an experiment to stdout. This is for
# use by the web interface, so it can send the lofgile to the user in
# a web page.
#
# The wrinkle is that the logfile only exists while the experiment is
# in transition, and we have to quit when the experiment is no longer in
# transition so that the web page can finish.
#

sub usage()
{
    print STDOUT "Usage: spewlogfile <pid> <eid>\n".
	"Spew the logfile for an experiment.\n";
    exit(-1);
}
my  $optlist = "";
  
#
# Configure variables
#
my $TB		= "@prefix@";
my $TBOPS       = "@TBOPSEMAIL@";
my $TBLOGS      = "@TBLOGSEMAIL@";

my $logname;

#
# Load the Testbed support stuff. 
#
use lib "@prefix@/lib";
use libdb;
use libtestbed;

# un-taint path
$ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

# Turn off line buffering on output
$| = 1; 

#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (@ARGV != 2) {
    usage();
}
my $pid   = $ARGV[0];
my $eid   = $ARGV[1];

#
# Untaint the arguments.
#
if ($pid =~ /^([-\@\w]+)$/) {
    $pid = $1;
}
else {
    die("*** Bad data in pid: $pid\n");
}	
if ($eid =~ /^([-\@\w]+)$/) {
    $eid = $1;
}
else {
    die("*** Bad data in eid: $eid\n");
}	

#
# Verify that this person is allowed to do this. 
#
if (!TBExptAccessCheck($UID, $pid, $eid, TB_EXPT_READINFO)) {
    die("*** $0:\n".
	"    You do not have permission to view log files for $pid/$eid!\n");
}

#
# Get the logfile name.
#
if (! TBExptGetLogFile($pid, $eid, \$logname)) {
    die("*** $0:\n".
	"    There is no logfile to view for $pid/$eid!\n");
}

use Fcntl;
use IO::Handle;
STDOUT->autoflush(1);

sysopen(LOG, $logname, O_RDONLY | O_NONBLOCK) or
    die("*** $0:\n".
	"    Could not open $logname. Must no longer be in transition!");

#
# Loop reading the file in nonblocking mode. Sleep between loops, and
# check for a change in status. For now, this is simply accomplished
# by checking to make sure the name of the logfile has not changed!
#
while (1) {
    my $tmp;
    
    while (sysread(LOG, $buf, 2048)) {
	print STDOUT "$buf";
    }
    if (! TBExptGetLogFile($pid, $eid, \$tmp) || $tmp ne $logname) {
	last;
    }
114
    sleep(2);
115 116 117 118
}
close(LOG);
exit(0);