3 # Copyright (C) 2004-2007 Internet Systems Consortium, Inc. ("ISC")
4 # Copyright (C) 2001 Internet Software Consortium.
6 # Permission to use, copy, modify, and/or distribute this software for any
7 # purpose with or without fee is hereby granted, provided that the above
8 # copyright notice and this permission notice appear in all copies.
10 # THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
11 # REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
12 # AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
13 # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
14 # LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
15 # OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16 # PERFORMANCE OF THIS SOFTWARE.
18 # Id: stop.pl,v 1.12 2007/06/19 23:47:00 tbox Exp
20 # Framework for stopping test servers
21 # Based on the type of server specified, signal the server to stop, wait
22 # briefly for it to die, and then kill it if it is still alive.
23 # If a server is specified, stop it. Otherwise, stop all servers for test.
29 # [--use-rndc] test [server]
31 # test - name of the test directory
32 # server - name of the server directory
34 my $usage = "usage: $0 [--use-rndc] test-directory [server-directory]";
37 while (@ARGV && $ARGV[0] =~ /^-/) {
38 my $opt = shift @ARGV;
39 if ($opt eq '--use-rndc') {
47 my $server = $ARGV[1];
51 die "$usage\n" unless defined($test);
52 die "No test directory: \"$test\"\n" unless (-d
$test);
53 die "No server directory: \"$server\"\n" if (defined($server) && !-d
"$test/$server");
56 my $testdir = abs_path
($test);
60 # Determine which servers need to be stopped.
61 if (defined $server) {
65 opendir DIR
, $testdir or die "$testdir: $!\n";
66 my @files = sort readdir DIR
;
69 my @ns = grep /^ns[0-9]*$/, @files;
70 my @lwresd = grep /^lwresd[0-9]*$/, @files;
71 my @ans = grep /^ans[0-9]*$/, @files;
73 push @servers, @ns, @lwresd, @ans;
77 # Stop the server(s), pass 1: rndc.
79 foreach my $server (grep /^ns/, @servers) {
83 wait_for_servers
(30, grep /^ns/, @servers);
88 foreach my $server (@servers) {
89 stop_signal
($server, "TERM");
92 wait_for_servers
(60, @servers);
95 foreach my $server (@servers) {
96 stop_signal
($server, "ABRT");
99 exit($errors ?
1 : 0);
103 # Return the full path to a given server's PID file.
104 sub server_pid_file
{
108 if ($server =~ /^ns/) {
109 $pid_file = "named.pid";
110 } elsif ($server =~ /^lwresd/) {
111 $pid_file = "lwresd.pid";
112 } elsif ($server =~ /^ans/) {
113 $pid_file = "ans.pid";
115 print "I:Unknown server type $server\n";
118 $pid_file = "$testdir/$server/$pid_file";
126 my $result = open FH
, "< $pid_file";
128 print "I:$pid_file: $!\n";
138 # Stop a named process with rndc.
142 return unless ($server =~ /^ns(\d+)$/);
143 my $ip = "10.53.0.$1";
145 # Ugly, but should work.
146 system("$ENV{RNDC} -c $testdir/../common/rndc.conf -s $ip -p 9953 stop | sed 's/^/I:$server /'");
150 # Stop a server by sending a signal to it.
152 my($server, $sig) = @_;
154 my $pid_file = server_pid_file
($server);
155 return unless -f
$pid_file;
157 my $pid = read_pid
($pid_file);
158 return unless defined($pid);
160 if ($sig eq 'ABRT') {
161 print "I:$server didn't die when sent a SIGTERM\n";
165 my $result = kill $sig, $pid;
167 print "I:$server died before a SIG$sig was sent\n";
175 sub wait_for_servers
{
176 my($timeout, @servers) = @_;
178 my @pid_files = grep { defined($_) }
179 map { server_pid_file
($_) } @servers;
181 while ($timeout > 0 && @pid_files > 0) {
182 @pid_files = grep { -f
$_ } @pid_files;
183 sleep 1 if (@pid_files > 0);