4612: Handle SIGINT and SIGTERM (fixes #4610) r=heinrich5991 a=def-

Code by Robyt3 taken from Teeworlds, added SIGTERM

## Checklist

- [x] Tested the change ingame
- [ ] Provided screenshots if it is a visual change
- [ ] Tested in combination with possibly related configuration options
- [ ] Written a unit test if it works standalone, system.c especially
- [ ] Considered possible null pointers and out of bounds array indexing
- [ ] Changed no physics that affect existing maps
- [ ] Tested the change with [ASan+UBSan or valgrind's memcheck](https://github.com/ddnet/ddnet/#using-addresssanitizer--undefinedbehavioursanitizer-or-valgrinds-memcheck) (optional)


Co-authored-by: Robert Müller <robert.mueller@uni-siegen.de>
Co-authored-by: Dennis Felsing <dennis@felsin9.de>
This commit is contained in:
bors[bot] 2022-01-17 23:51:58 +00:00 committed by GitHub
commit 054e7528dd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -48,6 +48,10 @@
#include <windows.h>
#endif
#include <signal.h>
volatile sig_atomic_t InterruptSignaled = 0;
CSnapIDPool::CSnapIDPool()
{
Reset();
@ -2714,6 +2718,11 @@ int CServer::Run()
PacketWaiting = x > 0 ? net_socket_read_wait(m_NetServer.Socket(), x) : true;
}
if(InterruptSignaled)
{
Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "server", "interrupted");
break;
}
}
}
const char *pDisconnectReason = "Server shutdown";
@ -3596,6 +3605,15 @@ void CServer::SnapSetStaticsize(int ItemType, int Size)
static CServer *CreateServer() { return new CServer(); }
void HandleSigIntTerm(int Param)
{
InterruptSignaled = 1;
// Exit the next time a signal is received
signal(SIGINT, SIG_DFL);
signal(SIGTERM, SIG_DFL);
}
int main(int argc, const char **argv) // ignore_convention
{
cmdline_fix(&argc, &argv);
@ -3624,6 +3642,9 @@ int main(int argc, const char **argv) // ignore_convention
return -1;
}
signal(SIGINT, HandleSigIntTerm);
signal(SIGTERM, HandleSigIntTerm);
CServer *pServer = CreateServer();
IKernel *pKernel = IKernel::Create();