]> git.donarmstrong.com Git - infobot.git/blobdiff - src/Shm.pl
* Try updating Debian.pl again
[infobot.git] / src / Shm.pl
index efdfced295665d79c98598e3a26facd549272e38..646c2097ff9c8bbeaa001a3b808ac2caff15bced 100644 (file)
@@ -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.");
@@ -48,12 +54,16 @@ sub shmRead {
     return '' if (&IsParam("noSHM"));
 
     if (shmread($key,$retval,$position,$size)) {
-       &DEBUG("shmRead($key): $retval");
+       #&DEBUG("shmRead($key): $retval");
        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;
@@ -82,7 +94,7 @@ sub shmWrite {
     my $read = &shmRead($key);
     $read =~ s/\0+//g;
     if ($read eq "") {
-       $str = sprintf("%s:%d:%d: ", $param{ircNick}, $bot_pid, time());
+       $str = sprintf("%s:%d:%d: ", $param{ircUser}, $bot_pid, time());
     } else {
        $str = $read ."||". $str;
     }
@@ -229,7 +241,7 @@ sub shmFlush {
     return if ($$ != $::bot_pid); # fork protection.
 
     if (@_) {
-       &ScheduleThis(5, "shmFlush");
+       &ScheduleThis(15, "shmFlush");
        return if ($_[0] eq "2");
     }