qdevice-heuristics-worker.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*
  2. * Copyright (c) 2015-2017 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Jan Friesse (jfriesse@redhat.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the Red Hat, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <limits.h>
  35. #include <errno.h>
  36. #include <poll.h>
  37. #include <signal.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <unistd.h>
  41. #include "dynar-str.h"
  42. #include "qdevice-config.h"
  43. #include "qdevice-heuristics-io.h"
  44. #include "qdevice-heuristics-worker.h"
  45. #include "qdevice-heuristics-worker-instance.h"
  46. #include "qdevice-heuristics-worker-log.h"
  47. #include "qdevice-heuristics-worker-cmd.h"
  48. /*
  49. * Declarations
  50. */
  51. static int qdevice_heuristics_worker_kill_list_timer_callback(void *data1,
  52. void *data2);
  53. static void qdevice_heuristics_worker_process_list_notify(
  54. enum process_list_notify_reason reason, const struct process_list_entry *entry,
  55. void *user_data);
  56. static void qdevice_heuristics_worker_signal_handlers_register(void);
  57. /*
  58. * Definitions
  59. */
  60. static void
  61. qdevice_heuristics_worker_process_list_notify(enum process_list_notify_reason reason,
  62. const struct process_list_entry *entry, void *user_data)
  63. {
  64. struct qdevice_heuristics_worker_instance *instance;
  65. instance = (struct qdevice_heuristics_worker_instance *)user_data;
  66. switch (reason) {
  67. case PROCESS_LIST_NOTIFY_REASON_EXECUTED:
  68. qdevice_heuristics_worker_log_printf(instance, LOG_DEBUG,
  69. "process %s executed", entry->name);
  70. break;
  71. case PROCESS_LIST_NOTIFY_REASON_FINISHED:
  72. if (!WIFEXITED(entry->exit_status) || WEXITSTATUS(entry->exit_status) != 0) {
  73. if (WIFEXITED(entry->exit_status)) {
  74. qdevice_heuristics_worker_log_printf(instance, LOG_WARNING,
  75. "process %s finished with status %d", entry->name,
  76. WEXITSTATUS(entry->exit_status));
  77. } else if (WIFSIGNALED(entry->exit_status)) {
  78. qdevice_heuristics_worker_log_printf(instance, LOG_WARNING,
  79. "process %s killed by signal %d", entry->name,
  80. WTERMSIG(entry->exit_status));
  81. } else {
  82. qdevice_heuristics_worker_log_printf(instance, LOG_WARNING,
  83. "process %s finished with non zero status", entry->name);
  84. }
  85. } else {
  86. qdevice_heuristics_worker_log_printf(instance, LOG_DEBUG,
  87. "process %s successfully finished", entry->name);
  88. }
  89. break;
  90. }
  91. }
  92. static void
  93. qdevice_heuristics_worker_signal_handlers_register(void)
  94. {
  95. struct sigaction act;
  96. act.sa_handler = SIG_DFL;
  97. sigemptyset(&act.sa_mask);
  98. act.sa_flags = SA_RESTART;
  99. sigaction(SIGCHLD, &act, NULL);
  100. act.sa_handler = SIG_IGN;
  101. sigemptyset(&act.sa_mask);
  102. act.sa_flags = SA_RESTART;
  103. sigaction(SIGPIPE, &act, NULL);
  104. act.sa_handler = SIG_IGN;
  105. sigemptyset(&act.sa_mask);
  106. act.sa_flags = SA_RESTART;
  107. sigaction(SIGINT, &act, NULL);
  108. }
  109. static int
  110. qdevice_heuristics_worker_kill_list_timer_callback(void *data1, void *data2)
  111. {
  112. struct qdevice_heuristics_worker_instance *instance;
  113. size_t kill_list_size;
  114. instance = (struct qdevice_heuristics_worker_instance *)data1;
  115. if (process_list_process_kill_list(&instance->main_process_list) != 0) {
  116. qdevice_heuristics_worker_log_printf(instance, LOG_CRIT,
  117. "qdevice_heuristics_worker_kill_list_timer_callback: process kill list failed. "
  118. "Shutting down worker");
  119. instance->schedule_exit = 1;
  120. return (0);
  121. }
  122. kill_list_size = process_list_get_kill_list_items(&instance->main_process_list);
  123. if (kill_list_size > 0) {
  124. qdevice_heuristics_worker_log_printf(instance, LOG_DEBUG,
  125. "Still waiting for %zu processes exit", kill_list_size);
  126. }
  127. /*
  128. * Schedule this timer again
  129. */
  130. return (-1);
  131. }
  132. int
  133. qdevice_heuristics_worker_exec_timeout_timer_callback(void *data1, void *data2)
  134. {
  135. struct qdevice_heuristics_worker_instance *instance;
  136. instance = (struct qdevice_heuristics_worker_instance *)data1;
  137. qdevice_heuristics_worker_log_printf(instance, LOG_WARNING,
  138. "Not all heuristics execs finished on time");
  139. process_list_move_active_entries_to_kill_list(&instance->main_process_list);
  140. instance->exec_timeout_timer = NULL;
  141. if (qdevice_heuristics_worker_cmd_write_exec_result(instance, instance->last_exec_seq_number,
  142. QDEVICE_HEURISTICS_EXEC_RESULT_FAIL) != 0) {
  143. instance->schedule_exit = 1;
  144. return (0);
  145. }
  146. return (0);
  147. }
  148. static int
  149. qdevice_heuristics_worker_poll(struct qdevice_heuristics_worker_instance *instance)
  150. {
  151. int poll_res;
  152. struct pollfd poll_input_fd;
  153. uint32_t timeout;
  154. int plist_summary;
  155. /*
  156. * Poll command input
  157. */
  158. poll_input_fd.fd = QDEVICE_HEURISTICS_WORKER_CMD_IN_FD;
  159. poll_input_fd.events = POLLIN;
  160. poll_input_fd.revents = 0;
  161. timeout = timer_list_time_to_expire_ms(&instance->main_timer_list);
  162. if (timeout > QDEVICE_MIN_HEURISTICS_TIMEOUT) {
  163. timeout = QDEVICE_MIN_HEURISTICS_TIMEOUT;
  164. }
  165. if ((poll_res = poll(&poll_input_fd, 1, timeout)) >= 0) {
  166. if (poll_input_fd.revents & POLLIN) {
  167. /*
  168. * POLLIN
  169. */
  170. if (qdevice_heuristics_worker_cmd_read_from_pipe(instance) != 0) {
  171. return (-1);
  172. }
  173. }
  174. if (poll_input_fd.revents & POLLOUT) {
  175. /*
  176. * Pollout shouldn't happen (critical error)
  177. */
  178. qdevice_heuristics_worker_log_printf(instance, LOG_CRIT,
  179. "qdevice_heuristics_worker_poll: POLLOUT set. Shutting down worker");
  180. return (-1);
  181. }
  182. if (poll_input_fd.revents & (POLLERR|POLLHUP|POLLNVAL) &&
  183. !(poll_input_fd.revents & (POLLIN|POLLOUT))) {
  184. /*
  185. * Qdevice closed pipe
  186. */
  187. return (-1);
  188. }
  189. }
  190. if (process_list_waitpid(&instance->main_process_list) != 0) {
  191. qdevice_heuristics_worker_log_printf(instance, LOG_CRIT,
  192. "qdevice_heuristics_worker_poll: Waitpid failed. Shutting down worker");
  193. return (-1);
  194. }
  195. if (instance->exec_timeout_timer != NULL) {
  196. plist_summary = process_list_get_summary_result_short(&instance->main_process_list);
  197. switch (plist_summary) {
  198. case -1:
  199. /*
  200. * Processes not finished -> continue
  201. */
  202. break;
  203. case 0:
  204. /*
  205. * All processes finished successfully
  206. */
  207. if (qdevice_heuristics_worker_cmd_write_exec_result(instance,
  208. instance->last_exec_seq_number, QDEVICE_HEURISTICS_EXEC_RESULT_PASS) != 0) {
  209. return (-1);
  210. }
  211. process_list_move_active_entries_to_kill_list(&instance->main_process_list);
  212. timer_list_delete(&instance->main_timer_list, instance->exec_timeout_timer);
  213. instance->exec_timeout_timer = NULL;
  214. break;
  215. case 1:
  216. /*
  217. * Some processes failed
  218. */
  219. if (qdevice_heuristics_worker_cmd_write_exec_result(instance,
  220. instance->last_exec_seq_number, QDEVICE_HEURISTICS_EXEC_RESULT_FAIL) != 0) {
  221. return (-1);
  222. }
  223. process_list_move_active_entries_to_kill_list(&instance->main_process_list);
  224. timer_list_delete(&instance->main_timer_list, instance->exec_timeout_timer);
  225. instance->exec_timeout_timer = NULL;
  226. break;
  227. default:
  228. qdevice_heuristics_worker_log_printf(instance, LOG_CRIT,
  229. "qdevice_heuristics_worker_poll: Unhandled "
  230. "process_list_get_summary_result. Shutting down worker");
  231. return (-1);
  232. break;
  233. }
  234. }
  235. timer_list_expire(&instance->main_timer_list);
  236. if (instance->schedule_exit) {
  237. return (-1);
  238. }
  239. return (0);
  240. }
  241. void
  242. qdevice_heuristics_worker_start(size_t ipc_max_send_receive_size, int use_execvp,
  243. size_t max_processes, uint32_t kill_list_interval)
  244. {
  245. struct qdevice_heuristics_worker_instance instance;
  246. memset(&instance, 0, sizeof(instance));
  247. instance.schedule_exit = 0;
  248. dynar_init(&instance.cmd_in_buffer, ipc_max_send_receive_size);
  249. dynar_init(&instance.cmd_out_buffer, ipc_max_send_receive_size);
  250. dynar_init(&instance.log_out_buffer, ipc_max_send_receive_size);
  251. process_list_init(&instance.main_process_list, max_processes, use_execvp,
  252. qdevice_heuristics_worker_process_list_notify, (void *)&instance);
  253. timer_list_init(&instance.main_timer_list);
  254. instance.kill_list_timer = timer_list_add(&instance.main_timer_list,
  255. kill_list_interval, qdevice_heuristics_worker_kill_list_timer_callback,
  256. (void *)&instance, NULL);
  257. if (instance.kill_list_timer == NULL) {
  258. qdevice_heuristics_worker_log_printf(&instance, LOG_CRIT,
  259. "Can't create kill list timer");
  260. return ;
  261. }
  262. instance.exec_timeout_timer = NULL;
  263. qdevice_heuristics_exec_list_init(&instance.exec_list);
  264. qdevice_heuristics_worker_signal_handlers_register();
  265. qdevice_heuristics_worker_log_printf(&instance, LOG_DEBUG, "Heuristic worker initialized");
  266. while (qdevice_heuristics_worker_poll(&instance) == 0) {
  267. }
  268. qdevice_heuristics_worker_log_printf(&instance, LOG_DEBUG, "Heuristic worker shutdown "
  269. "requested");
  270. qdevice_heuristics_exec_list_free(&instance.exec_list);
  271. timer_list_free(&instance.main_timer_list);
  272. qdevice_heuristics_worker_log_printf(&instance, LOG_DEBUG,
  273. "Waiting for all processes to exit");
  274. if (process_list_killall(&instance.main_process_list, kill_list_interval) != 0) {
  275. qdevice_heuristics_worker_log_printf(&instance, LOG_WARNING,
  276. "Not all process exited");
  277. }
  278. process_list_free(&instance.main_process_list);
  279. dynar_destroy(&instance.cmd_in_buffer);
  280. dynar_destroy(&instance.cmd_out_buffer);
  281. dynar_destroy(&instance.log_out_buffer);
  282. }