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
|
// Copyright Epic Games, Inc. All Rights Reserved.
#include "zenutil/openprocesscache.h"
#include <zencore/fmtutils.h>
#include <zencore/logging.h>
#if ZEN_PLATFORM_WINDOWS
# include <zencore/windows.h>
#else
# include <sys/mman.h>
#endif
//////////////////////////////////////////////////////////////////////////
namespace zen {
OpenProcessCache::OpenProcessCache()
#if ZEN_PLATFORM_WINDOWS
: m_GcThread(&OpenProcessCache::GcWorker, this)
#endif // ZEN_PLATFORM_WINDOWS
{
}
OpenProcessCache::~OpenProcessCache()
{
#if ZEN_PLATFORM_WINDOWS
try
{
m_GcExitEvent.Set();
if (m_GcThread.joinable())
{
m_GcThread.join();
}
RwLock::ExclusiveLockScope Lock(m_SessionsLock);
for (const auto& It : m_Sessions)
{
if (It.second.ProcessHandle == nullptr)
{
continue;
}
CloseHandle(It.second.ProcessHandle);
}
m_Sessions.clear();
}
catch (std::exception& Ex)
{
ZEN_ERROR("OpenProcessCache destructor failed with reason: `{}`", Ex.what());
}
#endif // ZEN_PLATFORM_WINDOWS
}
void*
OpenProcessCache::GetProcessHandle(Oid SessionId, int ProcessPid)
{
if (ProcessPid == 0)
{
return nullptr;
}
#if ZEN_PLATFORM_WINDOWS
RwLock::ExclusiveLockScope Lock(m_SessionsLock);
void* DeadHandle = nullptr;
if (auto It = m_Sessions.find(SessionId); It != m_Sessions.end())
{
if (ProcessPid == It->second.ProcessPid)
{
void* ProcessHandle = It->second.ProcessHandle;
ZEN_ASSERT(ProcessHandle != nullptr);
DWORD ExitCode = 0;
GetExitCodeProcess(It->second.ProcessHandle, &ExitCode);
if (ExitCode == STILL_ACTIVE)
{
return It->second.ProcessHandle;
}
}
// Remove the existing stale handle
ZEN_INFO("Closing stale target process pid {} for session: {}", It->second.ProcessPid, SessionId, It->second.ProcessHandle);
DeadHandle = It->second.ProcessHandle;
m_Sessions.erase(It);
}
// Cache new handle
void* ProcessHandle = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION | PROCESS_DUP_HANDLE, FALSE, ProcessPid);
ZEN_INFO("Opening target process pid {} for session {}: {}", ProcessPid, SessionId, ProcessHandle == nullptr ? "failed" : "success");
m_Sessions.insert_or_assign(SessionId, Process{.ProcessPid = ProcessPid, .ProcessHandle = ProcessHandle});
Lock.ReleaseNow();
if (DeadHandle != nullptr)
{
CloseHandle(DeadHandle);
}
return ProcessHandle;
#else // ZEN_PLATFORM_WINDOWS
ZEN_UNUSED(SessionId);
return nullptr;
#endif // ZEN_PLATFORM_WINDOWS
}
#if ZEN_PLATFORM_WINDOWS
void
OpenProcessCache::GCHandles()
{
std::vector<void*> DeadHandles;
RwLock::ExclusiveLockScope Lock(m_SessionsLock);
for (auto& It : m_Sessions)
{
if (It.second.ProcessHandle == nullptr)
{
continue;
}
ZEN_ASSERT(It.second.ProcessPid != 0);
DWORD ExitCode = 0;
GetExitCodeProcess(It.second.ProcessHandle, &ExitCode);
if (ExitCode == STILL_ACTIVE)
{
continue;
}
ZEN_INFO("GC stale target process pid {} for session {}: {}", It.second.ProcessPid, It.first, It.second.ProcessHandle);
DeadHandles.push_back(It.second.ProcessHandle);
It.second.ProcessPid = 0;
It.second.ProcessHandle = nullptr;
}
Lock.ReleaseNow();
for (auto Handle : DeadHandles)
{
CloseHandle(Handle);
}
}
void
OpenProcessCache::GcWorker()
{
while (!m_GcExitEvent.Wait(500))
{
try
{
GCHandles();
}
catch (std::exception& Ex)
{
ZEN_ERROR("gc of open process cache failed with reason: `{}`", Ex.what());
}
}
}
#endif // ZEN_PLATFORM_WINDOWS
} // namespace zen
|