summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorErmal <eri@pfsense.org>2013-09-04 08:48:23 +0000
committerErmal <eri@pfsense.org>2013-09-04 08:48:23 +0000
commit8171a2c2de98c8f505e99ddfb4859058cdb32cba (patch)
tree7e48ccaf7a971ccc1ab8fb2f7a2e9511d510eaaa
parentf33dcc5c79c54af7daf91a81cfdd7f489e8cb67c (diff)
downloadpfsense-8171a2c2de98c8f505e99ddfb4859058cdb32cba.zip
pfsense-8171a2c2de98c8f505e99ddfb4859058cdb32cba.tar.gz
Introduce two new functions to be used on locking.
- try_lock: used for trying to get an EXCLUSIVE lock for a specified timeout by default of 5 - unlock_force: which just releases any locks held on a specified lock Use this new functions on rc.openvpn to avoid spurious stale locks around.
-rw-r--r--etc/inc/util.inc33
-rwxr-xr-xetc/rc.openvpn8
2 files changed, 40 insertions, 1 deletions
diff --git a/etc/inc/util.inc b/etc/inc/util.inc
index e75b5cb..c276c21 100644
--- a/etc/inc/util.inc
+++ b/etc/inc/util.inc
@@ -132,6 +132,32 @@ function lock($lock, $op = LOCK_SH) {
}
}
+function try_lock($lock, $timeout = 5) {
+ global $g, $cfglckkeyconsumers;
+ if (!$lock)
+ die(gettext("WARNING: You must give a name as parameter to try_lock() function."));
+ if (!file_exists("{$g['tmp_path']}/{$lock}.lock")) {
+ @touch("{$g['tmp_path']}/{$lock}.lock");
+ @chmod("{$g['tmp_path']}/{$lock}.lock", 0666);
+ }
+ $cfglckkeyconsumers++;
+ if ($fp = fopen("{$g['tmp_path']}/{$lock}.lock", "w")) {
+ $trycounter = 0;
+ while(!flock($fp, LOCK_EX | LOCK_NB)) {
+ if ($trycounter >= $timeout) {
+ fclose($fp);
+ return NULL;
+ }
+ sleep(1);
+ $trycounter++;
+ }
+
+ return $fp;
+ }
+
+ return NULL;
+}
+
/* unlock configuration file */
function unlock($cfglckkey = 0) {
global $g, $cfglckkeyconsumers;
@@ -140,6 +166,13 @@ function unlock($cfglckkey = 0) {
return;
}
+/* unlock forcefully configuration file */
+function unlock_force($lock) {
+ global $g;
+
+ @unlink("{$g['tmp_path']}/{$lock}.lock");
+}
+
function send_event($cmd) {
global $g;
diff --git a/etc/rc.openvpn b/etc/rc.openvpn
index 4cabffe..f60ad56 100755
--- a/etc/rc.openvpn
+++ b/etc/rc.openvpn
@@ -80,7 +80,13 @@ if(is_array($config['openvpn']['openvpn-server']) || is_array($config['openvpn']
} else
return;
-$openvpnlck = lock('openvpn', LOCK_EX);
+$openvpnlck = try_lock('openvpn', 10);
+if (!$openvpnlck) {
+ log_error(gettext("Could not obtain openvpn lock for executing rc.openvpn for more than 10 seconds continuing..."));
+ unlock_force('openvpn');
+ $openvpnlck = lock('openvpn', LOCK_EX);
+}
+
$arg_array = explode(",",$argument);
foreach ($arg_array as $arg_element) {
$gwgroups = array();
OpenPOWER on IntegriCloud