4
0

votequorum.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. /*
  2. * Copyright (c) 2009 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Christine Caulfield (ccaulfie@redhat.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. /*
  35. * Provides a quorum API using the corosync executive
  36. */
  37. #include <config.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <unistd.h>
  41. #include <sys/types.h>
  42. #include <sys/socket.h>
  43. #include <errno.h>
  44. #include <qb/qbdefs.h>
  45. #include <qb/qbipcc.h>
  46. #include <corosync/corotypes.h>
  47. #include <corosync/corodefs.h>
  48. #include <corosync/hdb.h>
  49. #include <corosync/votequorum.h>
  50. #include <corosync/ipc_votequorum.h>
  51. #include "util.h"
  52. struct votequorum_inst {
  53. qb_ipcc_connection_t *c;
  54. int finalize;
  55. void *context;
  56. votequorum_callbacks_t callbacks;
  57. };
  58. DECLARE_HDB_DATABASE(votequorum_handle_t_db,NULL);
  59. cs_error_t votequorum_initialize (
  60. votequorum_handle_t *handle,
  61. votequorum_callbacks_t *callbacks)
  62. {
  63. cs_error_t error;
  64. struct votequorum_inst *votequorum_inst;
  65. error = hdb_error_to_cs(hdb_handle_create (&votequorum_handle_t_db, sizeof (struct votequorum_inst), handle));
  66. if (error != CS_OK) {
  67. goto error_no_destroy;
  68. }
  69. error = hdb_error_to_cs(hdb_handle_get (&votequorum_handle_t_db, *handle, (void *)&votequorum_inst));
  70. if (error != CS_OK) {
  71. goto error_destroy;
  72. }
  73. votequorum_inst->c = qb_ipcc_connect ("votequorum", IPC_REQUEST_SIZE);
  74. if (votequorum_inst->c == NULL) {
  75. error = qb_to_cs_error(-errno);
  76. goto error_put_destroy;
  77. }
  78. if (callbacks)
  79. memcpy(&votequorum_inst->callbacks, callbacks, sizeof (*callbacks));
  80. else
  81. memset(&votequorum_inst->callbacks, 0, sizeof (*callbacks));
  82. hdb_handle_put (&votequorum_handle_t_db, *handle);
  83. return (CS_OK);
  84. error_put_destroy:
  85. hdb_handle_put (&votequorum_handle_t_db, *handle);
  86. error_destroy:
  87. hdb_handle_destroy (&votequorum_handle_t_db, *handle);
  88. error_no_destroy:
  89. return (error);
  90. }
  91. cs_error_t votequorum_finalize (
  92. votequorum_handle_t handle)
  93. {
  94. struct votequorum_inst *votequorum_inst;
  95. cs_error_t error;
  96. error = hdb_error_to_cs(hdb_handle_get (&votequorum_handle_t_db, handle, (void *)&votequorum_inst));
  97. if (error != CS_OK) {
  98. return (error);
  99. }
  100. /*
  101. * Another thread has already started finalizing
  102. */
  103. if (votequorum_inst->finalize) {
  104. hdb_handle_put (&votequorum_handle_t_db, handle);
  105. return (CS_ERR_BAD_HANDLE);
  106. }
  107. votequorum_inst->finalize = 1;
  108. qb_ipcc_disconnect (votequorum_inst->c);
  109. hdb_handle_destroy (&votequorum_handle_t_db, handle);
  110. hdb_handle_put (&votequorum_handle_t_db, handle);
  111. return (CS_OK);
  112. }
  113. cs_error_t votequorum_getinfo (
  114. votequorum_handle_t handle,
  115. unsigned int nodeid,
  116. struct votequorum_info *info)
  117. {
  118. cs_error_t error;
  119. struct votequorum_inst *votequorum_inst;
  120. struct iovec iov;
  121. struct req_lib_votequorum_getinfo req_lib_votequorum_getinfo;
  122. struct res_lib_votequorum_getinfo res_lib_votequorum_getinfo;
  123. error = hdb_error_to_cs(hdb_handle_get (&votequorum_handle_t_db, handle, (void *)&votequorum_inst));
  124. if (error != CS_OK) {
  125. return (error);
  126. }
  127. req_lib_votequorum_getinfo.header.size = sizeof (struct req_lib_votequorum_getinfo);
  128. req_lib_votequorum_getinfo.header.id = MESSAGE_REQ_VOTEQUORUM_GETINFO;
  129. req_lib_votequorum_getinfo.nodeid = nodeid;
  130. iov.iov_base = (char *)&req_lib_votequorum_getinfo;
  131. iov.iov_len = sizeof (struct req_lib_votequorum_getinfo);
  132. error = qb_to_cs_error(qb_ipcc_sendv_recv (
  133. votequorum_inst->c,
  134. &iov,
  135. 1,
  136. &res_lib_votequorum_getinfo,
  137. sizeof (struct res_lib_votequorum_getinfo), CS_IPC_TIMEOUT_MS));
  138. if (error != CS_OK) {
  139. goto error_exit;
  140. }
  141. error = res_lib_votequorum_getinfo.header.error;
  142. info->node_id = res_lib_votequorum_getinfo.nodeid;
  143. info->node_votes = res_lib_votequorum_getinfo.votes;
  144. info->node_expected_votes = res_lib_votequorum_getinfo.expected_votes;
  145. info->highest_expected = res_lib_votequorum_getinfo.highest_expected;
  146. info->total_votes = res_lib_votequorum_getinfo.total_votes;
  147. info->quorum = res_lib_votequorum_getinfo.quorum;
  148. info->flags = res_lib_votequorum_getinfo.flags;
  149. error_exit:
  150. hdb_handle_put (&votequorum_handle_t_db, handle);
  151. return (error);
  152. }
  153. cs_error_t votequorum_setexpected (
  154. votequorum_handle_t handle,
  155. unsigned int expected_votes)
  156. {
  157. cs_error_t error;
  158. struct votequorum_inst *votequorum_inst;
  159. struct iovec iov;
  160. struct req_lib_votequorum_setexpected req_lib_votequorum_setexpected;
  161. struct res_lib_votequorum_status res_lib_votequorum_status;
  162. error = hdb_error_to_cs(hdb_handle_get (&votequorum_handle_t_db, handle, (void *)&votequorum_inst));
  163. if (error != CS_OK) {
  164. return (error);
  165. }
  166. req_lib_votequorum_setexpected.header.size = sizeof (struct req_lib_votequorum_setexpected);
  167. req_lib_votequorum_setexpected.header.id = MESSAGE_REQ_VOTEQUORUM_SETEXPECTED;
  168. req_lib_votequorum_setexpected.expected_votes = expected_votes;
  169. iov.iov_base = (char *)&req_lib_votequorum_setexpected;
  170. iov.iov_len = sizeof (struct req_lib_votequorum_setexpected);
  171. error = qb_to_cs_error(qb_ipcc_sendv_recv (
  172. votequorum_inst->c,
  173. &iov,
  174. 1,
  175. &res_lib_votequorum_status,
  176. sizeof (struct res_lib_votequorum_status), CS_IPC_TIMEOUT_MS));
  177. if (error != CS_OK) {
  178. goto error_exit;
  179. }
  180. error = res_lib_votequorum_status.header.error;
  181. error_exit:
  182. hdb_handle_put (&votequorum_handle_t_db, handle);
  183. return (error);
  184. }
  185. cs_error_t votequorum_setvotes (
  186. votequorum_handle_t handle,
  187. unsigned int nodeid,
  188. unsigned int votes)
  189. {
  190. cs_error_t error;
  191. struct votequorum_inst *votequorum_inst;
  192. struct iovec iov;
  193. struct req_lib_votequorum_setvotes req_lib_votequorum_setvotes;
  194. struct res_lib_votequorum_status res_lib_votequorum_status;
  195. error = hdb_error_to_cs(hdb_handle_get (&votequorum_handle_t_db, handle, (void *)&votequorum_inst));
  196. if (error != CS_OK) {
  197. return (error);
  198. }
  199. req_lib_votequorum_setvotes.header.size = sizeof (struct req_lib_votequorum_setvotes);
  200. req_lib_votequorum_setvotes.header.id = MESSAGE_REQ_VOTEQUORUM_SETVOTES;
  201. req_lib_votequorum_setvotes.nodeid = nodeid;
  202. req_lib_votequorum_setvotes.votes = votes;
  203. iov.iov_base = (char *)&req_lib_votequorum_setvotes;
  204. iov.iov_len = sizeof (struct req_lib_votequorum_setvotes);
  205. error = qb_to_cs_error(qb_ipcc_sendv_recv (
  206. votequorum_inst->c,
  207. &iov,
  208. 1,
  209. &res_lib_votequorum_status,
  210. sizeof (struct res_lib_votequorum_status), CS_IPC_TIMEOUT_MS));
  211. if (error != CS_OK) {
  212. goto error_exit;
  213. }
  214. error = res_lib_votequorum_status.header.error;
  215. error_exit:
  216. hdb_handle_put (&votequorum_handle_t_db, handle);
  217. return (error);
  218. }
  219. cs_error_t votequorum_qdisk_register (
  220. votequorum_handle_t handle,
  221. const char *name,
  222. unsigned int votes)
  223. {
  224. cs_error_t error;
  225. struct votequorum_inst *votequorum_inst;
  226. struct iovec iov;
  227. struct req_lib_votequorum_qdisk_register req_lib_votequorum_qdisk_register;
  228. struct res_lib_votequorum_status res_lib_votequorum_status;
  229. if (strlen(name) > VOTEQUORUM_MAX_QDISK_NAME_LEN)
  230. return CS_ERR_INVALID_PARAM;
  231. error = hdb_error_to_cs(hdb_handle_get (&votequorum_handle_t_db, handle, (void *)&votequorum_inst));
  232. if (error != CS_OK) {
  233. return (error);
  234. }
  235. req_lib_votequorum_qdisk_register.header.size = sizeof (struct req_lib_votequorum_qdisk_register);
  236. req_lib_votequorum_qdisk_register.header.id = MESSAGE_REQ_VOTEQUORUM_QDISK_REGISTER;
  237. strcpy(req_lib_votequorum_qdisk_register.name, name);
  238. req_lib_votequorum_qdisk_register.votes = votes;
  239. iov.iov_base = (char *)&req_lib_votequorum_qdisk_register;
  240. iov.iov_len = sizeof (struct req_lib_votequorum_qdisk_register);
  241. error = qb_to_cs_error(qb_ipcc_sendv_recv (
  242. votequorum_inst->c,
  243. &iov,
  244. 1,
  245. &res_lib_votequorum_status,
  246. sizeof (struct res_lib_votequorum_status), CS_IPC_TIMEOUT_MS));
  247. if (error != CS_OK) {
  248. goto error_exit;
  249. }
  250. error = res_lib_votequorum_status.header.error;
  251. error_exit:
  252. hdb_handle_put (&votequorum_handle_t_db, handle);
  253. return (error);
  254. }
  255. cs_error_t votequorum_qdisk_poll (
  256. votequorum_handle_t handle,
  257. unsigned int state)
  258. {
  259. cs_error_t error;
  260. struct votequorum_inst *votequorum_inst;
  261. struct iovec iov;
  262. struct req_lib_votequorum_qdisk_poll req_lib_votequorum_qdisk_poll;
  263. struct res_lib_votequorum_status res_lib_votequorum_status;
  264. error = hdb_error_to_cs(hdb_handle_get (&votequorum_handle_t_db, handle, (void *)&votequorum_inst));
  265. if (error != CS_OK) {
  266. return (error);
  267. }
  268. req_lib_votequorum_qdisk_poll.header.size = sizeof (struct req_lib_votequorum_qdisk_poll);
  269. req_lib_votequorum_qdisk_poll.header.id = MESSAGE_REQ_VOTEQUORUM_QDISK_POLL;
  270. req_lib_votequorum_qdisk_poll.state = state;
  271. iov.iov_base = (char *)&req_lib_votequorum_qdisk_poll;
  272. iov.iov_len = sizeof (struct req_lib_votequorum_qdisk_poll);
  273. error = qb_to_cs_error(qb_ipcc_sendv_recv (
  274. votequorum_inst->c,
  275. &iov,
  276. 1,
  277. &res_lib_votequorum_status,
  278. sizeof (struct res_lib_votequorum_status), CS_IPC_TIMEOUT_MS));
  279. if (error != CS_OK) {
  280. goto error_exit;
  281. }
  282. error = res_lib_votequorum_status.header.error;
  283. error_exit:
  284. hdb_handle_put (&votequorum_handle_t_db, handle);
  285. return (error);
  286. }
  287. cs_error_t votequorum_qdisk_unregister (
  288. votequorum_handle_t handle)
  289. {
  290. cs_error_t error;
  291. struct votequorum_inst *votequorum_inst;
  292. struct iovec iov;
  293. struct req_lib_votequorum_general req_lib_votequorum_general;
  294. struct res_lib_votequorum_status res_lib_votequorum_status;
  295. error = hdb_error_to_cs(hdb_handle_get (&votequorum_handle_t_db, handle, (void *)&votequorum_inst));
  296. if (error != CS_OK) {
  297. return (error);
  298. }
  299. req_lib_votequorum_general.header.size = sizeof (struct req_lib_votequorum_general);
  300. req_lib_votequorum_general.header.id = MESSAGE_REQ_VOTEQUORUM_QDISK_UNREGISTER;
  301. iov.iov_base = (char *)&req_lib_votequorum_general;
  302. iov.iov_len = sizeof (struct req_lib_votequorum_general);
  303. error = qb_to_cs_error(qb_ipcc_sendv_recv (
  304. votequorum_inst->c,
  305. &iov,
  306. 1,
  307. &res_lib_votequorum_status,
  308. sizeof (struct res_lib_votequorum_status), CS_IPC_TIMEOUT_MS));
  309. if (error != CS_OK) {
  310. goto error_exit;
  311. }
  312. error = res_lib_votequorum_status.header.error;
  313. error_exit:
  314. hdb_handle_put (&votequorum_handle_t_db, handle);
  315. return (error);
  316. }
  317. cs_error_t votequorum_qdisk_getinfo (
  318. votequorum_handle_t handle,
  319. struct votequorum_qdisk_info *qinfo)
  320. {
  321. cs_error_t error;
  322. struct votequorum_inst *votequorum_inst;
  323. struct iovec iov;
  324. struct req_lib_votequorum_general req_lib_votequorum_general;
  325. struct res_lib_votequorum_qdisk_getinfo res_lib_votequorum_qdisk_getinfo;
  326. error = hdb_error_to_cs(hdb_handle_get (&votequorum_handle_t_db, handle, (void *)&votequorum_inst));
  327. if (error != CS_OK) {
  328. return (error);
  329. }
  330. req_lib_votequorum_general.header.size = sizeof (struct req_lib_votequorum_general);
  331. req_lib_votequorum_general.header.id = MESSAGE_REQ_VOTEQUORUM_QDISK_GETINFO;
  332. iov.iov_base = (char *)&req_lib_votequorum_general;
  333. iov.iov_len = sizeof (struct req_lib_votequorum_general);
  334. error = qb_to_cs_error(qb_ipcc_sendv_recv (
  335. votequorum_inst->c,
  336. &iov,
  337. 1,
  338. &res_lib_votequorum_qdisk_getinfo,
  339. sizeof (struct res_lib_votequorum_qdisk_getinfo), CS_IPC_TIMEOUT_MS));
  340. if (error != CS_OK) {
  341. goto error_exit;
  342. }
  343. error = res_lib_votequorum_qdisk_getinfo.header.error;
  344. qinfo->votes = res_lib_votequorum_qdisk_getinfo.votes;
  345. qinfo->state = res_lib_votequorum_qdisk_getinfo.state;
  346. strcpy(qinfo->name, res_lib_votequorum_qdisk_getinfo.name);
  347. error_exit:
  348. hdb_handle_put (&votequorum_handle_t_db, handle);
  349. return (error);
  350. }
  351. cs_error_t votequorum_trackstart (
  352. votequorum_handle_t handle,
  353. uint64_t context,
  354. unsigned int flags)
  355. {
  356. cs_error_t error;
  357. struct votequorum_inst *votequorum_inst;
  358. struct iovec iov;
  359. struct req_lib_votequorum_trackstart req_lib_votequorum_trackstart;
  360. struct res_lib_votequorum_status res_lib_votequorum_status;
  361. error = hdb_error_to_cs(hdb_handle_get (&votequorum_handle_t_db, handle, (void *)&votequorum_inst));
  362. if (error != CS_OK) {
  363. return (error);
  364. }
  365. req_lib_votequorum_trackstart.header.size = sizeof (struct req_lib_votequorum_trackstart);
  366. req_lib_votequorum_trackstart.header.id = MESSAGE_REQ_VOTEQUORUM_TRACKSTART;
  367. req_lib_votequorum_trackstart.track_flags = flags;
  368. req_lib_votequorum_trackstart.context = context;
  369. iov.iov_base = (char *)&req_lib_votequorum_trackstart;
  370. iov.iov_len = sizeof (struct req_lib_votequorum_trackstart);
  371. error = qb_to_cs_error(qb_ipcc_sendv_recv (
  372. votequorum_inst->c,
  373. &iov,
  374. 1,
  375. &res_lib_votequorum_status,
  376. sizeof (struct res_lib_votequorum_status), CS_IPC_TIMEOUT_MS));
  377. if (error != CS_OK) {
  378. goto error_exit;
  379. }
  380. error = res_lib_votequorum_status.header.error;
  381. error_exit:
  382. hdb_handle_put (&votequorum_handle_t_db, handle);
  383. return (error);
  384. }
  385. cs_error_t votequorum_trackstop (
  386. votequorum_handle_t handle)
  387. {
  388. cs_error_t error;
  389. struct votequorum_inst *votequorum_inst;
  390. struct iovec iov;
  391. struct req_lib_votequorum_general req_lib_votequorum_general;
  392. struct res_lib_votequorum_status res_lib_votequorum_status;
  393. error = hdb_error_to_cs(hdb_handle_get (&votequorum_handle_t_db, handle, (void *)&votequorum_inst));
  394. if (error != CS_OK) {
  395. return (error);
  396. }
  397. req_lib_votequorum_general.header.size = sizeof (struct req_lib_votequorum_general);
  398. req_lib_votequorum_general.header.id = MESSAGE_REQ_VOTEQUORUM_TRACKSTOP;
  399. iov.iov_base = (char *)&req_lib_votequorum_general;
  400. iov.iov_len = sizeof (struct req_lib_votequorum_general);
  401. error = qb_to_cs_error(qb_ipcc_sendv_recv (
  402. votequorum_inst->c,
  403. &iov,
  404. 1,
  405. &res_lib_votequorum_status,
  406. sizeof (struct res_lib_votequorum_status), CS_IPC_TIMEOUT_MS));
  407. if (error != CS_OK) {
  408. goto error_exit;
  409. }
  410. error = res_lib_votequorum_status.header.error;
  411. error_exit:
  412. hdb_handle_put (&votequorum_handle_t_db, handle);
  413. return (error);
  414. }
  415. cs_error_t votequorum_context_get (
  416. votequorum_handle_t handle,
  417. void **context)
  418. {
  419. cs_error_t error;
  420. struct votequorum_inst *votequorum_inst;
  421. error = hdb_error_to_cs(hdb_handle_get (&votequorum_handle_t_db, handle, (void *)&votequorum_inst));
  422. if (error != CS_OK) {
  423. return (error);
  424. }
  425. *context = votequorum_inst->context;
  426. hdb_handle_put (&votequorum_handle_t_db, handle);
  427. return (CS_OK);
  428. }
  429. cs_error_t votequorum_context_set (
  430. votequorum_handle_t handle,
  431. void *context)
  432. {
  433. cs_error_t error;
  434. struct votequorum_inst *votequorum_inst;
  435. error = hdb_error_to_cs(hdb_handle_get (&votequorum_handle_t_db, handle, (void *)&votequorum_inst));
  436. if (error != CS_OK) {
  437. return (error);
  438. }
  439. votequorum_inst->context = context;
  440. hdb_handle_put (&votequorum_handle_t_db, handle);
  441. return (CS_OK);
  442. }
  443. cs_error_t votequorum_fd_get (
  444. votequorum_handle_t handle,
  445. int *fd)
  446. {
  447. cs_error_t error;
  448. struct votequorum_inst *votequorum_inst;
  449. error = hdb_error_to_cs(hdb_handle_get (&votequorum_handle_t_db, handle, (void *)&votequorum_inst));
  450. if (error != CS_OK) {
  451. return (error);
  452. }
  453. error = qb_to_cs_error(qb_ipcc_fd_get (votequorum_inst->c, fd));
  454. (void)hdb_handle_put (&votequorum_handle_t_db, handle);
  455. return (error);
  456. }
  457. cs_error_t votequorum_dispatch (
  458. votequorum_handle_t handle,
  459. cs_dispatch_flags_t dispatch_types)
  460. {
  461. int timeout = -1;
  462. cs_error_t error;
  463. int cont = 1; /* always continue do loop except when set to 0 */
  464. struct votequorum_inst *votequorum_inst;
  465. votequorum_callbacks_t callbacks;
  466. struct qb_ipc_response_header *dispatch_data;
  467. struct res_lib_votequorum_notification *res_lib_votequorum_notification;
  468. struct res_lib_votequorum_expectedvotes_notification *res_lib_votequorum_expectedvotes_notification;
  469. char dispatch_buf[IPC_DISPATCH_SIZE];
  470. if (dispatch_types != CS_DISPATCH_ONE &&
  471. dispatch_types != CS_DISPATCH_ALL &&
  472. dispatch_types != CS_DISPATCH_BLOCKING) {
  473. return (CS_ERR_INVALID_PARAM);
  474. }
  475. error = hdb_error_to_cs(hdb_handle_get (&votequorum_handle_t_db, handle,
  476. (void *)&votequorum_inst));
  477. if (error != CS_OK) {
  478. return (error);
  479. }
  480. /*
  481. * Timeout instantly for CS_DISPATCH_ONE or CS_DISPATCH_ALL and
  482. * wait indefinitely for CS_DISPATCH_BLOCKING
  483. */
  484. if (dispatch_types == CS_DISPATCH_ALL) {
  485. timeout = 0;
  486. }
  487. dispatch_data = (struct qb_ipc_response_header *)dispatch_buf;
  488. do {
  489. error = qb_to_cs_error (qb_ipcc_event_recv (
  490. votequorum_inst->c,
  491. dispatch_buf,
  492. IPC_DISPATCH_SIZE,
  493. timeout));
  494. if (error == CS_ERR_BAD_HANDLE) {
  495. error = CS_OK;
  496. goto error_put;
  497. }
  498. if (error == CS_ERR_TRY_AGAIN) {
  499. error = CS_OK;
  500. if (dispatch_types == CPG_DISPATCH_ALL) {
  501. break; /* exit do while cont is 1 loop */
  502. } else {
  503. continue; /* next poll */
  504. }
  505. }
  506. if (error != CS_OK) {
  507. goto error_put;
  508. }
  509. /*
  510. * Make copy of callbacks, message data, unlock instance, and call callback
  511. * A risk of this dispatch method is that the callback routines may
  512. * operate at the same time that votequorum_finalize has been called in another thread.
  513. */
  514. memcpy (&callbacks, &votequorum_inst->callbacks, sizeof (votequorum_callbacks_t));
  515. /*
  516. * Dispatch incoming message
  517. */
  518. switch (dispatch_data->id) {
  519. case MESSAGE_RES_VOTEQUORUM_NOTIFICATION:
  520. if (callbacks.votequorum_notify_fn == NULL) {
  521. break;
  522. }
  523. res_lib_votequorum_notification = (struct res_lib_votequorum_notification *)dispatch_data;
  524. callbacks.votequorum_notify_fn ( handle,
  525. res_lib_votequorum_notification->context,
  526. res_lib_votequorum_notification->quorate,
  527. res_lib_votequorum_notification->node_list_entries,
  528. (votequorum_node_t *)res_lib_votequorum_notification->node_list );
  529. ;
  530. break;
  531. case MESSAGE_RES_VOTEQUORUM_EXPECTEDVOTES_NOTIFICATION:
  532. if (callbacks.votequorum_expectedvotes_notify_fn == NULL) {
  533. break;
  534. }
  535. res_lib_votequorum_expectedvotes_notification = (struct res_lib_votequorum_expectedvotes_notification *)dispatch_data;
  536. callbacks.votequorum_expectedvotes_notify_fn ( handle,
  537. res_lib_votequorum_expectedvotes_notification->context,
  538. res_lib_votequorum_expectedvotes_notification->expected_votes);
  539. break;
  540. default:
  541. error = CS_ERR_LIBRARY;
  542. goto error_put;
  543. break;
  544. }
  545. /*
  546. * Determine if more messages should be processed
  547. */
  548. if (dispatch_types == CS_DISPATCH_ONE) {
  549. cont = 0;
  550. }
  551. } while (cont);
  552. goto error_put;
  553. error_put:
  554. hdb_handle_put (&votequorum_handle_t_db, handle);
  555. return (error);
  556. }