1# $OpenBSD: funcs.pl,v 1.37 2019/09/17 22:24:08 bluhm Exp $ 2 3# Copyright (c) 2010-2015 Alexander Bluhm <bluhm@openbsd.org> 4# 5# Permission to use, copy, modify, and distribute this software for any 6# purpose with or without fee is hereby granted, provided that the above 7# copyright notice and this permission notice appear in all copies. 8# 9# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 17use strict; 18use warnings; 19no warnings 'experimental::smartmatch'; 20use feature 'switch'; 21use Errno; 22use List::Util qw(first); 23use Socket; 24use Socket6; 25use Sys::Syslog qw(:standard :extended :macros); 26use Time::HiRes 'sleep'; 27use IO::Socket; 28use IO::Socket::INET6; 29 30my $firstlog = "syslogd regress test first message"; 31my $secondlog = "syslogd regress test second message"; 32my $thirdlog = "syslogd regress test third message"; 33my $testlog = "syslogd regress test log message"; 34my $downlog = "syslogd regress client shutdown"; 35my $charlog = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; 36 37sub find_ports { 38 my %args = @_; 39 my $num = delete $args{num} // 1; 40 my $domain = delete $args{domain} // AF_INET; 41 my $addr = delete $args{addr} // "127.0.0.1"; 42 my $proto = delete $args{proto} // "udp"; 43 $proto = "tcp" if $proto eq "tls"; 44 45 my @sockets = (1..$num); 46 foreach my $s (@sockets) { 47 $s = IO::Socket::INET6->new( 48 Domain => $domain, 49 LocalAddr => $addr, 50 Proto => $proto, 51 ) or die "find_ports: create and bind socket failed: $!"; 52 } 53 my @ports = map { $_->sockport() } @sockets; 54 55 return wantarray ? @ports : $ports[0]; 56} 57 58######################################################################## 59# Client funcs 60######################################################################## 61 62sub write_log { 63 my $self = shift; 64 65 write_message($self, $testlog); 66 IO::Handle::flush(\*STDOUT); 67 ${$self->{syslogd}}->loggrep($testlog, 2); 68 write_shutdown($self); 69} 70 71sub write_between2logs { 72 my $self = shift; 73 my $func = shift; 74 75 write_message($self, $firstlog); 76 $func->($self, @_); 77 write_message($self, $testlog); 78 IO::Handle::flush(\*STDOUT); 79 ${$self->{syslogd}}->loggrep($testlog, 2); 80 write_shutdown($self); 81} 82 83sub write_message { 84 my $self = shift; 85 86 if (defined($self->{connectdomain})) { 87 my $msg = join("", @_); 88 if ($self->{connectdomain} eq "sendsyslog") { 89 my $flags = $self->{connect}{flags} || 0; 90 sendsyslog($msg, $flags) 91 or die ref($self), " sendsyslog failed: $!"; 92 } elsif ($self->{connectproto} eq "udp") { 93 # writing UDP packets works only with syswrite() 94 defined(my $n = syswrite(STDOUT, $msg)) 95 or die ref($self), " write log line failed: $!"; 96 $n == length($msg) 97 or die ref($self), " short UDP write"; 98 } else { 99 print $msg; 100 print "\n" if $self->{connectproto} =~ /^(tcp|tls)$/; 101 } 102 print STDERR "<<< $msg\n"; 103 } else { 104 syslog(LOG_INFO, @_); 105 } 106} 107 108sub sendsyslog { 109 my $msg = shift; 110 my $flags = shift; 111 require 'sys/syscall.ph'; 112 return syscall(&SYS_sendsyslog, $msg, length($msg), $flags) != -1; 113} 114 115sub write_shutdown { 116 my $self = shift; 117 118 setlogsock("native") 119 or die ref($self), " setlogsock native failed: $!"; 120 syslog(LOG_NOTICE, $downlog); 121} 122 123sub write_lines { 124 my $self = shift; 125 my ($lines, $length) = @_; 126 127 foreach (1..$lines) { 128 write_chars($self, $length, " $_"); 129 } 130} 131 132sub write_lengths { 133 my $self = shift; 134 my ($lengths, $tail) = ref $_[0] ? @_ : [@_]; 135 136 write_chars($self, $lengths, $tail); 137} 138 139sub generate_chars { 140 my ($len) = @_; 141 142 my $msg = ""; 143 my $char = '0'; 144 for (my $i = 0; $i < $len; $i++) { 145 $msg .= $char; 146 given ($char) { 147 when(/9/) { $char = 'A' } 148 when(/Z/) { $char = 'a' } 149 when(/z/) { $char = '0' } 150 default { $char++ } 151 } 152 } 153 return $msg; 154} 155 156sub write_chars { 157 my $self = shift; 158 my ($length, $tail) = @_; 159 160 foreach my $len (ref $length ? @$length : $length) { 161 my $t = $tail // ""; 162 substr($t, 0, length($t) - $len, "") 163 if length($t) && length($t) > $len; 164 my $msg = generate_chars($len - length($t)); 165 $msg .= $t if length($t); 166 write_message($self, $msg); 167 # if client is sending too fast, syslogd will not see everything 168 sleep .01; 169 } 170} 171 172sub write_unix { 173 my $self = shift; 174 my $path = shift || "/dev/log"; 175 my $id = shift // $path; 176 177 my $u = IO::Socket::UNIX->new( 178 Type => SOCK_DGRAM, 179 Peer => $path, 180 ) or die ref($self), " connect to $path unix socket failed: $!"; 181 my $msg = "id $id unix socket: $testlog"; 182 print $u $msg; 183 print STDERR "<<< $msg\n"; 184} 185 186sub write_tcp { 187 my $self = shift; 188 my $fh = shift || \*STDOUT; 189 my $id = shift // $fh; 190 191 my $msg = "id $id tcp socket: $testlog"; 192 print $fh "$msg\n"; 193 print STDERR "<<< $msg\n"; 194} 195 196sub redo_connect { 197 my $self = shift; 198 my $func = shift; 199 200 $func->($self, @_); 201 if ($self->{cs}) { 202 # wait for possible icmp errors, port is open 203 sleep .1; 204 close(delete $self->{cs}) 205 or die ref($self), " close failed: $!"; 206 } 207 if (my $redo = shift @{$self->{redo}}) { 208 if (my $connect = $redo->{connect}) { 209 delete $self->{logsock}; 210 $self->{connectdomain} = $connect->{domain}; 211 $self->{connectaddr} = $connect->{addr}; 212 $self->{connectproto} = $connect->{proto}; 213 $self->{connectport} = $connect->{port}; 214 } elsif (my $logsock = $redo->{logsock}) { 215 delete $self->{connectdomain}; 216 delete $self->{connectaddr}; 217 delete $self->{connectproto}; 218 delete $self->{connectport}; 219 $self->{logsock} = $logsock; 220 } else { 221 die ref($self), " no connect or logsock in redo"; 222 } 223 } else { 224 delete $self->{connectdomain}; 225 delete $self->{connectaddr}; 226 delete $self->{connectproto}; 227 delete $self->{connectport}; 228 $self->{logsock} = { type => "native" }; 229 setlogsock($self->{logsock}) 230 or die ref($self), " setlogsock failed: $!"; 231 sleep .1; 232 write_log($self); 233 undef $self->{redo}; 234 } 235} 236 237######################################################################## 238# Server funcs 239######################################################################## 240 241sub read_log { 242 my $self = shift; 243 244 read_message($self, $downlog); 245} 246 247sub read_between2logs { 248 my $self = shift; 249 my $func = shift; 250 251 read_message($self, $firstlog); 252 $func->($self, @_); 253 read_message($self, $testlog); 254 read_message($self, $downlog); 255} 256 257sub accept_between2logs { 258 my $self = shift; 259 my $func = shift; 260 261 unless ($self->{redo}) { 262 read_message($self, $firstlog); 263 $func->($self, @_); 264 $self->{redo} = 1; 265 } else { 266 $self->{redo} = 0; 267 read_message($self, $testlog); 268 read_message($self, $downlog); 269 } 270} 271 272sub read_message { 273 my $self = shift; 274 my $regex = shift; 275 276 local $_; 277 for (;;) { 278 if ($self->{listenproto} eq "udp") { 279 # reading UDP packets works only with sysread() 280 defined(my $n = sysread(STDIN, $_, 8194)) 281 or die ref($self), " read log line failed: $!"; 282 last if $n == 0; 283 } else { 284 defined($_ = <STDIN>) 285 or last; 286 } 287 chomp; 288 print STDERR ">>> $_\n"; 289 last if /$regex/; 290 } 291} 292 293######################################################################## 294# Script funcs 295######################################################################## 296 297sub get_testlog { 298 return $testlog; 299} 300 301sub get_testgrep { 302 return qr/$testlog\r*$/; 303} 304 305sub get_firstlog { 306 return $firstlog; 307} 308 309sub get_secondlog { 310 return $secondlog; 311} 312 313sub get_thirdlog { 314 return $thirdlog; 315} 316 317sub get_charlog { 318 # add a space so that we match at the beginning of the message 319 return " $charlog"; 320} 321 322sub get_between2loggrep { 323 return ( 324 qr/$firstlog/ => 1, 325 qr/$testlog/ => 1, 326 ); 327} 328 329sub get_downlog { 330 return $downlog; 331} 332 333sub selector2config { 334 my %s2m = @_; 335 my $conf = ""; 336 my $i = 0; 337 foreach my $sel (sort keys %s2m) { 338 $conf .= "$sel\t\$objdir/file-$i.log\n"; 339 $i++; 340 } 341 return $conf; 342} 343 344sub selector2loggrep { 345 my %s2m = @_; 346 my %allmsg; 347 @allmsg{map { @$_} values %s2m} = (); 348 my @loggrep; 349 foreach my $sel (sort keys %s2m) { 350 my @m = @{$s2m{$sel}}; 351 my %msg; 352 $msg{$_}++ foreach (@m); 353 my %nomsg = %allmsg; 354 delete @nomsg{@m}; 355 push @loggrep, { 356 (map { qr/: $_$/ => $msg{$_} } sort keys %msg), 357 (map { qr/: $_$/ => 0 } sort keys %nomsg), 358 }; 359 } 360 return @loggrep; 361} 362 363sub check_logs { 364 my ($c, $r, $s, $m, %args) = @_; 365 366 return if $args{nocheck}; 367 368 check_log($c, $r, $s, @$m); 369 check_out($r, %args); 370 check_fstat($c, $r, $s); 371 check_ktrace($c, $r, $s); 372 if (my $file = $s->{"outfile"}) { 373 my $pattern = $s->{filegrep} || get_testgrep(); 374 check_pattern(ref $s, $file, $pattern, \&filegrep); 375 } 376 check_multifile(@{$args{multifile} || []}); 377} 378 379sub compare($$) { 380 local $_ = $_[1]; 381 if (/^\d+/) { 382 return $_[0] == $_; 383 } elsif (/^==(\d+)/) { 384 return $_[0] == $1; 385 } elsif (/^!=(\d+)/) { 386 return $_[0] != $1; 387 } elsif (/^>=(\d+)/) { 388 return $_[0] >= $1; 389 } elsif (/^<=(\d+)/) { 390 return $_[0] <= $1; 391 } elsif (/^~(\d+)/) { 392 return $1 * 0.8 <= $_[0] && $_[0] <= $1 * 1.2; 393 } 394 die "bad compare operator: $_"; 395} 396 397sub check_pattern { 398 my ($name, $proc, $pattern, $func) = @_; 399 400 $pattern = [ $pattern ] unless ref($pattern) eq 'ARRAY'; 401 foreach my $pat (@$pattern) { 402 if (ref($pat) eq 'HASH') { 403 foreach my $re (sort keys %$pat) { 404 my $num = $pat->{$re}; 405 my @matches = $func->($proc, $re); 406 compare(@matches, $num) 407 or die "$name matches '@matches': ", 408 "'$re' => $num"; 409 } 410 } else { 411 $func->($proc, $pat) 412 or die "$name log missing pattern: $pat"; 413 } 414 } 415} 416 417sub check_log { 418 foreach my $proc (@_) { 419 next unless $proc && !$proc->{nocheck}; 420 my $pattern = $proc->{loggrep} || get_testgrep(); 421 check_pattern(ref $proc, $proc, $pattern, \&loggrep); 422 } 423} 424 425sub loggrep { 426 my ($proc, $pattern) = @_; 427 428 return $proc->loggrep($pattern); 429} 430 431sub check_out { 432 my ($r, %args) = @_; 433 434 unless ($args{pipe}{nocheck}) { 435 $r->loggrep("bytes transferred", 1) or sleep 1; 436 } 437 foreach my $dev (qw(console user)) { 438 $args{$dev}{nocheck} ||= $args{tty}{nocheck}; 439 $args{$dev}{loggrep} ||= $args{tty}{loggrep}; 440 next if $args{$dev}{nocheck}; 441 my $ctl = $r->{"ctl$dev"}; 442 close($ctl); 443 my $file = $r->{"out$dev"}; 444 open(my $fh, '<', $file) 445 or die "Open file $file for reading failed: $!"; 446 grep { /^logout/ or /^console .* off/ } <$fh> or sleep 1; 447 close($fh); 448 } 449 450 foreach my $name (qw(file pipe console user)) { 451 next if $args{$name}{nocheck}; 452 my $file = $r->{"out$name"} or die; 453 my $pattern = $args{$name}{loggrep} || get_testgrep(); 454 check_pattern($name, $file, $pattern, \&filegrep); 455 } 456} 457 458sub check_fstat { 459 foreach my $proc (@_) { 460 my $pattern = $proc && $proc->{fstat} or next; 461 my $file = $proc->{fstatfile} or die; 462 check_pattern("fstat", $file, $pattern, \&filegrep); 463 } 464} 465 466sub filegrep { 467 my ($file, $pattern) = @_; 468 469 open(my $fh, '<', $file) 470 or die "Open file $file for reading failed: $!"; 471 return wantarray ? 472 grep { /$pattern/ } <$fh> : first { /$pattern/ } <$fh>; 473} 474 475sub check_ktrace { 476 foreach my $proc (@_) { 477 my $pattern = $proc && $proc->{ktrace} or next; 478 my $file = $proc->{ktracefile} or die; 479 check_pattern("ktrace", $file, $pattern, \&kdumpgrep); 480 } 481} 482 483sub kdumpgrep { 484 my ($file, $pattern) = @_; 485 486 my @sudo = ! -r $file && $ENV{SUDO} ? $ENV{SUDO} : (); 487 my @cmd = (@sudo, "kdump", "-f", $file); 488 open(my $fh, '-|', @cmd) 489 or die "Open pipe from '@cmd' failed: $!"; 490 my @matches = grep { /$pattern/ } <$fh>; 491 close($fh) or die $! ? 492 "Close pipe from '@cmd' failed: $!" : 493 "Command '@cmd' failed: $?"; 494 return wantarray ? @matches : $matches[0]; 495} 496 497sub create_multifile { 498 for (my $i = 0; $i < @_; $i++) { 499 my $file = "file-$i.log"; 500 open(my $fh, '>', $file) 501 or die "Create $file failed: $!"; 502 } 503} 504 505sub check_multifile { 506 for (my $i = 0; $i < @_; $i++) { 507 my $file = "file-$i.log"; 508 my $pattern = $_[$i]{loggrep} or die; 509 check_pattern("multifile $i", $file, $pattern, \&filegrep); 510 } 511} 512 5131; 514