Handle SIGINT (CTL+C) by gracefully shutting down

This commit is contained in:
2025-08-10 14:07:27 -04:00
parent 7579446388
commit d26b791dd2
4 changed files with 47 additions and 7 deletions
+20
View File
@@ -7,6 +7,7 @@
#include <componentThread.h>
#include <marionette/marionette.h>
#include <salmanoff.h>
#include <boost/asio/signal_set.hpp>
namespace smo {
@@ -19,8 +20,16 @@ void CrtCommandLineArgs::set(int argc, char *argv[], char *envp[])
namespace mrntt {
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)
{
// 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.initializeTls();
mrntt::exitCode = EXIT_SUCCESS;
static boost::asio::signal_set signals(self.getIoService(), SIGINT);
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();
std::cout << __func__ << ": " << PACKAGE_NAME << " " << PACKAGE_VERSION