dhcpd_wrapper.in 1.79 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
#!/usr/bin/perl -w
#
# EMULAB-COPYRIGHT
# Copyright (c) 2004 University of Utah and the Flux Group.
# All rights reserved.
#
use English;
use Getopt::Std;
use POSIX qw(setsid);

sub usage()
{
    print "Usage: dhcpd_wrapper\n";
    exit(1);
}
my $optlist  = "";
my $debug    = 0;

my $TBOPS      = "@TBOPSEMAIL@";
my $PIDFILE    = "/var/run/dhcpd_wrapper.pid";
my $LOGFILE    = "/var/log/dhcpd_wrapper.log";
my $DHCPD      = "/usr/local/sbin/dhcpd -f ";
#my $DHCPDOPTS = "-cf /usr/local/etc/dhcpd.conf.v3 fxp0";
my $DHCPDOPTS  = "";
my $childpid;

# Turn off line buffering on output
$| = 1;

# Only root.
if ($UID != 0) {
    die("*** $0:\n".
	"    Must be root to run this script!\n");
}

#
# Testbed support libs.
# 
use lib "@prefix@/lib";
use libtestbed;

# Signal handler to initiate cleanup in parent and the children.
sub Pcleanup($)
{
    my ($signame) = @_;

    $SIG{TERM} = 'IGNORE';

    if (defined($childpid)) {
	system("kill $childpid");
	waitpid($childpid, 0);
    }
    unlink $PIDFILE;
    exit(0);
}

# Daemonize;
if (!$debug && TBBackGround($LOGFILE)) {
    sleep(1);
    exit(0);
}
62 63 64 65 66 67 68 69 70

#
# Write our pid into the pid file so we can be killed later. 
#
system("echo '$PID' > $PIDFILE") == 0 or
    die("*** $0:\n".
	"    Could not create $PIDFILE!");

# Okay, cleanup function.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
$SIG{TERM} = \&Pcleanup;

# Fully disconnect from bootup. 
setsid();

# Loop forever, restarting DHCPD if it ever dies.
while (1) {
    $childpid = fork();

    die("*** $0:\n".
	"    Could not fork!\n")
	if ($childpid < 0);

    if ($childpid == 0) {
	exec("$DHCPD $DHCPDOPTS");
	die("*** $0:\n".
	    "    Could not exec $DHCPD!\n");
    }
    waitpid($childpid, 0);
    #
    # Anytime it exits, send email.
    #
    SENDMAIL($TBOPS, "DHCPD died",
	     "$DHCPD died with $?; restarting it.",
	     $TBOPS);
    sleep(1);
}
exit(0);