sp_groupProcess.cpp 6.4 KB

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