7 @ISA = qw(Exporter DynaLoader);
8 @EXPORT = qw(openlog closelog setlogmask syslog);
9 @EXPORT_OK = qw(setlogsock);
12 # it would be nice to try stream/unix first, since that will be
13 # most efficient. However streams are dodgy - see _syslog_send_stream
14 #my @connectMethods = ( 'stream', 'unix', 'tcp', 'udp' );
15 my @connectMethods = ( 'tcp', 'udp', 'unix', 'stream', 'console' );
16 if ($^O =~ /^(freebsd|linux)$/) {
17 @connectMethods = grep { $_ ne 'udp' } @connectMethods;
19 my @defaultMethods = @connectMethods;
20 my $syslog_path = undef;
22 my $current_proto = undef;
24 my $fail_time = undef;
31 Sys::Syslog, openlog, closelog, setlogmask, syslog - Perl interface to the UNIX syslog(3) calls
35 use Sys::Syslog; # all except setlogsock, or:
36 use Sys::Syslog qw(:DEFAULT setlogsock); # default set, plus setlogsock
38 setlogsock $sock_type;
39 openlog $ident, $logopt, $facility; # don't forget this
40 syslog $priority, $format, @args;
41 $oldmask = setlogmask $mask_priority;
46 Sys::Syslog is an interface to the UNIX C<syslog(3)> program.
47 Call C<syslog()> with a string priority and a list of C<printf()> args
48 just like C<syslog(3)>.
50 Syslog provides the functions:
54 =item openlog $ident, $logopt, $facility
56 I<$ident> is prepended to every message. I<$logopt> contains zero or
57 more of the words I<pid>, I<ndelay>, I<nowait>. The cons option is
58 ignored, since the failover mechanism will drop down to the console
59 automatically if all other media fail. I<$facility> specifies the
60 part of the system to report about, for example LOG_USER or LOG_LOCAL0:
61 see your C<syslog(3)> documentation for the facilities available in
64 B<You should use openlog() before calling syslog().>
66 =item syslog $priority, $format, @args
68 If I<$priority> permits, logs I<($format, @args)>
69 printed as by C<printf(3V)>, with the addition that I<%m>
70 is replaced with C<"$!"> (the latest error message).
72 If you didn't use openlog() before using syslog(), syslog will try to
73 guess the I<$ident> by extracting the shortest prefix of I<$format>
76 =item setlogmask $mask_priority
78 Sets log mask I<$mask_priority> and returns the old mask.
80 =item setlogsock $sock_type [$stream_location] (added in 5.004_02)
82 Sets the socket type to be used for the next call to
83 C<openlog()> or C<syslog()> and returns TRUE on success,
86 A value of 'unix' will connect to the UNIX domain socket (in some
87 systems a character special device) returned by the C<_PATH_LOG> macro
88 (if your system defines it), or F</dev/log> or F</dev/conslog>,
89 whatever is writable. A value of 'stream' will connect to the stream
90 indicated by the pathname provided as the optional second parameter.
91 (For example Solaris and IRIX require 'stream' instead of 'unix'.)
92 A value of 'inet' will connect to an INET socket (either tcp or udp,
93 tried in that order) returned by getservbyname(). 'tcp' and 'udp' can
94 also be given as values. The value 'console' will send messages
95 directly to the console, as for the 'cons' option in the logopts in
98 A reference to an array can also be passed as the first parameter.
99 When this calling method is used, the array should contain a list of
100 sock_types which are attempted in order.
102 The default is to try tcp, udp, unix, stream, console.
104 Giving an invalid value for sock_type will croak.
112 Note that C<openlog> now takes three arguments, just like C<openlog(3)>.
116 openlog($program, 'cons,pid', 'user');
117 syslog('info', 'this is another test');
118 syslog('mail|warning', 'this is a better test: %d', time);
121 syslog('debug', 'this is the last test');
124 openlog("$program $$", 'ndelay', 'user');
125 syslog('notice', 'fooprogram: this is really done');
129 syslog('info', 'problem was %m'); # %m == $! in syslog(3)
131 # Log to UDP port on $remotehost instead of logging locally
133 $Sys::Syslog::host = $remotehost;
134 openlog($program, 'ndelay', 'user');
135 syslog('info', 'something happened over here');
143 Tom Christiansen E<lt>F<tchrist@perl.com>E<gt> and Larry Wall
144 E<lt>F<larry@wall.org>E<gt>.
146 UNIX domain sockets added by Sean Robinson
147 E<lt>F<robinson_s@sc.maricopa.edu>E<gt> with support from Tim Bunce
148 E<lt>F<Tim.Bunce@ig.co.uk>E<gt> and the perl5-porters mailing list.
150 Dependency on F<syslog.ph> replaced with XS code by Tom Hughes
151 E<lt>F<tom@compton.nu>E<gt>.
153 Code for constant()s regenerated by Nicholas Clark E<lt>F<nick@ccl4.org>E<gt>.
155 Failover to different communication modes by Nick Williams
156 E<lt>F<Nick.Williams@morganstanley.com>E<gt>.
161 # This AUTOLOAD is used to 'autoload' constants from the constant()
166 ($constname = $AUTOLOAD) =~ s/.*:://;
167 croak "&Sys::Syslog::constant not defined" if $constname eq 'constant';
168 my ($error, $val) = constant($constname);
172 *$AUTOLOAD = sub { $val };
176 bootstrap Sys::Syslog $VERSION;
178 $maskpri = &LOG_UPTO(&LOG_DEBUG);
181 ($ident, $logopt, $facility) = @_; # package vars
182 $lo_pid = $logopt =~ /\bpid\b/;
183 $lo_ndelay = $logopt =~ /\bndelay\b/;
184 $lo_nowait = $logopt =~ /\bnowait\b/;
185 return 1 unless $lo_ndelay;
190 $facility = $ident = '';
195 local($oldmask) = $maskpri;
201 local($setsock) = shift;
202 $syslog_path = shift;
203 &disconnect if $connected;
205 @fallbackMethods = ();
206 @connectMethods = @defaultMethods;
207 if (ref $setsock eq 'ARRAY') {
208 @connectMethods = @$setsock;
209 } elsif (lc($setsock) eq 'stream') {
210 unless (defined $syslog_path) {
211 my @try = qw(/dev/log /dev/conslog);
212 if (length &_PATH_LOG) { # Undefined _PATH_LOG is "".
213 unshift @try, &_PATH_LOG;
221 carp "stream passed to setlogsock, but could not find any device"
222 unless defined $syslog_path;
224 unless (-w $syslog_path) {
225 carp "stream passed to setlogsock, but $syslog_path is not writable";
228 @connectMethods = ( 'stream' );
230 } elsif (lc($setsock) eq 'unix') {
231 if (length _PATH_LOG() && !defined $syslog_path) {
232 $syslog_path = _PATH_LOG();
233 @connectMethods = ( 'unix' );
235 carp 'unix passed to setlogsock, but path not available';
238 } elsif (lc($setsock) eq 'tcp') {
239 if (getservbyname('syslog', 'tcp') || getservbyname('syslogng', 'tcp')) {
240 @connectMethods = ( 'tcp' );
242 carp "tcp passed to setlogsock, but tcp service unavailable";
245 } elsif (lc($setsock) eq 'udp') {
246 if (getservbyname('syslog', 'udp')) {
247 @connectMethods = ( 'udp' );
249 carp "udp passed to setlogsock, but udp service unavailable";
252 } elsif (lc($setsock) eq 'inet') {
253 @connectMethods = ( 'tcp', 'udp' );
254 } elsif (lc($setsock) eq 'console') {
255 @connectMethods = ( 'console' );
257 carp "Invalid argument passed to setlogsock; must be 'stream', 'unix', 'tcp', 'udp' or 'inet'";
263 local($priority) = shift;
264 local($mask) = shift;
265 local($message, $whoami);
266 local(@words, $num, $numpri, $numfac, $sum);
267 local($facility) = $facility; # may need to change temporarily.
269 croak "syslog: expecting argument \$priority" unless $priority;
270 croak "syslog: expecting argument \$format" unless $mask;
272 @words = split(/\W+/, $priority, 2);# Allow "level" or "level|facility".
276 $num = &xlate($_); # Translate word to number.
277 if (/^kern$/ || $num < 0) {
278 croak "syslog: invalid level/facility: $_";
280 elsif ($num <= &LOG_PRIMASK) {
281 croak "syslog: too many levels given: $_" if defined($numpri);
283 return 0 unless &LOG_MASK($numpri) & $maskpri;
286 croak "syslog: too many facilities given: $_" if defined($numfac);
292 croak "syslog: level must be given" unless defined($numpri);
294 if (!defined($numfac)) { # Facility not specified in this call.
295 $facility = 'user' unless $facility;
296 $numfac = &xlate($facility);
299 &connect unless $connected;
303 if (!$whoami && $mask =~ /^(\S.*?):\s?(.*)/) {
309 ($whoami = getlogin) ||
310 ($whoami = getpwuid($<)) ||
311 ($whoami = 'syslog');
314 $whoami .= "[$$]" if $lo_pid;
316 $mask =~ s/(?<!%)%m/$!/g;
317 $mask .= "\n" unless $mask =~ /\n$/;
318 $message = sprintf ($mask, @_);
320 $sum = $numpri + $numfac;
321 my $buf = "<$sum>$whoami: $message\0";
323 # it's possible that we'll get an error from sending
324 # (e.g. if method is UDP and there is no UDP listener,
325 # then we'll get ECONNREFUSED on the send). So what we
326 # want to do at this point is to fallback onto a different
328 while (scalar @fallbackMethods || $syslog_send) {
329 if ($failed && (time - $fail_time) > 60) {
330 # it's been a while... maybe things have been fixed
331 @fallbackMethods = ();
333 $transmit_ok = 0; # make it look like a fresh attempt
336 if ($connected && !connection_ok()) {
337 # Something was OK, but has now broken. Remember coz we'll
338 # want to go back to what used to be OK.
339 $failed = $current_proto unless $failed;
343 &connect unless $connected;
344 $failed = undef if ($current_proto && $failed && $current_proto eq $failed);
346 if (&{$syslog_send}($buf)) {
350 # typically doesn't happen, since errors are rare from write().
354 # could not send, could not fallback onto a working
355 # connection method. Lose.
359 sub _syslog_send_console {
361 chop($buf); # delete the NUL from the end
362 # The console print is a method which could block
363 # so we do it in a child process and always return success
365 if (my $pid = fork) {
369 if (waitpid($pid, 0) >= 0) {
372 # it's possible that the caller has other
373 # plans for SIGCHLD, so let's not interfere
378 if (open(CONS, ">/dev/console")) {
379 my $ret = print CONS $buf . "\r";
380 exit ($ret) if defined $pid;
383 exit if defined $pid;
387 sub _syslog_send_stream {
389 # XXX: this only works if the OS stream implementation makes a write
390 # look like a putmsg() with simple header. For instance it works on
391 # Solaris 8 but not Solaris 7.
392 # To be correct, it should use a STREAMS API, but perl doesn't have one.
393 return syswrite(SYSLOG, $buf, length($buf));
395 sub _syslog_send_socket {
397 return syswrite(SYSLOG, $buf, length($buf));
398 #return send(SYSLOG, $buf, 0);
403 return $name+0 if $name =~ /^\s*\d+\s*$/;
405 $name = "LOG_$name" unless $name =~ /^LOG_/;
406 $name = "Sys::Syslog::$name";
407 # Can't have just eval { &$name } || -1 because some LOG_XXX may be zero.
408 my $value = eval { &$name };
409 defined $value ? $value : -1;
413 @fallbackMethods = @connectMethods unless (scalar @fallbackMethods);
414 if ($transmit_ok && $current_proto) {
415 # Retry what we were on, because it's worked in the past.
416 unshift(@fallbackMethods, $current_proto);
421 while ($proto = shift(@fallbackMethods)) {
422 my $fn = "connect_$proto";
423 $connected = &$fn(\@errs) unless (!defined &$fn);
424 last if ($connected);
429 $current_proto = $proto;
430 local($old) = select(SYSLOG); $| = 1; select($old);
432 @fallbackMethods = ();
433 foreach my $err (@errs) {
436 croak "no connection to syslog available";
443 require Sys::Hostname;
444 my($host_uniq) = Sys::Hostname::hostname();
445 ($host) = $host_uniq =~ /([A-Za-z0-9_.-]+)/; # allow FQDN (inc _)
447 my $tcp = getprotobyname('tcp');
449 push(@{$errs}, "getprotobyname failed for tcp");
452 my $syslog = getservbyname('syslog','tcp');
453 $syslog = getservbyname('syslogng','tcp') unless (defined $syslog);
454 if (!defined $syslog) {
455 push(@{$errs}, "getservbyname failed for tcp");
459 my $this = sockaddr_in($syslog, INADDR_ANY);
460 my $that = sockaddr_in($syslog, inet_aton($host));
462 push(@{$errs}, "can't lookup $host");
465 if (!socket(SYSLOG,AF_INET,SOCK_STREAM,$tcp)) {
466 push(@{$errs}, "tcp socket: $!");
469 setsockopt(SYSLOG, SOL_SOCKET, SO_KEEPALIVE, 1);
470 setsockopt(SYSLOG, IPPROTO_TCP, TCP_NODELAY, 1);
471 if (!CORE::connect(SYSLOG,$that)) {
472 push(@{$errs}, "tcp connect: $!");
475 $syslog_send = \&_syslog_send_socket;
482 require Sys::Hostname;
483 my($host_uniq) = Sys::Hostname::hostname();
484 ($host) = $host_uniq =~ /([A-Za-z0-9_.-]+)/; # allow FQDN (inc _)
486 my $udp = getprotobyname('udp');
488 push(@{$errs}, "getprotobyname failed for udp");
491 my $syslog = getservbyname('syslog','udp');
492 if (!defined $syslog) {
493 push(@{$errs}, "getservbyname failed for udp");
496 my $this = sockaddr_in($syslog, INADDR_ANY);
497 my $that = sockaddr_in($syslog, inet_aton($host));
499 push(@{$errs}, "can't lookup $host");
502 if (!socket(SYSLOG,AF_INET,SOCK_DGRAM,$udp)) {
503 push(@{$errs}, "udp socket: $!");
506 if (!CORE::connect(SYSLOG,$that)) {
507 push(@{$errs}, "udp connect: $!");
510 # We want to check that the UDP connect worked. However the only
511 # way to do that is to send a message and see if an ICMP is returned
512 _syslog_send_socket("");
513 if (!connection_ok()) {
514 push(@{$errs}, "udp connect: nobody listening");
517 $syslog_send = \&_syslog_send_socket;
523 # might want syslog_path to be variable based on syslog.h (if only
525 $syslog_path = '/dev/conslog';
526 if (!-w $syslog_path) {
527 push(@{$errs}, "stream $syslog_path is not writable");
530 if (!open(SYSLOG, ">" . $syslog_path)) {
531 push(@{$errs}, "stream can't open $syslog_path: $!");
534 $syslog_send = \&_syslog_send_stream;
540 if (length _PATH_LOG()) {
541 $syslog_path = _PATH_LOG();
543 push(@{$errs}, "_PATH_LOG not available in syslog.h");
546 my $that = sockaddr_un($syslog_path);
548 push(@{$errs}, "can't locate $syslog_path");
551 if (!socket(SYSLOG,AF_UNIX,SOCK_STREAM,0)) {
552 push(@{$errs}, "unix stream socket: $!");
555 if (!CORE::connect(SYSLOG,$that)) {
556 if (!socket(SYSLOG,AF_UNIX,SOCK_DGRAM,0)) {
557 push(@{$errs}, "unix dgram socket: $!");
560 if (!CORE::connect(SYSLOG,$that)) {
561 push(@{$errs}, "unix dgram connect: $!");
565 $syslog_send = \&_syslog_send_socket;
569 sub connect_console {
571 if (!-w '/dev/console') {
572 push(@{$errs}, "console is not writable");
575 $syslog_send = \&_syslog_send_console;
579 # to test if the connection is still good, we need to check if any
580 # errors are present on the connection. The errors will not be raised
581 # by a write. Instead, sockets are made readable and the next read
582 # would cause the error to be returned. Unfortunately the syslog
583 # 'protocol' never provides anything for us to read. But with
584 # judicious use of select(), we can see if it would be readable...
586 return 1 if (defined $current_proto && $current_proto eq 'console');
588 vec($rin, fileno(SYSLOG), 1) = 1;
589 my $ret = select $rin, undef, $rin, 0;
590 return ($ret ? 0 : 1);
596 $syslog_send = undef;