Modified to trap signals properly and not write to the root directory (only working directory)
authorDan
Sun, 23 Mar 2008 23:55:50 -0400
changeset 8 a8d108f37363
parent 7 3ba432f8390b
child 9 63a257541313
Modified to trap signals properly and not write to the root directory (only working directory)
functions.php
greyhound.php
--- a/functions.php	Sun Mar 23 23:17:06 2008 -0400
+++ b/functions.php	Sun Mar 23 23:55:50 2008 -0400
@@ -167,11 +167,12 @@
   if ( !isset($smarty[$theme_id]) )
   {
     $smarty[$theme_id] = new Smarty();
-    $smarty[$theme_id]->template_dir = "./themes/$theme_id";
-    $smarty[$theme_id]->compile_dir = "./themes/$theme_id/compiled";
-    $smarty[$theme_id]->cache_dir = "./cache";
+    $smarty[$theme_id]->template_dir = GREY_ROOT . "/themes/$theme_id";
+    if ( !is_dir("./compiled/$theme_id") )
+      @mkdir("./compiled/$theme_id");
+    $smarty[$theme_id]->compile_dir  = "./compiled/$theme_id";
     $smarty[$theme_id]->config_dir = "./config";
-    $httpd->add_handler("themes/$theme_id", 'dir', "./themes/$theme_id");
+    $httpd->add_handler("themes/$theme_id", 'dir', GREY_ROOT . "/themes/$theme_id");
   }
   return $smarty[$theme_id];
 }
--- a/greyhound.php	Sun Mar 23 23:17:06 2008 -0400
+++ b/greyhound.php	Sun Mar 23 23:55:50 2008 -0400
@@ -12,6 +12,13 @@
  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
  */
 
+// required for signal handling to work
+declare(ticks=1);
+
+// trap SIGTERM
+pcntl_signal(SIGTERM, 'sigterm');
+pcntl_signal(SIGINT,  'sigterm');
+
 $public = true;
 $allowcontrol = true;
 $theme = 'funkymonkey';
@@ -21,24 +28,23 @@
 // include files
 require('functions.php');
 
+// get the root
+define('GREY_ROOT', dirname(__FILE__));
+
+// create directories
+@mkdir('./compiled');
+
 // start up...
 
 status('Starting WebControl v0.1-hg');
 status('loading files');
 
 require('webserver.php');
-define('SMARTY_DIR', './smarty/');
-require('smarty/Smarty.class.php');
-require('playlist.php');
-require('json.php');
-require('ajax.php');
-
-status('initializing Smarty');
-$smarty = new Smarty();
-$smarty->template_dir = "./themes/$theme";
-$smarty->compile_dir = "./themes/$theme/compiled";
-$smarty->cache_dir = "./cache";
-$smarty->config_dir = "./config";
+define('SMARTY_DIR', GREY_ROOT . '/smarty/');
+require(GREY_ROOT . '/smarty/Smarty.class.php');
+require(GREY_ROOT . '/playlist.php');
+require(GREY_ROOT . '/json.php');
+require(GREY_ROOT . '/ajax.php');
 
 status('doing home directory detection');
 
@@ -51,6 +57,15 @@
 
 $homedir =& $_ENV['HOME'];
 
+// signal handler
+function sigterm($signal)
+{
+  global $httpd;
+  status("Caught SIGTERM, cleaning up.");
+  @socket_close($httpd->sock);
+  exit();
+}
+
 status('initializing playlist');
 
 // init playlist object
@@ -68,12 +83,11 @@
   
   // setup handlers
   status('initializing handlers');
-  $httpd->add_handler('index', 'function', 'amarok_playlist');
-  $httpd->add_handler('action.json', 'function', 'ajax_request_handler');
-  $httpd->add_handler('scripts', 'dir', './scripts');
-  $httpd->add_handler('favicon.ico', 'file', './amarok_icon.ico');
-  $httpd->add_handler('apple-touch-icon.png', 'file', './apple-touch-icon.png');
-  $httpd->add_handler("themes/$theme", 'dir', "./themes/$theme");
+  $httpd->add_handler('index',                'function', 'amarok_playlist');
+  $httpd->add_handler('action.json',          'function', 'ajax_request_handler');
+  $httpd->add_handler('scripts',              'dir',      GREY_ROOT . '/scripts');
+  $httpd->add_handler('favicon.ico',          'file',     GREY_ROOT . '/amarok_icon.ico');
+  $httpd->add_handler('apple-touch-icon.png', 'file',     GREY_ROOT . '/apple-touch-icon.png');
   $httpd->allow_dir_list = true;
   $httpd->default_document = 'index';