votequorum.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  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/qbipcc.h>
  45. #include <corosync/corotypes.h>
  46. #include <corosync/corodefs.h>
  47. #include <corosync/hdb.h>
  48. #include <corosync/votequorum.h>
  49. #include <corosync/ipc_votequorum.h>
  50. #include "util.h"
  51. struct votequorum_inst {
  52. qb_ipcc_connection_t *c;
  53. int finalize;
  54. void *context;
  55. votequorum_callbacks_t callbacks;
  56. };
  57. DECLARE_HDB_DATABASE(votequorum_handle_t_db,NULL);
  58. cs_error_t votequorum_initialize (
  59. votequorum_handle_t *handle,
  60. votequorum_callbacks_t *callbacks)
  61. {
  62. cs_error_t error;
  63. struct votequorum_inst *votequorum_inst;
  64. error = hdb_error_to_cs(hdb_handle_create (&votequorum_handle_t_db, sizeof (struct votequorum_inst), handle));
  65. if (error != CS_OK) {
  66. goto error_no_destroy;
  67. }
  68. error = hdb_error_to_cs(hdb_handle_get (&votequorum_handle_t_db, *handle, (void *)&votequorum_inst));
  69. if (error != CS_OK) {
  70. goto error_destroy;
  71. }
  72. votequorum_inst->c = qb_ipcc_connect ("votequorum", IPC_REQUEST_SIZE);
  73. if (votequorum_inst->c == NULL) {
  74. error = qb_to_cs_error(-errno);
  75. goto error_put_destroy;
  76. }
  77. if (error != CS_OK) {
  78. goto error_put_destroy;
  79. }
  80. if (callbacks)
  81. memcpy(&votequorum_inst->callbacks, callbacks, sizeof (*callbacks));
  82. else
  83. memset(&votequorum_inst->callbacks, 0, sizeof (*callbacks));
  84. hdb_handle_put (&votequorum_handle_t_db, *handle);
  85. return (CS_OK);
  86. error_put_destroy:
  87. hdb_handle_put (&votequorum_handle_t_db, *handle);
  88. error_destroy:
  89. hdb_handle_destroy (&votequorum_handle_t_db, *handle);
  90. error_no_destroy:
  91. return (error);
  92. }
  93. cs_error_t votequorum_finalize (
  94. votequorum_handle_t handle)
  95. {
  96. struct votequorum_inst *votequorum_inst;
  97. cs_error_t error;
  98. error = hdb_error_to_cs(hdb_handle_get (&votequorum_handle_t_db, handle, (void *)&votequorum_inst));
  99. if (error != CS_OK) {
  100. return (error);
  101. }
  102. /*
  103. * Another thread has already started finalizing
  104. */
  105. if (votequorum_inst->finalize) {
  106. hdb_handle_put (&votequorum_handle_t_db, handle);
  107. return (CS_ERR_BAD_HANDLE);
  108. }
  109. votequorum_inst->finalize = 1;
  110. qb_ipcc_disconnect (votequorum_inst->c);
  111. hdb_handle_destroy (&votequorum_handle_t_db, handle);
  112. hdb_handle_put (&votequorum_handle_t_db, handle);
  113. return (CS_OK);
  114. }
  115. cs_error_t votequorum_getinfo (
  116. votequorum_handle_t handle,
  117. unsigned int nodeid,
  118. struct votequorum_info *info)
  119. {
  120. cs_error_t error;
  121. struct votequorum_inst *votequorum_inst;
  122. struct iovec iov;
  123. struct req_lib_votequorum_getinfo req_lib_votequorum_getinfo;
  124. struct res_lib_votequorum_getinfo res_lib_votequorum_getinfo;
  125. error = hdb_error_to_cs(hdb_handle_get (&votequorum_handle_t_db, handle, (void *)&votequorum_inst));
  126. if (error != CS_OK) {
  127. return (error);
  128. }
  129. req_lib_votequorum_getinfo.header.size = sizeof (struct req_lib_votequorum_getinfo);
  130. req_lib_votequorum_getinfo.header.id = MESSAGE_REQ_VOTEQUORUM_GETINFO;
  131. req_lib_votequorum_getinfo.nodeid = nodeid;
  132. iov.iov_base = (char *)&req_lib_votequorum_getinfo;
  133. iov.iov_len = sizeof (struct req_lib_votequorum_getinfo);
  134. error = qb_to_cs_error(qb_ipcc_sendv_recv (
  135. votequorum_inst->c,
  136. &iov,
  137. 1,
  138. &res_lib_votequorum_getinfo,
  139. sizeof (struct res_lib_votequorum_getinfo), -1));
  140. if (error != CS_OK) {
  141. goto error_exit;
  142. }
  143. error = res_lib_votequorum_getinfo.header.error;
  144. info->node_id = res_lib_votequorum_getinfo.nodeid;
  145. info->node_votes = res_lib_votequorum_getinfo.votes;
  146. info->node_expected_votes = res_lib_votequorum_getinfo.expected_votes;
  147. info->highest_expected = res_lib_votequorum_getinfo.highest_expected;
  148. info->total_votes = res_lib_votequorum_getinfo.total_votes;
  149. info->quorum = res_lib_votequorum_getinfo.quorum;
  150. info->flags = res_lib_votequorum_getinfo.flags;
  151. error_exit:
  152. hdb_handle_put (&votequorum_handle_t_db, handle);
  153. return (error);
  154. }
  155. cs_error_t votequorum_setexpected (
  156. votequorum_handle_t handle,
  157. unsigned int expected_votes)
  158. {
  159. cs_error_t error;
  160. struct votequorum_inst *votequorum_inst;
  161. struct iovec iov;
  162. struct req_lib_votequorum_setexpected req_lib_votequorum_setexpected;
  163. struct res_lib_votequorum_status res_lib_votequorum_status;
  164. error = hdb_error_to_cs(hdb_handle_get (&votequorum_handle_t_db, handle, (void *)&votequorum_inst));
  165. if (error != CS_OK) {
  166. return (error);
  167. }
  168. req_lib_votequorum_setexpected.header.size = sizeof (struct req_lib_votequorum_setexpected);
  169. req_lib_votequorum_setexpected.header.id = MESSAGE_REQ_VOTEQUORUM_SETEXPECTED;
  170. req_lib_votequorum_setexpected.expected_votes = expected_votes;
  171. iov.iov_base = (char *)&req_lib_votequorum_setexpected;
  172. iov.iov_len = sizeof (struct req_lib_votequorum_setexpected);
  173. error = qb_to_cs_error(qb_ipcc_sendv_recv (
  174. votequorum_inst->c,
  175. &iov,
  176. 1,
  177. &res_lib_votequorum_status,
  178. sizeof (struct res_lib_votequorum_status), -1));
  179. if (error != CS_OK) {
  180. goto error_exit;
  181. }
  182. error = res_lib_votequorum_status.header.error;
  183. error_exit:
  184. hdb_handle_put (&votequorum_handle_t_db, handle);
  185. return (error);
  186. }
  187. cs_error_t votequorum_setvotes (
  188. votequorum_handle_t handle,
  189. unsigned int nodeid,
  190. unsigned int votes)
  191. {
  192. cs_error_t error;
  193. struct votequorum_inst *votequorum_inst;
  194. struct iovec iov;
  195. struct req_lib_votequorum_setvotes req_lib_votequorum_setvotes;
  196. struct res_lib_votequorum_status res_lib_votequorum_status;
  197. error = hdb_error_to_cs(hdb_handle_get (&votequorum_handle_t_db, handle, (void *)&votequorum_inst));
  198. if (error != CS_OK) {
  199. return (error);
  200. }
  201. req_lib_votequorum_setvotes.header.size = sizeof (struct req_lib_votequorum_setvotes);
  202. req_lib_votequorum_setvotes.header.id = MESSAGE_REQ_VOTEQUORUM_SETVOTES;
  203. req_lib_votequorum_setvotes.nodeid = nodeid;
  204. req_lib_votequorum_setvotes.votes = votes;
  205. iov.iov_base = (char *)&req_lib_votequorum_setvotes;
  206. iov.iov_len = sizeof (struct req_lib_votequorum_setvotes);
  207. error = qb_to_cs_error(qb_ipcc_sendv_recv (
  208. votequorum_inst->c,
  209. &iov,
  210. 1,
  211. &res_lib_votequorum_status,
  212. sizeof (struct res_lib_votequorum_status), -1));
  213. if (error != CS_OK) {
  214. goto error_exit;
  215. }
  216. error = res_lib_votequorum_status.header.error;
  217. error_exit:
  218. hdb_handle_put (&votequorum_handle_t_db, handle);
  219. return (error);
  220. }
  221. cs_error_t votequorum_qdisk_register (
  222. votequorum_handle_t handle,
  223. const char *name,
  224. unsigned int votes)
  225. {
  226. cs_error_t error;
  227. struct votequorum_inst *votequorum_inst;
  228. struct iovec iov;
  229. struct req_lib_votequorum_qdisk_register req_lib_votequorum_qdisk_register;
  230. struct res_lib_votequorum_status res_lib_votequorum_status;
  231. if (strlen(name) > VOTEQUORUM_MAX_QDISK_NAME_LEN)
  232. return CS_ERR_INVALID_PARAM;
  233. error = hdb_error_to_cs(hdb_handle_get (&votequorum_handle_t_db, handle, (void *)&votequorum_inst));
  234. if (error != CS_OK) {
  235. return (error);
  236. }
  237. req_lib_votequorum_qdisk_register.header.size = sizeof (struct req_lib_votequorum_qdisk_register);
  238. req_lib_votequorum_qdisk_register.header.id = MESSAGE_REQ_VOTEQUORUM_QDISK_REGISTER;
  239. strcpy(req_lib_votequorum_qdisk_register.name, name);
  240. req_lib_votequorum_qdisk_register.votes = votes;
  241. iov.iov_base = (char *)&req_lib_votequorum_qdisk_register;
  242. iov.iov_len = sizeof (struct req_lib_votequorum_qdisk_register);
  243. error = qb_to_cs_error(qb_ipcc_sendv_recv (
  244. votequorum_inst->c,
  245. &iov,
  246. 1,
  247. &res_lib_votequorum_status,
  248. sizeof (struct res_lib_votequorum_status), -1));
  249. if (error != CS_OK) {
  250. goto error_exit;
  251. }
  252. error = res_lib_votequorum_status.header.error;
  253. error_exit:
  254. hdb_handle_put (&votequorum_handle_t_db, handle);
  255. return (error);
  256. }
  257. cs_error_t votequorum_qdisk_poll (
  258. votequorum_handle_t handle,
  259. unsigned int state)
  260. {
  261. cs_error_t error;
  262. struct votequorum_inst *votequorum_inst;
  263. struct iovec iov;
  264. struct req_lib_votequorum_qdisk_poll req_lib_votequorum_qdisk_poll;
  265. struct res_lib_votequorum_status res_lib_votequorum_status;
  266. error = hdb_error_to_cs(hdb_handle_get (&votequorum_handle_t_db, handle, (void *)&votequorum_inst));
  267. if (error != CS_OK) {
  268. return (error);
  269. }
  270. req_lib_votequorum_qdisk_poll.header.size = sizeof (struct req_lib_votequorum_qdisk_poll);
  271. req_lib_votequorum_qdisk_poll.header.id = MESSAGE_REQ_VOTEQUORUM_QDISK_POLL;
  272. req_lib_votequorum_qdisk_poll.state = state;
  273. iov.iov_base = (char *)&req_lib_votequorum_qdisk_poll;
  274. iov.iov_len = sizeof (struct req_lib_votequorum_qdisk_poll);
  275. error = qb_to_cs_error(qb_ipcc_sendv_recv (
  276. votequorum_inst->c,
  277. &iov,
  278. 1,
  279. &res_lib_votequorum_status,
  280. sizeof (struct res_lib_votequorum_status), -1));
  281. if (error != CS_OK) {
  282. goto error_exit;
  283. }
  284. error = res_lib_votequorum_status.header.error;
  285. error_exit:
  286. hdb_handle_put (&votequorum_handle_t_db, handle);
  287. return (error);
  288. }
  289. cs_error_t votequorum_qdisk_unregister (
  290. votequorum_handle_t handle)
  291. {
  292. cs_error_t error;
  293. struct votequorum_inst *votequorum_inst;
  294. struct iovec iov;
  295. struct req_lib_votequorum_general req_lib_votequorum_general;
  296. struct res_lib_votequorum_status res_lib_votequorum_status;
  297. error = hdb_error_to_cs(hdb_handle_get (&votequorum_handle_t_db, handle, (void *)&votequorum_inst));
  298. if (error != CS_OK) {
  299. return (error);
  300. }
  301. req_lib_votequorum_general.header.size = sizeof (struct req_lib_votequorum_general);
  302. req_lib_votequorum_general.header.id = MESSAGE_REQ_VOTEQUORUM_QDISK_UNREGISTER;
  303. iov.iov_base = (char *)&req_lib_votequorum_general;
  304. iov.iov_len = sizeof (struct req_lib_votequorum_general);
  305. error = qb_to_cs_error(qb_ipcc_sendv_recv (
  306. votequorum_inst->c,
  307. &iov,
  308. 1,
  309. &res_lib_votequorum_status,
  310. sizeof (struct res_lib_votequorum_status), -1));
  311. if (error != CS_OK) {
  312. goto error_exit;
  313. }
  314. error = res_lib_votequorum_status.header.error;
  315. error_exit:
  316. hdb_handle_put (&votequorum_handle_t_db, handle);
  317. return (error);
  318. }
  319. cs_error_t votequorum_qdisk_getinfo (
  320. votequorum_handle_t handle,
  321. struct votequorum_qdisk_info *qinfo)
  322. {
  323. cs_error_t error;
  324. struct votequorum_inst *votequorum_inst;
  325. struct iovec iov;
  326. struct req_lib_votequorum_general req_lib_votequorum_general;
  327. struct res_lib_votequorum_qdisk_getinfo res_lib_votequorum_qdisk_getinfo;
  328. error = hdb_error_to_cs(hdb_handle_get (&votequorum_handle_t_db, handle, (void *)&votequorum_inst));
  329. if (error != CS_OK) {
  330. return (error);
  331. }
  332. req_lib_votequorum_general.header.size = sizeof (struct req_lib_votequorum_general);
  333. req_lib_votequorum_general.header.id = MESSAGE_REQ_VOTEQUORUM_QDISK_GETINFO;
  334. iov.iov_base = (char *)&req_lib_votequorum_general;
  335. iov.iov_len = sizeof (struct req_lib_votequorum_general);
  336. error = qb_to_cs_error(qb_ipcc_sendv_recv (
  337. votequorum_inst->c,
  338. &iov,
  339. 1,
  340. &res_lib_votequorum_qdisk_getinfo,
  341. sizeof (struct res_lib_votequorum_qdisk_getinfo), -1));
  342. if (error != CS_OK) {
  343. goto error_exit;
  344. }
  345. error = res_lib_votequorum_qdisk_getinfo.header.error;
  346. qinfo->votes = res_lib_votequorum_qdisk_getinfo.votes;
  347. qinfo->state = res_lib_votequorum_qdisk_getinfo.state;
  348. strcpy(qinfo->name, res_lib_votequorum_qdisk_getinfo.name);
  349. error_exit:
  350. hdb_handle_put (&votequorum_handle_t_db, handle);
  351. return (error);
  352. }
  353. cs_error_t votequorum_setstate (
  354. votequorum_handle_t handle)
  355. {
  356. cs_error_t error;
  357. struct votequorum_inst *votequorum_inst;
  358. struct iovec iov;
  359. struct req_lib_votequorum_general req_lib_votequorum_general;
  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_general.header.size = sizeof (struct req_lib_votequorum_general);
  366. req_lib_votequorum_general.header.id = MESSAGE_REQ_VOTEQUORUM_SETSTATE;
  367. iov.iov_base = (char *)&req_lib_votequorum_general;
  368. iov.iov_len = sizeof (struct req_lib_votequorum_general);
  369. error = qb_to_cs_error(qb_ipcc_sendv_recv (
  370. votequorum_inst->c,
  371. &iov,
  372. 1,
  373. &res_lib_votequorum_status,
  374. sizeof (struct res_lib_votequorum_status), -1));
  375. if (error != CS_OK) {
  376. goto error_exit;
  377. }
  378. error = res_lib_votequorum_status.header.error;
  379. error_exit:
  380. hdb_handle_put (&votequorum_handle_t_db, handle);
  381. return (error);
  382. }
  383. cs_error_t votequorum_leaving (
  384. votequorum_handle_t handle)
  385. {
  386. cs_error_t error;
  387. struct votequorum_inst *votequorum_inst;
  388. struct iovec iov;
  389. struct req_lib_votequorum_general req_lib_votequorum_general;
  390. struct res_lib_votequorum_status res_lib_votequorum_status;
  391. error = hdb_error_to_cs(hdb_handle_get (&votequorum_handle_t_db, handle, (void *)&votequorum_inst));
  392. if (error != CS_OK) {
  393. return (error);
  394. }
  395. req_lib_votequorum_general.header.size = sizeof (struct req_lib_votequorum_general);
  396. req_lib_votequorum_general.header.id = MESSAGE_REQ_VOTEQUORUM_LEAVING;
  397. iov.iov_base = (char *)&req_lib_votequorum_general;
  398. iov.iov_len = sizeof (struct req_lib_votequorum_general);
  399. error = qb_to_cs_error(qb_ipcc_sendv_recv (
  400. votequorum_inst->c,
  401. &iov,
  402. 1,
  403. &res_lib_votequorum_status,
  404. sizeof (struct res_lib_votequorum_status), -1));
  405. if (error != CS_OK) {
  406. goto error_exit;
  407. }
  408. error = res_lib_votequorum_status.header.error;
  409. error_exit:
  410. hdb_handle_put (&votequorum_handle_t_db, handle);
  411. return (error);
  412. }
  413. cs_error_t votequorum_trackstart (
  414. votequorum_handle_t handle,
  415. uint64_t context,
  416. unsigned int flags)
  417. {
  418. cs_error_t error;
  419. struct votequorum_inst *votequorum_inst;
  420. struct iovec iov;
  421. struct req_lib_votequorum_trackstart req_lib_votequorum_trackstart;
  422. struct res_lib_votequorum_status res_lib_votequorum_status;
  423. error = hdb_error_to_cs(hdb_handle_get (&votequorum_handle_t_db, handle, (void *)&votequorum_inst));
  424. if (error != CS_OK) {
  425. return (error);
  426. }
  427. req_lib_votequorum_trackstart.header.size = sizeof (struct req_lib_votequorum_trackstart);
  428. req_lib_votequorum_trackstart.header.id = MESSAGE_REQ_VOTEQUORUM_TRACKSTART;
  429. req_lib_votequorum_trackstart.track_flags = flags;
  430. req_lib_votequorum_trackstart.context = context;
  431. iov.iov_base = (char *)&req_lib_votequorum_trackstart;
  432. iov.iov_len = sizeof (struct req_lib_votequorum_trackstart);
  433. error = qb_to_cs_error(qb_ipcc_sendv_recv (
  434. votequorum_inst->c,
  435. &iov,
  436. 1,
  437. &res_lib_votequorum_status,
  438. sizeof (struct res_lib_votequorum_status), -1));
  439. if (error != CS_OK) {
  440. goto error_exit;
  441. }
  442. error = res_lib_votequorum_status.header.error;
  443. error_exit:
  444. hdb_handle_put (&votequorum_handle_t_db, handle);
  445. return (error);
  446. }
  447. cs_error_t votequorum_trackstop (
  448. votequorum_handle_t handle)
  449. {
  450. cs_error_t error;
  451. struct votequorum_inst *votequorum_inst;
  452. struct iovec iov;
  453. struct req_lib_votequorum_general req_lib_votequorum_general;
  454. struct res_lib_votequorum_status res_lib_votequorum_status;
  455. error = hdb_error_to_cs(hdb_handle_get (&votequorum_handle_t_db, handle, (void *)&votequorum_inst));
  456. if (error != CS_OK) {
  457. return (error);
  458. }
  459. req_lib_votequorum_general.header.size = sizeof (struct req_lib_votequorum_general);
  460. req_lib_votequorum_general.header.id = MESSAGE_REQ_VOTEQUORUM_TRACKSTOP;
  461. iov.iov_base = (char *)&req_lib_votequorum_general;
  462. iov.iov_len = sizeof (struct req_lib_votequorum_general);
  463. error = qb_to_cs_error(qb_ipcc_sendv_recv (
  464. votequorum_inst->c,
  465. &iov,
  466. 1,
  467. &res_lib_votequorum_status,
  468. sizeof (struct res_lib_votequorum_status), -1));
  469. if (error != CS_OK) {
  470. goto error_exit;
  471. }
  472. error = res_lib_votequorum_status.header.error;
  473. error_exit:
  474. hdb_handle_put (&votequorum_handle_t_db, handle);
  475. return (error);
  476. }
  477. cs_error_t votequorum_context_get (
  478. votequorum_handle_t handle,
  479. void **context)
  480. {
  481. cs_error_t error;
  482. struct votequorum_inst *votequorum_inst;
  483. error = hdb_error_to_cs(hdb_handle_get (&votequorum_handle_t_db, handle, (void *)&votequorum_inst));
  484. if (error != CS_OK) {
  485. return (error);
  486. }
  487. *context = votequorum_inst->context;
  488. hdb_handle_put (&votequorum_handle_t_db, handle);
  489. return (CS_OK);
  490. }
  491. cs_error_t votequorum_context_set (
  492. votequorum_handle_t handle,
  493. void *context)
  494. {
  495. cs_error_t error;
  496. struct votequorum_inst *votequorum_inst;
  497. error = hdb_error_to_cs(hdb_handle_get (&votequorum_handle_t_db, handle, (void *)&votequorum_inst));
  498. if (error != CS_OK) {
  499. return (error);
  500. }
  501. votequorum_inst->context = context;
  502. hdb_handle_put (&votequorum_handle_t_db, handle);
  503. return (CS_OK);
  504. }
  505. cs_error_t votequorum_fd_get (
  506. votequorum_handle_t handle,
  507. int *fd)
  508. {
  509. cs_error_t error;
  510. struct votequorum_inst *votequorum_inst;
  511. error = hdb_error_to_cs(hdb_handle_get (&votequorum_handle_t_db, handle, (void *)&votequorum_inst));
  512. if (error != CS_OK) {
  513. return (error);
  514. }
  515. error = qb_to_cs_error(qb_ipcc_fd_get (votequorum_inst->c, fd));
  516. (void)hdb_handle_put (&votequorum_handle_t_db, handle);
  517. return (error);
  518. }
  519. cs_error_t votequorum_dispatch (
  520. votequorum_handle_t handle,
  521. cs_dispatch_flags_t dispatch_types)
  522. {
  523. int timeout = -1;
  524. cs_error_t error;
  525. int cont = 1; /* always continue do loop except when set to 0 */
  526. struct votequorum_inst *votequorum_inst;
  527. votequorum_callbacks_t callbacks;
  528. struct qb_ipc_response_header *dispatch_data;
  529. struct res_lib_votequorum_notification *res_lib_votequorum_notification;
  530. struct res_lib_votequorum_expectedvotes_notification *res_lib_votequorum_expectedvotes_notification;
  531. char dispatch_buf[IPC_DISPATCH_SIZE];
  532. if (dispatch_types != CS_DISPATCH_ONE &&
  533. dispatch_types != CS_DISPATCH_ALL &&
  534. dispatch_types != CS_DISPATCH_BLOCKING) {
  535. return (CS_ERR_INVALID_PARAM);
  536. }
  537. error = hdb_error_to_cs(hdb_handle_get (&votequorum_handle_t_db, handle,
  538. (void *)&votequorum_inst));
  539. if (error != CS_OK) {
  540. return (error);
  541. }
  542. /*
  543. * Timeout instantly for CS_DISPATCH_ONE or CS_DISPATCH_ALL and
  544. * wait indefinitely for CS_DISPATCH_BLOCKING
  545. */
  546. if (dispatch_types == CS_DISPATCH_ALL) {
  547. timeout = 0;
  548. }
  549. dispatch_data = (struct qb_ipc_response_header *)dispatch_buf;
  550. do {
  551. error = qb_to_cs_error (qb_ipcc_event_recv (
  552. votequorum_inst->c,
  553. dispatch_buf,
  554. IPC_DISPATCH_SIZE,
  555. timeout));
  556. if (error == CS_ERR_BAD_HANDLE) {
  557. error = CS_OK;
  558. goto error_put;
  559. }
  560. if (error == CS_ERR_TRY_AGAIN) {
  561. error = CS_OK;
  562. if (dispatch_types == CPG_DISPATCH_ALL) {
  563. break; /* exit do while cont is 1 loop */
  564. } else {
  565. continue; /* next poll */
  566. }
  567. }
  568. if (error != CS_OK) {
  569. goto error_put;
  570. }
  571. /*
  572. * Make copy of callbacks, message data, unlock instance, and call callback
  573. * A risk of this dispatch method is that the callback routines may
  574. * operate at the same time that votequorum_finalize has been called in another thread.
  575. */
  576. memcpy (&callbacks, &votequorum_inst->callbacks, sizeof (votequorum_callbacks_t));
  577. /*
  578. * Dispatch incoming message
  579. */
  580. switch (dispatch_data->id) {
  581. case MESSAGE_RES_VOTEQUORUM_NOTIFICATION:
  582. if (callbacks.votequorum_notify_fn == NULL) {
  583. break;
  584. }
  585. res_lib_votequorum_notification = (struct res_lib_votequorum_notification *)dispatch_data;
  586. callbacks.votequorum_notify_fn ( handle,
  587. res_lib_votequorum_notification->context,
  588. res_lib_votequorum_notification->quorate,
  589. res_lib_votequorum_notification->node_list_entries,
  590. (votequorum_node_t *)res_lib_votequorum_notification->node_list );
  591. ;
  592. break;
  593. case MESSAGE_RES_VOTEQUORUM_EXPECTEDVOTES_NOTIFICATION:
  594. if (callbacks.votequorum_expectedvotes_notify_fn == NULL) {
  595. break;
  596. }
  597. res_lib_votequorum_expectedvotes_notification = (struct res_lib_votequorum_expectedvotes_notification *)dispatch_data;
  598. callbacks.votequorum_expectedvotes_notify_fn ( handle,
  599. res_lib_votequorum_expectedvotes_notification->context,
  600. res_lib_votequorum_expectedvotes_notification->expected_votes);
  601. break;
  602. default:
  603. error = CS_ERR_LIBRARY;
  604. goto error_put;
  605. break;
  606. }
  607. /*
  608. * Determine if more messages should be processed
  609. */
  610. if (dispatch_types == CS_DISPATCH_ONE) {
  611. cont = 0;
  612. }
  613. } while (cont);
  614. goto error_put;
  615. error_put:
  616. hdb_handle_put (&votequorum_handle_t_db, handle);
  617. return (error);
  618. }