[3] | 1 | # |
---|
| 2 | # Copyright (c) 2004-2007 - Consultas, PKG.fr |
---|
| 3 | # |
---|
| 4 | # This file is part of A2P. |
---|
| 5 | # |
---|
| 6 | # A2P is free software; you can redistribute it and/or modify |
---|
| 7 | # it under the terms of the GNU General Public License as published by |
---|
| 8 | # the Free Software Foundation; either version 2 of the License, or |
---|
| 9 | # (at your option) any later version. |
---|
| 10 | # |
---|
| 11 | # A2P is distributed in the hope that it will be useful, |
---|
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 14 | # GNU General Public License for more details. |
---|
| 15 | # |
---|
| 16 | # You should have received a copy of the GNU General Public License |
---|
| 17 | # along with A2P; if not, write to the Free Software |
---|
| 18 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 19 | # |
---|
| 20 | # $Id: Signal.pm 3 2007-10-18 16:20:19Z guillaume $ |
---|
| 21 | # |
---|
| 22 | |
---|
| 23 | package A2P::Signal; |
---|
| 24 | |
---|
| 25 | use strict ; |
---|
| 26 | use integer ; |
---|
| 27 | use Carp qw(longmess); |
---|
| 28 | use sigtrap qw(any normal-signals error-signals old-interface-signals IO URG CLD CHLD); |
---|
| 29 | use A2P::Globals ; |
---|
| 30 | use A2P::Syslog ; |
---|
| 31 | |
---|
| 32 | BEGIN { |
---|
| 33 | use Exporter (); |
---|
| 34 | |
---|
| 35 | our ( $VERSION , @ISA , @EXPORT , @EXPORT_OK ); |
---|
| 36 | |
---|
| 37 | $VERSION = sprintf "%s", q$Rev: 1029 $ =~ /(\d[0-9.]+)\s+/ ; |
---|
| 38 | |
---|
| 39 | @ISA = qw(Exporter); |
---|
| 40 | @EXPORT = qw( $do_init $do_check $do_quit $do_ping $do_test |
---|
| 41 | $Childquit @SigDebug @SigInfo @SigWarn @PIPE ); |
---|
| 42 | @EXPORT_OK = qw(&LogSigMessage); |
---|
| 43 | } |
---|
| 44 | our $VERSION ; |
---|
| 45 | |
---|
| 46 | ############################################################################# |
---|
| 47 | ## Signal handling vars ## |
---|
| 48 | ############################################################################# |
---|
| 49 | our @SigDebug = (); |
---|
| 50 | our @SigInfo = (); |
---|
| 51 | our @SigWarn = (); |
---|
| 52 | |
---|
| 53 | # Socket list to be checked when receiving a SIGPIPE |
---|
| 54 | our @PIPE ; |
---|
| 55 | |
---|
| 56 | # Some used flags |
---|
| 57 | our $do_init = 1 ; |
---|
| 58 | our $do_check = 1 ; |
---|
| 59 | our $do_quit = 0 ; |
---|
| 60 | our $do_ping = 0 ; |
---|
| 61 | our $do_test = 0 ; |
---|
| 62 | our $Childquit = 0 ; |
---|
| 63 | |
---|
| 64 | ############################################################################# |
---|
| 65 | ## Signal handling code ## |
---|
| 66 | ############################################################################# |
---|
| 67 | sub SigABRT { |
---|
| 68 | $do_quit += 5 ; |
---|
| 69 | push @SigWarn , "SIGABRT: Notified to abort ($do_quit)"; |
---|
| 70 | $MUSTQUIT ++ ; |
---|
| 71 | exit(1) if ( $do_quit > 9 ); |
---|
| 72 | $NO_SYSLOG_DEBUG = 0 if $SERVICE_DEBUG ; |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | sub SigQUIT { |
---|
| 76 | push @SigDebug , "Notified to quit by 'SIG$_[0]' signal"; |
---|
| 77 | exit(1) if ( ++ $do_quit > 3 ); |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | sub SigHUP { |
---|
| 81 | push @SigDebug , "SIGHUP received"; |
---|
| 82 | $do_init ++ ; |
---|
| 83 | my $touch_file = $SERVICE_TMP . '/init_' . $Progname ; |
---|
| 84 | if ( -d $SERVICE_TMP and -e $touch_file ) { |
---|
| 85 | my $now = time ; |
---|
| 86 | utime $now, $now, $touch_file ; |
---|
| 87 | |
---|
| 88 | } else { |
---|
| 89 | push @SigWarn , "Can't touch file '$touch_file' on SIGHUP" ; |
---|
| 90 | } |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | sub SigALRM { |
---|
| 94 | $do_check ++ if $DO_PING ; |
---|
| 95 | push @SigDebug , "SIGALRM received"; |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | sub SigDIE { |
---|
| 99 | my $info = $_[0] || "" ; |
---|
| 100 | my $sub ; |
---|
| 101 | chomp $info ; |
---|
| 102 | |
---|
| 103 | # Forget DIE signal when loading Storable.pm and Log/Agent.pm not present |
---|
| 104 | return if ( $info =~ /Log\/Agent.pm/ ); |
---|
| 105 | |
---|
| 106 | my ($package, $filename, $line) = caller(); |
---|
| 107 | |
---|
| 108 | # Forget Encoding integration signal... |
---|
| 109 | return if ( $filename =~ /Encode.pm/ ); |
---|
| 110 | |
---|
| 111 | # As a thread should has shutdown, we should check our services |
---|
| 112 | ++ $do_check ; |
---|
| 113 | |
---|
| 114 | push @SigWarn , "Got '__DIE__' signal", &longmess(@_) ; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | sub SigPIPE { |
---|
| 118 | my ($package, $filename, $line) = caller(); |
---|
| 119 | push @SigDebug , "SIG@_ received at L. $line in $filename: $! $?" ; |
---|
| 120 | unless ( grep { defined($_) } @PIPE ) { |
---|
| 121 | push @SigWarn , "Got '$_[0]' signal" ; |
---|
| 122 | $MUSTQUIT ++ ; |
---|
| 123 | } |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | sub SigINT { |
---|
| 127 | # Just print info in log about where we were called |
---|
| 128 | &Info("debug: SIG" . &longmess(@_)); |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | sub SigWARN { |
---|
| 132 | push @SigWarn , "Got '__WARN__' signal", &longmess(@_) ; |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | sub SigUSR1 { |
---|
| 136 | $do_test ++ ; |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | my $templogger = 0 ; |
---|
| 140 | my $temp_NO_SYSLOG_DEBUG = $NO_SYSLOG_DEBUG ; |
---|
| 141 | sub SigUSR2 { |
---|
| 142 | push @SigInfo , "Got 'USR2' signal", &longmess(@_) ; |
---|
| 143 | $templogger = $loggertid if $loggertid ; |
---|
| 144 | $loggertid = $loggertid ? 0 : $templogger ; |
---|
| 145 | # Switch advanced debugging mode |
---|
| 146 | if ( $ADVANCED_DEBUGGING = $ADVANCED_DEBUGGING ? 0 : 1 ) { |
---|
| 147 | $temp_NO_SYSLOG_DEBUG = $NO_SYSLOG_DEBUG ; |
---|
| 148 | $NO_SYSLOG_DEBUG = 0 ; |
---|
| 149 | } else { |
---|
| 150 | $NO_SYSLOG_DEBUG = $temp_NO_SYSLOG_DEBUG ; |
---|
| 151 | } |
---|
| 152 | &debugdev("ADVANCED_DEBUGGING=$ADVANCED_DEBUGGING NO_SYSLOG_DEBUG=$NO_SYSLOG_DEBUG"); |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | map { |
---|
| 156 | eval q|$SIG{'| . $_ . q|'} = \&Sig| . $_ |
---|
| 157 | } qw( PIPE ABRT QUIT HUP ALRM USR1 USR2 ); |
---|
| 158 | |
---|
| 159 | $SIG{'INT'} = \&SigINT ; |
---|
| 160 | $SIG{'SEGV'} = \&SigPIPE ; |
---|
| 161 | $SIG{'TERM'} = \&SigQUIT ; |
---|
| 162 | $SIG{'__DIE__'} = \&SigDIE ; |
---|
| 163 | $SIG{'__WARN__'} = \&SigWARN ; |
---|
| 164 | $SIG{'CHLD'} = sub { $Childquit ++ } ; |
---|
| 165 | |
---|
| 166 | sub _log_list { |
---|
| 167 | my $callback = shift ; |
---|
| 168 | my $list = shift ; |
---|
| 169 | my %mesg = () ; |
---|
| 170 | while (@{$list}) { |
---|
| 171 | my $message = shift @{$list} ; |
---|
| 172 | next unless (defined($message) and $message); |
---|
| 173 | $mesg{$message} = 0 unless (exists($mesg{$message})); |
---|
| 174 | &$callback($message. |
---|
| 175 | (($mesg{$message}++) ?" ($mesg{$message} times)" : "" )); |
---|
| 176 | } |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | sub LogSigMessage { |
---|
| 180 | &_log_list( \&Warn, \@SigWarn ); |
---|
| 181 | &_log_list( \&Info, \@SigInfo ); |
---|
| 182 | &_log_list( \&Debug, \@SigDebug ); |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | &Debug("Module " . __PACKAGE__ . " v$VERSION loaded"); |
---|
| 186 | |
---|
| 187 | 1; |
---|