Commit 5110abc4 authored by Leigh B Stoller's avatar Leigh B Stoller

Cleanup init/remote of the pidfile in /var/run and add a sig handler

for reopening the logfile for the benefit of newsyslog.
parent ae2122f7
......@@ -2,7 +2,7 @@
#
# EMULAB-COPYRIGHT
# Copyright (c) 2005, 2006, 2007 University of Utah and the Flux Group.
# Copyright (c) 2005-2010 University of Utah and the Flux Group.
# All rights reserved.
#
......@@ -52,7 +52,6 @@ my $TBOPSPID= TBOPSPID;
my $HOME= USERROOT() . "/elabckup";
sub fatal($);
sub daemonize();
sub misconfig($$);
sub IsNodeFree($);
sub UpdateCheckupState($$$$$);
......@@ -89,33 +88,49 @@ if (defined($options{"d"})) {
$debug = $options{"d"};
}
# Go to ground.
if (! $debug) {
daemonize();
#
# We need this user for running below.
#
my $elabckup = User->Lookup("elabckup");
if (!defined($elabckup)) {
fatal("Could not lookup 'elabckup' user. Exiting ...");
}
print "Checkup daemon starting... pid $$, at ".`date`;
# Need the unix uid for the backup user.
my $user = User->Lookup('elabckup');
fatal("Could not get object for backup user")
if (!defined($user));
my $ev_uid = $user->unix_uid();
# and need the unix gid for the group.
# and the project.
my $project = Project->Lookup($TBOPSPID);
fatal("Could not get object for $TBOPSPID project")
if (!defined($project));
my $unix_gid = $project->unix_gid();
print "Experiment head: $ev_uid\n"
if ($debug);
if (CheckDaemonRunning("checkup_daemon")) {
fatal("Not starting another checkup daemon!");
}
# Go to ground.
if (! $debug) {
if (TBBackGround($logfile)) {
exit(0);
}
}
if (MarkDaemonRunning("checkup_daemon")) {
fatal("Could not mark daemon as running!");
}
#
# Setup a signal handler for newsyslog.
#
sub handler()
{
my $SAVEEUID = $EUID;
$EUID = 0;
ReOpenLog($logfile);
$EUID = $SAVEEUID;
}
$SIG{HUP} = \&handler
if (!$debug);
# Switch to the elabckup user.
$GID = $unix_gid;
$EGID = "$unix_gid";
$EUID = $UID = $ev_uid;
print "Checkup daemon starting... pid $$, at ".`date`;
if ($elabckup->FlipTo($project->unix_gid())) {
fatal("Could not flipto $elabckup");
}
my $iter = 0;
MAINLOOP: while(1) {
......@@ -367,6 +382,7 @@ MAINLOOP: while(1) {
$iter += 1;
}
MarkDaemonStopped("checkup_daemon");
sub IsNodeFree($)
{
......@@ -423,36 +439,10 @@ sub misconfig($$)
DBQueryWarn("delete from checkups where type='$ctype'");
}
#
# Become a daemon.
#
sub daemonize()
{
my $mypid = fork();
if ($mypid) {
exit(0);
}
#
# We have to disconnect from the caller by redirecting both STDIN and
# STDOUT away from the pipe. Otherwise the caller will continue to wait
# even though the parent has exited.
#
open(STDIN, "< /dev/null") or
die("opening /dev/null for STDIN: $!");
#
# Open the batch log and start writing to it.
#
open(STDERR, ">> $logfile") or die("opening $logfile for STDERR: $!");
open(STDOUT, ">> $logfile") or die("opening $logfile for STDOUT: $!");
return 0;
}
sub fatal($) {
my($mesg) = $_[0];
MarkDaemonStopped("checkup_daemon");
die("*** $0:\n".
" $mesg\n");
}
......@@ -38,7 +38,7 @@ if ($UID != 0) {
my $TB = "@prefix@";
my $TBOPS = "@TBOPSEMAIL@";
my $BOSSNODE = "@BOSSNODE@";
my $batchlog = "$TB/log/poollog";
my $logfile = "$TB/log/poollog";
my $SWAPEXP = "$TB/bin/swapexp";
# Testbed Support library
......@@ -62,6 +62,7 @@ my $EVhandle;
# Prototypes.
sub SetupEventHandler();
sub fatal($);
sub cleanup();
sub notify($);
#
......@@ -93,12 +94,28 @@ if (defined($options{"n"})) {
$impotent = $options{"n"};
}
if (!$impotent && CheckDaemonRunning("pool_daemon")) {
fatal("Not starting another pool daemon!");
}
# Go to ground.
if (! ($debug || $impotent)) {
if (TBBackGround($batchlog)) {
if (TBBackGround($logfile)) {
exit(0);
}
}
if (!$impotent && MarkDaemonRunning("pool_daemon")) {
fatal("Could not mark daemon as running!");
}
#
# Setup a signal handler for newsyslog.
#
sub handler()
{
ReOpenLog($logfile);
}
$SIG{HUP} = \&handler
if (!($debug || $impotent));
print "Pool Daemon starting... pid $$, at ".`date`;
#
......@@ -124,6 +141,8 @@ if ($experiment->state() eq EXPTSTATE_NEW()) {
$experiment->SetState(EXPTSTATE_SWAPPED());
}
if ($experiment->state() eq EXPTSTATE_SWAPPED()) {
print STDERR "Pool Daemon exiting since the experiment is swapped\n";
cleanup();
exit(0);
}
......@@ -141,6 +160,7 @@ if (!defined($elabman)) {
my $image = Image->Lookup(TBOPSPID(), "FEDORA8-OVZ-STD");
if (!defined($image)) {
print STDERR "Pool Daemon exiting since there is suitable image\n";
cleanup();
exit(0);
}
......@@ -377,6 +397,7 @@ sub fatal($)
my ($msg) = @_;
SENDMAIL($TBOPS, "Pool Daemon Died", $msg, $TBOPS);
cleanup();
die($msg);
}
......@@ -387,3 +408,9 @@ sub notify($)
print "$msg\n";
SENDMAIL($TBOPS, "Pool Daemon Message", $msg, $TBOPS);
}
sub cleanup()
{
MarkDaemonStopped("pool_daemon")
if (!$impotent);
}
......@@ -2,7 +2,7 @@
#
# EMULAB-COPYRIGHT
# Copyright (c) 2000-2007 University of Utah and the Flux Group.
# Copyright (c) 2000-2010 University of Utah and the Flux Group.
# All rights reserved.
#
......@@ -64,7 +64,6 @@ my $NODEDEAD_EID= NODEDEAD_EID;
sub fatal($);
sub notify($);
sub daemonize();
sub freefromreloading($);
my $os_load = "$TB/bin/os_load -s";
......@@ -112,10 +111,31 @@ if (defined($options{"d"})) {
$debug = $options{"d"};
}
#
# Only one please.
#
if (CheckDaemonRunning("reload_daemon")) {
fatal("Not starting another reload daemon!");
}
# Go to ground.
if (! $debug) {
daemonize();
if (TBBackGround($logfile)) {
exit(0);
}
}
if (MarkDaemonRunning("reload_daemon")) {
fatal("Could not mark daemon as running!");
}
#
# Setup a signal handler for newsyslog.
#
sub handler()
{
ReOpenLog($logfile);
}
$SIG{HUP} = \&handler
if (!$debug);
print "Reload Daemon starting... pid $$, at ".`date`;
......@@ -556,6 +576,7 @@ sub fatal($)
local($msg) = $_[0];
SENDMAIL($TBOPS, "Reload Daemon Died", $msg, $TBOPS);
MarkDaemonStopped("reload_daemon");
die($msg);
}
......@@ -566,31 +587,3 @@ sub notify($)
print "$mesg\n";
SENDMAIL($TBOPS, "Reload Daemon Message", $mesg, $TBOPS);
}
#
# Become a daemon.
#
sub daemonize()
{
my $mypid = fork();
if ($mypid) {
exit(0);
}
#
# We have to disconnect from the caller by redirecting both STDIN and
# STDOUT away from the pipe. Otherwise the caller will continue to wait
# even though the parent has exited.
#
open(STDIN, "< /dev/null") or
die("opening /dev/null for STDIN: $!");
#
# Open the batch log and start writing to it.
#
open(STDERR, ">> $logfile") or die("opening $logfile for STDERR: $!");
open(STDOUT, ">> $logfile") or die("opening $logfile for STDOUT: $!");
TBdbfork();
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment