5 # Revision 4.0 91/03/20 01:26:24 lwall
8 # Revision 3.0.1.4 90/11/10 01:41:11 lwall
9 # patch38: syslog.pl was referencing an absolute path
11 # Revision 3.0.1.3 90/10/15 17:42:18 lwall
12 # patch29: various portability fixes
14 # Revision 3.0.1.1 90/08/09 03:57:17 lwall
15 # patch19: Initial revision
17 # Revision 1.2 90/06/11 18:45:30 18:45:30 root ()
18 # - Changed 'warn' to 'mail|warning' in test call (to give example of
19 # facility specification, and because 'warn' didn't work on HP-UX).
20 # - Fixed typo in &openlog ("ncons" should be "cons").
21 # - Added (package-global) $maskpri, and &setlogmask.
23 # - put argument test ahead of &connect (why waste cycles?),
24 # - allowed facility to be specified in &syslog's first arg (temporarily
25 # overrides any $facility set in &openlog), just as in syslog(3C),
26 # - do a return 0 when bit for $numpri not set in log mask (see syslog(3C)),
27 # - changed $whoami code to use getlogin, getpwuid($<) and 'syslog'
28 # (in that order) when $ident is null,
29 # - made PID logging consistent with syslog(3C) and subject to $lo_pid only,
30 # - fixed typo in "print CONS" statement ($<facility should be <$facility).
31 # - changed \n to \r in print CONS (\r is useful, $message already has a \n).
32 # - Changed &xlate to return -1 for an unknown name, instead of croaking.
35 # tom christiansen <tchrist@convex.com>
36 # modified to use sockets by Larry Wall <lwall@jpl-devvax.jpl.nasa.gov>
37 # NOTE: openlog now takes three arguments, just like openlog(3)
39 # call syslog() with a string priority and a list of printf() args
42 # usage: require 'syslog.pl';
44 # then (put these all in a script to test function)
47 # do openlog($program,'cons,pid','user');
48 # do syslog('info','this is another test');
49 # do syslog('mail|warning','this is a better test: %d', time);
52 # do syslog('debug','this is the last test');
53 # do openlog("$program $$",'ndelay','user');
54 # do syslog('notice','fooprogram: this is really done');
57 # do syslog('info','problem was %m'); # %m == $! in syslog(3)
61 $host = 'localhost' unless $host; # set $syslog'host to change
65 $maskpri = &LOG_UPTO(&LOG_DEBUG);
68 ($ident, $logopt, $facility) = @_; # package vars
69 $lo_pid = $logopt =~ /\bpid\b/;
70 $lo_ndelay = $logopt =~ /\bndelay\b/;
71 $lo_cons = $logopt =~ /\bcons\b/;
72 $lo_nowait = $logopt =~ /\bnowait\b/;
73 &connect if $lo_ndelay;
77 $facility = $ident = '';
82 local($oldmask) = $maskpri;
88 local($priority) = shift;
90 local($message, $whoami);
91 local(@words, $num, $numpri, $numfac, $sum);
92 local($facility) = $facility; # may need to change temporarily.
94 die "syslog: expected both priority and mask" unless $mask && $priority;
96 @words = split(/\W+/, $priority, 2);# Allow "level" or "level|facility".
100 $num = &xlate($_); # Translate word to number.
101 if (/^kern$/ || $num < 0) {
102 die "syslog: invalid level/facility: $_\n";
104 elsif ($num <= &LOG_PRIMASK) {
105 die "syslog: too many levels given: $_\n" if defined($numpri);
107 return 0 unless &LOG_MASK($numpri) & $maskpri;
110 die "syslog: too many facilities given: $_\n" if defined($numfac);
116 die "syslog: level must be given\n" unless defined($numpri);
118 if (!defined($numfac)) { # Facility not specified in this call.
119 $facility = 'user' unless $facility;
120 $numfac = &xlate($facility);
123 &connect unless $connected;
127 if (!$ident && $mask =~ /^(\S.*):\s?(.*)/) {
133 ($whoami = getlogin) ||
134 ($whoami = getpwuid($<)) ||
135 ($whoami = 'syslog');
138 $whoami .= "[$$]" if $lo_pid;
141 $mask .= "\n" unless $mask =~ /\n$/;
142 $message = sprintf ($mask, @_);
144 $sum = $numpri + $numfac;
145 unless (send(SYSLOG,"<$sum>$whoami: $message",0)) {
148 unless ($lo_nowait) {
149 do {$died = wait;} until $died == $pid || $died < 0;
153 open(CONS,">/dev/console");
154 print CONS "<$facility.$priority>$whoami: $message\r";
155 exit if defined $pid; # if fork failed, we're parent
165 $name = "LOG_$name" unless $name =~ /^LOG_/;
166 $name = "syslog'$name";
179 ($name,$aliases,$proto) = getprotobyname('udp');
182 ($name,$aliase,$port,$proto) = getservbyname('syslog','udp');
185 if (chop($myname = `hostname`)) {
186 ($name,$aliases,$addrtype,$length,@addrs) = gethostbyname($myname);
187 die "Can't lookup $myname\n" unless $name;
188 @bytes = unpack("C4",$addrs[0]);
193 $this = pack($pat, $af_inet, 0, @bytes);
195 if ($host =~ /^\d+\./) {
196 @bytes = split(/\./,$host);
199 ($name,$aliases,$addrtype,$length,@addrs) = gethostbyname($host);
200 die "Can't lookup $host\n" unless $name;
201 @bytes = unpack("C4",$addrs[0]);
203 $that = pack($pat,$af_inet,$syslog,@bytes);
205 socket(SYSLOG,$af_inet,$datagram,$udp) || die "socket: $!\n";
206 bind(SYSLOG,$this) || die "bind: $!\n";
207 connect(SYSLOG,$that) || die "connect: $!\n";
209 local($old) = select(SYSLOG); $| = 1; select($old);