votequorum.c 24 KB

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