pload.c 6.2 KB

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