sp_groupProcess.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. #include "precompile.h"
  2. #include "sp_groupProcess.h"
  3. #include <map>
  4. #include <string>
  5. #ifdef _WIN32
  6. #include <tlhelp32.h> //CreateToolhelp32Snapshot
  7. #include <objbase.h>
  8. #endif //_WIN32
  9. #include "toolkit.h"
  10. #include <winpr/synch.h>
  11. #include <winpr/string.h>
  12. #include <winpr/wlog.h>
  13. #define TAG SPBASE_TAG("sp_groupProcess")
  14. using namespace std;
  15. #define DEFAULT_WAITTIME 5000
  16. #define OPERATE_SUCCESS "success"
  17. #define OPERATE_FAIL "fail"
  18. #define GUID_LEN 64
  19. typedef enum {
  20. PIPE_PROCESS_START,
  21. PIPE_ENTITY_KILL,
  22. PIPE_PROCESS_QUERY,
  23. PIPE_PROCESS_KILL
  24. }PIPE_PROCESS_DO;
  25. map<string, sp_process_t*> mapProcess;
  26. class groupLock
  27. {
  28. public:
  29. groupLock() {
  30. static bool isInit = false;
  31. if (!isInit)
  32. {
  33. InitializeCriticalSection(&cs_);
  34. isInit = true;
  35. }
  36. EnterCriticalSection(&cs_);
  37. }
  38. ~groupLock() {
  39. LeaveCriticalSection(&cs_);
  40. }
  41. private:
  42. groupLock(const groupLock&);
  43. groupLock& operator =(const groupLock&);
  44. private:
  45. static CRITICAL_SECTION cs_;
  46. };
  47. CRITICAL_SECTION groupLock::cs_;
  48. int paramSplit(char* srcStr, char dstParam[10][MAX_PATH])
  49. {
  50. char *split = " ", *p = NULL;
  51. int i = 0;
  52. if (NULL == srcStr || NULL == dstParam)
  53. return -1;
  54. p = strtok(srcStr, split);
  55. for (i = 0; p != NULL; i++)
  56. {
  57. strcpy(&(dstParam[i][0]), p);
  58. p = strtok(NULL,split);
  59. }
  60. return i;
  61. }
  62. int getNewGuid(char *guidBuffer)
  63. {
  64. GUID guid;
  65. if (CoCreateGuid(&guid))
  66. return -1;
  67. _snprintf(guidBuffer, GUID_LEN,
  68. "%08X-%04X-%04x-%02X%02X-%02X%02X%02X%02X%02X%02X",
  69. guid.Data1, guid.Data2, guid.Data3,
  70. guid.Data4[0], guid.Data4[1], guid.Data4[2],
  71. guid.Data4[3], guid.Data4[4], guid.Data4[5],
  72. guid.Data4[6], guid.Data4[7]);
  73. return 0;
  74. }
  75. /**return str with "group0_{mod_name}" or "group{group_id} if group is not zero or entityName is null"**/
  76. string makeGroupMapName(int group, const char *entityName)
  77. {
  78. char tempGroupName[50] = "";
  79. if (0 == group && NULL != entityName)
  80. sprintf_s(tempGroupName, sizeof(tempGroupName), "group%d_%s", group, entityName);
  81. else
  82. sprintf_s(tempGroupName, sizeof(tempGroupName), "group%d", group);
  83. return string(tempGroupName);
  84. }
  85. sp_process_t* findGroupProcessInfo(int group, const char *entityName)
  86. {
  87. groupLock curLock;
  88. map<string, sp_process_t*>::iterator iter;
  89. iter = mapProcess.find(makeGroupMapName(group, entityName));
  90. if (iter != mapProcess.end())
  91. return iter->second;
  92. else
  93. return NULL;
  94. }
  95. int checkGroupProcesInfo(int group, const char *entityName)
  96. {
  97. sp_process_t* curProcess = findGroupProcessInfo(group, entityName);
  98. if (NULL == curProcess)
  99. return -1;
  100. if (process_exist_or_not(curProcess->pid))
  101. {
  102. char szCmd[256];
  103. sprintf_s(szCmd, 256, "TASKKILL /PID %d /F", curProcess->pid);
  104. WinExec(szCmd, SW_HIDE);
  105. RemoveGroupProcessInfo(group, entityName);
  106. return 0;
  107. }
  108. else
  109. {
  110. RemoveGroupProcessInfo(group, entityName);
  111. return -1;
  112. }
  113. }
  114. int AddGroupProcessInfo(int group, sp_process_t* curProcess, const char *entityName)
  115. {
  116. if (findGroupProcessInfo(group, entityName))
  117. return -1;
  118. groupLock curLock;
  119. mapProcess.insert(map<string, sp_process_t*>::value_type(makeGroupMapName(group, entityName), curProcess));
  120. return 0;
  121. }
  122. int RemoveGroupProcessInfo(int group, const char *entityName)
  123. {
  124. sp_process_t *curProcess = findGroupProcessInfo(group, entityName);
  125. groupLock curLock;
  126. if (!curProcess)
  127. return -1;
  128. if (curProcess->read_pipe)
  129. CloseHandle(curProcess->read_pipe);
  130. if (curProcess->process_Handle)
  131. CloseHandle(curProcess->process_Handle);
  132. mapProcess.erase(makeGroupMapName(group, entityName));
  133. free(curProcess);
  134. return 0;
  135. }
  136. int writePipe(const sp_process_t* curProcess, PIPE_PROCESS_DO code, const char *paramStr)
  137. {
  138. if (NULL == curProcess || NULL == curProcess->read_pipe || NULL == curProcess->write_pipe )
  139. return -1;
  140. char writeBuf[1024] = "";
  141. DWORD byteWrite = 0;
  142. switch(code)
  143. {
  144. case PIPE_PROCESS_START:
  145. sprintf_s(writeBuf, 1024, "start %s", paramStr);
  146. break;
  147. case PIPE_ENTITY_KILL:
  148. sprintf_s(writeBuf, 1024, "kill %s", paramStr);
  149. break;
  150. case PIPE_PROCESS_QUERY:
  151. sprintf_s(writeBuf, 1024, "query %s", paramStr);
  152. break;
  153. case PIPE_PROCESS_KILL:
  154. sprintf_s(writeBuf, 1024, "kill process");
  155. break;
  156. default:
  157. return -1;
  158. }
  159. OVERLAPPED overlap = { 0 };
  160. overlap.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  161. if (!WriteFile(curProcess->write_pipe, (LPCVOID)writeBuf, strlen(writeBuf) + 1, &byteWrite, &overlap)
  162. || byteWrite != strlen(writeBuf) + 1)
  163. return -1;
  164. if (WAIT_OBJECT_0 == WaitForSingleObject(overlap.hEvent, 5000)) {
  165. CloseHandle(overlap.hEvent);
  166. return 0;
  167. } else
  168. return -1;
  169. return 0;
  170. }
  171. int readPipe(const sp_process_t *curProcess, char *dstStr, DWORD waitTime)
  172. {
  173. if (NULL == dstStr || NULL == curProcess)
  174. return -1;
  175. DWORD byteRead = 0;
  176. OVERLAPPED overlap = { 0 };
  177. overlap.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  178. UCHAR buffer[100] = { 0 };
  179. DWORD dwRead = 0;
  180. ReadFile(curProcess->read_pipe, dstStr, MAX_PATH, &byteRead, &overlap);
  181. if (WAIT_OBJECT_0 == WaitForSingleObject(overlap.hEvent, waitTime))
  182. {
  183. CloseHandle(overlap.hEvent);
  184. return 0;
  185. }
  186. return -1;
  187. }
  188. int startModByPipe(const sp_process_t* curProcess, char *paramStr)
  189. {//do writePipe and readPipe
  190. if (NULL == curProcess || NULL == paramStr)
  191. return -1;
  192. char dstStr[MAX_PATH] = "";
  193. int result = 0;
  194. do
  195. {
  196. result = writePipe(curProcess, PIPE_PROCESS_START, paramStr);
  197. if (0 != result) {
  198. WLog_ERR(TAG, "writePipe failed! %d", result);
  199. break;
  200. }
  201. result = readPipe(curProcess, dstStr, DEFAULT_WAITTIME);
  202. if (0 != result) {
  203. WLog_ERR(TAG, "readPipe failed! %d", result);
  204. break;
  205. }
  206. if (!!strcmp(dstStr, OPERATE_SUCCESS)) {
  207. WLog_ERR(TAG, "readPipe returned: %s", dstStr);
  208. result = -1;
  209. break;
  210. }
  211. } while (false);
  212. return result;
  213. }
  214. #define PROCESS_WAITTIME 5000
  215. int killModByPipe(const sp_process_t *curProcess, char *paramStr)
  216. {
  217. if (NULL == curProcess || NULL == paramStr)
  218. return -1;
  219. groupLock curLock;
  220. char dstStr[MAX_PATH] = "";
  221. if (0 < curProcess->group)
  222. {
  223. if (0 == writePipe(curProcess, PIPE_ENTITY_KILL, paramStr)
  224. && 0 == readPipe(curProcess, dstStr, DEFAULT_WAITTIME)
  225. && !strcmp(dstStr, OPERATE_SUCCESS))
  226. return 0;
  227. }
  228. else if (0 == curProcess->group)
  229. {
  230. if (0 == writePipe(curProcess, PIPE_PROCESS_KILL, paramStr)
  231. &&
  232. WAIT_TIMEOUT != WaitForSingleObject(curProcess->process_Handle, PROCESS_WAITTIME))
  233. {
  234. RemoveGroupProcessInfo(curProcess->group, paramStr);
  235. return 0;
  236. }
  237. }
  238. return -1;
  239. }
  240. int queryModByPipe(const sp_process_t* curProcess, const char *paramStr, char dstParam[10][MAX_PATH])
  241. {
  242. if (NULL == curProcess || NULL == paramStr)
  243. return -1;
  244. char dstStr[MAX_PATH] = "";
  245. if (0 == writePipe(curProcess, PIPE_PROCESS_QUERY, paramStr)
  246. && 0 == readPipe(curProcess, dstStr, DEFAULT_WAITTIME))
  247. {
  248. int paramNum = paramSplit(dstStr, dstParam);
  249. if (-1 != paramNum && !strcmp(dstParam[0], OPERATE_SUCCESS))
  250. return paramNum;
  251. }
  252. return -1;
  253. }
  254. void ClearGroupProcessInfo()
  255. {
  256. auto it = mapProcess.begin();
  257. while (it != mapProcess.end())
  258. {
  259. sp_process_t* t = it->second;
  260. if (t != NULL) {
  261. free(t);
  262. it->second = NULL;
  263. }
  264. mapProcess.erase(it);
  265. it = mapProcess.begin();
  266. }
  267. }