pload.c 5.3 KB

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