1#!/usr/bin/perl
2#
3# Copyright (C) Internet Systems Consortium, Inc. ("ISC")
4#
5# SPDX-License-Identifier: MPL-2.0
6#
7# This Source Code Form is subject to the terms of the Mozilla Public
8# License, v. 2.0. If a copy of the MPL was not distributed with this
9# file, you can obtain one at https://mozilla.org/MPL/2.0/.
10#
11# See the COPYRIGHT file distributed with this work for additional
12# information regarding copyright ownership.
13
14# A simple nanny to make sure named stays running.
15
16$pid_file_location = '/var/run/named.pid';
17$nameserver_location = 'localhost';
18$dig_program = 'dig';
19$named_program =  'named';
20
21fork() && exit();
22
23for (;;) {
24	$pid = 0;
25	open(FILE, $pid_file_location) || goto restart;
26	$pid = <FILE>;
27	close(FILE);
28	chomp($pid);
29
30	$res = kill 0, $pid;
31
32	goto restart if ($res == 0);
33
34	$dig_command =
35	       "$dig_program +short . \@$nameserver_location > /dev/null";
36	$return = system($dig_command);
37	goto restart if ($return == 9);
38
39	sleep 30;
40	next;
41
42 restart:
43	if ($pid != 0) {
44		kill 15, $pid;
45		sleep 30;
46	}
47	system ($named_program);
48	sleep 120;
49}
50