summaryrefslogtreecommitdiffstats
path: root/etc/inc/util.inc
diff options
context:
space:
mode:
authorErmal <eri@pfsense.org>2012-07-31 11:07:53 +0000
committerErmal <eri@pfsense.org>2012-07-31 11:07:53 +0000
commiteb295a1bcb98c3631ee579cdf7dc2150d6393453 (patch)
tree6d2a0dffcb7b47f75485ff7138ec30d2cc338074 /etc/inc/util.inc
parentfa5c53fb3e4268b8d4ab801cd7c397020fe9ee46 (diff)
downloadpfsense-eb295a1bcb98c3631ee579cdf7dc2150d6393453.zip
pfsense-eb295a1bcb98c3631ee579cdf7dc2150d6393453.tar.gz
There is no need to remove the @ from function names. Also properly unlock in case of exception. Size is constant and we know it no need for extra call to shmop. Put some more error checking just in case
Diffstat (limited to 'etc/inc/util.inc')
-rw-r--r--etc/inc/util.inc54
1 files changed, 27 insertions, 27 deletions
diff --git a/etc/inc/util.inc b/etc/inc/util.inc
index 2dad476..ebbf702 100644
--- a/etc/inc/util.inc
+++ b/etc/inc/util.inc
@@ -187,60 +187,60 @@ function send_multiple_events($cmds) {
}
function refcount_init($reference) {
- /* Take out a lock while creating the shared memory, just in case something else tries at the same time. */
- $shm_lck_filename = "shm$reference";
- $shm_lck = lock($shm_lck_filename, LOCK_EX);
- $shm_size = 10;
- $shmid = shmop_open($reference, "c", 0644, $shm_size);
- shmop_write($shmid, str_pad("0", 10, "\x0", STR_PAD_RIGHT), 0);
- shmop_close($shmid);
- unlock($shm_lck);
+ $shmid = @shmop_open($reference, "c", 0644, 10);
+ @shmop_write($shmid, str_pad("0", 10, "\x0", STR_PAD_RIGHT), 0);
+ @shmop_close($shmid);
}
function refcount_reference($reference) {
+ /* Take out a lock across the shared memory read, increment, write sequence to make it atomic. */
+ $shm_lck = lock("shm{$reference}", LOCK_EX);
try {
- /* The first time through the shared memory will not be there. */
- /* So suppress the warning here and handle the init in the if. */
+ /* NOTE: A warning is generated when shared memory does not exist */
$shmid = @shmop_open($reference, "w", 0, 0);
if (!$shmid) {
refcount_init($reference);
- $shmid = shmop_open($reference, "w", 0, 0);
+ $shmid = @shmop_open($reference, "w", 0, 0);
+ if (!$shmid) {
+ log_error(gettext("Could not open shared memory {$reference}"));
+ return;
+ }
}
- /* Take out a lock across the shared memory read, increment, write sequence to make it atomic. */
- $shm_lck_filename = "shm$reference";
- $shm_lck = lock($shm_lck_filename, LOCK_EX);
- $shm_size = shmop_size($shmid);
- $shm_data = shmop_read($shmid, 0, $shm_size);
+ $shm_data = @shmop_read($shmid, 0, 10);
$shm_data = intval($shm_data) + 1;
- shmop_write($shmid, str_pad($shm_data, $shm_size, "\x0", STR_PAD_RIGHT), 0);
- shmop_close($shmid);
+ @shmop_write($shmid, str_pad($shm_data, 10, "\x0", STR_PAD_RIGHT), 0);
+ @shmop_close($shmid);
unlock($shm_lck);
} catch (Exception $e) {
log_error($e->getMessage());
+ unlock($shm_lck);
}
return $shm_data;
}
function refcount_unreference($reference) {
+ /* Take out a lock across the shared memory read, decrement, write sequence to make it atomic. */
+ $shm_lck = lock("shm{$reference}", LOCK_EX);
try {
- /* We assume that the shared memory exists. */
$shmid = @shmop_open($reference, "w", 0, 0);
- /* Take out a lock across the shared memory read, decrement, write sequence to make it atomic. */
- $shm_lck_filename = "shm$reference";
- $shm_lck = lock($shm_lck_filename, LOCK_EX);
- $shm_size = shmop_size($shmid);
- $shm_data = @shmop_read($shmid, 0, $shm_size);
+ if (!$shmid) {
+ refcount_init($reference);
+ log_error(gettext("Could not open shared memory {$reference}"));
+ return;
+ }
+ $shm_data = @shmop_read($shmid, 0, 10);
$shm_data = intval($shm_data) - 1;
if ($shm_data < 0) {
//debug_backtrace();
log_error(sprintf(gettext("Reference %s is going negative, not doing unreference."), $reference));
} else
- shmop_write($shmid, str_pad($shm_data, $shm_size, "\x0", STR_PAD_RIGHT), 0);
- shmop_close($shmid);
+ @shmop_write($shmid, str_pad($shm_data, 10, "\x0", STR_PAD_RIGHT), 0);
+ @shmop_close($shmid);
unlock($shm_lck);
} catch (Exception $e) {
log_error($e->getMessage());
+ unlock($shm_lck);
}
return $shm_data;
@@ -1849,4 +1849,4 @@ function setup_library_paths() {
}
}
-?> \ No newline at end of file
+?>
OpenPOWER on IntegriCloud