diff options
Diffstat (limited to 'src/zenserver/main.cpp')
| -rw-r--r-- | src/zenserver/main.cpp | 38 |
1 files changed, 32 insertions, 6 deletions
diff --git a/src/zenserver/main.cpp b/src/zenserver/main.cpp index 6b31dc82e..2d2b24bbb 100644 --- a/src/zenserver/main.cpp +++ b/src/zenserver/main.cpp @@ -103,21 +103,42 @@ ZenEntryPoint::Run() ServerState.Initialize(); ServerState.Sweep(); - ZenServerState::ZenServerEntry* Entry = ServerState.Lookup(m_ServerOptions.BasePort); - - if (Entry) + uint32_t AttachSponsorProcessRetriesLeft = 3; + ZenServerState::ZenServerEntry* Entry = ServerState.Lookup(m_ServerOptions.BasePort); + while (Entry) { if (m_ServerOptions.OwnerPid) { + if (!IsProcessRunning(m_ServerOptions.OwnerPid)) + { + ZEN_WARN("Sponsor owner pid {} is no longer running, will not add sponsor to process listening to port {} (pid: {})", + m_ServerOptions.OwnerPid, + m_ServerOptions.BasePort, + Entry->Pid.load()); + std::exit(1); + } ZEN_INFO( "Looks like there is already a process listening to this port {} (pid: {}), attaching owner pid {} to running instance", m_ServerOptions.BasePort, Entry->Pid.load(), m_ServerOptions.OwnerPid); - Entry->AddSponsorProcess(m_ServerOptions.OwnerPid); - - std::exit(0); + if (Entry->AddSponsorProcess(m_ServerOptions.OwnerPid)) + { + std::exit(0); + } + if (AttachSponsorProcessRetriesLeft-- > 0) + { + Entry = ServerState.Lookup(m_ServerOptions.BasePort); + } + else + { + ZEN_WARN("Failed to add sponsor owner pid {} to process listening to port {} (pid: {})", + m_ServerOptions.OwnerPid, + m_ServerOptions.BasePort, + Entry->Pid.load()); + std::exit(1); + } } else { @@ -329,6 +350,11 @@ main(int argc, char* argv[]) } } +#if ZEN_PLATFORM_LINUX | ZEN_PLATFORM_MAC + // Detach ourselves from any parent process + setsid(); +#endif + signal(SIGINT, utils::SignalCallbackHandler); signal(SIGTERM, utils::SignalCallbackHandler); |