pload.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 (IPC_SOCKET_NAME, PLOAD_SERVICE, &pload_inst->ipc_ctx);
  92. if (error != CS_OK) {
  93. goto error_put_destroy;
  94. }
  95. pthread_mutex_init (&pload_inst->response_mutex, NULL);
  96. pthread_mutex_init (&pload_inst->dispatch_mutex, NULL);
  97. (void)saHandleInstancePut (&pload_handle_t_db, *handle);
  98. return (CS_OK);
  99. error_put_destroy:
  100. (void)saHandleInstancePut (&pload_handle_t_db, *handle);
  101. error_destroy:
  102. (void)saHandleDestroy (&pload_handle_t_db, *handle);
  103. error_no_destroy:
  104. return (error);
  105. }
  106. unsigned int pload_finalize (
  107. pload_handle_t handle)
  108. {
  109. struct pload_inst *pload_inst;
  110. cs_error_t error;
  111. error = saHandleInstanceGet (&pload_handle_t_db, handle, (void *)&pload_inst);
  112. if (error != CS_OK) {
  113. return (error);
  114. }
  115. pthread_mutex_lock (&pload_inst->response_mutex);
  116. /*
  117. * Another thread has already started finalizing
  118. */
  119. if (pload_inst->finalize) {
  120. pthread_mutex_unlock (&pload_inst->response_mutex);
  121. (void)saHandleInstancePut (&pload_handle_t_db, handle);
  122. return (PLOAD_ERR_BAD_HANDLE);
  123. }
  124. pload_inst->finalize = 1;
  125. coroipcc_service_disconnect(pload_inst->ipc_ctx);
  126. pthread_mutex_unlock (&pload_inst->response_mutex);
  127. (void)saHandleDestroy (&pload_handle_t_db, handle);
  128. (void)saHandleInstancePut (&pload_handle_t_db, handle);
  129. return (PLOAD_OK);
  130. }
  131. unsigned int pload_fd_get (
  132. pload_handle_t handle,
  133. int *fd)
  134. {
  135. cs_error_t error;
  136. struct pload_inst *pload_inst;
  137. error = saHandleInstanceGet (&pload_handle_t_db, handle, (void *)&pload_inst);
  138. if (error != CS_OK) {
  139. return (error);
  140. }
  141. *fd = coroipcc_fd_get (pload_inst->ipc_ctx);
  142. (void)saHandleInstancePut (&pload_handle_t_db, handle);
  143. return (CS_OK);
  144. }
  145. unsigned int pload_start (
  146. pload_handle_t handle,
  147. unsigned int code,
  148. unsigned int msg_count,
  149. unsigned int msg_size)
  150. {
  151. unsigned int error;
  152. struct pload_inst *pload_inst;
  153. struct iovec iov;
  154. struct req_lib_pload_start req_lib_pload_start;
  155. struct res_lib_pload_start res_lib_pload_start;
  156. error = saHandleInstanceGet (&pload_handle_t_db, handle, (void *)&pload_inst);
  157. if (error != CS_OK) {
  158. return (error);
  159. }
  160. req_lib_pload_start.header.size = sizeof (struct req_lib_pload_start);
  161. req_lib_pload_start.header.id = MESSAGE_REQ_PLOAD_START;
  162. req_lib_pload_start.msg_code = code;
  163. req_lib_pload_start.msg_count = msg_count;
  164. req_lib_pload_start.msg_size = msg_size;
  165. iov.iov_base = (char *)&req_lib_pload_start;
  166. iov.iov_len = sizeof (struct req_lib_pload_start);
  167. pthread_mutex_lock (&pload_inst->response_mutex);
  168. error = coroipcc_msg_send_reply_receive(pload_inst->ipc_ctx,
  169. &iov,
  170. 1,
  171. &res_lib_pload_start,
  172. sizeof (struct res_lib_pload_start));
  173. pthread_mutex_unlock (&pload_inst->response_mutex);
  174. if (error != CS_OK) {
  175. goto error_exit;
  176. }
  177. error = res_lib_pload_start.header.error;
  178. error_exit:
  179. (void)saHandleInstancePut (&pload_handle_t_db, handle);
  180. return (error);
  181. }
  182. /** @} */