qdevice-heuristics.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. void
  49. qdevice_heuristics_init(struct qdevice_heuristics_instance *instance,
  50. struct qdevice_advanced_settings *advanced_settings)
  51. {
  52. int pipe_cmd_in[2], pipe_cmd_out[2], pipe_log_out[2];
  53. pid_t pid;
  54. if (pipe(pipe_cmd_in) != 0) {
  55. err(EXIT_FAILURE, "Can't create command input pipe");
  56. }
  57. if (pipe(pipe_cmd_out) != 0) {
  58. err(EXIT_FAILURE, "Can't create command output pipe");
  59. }
  60. if (pipe(pipe_log_out) != 0) {
  61. err(EXIT_FAILURE, "Can't create logging output pipe");
  62. }
  63. pid = fork();
  64. if (pid == -1) {
  65. err(EXIT_FAILURE, "Can't create child process");
  66. } else if (pid == 0) {
  67. /*
  68. * Child
  69. */
  70. (void)setsid();
  71. if (dup2(pipe_cmd_in[0], 0) == -1) {
  72. err(EXIT_FAILURE, "Can't dup2 command input pipe");
  73. }
  74. close(pipe_cmd_in[1]);
  75. close(pipe_cmd_in[0]);
  76. if (utils_fd_set_non_blocking(0) == -1) {
  77. err(EXIT_FAILURE, "Can't set non blocking flag on command input pipe");
  78. }
  79. if (dup2(pipe_cmd_out[1], 1) == -1) {
  80. err(EXIT_FAILURE, "Can't dup2 command output pipe");
  81. }
  82. close(pipe_cmd_out[0]);
  83. close(pipe_cmd_out[1]);
  84. if (dup2(pipe_log_out[1], 2) == -1) {
  85. err(EXIT_FAILURE, "Can't dup2 logging output pipe");
  86. }
  87. close(pipe_log_out[0]);
  88. close(pipe_log_out[1]);
  89. qdevice_heuristics_worker_start(advanced_settings->heuristics_ipc_max_send_receive_size,
  90. advanced_settings->heuristics_use_execvp, advanced_settings->heuristics_max_processes,
  91. advanced_settings->heuristics_kill_list_interval);
  92. qdevice_advanced_settings_destroy(advanced_settings);
  93. exit(EXIT_SUCCESS);
  94. } else {
  95. close(pipe_cmd_in[0]);
  96. close(pipe_cmd_out[1]);
  97. close(pipe_log_out[1]);
  98. qdevice_heuristics_instance_init(instance);
  99. instance->pipe_cmd_send = pipe_cmd_in[1];
  100. if (utils_fd_set_non_blocking(instance->pipe_cmd_send) == -1) {
  101. err(EXIT_FAILURE, "Can't set non blocking flag on command input pipe");
  102. }
  103. instance->pipe_cmd_recv = pipe_cmd_out[0];
  104. if (utils_fd_set_non_blocking(instance->pipe_cmd_recv) == -1) {
  105. err(EXIT_FAILURE, "Can't set non blocking flag on command output pipe");
  106. }
  107. instance->pipe_log_recv = pipe_log_out[0];
  108. if (utils_fd_set_non_blocking(instance->pipe_cmd_recv) == -1) {
  109. err(EXIT_FAILURE, "Can't set non blocking flag on logging output pipe");
  110. }
  111. instance->worker_pid = pid;
  112. send_buffer_list_init(&instance->cmd_out_buffer_list,
  113. advanced_settings->heuristics_ipc_max_send_buffers,
  114. advanced_settings->heuristics_ipc_max_send_receive_size);
  115. dynar_init(&instance->log_in_buffer,
  116. advanced_settings->heuristics_ipc_max_send_receive_size);
  117. dynar_init(&instance->cmd_in_buffer,
  118. advanced_settings->heuristics_ipc_max_send_receive_size);
  119. }
  120. }
  121. void
  122. qdevice_heuristics_destroy(struct qdevice_heuristics_instance *instance, int wait_for_worker_exit)
  123. {
  124. int status;
  125. /*
  126. * Close pipe_cmd_send. Heuristics worker catch the close of the fd and exits
  127. * properly.
  128. */
  129. close(instance->pipe_cmd_send);
  130. /*
  131. * When daemonization is used, heuristics worker is not a child of the corosync-qdevice
  132. * process any longer so it's not possible to wait for its exit.
  133. */
  134. if (wait_for_worker_exit) {
  135. log(LOG_DEBUG, "Waiting for heuristics worker to finish");
  136. if (waitpid(instance->worker_pid, &status, 0) == -1) {
  137. log_err(LOG_ERR, "Heuristics worker waitpid failed");
  138. } else {
  139. /*
  140. * Log what left in worker log buffer. Errors can be ignored
  141. */
  142. (void)qdevice_heuristics_log_read_from_pipe(instance);
  143. }
  144. }
  145. close(instance->pipe_cmd_recv);
  146. close(instance->pipe_log_recv);
  147. dynar_destroy(&instance->log_in_buffer);
  148. dynar_destroy(&instance->cmd_in_buffer);
  149. send_buffer_list_free(&instance->cmd_out_buffer_list);
  150. qdevice_heuristics_instance_destroy(instance);
  151. }
  152. int
  153. qdevice_heuristics_exec(struct qdevice_heuristics_instance *instance, int sync_in_progress)
  154. {
  155. uint32_t timeout;
  156. instance->expected_reply_seq_number++;
  157. instance->waiting_for_result = 1;
  158. if (sync_in_progress) {
  159. timeout = instance->sync_timeout;
  160. } else {
  161. timeout = instance->timeout;
  162. }
  163. return (qdevice_heuristics_cmd_write_exec(instance, timeout,
  164. instance->expected_reply_seq_number));
  165. }
  166. int
  167. qdevice_heuristics_waiting_for_result(const struct qdevice_heuristics_instance *instance)
  168. {
  169. return (instance->waiting_for_result);
  170. }
  171. int
  172. qdevice_heuristics_change_exec_list(struct qdevice_heuristics_instance *instance,
  173. const struct qdevice_heuristics_exec_list *new_exec_list, int sync_in_progress)
  174. {
  175. if (qdevice_heuristics_cmd_write_exec_list(instance, new_exec_list) != 0) {
  176. return (-1);
  177. }
  178. qdevice_heuristics_exec_list_free(&instance->exec_list);
  179. if (new_exec_list != NULL) {
  180. if (qdevice_heuristics_exec_list_clone(&instance->exec_list, new_exec_list) != 0) {
  181. log(LOG_ERR, "Can't clone exec list");
  182. return (-1);
  183. }
  184. }
  185. if (qdevice_heuristics_waiting_for_result(instance)) {
  186. if (qdevice_heuristics_exec(instance, sync_in_progress) != 0) {
  187. log(LOG_ERR, "Can't execute heuristics");
  188. return (-1);
  189. }
  190. }
  191. return (0);
  192. }