Skip to content

Commit

Permalink
Signal Handling: Set up a signal handler for common catastrophic fail…
Browse files Browse the repository at this point in the history
…ure signals (#165)

* signal handling

---------

Co-authored-by: Charity Kathure <[email protected]>
  • Loading branch information
CharityKathure and Charity Kathure authored Jan 16, 2024
1 parent 04885c6 commit aa6ea06
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
29 changes: 29 additions & 0 deletions LogMonitor/src/LogMonitor/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,27 @@ std::unique_ptr<EventMonitor> g_eventMon(nullptr);
std::vector<std::shared_ptr<LogFileMonitor>> g_logfileMonitors;
std::unique_ptr<EtwMonitor> g_etwMon(nullptr);

/// Handle signals.
///
/// \return None
///
void signalHandler(int signum) {
logWriter.TraceError(
Utility::FormatString(L"Catastrophic failure! Signal received: %d", signum).c_str()
);

// Terminate the program
std::exit(signum);
}

/// Set up a signal handler
///
/// \return None
///
void setSignalHandler(int signum, void (*handler)(int)) {
std::signal(signum, handler);
}

BOOL WINAPI ControlHandle(_In_ DWORD dwCtrlType)
{
switch (dwCtrlType)
Expand Down Expand Up @@ -249,6 +270,14 @@ int __cdecl wmain(int argc, WCHAR *argv[])
logWriter.TraceError(L"Invalid configuration file.");
}

//
// Set up a signal handler for common catastrophic failure signals
//
setSignalHandler(SIGSEGV, signalHandler); // Segmentation fault
setSignalHandler(SIGFPE, signalHandler); // Floating point exception
setSignalHandler(SIGILL, signalHandler); // Illegal instruction
setSignalHandler(SIGABRT, signalHandler); // Abort

//
// Set the Ctrl handler function, that propagates the Ctrl events to the child process.
//
Expand Down
2 changes: 2 additions & 0 deletions LogMonitor/src/LogMonitor/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include <queue>
#include <map>
#include <stdexcept>
#include <csignal>
#include <cstdlib>
#include <Windows.h>
#include <cctype>
#include <sal.h>
Expand Down

0 comments on commit aa6ea06

Please sign in to comment.