From 0027de0a544438f146cfc94f005fd6f4ba9f94d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ermal=20Lu=E7i?= Date: Fri, 8 May 2009 18:42:37 +0000 Subject: * Create two new functions lock($subsystem)/unlock() to have more reliable locking using semaphores. This function can sleep till the resource is free and can help find not well behaving code. * Remove most of the config_lock/config_unlock logics on the whole scripts/pages it is an abuse of this. If any sybsytem wants to lock can do so with its own lock. * Lock the config when doing a filter reload to avoid parallell recursion on this function, since it is not reentrant. This compenstates for the removal of lock aquiring from the scripts/pages. * config_lock/config_unlock are now compate shims that do nothing. They are preserved since packages 'abuse' them too. --- etc/inc/util.inc | 66 +++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 53 insertions(+), 13 deletions(-) (limited to 'etc/inc/util.inc') diff --git a/etc/inc/util.inc b/etc/inc/util.inc index 8968428..e1bc857 100644 --- a/etc/inc/util.inc +++ b/etc/inc/util.inc @@ -77,6 +77,53 @@ function killbyname($procname) { mwexec("/usr/bin/killall " . escapeshellarg($procname)); } +function config_lock() { + log_error("config_lock() is depricated please use lock()."); + return; +} +function config_unlock() { + log_error("config_unlock() is depricated please use unlock()."); + return; +} + +/* lock configuration file */ +function lock($file) { + global $g, $cfglckkeyconsumers; + + if (!$file) + die("WARNING: You must give a name as parameter to lock() function."); + if (!file_exists("{$g['tmp_path']}/{$lock}.lock")) + @touch("{$g['tmp_path']}/{$lock}.lock"); + $config_lock_key = ftok("{$g['tmp_path']}/{$lock}.lock", 'a'); + $cfglckkey = sem_get($config_lock_key, 1); + $cfglckkeyconsumers++; + if (!sem_acquire($cfglckkey)) { + log_error("WARNING: lock() - Could not acquire {$lock} lock!"); + sem_remove($cfglckkey); + return NULL; + } else if ($g['debug']) + log_error("lock() - Got config lock."); + + return $cfglckkey; +} + +/* unlock configuration file */ +function unlock($cfglckkey = 0) { + global $g, $cfglckkeyconsumers; + + if (!$cfglckkey) + return; + + if (!sem_release($cfglckkey)) + log_error("WARNING: unlock() - Could not unlock config lock."); + else { + if ($g['debug']) + log_error("Released config lock."); + sem_remove($cfglckkey); + } + $cfglckkeyconsumers--; +} + function is_module_loaded($module_name) { $running = `/sbin/kldstat | grep {$module_name} | /usr/bin/grep -v grep | /usr/bin/wc -l`; if (intval($running) >= 1) @@ -803,19 +850,12 @@ function run_plugins($directory) { $files = return_dir_as_array($directory); if (is_array($files)) { foreach ($files as $file) { - if($file) { - $text = file_get_contents($directory . $file); - if($text) { - if(stristr($file, ".sh") == true) { - mwexec($directory . $file . " start"); - } else { - if(!stristr($file,"CVS")) { - if($g['booting'] == true) - echo "\t{$file}... "; - require_once($directory . $file); - } - } - } + if (stristr($file, ".sh") == true) + mwexec($directory . $file . " start"); + else if (!stristr($file,"CVS")) { + if ($g['booting'] == true) + echo "\t{$file}... "; + require_once($directory . $file); } } } -- cgit v1.1