cpg.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  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: Christine Caulfield (ccaulfi@redhat.com)
  9. * Author: Jan Friesse (jfriesse@redhat.com)
  10. *
  11. * This software licensed under BSD license, the text of which follows:
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright notice,
  17. * this list of conditions and the following disclaimer.
  18. * - Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  22. * contributors may be used to endorse or promote products derived from this
  23. * software without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  29. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  30. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  32. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  33. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  34. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  35. * THE POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. /*
  38. * Provides a closed process group API using the coroipcc executive
  39. */
  40. #include <config.h>
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include <unistd.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/list.h>
  53. #include <corosync/cpg.h>
  54. #include <corosync/ipc_cpg.h>
  55. #include "util.h"
  56. struct cpg_inst {
  57. hdb_handle_t handle;
  58. int finalize;
  59. cpg_callbacks_t callbacks;
  60. void *context;
  61. struct list_head iteration_list_head;
  62. };
  63. DECLARE_HDB_DATABASE(cpg_handle_t_db,NULL);
  64. struct cpg_iteration_instance_t {
  65. cpg_iteration_handle_t cpg_iteration_handle;
  66. hdb_handle_t conn_handle;
  67. hdb_handle_t executive_iteration_handle;
  68. struct list_head list;
  69. };
  70. DECLARE_HDB_DATABASE(cpg_iteration_handle_t_db,NULL);
  71. /*
  72. * Internal (not visible by API) functions
  73. */
  74. static void cpg_iteration_instance_finalize (struct cpg_iteration_instance_t *cpg_iteration_instance)
  75. {
  76. list_del (&cpg_iteration_instance->list);
  77. hdb_handle_destroy (&cpg_iteration_handle_t_db, cpg_iteration_instance->cpg_iteration_handle);
  78. }
  79. static void cpg_inst_finalize (struct cpg_inst *cpg_inst, hdb_handle_t handle)
  80. {
  81. struct list_head *iter, *iter_next;
  82. struct cpg_iteration_instance_t *cpg_iteration_instance;
  83. /*
  84. * Traverse thru iteration instances and delete them
  85. */
  86. for (iter = cpg_inst->iteration_list_head.next; iter != &cpg_inst->iteration_list_head;iter = iter_next) {
  87. iter_next = iter->next;
  88. cpg_iteration_instance = list_entry (iter, struct cpg_iteration_instance_t, list);
  89. cpg_iteration_instance_finalize (cpg_iteration_instance);
  90. }
  91. hdb_handle_destroy (&cpg_handle_t_db, handle);
  92. }
  93. /**
  94. * @defgroup cpg_coroipcc The closed process group API
  95. * @ingroup coroipcc
  96. *
  97. * @{
  98. */
  99. cs_error_t cpg_initialize (
  100. cpg_handle_t *handle,
  101. cpg_callbacks_t *callbacks)
  102. {
  103. cs_error_t error;
  104. struct cpg_inst *cpg_inst;
  105. error = hdb_error_to_cs (hdb_handle_create (&cpg_handle_t_db, sizeof (struct cpg_inst), handle));
  106. if (error != CS_OK) {
  107. goto error_no_destroy;
  108. }
  109. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, *handle, (void *)&cpg_inst));
  110. if (error != CS_OK) {
  111. goto error_destroy;
  112. }
  113. error = coroipcc_service_connect (
  114. COROSYNC_SOCKET_NAME,
  115. CPG_SERVICE,
  116. IPC_REQUEST_SIZE,
  117. IPC_RESPONSE_SIZE,
  118. IPC_DISPATCH_SIZE,
  119. &cpg_inst->handle);
  120. if (error != CS_OK) {
  121. goto error_put_destroy;
  122. }
  123. if (callbacks) {
  124. memcpy (&cpg_inst->callbacks, callbacks, sizeof (cpg_callbacks_t));
  125. }
  126. list_init(&cpg_inst->iteration_list_head);
  127. hdb_handle_put (&cpg_handle_t_db, *handle);
  128. return (CS_OK);
  129. error_put_destroy:
  130. hdb_handle_put (&cpg_handle_t_db, *handle);
  131. error_destroy:
  132. hdb_handle_destroy (&cpg_handle_t_db, *handle);
  133. error_no_destroy:
  134. return (error);
  135. }
  136. cs_error_t cpg_finalize (
  137. cpg_handle_t handle)
  138. {
  139. struct cpg_inst *cpg_inst;
  140. cs_error_t error;
  141. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  142. if (error != CS_OK) {
  143. return (error);
  144. }
  145. /*
  146. * Another thread has already started finalizing
  147. */
  148. if (cpg_inst->finalize) {
  149. hdb_handle_put (&cpg_handle_t_db, handle);
  150. return (CPG_ERR_BAD_HANDLE);
  151. }
  152. cpg_inst->finalize = 1;
  153. coroipcc_service_disconnect (cpg_inst->handle);
  154. cpg_inst_finalize (cpg_inst, handle);
  155. hdb_handle_put (&cpg_handle_t_db, handle);
  156. return (CPG_OK);
  157. }
  158. cs_error_t cpg_fd_get (
  159. cpg_handle_t handle,
  160. int *fd)
  161. {
  162. cs_error_t error;
  163. struct cpg_inst *cpg_inst;
  164. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  165. if (error != CS_OK) {
  166. return (error);
  167. }
  168. error = coroipcc_fd_get (cpg_inst->handle, fd);
  169. hdb_handle_put (&cpg_handle_t_db, handle);
  170. return (error);
  171. }
  172. cs_error_t cpg_context_get (
  173. cpg_handle_t handle,
  174. void **context)
  175. {
  176. cs_error_t error;
  177. struct cpg_inst *cpg_inst;
  178. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  179. if (error != CS_OK) {
  180. return (error);
  181. }
  182. *context = cpg_inst->context;
  183. hdb_handle_put (&cpg_handle_t_db, handle);
  184. return (CS_OK);
  185. }
  186. cs_error_t cpg_context_set (
  187. cpg_handle_t handle,
  188. void *context)
  189. {
  190. cs_error_t error;
  191. struct cpg_inst *cpg_inst;
  192. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  193. if (error != CS_OK) {
  194. return (error);
  195. }
  196. cpg_inst->context = context;
  197. hdb_handle_put (&cpg_handle_t_db, handle);
  198. return (CS_OK);
  199. }
  200. cs_error_t cpg_dispatch (
  201. cpg_handle_t handle,
  202. cs_dispatch_flags_t dispatch_types)
  203. {
  204. int timeout = -1;
  205. cs_error_t error;
  206. int cont = 1; /* always continue do loop except when set to 0 */
  207. struct cpg_inst *cpg_inst;
  208. struct res_lib_cpg_confchg_callback *res_cpg_confchg_callback;
  209. struct res_lib_cpg_deliver_callback *res_cpg_deliver_callback;
  210. cpg_callbacks_t callbacks;
  211. coroipc_response_header_t *dispatch_data;
  212. int ignore_dispatch = 0;
  213. struct cpg_address member_list[CPG_MEMBERS_MAX];
  214. struct cpg_address left_list[CPG_MEMBERS_MAX];
  215. struct cpg_address joined_list[CPG_MEMBERS_MAX];
  216. struct cpg_name group_name;
  217. mar_cpg_address_t *left_list_start;
  218. mar_cpg_address_t *joined_list_start;
  219. unsigned int i;
  220. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  221. if (error != CS_OK) {
  222. return (error);
  223. }
  224. /*
  225. * Timeout instantly for SA_DISPATCH_ONE or SA_DISPATCH_ALL and
  226. * wait indefinately for SA_DISPATCH_BLOCKING
  227. */
  228. if (dispatch_types == CPG_DISPATCH_ALL) {
  229. timeout = 0;
  230. }
  231. do {
  232. error = coroipcc_dispatch_get (
  233. cpg_inst->handle,
  234. (void **)&dispatch_data,
  235. timeout);
  236. if (error == CS_ERR_BAD_HANDLE) {
  237. error = CS_OK;
  238. goto error_put;
  239. }
  240. if (error != CS_OK) {
  241. goto error_put;
  242. }
  243. if (dispatch_data == NULL) {
  244. if (dispatch_types == CPG_DISPATCH_ALL) {
  245. break; /* exit do while cont is 1 loop */
  246. } else {
  247. continue; /* next poll */
  248. }
  249. }
  250. /*
  251. * Make copy of callbacks, message data, unlock instance, and call callback
  252. * A risk of this dispatch method is that the callback routines may
  253. * operate at the same time that cpgFinalize has been called.
  254. */
  255. memcpy (&callbacks, &cpg_inst->callbacks, sizeof (cpg_callbacks_t));
  256. /*
  257. * Dispatch incoming message
  258. */
  259. switch (dispatch_data->id) {
  260. case MESSAGE_RES_CPG_DELIVER_CALLBACK:
  261. if (callbacks.cpg_deliver_fn == NULL) {
  262. continue;
  263. }
  264. res_cpg_deliver_callback = (struct res_lib_cpg_deliver_callback *)dispatch_data;
  265. marshall_from_mar_cpg_name_t (
  266. &group_name,
  267. &res_cpg_deliver_callback->group_name);
  268. callbacks.cpg_deliver_fn (handle,
  269. &group_name,
  270. res_cpg_deliver_callback->nodeid,
  271. res_cpg_deliver_callback->pid,
  272. &res_cpg_deliver_callback->message,
  273. res_cpg_deliver_callback->msglen);
  274. break;
  275. case MESSAGE_RES_CPG_CONFCHG_CALLBACK:
  276. if (callbacks.cpg_confchg_fn == NULL) {
  277. continue;
  278. }
  279. res_cpg_confchg_callback = (struct res_lib_cpg_confchg_callback *)dispatch_data;
  280. for (i = 0; i < res_cpg_confchg_callback->member_list_entries; i++) {
  281. marshall_from_mar_cpg_address_t (&member_list[i],
  282. &res_cpg_confchg_callback->member_list[i]);
  283. }
  284. left_list_start = res_cpg_confchg_callback->member_list +
  285. res_cpg_confchg_callback->member_list_entries;
  286. for (i = 0; i < res_cpg_confchg_callback->left_list_entries; i++) {
  287. marshall_from_mar_cpg_address_t (&left_list[i],
  288. &left_list_start[i]);
  289. }
  290. joined_list_start = res_cpg_confchg_callback->member_list +
  291. res_cpg_confchg_callback->member_list_entries +
  292. res_cpg_confchg_callback->left_list_entries;
  293. for (i = 0; i < res_cpg_confchg_callback->joined_list_entries; i++) {
  294. marshall_from_mar_cpg_address_t (&joined_list[i],
  295. &joined_list_start[i]);
  296. }
  297. marshall_from_mar_cpg_name_t (
  298. &group_name,
  299. &res_cpg_confchg_callback->group_name);
  300. callbacks.cpg_confchg_fn (handle,
  301. &group_name,
  302. member_list,
  303. res_cpg_confchg_callback->member_list_entries,
  304. left_list,
  305. res_cpg_confchg_callback->left_list_entries,
  306. joined_list,
  307. res_cpg_confchg_callback->joined_list_entries);
  308. break;
  309. default:
  310. coroipcc_dispatch_put (cpg_inst->handle);
  311. error = CS_ERR_LIBRARY;
  312. goto error_put;
  313. break;
  314. }
  315. coroipcc_dispatch_put (cpg_inst->handle);
  316. /*
  317. * Determine if more messages should be processed
  318. * */
  319. switch (dispatch_types) {
  320. case CPG_DISPATCH_ONE:
  321. if (ignore_dispatch) {
  322. ignore_dispatch = 0;
  323. } else {
  324. cont = 0;
  325. }
  326. break;
  327. case CPG_DISPATCH_ALL:
  328. if (ignore_dispatch) {
  329. ignore_dispatch = 0;
  330. }
  331. break;
  332. case CPG_DISPATCH_BLOCKING:
  333. break;
  334. }
  335. } while (cont);
  336. error_put:
  337. hdb_handle_put (&cpg_handle_t_db, handle);
  338. return (error);
  339. }
  340. cs_error_t cpg_join (
  341. cpg_handle_t handle,
  342. const struct cpg_name *group)
  343. {
  344. cs_error_t error;
  345. struct cpg_inst *cpg_inst;
  346. struct iovec iov[2];
  347. struct req_lib_cpg_join req_lib_cpg_join;
  348. struct res_lib_cpg_join res_lib_cpg_join;
  349. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  350. if (error != CS_OK) {
  351. return (error);
  352. }
  353. /* Now join */
  354. req_lib_cpg_join.header.size = sizeof (struct req_lib_cpg_join);
  355. req_lib_cpg_join.header.id = MESSAGE_REQ_CPG_JOIN;
  356. req_lib_cpg_join.pid = getpid();
  357. marshall_to_mar_cpg_name_t (&req_lib_cpg_join.group_name,
  358. group);
  359. iov[0].iov_base = (void *)&req_lib_cpg_join;
  360. iov[0].iov_len = sizeof (struct req_lib_cpg_join);
  361. do {
  362. error = coroipcc_msg_send_reply_receive (cpg_inst->handle, iov, 1,
  363. &res_lib_cpg_join, sizeof (struct res_lib_cpg_join));
  364. if (error != CS_OK) {
  365. goto error_exit;
  366. }
  367. } while (res_lib_cpg_join.header.error == CPG_ERR_BUSY);
  368. error = res_lib_cpg_join.header.error;
  369. error_exit:
  370. hdb_handle_put (&cpg_handle_t_db, handle);
  371. return (error);
  372. }
  373. cs_error_t cpg_leave (
  374. cpg_handle_t handle,
  375. const struct cpg_name *group)
  376. {
  377. cs_error_t error;
  378. struct cpg_inst *cpg_inst;
  379. struct iovec iov[2];
  380. struct req_lib_cpg_leave req_lib_cpg_leave;
  381. struct res_lib_cpg_leave res_lib_cpg_leave;
  382. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  383. if (error != CS_OK) {
  384. return (error);
  385. }
  386. req_lib_cpg_leave.header.size = sizeof (struct req_lib_cpg_leave);
  387. req_lib_cpg_leave.header.id = MESSAGE_REQ_CPG_LEAVE;
  388. req_lib_cpg_leave.pid = getpid();
  389. marshall_to_mar_cpg_name_t (&req_lib_cpg_leave.group_name,
  390. group);
  391. iov[0].iov_base = (void *)&req_lib_cpg_leave;
  392. iov[0].iov_len = sizeof (struct req_lib_cpg_leave);
  393. do {
  394. error = coroipcc_msg_send_reply_receive (cpg_inst->handle, iov, 1,
  395. &res_lib_cpg_leave, sizeof (struct res_lib_cpg_leave));
  396. if (error != CS_OK) {
  397. goto error_exit;
  398. }
  399. } while (res_lib_cpg_leave.header.error == CPG_ERR_BUSY);
  400. error = res_lib_cpg_leave.header.error;
  401. error_exit:
  402. hdb_handle_put (&cpg_handle_t_db, handle);
  403. return (error);
  404. }
  405. cs_error_t cpg_membership_get (
  406. cpg_handle_t handle,
  407. struct cpg_name *group_name,
  408. struct cpg_address *member_list,
  409. int *member_list_entries)
  410. {
  411. cs_error_t error;
  412. struct cpg_inst *cpg_inst;
  413. struct iovec iov;
  414. struct req_lib_cpg_membership req_lib_cpg_membership_get;
  415. struct res_lib_cpg_confchg_callback res_lib_cpg_membership_get;
  416. unsigned int i;
  417. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  418. if (error != CS_OK) {
  419. return (error);
  420. }
  421. req_lib_cpg_membership_get.header.size = sizeof (coroipc_request_header_t);
  422. req_lib_cpg_membership_get.header.id = MESSAGE_REQ_CPG_MEMBERSHIP;
  423. iov.iov_base = (void *)&req_lib_cpg_membership_get;
  424. iov.iov_len = sizeof (coroipc_request_header_t);
  425. do {
  426. error = coroipcc_msg_send_reply_receive (cpg_inst->handle, &iov, 1,
  427. &res_lib_cpg_membership_get, sizeof (coroipc_response_header_t));
  428. if (error != CS_OK) {
  429. goto error_exit;
  430. }
  431. } while (res_lib_cpg_membership_get.header.error == CPG_ERR_BUSY);
  432. error = res_lib_cpg_membership_get.header.error;
  433. /*
  434. * Copy results to caller
  435. */
  436. *member_list_entries = res_lib_cpg_membership_get.member_list_entries;
  437. if (member_list) {
  438. for (i = 0; i < res_lib_cpg_membership_get.member_list_entries; i++) {
  439. marshall_from_mar_cpg_address_t (&member_list[i],
  440. &res_lib_cpg_membership_get.member_list[i]);
  441. }
  442. }
  443. error_exit:
  444. hdb_handle_put (&cpg_handle_t_db, handle);
  445. return (error);
  446. }
  447. cs_error_t cpg_local_get (
  448. cpg_handle_t handle,
  449. unsigned int *local_nodeid)
  450. {
  451. cs_error_t error;
  452. struct cpg_inst *cpg_inst;
  453. struct iovec iov;
  454. struct req_lib_cpg_local_get req_lib_cpg_local_get;
  455. struct res_lib_cpg_local_get res_lib_cpg_local_get;
  456. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  457. if (error != CS_OK) {
  458. return (error);
  459. }
  460. req_lib_cpg_local_get.header.size = sizeof (coroipc_request_header_t);
  461. req_lib_cpg_local_get.header.id = MESSAGE_REQ_CPG_LOCAL_GET;
  462. iov.iov_base = (void *)&req_lib_cpg_local_get;
  463. iov.iov_len = sizeof (struct req_lib_cpg_local_get);
  464. error = coroipcc_msg_send_reply_receive (cpg_inst->handle, &iov, 1,
  465. &res_lib_cpg_local_get, sizeof (res_lib_cpg_local_get));
  466. if (error != CS_OK) {
  467. goto error_exit;
  468. }
  469. error = res_lib_cpg_local_get.header.error;
  470. *local_nodeid = res_lib_cpg_local_get.local_nodeid;
  471. error_exit:
  472. hdb_handle_put (&cpg_handle_t_db, handle);
  473. return (error);
  474. }
  475. cs_error_t cpg_flow_control_state_get (
  476. cpg_handle_t handle,
  477. cpg_flow_control_state_t *flow_control_state)
  478. {
  479. cs_error_t error;
  480. struct cpg_inst *cpg_inst;
  481. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  482. if (error != CS_OK) {
  483. return (error);
  484. }
  485. error = coroipcc_dispatch_flow_control_get (cpg_inst->handle, (unsigned int *)flow_control_state);
  486. hdb_handle_put (&cpg_handle_t_db, handle);
  487. return (error);
  488. }
  489. cs_error_t cpg_zcb_alloc (
  490. cpg_handle_t handle,
  491. size_t size,
  492. void **buffer)
  493. {
  494. cs_error_t error;
  495. struct cpg_inst *cpg_inst;
  496. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  497. if (error != CS_OK) {
  498. return (error);
  499. }
  500. error = coroipcc_zcb_alloc (cpg_inst->handle,
  501. buffer,
  502. size,
  503. sizeof (struct req_lib_cpg_mcast));
  504. hdb_handle_put (&cpg_handle_t_db, handle);
  505. *buffer = ((char *)*buffer) + sizeof (struct req_lib_cpg_mcast);
  506. return (error);
  507. }
  508. cs_error_t cpg_zcb_free (
  509. cpg_handle_t handle,
  510. void *buffer)
  511. {
  512. cs_error_t error;
  513. struct cpg_inst *cpg_inst;
  514. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  515. if (error != CS_OK) {
  516. return (error);
  517. }
  518. coroipcc_zcb_free (cpg_inst->handle, ((char *)buffer) - sizeof (struct req_lib_cpg_mcast));
  519. hdb_handle_put (&cpg_handle_t_db, handle);
  520. return (error);
  521. }
  522. cs_error_t cpg_zcb_mcast_joined (
  523. cpg_handle_t handle,
  524. cpg_guarantee_t guarantee,
  525. void *msg,
  526. size_t msg_len)
  527. {
  528. cs_error_t error;
  529. struct cpg_inst *cpg_inst;
  530. struct req_lib_cpg_mcast *req_lib_cpg_mcast;
  531. struct res_lib_cpg_mcast res_lib_cpg_mcast;
  532. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  533. if (error != CS_OK) {
  534. return (error);
  535. }
  536. req_lib_cpg_mcast = (struct req_lib_cpg_mcast *)(((char *)msg) - sizeof (struct req_lib_cpg_mcast));
  537. req_lib_cpg_mcast->header.size = sizeof (struct req_lib_cpg_mcast) +
  538. msg_len;
  539. req_lib_cpg_mcast->header.id = MESSAGE_REQ_CPG_MCAST;
  540. req_lib_cpg_mcast->guarantee = guarantee;
  541. req_lib_cpg_mcast->msglen = msg_len;
  542. error = coroipcc_zcb_msg_send_reply_receive (
  543. cpg_inst->handle,
  544. req_lib_cpg_mcast,
  545. &res_lib_cpg_mcast,
  546. sizeof (res_lib_cpg_mcast));
  547. if (error != CS_OK) {
  548. goto error_exit;
  549. }
  550. error = res_lib_cpg_mcast.header.error;
  551. error_exit:
  552. hdb_handle_put (&cpg_handle_t_db, handle);
  553. return (error);
  554. }
  555. cs_error_t cpg_mcast_joined (
  556. cpg_handle_t handle,
  557. cpg_guarantee_t guarantee,
  558. const struct iovec *iovec,
  559. unsigned int iov_len)
  560. {
  561. int i;
  562. cs_error_t error;
  563. struct cpg_inst *cpg_inst;
  564. struct iovec iov[64];
  565. struct req_lib_cpg_mcast req_lib_cpg_mcast;
  566. struct res_lib_cpg_mcast res_lib_cpg_mcast;
  567. size_t msg_len = 0;
  568. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  569. if (error != CS_OK) {
  570. return (error);
  571. }
  572. for (i = 0; i < iov_len; i++ ) {
  573. msg_len += iovec[i].iov_len;
  574. }
  575. req_lib_cpg_mcast.header.size = sizeof (struct req_lib_cpg_mcast) +
  576. msg_len;
  577. req_lib_cpg_mcast.header.id = MESSAGE_REQ_CPG_MCAST;
  578. req_lib_cpg_mcast.guarantee = guarantee;
  579. req_lib_cpg_mcast.msglen = msg_len;
  580. iov[0].iov_base = (void *)&req_lib_cpg_mcast;
  581. iov[0].iov_len = sizeof (struct req_lib_cpg_mcast);
  582. memcpy (&iov[1], iovec, iov_len * sizeof (struct iovec));
  583. error = coroipcc_msg_send_reply_receive (cpg_inst->handle, iov,
  584. iov_len + 1, &res_lib_cpg_mcast, sizeof (res_lib_cpg_mcast));
  585. if (error != CS_OK) {
  586. goto error_exit;
  587. }
  588. error = res_lib_cpg_mcast.header.error;
  589. error_exit:
  590. hdb_handle_put (&cpg_handle_t_db, handle);
  591. return (error);
  592. }
  593. cs_error_t cpg_iteration_initialize(
  594. cpg_handle_t handle,
  595. cpg_iteration_type_t iteration_type,
  596. const struct cpg_name *group,
  597. cpg_iteration_handle_t *cpg_iteration_handle)
  598. {
  599. cs_error_t error;
  600. struct iovec iov;
  601. struct cpg_inst *cpg_inst;
  602. struct cpg_iteration_instance_t *cpg_iteration_instance;
  603. struct req_lib_cpg_iterationinitialize req_lib_cpg_iterationinitialize;
  604. struct res_lib_cpg_iterationinitialize res_lib_cpg_iterationinitialize;
  605. if (cpg_iteration_handle == NULL) {
  606. return (CS_ERR_INVALID_PARAM);
  607. }
  608. if ((iteration_type == CPG_ITERATION_ONE_GROUP && group == NULL) ||
  609. (iteration_type != CPG_ITERATION_ONE_GROUP && group != NULL)) {
  610. return (CS_ERR_INVALID_PARAM);
  611. }
  612. if (iteration_type != CPG_ITERATION_NAME_ONLY && iteration_type != CPG_ITERATION_ONE_GROUP &&
  613. iteration_type != CPG_ITERATION_ALL) {
  614. return (CS_ERR_INVALID_PARAM);
  615. }
  616. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  617. if (error != CS_OK) {
  618. return (error);
  619. }
  620. error = hdb_error_to_cs (hdb_handle_create (&cpg_iteration_handle_t_db,
  621. sizeof (struct cpg_iteration_instance_t), cpg_iteration_handle));
  622. if (error != CS_OK) {
  623. goto error_put_cpg_db;
  624. }
  625. error = hdb_error_to_cs (hdb_handle_get (&cpg_iteration_handle_t_db, *cpg_iteration_handle,
  626. (void *)&cpg_iteration_instance));
  627. if (error != CS_OK) {
  628. goto error_destroy;
  629. }
  630. cpg_iteration_instance->conn_handle = cpg_inst->handle;
  631. list_init (&cpg_iteration_instance->list);
  632. req_lib_cpg_iterationinitialize.header.size = sizeof (struct req_lib_cpg_iterationinitialize);
  633. req_lib_cpg_iterationinitialize.header.id = MESSAGE_REQ_CPG_ITERATIONINITIALIZE;
  634. req_lib_cpg_iterationinitialize.iteration_type = iteration_type;
  635. if (group) {
  636. marshall_to_mar_cpg_name_t (&req_lib_cpg_iterationinitialize.group_name, group);
  637. }
  638. iov.iov_base = (void *)&req_lib_cpg_iterationinitialize;
  639. iov.iov_len = sizeof (struct req_lib_cpg_iterationinitialize);
  640. error = coroipcc_msg_send_reply_receive (cpg_inst->handle,
  641. &iov,
  642. 1,
  643. &res_lib_cpg_iterationinitialize,
  644. sizeof (struct res_lib_cpg_iterationinitialize));
  645. if (error != CS_OK) {
  646. goto error_put_destroy;
  647. }
  648. cpg_iteration_instance->executive_iteration_handle =
  649. res_lib_cpg_iterationinitialize.iteration_handle;
  650. cpg_iteration_instance->cpg_iteration_handle = *cpg_iteration_handle;
  651. list_add (&cpg_iteration_instance->list, &cpg_inst->iteration_list_head);
  652. hdb_handle_put (&cpg_iteration_handle_t_db, *cpg_iteration_handle);
  653. hdb_handle_put (&cpg_handle_t_db, handle);
  654. return (res_lib_cpg_iterationinitialize.header.error);
  655. error_put_destroy:
  656. hdb_handle_put (&cpg_iteration_handle_t_db, *cpg_iteration_handle);
  657. error_destroy:
  658. hdb_handle_destroy (&cpg_iteration_handle_t_db, *cpg_iteration_handle);
  659. error_put_cpg_db:
  660. hdb_handle_put (&cpg_handle_t_db, handle);
  661. return (error);
  662. }
  663. cs_error_t cpg_iteration_next(
  664. cpg_iteration_handle_t handle,
  665. struct cpg_iteration_description_t *description)
  666. {
  667. cs_error_t error;
  668. struct cpg_iteration_instance_t *cpg_iteration_instance;
  669. struct req_lib_cpg_iterationnext req_lib_cpg_iterationnext;
  670. struct res_lib_cpg_iterationnext *res_lib_cpg_iterationnext;
  671. struct iovec iov;
  672. void *return_address;
  673. if (description == NULL) {
  674. return CS_ERR_INVALID_PARAM;
  675. }
  676. error = hdb_error_to_cs (hdb_handle_get (&cpg_iteration_handle_t_db, handle,
  677. (void *)&cpg_iteration_instance));
  678. if (error != CS_OK) {
  679. goto error_exit;
  680. }
  681. req_lib_cpg_iterationnext.header.size = sizeof (struct req_lib_cpg_iterationnext);
  682. req_lib_cpg_iterationnext.header.id = MESSAGE_REQ_CPG_ITERATIONNEXT;
  683. req_lib_cpg_iterationnext.iteration_handle = cpg_iteration_instance->executive_iteration_handle;
  684. iov.iov_base = (void *)&req_lib_cpg_iterationnext;
  685. iov.iov_len = sizeof (struct req_lib_cpg_iterationnext);
  686. error = coroipcc_msg_send_reply_receive_in_buf_get (cpg_iteration_instance->conn_handle,
  687. &iov,
  688. 1,
  689. &return_address);
  690. res_lib_cpg_iterationnext = return_address;
  691. if (error != CS_OK) {
  692. goto error_put;
  693. }
  694. marshall_from_mar_cpg_iteration_description_t(
  695. description,
  696. &res_lib_cpg_iterationnext->description);
  697. error = (error == CS_OK ? res_lib_cpg_iterationnext->header.error : error);
  698. coroipcc_msg_send_reply_receive_in_buf_put(
  699. cpg_iteration_instance->conn_handle);
  700. error_put:
  701. hdb_handle_put (&cpg_iteration_handle_t_db, handle);
  702. error_exit:
  703. return (error);
  704. }
  705. cs_error_t cpg_iteration_finalize (
  706. cpg_iteration_handle_t handle)
  707. {
  708. cs_error_t error;
  709. struct iovec iov;
  710. struct cpg_iteration_instance_t *cpg_iteration_instance;
  711. struct req_lib_cpg_iterationfinalize req_lib_cpg_iterationfinalize;
  712. struct res_lib_cpg_iterationfinalize res_lib_cpg_iterationfinalize;
  713. error = hdb_error_to_cs (hdb_handle_get (&cpg_iteration_handle_t_db, handle,
  714. (void *)&cpg_iteration_instance));
  715. if (error != CS_OK) {
  716. goto error_exit;
  717. }
  718. req_lib_cpg_iterationfinalize.header.size = sizeof (struct req_lib_cpg_iterationfinalize);
  719. req_lib_cpg_iterationfinalize.header.id = MESSAGE_REQ_CPG_ITERATIONFINALIZE;
  720. req_lib_cpg_iterationfinalize.iteration_handle = cpg_iteration_instance->executive_iteration_handle;
  721. iov.iov_base = (void *)&req_lib_cpg_iterationfinalize;
  722. iov.iov_len = sizeof (struct req_lib_cpg_iterationfinalize);
  723. error = coroipcc_msg_send_reply_receive (cpg_iteration_instance->conn_handle,
  724. &iov,
  725. 1,
  726. &res_lib_cpg_iterationfinalize,
  727. sizeof (struct req_lib_cpg_iterationfinalize));
  728. if (error != CS_OK) {
  729. goto error_put;
  730. }
  731. cpg_iteration_instance_finalize (cpg_iteration_instance);
  732. hdb_handle_put (&cpg_iteration_handle_t_db, cpg_iteration_instance->cpg_iteration_handle);
  733. return (res_lib_cpg_iterationfinalize.header.error);
  734. error_put:
  735. hdb_handle_put (&cpg_iteration_handle_t_db, handle);
  736. error_exit:
  737. return (error);
  738. }
  739. /** @} */