sp_runTask.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #include "precompile.h"
  2. #include "sp_runTask.h"
  3. #include <string>
  4. #include <vector>
  5. #include "sp_dir.h"
  6. #include <winpr/synch.h>
  7. #include <winpr/file.h>
  8. #if defined(_MSC_VER)
  9. #include <Windows.h>
  10. #include <TlHelp32.h>
  11. #endif //_MSC_VER
  12. #include <SpBase.h>
  13. #include "fileutil.h"
  14. #include "sp_cfg.h"
  15. extern std::vector<std::string> getKillArr();
  16. extern std::vector<std::string> getStartArr();
  17. #if defined(RVC_OS_WIN)
  18. int RunProc(const char* csPath)
  19. {
  20. STARTUPINFO si;
  21. PROCESS_INFORMATION pi;
  22. ZeroMemory(&si, sizeof(si));
  23. si.cb = sizeof(si);
  24. si.lpReserved = NULL;
  25. si.lpDesktop = NULL;
  26. si.lpTitle = NULL;
  27. si.dwFlags = STARTF_USESHOWWINDOW;
  28. si.wShowWindow = SW_HIDE;
  29. si.cbReserved2 = NULL;
  30. si.lpReserved2 = NULL;
  31. ZeroMemory(&pi, sizeof(pi));
  32. if (!CreateProcess(NULL, (LPSTR)csPath, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
  33. {
  34. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("CreateProcess failed (%d).\n", GetLastError());
  35. return -1;
  36. }
  37. WaitForSingleObject(pi.hProcess, INFINITE);
  38. // Close process and thread handles.
  39. CloseHandle(pi.hProcess);
  40. CloseHandle(pi.hThread);
  41. return 0;
  42. }
  43. HANDLE FindProcessByName(const char* pProcessName)
  44. {
  45. PROCESSENTRY32 pe32;
  46. pe32.dwSize = sizeof(pe32);
  47. HANDLE hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  48. BOOL bMore = ::Process32First(hProcessSnap, &pe32);
  49. while (bMore)
  50. {
  51. if (stricmp(pProcessName, pe32.szExeFile) == 0)
  52. {
  53. return OpenProcess(PROCESS_TERMINATE, FALSE, pe32.th32ProcessID);
  54. }
  55. bMore = ::Process32Next(hProcessSnap, &pe32);
  56. }
  57. return NULL;
  58. }
  59. int sp_runtask_startprocess()
  60. {
  61. auto startArr = getStartArr();
  62. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("sp_runtask_killprocess::getStartArr num %d", startArr.size());
  63. for each (auto it in startArr) {
  64. if (!ExistsFileA(it.c_str())) {
  65. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("%s not exist", it.c_str());
  66. return 0;
  67. }
  68. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("run %s", it.c_str());
  69. WinExec(it.c_str(), SW_HIDE);
  70. }
  71. return 0;
  72. }
  73. #endif //RVC_OS_WIN
  74. int sp_runtask_killprocess()
  75. {
  76. auto killArr = getKillArr();
  77. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("sp_runtask_killprocess::getKillArr num %d", killArr.size());
  78. #if defined(RVC_OS_WIN)
  79. for each (auto it in killArr) {
  80. char cmdStr[MAX_PATH] = "";
  81. sprintf(cmdStr, "taskkill /f /im %s", it.c_str());
  82. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("run %s", cmdStr);
  83. WinExec(cmdStr, SW_HIDE);
  84. int i = 0;
  85. for (i = 0; i < 20; i++) {
  86. if (NULL == FindProcessByName(it.c_str()))
  87. break;
  88. Sleep(100);
  89. }
  90. if (i == 20) {
  91. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("kill %s failed", it.c_str());
  92. }
  93. }
  94. #else
  95. for (auto it = killArr.begin(); it != killArr.end(); ++it) {
  96. char cmdStr[MAX_PATH] = "";
  97. sprintf(cmdStr, "sudo killall -9 %s", it->c_str());
  98. system(cmdStr);
  99. }
  100. #endif //RVC_OS_WIN
  101. if (killArr.size() > 0)
  102. Sleep(100);
  103. return 0;
  104. }
  105. void sp_tryquickStartCef()
  106. {
  107. #if defined(RVC_OS_WIN)
  108. char tmp[MAX_PATH];
  109. GetModuleFileNameA(NULL, tmp, MAX_PATH);
  110. *strrchr(tmp, SPLIT_SLASH) = 0;
  111. sprintf(tmp, "%s" SPLIT_SLASH_STR "Chromium" SPLIT_SLASH_STR "cefclient.exe", tmp);
  112. if (!ExistsFile(tmp))
  113. {
  114. std::string cmd = tmp;
  115. cmd.append(" --listpeers");
  116. WinExec(cmd.c_str(), SW_HIDE);
  117. }
  118. #endif
  119. }
  120. int sp_runtask_loadLogLevel()
  121. {
  122. sp_tryRefreshLogLevelFromCacheConfig();
  123. return 0;
  124. }