~ubuntu-branches/ubuntu/utopic/spamassassin/utopic-updates

« back to all changes in this revision

Viewing changes to lib/Mail/SpamAssassin/Logger/Syslog.pm

  • Committer: Package Import Robot
  • Author(s): Noah Meyerhans
  • Date: 2014-02-14 22:45:15 UTC
  • mfrom: (0.8.1) (0.6.2) (5.1.22 sid)
  • Revision ID: package-import@ubuntu.com-20140214224515-z1es2twos8xh7n2y
Tags: 3.4.0-1
* New upstream version! (Closes: 738963, 738872, 738867)
* Scrub the environment when switching to the debian-spamd user in
  postinst and cron.daily. (Closes: 738951)
* Enhancements to postinst to better manage ownership of
  /var/lib/spamassassin, via Iain Lane <iain.lane@canonical.com>
  (Closes: 738974)

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
use Sys::Syslog qw(:DEFAULT setlogsock);
40
40
use Mail::SpamAssassin::Logger;
41
41
 
42
 
use vars qw(@ISA);
 
42
use vars qw(@ISA %prio_map $syslog_open);
43
43
@ISA = ();
44
44
 
 
45
BEGIN {
 
46
  # %prio_map maps Logger.pm log level names (warn, error, info, dbg)
 
47
  # into standard Sys::Syslog::syslog() log level names
 
48
  #
 
49
  %prio_map = (dbg => 'debug', debug => 'debug', info => 'info',
 
50
               notice => 'notice', warn => 'warning', warning => 'warning',
 
51
               error => 'err', err => 'err', crit => 'crit', alert => 'alert',
 
52
               emerg => 'emerg');
 
53
 
 
54
  # make sure never to hit the CPAN-RT#56826 bug (memory corruption
 
55
  # when closelog() is called twice), fixed in Sys-Syslog 0.28
 
56
  $syslog_open = 0;
 
57
}
 
58
 
45
59
sub new {
46
60
  my $class = shift;
47
61
 
54
68
  $self->{disabled} = 0;
55
69
  $self->{consecutive_failures} = 0;
56
70
  $self->{failure_threshold} = 10;
 
71
  $self->{SIGPIPE_RECEIVED} = 0;
57
72
 
58
73
  # parameters
59
74
  my %params = @_;
87
102
    dbg("logger: opening syslog with $log_socket socket");
88
103
    # the next call is required to actually open the socket
89
104
    openlog($self->{ident}, 'cons,pid,ndelay', $self->{log_facility});
 
105
    $syslog_open = 1;
90
106
    1;
91
107
  } or do {
92
108
    $eval_stat = $@ ne '' ? $@ : "errno=$!";  chomp $eval_stat;
105
121
      setlogsock('inet') or die "setlogsock('inet') failed: $!";
106
122
      dbg("logger: opening syslog using inet socket");
107
123
      openlog($self->{ident}, 'cons,pid,ndelay', $self->{log_facility});
 
124
      $syslog_open = 1;
108
125
      1;
109
126
    } or do {
110
127
      $eval_stat = $@ ne '' ? $@ : "errno=$!";  chomp $eval_stat;
128
145
  return if $self->{disabled};
129
146
 
130
147
  # map level names
131
 
  # info is already info
132
 
  $level = 'debug' if $level eq 'dbg';
133
 
  $level = 'warning' if $level eq 'warn';
134
 
  $level = 'err' if $level eq 'error';
 
148
  $level = $prio_map{$level};
 
149
  if (!defined $level) {  # just in case
 
150
    $level = 'err';
 
151
    $msg = '(bad prio: ' . $_[1] . ') ' . $msg;
 
152
  }
135
153
 
136
154
  # install a new handler for SIGPIPE -- this signal has been
137
155
  # found to occur with syslog-ng after syslog-ng restarts.
138
156
  local $SIG{'PIPE'} = sub {
139
157
    $self->{SIGPIPE_RECEIVED}++;
140
158
    # force a log-close.   trap possible die() calls
141
 
    eval { closelog(); };
 
159
    eval { closelog() } if $syslog_open;
 
160
    $syslog_open = 0;
142
161
  };
143
162
 
144
163
  my $timestamp = '';
197
216
  eval {
198
217
    # SIGPIPE received when writing to syslog -- close and reopen
199
218
    # the log handle, then try again.
200
 
    closelog();
 
219
    closelog() if $syslog_open;
 
220
    $syslog_open = 0;
201
221
    openlog($self->{ident}, 'cons,pid,ndelay', $self->{log_facility});
 
222
    $syslog_open = 1;
202
223
    syslog('debug', "%s", "syslog reopened");
203
224
    syslog('info', "%s", $msg);
204
225
 
208
229
    syslog('info', "%s", $msg);
209
230
 
210
231
    # if we've received multiple sigpipes, logging is probably still broken.
211
 
    if ($self->{SIGPIPE_RECEIVED} > 1) {
 
232
    if ($self->{SIGPIPE_RECEIVED}) {
212
233
      warn "logger: syslog failure: multiple SIGPIPEs received\n";
213
234
      $self->{disabled} = 1;
214
235
    }
239
260
sub close_log {
240
261
  my ($self) = @_;
241
262
 
242
 
  closelog();
 
263
  closelog() if $syslog_open;
 
264
  $syslog_open = 0;
243
265
}
244
266
 
245
267
1;