Handle SIGINT (CTL+C) by gracefully shutting down
This commit is contained in:
@@ -5,6 +5,7 @@
|
|||||||
#include <boost/asio.hpp>
|
#include <boost/asio.hpp>
|
||||||
#include <mind.h>
|
#include <mind.h>
|
||||||
#include <componentThread.h>
|
#include <componentThread.h>
|
||||||
|
#include <marionette/marionette.h>
|
||||||
|
|
||||||
namespace smo {
|
namespace smo {
|
||||||
|
|
||||||
@@ -381,13 +382,28 @@ void ComponentThread::exceptionInd(ComponentThread& thread)
|
|||||||
std::cerr << "Mrntt: Exception occurred: in thread "
|
std::cerr << "Mrntt: Exception occurred: in thread "
|
||||||
<< thread.name << ". Killing Salmanoff." << "\n";
|
<< thread.name << ". Killing Salmanoff." << "\n";
|
||||||
|
|
||||||
smo::mind.finalizeReq(
|
// Delegate to common shutdown request
|
||||||
[]()
|
mind.finalizeReq(smo::mrntt::exitMarionetteLoop);
|
||||||
{
|
});
|
||||||
mrntt::mrntt->keepLooping = false;
|
}
|
||||||
mrntt::mrntt->getIoService().stop();
|
|
||||||
std::cout << "Mrntt: Signaled main loop to exit." << "\n";
|
void ComponentThread::userShutdownInd()
|
||||||
});
|
{
|
||||||
|
if (this->id != ComponentThread::MRNTT)
|
||||||
|
{
|
||||||
|
throw std::runtime_error(std::string(__func__)
|
||||||
|
+ ": invoked on non-mrntt thread " + this->name);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Post the user shutdown to the mrntt thread.
|
||||||
|
this->getIoService().post(
|
||||||
|
[]()
|
||||||
|
{
|
||||||
|
std::cerr << "Mrntt: User requested shutdown (SIGINT)."
|
||||||
|
<< " Killing Salmanoff." << "\n";
|
||||||
|
|
||||||
|
// Delegate to common shutdown request
|
||||||
|
mind.finalizeReq(smo::mrntt::exitMarionetteLoop);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -110,6 +110,8 @@ public:
|
|||||||
|
|
||||||
// Intentionally doesn't take a callback.
|
// Intentionally doesn't take a callback.
|
||||||
void exceptionInd(ComponentThread& thread);
|
void exceptionInd(ComponentThread& thread);
|
||||||
|
// Intentionally doesn't take a callback.
|
||||||
|
void userShutdownInd();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ThreadId id;
|
ThreadId id;
|
||||||
|
|||||||
@@ -13,6 +13,8 @@ class Marionette
|
|||||||
{
|
{
|
||||||
};
|
};
|
||||||
|
|
||||||
|
void exitMarionetteLoop();
|
||||||
|
|
||||||
} // namespace mrntt
|
} // namespace mrntt
|
||||||
|
|
||||||
struct CrtCommandLineArgs
|
struct CrtCommandLineArgs
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
#include <componentThread.h>
|
#include <componentThread.h>
|
||||||
#include <marionette/marionette.h>
|
#include <marionette/marionette.h>
|
||||||
#include <salmanoff.h>
|
#include <salmanoff.h>
|
||||||
|
#include <boost/asio/signal_set.hpp>
|
||||||
|
|
||||||
namespace smo {
|
namespace smo {
|
||||||
|
|
||||||
@@ -19,8 +20,16 @@ void CrtCommandLineArgs::set(int argc, char *argv[], char *envp[])
|
|||||||
|
|
||||||
namespace mrntt {
|
namespace mrntt {
|
||||||
std::atomic<int> exitCode;
|
std::atomic<int> exitCode;
|
||||||
|
|
||||||
|
void exitMarionetteLoop()
|
||||||
|
{
|
||||||
|
mrntt::mrntt->keepLooping = false;
|
||||||
|
mrntt::mrntt->getIoService().stop();
|
||||||
|
std::cout << "Mrntt: Signaled main loop to exit." << "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} // namespace mrntt
|
||||||
|
|
||||||
void ComponentThread::marionetteMain(ComponentThread& self)
|
void ComponentThread::marionetteMain(ComponentThread& self)
|
||||||
{
|
{
|
||||||
// Wait for CRT's main() to post us the command line args.
|
// Wait for CRT's main() to post us the command line args.
|
||||||
@@ -28,8 +37,19 @@ void ComponentThread::marionetteMain(ComponentThread& self)
|
|||||||
self.getIoService().run();
|
self.getIoService().run();
|
||||||
self.initializeTls();
|
self.initializeTls();
|
||||||
mrntt::exitCode = EXIT_SUCCESS;
|
mrntt::exitCode = EXIT_SUCCESS;
|
||||||
|
static boost::asio::signal_set signals(self.getIoService(), SIGINT);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// Register SIGINT (Ctrl+C) handler to request user shutdown
|
||||||
|
signals.async_wait(
|
||||||
|
[&self](const boost::system::error_code& ec, int /*signal*/)
|
||||||
|
{
|
||||||
|
if (ec) { return; }
|
||||||
|
// Post user shutdown indication
|
||||||
|
self.userShutdownInd();
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
OptionParser &options = OptionParser::getOptions();
|
OptionParser &options = OptionParser::getOptions();
|
||||||
|
|
||||||
std::cout << __func__ << ": " << PACKAGE_NAME << " " << PACKAGE_VERSION
|
std::cout << __func__ << ": " << PACKAGE_NAME << " " << PACKAGE_VERSION
|
||||||
|
|||||||
Reference in New Issue
Block a user