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

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

#
# Only known and logged in users can do this.
#
16 17 18
$this_user = CheckLoginOrDie();
$uid       = $this_user->uid();
$isadmin   = ISADMIN();
19 20

#
21
# Verify Page arguments
22
#
23 24 25 26 27 28 29
$reqargs = RequiredPageArguments("experiment", PAGEARG_EXPERIMENT,
				 "mode",       PAGEARG_STRING);
$optargs = OptionalPageArguments("duration",   PAGEARG_INTEGER,
				 "canceled",   PAGEARG_STRING,
				 "confirmed",  PAGEARG_STRING,
				 "clear_last", PAGEARG_BOOLEAN,
				 "clear_bootstrap", PAGEARG_BOOLEAN);
30

31 32 33 34 35 36
# Need these below.
$pid = $experiment->pid();
$eid = $experiment->eid();
$gid = $experiment->gid();
$expstate = $experiment->state();
$unix_gid = $experiment->UnixGID();
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

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

52 53
if (strcmp($mode, "record") && strcmp($mode, "clear")) {
    PAGEARGERROR("Mode value, $mode, is not 'record' or 'clear'!");
54 55 56 57 58 59
}

#
# Get the duration of the feedback run, default to 30 seconds if nothing was
# given.
#
60 61
if (!isset($duration)) {
     $duration = 30; # seconds
62
}
63 64
elseif ($duration < 3) {
    PAGEARGERROR("Duration must be an integer >= 3");
65 66 67 68 69 70 71 72
}

#
# 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.
#
73
if (isset($canceled) && $canceled) {
74 75 76 77 78 79 80 81 82 83
    PAGEHEADER("$mode Feedback");
	
    echo "<center><h3><br>
          Operation canceled!
          </h3></center>\n";
    
    PAGEFOOTER();
    return;
}

84
if (!isset($confirmed)) {
85 86
    PAGEHEADER("$mode feedback");

87
    echo $experiment->PageHeader();
88 89 90 91 92 93 94 95 96 97 98 99 100

    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";
    }
    
101 102 103
    $experiment->Show(1);

    $url = CreateURL("feedback", $experiment, "mode", $mode);
104
    
105
    echo "<form action='$url' method=post>";
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

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

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.
162 163
    $fp = popen("$TBSUEXEC_PATH $uid $pid,$unix_gid webfeedback ".
		"-d $duration $pid $gid $eid",	"r");
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
    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 != "") {
200
	$retval = SUEXEC($uid, "$pid,$unix_gid",
201 202
			 "webfeedback $options $pid $gid $eid",
			 SUEXEC_ACTION_USERERROR);
203 204 205 206 207
	
	#
	# Avoid	SIGPROF in child.
	# 
	set_time_limit(0);
208 209

	$nsdata = $experiment->NSFile();
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
	
	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("");
	}
242 243
    }
    
244
    echo "<center><h3><br>Done!</h3></center>\n";
245 246 247 248 249 250

    PAGEFOOTER();
    return;
}

?>