From bb2bffc10af741b56986570a834c96cad7c6fdfb Mon Sep 17 00:00:00 2001 From: zcy <290198252@qq.com> Date: Fri, 13 Aug 2021 00:29:17 +0800 Subject: [PATCH] no message --- general/src/function/daemon.cpp | 728 +++++++++++++++++++++++++------- general/src/function/daemon.h | 69 ++- 2 files changed, 649 insertions(+), 148 deletions(-) diff --git a/general/src/function/daemon.cpp b/general/src/function/daemon.cpp index 305a2c8..806c420 100644 --- a/general/src/function/daemon.cpp +++ b/general/src/function/daemon.cpp @@ -1,439 +1,881 @@ /* + * @Author: your name + * @Date: 2021-07-23 23:39:57 - * @LastEditTime: 2021-08-12 00:34:35 + + * @LastEditTime: 2021-08-13 00:28:07 + * @LastEditors: Please set LastEditors + * @Description: In User Settings Edit + * @FilePath: \generallib\general\src\function\daemon.c + */ + #include "daemon.h" -#ifdef linux -void _process_start(string path){ - auto file = popen(path.c_str(),"r"); - if(file < 0){ - fprintf(stderr,"execl failed:%s",strerror(errno)); - return; - } - pclose(file); -} -vector StripList(const char *in,const char* d){ - vector ret; - vector pos; - vector pos_strip_continue; - int lastpos = -1; - if((nullptr == in)|| (nullptr == d)){ - return ret; - } - int len = strlen(in); - int sublen = strlen(d); - - if( len <= 0) - return ret; - if(sublen <= 0) - return ret; - - for(int i = 0;i < (len - strlen(d)) ; i++){ - bool found = true; - for(int j = 0;j < strlen(d);j++){ - if(in[i + j] != d[j]){ - found = false; - } - } - if(found){ - i += sublen - 1; - pos.push_back(i); - continue; - } - } - for(int i = 0; i < pos.size() - 1;i++){ - if(pos[i + 1] != (pos[i] + sublen)){ - pos_strip_continue.push_back(pos[i] + 1); - pos_strip_continue.push_back(pos[i + 1] - 1); - } - } - if(pos_strip_continue[pos_strip_continue.size() - 1] != (strlen(in) - 1)){ - pos_strip_continue.push_back(pos_strip_continue[pos_strip_continue.size() - 1] + 2); - pos_strip_continue.push_back(strlen(in) - 1); - } - for(int i = 0;i < pos_strip_continue.size()/2;i++){ - string ins = string(&in[pos_strip_continue[i*2]],&(in[pos_strip_continue[i*2 + 1]]) + 1); - ret.push_back(ins); - } - return ret; -} typedef struct{ + uint16_t pid; + string process_name; + }RuningProcess; + +vector RangeProcess(); + + + +#ifdef linux + +void _process_start(string path){ + + auto file = popen(path.c_str(),"r"); + + if(file < 0){ + + fprintf(stderr,"execl failed:%s",strerror(errno)); + + return; + + } + + pclose(file); + +} + + + + + +inline bool existed (const std::string& name) { + + return ( access( name.c_str(), F_OK ) != -1 ); + +} + + + +vector StripList(const char *in,const char* d){ + + vector ret; + + vector pos; + + vector pos_strip_continue; + + + + int lastpos = -1; + + if((nullptr == in)|| (nullptr == d)){ + + return ret; + + } + + int len = strlen(in); + + int sublen = strlen(d); + + + + if( len <= 0) + + return ret; + + if(sublen <= 0) + + return ret; + + + + for(int i = 0;i < (len - strlen(d)) ; i++){ + + bool found = true; + + for(int j = 0;j < strlen(d);j++){ + + if(in[i + j] != d[j]){ + + found = false; + + } + + } + + if(found){ + + i += sublen - 1; + + pos.push_back(i); + + continue; + + } + + } + + for(int i = 0; i < pos.size() - 1;i++){ + + if(pos[i + 1] != (pos[i] + sublen)){ + + pos_strip_continue.push_back(pos[i] + 1); + + pos_strip_continue.push_back(pos[i + 1] - 1); + + } + + } + + if(pos_strip_continue[pos_strip_continue.size() - 1] != (strlen(in) - 1)){ + + pos_strip_continue.push_back(pos_strip_continue[pos_strip_continue.size() - 1] + 2); + + pos_strip_continue.push_back(strlen(in) - 1); + + } + + for(int i = 0;i < pos_strip_continue.size()/2;i++){ + + string ins = string(&in[pos_strip_continue[i*2]],&(in[pos_strip_continue[i*2 + 1]]) + 1); + + ret.push_back(ins); + + } + + return ret; + +} + + + + + vector RangeProcess(){ + FILE *pstr; + char cmd[128],buff[512],*p; + vector ret; + int iPID; + int pidPosition = 1; + int pInfoPosition = 7; + + memset(cmd,0,sizeof(cmd)); + sprintf(cmd, "ps "); + pstr = popen(cmd, "r"); + + if(pstr==NULL) { + return ret; + } + memset(buff,0,sizeof(buff)); + bool first = true; + while( 1 ) { + RuningProcess ins; + fgets(buff,512,pstr); + if(first){ + first = false; + continue; + } + if(feof(pstr)) + { + break; + } + auto trip = StripList(buff," "); + ins.pid = atoi(trip[0].c_str()); + ins.process_name = trip[3]; + ret.push_back(ins); + } + pclose(pstr); + return ret; + } + + #endif + + #ifdef _WIN32 + + 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; + TOKEN_PRIVILEGES NewState; + + if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &tokenhandle)) + { + LookupPrivilegeValue(NULL, lpName, &NewState.Privileges[0].Luid); + NewState.PrivilegeCount = 1; + NewState.Privileges[0].Attributes = opt != 0 ? 2 : 0; + AdjustTokenPrivileges(tokenhandle, FALSE, &NewState, sizeof(NewState), NULL, NULL); + CloseHandle(tokenhandle); + return 1; + } + else + { + return 0; + } + } + + static int RangeProcess() + { + DWORD Proc_pid[1024], Retn_bytes, Proc_count, Retn_bytes2; + unsigned int i; + HMODULE hMod[1024]; + HANDLE hProcess; + char szModName[MAX_PATH]; + 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); + } + CloseHandle(hProcess); + } + SetProcessPrivilege("SeDebugPrivilege", 0); + } + return 0; + } + + static int test_fork() + { + char szCommandLine[] = "D:\\game\\The Legend of Zelda Breath of the Wild\\cemu\\cemu.exe"; + STARTUPINFO si = {sizeof(si)}; + PROCESS_INFORMATION pi; + si.dwFlags = STARTF_USESHOWWINDOW; //指定wShowWindow成员有效 - si.wShowWindow = TRUE; //此成员设为TRUE的话则显示新建进程的主窗�? + + si.wShowWindow = TRUE; //此成员设为TRUE的话则显示新建进程的主窗 + BOOL bRet = CreateProcess( + NULL, //不在此指定可执行文件的文件名 - szCommandLine, //命令行参�? - NULL, //默认进程安全�? - NULL, //默认进程安全�? + + szCommandLine, //命令行参 + + 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); + printf("new thread id %d\n", pi.dwThreadId); + } + return 0; + } + #endif + + DaemonizeMonitor::DaemonizeMonitor(string path){ + this->m_path_process.push_back(path); + + } + + +void start_process(string path) + +{ + + std::thread p(_process_start,path); + + p.detach(); + +} + + + int DaemonizeMonitor::AddNewProcess(string path){ + this->m_path_process.push_back(path); -#ifdef _WIN32 - 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; + + if(path ==""){ + + return -1; + } - return -1; + +#ifdef linux + + if(!existed(path)){ + + return -1; + + } + + start_process(path); #endif + + + +#ifdef _WIN32 + + 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; + + + +#endif + } + + int DaemonizeMonitor::StartMonitor(){ + #ifdef _WIN32 + 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){ - // 没找到该应用实例就重启应�? + + // 没找到该应用实例就重启应 + auto it = m_path_process.begin(); + while (it != m_path_process.end()) + { + if (*it == itr->first) + { + it = m_path_process.erase(it); + } + + else + it++; + } + for(auto it = m_path_process.begin();it != m_path_process.end();it++){ + std::cout<<*it<AddNewProcess(itr->first); + } + } + } + } + return 0; + #endif #ifdef linux - + + while (true) + + { + + auto process = RangeProcess(); + + usleep(50000); + + for(auto itr = m_running_pid.begin();itr != m_running_pid.end();itr++){ + + bool found = false; + + for(auto itr2 = process.begin();itr2 != process.end();itr2++){ + + if(itr->second == itr2->pid){ + + std::cout<second<pid<first) + + { + + it = m_path_process.erase(it); + + } + + + + else + + it++; + + } + + for(auto it = m_path_process.begin();it != m_path_process.end();it++){ + + std::cout<<*it< this->AddNewProcess(itr->first)) + + std::cout<<"error not found"<= 201103L + #pragma message("编译器支持c++11") + #endif + + + +#ifdef linux + +#include + +#endif + #include + #include + #include + using namespace std; -extern "C"{ -#include -#include -} + + + + + + + +#ifdef WIN32 + #include + #include + #include + +#endif + #include + #include + #include + #include + #include + +#include + +#include + +#include + + + + + class DaemonizeMonitor{ + + public: + DaemonizeMonitor(string path); + int AddNewProcess(string); + int StopMonitor(string); + int StopProcess(string); + int StartMonitor(); + private: + std::vector m_path_process; + std::map m_running_pid; + }; \ No newline at end of file