diff options
author | Ermal <eri@pfsense.org> | 2013-09-04 08:48:23 +0000 |
---|---|---|
committer | Ermal <eri@pfsense.org> | 2013-09-04 08:48:23 +0000 |
commit | 8171a2c2de98c8f505e99ddfb4859058cdb32cba (patch) | |
tree | 7e48ccaf7a971ccc1ab8fb2f7a2e9511d510eaaa /etc/inc/util.inc | |
parent | f33dcc5c79c54af7daf91a81cfdd7f489e8cb67c (diff) | |
download | pfsense-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.
Diffstat (limited to 'etc/inc/util.inc')
-rw-r--r-- | etc/inc/util.inc | 33 |
1 files changed, 33 insertions, 0 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; |