qdevice-pr-poll-loop-cb.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 "log.h"
  35. #include "qdevice-cmap.h"
  36. #include "qdevice-heuristics-cmd.h"
  37. #include "qdevice-heuristics-log.h"
  38. #include "qdevice-pr-poll-loop-cb.h"
  39. #include "qdevice-votequorum.h"
  40. static int
  41. heuristics_pipe_log_recv_read_cb(int fd, void *user_data1, void *user_data2)
  42. {
  43. struct qdevice_instance *instance = (struct qdevice_instance *)user_data1;
  44. int res;
  45. res = qdevice_heuristics_log_read_from_pipe(&instance->heuristics_instance);
  46. if (res == -1) {
  47. instance->heuristics_closed = 1;
  48. return (-1);
  49. }
  50. return (0);
  51. }
  52. /*
  53. * Callback is shared for all heuristics pipes
  54. */
  55. static int
  56. heuristics_pipe_err_cb(int fd, short revents, void *user_data1, void *user_data2)
  57. {
  58. struct qdevice_instance *instance = (struct qdevice_instance *)user_data1;
  59. instance->heuristics_closed = 1;
  60. /*
  61. * Closed pipe doesn't mean return of PR_POLL_READ. To display
  62. * better log message, we call read log as if POLLIN would
  63. * be set. Ignore error code because loop closes anyway.
  64. */
  65. (void)qdevice_heuristics_log_read_from_pipe(&instance->heuristics_instance);
  66. log(LOG_DEBUG, "POLL_ERR (%u) on heuristics pipe. "
  67. "Disconnecting.", revents);
  68. return (-1);
  69. }
  70. static int
  71. heuristics_pipe_cmd_recv_read_cb(int fd, void *user_data1, void *user_data2)
  72. {
  73. struct qdevice_instance *instance = (struct qdevice_instance *)user_data1;
  74. int res;
  75. res = qdevice_heuristics_cmd_read_from_pipe(&instance->heuristics_instance);
  76. if (res == -1) {
  77. instance->heuristics_closed = 1;
  78. return (-1);
  79. }
  80. return (0);
  81. }
  82. static int
  83. heuristics_pipe_cmd_send_set_events_cb(int fd, short *events, void *user_data1, void *user_data2)
  84. {
  85. struct qdevice_instance *instance = (struct qdevice_instance *)user_data1;
  86. int res;
  87. res = -1;
  88. if (!send_buffer_list_empty(&instance->heuristics_instance.cmd_out_buffer_list)) {
  89. *events |= POLLOUT;
  90. res = 0;
  91. }
  92. return (res);
  93. }
  94. static int
  95. heuristics_pipe_cmd_send_write_cb(int fd, void *user_data1, void *user_data2)
  96. {
  97. struct qdevice_instance *instance = (struct qdevice_instance *)user_data1;
  98. int res;
  99. res = qdevice_heuristics_cmd_write(&instance->heuristics_instance);
  100. if (res == -1) {
  101. instance->heuristics_closed = 1;
  102. return (-1);
  103. }
  104. return (0);
  105. }
  106. static int
  107. votequorum_read_cb(int fd, void *user_data1, void *user_data2)
  108. {
  109. struct qdevice_instance *instance = (struct qdevice_instance *)user_data1;
  110. int res;
  111. res = qdevice_votequorum_dispatch(instance);
  112. if (res == -1) {
  113. instance->votequorum_closed = 1;
  114. return (-1);
  115. }
  116. return (0);
  117. }
  118. static int
  119. votequorum_err_cb(int fd, short revents, void *user_data1, void *user_data2)
  120. {
  121. struct qdevice_instance *instance = (struct qdevice_instance *)user_data1;
  122. instance->votequorum_closed = 1;
  123. log(LOG_DEBUG, "POLL_ERR (%u) on corosync socket. "
  124. "Disconnecting.", revents);
  125. return (-1);
  126. }
  127. static int
  128. cmap_set_events_cb(int fd, short *events, void *user_data1, void *user_data2)
  129. {
  130. struct qdevice_instance *instance = (struct qdevice_instance *)user_data1;
  131. if (instance->sync_in_progress) {
  132. /*
  133. * During sync cmap is blocked -> don't add fd
  134. */
  135. return (-1);
  136. }
  137. return (0);
  138. }
  139. static int
  140. cmap_read_cb(int fd, void *user_data1, void *user_data2)
  141. {
  142. struct qdevice_instance *instance = (struct qdevice_instance *)user_data1;
  143. int res;
  144. res = qdevice_cmap_dispatch(instance);
  145. if (res == -1) {
  146. instance->cmap_closed = 1;
  147. return (-1);
  148. }
  149. return (0);
  150. }
  151. static int
  152. cmap_err_cb(int fd, short revents, void *user_data1, void *user_data2)
  153. {
  154. struct qdevice_instance *instance = (struct qdevice_instance *)user_data1;
  155. instance->cmap_closed = 1;
  156. log(LOG_DEBUG, "POLL_ERR (%u) on corosync socket. "
  157. "Disconnecting.", revents);
  158. return (-1);
  159. }
  160. int
  161. qdevice_pr_poll_loop_cb_register(struct qdevice_instance *instance)
  162. {
  163. if (pr_poll_loop_add_fd(&instance->main_poll_loop, instance->heuristics_instance.pipe_log_recv,
  164. POLLIN, NULL, heuristics_pipe_log_recv_read_cb, NULL, heuristics_pipe_err_cb,
  165. instance, NULL) != 0) {
  166. log(LOG_ERR, "Can't add heuristics log pipe to main poll loop");
  167. return (-1);
  168. }
  169. if (pr_poll_loop_add_fd(&instance->main_poll_loop, instance->heuristics_instance.pipe_cmd_recv,
  170. POLLIN, NULL, heuristics_pipe_cmd_recv_read_cb, NULL, heuristics_pipe_err_cb,
  171. instance, NULL) != 0) {
  172. log(LOG_ERR, "Can't add heuristics cmd recv pipe to main poll loop");
  173. return (-1);
  174. }
  175. if (pr_poll_loop_add_fd(&instance->main_poll_loop, instance->heuristics_instance.pipe_cmd_send,
  176. 0, heuristics_pipe_cmd_send_set_events_cb, NULL,
  177. heuristics_pipe_cmd_send_write_cb, heuristics_pipe_err_cb,
  178. instance, NULL) != 0) {
  179. log(LOG_ERR, "Can't add heuristics cmd send pipe to main poll loop");
  180. return (-1);
  181. }
  182. if (pr_poll_loop_add_fd(&instance->main_poll_loop, instance->votequorum_poll_fd,
  183. POLLIN, NULL, votequorum_read_cb, NULL, votequorum_err_cb,
  184. instance, NULL) != 0) {
  185. log(LOG_ERR, "Can't add votequorum fd to main poll loop");
  186. return (-1);
  187. }
  188. if (pr_poll_loop_add_fd(&instance->main_poll_loop, instance->cmap_poll_fd,
  189. POLLIN, cmap_set_events_cb, cmap_read_cb, NULL, cmap_err_cb,
  190. instance, NULL) != 0) {
  191. log(LOG_ERR, "Can't add votequorum fd to main poll loop");
  192. return (-1);
  193. }
  194. return (0);
  195. }