Skip to content. Skip to navigation

Ian Lawrence

Sections
Personal tools
You are here: Home Blog Securely restarting icecast stream server

Securely restarting icecast stream server

Running php in safe mode means the exec() system command will not work. This shows you how to securely execute commands (in this case to start/stop/restart an icecast streaming server)

Juba from Radio Muda has written a GTK front
end to icecast called Theorur and released it under
the GNU GPL.


We noticed some problems with Theorur and the
streams not showing up on the estudiolivre stream
aggregator
and nailed the problem down to
being unable to:

kill -HUP icecast.

so I hacked this together (using lots of borrowed code :)

Create a directory:

mkdir /usr/local/php_safe_bin

Create a sym link to the needed binaries:

ln -s /usr/bin/kill /usr/local/php_safe_bin/
ln -s /usr/bin/ps /usr/local/php_safe_bin/

Your php.ini should have these lines:

safe_mode = On
safe_mode_gid = On
safe_mode_exec_dir = "/usr/local/php_safe_bin"

create a file icecast_control.php with this in it:

<?
# Change to the directory you have your icecast conf file in. You could
work the conf file into the exec command further down the script if you
want to do that instead.
chdir('/path/to/your/conf');

# Not sure if this is needed.
header("Cache-Control: no-store, no-cache");

# Get ICES and IceCast Process ID's. Icecast PID is not needed and is
commented out.
$pid = exec("ps -o \"%p\" --noheaders -C ices");
# $pidicecast = exec("ps -o \"%p\" --noheaders -C icecast");

$self=$_SERVER["PHP_SELF"];

echo "
<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"
\"http://www.w3.org/TR/2002/REC-xhtml1-20020801/DTD/xhtml1-transitional.dtd\">
<html>
<head>
<title>ICES Web Interface - Basic</title>
<meta http-equiv=\"content-type\" content=\"text/html;
charset=iso-8859-1\" />
<meta http-equiv=\"content-language\" content=\"en\" />
<meta name=\"robots\" content=\"noindex, nofollow\" />
</head>
<body>
";

#########
# If ICES doesn't have a Process ID it is not running. Return button to
start it in this case.
if ($pid <=1) {

echo "
ICES NOT RUNNING!

<br /><br />

<table border=\"1\">
<tr>
<td>
<form enctype=\"multipart/form-data\" action=\"$self\" method=\"post\" >
<input type=\"hidden\" name=\"action\" value=\"START ICES\" />
<input type=\"submit\" value=\"START ICES\" />
</form>
</tr>
</td>
</table>
</body>
</html>
";
}

#########
# If ICES does have a Process ID it is running. Return buttons to reload
the conf file and playlist, skip the current song or kill ICES.

else {
echo "
ICES running on: $pid

<br /><br />

<table border=\"1\">
<tr>
<td>
<form enctype=\"multipart/form-data\" action=\"$self\" method=\"post\" >
<input type=\"hidden\" name=\"action\" value=\"RELOAD\" />
<input type=\"submit\" value=\"RELOAD\" />
</form>
</td>
<td>
<form enctype=\"multipart/form-data\" action=\"$self\" method=\"post\" >
<input type=\"hidden\" name=\"action\" value=\"SKIP\" />
<input type=\"submit\" value=\"SKIP->\" />
</form>
</td>
<td>
<form enctype=\"multipart/form-data\" action=\"$self\" method=\"post\" >
<input type=\"hidden\" name=\"action\" value=\"KILL\" />
<input type=\"submit\" value=\"STOP\" />
</form>
</td>
</tr>
</table>
</body>
</html>
";
}

#########
# Define functions. I'm sure these can be cleaned up alot. But they work.

function ices_start() {
$handle = popen('ices & 2>&1', 'r');
pclose($handle);
}

function ices_kill() {
$pid = exec("ps -o \"%p\" --noheaders -C ices");
echo exec("kill -KILL $pid");
}

function ices_reload() {

$pid = exec("ps -o \"%p\" --noheaders -C ices");
echo exec("kill -SIGHUP $pid");
}

function ices_skip() {
$pid = exec("ps -o \"%p\" --noheaders -C ices");
echo exec("kill -USR1 $pid");
}

#########
# Match form actions to functions.

$action = $_POST['action'];

if ( $action == "START ICES" ) {
ices_start();
}

if ( $action == "RELOAD" ) {
ices_reload();
}

if ( $action == "SKIP" ) {
ices_skip();
}

if ( $action == "KILL") {
ices_kill();
}

?>
and restart apache


_____
tags:
Thursday, April 12, 2007 in CodeEstudiolivre  | Permalink | 
del.icio.us   Digg   Yahoo   Google   Spurl
Blog
« November 2017 »
Su Mo Tu We Th Fr Sa
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30
Categories:
Borala (4)
Bricolabs (12)
Code (57)
Estudiolivre (12)
Life (26)
MetaReciclagem (9)
Thoughts (16)
Work (41)