5 # Revision 4.1 92/08/07 18:24:15 lwall
7 # Revision 4.0.1.1 92/06/08 13:48:05 lwall
8 # patch20: new warning for ambiguous use of unary operators
10 # Revision 4.0 91/03/20 01:26:24 lwall
13 # Revision 3.0.1.4 90/11/10 01:41:11 lwall
14 # patch38: syslog.pl was referencing an absolute path
16 # Revision 3.0.1.3 90/10/15 17:42:18 lwall
17 # patch29: various portability fixes
19 # Revision 3.0.1.1 90/08/09 03:57:17 lwall
20 # patch19: Initial revision
22 # Revision 1.2 90/06/11 18:45:30 18:45:30 root ()
23 # - Changed 'warn' to 'mail|warning' in test call (to give example of
24 # facility specification, and because 'warn' didn't work on HP-UX).
25 # - Fixed typo in &openlog ("ncons" should be "cons").
26 # - Added (package-global) $maskpri, and &setlogmask.
28 # - put argument test ahead of &connect (why waste cycles?),
29 # - allowed facility to be specified in &syslog's first arg (temporarily
30 # overrides any $facility set in &openlog), just as in syslog(3C),
31 # - do a return 0 when bit for $numpri not set in log mask (see syslog(3C)),
32 # - changed $whoami code to use getlogin, getpwuid($<) and 'syslog'
33 # (in that order) when $ident is null,
34 # - made PID logging consistent with syslog(3C) and subject to $lo_pid only,
35 # - fixed typo in "print CONS" statement ($<facility should be <$facility).
36 # - changed \n to \r in print CONS (\r is useful, $message already has a \n).
37 # - Changed &xlate to return -1 for an unknown name, instead of croaking.
40 # tom christiansen <tchrist@convex.com>
41 # modified to use sockets by Larry Wall <lwall@jpl-devvax.jpl.nasa.gov>
42 # NOTE: openlog now takes three arguments, just like openlog(3)
44 # call syslog() with a string priority and a list of printf() args
47 # usage: require 'syslog.pl';
49 # then (put these all in a script to test function)
52 # do openlog($program,'cons,pid','user');
53 # do syslog('info','this is another test');
54 # do syslog('mail|warning','this is a better test: %d', time);
57 # do syslog('debug','this is the last test');
58 # do openlog("$program $$",'ndelay','user');
59 # do syslog('notice','fooprogram: this is really done');
62 # do syslog('info','problem was %m'); # %m == $! in syslog(3)
66 $host = 'localhost' unless $host; # set $syslog'host to change
70 $maskpri = &LOG_UPTO(&LOG_DEBUG);
73 ($ident, $logopt, $facility) = @_; # package vars
74 $lo_pid = $logopt =~ /\bpid\b/;
75 $lo_ndelay = $logopt =~ /\bndelay\b/;
76 $lo_cons = $logopt =~ /\bcons\b/;
77 $lo_nowait = $logopt =~ /\bnowait\b/;
78 &connect if $lo_ndelay;
82 $facility = $ident = '';
87 local($oldmask) = $maskpri;
93 local($priority) = shift;
95 local($message, $whoami);
96 local(@words, $num, $numpri, $numfac, $sum);
97 local($facility) = $facility; # may need to change temporarily.
99 die "syslog: expected both priority and mask" unless $mask && $priority;
101 @words = split(/\W+/, $priority, 2);# Allow "level" or "level|facility".
105 $num = &xlate($_); # Translate word to number.
106 if (/^kern$/ || $num < 0) {
107 die "syslog: invalid level/facility: $_\n";
109 elsif ($num <= &LOG_PRIMASK) {
110 die "syslog: too many levels given: $_\n" if defined($numpri);
112 return 0 unless &LOG_MASK($numpri) & $maskpri;
115 die "syslog: too many facilities given: $_\n" if defined($numfac);
121 die "syslog: level must be given\n" unless defined($numpri);
123 if (!defined($numfac)) { # Facility not specified in this call.
124 $facility = 'user' unless $facility;
125 $numfac = &xlate($facility);
128 &connect unless $connected;
132 if (!$ident && $mask =~ /^(\S.*):\s?(.*)/) {
138 ($whoami = getlogin) ||
139 ($whoami = getpwuid($<)) ||
140 ($whoami = 'syslog');
143 $whoami .= "[$$]" if $lo_pid;
146 $mask .= "\n" unless $mask =~ /\n$/;
147 $message = sprintf ($mask, @_);
149 $sum = $numpri + $numfac;
150 unless (send(SYSLOG,"<$sum>$whoami: $message",0)) {
153 unless ($lo_nowait) {
154 do {$died = wait;} until $died == $pid || $died < 0;
158 open(CONS,">/dev/console");
159 print CONS "<$facility.$priority>$whoami: $message\r";
160 exit if defined $pid; # if fork failed, we're parent
170 $name = "LOG_$name" unless $name =~ /^LOG_/;
171 $name = "syslog'$name";
184 ($name,$aliases,$proto) = getprotobyname('udp');
187 ($name,$aliase,$port,$proto) = getservbyname('syslog','udp');
190 if (chop($myname = `hostname`)) {
191 ($name,$aliases,$addrtype,$length,@addrs) = gethostbyname($myname);
192 die "Can't lookup $myname\n" unless $name;
193 @bytes = unpack("C4",$addrs[0]);
198 $this = pack($pat, $af_inet, 0, @bytes);
200 if ($host =~ /^\d+\./) {
201 @bytes = split(/\./,$host);
204 ($name,$aliases,$addrtype,$length,@addrs) = gethostbyname($host);
205 die "Can't lookup $host\n" unless $name;
206 @bytes = unpack("C4",$addrs[0]);
208 $that = pack($pat,$af_inet,$syslog,@bytes);
210 socket(SYSLOG,$af_inet,$datagram,$udp) || die "socket: $!\n";
211 bind(SYSLOG,$this) || die "bind: $!\n";
212 connect(SYSLOG,$that) || die "connect: $!\n";
214 local($old) = select(SYSLOG); $| = 1; select($old);