parse-ns.in 1.8 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
#!/usr/bin/perl -w
use English;
use Getopt::Std;
use BSD::Resource;
use POSIX qw(:signal_h);
    
#
# Simply a wrapper for the parser. Passes on its arguments to parse.tcl.
#
sub usage()
{
    print STDOUT
	"Usage: parse-ns [args ...]\n".
        "Where options and arguments are those required by parse.tcl\n";
 
    exit(-1);
}

#
# Configure variables
#
my $TB       = "@prefix@";
my $TBOPS    = "@TBOPSEMAIL@";

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

#
# Untaint the path
# 
Leigh B. Stoller's avatar
Leigh B. Stoller committed
33
$ENV{'PATH'} = "/bin:/usr/bin:$TB/libexec:$TB/lib/ns2ir".
34 35 36 37 38 39 40
    ":$TB/libexec/ns2ir:$TB/sbin:$TB/bin";
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
41
use libdb;
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
use libtestbed;

#
# Fork a child process to run the parser in.
#
my $pid = fork();

if (!defined($pid)) {
    die("*** $0:\n".
	"    Could not fork a new process!");
}

#
# Child runs the parser, niced down, and then exits.
#
if (! $pid) {
    # Set the CPU limit for us.
    setrlimit(RLIMIT_CPU, 60, 60);
    
    # Give parent a chance to react.
    sleep(1);

    system("nice parse.tcl @ARGV");
65
    exit($? >> 8);
66 67 68 69 70 71 72 73 74 75 76 77 78
}

#
# Parent waits.
#
waitpid($pid, 0);

#
# If the child was KILLed, then it overran its time limit.
# Send email. Otherwise, exit with result of child.
#
if (($? >> 8) == SIGKILL) {
    my $msg = "Overran its CPU limit on 'parse.tcl @ARGV'";
79 80 81 82
    my $user_name;
    my $user_email;
    my $dbuid;
    my $to;
83

84 85 86 87 88 89 90 91 92 93 94 95 96
    #
    # Get email info for user.
    #
    if (UNIX2DBUID($UID, \$dbuid) &&
	UserDBInfo($dbuid, \$user_name, \$user_email)) {
	$to = "$user_name <$user_email>";
    }
    else {
	$to = $TBOPS;
    }

    SENDMAIL($to, "Parser Exceeded CPU Limit", $msg,
	     undef, "CC: $TBOPS", ($ARGV[$#ARGV]));
97 98 99 100
    print STDERR "Parser Exceeded CPU Limit: 'parse.tcl @ARGV'\n";
    exit(15);
}
exit($? >> 8);