diff --git a/.vscode/settings.json b/.vscode/settings.json index be6b8cb..9f6bb22 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,7 @@ { "files.associations": { "*.go": "go", + "*.cc": "cpp", "*.rh": "cpp", "iostream": "cpp", "list": "cpp", @@ -8,7 +9,8 @@ "map": "cpp", "thread": "cpp", "vector": "cpp", - "xmemory": "cpp" + "xmemory": "cpp", + "xtree": "cpp" }, "cmake.buildDirectory": "${workspaceFolder}/build", "cmake.mingwSearchDirs": [ diff --git a/general/src/function/daemon.cpp b/general/src/function/daemon.cpp index defd2cb..aa44c2e 100644 --- a/general/src/function/daemon.cpp +++ b/general/src/function/daemon.cpp @@ -1,28 +1,9 @@ -/* - - * @Author: your name - - * @Date: 2021-07-23 23:39:57 - - * @LastEditTime: 2021-10-06 23:42:13 - - * @LastEditors: Please set LastEditors - - * @Description: In User Settings Edit - - * @FilePath: \generallib\general\src\function\daemon.c - - */ - #include "daemon.h" typedef struct { - uint16_t pid; - string process_name; - } RuningProcess; vector RangeProcess(); @@ -204,62 +185,40 @@ vector RangeProcess() 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. @@ -273,47 +232,30 @@ static BOOL KillProcessByName(const TCHAR *lpszProcessName) } } 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; } } @@ -365,56 +307,32 @@ static BOOL SetProcessPrivilege(char *lpName, BOOL opt) // } 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的话则显示新建进程的主窗 - BOOL bRet = CreateProcess( - 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; } @@ -422,19 +340,13 @@ static int test_fork() DaemonizeMonitor::DaemonizeMonitor(string path) { - #ifdef linux - auto process = RangeProcess(); - for (auto itr = process.begin(); itr != process.end(); itr++) { - std::cout << itr->process_name << std::endl; - m_pids[itr->process_name.substr(0, itr->process_name.size() - 1)] = itr->pid; } - #endif auto strip = StripList(path.c_str(), "/"); if(strip.size() > 0){ @@ -448,7 +360,6 @@ DaemonizeMonitor::DaemonizeMonitor(string path) } #ifdef linux - void start_process(string path) { @@ -481,10 +392,7 @@ int DaemonizeMonitor::AddNewProcess(string path) } if (m_pids[strip[strip.size() - 1]] > 0) this->m_running_pid[strip[strip.size() - 1]] = m_pids[strip[strip.size() - 1]]; - #endif - - #ifdef _WIN32 STARTUPINFO si = {sizeof(si)}; PROCESS_INFORMATION pi; @@ -520,25 +428,15 @@ 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; @@ -563,7 +461,6 @@ int DaemonizeMonitor::StartMonitor() CloseHandle(hProcess); } SetProcessPrivilege("SeDebugPrivilege", 0); - if (!found) { // 没找到该应用实例就重启应 @@ -591,7 +488,6 @@ int DaemonizeMonitor::StartMonitor() #ifdef linux while (true){ - auto process = RangeProcess(); usleep(50000); for (auto itr = m_running_pid.begin(); itr != m_running_pid.end(); itr++) @@ -606,7 +502,6 @@ int DaemonizeMonitor::StartMonitor() break; } } - if (!found) { // 没找到该应用实例就重启应