]> Frank Brehm's Git Trees - scripts/solaris.git/commitdiff
Sichereres Aufrufen von gammu
authorFrank Brehm <frank@brehm-online.com>
Wed, 19 Oct 2005 15:25:36 +0000 (15:25 +0000)
committerFrank Brehm <frank@brehm-online.com>
Wed, 19 Oct 2005 15:25:36 +0000 (15:25 +0000)
smsd.pl

diff --git a/smsd.pl b/smsd.pl
index c00120d73784c70a87efffa9d8427b287d2e6b0b..b852bccc7d71aa6873016bd53f82287d2241aa7a 100755 (executable)
--- a/smsd.pl
+++ b/smsd.pl
@@ -18,6 +18,8 @@ use Pod::Usage;
 use Mail::Address;
 use Email::Valid;
 use IPC::Open3;
+use English '-no_match_vars';
+use Config;
 
 $| = 1;
 
@@ -140,10 +142,13 @@ my $do        = 1;
 my $reload    = 0;
 my $childs    = 0;
 my $zombies   = 0;
+my $timeout   = 30;
 
-my ( $help, $show_version, $cmdline_verbose );
+my ( $help, $show_version, $cmdline_verbose, $signal_name, $i );
 my $o_inbox  = 1;
 my $o_outbox = 1;
+my %signo;
+my @signame;
 
 my $config_file = $0;
 $config_file =~ s/\.pl$//i;
@@ -152,6 +157,16 @@ $config_file .= ".conf";
 my $o_config_file  = undef;
 my $dedicated_conf = undef;
 
+# Signalnamen einlesen ...
+defined $Config{'sig_name'} || die "Keine Signame definiert ?";
+$i = 0;
+
+foreach $signal_name ( split(' ', $Config{'sig_name'}) ) {
+    $signo{$signal_name} = $i;
+    $signame[$i] = $signal_name;
+    $i++;
+}
+
 unless (
   GetOptions(
     "conf|config|c=s"  => \$o_config_file,
@@ -744,16 +759,23 @@ sub perform_send( $ ) {
   my $sms = shift;
 
   my ( @EMailRcpt, @R, @S );
-  my ( $rec, $CmdLine, $smsc, $res, $mail_subject, $out );
+  my ( $rec, $CmdLine, $smsc, $res, $mail_subject, $out, $gammu_pid );
 
   my $rcpt = $sms->{'header'}->recipient();
   my $emails = $sms->{'header'}->confirmemailaddress() ||
                $sms->{'header'}->confirmemailrecipient() ||
                $sms->{'header'}->confirmemail();
 
-  my $params = sprintf "--sendsms TEXT %%s -autolen %d -validity MAX", $maxlen;
+  # my $params = sprintf "--sendsms TEXT %%s -autolen %d -validity MAX", $maxlen;
+  my @Params = (
+    '--sendsms',
+    'TEXT'      => 'recipient',
+    '-autolen'  => $maxlen,
+    '-validity' => 'MAX',
+  );
   $smsc = $sms->{'header'}->smscnumber() || $sms->{'header'}->smsc();
-  $params .= sprintf( " -smscnumber %s", $smsc ) if $smsc;
+  push @Params, '-smscnumber' => $smsc if $smsc;
+  #$params .= sprintf( " -smscnumber %s", $smsc ) if $smsc;
 
   if ( $emails ) {
 
@@ -774,11 +796,58 @@ sub perform_send( $ ) {
 
   }
 
-  $CmdLine = "| $GammuExe " . sprintf( $params, $sms->{'header'}->recipient() );
+  $Params[2] = $sms->{'header'}->recipient();
+  #$CmdLine = "$GammuExe " . sprintf( $params, $sms->{'header'}->recipient() );
+  $CmdLine = join " ", $GammuExe, @Params;
   debug "Calling '" . $CmdLine . "'.";
   $res = 1;
   $mail_subject = sprintf( "SMS to %s successfully sent.", $sms->{'header'}->recipient() );
+
   unless ( $test ) {
+
+    {
+
+      # sicheres Forken
+      $gammu_pid = open( GAMMU, "|-");
+
+      local $SIG{'PIPE'} = sub { die "whoops, $GammuExe pipe broke" };
+      local $SIG{'ALRM'} = sub { die "Timeout\n" };
+
+      if ( $gammu_pid ) {  # parent
+
+        eval {
+          alarm $timeout;
+          print GAMMU $sms->body();
+          close( GAMMU ) || error "kid exited $?";
+        };
+
+        if ( $@ ) {
+
+          chomp $@;
+          error "Irgendwas ging schief: " . $@;
+
+          if ( kill 0, $gammu_pid ) {
+            error "Kindprozess $gammu_pid laeuft noch, wird gekillt.";
+            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!";
+              kill $signo{'KILL'}, $gammu_pid;
+            }
+          }
+
+        }
+
+      } else { # child
+
+        ($EUID, $EGID) = ($UID, $GID);
+        exec($GammuExe, @Params) || error( "can't exec program: $!"), die;
+        # Bis hierhin sollte ich niemals kommen
+      }
+
+    }
+
     if ( open GAMMU, $CmdLine ) {
       print GAMMU $sms->body();
       close GAMMU;