daemon_wrapper.in 5.14 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2004, 2005, 2006, 2016 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
23 24 25 26 27 28 29
#
use English;
use Getopt::Std;
use POSIX qw(setsid);

sub usage()
{
30
    print STDERR "Usage: daemon_wrapper [-i interval] [-p pidfile] [-l logfile] [-n name] command args ...\n";
31 32
    exit(1);
}
33
my $optlist  = "di:p:l:n:";
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 62 63 64 65 66 67 68 69 70 71 72
my $debug    = 0;
my $mininterval = 1;
my $maxinterval = (24 * 60 * 60);
my $interval = $mininterval;

my $TBOPS      = "@TBOPSEMAIL@";
my $pidfile;
my $logfile;
my $daemon;
my $cmd;
my $cmdargs;
my $childpid;

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

#
# Parse arguments
#
my %options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"d"})) {
    $debug = 1;
}
if (defined($options{"i"})) {
    $interval = int($options{"i"});
    if ($interval < $mininterval || $interval > $maxinterval) {
	usage();
    }
    $mininterval = $interval;
}
if (defined($options{"p"})) {
    $pidfile = $options{"p"};
}
if (defined($options{"l"})) {
    $logfile = $options{"l"};
}
73 74 75 76 77 78 79 80 81
if (defined($options{"n"})) {
    if ($options{"n"} =~ /^([-\w]+)$/) {
	$daemon = $1;
    } else {
	print STDERR "Bogus characters in name string\n";
	exit(1);
    }
    
}
82 83 84 85
if (@ARGV < 1) {
    usage();
}
$cmd = shift;
86 87 88 89 90 91
if (!defined($daemon)) {
    if ($cmd =~ /.*\/([-\w]+)$/) {
	$daemon = $1;
    } else {
	$daemon = "pid$$";
    }
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
}
$cmdargs = join(' ', @ARGV);

$pidfile = "/var/run/${daemon}_wrapper.pid"
    if (!defined($pidfile));
$logfile = "/var/log/${daemon}_wrapper.log"
    if (!defined($logfile));

# 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;
    print TBTimeStampWithDate(),
          " [$PID]: wrapper exiting\n";
    exit(0);
}
128 129 130 131 132
# Signal handler for newsyslog.
sub Loghandler()
{
    ReOpenLog($logfile);
}
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148

# Daemonize;
if (!$debug && TBBackGround($logfile)) {
    sleep(1);
    exit(0);
}

#
# 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.
$SIG{TERM} = \&Pcleanup;
149 150 151
# And reopen logfile for newsyslog.
$SIG{HUP} = \&Loghandler
    if (! $debug);
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238

# Fully disconnect from bootup. 
setsid();

print TBTimeStampWithDate(),
      " [$PID]: wrapper starting '$cmd",
      $cmdargs ? " $cmdargs" : "", "'\n";

# Loop forever, restarting the daemon if it ever dies.
while (1) {
    my $runtime = time();

    $childpid = fork();

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

    if ($childpid == 0) {
	if (!exec("$cmd $cmdargs")) {
	    print STDERR "*** $0:\n".
		         "    Could not exec $cmd!\n";
	    exit(1);
	}
    }
    print TBTimeStampWithDate(),
          " [$PID]: $daemon ($childpid) started\n";

    waitpid($childpid, 0);
    my $exitstat = $?;
    my $ecode = $exitstat >> 8;
    my $esig = $exitstat & 0x3F;
    my $lpid = $childpid;
    undef $childpid;

    my $msg;
    if ($esig != 0) {
	$msg = "killed by signal $esig";
    } else {
	$msg = "died with exit code $ecode";
    }

    $runtime = time() - $runtime;
    print TBTimeStampWithDate(),
          " [$PID]: $daemon ($lpid) $msg ",
          "after $runtime seconds, ";

    if ($ecode == 1) {
	print "not restarting!\n";
	SENDMAIL($TBOPS, "$daemon died",
		 "$daemon died with exit code 1\n".
		 "Not restarting it; might be a configuration error",
		 $TBOPS);
	unlink $pidfile;
	die("*** $0:\n".
	    "    Could not start $daemon! Might be a configuration error.\n");
    }
    #
    # Anytime it exits, send email (unless TERM).
    #
    if ($exitstat != 15) {
	SENDMAIL($TBOPS, "$daemon died",
		 "$daemon $msg after $runtime seconds; restarting it.",
		 $TBOPS);
    }

    #
    # If the process ran for less than 5 seconds, there may be a problem
    # with daemon startup.  To avoid spamming testbed-ops for a process
    # that will never start, back off the checks exponentially up to some
    # maximum interval.
    #
    if ($runtime < 5) {
	$interval *= 2;
	if ($interval > $maxinterval) {
	    $interval = $maxinterval;
	}
    } else {
	$interval = $mininterval;
    }

    print "restarting in $interval seconds\n";

    sleep($interval);
}
exit(0);