From c184472f1c4206a3bcd76ff8e9d27dc2eb1a4c4c Mon Sep 17 00:00:00 2001 From: paulr Date: Thu, 22 Sep 2011 16:43:14 +1000 Subject: [PATCH] changed how configs work --- bin/cbfwrd.php | 45 ++++++++++----------------- doco/configs.txt | 7 ++++- libcbfwr/comms.php | 13 ++++---- libcbfwr/config.php | 63 ++++++++++++--------------------------- libcbfwr/lib.php | 4 ++ libcbfwr/web.php | 17 ++++++++++- unittests/readconfigfromshm.php | 19 ++++++++--- unittests/shmtest.php | 29 ++++++++++++++++++ 8 files changed, 112 insertions(+), 85 deletions(-) create mode 100644 unittests/shmtest.php diff --git a/bin/cbfwrd.php b/bin/cbfwrd.php index b8360e5..4abcbbd 100644 --- a/bin/cbfwrd.php +++ b/bin/cbfwrd.php @@ -19,14 +19,16 @@ glcas_pluginLoader(); error_log("CBFWRD starting"); +$comms = new Comms; -$config = new Config(); -global $config; if(isset($argv[1])) { + // we get called here on boot. if($argv[1] == "boot") { - // we get called here on boot. - $config->bootHardware(); + $config = new Config(false); + $config->findHardware(); + $conf = $config->getConfig(); + $comms->putConfig($conf, 1); exit(0); } } @@ -37,10 +39,18 @@ $cont = true; // setup our sem/shm stuff // do the initial stuff -$comms = new Comms; -$config->loadConfig(); +$config = new Config(true); +$config->loadConfigFile(); $rawconf = $config->getConfig(); $comms->putConfig($rawconf); +$config->applyConfig(); +$runconf["status"] = "nochange"; +$comms->putConfig($runconf, 2); +$res = $comms->getConfig(3); +if($res === false) { + $comms->putConfig($runconf, 3); +} + while($cont) { $msg = $comms->waitForMessage(); @@ -59,30 +69,9 @@ function cbfwd_commandProcessor($command) $comms->lockConfigs(); switch($cmds[0]) { - case "goodone": - break; - - // interface change requests - case "interface": - $int = $cmds[1]; - if($cmds[2] == "changename") { - $rawconf = $comms->getConfig(); - if(isset($rawconf["interface"]["$int"]["name"])) $rawconf["interface"]["$int"]["name"] = $cmds[3]; - $comms->putConfig($rawconf); - } + case "applyclientconfig": break; - case "zone": - $cmd = $cmds[1]; - switch($cmd) { - case "add": - $zonename = $cmds[2]; - $rawconf = $comms->getConfig(); - $rawconf["zone"][$zonename]["name"] = 1; - $comms->putConfig($rawconf); - break; - - } } $comms->unlockConfigs(); } diff --git a/doco/configs.txt b/doco/configs.txt index 7184e3b..47d3e5b 100644 --- a/doco/configs.txt +++ b/doco/configs.txt @@ -3,4 +3,9 @@ there are 3 areas in shared memory reserved for configuration. 0 - fs configuration 1 - boot hardware config 2 - running config (deltas from 0) -3 - web config (contains only deltas from 0). \ No newline at end of file +3 - web config (contains only deltas from 0). + +web config writes directly to 3 and messages cbrwrd about stuff. + +config layout is like this: + diff --git a/libcbfwr/comms.php b/libcbfwr/comms.php index a11f9b3..b0c4f04 100644 --- a/libcbfwr/comms.php +++ b/libcbfwr/comms.php @@ -1,11 +1,12 @@ semres); - $shm_space = shm_attach($STORE_KEY, 16*1024*1024); + $shm_space = shm_attach($STORE_KEY, $CONF_STORE_SIZE); $config = shm_get_var($shm_space, $type); sem_release($this->semres); @@ -43,9 +44,9 @@ class Comms { // type = 3 for web config function putConfig($config, $type=0) { - global $MESSAGE_KEY, $LOCKING_KEY, $STORE_KEY; + global $MESSAGE_KEY, $LOCKING_KEY, $STORE_KEY, $CONF_STORE_SIZE; sem_acquire($this->semres); - $shm_space = shm_attach($STORE_KEY, 16*1024*1024); + $shm_space = shm_attach($STORE_KEY, $CONF_STORE_SIZE); shm_put_var($shm_space, $type, $config); sem_release($this->semres); } diff --git a/libcbfwr/config.php b/libcbfwr/config.php index 7bffc1d..3c7a54f 100644 --- a/libcbfwr/config.php +++ b/libcbfwr/config.php @@ -4,11 +4,10 @@ class Config { // here we load a config if we can find it // there are two sides to every class, the fwd side // and the web page site (command line is web for all intents) - function __construct() + function __construct($look_for_config = false) { - global $AM_DAEMON; - if($AM_DAEMON) { + if($look_for_config) { $this->config_file = null; if(file_exists("../var/fw.conf")) { $this->config_file = realpath("../var/fw.conf"); @@ -28,47 +27,18 @@ class Config { $this->config_file = "/var/lib/fwd/fw.conf"; } else if(is_dir("../var/")) { echo "no config file found. Will use ../var/fw.conf for now\n"; - $this->config_file = "../var/"; + $this->config_file = "../var/fw.conf"; } else { echo "No directory where i can create a config, bailing\n"; $this->config["status"] = "nodir"; } } - - - } } - function bootHardware() + function loadConfig($config) { - $this->findHardware(); - - $mycomm = new Comms(); - $mycomm->putConfig($this->config, 1); - - } - - function loadConfig() - { - global $AM_DAEMON; - - if($AM_DAEMON) { - if($this->config["status"] == "conf") { - $this->loadConfigFile($this->config_file); - $this->applyConfig(); - $this->findHardware(); - - $mycomm = new Comms(); - $mycomm->putConfig($this->config); - $this->boot_config = $mycomm->getConfig(1); - } - } else { - $mycomm = new Comms(); - $this->config = $mycomm->getConfig(0); - $this->boot_config = $mycomm->getConfig(1); - - } + $this->config = $config; } function findHardware() @@ -105,11 +75,6 @@ class Config { return $this->config; } - function getBootConfig() - { - return $this->boot_config; - } - function applyConfig() { global $AM_DAEMON; @@ -119,10 +84,21 @@ class Config { return true; } - function loadConfigFile($file) + function mergeConfig($configone, $configtwo) { - global $AM_DAEMON; - if(!$AM_DAEMON) return true; + // yep + } + + function loadConfigFile($file=null) + { + + if($file == null) { + if($this->config["status"] == "nodir") { + return "nodir"; + } else $file = $this->config_file; + } + + if(!file_exists($file)) return "noconf"; $fp = fopen($file, "r"); @@ -351,7 +327,6 @@ class Config { private $config_file; private $config; - private $boot_config; }; ?> \ No newline at end of file diff --git a/libcbfwr/lib.php b/libcbfwr/lib.php index a179924..67bf0e3 100644 --- a/libcbfwr/lib.php +++ b/libcbfwr/lib.php @@ -18,6 +18,10 @@ error_log("added libglcas path as $adpath"); set_include_path(get_include_path().PATH_SEPARATOR.$adpath); */ + +global $BASE_URL; + + require_once("libcbfwr/config.php"); require_once("libcbfwr/comms.php"); require_once("libcbfwr/web.php"); diff --git a/libcbfwr/web.php b/libcbfwr/web.php index 66e7cdf..87e7bf4 100644 --- a/libcbfwr/web.php +++ b/libcbfwr/web.php @@ -26,10 +26,15 @@ class CBFWWeb { $bodycontent = null; $conf = new Config(); - $conf->loadConfig(); + $comms = new Comms(); + $conf->loadConfig($comms->getConfig(0)); $rconfig = $conf->getConfig(); if($rconfig["status"] != "conf") { + if($rconfig["status"] == "nodir") { + cbfw_startnodir(); + return 0; + } cbfw_startinstaller(); return 0; } @@ -292,5 +297,15 @@ Now, tell me where you want me to create the webconfig file:
+ +

Cant Run

+There is no config and no directory where i can store one. Login to the server and either create /var/run/cbfwr or /var + + \ No newline at end of file diff --git a/unittests/readconfigfromshm.php b/unittests/readconfigfromshm.php index ce5a3fe..d5c585e 100644 --- a/unittests/readconfigfromshm.php +++ b/unittests/readconfigfromshm.php @@ -19,11 +19,20 @@ glcas_pluginLoader(); $comms = new Comms(); -while(true) { - sleep(5); - $conf = $comms->getConfig(); +echo "0 - fs config"; +$conf = $comms->getConfig(0); +print_r($conf); - print_r($conf); -} +echo "1 - boot hardware"; +$conf = $comms->getConfig(1); +print_r($conf); + +echo "2 - running config"; +$conf = $comms->getConfig(2); +print_r($conf); + +echo "3 - client config"; +$conf = $comms->getConfig(3); +print_r($conf); ?> \ No newline at end of file diff --git a/unittests/shmtest.php b/unittests/shmtest.php new file mode 100644 index 0000000..6636c22 --- /dev/null +++ b/unittests/shmtest.php @@ -0,0 +1,29 @@ +getConfig(0); + + print_r($conf); +} + +?> \ No newline at end of file -- 1.7.0.4