Skip to content

Commit

Permalink
tg_cpphost: Sperate exithandlers to one lib
Browse files Browse the repository at this point in the history
  • Loading branch information
Royna2544 committed Oct 17, 2023
1 parent 36372a9 commit c802661
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 30 deletions.
5 changes: 4 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@ set(LIBUTILS_SRC_LIST src/utils/libutils.cpp)

# System specific
if (WIN32)
set(SRC_LIST src/windows/sighandler.c
set(SRC_LIST src/exithandlers/handler_windows.cpp
${SRC_LIST})

set(LIBUTILS_SRC_LIST src/popen_wdt/popen_wdt_windows.c
src/utils/libutils_windows.cpp
${LIBUTILS_SRC_LIST})
else()
set(SRC_LIST src/exithandlers/handler_posix.cpp
${SRC_LIST})

set(LIBUTILS_SRC_LIST src/popen_wdt/popen_wdt_posix.c
src/utils/libutils_posix.cpp
${LIBUTILS_SRC_LIST})
Expand Down
5 changes: 5 additions & 0 deletions src/exithandlers/handler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

using exit_handler_t = void(*)(int);

void installExitHandler(exit_handler_t fn);
13 changes: 13 additions & 0 deletions src/exithandlers/handler_posix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "handler.h"
#include <csignal>
#include <cstdlib>

static exit_handler_t fn;

void installExitHandler(exit_handler_t fn_) {
fn = fn_;
std::signal(SIGINT, fn);
std::signal(SIGTERM, fn);
static auto fnV = []() { fn(0); };
std::atexit(fnV);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "handler.h"

#include <windows.h>

static void (*cleanupFn)() = NULL;
static exit_handler_t fn;

static BOOL WINAPI CtrlHandler(DWORD fdwCtrlType) {
switch (fdwCtrlType) {
Expand All @@ -9,16 +11,15 @@ static BOOL WINAPI CtrlHandler(DWORD fdwCtrlType) {
case CTRL_BREAK_EVENT:
case CTRL_LOGOFF_EVENT:
case CTRL_SHUTDOWN_EVENT:
if (cleanupFn)
cleanupFn();
return FALSE;

if (fn)
fn();
default:
return FALSE;
}
}

BOOL installHandler(void (*cleanupFn_)()) {
cleanupFn = cleanupFn_;
return SetConsoleCtrlHandler(CtrlHandler, TRUE);
void installExitHandler(exit_handler_t fn_);
fn = fn_;
SetConsoleCtrlHandler(CtrlHandler, TRUE);
return 0;
}
15 changes: 2 additions & 13 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include <cctype>
#include <chrono>
#include <cmath>
#include <csignal>
#include <cstdio>
#include <cstdlib>
#include <exception>
Expand All @@ -32,10 +31,6 @@
#include <Database.h>
#endif

#ifdef __WIN32
#include <windows/sighandler.h>
#endif

#include <date.h>
#include <Authorization.h>
#include <BotAddCommand.h>
Expand All @@ -45,6 +40,7 @@
#include <NamespaceImport.h>
#include <Timer.h>

#include "exithandlers/handler.h"
#include "utils/libutils.h"

using std::chrono_literals::operator""s;
Expand Down Expand Up @@ -654,14 +650,7 @@ int main(void) {
exited = true;
std::exit(0);
};
static auto cleanupVoidFunc = [] { cleanupFunc(0); };

std::signal(SIGINT, cleanupFunc);
std::signal(SIGTERM, cleanupFunc);
std::atexit(cleanupVoidFunc);
#ifdef __WIN32
installHandler(cleanupVoidFunc);
#endif
installExitHandler(cleanupFunc);
int64_t lastcrash = 0;

PRETTYF("Debug: Token: %s", token.c_str());
Expand Down
8 changes: 0 additions & 8 deletions src/windows/sighandler.h

This file was deleted.

0 comments on commit c802661

Please sign in to comment.