cpg.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  1. /*
  2. * vi: set autoindent tabstop=4 shiftwidth=4 :
  3. *
  4. * Copyright (c) 2006-2009 Red Hat, Inc.
  5. *
  6. * All rights reserved.
  7. *
  8. * Author: Patrick Caulfield (pcaulfie@redhat.com)
  9. *
  10. * This software licensed under BSD license, the text of which follows:
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * - Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  21. * contributors may be used to endorse or promote products derived from this
  22. * software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  28. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  34. * THE POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. /*
  37. * Provides a closed process group API using the coroipcc executive
  38. */
  39. #include <config.h>
  40. #include <stdlib.h>
  41. #include <string.h>
  42. #include <unistd.h>
  43. #include <pthread.h>
  44. #include <sys/types.h>
  45. #include <sys/socket.h>
  46. #include <errno.h>
  47. #include <corosync/corotypes.h>
  48. #include <corosync/coroipc_types.h>
  49. #include <corosync/coroipcc.h>
  50. #include <corosync/corodefs.h>
  51. #include <corosync/hdb.h>
  52. #include <corosync/cpg.h>
  53. #include <corosync/ipc_cpg.h>
  54. #include "util.h"
  55. struct cpg_inst {
  56. void *ipc_ctx;
  57. int finalize;
  58. cpg_callbacks_t callbacks;
  59. void *context;
  60. pthread_mutex_t response_mutex;
  61. pthread_mutex_t dispatch_mutex;
  62. };
  63. static void cpg_instance_destructor (void *instance);
  64. DECLARE_HDB_DATABASE(cpg_handle_t_db,cpg_instance_destructor);
  65. /*
  66. * Clean up function for a cpg instance (cpg_nitialize) handle
  67. */
  68. static void cpg_instance_destructor (void *instance)
  69. {
  70. struct cpg_inst *cpg_inst = instance;
  71. pthread_mutex_destroy (&cpg_inst->response_mutex);
  72. pthread_mutex_destroy (&cpg_inst->dispatch_mutex);
  73. }
  74. /**
  75. * @defgroup cpg_coroipcc The closed process group API
  76. * @ingroup coroipcc
  77. *
  78. * @{
  79. */
  80. cs_error_t cpg_initialize (
  81. cpg_handle_t *handle,
  82. cpg_callbacks_t *callbacks)
  83. {
  84. cs_error_t error;
  85. struct cpg_inst *cpg_inst;
  86. error = hdb_error_to_cs (hdb_handle_create (&cpg_handle_t_db, sizeof (struct cpg_inst), handle));
  87. if (error != CS_OK) {
  88. goto error_no_destroy;
  89. }
  90. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, *handle, (void *)&cpg_inst));
  91. if (error != CS_OK) {
  92. goto error_destroy;
  93. }
  94. error = coroipcc_service_connect (
  95. COROSYNC_SOCKET_NAME,
  96. CPG_SERVICE,
  97. IPC_REQUEST_SIZE,
  98. IPC_RESPONSE_SIZE,
  99. IPC_DISPATCH_SIZE,
  100. &cpg_inst->ipc_ctx);
  101. if (error != CS_OK) {
  102. goto error_put_destroy;
  103. }
  104. memcpy (&cpg_inst->callbacks, callbacks, sizeof (cpg_callbacks_t));
  105. pthread_mutex_init (&cpg_inst->response_mutex, NULL);
  106. pthread_mutex_init (&cpg_inst->dispatch_mutex, NULL);
  107. hdb_handle_put (&cpg_handle_t_db, *handle);
  108. return (CS_OK);
  109. error_put_destroy:
  110. hdb_handle_put (&cpg_handle_t_db, *handle);
  111. error_destroy:
  112. hdb_handle_destroy (&cpg_handle_t_db, *handle);
  113. error_no_destroy:
  114. return (error);
  115. }
  116. cs_error_t cpg_finalize (
  117. cpg_handle_t handle)
  118. {
  119. struct cpg_inst *cpg_inst;
  120. cs_error_t error;
  121. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  122. if (error != CS_OK) {
  123. return (error);
  124. }
  125. pthread_mutex_lock (&cpg_inst->response_mutex);
  126. /*
  127. * Another thread has already started finalizing
  128. */
  129. if (cpg_inst->finalize) {
  130. pthread_mutex_unlock (&cpg_inst->response_mutex);
  131. hdb_handle_put (&cpg_handle_t_db, handle);
  132. return (CPG_ERR_BAD_HANDLE);
  133. }
  134. cpg_inst->finalize = 1;
  135. coroipcc_service_disconnect (cpg_inst->ipc_ctx);
  136. pthread_mutex_unlock (&cpg_inst->response_mutex);
  137. hdb_handle_destroy (&cpg_handle_t_db, handle);
  138. hdb_handle_put (&cpg_handle_t_db, handle);
  139. return (CPG_OK);
  140. }
  141. cs_error_t cpg_fd_get (
  142. cpg_handle_t handle,
  143. int *fd)
  144. {
  145. cs_error_t error;
  146. struct cpg_inst *cpg_inst;
  147. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  148. if (error != CS_OK) {
  149. return (error);
  150. }
  151. *fd = coroipcc_fd_get (cpg_inst->ipc_ctx);
  152. hdb_handle_put (&cpg_handle_t_db, handle);
  153. return (CS_OK);
  154. }
  155. cs_error_t cpg_context_get (
  156. cpg_handle_t handle,
  157. void **context)
  158. {
  159. cs_error_t error;
  160. struct cpg_inst *cpg_inst;
  161. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  162. if (error != CS_OK) {
  163. return (error);
  164. }
  165. *context = cpg_inst->context;
  166. hdb_handle_put (&cpg_handle_t_db, handle);
  167. return (CS_OK);
  168. }
  169. cs_error_t cpg_context_set (
  170. cpg_handle_t handle,
  171. void *context)
  172. {
  173. cs_error_t error;
  174. struct cpg_inst *cpg_inst;
  175. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  176. if (error != CS_OK) {
  177. return (error);
  178. }
  179. cpg_inst->context = context;
  180. hdb_handle_put (&cpg_handle_t_db, handle);
  181. return (CS_OK);
  182. }
  183. cs_error_t cpg_dispatch (
  184. cpg_handle_t handle,
  185. cs_dispatch_flags_t dispatch_types)
  186. {
  187. int timeout = -1;
  188. cs_error_t error;
  189. int cont = 1; /* always continue do loop except when set to 0 */
  190. int dispatch_avail;
  191. struct cpg_inst *cpg_inst;
  192. struct res_lib_cpg_confchg_callback *res_cpg_confchg_callback;
  193. struct res_lib_cpg_deliver_callback *res_cpg_deliver_callback;
  194. cpg_callbacks_t callbacks;
  195. coroipc_response_header_t *dispatch_data;
  196. int ignore_dispatch = 0;
  197. struct cpg_address member_list[CPG_MEMBERS_MAX];
  198. struct cpg_address left_list[CPG_MEMBERS_MAX];
  199. struct cpg_address joined_list[CPG_MEMBERS_MAX];
  200. struct cpg_name group_name;
  201. mar_cpg_address_t *left_list_start;
  202. mar_cpg_address_t *joined_list_start;
  203. unsigned int i;
  204. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  205. if (error != CS_OK) {
  206. return (error);
  207. }
  208. /*
  209. * Timeout instantly for SA_DISPATCH_ONE or SA_DISPATCH_ALL and
  210. * wait indefinately for SA_DISPATCH_BLOCKING
  211. */
  212. if (dispatch_types == CPG_DISPATCH_ALL) {
  213. timeout = 0;
  214. }
  215. do {
  216. pthread_mutex_lock (&cpg_inst->dispatch_mutex);
  217. dispatch_avail = coroipcc_dispatch_get (
  218. cpg_inst->ipc_ctx,
  219. (void **)&dispatch_data,
  220. timeout);
  221. pthread_mutex_unlock (&cpg_inst->dispatch_mutex);
  222. if (dispatch_avail == 0 && dispatch_types == CPG_DISPATCH_ALL) {
  223. pthread_mutex_unlock (&cpg_inst->dispatch_mutex);
  224. break; /* exit do while cont is 1 loop */
  225. } else
  226. if (dispatch_avail == 0) {
  227. pthread_mutex_unlock (&cpg_inst->dispatch_mutex);
  228. continue; /* next poll */
  229. }
  230. if (dispatch_avail == -1) {
  231. if (cpg_inst->finalize == 1) {
  232. error = CS_OK;
  233. } else {
  234. error = CS_ERR_LIBRARY;
  235. }
  236. goto error_put;
  237. }
  238. /*
  239. * Make copy of callbacks, message data, unlock instance, and call callback
  240. * A risk of this dispatch method is that the callback routines may
  241. * operate at the same time that cpgFinalize has been called.
  242. */
  243. memcpy (&callbacks, &cpg_inst->callbacks, sizeof (cpg_callbacks_t));
  244. /*
  245. * Dispatch incoming message
  246. */
  247. switch (dispatch_data->id) {
  248. case MESSAGE_RES_CPG_DELIVER_CALLBACK:
  249. res_cpg_deliver_callback = (struct res_lib_cpg_deliver_callback *)dispatch_data;
  250. marshall_from_mar_cpg_name_t (
  251. &group_name,
  252. &res_cpg_deliver_callback->group_name);
  253. callbacks.cpg_deliver_fn (handle,
  254. &group_name,
  255. res_cpg_deliver_callback->nodeid,
  256. res_cpg_deliver_callback->pid,
  257. &res_cpg_deliver_callback->message,
  258. res_cpg_deliver_callback->msglen);
  259. break;
  260. case MESSAGE_RES_CPG_CONFCHG_CALLBACK:
  261. res_cpg_confchg_callback = (struct res_lib_cpg_confchg_callback *)dispatch_data;
  262. for (i = 0; i < res_cpg_confchg_callback->member_list_entries; i++) {
  263. marshall_from_mar_cpg_address_t (&member_list[i],
  264. &res_cpg_confchg_callback->member_list[i]);
  265. }
  266. left_list_start = res_cpg_confchg_callback->member_list +
  267. res_cpg_confchg_callback->member_list_entries;
  268. for (i = 0; i < res_cpg_confchg_callback->left_list_entries; i++) {
  269. marshall_from_mar_cpg_address_t (&left_list[i],
  270. &left_list_start[i]);
  271. }
  272. joined_list_start = res_cpg_confchg_callback->member_list +
  273. res_cpg_confchg_callback->member_list_entries +
  274. res_cpg_confchg_callback->left_list_entries;
  275. for (i = 0; i < res_cpg_confchg_callback->joined_list_entries; i++) {
  276. marshall_from_mar_cpg_address_t (&joined_list[i],
  277. &joined_list_start[i]);
  278. }
  279. marshall_from_mar_cpg_name_t (
  280. &group_name,
  281. &res_cpg_confchg_callback->group_name);
  282. callbacks.cpg_confchg_fn (handle,
  283. &group_name,
  284. member_list,
  285. res_cpg_confchg_callback->member_list_entries,
  286. left_list,
  287. res_cpg_confchg_callback->left_list_entries,
  288. joined_list,
  289. res_cpg_confchg_callback->joined_list_entries);
  290. break;
  291. default:
  292. coroipcc_dispatch_put (cpg_inst->ipc_ctx);
  293. error = CS_ERR_LIBRARY;
  294. goto error_put;
  295. break;
  296. }
  297. coroipcc_dispatch_put (cpg_inst->ipc_ctx);
  298. /*
  299. * Determine if more messages should be processed
  300. * */
  301. switch (dispatch_types) {
  302. case CPG_DISPATCH_ONE:
  303. if (ignore_dispatch) {
  304. ignore_dispatch = 0;
  305. } else {
  306. cont = 0;
  307. }
  308. break;
  309. case CPG_DISPATCH_ALL:
  310. if (ignore_dispatch) {
  311. ignore_dispatch = 0;
  312. }
  313. break;
  314. case CPG_DISPATCH_BLOCKING:
  315. break;
  316. }
  317. } while (cont);
  318. error_put:
  319. hdb_handle_put (&cpg_handle_t_db, handle);
  320. return (error);
  321. }
  322. cs_error_t cpg_join (
  323. cpg_handle_t handle,
  324. const struct cpg_name *group)
  325. {
  326. cs_error_t error;
  327. struct cpg_inst *cpg_inst;
  328. struct iovec iov[2];
  329. struct req_lib_cpg_join req_lib_cpg_join;
  330. struct res_lib_cpg_join res_lib_cpg_join;
  331. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  332. if (error != CS_OK) {
  333. return (error);
  334. }
  335. /* Now join */
  336. req_lib_cpg_join.header.size = sizeof (struct req_lib_cpg_join);
  337. req_lib_cpg_join.header.id = MESSAGE_REQ_CPG_JOIN;
  338. req_lib_cpg_join.pid = getpid();
  339. marshall_to_mar_cpg_name_t (&req_lib_cpg_join.group_name,
  340. group);
  341. iov[0].iov_base = &req_lib_cpg_join;
  342. iov[0].iov_len = sizeof (struct req_lib_cpg_join);
  343. do {
  344. pthread_mutex_lock (&cpg_inst->response_mutex);
  345. error = coroipcc_msg_send_reply_receive (cpg_inst->ipc_ctx, iov, 1,
  346. &res_lib_cpg_join, sizeof (struct res_lib_cpg_join));
  347. pthread_mutex_unlock (&cpg_inst->response_mutex);
  348. if (error != CS_OK) {
  349. goto error_exit;
  350. }
  351. } while (res_lib_cpg_join.header.error == CPG_ERR_BUSY);
  352. error = res_lib_cpg_join.header.error;
  353. error_exit:
  354. hdb_handle_put (&cpg_handle_t_db, handle);
  355. return (error);
  356. }
  357. cs_error_t cpg_leave (
  358. cpg_handle_t handle,
  359. const struct cpg_name *group)
  360. {
  361. cs_error_t error;
  362. struct cpg_inst *cpg_inst;
  363. struct iovec iov[2];
  364. struct req_lib_cpg_leave req_lib_cpg_leave;
  365. struct res_lib_cpg_leave res_lib_cpg_leave;
  366. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  367. if (error != CS_OK) {
  368. return (error);
  369. }
  370. req_lib_cpg_leave.header.size = sizeof (struct req_lib_cpg_leave);
  371. req_lib_cpg_leave.header.id = MESSAGE_REQ_CPG_LEAVE;
  372. req_lib_cpg_leave.pid = getpid();
  373. marshall_to_mar_cpg_name_t (&req_lib_cpg_leave.group_name,
  374. group);
  375. iov[0].iov_base = &req_lib_cpg_leave;
  376. iov[0].iov_len = sizeof (struct req_lib_cpg_leave);
  377. do {
  378. pthread_mutex_lock (&cpg_inst->response_mutex);
  379. error = coroipcc_msg_send_reply_receive (cpg_inst->ipc_ctx, iov, 1,
  380. &res_lib_cpg_leave, sizeof (struct res_lib_cpg_leave));
  381. pthread_mutex_unlock (&cpg_inst->response_mutex);
  382. if (error != CS_OK) {
  383. goto error_exit;
  384. }
  385. } while (res_lib_cpg_leave.header.error == CPG_ERR_BUSY);
  386. error = res_lib_cpg_leave.header.error;
  387. error_exit:
  388. hdb_handle_put (&cpg_handle_t_db, handle);
  389. return (error);
  390. }
  391. cs_error_t cpg_membership_get (
  392. cpg_handle_t handle,
  393. struct cpg_name *group_name,
  394. struct cpg_address *member_list,
  395. int *member_list_entries)
  396. {
  397. cs_error_t error;
  398. struct cpg_inst *cpg_inst;
  399. struct iovec iov;
  400. struct req_lib_cpg_membership req_lib_cpg_membership_get;
  401. struct res_lib_cpg_confchg_callback res_lib_cpg_membership_get;
  402. unsigned int i;
  403. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  404. if (error != CS_OK) {
  405. return (error);
  406. }
  407. req_lib_cpg_membership_get.header.size = sizeof (coroipc_request_header_t);
  408. req_lib_cpg_membership_get.header.id = MESSAGE_REQ_CPG_MEMBERSHIP;
  409. iov.iov_base = &req_lib_cpg_membership_get;
  410. iov.iov_len = sizeof (coroipc_request_header_t);
  411. do {
  412. pthread_mutex_lock (&cpg_inst->response_mutex);
  413. error = coroipcc_msg_send_reply_receive (cpg_inst->ipc_ctx, &iov, 1,
  414. &res_lib_cpg_membership_get, sizeof (coroipc_response_header_t));
  415. pthread_mutex_unlock (&cpg_inst->response_mutex);
  416. if (error != CS_OK) {
  417. goto error_exit;
  418. }
  419. } while (res_lib_cpg_membership_get.header.error == CPG_ERR_BUSY);
  420. error = res_lib_cpg_membership_get.header.error;
  421. /*
  422. * Copy results to caller
  423. */
  424. *member_list_entries = res_lib_cpg_membership_get.member_list_entries;
  425. if (member_list) {
  426. for (i = 0; i < res_lib_cpg_membership_get.member_list_entries; i++) {
  427. marshall_from_mar_cpg_address_t (&member_list[i],
  428. &res_lib_cpg_membership_get.member_list[i]);
  429. }
  430. }
  431. error_exit:
  432. hdb_handle_put (&cpg_handle_t_db, handle);
  433. return (error);
  434. }
  435. cs_error_t cpg_local_get (
  436. cpg_handle_t handle,
  437. unsigned int *local_nodeid)
  438. {
  439. cs_error_t error;
  440. struct cpg_inst *cpg_inst;
  441. struct iovec iov;
  442. struct req_lib_cpg_local_get req_lib_cpg_local_get;
  443. struct res_lib_cpg_local_get res_lib_cpg_local_get;
  444. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  445. if (error != CS_OK) {
  446. return (error);
  447. }
  448. req_lib_cpg_local_get.header.size = sizeof (coroipc_request_header_t);
  449. req_lib_cpg_local_get.header.id = MESSAGE_REQ_CPG_LOCAL_GET;
  450. iov.iov_base = &req_lib_cpg_local_get;
  451. iov.iov_len = sizeof (struct req_lib_cpg_local_get);
  452. pthread_mutex_lock (&cpg_inst->response_mutex);
  453. error = coroipcc_msg_send_reply_receive (cpg_inst->ipc_ctx, &iov, 1,
  454. &res_lib_cpg_local_get, sizeof (res_lib_cpg_local_get));
  455. pthread_mutex_unlock (&cpg_inst->response_mutex);
  456. if (error != CS_OK) {
  457. goto error_exit;
  458. }
  459. error = res_lib_cpg_local_get.header.error;
  460. *local_nodeid = res_lib_cpg_local_get.local_nodeid;
  461. error_exit:
  462. hdb_handle_put (&cpg_handle_t_db, handle);
  463. return (error);
  464. }
  465. cs_error_t cpg_flow_control_state_get (
  466. cpg_handle_t handle,
  467. cpg_flow_control_state_t *flow_control_state)
  468. {
  469. cs_error_t error;
  470. struct cpg_inst *cpg_inst;
  471. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  472. if (error != CS_OK) {
  473. return (error);
  474. }
  475. *flow_control_state = coroipcc_dispatch_flow_control_get (cpg_inst->ipc_ctx);
  476. hdb_handle_put (&cpg_handle_t_db, handle);
  477. return (error);
  478. }
  479. cs_error_t cpg_zcb_alloc (
  480. cpg_handle_t handle,
  481. size_t size,
  482. void **buffer)
  483. {
  484. cs_error_t error;
  485. struct cpg_inst *cpg_inst;
  486. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  487. if (error != CS_OK) {
  488. return (error);
  489. }
  490. error = coroipcc_zcb_alloc (cpg_inst->ipc_ctx,
  491. buffer,
  492. size,
  493. sizeof (struct req_lib_cpg_mcast));
  494. hdb_handle_put (&cpg_handle_t_db, handle);
  495. *buffer = ((char *)*buffer) + sizeof (struct req_lib_cpg_mcast);
  496. return (error);
  497. }
  498. cs_error_t cpg_zcb_free (
  499. cpg_handle_t handle,
  500. void *buffer)
  501. {
  502. cs_error_t error;
  503. struct cpg_inst *cpg_inst;
  504. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  505. if (error != CS_OK) {
  506. return (error);
  507. }
  508. coroipcc_zcb_free (cpg_inst->ipc_ctx, ((char *)buffer) - sizeof (struct req_lib_cpg_mcast));
  509. hdb_handle_put (&cpg_handle_t_db, handle);
  510. return (error);
  511. }
  512. cs_error_t cpg_zcb_mcast_joined (
  513. cpg_handle_t handle,
  514. cpg_guarantee_t guarantee,
  515. void *msg,
  516. size_t msg_len)
  517. {
  518. cs_error_t error;
  519. struct cpg_inst *cpg_inst;
  520. struct req_lib_cpg_mcast *req_lib_cpg_mcast;
  521. struct res_lib_cpg_mcast res_lib_cpg_mcast;
  522. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  523. if (error != CS_OK) {
  524. return (error);
  525. }
  526. req_lib_cpg_mcast = (struct req_lib_cpg_mcast *)(((char *)msg) - sizeof (struct req_lib_cpg_mcast));
  527. req_lib_cpg_mcast->header.size = sizeof (struct req_lib_cpg_mcast) +
  528. msg_len;
  529. req_lib_cpg_mcast->header.id = MESSAGE_REQ_CPG_MCAST;
  530. req_lib_cpg_mcast->guarantee = guarantee;
  531. req_lib_cpg_mcast->msglen = msg_len;
  532. pthread_mutex_lock (&cpg_inst->response_mutex);
  533. error = coroipcc_zcb_msg_send_reply_receive (
  534. cpg_inst->ipc_ctx,
  535. req_lib_cpg_mcast,
  536. &res_lib_cpg_mcast,
  537. sizeof (res_lib_cpg_mcast));
  538. pthread_mutex_unlock (&cpg_inst->response_mutex);
  539. if (error != CS_OK) {
  540. goto error_exit;
  541. }
  542. error = res_lib_cpg_mcast.header.error;
  543. error_exit:
  544. hdb_handle_put (&cpg_handle_t_db, handle);
  545. return (error);
  546. }
  547. cs_error_t cpg_mcast_joined (
  548. cpg_handle_t handle,
  549. cpg_guarantee_t guarantee,
  550. const struct iovec *iovec,
  551. unsigned int iov_len)
  552. {
  553. int i;
  554. cs_error_t error;
  555. struct cpg_inst *cpg_inst;
  556. struct iovec iov[64];
  557. struct req_lib_cpg_mcast req_lib_cpg_mcast;
  558. struct res_lib_cpg_mcast res_lib_cpg_mcast;
  559. size_t msg_len = 0;
  560. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  561. if (error != CS_OK) {
  562. return (error);
  563. }
  564. for (i = 0; i < iov_len; i++ ) {
  565. msg_len += iovec[i].iov_len;
  566. }
  567. req_lib_cpg_mcast.header.size = sizeof (struct req_lib_cpg_mcast) +
  568. msg_len;
  569. req_lib_cpg_mcast.header.id = MESSAGE_REQ_CPG_MCAST;
  570. req_lib_cpg_mcast.guarantee = guarantee;
  571. req_lib_cpg_mcast.msglen = msg_len;
  572. iov[0].iov_base = &req_lib_cpg_mcast;
  573. iov[0].iov_len = sizeof (struct req_lib_cpg_mcast);
  574. memcpy (&iov[1], iovec, iov_len * sizeof (struct iovec));
  575. pthread_mutex_lock (&cpg_inst->response_mutex);
  576. error = coroipcc_msg_send_reply_receive (cpg_inst->ipc_ctx, iov,
  577. iov_len + 1, &res_lib_cpg_mcast, sizeof (res_lib_cpg_mcast));
  578. pthread_mutex_unlock (&cpg_inst->response_mutex);
  579. if (error != CS_OK) {
  580. goto error_exit;
  581. }
  582. error = res_lib_cpg_mcast.header.error;
  583. error_exit:
  584. hdb_handle_put (&cpg_handle_t_db, handle);
  585. return (error);
  586. }
  587. /** @} */