pload.c 6.1 KB

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