cpg.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. /*
  2. * vi: set autoindent tabstop=4 shiftwidth=4 :
  3. *
  4. * Copyright (c) 2006 Red Hat, Inc.
  5. *
  6. * All rights reserved.
  7. *
  8. * Author: Patrick Caulfield (pcaulfie@redhat.com)
  9. *
  10. * This software licensed under BSD license, the text of which follows:
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * - Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  21. * contributors may be used to endorse or promote products derived from this
  22. * software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  28. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  34. * THE POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. /*
  37. * Provides a closed process group API using the openais executive
  38. */
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #include <unistd.h>
  42. #include <pthread.h>
  43. #include <sys/types.h>
  44. #include <sys/socket.h>
  45. #include <errno.h>
  46. #include "../include/saAis.h"
  47. #include "../include/ipc_cpg.h"
  48. #include "../include/cpg.h"
  49. #include "util.h"
  50. struct cpg_inst {
  51. int response_fd;
  52. int dispatch_fd;
  53. int finalize;
  54. cpg_callbacks_t callbacks;
  55. pthread_mutex_t response_mutex;
  56. pthread_mutex_t dispatch_mutex;
  57. };
  58. static void cpg_instance_destructor (void *instance);
  59. static struct saHandleDatabase cpg_handle_t_db = {
  60. .handleCount = 0,
  61. .handles = 0,
  62. .mutex = PTHREAD_MUTEX_INITIALIZER,
  63. .handleInstanceDestructor = cpg_instance_destructor
  64. };
  65. /*
  66. * Clean up function for a cpg instance (cpg_nitialize) handle
  67. */
  68. static void cpg_instance_destructor (void *instance)
  69. {
  70. }
  71. cpg_error_t cpg_initialize (
  72. cpg_handle_t *handle,
  73. cpg_callbacks_t *callbacks)
  74. {
  75. SaAisErrorT error;
  76. struct cpg_inst *cpg_inst;
  77. error = saHandleCreate (&cpg_handle_t_db, sizeof (struct cpg_inst), handle);
  78. if (error != SA_AIS_OK) {
  79. goto error_no_destroy;
  80. }
  81. error = saHandleInstanceGet (&cpg_handle_t_db, *handle, (void *)&cpg_inst);
  82. if (error != SA_AIS_OK) {
  83. goto error_destroy;
  84. }
  85. error = saServiceConnectTwo (&cpg_inst->dispatch_fd,
  86. &cpg_inst->response_fd,
  87. CPG_SERVICE);
  88. if (error != SA_AIS_OK) {
  89. goto error_put_destroy;
  90. }
  91. memcpy (&cpg_inst->callbacks, callbacks, sizeof (cpg_callbacks_t));
  92. pthread_mutex_init (&cpg_inst->response_mutex, NULL);
  93. pthread_mutex_init (&cpg_inst->dispatch_mutex, NULL);
  94. saHandleInstancePut (&cpg_handle_t_db, *handle);
  95. return (SA_AIS_OK);
  96. error_put_destroy:
  97. saHandleInstancePut (&cpg_handle_t_db, *handle);
  98. error_destroy:
  99. saHandleDestroy (&cpg_handle_t_db, *handle);
  100. error_no_destroy:
  101. return (error);
  102. }
  103. cpg_error_t cpg_finalize (
  104. cpg_handle_t handle)
  105. {
  106. struct cpg_inst *cpg_inst;
  107. SaAisErrorT error;
  108. error = saHandleInstanceGet (&cpg_handle_t_db, handle, (void *)&cpg_inst);
  109. if (error != SA_AIS_OK) {
  110. return (error);
  111. }
  112. pthread_mutex_lock (&cpg_inst->response_mutex);
  113. /*
  114. * Another thread has already started finalizing
  115. */
  116. if (cpg_inst->finalize) {
  117. pthread_mutex_unlock (&cpg_inst->response_mutex);
  118. saHandleInstancePut (&cpg_handle_t_db, handle);
  119. return (CPG_ERR_BAD_HANDLE);
  120. }
  121. cpg_inst->finalize = 1;
  122. pthread_mutex_unlock (&cpg_inst->response_mutex);
  123. saHandleDestroy (&cpg_handle_t_db, handle);
  124. /*
  125. * Disconnect from the server
  126. */
  127. if (cpg_inst->response_fd != -1) {
  128. shutdown(cpg_inst->response_fd, 0);
  129. close(cpg_inst->response_fd);
  130. }
  131. if (cpg_inst->dispatch_fd != -1) {
  132. shutdown(cpg_inst->dispatch_fd, 0);
  133. close(cpg_inst->dispatch_fd);
  134. }
  135. saHandleInstancePut (&cpg_handle_t_db, handle);
  136. return (CPG_OK);
  137. }
  138. cpg_error_t cpg_fd_get (
  139. cpg_handle_t handle,
  140. int *fd)
  141. {
  142. SaAisErrorT error;
  143. struct cpg_inst *cpg_inst;
  144. error = saHandleInstanceGet (&cpg_handle_t_db, handle, (void *)&cpg_inst);
  145. if (error != SA_AIS_OK) {
  146. return (error);
  147. }
  148. *fd = cpg_inst->dispatch_fd;
  149. saHandleInstancePut (&cpg_handle_t_db, handle);
  150. return (SA_AIS_OK);
  151. }
  152. struct res_overlay {
  153. struct res_header header;
  154. char data[512000];
  155. };
  156. cpg_error_t cpg_dispatch (
  157. cpg_handle_t handle,
  158. cpg_dispatch_t dispatch_types)
  159. {
  160. struct pollfd ufds;
  161. int timeout = -1;
  162. SaAisErrorT error;
  163. int cont = 1; /* always continue do loop except when set to 0 */
  164. int dispatch_avail;
  165. struct cpg_inst *cpg_inst;
  166. struct res_lib_cpg_confchg_callback *res_cpg_confchg_callback;
  167. struct res_lib_cpg_deliver_callback *res_cpg_deliver_callback;
  168. cpg_callbacks_t callbacks;
  169. struct res_overlay dispatch_data;
  170. int ignore_dispatch = 0;
  171. error = saHandleInstanceGet (&cpg_handle_t_db, handle, (void *)&cpg_inst);
  172. if (error != SA_AIS_OK) {
  173. return (error);
  174. }
  175. /*
  176. * Timeout instantly for SA_DISPATCH_ONE or SA_DISPATCH_ALL and
  177. * wait indefinately for SA_DISPATCH_BLOCKING
  178. */
  179. if (dispatch_types == CPG_DISPATCH_ALL) {
  180. timeout = 0;
  181. }
  182. do {
  183. ufds.fd = cpg_inst->dispatch_fd;
  184. ufds.events = POLLIN;
  185. ufds.revents = 0;
  186. error = saPollRetry (&ufds, 1, timeout);
  187. if (error != SA_AIS_OK) {
  188. goto error_nounlock;
  189. }
  190. pthread_mutex_lock (&cpg_inst->dispatch_mutex);
  191. /*
  192. * Regather poll data in case ufds has changed since taking lock
  193. */
  194. error = saPollRetry (&ufds, 1, timeout);
  195. if (error != SA_AIS_OK) {
  196. goto error_nounlock;
  197. }
  198. /*
  199. * Handle has been finalized in another thread
  200. */
  201. if (cpg_inst->finalize == 1) {
  202. error = CPG_OK;
  203. pthread_mutex_unlock (&cpg_inst->dispatch_mutex);
  204. goto error_unlock;
  205. }
  206. dispatch_avail = ufds.revents & POLLIN;
  207. if (dispatch_avail == 0 && dispatch_types == CPG_DISPATCH_ALL) {
  208. pthread_mutex_unlock (&cpg_inst->dispatch_mutex);
  209. break; /* exit do while cont is 1 loop */
  210. } else
  211. if (dispatch_avail == 0) {
  212. pthread_mutex_unlock (&cpg_inst->dispatch_mutex);
  213. continue; /* next poll */
  214. }
  215. if (ufds.revents & POLLIN) {
  216. /*
  217. * Queue empty, read response from socket
  218. */
  219. error = saRecvRetry (cpg_inst->dispatch_fd, &dispatch_data.header,
  220. sizeof (struct res_header));
  221. if (error != SA_AIS_OK) {
  222. goto error_unlock;
  223. }
  224. if (dispatch_data.header.size > sizeof (struct res_header)) {
  225. error = saRecvRetry (cpg_inst->dispatch_fd, &dispatch_data.data,
  226. dispatch_data.header.size - sizeof (struct res_header));
  227. if (error != SA_AIS_OK) {
  228. goto error_unlock;
  229. }
  230. }
  231. } else {
  232. pthread_mutex_unlock (&cpg_inst->dispatch_mutex);
  233. continue;
  234. }
  235. /*
  236. * Make copy of callbacks, message data, unlock instance, and call callback
  237. * A risk of this dispatch method is that the callback routines may
  238. * operate at the same time that cpgFinalize has been called.
  239. */
  240. memcpy (&callbacks, &cpg_inst->callbacks, sizeof (cpg_callbacks_t));
  241. pthread_mutex_unlock (&cpg_inst->dispatch_mutex);
  242. /*
  243. * Dispatch incoming message
  244. */
  245. switch (dispatch_data.header.id) {
  246. case MESSAGE_RES_CPG_DELIVER_CALLBACK:
  247. res_cpg_deliver_callback = (struct res_lib_cpg_deliver_callback *)&dispatch_data;
  248. callbacks.cpg_deliver_fn (handle,
  249. &res_cpg_deliver_callback->groupName,
  250. res_cpg_deliver_callback->nodeid,
  251. res_cpg_deliver_callback->pid,
  252. &res_cpg_deliver_callback->message,
  253. res_cpg_deliver_callback->msglen);
  254. break;
  255. case MESSAGE_RES_CPG_CONFCHG_CALLBACK:
  256. res_cpg_confchg_callback = (struct res_lib_cpg_confchg_callback *)&dispatch_data;
  257. callbacks.cpg_confchg_fn (handle,
  258. &res_cpg_confchg_callback->groupName,
  259. (struct cpg_address *)res_cpg_confchg_callback->member_list,
  260. res_cpg_confchg_callback->member_list_entries,
  261. (struct cpg_address *)res_cpg_confchg_callback->member_list + res_cpg_confchg_callback->member_list_entries,
  262. res_cpg_confchg_callback->left_list_entries,
  263. (struct cpg_address *)res_cpg_confchg_callback->member_list + res_cpg_confchg_callback->member_list_entries + res_cpg_confchg_callback->left_list_entries,
  264. res_cpg_confchg_callback->joined_list_entries);
  265. break;
  266. default:
  267. error = SA_AIS_ERR_LIBRARY;
  268. goto error_nounlock;
  269. break;
  270. }
  271. /*
  272. * Determine if more messages should be processed
  273. * */
  274. switch (dispatch_types) {
  275. case CPG_DISPATCH_ONE:
  276. if (ignore_dispatch) {
  277. ignore_dispatch = 0;
  278. } else {
  279. cont = 0;
  280. }
  281. break;
  282. case CPG_DISPATCH_ALL:
  283. if (ignore_dispatch) {
  284. ignore_dispatch = 0;
  285. }
  286. break;
  287. case CPG_DISPATCH_BLOCKING:
  288. break;
  289. }
  290. } while (cont);
  291. error_unlock:
  292. saHandleInstancePut (&cpg_handle_t_db, handle);
  293. error_nounlock:
  294. return (error);
  295. }
  296. cpg_error_t cpg_join (
  297. cpg_handle_t handle,
  298. struct cpg_name *group)
  299. {
  300. cpg_error_t error;
  301. struct cpg_inst *cpg_inst;
  302. struct iovec iov[2];
  303. struct req_lib_cpg_join req_lib_cpg_join;
  304. struct res_lib_cpg_join res_lib_cpg_join;
  305. struct req_lib_cpg_trackstart req_lib_cpg_trackstart;
  306. struct res_lib_cpg_trackstart res_lib_cpg_trackstart;
  307. error = saHandleInstanceGet (&cpg_handle_t_db, handle, (void *)&cpg_inst);
  308. if (error != SA_AIS_OK) {
  309. return (error);
  310. }
  311. pthread_mutex_lock (&cpg_inst->response_mutex);
  312. /* Automatically add a tracker */
  313. req_lib_cpg_trackstart.header.size = sizeof (struct req_lib_cpg_trackstart);
  314. req_lib_cpg_trackstart.header.id = MESSAGE_REQ_CPG_TRACKSTART;
  315. memcpy(&req_lib_cpg_trackstart.groupName, group, sizeof(struct cpg_name));
  316. iov[0].iov_base = &req_lib_cpg_trackstart;
  317. iov[0].iov_len = sizeof (struct req_lib_cpg_trackstart);
  318. error = saSendMsgReceiveReply (cpg_inst->dispatch_fd, iov, 1,
  319. &res_lib_cpg_trackstart, sizeof (struct res_lib_cpg_trackstart));
  320. if (error != SA_AIS_OK) {
  321. pthread_mutex_unlock (&cpg_inst->response_mutex);
  322. goto error_exit;
  323. }
  324. /* Now join */
  325. req_lib_cpg_join.header.size = sizeof (struct req_lib_cpg_join);
  326. req_lib_cpg_join.header.id = MESSAGE_REQ_CPG_JOIN;
  327. req_lib_cpg_join.pid = getpid();
  328. memcpy(&req_lib_cpg_join.groupName, group, sizeof(struct cpg_name));
  329. iov[0].iov_base = &req_lib_cpg_join;
  330. iov[0].iov_len = sizeof (struct req_lib_cpg_join);
  331. error = saSendMsgReceiveReply (cpg_inst->response_fd, iov, 1,
  332. &res_lib_cpg_join, sizeof (struct res_lib_cpg_join));
  333. pthread_mutex_unlock (&cpg_inst->response_mutex);
  334. if (error != SA_AIS_OK) {
  335. goto error_exit;
  336. }
  337. error = res_lib_cpg_join.header.error;
  338. error_exit:
  339. saHandleInstancePut (&cpg_handle_t_db, handle);
  340. return (error);
  341. }
  342. cpg_error_t cpg_leave (
  343. cpg_handle_t handle,
  344. struct cpg_name *group)
  345. {
  346. cpg_error_t error;
  347. struct cpg_inst *cpg_inst;
  348. struct iovec iov[2];
  349. struct req_lib_cpg_leave req_lib_cpg_leave;
  350. struct res_lib_cpg_leave res_lib_cpg_leave;
  351. error = saHandleInstanceGet (&cpg_handle_t_db, handle, (void *)&cpg_inst);
  352. if (error != SA_AIS_OK) {
  353. return (error);
  354. }
  355. req_lib_cpg_leave.header.size = sizeof (struct req_lib_cpg_leave);
  356. req_lib_cpg_leave.header.id = MESSAGE_REQ_CPG_LEAVE;
  357. req_lib_cpg_leave.pid = getpid();
  358. memcpy(&req_lib_cpg_leave.groupName, group, sizeof(struct cpg_name));
  359. iov[0].iov_base = &req_lib_cpg_leave;
  360. iov[0].iov_len = sizeof (struct req_lib_cpg_leave);
  361. pthread_mutex_lock (&cpg_inst->response_mutex);
  362. error = saSendMsgReceiveReply (cpg_inst->response_fd, iov, 1,
  363. &res_lib_cpg_leave, sizeof (struct res_lib_cpg_leave));
  364. pthread_mutex_unlock (&cpg_inst->response_mutex);
  365. if (error != SA_AIS_OK) {
  366. goto error_exit;
  367. }
  368. error = res_lib_cpg_leave.header.error;
  369. error_exit:
  370. saHandleInstancePut (&cpg_handle_t_db, handle);
  371. return (error);
  372. }
  373. cpg_error_t cpg_mcast_joined (
  374. cpg_handle_t handle,
  375. cpg_guarantee_t guarantee,
  376. struct iovec *iovec,
  377. int iov_len)
  378. {
  379. int i;
  380. cpg_error_t error;
  381. struct cpg_inst *cpg_inst;
  382. struct iovec iov[64];
  383. struct req_lib_cpg_mcast req_lib_cpg_mcast;
  384. struct res_header res_lib_cpg_mcast;
  385. int msg_len = 0;
  386. error = saHandleInstanceGet (&cpg_handle_t_db, handle, (void *)&cpg_inst);
  387. if (error != SA_AIS_OK) {
  388. return (error);
  389. }
  390. for (i = 0; i < iov_len; i++ ) {
  391. msg_len += iovec[i].iov_len;
  392. }
  393. req_lib_cpg_mcast.header.size = sizeof (struct req_lib_cpg_mcast) +
  394. msg_len;
  395. req_lib_cpg_mcast.header.id = MESSAGE_REQ_CPG_MCAST;
  396. req_lib_cpg_mcast.guarantee = guarantee;
  397. req_lib_cpg_mcast.msglen = msg_len;
  398. iov[0].iov_base = &req_lib_cpg_mcast;
  399. iov[0].iov_len = sizeof (struct req_lib_cpg_mcast);
  400. memcpy (&iov[1], iovec, iov_len * sizeof (struct iovec));
  401. pthread_mutex_lock (&cpg_inst->response_mutex);
  402. error = saSendMsgReceiveReply (cpg_inst->response_fd, iov, iov_len + 1,
  403. &res_lib_cpg_mcast, sizeof (struct res_header));
  404. pthread_mutex_unlock (&cpg_inst->response_mutex);
  405. if (error != SA_AIS_OK) {
  406. goto error_exit;
  407. }
  408. error = res_lib_cpg_mcast.error;
  409. error_exit:
  410. saHandleInstancePut (&cpg_handle_t_db, handle);
  411. return (error);
  412. }
  413. cpg_error_t cpg_membership_get (
  414. cpg_handle_t handle,
  415. struct cpg_name *groupName,
  416. struct cpg_address *member_list,
  417. int *member_list_entries)
  418. {
  419. cpg_error_t error;
  420. struct cpg_inst *cpg_inst;
  421. struct iovec iov;
  422. struct req_lib_cpg_membership req_lib_cpg_membership_get;
  423. struct res_lib_cpg_confchg_callback res_lib_cpg_membership_get;
  424. error = saHandleInstanceGet (&cpg_handle_t_db, handle, (void *)&cpg_inst);
  425. if (error != SA_AIS_OK) {
  426. return (error);
  427. }
  428. req_lib_cpg_membership_get.header.size = sizeof (struct req_header);
  429. req_lib_cpg_membership_get.header.id = MESSAGE_REQ_CPG_MEMBERSHIP;
  430. memcpy(&req_lib_cpg_membership_get.groupName, groupName, sizeof(struct cpg_name));
  431. iov.iov_base = &req_lib_cpg_membership_get;
  432. iov.iov_len = sizeof (struct req_header);
  433. pthread_mutex_lock (&cpg_inst->response_mutex);
  434. error = saSendMsgReceiveReply (cpg_inst->response_fd, &iov, 1,
  435. &res_lib_cpg_membership_get, sizeof (struct res_header));
  436. pthread_mutex_unlock (&cpg_inst->response_mutex);
  437. if (error != SA_AIS_OK) {
  438. goto error_exit;
  439. }
  440. error = res_lib_cpg_membership_get.header.error;
  441. /*
  442. * Copy results to caller
  443. */
  444. *member_list_entries = res_lib_cpg_membership_get.member_list_entries;
  445. if (member_list) {
  446. memcpy (member_list, &res_lib_cpg_membership_get.member_list,
  447. *member_list_entries * sizeof (struct cpg_address));
  448. }
  449. error_exit:
  450. saHandleInstancePut (&cpg_handle_t_db, handle);
  451. return (error);
  452. }