Skip to content

Commit

Permalink
signals: Add signal handling to most other processes.
Browse files Browse the repository at this point in the history
Should allow clean exits.  In practice, this causes xorp_fea
to core dump.  I plan to instrument rtrmgr to be able to start
processes under valgrind to further debug this.

Signed-off-by: Ben Greear <greearb@candelatech.com>
  • Loading branch information
greearb committed Jul 29, 2010
1 parent 628f728 commit 6dea756
Show file tree
Hide file tree
Showing 29 changed files with 125 additions and 158 deletions.
2 changes: 1 addition & 1 deletion xorp/bgp/bgp.cc
Expand Up @@ -745,7 +745,7 @@ BGPMain::main_loop()
static XorpTimer t = eventloop().
new_periodic_ms(1000, callback(check_callback_duration));
#endif
while ( run() ) {
while ( xorp_do_run ) {
#if defined(DEBUG_MAXIMUM_DELAY)
static time_t last = 0;
time_t current = time(0);
Expand Down
6 changes: 1 addition & 5 deletions xorp/bgp/bgp.hh
Expand Up @@ -545,11 +545,7 @@ public:

void main_loop();

/**
* shutdown BGP cleanly
*/
void terminate() { _exit_loop = true; }
bool run() {return !_exit_loop;}
void terminate() { xorp_do_run = 0; }

XorpFd create_listener(const Iptuple& iptuple);
LocalData *get_local_data();
Expand Down
16 changes: 3 additions & 13 deletions xorp/bgp/main.cc
Expand Up @@ -34,16 +34,6 @@
#include "xrl_target.hh"


BGPMain *bgpmain;

void
terminate_main_loop(int sig)
{
debug_msg("Signal %d\n", sig);
UNUSED(sig);
bgpmain->terminate();
}

int
main(int /*argc*/, char **argv)
{
Expand All @@ -64,14 +54,14 @@ main(int /*argc*/, char **argv)

comm_init();

setup_dflt_sighandlers();

try {
EventLoop eventloop;

// signal(SIGINT, terminate_main_loop);

BGPMain bgp(eventloop);
bgpmain = &bgp; // An external reference for the
// signal handler.

/*
** By default assume there is a rib running.
Expand All @@ -82,7 +72,7 @@ main(int /*argc*/, char **argv)
** Wait for our local configuration information and for the
** FEA and RIB to start.
*/
while (bgp.get_xrl_target()->waiting() && bgp.run()) {
while (xorp_do_run && bgp.get_xrl_target()->waiting()) {
eventloop.run();
}

Expand Down
2 changes: 0 additions & 2 deletions xorp/fea/io_ip_manager.cc
Expand Up @@ -274,8 +274,6 @@ IoIpComm::remove_filter(InputFilter* filter)
return (XORP_ERROR);
}

XLOG_ASSERT(! _io_ip_plugins.empty());

_input_filters.erase(i);
if (_input_filters.empty()) {
deallocate_io_ip_plugins();
Expand Down
8 changes: 5 additions & 3 deletions xorp/fea/xorp_fea.cc
Expand Up @@ -90,8 +90,10 @@ usage(const char *argv0, int exit_value)
}

static void
fea_main(const string& finder_hostname, uint16_t finder_port)
{
fea_main(const string& finder_hostname, uint16_t finder_port) {

setup_dflt_sighandlers();

EventLoop eventloop;
XrlFeaNode xrl_fea_node(eventloop, xrl_fea_targetname,
xrl_finder_targetname, finder_hostname,
Expand All @@ -103,7 +105,7 @@ fea_main(const string& finder_hostname, uint16_t finder_port)
//
// Main loop
//
while (! xrl_fea_node.is_shutdown_received()) {
while (xorp_do_run && !xrl_fea_node.is_shutdown_received()) {
eventloop.run();
}

Expand Down
8 changes: 5 additions & 3 deletions xorp/fib2mrib/xorp_fib2mrib.cc
Expand Up @@ -88,8 +88,10 @@ usage(const char *argv0, int exit_value)
}

static void
fib2mrib_main(const string& finder_hostname, uint16_t finder_port)
{
fib2mrib_main(const string& finder_hostname, uint16_t finder_port) {

setup_dflt_sighandlers();

//
// Init stuff
//
Expand All @@ -114,7 +116,7 @@ fib2mrib_main(const string& finder_hostname, uint16_t finder_port)
//
// Main loop
//
while (! xrl_fib2mrib_node.is_done()) {
while (xorp_do_run && !xrl_fib2mrib_node.is_done()) {
eventloop.run();
}
}
Expand Down
47 changes: 26 additions & 21 deletions xorp/libxipc/call_xrl.cc
Expand Up @@ -99,7 +99,7 @@ call_xrl(EventLoop& e, XrlRouter& router, const char* request)
done = false;
resolve_failed = true;

while (done == false && tries <= retry_count) {
while (xorp_do_run && done == false && tries <= retry_count) {
resolve_failed = false;
router.send(x, callback(&response_handler,
&done,
Expand All @@ -108,7 +108,7 @@ call_xrl(EventLoop& e, XrlRouter& router, const char* request)

bool timed_out = false;
XorpTimer timeout = e.set_flag_after_ms(wait_time, &timed_out);
while (timed_out == false && done == false) {
while (xorp_do_run && timed_out == false && done == false) {
// NB we don't test for resolve failed here because if
// resolved failed we want to wait before retrying.
e.run();
Expand All @@ -127,9 +127,11 @@ call_xrl(EventLoop& e, XrlRouter& router, const char* request)
}

if (router.connected() == false) {
XLOG_FATAL("Lost connection to finder\n");
XLOG_ERROR("Lost connection to finder\n");
xorp_do_run = 0;
break;
}
}
}//while

if (resolve_failed) {
XLOG_WARNING("request: %s resolve failed", request);
Expand Down Expand Up @@ -294,36 +296,39 @@ main(int argc, char* const argv[])
argc -= optind;
argv += optind;

setup_dflt_sighandlers();

int rv = 0;
try {
EventLoop e;
XrlStdRouter router(e, ROUTER_NAME, finder_host.c_str(), port);

router.finalize();

while (false == router.failed() && false == router.ready()) {
while (xorp_do_run && !router.failed() && !router.ready()) {
e.run();
}

if (true == router.failed()) {
if (router.failed()) {
XLOG_ERROR("Router failed to communicate with finder.\n");
exit(1);
rv = 1;
}

if (false == router.ready()) {
else if (!router.ready()) {
XLOG_ERROR("Connected to finder, but did not become ready.\n");
exit(2);
rv = 2;
}

if (fileinput) {
if (input_files(e, router, argc, argv, pponly)) {
return -1;
}
} else if (argc != 0) {
if (input_cmds(e, router, argc, argv)) {
return -1;
else {
if (fileinput) {
if (input_files(e, router, argc, argv, pponly)) {
rv = -1;
}
} else if (argc != 0) {
if (input_cmds(e, router, argc, argv)) {
rv = -1;
}
} else {
usage();
}
} else {
usage();
}
} catch(...) {
xorp_catch_standard_exceptions();
Expand All @@ -335,5 +340,5 @@ main(int argc, char* const argv[])
xlog_stop();
xlog_exit();

return 0;
return rv;
}
35 changes: 6 additions & 29 deletions xorp/libxipc/finder_main.cc
Expand Up @@ -49,8 +49,6 @@
#include "permits.hh"


static bool gbl_sig_exit = false; // Exit signal() recv

static bool
print_twirl()
{
Expand All @@ -75,35 +73,14 @@ usage()
"[-i <interface>]\n");
}

void
finder_sig_handler(int s)
{
if (s == SIGINT) {
fprintf(stderr, "SIGINT received. Exiting.\n");
#ifdef SIGHUP
} else if (s == SIGHUP) {
fprintf(stderr, "SIGHUP received. Exiting.\n");
#endif
} else if (s == SIGTERM) {
fprintf(stderr, "SIGTERM received. Exiting.\n");
} else {
fprintf(stderr, "SIGNAL (%d) received. Exiting.\n", s);
}
gbl_sig_exit = true;
}

static void
finder_main(int argc, char* const argv[])
{
bool run_verbose = false;
list<IPv4> bind_addrs;
uint16_t bind_port = FinderConstants::FINDER_DEFAULT_PORT();

#ifdef SIGHUP
signal(SIGHUP, finder_sig_handler);
#endif
signal(SIGINT, finder_sig_handler);
signal(SIGTERM, finder_sig_handler);
setup_dflt_sighandlers();

int ch;
while ((ch = getopt(argc, argv, "a:i:n:p:hv")) != -1) {
Expand Down Expand Up @@ -205,16 +182,16 @@ finder_main(int argc, char* const argv[])
if (run_verbose)
twirl = e.new_periodic_ms(250, callback(print_twirl));

while (gbl_sig_exit == false) {
while (xorp_do_run) {
e.run();
}
} catch (const InvalidPort& i) {
fprintf(stderr, "%s: a finder may already be running.\n",
i.why().c_str());
XLOG_ERROR("%s: a finder may already be running.\n",
i.why().c_str());
exit(-1);
} catch (const InvalidAddress& i) {
fprintf(stderr, "Invalid finder server adddress: %s.\n",
i.why().c_str());
XLOG_ERROR("Invalid finder server adddress: %s.\n",
i.why().c_str());
exit(-1);
} catch (...) {
xorp_catch_standard_exceptions();
Expand Down
11 changes: 10 additions & 1 deletion xorp/libxipc/xrl_pf_unix.cc
Expand Up @@ -25,6 +25,7 @@
#include "xrl_pf_unix.hh"
#include "libcomm/comm_api.h"
#include "sockutil.hh"
#include "libxorp/xlog.h"

const char* XrlPFUNIXListener::_protocol = "unix";

Expand All @@ -46,6 +47,8 @@ XrlPFUNIXListener::XrlPFUNIXListener(EventLoop& e, XrlDispatcher* xr)
_address_slash_port = path;
encode_address(_address_slash_port);

XLOG_INFO("Creating XlfPFUNIXListener: %p path: %s\n", this, path.c_str());

_eventloop.add_ioevent_cb(_sock, IOT_ACCEPT,
callback(dynamic_cast<XrlPFSTCPListener*>(this),
&XrlPFSTCPListener::connect_hook));
Expand Down Expand Up @@ -75,7 +78,13 @@ XrlPFUNIXListener::~XrlPFUNIXListener()
// sort this out.
string path = _address_slash_port;
decode_address(path);
unlink(path.c_str());
if (unlink(path.c_str()) != 0) {
XLOG_ERROR("Failed to unlink path while destructing XrlPFUNIXListener: %p -:%s:-, error: %s\n",
this, path.c_str(), strerror(errno));
}
else {
XLOG_INFO("Destructing XlfPFUNIXListener: %p unlinked path: %s\n", this, path.c_str());
}
}

const char*
Expand Down
8 changes: 7 additions & 1 deletion xorp/libxipc/xrl_router.cc
Expand Up @@ -489,7 +489,13 @@ XrlRouter::resolve_callback(const XrlError& e,
{
list<XrlRouterDispatchState*>::iterator i;
i = find(_dsl.begin(), _dsl.end(), ds);
XLOG_ASSERT(i == _dsl.begin());

// It seems that the finder_client force_failure logic can cause
// callbacks to be called out of order, so this assert fails.
// Test case is to 'killall -r xorp' when lots of things are running.
// --Ben
//XLOG_ASSERT(i == _dsl.begin());

_dsl.erase(i);

if (e == XrlError::OKAY()) {
Expand Down
2 changes: 1 addition & 1 deletion xorp/libxipc/xrl_std_router.hh
Expand Up @@ -68,7 +68,7 @@ public:
uint16_t finder_port,
bool unix_socket = UNIX_SOCKET_DEFAULT);

~XrlStdRouter();
virtual ~XrlStdRouter();

private:
void construct(bool unix_socket);
Expand Down
15 changes: 9 additions & 6 deletions xorp/libxorp/eventloop.cc
Expand Up @@ -28,6 +28,7 @@
#include "libxorp/debug.h"
#include "libxipc/finder.hh"
#include "eventloop.hh"
#include <sys/types.h>
#include <signal.h>

//
Expand All @@ -44,27 +45,29 @@ void dflt_sig_handler(int signo) {

switch (signo) {
case SIGTERM:
cerr << "Got SIGTERM, shutting down.\n";
XLOG_WARNING("Got SIGTERM, shutting down.\n");
goto do_terminate;
case SIGINT:
cerr << "Got SIGINT, shutting down.\n";
XLOG_WARNING("Got SIGINT, shutting down.\n");
goto do_terminate;
case SIGXCPU:
cerr << "Got SIGXCPU, shutting down.\n";
XLOG_WARNING("Got SIGXCPU, shutting down.\n");
goto do_terminate;
case SIGXFSZ:
cerr << "Got SIGXFSZ, shutting down.\n";
XLOG_WARNING("Got SIGXFSZ, shutting down.\n");
goto do_terminate;

default:
cerr << "WARNING: Ignoring un-handled error in dflt_sig_handler: " << signo << endl;
XLOG_WARNING("WARNING: Ignoring un-handled error in dflt_sig_handler: %i\n", signo);
return;
}//switch

do_terminate:
cerr << flush;
xorp_do_run = 0;

// Now, kick any selects that are blocking
kill(getpid(), SIGURG);

}//dflt_sig_handler


Expand Down
1 change: 1 addition & 0 deletions xorp/libxorp/eventloop.hh
Expand Up @@ -44,6 +44,7 @@
// to bail out and gracefully exit.
extern int xorp_do_run;
void setup_dflt_sighandlers();
void dflt_sig_handler(int signo);


/**
Expand Down

0 comments on commit 6dea756

Please sign in to comment.