use Carp;
@ISA = qw(Exporter);
-@EXPORT = qw(openlog closelog setlogmask syslog);
+@EXPORT = qw(openlog closelog setlogmask setlogsock syslog);
use Socket;
use Sys::Hostname;
# 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)
+# 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
=head1 NAME
-Sys::Syslog, openlog, closelog, setlogmask, syslog - Perl interface to the UNIX syslog(3) calls
+Sys::Syslog, openlog, closelog, setlogmask, setlogsock, syslog - Perl interface to the UNIX syslog(3) calls
=head1 SYNOPSIS
use Sys::Syslog;
+ setlogsock $sock_unix;
openlog $ident, $logopt, $facility;
syslog $priority, $format, @args;
$oldmask = setlogmask $mask_priority;
Sets log mask I<$mask_priority> and returns the old mask.
+=item setlogsock $sock_unix
+
+Defines or undefines the boolean I<$sock_unix>;
+a value equalling C<unix> defines I<$sock_unix>, a value
+equalling C<inet >undefines I<$sock_unix>, and any other
+value croaks.
+The I<$sock_unix> boolean determines whether the next
+call to C<openlog()> or C<syslog()> will connect to the
+UNIX domain socket returned by C<_PATH_LOG> in
+I<syslog.ph> or to the INET socket returned by
+getservbyname().
+
+The default is for the INET socket to be used.
+
=item closelog
Closes the log file.
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 AUTHOR
-Tom Christiansen E<lt>F<tchrist@perl.com>E<gt> and Larry Wall E<lt>F<larry@wall.org>E<gt>
+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
$oldmask;
}
+sub setlogsock {
+ local($setsock) = shift;
+ if (lc($setsock) eq 'unix') {
+ $sock_unix = 1;
+ } elsif (lc($setsock) eq 'inet') {
+ undef($sock_unix);
+ } else {
+ croak "Invalid argument passed to setlogsock; must be 'unix' or 'inet'";
+ }
+}
+
sub syslog {
local($priority) = shift;
local($mask) = shift;
$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) {
my($host_uniq) = Sys::Hostname::hostname();
($host) = $host_uniq =~ /([A-Za-z0-9_.-]+)/; # allow FQDN (inc _)
}
- 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: $!";
+ unless ( $sock_unix ) {
+ 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 "open: $!";
+ connect(SYSLOG,$that) || croak "connect: $!";
+ }
local($old) = select(SYSLOG); $| = 1; select($old);
$connected = 1;
}