43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews#!/usr/bin/perl -w
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews#
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews# Copyright (C) 2010, 2012, 2016 Internet Systems Consortium, Inc. ("ISC")
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews#
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews# This Source Code Form is subject to the terms of the Mozilla Public
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews# License, v. 2.0. If a copy of the MPL was not distributed with this
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews# file, You can obtain one at http://mozilla.org/MPL/2.0/.
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews# $Id: stop.pl,v 1.2 2010/06/17 05:38:05 marka Exp $
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews# Framework for stopping test servers
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews# Based on the type of server specified, signal the server to stop, wait
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews# briefly for it to die, and then kill it if it is still alive.
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews# If a server is specified, stop it. Otherwise, stop all servers for test.
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrewsuse strict;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrewsuse Cwd 'abs_path';
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews# Option handling
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews# [--use-rndc] test [server]
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews#
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews# test - name of the test directory
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews# server - name of the server directory
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrewsmy $usage = "usage: $0 [--use-rndc] test-directory [server-directory]";
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrewsmy $use_rndc;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrewswhile (@ARGV && $ARGV[0] =~ /^-/) {
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews my $opt = shift @ARGV;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews if ($opt eq '--use-rndc') {
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews $use_rndc = 1;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews } else {
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews die "$usage\n";
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews }
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews}
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrewsmy $test = $ARGV[0];
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrewsmy $server = $ARGV[1];
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrewsmy $errors = 0;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrewsdie "$usage\n" unless defined($test);
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrewsdie "No test directory: \"$test\"\n" unless (-d $test);
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrewsdie "No server directory: \"$server\"\n" if (defined($server) && !-d "$test/$server");
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews# Global variables
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrewsmy $testdir = abs_path($test);
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrewsmy @servers;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews# Determine which servers need to be stopped.
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrewsif (defined $server) {
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews @servers = ($server);
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews} else {
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews local *DIR;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews opendir DIR, $testdir or die "$testdir: $!\n";
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews my @files = sort readdir DIR;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews closedir DIR;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews my @ns = grep /^ns[0-9]*$/, @files;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews push @servers, @ns;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews}
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews# Stop the server(s), pass 1: rndc.
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrewsif ($use_rndc) {
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews foreach my $server (grep /^ns/, @servers) {
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews stop_rndc($server);
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews }
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews wait_for_servers(30, grep /^ns/, @servers);
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews}
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews# Pass 2: SIGTERM
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrewsforeach my $server (@servers) {
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews stop_signal($server, "TERM");
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews}
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrewswait_for_servers(60, @servers);
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews# Pass 3: SIGABRT
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrewsforeach my $server (@servers) {
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews stop_signal($server, "ABRT");
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews}
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrewsexit($errors ? 1 : 0);
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews# Subroutines
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews# Return the full path to a given server's PID file.
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrewssub server_pid_file {
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews my($server) = @_;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews my $pid_file;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews if ($server =~ /^ns/) {
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews $pid_file = "named.pid";
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews } else {
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews print "I:Unknown server type $server\n";
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews exit 1;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews }
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews $pid_file = "$testdir/$server/$pid_file";
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews}
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews# Read a PID.
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrewssub read_pid {
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews my($pid_file) = @_;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews local *FH;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews my $result = open FH, "< $pid_file";
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews if (!$result) {
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews print "I:$pid_file: $!\n";
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews unlink $pid_file;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews return;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews }
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews my $pid = <FH>;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews chomp($pid);
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews return $pid;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews}
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews# Stop a named process with rndc.
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrewssub stop_rndc {
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews my($server) = @_;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews return unless ($server =~ /^ns(\d+)$/);
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews my $ip = "10.53.0.$1";
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews # Ugly, but should work.
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews system("$ENV{RNDC} -c $testdir/../common/rndc.conf -s $ip -p 9953 stop | sed 's/^/I:$server /'");
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews return;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews}
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews# Stop a server by sending a signal to it.
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrewssub stop_signal {
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews my($server, $sig) = @_;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews my $pid_file = server_pid_file($server);
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews return unless -f $pid_file;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews my $pid = read_pid($pid_file);
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews return unless defined($pid);
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews if ($sig eq 'ABRT') {
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews print "I:$server didn't die when sent a SIGTERM\n";
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews $errors++;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews }
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews my $result = kill $sig, $pid;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews if (!$result) {
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews print "I:$server died before a SIG$sig was sent\n";
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews unlink $pid_file;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews $errors++;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews }
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews return;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews}
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrewssub wait_for_servers {
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews my($timeout, @servers) = @_;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews my @pid_files = grep { defined($_) }
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews map { server_pid_file($_) } @servers;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews while ($timeout > 0 && @pid_files > 0) {
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews @pid_files = grep { -f $_ } @pid_files;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews sleep 1 if (@pid_files > 0);
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews $timeout--;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews }
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews return;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews}