eventsys.proxy.in 8.49 KB
Newer Older
1 2 3
#!/usr/bin/perl -w

#
Mike Hibler's avatar
Mike Hibler committed
4
# Copyright (c) 2000-2017 University of Utah and the Flux Group.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
# 
# {{{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/>.
# 
# }}}
24 25 26 27 28 29 30 31
#

use English;
use Getopt::Std;
use Errno;
use POSIX ":sys_wait_h";
    
#
32 33
# A wrapper for controlling from boss the event scheduler running on ops.
# This wrapper runs on ops.
34 35 36 37 38 39 40 41
#
# The first argument option is the user to run this script as, since we
# get invoked by a root ssh from boss. 
#
#
sub usage()
{
    print "Usage: eventsys.proxy -u user -g gid -e pid/eid -k keyfile ".
42
	"-l logfile -t record_file start|stop|replay\n";
43 44
    exit(-1);
}
45
my $optlist = "u:e:k:dl:g:t:aNp:";
46
my $debug   = 0;
47
my $runagent= 0;
48 49 50 51 52
my $user;
my $pid;
my $eid;
my $keyfile;
my $logfile;
53
my $recordfile;
54 55 56 57 58 59 60
my $action;

#
# Configure variables
#
my $TB       = "@prefix@";
my $TBOPS    = "@TBOPSEMAIL@";
61
my $CONTROL  = "@USERNODE@";
62
my $sched    = "$TB/sbin/event-sched";
63
my $agent    = "$TB/sbin/program-agent";
64 65
my $PIDDIR   = "/var/run/emulab/evsched";
my $PIDFILE;
66
my $EXPDIR;
67
my $epid;
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

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

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

#
# Only real root, cause the script has to read/write a pid file that
# cannot be accessed by the user.
#
if ($UID != 0) {
    die("*** $0:\n".
	"    Must be root to run this script!\n");
}

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

95 96 97
# Protos
sub StartProgram($@);

98 99 100 101 102 103 104 105 106 107 108 109 110 111
#
# 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 != 1) {
    usage();
}
if (! defined($options{"u"}) ||
    ! defined($options{"e"}) ||
    ! defined($options{"g"}) ||
112
    ! defined($options{"p"}) ||
113 114 115 116 117 118 119 120
    ! defined($options{"l"}) ||
    ! defined($options{"k"})) {
    usage();
}    
	    
if (defined($options{"d"})) {
    $debug = 1;
}
121 122 123
if (defined($options{"a"})) {
    $runagent = 1;
}
124 125 126
if (defined($options{"t"})) {
    $recordfile = $options{"t"};
}
127 128 129
if (defined($options{"N"})) {
    $sched = "$TB/sbin/new-event-sched";
}
130 131 132 133

$logfile    = $options{"l"};
$keyfile    = $options{"k"};
$user       = $options{"u"};
134 135
$unix_gname  = $options{"g"};
$unix_pname = $options{"p"};
136 137 138 139 140 141 142 143 144 145
$action     = $ARGV[0];

if ($options{"e"} =~ /^([-\w]*)\/([-\w]*)$/) {
    $pid = $1;
    $eid = $2;
}
else {
    usage();
}
$PIDFILE = "$PIDDIR/${pid}_${eid}.pid";
146
$EXPDIR  = PROJROOT() . "/$pid/exp/$eid";
147

148 149
# Look for existing pid file and get it.
if (-e $PIDFILE) {
150
    $epid = `cat $PIDFILE`;
151 152 153 154 155 156 157 158 159 160
    # untaint
    if ($epid =~ /^(\d*)$/) {
	$epid = $1;
    }
    else {
	die("*** $0:\n".
	    "    Bad data in pid: $epid!\n");
    }
}

161 162 163 164
#
# Deal with stop and replay.
#
if ($action eq "stop" || $action eq "replay") {
165 166 167 168 169
    if (-e $PIDFILE) {
	# Send any swapout events and wait, at most three seconds, for them to
	# complete.
	system("$TB/bin/tevc -w -t 3 -e ${pid}/${eid} now __ns_swapout run");

170 171 172 173
	# Send any teardown events and wait, at most five seconds, for them to
	# complete.
	system("$TB/bin/tevc -w -t 5 -e ${pid}/${eid} now __ns_teardown run");

174 175
	unlink($PIDFILE);

176 177 178 179 180
	if (kill(0, $epid) || ! $!{ESRCH}) {
	    if (! kill('TERM', $epid)) {
		die("*** $0:\n".
		    "Failed to stop event system for $pid/$eid! - $! $epid\n");
	    }
Mike Hibler's avatar
Mike Hibler committed
181 182 183 184 185 186 187

	    # Wait a bit and see if the process still exists. Warn if so.
	    sleep(2);
	    if (kill(0, $epid) || ! $!{ESRCH}) {
		print STDERR
		    "WARNING: event scheduler (pid $epid) did not die!\n";
	    }
188 189 190 191 192 193 194 195 196
	}
    }
    
    if ($action eq "stop") {
	exit(0);
    }
    # replay continues below, but give exiting scheduler a chance to react!
    sleep(1);
}
197 198 199 200 201 202 203 204 205 206
else {
    #
    # If asked to start, but already running, do nothing.
    #
    if (-e $PIDFILE && (kill(0, $epid) || ! $!{ESRCH})) {
	print STDERR
	    "*** event scheduler already running; not starting another\n";
	exit(0);
    }
}
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225

#
# Make sure the pid directory exists.
#
if (! -d $PIDDIR) {
    if (system("mkdir -p -m 775 $PIDDIR")) {
	die("*** $0:\n".
	    "    Could not mkdir $PIDDIR\n");
    }
}

#
# Okay, now flip to user before running the event scheduler. Must put the
# user into both the project group and the experiment subgroup.
#
my (undef,undef,$unix_uid) = getpwnam($user) or
    die("*** $0:\n".
	"    No such user $user\n");

226
my (undef,undef,$unix_ggid) = getgrnam($unix_gname) or
227
    die("*** $0:\n".
228
	"    No such group $unix_gname\n");
229

230
my (undef,undef,$unix_pgid) = getgrnam($unix_pname) or
231
    die("*** $0:\n".
232
	"    No such group $unix_pname\n");
233

234
my $LOGDIR = `dirname $logfile`;
235
$LOGDIR =~ s/\s+$//;
236 237 238 239 240 241 242
if (! -d $LOGDIR) {
    if (system("mkdir -p -m 775 $LOGDIR")) {
	die("*** $0:\n".
	    "    Could not mkdir $LOGDIR\n");
    }
}

243
#
244
# Create a child whose output is directed into the logfile. Parent waits
245 246 247 248 249 250 251 252 253 254 255
# a moment and then exits.
#
if (-e $logfile) {
    system("mv -f $logfile ${logfile}.old");
}

if (my $childpid = TBBackGround($logfile)) {
    #
    # Delay a moment, and they look for an exit status. This is intended
    # to catch startup problems.
    #
256
    sleep(2);
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
    my $foo = waitpid($childpid, &WNOHANG);
    if ($foo) {
	my $status = $?;
	unlink($PIDFILE);

	system("cat $logfile")
	    if (-s $logfile);
	
	die("*** $0:\n".
	    "    Failed to start event system for $pid/$eid: $foo $status!\n");
    }
    exit(0);
}

#
272
# Write out a pid file prior to flipping; the user is not granted
273 274 275 276 277 278 279
# access to this pid file.
#
if (system("echo '$PID' > $PIDFILE")) {
    die("*** $0:\n".
	"    Could not create $PIDFILE!");
}

280 281 282 283 284
#
# We will have two subprocesses.
#
my $schedpid;
my $agentpid;
285

286 287 288 289 290 291 292 293 294
#
# Catch TERM to kill off the scheduler and the agent. The death is picked
# up in the loop below.
#
sub handler ($) {
    my ($signame) = @_;
    
    $SIG{TERM} = 'IGNORE';
    
Mike Hibler's avatar
Mike Hibler committed
295 296 297
    my $spid = defined($schedpid) ? $schedpid : "";
    my $apid = defined($agentpid) ? $agentpid : "";
    print "Caught a TERM; killing the scheduler ($spid) and agent ($apid)\n";
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
    
    kill('TERM', $schedpid)
	if (defined($schedpid));
    kill('TERM', $agentpid)
	if (defined($agentpid));
    sleep(1);
}
$SIG{TERM} = \&handler;

#
# Set the command lines for the programs
#
my @sched_command_options = ();

push(@sched_command_options, "-d")
    if ($debug);
push(@sched_command_options, ("-t", $recordfile))
    if ($recordfile);
push(@sched_command_options, ("-s", "localhost", "-k", $keyfile, $pid, $eid));

my @agent_command_options = ("-u", $user, "-d", "-e", "$pid/$eid",
			     "-k", $keyfile,
320
			     "-v", "ops",
321 322
			     "-c", "$EXPDIR/tbdata/program_agents",
			     "-f", "$EXPDIR/tbdata/environment",
323 324
			     "-l", "$EXPDIR/logs/progagent.debug",
			     "-o", "$EXPDIR/ops");
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390

#
# We want to catch these programs if they exit abnormally.
#
$schedpid = StartProgram($sched, @sched_command_options);
$agentpid = StartProgram($agent, @agent_command_options)
    if ($runagent);

while (1) {
    my $waitpid = wait();
    my $exitstatus = $?;
    my $which;

    # No more children.
    last
	if ($waitpid < 0);

    if ($waitpid == $schedpid) {
	$schedpid = undef;
	$which    = "Event Scheduler";
    }
    else {
	$agentpid = undef;
	$which    = "Program Agent";
    }

    #
    # Send mail about abnormal exit.
    # 
    if ($?) {
	SENDMAIL($user,
		 "$which for $pid/$eid died on $CONTROL",
		 "$which exited with status: $?",
		 "$user",
		 "CC: $TBOPS");
    }

    last
	if (! (defined($schedpid) || defined($agentpid)));
}
exit(0);

sub StartProgram($@)
{
    my ($command, @arguments) = @_;

    my $mypid = fork();
    if ($mypid) {
	return $mypid;
    }
    select(undef, undef, undef, 0.2);

    # Flip to user and never go back
    $GID            = $unix_ggid;
    $EGID           = "$unix_ggid $unix_ggid $unix_pgid";
    $EUID = $UID    = $unix_uid;
    $ENV{'USER'}    = $user;
    $ENV{'LOGNAME'} = $user;

    print "$command @arguments\n";

    exec $command, @arguments;
    
    die("*** $0:\n".
	"    Could not exec $command!\n");
}
391