X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;ds=sidebyside;f=src%2FShm.pl;h=cebe4dc59b2b8c9a7142c8b4a7761c4207ee5cfc;hb=ad88d63a585d6a206e651a9ea324d8610db7231d;hp=5ab3d186501a89807fff7662fa83aa049adb5b92;hpb=8a79d430aef83402420b5a8378e78cfa01051c93;p=infobot.git diff --git a/src/Shm.pl b/src/Shm.pl index 5ab3d18..cebe4dc 100644 --- a/src/Shm.pl +++ b/src/Shm.pl @@ -48,6 +48,7 @@ sub shmRead { return '' if (&IsParam("noSHM")); if (shmread($key,$retval,$position,$size)) { + #&DEBUG("shmRead($key): $retval"); return $retval; } else { &ERROR("shmRead: failed: $!"); @@ -64,22 +65,30 @@ sub shmWrite { return if (&IsParam("noSHM")); - # NULL hack. - ### TODO: create shmClear to deal with this. - if ($str !~ /^$/) { - my $read = &shmRead($key); - $read =~ s/\0+//g; + if (length($str) > $size) { + &status("ERROR: length(str) (..)>$size..."); + return; + } - if ($str eq "") { - $str = time().": "; # time stamping, null. - } elsif ($read eq "") { - $str = time().": "; # timestamping. - } else { - $str = $read ."||". $str; + if (length($str) == 0) { + # does $size overwrite the whole lot? + # if not, set to 2000. + if (!shmwrite($key, '', $position, $size)) { + &ERROR("shmWrite: failed: $!"); } + return; + } + + my $read = &shmRead($key); + $read =~ s/\0+//g; + if ($read eq "") { + $str = sprintf("%s:%d:%d: ", $param{ircUser}, $bot_pid, time()); + } else { + $str = $read ."||". $str; } - if (!shmwrite($key,$str,$position,$size)) { + if (!shmwrite($key, $str, $position, $size)) { + &DEBUG("shmWrite($key, $str)"); &ERROR("shmWrite: failed: $!"); } } @@ -134,9 +143,9 @@ sub addForked { if ($count > 3) { # 3 seconds. my $list = join(', ', keys %forked); if (defined $who) { - &msg($who, "exceeded allowed forked count: $list"); + &msg($who, "exceeded allowed forked count (shm $shm): $list"); } else { - &status("Fork: I ran too many forked processes :) Giving up $name."); + &status("Fork: I ran too many forked processes :) Giving up $name. Shm: $shm"); } return 0; @@ -220,15 +229,22 @@ sub shmFlush { return if ($$ != $::bot_pid); # fork protection. if (@_) { - &ScheduleThis(5, "shmFlush"); + &ScheduleThis(15, "shmFlush"); return if ($_[0] eq "2"); } my $time; my $shmmsg = &shmRead($shm); - $shmmsg =~ s/\0//g; # remove padded \0's. - if ($shmmsg =~ s/^(\d+): //) { - $time = $1; + # remove padded \0's. + $shmmsg =~ s/\0//g; + return if (length($shmmsg) == 0); + if ($shmmsg =~ s/^(\S+):(\d+):(\d+): //) { + my $n = $1; + my $pid = $2; + $time = $3; + } else { + &status("warn: shmmsg='$shmmsg'."); + return; } foreach (split '\|\|', $shmmsg) {