]> Frank Brehm's Git Trees - scripts/solaris.git/commitdiff
incoming ebenfalls auf sicheres forken umgestellt
authorFrank Brehm <frank@brehm-online.com>
Thu, 20 Oct 2005 08:37:02 +0000 (08:37 +0000)
committerFrank Brehm <frank@brehm-online.com>
Thu, 20 Oct 2005 08:37:02 +0000 (08:37 +0000)
smsd.pl

diff --git a/smsd.pl b/smsd.pl
index 0746c21e341da49a08dbaffe80619e2c22bbfb68..27da9f23c5e0296f8816e1d100c920286b110343 100755 (executable)
--- a/smsd.pl
+++ b/smsd.pl
@@ -23,7 +23,12 @@ use Config;
 
 $| = 1;
 
-our $VERSION = '1.0.3';
+my $Revisn = <<'ENDE';
+ $Revision$
+ENDE
+$Revisn =~ s/^.*:\s*(\S+)\s*\$.*/$1/s;
+
+our $VERSION = '1.1.' . $Revisn;
 
 =head1 NAME
 
@@ -527,19 +532,72 @@ Performs all incoming SMS-files.
 
 sub do_incoming() {
 
-  my ( $i, $CmdLine, $result, $sms, $id, $count, $osms, $filename, $lockname, $error_code );
+  my ( $i, $CmdLine, $result, $sms, $id, $count, $osms, $filename, $lockname, $error_code, $gammu_pid );
 
   my ( %SMS, %SMSChains );
   my ( @SMS, @Order, @SmsDelete );
 
+  my @Params = (
+    '--getsms',
+    $inbox_folder_nr,
+    0
+  );
+
   $mark = "incoming";
   debug "Reading incoming SMS ...\n";
 
   for ( $i = 1; $i <= $count_locations; $i++ ) {
 
-    $CmdLine = $GammuExe . " --getsms " . $inbox_folder_nr . " " . $i;
+    $Params[2] = $i;
+    $CmdLine = join( " ", $GammuExe, @Params );
+    #$CmdLine = $GammuExe . " --getsms " . $inbox_folder_nr . " " . $i;
     debug "Executing: '" . $CmdLine . "'", 2;
-    $result = `$CmdLine`;
+    #$result = `$CmdLine`;
+
+    # sicheres Forken
+    $gammu_pid = open( GAMMU, "|-");
+
+    local $SIG{'ALRM'} = sub { die "Timeout\n" };
+
+    if ( $gammu_pid ) {  # parent
+
+      $result = "";
+      eval {
+        alarm $timeout;
+        while ( <GAMMU> ) {
+          $result .= $_;
+        }
+        close( GAMMU ) || error "kid exited $?", "warning";
+      };
+      alarm 0;
+
+      if ( $@ ) {
+
+        chomp $@;
+        error "Irgendwas ging schief: " . $@, "warning";
+
+        if ( kill 0, $gammu_pid ) {
+          error "Kindprozess $gammu_pid laeuft noch, wird gekillt.", "warning";
+          kill $signo{'TERM'}, $gammu_pid;
+          error "Warte 2 Sekunden ...";
+          sleep 2;
+          if ( kill 0, $gammu_pid ) {
+            error "Kindprozess $gammu_pid laeuft immer noch, wird mit der Keule erschlagen!", "err";
+            kill $signo{'KILL'}, $gammu_pid;
+          }
+        }
+
+      }
+      alarm 0;
+
+    } else { # child
+
+      ($EUID, $EGID) = ($UID, $GID);
+      exec($GammuExe, @Params) || error( "can't exec program: $!", "err"), die;
+      # Bis hierhin sollte ich niemals kommen
+
+    }
+
     unless ( $? ) {   # Erfolgreich!!!
       $sms = new SMS();
       $sms->parse_from_gammu( $result );
@@ -818,22 +876,22 @@ sub perform_send( $ ) {
         eval {
           alarm $timeout;
           print GAMMU $sms->body();
-          close( GAMMU ) || error "kid exited $?";
+          close( GAMMU ) || error "kid exited $?", "warning";
         };
         alarm 0;
 
         if ( $@ ) {
 
           chomp $@;
-          error "Irgendwas ging schief: " . $@;
+          error "Irgendwas ging schief: " . $@, "warning";
 
           if ( kill 0, $gammu_pid ) {
-            error "Kindprozess $gammu_pid laeuft noch, wird gekillt.";
+            error "Kindprozess $gammu_pid laeuft noch, wird gekillt.", "warning";
             kill $signo{'TERM'}, $gammu_pid;
             error "Warte 2 Sekunden ...";
             sleep 2;
             if ( kill 0, $gammu_pid ) {
-              error "Kindprozess $gammu_pid laeuft immer noch, wird mit der Keule erschlagen!";
+              error "Kindprozess $gammu_pid laeuft immer noch, wird mit der Keule erschlagen!", "err";
               kill $signo{'KILL'}, $gammu_pid;
             }
           }
@@ -844,7 +902,7 @@ sub perform_send( $ ) {
       } else { # child
 
         ($EUID, $EGID) = ($UID, $GID);
-        exec($GammuExe, @Params) || error( "can't exec program: $!"), die;
+        exec($GammuExe, @Params) || error( "can't exec program: $!", "err"), die;
         # Bis hierhin sollte ich niemals kommen
       }