1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
# $OpenBSD: funcs.pl,v 1.9 2015/01/01 19:58:48 bluhm Exp $
# Copyright (c) 2010-2015 Alexander Bluhm <bluhm@openbsd.org>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
use strict;
use warnings;
use Errno;
use List::Util qw(first);
use Socket;
use Socket6;
use Sys::Syslog qw(:standard :extended :macros);
use IO::Socket;
use IO::Socket::INET6;
my $firstlog = "syslogd regress test first message";
my $testlog = "syslogd regress test log message";
my $downlog = "syslogd regress client shutdown";
sub find_ports {
my %args = @_;
my $num = delete $args{num} // 1;
my $domain = delete $args{domain} // AF_INET;
my $addr = delete $args{addr} // "127.0.0.1";
my $proto = delete $args{proto} // "udp";
my @sockets = (1..$num);
foreach my $s (@sockets) {
$s = IO::Socket::INET6->new(
Domain => $domain,
LocalAddr => $addr,
Proto => $proto,
) or die "find_ports: create and bind socket failed: $!";
}
my @ports = map { $_->sockport() } @sockets;
return wantarray ? @ports : $ports[0];
}
########################################################################
# Client funcs
########################################################################
sub write_log {
my $self = shift;
write_message($self, $testlog);
write_shutdown($self, @_);
}
sub write_between2logs {
my $self = shift;
my $func = shift;
write_message($self, $firstlog);
$func->($self, @_);
write_message($self, $testlog);
write_shutdown($self, @_);
}
sub write_message {
my $self = shift;
if (defined($self->{connectdomain})) {
print @_;
print STDERR @_, "\n";
} else {
syslog(LOG_INFO, @_);
}
}
sub write_shutdown {
my $self = shift;
setlogsock("native")
or die ref($self), " setlogsock native failed: $!";
syslog(LOG_NOTICE, $downlog);
}
sub write_unix {
my $self = shift;
my $path = shift || "/dev/log";
my $u = IO::Socket::UNIX->new(
Type => SOCK_DGRAM,
Peer => $path,
) or die ref($self), " connect to $path unix socket failed: $!";
my $msg = get_testlog(). " $path unix socket";
print $u $msg;
print STDERR $msg, "\n";
}
########################################################################
# Server funcs
########################################################################
sub read_log {
my $self = shift;
read_message($self, $downlog, @_);
}
sub read_between2logs {
my $self = shift;
my $func = shift;
unless ($self->{redo}) {
read_message($self, $firstlog, @_);
}
$func->($self, @_);
unless ($self->{redo}) {
read_message($self, $testlog, @_);
read_message($self, $downlog, @_);
}
}
sub read_message {
my $self = shift;
my $regex = shift;
local $_;
for (;;) {
# reading udp packets works only with sysread()
defined(my $n = sysread(STDIN, $_, 8194))
or die ref($self), " read log line failed: $!";
last if $n == 0;
chomp;
print STDERR ">>> $_\n";
last if /$regex/;
}
}
########################################################################
# Script funcs
########################################################################
sub get_testlog {
return $testlog;
}
sub get_between2loggrep {
return (
qr/$firstlog/ => 1,
qr/$testlog/ => 1,
);
}
sub get_downlog {
return $downlog;
}
sub check_logs {
my ($c, $r, $s, $m, %args) = @_;
return if $args{nocheck};
check_log($c, $r, $s, @$m);
check_out($r, %args);
check_stat($r, %args);
check_kdump($c, $r, $s);
if (my $file = $s->{"outfile"}) {
my $pattern = $s->{filegrep} || $testlog;
check_pattern(ref $s, $file, $pattern, \&filegrep);
}
}
sub compare($$) {
local $_ = $_[1];
if (/^\d+/) {
return $_[0] == $_;
} elsif (/^==(\d+)/) {
return $_[0] == $1;
} elsif (/^!=(\d+)/) {
return $_[0] != $1;
} elsif (/^>=(\d+)/) {
return $_[0] >= $1;
} elsif (/^<=(\d+)/) {
return $_[0] <= $1;
}
die "bad compare operator: $_";
}
sub check_pattern {
my ($name, $proc, $pattern, $func) = @_;
$pattern = [ $pattern ] unless ref($pattern) eq 'ARRAY';
foreach my $pat (@$pattern) {
if (ref($pat) eq 'HASH') {
while (my($re, $num) = each %$pat) {
my @matches = $func->($proc, $re);
compare(@matches, $num)
or die "$name matches '@matches': ",
"'$re' => $num";
}
} else {
$func->($proc, $pat)
or die "$name log missing pattern: $pat";
}
}
}
sub check_log {
foreach my $proc (@_) {
next unless $proc && !$proc->{nocheck};
my $pattern = $proc->{loggrep} || $testlog;
check_pattern(ref $proc, $proc, $pattern, \&loggrep);
}
}
sub loggrep {
my ($proc, $pattern) = @_;
return $proc->loggrep($pattern);
}
sub check_out {
my ($r, %args) = @_;
foreach my $name (qw(file pipe)) {
next if $args{$name}{nocheck};
my $file = $r->{"out$name"} or die;
my $pattern = $args{$name}{loggrep} || $testlog;
check_pattern($name, $file, $pattern, \&filegrep);
}
}
sub check_stat {
my ($r, %args) = @_;
foreach my $name (qw(fstat)) {
next unless $r && $r->{$name};
my $file = $r->{"${name}file"} or die;
my $pattern = $args{$name}{loggrep} or die;
check_pattern($name, $file, $pattern, \&filegrep);
}
}
sub filegrep {
my ($file, $pattern) = @_;
open(my $fh, '<', $file)
or die "Open file $file for reading failed: $!";
return wantarray ?
grep { /$pattern/ } <$fh> : first { /$pattern/ } <$fh>;
}
sub check_kdump {
foreach my $proc (@_) {
next unless $proc && $proc->{ktrace};
my $file = $proc->{ktracefile} or die;
my $pattern = $proc->{kdump} or die;
check_pattern(ref $proc, $file, $pattern, \&kdumpgrep);
}
}
sub kdumpgrep {
my ($file, $pattern) = @_;
my @sudo = ! -r $file && $ENV{SUDO} ? $ENV{SUDO} : ();
my @cmd = (@sudo, "kdump", "-f", $file);
open(my $fh, '-|', @cmd)
or die "Open pipe from '@cmd' failed: $!";
my @matches = grep { /$pattern/ } <$fh>;
close($fh) or die $! ?
"Close pipe from '@cmd' failed: $!" :
"Command '@cmd' failed: $?";
return wantarray ? @matches : $matches[0];
}
1;
|