pload.c 6.1 KB

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