process_monitor.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. #include "precompile.h"
  2. #include "process_monitor.h"
  3. #include "list.h"
  4. #include "spinlock.h"
  5. #include "dbgutil.h"
  6. #include <signal.h>
  7. #include <winpr/synch.h>
  8. #include <winpr/thread.h>
  9. #include <winpr/wlog.h>
  10. #define TAG TOOLKIT_TAG("process_monitor")
  11. #define CMD_REMOVE 0
  12. #define CMD_ADD 1
  13. #define CMD_EXIT 2
  14. #define EVT_IDX 0
  15. #define INIT_CNT 1
  16. typedef struct cmd_entry_t
  17. {
  18. struct list_head entry;
  19. int cmd;
  20. HANDLE hproc;
  21. tk_process_t* proc;
  22. HANDLE wait_evt;
  23. int result;
  24. }cmd_entry_t;
  25. typedef struct monitor_t
  26. {
  27. struct list_head entry;
  28. HANDLE thread;
  29. /*[evt, process_handle1, process_handle2,..., process_handleN] */
  30. HANDLE arr_handle[MAXIMUM_WAIT_OBJECTS];
  31. int arr_cnt;
  32. struct list_head cmd_list;
  33. process_monitor_t *parent;
  34. }monitor_t;
  35. struct process_monitor_t
  36. {
  37. struct list_head full_monitor_list;
  38. struct list_head avail_monitor_list;
  39. CRITICAL_SECTION lock;
  40. process_monitor_on_detect_process_end cb;
  41. void *user_data;
  42. };
  43. static void process_monitor_lock(process_monitor_t *pm)
  44. {
  45. EnterCriticalSection(&pm->lock);
  46. }
  47. static void process_monitor_unlock(process_monitor_t *pm)
  48. {
  49. LeaveCriticalSection(&pm->lock);
  50. }
  51. static unsigned int __stdcall monitor_thread_proc(void *arg)
  52. {
  53. monitor_t *monitor = (monitor_t*)arg;
  54. for (;;) {
  55. DWORD dwRet = WaitForSingleObject(monitor->arr_handle[0], 500);
  56. int i;
  57. if (dwRet == WAIT_OBJECT_0) {
  58. cmd_entry_t* cmd = list_first_entry(&monitor->cmd_list, cmd_entry_t, entry);
  59. list_del(&cmd->entry);
  60. if (cmd->cmd == CMD_ADD) {
  61. monitor->arr_handle[monitor->arr_cnt++] = cmd->hproc;
  62. cmd->result = 0;
  63. if (cmd->wait_evt)
  64. SetEvent(cmd->wait_evt);
  65. }
  66. else if (cmd->cmd == CMD_REMOVE) {
  67. int i;
  68. cmd->result = -1;
  69. for (i = 0; i < monitor->arr_cnt; ++i) {
  70. if (monitor->arr_handle[i] == cmd->hproc) {
  71. if (i != monitor->arr_cnt - 1) {
  72. monitor->arr_handle[i] = monitor->arr_handle[monitor->arr_cnt - 1];
  73. }
  74. monitor->arr_cnt--;
  75. cmd->result = 0;
  76. break;
  77. }
  78. }
  79. if (cmd->wait_evt)
  80. SetEvent(cmd->wait_evt);
  81. }
  82. else if (cmd->cmd == CMD_EXIT) {
  83. cmd->result = 0;
  84. if (cmd->wait_evt)
  85. SetEvent(cmd->wait_evt);
  86. break;
  87. }
  88. else {
  89. cmd->result = -1;
  90. if (cmd->wait_evt)
  91. SetEvent(cmd->wait_evt);
  92. }
  93. }
  94. else if (dwRet != WAIT_TIMEOUT) {
  95. WLog_ERR(TAG, "WaitForSingleObject failed!");
  96. abort();
  97. }
  98. for (i = 1; i < monitor->arr_cnt; ++i) {
  99. dwRet = WaitForSingleObject(monitor->arr_handle[i], 0);
  100. if (dwRet == WAIT_OBJECT_0) {
  101. int idx = i;
  102. HANDLE hProc = monitor->arr_handle[idx];
  103. WLog_DBG(TAG, "process exit, execute callback!");
  104. if ((*monitor->parent->cb)(monitor->parent, hProc, monitor->parent->user_data)) {
  105. if (monitor->arr_handle[idx] == hProc) {
  106. if (idx != monitor->arr_cnt - 1)
  107. monitor->arr_handle[idx] = monitor->arr_handle[monitor->arr_cnt - 1];
  108. monitor->arr_cnt--;
  109. }
  110. else {
  111. TOOLKIT_ASSERT(false);
  112. }
  113. }
  114. }
  115. }
  116. }
  117. return 0;
  118. }
  119. TOOLKIT_API int process_monitor_add(process_monitor_t *monitor, tk_process_t* process)
  120. {
  121. cmd_entry_t ce;
  122. monitor_t *m = NULL;
  123. TOOLKIT_ASSERT(monitor);
  124. TOOLKIT_ASSERT(process != NULL);
  125. ce.wait_evt = CreateEventA(NULL, FALSE, FALSE, NULL);
  126. if (!ce.wait_evt)
  127. return -1;
  128. ce.cmd = CMD_ADD;
  129. ce.hproc = process->handle;
  130. process_monitor_lock(monitor);
  131. if (list_empty(&monitor->avail_monitor_list)) {
  132. m = MALLOC_T(monitor_t);
  133. if(m == NULL) {
  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. process_monitor_unlock(monitor);
  145. return -1;
  146. }
  147. m->parent = monitor;
  148. m->thread = (HANDLE)_beginthreadex(NULL, 0, &monitor_thread_proc, m, 0, NULL);
  149. if (!m->thread) {
  150. CloseHandle(m->arr_handle[EVT_IDX]);
  151. free(m);
  152. process_monitor_unlock(monitor);
  153. return -1;
  154. }
  155. INIT_LIST_HEAD(&m->cmd_list);
  156. /*only one elem at current time*/
  157. list_add_tail(&m->entry, &monitor->avail_monitor_list);
  158. } else {
  159. /*get the existed elem*/
  160. m = list_first_entry(&monitor->avail_monitor_list, monitor_t, entry);
  161. }
  162. list_add_tail(&ce.entry, &m->cmd_list);
  163. ReleaseSemaphore(m->arr_handle[EVT_IDX], 1, NULL);
  164. /*wait CMD_ADD cmd result.*/
  165. WaitForSingleObject(ce.wait_evt, INFINITE);
  166. if (m->arr_cnt == MAXIMUM_WAIT_OBJECTS) {
  167. list_del(&m->entry);
  168. list_add_tail(&m->entry, &monitor->full_monitor_list);
  169. }
  170. process_monitor_unlock(monitor);
  171. CloseHandle(ce.wait_evt);
  172. WLog_DBG(TAG, "process monitor add: %d", ce.result);
  173. return ce.result;
  174. }
  175. TOOLKIT_API int process_monitor_remove(process_monitor_t *monitor, tk_process_t* process)
  176. {
  177. cmd_entry_t ce;
  178. TOOLKIT_ASSERT(monitor);
  179. TOOLKIT_ASSERT(process);
  180. ce.wait_evt = CreateEventA(NULL, FALSE, FALSE, NULL);
  181. if (!ce.wait_evt)
  182. return -1;
  183. ce.cmd = CMD_REMOVE;
  184. ce.hproc = process->handle;
  185. ce.result = -1;
  186. process_monitor_lock(monitor);
  187. {
  188. monitor_t *pos;
  189. list_for_each_entry(pos, &monitor->full_monitor_list, monitor_t, entry) {
  190. list_add_tail(&ce.entry, &pos->cmd_list);
  191. ReleaseSemaphore(pos->arr_handle[EVT_IDX], 1, NULL);
  192. WaitForSingleObject(ce.wait_evt, INFINITE);
  193. if (ce.result == 0) {
  194. list_del(&pos->entry);
  195. list_add_tail(&pos->entry, &monitor->avail_monitor_list);
  196. break;
  197. }
  198. }
  199. if (ce.result == -1) {
  200. list_for_each_entry(pos, &monitor->avail_monitor_list, monitor_t, entry) {
  201. list_add_tail(&ce.entry, &pos->cmd_list);
  202. ReleaseSemaphore(pos->arr_handle[EVT_IDX], 1, NULL);
  203. WaitForSingleObject(ce.wait_evt, INFINITE);
  204. if (ce.result == 0) {
  205. break;
  206. }
  207. }
  208. }
  209. }
  210. process_monitor_unlock(monitor);
  211. CloseHandle(ce.wait_evt);
  212. WLog_DBG(TAG, "process monitor remove: %d", ce.result);
  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. static void child_exit_handler(int signo)
  255. {
  256. pid_t pid;
  257. int exit_status;
  258. int saved_errno = errno;
  259. WLog_DBG(TAG, "sigaction received: %d", signo);
  260. while ((pid = waitpid(-1, &exit_status, WNOHANG)) > 0) {
  261. /* do nothing */
  262. }
  263. WLog_DBG(TAG, "sigaction deal %d done!", signo);
  264. errno = saved_errno;
  265. }
  266. TOOLKIT_API int process_monitor_start(process_monitor_t *monitor)
  267. {
  268. #if 1
  269. struct sigaction child_act;
  270. memset(&child_act, 0, sizeof(struct sigaction));
  271. child_act.sa_handler = SIG_DFL;
  272. child_act.sa_flags = SA_NOCLDWAIT;
  273. sigemptyset(&child_act.sa_mask);
  274. if (sigaction(SIGCHLD, &child_act, NULL) == -1) {
  275. WLog_WARN(TAG, "sigaction for sigchld failed: %s(%d)", strerror(errno), errno);
  276. return -1;
  277. }
  278. WLog_DBG(TAG, "sigaction for sigchld succ");
  279. #endif
  280. return 0;
  281. }
  282. TOOLKIT_API int process_monitor_stop(process_monitor_t *monitor)
  283. {
  284. return 0;
  285. }