aboutsummaryrefslogtreecommitdiff
path: root/src/zen/cmds/up_cmd.cpp
blob: 1f23e681986c24c0622b2045ef23cb86cc5bb453 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
// Copyright Epic Games, Inc. All Rights Reserved.

#include "up_cmd.h"

#include <zencore/basicfile.h>
#include <zencore/compactbinary.h>
#include <zencore/compactbinaryutil.h>
#include <zencore/filesystem.h>
#include <zencore/fmtutils.h>
#include <zencore/logging.h>
#include <zencore/process.h>
#include <zenutil/zenserverprocess.h>

namespace zen {

namespace {

	bool TryShutdownByPid(ZenServerState& Instance, uint32_t Pid, const std::filesystem::path& ProgramBaseDir, bool ForceTerminate)
	{
		Instance.Sweep();

		uint16_t DesiredPort = 0;
		Instance.Snapshot([&](const ZenServerState::ZenServerEntry& Entry) {
			if (Entry.Pid.load() == Pid)
			{
				DesiredPort = Entry.DesiredListenPort.load();
			}
		});

		ZenServerState::ZenServerEntry* Entry = (DesiredPort != 0) ? Instance.Lookup(DesiredPort) : nullptr;
		if (Entry && Entry->Pid.load() != Pid)
		{
			Entry = nullptr;
		}

		if (Entry)
		{
			if (ShutdownZenServer(ConsoleLog(), Instance, Entry, ProgramBaseDir))
			{
				return true;
			}
		}

		std::error_code Ec;
		ProcessHandle	Proc;
		Proc.Initialize(int(Pid), Ec);
		if (!Ec && Proc.IsValid() && !Proc.IsRunning())
		{
			return true;
		}

		if (ForceTerminate && !Ec && Proc.IsValid() && Proc.IsRunning())
		{
			ZEN_CONSOLE_WARN("Hard terminating zen process with pid ({})", Pid);
			if (Proc.Terminate(0))
			{
				ZEN_CONSOLE("Terminate complete");
				return true;
			}
		}

		return false;
	}

}  // namespace

UpCommand::UpCommand()
{
	m_Options.add_option("", "p", "port", "Host port", cxxopts::value(m_Port)->default_value("0"), "<hostport>");
	m_Options.add_option("", "b", "base-dir", "Parent folder of server executable", cxxopts::value(m_ProgramBaseDir), "<directory>");
	m_Options.add_option("", "c", "show-console", "Open a console window for the zenserver process", cxxopts::value(m_ShowConsole), "");
	m_Options.add_option("",
						 "l",
						 "show-log",
						 "Show the output log of the zenserver process after successful start",
						 cxxopts::value(m_ShowLog),
						 "");
}

UpCommand::~UpCommand() = default;

void
UpCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv)
{
	if (!ParseOptions(argc, argv))
	{
		return;
	}

	if (m_ShowConsole && m_ShowLog)
	{
		throw OptionParseException("'--show-console' conflicts with '--show-log'", m_Options.help());
	}

	if (m_ProgramBaseDir.empty())
	{
		m_ProgramBaseDir = GetRunningExecutablePath().parent_path();
	}
	MakeSafeAbsolutePathInPlace(m_ProgramBaseDir);

	std::optional<int> StartResult = StartupZenServer(ConsoleLog(),
													  {.ProgramBaseDir		   = m_ProgramBaseDir,
													   .Port				   = m_Port,
													   .OpenConsole			   = m_ShowConsole,
													   .ShowLog				   = m_ShowLog,
													   .ExtraArgs			   = GlobalOptions.PassthroughCommandLine,
													   .EnableExecutionHistory = GlobalOptions.EnableExecutionHistory});
	if (!StartResult.has_value())
	{
		ZEN_CONSOLE("Zen server already running");
		return;
	}
	if (*StartResult != 0)
	{
		throw ErrorWithReturnCode("Zen server failed to start", *StartResult);
	}

	ZEN_CONSOLE("Zen server up");
}

//////////////////////////////////////////////////////////////////////////

AttachCommand::AttachCommand()
{
	m_Options.add_option("", "p", "port", "Host port", cxxopts::value(m_Port)->default_value("8558"), "<hostport>");
	m_Options.add_option("lifetime", "", "owner-pid", "Specify owning process id", cxxopts::value(m_OwnerPid), "<identifier>");
	m_Options.add_option("", "", "data-dir", "Path to data directory to inspect for running server", cxxopts::value(m_DataDir), "<file>");
}

AttachCommand::~AttachCommand() = default;

void
AttachCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv)
{
	ZEN_UNUSED(GlobalOptions);

	if (!ParseOptions(argc, argv))
	{
		return;
	}

	if (!m_DataDir.empty())
	{
		MakeSafeAbsolutePathInPlace(m_DataDir);
	}

	ZenServerState Instance;
	Instance.Initialize();
	Instance.Sweep();
	ZenServerState::ZenServerEntry* Entry = Instance.Lookup(m_Port);

	if (!m_DataDir.empty())
	{
		if (!LockFile::IsHeldLive(m_DataDir / ".lock", /*AttemptCleanup*/ false))
		{
			throw std::runtime_error(fmt::format("No live zen server holding lock file in directory '{}'", m_DataDir));
		}
		CbValidateError ValidateResult = CbValidateError::None;
		if (CbObject LockFileObject =
				ValidateAndReadCompactBinaryObject(IoBufferBuilder::MakeFromFile(m_DataDir / ".lock"), ValidateResult);
			ValidateResult == CbValidateError::None && LockFileObject)
		{
			LockFileInfo Info = ReadLockFilePayload(LockFileObject);
			std::string	 Reason;
			if (!ValidateLockFileInfo(Info, Reason))
			{
				throw std::runtime_error(fmt::format("Lock file in directory '{}' is not valid. Reason: '{}'", m_DataDir, Reason));
			}
			Entry = Instance.LookupByEffectivePort(Info.EffectiveListenPort);
		}
		else
		{
			throw std::runtime_error(
				fmt::format("Lock file in directory '{}' is malformed. Reason: '{}'", m_DataDir, ToString(ValidateResult)));
		}
	}

	if (!Entry)
	{
		throw std::runtime_error("No zen server instance to add sponsor process to");
	}

	// Sponsor processes are checked every second, so 2 second wait time should be enough
	if (!Entry->AddSponsorProcess(m_OwnerPid, 2000))
	{
		throw std::runtime_error("Unable to add sponsor process to running zen server instance");
	}

	ZEN_CONSOLE("Added sponsor process {} to running instance {} on port {}", m_OwnerPid, Entry->Pid.load(), m_Port);
}

//////////////////////////////////////////////////////////////////////////

DownCommand::DownCommand()
{
	m_Options.add_option("", "p", "port", "Host port", cxxopts::value(m_Port)->default_value("0"), "<hostport>");
	m_Options.add_option("", "a", "all", "Shut down all running zen server instances", cxxopts::value(m_All), "");
	m_Options.add_option("", "f", "force", "Force terminate if graceful shutdown fails", cxxopts::value(m_ForceTerminate), "<force>");
	m_Options.add_option("", "b", "base-dir", "Parent folder of server executable", cxxopts::value(m_ProgramBaseDir), "<directory>");
	m_Options.add_option("", "", "data-dir", "Path to data directory to inspect for running server", cxxopts::value(m_DataDir), "<file>");
	m_Options.add_option("", "", "pid", "Shut down zen server process by PID", cxxopts::value(m_Pid)->default_value("0"), "<pid>");
	m_Options.add_option("",
						 "",
						 "executable",
						 "Shut down all zen server processes matching executable path",
						 cxxopts::value(m_ExecutablePath),
						 "<path>");
}

DownCommand::~DownCommand() = default;

void
DownCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv)
{
	ZEN_UNUSED(GlobalOptions);

	if (!ParseOptions(argc, argv))
	{
		return;
	}

	const bool HasPid		 = m_Pid != 0;
	const bool HasExecutable = !m_ExecutablePath.empty();
	const bool HasDataDir	 = !m_DataDir.empty();
	const int  SelectorCount = int(m_All) + int(HasPid) + int(HasExecutable) + int(HasDataDir);
	if (SelectorCount > 1)
	{
		throw OptionParseException("--all, --pid, --executable, and --data-dir are mutually exclusive", m_Options.help());
	}

	if (m_ProgramBaseDir.empty())
	{
		std::filesystem::path ExePath = GetRunningExecutablePath();
		m_ProgramBaseDir			  = ExePath.parent_path();
	}
	MakeSafeAbsolutePathInPlace(m_ProgramBaseDir);
	if (!m_DataDir.empty())
	{
		MakeSafeAbsolutePathInPlace(m_DataDir);
	}
	if (!m_ExecutablePath.empty() && m_ExecutablePath.has_parent_path())
	{
		MakeSafeAbsolutePathInPlace(m_ExecutablePath);
	}

	// Discover executing instances
	ZenServerState Instance;
	Instance.Initialize();

	if (HasPid)
	{
		if (!TryShutdownByPid(Instance, m_Pid, m_ProgramBaseDir, m_ForceTerminate))
		{
			throw std::runtime_error(fmt::format("Failed to shut down zen process with pid {}, use --force to hard terminate", m_Pid));
		}
		ZEN_CONSOLE("Zen server with pid {} is down", m_Pid);
		return;
	}

	if (HasExecutable)
	{
		int ShutdownCount = 0;
		while (true)
		{
			ProcessHandle	Proc;
			std::error_code Ec = FindProcess(m_ExecutablePath, Proc, /*IncludeSelf*/ false);
			if (Ec)
			{
				throw std::system_error(Ec, fmt::format("FindProcess failed for '{}'", m_ExecutablePath));
			}
			if (!Proc.IsValid())
			{
				break;
			}
			const uint32_t Pid = uint32_t(Proc.Pid());
			if (!TryShutdownByPid(Instance, Pid, m_ProgramBaseDir, m_ForceTerminate))
			{
				throw std::runtime_error(fmt::format("Failed to shut down zen process with pid {}, use --force to hard terminate", Pid));
			}
			++ShutdownCount;
		}
		if (ShutdownCount == 0)
		{
			ZEN_CONSOLE("No zen server processes matching executable '{}'", m_ExecutablePath);
		}
		else
		{
			ZEN_CONSOLE("Shut down {} zen server instance(s) matching executable '{}'", ShutdownCount, m_ExecutablePath);
		}
		return;
	}

	if (m_All)
	{
		struct EntryInfo
		{
			uint16_t Port = 0;
			uint32_t Pid  = 0;
		};
		std::vector<EntryInfo> Entries;
		Instance.Snapshot([&Entries](const ZenServerState::ZenServerEntry& Entry) {
			uint16_t Port = Entry.DesiredListenPort.load();
			uint32_t Pid  = Entry.Pid.load();
			if (Port != 0 && Pid != 0)
			{
				Entries.push_back({Port, Pid});
			}
		});

		if (Entries.empty())
		{
			ZEN_CONSOLE("No zen server instances to bring down");
			return;
		}

		int FailCount = 0;
		for (const EntryInfo& Info : Entries)
		{
			if (!TryShutdownByPid(Instance, Info.Pid, m_ProgramBaseDir, m_ForceTerminate))
			{
				ZEN_CONSOLE_WARN("Failed to shutdown server on port {} (pid {})", Info.Port, Info.Pid);
				++FailCount;
			}
		}

		if (FailCount > 0 && !m_ForceTerminate)
		{
			throw std::runtime_error(fmt::format("Failed to shutdown {} instance(s), use --force to hard terminate", FailCount));
		}
		return;
	}

	ZenServerState::ZenServerEntry* Entry = Instance.Lookup(m_Port);

	if (!m_DataDir.empty())
	{
		if (!LockFile::IsHeldLive(m_DataDir / ".lock", /*AttemptCleanup*/ true))
		{
			ZEN_CONSOLE("No live zen server holding lock file in '{}', nothing to do", m_DataDir);
			return;
		}
		CbValidateError ValidateResult = CbValidateError::None;
		if (CbObject LockFileObject =
				ValidateAndReadCompactBinaryObject(IoBufferBuilder::MakeFromFile(m_DataDir / ".lock"), ValidateResult);
			ValidateResult == CbValidateError::None && LockFileObject)
		{
			LockFileInfo Info = ReadLockFilePayload(LockFileObject);
			std::string	 Reason;
			if (!ValidateLockFileInfo(Info, Reason))
			{
				throw std::runtime_error(fmt::format("Lock file in directory '{}' is not valid. Reason: '{}'", m_DataDir, Reason));
			}
			Entry = Instance.LookupByEffectivePort(Info.EffectiveListenPort);
		}
		else
		{
			throw std::runtime_error(
				fmt::format("Lock file in directory '{}' is malformed. Reason: '{}'", m_DataDir, ToString(ValidateResult)));
		}
	}

	if (Entry)
	{
		if (ShutdownZenServer(ConsoleLog(), Instance, Entry, m_ProgramBaseDir))
		{
			return;
		}
	}

	if (m_ForceTerminate)
	{
		// Try to find the running executable by path name
		std::filesystem::path ServerExePath = m_ProgramBaseDir / "zenserver" ZEN_EXE_SUFFIX_LITERAL;
		ProcessHandle		  RunningProcess;
		if (std::error_code Ec = FindProcess(ServerExePath, RunningProcess, /*IncludeSelf*/ false); !Ec)
		{
			ZEN_CONSOLE_WARN("Attempting hard terminate of zen process with pid ({})", RunningProcess.Pid());
			if (RunningProcess.Terminate(0))
			{
				ZEN_CONSOLE("Terminate complete");
				return;
			}
			throw std::runtime_error("Failed to terminate server, still running");
		}
		else
		{
			ZEN_CONSOLE_WARN("Failed to find process '{}', reason: {}", ServerExePath.string(), Ec.message());
		}
	}
	else if (Entry)
	{
		throw std::runtime_error(
			fmt::format("Failed to shut down server on port {}, use --force to hard terminate process", Entry->DesiredListenPort.load()));
	}

	ZEN_CONSOLE("No zen server to bring down");
}

}  // namespace zen