1#!/usr/bin/perl
2#
3# Copyright (C) 2004, 2007  Internet Systems Consortium, Inc. ("ISC")
4# Copyright (C) 2000, 2001  Internet Software Consortium.
5#
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.
9#
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.
17
18# $Id: nanny.pl,v 1.11 2007/06/19 23:47:07 tbox Exp $
19
20# A simple nanny to make sure named stays running.
21
22$pid_file_location = '/var/run/named.pid';
23$nameserver_location = 'localhost';
24$dig_program = 'dig';
25$named_program =  'named';
26
27fork() && exit();
28
29for (;;) {
30	$pid = 0;
31	open(FILE, $pid_file_location) || goto restart;
32	$pid = <FILE>;
33	close(FILE);
34	chomp($pid);
35
36	$res = kill 0, $pid;
37
38	goto restart if ($res == 0);
39
40	$dig_command =
41	       "$dig_program +short . \@$nameserver_location > /dev/null";
42	$return = system($dig_command);
43	goto restart if ($return == 9);
44
45	sleep 30;
46	next;
47
48 restart:
49	if ($pid != 0) {
50		kill 15, $pid;
51		sleep 30;
52	}
53	system ($named_program);
54	sleep 120;
55}
56