Commit fae391d1 authored by Mac Newbold's avatar Mac Newbold

Add proper subscription to only stuff I care about and proper exiting when a...

Add proper subscription to only stuff I care about and proper exiting when a signal is received (instead of a 'natural' death like exit or die).
parent 985af3c5
......@@ -32,7 +32,7 @@ use event;
use libdb;
use libtestbed;
use Getopt::Std;
use strict;
#use strict;
use English;
use POSIX qw(strftime);
......@@ -70,6 +70,17 @@ if ($opt{p}) { $port = $opt{p}; }
if ($opt{d}) { $debug = 1; } else { $debug = 0; }
if ($opt{f}) { $nofork = 1; } else { $nofork = 0; }
#
# Grab some constants into variables
#
my $TBRESET = TBDB_TBCONTROL_RESET;
my $TBRELOADDONE = TBDB_TBCONTROL_RELOADDONE;
my $TBTIMEOUT = TBDB_TBCONTROL_TIMEOUT;
my $TBNOTIMEOUT = TBDB_NO_STATE_TIMEOUT;
my $TBNODESTATE = TBDB_TBEVENT_NODESTATE;
my $TBNODEOPMODE = TBDB_TBEVENT_NODEOPMODE;
my $TBCONTROL = TBDB_TBEVENT_TBCONTROL;
#
# Background
#
......@@ -100,6 +111,8 @@ if (!$handle) { die "Unable to register with event system\n"; }
my $tuple = address_tuple_alloc();
if (!$tuple) { die "Could not allocate an address tuple\n"; }
%$tuple = ( objtype => join(",",$TBNODESTATE,$TBNODEOPMODE,$TBCONTROL) );
if (!event_subscribe($handle,\&handleEvent,$tuple)) {
die "Could not subscribe to events\n";
}
......@@ -119,20 +132,17 @@ my %triggers = getTriggers();
#
my $do_reload = 0;
#
# Grab some constants into variables
#
my $TBRESET = TBDB_TBCONTROL_RESET;
my $TBRELOADDONE = TBDB_TBCONTROL_RELOADDONE;
my $TBTIMEOUT = TBDB_TBCONTROL_TIMEOUT;
my $TBNOTIMEOUT = TBDB_NO_STATE_TIMEOUT;
#
# Make the daemon reload database state on a sighup - but I'm worried
# about what would happen if we tried to do this mid-loop. So, we'll
# just set a flag and do it when we're done with our current pass.
#
$SIG{HUP} = sub { info("Recieved a SIGHUP\n"); $do_reload = 1; };
$SIG{INT} = \&cleanup;
$SIG{QUIT} = \&cleanup;
$SIG{ABRT} = \&cleanup;
$SIG{TERM} = \&cleanup;
$SIG{KILL} = \&cleanup;
info("stated starting up\n");
......@@ -304,17 +314,17 @@ sub handleEvent($$$) {
SWITCH: for ($objtype) {
(/TBNODESTATE/) && do {
(/$TBNODESTATE/) && do {
stateTransition($objname,$eventtype);
last;
};
(/TBNODEOPMODE/) && do {
(/$TBNODEOPMODE/) && do {
opModeTransition($objname,$eventtype);
notify("Use of deprecated event TBNODEOPMODE:\n".
"$objname->$eventtype\n");
last;
};
(/TBCONTROL/) && do {
(/$TBCONTROL/) && do {
handleCtrlEvent($objname,$eventtype);
last;
};
......@@ -647,7 +657,7 @@ sub debug(@) {
sub notify($) {
my $message = shift;
if (!$debug) {
SENDMAIL('$TBOPS',"Node State Daemon Messsage",$message,'$TBOPS');
SENDMAIL($TBOPS,"Node State Daemon Messsage",$message,$TBOPS);
} else {
print "notify: Not sending mail in debug mode\n";
}
......@@ -661,16 +671,23 @@ sub info($) {
print strftime("%b %e %H:%M:%S",localtime)." stated[$$]: $message";
}
#
# This gets called if we catch a signal (TERM, etc.)
#
sub cleanup {
info("Signal received, exiting\n");
exit(0);
}
#
# This gets called if we die of 'natural causes' (exit, die, etc.)
#
END {
if ($handle) {
info("Exiting, cleaning up\n");
if (event_unregister($handle) == 0) {
die "Unable to unregister with event system\n";
}
if ($handle) {
info("Exiting, cleaning up\n");
if (event_unregister($handle) == 0) {
die "Unable to unregister with event system\n";
}
}
}
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