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