feedback.php3 6.79 KB
Newer Older
1 2
<?php
#
3
# Copyright (c) 2000-2011 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{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/>.
# 
# }}}
23 24 25 26 27 28 29 30 31 32
#
include("defs.php3");

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

#
# Only known and logged in users can do this.
#
33 34 35
$this_user = CheckLoginOrDie();
$uid       = $this_user->uid();
$isadmin   = ISADMIN();
36 37

#
38
# Verify Page arguments
39
#
40 41 42 43 44 45 46
$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);
47

48 49 50 51 52 53
# Need these below.
$pid = $experiment->pid();
$eid = $experiment->eid();
$gid = $experiment->gid();
$expstate = $experiment->state();
$unix_gid = $experiment->UnixGID();
54 55
$project  = $experiment->Project();
$unix_pid = $project->unix_gid();
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

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

71 72
if (strcmp($mode, "record") && strcmp($mode, "clear")) {
    PAGEARGERROR("Mode value, $mode, is not 'record' or 'clear'!");
73 74 75 76 77 78
}

#
# Get the duration of the feedback run, default to 30 seconds if nothing was
# given.
#
79 80
if (!isset($duration)) {
     $duration = 30; # seconds
81
}
82 83
elseif ($duration < 3) {
    PAGEARGERROR("Duration must be an integer >= 3");
84 85 86 87 88 89 90 91
}

#
# 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.
#
92
if (isset($canceled) && $canceled) {
93 94 95 96 97 98 99 100 101 102
    PAGEHEADER("$mode Feedback");
	
    echo "<center><h3><br>
          Operation canceled!
          </h3></center>\n";
    
    PAGEFOOTER();
    return;
}

103
if (!isset($confirmed)) {
104 105
    PAGEHEADER("$mode feedback");

106
    echo $experiment->PageHeader();
107 108 109 110 111 112 113 114 115 116 117 118 119

    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";
    }
    
120 121 122
    $experiment->Show(1);

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

    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.
181
    $fp = popen("$TBSUEXEC_PATH $uid $unix_pid,$unix_gid webfeedback ".
182
		"-d $duration $pid $gid $eid",	"r");
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
    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 != "") {
219
	$retval = SUEXEC($uid, "$unix_pid,$unix_gid",
220 221
			 "webfeedback $options $pid $gid $eid",
			 SUEXEC_ACTION_USERERROR);
222 223 224 225 226
	
	#
	# Avoid	SIGPROF in child.
	# 
	set_time_limit(0);
227 228

	$nsdata = $experiment->NSFile();
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);
	
242
	$retval = SUEXEC($uid, "$unix_pid,$unix_gid",
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
			 "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("");
	}
261 262
    }
    
263
    echo "<center><h3><br>Done!</h3></center>\n";
264 265 266 267 268 269

    PAGEFOOTER();
    return;
}

?>