Ignore SIGPIPE so we dont crash on broken pipes

This commit is contained in:
Lion Kortlepel
2020-11-09 23:34:44 +01:00
parent 2d11841a68
commit 60c7997c6b
+36 -18
View File
@@ -1,9 +1,22 @@
#include "CustomAssert.h" #include "CustomAssert.h"
#include <curl/curl.h> #include <curl/curl.h>
#include "Startup.h" #include "Startup.h"
#include <iostream> #include <iostream>
#include <thread> #include <thread>
#include "Security/Xor.h"
#ifndef WIN32
#include <signal.h>
void UnixSignalHandler(int sig) {
switch (sig) {
case SIGPIPE:
warn(Sec("ignored signal SIGPIPE: Pipe broken"));
break;
default:
error(Sec("Signal arrived in handler but was not handled: ") + std::to_string(sig));
break;
}
}
#endif // WIN32
[[noreturn]] void loop(){ [[noreturn]] void loop(){
DebugPrintTID(); DebugPrintTID();
@@ -15,23 +28,28 @@
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
try { try {
DebugPrintTID(); #ifndef WIN32
// curl needs to be initialized to properly deallocate its resources later // ignore SIGPIPE, the signal that is sent for example when a client
Assert(curl_global_init(CURL_GLOBAL_DEFAULT) == CURLE_OK); // disconnects while data is being sent to him ("broken pipe").
#ifdef DEBUG signal(SIGPIPE, UnixSignalHandler);
std::thread t1(loop); #endif // WIN32
t1.detach(); DebugPrintTID();
#endif // curl needs to be initialized to properly deallocate its resources later
ConsoleInit(); Assert(curl_global_init(CURL_GLOBAL_DEFAULT) == CURLE_OK);
InitServer(argc,argv); #ifdef DEBUG
InitConfig(); std::thread t1(loop);
InitLua(); t1.detach();
InitRes(); #endif
HBInit(); ConsoleInit();
StatInit(); InitServer(argc,argv);
NetMain(); InitConfig();
// clean up curl at the end to be sure InitLua();
curl_global_cleanup(); InitRes();
HBInit();
StatInit();
NetMain();
// clean up curl at the end to be sure
curl_global_cleanup();
} catch (const std::exception& e) { } catch (const std::exception& e) {
error(std::string(e.what())); error(std::string(e.what()));
throw; throw;