1# 2# syslog.pl 3# 4# $Log: syslog.pl,v $ 5# Revision 1.7 2003/12/03 03:02:36 millert 6# Resolve conflicts for perl 5.8.2, remove old files, and add OpenBSD-specific scaffolding 7# 8# 9# tom christiansen <tchrist@convex.com> 10# modified to use sockets by Larry Wall <lwall@jpl-devvax.jpl.nasa.gov> 11# NOTE: openlog now takes three arguments, just like openlog(3) 12# 13# call syslog() with a string priority and a list of printf() args 14# like syslog(3) 15# 16# usage: require 'syslog.pl'; 17# 18# then (put these all in a script to test function) 19# 20# 21# do openlog($program,'cons,pid','user'); 22# do syslog('info','this is another test'); 23# do syslog('mail|warning','this is a better test: %d', time); 24# do closelog(); 25# 26# do syslog('debug','this is the last test'); 27# do openlog("$program $$",'ndelay','user'); 28# do syslog('notice','fooprogram: this is really done'); 29# 30# $! = 55; 31# do syslog('info','problem was %m'); # %m == $! in syslog(3) 32 33package syslog; 34 35use warnings::register; 36 37$host = 'localhost' unless $host; # set $syslog'host to change 38 39if ($] >= 5 && warnings::enabled()) { 40 warnings::warn("You should 'use Sys::Syslog' instead; continuing"); 41} 42 43require 'syslog.ph'; 44 45 eval 'use Socket; 1' || 46 eval { require "socket.ph" } || 47 require "sys/socket.ph"; 48 49$maskpri = &LOG_UPTO(&LOG_DEBUG); 50 51sub main'openlog { 52 ($ident, $logopt, $facility) = @_; # package vars 53 $lo_pid = $logopt =~ /\bpid\b/; 54 $lo_ndelay = $logopt =~ /\bndelay\b/; 55 $lo_cons = $logopt =~ /\bcons\b/; 56 $lo_nowait = $logopt =~ /\bnowait\b/; 57 &connect if $lo_ndelay; 58} 59 60sub main'closelog { 61 $facility = $ident = ''; 62 &disconnect; 63} 64 65sub main'setlogmask { 66 local($oldmask) = $maskpri; 67 $maskpri = shift; 68 $oldmask; 69} 70 71sub main'syslog { 72 local($priority) = shift; 73 local($mask) = shift; 74 local($message, $whoami); 75 local(@words, $num, $numpri, $numfac, $sum); 76 local($facility) = $facility; # may need to change temporarily. 77 78 die "syslog: expected both priority and mask" unless $mask && $priority; 79 80 @words = split(/\W+/, $priority, 2);# Allow "level" or "level|facility". 81 undef $numpri; 82 undef $numfac; 83 foreach (@words) { 84 $num = &xlate($_); # Translate word to number. 85 if (/^kern$/ || $num < 0) { 86 die "syslog: invalid level/facility: $_\n"; 87 } 88 elsif ($num <= &LOG_PRIMASK) { 89 die "syslog: too many levels given: $_\n" if defined($numpri); 90 $numpri = $num; 91 return 0 unless &LOG_MASK($numpri) & $maskpri; 92 } 93 else { 94 die "syslog: too many facilities given: $_\n" if defined($numfac); 95 $facility = $_; 96 $numfac = $num; 97 } 98 } 99 100 die "syslog: level must be given\n" unless defined($numpri); 101 102 if (!defined($numfac)) { # Facility not specified in this call. 103 $facility = 'user' unless $facility; 104 $numfac = &xlate($facility); 105 } 106 107 &connect unless $connected; 108 109 $whoami = $ident; 110 111 if (!$ident && $mask =~ /^(\S.*):\s?(.*)/) { 112 $whoami = $1; 113 $mask = $2; 114 } 115 116 unless ($whoami) { 117 ($whoami = getlogin) || 118 ($whoami = getpwuid($<)) || 119 ($whoami = 'syslog'); 120 } 121 122 $whoami .= "[$$]" if $lo_pid; 123 124 $mask =~ s/%m/$!/g; 125 $mask .= "\n" unless $mask =~ /\n$/; 126 $message = sprintf ($mask, @_); 127 128 $sum = $numpri + $numfac; 129 unless (send(SYSLOG,"<$sum>$whoami: $message",0)) { 130 if ($lo_cons) { 131 if ($pid = fork) { 132 unless ($lo_nowait) { 133 do {$died = wait;} until $died == $pid || $died < 0; 134 } 135 } 136 else { 137 open(CONS,">/dev/console"); 138 print CONS "<$facility.$priority>$whoami: $message\r"; 139 exit if defined $pid; # if fork failed, we're parent 140 close CONS; 141 } 142 } 143 } 144} 145 146sub xlate { 147 local($name) = @_; 148 $name = uc $name; 149 $name = "LOG_$name" unless $name =~ /^LOG_/; 150 $name = "syslog'$name"; 151 defined &$name ? &$name : -1; 152} 153 154sub connect { 155 $pat = 'S n C4 x8'; 156 157 $af_unix = &AF_UNIX; 158 $af_inet = &AF_INET; 159 160 $stream = &SOCK_STREAM; 161 $datagram = &SOCK_DGRAM; 162 163 ($name,$aliases,$proto) = getprotobyname('udp'); 164 $udp = $proto; 165 166 ($name,$aliases,$port,$proto) = getservbyname('syslog','udp'); 167 $syslog = $port; 168 169 if (chop($myname = `hostname`)) { 170 ($name,$aliases,$addrtype,$length,@addrs) = gethostbyname($myname); 171 die "Can't lookup $myname\n" unless $name; 172 @bytes = unpack("C4",$addrs[0]); 173 } 174 else { 175 @bytes = (0,0,0,0); 176 } 177 $this = pack($pat, $af_inet, 0, @bytes); 178 179 if ($host =~ /^\d+\./) { 180 @bytes = split(/\./,$host); 181 } 182 else { 183 ($name,$aliases,$addrtype,$length,@addrs) = gethostbyname($host); 184 die "Can't lookup $host\n" unless $name; 185 @bytes = unpack("C4",$addrs[0]); 186 } 187 $that = pack($pat,$af_inet,$syslog,@bytes); 188 189 socket(SYSLOG,$af_inet,$datagram,$udp) || die "socket: $!\n"; 190 bind(SYSLOG,$this) || die "bind: $!\n"; 191 connect(SYSLOG,$that) || die "connect: $!\n"; 192 193 local($old) = select(SYSLOG); $| = 1; select($old); 194 $connected = 1; 195} 196 197sub disconnect { 198 close SYSLOG; 199 $connected = 0; 200} 201 2021; 203