cpg.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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. /**
  72. * @defgroup cpg_openais The closed process group API
  73. * @ingroup openais
  74. *
  75. * @{
  76. */
  77. cpg_error_t cpg_initialize (
  78. cpg_handle_t *handle,
  79. cpg_callbacks_t *callbacks)
  80. {
  81. SaAisErrorT error;
  82. struct cpg_inst *cpg_inst;
  83. error = saHandleCreate (&cpg_handle_t_db, sizeof (struct cpg_inst), handle);
  84. if (error != SA_AIS_OK) {
  85. goto error_no_destroy;
  86. }
  87. error = saHandleInstanceGet (&cpg_handle_t_db, *handle, (void *)&cpg_inst);
  88. if (error != SA_AIS_OK) {
  89. goto error_destroy;
  90. }
  91. error = saServiceConnectTwo (&cpg_inst->dispatch_fd,
  92. &cpg_inst->response_fd,
  93. CPG_SERVICE);
  94. if (error != SA_AIS_OK) {
  95. goto error_put_destroy;
  96. }
  97. memcpy (&cpg_inst->callbacks, callbacks, sizeof (cpg_callbacks_t));
  98. pthread_mutex_init (&cpg_inst->response_mutex, NULL);
  99. pthread_mutex_init (&cpg_inst->dispatch_mutex, NULL);
  100. saHandleInstancePut (&cpg_handle_t_db, *handle);
  101. return (SA_AIS_OK);
  102. error_put_destroy:
  103. saHandleInstancePut (&cpg_handle_t_db, *handle);
  104. error_destroy:
  105. saHandleDestroy (&cpg_handle_t_db, *handle);
  106. error_no_destroy:
  107. return (error);
  108. }
  109. cpg_error_t cpg_finalize (
  110. cpg_handle_t handle)
  111. {
  112. struct cpg_inst *cpg_inst;
  113. SaAisErrorT error;
  114. error = saHandleInstanceGet (&cpg_handle_t_db, handle, (void *)&cpg_inst);
  115. if (error != SA_AIS_OK) {
  116. return (error);
  117. }
  118. pthread_mutex_lock (&cpg_inst->response_mutex);
  119. /*
  120. * Another thread has already started finalizing
  121. */
  122. if (cpg_inst->finalize) {
  123. pthread_mutex_unlock (&cpg_inst->response_mutex);
  124. saHandleInstancePut (&cpg_handle_t_db, handle);
  125. return (CPG_ERR_BAD_HANDLE);
  126. }
  127. cpg_inst->finalize = 1;
  128. pthread_mutex_unlock (&cpg_inst->response_mutex);
  129. saHandleDestroy (&cpg_handle_t_db, handle);
  130. /*
  131. * Disconnect from the server
  132. */
  133. if (cpg_inst->response_fd != -1) {
  134. shutdown(cpg_inst->response_fd, 0);
  135. close(cpg_inst->response_fd);
  136. }
  137. if (cpg_inst->dispatch_fd != -1) {
  138. shutdown(cpg_inst->dispatch_fd, 0);
  139. close(cpg_inst->dispatch_fd);
  140. }
  141. saHandleInstancePut (&cpg_handle_t_db, handle);
  142. return (CPG_OK);
  143. }
  144. cpg_error_t cpg_fd_get (
  145. cpg_handle_t handle,
  146. int *fd)
  147. {
  148. SaAisErrorT error;
  149. struct cpg_inst *cpg_inst;
  150. error = saHandleInstanceGet (&cpg_handle_t_db, handle, (void *)&cpg_inst);
  151. if (error != SA_AIS_OK) {
  152. return (error);
  153. }
  154. *fd = cpg_inst->dispatch_fd;
  155. saHandleInstancePut (&cpg_handle_t_db, handle);
  156. return (SA_AIS_OK);
  157. }
  158. struct res_overlay {
  159. struct res_header header;
  160. char data[512000];
  161. };
  162. cpg_error_t cpg_dispatch (
  163. cpg_handle_t handle,
  164. cpg_dispatch_t dispatch_types)
  165. {
  166. struct pollfd ufds;
  167. int timeout = -1;
  168. SaAisErrorT error;
  169. int cont = 1; /* always continue do loop except when set to 0 */
  170. int dispatch_avail;
  171. struct cpg_inst *cpg_inst;
  172. struct res_lib_cpg_confchg_callback *res_cpg_confchg_callback;
  173. struct res_lib_cpg_deliver_callback *res_cpg_deliver_callback;
  174. cpg_callbacks_t callbacks;
  175. struct res_overlay dispatch_data;
  176. int ignore_dispatch = 0;
  177. error = saHandleInstanceGet (&cpg_handle_t_db, handle, (void *)&cpg_inst);
  178. if (error != SA_AIS_OK) {
  179. return (error);
  180. }
  181. /*
  182. * Timeout instantly for SA_DISPATCH_ONE or SA_DISPATCH_ALL and
  183. * wait indefinately for SA_DISPATCH_BLOCKING
  184. */
  185. if (dispatch_types == CPG_DISPATCH_ALL) {
  186. timeout = 0;
  187. }
  188. do {
  189. ufds.fd = cpg_inst->dispatch_fd;
  190. ufds.events = POLLIN;
  191. ufds.revents = 0;
  192. error = saPollRetry (&ufds, 1, timeout);
  193. if (error != SA_AIS_OK) {
  194. goto error_nounlock;
  195. }
  196. pthread_mutex_lock (&cpg_inst->dispatch_mutex);
  197. /*
  198. * Regather poll data in case ufds has changed since taking lock
  199. */
  200. error = saPollRetry (&ufds, 1, timeout);
  201. if (error != SA_AIS_OK) {
  202. goto error_nounlock;
  203. }
  204. /*
  205. * Handle has been finalized in another thread
  206. */
  207. if (cpg_inst->finalize == 1) {
  208. error = CPG_OK;
  209. pthread_mutex_unlock (&cpg_inst->dispatch_mutex);
  210. goto error_unlock;
  211. }
  212. dispatch_avail = ufds.revents & POLLIN;
  213. if (dispatch_avail == 0 && dispatch_types == CPG_DISPATCH_ALL) {
  214. pthread_mutex_unlock (&cpg_inst->dispatch_mutex);
  215. break; /* exit do while cont is 1 loop */
  216. } else
  217. if (dispatch_avail == 0) {
  218. pthread_mutex_unlock (&cpg_inst->dispatch_mutex);
  219. continue; /* next poll */
  220. }
  221. if (ufds.revents & POLLIN) {
  222. /*
  223. * Queue empty, read response from socket
  224. */
  225. error = saRecvRetry (cpg_inst->dispatch_fd, &dispatch_data.header,
  226. sizeof (struct res_header));
  227. if (error != SA_AIS_OK) {
  228. goto error_unlock;
  229. }
  230. if (dispatch_data.header.size > sizeof (struct res_header)) {
  231. error = saRecvRetry (cpg_inst->dispatch_fd, &dispatch_data.data,
  232. dispatch_data.header.size - sizeof (struct res_header));
  233. if (error != SA_AIS_OK) {
  234. goto error_unlock;
  235. }
  236. }
  237. } else {
  238. pthread_mutex_unlock (&cpg_inst->dispatch_mutex);
  239. continue;
  240. }
  241. /*
  242. * Make copy of callbacks, message data, unlock instance, and call callback
  243. * A risk of this dispatch method is that the callback routines may
  244. * operate at the same time that cpgFinalize has been called.
  245. */
  246. memcpy (&callbacks, &cpg_inst->callbacks, sizeof (cpg_callbacks_t));
  247. pthread_mutex_unlock (&cpg_inst->dispatch_mutex);
  248. /*
  249. * Dispatch incoming message
  250. */
  251. switch (dispatch_data.header.id) {
  252. case MESSAGE_RES_CPG_DELIVER_CALLBACK:
  253. res_cpg_deliver_callback = (struct res_lib_cpg_deliver_callback *)&dispatch_data;
  254. callbacks.cpg_deliver_fn (handle,
  255. &res_cpg_deliver_callback->group_name,
  256. res_cpg_deliver_callback->nodeid,
  257. res_cpg_deliver_callback->pid,
  258. &res_cpg_deliver_callback->message,
  259. res_cpg_deliver_callback->msglen);
  260. break;
  261. case MESSAGE_RES_CPG_CONFCHG_CALLBACK:
  262. res_cpg_confchg_callback = (struct res_lib_cpg_confchg_callback *)&dispatch_data;
  263. callbacks.cpg_confchg_fn (handle,
  264. &res_cpg_confchg_callback->group_name,
  265. (struct cpg_address *)res_cpg_confchg_callback->member_list,
  266. res_cpg_confchg_callback->member_list_entries,
  267. (struct cpg_address *)res_cpg_confchg_callback->member_list + res_cpg_confchg_callback->member_list_entries,
  268. res_cpg_confchg_callback->left_list_entries,
  269. (struct cpg_address *)res_cpg_confchg_callback->member_list + res_cpg_confchg_callback->member_list_entries + res_cpg_confchg_callback->left_list_entries,
  270. res_cpg_confchg_callback->joined_list_entries);
  271. break;
  272. default:
  273. error = SA_AIS_ERR_LIBRARY;
  274. goto error_nounlock;
  275. break;
  276. }
  277. /*
  278. * Determine if more messages should be processed
  279. * */
  280. switch (dispatch_types) {
  281. case CPG_DISPATCH_ONE:
  282. if (ignore_dispatch) {
  283. ignore_dispatch = 0;
  284. } else {
  285. cont = 0;
  286. }
  287. break;
  288. case CPG_DISPATCH_ALL:
  289. if (ignore_dispatch) {
  290. ignore_dispatch = 0;
  291. }
  292. break;
  293. case CPG_DISPATCH_BLOCKING:
  294. break;
  295. }
  296. } while (cont);
  297. error_unlock:
  298. saHandleInstancePut (&cpg_handle_t_db, handle);
  299. error_nounlock:
  300. return (error);
  301. }
  302. cpg_error_t cpg_join (
  303. cpg_handle_t handle,
  304. struct cpg_name *group)
  305. {
  306. cpg_error_t error;
  307. struct cpg_inst *cpg_inst;
  308. struct iovec iov[2];
  309. struct req_lib_cpg_join req_lib_cpg_join;
  310. struct res_lib_cpg_join res_lib_cpg_join;
  311. struct req_lib_cpg_trackstart req_lib_cpg_trackstart;
  312. struct res_lib_cpg_trackstart res_lib_cpg_trackstart;
  313. error = saHandleInstanceGet (&cpg_handle_t_db, handle, (void *)&cpg_inst);
  314. if (error != SA_AIS_OK) {
  315. return (error);
  316. }
  317. pthread_mutex_lock (&cpg_inst->response_mutex);
  318. /* Automatically add a tracker */
  319. req_lib_cpg_trackstart.header.size = sizeof (struct req_lib_cpg_trackstart);
  320. req_lib_cpg_trackstart.header.id = MESSAGE_REQ_CPG_TRACKSTART;
  321. memcpy(&req_lib_cpg_trackstart.group_name, group, sizeof(struct cpg_name));
  322. iov[0].iov_base = &req_lib_cpg_trackstart;
  323. iov[0].iov_len = sizeof (struct req_lib_cpg_trackstart);
  324. error = saSendMsgReceiveReply (cpg_inst->dispatch_fd, iov, 1,
  325. &res_lib_cpg_trackstart, sizeof (struct res_lib_cpg_trackstart));
  326. if (error != SA_AIS_OK) {
  327. pthread_mutex_unlock (&cpg_inst->response_mutex);
  328. goto error_exit;
  329. }
  330. /* Now join */
  331. req_lib_cpg_join.header.size = sizeof (struct req_lib_cpg_join);
  332. req_lib_cpg_join.header.id = MESSAGE_REQ_CPG_JOIN;
  333. req_lib_cpg_join.pid = getpid();
  334. memcpy(&req_lib_cpg_join.group_name, group, sizeof(struct cpg_name));
  335. iov[0].iov_base = &req_lib_cpg_join;
  336. iov[0].iov_len = sizeof (struct req_lib_cpg_join);
  337. error = saSendMsgReceiveReply (cpg_inst->response_fd, iov, 1,
  338. &res_lib_cpg_join, sizeof (struct res_lib_cpg_join));
  339. pthread_mutex_unlock (&cpg_inst->response_mutex);
  340. if (error != SA_AIS_OK) {
  341. goto error_exit;
  342. }
  343. error = res_lib_cpg_join.header.error;
  344. error_exit:
  345. saHandleInstancePut (&cpg_handle_t_db, handle);
  346. return (error);
  347. }
  348. cpg_error_t cpg_leave (
  349. cpg_handle_t handle,
  350. struct cpg_name *group)
  351. {
  352. cpg_error_t error;
  353. struct cpg_inst *cpg_inst;
  354. struct iovec iov[2];
  355. struct req_lib_cpg_leave req_lib_cpg_leave;
  356. struct res_lib_cpg_leave res_lib_cpg_leave;
  357. error = saHandleInstanceGet (&cpg_handle_t_db, handle, (void *)&cpg_inst);
  358. if (error != SA_AIS_OK) {
  359. return (error);
  360. }
  361. req_lib_cpg_leave.header.size = sizeof (struct req_lib_cpg_leave);
  362. req_lib_cpg_leave.header.id = MESSAGE_REQ_CPG_LEAVE;
  363. req_lib_cpg_leave.pid = getpid();
  364. memcpy(&req_lib_cpg_leave.group_name, group, sizeof(struct cpg_name));
  365. iov[0].iov_base = &req_lib_cpg_leave;
  366. iov[0].iov_len = sizeof (struct req_lib_cpg_leave);
  367. pthread_mutex_lock (&cpg_inst->response_mutex);
  368. error = saSendMsgReceiveReply (cpg_inst->response_fd, iov, 1,
  369. &res_lib_cpg_leave, sizeof (struct res_lib_cpg_leave));
  370. pthread_mutex_unlock (&cpg_inst->response_mutex);
  371. if (error != SA_AIS_OK) {
  372. goto error_exit;
  373. }
  374. error = res_lib_cpg_leave.header.error;
  375. error_exit:
  376. saHandleInstancePut (&cpg_handle_t_db, handle);
  377. return (error);
  378. }
  379. cpg_error_t cpg_mcast_joined (
  380. cpg_handle_t handle,
  381. cpg_guarantee_t guarantee,
  382. struct iovec *iovec,
  383. int iov_len)
  384. {
  385. int i;
  386. cpg_error_t error;
  387. struct cpg_inst *cpg_inst;
  388. struct iovec iov[64];
  389. struct req_lib_cpg_mcast req_lib_cpg_mcast;
  390. struct res_header res_lib_cpg_mcast;
  391. int msg_len = 0;
  392. error = saHandleInstanceGet (&cpg_handle_t_db, handle, (void *)&cpg_inst);
  393. if (error != SA_AIS_OK) {
  394. return (error);
  395. }
  396. for (i = 0; i < iov_len; i++ ) {
  397. msg_len += iovec[i].iov_len;
  398. }
  399. req_lib_cpg_mcast.header.size = sizeof (struct req_lib_cpg_mcast) +
  400. msg_len;
  401. req_lib_cpg_mcast.header.id = MESSAGE_REQ_CPG_MCAST;
  402. req_lib_cpg_mcast.guarantee = guarantee;
  403. req_lib_cpg_mcast.msglen = msg_len;
  404. iov[0].iov_base = &req_lib_cpg_mcast;
  405. iov[0].iov_len = sizeof (struct req_lib_cpg_mcast);
  406. memcpy (&iov[1], iovec, iov_len * sizeof (struct iovec));
  407. pthread_mutex_lock (&cpg_inst->response_mutex);
  408. error = saSendMsgReceiveReply (cpg_inst->response_fd, iov, iov_len + 1,
  409. &res_lib_cpg_mcast, sizeof (struct res_header));
  410. pthread_mutex_unlock (&cpg_inst->response_mutex);
  411. if (error != SA_AIS_OK) {
  412. goto error_exit;
  413. }
  414. error = res_lib_cpg_mcast.error;
  415. error_exit:
  416. saHandleInstancePut (&cpg_handle_t_db, handle);
  417. return (error);
  418. }
  419. cpg_error_t cpg_membership_get (
  420. cpg_handle_t handle,
  421. struct cpg_name *group_name,
  422. struct cpg_address *member_list,
  423. int *member_list_entries)
  424. {
  425. cpg_error_t error;
  426. struct cpg_inst *cpg_inst;
  427. struct iovec iov;
  428. struct req_lib_cpg_membership req_lib_cpg_membership_get;
  429. struct res_lib_cpg_confchg_callback res_lib_cpg_membership_get;
  430. error = saHandleInstanceGet (&cpg_handle_t_db, handle, (void *)&cpg_inst);
  431. if (error != SA_AIS_OK) {
  432. return (error);
  433. }
  434. req_lib_cpg_membership_get.header.size = sizeof (struct req_header);
  435. req_lib_cpg_membership_get.header.id = MESSAGE_REQ_CPG_MEMBERSHIP;
  436. memcpy(&req_lib_cpg_membership_get.group_name, group_name, sizeof(struct cpg_name));
  437. iov.iov_base = &req_lib_cpg_membership_get;
  438. iov.iov_len = sizeof (struct req_header);
  439. pthread_mutex_lock (&cpg_inst->response_mutex);
  440. error = saSendMsgReceiveReply (cpg_inst->response_fd, &iov, 1,
  441. &res_lib_cpg_membership_get, sizeof (struct res_header));
  442. pthread_mutex_unlock (&cpg_inst->response_mutex);
  443. if (error != SA_AIS_OK) {
  444. goto error_exit;
  445. }
  446. error = res_lib_cpg_membership_get.header.error;
  447. /*
  448. * Copy results to caller
  449. */
  450. *member_list_entries = res_lib_cpg_membership_get.member_list_entries;
  451. if (member_list) {
  452. memcpy (member_list, &res_lib_cpg_membership_get.member_list,
  453. *member_list_entries * sizeof (struct cpg_address));
  454. }
  455. error_exit:
  456. saHandleInstancePut (&cpg_handle_t_db, handle);
  457. return (error);
  458. }
  459. /** @} */