43383a70d36d2c19da0354e96114817f55cae66aMichael Sawyer#!/usr/bin/perl
43383a70d36d2c19da0354e96114817f55cae66aMichael Sawyer#
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews# Copyright (C) 2000, 2001, 2004, 2007, 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/.
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews
ea94d370123a5892f6c47a97f21d1b28d44bb168Tinderbox User# $Id$
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence
43383a70d36d2c19da0354e96114817f55cae66aMichael Sawyer# A simple nanny to make sure named stays running.
43383a70d36d2c19da0354e96114817f55cae66aMichael Sawyer
43383a70d36d2c19da0354e96114817f55cae66aMichael Sawyer$pid_file_location = '/var/run/named.pid';
43383a70d36d2c19da0354e96114817f55cae66aMichael Sawyer$nameserver_location = 'localhost';
43383a70d36d2c19da0354e96114817f55cae66aMichael Sawyer$dig_program = 'dig';
43383a70d36d2c19da0354e96114817f55cae66aMichael Sawyer$named_program = 'named';
43383a70d36d2c19da0354e96114817f55cae66aMichael Sawyer
43383a70d36d2c19da0354e96114817f55cae66aMichael Sawyerfork() && exit();
43383a70d36d2c19da0354e96114817f55cae66aMichael Sawyer
43383a70d36d2c19da0354e96114817f55cae66aMichael Sawyerfor (;;) {
43383a70d36d2c19da0354e96114817f55cae66aMichael Sawyer $pid = 0;
43383a70d36d2c19da0354e96114817f55cae66aMichael Sawyer open(FILE, $pid_file_location) || goto restart;
43383a70d36d2c19da0354e96114817f55cae66aMichael Sawyer $pid = <FILE>;
43383a70d36d2c19da0354e96114817f55cae66aMichael Sawyer close(FILE);
43383a70d36d2c19da0354e96114817f55cae66aMichael Sawyer chomp($pid);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
f004babe63a6a50bef91701d6f682ab27869c854Michael Sawyer $ps_command = "ps -axw | grep named | grep $pid | grep -v grep |";
43383a70d36d2c19da0354e96114817f55cae66aMichael Sawyer # ps_command MUST end in a pipe (|) character!
43383a70d36d2c19da0354e96114817f55cae66aMichael Sawyer
43383a70d36d2c19da0354e96114817f55cae66aMichael Sawyer # Make sure there's a named on the pid we just got. Since there is no
43383a70d36d2c19da0354e96114817f55cae66aMichael Sawyer # good way to test this, use the output from ps.
43383a70d36d2c19da0354e96114817f55cae66aMichael Sawyer open(PIPE, $ps_command) || die($ps_command);
43383a70d36d2c19da0354e96114817f55cae66aMichael Sawyer $line = <PIPE>;
43383a70d36d2c19da0354e96114817f55cae66aMichael Sawyer close(PIPE);
43383a70d36d2c19da0354e96114817f55cae66aMichael Sawyer chomp($line);
43383a70d36d2c19da0354e96114817f55cae66aMichael Sawyer
43383a70d36d2c19da0354e96114817f55cae66aMichael Sawyer goto restart if ($line eq "");
43383a70d36d2c19da0354e96114817f55cae66aMichael Sawyer
43383a70d36d2c19da0354e96114817f55cae66aMichael Sawyer $dig_command = "$dig_program +short . \@$nameserver_location > /dev/null";
43383a70d36d2c19da0354e96114817f55cae66aMichael Sawyer $return = system($dig_command);
43383a70d36d2c19da0354e96114817f55cae66aMichael Sawyer goto restart if ($return == 9);
43383a70d36d2c19da0354e96114817f55cae66aMichael Sawyer
f004babe63a6a50bef91701d6f682ab27869c854Michael Sawyer sleep 30;
43383a70d36d2c19da0354e96114817f55cae66aMichael Sawyer next;
43383a70d36d2c19da0354e96114817f55cae66aMichael Sawyer
43383a70d36d2c19da0354e96114817f55cae66aMichael Sawyer restart:
43383a70d36d2c19da0354e96114817f55cae66aMichael Sawyer if ($pid != 0) {
43383a70d36d2c19da0354e96114817f55cae66aMichael Sawyer kill 15, $pid;
43383a70d36d2c19da0354e96114817f55cae66aMichael Sawyer sleep 30;
43383a70d36d2c19da0354e96114817f55cae66aMichael Sawyer }
43383a70d36d2c19da0354e96114817f55cae66aMichael Sawyer system ($named_program);
43383a70d36d2c19da0354e96114817f55cae66aMichael Sawyer sleep 120;
43383a70d36d2c19da0354e96114817f55cae66aMichael Sawyer}