linktest_control.in 7.52 KB
Newer Older
1
2
3
#!/usr/bin/perl -wT
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2000-2005 University of Utah and the Flux Group.
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# All rights reserved.
#
use English;
use Getopt::Std;
use POSIX;

#
# Run the linktest code from experiment swapin; this script serves as
# a wrapper for run_linktest.pl. We store the pid in the DB, and allow
# for linktest cancelation from the webpage. This script is only run on
# boss; otherwise the user is running run_linktest.pl directly on ops or
# on a node, and can terminate linktest directly (via ^C or SIGTERM).
#
sub usage()
{
20
21
    print("Usage: linktest_control [-d] [-t timeout] [-k | -l <level>] ".
	  "[-o <file>] <pid> <eid>\n".
22
23
	  "-l   - Run linktest at a specific level; defaults to DB value.\n".
	  "-k   - Kill a currently running linktest.\n".
24
	  "-t   - Specify timeout in seconds.\n".
25
	  "-o   - Specify output file for linktest results.\n".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
26
	  "-m   - Send email to swapper if linktest fails.\n".
27
28
29
	  "-d   - Turn on debugging output.\n");
    exit(-1);
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
30
my $optlist  = "dkl:o:t:m";
31
32
my $debug    = 0;
my $cancel   = 0;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
33
my $sendmail = 0;
34
my $timeout;
35
36
my $level;
my $output;
37
38
my $expstate;
my $dbuid;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
39
40
my $user_name;
my $user_email;
41
42
43
44
45
46
47
my $child_pid;			# Child run_linktest process.

#
# Configure variables
#
my $TB		= "@prefix@";
my $TBOPS       = "@TBOPSEMAIL@";
48
my $CONTROL	= "@USERNODE@";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
49
my $TBBASE	= "@TBBASE@";
50
my $SSH		= "$TB/bin/sshtb";
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

#
# Testbed Support libraries
#
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; 

68
69
70
71
72
if ($EUID != 0) {
    die("*** $0:\n".
	"    Must be root! Maybe its a development version?\n");
}

73
74
75
76
77
78
79
80
81
82
83
#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"d"})) {
    $debug = 1;
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
84
85
86
if (defined($options{"m"})) {
    $sendmail = 1;
}
87
88
89
if (defined($options{"k"})) {
    $cancel = 1;
}
90
91
92
93
94
95
96
97
98
99
if (defined($options{"t"})) {
    $timeout = $options{"t"};

    if ($timeout =~ /^(\d*)$/) {
	$timeout = $1;
    }
    else {
	die("Bad data in timeout: $timeout");
    }
}
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
if (defined($options{"l"})) {
    $level = $options{"l"};

    if ($level =~ /^(\d*)$/) {
	$level = $1;
    }
    else {
	die("Bad data in level: $level.");
    }
}
if (defined($options{"o"})) {
    $output = $options{"o"};

    # Note different taint check (allow /).
    if ($output =~ /^([-\w\.\/]+)$/) {
	$output = $1;
    }
    else {
	die("Bad data in output file: $output\n");
    }
}
if (@ARGV != 2) {
    usage();
}
my $pid = $ARGV[0];
my $eid = $ARGV[1];

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

143
144
145
146
147
148
149
#
# Check state. Only in the active state
#
if (! ($expstate = ExpState($pid, $eid))) {
    die("*** $0:\n".
	"    No such experiment $pid/$eid!\n");
}
Timothy Stack's avatar
   
Timothy Stack committed
150
if ($expstate ne EXPTSTATE_ACTIVE && $expstate ne EXPTSTATE_ACTIVATING) {
151
    die("*** $0:\n".
Timothy Stack's avatar
   
Timothy Stack committed
152
	"    Experiment $pid/$eid must be active!\n");
153
154
155
156
157
158
159
160
}

# Need this to pass to boss.
if (! UNIX2DBUID($UID, \$dbuid)) {
    die("*** $0:\n".
	"    You do not exist in the Emulab Database!\n");
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
161
162
163
164
165
166
167
168
#
# Get email info for user.
#
if (! UserDBInfo($dbuid, \$user_name, \$user_email)) {
    die("*** $0:\n".
	"    Cannot determine your name and email address.\n");
}

169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#
# Check permission. Only people with permission to destroy the experiment
# can do this.
#
if (! TBExptAccessCheck($UID, $pid, $eid, TB_EXPT_DESTROY)) {
    die("*** $0:\n".
	"    You do not have permission to start/stop linktest for ".
	"$pid/$eid!\n");
}

# Need the unix_gid info to pass to boss.
my $gid = ExpGroup($pid, $eid);
my ($unix_gid, $unix_gidname);

if (! TBGroupUnixInfo($pid, $gid, \$unix_gid, \$unix_gidname)) {
    die("*** $0:\n".
	"    Could not get unix group info for $pid/$gid!\n");
}

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
243
244
245
246
247
248
249
250
#
# Lets see if there is a linktest running already.
#
my $query_result =
    DBQueryFatal("select linktest_level,linktest_pid ".
		 "from experiments where eid='$eid' and pid='$pid'");

if (! $query_result->numrows) {
    die("*** $0:\n".
	"    No such experiment $pid/$eid exists!\n");
}
my ($linktest_level, $linktest_pid) = $query_result->fetchrow_array();

if (defined($linktest_pid) && $linktest_pid) {
    if (! kill(0, $linktest_pid) && ($ERRNO == ESRCH)) {
	DBQueryFatal("update experiments set linktest_pid=0 ".
		     "where pid='$pid' and eid='$eid'");
	
	if ($cancel) {
	    print("Linktest has already exited on experiment $pid/$eid!\n");
	    exit(0);
	}
	else {
	    print("Clearing stale linktest pid from DB for $pid/$eid!\n");
	}
    }
    elsif (!$cancel) {
	die("*** $0:\n".
	    "    Linktest is already running on experiment $pid/$eid!\n");
    }
    else {
	if (! kill('TERM', $linktest_pid)) {
	    SENDMAIL($TBOPS,
		     "Failed to stop linktest daemon for $pid/$eid",
		     "Could not kill(TERM) process $linktest_pid: $? $!");

	    die("*** $0:\n".
		"    Failed to stop linktest daemon for $pid/$eid!\n");
	}
	exit(0);
    }
}
elsif ($cancel) {
    die("*** $0:\n".
	"    Linktest is not running on experiment $pid/$eid!\n")
}

#
# Okay, lets run linktest. First set up a handler so that we can catch
# a termination signal and kill ourselves off. 
#
sub cleanup()
{
    DBQueryFatal("update experiments set linktest_pid=0 ".
		 "where pid='$pid' and eid='$eid'");
}

sub handler($)
{
    $SIG{TERM} = 'IGNORE';
    $SIG{INT}  = 'IGNORE';

    if (defined($child_pid)) {
251
	kill('HUP', $child_pid);
252
253
254
255
256
257
258
259
	waitpid($child_pid, 0);
	undef($child_pid);
    }
    cleanup();
    die("*** $0:\n".
	"    Linktest has been canceled on experiment $pid/$eid!\n")
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
260
261
262
263
264
265
266
267
268
269
270
271
272
273
sub notify()
{
    my $lev = (defined($level) ? $level : $linktest_level);
    
    SENDMAIL("$user_name <$user_email>",
	     "Linktest Failure: $pid/$eid",
	     "Failure in linktest (level $lev); ".
	     "returned non-zero status.\n".
	     "Activity log file at:\n\n".
	     "    $TBBASE/spewlogfile.php3?pid=${pid}&eid=${eid}\n",
	     "$user_name <$user_email>",
	     "Cc: $TBOPS");
}

274
275
276
277
#
# Fork a child to run the actual linktest script. The parent just waits
# for child to exit, or to be signaled to terminate the child.
#
278
279
280
281
282
283
284
if (defined($output)) {
    $child_pid = TBBackGround($output);
}
else {
    $child_pid = fork();
}
if ($child_pid < 0) {
285
    die("*** $0:\n".
286
	"    Linktest could not fork a new process for $pid/$eid!\n");
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
}

if ($child_pid) {
    #
    # Parent. 
    #
    $SIG{TERM} = \&handler;
    $SIG{INT}  = \&handler;

    DBQueryFatal("update experiments set linktest_pid=$$ ".
		 "where pid='$pid' and eid='$eid'");
    
    waitpid($child_pid, 0);
    my $exitval = $? >> 8;
    cleanup();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
302
303
    notify()
	if ($exitval && $sendmail);
304
305
306
307
    exit($exitval);
}

#
308
309
310
# Child execs an ssh to users.emulab.net where linktest is actually run.
# We tell ssh to allocate a tty so that we can kill it with TERM and have
# everything die off properly.
311
#
312
313
314
315
# XXX: "-t -t" to ssh is so that kill -TERM carries across to ops. 
#
my @cmdargs = ("$SSH", "-t", "-t", "-1", "-F", "/dev/null",
	       "-host", $CONTROL, "exec", "$TB/sbin/linktest.proxy");
316
push(@cmdargs, ("-d", "$debug"))
317
    if ($debug);
318
push(@cmdargs, ("-t", "$timeout"))
319
    if (defined($timeout));
320
321
322
323
324
push(@cmdargs, "-l");
push(@cmdargs, (defined($level) ? $level : $linktest_level));
push(@cmdargs, ("-g", $unix_gidname));
push(@cmdargs, ("-u", $dbuid));
push(@cmdargs, ("-e", "$pid/$eid"));
325

326
print "Running '@cmdargs'\n"
327
    if ($debug);
328

329
330
# For sshtb
$UID=0;
331
exec(@cmdargs);
332
333
die("*** $0:\n".
    "    Could not exec run_linktest.pl\n");