rungenilib.proxy.in 6.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
#!/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/>.
# 
# }}}
#
25
use strict;
26
use English;
27
use Getopt::Std;
28 29 30 31 32 33 34 35 36 37 38 39 40 41
use BSD::Resource;
use POSIX qw(:signal_h);
    
#
# Simply a wrapper for the geni-lib python environment
#
sub usage()
{
    print STDOUT
	"Usage: rungenilib.proxy -u user [args ...]\n".
        "Where options and arguments are those required by geni-lib\n";
 
    exit(-1);
}
42
my $optlist = "u:vpb:W";
43 44 45
my $user;
my $getparams= 0;
my $paramsize;
46
my $warningsfatal = 0;
47 48 49 50 51 52 53 54 55 56 57 58 59

#
# 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";
60 61
my $ofile   = "$$.out";
my $pfile   = "$$.json";
62

63 64
# Protos
sub fatal($);
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

#
# 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;

#
85
# Parse command arguments. Once we return from getopts, all that should
86 87
# left are the required arguments.
#
88 89
my %options = ();
if (! getopts($optlist, \%options)) {
90 91
    usage();
}
92 93 94 95 96 97 98 99 100 101 102 103
if (defined($options{"d"})) {
    $debug = 1;
}
if (defined($options{"p"})) {
    $getparams = 1;
}
if (defined($options{"b"})) {
    $paramsize = $options{"b"};
}
if (defined($options{"u"})) {
    $user = $options{"u"};
}
104 105 106
if (defined($options{"W"})) {
    $warningsfatal = 1;
}
107 108 109 110 111 112 113 114 115 116 117 118

#
# 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!");
    }

119
    my (undef,undef,$unix_uid) = getpwnam($user) or
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
	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");
    }
}

#
164 165
# Take our input and write it to the temp file. If we are also getting
# a parameter file in the stream, write that to another file.
166
#
167 168 169 170 171 172 173 174 175 176 177 178 179 180
if (defined($paramsize) && $paramsize) {
    my $paramstr;
    
    open(PTMP, ">$pfile") ||
	fatal("Couldn't open $pfile\n");

    my $cc = read(STDIN, $paramstr, $paramsize);
    if (!defined($cc) || $cc != $paramsize) {
	fatal("Could not read paramter block from stdin");
    }
    print PTMP $paramstr;
    close(PTMP);
    chmod(0644, $pfile);
}
181 182 183 184 185 186 187 188
open(TMP, ">$ifile") ||
    fatal("Couldn't open $ifile\n");
while (<STDIN>) {
    print TMP $_;
}
close(TMP);
chmod(0644, $ifile);

189 190 191 192 193 194 195 196 197 198 199
#
# Need to add things to the environment for the portal module.
#
$ENV{'GENILIB_PORTAL_MODE'} = "Yep";
$ENV{'GENILIB_PORTAL_REQUEST_PATH'} = $ofile;
if ($getparams) {
    $ENV{'GENILIB_PORTAL_DUMPPARAMS_PATH'} = $ofile;
}
elsif (defined($paramsize) && $paramsize) {
    $ENV{'GENILIB_PORTAL_PARAMS_PATH'} = $pfile;
}
200 201 202
if ($warningsfatal) {
    $ENV{'GENILIB_PORTAL_WARNINGS_ARE_FATAL'} = "1";
}
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 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
#
# 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);

281 282
sub fatal($) {
    my ($mesg) = $_[0];
283

284 285 286 287
    print STDERR "*** $0:\n".
	         "    $mesg\n";
    exit(-1);
}