votequorum.c 23 KB

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