votequorum.c 21 KB

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