5.005_03-MAINT_TRIAL_5 utils/h2xs fixing -A & more
[p5sagit/p5-mst-13.2.git] / lib / Sys / Syslog.pm
index 0a0d25e..f0cbb71 100644 (file)
@@ -5,36 +5,114 @@ use Carp;
 
 @ISA = qw(Exporter);
 @EXPORT = qw(openlog closelog setlogmask syslog);
+@EXPORT_OK = qw(setlogsock);
 
+use Socket;
+use Sys::Hostname;
+
+# adapted from syslog.pl
 #
-# syslog.pl
-#
-# $Log:        syslog.pl,v $
-# 
-# tom christiansen <tchrist@convex.com>
+# Tom Christiansen <tchrist@convex.com>
 # modified to use sockets by Larry Wall <lwall@jpl-devvax.jpl.nasa.gov>
 # NOTE: openlog now takes three arguments, just like openlog(3)
-#
-# call syslog() with a string priority and a list of printf() args
-# like syslog(3)
-#
-#  usage: use Syslog;
-#
-#  then (put these all in a script to test function)
-#              
-#      openlog($program,'cons,pid','user');
-#      syslog('info','this is another test');
-#      syslog('mail|warning','this is a better test: %d', time);
-#      closelog();
-#      
-#      syslog('debug','this is the last test');
-#      openlog("$program $$",'ndelay','user');
-#      syslog('notice','fooprogram: this is really done');
-#
-#      $! = 55;
-#      syslog('info','problem was %m'); # %m == $! in syslog(3)
+# Modified to add UNIX domain sockets by Sean Robinson <robinson_s@sc.maricopa.edu>
+#  with support from Tim Bunce <Tim.Bunce@ig.co.uk> and the perl5-porters mailing list
+
+# Todo: enable connect to try all three types before failing (auto setlogsock)?
+
+=head1 NAME
+
+Sys::Syslog, openlog, closelog, setlogmask, syslog - Perl interface to the UNIX syslog(3) calls
+
+=head1 SYNOPSIS
+
+    use Sys::Syslog;                          # all except setlogsock, or:
+    use Sys::Syslog qw(:DEFAULT setlogsock);  # default set, plus setlogsock
+
+    setlogsock $sock_type;
+    openlog $ident, $logopt, $facility;
+    syslog $priority, $format, @args;
+    $oldmask = setlogmask $mask_priority;
+    closelog;
+
+=head1 DESCRIPTION
+
+Sys::Syslog is an interface to the UNIX C<syslog(3)> program.
+Call C<syslog()> with a string priority and a list of C<printf()> args
+just like C<syslog(3)>.
+
+Syslog provides the functions:
+
+=over
+
+=item openlog $ident, $logopt, $facility
+
+I<$ident> is prepended to every message.
+I<$logopt> contains zero or more of the words I<pid>, I<ndelay>, I<cons>, I<nowait>.
+I<$facility> specifies the part of the system
+
+=item syslog $priority, $format, @args
+
+If I<$priority> permits, logs I<($format, @args)>
+printed as by C<printf(3V)>, with the addition that I<%m>
+is replaced with C<"$!"> (the latest error message).
+
+=item setlogmask $mask_priority
+
+Sets log mask I<$mask_priority> and returns the old mask.
+
+=item setlogsock $sock_type (added in 5.004_02)
+
+Sets the socket type to be used for the next call to
+C<openlog()> or C<syslog()> and returns TRUE on success,
+undef on failure.
+
+A value of 'unix' will connect to the UNIX domain socket returned by
+C<_PATH_LOG> in F<syslog.ph>.  A value of 'inet' will connect to an
+INET socket returned by getservbyname().  Any other value croaks.
+
+The default is for the INET socket to be used.
+
+=item closelog
+
+Closes the log file.
 
-$host = 'localhost' unless $host;      # set $Syslog::host to change
+=back
+
+Note that C<openlog> now takes three arguments, just like C<openlog(3)>.
+
+=head1 EXAMPLES
+
+    openlog($program, 'cons,pid', 'user');
+    syslog('info', 'this is another test');
+    syslog('mail|warning', 'this is a better test: %d', time);
+    closelog();
+
+    syslog('debug', 'this is the last test');
+
+    setlogsock('unix');
+    openlog("$program $$", 'ndelay', 'user');
+    syslog('notice', 'fooprogram: this is really done');
+
+    setlogsock('inet');
+    $! = 55;
+    syslog('info', 'problem was %m'); # %m == $! in syslog(3)
+
+=head1 DEPENDENCIES
+
+B<Sys::Syslog> needs F<syslog.ph>, which can be created with C<h2ph>.
+
+=head1 SEE ALSO
+
+L<syslog(3)>
+
+=head1 AUTHOR
+
+Tom Christiansen E<lt>F<tchrist@perl.com>E<gt> and Larry Wall E<lt>F<larry@wall.org>E<gt>.
+UNIX domain sockets added by Sean Robinson E<lt>F<robinson_s@sc.maricopa.edu>E<gt>
+with support from Tim Bunce <Tim.Bunce@ig.co.uk> and the perl5-porters mailing list.
+
+=cut
 
 require 'syslog.ph';
 
@@ -46,7 +124,8 @@ sub openlog {
     $lo_ndelay = $logopt =~ /\bndelay\b/;
     $lo_cons = $logopt =~ /\bcons\b/;
     $lo_nowait = $logopt =~ /\bnowait\b/;
-    &connect if $lo_ndelay;
+    return 1 unless $lo_ndelay;
+    &connect;
 } 
 
 sub closelog {
@@ -60,6 +139,27 @@ sub setlogmask {
     $oldmask;
 }
  
+sub setlogsock {
+    local($setsock) = shift;
+    &disconnect if $connected;
+    if (lc($setsock) eq 'unix') {
+        if (defined &_PATH_LOG) {
+            $sock_type = 1;
+        } else {
+            return undef;
+        }
+    } elsif (lc($setsock) eq 'inet') {
+        if (getservbyname('syslog','udp')) {
+            undef($sock_type);
+        } else {
+            return undef;
+        }
+    } else {
+        croak "Invalid argument passed to setlogsock; must be 'unix' or 'inet'";
+    }
+    return 1;
+}
+
 sub syslog {
     local($priority) = shift;
     local($mask) = shift;
@@ -100,7 +200,7 @@ sub syslog {
 
     $whoami = $ident;
 
-    if (!$ident && $mask =~ /^(\S.*):\s?(.*)/) {
+    if (!$whoami && $mask =~ /^(\S.*?):\s?(.*)/) {
        $whoami = $1;
        $mask = $2;
     } 
@@ -118,11 +218,11 @@ sub syslog {
     $message = sprintf ($mask, @_);
 
     $sum = $numpri + $numfac;
-    unless (send(SYSLOG,"<$sum>$whoami: $message",0)) {
+    unless (send(SYSLOG,"<$sum>$whoami: $message\0",0)) {
        if ($lo_cons) {
            if ($pid = fork) {
                unless ($lo_nowait) {
-                   do {$died = wait;} until $died == $pid || $died < 0;
+                   $died = waitpid($pid, 0);
                }
            }
            else {
@@ -137,51 +237,34 @@ sub syslog {
 
 sub xlate {
     local($name) = @_;
-    $name =~ y/a-z/A-Z/;
+    $name = uc $name;
     $name = "LOG_$name" unless $name =~ /^LOG_/;
     $name = "Sys::Syslog::$name";
-    eval(&$name) || -1;
+    defined &$name ? &$name : -1;
 }
 
 sub connect {
-    $pat = 'S n C4 x8';
-
-    $af_unix = 1;
-    $af_inet = 2;
-
-    $stream = 1;
-    $datagram = 2;
-
-    ($name,$aliases,$proto) = getprotobyname('udp');
-    $udp = $proto;
-
-    ($name,$aliase,$port,$proto) = getservbyname('syslog','udp');
-    $syslog = $port;
-
-    if (chop($myname = `hostname`)) {
-       ($name,$aliases,$addrtype,$length,@addrs) = gethostbyname($myname);
-       croak "Can't lookup $myname" unless $name;
-       @bytes = unpack("C4",$addrs[0]);
+    unless ($host) {
+       require Sys::Hostname;
+       my($host_uniq) = Sys::Hostname::hostname();
+       ($host) = $host_uniq =~ /([A-Za-z0-9_.-]+)/; # allow FQDN (inc _)
     }
-    else {
-       @bytes = (0,0,0,0);
+    unless ( $sock_type ) {
+        my $udp = getprotobyname('udp');
+        my $syslog = getservbyname('syslog','udp');
+        my $this = sockaddr_in($syslog, INADDR_ANY);
+        my $that = sockaddr_in($syslog, inet_aton($host) || croak "Can't lookup $host");
+        socket(SYSLOG,AF_INET,SOCK_DGRAM,$udp)           || croak "socket: $!";
+        connect(SYSLOG,$that)                            || croak "connect: $!";
+    } else {
+        my $syslog = &_PATH_LOG                          || croak "_PATH_LOG not found in syslog.ph";
+        my $that = sockaddr_un($syslog)                  || croak "Can't locate $syslog";
+        socket(SYSLOG,AF_UNIX,SOCK_STREAM,0)             || croak "socket: $!";
+        if (!connect(SYSLOG,$that)) {
+            socket(SYSLOG,AF_UNIX,SOCK_DGRAM,0)          || croak "socket: $!";
+            connect(SYSLOG,$that) || croak "connect: $! (SOCK_DGRAM after trying SOCK_STREAM)";
+        }
     }
-    $this = pack($pat, $af_inet, 0, @bytes);
-
-    if ($host =~ /^\d+\./) {
-       @bytes = split(/\./,$host);
-    }
-    else {
-       ($name,$aliases,$addrtype,$length,@addrs) = gethostbyname($host);
-       croak "Can't lookup $host" unless $name;
-       @bytes = unpack("C4",$addrs[0]);
-    }
-    $that = pack($pat,$af_inet,$syslog,@bytes);
-
-    socket(SYSLOG,$af_inet,$datagram,$udp) || croak "socket: $!";
-    bind(SYSLOG,$this) || croak "bind: $!";
-    connect(SYSLOG,$that) || croak "connect: $!";
-
     local($old) = select(SYSLOG); $| = 1; select($old);
     $connected = 1;
 }
@@ -192,4 +275,3 @@ sub disconnect {
 }
 
 1;
-