no message

master
zcy 2021-08-17 00:41:26 +08:00
parent bb2bffc10a
commit 1555afea31
1 changed files with 262 additions and 358 deletions

View File

@ -4,7 +4,7 @@
* @Date: 2021-07-23 23:39:57
* @LastEditTime: 2021-08-13 00:28:07
* @LastEditTime: 2021-08-17 00:39:53
* @LastEditors: Please set LastEditors
@ -16,250 +16,217 @@
#include "daemon.h"
typedef struct
{
uint16_t pid;
typedef struct{
uint16_t pid;
string process_name;
}RuningProcess;
string process_name;
} RuningProcess;
vector<RuningProcess> RangeProcess();
vector<string> StripList(const char *in, const char *d)
{
vector<string> ret;
vector<uint16_t> pos;
vector<uint16_t> 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;
}
}
if (pos.size() == 0)
{
ret.push_back(string(in));
return ret;
}
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;
}
inline bool existed(const std::string &name)
{
return (access(name.c_str(), F_OK) != -1);
}
#ifdef linux
void _process_start(string path){
void _process_start(string path)
{
auto file = popen(path.c_str(),"r");
auto file = popen(path.c_str(), "r");
if(file < 0){
if (file < 0)
{
fprintf(stderr,"execl failed:%s",strerror(errno));
fprintf(stderr, "execl failed:%s", strerror(errno));
return;
}
pclose(file);
return;
}
pclose(file);
}
vector<RuningProcess> RangeProcess()
{
FILE *pstr;
char cmd[128], buff[512], *p;
vector<RuningProcess> ret;
inline bool existed (const std::string& name) {
int iPID;
return ( access( name.c_str(), F_OK ) != -1 );
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;
}
vector<string> StripList(const char *in,const char* d){
vector<string> ret;
vector<uint16_t> pos;
vector<uint16_t> 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<RuningProcess> RangeProcess(){
FILE *pstr;
char cmd[128],buff[512],*p;
vector<RuningProcess> 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.
{
@ -267,23 +234,17 @@ static BOOL KillProcess(DWORD dwPid)
CloseHandle(hPrc);
return FALSE;
}
else
WaitForSingleObject(hPrc, 2000); // At most ,waite 2000 millisecond.
CloseHandle(hPrc);
return TRUE;
}
static BOOL KillProcessByName(const TCHAR *lpszProcessName)
{
@ -292,14 +253,10 @@ static BOOL KillProcessByName(const TCHAR *lpszProcessName)
BOOL retval = TRUE;
if (lpszProcessName == NULL)
return -1;
DWORD dwRet = 0;
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
@ -310,15 +267,13 @@ static BOOL KillProcessByName(const TCHAR *lpszProcessName)
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);
printf("kill process find %s\r\n", processInfo.szExeFile);
if (strcmp(processInfo.szExeFile, lpszProcessName) == 0)
@ -330,7 +285,7 @@ static BOOL KillProcessByName(const TCHAR *lpszProcessName)
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, TRUE, pid);
printf("kill process pid %d\r\n",pid);
printf("kill process pid %d\r\n", pid);
if (TerminateProcess(hProcess, 0) != TRUE)
@ -339,13 +294,9 @@ static BOOL KillProcessByName(const TCHAR *lpszProcessName)
retval = FALSE;
break;
}
}
flag = Process32Next(hSnapshot, &processInfo);
} // while (flag != 0)
@ -356,16 +307,9 @@ static BOOL KillProcessByName(const TCHAR *lpszProcessName)
return FALSE;
return retval;
}
static BOOL SetProcessPrivilege(char *lpName, BOOL opt)
{
@ -374,8 +318,6 @@ static BOOL SetProcessPrivilege(char *lpName, BOOL opt)
TOKEN_PRIVILEGES NewState;
if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &tokenhandle))
{
@ -391,7 +333,6 @@ static BOOL SetProcessPrivilege(char *lpName, BOOL opt)
CloseHandle(tokenhandle);
return 1;
}
else
@ -399,17 +340,11 @@ static BOOL SetProcessPrivilege(char *lpName, BOOL opt)
{
return 0;
}
}
static int RangeProcess()
{
DWORD Proc_pid[1024], Retn_bytes, Proc_count, Retn_bytes2;
unsigned int i;
@ -443,23 +378,17 @@ static int RangeProcess()
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()
{
@ -472,25 +401,25 @@ static int test_fork()
si.dwFlags = STARTF_USESHOWWINDOW; //指定wShowWindow成员有效
si.wShowWindow = TRUE; //此成员设为TRUE的话则显示新建进程的主窗
si.wShowWindow = TRUE; //此成员设为TRUE的话则显示新建进程的主窗
BOOL bRet = CreateProcess(
NULL, //不在此指定可执行文件的文件名
NULL, //不在此指定可执行文件的文件名
szCommandLine, //命令行参
szCommandLine, //命令行参
NULL, //默认进程安全
NULL, //默认进程安全
NULL, //默认进程安全
NULL, //默认进程安全
FALSE, //指定当前进程内句柄不可以被子进程继承
FALSE, //指定当前进程内句柄不可以被子进程继承
CREATE_NEW_CONSOLE, //为新进程创建一个新的控制台窗口
NULL, //使用本进程的环境变量
NULL, //使用本进程的环境变量
NULL, //使用本进程的驱动器和目录
NULL, //使用本进程的驱动器和目录
&si,
@ -509,63 +438,69 @@ static int test_fork()
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)
DaemonizeMonitor::DaemonizeMonitor(string path)
{
std::thread p(_process_start,path);
#ifdef linux
p.detach();
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(), "/");
this->m_path_process.push_back(path);
}
#ifdef linux
void start_process(string path)
{
int DaemonizeMonitor::AddNewProcess(string path){
std::thread p(_process_start, path);
p.detach();
}
#endif
int DaemonizeMonitor::AddNewProcess(string path)
{
this->m_path_process.push_back(path);
if(path ==""){
if (path == "")
{
return -1;
}
#ifdef linux
if(!existed(path)){
if (!existed(path))
{
return -1;
}
start_process(path);
#endif
#ifdef _WIN32
STARTUPINFO si = {sizeof(si)};
@ -574,25 +509,25 @@ int DaemonizeMonitor::AddNewProcess(string path){
si.dwFlags = STARTF_USESHOWWINDOW; //指定wShowWindow成员有效
si.wShowWindow = TRUE; //此成员设为TRUE的话则显示新建进程的主窗
si.wShowWindow = TRUE; //此成员设为TRUE的话则显示新建进程的主窗
BOOL bRet = CreateProcess(
NULL, //不在此指定可执行文件的文件名
NULL, //不在此指定可执行文件的文件名
(LPSTR)path.c_str(), //命令行参
(LPSTR)path.c_str(), //命令行参
NULL, //默认进程安全
NULL, //默认进程安全
NULL, //默认进程安全
NULL, //默认进程安全
FALSE, //指定当前进程内句柄不可以被子进程继承
FALSE, //指定当前进程内句柄不可以被子进程继承
CREATE_NEW_CONSOLE, //为新进程创建一个新的控制台窗口
NULL, //使用本进程的环境变量
NULL, //使用本进程的环境变量
NULL, //使用本进程的驱动器和目录
NULL, //使用本进程的驱动器和目录
&si,
@ -608,27 +543,20 @@ int DaemonizeMonitor::AddNewProcess(string path){
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(){
int DaemonizeMonitor::StartMonitor()
{
#ifdef _WIN32
@ -650,7 +578,8 @@ int DaemonizeMonitor::StartMonitor(){
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;
@ -678,23 +607,22 @@ int DaemonizeMonitor::StartMonitor(){
// printf("PID=%d Path=%s\n", Proc_pid[i], szModName);
if(itr->second == Proc_pid[i]){
if (itr->second == Proc_pid[i])
{
found = true;
break;
}
}
CloseHandle(hProcess);
}
SetProcessPrivilege("SeDebugPrivilege", 0);
if(!found){
if (!found)
{
// 没找到该应用实例就重启应
@ -709,31 +637,23 @@ int DaemonizeMonitor::StartMonitor(){
{
it = m_path_process.erase(it);
}
else
it++;
}
for(auto it = m_path_process.begin();it != m_path_process.end();it++){
std::cout<<*it<<std::endl;
for (auto it = m_path_process.begin(); it != m_path_process.end(); it++)
{
std::cout << *it << std::endl;
}
this->AddNewProcess(itr->first);
}
}
}
}
return 0;
@ -748,25 +668,28 @@ int DaemonizeMonitor::StartMonitor(){
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++)
{
bool found = false;
for(auto itr2 = process.begin();itr2 != process.end();itr2++){
for (auto itr2 = process.begin(); itr2 != process.end(); itr2++)
{
if(itr->second == itr2->pid){
if (itr->second == itr2->pid)
{
std::cout<<itr->second<<itr2->pid<<std::endl;
std::cout << itr->second << itr2->pid << std::endl;
found = true;
break;
}
if(!found){
if (!found)
{
// 没找到该应用实例就重启应
@ -781,101 +704,82 @@ int DaemonizeMonitor::StartMonitor(){
{
it = m_path_process.erase(it);
}
else
it++;
}
for(auto it = m_path_process.begin();it != m_path_process.end();it++){
std::cout<<*it<<std::endl;
for (auto it = m_path_process.begin(); it != m_path_process.end(); it++)
{
std::cout << *it << std::endl;
}
std::cout<<"add new process"<<std::endl;
std::cout << "add new process" << std::endl;
if( 0 > this->AddNewProcess(itr->first))
std::cout<<"error not found"<<std::endl;
if (0 > this->AddNewProcess(itr->first))
std::cout << "error not found" << std::endl;
}
}
}
}
#endif
}
int DaemonizeMonitor::StopMonitor(string){
int DaemonizeMonitor::StopMonitor(string)
{
#ifdef _WIN32
return 0;
#endif
#endif
}
int DaemonizeMonitor::StopProcess(string path)
{
int DaemonizeMonitor::StopProcess(string path){
if(path ==""){
if (path == "")
{
return -1;
}
if(!existed(path)){
if (!existed(path))
{
return -1;
}
#ifdef _WIN32
if(m_running_pid.find(path) != m_running_pid.end()){
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))
if (KillProcess(pid))
return 0;
else{
return -1;
else
{
return -1;
}
}
return 0;
#endif
#ifdef linux
#endif
}