Regen embed enable slab for PERL_IMPLICIT_SYS
[p5sagit/p5-mst-13.2.git] / ext / Sys / Syslog / Syslog.pm
CommitLineData
a0d0e21e 1package Sys::Syslog;
2require 5.000;
3require Exporter;
8ce86de8 4require DynaLoader;
a0d0e21e 5use Carp;
6
8ce86de8 7@ISA = qw(Exporter DynaLoader);
a0d0e21e 8@EXPORT = qw(openlog closelog setlogmask syslog);
3ffabb8c 9@EXPORT_OK = qw(setlogsock);
b903fcff 10$VERSION = '0.02';
a0d0e21e 11
37120919 12use Socket;
55497cff 13use Sys::Hostname;
37120919 14
5be1dfc7 15# adapted from syslog.pl
a0d0e21e 16#
5be1dfc7 17# Tom Christiansen <tchrist@convex.com>
a0d0e21e 18# modified to use sockets by Larry Wall <lwall@jpl-devvax.jpl.nasa.gov>
19# NOTE: openlog now takes three arguments, just like openlog(3)
3ffabb8c 20# Modified to add UNIX domain sockets by Sean Robinson <robinson_s@sc.maricopa.edu>
21# with support from Tim Bunce <Tim.Bunce@ig.co.uk> and the perl5-porters mailing list
8ce86de8 22# Modified to use an XS backend instead of syslog.ph by Tom Hughes <tom@compton.nu>
3ffabb8c 23
24# Todo: enable connect to try all three types before failing (auto setlogsock)?
5be1dfc7 25
26=head1 NAME
27
28Sys::Syslog, openlog, closelog, setlogmask, syslog - Perl interface to the UNIX syslog(3) calls
29
30=head1 SYNOPSIS
31
3ffabb8c 32 use Sys::Syslog; # all except setlogsock, or:
33 use Sys::Syslog qw(:DEFAULT setlogsock); # default set, plus setlogsock
5be1dfc7 34
3ffabb8c 35 setlogsock $sock_type;
5be1dfc7 36 openlog $ident, $logopt, $facility;
2eae817d 37 syslog $priority, $format, @args;
5be1dfc7 38 $oldmask = setlogmask $mask_priority;
39 closelog;
40
41=head1 DESCRIPTION
42
43Sys::Syslog is an interface to the UNIX C<syslog(3)> program.
44Call C<syslog()> with a string priority and a list of C<printf()> args
45just like C<syslog(3)>.
46
47Syslog provides the functions:
48
bbc7dcd2 49=over 4
5be1dfc7 50
51=item openlog $ident, $logopt, $facility
52
53I<$ident> is prepended to every message.
b12e51c8 54I<$logopt> contains zero or more of the words I<pid>, I<ndelay>, I<cons>, I<nowait>.
5be1dfc7 55I<$facility> specifies the part of the system
56
2eae817d 57=item syslog $priority, $format, @args
5be1dfc7 58
2eae817d 59If I<$priority> permits, logs I<($format, @args)>
5be1dfc7 60printed as by C<printf(3V)>, with the addition that I<%m>
61is replaced with C<"$!"> (the latest error message).
62
63=item setlogmask $mask_priority
64
65Sets log mask I<$mask_priority> and returns the old mask.
66
3ffabb8c 67=item setlogsock $sock_type (added in 5.004_02)
68
cb63fe9d 69Sets the socket type to be used for the next call to
3ffabb8c 70C<openlog()> or C<syslog()> and returns TRUE on success,
71undef on failure.
72
150b260b 73A value of 'unix' will connect to the UNIX domain socket returned by the
74C<_PATH_LOG> macro (if you system defines it) in F<syslog.h>. A value of
75'inet' will connect to an INET socket returned by getservbyname(). If
76C<_PATH_LOG> is unavailable or if getservbyname() fails, returns undef. Any
77other value croaks.
cb63fe9d 78
79The default is for the INET socket to be used.
80
5be1dfc7 81=item closelog
82
83Closes the log file.
84
85=back
86
87Note that C<openlog> now takes three arguments, just like C<openlog(3)>.
88
89=head1 EXAMPLES
90
91 openlog($program, 'cons,pid', 'user');
92 syslog('info', 'this is another test');
93 syslog('mail|warning', 'this is a better test: %d', time);
94 closelog();
95
96 syslog('debug', 'this is the last test');
cb63fe9d 97
98 setlogsock('unix');
5be1dfc7 99 openlog("$program $$", 'ndelay', 'user');
100 syslog('notice', 'fooprogram: this is really done');
101
cb63fe9d 102 setlogsock('inet');
5be1dfc7 103 $! = 55;
104 syslog('info', 'problem was %m'); # %m == $! in syslog(3)
105
5be1dfc7 106=head1 SEE ALSO
107
108L<syslog(3)>
109
110=head1 AUTHOR
111
150b260b 112Tom Christiansen E<lt>F<tchrist@perl.com>E<gt> and Larry Wall
113E<lt>F<larry@wall.org>E<gt>.
114
115UNIX domain sockets added by Sean Robinson
116E<lt>F<robinson_s@sc.maricopa.edu>E<gt> with support from Tim Bunce
117E<lt>F<Tim.Bunce@ig.co.uk>E<gt> and the perl5-porters mailing list.
118
119Dependency on F<syslog.ph> replaced with XS code by Tom Hughes
120E<lt>F<tom@compton.nu>E<gt>.
5be1dfc7 121
b903fcff 122Code for constant()s regenerated by Nicholas Clark E<lt>nick@ccl4.orgE<gt>.
123
5be1dfc7 124=cut
a0d0e21e 125
8ce86de8 126sub AUTOLOAD {
127 # This AUTOLOAD is used to 'autoload' constants from the constant()
128 # XS function.
129
130 my $constname;
131 our $AUTOLOAD;
132 ($constname = $AUTOLOAD) =~ s/.*:://;
b903fcff 133 croak "&Sys::Syslog::constant not defined" if $constname eq 'constant';
134 my ($error, $val) = constant($constname);
135 if ($error) {
136 croak $error;
8ce86de8 137 }
138 *$AUTOLOAD = sub { $val };
139 goto &$AUTOLOAD;
140}
141
142bootstrap Sys::Syslog $VERSION;
a0d0e21e 143
144$maskpri = &LOG_UPTO(&LOG_DEBUG);
145
146sub openlog {
147 ($ident, $logopt, $facility) = @_; # package vars
148 $lo_pid = $logopt =~ /\bpid\b/;
149 $lo_ndelay = $logopt =~ /\bndelay\b/;
150 $lo_cons = $logopt =~ /\bcons\b/;
151 $lo_nowait = $logopt =~ /\bnowait\b/;
a8710ca1 152 return 1 unless $lo_ndelay;
153 &connect;
a0d0e21e 154}
155
156sub closelog {
157 $facility = $ident = '';
158 &disconnect;
159}
160
161sub setlogmask {
162 local($oldmask) = $maskpri;
163 $maskpri = shift;
164 $oldmask;
165}
166
cb63fe9d 167sub setlogsock {
168 local($setsock) = shift;
3ffabb8c 169 &disconnect if $connected;
cb63fe9d 170 if (lc($setsock) eq 'unix') {
150b260b 171 if (length _PATH_LOG()) {
3ffabb8c 172 $sock_type = 1;
173 } else {
174 return undef;
175 }
cb63fe9d 176 } elsif (lc($setsock) eq 'inet') {
3ffabb8c 177 if (getservbyname('syslog','udp')) {
178 undef($sock_type);
179 } else {
180 return undef;
181 }
cb63fe9d 182 } else {
183 croak "Invalid argument passed to setlogsock; must be 'unix' or 'inet'";
184 }
f8b75b0c 185 return 1;
cb63fe9d 186}
187
a0d0e21e 188sub syslog {
189 local($priority) = shift;
190 local($mask) = shift;
191 local($message, $whoami);
192 local(@words, $num, $numpri, $numfac, $sum);
193 local($facility) = $facility; # may need to change temporarily.
194
195 croak "syslog: expected both priority and mask" unless $mask && $priority;
196
197 @words = split(/\W+/, $priority, 2);# Allow "level" or "level|facility".
198 undef $numpri;
199 undef $numfac;
200 foreach (@words) {
201 $num = &xlate($_); # Translate word to number.
202 if (/^kern$/ || $num < 0) {
203 croak "syslog: invalid level/facility: $_";
204 }
205 elsif ($num <= &LOG_PRIMASK) {
206 croak "syslog: too many levels given: $_" if defined($numpri);
207 $numpri = $num;
208 return 0 unless &LOG_MASK($numpri) & $maskpri;
209 }
210 else {
211 croak "syslog: too many facilities given: $_" if defined($numfac);
212 $facility = $_;
213 $numfac = $num;
214 }
215 }
216
217 croak "syslog: level must be given" unless defined($numpri);
218
219 if (!defined($numfac)) { # Facility not specified in this call.
220 $facility = 'user' unless $facility;
221 $numfac = &xlate($facility);
222 }
223
224 &connect unless $connected;
225
226 $whoami = $ident;
227
5dad0344 228 if (!$whoami && $mask =~ /^(\S.*?):\s?(.*)/) {
a0d0e21e 229 $whoami = $1;
230 $mask = $2;
231 }
232
233 unless ($whoami) {
234 ($whoami = getlogin) ||
235 ($whoami = getpwuid($<)) ||
236 ($whoami = 'syslog');
237 }
238
239 $whoami .= "[$$]" if $lo_pid;
240
241 $mask =~ s/%m/$!/g;
242 $mask .= "\n" unless $mask =~ /\n$/;
243 $message = sprintf ($mask, @_);
244
245 $sum = $numpri + $numfac;
cb63fe9d 246 unless (send(SYSLOG,"<$sum>$whoami: $message\0",0)) {
a0d0e21e 247 if ($lo_cons) {
248 if ($pid = fork) {
249 unless ($lo_nowait) {
cb1a09d0 250 $died = waitpid($pid, 0);
a0d0e21e 251 }
252 }
253 else {
aa667c33 254 if (open(CONS,">/dev/console")) {
255 print CONS "<$facility.$priority>$whoami: $message\r";
aa667c33 256 close CONS;
257 }
9668e9c1 258 exit if defined $pid; # if fork failed, we're parent
a0d0e21e 259 }
260 }
261 }
262}
263
264sub xlate {
265 local($name) = @_;
55497cff 266 $name = uc $name;
a0d0e21e 267 $name = "LOG_$name" unless $name =~ /^LOG_/;
748a9306 268 $name = "Sys::Syslog::$name";
2c3b42a1 269 # Can't have just eval { &$name } || -1 because some LOG_XXX may be zero.
270 my $value = eval { &$name };
271 defined $value ? $value : -1;
a0d0e21e 272}
273
274sub connect {
4fc7577b 275 unless ($host) {
276 require Sys::Hostname;
2eae817d 277 my($host_uniq) = Sys::Hostname::hostname();
3e3baf6d 278 ($host) = $host_uniq =~ /([A-Za-z0-9_.-]+)/; # allow FQDN (inc _)
4fc7577b 279 }
3ffabb8c 280 unless ( $sock_type ) {
7c40f2ff 281 my $udp = getprotobyname('udp') || croak "getprotobyname failed for udp";
282 my $syslog = getservbyname('syslog','udp') || croak "getservbyname failed";
cb63fe9d 283 my $this = sockaddr_in($syslog, INADDR_ANY);
284 my $that = sockaddr_in($syslog, inet_aton($host) || croak "Can't lookup $host");
285 socket(SYSLOG,AF_INET,SOCK_DGRAM,$udp) || croak "socket: $!";
286 connect(SYSLOG,$that) || croak "connect: $!";
287 } else {
150b260b 288 my $syslog = _PATH_LOG();
289 length($syslog) || croak "_PATH_LOG unavailable in syslog.h";
cb63fe9d 290 my $that = sockaddr_un($syslog) || croak "Can't locate $syslog";
3ffabb8c 291 socket(SYSLOG,AF_UNIX,SOCK_STREAM,0) || croak "socket: $!";
292 if (!connect(SYSLOG,$that)) {
293 socket(SYSLOG,AF_UNIX,SOCK_DGRAM,0) || croak "socket: $!";
294 connect(SYSLOG,$that) || croak "connect: $! (SOCK_DGRAM after trying SOCK_STREAM)";
295 }
cb63fe9d 296 }
a0d0e21e 297 local($old) = select(SYSLOG); $| = 1; select($old);
298 $connected = 1;
299}
300
301sub disconnect {
302 close SYSLOG;
303 $connected = 0;
304}
305
3061;