linktest_control.in 5.39 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/usr/bin/perl -wT
#
# EMULAB-COPYRIGHT
# Copyright (c) 2000-2004 University of Utah and the Flux Group.
# 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
26
27
28
	  "-o   - Specify output file for linktest results.\n".
	  "-d   - Turn on debugging output.\n");
    exit(-1);
}
29
my $optlist  = "dkl:o:t:";
30
31
my $debug    = 0;
my $cancel   = 0;
32
my $timeout;
33
34
35
36
37
38
39
40
41
my $level;
my $output;
my $child_pid;			# Child run_linktest process.

#
# Configure variables
#
my $TB		= "@prefix@";
my $TBOPS       = "@TBOPSEMAIL@";
42
43
my $CONTROL	= "@USERNODE@";
my $SSH		= "$TB/bin/sshtb";
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

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

#
# 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;
}
if (defined($options{"k"})) {
    $cancel = 1;
}
75
76
77
78
79
80
81
82
83
84
if (defined($options{"t"})) {
    $timeout = $options{"t"};

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

#
# 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)) {
	kill('TERM', $child_pid);
	waitpid($child_pid, 0);
	undef($child_pid);
    }
    cleanup();
    die("*** $0:\n".
	"    Linktest has been canceled on experiment $pid/$eid!\n")
}

#
# 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.
#
if (($child_pid = fork()) < 0) {
    die("*** $0:\n".
	"    Linktest could not fork a new process for $pid/$eid!\n")
}

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();
    exit($exitval);
}

#
226
227
228
# 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.
229
#
230
231
232
233
234
235
236
237
238
239
240
241
my $cmd = "$SSH -t -host $CONTROL $TB/bin/run_linktest.pl -v";
$cmd .= " -d $debug"
    if ($debug);
$cmd .= " -t $timeout"
    if (defined($timeout));
$cmd .= " -o $output"
    if (defined($output));
$cmd .= " -l " . (defined($level) ? $level : $linktest_level);
$cmd .= " -s ops -e $pid/$eid";

print "Running '$cmd'\n"
    if ($debug);
242

243
exec($cmd);
244
245
die("*** $0:\n".
    "    Could not exec run_linktest.pl\n");