Commit | Line | Data |
0f85fab0 |
1 | # |
2 | # syslog.pl |
3 | # |
395c3793 |
4 | # $Log: syslog.pl,v $ |
2b69d0c2 |
5 | # Revision 4.0.1.1 92/06/08 13:48:05 lwall |
6 | # patch20: new warning for ambiguous use of unary operators |
7 | # |
fe14fcc3 |
8 | # Revision 4.0 91/03/20 01:26:24 lwall |
9 | # 4.0 baseline. |
10 | # |
34de22dd |
11 | # Revision 3.0.1.4 90/11/10 01:41:11 lwall |
12 | # patch38: syslog.pl was referencing an absolute path |
13 | # |
14 | # Revision 3.0.1.3 90/10/15 17:42:18 lwall |
15 | # patch29: various portability fixes |
fe14fcc3 |
16 | # |
395c3793 |
17 | # Revision 3.0.1.1 90/08/09 03:57:17 lwall |
18 | # patch19: Initial revision |
19 | # |
20 | # Revision 1.2 90/06/11 18:45:30 18:45:30 root () |
21 | # - Changed 'warn' to 'mail|warning' in test call (to give example of |
22 | # facility specification, and because 'warn' didn't work on HP-UX). |
23 | # - Fixed typo in &openlog ("ncons" should be "cons"). |
24 | # - Added (package-global) $maskpri, and &setlogmask. |
25 | # - In &syslog: |
26 | # - put argument test ahead of &connect (why waste cycles?), |
27 | # - allowed facility to be specified in &syslog's first arg (temporarily |
28 | # overrides any $facility set in &openlog), just as in syslog(3C), |
29 | # - do a return 0 when bit for $numpri not set in log mask (see syslog(3C)), |
30 | # - changed $whoami code to use getlogin, getpwuid($<) and 'syslog' |
31 | # (in that order) when $ident is null, |
32 | # - made PID logging consistent with syslog(3C) and subject to $lo_pid only, |
33 | # - fixed typo in "print CONS" statement ($<facility should be <$facility). |
34 | # - changed \n to \r in print CONS (\r is useful, $message already has a \n). |
35 | # - Changed &xlate to return -1 for an unknown name, instead of croaking. |
36 | # |
37 | # |
0f85fab0 |
38 | # tom christiansen <tchrist@convex.com> |
39 | # modified to use sockets by Larry Wall <lwall@jpl-devvax.jpl.nasa.gov> |
40 | # NOTE: openlog now takes three arguments, just like openlog(3) |
41 | # |
42 | # call syslog() with a string priority and a list of printf() args |
43 | # like syslog(3) |
44 | # |
e929a76b |
45 | # usage: require 'syslog.pl'; |
0f85fab0 |
46 | # |
47 | # then (put these all in a script to test function) |
48 | # |
49 | # |
50 | # do openlog($program,'cons,pid','user'); |
51 | # do syslog('info','this is another test'); |
395c3793 |
52 | # do syslog('mail|warning','this is a better test: %d', time); |
0f85fab0 |
53 | # do closelog(); |
54 | # |
55 | # do syslog('debug','this is the last test'); |
56 | # do openlog("$program $$",'ndelay','user'); |
57 | # do syslog('notice','fooprogram: this is really done'); |
58 | # |
59 | # $! = 55; |
60 | # do syslog('info','problem was %m'); # %m == $! in syslog(3) |
61 | |
62 | package syslog; |
63 | |
64 | $host = 'localhost' unless $host; # set $syslog'host to change |
65 | |
34de22dd |
66 | require 'syslog.ph'; |
395c3793 |
67 | |
68 | $maskpri = &LOG_UPTO(&LOG_DEBUG); |
0f85fab0 |
69 | |
70 | sub main'openlog { |
71 | ($ident, $logopt, $facility) = @_; # package vars |
72 | $lo_pid = $logopt =~ /\bpid\b/; |
73 | $lo_ndelay = $logopt =~ /\bndelay\b/; |
395c3793 |
74 | $lo_cons = $logopt =~ /\bcons\b/; |
0f85fab0 |
75 | $lo_nowait = $logopt =~ /\bnowait\b/; |
76 | &connect if $lo_ndelay; |
77 | } |
78 | |
79 | sub main'closelog { |
80 | $facility = $ident = ''; |
81 | &disconnect; |
82 | } |
395c3793 |
83 | |
84 | sub main'setlogmask { |
85 | local($oldmask) = $maskpri; |
86 | $maskpri = shift; |
87 | $oldmask; |
88 | } |
0f85fab0 |
89 | |
90 | sub main'syslog { |
91 | local($priority) = shift; |
92 | local($mask) = shift; |
93 | local($message, $whoami); |
395c3793 |
94 | local(@words, $num, $numpri, $numfac, $sum); |
95 | local($facility) = $facility; # may need to change temporarily. |
0f85fab0 |
96 | |
395c3793 |
97 | die "syslog: expected both priority and mask" unless $mask && $priority; |
0f85fab0 |
98 | |
395c3793 |
99 | @words = split(/\W+/, $priority, 2);# Allow "level" or "level|facility". |
100 | undef $numpri; |
101 | undef $numfac; |
102 | foreach (@words) { |
103 | $num = &xlate($_); # Translate word to number. |
104 | if (/^kern$/ || $num < 0) { |
105 | die "syslog: invalid level/facility: $_\n"; |
106 | } |
107 | elsif ($num <= &LOG_PRIMASK) { |
108 | die "syslog: too many levels given: $_\n" if defined($numpri); |
109 | $numpri = $num; |
110 | return 0 unless &LOG_MASK($numpri) & $maskpri; |
111 | } |
112 | else { |
113 | die "syslog: too many facilities given: $_\n" if defined($numfac); |
114 | $facility = $_; |
115 | $numfac = $num; |
116 | } |
117 | } |
0f85fab0 |
118 | |
395c3793 |
119 | die "syslog: level must be given\n" unless defined($numpri); |
120 | |
121 | if (!defined($numfac)) { # Facility not specified in this call. |
122 | $facility = 'user' unless $facility; |
123 | $numfac = &xlate($facility); |
124 | } |
125 | |
126 | &connect unless $connected; |
0f85fab0 |
127 | |
395c3793 |
128 | $whoami = $ident; |
0f85fab0 |
129 | |
130 | if (!$ident && $mask =~ /^(\S.*):\s?(.*)/) { |
131 | $whoami = $1; |
132 | $mask = $2; |
133 | } |
395c3793 |
134 | |
135 | unless ($whoami) { |
136 | ($whoami = getlogin) || |
137 | ($whoami = getpwuid($<)) || |
138 | ($whoami = 'syslog'); |
139 | } |
140 | |
141 | $whoami .= "[$$]" if $lo_pid; |
0f85fab0 |
142 | |
143 | $mask =~ s/%m/$!/g; |
144 | $mask .= "\n" unless $mask =~ /\n$/; |
145 | $message = sprintf ($mask, @_); |
146 | |
395c3793 |
147 | $sum = $numpri + $numfac; |
0f85fab0 |
148 | unless (send(SYSLOG,"<$sum>$whoami: $message",0)) { |
149 | if ($lo_cons) { |
150 | if ($pid = fork) { |
151 | unless ($lo_nowait) { |
152 | do {$died = wait;} until $died == $pid || $died < 0; |
153 | } |
154 | } |
155 | else { |
156 | open(CONS,">/dev/console"); |
395c3793 |
157 | print CONS "<$facility.$priority>$whoami: $message\r"; |
0f85fab0 |
158 | exit if defined $pid; # if fork failed, we're parent |
159 | close CONS; |
160 | } |
161 | } |
162 | } |
163 | } |
164 | |
165 | sub xlate { |
166 | local($name) = @_; |
167 | $name =~ y/a-z/A-Z/; |
168 | $name = "LOG_$name" unless $name =~ /^LOG_/; |
169 | $name = "syslog'$name"; |
2b69d0c2 |
170 | eval(&$name) || -1; |
0f85fab0 |
171 | } |
172 | |
173 | sub connect { |
174 | $pat = 'S n C4 x8'; |
175 | |
176 | $af_unix = 1; |
177 | $af_inet = 2; |
178 | |
179 | $stream = 1; |
180 | $datagram = 2; |
181 | |
182 | ($name,$aliases,$proto) = getprotobyname('udp'); |
183 | $udp = $proto; |
184 | |
185 | ($name,$aliase,$port,$proto) = getservbyname('syslog','udp'); |
186 | $syslog = $port; |
187 | |
188 | if (chop($myname = `hostname`)) { |
189 | ($name,$aliases,$addrtype,$length,@addrs) = gethostbyname($myname); |
190 | die "Can't lookup $myname\n" unless $name; |
191 | @bytes = unpack("C4",$addrs[0]); |
192 | } |
193 | else { |
194 | @bytes = (0,0,0,0); |
195 | } |
196 | $this = pack($pat, $af_inet, 0, @bytes); |
197 | |
198 | if ($host =~ /^\d+\./) { |
199 | @bytes = split(/\./,$host); |
200 | } |
201 | else { |
202 | ($name,$aliases,$addrtype,$length,@addrs) = gethostbyname($host); |
203 | die "Can't lookup $host\n" unless $name; |
204 | @bytes = unpack("C4",$addrs[0]); |
205 | } |
206 | $that = pack($pat,$af_inet,$syslog,@bytes); |
207 | |
208 | socket(SYSLOG,$af_inet,$datagram,$udp) || die "socket: $!\n"; |
209 | bind(SYSLOG,$this) || die "bind: $!\n"; |
210 | connect(SYSLOG,$that) || die "connect: $!\n"; |
211 | |
212 | local($old) = select(SYSLOG); $| = 1; select($old); |
213 | $connected = 1; |
214 | } |
215 | |
216 | sub disconnect { |
217 | close SYSLOG; |
218 | $connected = 0; |
219 | } |
220 | |
221 | 1; |