qdevice-heuristics.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*
  2. * Copyright (c) 2015-2020 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 "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(EXIT_FAILURE, "Can't create command input pipe");
  57. }
  58. if (pipe(pipe_cmd_out) != 0) {
  59. err(EXIT_FAILURE, "Can't create command output pipe");
  60. }
  61. if (pipe(pipe_log_out) != 0) {
  62. err(EXIT_FAILURE, "Can't create logging output pipe");
  63. }
  64. pid = fork();
  65. if (pid == -1) {
  66. err(EXIT_FAILURE, "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(EXIT_FAILURE, "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(EXIT_FAILURE, "Can't set non blocking flag on command input pipe");
  79. }
  80. if (dup2(pipe_cmd_out[1], 1) == -1) {
  81. err(EXIT_FAILURE, "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(EXIT_FAILURE, "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(EXIT_SUCCESS);
  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(EXIT_FAILURE, "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(EXIT_FAILURE, "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(EXIT_FAILURE, "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, int wait_for_worker_exit)
  124. {
  125. int status;
  126. /*
  127. * Close pipe_cmd_send. Heuristics worker catch the close of the fd and exits
  128. * properly.
  129. */
  130. close(instance->pipe_cmd_send);
  131. /*
  132. * When daemonization is used, heuristics worker is not a child of the corosync-qdevice
  133. * process any longer so it's not possible to wait for its exit.
  134. */
  135. if (wait_for_worker_exit) {
  136. log(LOG_DEBUG, "Waiting for heuristics worker to finish");
  137. if (waitpid(instance->worker_pid, &status, 0) == -1) {
  138. log_err(LOG_ERR, "Heuristics worker waitpid failed");
  139. } else {
  140. /*
  141. * Log what left in worker log buffer. Errors can be ignored
  142. */
  143. (void)qdevice_heuristics_log_read_from_pipe(instance);
  144. }
  145. }
  146. close(instance->pipe_cmd_recv);
  147. close(instance->pipe_log_recv);
  148. dynar_destroy(&instance->log_in_buffer);
  149. dynar_destroy(&instance->cmd_in_buffer);
  150. send_buffer_list_free(&instance->cmd_out_buffer_list);
  151. qdevice_heuristics_instance_destroy(instance);
  152. }
  153. int
  154. qdevice_heuristics_exec(struct qdevice_heuristics_instance *instance, int sync_in_progress)
  155. {
  156. uint32_t timeout;
  157. instance->expected_reply_seq_number++;
  158. instance->waiting_for_result = 1;
  159. if (sync_in_progress) {
  160. timeout = instance->sync_timeout;
  161. } else {
  162. timeout = instance->timeout;
  163. }
  164. return (qdevice_heuristics_cmd_write_exec(instance, timeout,
  165. instance->expected_reply_seq_number));
  166. }
  167. int
  168. qdevice_heuristics_waiting_for_result(const struct qdevice_heuristics_instance *instance)
  169. {
  170. return (instance->waiting_for_result);
  171. }
  172. int
  173. qdevice_heuristics_change_exec_list(struct qdevice_heuristics_instance *instance,
  174. const struct qdevice_heuristics_exec_list *new_exec_list, int sync_in_progress)
  175. {
  176. if (qdevice_heuristics_cmd_write_exec_list(instance, new_exec_list) != 0) {
  177. return (-1);
  178. }
  179. qdevice_heuristics_exec_list_free(&instance->exec_list);
  180. if (new_exec_list != NULL) {
  181. if (qdevice_heuristics_exec_list_clone(&instance->exec_list, new_exec_list) != 0) {
  182. log(LOG_ERR, "Can't clone exec list");
  183. return (-1);
  184. }
  185. }
  186. if (qdevice_heuristics_waiting_for_result(instance)) {
  187. if (qdevice_heuristics_exec(instance, sync_in_progress) != 0) {
  188. log(LOG_ERR, "Can't execute heuristics");
  189. return (-1);
  190. }
  191. }
  192. return (0);
  193. }
  194. int
  195. qdevice_heuristics_wait_for_initial_exec_result(struct qdevice_heuristics_instance *instance)
  196. {
  197. struct pollfd pfds[QDEVICE_HEURISTICS_WAIT_FOR_INITIAL_EXEC_RESULT_MAX_PFDS];
  198. int no_pfds;
  199. int poll_res;
  200. int timeout;
  201. int i;
  202. int case_processed;
  203. int res;
  204. while (!instance->qdevice_instance_ptr->vq_node_list_initial_heuristics_finished) {
  205. no_pfds = 0;
  206. assert(no_pfds < QDEVICE_HEURISTICS_WAIT_FOR_INITIAL_EXEC_RESULT_MAX_PFDS);
  207. pfds[no_pfds].fd = instance->pipe_log_recv;
  208. pfds[no_pfds].events = POLLIN;
  209. pfds[no_pfds].revents = 0;
  210. no_pfds++;
  211. assert(no_pfds < QDEVICE_HEURISTICS_WAIT_FOR_INITIAL_EXEC_RESULT_MAX_PFDS);
  212. pfds[no_pfds].fd = instance->pipe_cmd_recv;
  213. pfds[no_pfds].events = POLLIN;
  214. pfds[no_pfds].revents = 0;
  215. no_pfds++;
  216. assert(no_pfds < QDEVICE_HEURISTICS_WAIT_FOR_INITIAL_EXEC_RESULT_MAX_PFDS);
  217. pfds[no_pfds].fd = instance->qdevice_instance_ptr->votequorum_poll_fd;
  218. pfds[no_pfds].events = POLLIN;
  219. pfds[no_pfds].revents = 0;
  220. no_pfds++;
  221. if (!send_buffer_list_empty(&instance->cmd_out_buffer_list)) {
  222. assert(no_pfds < QDEVICE_HEURISTICS_WAIT_FOR_INITIAL_EXEC_RESULT_MAX_PFDS);
  223. pfds[no_pfds].fd = instance->pipe_cmd_send;
  224. pfds[no_pfds].events = POLLOUT;
  225. pfds[no_pfds].revents = 0;
  226. no_pfds++;
  227. }
  228. /*
  229. * We know this is never larger than QDEVICE_DEFAULT_HEURISTICS_MAX_TIMEOUT * 2
  230. */
  231. timeout = (int)instance->sync_timeout * 2;
  232. poll_res = poll(pfds, no_pfds, timeout);
  233. if (poll_res > 0) {
  234. for (i = 0; i < no_pfds; i++) {
  235. if (pfds[i].revents & POLLIN) {
  236. case_processed = 0;
  237. switch (i) {
  238. case 0:
  239. case_processed = 1;
  240. res = qdevice_heuristics_log_read_from_pipe(instance);
  241. if (res == -1) {
  242. return (-1);
  243. }
  244. break;
  245. case 1:
  246. case_processed = 1;
  247. res = qdevice_heuristics_cmd_read_from_pipe(instance);
  248. if (res == -1) {
  249. return (-1);
  250. }
  251. break;
  252. case 2:
  253. case_processed = 1;
  254. res = qdevice_votequorum_dispatch(instance->qdevice_instance_ptr);
  255. if (res == -1) {
  256. return (-1);
  257. }
  258. case 3:
  259. /*
  260. * Read on heuristics cmd send fs shouldn't happen
  261. */
  262. break;
  263. }
  264. if (!case_processed) {
  265. log(LOG_CRIT, "Unhandled read on poll descriptor %u", i);
  266. exit(EXIT_FAILURE);
  267. }
  268. }
  269. if (pfds[i].revents & POLLOUT) {
  270. case_processed = 0;
  271. switch (i) {
  272. case 0:
  273. case 1:
  274. case 2:
  275. /*
  276. * Write on heuristics log, cmd recv or vq shouldn't happen
  277. */
  278. break;
  279. case 3:
  280. case_processed = 1;
  281. res = qdevice_heuristics_cmd_write(instance);
  282. if (res == -1) {
  283. return (-1);
  284. }
  285. break;
  286. }
  287. if (!case_processed) {
  288. log(LOG_CRIT, "Unhandled write on poll descriptor %u", i);
  289. exit(EXIT_FAILURE);
  290. }
  291. }
  292. if ((pfds[i].revents & (POLLERR|POLLHUP|POLLNVAL)) &&
  293. !(pfds[i].revents & (POLLIN|POLLOUT))) {
  294. switch (i) {
  295. case 0:
  296. case 1:
  297. case 3:
  298. /*
  299. * Closed pipe doesn't mean return of POLLIN. To display
  300. * better log message, we call read log as if POLLIN would
  301. * be set.
  302. */
  303. res = qdevice_heuristics_log_read_from_pipe(instance);
  304. if (res == -1) {
  305. return (-1);
  306. }
  307. log(LOG_ERR, "POLLERR (%u) on heuristics pipe. Exiting",
  308. pfds[i].revents);
  309. return (-1);
  310. break;
  311. case 2:
  312. log(LOG_ERR, "POLLERR (%u) on corosync socket. Exiting",
  313. pfds[i].revents);
  314. return (-1);
  315. break;
  316. }
  317. }
  318. }
  319. } else if (poll_res == 0) {
  320. log(LOG_ERR, "Timeout waiting for initial heuristics exec result");
  321. return (-1);
  322. } else {
  323. log_err(LOG_ERR, "Initial heuristics exec result poll failed");
  324. return (-1);
  325. }
  326. }
  327. return (0);
  328. }