process_monitor.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. #include "precompile.h"
  2. #include "process_monitor.h"
  3. #include "list.h"
  4. #include "spinlock.h"
  5. #include "dbgutil.h"
  6. #include <winpr/synch.h>
  7. #include <winpr/thread.h>
  8. #define CMD_REMOVE 0
  9. #define CMD_ADD 1
  10. #define CMD_EXIT 2
  11. #define EVT_IDX 0
  12. #define INIT_CNT 1
  13. typedef struct cmd_entry_t
  14. {
  15. struct list_head entry;
  16. int cmd;
  17. HANDLE hproc;
  18. HANDLE wait_evt;
  19. int result;
  20. }cmd_entry_t;
  21. typedef struct monitor_t
  22. {
  23. struct list_head entry;
  24. HANDLE thread;
  25. /*[evt, process_handle1, process_handle2,..., process_handleN] */
  26. HANDLE arr_handle[MAXIMUM_WAIT_OBJECTS];
  27. int arr_cnt;
  28. struct list_head cmd_list;
  29. process_monitor_t *parent;
  30. }monitor_t;
  31. struct process_monitor_t
  32. {
  33. struct list_head full_monitor_list;
  34. struct list_head avail_monitor_list;
  35. CRITICAL_SECTION lock;
  36. process_monitor_on_detect_process_end cb;
  37. void *user_data;
  38. };
  39. static void process_monitor_lock(process_monitor_t *pm)
  40. {
  41. EnterCriticalSection(&pm->lock);
  42. }
  43. static void process_monitor_unlock(process_monitor_t *pm)
  44. {
  45. LeaveCriticalSection(&pm->lock);
  46. }
  47. static unsigned int __stdcall monitor_thread_proc(void *arg)
  48. {
  49. monitor_t *monitor = (monitor_t*)arg;
  50. for (;;) {
  51. DWORD dwRet = WaitForMultipleObjects(monitor->arr_cnt, monitor->arr_handle, FALSE, INFINITE);
  52. if (dwRet == WAIT_OBJECT_0+EVT_IDX)
  53. {
  54. cmd_entry_t *cmd = list_first_entry(&monitor->cmd_list, cmd_entry_t, entry);
  55. list_del(&cmd->entry);
  56. if (cmd->cmd == CMD_ADD)
  57. {
  58. monitor->arr_handle[monitor->arr_cnt++] = cmd->hproc;
  59. cmd->result = 0;
  60. if (cmd->wait_evt)
  61. SetEvent(cmd->wait_evt);
  62. }
  63. else if (cmd->cmd == CMD_REMOVE)
  64. {
  65. int i;
  66. cmd->result = -1;
  67. for (i = 0; i < monitor->arr_cnt; ++i)
  68. {
  69. if (monitor->arr_handle[i] == cmd->hproc) {
  70. if (i != monitor->arr_cnt-1) {
  71. monitor->arr_handle[i] = monitor->arr_handle[monitor->arr_cnt-1];
  72. }
  73. monitor->arr_cnt--;
  74. cmd->result = 0;
  75. break;
  76. }
  77. }
  78. if (cmd->wait_evt)
  79. SetEvent(cmd->wait_evt);
  80. }
  81. else if (cmd->cmd == CMD_EXIT)
  82. {
  83. cmd->result = 0;
  84. if (cmd->wait_evt)
  85. SetEvent(cmd->wait_evt);
  86. break;
  87. }
  88. else
  89. {
  90. cmd->result = -1;
  91. if (cmd->wait_evt)
  92. SetEvent(cmd->wait_evt);
  93. }
  94. }
  95. else if (dwRet >= WAIT_OBJECT_0 && dwRet < WAIT_OBJECT_0 + monitor->arr_cnt)
  96. {
  97. int idx = dwRet - WAIT_OBJECT_0;
  98. HANDLE hProc = monitor->arr_handle[idx];
  99. if ((*monitor->parent->cb)(monitor->parent, hProc, monitor->parent->user_data)) {
  100. if (monitor->arr_handle[idx] == hProc) {
  101. if (idx != monitor->arr_cnt-1)
  102. monitor->arr_handle[idx] = monitor->arr_handle[monitor->arr_cnt-1];
  103. monitor->arr_cnt --;
  104. }
  105. else {
  106. TOOLKIT_ASSERT(0);
  107. }
  108. }
  109. }
  110. else
  111. {
  112. // TOOLKIT_ASSERT(0);
  113. // return -1;
  114. }
  115. }
  116. return 0;
  117. }
  118. TOOLKIT_API int process_monitor_add(process_monitor_t *monitor, tk_process_t* process)
  119. {
  120. cmd_entry_t ce;
  121. monitor_t *m = NULL;
  122. TOOLKIT_ASSERT(monitor);
  123. TOOLKIT_ASSERT(process != NULL);
  124. ce.wait_evt = CreateEventA(NULL, FALSE, FALSE, NULL);
  125. if (!ce.wait_evt)
  126. return -1;
  127. ce.cmd = CMD_ADD;
  128. ce.hproc = process->handle;
  129. process_monitor_lock(monitor);
  130. if (list_empty(&monitor->avail_monitor_list)) {
  131. m = MALLOC_T(monitor_t);
  132. if(m == NULL) {
  133. CloseHandle(ce.wait_evt);
  134. return -1;
  135. }
  136. m->arr_cnt = INIT_CNT;
  137. /*
  138. *The state of a semaphore is signaled when its count is greater than zero
  139. *the count is increased by a specified amount by calling ReleaseSemaphore.
  140. */
  141. m->arr_handle[EVT_IDX] = CreateSemaphoreA(NULL, 0, 0x7fffffff, NULL);
  142. if (!m->arr_handle[EVT_IDX]) {
  143. free(m);
  144. CloseHandle(ce.wait_evt);
  145. process_monitor_unlock(monitor);
  146. return -1;
  147. }
  148. m->parent = monitor;
  149. m->thread = (HANDLE)_beginthreadex(NULL, 0, &monitor_thread_proc, m, 0, NULL);
  150. if (!m->thread) {
  151. CloseHandle(m->arr_handle[EVT_IDX]);
  152. CloseHandle(ce.wait_evt);
  153. free(m);
  154. process_monitor_unlock(monitor);
  155. return -1;
  156. }
  157. INIT_LIST_HEAD(&m->cmd_list);
  158. /*only one elem at current time*/
  159. list_add_tail(&m->entry, &monitor->avail_monitor_list);
  160. } else {
  161. /*get the existed elem*/
  162. m = list_first_entry(&monitor->avail_monitor_list, monitor_t, entry);
  163. }
  164. list_add_tail(&ce.entry, &m->cmd_list);
  165. ReleaseSemaphore(m->arr_handle[EVT_IDX], 1, NULL);
  166. /*wait CMD_ADD cmd result.*/
  167. WaitForSingleObject(ce.wait_evt, INFINITE);
  168. if (m->arr_cnt == MAXIMUM_WAIT_OBJECTS) {
  169. list_del(&m->entry);
  170. list_add_tail(&m->entry, &monitor->full_monitor_list);
  171. }
  172. process_monitor_unlock(monitor);
  173. CloseHandle(ce.wait_evt);
  174. return ce.result;
  175. }
  176. TOOLKIT_API int process_monitor_remove(process_monitor_t *monitor, tk_process_t* process)
  177. {
  178. cmd_entry_t ce;
  179. TOOLKIT_ASSERT(monitor);
  180. TOOLKIT_ASSERT(process);
  181. ce.wait_evt = CreateEventA(NULL, FALSE, FALSE, NULL);
  182. if (!ce.wait_evt)
  183. return -1;
  184. ce.cmd = CMD_REMOVE;
  185. ce.hproc = process->handle;
  186. ce.result = -1;
  187. process_monitor_lock(monitor);
  188. {
  189. monitor_t *pos;
  190. list_for_each_entry(pos, &monitor->full_monitor_list, monitor_t, entry) {
  191. list_add_tail(&ce.entry, &pos->cmd_list);
  192. ReleaseSemaphore(pos->arr_handle[EVT_IDX], 1, NULL);
  193. WaitForSingleObject(ce.wait_evt, INFINITE);
  194. if (ce.result == 0) {
  195. list_del(&pos->entry);
  196. list_add_tail(&pos->entry, &monitor->avail_monitor_list);
  197. break;
  198. }
  199. }
  200. if (ce.result == -1) {
  201. list_for_each_entry(pos, &monitor->avail_monitor_list, monitor_t, entry) {
  202. list_add_tail(&ce.entry, &pos->cmd_list);
  203. ReleaseSemaphore(pos->arr_handle[EVT_IDX], 1, NULL);
  204. WaitForSingleObject(ce.wait_evt, INFINITE);
  205. if (ce.result == 0) {
  206. break;
  207. }
  208. }
  209. }
  210. }
  211. process_monitor_unlock(monitor);
  212. CloseHandle(ce.wait_evt);
  213. return ce.result;
  214. }
  215. TOOLKIT_API int process_monitor_create(process_monitor_t **p_monitor)
  216. {
  217. process_monitor_t *pm = MALLOC_T(process_monitor_t);
  218. INIT_LIST_HEAD(&pm->full_monitor_list);
  219. INIT_LIST_HEAD(&pm->avail_monitor_list);
  220. InitializeCriticalSection(&pm->lock);
  221. pm->cb = NULL;
  222. *p_monitor = pm;
  223. return 0;
  224. }
  225. TOOLKIT_API int process_monitor_destroy(process_monitor_t *monitor)
  226. {
  227. int i;
  228. struct list_head *lists[] = {&monitor->avail_monitor_list, &monitor->full_monitor_list};
  229. for (i = 0; i < array_size(lists); ++i) {
  230. monitor_t *pos, *n;
  231. list_for_each_entry_safe(pos, n, lists[i], monitor_t, entry) {
  232. cmd_entry_t ce;
  233. ce.cmd = CMD_EXIT;
  234. ce.wait_evt = NULL;
  235. list_add_tail(&ce.entry, &pos->cmd_list);
  236. ReleaseSemaphore(pos->arr_handle[EVT_IDX], 1, NULL);
  237. WaitForSingleObject(pos->thread, INFINITE);
  238. list_del(&pos->entry);
  239. CloseHandle(pos->thread);
  240. CloseHandle(pos->arr_handle[EVT_IDX]);
  241. free(pos);
  242. }
  243. }
  244. DeleteCriticalSection(&monitor->lock);
  245. free(monitor);
  246. return 0;
  247. }
  248. TOOLKIT_API int process_monitor_set_cb(process_monitor_t *monitor, process_monitor_on_detect_process_end cb, void *user_data)
  249. {
  250. monitor->cb = cb;
  251. monitor->user_data = user_data;
  252. return 0;
  253. }
  254. TOOLKIT_API int process_monitor_stop(process_monitor_t *monitor)
  255. {
  256. return 0;
  257. }