Commit | Line | Data |
a0d0e21e |
1 | package Sys::Syslog; |
8168e71f |
2 | use strict; |
f93f88eb |
3 | use warnings; |
89c3c464 |
4 | use warnings::register; |
8168e71f |
5 | use Carp; |
f93f88eb |
6 | use Exporter (); |
a650b841 |
7 | use Fcntl qw(O_WRONLY); |
07b7e4bc |
8 | use File::Basename; |
6e4ef777 |
9 | use POSIX qw(strftime setlocale LC_TIME); |
10 | use Socket ':all'; |
d329efa2 |
11 | require 5.005; |
a0d0e21e |
12 | |
89c3c464 |
13 | { no strict 'vars'; |
f93f88eb |
14 | $VERSION = '0.25'; |
89c3c464 |
15 | @ISA = qw(Exporter); |
942974c1 |
16 | |
89c3c464 |
17 | %EXPORT_TAGS = ( |
4b035b3d |
18 | standard => [qw(openlog syslog closelog setlogmask)], |
19 | extended => [qw(setlogsock)], |
20 | macros => [ |
21 | # levels |
22 | qw( |
23 | LOG_ALERT LOG_CRIT LOG_DEBUG LOG_EMERG LOG_ERR |
24 | LOG_INFO LOG_NOTICE LOG_WARNING |
25 | ), |
26 | |
a650b841 |
27 | # standard facilities |
4b035b3d |
28 | qw( |
a650b841 |
29 | LOG_AUTH LOG_AUTHPRIV LOG_CRON LOG_DAEMON LOG_FTP LOG_KERN |
30 | LOG_LOCAL0 LOG_LOCAL1 LOG_LOCAL2 LOG_LOCAL3 LOG_LOCAL4 |
31 | LOG_LOCAL5 LOG_LOCAL6 LOG_LOCAL7 LOG_LPR LOG_MAIL LOG_NEWS |
32 | LOG_SYSLOG LOG_USER LOG_UUCP |
33 | ), |
34 | # Mac OS X specific facilities |
35 | qw( LOG_INSTALL LOG_LAUNCHD LOG_NETINFO LOG_RAS LOG_REMOTEAUTH ), |
36 | # modern BSD specific facilities |
37 | qw( LOG_CONSOLE LOG_NTP LOG_SECURITY ), |
38 | # IRIX specific facilities |
39 | qw( LOG_AUDIT LOG_LFMT ), |
4b035b3d |
40 | |
41 | # options |
42 | qw( |
43 | LOG_CONS LOG_PID LOG_NDELAY LOG_NOWAIT LOG_ODELAY LOG_PERROR |
44 | ), |
45 | |
46 | # others macros |
47 | qw( |
48 | LOG_FACMASK LOG_NFACILITIES LOG_PRIMASK |
49 | LOG_MASK LOG_UPTO |
50 | ), |
51 | ], |
89c3c464 |
52 | ); |
942974c1 |
53 | |
89c3c464 |
54 | @EXPORT = ( |
07b7e4bc |
55 | @{$EXPORT_TAGS{standard}}, |
89c3c464 |
56 | ); |
942974c1 |
57 | |
89c3c464 |
58 | @EXPORT_OK = ( |
07b7e4bc |
59 | @{$EXPORT_TAGS{extended}}, |
60 | @{$EXPORT_TAGS{macros}}, |
89c3c464 |
61 | ); |
62 | |
63 | eval { |
64 | require XSLoader; |
65 | XSLoader::load('Sys::Syslog', $VERSION); |
66 | 1 |
67 | } or do { |
68 | require DynaLoader; |
69 | push @ISA, 'DynaLoader'; |
70 | bootstrap Sys::Syslog $VERSION; |
71 | }; |
72 | } |
73 | |
74 | |
75 | # |
76 | # Public variables |
77 | # |
a650b841 |
78 | use vars qw($host); # host to send syslog messages to (see notes at end) |
89c3c464 |
79 | |
f93f88eb |
80 | # |
81 | # Prototypes |
82 | # |
83 | sub silent_eval (&); |
84 | |
89c3c464 |
85 | # |
86 | # Global variables |
87 | # |
a650b841 |
88 | use vars qw($facility); |
89c3c464 |
89 | my $connected = 0; # flag to indicate if we're connected or not |
90 | my $syslog_send; # coderef of the function used to send messages |
91 | my $syslog_path = undef; # syslog path for "stream" and "unix" mechanisms |
a650b841 |
92 | my $syslog_xobj = undef; # if defined, holds the external object used to send messages |
89c3c464 |
93 | my $transmit_ok = 0; # flag to indicate if the last message was transmited |
f93f88eb |
94 | my $sock_timeout = 0; # socket timeout, see below |
89c3c464 |
95 | my $current_proto = undef; # current mechanism used to transmit messages |
96 | my $ident = ''; # identifiant prepended to each message |
a650b841 |
97 | $facility = ''; # current facility |
89c3c464 |
98 | my $maskpri = LOG_UPTO(&LOG_DEBUG); # current log mask |
99 | |
100 | my %options = ( |
101 | ndelay => 0, |
102 | nofatal => 0, |
103 | nowait => 0, |
35a209d1 |
104 | perror => 0, |
89c3c464 |
105 | pid => 0, |
942974c1 |
106 | ); |
a0d0e21e |
107 | |
a650b841 |
108 | # Default is now to first use the native mechanism, so Perl programs |
d329efa2 |
109 | # behave like other normal Unix programs, then try other mechanisms. |
110 | my @connectMethods = qw(native tcp udp unix pipe stream console); |
dbfdd438 |
111 | if ($^O =~ /^(freebsd|linux)$/) { |
112 | @connectMethods = grep { $_ ne 'udp' } @connectMethods; |
113 | } |
a650b841 |
114 | |
f93f88eb |
115 | # And on Win32 systems, we try to use the native mechanism for this |
116 | # platform, the events logger, available through Win32::EventLog. |
26f266f7 |
117 | EVENTLOG: { |
26f266f7 |
118 | my $is_Win32 = $^O =~ /Win32/i; |
a650b841 |
119 | |
f93f88eb |
120 | if (can_load("Sys::Syslog::Win32")) { |
26f266f7 |
121 | unshift @connectMethods, 'eventlog'; |
122 | } |
123 | elsif ($is_Win32) { |
124 | warn $@; |
125 | } |
126 | } |
35a209d1 |
127 | |
23642f4b |
128 | my @defaultMethods = @connectMethods; |
89c3c464 |
129 | my @fallbackMethods = (); |
8168e71f |
130 | |
f93f88eb |
131 | # The timeout in connection_ok() was pushed up to 0.25 sec in |
132 | # Sys::Syslog v0.19 in order to address a heisenbug on MacOSX: |
133 | # http://london.pm.org/pipermail/london.pm/Week-of-Mon-20061211/005961.html |
134 | # |
135 | # However, this also had the effect of slowing this test for |
136 | # all other operating systems, which apparently impacted some |
137 | # users (cf. CPAN-RT #34753). So, in order to make everybody |
138 | # happy, the timeout is now zero by default on all systems |
139 | # except on OSX where it is set to 250 msec, and can be set |
140 | # with the infamous setlogsock() function. |
141 | $sock_timeout = 0.25 if $^O =~ /darwin/; |
142 | |
89c3c464 |
143 | # coderef for a nicer handling of errors |
144 | my $err_sub = $options{nofatal} ? \&warnings::warnif : \&croak; |
5be1dfc7 |
145 | |
5be1dfc7 |
146 | |
89c3c464 |
147 | sub AUTOLOAD { |
148 | # This AUTOLOAD is used to 'autoload' constants from the constant() |
149 | # XS function. |
150 | no strict 'vars'; |
151 | my $constname; |
152 | ($constname = $AUTOLOAD) =~ s/.*:://; |
153 | croak "Sys::Syslog::constant() not defined" if $constname eq 'constant'; |
154 | my ($error, $val) = constant($constname); |
a650b841 |
155 | croak $error if $error; |
89c3c464 |
156 | no strict 'refs'; |
157 | *$AUTOLOAD = sub { $val }; |
158 | goto &$AUTOLOAD; |
159 | } |
5be1dfc7 |
160 | |
5be1dfc7 |
161 | |
89c3c464 |
162 | sub openlog { |
163 | ($ident, my $logopt, $facility) = @_; |
8168e71f |
164 | |
a650b841 |
165 | # default values |
166 | $ident ||= basename($0) || getlogin() || getpwuid($<) || 'syslog'; |
167 | $logopt ||= ''; |
168 | $facility ||= LOG_USER(); |
169 | |
89c3c464 |
170 | for my $opt (split /\b/, $logopt) { |
171 | $options{$opt} = 1 if exists $options{$opt} |
172 | } |
5be1dfc7 |
173 | |
f93f88eb |
174 | $err_sub = delete $options{nofatal} ? \&warnings::warnif : \&croak; |
89c3c464 |
175 | return 1 unless $options{ndelay}; |
176 | connect_log(); |
177 | } |
5be1dfc7 |
178 | |
89c3c464 |
179 | sub closelog { |
180 | $facility = $ident = ''; |
181 | disconnect_log(); |
182 | } |
8168e71f |
183 | |
89c3c464 |
184 | sub setlogmask { |
185 | my $oldmask = $maskpri; |
186 | $maskpri = shift unless $_[0] == 0; |
187 | $oldmask; |
188 | } |
07b7e4bc |
189 | |
89c3c464 |
190 | sub setlogsock { |
f93f88eb |
191 | my ($setsock, $setpath, $settime) = @_; |
192 | |
193 | $syslog_path = $setpath if defined $setpath; |
194 | $sock_timeout = $settime if defined $settime; |
195 | |
89c3c464 |
196 | disconnect_log() if $connected; |
197 | $transmit_ok = 0; |
198 | @fallbackMethods = (); |
199 | @connectMethods = @defaultMethods; |
942974c1 |
200 | |
89c3c464 |
201 | if (ref $setsock eq 'ARRAY') { |
202 | @connectMethods = @$setsock; |
942974c1 |
203 | |
89c3c464 |
204 | } elsif (lc $setsock eq 'stream') { |
a650b841 |
205 | if (not defined $syslog_path) { |
89c3c464 |
206 | my @try = qw(/dev/log /dev/conslog); |
a650b841 |
207 | |
208 | if (length &_PATH_LOG) { # Undefined _PATH_LOG is "". |
89c3c464 |
209 | unshift @try, &_PATH_LOG; |
210 | } |
a650b841 |
211 | |
89c3c464 |
212 | for my $try (@try) { |
213 | if (-w $try) { |
214 | $syslog_path = $try; |
215 | last; |
216 | } |
217 | } |
a650b841 |
218 | |
219 | if (not defined $syslog_path) { |
220 | warnings::warnif "stream passed to setlogsock, but could not find any device"; |
221 | return undef |
222 | } |
89c3c464 |
223 | } |
a650b841 |
224 | |
225 | if (not -w $syslog_path) { |
07b7e4bc |
226 | warnings::warnif "stream passed to setlogsock, but $syslog_path is not writable"; |
89c3c464 |
227 | return undef; |
228 | } else { |
a650b841 |
229 | @connectMethods = qw(stream); |
89c3c464 |
230 | } |
942974c1 |
231 | |
89c3c464 |
232 | } elsif (lc $setsock eq 'unix') { |
8edeb3ad |
233 | if (length _PATH_LOG() || (defined $syslog_path && -w $syslog_path)) { |
234 | $syslog_path = _PATH_LOG() unless defined $syslog_path; |
a650b841 |
235 | @connectMethods = qw(unix); |
89c3c464 |
236 | } else { |
237 | warnings::warnif 'unix passed to setlogsock, but path not available'; |
238 | return undef; |
239 | } |
8168e71f |
240 | |
d329efa2 |
241 | } elsif (lc $setsock eq 'pipe') { |
242 | for my $path ($syslog_path, &_PATH_LOG, "/dev/log") { |
328c41c4 |
243 | next unless defined $path and length $path and -p $path and -w _; |
d329efa2 |
244 | $syslog_path = $path; |
245 | last |
246 | } |
247 | |
248 | if (not $syslog_path) { |
249 | warnings::warnif "pipe passed to setlogsock, but path not available"; |
250 | return undef |
251 | } |
252 | |
253 | @connectMethods = qw(pipe); |
254 | |
89c3c464 |
255 | } elsif (lc $setsock eq 'native') { |
a650b841 |
256 | @connectMethods = qw(native); |
257 | |
258 | } elsif (lc $setsock eq 'eventlog') { |
f93f88eb |
259 | if (can_load("Win32::EventLog")) { |
a650b841 |
260 | @connectMethods = qw(eventlog); |
261 | } else { |
35a209d1 |
262 | warnings::warnif "eventlog passed to setlogsock, but no Win32 API available"; |
263 | $@ = ""; |
d329efa2 |
264 | return undef; |
a650b841 |
265 | } |
8168e71f |
266 | |
89c3c464 |
267 | } elsif (lc $setsock eq 'tcp') { |
268 | if (getservbyname('syslog', 'tcp') || getservbyname('syslogng', 'tcp')) { |
a650b841 |
269 | @connectMethods = qw(tcp); |
f93f88eb |
270 | $host = $syslog_path; |
89c3c464 |
271 | } else { |
272 | warnings::warnif "tcp passed to setlogsock, but tcp service unavailable"; |
273 | return undef; |
274 | } |
942974c1 |
275 | |
89c3c464 |
276 | } elsif (lc $setsock eq 'udp') { |
277 | if (getservbyname('syslog', 'udp')) { |
a650b841 |
278 | @connectMethods = qw(udp); |
f93f88eb |
279 | $host = $syslog_path; |
89c3c464 |
280 | } else { |
281 | warnings::warnif "udp passed to setlogsock, but udp service unavailable"; |
282 | return undef; |
283 | } |
942974c1 |
284 | |
89c3c464 |
285 | } elsif (lc $setsock eq 'inet') { |
286 | @connectMethods = ( 'tcp', 'udp' ); |
942974c1 |
287 | |
89c3c464 |
288 | } elsif (lc $setsock eq 'console') { |
a650b841 |
289 | @connectMethods = qw(console); |
942974c1 |
290 | |
89c3c464 |
291 | } else { |
d329efa2 |
292 | croak "Invalid argument passed to setlogsock; must be 'stream', 'pipe', ", |
293 | "'unix', 'native', 'eventlog', 'tcp', 'udp' or 'inet'" |
89c3c464 |
294 | } |
942974c1 |
295 | |
89c3c464 |
296 | return 1; |
297 | } |
942974c1 |
298 | |
89c3c464 |
299 | sub syslog { |
300 | my $priority = shift; |
301 | my $mask = shift; |
302 | my ($message, $buf); |
303 | my (@words, $num, $numpri, $numfac, $sum); |
304 | my $failed = undef; |
305 | my $fail_time = undef; |
8edeb3ad |
306 | my $error = $!; |
8168e71f |
307 | |
a650b841 |
308 | # if $ident is undefined, it means openlog() wasn't previously called |
309 | # so do it now in order to have sensible defaults |
310 | openlog() unless $ident; |
311 | |
312 | local $facility = $facility; # may need to change temporarily. |
8168e71f |
313 | |
89c3c464 |
314 | croak "syslog: expecting argument \$priority" unless defined $priority; |
315 | croak "syslog: expecting argument \$format" unless defined $mask; |
5be1dfc7 |
316 | |
f93f88eb |
317 | croak "syslog: invalid level/facility: $priority" if $priority =~ /^-\d+$/; |
8edeb3ad |
318 | @words = split(/\W+/, $priority, 2); # Allow "level" or "level|facility". |
89c3c464 |
319 | undef $numpri; |
320 | undef $numfac; |
5be1dfc7 |
321 | |
f93f88eb |
322 | for my $word (@words) { |
323 | next if length $word == 0; |
324 | |
325 | $num = xlate($word); # Translate word to number. |
326 | |
327 | if ($num < 0) { |
328 | croak "syslog: invalid level/facility: $word" |
329 | } |
330 | elsif ($num <= &LOG_PRIMASK) { |
331 | croak "syslog: too many levels given: $word" if defined $numpri; |
332 | $numpri = $num; |
333 | return 0 unless LOG_MASK($numpri) & $maskpri; |
334 | } |
335 | else { |
336 | croak "syslog: too many facilities given: $word" if defined $numfac; |
337 | $facility = $word; |
338 | $numfac = $num; |
339 | } |
89c3c464 |
340 | } |
5be1dfc7 |
341 | |
89c3c464 |
342 | croak "syslog: level must be given" unless defined $numpri; |
942974c1 |
343 | |
89c3c464 |
344 | if (not defined $numfac) { # Facility not specified in this call. |
345 | $facility = 'user' unless $facility; |
346 | $numfac = xlate($facility); |
347 | } |
3d256c0f |
348 | |
89c3c464 |
349 | connect_log() unless $connected; |
8168e71f |
350 | |
89c3c464 |
351 | if ($mask =~ /%m/) { |
07b7e4bc |
352 | # escape percent signs for sprintf() |
8edeb3ad |
353 | $error =~ s/%/%%/g if @_; |
a650b841 |
354 | # replace %m with $error, if preceded by an even number of percent signs |
8edeb3ad |
355 | $mask =~ s/(?<!%)((?:%%)*)%m/$1$error/g; |
89c3c464 |
356 | } |
5be1dfc7 |
357 | |
89c3c464 |
358 | $mask .= "\n" unless $mask =~ /\n$/; |
359 | $message = @_ ? sprintf($mask, @_) : $mask; |
942974c1 |
360 | |
d329efa2 |
361 | # See CPAN-RT#24431. Opened on Apple Radar as bug #4944407 on 2007.01.21 |
35a209d1 |
362 | # Supposedly resolved on Leopard. |
d329efa2 |
363 | chomp $message if $^O =~ /darwin/; |
364 | |
365 | if ($current_proto eq 'native') { |
89c3c464 |
366 | $buf = $message; |
a650b841 |
367 | } |
368 | elsif ($current_proto eq 'eventlog') { |
369 | $buf = $message; |
370 | } |
371 | else { |
89c3c464 |
372 | my $whoami = $ident; |
89c3c464 |
373 | $whoami .= "[$$]" if $options{pid}; |
942974c1 |
374 | |
89c3c464 |
375 | $sum = $numpri + $numfac; |
376 | my $oldlocale = setlocale(LC_TIME); |
377 | setlocale(LC_TIME, 'C'); |
378 | my $timestamp = strftime "%b %e %T", localtime; |
379 | setlocale(LC_TIME, $oldlocale); |
380 | $buf = "<$sum>$timestamp $whoami: $message\0"; |
381 | } |
942974c1 |
382 | |
35a209d1 |
383 | # handle PERROR option |
384 | # "native" mechanism already handles it by itself |
385 | if ($options{perror} and $current_proto ne 'native') { |
386 | chomp $message; |
387 | my $whoami = $ident; |
388 | $whoami .= "[$$]" if $options{pid}; |
389 | print STDERR "$whoami: $message\n"; |
390 | } |
391 | |
89c3c464 |
392 | # it's possible that we'll get an error from sending |
393 | # (e.g. if method is UDP and there is no UDP listener, |
394 | # then we'll get ECONNREFUSED on the send). So what we |
395 | # want to do at this point is to fallback onto a different |
396 | # connection method. |
397 | while (scalar @fallbackMethods || $syslog_send) { |
398 | if ($failed && (time - $fail_time) > 60) { |
399 | # it's been a while... maybe things have been fixed |
400 | @fallbackMethods = (); |
401 | disconnect_log(); |
402 | $transmit_ok = 0; # make it look like a fresh attempt |
403 | connect_log(); |
404 | } |
942974c1 |
405 | |
89c3c464 |
406 | if ($connected && !connection_ok()) { |
407 | # Something was OK, but has now broken. Remember coz we'll |
408 | # want to go back to what used to be OK. |
409 | $failed = $current_proto unless $failed; |
410 | $fail_time = time; |
411 | disconnect_log(); |
412 | } |
942974c1 |
413 | |
89c3c464 |
414 | connect_log() unless $connected; |
415 | $failed = undef if ($current_proto && $failed && $current_proto eq $failed); |
942974c1 |
416 | |
89c3c464 |
417 | if ($syslog_send) { |
a650b841 |
418 | if ($syslog_send->($buf, $numpri, $numfac)) { |
89c3c464 |
419 | $transmit_ok++; |
420 | return 1; |
421 | } |
422 | # typically doesn't happen, since errors are rare from write(). |
423 | disconnect_log(); |
424 | } |
425 | } |
426 | # could not send, could not fallback onto a working |
427 | # connection method. Lose. |
428 | return 0; |
429 | } |
942974c1 |
430 | |
89c3c464 |
431 | sub _syslog_send_console { |
432 | my ($buf) = @_; |
433 | chop($buf); # delete the NUL from the end |
434 | # The console print is a method which could block |
435 | # so we do it in a child process and always return success |
436 | # to the caller. |
437 | if (my $pid = fork) { |
942974c1 |
438 | |
89c3c464 |
439 | if ($options{nowait}) { |
440 | return 1; |
441 | } else { |
442 | if (waitpid($pid, 0) >= 0) { |
443 | return ($? >> 8); |
444 | } else { |
445 | # it's possible that the caller has other |
446 | # plans for SIGCHLD, so let's not interfere |
447 | return 1; |
448 | } |
449 | } |
450 | } else { |
451 | if (open(CONS, ">/dev/console")) { |
452 | my $ret = print CONS $buf . "\r"; # XXX: should this be \x0A ? |
453 | exit $ret if defined $pid; |
454 | close CONS; |
455 | } |
456 | exit if defined $pid; |
457 | } |
458 | } |
942974c1 |
459 | |
89c3c464 |
460 | sub _syslog_send_stream { |
461 | my ($buf) = @_; |
462 | # XXX: this only works if the OS stream implementation makes a write |
463 | # look like a putmsg() with simple header. For instance it works on |
464 | # Solaris 8 but not Solaris 7. |
465 | # To be correct, it should use a STREAMS API, but perl doesn't have one. |
466 | return syswrite(SYSLOG, $buf, length($buf)); |
467 | } |
942974c1 |
468 | |
d329efa2 |
469 | sub _syslog_send_pipe { |
470 | my ($buf) = @_; |
471 | return print SYSLOG $buf; |
472 | } |
473 | |
89c3c464 |
474 | sub _syslog_send_socket { |
475 | my ($buf) = @_; |
476 | return syswrite(SYSLOG, $buf, length($buf)); |
477 | #return send(SYSLOG, $buf, 0); |
478 | } |
942974c1 |
479 | |
89c3c464 |
480 | sub _syslog_send_native { |
481 | my ($buf, $numpri) = @_; |
a650b841 |
482 | syslog_xs($numpri, $buf); |
483 | return 1; |
89c3c464 |
484 | } |
ce43db9b |
485 | |
5be1dfc7 |
486 | |
89c3c464 |
487 | # xlate() |
488 | # ----- |
489 | # private function to translate names to numeric values |
490 | # |
491 | sub xlate { |
f93f88eb |
492 | my ($name) = @_; |
493 | |
89c3c464 |
494 | return $name+0 if $name =~ /^\s*\d+\s*$/; |
495 | $name = uc $name; |
496 | $name = "LOG_$name" unless $name =~ /^LOG_/; |
f93f88eb |
497 | my $value = constant($name); |
498 | $value = -1 if $value =~ /not a valid/; |
499 | |
35a209d1 |
500 | return defined $value ? $value : -1; |
89c3c464 |
501 | } |
5be1dfc7 |
502 | |
942974c1 |
503 | |
89c3c464 |
504 | # connect_log() |
505 | # ----------- |
506 | # This function acts as a kind of front-end: it tries to connect to |
507 | # a syslog service using the selected methods, trying each one in the |
508 | # selected order. |
509 | # |
510 | sub connect_log { |
511 | @fallbackMethods = @connectMethods unless scalar @fallbackMethods; |
07b7e4bc |
512 | |
89c3c464 |
513 | if ($transmit_ok && $current_proto) { |
514 | # Retry what we were on, because it has worked in the past. |
515 | unshift(@fallbackMethods, $current_proto); |
516 | } |
07b7e4bc |
517 | |
89c3c464 |
518 | $connected = 0; |
519 | my @errs = (); |
520 | my $proto = undef; |
07b7e4bc |
521 | |
89c3c464 |
522 | while ($proto = shift @fallbackMethods) { |
523 | no strict 'refs'; |
524 | my $fn = "connect_$proto"; |
525 | $connected = &$fn(\@errs) if defined &$fn; |
526 | last if $connected; |
527 | } |
3d256c0f |
528 | |
89c3c464 |
529 | $transmit_ok = 0; |
530 | if ($connected) { |
531 | $current_proto = $proto; |
a650b841 |
532 | my ($old) = select(SYSLOG); $| = 1; select($old); |
89c3c464 |
533 | } else { |
534 | @fallbackMethods = (); |
535 | $err_sub->(join "\n\t- ", "no connection to syslog available", @errs); |
536 | return undef; |
537 | } |
538 | } |
942974c1 |
539 | |
89c3c464 |
540 | sub connect_tcp { |
541 | my ($errs) = @_; |
4b035b3d |
542 | |
89c3c464 |
543 | my $tcp = getprotobyname('tcp'); |
544 | if (!defined $tcp) { |
545 | push @$errs, "getprotobyname failed for tcp"; |
546 | return 0; |
547 | } |
4b035b3d |
548 | |
549 | my $syslog = getservbyname('syslog', 'tcp'); |
550 | $syslog = getservbyname('syslogng', 'tcp') unless defined $syslog; |
89c3c464 |
551 | if (!defined $syslog) { |
552 | push @$errs, "getservbyname failed for syslog/tcp and syslogng/tcp"; |
553 | return 0; |
554 | } |
942974c1 |
555 | |
4b035b3d |
556 | my $addr; |
89c3c464 |
557 | if (defined $host) { |
4b035b3d |
558 | $addr = inet_aton($host); |
559 | if (!$addr) { |
89c3c464 |
560 | push @$errs, "can't lookup $host"; |
561 | return 0; |
562 | } |
563 | } else { |
4b035b3d |
564 | $addr = INADDR_LOOPBACK; |
89c3c464 |
565 | } |
4b035b3d |
566 | $addr = sockaddr_in($syslog, $addr); |
942974c1 |
567 | |
89c3c464 |
568 | if (!socket(SYSLOG, AF_INET, SOCK_STREAM, $tcp)) { |
569 | push @$errs, "tcp socket: $!"; |
570 | return 0; |
571 | } |
a650b841 |
572 | |
89c3c464 |
573 | setsockopt(SYSLOG, SOL_SOCKET, SO_KEEPALIVE, 1); |
f93f88eb |
574 | if (silent_eval { IPPROTO_TCP() }) { |
d329efa2 |
575 | # These constants don't exist in 5.005. They were added in 1999 |
576 | setsockopt(SYSLOG, IPPROTO_TCP(), TCP_NODELAY(), 1); |
577 | } |
4b035b3d |
578 | if (!connect(SYSLOG, $addr)) { |
89c3c464 |
579 | push @$errs, "tcp connect: $!"; |
580 | return 0; |
581 | } |
4b035b3d |
582 | |
89c3c464 |
583 | $syslog_send = \&_syslog_send_socket; |
4b035b3d |
584 | |
89c3c464 |
585 | return 1; |
586 | } |
942974c1 |
587 | |
89c3c464 |
588 | sub connect_udp { |
589 | my ($errs) = @_; |
4b035b3d |
590 | |
89c3c464 |
591 | my $udp = getprotobyname('udp'); |
592 | if (!defined $udp) { |
593 | push @$errs, "getprotobyname failed for udp"; |
594 | return 0; |
595 | } |
4b035b3d |
596 | |
597 | my $syslog = getservbyname('syslog', 'udp'); |
89c3c464 |
598 | if (!defined $syslog) { |
599 | push @$errs, "getservbyname failed for syslog/udp"; |
600 | return 0; |
601 | } |
4b035b3d |
602 | |
603 | my $addr; |
89c3c464 |
604 | if (defined $host) { |
4b035b3d |
605 | $addr = inet_aton($host); |
606 | if (!$addr) { |
89c3c464 |
607 | push @$errs, "can't lookup $host"; |
608 | return 0; |
609 | } |
610 | } else { |
4b035b3d |
611 | $addr = INADDR_LOOPBACK; |
89c3c464 |
612 | } |
4b035b3d |
613 | $addr = sockaddr_in($syslog, $addr); |
942974c1 |
614 | |
89c3c464 |
615 | if (!socket(SYSLOG, AF_INET, SOCK_DGRAM, $udp)) { |
616 | push @$errs, "udp socket: $!"; |
617 | return 0; |
618 | } |
4b035b3d |
619 | if (!connect(SYSLOG, $addr)) { |
89c3c464 |
620 | push @$errs, "udp connect: $!"; |
621 | return 0; |
622 | } |
4b035b3d |
623 | |
89c3c464 |
624 | # We want to check that the UDP connect worked. However the only |
625 | # way to do that is to send a message and see if an ICMP is returned |
626 | _syslog_send_socket(""); |
627 | if (!connection_ok()) { |
628 | push @$errs, "udp connect: nobody listening"; |
629 | return 0; |
630 | } |
4b035b3d |
631 | |
89c3c464 |
632 | $syslog_send = \&_syslog_send_socket; |
4b035b3d |
633 | |
89c3c464 |
634 | return 1; |
635 | } |
9903e4c8 |
636 | |
89c3c464 |
637 | sub connect_stream { |
638 | my ($errs) = @_; |
639 | # might want syslog_path to be variable based on syslog.h (if only |
640 | # it were in there!) |
8edeb3ad |
641 | $syslog_path = '/dev/conslog' unless defined $syslog_path; |
89c3c464 |
642 | if (!-w $syslog_path) { |
643 | push @$errs, "stream $syslog_path is not writable"; |
644 | return 0; |
645 | } |
f93f88eb |
646 | if (!sysopen(SYSLOG, $syslog_path, O_WRONLY, 0400)) { |
89c3c464 |
647 | push @$errs, "stream can't open $syslog_path: $!"; |
648 | return 0; |
649 | } |
650 | $syslog_send = \&_syslog_send_stream; |
651 | return 1; |
652 | } |
942974c1 |
653 | |
d329efa2 |
654 | sub connect_pipe { |
655 | my ($errs) = @_; |
656 | |
657 | $syslog_path ||= &_PATH_LOG || "/dev/log"; |
658 | |
659 | if (not -w $syslog_path) { |
660 | push @$errs, "$syslog_path is not writable"; |
661 | return 0; |
662 | } |
663 | |
664 | if (not open(SYSLOG, ">$syslog_path")) { |
665 | push @$errs, "can't write to $syslog_path: $!"; |
666 | return 0; |
667 | } |
668 | |
669 | $syslog_send = \&_syslog_send_pipe; |
670 | |
671 | return 1; |
672 | } |
673 | |
89c3c464 |
674 | sub connect_unix { |
675 | my ($errs) = @_; |
4b035b3d |
676 | |
677 | $syslog_path ||= _PATH_LOG() if length _PATH_LOG(); |
678 | |
679 | if (not defined $syslog_path) { |
680 | push @$errs, "_PATH_LOG not available in syslog.h and no user-supplied socket path"; |
89c3c464 |
681 | return 0; |
682 | } |
4b035b3d |
683 | |
35a209d1 |
684 | if (not (-S $syslog_path or -c _)) { |
89c3c464 |
685 | push @$errs, "$syslog_path is not a socket"; |
686 | return 0; |
687 | } |
4b035b3d |
688 | |
689 | my $addr = sockaddr_un($syslog_path); |
690 | if (!$addr) { |
89c3c464 |
691 | push @$errs, "can't locate $syslog_path"; |
692 | return 0; |
693 | } |
4b035b3d |
694 | if (!socket(SYSLOG, AF_UNIX, SOCK_STREAM, 0)) { |
89c3c464 |
695 | push @$errs, "unix stream socket: $!"; |
696 | return 0; |
697 | } |
a650b841 |
698 | |
4b035b3d |
699 | if (!connect(SYSLOG, $addr)) { |
700 | if (!socket(SYSLOG, AF_UNIX, SOCK_DGRAM, 0)) { |
89c3c464 |
701 | push @$errs, "unix dgram socket: $!"; |
702 | return 0; |
703 | } |
4b035b3d |
704 | if (!connect(SYSLOG, $addr)) { |
89c3c464 |
705 | push @$errs, "unix dgram connect: $!"; |
706 | return 0; |
707 | } |
708 | } |
4b035b3d |
709 | |
89c3c464 |
710 | $syslog_send = \&_syslog_send_socket; |
4b035b3d |
711 | |
89c3c464 |
712 | return 1; |
713 | } |
942974c1 |
714 | |
89c3c464 |
715 | sub connect_native { |
716 | my ($errs) = @_; |
717 | my $logopt = 0; |
5be1dfc7 |
718 | |
89c3c464 |
719 | # reconstruct the numeric equivalent of the options |
720 | for my $opt (keys %options) { |
721 | $logopt += xlate($opt) if $options{$opt} |
722 | } |
942974c1 |
723 | |
f93f88eb |
724 | openlog_xs($ident, $logopt, xlate($facility)); |
89c3c464 |
725 | $syslog_send = \&_syslog_send_native; |
942974c1 |
726 | |
89c3c464 |
727 | return 1; |
728 | } |
6e4ef777 |
729 | |
a650b841 |
730 | sub connect_eventlog { |
731 | my ($errs) = @_; |
732 | |
733 | $syslog_xobj = Sys::Syslog::Win32::_install(); |
734 | $syslog_send = \&Sys::Syslog::Win32::_syslog_send; |
735 | |
736 | return 1; |
737 | } |
738 | |
89c3c464 |
739 | sub connect_console { |
740 | my ($errs) = @_; |
741 | if (!-w '/dev/console') { |
742 | push @$errs, "console is not writable"; |
743 | return 0; |
744 | } |
745 | $syslog_send = \&_syslog_send_console; |
746 | return 1; |
747 | } |
6e4ef777 |
748 | |
a650b841 |
749 | # To test if the connection is still good, we need to check if any |
89c3c464 |
750 | # errors are present on the connection. The errors will not be raised |
751 | # by a write. Instead, sockets are made readable and the next read |
752 | # would cause the error to be returned. Unfortunately the syslog |
753 | # 'protocol' never provides anything for us to read. But with |
754 | # judicious use of select(), we can see if it would be readable... |
755 | sub connection_ok { |
756 | return 1 if defined $current_proto and ( |
757 | $current_proto eq 'native' or $current_proto eq 'console' |
a650b841 |
758 | or $current_proto eq 'eventlog' |
89c3c464 |
759 | ); |
a650b841 |
760 | |
89c3c464 |
761 | my $rin = ''; |
762 | vec($rin, fileno(SYSLOG), 1) = 1; |
f93f88eb |
763 | my $ret = select $rin, undef, $rin, $sock_timeout; |
89c3c464 |
764 | return ($ret ? 0 : 1); |
765 | } |
942974c1 |
766 | |
89c3c464 |
767 | sub disconnect_log { |
768 | $connected = 0; |
769 | $syslog_send = undef; |
942974c1 |
770 | |
a650b841 |
771 | if (defined $current_proto and $current_proto eq 'native') { |
772 | closelog_xs(); |
773 | return 1; |
774 | } |
775 | elsif (defined $current_proto and $current_proto eq 'eventlog') { |
776 | $syslog_xobj->Close(); |
89c3c464 |
777 | return 1; |
778 | } |
6e4ef777 |
779 | |
89c3c464 |
780 | return close SYSLOG; |
781 | } |
6e4ef777 |
782 | |
f93f88eb |
783 | |
784 | # |
785 | # Wrappers around eval() that makes sure that nobody, and I say NOBODY, |
786 | # ever knows that I wanted to test if something was here or not. |
787 | # It is needed because some applications are trying to be too smart, |
788 | # do it wrong, and it ends up in EPIC FAIL. |
789 | # Yes I'm speaking of YOU, SpamAssassin. |
790 | # |
791 | sub silent_eval (&) { |
792 | local($SIG{__DIE__}, $SIG{__WARN__}, $@); |
793 | return eval $_[0] |
794 | } |
795 | |
796 | sub can_load { |
797 | local($SIG{__DIE__}, $SIG{__WARN__}, $@); |
798 | return eval "use $_[0]; 1" |
799 | } |
800 | |
801 | |
802 | "Eighth Rule: read the documentation." |
942974c1 |
803 | |
89c3c464 |
804 | __END__ |
5be1dfc7 |
805 | |
89c3c464 |
806 | =head1 NAME |
8168e71f |
807 | |
89c3c464 |
808 | Sys::Syslog - Perl interface to the UNIX syslog(3) calls |
3ffabb8c |
809 | |
89c3c464 |
810 | =head1 VERSION |
3ffabb8c |
811 | |
f93f88eb |
812 | Version 0.25 |
23642f4b |
813 | |
89c3c464 |
814 | =head1 SYNOPSIS |
cb63fe9d |
815 | |
89c3c464 |
816 | use Sys::Syslog; # all except setlogsock(), or: |
817 | use Sys::Syslog qw(:DEFAULT setlogsock); # default set, plus setlogsock() |
818 | use Sys::Syslog qw(:standard :macros); # standard functions, plus macros |
23642f4b |
819 | |
89c3c464 |
820 | openlog $ident, $logopt, $facility; # don't forget this |
821 | syslog $priority, $format, @args; |
822 | $oldmask = setlogmask $mask_priority; |
823 | closelog; |
cb63fe9d |
824 | |
942974c1 |
825 | |
89c3c464 |
826 | =head1 DESCRIPTION |
5be1dfc7 |
827 | |
89c3c464 |
828 | C<Sys::Syslog> is an interface to the UNIX C<syslog(3)> program. |
829 | Call C<syslog()> with a string priority and a list of C<printf()> args |
830 | just like C<syslog(3)>. |
5be1dfc7 |
831 | |
a650b841 |
832 | You can find a kind of FAQ in L<"THE RULES OF SYS::SYSLOG">. Please read |
833 | it before coding, and again before asking questions. |
834 | |
5be1dfc7 |
835 | |
89c3c464 |
836 | =head1 EXPORTS |
5be1dfc7 |
837 | |
89c3c464 |
838 | C<Sys::Syslog> exports the following C<Exporter> tags: |
5be1dfc7 |
839 | |
89c3c464 |
840 | =over 4 |
841 | |
842 | =item * |
843 | |
844 | C<:standard> exports the standard C<syslog(3)> functions: |
845 | |
846 | openlog closelog setlogmask syslog |
847 | |
848 | =item * |
849 | |
850 | C<:extended> exports the Perl specific functions for C<syslog(3)>: |
851 | |
852 | setlogsock |
853 | |
854 | =item * |
855 | |
856 | C<:macros> exports the symbols corresponding to most of your C<syslog(3)> |
857 | macros and the C<LOG_UPTO()> and C<LOG_MASK()> functions. |
858 | See L<"CONSTANTS"> for the supported constants and their meaning. |
859 | |
860 | =back |
861 | |
862 | By default, C<Sys::Syslog> exports the symbols from the C<:standard> tag. |
863 | |
864 | |
865 | =head1 FUNCTIONS |
866 | |
867 | =over 4 |
868 | |
869 | =item B<openlog($ident, $logopt, $facility)> |
870 | |
871 | Opens the syslog. |
872 | C<$ident> is prepended to every message. C<$logopt> contains zero or |
873 | more of the options detailed below. C<$facility> specifies the part |
874 | of the system to report about, for example C<LOG_USER> or C<LOG_LOCAL0>: |
875 | see L<"Facilities"> for a list of well-known facilities, and your |
876 | C<syslog(3)> documentation for the facilities available in your system. |
877 | Check L<"SEE ALSO"> for useful links. Facility can be given as a string |
878 | or a numeric macro. |
879 | |
880 | This function will croak if it can't connect to the syslog daemon. |
881 | |
882 | Note that C<openlog()> now takes three arguments, just like C<openlog(3)>. |
883 | |
884 | B<You should use C<openlog()> before calling C<syslog()>.> |
885 | |
886 | B<Options> |
887 | |
888 | =over 4 |
889 | |
890 | =item * |
891 | |
892 | C<cons> - This option is ignored, since the failover mechanism will drop |
893 | down to the console automatically if all other media fail. |
894 | |
895 | =item * |
896 | |
897 | C<ndelay> - Open the connection immediately (normally, the connection is |
898 | opened when the first message is logged). |
899 | |
900 | =item * |
901 | |
902 | C<nofatal> - When set to true, C<openlog()> and C<syslog()> will only |
903 | emit warnings instead of dying if the connection to the syslog can't |
904 | be established. |
905 | |
906 | =item * |
907 | |
908 | C<nowait> - Don't wait for child processes that may have been created |
909 | while logging the message. (The GNU C library does not create a child |
910 | process, so this option has no effect on Linux.) |
911 | |
912 | =item * |
913 | |
35a209d1 |
914 | C<perror> - Write the message to standard error output as well to the |
915 | system log. |
916 | |
917 | =item * |
918 | |
89c3c464 |
919 | C<pid> - Include PID with each message. |
920 | |
921 | =back |
922 | |
923 | B<Examples> |
924 | |
925 | Open the syslog with options C<ndelay> and C<pid>, and with facility C<LOCAL0>: |
926 | |
927 | openlog($name, "ndelay,pid", "local0"); |
928 | |
929 | Same thing, but this time using the macro corresponding to C<LOCAL0>: |
930 | |
931 | openlog($name, "ndelay,pid", LOG_LOCAL0); |
932 | |
933 | |
934 | =item B<syslog($priority, $message)> |
935 | |
936 | =item B<syslog($priority, $format, @args)> |
937 | |
938 | If C<$priority> permits, logs C<$message> or C<sprintf($format, @args)> |
939 | with the addition that C<%m> in $message or C<$format> is replaced with |
940 | C<"$!"> (the latest error message). |
941 | |
942 | C<$priority> can specify a level, or a level and a facility. Levels and |
a650b841 |
943 | facilities can be given as strings or as macros. When using the C<eventlog> |
944 | mechanism, priorities C<DEBUG> and C<INFO> are mapped to event type |
945 | C<informational>, C<NOTICE> and C<WARNIN> to C<warning> and C<ERR> to |
946 | C<EMERG> to C<error>. |
89c3c464 |
947 | |
948 | If you didn't use C<openlog()> before using C<syslog()>, C<syslog()> will |
949 | try to guess the C<$ident> by extracting the shortest prefix of |
950 | C<$format> that ends in a C<":">. |
951 | |
952 | B<Examples> |
953 | |
954 | syslog("info", $message); # informational level |
955 | syslog(LOG_INFO, $message); # informational level |
956 | |
957 | syslog("info|local0", $message); # information level, Local0 facility |
958 | syslog(LOG_INFO|LOG_LOCAL0, $message); # information level, Local0 facility |
959 | |
960 | =over 4 |
961 | |
962 | =item B<Note> |
963 | |
964 | C<Sys::Syslog> version v0.07 and older passed the C<$message> as the |
965 | formatting string to C<sprintf()> even when no formatting arguments |
966 | were provided. If the code calling C<syslog()> might execute with |
967 | older versions of this module, make sure to call the function as |
968 | C<syslog($priority, "%s", $message)> instead of C<syslog($priority, |
969 | $message)>. This protects against hostile formatting sequences that |
970 | might show up if $message contains tainted data. |
971 | |
972 | =back |
973 | |
974 | |
975 | =item B<setlogmask($mask_priority)> |
976 | |
977 | Sets the log mask for the current process to C<$mask_priority> and |
978 | returns the old mask. If the mask argument is 0, the current log mask |
979 | is not modified. See L<"Levels"> for the list of available levels. |
980 | You can use the C<LOG_UPTO()> function to allow all levels up to a |
981 | given priority (but it only accept the numeric macros as arguments). |
982 | |
983 | B<Examples> |
984 | |
985 | Only log errors: |
986 | |
987 | setlogmask( LOG_MASK(LOG_ERR) ); |
988 | |
989 | Log everything except informational messages: |
990 | |
991 | setlogmask( ~(LOG_MASK(LOG_INFO)) ); |
992 | |
993 | Log critical messages, errors and warnings: |
994 | |
995 | setlogmask( LOG_MASK(LOG_CRIT) | LOG_MASK(LOG_ERR) | LOG_MASK(LOG_WARNING) ); |
996 | |
997 | Log all messages up to debug: |
998 | |
999 | setlogmask( LOG_UPTO(LOG_DEBUG) ); |
1000 | |
1001 | |
1002 | =item B<setlogsock($sock_type)> |
1003 | |
07b7e4bc |
1004 | =item B<setlogsock($sock_type, $stream_location)> (added in Perl 5.004_02) |
89c3c464 |
1005 | |
f93f88eb |
1006 | =item B<setlogsock($sock_type, $stream_location, $sock_timeout)> (added in 0.25) |
1007 | |
89c3c464 |
1008 | Sets the socket type to be used for the next call to |
1009 | C<openlog()> or C<syslog()> and returns true on success, |
4b035b3d |
1010 | C<undef> on failure. The available mechanisms are: |
1011 | |
1012 | =over |
1013 | |
1014 | =item * |
1015 | |
07b7e4bc |
1016 | C<"native"> - use the native C functions from your C<syslog(3)> library |
1017 | (added in C<Sys::Syslog> 0.15). |
4b035b3d |
1018 | |
1019 | =item * |
1020 | |
d329efa2 |
1021 | C<"eventlog"> - send messages to the Win32 events logger (Win32 only; |
1022 | added in C<Sys::Syslog> 0.19). |
1023 | |
1024 | =item * |
1025 | |
4b035b3d |
1026 | C<"tcp"> - connect to a TCP socket, on the C<syslog/tcp> or C<syslogng/tcp> |
f93f88eb |
1027 | service. If defined, the second parameter is used as a hostname to connect to. |
4b035b3d |
1028 | |
1029 | =item * |
1030 | |
1031 | C<"udp"> - connect to a UDP socket, on the C<syslog/udp> service. |
f93f88eb |
1032 | If defined, the second parameter is used as a hostname to connect to, |
1033 | and the third parameter as the timeout used to check for UDP response. |
4b035b3d |
1034 | |
1035 | =item * |
1036 | |
f93f88eb |
1037 | C<"inet"> - connect to an INET socket, either TCP or UDP, tried in that |
1038 | order. If defined, the second parameter is used as a hostname to connect to. |
4b035b3d |
1039 | |
1040 | =item * |
1041 | |
1042 | C<"unix"> - connect to a UNIX domain socket (in some systems a character |
1043 | special device). The name of that socket is the second parameter or, if |
1044 | you omit the second parameter, the value returned by the C<_PATH_LOG> macro |
1045 | (if your system defines it), or F</dev/log> or F</dev/conslog>, whatever is |
1046 | writable. |
1047 | |
1048 | =item * |
1049 | |
1050 | C<"stream"> - connect to the stream indicated by the pathname provided as |
1051 | the optional second parameter, or, if omitted, to F</dev/conslog>. |
1052 | For example Solaris and IRIX system may prefer C<"stream"> instead of C<"unix">. |
1053 | |
1054 | =item * |
1055 | |
d329efa2 |
1056 | C<"pipe"> - connect to the named pipe indicated by the pathname provided as |
1057 | the optional second parameter, or, if omitted, to the value returned by |
1058 | the C<_PATH_LOG> macro (if your system defines it), or F</dev/log> |
1059 | (added in C<Sys::Syslog> 0.21). |
4b035b3d |
1060 | |
a650b841 |
1061 | =item * |
1062 | |
d329efa2 |
1063 | C<"console"> - send messages directly to the console, as for the C<"cons"> |
1064 | option of C<openlog()>. |
a650b841 |
1065 | |
4b035b3d |
1066 | =back |
89c3c464 |
1067 | |
1068 | A reference to an array can also be passed as the first parameter. |
1069 | When this calling method is used, the array should contain a list of |
4b035b3d |
1070 | mechanisms which are attempted in order. |
89c3c464 |
1071 | |
f93f88eb |
1072 | The default is to try C<native>, C<tcp>, C<udp>, C<unix>, C<pipe>, C<stream>, |
1073 | C<console>. |
35a209d1 |
1074 | Under systems with the Win32 API, C<eventlog> will be added as the first |
1075 | mechanism to try if C<Win32::EventLog> is available. |
89c3c464 |
1076 | |
07b7e4bc |
1077 | Giving an invalid value for C<$sock_type> will C<croak>. |
89c3c464 |
1078 | |
4b035b3d |
1079 | B<Examples> |
1080 | |
1081 | Select the UDP socket mechanism: |
1082 | |
1083 | setlogsock("udp"); |
1084 | |
1085 | Select the native, UDP socket then UNIX domain socket mechanisms: |
1086 | |
1087 | setlogsock(["native", "udp", "unix"]); |
1088 | |
07b7e4bc |
1089 | =over |
1090 | |
1091 | =item B<Note> |
1092 | |
1093 | Now that the "native" mechanism is supported by C<Sys::Syslog> and selected |
1094 | by default, the use of the C<setlogsock()> function is discouraged because |
1095 | other mechanisms are less portable across operating systems. Authors of |
1096 | modules and programs that use this function, especially its cargo-cult form |
1097 | C<setlogsock("unix")>, are advised to remove any occurence of it unless they |
1098 | specifically want to use a given mechanism (like TCP or UDP to connect to |
1099 | a remote host). |
1100 | |
1101 | =back |
89c3c464 |
1102 | |
1103 | =item B<closelog()> |
1104 | |
4b035b3d |
1105 | Closes the log file and returns true on success. |
89c3c464 |
1106 | |
1107 | =back |
1108 | |
1109 | |
a650b841 |
1110 | =head1 THE RULES OF SYS::SYSLOG |
1111 | |
1112 | I<The First Rule of Sys::Syslog is:> |
1113 | You do not call C<setlogsock>. |
1114 | |
1115 | I<The Second Rule of Sys::Syslog is:> |
1116 | You B<do not> call C<setlogsock>. |
1117 | |
1118 | I<The Third Rule of Sys::Syslog is:> |
1119 | The program crashes, C<die>s, calls C<closelog>, the log is over. |
1120 | |
1121 | I<The Fourth Rule of Sys::Syslog is:> |
1122 | One facility, one priority. |
1123 | |
1124 | I<The Fifth Rule of Sys::Syslog is:> |
1125 | One log at a time. |
1126 | |
1127 | I<The Sixth Rule of Sys::Syslog is:> |
1128 | No C<syslog> before C<openlog>. |
1129 | |
1130 | I<The Seventh Rule of Sys::Syslog is:> |
1131 | Logs will go on as long as they have to. |
1132 | |
1133 | I<The Eighth, and Final Rule of Sys::Syslog is:> |
1134 | If this is your first use of Sys::Syslog, you must read the doc. |
1135 | |
1136 | |
89c3c464 |
1137 | =head1 EXAMPLES |
1138 | |
a650b841 |
1139 | An example: |
1140 | |
89c3c464 |
1141 | openlog($program, 'cons,pid', 'user'); |
1142 | syslog('info', '%s', 'this is another test'); |
1143 | syslog('mail|warning', 'this is a better test: %d', time); |
1144 | closelog(); |
5be1dfc7 |
1145 | |
1146 | syslog('debug', 'this is the last test'); |
cb63fe9d |
1147 | |
a650b841 |
1148 | Another example: |
1149 | |
5be1dfc7 |
1150 | openlog("$program $$", 'ndelay', 'user'); |
1151 | syslog('notice', 'fooprogram: this is really done'); |
1152 | |
a650b841 |
1153 | Example of use of C<%m>: |
1154 | |
5be1dfc7 |
1155 | $! = 55; |
6e4ef777 |
1156 | syslog('info', 'problem was %m'); # %m == $! in syslog(3) |
1157 | |
1158 | Log to UDP port on C<$remotehost> instead of logging locally: |
5be1dfc7 |
1159 | |
f93f88eb |
1160 | setlogsock("udp", $remotehost); |
476b65d9 |
1161 | openlog($program, 'ndelay', 'user'); |
1162 | syslog('info', 'something happened over here'); |
1163 | |
8168e71f |
1164 | |
1165 | =head1 CONSTANTS |
1166 | |
1167 | =head2 Facilities |
1168 | |
1169 | =over 4 |
1170 | |
1171 | =item * |
1172 | |
a650b841 |
1173 | C<LOG_AUDIT> - audit daemon (IRIX); falls back to C<LOG_AUTH> |
1174 | |
1175 | =item * |
1176 | |
8168e71f |
1177 | C<LOG_AUTH> - security/authorization messages |
1178 | |
1179 | =item * |
1180 | |
1181 | C<LOG_AUTHPRIV> - security/authorization messages (private) |
1182 | |
1183 | =item * |
1184 | |
a650b841 |
1185 | C<LOG_CONSOLE> - C</dev/console> output (FreeBSD); falls back to C<LOG_USER> |
1186 | |
1187 | =item * |
1188 | |
4b035b3d |
1189 | C<LOG_CRON> - clock daemons (B<cron> and B<at>) |
8168e71f |
1190 | |
1191 | =item * |
1192 | |
1193 | C<LOG_DAEMON> - system daemons without separate facility value |
1194 | |
1195 | =item * |
1196 | |
4b035b3d |
1197 | C<LOG_FTP> - FTP daemon |
8168e71f |
1198 | |
1199 | =item * |
1200 | |
1201 | C<LOG_KERN> - kernel messages |
1202 | |
1203 | =item * |
1204 | |
a650b841 |
1205 | C<LOG_INSTALL> - installer subsystem (Mac OS X); falls back to C<LOG_USER> |
4b035b3d |
1206 | |
1207 | =item * |
1208 | |
a650b841 |
1209 | C<LOG_LAUNCHD> - launchd - general bootstrap daemon (Mac OS X); |
1210 | falls back to C<LOG_DAEMON> |
1211 | |
1212 | =item * |
1213 | |
1214 | C<LOG_LFMT> - logalert facility; falls back to C<LOG_USER> |
4b035b3d |
1215 | |
1216 | =item * |
1217 | |
8168e71f |
1218 | C<LOG_LOCAL0> through C<LOG_LOCAL7> - reserved for local use |
1219 | |
1220 | =item * |
1221 | |
1222 | C<LOG_LPR> - line printer subsystem |
1223 | |
1224 | =item * |
1225 | |
1226 | C<LOG_MAIL> - mail subsystem |
1227 | |
1228 | =item * |
1229 | |
a650b841 |
1230 | C<LOG_NETINFO> - NetInfo subsystem (Mac OS X); falls back to C<LOG_DAEMON> |
4b035b3d |
1231 | |
1232 | =item * |
1233 | |
8168e71f |
1234 | C<LOG_NEWS> - USENET news subsystem |
1235 | |
1236 | =item * |
1237 | |
a650b841 |
1238 | C<LOG_NTP> - NTP subsystem (FreeBSD, NetBSD); falls back to C<LOG_DAEMON> |
1239 | |
1240 | =item * |
1241 | |
1242 | C<LOG_RAS> - Remote Access Service (VPN / PPP) (Mac OS X); |
1243 | falls back to C<LOG_AUTH> |
4b035b3d |
1244 | |
1245 | =item * |
1246 | |
a650b841 |
1247 | C<LOG_REMOTEAUTH> - remote authentication/authorization (Mac OS X); |
1248 | falls back to C<LOG_AUTH> |
1249 | |
1250 | =item * |
1251 | |
1252 | C<LOG_SECURITY> - security subsystems (firewalling, etc.) (FreeBSD); |
1253 | falls back to C<LOG_AUTH> |
4b035b3d |
1254 | |
1255 | =item * |
1256 | |
8168e71f |
1257 | C<LOG_SYSLOG> - messages generated internally by B<syslogd> |
1258 | |
1259 | =item * |
1260 | |
1261 | C<LOG_USER> (default) - generic user-level messages |
1262 | |
1263 | =item * |
1264 | |
1265 | C<LOG_UUCP> - UUCP subsystem |
1266 | |
1267 | =back |
1268 | |
1269 | |
1270 | =head2 Levels |
1271 | |
1272 | =over 4 |
1273 | |
1274 | =item * |
1275 | |
1276 | C<LOG_EMERG> - system is unusable |
1277 | |
1278 | =item * |
1279 | |
1280 | C<LOG_ALERT> - action must be taken immediately |
1281 | |
1282 | =item * |
1283 | |
1284 | C<LOG_CRIT> - critical conditions |
1285 | |
1286 | =item * |
1287 | |
942974c1 |
1288 | C<LOG_ERR> - error conditions |
8168e71f |
1289 | |
1290 | =item * |
1291 | |
1292 | C<LOG_WARNING> - warning conditions |
1293 | |
1294 | =item * |
1295 | |
1296 | C<LOG_NOTICE> - normal, but significant, condition |
1297 | |
1298 | =item * |
1299 | |
1300 | C<LOG_INFO> - informational message |
1301 | |
1302 | =item * |
1303 | |
1304 | C<LOG_DEBUG> - debug-level message |
1305 | |
1306 | =back |
1307 | |
1308 | |
1309 | =head1 DIAGNOSTICS |
1310 | |
a650b841 |
1311 | =over |
8168e71f |
1312 | |
a650b841 |
1313 | =item C<Invalid argument passed to setlogsock> |
8168e71f |
1314 | |
1315 | B<(F)> You gave C<setlogsock()> an invalid value for C<$sock_type>. |
1316 | |
35a209d1 |
1317 | =item C<eventlog passed to setlogsock, but no Win32 API available> |
a650b841 |
1318 | |
1319 | B<(W)> You asked C<setlogsock()> to use the Win32 event logger but the |
1320 | operating system running the program isn't Win32 or does not provides Win32 |
35a209d1 |
1321 | compatible facilities. |
a650b841 |
1322 | |
1323 | =item C<no connection to syslog available> |
8168e71f |
1324 | |
1325 | B<(F)> C<syslog()> failed to connect to the specified socket. |
1326 | |
a650b841 |
1327 | =item C<stream passed to setlogsock, but %s is not writable> |
8168e71f |
1328 | |
942974c1 |
1329 | B<(W)> You asked C<setlogsock()> to use a stream socket, but the given |
8168e71f |
1330 | path is not writable. |
1331 | |
a650b841 |
1332 | =item C<stream passed to setlogsock, but could not find any device> |
8168e71f |
1333 | |
942974c1 |
1334 | B<(W)> You asked C<setlogsock()> to use a stream socket, but didn't |
8168e71f |
1335 | provide a path, and C<Sys::Syslog> was unable to find an appropriate one. |
1336 | |
a650b841 |
1337 | =item C<tcp passed to setlogsock, but tcp service unavailable> |
8168e71f |
1338 | |
942974c1 |
1339 | B<(W)> You asked C<setlogsock()> to use a TCP socket, but the service |
8168e71f |
1340 | is not available on the system. |
1341 | |
a650b841 |
1342 | =item C<syslog: expecting argument %s> |
8168e71f |
1343 | |
1344 | B<(F)> You forgot to give C<syslog()> the indicated argument. |
1345 | |
a650b841 |
1346 | =item C<syslog: invalid level/facility: %s> |
8168e71f |
1347 | |
6e4ef777 |
1348 | B<(F)> You specified an invalid level or facility. |
8168e71f |
1349 | |
a650b841 |
1350 | =item C<syslog: too many levels given: %s> |
8168e71f |
1351 | |
1352 | B<(F)> You specified too many levels. |
1353 | |
a650b841 |
1354 | =item C<syslog: too many facilities given: %s> |
8168e71f |
1355 | |
1356 | B<(F)> You specified too many facilities. |
1357 | |
a650b841 |
1358 | =item C<syslog: level must be given> |
8168e71f |
1359 | |
1360 | B<(F)> You forgot to specify a level. |
1361 | |
a650b841 |
1362 | =item C<udp passed to setlogsock, but udp service unavailable> |
8168e71f |
1363 | |
942974c1 |
1364 | B<(W)> You asked C<setlogsock()> to use a UDP socket, but the service |
8168e71f |
1365 | is not available on the system. |
1366 | |
a650b841 |
1367 | =item C<unix passed to setlogsock, but path not available> |
8168e71f |
1368 | |
942974c1 |
1369 | B<(W)> You asked C<setlogsock()> to use a UNIX socket, but C<Sys::Syslog> |
8168e71f |
1370 | was unable to find an appropriate an appropriate device. |
1371 | |
1372 | =back |
1373 | |
1374 | |
5be1dfc7 |
1375 | =head1 SEE ALSO |
1376 | |
a650b841 |
1377 | =head2 Manual Pages |
1378 | |
5be1dfc7 |
1379 | L<syslog(3)> |
1380 | |
6e4ef777 |
1381 | SUSv3 issue 6, IEEE Std 1003.1, 2004 edition, |
1382 | L<http://www.opengroup.org/onlinepubs/000095399/basedefs/syslog.h.html> |
1383 | |
1384 | GNU C Library documentation on syslog, |
1385 | L<http://www.gnu.org/software/libc/manual/html_node/Syslog.html> |
1386 | |
1387 | Solaris 10 documentation on syslog, |
f93f88eb |
1388 | L<http://docs.sun.com/app/docs/doc/816-5168/syslog-3c?a=view> |
1389 | |
1390 | Mac OS X documentation on syslog, |
1391 | L<http://developer.apple.com/documentation/Darwin/Reference/ManPages/man3/syslog.3.html> |
6e4ef777 |
1392 | |
f93f88eb |
1393 | IRIX 6.5 documentation on syslog, |
1394 | L<http://techpubs.sgi.com/library/tpl/cgi-bin/getdoc.cgi?coll=0650&db=man&fname=3c+syslog> |
a650b841 |
1395 | |
6e4ef777 |
1396 | AIX 5L 5.3 documentation on syslog, |
d329efa2 |
1397 | L<http://publib.boulder.ibm.com/infocenter/pseries/v5r3/index.jsp?topic=/com.ibm.aix.basetechref/doc/basetrf2/syslog.htm> |
6e4ef777 |
1398 | |
1399 | HP-UX 11i documentation on syslog, |
f93f88eb |
1400 | L<http://docs.hp.com/en/B2355-60130/syslog.3C.html> |
6e4ef777 |
1401 | |
1402 | Tru64 5.1 documentation on syslog, |
1403 | L<http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/V51_HTML/MAN/MAN3/0193____.HTM> |
1404 | |
1405 | Stratus VOS 15.1, |
1406 | L<http://stratadoc.stratus.com/vos/15.1.1/r502-01/wwhelp/wwhimpl/js/html/wwhelp.htm?context=r502-01&file=ch5r502-01bi.html> |
1407 | |
a650b841 |
1408 | =head2 RFCs |
1409 | |
6e4ef777 |
1410 | I<RFC 3164 - The BSD syslog Protocol>, L<http://www.faqs.org/rfcs/rfc3164.html> |
1411 | -- Please note that this is an informational RFC, and therefore does not |
1412 | specify a standard of any kind. |
1413 | |
1414 | I<RFC 3195 - Reliable Delivery for syslog>, L<http://www.faqs.org/rfcs/rfc3195.html> |
1415 | |
a650b841 |
1416 | =head2 Articles |
1417 | |
04f98b29 |
1418 | I<Syslogging with Perl>, L<http://lexington.pm.org/meetings/022001.html> |
1419 | |
a650b841 |
1420 | =head2 Event Log |
8168e71f |
1421 | |
a650b841 |
1422 | Windows Event Log, |
1423 | L<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wes/wes/windows_event_log.asp> |
5be1dfc7 |
1424 | |
a650b841 |
1425 | |
1426 | =head1 AUTHORS & ACKNOWLEDGEMENTS |
1427 | |
1428 | Tom Christiansen E<lt>F<tchrist (at) perl.com>E<gt> and Larry Wall |
1429 | E<lt>F<larry (at) wall.org>E<gt>. |
150b260b |
1430 | |
1431 | UNIX domain sockets added by Sean Robinson |
a650b841 |
1432 | E<lt>F<robinson_s (at) sc.maricopa.edu>E<gt> with support from Tim Bunce |
1433 | E<lt>F<Tim.Bunce (at) ig.co.uk>E<gt> and the C<perl5-porters> mailing list. |
150b260b |
1434 | |
1435 | Dependency on F<syslog.ph> replaced with XS code by Tom Hughes |
a650b841 |
1436 | E<lt>F<tom (at) compton.nu>E<gt>. |
5be1dfc7 |
1437 | |
a650b841 |
1438 | Code for C<constant()>s regenerated by Nicholas Clark E<lt>F<nick (at) ccl4.org>E<gt>. |
23642f4b |
1439 | |
1440 | Failover to different communication modes by Nick Williams |
a650b841 |
1441 | E<lt>F<Nick.Williams (at) morganstanley.com>E<gt>. |
1442 | |
1443 | Extracted from core distribution for publishing on the CPAN by |
1444 | SE<eacute>bastien Aperghis-Tramoni E<lt>sebastien (at) aperghis.netE<gt>. |
b903fcff |
1445 | |
89c3c464 |
1446 | XS code for using native C functions borrowed from C<L<Unix::Syslog>>, |
a650b841 |
1447 | written by Marcus Harnisch E<lt>F<marcus.harnisch (at) gmx.net>E<gt>. |
89c3c464 |
1448 | |
a650b841 |
1449 | Yves Orton suggested and helped for making C<Sys::Syslog> use the native |
1450 | event logger under Win32 systems. |
1451 | |
1452 | Jerry D. Hedden and Reini Urban provided greatly appreciated help to |
1453 | debug and polish C<Sys::Syslog> under Cygwin. |
8168e71f |
1454 | |
1455 | |
1456 | =head1 BUGS |
1457 | |
1458 | Please report any bugs or feature requests to |
a650b841 |
1459 | C<bug-sys-syslog (at) rt.cpan.org>, or through the web interface at |
35a209d1 |
1460 | L<http://rt.cpan.org/Public/Dist/Display.html?Name=Sys-Syslog>. |
8168e71f |
1461 | I will be notified, and then you'll automatically be notified of progress on |
1462 | your bug as I make changes. |
1463 | |
1464 | |
1465 | =head1 SUPPORT |
1466 | |
1467 | You can find documentation for this module with the perldoc command. |
1468 | |
1469 | perldoc Sys::Syslog |
1470 | |
1471 | You can also look for information at: |
1472 | |
1473 | =over 4 |
1474 | |
1475 | =item * AnnoCPAN: Annotated CPAN documentation |
1476 | |
1477 | L<http://annocpan.org/dist/Sys-Syslog> |
1478 | |
1479 | =item * CPAN Ratings |
1480 | |
1481 | L<http://cpanratings.perl.org/d/Sys-Syslog> |
1482 | |
1483 | =item * RT: CPAN's request tracker |
1484 | |
1485 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Sys-Syslog> |
1486 | |
1487 | =item * Search CPAN |
1488 | |
6e4ef777 |
1489 | L<http://search.cpan.org/dist/Sys-Syslog/> |
1490 | |
1491 | =item * Kobes' CPAN Search |
1492 | |
1493 | L<http://cpan.uwinnipeg.ca/dist/Sys-Syslog> |
1494 | |
1495 | =item * Perl Documentation |
1496 | |
1497 | L<http://perldoc.perl.org/Sys/Syslog.html> |
8168e71f |
1498 | |
1499 | =back |
1500 | |
1501 | |
35a209d1 |
1502 | =head1 COPYRIGHT |
1503 | |
f93f88eb |
1504 | Copyright (C) 1990-2008 by Larry Wall and others. |
35a209d1 |
1505 | |
1506 | |
8168e71f |
1507 | =head1 LICENSE |
1508 | |
1509 | This program is free software; you can redistribute it and/or modify it |
1510 | under the same terms as Perl itself. |
1511 | |
5be1dfc7 |
1512 | =cut |
a650b841 |
1513 | |
1514 | =begin comment |
1515 | |
1516 | Notes for the future maintainer (even if it's still me..) |
1517 | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
1518 | |
1519 | Using Google Code Search, I search who on Earth was relying on $host being |
1520 | public. It found 5 hits: |
1521 | |
1522 | * First was inside Indigo Star Perl2exe documentation. Just an old version |
1523 | of Sys::Syslog. |
1524 | |
1525 | |
1526 | * One real hit was inside DalWeathDB, a weather related program. It simply |
1527 | does a |
1528 | |
1529 | $Sys::Syslog::host = '127.0.0.1'; |
1530 | |
1531 | - L<http://www.gallistel.net/nparker/weather/code/> |
1532 | |
1533 | |
1534 | * Two hits were in TPC, a fax server thingy. It does a |
1535 | |
1536 | $Sys::Syslog::host = $TPC::LOGHOST; |
1537 | |
1538 | but also has this strange piece of code: |
1539 | |
1540 | # work around perl5.003 bug |
1541 | sub Sys::Syslog::hostname {} |
1542 | |
1543 | I don't know what bug the author referred to. |
1544 | |
1545 | - L<http://www.tpc.int/> |
1546 | - L<ftp://ftp.tpc.int/tpc/server/UNIX/> |
1547 | - L<ftp://ftp-usa.tpc.int/pub/tpc/server/UNIX/> |
1548 | |
1549 | |
1550 | * Last hit was in Filefix, which seems to be a FIDOnet mail program (!). |
1551 | This one does not use $host, but has the following piece of code: |
1552 | |
1553 | sub Sys::Syslog::hostname |
1554 | { |
1555 | use Sys::Hostname; |
1556 | return hostname; |
1557 | } |
1558 | |
1559 | I guess this was a more elaborate form of the previous bit, maybe because |
1560 | of a bug in Sys::Syslog back then? |
1561 | |
1562 | - L<ftp://ftp.kiae.su/pub/unix/fido/> |
1563 | |
d329efa2 |
1564 | |
1565 | Links |
1566 | ----- |
f93f88eb |
1567 | Linux Fast-STREAMS |
1568 | - L<http://www.openss7.org/streams.html> |
1569 | |
d329efa2 |
1570 | II12021: SYSLOGD HOWTO TCPIPINFO (z/OS, OS/390, MVS) |
1571 | - L<http://www-1.ibm.com/support/docview.wss?uid=isg1II12021> |
1572 | |
1573 | Getting the most out of the Event Viewer |
1574 | - L<http://www.codeproject.com/dotnet/evtvwr.asp?print=true> |
1575 | |
1576 | Log events to the Windows NT Event Log with JNI |
1577 | - L<http://www.javaworld.com/javaworld/jw-09-2001/jw-0928-ntmessages.html> |
1578 | |
a650b841 |
1579 | =end comment |
d329efa2 |
1580 | |