]> git.donarmstrong.com Git - infobot.git/blobdiff - src/Shm.pl
ws
[infobot.git] / src / Shm.pl
index 136bd165b26544daa7a22725c4f37b69af5c8701..efdfced295665d79c98598e3a26facd549272e38 100644 (file)
@@ -5,7 +5,7 @@
 #   Created: 20000124
 #
 
-if (&IsParam("useStrict")) { use strict; }
+# use strict;  # TODO
 
 use POSIX qw(_exit);
 
@@ -13,6 +13,11 @@ sub openSHM {
     my $IPC_PRIVATE = 0;
     my $size = 2000;
 
+    if (&IsParam("noSHM")) {
+       &status("Shared memory: Disabled. WARNING: bot may become unreliable");
+       return 0;
+    }
+
     if (defined( $_ = shmget($IPC_PRIVATE, $size, 0777) )) {
        &status("Created shared memory (shm) key: [$_]");
        return $_;
@@ -40,7 +45,10 @@ sub shmRead {
     my $size = 3*80;
     my $retval = '';
 
+    return '' if (&IsParam("noSHM"));
+
     if (shmread($key,$retval,$position,$size)) {
+       &DEBUG("shmRead($key): $retval");
        return $retval;
     } else {
        &ERROR("shmRead: failed: $!");
@@ -55,22 +63,32 @@ sub shmWrite {
     my $position = 0;
     my $size = 80*3;
 
-    # NULL hack.
-    ### TODO: create shmClear to deal with this.
-    if ($str !~ /^$/) {
-       my $read = &shmRead($key);
-       $read =~ s/\0+//g;
+    return if (&IsParam("noSHM"));
 
-       if ($str eq "") {
-           $str = time().": ";         # time stamping, null.
-       } elsif ($read eq "") {
-           $str = time().": ";         # timestamping.
-       } else {
-           $str = $read ."||". $str;
+    if (length($str) > $size) {
+       &status("ERROR: length(str) (..)>$size...");
+       return;
+    }
+
+    if (length($str) == 0) {
+       # does $size overwrite the whole lot?
+       # if not, set to 2000.
+       if (!shmwrite($key, '', $position, $size)) {
+           &ERROR("shmWrite: failed: $!");
        }
+       return;
     }
 
-    if (!shmwrite($key,$str,$position,$size)) {
+    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)) {
+       &DEBUG("shmWrite($key, $str)");
        &ERROR("shmWrite: failed: $!");
     }
 }
@@ -92,12 +110,30 @@ sub addForked {
     }
 
     foreach (keys %forked) {
-       my $time = time() - $forked{$_}{Time};
+       my $n = $_;
+       my $time = time() - $forked{$n}{Time};
        next unless ($time > $forker_timeout);
 
        ### TODO: use &time2string()?
-       &WARN("Fork: looks like we lost '$_', executed $time ago.");
-       delete $forked{$_};
+       &WARN("Fork: looks like we lost '$n', executed $time ago");
+
+       my $pid = $forked{$n}{PID};
+       if (!defined $pid) {
+           &WARN("Fork: no pid for $n.");
+           delete $forked{$n};
+           next;
+       }
+
+       if ($pid == $bot_pid) {
+           # don't kill parent, just warn.
+           &status("Fork: pid == \$bot_pid == \$\$ ($bot_pid)");
+
+       } elsif ( -d "/proc/$pid") {    # pid != bot_pid.
+           &status("Fork: killing $name ($pid)");
+           kill 9, $pid;
+       }
+
+       delete $forked{$n};
     }
 
     my $count = 0;
@@ -107,22 +143,35 @@ 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;
        }
 
        $count++;
     }
 
-    if (exists $forked{$name}) {
+    if (exists $forked{$name} and !scalar keys %{ $forked{$name} }) {
+       &WARN("addF: forked{$name} exists but is empty; deleting.");
+       undef $forked{$name};
+    }
+
+    if (exists $forked{$name} and scalar keys %{ $forked{$name} }) {
        my $time        = $forked{$name}{Time};
        my $continue    = 0;
 
-       if (-d "/proc/$forked{$name}{PID}") {
+       $continue++ if ($forked{$name}{PID} == $$);
+
+       if ($continue) {
+           &WARN("hrm.. fork pid == mypid == $$; how did this happen?");
+
+       } elsif ( -d "/proc/$forked{$name}{PID}") {
            &status("fork: still running; good. BAIL OUT.");
+           return 0;
+
        } else {
            &WARN("Found dead fork; removing and resetting.");
            $continue = 1;
@@ -130,8 +179,10 @@ sub addForked {
 
        if ($continue) {
            # NOTHING.
+
        } elsif (time() - $time > 900) {        # stale fork > 15m.
            &status("forked: forked{$name} presumably exited without notifying us.");
+
        } else {                                # fresh fork.
            &msg($who, "$name is already running ". &Time2String(time() - $time));
            return 0;
@@ -139,6 +190,7 @@ sub addForked {
     }
 
     $forked{$name}{Time}       = time();
+    $forked{$name}{PID}                = $$;
     $forkedtime                        = time();
     $count{'Fork'}++;
     return 1;
@@ -154,6 +206,10 @@ sub delForked {
        POSIX::_exit(0);
     }
 
+    if ($name =~ /\.pl/) {
+       &WARN("dF: name is name of source file ($name). FIX IT!");
+    }
+
     &showProc();       # just for informational purposes.
 
     if (exists $forked{$name}) {
@@ -169,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;