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