cpg.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  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. 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 = saHandleInstanceGet (&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 == CS_DISPATCH_ALL) {
  229. timeout = 0;
  230. }
  231. do {
  232. ufds.fd = cpg_inst->dispatch_fd;
  233. ufds.events = POLLIN;
  234. ufds.revents = 0;
  235. error = saPollRetry (&ufds, 1, timeout);
  236. if (error != CS_OK) {
  237. goto error_nounlock;
  238. }
  239. pthread_mutex_lock (&cpg_inst->dispatch_mutex);
  240. /*
  241. * Regather poll data in case ufds has changed since taking lock
  242. */
  243. error = saPollRetry (&ufds, 1, timeout);
  244. if (error != CS_OK) {
  245. goto error_nounlock;
  246. }
  247. /*
  248. * Handle has been finalized in another thread
  249. */
  250. if (cpg_inst->finalize == 1) {
  251. error = CS_OK;
  252. pthread_mutex_unlock (&cpg_inst->dispatch_mutex);
  253. goto error_unlock;
  254. }
  255. dispatch_avail = ufds.revents & POLLIN;
  256. if (dispatch_avail == 0 && dispatch_types == CS_DISPATCH_ALL) {
  257. pthread_mutex_unlock (&cpg_inst->dispatch_mutex);
  258. break; /* exit do while cont is 1 loop */
  259. } else
  260. if (dispatch_avail == 0) {
  261. pthread_mutex_unlock (&cpg_inst->dispatch_mutex);
  262. continue; /* next poll */
  263. }
  264. if (ufds.revents & POLLIN) {
  265. /*
  266. * Queue empty, read response from socket
  267. */
  268. error = saRecvRetry (cpg_inst->dispatch_fd, &dispatch_data.header,
  269. sizeof (mar_res_header_t));
  270. if (error != CS_OK) {
  271. goto error_unlock;
  272. }
  273. if (dispatch_data.header.size > sizeof (mar_res_header_t)) {
  274. error = saRecvRetry (cpg_inst->dispatch_fd, &dispatch_data.data,
  275. dispatch_data.header.size - sizeof (mar_res_header_t));
  276. if (error != CS_OK) {
  277. goto error_unlock;
  278. }
  279. }
  280. } else {
  281. pthread_mutex_unlock (&cpg_inst->dispatch_mutex);
  282. continue;
  283. }
  284. /*
  285. * Make copy of callbacks, message data, unlock instance, and call callback
  286. * A risk of this dispatch method is that the callback routines may
  287. * operate at the same time that cpgFinalize has been called.
  288. */
  289. memcpy (&callbacks, &cpg_inst->callbacks, sizeof (cpg_callbacks_t));
  290. pthread_mutex_unlock (&cpg_inst->dispatch_mutex);
  291. /*
  292. * Dispatch incoming message
  293. */
  294. switch (dispatch_data.header.id) {
  295. case MESSAGE_RES_CPG_DELIVER_CALLBACK:
  296. res_cpg_deliver_callback = (struct res_lib_cpg_deliver_callback *)&dispatch_data;
  297. cpg_inst->flow_control_state = res_cpg_deliver_callback->flow_control_state;
  298. marshall_from_mar_cpg_name_t (
  299. &group_name,
  300. &res_cpg_deliver_callback->group_name);
  301. callbacks.cpg_deliver_fn (handle,
  302. &group_name,
  303. res_cpg_deliver_callback->nodeid,
  304. res_cpg_deliver_callback->pid,
  305. &res_cpg_deliver_callback->message,
  306. res_cpg_deliver_callback->msglen);
  307. break;
  308. case MESSAGE_RES_CPG_CONFCHG_CALLBACK:
  309. res_cpg_confchg_callback = (struct res_lib_cpg_confchg_callback *)&dispatch_data;
  310. for (i = 0; i < res_cpg_confchg_callback->member_list_entries; i++) {
  311. marshall_from_mar_cpg_address_t (&member_list[i],
  312. &res_cpg_confchg_callback->member_list[i]);
  313. }
  314. left_list_start = res_cpg_confchg_callback->member_list +
  315. res_cpg_confchg_callback->member_list_entries;
  316. for (i = 0; i < res_cpg_confchg_callback->left_list_entries; i++) {
  317. marshall_from_mar_cpg_address_t (&left_list[i],
  318. &left_list_start[i]);
  319. }
  320. joined_list_start = res_cpg_confchg_callback->member_list +
  321. res_cpg_confchg_callback->member_list_entries +
  322. res_cpg_confchg_callback->left_list_entries;
  323. for (i = 0; i < res_cpg_confchg_callback->joined_list_entries; i++) {
  324. marshall_from_mar_cpg_address_t (&joined_list[i],
  325. &joined_list_start[i]);
  326. }
  327. marshall_from_mar_cpg_name_t (
  328. &group_name,
  329. &res_cpg_confchg_callback->group_name);
  330. callbacks.cpg_confchg_fn (handle,
  331. &group_name,
  332. member_list,
  333. res_cpg_confchg_callback->member_list_entries,
  334. left_list,
  335. res_cpg_confchg_callback->left_list_entries,
  336. joined_list,
  337. res_cpg_confchg_callback->joined_list_entries);
  338. break;
  339. case MESSAGE_RES_CPG_GROUPS_CALLBACK:
  340. res_lib_cpg_groups_get_callback = (struct res_lib_cpg_groups_get_callback *)&dispatch_data;
  341. marshall_from_mar_cpg_name_t (
  342. &group_name,
  343. &res_lib_cpg_groups_get_callback->group_name);
  344. for (i = 0; i < res_lib_cpg_groups_get_callback->num_members; i++) {
  345. marshall_from_mar_cpg_address_t (&member_list[i],
  346. &res_lib_cpg_groups_get_callback->member_list[i]);
  347. }
  348. callbacks.cpg_groups_get_fn(handle,
  349. res_lib_cpg_groups_get_callback->group_num,
  350. res_lib_cpg_groups_get_callback->total_groups,
  351. &group_name,
  352. member_list,
  353. res_lib_cpg_groups_get_callback->num_members);
  354. break;
  355. case MESSAGE_RES_CPG_FLOWCONTROL_CALLBACK:
  356. res_cpg_flowcontrol_callback = (struct res_lib_cpg_flowcontrol_callback *)&dispatch_data;
  357. cpg_inst->flow_control_state = res_cpg_flowcontrol_callback->flow_control_state;
  358. break;
  359. default:
  360. error = CS_ERR_LIBRARY;
  361. goto error_nounlock;
  362. break;
  363. }
  364. /*
  365. * Determine if more messages should be processed
  366. * */
  367. switch (dispatch_types) {
  368. case CS_DISPATCH_ONE:
  369. if (ignore_dispatch) {
  370. ignore_dispatch = 0;
  371. } else {
  372. cont = 0;
  373. }
  374. break;
  375. case CS_DISPATCH_ALL:
  376. if (ignore_dispatch) {
  377. ignore_dispatch = 0;
  378. }
  379. break;
  380. case CS_DISPATCH_BLOCKING:
  381. break;
  382. }
  383. } while (cont);
  384. error_unlock:
  385. (void)saHandleInstancePut (&cpg_handle_t_db, handle);
  386. error_nounlock:
  387. return (error);
  388. }
  389. cs_error_t cpg_join (
  390. cpg_handle_t handle,
  391. struct cpg_name *group)
  392. {
  393. cs_error_t error;
  394. struct cpg_inst *cpg_inst;
  395. struct iovec iov[2];
  396. struct req_lib_cpg_join req_lib_cpg_join;
  397. struct res_lib_cpg_join res_lib_cpg_join;
  398. struct req_lib_cpg_trackstart req_lib_cpg_trackstart;
  399. struct res_lib_cpg_trackstart res_lib_cpg_trackstart;
  400. error = saHandleInstanceGet (&cpg_handle_t_db, handle, (void *)&cpg_inst);
  401. if (error != CS_OK) {
  402. return (error);
  403. }
  404. pthread_mutex_lock (&cpg_inst->response_mutex);
  405. /* Automatically add a tracker */
  406. req_lib_cpg_trackstart.header.size = sizeof (struct req_lib_cpg_trackstart);
  407. req_lib_cpg_trackstart.header.id = MESSAGE_REQ_CPG_TRACKSTART;
  408. marshall_to_mar_cpg_name_t (&req_lib_cpg_trackstart.group_name,
  409. group);
  410. iov[0].iov_base = (char *)&req_lib_cpg_trackstart;
  411. iov[0].iov_len = sizeof (struct req_lib_cpg_trackstart);
  412. error = saSendMsgReceiveReply (cpg_inst->dispatch_fd, iov, 1,
  413. &res_lib_cpg_trackstart, sizeof (struct res_lib_cpg_trackstart));
  414. if (error != CS_OK) {
  415. pthread_mutex_unlock (&cpg_inst->response_mutex);
  416. goto error_exit;
  417. }
  418. /* Now join */
  419. req_lib_cpg_join.header.size = sizeof (struct req_lib_cpg_join);
  420. req_lib_cpg_join.header.id = MESSAGE_REQ_CPG_JOIN;
  421. req_lib_cpg_join.pid = getpid();
  422. marshall_to_mar_cpg_name_t (&req_lib_cpg_join.group_name,
  423. group);
  424. iov[0].iov_base = (char *)&req_lib_cpg_join;
  425. iov[0].iov_len = sizeof (struct req_lib_cpg_join);
  426. error = saSendMsgReceiveReply (cpg_inst->response_fd, iov, 1,
  427. &res_lib_cpg_join, sizeof (struct res_lib_cpg_join));
  428. pthread_mutex_unlock (&cpg_inst->response_mutex);
  429. if (error != CS_OK) {
  430. goto error_exit;
  431. }
  432. error = res_lib_cpg_join.header.error;
  433. error_exit:
  434. (void)saHandleInstancePut (&cpg_handle_t_db, handle);
  435. return (error);
  436. }
  437. cs_error_t cpg_leave (
  438. cpg_handle_t handle,
  439. struct cpg_name *group)
  440. {
  441. cs_error_t error;
  442. struct cpg_inst *cpg_inst;
  443. struct iovec iov[2];
  444. struct req_lib_cpg_leave req_lib_cpg_leave;
  445. struct res_lib_cpg_leave res_lib_cpg_leave;
  446. error = saHandleInstanceGet (&cpg_handle_t_db, handle, (void *)&cpg_inst);
  447. if (error != CS_OK) {
  448. return (error);
  449. }
  450. req_lib_cpg_leave.header.size = sizeof (struct req_lib_cpg_leave);
  451. req_lib_cpg_leave.header.id = MESSAGE_REQ_CPG_LEAVE;
  452. req_lib_cpg_leave.pid = getpid();
  453. marshall_to_mar_cpg_name_t (&req_lib_cpg_leave.group_name,
  454. group);
  455. iov[0].iov_base = (char *)&req_lib_cpg_leave;
  456. iov[0].iov_len = sizeof (struct req_lib_cpg_leave);
  457. pthread_mutex_lock (&cpg_inst->response_mutex);
  458. error = saSendMsgReceiveReply (cpg_inst->response_fd, iov, 1,
  459. &res_lib_cpg_leave, sizeof (struct res_lib_cpg_leave));
  460. pthread_mutex_unlock (&cpg_inst->response_mutex);
  461. if (error != CS_OK) {
  462. goto error_exit;
  463. }
  464. error = res_lib_cpg_leave.header.error;
  465. error_exit:
  466. (void)saHandleInstancePut (&cpg_handle_t_db, handle);
  467. return (error);
  468. }
  469. cs_error_t cpg_mcast_joined (
  470. cpg_handle_t handle,
  471. cpg_guarantee_t guarantee,
  472. struct iovec *iovec,
  473. int iov_len)
  474. {
  475. int i;
  476. cs_error_t error;
  477. struct cpg_inst *cpg_inst;
  478. struct iovec iov[64];
  479. struct req_lib_cpg_mcast req_lib_cpg_mcast;
  480. struct res_lib_cpg_mcast res_lib_cpg_mcast;
  481. int msg_len = 0;
  482. error = saHandleInstanceGet (&cpg_handle_t_db, handle, (void *)&cpg_inst);
  483. if (error != CS_OK) {
  484. return (error);
  485. }
  486. for (i = 0; i < iov_len; i++ ) {
  487. msg_len += iovec[i].iov_len;
  488. }
  489. req_lib_cpg_mcast.header.size = sizeof (struct req_lib_cpg_mcast) +
  490. msg_len;
  491. req_lib_cpg_mcast.header.id = MESSAGE_REQ_CPG_MCAST;
  492. req_lib_cpg_mcast.guarantee = guarantee;
  493. req_lib_cpg_mcast.msglen = msg_len;
  494. iov[0].iov_base = (char *)&req_lib_cpg_mcast;
  495. iov[0].iov_len = sizeof (struct req_lib_cpg_mcast);
  496. memcpy (&iov[1], iovec, iov_len * sizeof (struct iovec));
  497. pthread_mutex_lock (&cpg_inst->response_mutex);
  498. error = saSendMsgReceiveReply (cpg_inst->response_fd, iov, iov_len + 1,
  499. &res_lib_cpg_mcast, sizeof (res_lib_cpg_mcast));
  500. pthread_mutex_unlock (&cpg_inst->response_mutex);
  501. if (error != CS_OK) {
  502. goto error_exit;
  503. }
  504. /* Only update the flow control state when the return value is OK.
  505. * Otherwise the flow control state is not guaranteed to be valid in the
  506. * return message.
  507. * Also, don't set to ENABLED if the return value is TRY_AGAIN as this can lead
  508. * to Flow Control State sync issues between AIS LIB and EXEC.
  509. */
  510. if (res_lib_cpg_mcast.header.error == CS_OK) {
  511. cpg_inst->flow_control_state = res_lib_cpg_mcast.flow_control_state;
  512. }
  513. error = res_lib_cpg_mcast.header.error;
  514. error_exit:
  515. (void)saHandleInstancePut (&cpg_handle_t_db, handle);
  516. return (error);
  517. }
  518. cs_error_t cpg_membership_get (
  519. cpg_handle_t handle,
  520. struct cpg_name *group_name,
  521. struct cpg_address *member_list,
  522. int *member_list_entries)
  523. {
  524. cs_error_t error;
  525. struct cpg_inst *cpg_inst;
  526. struct iovec iov;
  527. struct req_lib_cpg_membership req_lib_cpg_membership_get;
  528. struct res_lib_cpg_confchg_callback res_lib_cpg_membership_get;
  529. unsigned int i;
  530. error = saHandleInstanceGet (&cpg_handle_t_db, handle, (void *)&cpg_inst);
  531. if (error != CS_OK) {
  532. return (error);
  533. }
  534. req_lib_cpg_membership_get.header.size = sizeof (mar_req_header_t);
  535. req_lib_cpg_membership_get.header.id = MESSAGE_REQ_CPG_MEMBERSHIP;
  536. marshall_to_mar_cpg_name_t (&req_lib_cpg_membership_get.group_name,
  537. group_name);
  538. iov.iov_base = (char *)&req_lib_cpg_membership_get;
  539. iov.iov_len = sizeof (mar_req_header_t);
  540. pthread_mutex_lock (&cpg_inst->response_mutex);
  541. error = saSendMsgReceiveReply (cpg_inst->response_fd, &iov, 1,
  542. &res_lib_cpg_membership_get, sizeof (mar_res_header_t));
  543. pthread_mutex_unlock (&cpg_inst->response_mutex);
  544. if (error != CS_OK) {
  545. goto error_exit;
  546. }
  547. error = res_lib_cpg_membership_get.header.error;
  548. /*
  549. * Copy results to caller
  550. */
  551. *member_list_entries = res_lib_cpg_membership_get.member_list_entries;
  552. if (member_list) {
  553. for (i = 0; i < res_lib_cpg_membership_get.member_list_entries; i++) {
  554. marshall_from_mar_cpg_address_t (&member_list[i],
  555. &res_lib_cpg_membership_get.member_list[i]);
  556. }
  557. }
  558. error_exit:
  559. (void)saHandleInstancePut (&cpg_handle_t_db, handle);
  560. return (error);
  561. }
  562. cs_error_t cpg_local_get (
  563. cpg_handle_t handle,
  564. unsigned int *local_nodeid)
  565. {
  566. cs_error_t error;
  567. struct cpg_inst *cpg_inst;
  568. struct iovec iov;
  569. struct req_lib_cpg_local_get req_lib_cpg_local_get;
  570. struct res_lib_cpg_local_get res_lib_cpg_local_get;
  571. error = saHandleInstanceGet (&cpg_handle_t_db, handle, (void *)&cpg_inst);
  572. if (error != CS_OK) {
  573. return (error);
  574. }
  575. req_lib_cpg_local_get.header.size = sizeof (mar_req_header_t);
  576. req_lib_cpg_local_get.header.id = MESSAGE_REQ_CPG_LOCAL_GET;
  577. iov.iov_base = &req_lib_cpg_local_get;
  578. iov.iov_len = sizeof (struct req_lib_cpg_local_get);
  579. pthread_mutex_lock (&cpg_inst->response_mutex);
  580. error = saSendMsgReceiveReply (cpg_inst->response_fd, &iov, 1,
  581. &res_lib_cpg_local_get, sizeof (res_lib_cpg_local_get));
  582. pthread_mutex_unlock (&cpg_inst->response_mutex);
  583. if (error != CS_OK) {
  584. goto error_exit;
  585. }
  586. error = res_lib_cpg_local_get.header.error;
  587. *local_nodeid = res_lib_cpg_local_get.local_nodeid;
  588. error_exit:
  589. (void)saHandleInstancePut (&cpg_handle_t_db, handle);
  590. return (error);
  591. }
  592. cs_error_t cpg_groups_get (
  593. cpg_handle_t handle,
  594. unsigned int *num_groups)
  595. {
  596. cs_error_t error;
  597. struct cpg_inst *cpg_inst;
  598. struct iovec iov;
  599. struct req_lib_cpg_groups_get req_lib_cpg_groups_get;
  600. struct res_lib_cpg_groups_get res_lib_cpg_groups_get;
  601. error = saHandleInstanceGet (&cpg_handle_t_db, handle, (void *)&cpg_inst);
  602. if (error != CS_OK) {
  603. return (error);
  604. }
  605. req_lib_cpg_groups_get.header.size = sizeof (mar_req_header_t);
  606. req_lib_cpg_groups_get.header.id = MESSAGE_REQ_CPG_GROUPS_GET;
  607. iov.iov_base = &req_lib_cpg_groups_get;
  608. iov.iov_len = sizeof (struct req_lib_cpg_groups_get);
  609. pthread_mutex_lock (&cpg_inst->response_mutex);
  610. error = saSendMsgReceiveReply (cpg_inst->response_fd, &iov, 1,
  611. &res_lib_cpg_groups_get, sizeof (res_lib_cpg_groups_get));
  612. pthread_mutex_unlock (&cpg_inst->response_mutex);
  613. if (error != CS_OK) {
  614. goto error_exit;
  615. }
  616. *num_groups = res_lib_cpg_groups_get.num_groups;
  617. error = res_lib_cpg_groups_get.header.error;
  618. /* Real output is delivered via a callback */
  619. error_exit:
  620. (void)saHandleInstancePut (&cpg_handle_t_db, handle);
  621. return (error);
  622. }
  623. cs_error_t cpg_flow_control_state_get (
  624. cpg_handle_t handle,
  625. cpg_flow_control_state_t *flow_control_state)
  626. {
  627. cs_error_t error;
  628. struct cpg_inst *cpg_inst;
  629. error = saHandleInstanceGet (&cpg_handle_t_db, handle, (void *)&cpg_inst);
  630. if (error != CS_OK) {
  631. return (error);
  632. }
  633. *flow_control_state = cpg_inst->flow_control_state;
  634. (void)saHandleInstancePut (&cpg_handle_t_db, handle);
  635. return (error);
  636. }
  637. /** @} */