cpg.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. /*
  2. * vi: set autoindent tabstop=4 shiftwidth=4 :
  3. *
  4. * Copyright (c) 2006-2008 Red Hat, Inc.
  5. *
  6. * All rights reserved.
  7. *
  8. * Author: Christine Caulfield (ccaulfie@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 corosync executive
  38. */
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #include <unistd.h>
  42. #include <pthread.h>
  43. #include <sys/types.h>
  44. #include <sys/socket.h>
  45. #include <errno.h>
  46. #include <corosync/corotypes.h>
  47. #include <corosync/cpg.h>
  48. #include <corosync/ipc_cpg.h>
  49. #include <corosync/mar_cpg.h>
  50. #include <corosync/ais_util.h>
  51. struct cpg_inst {
  52. int response_fd;
  53. int dispatch_fd;
  54. int finalize;
  55. cpg_flow_control_state_t flow_control_state;
  56. cpg_callbacks_t callbacks;
  57. void *context;
  58. pthread_mutex_t response_mutex;
  59. pthread_mutex_t dispatch_mutex;
  60. };
  61. static void cpg_instance_destructor (void *instance);
  62. static struct saHandleDatabase cpg_handle_t_db = {
  63. .handleCount = 0,
  64. .handles = 0,
  65. .mutex = PTHREAD_MUTEX_INITIALIZER,
  66. .handleInstanceDestructor = cpg_instance_destructor
  67. };
  68. /*
  69. * Clean up function for a cpg instance (cpg_initialize) handle
  70. */
  71. static void cpg_instance_destructor (void *instance)
  72. {
  73. struct cpg_inst *cpg_inst = instance;
  74. pthread_mutex_destroy (&cpg_inst->response_mutex);
  75. pthread_mutex_destroy (&cpg_inst->dispatch_mutex);
  76. }
  77. /**
  78. * @defgroup cpg_corosync The closed process group API
  79. * @ingroup corosync
  80. *
  81. * @{
  82. */
  83. cs_error_t cpg_initialize (
  84. cpg_handle_t *handle,
  85. cpg_callbacks_t *callbacks)
  86. {
  87. cs_error_t error;
  88. struct cpg_inst *cpg_inst;
  89. error = saHandleCreate (&cpg_handle_t_db, sizeof (struct cpg_inst), handle);
  90. if (error != CS_OK) {
  91. goto error_no_destroy;
  92. }
  93. error = saHandleInstanceGet (&cpg_handle_t_db, *handle, (void *)&cpg_inst);
  94. if (error != CS_OK) {
  95. goto error_destroy;
  96. }
  97. error = saServiceConnect (&cpg_inst->dispatch_fd,
  98. &cpg_inst->response_fd,
  99. CPG_SERVICE);
  100. if (error != CS_OK) {
  101. goto error_put_destroy;
  102. }
  103. memcpy (&cpg_inst->callbacks, callbacks, sizeof (cpg_callbacks_t));
  104. pthread_mutex_init (&cpg_inst->response_mutex, NULL);
  105. pthread_mutex_init (&cpg_inst->dispatch_mutex, NULL);
  106. (void)saHandleInstancePut (&cpg_handle_t_db, *handle);
  107. return (CS_OK);
  108. error_put_destroy:
  109. (void)saHandleInstancePut (&cpg_handle_t_db, *handle);
  110. error_destroy:
  111. (void)saHandleDestroy (&cpg_handle_t_db, *handle);
  112. error_no_destroy:
  113. return (error);
  114. }
  115. cs_error_t cpg_finalize (
  116. cpg_handle_t handle)
  117. {
  118. struct cpg_inst *cpg_inst;
  119. cs_error_t error;
  120. error = saHandleInstanceGet (&cpg_handle_t_db, handle, (void *)&cpg_inst);
  121. if (error != CS_OK) {
  122. return (error);
  123. }
  124. pthread_mutex_lock (&cpg_inst->response_mutex);
  125. /*
  126. * Another thread has already started finalizing
  127. */
  128. if (cpg_inst->finalize) {
  129. pthread_mutex_unlock (&cpg_inst->response_mutex);
  130. (void)saHandleInstancePut (&cpg_handle_t_db, handle);
  131. return (CS_ERR_BAD_HANDLE);
  132. }
  133. cpg_inst->finalize = 1;
  134. pthread_mutex_unlock (&cpg_inst->response_mutex);
  135. (void)saHandleDestroy (&cpg_handle_t_db, handle);
  136. /*
  137. * Disconnect from the server
  138. */
  139. if (cpg_inst->response_fd != -1) {
  140. shutdown(cpg_inst->response_fd, 0);
  141. close(cpg_inst->response_fd);
  142. }
  143. if (cpg_inst->dispatch_fd != -1) {
  144. shutdown(cpg_inst->dispatch_fd, 0);
  145. close(cpg_inst->dispatch_fd);
  146. }
  147. (void)saHandleInstancePut (&cpg_handle_t_db, handle);
  148. return (CS_OK);
  149. }
  150. cs_error_t cpg_fd_get (
  151. cpg_handle_t handle,
  152. int *fd)
  153. {
  154. cs_error_t error;
  155. struct cpg_inst *cpg_inst;
  156. error = saHandleInstanceGet (&cpg_handle_t_db, handle, (void *)&cpg_inst);
  157. if (error != CS_OK) {
  158. return (error);
  159. }
  160. *fd = cpg_inst->dispatch_fd;
  161. (void)saHandleInstancePut (&cpg_handle_t_db, handle);
  162. return (CS_OK);
  163. }
  164. cs_error_t cpg_context_get (
  165. cpg_handle_t handle,
  166. void **context)
  167. {
  168. cs_error_t error;
  169. struct cpg_inst *cpg_inst;
  170. error = saHandleInstanceGet (&cpg_handle_t_db, handle, (void *)&cpg_inst);
  171. if (error != CS_OK) {
  172. return (error);
  173. }
  174. *context = cpg_inst->context;
  175. (void)saHandleInstancePut (&cpg_handle_t_db, handle);
  176. return (CS_OK);
  177. }
  178. cs_error_t cpg_context_set (
  179. cpg_handle_t handle,
  180. void *context)
  181. {
  182. cs_error_t error;
  183. struct cpg_inst *cpg_inst;
  184. error = saHandleInstanceGet (&cpg_handle_t_db, handle, (void *)&cpg_inst);
  185. if (error != CS_OK) {
  186. return (error);
  187. }
  188. cpg_inst->context = context;
  189. (void)saHandleInstancePut (&cpg_handle_t_db, handle);
  190. return (CS_OK);
  191. }
  192. struct cpg_res_overlay {
  193. mar_res_header_t header __attribute__((aligned(8)));
  194. char data[512000];
  195. };
  196. cs_error_t cpg_dispatch (
  197. cpg_handle_t handle,
  198. cs_dispatch_flags_t dispatch_types)
  199. {
  200. struct pollfd ufds;
  201. int timeout = -1;
  202. cs_error_t error;
  203. int cont = 1; /* always continue do loop except when set to 0 */
  204. int dispatch_avail;
  205. struct cpg_inst *cpg_inst;
  206. struct res_lib_cpg_flowcontrol_callback *res_cpg_flowcontrol_callback;
  207. struct res_lib_cpg_confchg_callback *res_cpg_confchg_callback;
  208. struct res_lib_cpg_deliver_callback *res_cpg_deliver_callback;
  209. struct res_lib_cpg_groups_get_callback *res_lib_cpg_groups_get_callback;
  210. cpg_callbacks_t callbacks;
  211. struct cpg_res_overlay dispatch_data;
  212. struct cpg_address member_list[CPG_MEMBERS_MAX];
  213. struct cpg_address left_list[CPG_MEMBERS_MAX];
  214. struct cpg_address joined_list[CPG_MEMBERS_MAX];
  215. struct cpg_name group_name;
  216. mar_cpg_address_t *left_list_start;
  217. mar_cpg_address_t *joined_list_start;
  218. unsigned int i;
  219. error = saHandleInstanceGet (&cpg_handle_t_db, handle, (void *)&cpg_inst);
  220. if (error != CS_OK) {
  221. return (error);
  222. }
  223. /*
  224. * Timeout instantly for SA_DISPATCH_ONE or SA_DISPATCH_ALL and
  225. * wait indefinately for SA_DISPATCH_BLOCKING
  226. */
  227. if (dispatch_types == CS_DISPATCH_ALL) {
  228. timeout = 0;
  229. }
  230. do {
  231. ufds.fd = cpg_inst->dispatch_fd;
  232. ufds.events = POLLIN;
  233. ufds.revents = 0;
  234. error = saPollRetry (&ufds, 1, timeout);
  235. if (error != CS_OK) {
  236. goto error_nounlock;
  237. }
  238. pthread_mutex_lock (&cpg_inst->dispatch_mutex);
  239. /*
  240. * Regather poll data in case ufds has changed since taking lock
  241. */
  242. error = saPollRetry (&ufds, 1, timeout);
  243. if (error != CS_OK) {
  244. goto error_nounlock;
  245. }
  246. /*
  247. * Handle has been finalized in another thread
  248. */
  249. if (cpg_inst->finalize == 1) {
  250. error = CS_OK;
  251. pthread_mutex_unlock (&cpg_inst->dispatch_mutex);
  252. goto error_unlock;
  253. }
  254. dispatch_avail = ufds.revents & POLLIN;
  255. if (dispatch_avail == 0 && dispatch_types == CS_DISPATCH_ALL) {
  256. pthread_mutex_unlock (&cpg_inst->dispatch_mutex);
  257. break; /* exit do while cont is 1 loop */
  258. } else
  259. if (dispatch_avail == 0) {
  260. pthread_mutex_unlock (&cpg_inst->dispatch_mutex);
  261. continue; /* next poll */
  262. }
  263. if (ufds.revents & POLLIN) {
  264. /*
  265. * Queue empty, read response from socket
  266. */
  267. error = saRecvRetry (cpg_inst->dispatch_fd, &dispatch_data.header,
  268. sizeof (mar_res_header_t));
  269. if (error != CS_OK) {
  270. goto error_unlock;
  271. }
  272. if (dispatch_data.header.size > sizeof (mar_res_header_t)) {
  273. error = saRecvRetry (cpg_inst->dispatch_fd, &dispatch_data.data,
  274. dispatch_data.header.size - sizeof (mar_res_header_t));
  275. if (error != CS_OK) {
  276. goto error_unlock;
  277. }
  278. }
  279. } else {
  280. pthread_mutex_unlock (&cpg_inst->dispatch_mutex);
  281. continue;
  282. }
  283. /*
  284. * Make copy of callbacks, message data, unlock instance, and call callback
  285. * A risk of this dispatch method is that the callback routines may
  286. * operate at the same time that cpgFinalize has been called.
  287. */
  288. memcpy (&callbacks, &cpg_inst->callbacks, sizeof (cpg_callbacks_t));
  289. pthread_mutex_unlock (&cpg_inst->dispatch_mutex);
  290. /*
  291. * Dispatch incoming message
  292. */
  293. switch (dispatch_data.header.id) {
  294. case MESSAGE_RES_CPG_DELIVER_CALLBACK:
  295. res_cpg_deliver_callback = (struct res_lib_cpg_deliver_callback *)&dispatch_data;
  296. cpg_inst->flow_control_state = res_cpg_deliver_callback->flow_control_state;
  297. marshall_from_mar_cpg_name_t (
  298. &group_name,
  299. &res_cpg_deliver_callback->group_name);
  300. callbacks.cpg_deliver_fn (handle,
  301. &group_name,
  302. res_cpg_deliver_callback->nodeid,
  303. res_cpg_deliver_callback->pid,
  304. &res_cpg_deliver_callback->message,
  305. res_cpg_deliver_callback->msglen);
  306. break;
  307. case MESSAGE_RES_CPG_CONFCHG_CALLBACK:
  308. res_cpg_confchg_callback = (struct res_lib_cpg_confchg_callback *)&dispatch_data;
  309. for (i = 0; i < res_cpg_confchg_callback->member_list_entries; i++) {
  310. marshall_from_mar_cpg_address_t (&member_list[i],
  311. &res_cpg_confchg_callback->member_list[i]);
  312. }
  313. left_list_start = res_cpg_confchg_callback->member_list +
  314. res_cpg_confchg_callback->member_list_entries;
  315. for (i = 0; i < res_cpg_confchg_callback->left_list_entries; i++) {
  316. marshall_from_mar_cpg_address_t (&left_list[i],
  317. &left_list_start[i]);
  318. }
  319. joined_list_start = res_cpg_confchg_callback->member_list +
  320. res_cpg_confchg_callback->member_list_entries +
  321. res_cpg_confchg_callback->left_list_entries;
  322. for (i = 0; i < res_cpg_confchg_callback->joined_list_entries; i++) {
  323. marshall_from_mar_cpg_address_t (&joined_list[i],
  324. &joined_list_start[i]);
  325. }
  326. marshall_from_mar_cpg_name_t (
  327. &group_name,
  328. &res_cpg_confchg_callback->group_name);
  329. callbacks.cpg_confchg_fn (handle,
  330. &group_name,
  331. member_list,
  332. res_cpg_confchg_callback->member_list_entries,
  333. left_list,
  334. res_cpg_confchg_callback->left_list_entries,
  335. joined_list,
  336. res_cpg_confchg_callback->joined_list_entries);
  337. break;
  338. case MESSAGE_RES_CPG_GROUPS_CALLBACK:
  339. res_lib_cpg_groups_get_callback = (struct res_lib_cpg_groups_get_callback *)&dispatch_data;
  340. marshall_from_mar_cpg_name_t (
  341. &group_name,
  342. &res_lib_cpg_groups_get_callback->group_name);
  343. for (i = 0; i < res_lib_cpg_groups_get_callback->num_members; i++) {
  344. marshall_from_mar_cpg_address_t (&member_list[i],
  345. &res_lib_cpg_groups_get_callback->member_list[i]);
  346. }
  347. callbacks.cpg_groups_get_fn(handle,
  348. res_lib_cpg_groups_get_callback->group_num,
  349. res_lib_cpg_groups_get_callback->total_groups,
  350. &group_name,
  351. member_list,
  352. res_lib_cpg_groups_get_callback->num_members);
  353. break;
  354. case MESSAGE_RES_CPG_FLOWCONTROL_CALLBACK:
  355. res_cpg_flowcontrol_callback = (struct res_lib_cpg_flowcontrol_callback *)&dispatch_data;
  356. cpg_inst->flow_control_state = res_cpg_flowcontrol_callback->flow_control_state;
  357. break;
  358. default:
  359. error = CS_ERR_LIBRARY;
  360. goto error_nounlock;
  361. break;
  362. }
  363. /*
  364. * Determine if more messages should be processed
  365. * */
  366. switch (dispatch_types) {
  367. case CS_DISPATCH_ONE:
  368. cont = 0;
  369. break;
  370. case CS_DISPATCH_ALL:
  371. break;
  372. case CS_DISPATCH_BLOCKING:
  373. break;
  374. }
  375. } while (cont);
  376. error_unlock:
  377. (void)saHandleInstancePut (&cpg_handle_t_db, handle);
  378. error_nounlock:
  379. return (error);
  380. }
  381. cs_error_t cpg_join (
  382. cpg_handle_t handle,
  383. struct cpg_name *group)
  384. {
  385. cs_error_t error;
  386. struct cpg_inst *cpg_inst;
  387. struct iovec iov[2];
  388. struct req_lib_cpg_join req_lib_cpg_join;
  389. struct res_lib_cpg_join res_lib_cpg_join;
  390. struct req_lib_cpg_trackstart req_lib_cpg_trackstart;
  391. struct res_lib_cpg_trackstart res_lib_cpg_trackstart;
  392. error = saHandleInstanceGet (&cpg_handle_t_db, handle, (void *)&cpg_inst);
  393. if (error != CS_OK) {
  394. return (error);
  395. }
  396. pthread_mutex_lock (&cpg_inst->response_mutex);
  397. /* Automatically add a tracker */
  398. req_lib_cpg_trackstart.header.size = sizeof (struct req_lib_cpg_trackstart);
  399. req_lib_cpg_trackstart.header.id = MESSAGE_REQ_CPG_TRACKSTART;
  400. marshall_to_mar_cpg_name_t (&req_lib_cpg_trackstart.group_name,
  401. group);
  402. iov[0].iov_base = (char *)&req_lib_cpg_trackstart;
  403. iov[0].iov_len = sizeof (struct req_lib_cpg_trackstart);
  404. error = saSendMsgReceiveReply (cpg_inst->dispatch_fd, iov, 1,
  405. &res_lib_cpg_trackstart, sizeof (struct res_lib_cpg_trackstart));
  406. if (error != CS_OK) {
  407. pthread_mutex_unlock (&cpg_inst->response_mutex);
  408. goto error_exit;
  409. }
  410. /* Now join */
  411. req_lib_cpg_join.header.size = sizeof (struct req_lib_cpg_join);
  412. req_lib_cpg_join.header.id = MESSAGE_REQ_CPG_JOIN;
  413. req_lib_cpg_join.pid = getpid();
  414. marshall_to_mar_cpg_name_t (&req_lib_cpg_join.group_name,
  415. group);
  416. iov[0].iov_base = (char *)&req_lib_cpg_join;
  417. iov[0].iov_len = sizeof (struct req_lib_cpg_join);
  418. error = saSendMsgReceiveReply (cpg_inst->response_fd, iov, 1,
  419. &res_lib_cpg_join, sizeof (struct res_lib_cpg_join));
  420. pthread_mutex_unlock (&cpg_inst->response_mutex);
  421. if (error != CS_OK) {
  422. goto error_exit;
  423. }
  424. error = res_lib_cpg_join.header.error;
  425. error_exit:
  426. (void)saHandleInstancePut (&cpg_handle_t_db, handle);
  427. return (error);
  428. }
  429. cs_error_t cpg_leave (
  430. cpg_handle_t handle,
  431. struct cpg_name *group)
  432. {
  433. cs_error_t error;
  434. struct cpg_inst *cpg_inst;
  435. struct iovec iov[2];
  436. struct req_lib_cpg_leave req_lib_cpg_leave;
  437. struct res_lib_cpg_leave res_lib_cpg_leave;
  438. error = saHandleInstanceGet (&cpg_handle_t_db, handle, (void *)&cpg_inst);
  439. if (error != CS_OK) {
  440. return (error);
  441. }
  442. req_lib_cpg_leave.header.size = sizeof (struct req_lib_cpg_leave);
  443. req_lib_cpg_leave.header.id = MESSAGE_REQ_CPG_LEAVE;
  444. req_lib_cpg_leave.pid = getpid();
  445. marshall_to_mar_cpg_name_t (&req_lib_cpg_leave.group_name,
  446. group);
  447. iov[0].iov_base = (char *)&req_lib_cpg_leave;
  448. iov[0].iov_len = sizeof (struct req_lib_cpg_leave);
  449. pthread_mutex_lock (&cpg_inst->response_mutex);
  450. error = saSendMsgReceiveReply (cpg_inst->response_fd, iov, 1,
  451. &res_lib_cpg_leave, sizeof (struct res_lib_cpg_leave));
  452. pthread_mutex_unlock (&cpg_inst->response_mutex);
  453. if (error != CS_OK) {
  454. goto error_exit;
  455. }
  456. error = res_lib_cpg_leave.header.error;
  457. error_exit:
  458. (void)saHandleInstancePut (&cpg_handle_t_db, handle);
  459. return (error);
  460. }
  461. cs_error_t cpg_mcast_joined (
  462. cpg_handle_t handle,
  463. cpg_guarantee_t guarantee,
  464. struct iovec *iovec,
  465. int iov_len)
  466. {
  467. int i;
  468. cs_error_t error;
  469. struct cpg_inst *cpg_inst;
  470. struct iovec iov[64];
  471. struct req_lib_cpg_mcast req_lib_cpg_mcast;
  472. struct res_lib_cpg_mcast res_lib_cpg_mcast;
  473. int msg_len = 0;
  474. error = saHandleInstanceGet (&cpg_handle_t_db, handle, (void *)&cpg_inst);
  475. if (error != CS_OK) {
  476. return (error);
  477. }
  478. for (i = 0; i < iov_len; i++ ) {
  479. msg_len += iovec[i].iov_len;
  480. }
  481. req_lib_cpg_mcast.header.size = sizeof (struct req_lib_cpg_mcast) +
  482. msg_len;
  483. req_lib_cpg_mcast.header.id = MESSAGE_REQ_CPG_MCAST;
  484. req_lib_cpg_mcast.guarantee = guarantee;
  485. req_lib_cpg_mcast.msglen = msg_len;
  486. iov[0].iov_base = (char *)&req_lib_cpg_mcast;
  487. iov[0].iov_len = sizeof (struct req_lib_cpg_mcast);
  488. memcpy (&iov[1], iovec, iov_len * sizeof (struct iovec));
  489. pthread_mutex_lock (&cpg_inst->response_mutex);
  490. error = saSendMsgReceiveReply (cpg_inst->response_fd, iov, iov_len + 1,
  491. &res_lib_cpg_mcast, sizeof (res_lib_cpg_mcast));
  492. pthread_mutex_unlock (&cpg_inst->response_mutex);
  493. if (error != CS_OK) {
  494. goto error_exit;
  495. }
  496. /* Only update the flow control state when the return value is OK.
  497. * Otherwise the flow control state is not guaranteed to be valid in the
  498. * return message.
  499. * Also, don't set to ENABLED if the return value is TRY_AGAIN as this can lead
  500. * to Flow Control State sync issues between AIS LIB and EXEC.
  501. */
  502. if (res_lib_cpg_mcast.header.error == CS_OK) {
  503. cpg_inst->flow_control_state = res_lib_cpg_mcast.flow_control_state;
  504. }
  505. error = res_lib_cpg_mcast.header.error;
  506. error_exit:
  507. (void)saHandleInstancePut (&cpg_handle_t_db, handle);
  508. return (error);
  509. }
  510. cs_error_t cpg_membership_get (
  511. cpg_handle_t handle,
  512. struct cpg_name *group_name,
  513. struct cpg_address *member_list,
  514. int *member_list_entries)
  515. {
  516. cs_error_t error;
  517. struct cpg_inst *cpg_inst;
  518. struct iovec iov;
  519. struct req_lib_cpg_membership req_lib_cpg_membership_get;
  520. struct res_lib_cpg_confchg_callback *res_lib_cpg_membership_get;
  521. mar_res_header_t header;
  522. unsigned int i, bytesleft;
  523. char *buffer = NULL;
  524. error = saHandleInstanceGet (&cpg_handle_t_db, handle, (void *)&cpg_inst);
  525. if (error != CS_OK) {
  526. return (error);
  527. }
  528. req_lib_cpg_membership_get.header.size = sizeof (req_lib_cpg_membership_get);
  529. req_lib_cpg_membership_get.header.id = MESSAGE_REQ_CPG_MEMBERSHIP;
  530. iov.iov_base = (char *)&req_lib_cpg_membership_get;
  531. iov.iov_len = sizeof (req_lib_cpg_membership_get);
  532. pthread_mutex_lock (&cpg_inst->response_mutex);
  533. error = saSendMsgReceiveReply (cpg_inst->response_fd, &iov, 1,
  534. &header, sizeof (header));
  535. if (error != CS_OK) {
  536. goto error_exit;
  537. }
  538. buffer = malloc(header.size);
  539. if (buffer == NULL) {
  540. error = CS_ERR_NO_MEMORY;
  541. goto error_exit;
  542. }
  543. memcpy (buffer, &header, sizeof (header));
  544. bytesleft = header.size - sizeof (header);
  545. error = saRecvRetry (cpg_inst->response_fd,
  546. buffer + sizeof (header), bytesleft);
  547. if (error != CS_OK) {
  548. goto error_exit;
  549. }
  550. error = header.error;
  551. if (error != CS_OK) {
  552. goto error_exit;
  553. }
  554. res_lib_cpg_membership_get = (struct res_lib_cpg_confchg_callback *) buffer;
  555. /*
  556. * Copy results to caller
  557. */
  558. *member_list_entries = res_lib_cpg_membership_get->member_list_entries;
  559. if (member_list) {
  560. for (i = 0; i < res_lib_cpg_membership_get->member_list_entries; i++) {
  561. marshall_from_mar_cpg_address_t (&member_list[i],
  562. &res_lib_cpg_membership_get->member_list[i]);
  563. }
  564. }
  565. error_exit:
  566. if (buffer != NULL)
  567. free(buffer);
  568. pthread_mutex_unlock (&cpg_inst->response_mutex);
  569. (void)saHandleInstancePut (&cpg_handle_t_db, handle);
  570. return (error);
  571. }
  572. cs_error_t cpg_local_get (
  573. cpg_handle_t handle,
  574. unsigned int *local_nodeid)
  575. {
  576. cs_error_t error;
  577. struct cpg_inst *cpg_inst;
  578. struct iovec iov;
  579. struct req_lib_cpg_local_get req_lib_cpg_local_get;
  580. struct res_lib_cpg_local_get res_lib_cpg_local_get;
  581. error = saHandleInstanceGet (&cpg_handle_t_db, handle, (void *)&cpg_inst);
  582. if (error != CS_OK) {
  583. return (error);
  584. }
  585. req_lib_cpg_local_get.header.size = sizeof (mar_req_header_t);
  586. req_lib_cpg_local_get.header.id = MESSAGE_REQ_CPG_LOCAL_GET;
  587. iov.iov_base = &req_lib_cpg_local_get;
  588. iov.iov_len = sizeof (struct req_lib_cpg_local_get);
  589. pthread_mutex_lock (&cpg_inst->response_mutex);
  590. error = saSendMsgReceiveReply (cpg_inst->response_fd, &iov, 1,
  591. &res_lib_cpg_local_get, sizeof (res_lib_cpg_local_get));
  592. pthread_mutex_unlock (&cpg_inst->response_mutex);
  593. if (error != CS_OK) {
  594. goto error_exit;
  595. }
  596. error = res_lib_cpg_local_get.header.error;
  597. *local_nodeid = res_lib_cpg_local_get.local_nodeid;
  598. error_exit:
  599. (void)saHandleInstancePut (&cpg_handle_t_db, handle);
  600. return (error);
  601. }
  602. cs_error_t cpg_groups_get (
  603. cpg_handle_t handle,
  604. unsigned int *num_groups)
  605. {
  606. cs_error_t error;
  607. struct cpg_inst *cpg_inst;
  608. struct iovec iov;
  609. struct req_lib_cpg_groups_get req_lib_cpg_groups_get;
  610. struct res_lib_cpg_groups_get res_lib_cpg_groups_get;
  611. error = saHandleInstanceGet (&cpg_handle_t_db, handle, (void *)&cpg_inst);
  612. if (error != CS_OK) {
  613. return (error);
  614. }
  615. req_lib_cpg_groups_get.header.size = sizeof (mar_req_header_t);
  616. req_lib_cpg_groups_get.header.id = MESSAGE_REQ_CPG_GROUPS_GET;
  617. iov.iov_base = &req_lib_cpg_groups_get;
  618. iov.iov_len = sizeof (struct req_lib_cpg_groups_get);
  619. pthread_mutex_lock (&cpg_inst->response_mutex);
  620. error = saSendMsgReceiveReply (cpg_inst->response_fd, &iov, 1,
  621. &res_lib_cpg_groups_get, sizeof (res_lib_cpg_groups_get));
  622. pthread_mutex_unlock (&cpg_inst->response_mutex);
  623. if (error != CS_OK) {
  624. goto error_exit;
  625. }
  626. *num_groups = res_lib_cpg_groups_get.num_groups;
  627. error = res_lib_cpg_groups_get.header.error;
  628. /* Real output is delivered via a callback */
  629. error_exit:
  630. (void)saHandleInstancePut (&cpg_handle_t_db, handle);
  631. return (error);
  632. }
  633. cs_error_t cpg_flow_control_state_get (
  634. cpg_handle_t handle,
  635. cpg_flow_control_state_t *flow_control_state)
  636. {
  637. cs_error_t error;
  638. struct cpg_inst *cpg_inst;
  639. error = saHandleInstanceGet (&cpg_handle_t_db, handle, (void *)&cpg_inst);
  640. if (error != CS_OK) {
  641. return (error);
  642. }
  643. *flow_control_state = cpg_inst->flow_control_state;
  644. (void)saHandleInstancePut (&cpg_handle_t_db, handle);
  645. return (error);
  646. }
  647. /** @} */