pload.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Copyright (c) 2008 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Steven Dake (sdake@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 MontaVista Software, 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 <stdlib.h>
  35. #include <string.h>
  36. #include <unistd.h>
  37. #include <pthread.h>
  38. #include <sys/types.h>
  39. #include <sys/socket.h>
  40. #include <errno.h>
  41. #include <corosync/swab.h>
  42. #include <corosync/totem/totem.h>
  43. #include <corosync/saAis.h>
  44. #include <corosync/ipc_pload.h>
  45. #include <corosync/pload.h>
  46. #include <corosync/ais_util.h>
  47. static void pload_instance_destructor (void *instance);
  48. struct pload_inst {
  49. int dispatch_fd;
  50. int response_fd;
  51. pthread_mutex_t response_mutex;
  52. pthread_mutex_t dispatch_mutex;
  53. unsigned int finalize;
  54. };
  55. static struct saHandleDatabase pload_handle_t_db = {
  56. .handleCount = 0,
  57. .handles = 0,
  58. .mutex = PTHREAD_MUTEX_INITIALIZER,
  59. .handleInstanceDestructor = pload_instance_destructor
  60. };
  61. /*
  62. * Clean up function for an evt instance (saEvtInitialize) handle
  63. */
  64. static void pload_instance_destructor (void *instance)
  65. {
  66. struct pload_inst *pload_inst = instance;
  67. pthread_mutex_destroy (&pload_inst->response_mutex);
  68. pthread_mutex_destroy (&pload_inst->dispatch_mutex);
  69. }
  70. /**
  71. * @defgroup pload_corosync The extended virtual synchrony passthrough API
  72. * @ingroup corosync
  73. *
  74. * @{
  75. */
  76. /**
  77. * test
  78. * @param handle The handle of pload initialize
  79. * @param callbacks The callbacks for pload_initialize
  80. * @returns PLOAD_OK
  81. */
  82. unsigned int pload_initialize (
  83. pload_handle_t *handle,
  84. pload_callbacks_t *callbacks)
  85. {
  86. SaAisErrorT error;
  87. struct pload_inst *pload_inst;
  88. error = saHandleCreate (&pload_handle_t_db, sizeof (struct pload_inst), handle);
  89. if (error != SA_AIS_OK) {
  90. goto error_no_destroy;
  91. }
  92. error = saHandleInstanceGet (&pload_handle_t_db, *handle, (void *)&pload_inst);
  93. if (error != SA_AIS_OK) {
  94. goto error_destroy;
  95. }
  96. error = saServiceConnect (&pload_inst->response_fd,
  97. &pload_inst->dispatch_fd,
  98. PLOAD_SERVICE);
  99. if (error != SA_AIS_OK) {
  100. goto error_put_destroy;
  101. }
  102. pthread_mutex_init (&pload_inst->response_mutex, NULL);
  103. pthread_mutex_init (&pload_inst->dispatch_mutex, NULL);
  104. saHandleInstancePut (&pload_handle_t_db, *handle);
  105. return (SA_AIS_OK);
  106. error_put_destroy:
  107. saHandleInstancePut (&pload_handle_t_db, *handle);
  108. error_destroy:
  109. saHandleDestroy (&pload_handle_t_db, *handle);
  110. error_no_destroy:
  111. return (error);
  112. }
  113. unsigned int pload_finalize (
  114. pload_handle_t handle)
  115. {
  116. struct pload_inst *pload_inst;
  117. SaAisErrorT error;
  118. error = saHandleInstanceGet (&pload_handle_t_db, handle, (void *)&pload_inst);
  119. if (error != SA_AIS_OK) {
  120. return (error);
  121. }
  122. // TODO is the locking right here
  123. pthread_mutex_lock (&pload_inst->response_mutex);
  124. /*
  125. * Another thread has already started finalizing
  126. */
  127. if (pload_inst->finalize) {
  128. pthread_mutex_unlock (&pload_inst->response_mutex);
  129. saHandleInstancePut (&pload_handle_t_db, handle);
  130. return (PLOAD_ERR_BAD_HANDLE);
  131. }
  132. pload_inst->finalize = 1;
  133. pthread_mutex_unlock (&pload_inst->response_mutex);
  134. saHandleDestroy (&pload_handle_t_db, handle);
  135. /*
  136. * Disconnect from the server
  137. */
  138. if (pload_inst->response_fd != -1) {
  139. shutdown(pload_inst->response_fd, 0);
  140. close(pload_inst->response_fd);
  141. }
  142. if (pload_inst->dispatch_fd != -1) {
  143. shutdown(pload_inst->dispatch_fd, 0);
  144. close(pload_inst->dispatch_fd);
  145. }
  146. saHandleInstancePut (&pload_handle_t_db, handle);
  147. return (PLOAD_OK);
  148. }
  149. unsigned int pload_fd_get (
  150. pload_handle_t handle,
  151. int *fd)
  152. {
  153. SaAisErrorT error;
  154. struct pload_inst *pload_inst;
  155. error = saHandleInstanceGet (&pload_handle_t_db, handle, (void *)&pload_inst);
  156. if (error != SA_AIS_OK) {
  157. return (error);
  158. }
  159. *fd = pload_inst->dispatch_fd;
  160. saHandleInstancePut (&pload_handle_t_db, handle);
  161. return (SA_AIS_OK);
  162. }
  163. unsigned int pload_start (
  164. pload_handle_t handle,
  165. unsigned int code,
  166. unsigned int msg_count,
  167. unsigned int msg_size)
  168. {
  169. unsigned int error;
  170. struct pload_inst *pload_inst;
  171. struct iovec iov;
  172. struct req_lib_pload_start req_lib_pload_start;
  173. struct res_lib_pload_start res_lib_pload_start;
  174. error = saHandleInstanceGet (&pload_handle_t_db, handle, (void *)&pload_inst);
  175. if (error != SA_AIS_OK) {
  176. return (error);
  177. }
  178. req_lib_pload_start.header.size = sizeof (struct req_lib_pload_start);
  179. req_lib_pload_start.header.id = MESSAGE_REQ_PLOAD_START;
  180. req_lib_pload_start.msg_code = code;
  181. req_lib_pload_start.msg_count = msg_count;
  182. req_lib_pload_start.msg_size = msg_size;
  183. iov.iov_base = (char *)&req_lib_pload_start;
  184. iov.iov_len = sizeof (struct req_lib_pload_start);
  185. pthread_mutex_lock (&pload_inst->response_mutex);
  186. error = saSendMsgReceiveReply (pload_inst->response_fd, &iov, 1,
  187. &res_lib_pload_start, sizeof (struct res_lib_pload_start));
  188. pthread_mutex_unlock (&pload_inst->response_mutex);
  189. if (error != SA_AIS_OK) {
  190. goto error_exit;
  191. }
  192. error = res_lib_pload_start.header.error;
  193. error_exit:
  194. saHandleInstancePut (&pload_handle_t_db, handle);
  195. return (error);
  196. }
  197. /** @} */