feedback.php3 6.98 KB
Newer Older
1 2 3
<?php
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2000-2006 University of Utah and the Flux Group.
5 6 7 8 9 10 11 12 13 14 15 16
# All rights reserved.
#
include("defs.php3");
include("showstuff.php3");

#
# No PAGEHEADER since we spit out a Location header later. See below.
# 

#
# Only known and logged in users can do this.
#
17 18 19
$this_user = CheckLoginOrDie();
$uid       = $this_user->uid();
$isadmin   = ISADMIN();
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

#
# Check to make sure a valid experiment.
#
if (isset($pid) && strcmp($pid, "") &&
    isset($eid) && strcmp($eid, "")) {
    if (! TBvalid_eid($eid)) {
	PAGEARGERROR("$eid contains invalid characters!");
    }
    if (! TBvalid_pid($pid)) {
	PAGEARGERROR("$pid contains invalid characters!");
    }
    if (! TBValidExperiment($pid, $eid)) {
	USERERROR("$pid/$eid is not a valid experiment!", 1);
    }
    if (! TBExptAccessCheck($uid, $pid, $eid, $TB_EXPT_MODIFY)) {
	USERERROR("You do not have permission to run feedback on $pid/$eid!",
		  1);
    }
}
else {
    PAGEARGERROR("Must specify pid and eid!");
}

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
$expstate = TBExptState($pid, $eid);

if (strcmp($expstate, $TB_EXPTSTATE_ACTIVE) &&
    strcmp($expstate, $TB_EXPTSTATE_SWAPPED)) {
    USERERROR("You cannot modify an experiment in transition.", 1);
}

if (!strcmp($expstate, $TB_EXPTSTATE_ACTIVE)) {
	$reboot = 1;
	$eventrestart = 1;
}
else {
	$reboot = 0;
	$eventrestart = 0;
}

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
if (isset($mode) && strcmp($mode, "")) {
    if (strcmp($mode, "record") && strcmp($mode, "clear")) {
	PAGEARGERROR("Mode value, $mode, is not 'record' or 'clear'!");
    }
}
else {
    PAGEARGERROR("Must specify mode!");
}

$query_result = DBQueryFatal("select gid from experiments ".
			     "where pid='$pid' and eid='$eid'");
$row = mysql_fetch_array($query_result);
$gid = $row[0];

#
# Get the duration of the feedback run, default to 30 seconds if nothing was
# given.
#
if (!isset($duration) || $duration == "") {
	$duration = 30; # seconds
}
elseif (! TBvalid_tinyint($duration) || $duration < 3) {
	PAGEARGERROR("Duration must be an integer >= 3");
}

#
# We run this twice. The first time we are checking for a confirmation
# by putting up a form. The next time through the confirmation will be
# set. Or, the user can hit the cancel button, in which case we should
# probably redirect the browser back up a level.
#
if ($canceled) {
    PAGEHEADER("$mode Feedback");
	
    echo "<center><h3><br>
          Operation canceled!
          </h3></center>\n";
    
    PAGEFOOTER();
    return;
}

if (!$confirmed) {
    PAGEHEADER("$mode feedback");

    echo "<font size=+2>Experiment <b>".
	"<a href='showproject.php3?pid=$pid'>$pid</a>/".
	"<a href='showexp.php3?pid=$pid&eid=$eid'>$eid</a></b></font>\n";

    if(strcmp($mode, "record") == 0) {
	echo "<center><font size=+2><br>
                      How much feedback data should be recorded?<br>
                      Must be atleast 3 seconds.
                      </font>\n";
    }
    else if(strcmp($mode, "clear") == 0) {
	echo "<center><font size=+2><br>
                      Really clear feedback data?
                      </font>\n";
    }
    
    SHOWEXP($pid, $eid, 1);
    
    echo "<form action=feedback.php3 method=get>";
    echo "<input type=hidden name=pid value=$pid>\n";
    echo "<input type=hidden name=eid value=$eid>\n";
    echo "<input type=hidden name=mode value=$mode>\n";

    if(strcmp($mode, "record") == 0) {
	echo "<table align=center border=1>\n";
	echo "<tr>
                  <td>
                  <input type='text' name='duration' value='$duration'> seconds
                  </td>
              </tr>
              </table><br>\n";
    }
    else if(strcmp($mode, "clear") == 0) {
	echo "<br>Data to clear:</br>";
	echo "<table align=center border=1>\n";
	echo "<tr>
                  <td>
                  <input type='checkbox' name='clear_last' value='1' checked='1'> Last recorded trace.
                  </td>
              </tr>
              <tr>
                  <td>
                  <input type='checkbox' name='clear_bootstrap' value='1'> Bootstrap data.
                  </td>
              </tr>
              </table><br>\n";
    }

    echo "<b><input type=submit name=confirmed value=Confirm></b>\n";
    echo "<b><input type=submit name=canceled value=Cancel></b>\n";
    echo "</form>\n";
    echo "</center>\n";
    
    PAGEFOOTER();
    return;
}

function SPEWCLEANUP()
{
    global $fp;

    if (connection_aborted() && $fp) {
	pclose($fp);
    }
    exit();
}

# For backend.
TBGroupUnixInfo($pid, $gid, $unix_gid, $unix_name);

if (strcmp($mode, "record") == 0) {
    #
    # A cleanup function to keep the child from becoming a zombie, since
    # the script is terminated, but the children are left to roam.
    #
    $fp = 0;
    
    register_shutdown_function("SPEWCLEANUP");
    ignore_user_abort(1);
    
    # Start the script and pipe its output to the user.
186 187
    $fp = popen("$TBSUEXEC_PATH $uid $pid,$unix_gid webfeedback ".
		"-d $duration $pid $gid $eid",	"r");
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
    if (! $fp) {
	USERERROR("Feedback failed!", 1);
    }

    header("Content-Type: text/plain");
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
    header("Cache-Control: no-cache, must-revalidate");
    header("Pragma: no-cache");
    flush();
    
    echo date("D M d G:i:s T");
    echo "\n";
    echo "Recording feedback for $duration seconds\n";
    flush();
    while (!feof($fp)) {
	$string = fgets($fp, 1024);
	echo "$string";
	flush();
    }
    $retval = pclose($fp);
    $fp = 0;
    if ($retval == 0)
	echo "Feedback run was successful!\n";
    echo date("D M d G:i:s T");
    echo "\n";
    return;
}
else if (strcmp($mode, "clear") == 0) {
    PAGEHEADER("Clearing feedback");

    $options = "";
    if (isset($clear_last) && !strcmp($clear_last, "1"))
	$options .= " -c";
    if (isset($clear_bootstrap) && !strcmp($clear_bootstrap, "1"))
	$options .= " -b";
    if ($options != "") {
224
	$retval = SUEXEC($uid, "$pid,$unix_gid",
225 226
			 "webfeedback $options $pid $gid $eid",
			 SUEXEC_ACTION_USERERROR);
227 228 229 230 231 232 233 234 235 236
	
	#
	# Avoid	SIGPROF in child.
	# 
	set_time_limit(0);
	
	$query_result = DBQueryFatal("SELECT nsfile from nsfiles ".
				     "where pid='$pid' and eid='$eid'");
	if (mysql_num_rows($query_result)) {
	    $row    = mysql_fetch_array($query_result);
237
	    $nsdata = $row[nsfile];
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
	}
	else {
	    $nsdata = ""; # XXX what to do...
	}
	
	list($usec, $sec) = explode(' ', microtime());
	srand((float) $sec + ((float) $usec * 100000));
	$foo = rand();
	$nsfile = "/tmp/$uid-$foo.nsfile";
	
	if (! ($fp = fopen($nsfile, "w"))) {
	    TBERROR("Could not create temporary file $nsfile", 1);
	}
	fwrite($fp, $nsdata);
	fclose($fp);
	chmod($nsfile, 0666);
	
	$retval = SUEXEC($uid, "$pid,$unix_gid",
			 "webswapexp " . ($reboot ? "-r " : "") .
			 ($eventrestart ? "-e " : "") .
			 "-s modify $pid $eid $nsfile",
			 SUEXEC_ACTION_IGNORE);
	
	unlink($nsfile);
	
	#
	# Fatal Error. Report to the user, even though there is not much he can
	# do with the error. Also reports to tbops.
	# 
	if ($retval < 0) {
	    SUEXECERROR(SUEXEC_ACTION_DIE);
	#
	# Never returns ...
	#
	    die("");
	}
274 275
    }
    
276
    echo "<center><h3><br>Done!</h3></center>\n";
277 278 279 280 281 282

    PAGEFOOTER();
    return;
}

?>