aboutsummaryrefslogtreecommitdiff
path: root/src/zen/cmds/status_cmd.cpp
blob: 6ed3c42e1dbfce2ae57d057d0b95cbf327b1d0b4 (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
// Copyright Epic Games, Inc. All Rights Reserved.

#include "status_cmd.h"

#include <zencore/compactbinary.h>
#include <zencore/compactbinaryutil.h>
#include <zencore/filesystem.h>
#include <zencore/fmtutils.h>
#include <zencore/logging.h>
#include <zencore/string.h>
#include <zencore/uid.h>
#include <zenutil/zenserverprocess.h>

namespace zen {

StatusCommand::StatusCommand()
{
	m_Options.add_option("", "p", "port", "Host port", cxxopts::value(m_Port)->default_value("0"), "<hostport>");
	m_Options.add_option("", "", "data-dir", "Path to data directory to inspect for running server", cxxopts::value(m_DataDir), "<file>");
}

StatusCommand::~StatusCommand() = default;

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

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

	uint16_t EffectivePort = 0;
	if (!m_DataDir.empty())
	{
		if (!IsFile(m_DataDir / ".lock"))
		{
			throw std::runtime_error(fmt::format("Lock file does not exist in directory '{}'", m_DataDir));
		}
		CbValidateError ValidateResult = CbValidateError::None;
		if (CbObject LockFileObject =
				ValidateAndReadCompactBinaryObject(IoBufferBuilder::MakeFromFile(m_DataDir / ".lock"), ValidateResult);
			ValidateResult == CbValidateError::None)
		{
			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));
			}
			EffectivePort = Info.EffectiveListenPort;
		}
		else
		{
			throw std::runtime_error(
				fmt::format("Lock file in directory '{}' is malformed. Reason: '{}'", m_DataDir, ToString(ValidateResult)));
		}
	}

	ZenServerState State;
	if (!State.InitializeReadOnly())
	{
		ZEN_CONSOLE("No Zen state found");
		return;
	}

	ZEN_CONSOLE("{:>5} {:>6} {:>24}  {}", "port", "pid", "session", "socket");
	State.Snapshot([&](const ZenServerState::ZenServerEntry& Entry) {
		bool MatchesAnyPort		  = (m_Port == 0) && (EffectivePort == 0);
		bool MatchesEffectivePort = (EffectivePort != 0) && (Entry.EffectiveListenPort.load() == EffectivePort);
		bool MatchesDesiredPort	  = (m_Port != 0) && (Entry.DesiredListenPort.load() == m_Port);
		if (MatchesAnyPort || MatchesEffectivePort || MatchesDesiredPort)
		{
			StringBuilder<25> SessionStringBuilder;
			Entry.GetSessionId().ToString(SessionStringBuilder);

			std::string SocketPath;
			if (Entry.HasInstanceInfo())
			{
				ZenServerInstanceInfo Info;
				if (Info.OpenReadOnly(Entry.GetSessionId()))
				{
					InstanceInfoData Data = Info.Read();
					if (!Data.UnixSocketPath.empty())
					{
						SocketPath = PathToUtf8(Data.UnixSocketPath);
					}
				}
			}
			std::string PortStr = Entry.IsNoNetwork() ? std::string("-") : fmt::to_string(Entry.EffectiveListenPort.load());
			ZEN_CONSOLE("{:>5} {:>6} {:>24}  {}", PortStr, Entry.Pid.load(), SessionStringBuilder, SocketPath);
		}
	});
}

}  // namespace zen