4
0

qdevice-heuristics.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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 <sys/types.h>
  35. #include <sys/wait.h>
  36. #include <err.h>
  37. #include <poll.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <unistd.h>
  41. #include "qdevice-log.h"
  42. #include "qdevice-heuristics.h"
  43. #include "qdevice-heuristics-cmd.h"
  44. #include "qdevice-heuristics-worker.h"
  45. #include "qdevice-heuristics-io.h"
  46. #include "qdevice-votequorum.h"
  47. #include "utils.h"
  48. #define QDEVICE_HEURISTICS_WAIT_FOR_INITIAL_EXEC_RESULT_MAX_PFDS 5
  49. void
  50. qdevice_heuristics_init(struct qdevice_heuristics_instance *instance,
  51. struct qdevice_advanced_settings *advanced_settings)
  52. {
  53. int pipe_cmd_in[2], pipe_cmd_out[2], pipe_log_out[2];
  54. pid_t pid;
  55. if (pipe(pipe_cmd_in) != 0) {
  56. err(1, "Can't create command input pipe");
  57. }
  58. if (pipe(pipe_cmd_out) != 0) {
  59. err(1, "Can't create command output pipe");
  60. }
  61. if (pipe(pipe_log_out) != 0) {
  62. err(1, "Can't create logging output pipe");
  63. }
  64. pid = fork();
  65. if (pid == -1) {
  66. err(1, "Can't create child process");
  67. } else if (pid == 0) {
  68. /*
  69. * Child
  70. */
  71. (void)setsid();
  72. if (dup2(pipe_cmd_in[0], 0) == -1) {
  73. err(1, "Can't dup2 command input pipe");
  74. }
  75. close(pipe_cmd_in[1]);
  76. close(pipe_cmd_in[0]);
  77. if (utils_fd_set_non_blocking(0) == -1) {
  78. err(1, "Can't set non blocking flag on command input pipe");
  79. }
  80. if (dup2(pipe_cmd_out[1], 1) == -1) {
  81. err(1, "Can't dup2 command output pipe");
  82. }
  83. close(pipe_cmd_out[0]);
  84. close(pipe_cmd_out[1]);
  85. if (dup2(pipe_log_out[1], 2) == -1) {
  86. err(1, "Can't dup2 logging output pipe");
  87. }
  88. close(pipe_log_out[0]);
  89. close(pipe_log_out[1]);
  90. qdevice_heuristics_worker_start(advanced_settings->heuristics_ipc_max_send_receive_size,
  91. advanced_settings->heuristics_use_execvp, advanced_settings->heuristics_max_processes,
  92. advanced_settings->heuristics_kill_list_interval);
  93. qdevice_advanced_settings_destroy(advanced_settings);
  94. exit(0);
  95. } else {
  96. close(pipe_cmd_in[0]);
  97. close(pipe_cmd_out[1]);
  98. close(pipe_log_out[1]);
  99. qdevice_heuristics_instance_init(instance);
  100. instance->pipe_cmd_send = pipe_cmd_in[1];
  101. if (utils_fd_set_non_blocking(instance->pipe_cmd_send) == -1) {
  102. err(1, "Can't set non blocking flag on command input pipe");
  103. }
  104. instance->pipe_cmd_recv = pipe_cmd_out[0];
  105. if (utils_fd_set_non_blocking(instance->pipe_cmd_recv) == -1) {
  106. err(1, "Can't set non blocking flag on command output pipe");
  107. }
  108. instance->pipe_log_recv = pipe_log_out[0];
  109. if (utils_fd_set_non_blocking(instance->pipe_cmd_recv) == -1) {
  110. err(1, "Can't set non blocking flag on logging output pipe");
  111. }
  112. instance->worker_pid = pid;
  113. send_buffer_list_init(&instance->cmd_out_buffer_list,
  114. advanced_settings->heuristics_ipc_max_send_buffers,
  115. advanced_settings->heuristics_ipc_max_send_receive_size);
  116. dynar_init(&instance->log_in_buffer,
  117. advanced_settings->heuristics_ipc_max_send_receive_size);
  118. dynar_init(&instance->cmd_in_buffer,
  119. advanced_settings->heuristics_ipc_max_send_receive_size);
  120. }
  121. }
  122. void
  123. qdevice_heuristics_destroy(struct qdevice_heuristics_instance *instance)
  124. {
  125. int status;
  126. /*
  127. * Close of pipe_cmd_send result is correct and almost instant exit of worker
  128. */
  129. close(instance->pipe_cmd_send);
  130. qdevice_log(LOG_DEBUG, "Waiting for heuristics worker to finish");
  131. if (waitpid(instance->worker_pid, &status, 0) == -1) {
  132. qdevice_log_err(LOG_ERR, "Heuristics worker waitpid failed");
  133. } else {
  134. /*
  135. * Log what left in worker log buffer. Errors can be ignored
  136. */
  137. (void)qdevice_heuristics_log_read_from_pipe(instance);
  138. }
  139. close(instance->pipe_cmd_recv);
  140. close(instance->pipe_log_recv);
  141. dynar_destroy(&instance->log_in_buffer);
  142. dynar_destroy(&instance->cmd_in_buffer);
  143. send_buffer_list_free(&instance->cmd_out_buffer_list);
  144. qdevice_heuristics_instance_destroy(instance);
  145. }
  146. int
  147. qdevice_heuristics_exec(struct qdevice_heuristics_instance *instance, int sync_in_progress)
  148. {
  149. uint32_t timeout;
  150. instance->expected_reply_seq_number++;
  151. instance->waiting_for_result = 1;
  152. if (sync_in_progress) {
  153. timeout = instance->sync_timeout;
  154. } else {
  155. timeout = instance->timeout;
  156. }
  157. return (qdevice_heuristics_cmd_write_exec(instance, timeout,
  158. instance->expected_reply_seq_number));
  159. }
  160. int
  161. qdevice_heuristics_waiting_for_result(const struct qdevice_heuristics_instance *instance)
  162. {
  163. return (instance->waiting_for_result);
  164. }
  165. int
  166. qdevice_heuristics_change_exec_list(struct qdevice_heuristics_instance *instance,
  167. const struct qdevice_heuristics_exec_list *new_exec_list, int sync_in_progress)
  168. {
  169. if (qdevice_heuristics_cmd_write_exec_list(instance, new_exec_list) != 0) {
  170. return (-1);
  171. }
  172. qdevice_heuristics_exec_list_free(&instance->exec_list);
  173. if (new_exec_list != NULL) {
  174. if (qdevice_heuristics_exec_list_clone(&instance->exec_list, new_exec_list) != 0) {
  175. qdevice_log(LOG_ERR, "Can't clone exec list");
  176. return (-1);
  177. }
  178. }
  179. if (qdevice_heuristics_waiting_for_result(instance)) {
  180. if (qdevice_heuristics_exec(instance, sync_in_progress) != 0) {
  181. qdevice_log(LOG_ERR, "Can't execute heuristics");
  182. return (-1);
  183. }
  184. }
  185. return (0);
  186. }
  187. int
  188. qdevice_heuristics_wait_for_initial_exec_result(struct qdevice_heuristics_instance *instance)
  189. {
  190. struct pollfd pfds[QDEVICE_HEURISTICS_WAIT_FOR_INITIAL_EXEC_RESULT_MAX_PFDS];
  191. int no_pfds;
  192. int poll_res;
  193. int timeout;
  194. int i;
  195. int case_processed;
  196. int res;
  197. while (!instance->qdevice_instance_ptr->vq_node_list_initial_heuristics_finished) {
  198. no_pfds = 0;
  199. assert(no_pfds < QDEVICE_HEURISTICS_WAIT_FOR_INITIAL_EXEC_RESULT_MAX_PFDS);
  200. pfds[no_pfds].fd = instance->pipe_log_recv;
  201. pfds[no_pfds].events = POLLIN;
  202. pfds[no_pfds].revents = 0;
  203. no_pfds++;
  204. assert(no_pfds < QDEVICE_HEURISTICS_WAIT_FOR_INITIAL_EXEC_RESULT_MAX_PFDS);
  205. pfds[no_pfds].fd = instance->pipe_cmd_recv;
  206. pfds[no_pfds].events = POLLIN;
  207. pfds[no_pfds].revents = 0;
  208. no_pfds++;
  209. assert(no_pfds < QDEVICE_HEURISTICS_WAIT_FOR_INITIAL_EXEC_RESULT_MAX_PFDS);
  210. pfds[no_pfds].fd = instance->qdevice_instance_ptr->votequorum_poll_fd;
  211. pfds[no_pfds].events = POLLIN;
  212. pfds[no_pfds].revents = 0;
  213. no_pfds++;
  214. if (!send_buffer_list_empty(&instance->cmd_out_buffer_list)) {
  215. assert(no_pfds < QDEVICE_HEURISTICS_WAIT_FOR_INITIAL_EXEC_RESULT_MAX_PFDS);
  216. pfds[no_pfds].fd = instance->pipe_cmd_send;
  217. pfds[no_pfds].events = POLLOUT;
  218. pfds[no_pfds].revents = 0;
  219. no_pfds++;
  220. }
  221. /*
  222. * We know this is never larger than QDEVICE_DEFAULT_HEURISTICS_MAX_TIMEOUT * 2
  223. */
  224. timeout = (int)instance->sync_timeout * 2;
  225. poll_res = poll(pfds, no_pfds, timeout);
  226. if (poll_res > 0) {
  227. for (i = 0; i < no_pfds; i++) {
  228. if (pfds[i].revents & POLLIN) {
  229. case_processed = 0;
  230. switch (i) {
  231. case 0:
  232. case_processed = 1;
  233. res = qdevice_heuristics_log_read_from_pipe(instance);
  234. if (res == -1) {
  235. return (-1);
  236. }
  237. break;
  238. case 1:
  239. case_processed = 1;
  240. res = qdevice_heuristics_cmd_read_from_pipe(instance);
  241. if (res == -1) {
  242. return (-1);
  243. }
  244. break;
  245. case 2:
  246. case_processed = 1;
  247. res = qdevice_votequorum_dispatch(instance->qdevice_instance_ptr);
  248. if (res == -1) {
  249. return (-1);
  250. }
  251. case 3:
  252. /*
  253. * Read on heuristics cmd send fs shouldn't happen
  254. */
  255. break;
  256. }
  257. if (!case_processed) {
  258. qdevice_log(LOG_CRIT, "Unhandled read on poll descriptor %u", i);
  259. exit(1);
  260. }
  261. }
  262. if (pfds[i].revents & POLLOUT) {
  263. case_processed = 0;
  264. switch (i) {
  265. case 0:
  266. case 1:
  267. case 2:
  268. /*
  269. * Write on heuristics log, cmd recv or vq shouldn't happen
  270. */
  271. break;
  272. case 3:
  273. case_processed = 1;
  274. res = qdevice_heuristics_cmd_write(instance);
  275. if (res == -1) {
  276. return (-1);
  277. }
  278. break;
  279. }
  280. if (!case_processed) {
  281. qdevice_log(LOG_CRIT, "Unhandled write on poll descriptor %u", i);
  282. exit(1);
  283. }
  284. }
  285. if ((pfds[i].revents & (POLLERR|POLLHUP|POLLNVAL)) &&
  286. !(pfds[i].revents & (POLLIN|POLLOUT))) {
  287. switch (i) {
  288. case 0:
  289. case 1:
  290. case 3:
  291. /*
  292. * Closed pipe doesn't mean return of POLLIN. To display
  293. * better log message, we call read log as if POLLIN would
  294. * be set.
  295. */
  296. res = qdevice_heuristics_log_read_from_pipe(instance);
  297. if (res == -1) {
  298. return (-1);
  299. }
  300. qdevice_log(LOG_ERR, "POLLERR (%u) on heuristics pipe. Exiting");
  301. return (-1);
  302. break;
  303. case 2:
  304. qdevice_log(LOG_ERR, "POLLERR (%u) on corosync socket. Exiting");
  305. return (-1);
  306. break;
  307. }
  308. }
  309. }
  310. } else if (poll_res == 0) {
  311. qdevice_log(LOG_ERR, "Timeout waiting for initial heuristics exec result");
  312. return (-1);
  313. } else {
  314. qdevice_log_err(LOG_ERR, "Initial heuristics exec result poll failed");
  315. return (-1);
  316. }
  317. }
  318. return (0);
  319. }