no message

master
zcy 2022-03-23 20:42:47 +08:00
parent b3a0fd617b
commit b452eae0c7
2 changed files with 3 additions and 106 deletions

View File

@ -1,6 +1,7 @@
{ {
"files.associations": { "files.associations": {
"*.go": "go", "*.go": "go",
"*.cc": "cpp",
"*.rh": "cpp", "*.rh": "cpp",
"iostream": "cpp", "iostream": "cpp",
"list": "cpp", "list": "cpp",
@ -8,7 +9,8 @@
"map": "cpp", "map": "cpp",
"thread": "cpp", "thread": "cpp",
"vector": "cpp", "vector": "cpp",
"xmemory": "cpp" "xmemory": "cpp",
"xtree": "cpp"
}, },
"cmake.buildDirectory": "${workspaceFolder}/build", "cmake.buildDirectory": "${workspaceFolder}/build",
"cmake.mingwSearchDirs": [ "cmake.mingwSearchDirs": [

View File

@ -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" #include "daemon.h"
typedef struct typedef struct
{ {
uint16_t pid; uint16_t pid;
string process_name; string process_name;
} RuningProcess; } RuningProcess;
vector<RuningProcess> RangeProcess(); vector<RuningProcess> RangeProcess();
@ -204,62 +185,40 @@ vector<RuningProcess> RangeProcess()
static BOOL KillProcess(DWORD dwPid) static BOOL KillProcess(DWORD dwPid)
{ {
HANDLE hPrc; HANDLE hPrc;
if (0 == dwPid) if (0 == dwPid)
return FALSE; return FALSE;
hPrc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwPid); // Opens handle to the process. hPrc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwPid); // Opens handle to the process.
if (!TerminateProcess(hPrc, 0)) // Terminates a process. if (!TerminateProcess(hPrc, 0)) // Terminates a process.
{ {
CloseHandle(hPrc); CloseHandle(hPrc);
return FALSE; return FALSE;
} }
else else
WaitForSingleObject(hPrc, 2000); // At most ,waite 2000 millisecond. WaitForSingleObject(hPrc, 2000); // At most ,waite 2000 millisecond.
CloseHandle(hPrc); CloseHandle(hPrc);
return TRUE; return TRUE;
} }
static BOOL KillProcessByName(const TCHAR *lpszProcessName) static BOOL KillProcessByName(const TCHAR *lpszProcessName)
{ {
unsigned int pid = -1; unsigned int pid = -1;
BOOL retval = TRUE; BOOL retval = TRUE;
if (lpszProcessName == NULL) if (lpszProcessName == NULL)
return -1; return -1;
DWORD dwRet = 0; DWORD dwRet = 0;
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
PROCESSENTRY32 processInfo; PROCESSENTRY32 processInfo;
processInfo.dwSize = sizeof(PROCESSENTRY32); processInfo.dwSize = sizeof(PROCESSENTRY32);
int flag = Process32First(hSnapshot, &processInfo); int flag = Process32First(hSnapshot, &processInfo);
// Find the process with name as same as lpszProcessName // Find the process with name as same as lpszProcessName
while (flag != 0) while (flag != 0)
{ {
printf("kill process find %s\r\n", processInfo.szExeFile); printf("kill process find %s\r\n", processInfo.szExeFile);
if (strcmp(processInfo.szExeFile, lpszProcessName) == 0) if (strcmp(processInfo.szExeFile, lpszProcessName) == 0)
{ {
// Terminate the process. // Terminate the process.
@ -273,47 +232,30 @@ static BOOL KillProcessByName(const TCHAR *lpszProcessName)
} }
} }
flag = Process32Next(hSnapshot, &processInfo); flag = Process32Next(hSnapshot, &processInfo);
} // while (flag != 0) } // while (flag != 0)
CloseHandle(hSnapshot); CloseHandle(hSnapshot);
if (pid == -1) if (pid == -1)
return FALSE; return FALSE;
return retval; return retval;
} }
static BOOL SetProcessPrivilege(char *lpName, BOOL opt) static BOOL SetProcessPrivilege(char *lpName, BOOL opt)
{ {
HANDLE tokenhandle; HANDLE tokenhandle;
TOKEN_PRIVILEGES NewState; TOKEN_PRIVILEGES NewState;
if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &tokenhandle)) if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &tokenhandle))
{ {
LookupPrivilegeValue(NULL, lpName, &NewState.Privileges[0].Luid); LookupPrivilegeValue(NULL, lpName, &NewState.Privileges[0].Luid);
NewState.PrivilegeCount = 1; NewState.PrivilegeCount = 1;
NewState.Privileges[0].Attributes = opt != 0 ? 2 : 0; NewState.Privileges[0].Attributes = opt != 0 ? 2 : 0;
AdjustTokenPrivileges(tokenhandle, FALSE, &NewState, sizeof(NewState), NULL, NULL); AdjustTokenPrivileges(tokenhandle, FALSE, &NewState, sizeof(NewState), NULL, NULL);
CloseHandle(tokenhandle); CloseHandle(tokenhandle);
return 1; return 1;
} }
else else
{ {
return 0; return 0;
} }
} }
@ -365,56 +307,32 @@ static BOOL SetProcessPrivilege(char *lpName, BOOL opt)
// } // }
static 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)};
PROCESS_INFORMATION pi; PROCESS_INFORMATION pi;
si.dwFlags = STARTF_USESHOWWINDOW; //指定wShowWindow成员有效 si.dwFlags = STARTF_USESHOWWINDOW; //指定wShowWindow成员有效
si.wShowWindow = TRUE; //此成员设为TRUE的话则显示新建进程的主窗 si.wShowWindow = TRUE; //此成员设为TRUE的话则显示新建进程的主窗
BOOL bRet = CreateProcess( BOOL bRet = CreateProcess(
NULL, //不在此指定可执行文件的文件名 NULL, //不在此指定可执行文件的文件名
szCommandLine, //命令行参 szCommandLine, //命令行参
NULL, //默认进程安全 NULL, //默认进程安全
NULL, //默认进程安全 NULL, //默认进程安全
FALSE, //指定当前进程内句柄不可以被子进程继承 FALSE, //指定当前进程内句柄不可以被子进程继承
CREATE_NEW_CONSOLE, //为新进程创建一个新的控制台窗口 CREATE_NEW_CONSOLE, //为新进程创建一个新的控制台窗口
NULL, //使用本进程的环境变量 NULL, //使用本进程的环境变量
NULL, //使用本进程的驱动器和目录 NULL, //使用本进程的驱动器和目录
&si, &si,
&pi); &pi);
if (bRet) if (bRet)
{ {
//不使用的句柄最好关 //不使用的句柄最好关
CloseHandle(pi.hThread); CloseHandle(pi.hThread);
CloseHandle(pi.hProcess); CloseHandle(pi.hProcess);
printf("new process id %d\n", pi.dwProcessId); printf("new process id %d\n", pi.dwProcessId);
printf("new thread id %d\n", pi.dwThreadId); printf("new thread id %d\n", pi.dwThreadId);
} }
return 0; return 0;
} }
@ -422,19 +340,13 @@ static int test_fork()
DaemonizeMonitor::DaemonizeMonitor(string path) DaemonizeMonitor::DaemonizeMonitor(string path)
{ {
#ifdef linux #ifdef linux
auto process = RangeProcess(); auto process = RangeProcess();
for (auto itr = process.begin(); itr != process.end(); itr++) for (auto itr = process.begin(); itr != process.end(); itr++)
{ {
std::cout << itr->process_name << std::endl; std::cout << itr->process_name << std::endl;
m_pids[itr->process_name.substr(0, itr->process_name.size() - 1)] = itr->pid; m_pids[itr->process_name.substr(0, itr->process_name.size() - 1)] = itr->pid;
} }
#endif #endif
auto strip = StripList(path.c_str(), "/"); auto strip = StripList(path.c_str(), "/");
if(strip.size() > 0){ if(strip.size() > 0){
@ -448,7 +360,6 @@ DaemonizeMonitor::DaemonizeMonitor(string path)
} }
#ifdef linux #ifdef linux
void start_process(string path) void start_process(string path)
{ {
@ -481,10 +392,7 @@ int DaemonizeMonitor::AddNewProcess(string path)
} }
if (m_pids[strip[strip.size() - 1]] > 0) if (m_pids[strip[strip.size() - 1]] > 0)
this->m_running_pid[strip[strip.size() - 1]] = m_pids[strip[strip.size() - 1]]; this->m_running_pid[strip[strip.size() - 1]] = m_pids[strip[strip.size() - 1]];
#endif #endif
#ifdef _WIN32 #ifdef _WIN32
STARTUPINFO si = {sizeof(si)}; STARTUPINFO si = {sizeof(si)};
PROCESS_INFORMATION pi; PROCESS_INFORMATION pi;
@ -520,25 +428,15 @@ int DaemonizeMonitor::StartMonitor()
{ {
#ifdef _WIN32 #ifdef _WIN32
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;
HMODULE hMod[1024]; HMODULE hMod[1024];
HANDLE hProcess; HANDLE hProcess;
char szModName[MAX_PATH]; char szModName[MAX_PATH];
while (true) while (true)
{ {
/* code */ /* code */
Sleep(1000); Sleep(1000);
for (auto itr = m_running_pid.begin(); itr != m_running_pid.end(); itr++) for (auto itr = m_running_pid.begin(); itr != m_running_pid.end(); itr++)
{ {
bool found = false; bool found = false;
@ -563,7 +461,6 @@ int DaemonizeMonitor::StartMonitor()
CloseHandle(hProcess); CloseHandle(hProcess);
} }
SetProcessPrivilege("SeDebugPrivilege", 0); SetProcessPrivilege("SeDebugPrivilege", 0);
if (!found) if (!found)
{ {
// 没找到该应用实例就重启应 // 没找到该应用实例就重启应
@ -591,7 +488,6 @@ int DaemonizeMonitor::StartMonitor()
#ifdef linux #ifdef linux
while (true){ while (true){
auto process = RangeProcess(); auto process = RangeProcess();
usleep(50000); usleep(50000);
for (auto itr = m_running_pid.begin(); itr != m_running_pid.end(); itr++) for (auto itr = m_running_pid.begin(); itr != m_running_pid.end(); itr++)
@ -606,7 +502,6 @@ int DaemonizeMonitor::StartMonitor()
break; break;
} }
} }
if (!found) if (!found)
{ {
// 没找到该应用实例就重启应 // 没找到该应用实例就重启应