6 # tom christiansen <tchrist@convex.com>
7 # modified to use sockets by Larry Wall <lwall@jpl-devvax.jpl.nasa.gov>
8 # NOTE: openlog now takes three arguments, just like openlog(3)
10 # call syslog() with a string priority and a list of printf() args
13 # usage: require 'syslog.pl';
15 # then (put these all in a script to test function)
18 # do openlog($program,'cons,pid','user');
19 # do syslog('info','this is another test');
20 # do syslog('mail|warning','this is a better test: %d', time);
23 # do syslog('debug','this is the last test');
24 # do openlog("$program $$",'ndelay','user');
25 # do syslog('notice','fooprogram: this is really done');
28 # do syslog('info','problem was %m'); # %m == $! in syslog(3)
32 $host = 'localhost' unless $host; # set $syslog'host to change
36 $maskpri = &LOG_UPTO(&LOG_DEBUG);
39 ($ident, $logopt, $facility) = @_; # package vars
40 $lo_pid = $logopt =~ /\bpid\b/;
41 $lo_ndelay = $logopt =~ /\bndelay\b/;
42 $lo_cons = $logopt =~ /\bcons\b/;
43 $lo_nowait = $logopt =~ /\bnowait\b/;
44 &connect if $lo_ndelay;
48 $facility = $ident = '';
53 local($oldmask) = $maskpri;
59 local($priority) = shift;
61 local($message, $whoami);
62 local(@words, $num, $numpri, $numfac, $sum);
63 local($facility) = $facility; # may need to change temporarily.
65 die "syslog: expected both priority and mask" unless $mask && $priority;
67 @words = split(/\W+/, $priority, 2);# Allow "level" or "level|facility".
71 $num = &xlate($_); # Translate word to number.
72 if (/^kern$/ || $num < 0) {
73 die "syslog: invalid level/facility: $_\n";
75 elsif ($num <= &LOG_PRIMASK) {
76 die "syslog: too many levels given: $_\n" if defined($numpri);
78 return 0 unless &LOG_MASK($numpri) & $maskpri;
81 die "syslog: too many facilities given: $_\n" if defined($numfac);
87 die "syslog: level must be given\n" unless defined($numpri);
89 if (!defined($numfac)) { # Facility not specified in this call.
90 $facility = 'user' unless $facility;
91 $numfac = &xlate($facility);
94 &connect unless $connected;
98 if (!$ident && $mask =~ /^(\S.*):\s?(.*)/) {
104 ($whoami = getlogin) ||
105 ($whoami = getpwuid($<)) ||
106 ($whoami = 'syslog');
109 $whoami .= "[$$]" if $lo_pid;
112 $mask .= "\n" unless $mask =~ /\n$/;
113 $message = sprintf ($mask, @_);
115 $sum = $numpri + $numfac;
116 unless (send(SYSLOG,"<$sum>$whoami: $message",0)) {
119 unless ($lo_nowait) {
120 do {$died = wait;} until $died == $pid || $died < 0;
124 open(CONS,">/dev/console");
125 print CONS "<$facility.$priority>$whoami: $message\r";
126 exit if defined $pid; # if fork failed, we're parent
136 $name = "LOG_$name" unless $name =~ /^LOG_/;
137 $name = "syslog'$name";
150 ($name,$aliases,$proto) = getprotobyname('udp');
153 ($name,$aliase,$port,$proto) = getservbyname('syslog','udp');
156 if (chop($myname = `hostname`)) {
157 ($name,$aliases,$addrtype,$length,@addrs) = gethostbyname($myname);
158 die "Can't lookup $myname\n" unless $name;
159 @bytes = unpack("C4",$addrs[0]);
164 $this = pack($pat, $af_inet, 0, @bytes);
166 if ($host =~ /^\d+\./) {
167 @bytes = split(/\./,$host);
170 ($name,$aliases,$addrtype,$length,@addrs) = gethostbyname($host);
171 die "Can't lookup $host\n" unless $name;
172 @bytes = unpack("C4",$addrs[0]);
174 $that = pack($pat,$af_inet,$syslog,@bytes);
176 socket(SYSLOG,$af_inet,$datagram,$udp) || die "socket: $!\n";
177 bind(SYSLOG,$this) || die "bind: $!\n";
178 connect(SYSLOG,$that) || die "connect: $!\n";
180 local($old) = select(SYSLOG); $| = 1; select($old);