From: Frank Brehm Date: Wed, 19 Oct 2005 15:25:36 +0000 (+0000) Subject: Sichereres Aufrufen von gammu X-Git-Url: https://git.uhu-banane.org/?a=commitdiff_plain;h=ed716728b1e8669c96f0603567c1189fad036793;p=scripts%2Fsolaris.git Sichereres Aufrufen von gammu --- diff --git a/smsd.pl b/smsd.pl index c00120d..b852bcc 100755 --- 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;