X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=src%2FShm.pl;h=efdfced295665d79c98598e3a26facd549272e38;hb=f7cae48a17d6decd0a9bd997188271daa0a885b1;hp=34b68f2f1e7d1d3314f330cb4809fd15c835f97e;hpb=cc8ab9a5235b29e43af923836d24579413c882d6;p=infobot.git diff --git a/src/Shm.pl b/src/Shm.pl index 34b68f2..efdfced 100644 --- a/src/Shm.pl +++ b/src/Shm.pl @@ -5,7 +5,8 @@ # Created: 20000124 # -#use strict; +# use strict; # TODO + use POSIX qw(_exit); sub openSHM { @@ -13,7 +14,7 @@ sub openSHM { my $size = 2000; if (&IsParam("noSHM")) { - &status("Created shared memory: disabled. [bot may become unreliable]"); + &status("Shared memory: Disabled. WARNING: bot may become unreliable"); return 0; } @@ -47,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: $!"); @@ -63,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{ircNick}, $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: $!"); } } @@ -114,13 +124,11 @@ sub addForked { next; } - # don't kill parent! - if ($pid == $$) { - &status("Fork: pid == \$\$ ($$)"); - next; - } + if ($pid == $bot_pid) { + # don't kill parent, just warn. + &status("Fork: pid == \$bot_pid == \$\$ ($bot_pid)"); - if ( -d "/proc/$pid") { + } elsif ( -d "/proc/$pid") { # pid != bot_pid. &status("Fork: killing $name ($pid)"); kill 9, $pid; } @@ -135,10 +143,11 @@ sub addForked { if ($count > 3) { # 3 seconds. my $list = join(', ', keys %forked); if (defined $who) { - &msg($who, "already running ($list) => exceeded allowed forked processes count (1?)."); + &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; } @@ -216,4 +225,54 @@ sub delForked { POSIX::_exit(0); } +sub shmFlush { + return if ($$ != $::bot_pid); # fork protection. + + if (@_) { + &ScheduleThis(5, "shmFlush"); + return if ($_[0] eq "2"); + } + + my $time; + my $shmmsg = &shmRead($shm); + # 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) { + next if (/^$/); + &VERB("shm: Processing '$_'.",2); + + if (/^DCC SEND (\S+) (\S+)$/) { + my ($nick,$file) = ($1,$2); + if (exists $dcc{'SEND'}{$who}) { + &msg($nick, "DCC already active."); + } else { + &DEBUG("shm: dcc sending $2 to $1."); + $conn->new_send($1,$2); + $dcc{'SEND'}{$who} = time(); + } + } elsif (/^SET FORKPID (\S+) (\S+)/) { + $forked{$1}{PID} = $2; + } elsif (/^DELETE FORK (\S+)$/) { + delete $forked{$1}; + } elsif (/^EVAL (.*)$/) { + &DEBUG("evaling '$1'."); + eval $1; + } else { + &DEBUG("shm: unknown msg. ($_)"); + } + } + + &shmWrite($shm,"") if ($shmmsg ne ""); +} + 1;