cpg.c 16 KB

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