votequorum.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  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_setstate (
  352. votequorum_handle_t handle)
  353. {
  354. cs_error_t error;
  355. struct votequorum_inst *votequorum_inst;
  356. struct iovec iov;
  357. struct req_lib_votequorum_general req_lib_votequorum_general;
  358. struct res_lib_votequorum_status res_lib_votequorum_status;
  359. error = hdb_error_to_cs(hdb_handle_get (&votequorum_handle_t_db, handle, (void *)&votequorum_inst));
  360. if (error != CS_OK) {
  361. return (error);
  362. }
  363. req_lib_votequorum_general.header.size = sizeof (struct req_lib_votequorum_general);
  364. req_lib_votequorum_general.header.id = MESSAGE_REQ_VOTEQUORUM_SETSTATE;
  365. iov.iov_base = (char *)&req_lib_votequorum_general;
  366. iov.iov_len = sizeof (struct req_lib_votequorum_general);
  367. error = qb_to_cs_error(qb_ipcc_sendv_recv (
  368. votequorum_inst->c,
  369. &iov,
  370. 1,
  371. &res_lib_votequorum_status,
  372. sizeof (struct res_lib_votequorum_status), CS_IPC_TIMEOUT_MS));
  373. if (error != CS_OK) {
  374. goto error_exit;
  375. }
  376. error = res_lib_votequorum_status.header.error;
  377. error_exit:
  378. hdb_handle_put (&votequorum_handle_t_db, handle);
  379. return (error);
  380. }
  381. cs_error_t votequorum_leaving (
  382. votequorum_handle_t handle)
  383. {
  384. cs_error_t error;
  385. struct votequorum_inst *votequorum_inst;
  386. struct iovec iov;
  387. struct req_lib_votequorum_general req_lib_votequorum_general;
  388. struct res_lib_votequorum_status res_lib_votequorum_status;
  389. error = hdb_error_to_cs(hdb_handle_get (&votequorum_handle_t_db, handle, (void *)&votequorum_inst));
  390. if (error != CS_OK) {
  391. return (error);
  392. }
  393. req_lib_votequorum_general.header.size = sizeof (struct req_lib_votequorum_general);
  394. req_lib_votequorum_general.header.id = MESSAGE_REQ_VOTEQUORUM_LEAVING;
  395. iov.iov_base = (char *)&req_lib_votequorum_general;
  396. iov.iov_len = sizeof (struct req_lib_votequorum_general);
  397. error = qb_to_cs_error(qb_ipcc_sendv_recv (
  398. votequorum_inst->c,
  399. &iov,
  400. 1,
  401. &res_lib_votequorum_status,
  402. sizeof (struct res_lib_votequorum_status), CS_IPC_TIMEOUT_MS));
  403. if (error != CS_OK) {
  404. goto error_exit;
  405. }
  406. error = res_lib_votequorum_status.header.error;
  407. error_exit:
  408. hdb_handle_put (&votequorum_handle_t_db, handle);
  409. return (error);
  410. }
  411. cs_error_t votequorum_trackstart (
  412. votequorum_handle_t handle,
  413. uint64_t context,
  414. unsigned int flags)
  415. {
  416. cs_error_t error;
  417. struct votequorum_inst *votequorum_inst;
  418. struct iovec iov;
  419. struct req_lib_votequorum_trackstart req_lib_votequorum_trackstart;
  420. struct res_lib_votequorum_status res_lib_votequorum_status;
  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. req_lib_votequorum_trackstart.header.size = sizeof (struct req_lib_votequorum_trackstart);
  426. req_lib_votequorum_trackstart.header.id = MESSAGE_REQ_VOTEQUORUM_TRACKSTART;
  427. req_lib_votequorum_trackstart.track_flags = flags;
  428. req_lib_votequorum_trackstart.context = context;
  429. iov.iov_base = (char *)&req_lib_votequorum_trackstart;
  430. iov.iov_len = sizeof (struct req_lib_votequorum_trackstart);
  431. error = qb_to_cs_error(qb_ipcc_sendv_recv (
  432. votequorum_inst->c,
  433. &iov,
  434. 1,
  435. &res_lib_votequorum_status,
  436. sizeof (struct res_lib_votequorum_status), CS_IPC_TIMEOUT_MS));
  437. if (error != CS_OK) {
  438. goto error_exit;
  439. }
  440. error = res_lib_votequorum_status.header.error;
  441. error_exit:
  442. hdb_handle_put (&votequorum_handle_t_db, handle);
  443. return (error);
  444. }
  445. cs_error_t votequorum_trackstop (
  446. votequorum_handle_t handle)
  447. {
  448. cs_error_t error;
  449. struct votequorum_inst *votequorum_inst;
  450. struct iovec iov;
  451. struct req_lib_votequorum_general req_lib_votequorum_general;
  452. struct res_lib_votequorum_status res_lib_votequorum_status;
  453. error = hdb_error_to_cs(hdb_handle_get (&votequorum_handle_t_db, handle, (void *)&votequorum_inst));
  454. if (error != CS_OK) {
  455. return (error);
  456. }
  457. req_lib_votequorum_general.header.size = sizeof (struct req_lib_votequorum_general);
  458. req_lib_votequorum_general.header.id = MESSAGE_REQ_VOTEQUORUM_TRACKSTOP;
  459. iov.iov_base = (char *)&req_lib_votequorum_general;
  460. iov.iov_len = sizeof (struct req_lib_votequorum_general);
  461. error = qb_to_cs_error(qb_ipcc_sendv_recv (
  462. votequorum_inst->c,
  463. &iov,
  464. 1,
  465. &res_lib_votequorum_status,
  466. sizeof (struct res_lib_votequorum_status), CS_IPC_TIMEOUT_MS));
  467. if (error != CS_OK) {
  468. goto error_exit;
  469. }
  470. error = res_lib_votequorum_status.header.error;
  471. error_exit:
  472. hdb_handle_put (&votequorum_handle_t_db, handle);
  473. return (error);
  474. }
  475. cs_error_t votequorum_context_get (
  476. votequorum_handle_t handle,
  477. void **context)
  478. {
  479. cs_error_t error;
  480. struct votequorum_inst *votequorum_inst;
  481. error = hdb_error_to_cs(hdb_handle_get (&votequorum_handle_t_db, handle, (void *)&votequorum_inst));
  482. if (error != CS_OK) {
  483. return (error);
  484. }
  485. *context = votequorum_inst->context;
  486. hdb_handle_put (&votequorum_handle_t_db, handle);
  487. return (CS_OK);
  488. }
  489. cs_error_t votequorum_context_set (
  490. votequorum_handle_t handle,
  491. void *context)
  492. {
  493. cs_error_t error;
  494. struct votequorum_inst *votequorum_inst;
  495. error = hdb_error_to_cs(hdb_handle_get (&votequorum_handle_t_db, handle, (void *)&votequorum_inst));
  496. if (error != CS_OK) {
  497. return (error);
  498. }
  499. votequorum_inst->context = context;
  500. hdb_handle_put (&votequorum_handle_t_db, handle);
  501. return (CS_OK);
  502. }
  503. cs_error_t votequorum_fd_get (
  504. votequorum_handle_t handle,
  505. int *fd)
  506. {
  507. cs_error_t error;
  508. struct votequorum_inst *votequorum_inst;
  509. error = hdb_error_to_cs(hdb_handle_get (&votequorum_handle_t_db, handle, (void *)&votequorum_inst));
  510. if (error != CS_OK) {
  511. return (error);
  512. }
  513. error = qb_to_cs_error(qb_ipcc_fd_get (votequorum_inst->c, fd));
  514. (void)hdb_handle_put (&votequorum_handle_t_db, handle);
  515. return (error);
  516. }
  517. cs_error_t votequorum_dispatch (
  518. votequorum_handle_t handle,
  519. cs_dispatch_flags_t dispatch_types)
  520. {
  521. int timeout = -1;
  522. cs_error_t error;
  523. int cont = 1; /* always continue do loop except when set to 0 */
  524. struct votequorum_inst *votequorum_inst;
  525. votequorum_callbacks_t callbacks;
  526. struct qb_ipc_response_header *dispatch_data;
  527. struct res_lib_votequorum_notification *res_lib_votequorum_notification;
  528. struct res_lib_votequorum_expectedvotes_notification *res_lib_votequorum_expectedvotes_notification;
  529. char dispatch_buf[IPC_DISPATCH_SIZE];
  530. if (dispatch_types != CS_DISPATCH_ONE &&
  531. dispatch_types != CS_DISPATCH_ALL &&
  532. dispatch_types != CS_DISPATCH_BLOCKING) {
  533. return (CS_ERR_INVALID_PARAM);
  534. }
  535. error = hdb_error_to_cs(hdb_handle_get (&votequorum_handle_t_db, handle,
  536. (void *)&votequorum_inst));
  537. if (error != CS_OK) {
  538. return (error);
  539. }
  540. /*
  541. * Timeout instantly for CS_DISPATCH_ONE or CS_DISPATCH_ALL and
  542. * wait indefinitely for CS_DISPATCH_BLOCKING
  543. */
  544. if (dispatch_types == CS_DISPATCH_ALL) {
  545. timeout = 0;
  546. }
  547. dispatch_data = (struct qb_ipc_response_header *)dispatch_buf;
  548. do {
  549. error = qb_to_cs_error (qb_ipcc_event_recv (
  550. votequorum_inst->c,
  551. dispatch_buf,
  552. IPC_DISPATCH_SIZE,
  553. timeout));
  554. if (error == CS_ERR_BAD_HANDLE) {
  555. error = CS_OK;
  556. goto error_put;
  557. }
  558. if (error == CS_ERR_TRY_AGAIN) {
  559. error = CS_OK;
  560. if (dispatch_types == CPG_DISPATCH_ALL) {
  561. break; /* exit do while cont is 1 loop */
  562. } else {
  563. continue; /* next poll */
  564. }
  565. }
  566. if (error != CS_OK) {
  567. goto error_put;
  568. }
  569. /*
  570. * Make copy of callbacks, message data, unlock instance, and call callback
  571. * A risk of this dispatch method is that the callback routines may
  572. * operate at the same time that votequorum_finalize has been called in another thread.
  573. */
  574. memcpy (&callbacks, &votequorum_inst->callbacks, sizeof (votequorum_callbacks_t));
  575. /*
  576. * Dispatch incoming message
  577. */
  578. switch (dispatch_data->id) {
  579. case MESSAGE_RES_VOTEQUORUM_NOTIFICATION:
  580. if (callbacks.votequorum_notify_fn == NULL) {
  581. break;
  582. }
  583. res_lib_votequorum_notification = (struct res_lib_votequorum_notification *)dispatch_data;
  584. callbacks.votequorum_notify_fn ( handle,
  585. res_lib_votequorum_notification->context,
  586. res_lib_votequorum_notification->quorate,
  587. res_lib_votequorum_notification->node_list_entries,
  588. (votequorum_node_t *)res_lib_votequorum_notification->node_list );
  589. ;
  590. break;
  591. case MESSAGE_RES_VOTEQUORUM_EXPECTEDVOTES_NOTIFICATION:
  592. if (callbacks.votequorum_expectedvotes_notify_fn == NULL) {
  593. break;
  594. }
  595. res_lib_votequorum_expectedvotes_notification = (struct res_lib_votequorum_expectedvotes_notification *)dispatch_data;
  596. callbacks.votequorum_expectedvotes_notify_fn ( handle,
  597. res_lib_votequorum_expectedvotes_notification->context,
  598. res_lib_votequorum_expectedvotes_notification->expected_votes);
  599. break;
  600. default:
  601. error = CS_ERR_LIBRARY;
  602. goto error_put;
  603. break;
  604. }
  605. /*
  606. * Determine if more messages should be processed
  607. */
  608. if (dispatch_types == CS_DISPATCH_ONE) {
  609. cont = 0;
  610. }
  611. } while (cont);
  612. goto error_put;
  613. error_put:
  614. hdb_handle_put (&votequorum_handle_t_db, handle);
  615. return (error);
  616. }