votequorum.c 23 KB

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