aboutsummaryrefslogtreecommitdiff
path: root/src/zenserver
diff options
context:
space:
mode:
authorDan Engelbrecht <[email protected]>2024-04-17 12:42:01 +0200
committerGitHub Enterprise <[email protected]>2024-04-17 12:42:01 +0200
commitc956e958e0a386f24e6865ad62ee4fe640f93b18 (patch)
tree96a2d52c5e33e9af76d36535b0c675d56b685617 /src/zenserver
parentgc v2 disk freed space fix and oplog stats report improvement (#45) (diff)
downloadzen-c956e958e0a386f24e6865ad62ee4fe640f93b18.tar.xz
zen-c956e958e0a386f24e6865ad62ee4fe640f93b18.zip
zen startup hardening (#49)
- Feature: `zen up` command improvements - --`port` allows you to specify a base port when starting an instance - --`base-dir` allows you to specify a base directory for the zenserver executable if it is not located next to the zen.exe executable - Feature: `zen down` - --`port` allows you to specify a base port when shutting down an instance - --`base-dir` allows you to specify a base directory for the zenserver executable if it is not located next to the zen.exe executable - --`force` if regular shutdown fails it tries to find a running zenserver.exe process and terminate it - If it fails to attach to the running server it now waits for it to exit when setting the RequestExit shared memory flag - Improvement: zenserver now checks the RequestExit flag in the shared memory and exist gracefully if it is set - Improvement: When adding a sponsor process to a running zenserver instance, we wait for it to be picked up from the shared memory section to determine success/fail
Diffstat (limited to 'src/zenserver')
-rw-r--r--src/zenserver/main.cpp38
-rw-r--r--src/zenserver/zenserver.cpp21
-rw-r--r--src/zenserver/zenserver.h2
3 files changed, 55 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);
diff --git a/src/zenserver/zenserver.cpp b/src/zenserver/zenserver.cpp
index e6e451952..86dbc9617 100644
--- a/src/zenserver/zenserver.cpp
+++ b/src/zenserver/zenserver.cpp
@@ -475,6 +475,8 @@ ZenServer::InitializeState(const ZenServerOptions& ServerOptions)
EnqueueStateMarkerTimer();
}
+
+ EnqueueStateExitFlagTimer();
}
void
@@ -805,6 +807,14 @@ ZenServer::EnqueueSigIntTimer()
}
void
+ZenServer::EnqueueStateExitFlagTimer()
+{
+ m_SigIntTimer.expires_after(std::chrono::milliseconds(500));
+ m_SigIntTimer.async_wait([this](const asio::error_code&) { CheckStateExitFlag(); });
+ EnsureIoRunner();
+}
+
+void
ZenServer::EnqueueStatsReportingTimer()
{
m_StatsReportingTimer.expires_after(std::chrono::milliseconds(500));
@@ -858,6 +868,17 @@ ZenServer::CheckSigInt()
EnqueueSigIntTimer();
}
+void
+ZenServer::CheckStateExitFlag()
+{
+ if (m_ServerEntry && m_ServerEntry->IsShutdownRequested())
+ {
+ RequestExit(0);
+ return;
+ }
+ EnqueueStateExitFlagTimer();
+}
+
bool
ZenServer::UpdateProcessMonitor()
{
diff --git a/src/zenserver/zenserver.h b/src/zenserver/zenserver.h
index dd259f855..550047a5d 100644
--- a/src/zenserver/zenserver.h
+++ b/src/zenserver/zenserver.h
@@ -74,9 +74,11 @@ public:
void EnqueueProcessMonitorTimer();
void EnqueueStateMarkerTimer();
void EnqueueSigIntTimer();
+ void EnqueueStateExitFlagTimer();
void EnqueueStatsReportingTimer();
void CheckStateMarker();
void CheckSigInt();
+ void CheckStateExitFlag();
void CheckOwnerPid();
bool UpdateProcessMonitor();
void ScrubStorage();