votequorum.c 21 KB

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