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

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