From fbd141c067fd24f90775616a4b6d15b2efa8be3d Mon Sep 17 00:00:00 2001
From: dondelelcaro <dondelelcaro@c11ca15a-4712-0410-83d8-924469b57eb5>
Date: Fri, 23 Jun 2006 20:09:31 +0000
Subject: [PATCH]  * Don't open shm segments if a read fails

git-svn-id: https://svn.code.sf.net/p/infobot/code/branches/don/dpkg@1295 c11ca15a-4712-0410-83d8-924469b57eb5
---
 src/Shm.pl | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/src/Shm.pl b/src/Shm.pl
index cebe4dc..646c209 100644
--- a/src/Shm.pl
+++ b/src/Shm.pl
@@ -9,6 +9,8 @@
 
 use POSIX qw(_exit);
 
+my %shm_keys;
+
 sub openSHM {
     my $IPC_PRIVATE = 0;
     my $size = 2000;
@@ -20,6 +22,10 @@ sub openSHM {
 
     if (defined( $_ = shmget($IPC_PRIVATE, $size, 0777) )) {
 	&status("Created shared memory (shm) key: [$_]");
+	$shm_keys{$_} = {time     => time,
+			 accessed => 0,
+			 key      => $_,
+			};
 	return $_;
     } else {
 	&ERROR("openSHM: failed.");
@@ -52,8 +58,12 @@ sub shmRead {
 	return $retval;
     } else {
 	&ERROR("shmRead: failed: $!");
+	if (exists $shm_keys{$_}) {
+	      closeSHM($key);
+	}
 	### TODO: if this fails, never try again.
-	&openSHM();
+	# What use is opening a SHM segment if we're not going to read it?
+	# &openSHM();
 	return '';
     }
 }
@@ -65,6 +75,8 @@ sub shmWrite {
 
     return if (&IsParam("noSHM"));
 
+    $shm_keys{$keys}{accessed} = 1;
+
     if (length($str) > $size) {
 	&status("ERROR: length(str) (..)>$size...");
 	return;
-- 
2.39.5