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
|
#!/usr/bin/perl -w
# Copyright (C) Internet Systems Consortium, Inc. ("ISC")
#
# SPDX-License-Identifier: MPL-2.0
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, you can obtain one at https://mozilla.org/MPL/2.0/.
#
# See the COPYRIGHT file distributed with this work for additional
# information regarding copyright ownership.
# Framework for stopping test servers
# Based on the type of server specified, signal the server to stop, wait
# briefly for it to die, and then kill it if it is still alive.
# If a server is specified, stop it. Otherwise, stop all servers for test.
use strict;
use warnings;
use Cwd ':DEFAULT', 'abs_path';
use English '-no_match_vars';
use Getopt::Long;
# Usage:
# perl stop.pl [--use-rndc [--port port]] test [server]
#
# --use-rndc Attempt to stop the server via the "rndc stop" command.
#
# --port port Only relevant if --use-rndc is specified, this sets the
# command port over which the attempt should be made. If
# not specified, port 9953 is used.
#
# test Name of the test directory.
#
# server Name of the server directory.
my $usage = "usage: $0 [--use-rndc [--halt] [--port port]] test-directory [server-directory]";
my $use_rndc = 0;
my $halt = 0;
my $rndc_port = 9953;
my $errors = 0;
GetOptions(
'use-rndc!' => \$use_rndc,
'halt!' => \$halt,
'port=i' => \$rndc_port
) or die "$usage\n";
my ( $test, $server_arg ) = @ARGV;
if (!$test) {
die "$usage\n";
}
# Global variables
my $builddir = $ENV{'builddir'};
my $srcdir = $ENV{'srcdir'};
my $testdir = "$builddir/$test";
if (! -d $testdir) {
die "No test directory: \"$testdir\"\n";
}
if ($server_arg && ! -d "$testdir/$server_arg") {
die "No server directory: \"$testdir/$server_arg\"\n";
}
my $RNDC = $ENV{RNDC};
my @ns;
my @ans;
if ($server_arg) {
if ($server_arg =~ /^ns/) {
push(@ns, $server_arg);
} elsif ($server_arg =~ /^ans/) {
push(@ans, $server_arg);
} else {
print "$0: ns or ans directory expected";
print "I:$test:failed";
}
} else {
# Determine which servers need to be stopped for this test.
opendir DIR, $testdir or die "unable to read test directory: \"$test\" ($OS_ERROR)\n";
my @files = sort readdir DIR;
closedir DIR;
@ns = grep /^ns[0-9]*$/, @files;
@ans = grep /^ans[0-9]*$/, @files;
}
# Stop the server(s), pass 1: rndc.
if ($use_rndc) {
foreach my $name(@ns) {
stop_rndc($name, $rndc_port);
}
@ns = wait_for_servers(30, @ns);
}
# Pass 2: SIGTERM
foreach my $name (@ns) {
stop_signal($name, "TERM");
}
@ns = wait_for_servers(60, @ns);
foreach my $name(@ans) {
stop_signal($name, "TERM", 1);
}
@ans = wait_for_servers(1200, @ans);
# Pass 3: SIGABRT
foreach my $name (@ns) {
print "I:$test:$name didn't die when sent a SIGTERM\n";
stop_signal($name, "ABRT");
$errors = 1;
}
foreach my $name (@ans) {
print "I:$test:$name didn't die when sent a SIGTERM\n";
stop_signal($name, "ABRT", 1);
$errors = 1;
}
exit($errors);
# Subroutines
# Return the full path to a given server's lock file.
sub server_lock_file {
my ( $server ) = @_;
return $testdir . "/" . $server . "/named.lock" if ($server =~ /^ns/);
return if ($server =~ /^ans/);
die "Unknown server type $server\n";
}
# Return the full path to a given server's PID file.
sub server_pid_file {
my ( $server ) = @_;
return $testdir . "/" . $server . "/named.pid" if ($server =~ /^ns/);
return $testdir . "/" . $server . "/ans.pid" if ($server =~ /^ans/);
die "Unknown server type $server\n";
}
# Read a PID.
sub read_pid {
my ( $pid_file ) = @_;
return unless -f $pid_file;
# we don't really care about the race condition here
my $result = open(my $fh, "<", $pid_file);
if (!defined($result)) {
print "I:$test:$pid_file: $!\n";
unlink $pid_file;
return;
}
my $pid = <$fh>;
return unless defined($pid);
chomp($pid);
return $pid;
}
# Stop a named process with rndc.
sub stop_rndc {
my ( $server, $port ) = @_;
my $n;
if ($server =~ /^ns(\d+)/) {
$n = $1;
} else {
die "unable to parse server number from name \"$server\"\n";
}
my $ip = "10.53.0.$n";
if (-e "$testdir/$server/named.ipv6-only") {
$ip = "fd92:7065:b8e:ffff::$n";
}
my $how = $halt ? "halt" : "stop";
# Ugly, but should work.
system("$RNDC -c ../common/rndc.conf -s $ip -p $port $how | sed 's/^/I:$test:$server /'");
return;
}
sub server_died {
my ( $server, $signal ) = @_;
print "I:$test:$server died before a SIG$signal was sent\n";
$errors = 1;
my $pid_file = server_pid_file($server);
unlink($pid_file);
return;
}
sub send_signal {
my ( $signal, $pid, $ans ) = @_;
if (! defined $ans) {
$ans = 0;
}
my $result = 0;
$result = kill $signal, $pid;
return $result;
}
# Stop a server by sending a signal to it.
sub stop_signal {
my ( $server, $signal, $ans ) = @_;
if (! defined $ans) {
$ans = 0;
}
my $pid_file = server_pid_file($server);
my $pid = read_pid($pid_file);
return unless defined($pid);
# Send signal to the server, and bail out if signal can't be sent
if (send_signal($signal, $pid, $ans) != 1) {
server_died($server, $signal);
return;
}
return;
}
sub pid_file_exists {
my ( $server ) = @_;
my $pid_file = server_pid_file($server);
my $pid = read_pid($pid_file);
return unless defined($pid);
# If we're here, the PID file hasn't been cleaned up yet
if (send_signal(0, $pid) == 0) {
print "I:$test:$server crashed on shutdown\n";
$errors = 1;
return;
}
return $server;
}
sub lock_file_exists {
my ( $server ) = @_;
my $lock_file = server_lock_file($server);
return unless defined($lock_file) && -f $lock_file;
return $server;
}
sub wait_for_servers {
my ( $timeout, @servers ) = @_;
while ($timeout > 0 && @servers > 0) {
sleep 1 if (@servers > 0);
@servers =
grep { defined($_) }
map { pid_file_exists($_) || lock_file_exists($_) } @servers;
$timeout--;
}
return @servers;
}
|