-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDaemon.hpp
293 lines (249 loc) · 5.01 KB
/
Daemon.hpp
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
#ifndef Daemon_hpp_
#define Daemon_hpp_
//#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <stdio.h>
#include <psapi.h>
#include <string>
#include "StrTool.hpp"
#include "SimpleFileLog.hpp"
#pragma comment (lib,"Psapi.lib")
namespace util
{
/**
* @brief 自动释放句柄2abcdefg222111
* @author liuheng
*
* 自动释放句柄
*/
class AutoCloseHandler{
public:
AutoCloseHandler(HANDLE h = NULL)
: h_(h)
{
}
~AutoCloseHandler()
{
Close();
}
void Reset(HANDLE h)
{
Close();
h_ = h;
}
operator HANDLE()
{
return h_;
}
private:
void Close()
{
if (h_ != NULL)
{
CloseHandle(h_);
h_ = NULL;
}
}
private:
HANDLE h_;
};
//////////////////////////////////////////////////////////////////////////
static bool GetExeName(char* strExeName)
{
//获取当前进程的ID
DWORD processID = ::GetCurrentProcessId();
//通过进程ID获取到进程的句柄
AutoCloseHandler hProcess(OpenProcess( PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ,
FALSE, processID ));
if (NULL != hProcess )
{
HMODULE hMod;
DWORD cbNeeded;
//获取当前模块的句柄
if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod),
&cbNeeded) )
{
//获取进程的绝对路径
GetModuleFileNameExA(hProcess, hMod, strExeName, MAX_PATH);
if(strExeName[0] == '\0')
{
return false;
}
}
}
return true;
}
static bool RunExename(char* exe, HANDLE& phandler)
{
STARTUPINFOA si = { sizeof(si)};
PROCESS_INFORMATION pi;
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = TRUE; //TRUE表示显示创建的进程的窗口
BOOL bRet = ::CreateProcessA (
NULL,
exe,
NULL,
NULL,
FALSE,
CREATE_NEW_CONSOLE,
NULL,
NULL,
&si,
&pi);
if(bRet)
{
phandler = pi.hProcess;
::CloseHandle (pi.hThread);
//::CloseHandle (pi.hProcess);
}
return bRet;
}
//////////////////////////////////////////////////////////////////////////
/**
* @brief 守护类
* @author liuheng
* @param[in] arg0 desc
* @param[in] arg1 desc
* @retval true desc
* @return desc
*
*/
class Daemon{
public:
Daemon(const char* cmdline)
:is_daemon_(true)
,my_pid_(0)
,start_time_(0)
,daemon_thread_(NULL)
,last_restarthour_(0)
,last_restartday_(0)
{
memset(exe_, 0, MAX_PATH*sizeof(char));
memset(argv_, 0, MAX_PATH*sizeof(char));
const char* firstspace = strstr(cmdline, " ");
if (firstspace != NULL)
{
strcat(argv_, firstspace);
}
if (strstr(cmdline, "_notdaemon_") != NULL)
{
is_daemon_ = false;
}
GetExeName(exe_);
my_pid_ = GetCurrentProcessId();
}
~Daemon()
{
}
void start(const std::string& restart_hours){
if (is_daemon_ == true)
{
util::FileLog::Ins().Init();
util::FileLog::Ins().Info("守护进程[%d]启动,守护规则:%s", my_pid_, restart_hours.c_str());
// 守护进程
util::Str2Set(restart_hours, restart_hours_, ',');
daemon_thread_ = CreateThread(NULL, 0, DaemonThread, this, 0, NULL);
WaitForSingleObject(daemon_thread_, INFINITE);
}
}
protected:
static DWORD WINAPI DaemonThread(PVOID pParam)
{
Daemon* pThis = (Daemon*)pParam;
return pThis->DaemonFunc();
}
DWORD DaemonFunc()
{
// daemon
while (true)
{
util::FileLog::Ins().Info("守护进程[%d],检测一次执行进程", my_pid_);
bool isexist = false;
if(ach_ != NULL)
{
isexist = true;
DWORD ec = WaitForSingleObject(ach_, 60*1000);
if (ec == WAIT_OBJECT_0)
{
util::FileLog::Ins().Info("###检测到执行进程退出了");
ach_.Reset(NULL);
isexist = false;
}
}else
{
util::FileLog::Ins().Info("###检测到执行进程不存在了");
}
SYSTEMTIME st;
GetLocalTime(&st);
int curday = st.wDay;
int curhour = st.wHour;
if (isexist)
{
// exist
util::FileLog::Ins().Info("~~~找到执行进程,判断是否需要重启");
char strhour[10];
sprintf_s(strhour, "%d", curhour);
if (restart_hours_.find(strhour)!=restart_hours_.end())
{
if (last_restarthour_!=curhour || last_restartday_!=curday)
{
// kill process
util::FileLog::Ins().Info("~~~需要重新启动执行进程了");
stop_child();
Sleep(2000);
}
}
}else{
// not exist
util::FileLog::Ins().Info("###没有找到执行进程,创建一个");
if(start_child()){
last_restartday_ = curday;
last_restarthour_ = curhour;
}
}
}
return 0;
}
private:
bool stop_child(){
char cmdline[MAX_PATH*2] = {0};
if(ach_ != NULL)
{
bool ret = TerminateProcess(ach_, 0);
util::FileLog::Ins().Info("结束进程失败[%d][%d]", ret, GetLastError());
}
return true;
}
bool start_child(){
char cmdline[MAX_PATH*2] = {0};
strcpy(cmdline, exe_);
strcat(cmdline, argv_);
strcat(cmdline, " _notdaemon_");
HANDLE phandler = NULL;
bool ret = RunExename(cmdline, phandler);
if (ret)
{
ach_.Reset(phandler);
}
start_time_ = time(NULL);
return ret;
}
private:
bool is_daemon_;
// 当前进程
int my_pid_;
// 被守护的进程
AutoCloseHandler ach_;
// 启动信息
char exe_[MAX_PATH];
char argv_[MAX_PATH];
// 时间
int start_time_;
int last_restartday_;
int last_restarthour_;
std::set<std::string> restart_hours_;
HANDLE daemon_thread_;
};
}
#endif