123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- #include "precompile.h"
- #include "sp_runTask.h"
- #include <string>
- #include <vector>
- #include "sp_dir.h"
- #include <winpr/synch.h>
- #include <winpr/file.h>
- #if defined(_MSC_VER)
- #include <Windows.h>
- #include <TlHelp32.h>
- #endif //_MSC_VER
- #include <SpBase.h>
- #include "fileutil.h"
- #include "sp_cfg.h"
- extern std::vector<std::string> getKillArr();
- extern std::vector<std::string> getStartArr();
- #if defined(RVC_OS_WIN)
- int RunProc(const char* csPath)
- {
- STARTUPINFO si;
- PROCESS_INFORMATION pi;
- ZeroMemory(&si, sizeof(si));
- si.cb = sizeof(si);
- si.lpReserved = NULL;
- si.lpDesktop = NULL;
- si.lpTitle = NULL;
- si.dwFlags = STARTF_USESHOWWINDOW;
- si.wShowWindow = SW_HIDE;
- si.cbReserved2 = NULL;
- si.lpReserved2 = NULL;
- ZeroMemory(&pi, sizeof(pi));
- if (!CreateProcess(NULL, (LPSTR)csPath, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
- {
- DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("CreateProcess failed (%d).\n", GetLastError());
- return -1;
- }
- WaitForSingleObject(pi.hProcess, INFINITE);
- // Close process and thread handles.
- CloseHandle(pi.hProcess);
- CloseHandle(pi.hThread);
- return 0;
- }
- HANDLE FindProcessByName(const char* pProcessName)
- {
- PROCESSENTRY32 pe32;
- pe32.dwSize = sizeof(pe32);
- HANDLE hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
- BOOL bMore = ::Process32First(hProcessSnap, &pe32);
- while (bMore)
- {
- if (stricmp(pProcessName, pe32.szExeFile) == 0)
- {
- return OpenProcess(PROCESS_TERMINATE, FALSE, pe32.th32ProcessID);
- }
- bMore = ::Process32Next(hProcessSnap, &pe32);
- }
- return NULL;
- }
- int sp_runtask_startprocess()
- {
- auto startArr = getStartArr();
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("sp_runtask_killprocess::getStartArr num %d", startArr.size());
- for each (auto it in startArr) {
- if (!ExistsFileA(it.c_str())) {
- DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("%s not exist", it.c_str());
- return 0;
- }
- DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("run %s", it.c_str());
- WinExec(it.c_str(), SW_HIDE);
- }
- return 0;
- }
- #endif //RVC_OS_WIN
- int sp_runtask_killprocess()
- {
- auto killArr = getKillArr();
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("sp_runtask_killprocess::getKillArr num %d", killArr.size());
- #if defined(RVC_OS_WIN)
- for each (auto it in killArr) {
- char cmdStr[MAX_PATH] = "";
- sprintf(cmdStr, "taskkill /f /im %s", it.c_str());
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("run %s", cmdStr);
- WinExec(cmdStr, SW_HIDE);
- int i = 0;
- for (i = 0; i < 20; i++) {
- if (NULL == FindProcessByName(it.c_str()))
- break;
- Sleep(100);
- }
- if (i == 20) {
- DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("kill %s failed", it.c_str());
- }
- }
- #else
- for (auto it = killArr.begin(); it != killArr.end(); ++it) {
- char cmdStr[MAX_PATH] = "";
- sprintf(cmdStr, "sudo killall -9 %s", it->c_str());
- system(cmdStr);
- }
- #endif //RVC_OS_WIN
- if (killArr.size() > 0)
- Sleep(100);
- return 0;
- }
- void sp_tryquickStartCef()
- {
- #if defined(RVC_OS_WIN)
- char tmp[MAX_PATH];
- GetModuleFileNameA(NULL, tmp, MAX_PATH);
- *strrchr(tmp, SPLIT_SLASH) = 0;
- sprintf(tmp, "%s" SPLIT_SLASH_STR "Chromium" SPLIT_SLASH_STR "cefclient.exe", tmp);
- if (!ExistsFile(tmp))
- {
- std::string cmd = tmp;
- cmd.append(" --listpeers");
- WinExec(cmd.c_str(), SW_HIDE);
- }
- #endif
- }
- int sp_runtask_loadLogLevel()
- {
- sp_tryRefreshLogLevelFromCacheConfig();
- return 0;
- }
|