summaryrefslogtreecommitdiff
path: root/regress/usr.sbin/syslogd/syslogd.pl
blob: ce66ee2bf54c6239830dd5fab53f392476f17eb0 (plain)
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
#!/usr/bin/perl
#	$OpenBSD: syslogd.pl,v 1.11 2019/09/17 22:24:08 bluhm Exp $

# Copyright (c) 2010-2014 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 Socket;
use Socket6;

use Client;
use Syslogd;
use Server;
use Syslogc;
use RSyslogd;
require 'funcs.pl';

sub usage {
	die "usage: syslogd.pl [test-args.pl]\n";
}

my $testfile;
our %args;
if (@ARGV and -f $ARGV[-1]) {
	$testfile = pop;
	do $testfile
	    or die "Do test file $testfile failed: ", $@ || $!;
}
@ARGV == 0 or usage();

create_multifile(@{$args{multifile} || []});
foreach my $name (qw(client syslogd server rsyslogd)) {
	$args{$name} or next;
	foreach my $action (qw(connect listen)) {
		my $h = $args{$name}{$action} or next;
		defined $h->{domain}
		    or die "No domain specified in $name $action";
		foreach my $k (qw(domain proto addr port)) {
			next unless defined $h->{$k};
			$args{$name}{"$action$k"} = $h->{$k};
		}
	}
}
my($s, $c, $r, $rc, @m);
$s = RSyslogd->new(
    %{$args{rsyslogd}},
    listenport          => scalar find_ports(%{$args{rsyslogd}{listen}}),
    testfile            => $testfile,
) if $args{rsyslogd}{listen} && !$args{rsyslogd}{connect};
$s ||= Server->new(
    func                => \&read_log,
    listendomain        => AF_INET,
    listenaddr          => "127.0.0.1",
    %{$args{server}},
    testfile            => $testfile,
    client              => \$c,
    syslogd             => \$r,
) unless $args{server}{noserver};
$args{syslogc} = [ $args{syslogc} ] if ref $args{syslogc} eq 'HASH';
my $i = 0;
@m = map { Syslogc->new(
    %{$_},
    testfile            => $testfile,
    ktracefile          => "syslogc-$i.ktrace",
    logfile             => "syslogc-".$i++.".log",
) } @{$args{syslogc}};
$r = Syslogd->new(
    connectaddr         => "127.0.0.1",
    connectport         => $s && $s->{listenport},
    ctlsock		=> @m && $m[0]->{ctlsock},
    %{$args{syslogd}},
    testfile            => $testfile,
    client              => \$c,
    server              => \$s,
);
$rc = RSyslogd->new(
    %{$args{rsyslogd}},
    listenport          => scalar find_ports(%{$args{rsyslogd}{listen}}),
    testfile            => $testfile,
) if $args{rsyslogd}{connect};
$c = Client->new(
    func                => \&write_log,
    connectport         => $rc && $rc->{listenport},
    %{$args{client}},
    testfile            => $testfile,
    syslogd             => \$r,
    server              => \$s,
) unless $args{client}{noclient};
($rc, $c) = ($c, $rc) if $rc;  # chain client -> rsyslogd -> syslogd

if (!$args{client}{noclient} && $c->{early}) {
	$c->run->up;
	$c->loggrep(get_firstlog(), 10)
	   or die ref($c), " no first log during early startup";
}
$r->run unless $r->{late};
$s->run->up unless $args{server}{noserver};
$r->run if $r->{late};
$r->up;
my $control = 0;
foreach (@m) {
	if ($_->{early} || $_->{stop}) {
		$_->run->up;
		$control++;
	}
}
$r->loggrep("Accepting control connection") if $control;
foreach (@m) {
	if ($_->{stop}) {
		$_->kill('STOP');
	}
}
$c->run->up if !$args{client}{noclient} && !$c->{early};
$rc->run->up if $args{rsyslogd}{connect};

$rc->down if $args{rsyslogd}{connect};
$c->down if !$args{client}{noclient} && !$c->{early};
$s->down unless $args{server}{noserver};
foreach (@m) {
	if ($_->{stop}) {
		$_->kill('CONT');
		$_->down;
	} elsif ($_->{early}) {
		$_->down;
	} else {
		$_->run->up->down;
	}
}
$r->kill_child;
$r->down;
$c->down if !$args{client}{noclient} && $c->{early};

$args{check}->({client => $c, syslogd => $r, server => $s}) if $args{check};
check_logs($c, $r, $s, \@m, %args);