实现windows版本daemonize
parent
791e274f52
commit
b3f5cc3589
|
@ -44,10 +44,17 @@ file(COPY ${THIRD} DESTINATION ${LIBRARY_OUTPUT_PATH}/inc/third/include/
|
||||||
FILE_PERMISSIONS OWNER_READ OWNER_WRITE GROUP_WRITE GROUP_READ WORLD_READ)
|
FILE_PERMISSIONS OWNER_READ OWNER_WRITE GROUP_WRITE GROUP_READ WORLD_READ)
|
||||||
message( "copy net \n")
|
message( "copy net \n")
|
||||||
|
|
||||||
|
message( "copy function library \n")
|
||||||
|
file(GLOB FUNCTION ${PROJECT_SOURCE_DIR}/general/src/function/*.h)
|
||||||
|
message("function " ${FUNCTION})
|
||||||
|
file(COPY ${FUNCTION} DESTINATION ${LIBRARY_OUTPUT_PATH}/inc/function/
|
||||||
|
FILE_PERMISSIONS OWNER_READ OWNER_WRITE GROUP_WRITE GROUP_READ WORLD_READ)
|
||||||
|
|
||||||
|
message( "copy net \n")
|
||||||
file(GLOB NET ${PROJECT_SOURCE_DIR}/general/src/net/*.h)
|
file(GLOB NET ${PROJECT_SOURCE_DIR}/general/src/net/*.h)
|
||||||
file(COPY ${NET} DESTINATION ${LIBRARY_OUTPUT_PATH}/inc/
|
file(COPY ${NET} DESTINATION ${LIBRARY_OUTPUT_PATH}/inc/net/
|
||||||
FILE_PERMISSIONS OWNER_READ OWNER_WRITE GROUP_WRITE GROUP_READ WORLD_READ)
|
FILE_PERMISSIONS OWNER_READ OWNER_WRITE GROUP_WRITE GROUP_READ WORLD_READ)
|
||||||
|
|
||||||
file(GLOB ALGORITHM ${PROJECT_SOURCE_DIR}/general/src/algorithm/*)
|
file(GLOB ALGORITHM ${PROJECT_SOURCE_DIR}/general/src/algorithm/*)
|
||||||
file(COPY ${ALGORITHM} DESTINATION ${LIBRARY_OUTPUT_PATH}/inc/
|
file(COPY ${ALGORITHM} DESTINATION ${LIBRARY_OUTPUT_PATH}/inc/algorithm/
|
||||||
FILE_PERMISSIONS OWNER_READ OWNER_WRITE GROUP_WRITE GROUP_READ WORLD_READ)
|
FILE_PERMISSIONS OWNER_READ OWNER_WRITE GROUP_WRITE GROUP_READ WORLD_READ)
|
||||||
|
|
|
@ -1,14 +1,77 @@
|
||||||
/*
|
/*
|
||||||
* @Author: your name
|
* @Author: your name
|
||||||
* @Date: 2021-07-23 23:39:57
|
* @Date: 2021-07-23 23:39:57
|
||||||
* @LastEditTime: 2021-07-25 02:31:24
|
* @LastEditTime: 2021-07-26 00:28:04
|
||||||
* @LastEditors: your name
|
* @LastEditors: Please set LastEditors
|
||||||
* @Description: In User Settings Edit
|
* @Description: In User Settings Edit
|
||||||
* @FilePath: \generallib\general\src\function\daemon.c
|
* @FilePath: \generallib\general\src\function\daemon.c
|
||||||
*/
|
*/
|
||||||
#include "daemon.h"
|
#include "daemon.h"
|
||||||
|
|
||||||
BOOL SetProcessPrivilege(char *lpName, BOOL opt)
|
|
||||||
|
static BOOL KillProcess(DWORD dwPid)
|
||||||
|
{
|
||||||
|
HANDLE hPrc;
|
||||||
|
|
||||||
|
if (0 == dwPid)
|
||||||
|
return FALSE;
|
||||||
|
|
||||||
|
hPrc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwPid); // Opens handle to the process.
|
||||||
|
|
||||||
|
if (!TerminateProcess(hPrc, 0)) // Terminates a process.
|
||||||
|
{
|
||||||
|
CloseHandle(hPrc);
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
WaitForSingleObject(hPrc, 2000); // At most ,waite 2000 millisecond.
|
||||||
|
|
||||||
|
CloseHandle(hPrc);
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
static BOOL KillProcessByName(const TCHAR *lpszProcessName)
|
||||||
|
{
|
||||||
|
unsigned int pid = -1;
|
||||||
|
BOOL retval = TRUE;
|
||||||
|
|
||||||
|
if (lpszProcessName == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
DWORD dwRet = 0;
|
||||||
|
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
|
||||||
|
PROCESSENTRY32 processInfo;
|
||||||
|
processInfo.dwSize = sizeof(PROCESSENTRY32);
|
||||||
|
int flag = Process32First(hSnapshot, &processInfo);
|
||||||
|
|
||||||
|
// Find the process with name as same as lpszProcessName
|
||||||
|
while (flag != 0)
|
||||||
|
{
|
||||||
|
printf("kill process find %s\r\n",processInfo.szExeFile);
|
||||||
|
if (strcmp(processInfo.szExeFile, lpszProcessName) == 0)
|
||||||
|
{
|
||||||
|
// Terminate the process.
|
||||||
|
pid = processInfo.th32ProcessID;
|
||||||
|
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, TRUE, pid);
|
||||||
|
printf("kill process pid %d\r\n",pid);
|
||||||
|
if (TerminateProcess(hProcess, 0) != TRUE)
|
||||||
|
{ // Failed to terminate it.
|
||||||
|
retval = FALSE;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
flag = Process32Next(hSnapshot, &processInfo);
|
||||||
|
} // while (flag != 0)
|
||||||
|
CloseHandle(hSnapshot);
|
||||||
|
if (pid == -1)
|
||||||
|
return FALSE;
|
||||||
|
|
||||||
|
return retval;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static BOOL SetProcessPrivilege(char *lpName, BOOL opt)
|
||||||
{
|
{
|
||||||
HANDLE tokenhandle;
|
HANDLE tokenhandle;
|
||||||
TOKEN_PRIVILEGES NewState;
|
TOKEN_PRIVILEGES NewState;
|
||||||
|
@ -28,7 +91,7 @@ BOOL SetProcessPrivilege(char *lpName, BOOL opt)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int RangeProcess()
|
static int RangeProcess()
|
||||||
{
|
{
|
||||||
DWORD Proc_pid[1024], Retn_bytes, Proc_count, Retn_bytes2;
|
DWORD Proc_pid[1024], Retn_bytes, Proc_count, Retn_bytes2;
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
|
@ -55,7 +118,7 @@ int RangeProcess()
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int test_fork()
|
static int test_fork()
|
||||||
{
|
{
|
||||||
char szCommandLine[] = "D:\\game\\The Legend of Zelda Breath of the Wild\\cemu\\cemu.exe";
|
char szCommandLine[] = "D:\\game\\The Legend of Zelda Breath of the Wild\\cemu\\cemu.exe";
|
||||||
STARTUPINFO si = {sizeof(si)};
|
STARTUPINFO si = {sizeof(si)};
|
||||||
|
@ -83,3 +146,109 @@ int test_fork()
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DaemonizeMonitor::DaemonizeMonitor(string path){
|
||||||
|
this->m_path_process.push_back(path);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int DaemonizeMonitor::AddNewProcess(string path){
|
||||||
|
this->m_path_process.push_back(path);
|
||||||
|
STARTUPINFO si = {sizeof(si)};
|
||||||
|
PROCESS_INFORMATION pi;
|
||||||
|
si.dwFlags = STARTF_USESHOWWINDOW; //指定wShowWindow成员有效
|
||||||
|
si.wShowWindow = TRUE; //此成员设为TRUE的话则显示新建进程的主窗口
|
||||||
|
BOOL bRet = CreateProcess(
|
||||||
|
NULL, //不在此指定可执行文件的文件名
|
||||||
|
(LPSTR)path.c_str(), //命令行参数
|
||||||
|
NULL, //默认进程安全性
|
||||||
|
NULL, //默认进程安全性
|
||||||
|
FALSE, //指定当前进程内句柄不可以被子进程继承
|
||||||
|
CREATE_NEW_CONSOLE, //为新进程创建一个新的控制台窗口
|
||||||
|
NULL, //使用本进程的环境变量
|
||||||
|
NULL, //使用本进程的驱动器和目录
|
||||||
|
&si,
|
||||||
|
&pi);
|
||||||
|
if (bRet)
|
||||||
|
{
|
||||||
|
//不使用的句柄最好关掉
|
||||||
|
CloseHandle(pi.hThread);
|
||||||
|
CloseHandle(pi.hProcess);
|
||||||
|
|
||||||
|
printf("new process id %d\n", pi.dwProcessId);
|
||||||
|
this->m_running_pid[path] = pi.dwProcessId;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int DaemonizeMonitor::StartMonitor(){
|
||||||
|
DWORD Proc_pid[1024], Retn_bytes, Proc_count, Retn_bytes2;
|
||||||
|
unsigned int i;
|
||||||
|
HMODULE hMod[1024];
|
||||||
|
HANDLE hProcess;
|
||||||
|
char szModName[MAX_PATH];
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
/* code */
|
||||||
|
Sleep(1000);
|
||||||
|
for(auto itr = m_running_pid.begin();itr != m_running_pid.end();itr++){
|
||||||
|
bool found = false;
|
||||||
|
if (EnumProcesses(Proc_pid, sizeof(Proc_pid), &Retn_bytes))
|
||||||
|
{
|
||||||
|
Proc_count = Retn_bytes / sizeof(DWORD);
|
||||||
|
SetProcessPrivilege("SeDebugPrivilege", 1);
|
||||||
|
for (i = 0; i < Proc_count; i++)
|
||||||
|
{
|
||||||
|
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, Proc_pid[i]);
|
||||||
|
if (hProcess != NULL)
|
||||||
|
{
|
||||||
|
EnumProcessModules(hProcess, hMod, sizeof(hMod), &Retn_bytes2);
|
||||||
|
GetModuleFileNameEx(hProcess, hMod[0], szModName, sizeof(szModName));
|
||||||
|
printf("PID=%d Path=%s\n", Proc_pid[i], szModName);
|
||||||
|
if(itr->second == Proc_pid[i]){
|
||||||
|
found = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
CloseHandle(hProcess);
|
||||||
|
}
|
||||||
|
SetProcessPrivilege("SeDebugPrivilege", 0);
|
||||||
|
if(!found){
|
||||||
|
// 没找到该应用实例就重启应用
|
||||||
|
for(int i = 0;i < m_path_process.size();i++){
|
||||||
|
if(itr->first == m_path_process.at(i)){
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this->AddNewProcess(itr->first);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int DaemonizeMonitor::StopMonitor(string){
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
*/
|
||||||
|
int DaemonizeMonitor::StopProcess(string path){
|
||||||
|
if(m_running_pid.find(path) != m_running_pid.end()){
|
||||||
|
DWORD pid = m_running_pid.at(path);
|
||||||
|
m_running_pid.erase(path);
|
||||||
|
if(KillProcess(pid))
|
||||||
|
return 0;
|
||||||
|
else{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -1,7 +1,7 @@
|
||||||
/*
|
/*
|
||||||
* @Author: your name
|
* @Author: your name
|
||||||
* @Date: 2021-07-23 23:40:36
|
* @Date: 2021-07-23 23:40:36
|
||||||
* @LastEditTime: 2021-07-25 02:42:00
|
* @LastEditTime: 2021-07-26 00:03:14
|
||||||
* @LastEditors: Please set LastEditors
|
* @LastEditors: Please set LastEditors
|
||||||
* @Description: In User Settings Edi
|
* @Description: In User Settings Edi
|
||||||
* @FilePath: \generallib\general\src\function\daemon.h
|
* @FilePath: \generallib\general\src\function\daemon.h
|
||||||
|
@ -23,12 +23,16 @@ extern "C"{
|
||||||
#include <psapi.h>
|
#include <psapi.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <map>
|
||||||
|
#include <stdint.h>
|
||||||
class DaemonizeMonitor{
|
class DaemonizeMonitor{
|
||||||
public:
|
public:
|
||||||
DaemonizeMonitor(string path);
|
DaemonizeMonitor(string path);
|
||||||
int Start();
|
|
||||||
int AddNewProcess(string);
|
int AddNewProcess(string);
|
||||||
|
int StopMonitor(string);
|
||||||
|
int StopProcess(string);
|
||||||
|
int StartMonitor();
|
||||||
private:
|
private:
|
||||||
std::vector<string> m_path_process;
|
std::vector<string> m_path_process;
|
||||||
|
std::map<string,uint32_t> m_running_pid;
|
||||||
};
|
};
|
|
@ -1,7 +1,7 @@
|
||||||
/*
|
/*
|
||||||
* @Author: your name
|
* @Author: your name
|
||||||
* @Date: 2021-03-15 23:07:25
|
* @Date: 2021-03-15 23:07:25
|
||||||
* @LastEditTime: 2021-07-24 23:19:57
|
* @LastEditTime: 2021-07-25 23:47:46
|
||||||
* @LastEditors: Please set LastEditors
|
* @LastEditors: Please set LastEditors
|
||||||
* @Description: In User Settings Edit
|
* @Description: In User Settings Edit
|
||||||
* @FilePath: \cpp11\cpp11_test.cpp
|
* @FilePath: \cpp11\cpp11_test.cpp
|
||||||
|
@ -25,6 +25,73 @@ extern "C"{
|
||||||
#include <Tlhelp32.h>
|
#include <Tlhelp32.h>
|
||||||
#include "loger.h"
|
#include "loger.h"
|
||||||
#include <psapi.h>
|
#include <psapi.h>
|
||||||
|
#include "function/daemon.h"
|
||||||
|
|
||||||
|
// [Added by thinkhy 09/12/20]
|
||||||
|
// Description: Kill process(es) by PID.
|
||||||
|
// Reference: http://www.vckbase.com/document/viewdoc/?id=1882
|
||||||
|
// RETVALUE: SUCCESS TRUE
|
||||||
|
// FAILED FALSE
|
||||||
|
BOOL KillProcess(DWORD dwPid)
|
||||||
|
{
|
||||||
|
HANDLE hPrc;
|
||||||
|
|
||||||
|
if (0 == dwPid)
|
||||||
|
return FALSE;
|
||||||
|
|
||||||
|
hPrc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwPid); // Opens handle to the process.
|
||||||
|
|
||||||
|
if (!TerminateProcess(hPrc, 0)) // Terminates a process.
|
||||||
|
{
|
||||||
|
CloseHandle(hPrc);
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
WaitForSingleObject(hPrc, 2000); // At most ,waite 2000 millisecond.
|
||||||
|
|
||||||
|
CloseHandle(hPrc);
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOL KillProcessByName(const TCHAR *lpszProcessName)
|
||||||
|
{
|
||||||
|
unsigned int pid = -1;
|
||||||
|
BOOL retval = TRUE;
|
||||||
|
|
||||||
|
if (lpszProcessName == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
DWORD dwRet = 0;
|
||||||
|
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
|
||||||
|
PROCESSENTRY32 processInfo;
|
||||||
|
processInfo.dwSize = sizeof(PROCESSENTRY32);
|
||||||
|
int flag = Process32First(hSnapshot, &processInfo);
|
||||||
|
|
||||||
|
// Find the process with name as same as lpszProcessName
|
||||||
|
while (flag != 0)
|
||||||
|
{
|
||||||
|
printf("kill process find %s\r\n",processInfo.szExeFile);
|
||||||
|
if (strcmp(processInfo.szExeFile, lpszProcessName) == 0)
|
||||||
|
{
|
||||||
|
// Terminate the process.
|
||||||
|
pid = processInfo.th32ProcessID;
|
||||||
|
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, TRUE, pid);
|
||||||
|
printf("kill process pid %d\r\n",pid);
|
||||||
|
if (TerminateProcess(hProcess, 0) != TRUE)
|
||||||
|
{ // Failed to terminate it.
|
||||||
|
retval = FALSE;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
flag = Process32Next(hSnapshot, &processInfo);
|
||||||
|
} // while (flag != 0)
|
||||||
|
CloseHandle(hSnapshot);
|
||||||
|
if (pid == -1)
|
||||||
|
return FALSE;
|
||||||
|
|
||||||
|
return retval;
|
||||||
|
}
|
||||||
|
|
||||||
BOOL SetProcessPrivilege(char *lpName, BOOL opt)
|
BOOL SetProcessPrivilege(char *lpName, BOOL opt)
|
||||||
{
|
{
|
||||||
|
@ -73,9 +140,12 @@ int RangeProcess()
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int test_fork()
|
int test_fork(char *szCommandLine)
|
||||||
{
|
{
|
||||||
char szCommandLine[] = "D:\\game\\The Legend of Zelda Breath of the Wild\\cemu\\cemu.exe";
|
if(nullptr == szCommandLine)
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
STARTUPINFO si = {sizeof(si)};
|
STARTUPINFO si = {sizeof(si)};
|
||||||
PROCESS_INFORMATION pi;
|
PROCESS_INFORMATION pi;
|
||||||
si.dwFlags = STARTF_USESHOWWINDOW; //指定wShowWindow成员有效
|
si.dwFlags = STARTF_USESHOWWINDOW; //指定wShowWindow成员有效
|
||||||
|
@ -111,12 +181,19 @@ int main(int argc, char **argv)
|
||||||
// }catch( std::exception e){
|
// }catch( std::exception e){
|
||||||
// std::cout<<"exception"<<e.what();
|
// std::cout<<"exception"<<e.what();
|
||||||
// }
|
// }
|
||||||
test_fork();
|
// char szCommandLine[] = "Notepad.exe";
|
||||||
while (true)
|
// test_fork(szCommandLine);
|
||||||
{
|
// while (true)
|
||||||
RangeProcess();
|
// {
|
||||||
Sleep(1000);
|
// RangeProcess();
|
||||||
}
|
// Sleep(1000);
|
||||||
|
// KillProcessByName(szCommandLine);
|
||||||
|
|
||||||
|
// }
|
||||||
|
|
||||||
|
DaemonizeMonitor mi("Notepad.exe");
|
||||||
|
mi.AddNewProcess("Notepad.exe");
|
||||||
|
mi.StartMonitor();
|
||||||
|
|
||||||
// std::list<int>
|
// std::list<int>
|
||||||
// i;
|
// i;
|
||||||
|
|
Loading…
Reference in New Issue