votequorum.c 24 KB

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