pload.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 <sys/types.h>
  39. #include <sys/socket.h>
  40. #include <errno.h>
  41. #include <corosync/corotypes.h>
  42. #include <corosync/coroipcc.h>
  43. #include <corosync/coroipc_types.h>
  44. #include <corosync/corodefs.h>
  45. #include <corosync/hdb.h>
  46. #include <corosync/pload.h>
  47. #include <corosync/ipc_pload.h>
  48. #include "util.h"
  49. struct pload_inst {
  50. hdb_handle_t handle;
  51. unsigned int finalize;
  52. };
  53. DECLARE_HDB_DATABASE(pload_handle_t_db,NULL);
  54. /**
  55. * @defgroup pload_corosync The extended virtual synchrony passthrough API
  56. * @ingroup corosync
  57. *
  58. * @{
  59. */
  60. /**
  61. * test
  62. * @param handle The handle of pload initialize
  63. * @param callbacks The callbacks for pload_initialize
  64. * @returns PLOAD_OK
  65. */
  66. unsigned int pload_initialize (
  67. pload_handle_t *handle,
  68. pload_callbacks_t *callbacks)
  69. {
  70. cs_error_t error;
  71. struct pload_inst *pload_inst;
  72. error = hdb_error_to_cs(hdb_handle_create (&pload_handle_t_db, sizeof (struct pload_inst), handle));
  73. if (error != CS_OK) {
  74. goto error_no_destroy;
  75. }
  76. error = hdb_error_to_cs(hdb_handle_get (&pload_handle_t_db, *handle, (void *)&pload_inst));
  77. if (error != CS_OK) {
  78. goto error_destroy;
  79. }
  80. error = coroipcc_service_connect (
  81. COROSYNC_SOCKET_NAME,
  82. PLOAD_SERVICE,
  83. IPC_REQUEST_SIZE,
  84. IPC_RESPONSE_SIZE,
  85. IPC_DISPATCH_SIZE,
  86. &pload_inst->handle);
  87. if (error != CS_OK) {
  88. goto error_put_destroy;
  89. }
  90. (void)hdb_handle_put (&pload_handle_t_db, *handle);
  91. return (CS_OK);
  92. error_put_destroy:
  93. (void)hdb_handle_put (&pload_handle_t_db, *handle);
  94. error_destroy:
  95. (void)hdb_handle_destroy (&pload_handle_t_db, *handle);
  96. error_no_destroy:
  97. return (error);
  98. }
  99. unsigned int pload_finalize (
  100. pload_handle_t handle)
  101. {
  102. struct pload_inst *pload_inst;
  103. cs_error_t error;
  104. error = hdb_error_to_cs (hdb_handle_get (&pload_handle_t_db, handle, (void *)&pload_inst));
  105. if (error != CS_OK) {
  106. return (error);
  107. }
  108. /*
  109. * Another thread has already started finalizing
  110. */
  111. if (pload_inst->finalize) {
  112. (void)hdb_handle_put (&pload_handle_t_db, handle);
  113. return (PLOAD_ERR_BAD_HANDLE);
  114. }
  115. pload_inst->finalize = 1;
  116. coroipcc_service_disconnect(pload_inst->handle);
  117. (void)hdb_handle_destroy (&pload_handle_t_db, handle);
  118. (void)hdb_handle_put (&pload_handle_t_db, handle);
  119. return (PLOAD_OK);
  120. }
  121. unsigned int pload_fd_get (
  122. pload_handle_t handle,
  123. int *fd)
  124. {
  125. cs_error_t error;
  126. struct pload_inst *pload_inst;
  127. error = hdb_error_to_cs(hdb_handle_get (&pload_handle_t_db, handle, (void *)&pload_inst));
  128. if (error != CS_OK) {
  129. return (error);
  130. }
  131. coroipcc_fd_get (pload_inst->handle, fd);
  132. (void)hdb_handle_put (&pload_handle_t_db, handle);
  133. return (CS_OK);
  134. }
  135. unsigned int pload_start (
  136. pload_handle_t handle,
  137. unsigned int code,
  138. unsigned int msg_count,
  139. unsigned int msg_size)
  140. {
  141. unsigned int error;
  142. struct pload_inst *pload_inst;
  143. struct iovec iov;
  144. struct req_lib_pload_start req_lib_pload_start;
  145. struct res_lib_pload_start res_lib_pload_start;
  146. error = hdb_error_to_cs(hdb_handle_get (&pload_handle_t_db, handle, (void *)&pload_inst));
  147. if (error != CS_OK) {
  148. return (error);
  149. }
  150. req_lib_pload_start.header.size = sizeof (struct req_lib_pload_start);
  151. req_lib_pload_start.header.id = MESSAGE_REQ_PLOAD_START;
  152. req_lib_pload_start.msg_code = code;
  153. req_lib_pload_start.msg_count = msg_count;
  154. req_lib_pload_start.msg_size = msg_size;
  155. iov.iov_base = (char *)&req_lib_pload_start;
  156. iov.iov_len = sizeof (struct req_lib_pload_start);
  157. error = coroipcc_msg_send_reply_receive(pload_inst->handle,
  158. &iov,
  159. 1,
  160. &res_lib_pload_start,
  161. sizeof (struct res_lib_pload_start));
  162. if (error != CS_OK) {
  163. goto error_exit;
  164. }
  165. error = res_lib_pload_start.header.error;
  166. error_exit:
  167. (void)hdb_handle_put (&pload_handle_t_db, handle);
  168. return (error);
  169. }
  170. /** @} */