cpg.c 16 KB

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