votequorum.c 21 KB

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