rungenilib.proxy.in 5.53 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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
#!/usr/bin/perl -w

#
# Copyright (c) 2000-2014 University of Utah and the Flux Group.
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
#

use English;
use Getopt::Long;
use BSD::Resource;
use POSIX qw(:signal_h);
    
#
# Simply a wrapper for the geni-lib python environment
#
# When run in "impotent" mode, there is no output, just an exit code. 
#
sub usage()
{
    print STDOUT
	"Usage: rungenilib.proxy -u user [args ...]\n".
        "Where options and arguments are those required by geni-lib\n";
 
    exit(-1);
}

#
# Configure variables
#
my $TB       = "@prefix@";
my $TBOPS    = "@TBOPSEMAIL@";
my $TESTMODE = 0;
my $GENILIB  = "$TB/lib/geni-lib/";
my $debug    = 0;

# Locals
my $tempdir = "/tmp/genilib-$$";
my $ifile   = "$$.py";
my $ofile   = "$$.rspec";

my $optlist = "u:v";

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

#
# Untaint the path
# 
$ENV{'PATH'} = "/bin:/usr/bin:/sbin:/usr/sbin";
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
$ENV{"PYTHONPATH"} = $GENILIB;

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use libtestbed;

my $user;

#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
if (! GetOptions("u:s" => \$user)) {
    usage();
}

#
# First option has to be the -u option, the user to run this script as.
# In testmode, we are not run as root, so run as the current user, and
# in the current directory (not invoked with ssh in testmode). 
#
if (! $TESTMODE) {
    if ($UID != 0) {
	die("*** $0:\n".
	    "    Must be root to run this script!");
    }

    (undef,undef,$unix_uid) = getpwnam($user) or
	die("*** $0:\n".
	    "    No such user $user\n");

    #
    # Need the entire group list for the user, cause of subgroups, and
    # cause thats the correct thing to do. Too bad perl does not have a
    # getgrouplist function like the C library.
    #
    my $glist = `id -G $user`;
    if ($glist =~ /^([\d ]*)$/) {
	$glist = $1;
    }
    else {
	die("*** $0:\n".
	    "    Unexpected results from 'id -G $user': $glist\n");
    }

    # Need to split off the first group and create a proper list for $GUID.
    my @gglist   = split(" ", $glist);
    my $unix_gid = $gglist[0];
    $glist       = "$unix_gid $glist";

    # Flip to user and never go back!
    $GID            = $unix_gid;
    $EGID           = $glist;
    $EUID = $UID    = $unix_uid;
    $ENV{'USER'}    = $user;
    $ENV{'LOGNAME'} = $user;

    #
    # Create the tempdir and chmod it to keep people out.
    #
    if (! mkdir($tempdir, 0750)) {
	die("Could not mkdir $tempdir: $!\n");
    }
    if (! chmod(0750, $tempdir)) {
	die("Could not chmod $tempdir to 0750: $!\n");
    }
    if (! chdir($tempdir)) {
	die("Could not chdir to $tempdir: $!\n");
    }
}

#
# Take our input and write it to the temp file. 
#
open(TMP, ">$ifile") ||
    fatal("Couldn't open $ifile\n");
#
# Prepend our little helper function that spits the rspec to a file.
#
print TMP "\n\n";
print TMP "def printRspec(rspec):\n";
print TMP "  f = open('$ofile', 'w+')\n";
print TMP "  f.write(str(rspec))\n";
print TMP "  f.close()\n";
print TMP "  pass\n\n";

while (<STDIN>) {
    print TMP $_;
}
close(TMP);
chmod(0644, $ifile);

#
# 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, 600, 600);
    
    # Give parent a chance to react.
    sleep(1);

    #
    # Dup stdout to stderr; all output is considered error output.
    # which allows us to use stdout to send the rspec back to boss.
    #
    close(STDOUT);
    POSIX::dup2(fileno(STDERR), 1);

    exec("nice -15 /usr/local/bin/python $ifile");
    die("Could not exec the parser!\n");
}

#
# Parent waits.
#
waitpid($pid, 0);
my $exit_status = $?;

#
# If the child was KILLed, then it overran its time limit.
# Send email. Otherwise, exit with result of child.
#
if (($exit_status & 0xff) == SIGKILL) {
    print STDERR "geni-lib Exceeded CPU Limit\n";
    $exit_status = 15;
}
elsif ($exit_status & 0xff) {
    # Get the mapping from signal num. to name
    use Config;
    my (%sig_num, @sig_name);
    my @names = split ' ', $Config{sig_name};
    @sig_num{@names} = split ' ', $Config{sig_num};
    foreach (@names) {$sig_name[$sig_num{$_}] ||= $_}
    
    my $signal = $exit_status & 0x7f;
    my $signame = $sig_name[$signal];
    
    print STDERR "geni-lib Died with SIG$signame.\n";
    $exit_status = 128 + $signal;
}
elsif ($exit_status) {
    $exit_status = $exit_status >> 8;
}
elsif (! -s $ofile)  {
    print STDERR "Cannot find the rspec, did you call printRspec(rspec)?\n";
    $exit_status = 1;
}
else {
    # Send the rspec output to stdout (back to boss).
    system("cat $ofile");
}
unless($debug) {
    if (-d $tempdir) {
	system("/bin/rm -r $tempdir");
    }
}
exit($exit_status);