Repository navigation
Expand file tree
/
Copy pathProcessManager.cpp
More file actions
213 lines (195 loc) · 5.85 KB
/
Copy pathProcessManager.cpp
File metadata and controls
213 lines (195 loc) · 5.85 KB
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
#include "ProcessManager.h"
#include <sstream>
#include "GetCmdLine.h"
ProcessManager::ProcessManager(std::wstring cmdline)
{
_trdalive = false;
_log = NULL;
_app_status = "Stopped";
_log = new Logging::LogFile("logfile.txt", false, "Ragnarok",Logging::Fine);
_cmdline = cmdline;
std::thread * _exeTrd = NULL;
_OnProcStart = [] {return; };
_OnProcCrash = [] {return; };
_OnProcManuallyStopped = [] { return; };
_OnProcRestart = [] { return; };
}
ProcessManager::ProcessManager(std::wstring cmdline,
std::function<void()> OnProcStart,
std::function<void()> OnProcCrash,
std::function<void()> OnProcManuallyStopped,
std::function<void()> OnProcRestart
) : ProcessManager(cmdline)
{
_OnProcStart = OnProcStart;
_OnProcCrash = OnProcCrash;
_OnProcManuallyStopped = OnProcManuallyStopped;
_OnProcRestart = OnProcRestart;
}
ProcessManager::ProcessManager(DWORD pid)
{
_trdalive = false;
_log = NULL;
_app_status = "Stopped";
_log = new Logging::LogFile("logfile.txt", false, "Ragnarok",Logging::Fine);
_exeTrd = NULL;
_OnProcStart = [] {return; };
_OnProcCrash = [] {return; };
_OnProcManuallyStopped = [] { return; };
_OnProcRestart = [] { return; };
ZeroMemory(&_pi, sizeof(_pi));
HANDLE handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
if (handle == NULL){
std::string error = "Failed to open process with error ";
error.append(std::to_string(GetLastError()));
_log->LogMessage(Logging::Sever,format,error);
delete _log;
throw MsgException(error);
}
PWSTR cmdline = GetCMDLine(pid, _log);
_cmdline.append(cmdline);
_pi.hProcess = handle;
_pi.dwProcessId = pid;
_trdalive = true;
_exeTrd = new std::thread(&ProcessManager::threadfunc, this);
}
ProcessManager::ProcessManager(DWORD pid,
std::function<void()> OnProcStart,
std::function<void()> OnProcCrash,
std::function<void()> OnProcManuallyStopped,
std::function<void()> OnProcRestart
)
{
_trdalive = false;
_log = NULL;
_app_status = "Stopped";
_log = new Logging::LogFile("logfile.txt", false, "Ragnrok", Logging::Fine);
_exeTrd = NULL;
_OnProcStart = [] {return; };
_OnProcCrash = [] {return; };
_OnProcManuallyStopped = [] { return; };
ZeroMemory(&_pi, sizeof(_pi));
HANDLE handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
if (handle == NULL){
std::string error = "Failed to open process with error ";
error.append(std::to_string(GetLastError()));
_log->LogMessage(Logging::Sever,format,error);
delete _log;
throw MsgException(error);
}
PWSTR cmdline = GetCMDLine(pid, _log);
_cmdline.append(cmdline);
_pi.hProcess = handle;
_pi.dwProcessId = pid;
_trdalive = true;
_exeTrd = new std::thread(&ProcessManager::threadfunc, this);
_OnProcStart = OnProcStart;
_OnProcCrash = OnProcCrash;
_OnProcManuallyStopped = OnProcManuallyStopped;
_OnProcRestart = OnProcRestart;
}
void ProcessManager::StartProcess()
{
std::unique_lock<std::mutex> guard(_mu);
if (_trdalive) return;
_stopProcess();
_startProcess();
_trdalive = true;
_exeTrd = new std::thread(&ProcessManager::threadfunc, this);
std::thread t(_OnProcStart);
t.detach();
}
void ProcessManager::RestartProcess(){
std::unique_lock<std::mutex> guard(_mu);
_stopProcess();
_startProcess();
_trdalive = true;
_exeTrd = new std::thread(&ProcessManager::threadfunc, this);
std::thread t(_OnProcRestart);
t.detach();
}
void ProcessManager::StopProcess(){
std::unique_lock<std::mutex> guard(_mu);
_stopProcess();
std::thread t(_OnProcManuallyStopped);
t.detach();
}
ProcessStatus ProcessManager::GetStatus(){
std::lock_guard<std::mutex> guard(_mu);
return ProcessStatus(_pi.hProcess, _pi.dwProcessId, _cmdline, _app_status);
}
ProcessManager::~ProcessManager()
{
_stopProcess();
if (_log)
delete _log;
if (_exeTrd != NULL){
if (_exeTrd->joinable())
_exeTrd->join();
}
}
void ProcessManager::_startProcess(){
//clear memory
ZeroMemory(&_si, sizeof(_si));
_si.cb = sizeof(_si);
ZeroMemory(&_pi, sizeof(_pi));
wchar_t* cmdLine = const_cast<wchar_t*>(_cmdline.c_str());
_log->LogMessage(Logging::Info, format ,"Trying to start process");
if (!CreateProcess(NULL, // No module name (use command line)
cmdLine, // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&_si, // Pointer to STARTUPINFO structure
&_pi) // Pointer to PROCESS_INFORMATION structure
)
{
std::ostringstream strStream;
strStream << "Process couldn't start. Error code " << GetLastError();
_log->LogMessage(Logging::Warning, format ,strStream.str());
throw MsgException(strStream.str());
}
else {
_app_status = "Running";
_log->LogMessage(Logging::Info, format, "Process started succesfully");
}
}
void ProcessManager::_stopProcess(){
_log->LogMessage(Logging::Info, format, "Trying to shutdown the process");
if (_exeTrd != NULL && _trdalive){
_trdalive = false;
TerminateProcess(_pi.hProcess, 0);
_exeTrd->join();
delete _exeTrd;
_exeTrd = NULL;
_log->LogMessage(Logging::Info, format,"Process terminated");
_app_status = "Stopped";
}
else {
_log->LogMessage(Logging::Info, format, "Process is not running thus it wasn't stopped");
}
}
void ProcessManager::threadfunc(){
std::unique_lock<std::mutex> guard(_mu2);
while (_trdalive){
WaitForSingleObject(_pi.hProcess, INFINITE);
if (_trdalive){
unsigned long exitCode;
GetExitCodeProcess(_pi.hProcess, &exitCode);
_app_status = "Restarting";
CloseHandle(_pi.hProcess);
CloseHandle(_pi.hThread);
std::ostringstream str;
str << "Process terminated itself with code " << exitCode;
_log->LogMessage(Logging::Info, format, str.str());
_startProcess();
std::thread t(_OnProcCrash);
t.detach();
}
}
CloseHandle(_pi.hProcess);
CloseHandle(_pi.hThread);
}