cpg.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470
  1. /*
  2. * Copyright (c) 2006-2009 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Christine Caulfield (ccaulfie@redhat.com)
  7. * Author: Jan Friesse (jfriesse@redhat.com)
  8. *
  9. * This software licensed under BSD license, the text of which follows:
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * - Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  20. * contributors may be used to endorse or promote products derived from this
  21. * software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  33. * THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. #include <config.h>
  36. #ifdef HAVE_ALLOCA_H
  37. #include <alloca.h>
  38. #endif
  39. #include <sys/types.h>
  40. #include <sys/socket.h>
  41. #include <sys/un.h>
  42. #include <sys/ioctl.h>
  43. #include <netinet/in.h>
  44. #include <sys/uio.h>
  45. #include <unistd.h>
  46. #include <fcntl.h>
  47. #include <stdlib.h>
  48. #include <stdio.h>
  49. #include <errno.h>
  50. #include <time.h>
  51. #include <unistd.h>
  52. #include <netinet/in.h>
  53. #include <arpa/inet.h>
  54. #include <corosync/corotypes.h>
  55. #include <corosync/coroipc_types.h>
  56. #include <corosync/corodefs.h>
  57. #include <corosync/list.h>
  58. #include <corosync/jhash.h>
  59. #include <corosync/lcr/lcr_comp.h>
  60. #include <corosync/engine/logsys.h>
  61. #include <corosync/engine/coroapi.h>
  62. #include <corosync/cpg.h>
  63. #include <corosync/ipc_cpg.h>
  64. LOGSYS_DECLARE_SUBSYS ("CPG");
  65. #define GROUP_HASH_SIZE 32
  66. enum cpg_message_req_types {
  67. MESSAGE_REQ_EXEC_CPG_PROCJOIN = 0,
  68. MESSAGE_REQ_EXEC_CPG_PROCLEAVE = 1,
  69. MESSAGE_REQ_EXEC_CPG_JOINLIST = 2,
  70. MESSAGE_REQ_EXEC_CPG_MCAST = 3,
  71. MESSAGE_REQ_EXEC_CPG_DOWNLIST = 4
  72. };
  73. /*
  74. * state` exec deliver
  75. * match group name, pid -> if matched deliver for YES:
  76. * XXX indicates impossible state
  77. *
  78. * join leave mcast
  79. * UNJOINED XXX XXX NO
  80. * LEAVE_STARTED XXX YES(unjoined_enter) YES
  81. * JOIN_STARTED YES(join_started_enter) XXX NO
  82. * JOIN_COMPLETED XXX NO YES
  83. *
  84. * join_started_enter
  85. * set JOIN_COMPLETED
  86. * add entry to process_info list
  87. * unjoined_enter
  88. * set UNJOINED
  89. * delete entry from process_info list
  90. *
  91. *
  92. * library accept join error codes
  93. * UNJOINED YES(CPG_OK) set JOIN_STARTED
  94. * LEAVE_STARTED NO(CPG_ERR_BUSY)
  95. * JOIN_STARTED NO(CPG_ERR_EXIST)
  96. * JOIN_COMPlETED NO(CPG_ERR_EXIST)
  97. *
  98. * library accept leave error codes
  99. * UNJOINED NO(CPG_ERR_NOT_EXIST)
  100. * LEAVE_STARTED NO(CPG_ERR_NOT_EXIST)
  101. * JOIN_STARTED NO(CPG_ERR_BUSY)
  102. * JOIN_COMPLETED YES(CPG_OK) set LEAVE_STARTED
  103. *
  104. * library accept mcast
  105. * UNJOINED NO(CPG_ERR_NOT_EXIST)
  106. * LEAVE_STARTED NO(CPG_ERR_NOT_EXIST)
  107. * JOIN_STARTED YES(CPG_OK)
  108. * JOIN_COMPLETED YES(CPG_OK)
  109. */
  110. enum cpd_state {
  111. CPD_STATE_UNJOINED,
  112. CPD_STATE_LEAVE_STARTED,
  113. CPD_STATE_JOIN_STARTED,
  114. CPD_STATE_JOIN_COMPLETED
  115. };
  116. enum cpg_sync_state {
  117. CPGSYNC_DOWNLIST,
  118. CPGSYNC_JOINLIST
  119. };
  120. struct cpg_pd {
  121. void *conn;
  122. mar_cpg_name_t group_name;
  123. uint32_t pid;
  124. enum cpd_state cpd_state;
  125. struct list_head list;
  126. struct list_head iteration_instance_list_head;
  127. };
  128. struct cpg_iteration_instance {
  129. hdb_handle_t handle;
  130. struct list_head list;
  131. struct list_head items_list_head; /* List of process_info */
  132. struct list_head *current_pointer;
  133. };
  134. DECLARE_HDB_DATABASE(cpg_iteration_handle_t_db,NULL);
  135. DECLARE_LIST_INIT(cpg_pd_list_head);
  136. static unsigned int my_member_list[PROCESSOR_COUNT_MAX];
  137. static unsigned int my_member_list_entries;
  138. static unsigned int my_old_member_list[PROCESSOR_COUNT_MAX];
  139. static unsigned int my_old_member_list_entries = 0;
  140. static struct corosync_api_v1 *api = NULL;
  141. static enum cpg_sync_state my_sync_state = CPGSYNC_DOWNLIST;
  142. struct process_info {
  143. unsigned int nodeid;
  144. uint32_t pid;
  145. mar_cpg_name_t group;
  146. struct list_head list; /* on the group_info members list */
  147. };
  148. DECLARE_LIST_INIT(process_info_list_head);
  149. struct join_list_entry {
  150. uint32_t pid;
  151. mar_cpg_name_t group_name;
  152. };
  153. /*
  154. * Service Interfaces required by service_message_handler struct
  155. */
  156. static int cpg_exec_init_fn (struct corosync_api_v1 *);
  157. static int cpg_lib_init_fn (void *conn);
  158. static int cpg_lib_exit_fn (void *conn);
  159. static void message_handler_req_exec_cpg_procjoin (
  160. const void *message,
  161. unsigned int nodeid);
  162. static void message_handler_req_exec_cpg_procleave (
  163. const void *message,
  164. unsigned int nodeid);
  165. static void message_handler_req_exec_cpg_joinlist (
  166. const void *message,
  167. unsigned int nodeid);
  168. static void message_handler_req_exec_cpg_mcast (
  169. const void *message,
  170. unsigned int nodeid);
  171. static void message_handler_req_exec_cpg_downlist (
  172. const void *message,
  173. unsigned int nodeid);
  174. static void exec_cpg_procjoin_endian_convert (void *msg);
  175. static void exec_cpg_joinlist_endian_convert (void *msg);
  176. static void exec_cpg_mcast_endian_convert (void *msg);
  177. static void exec_cpg_downlist_endian_convert (void *msg);
  178. static void message_handler_req_lib_cpg_join (void *conn, const void *message);
  179. static void message_handler_req_lib_cpg_leave (void *conn, const void *message);
  180. static void message_handler_req_lib_cpg_mcast (void *conn, const void *message);
  181. static void message_handler_req_lib_cpg_membership (void *conn,
  182. const void *message);
  183. static void message_handler_req_lib_cpg_local_get (void *conn,
  184. const void *message);
  185. static void message_handler_req_lib_cpg_iteration_initialize (
  186. void *conn,
  187. const void *message);
  188. static void message_handler_req_lib_cpg_iteration_next (
  189. void *conn,
  190. const void *message);
  191. static void message_handler_req_lib_cpg_iteration_finalize (
  192. void *conn,
  193. const void *message);
  194. static int cpg_node_joinleave_send (unsigned int pid, const mar_cpg_name_t *group_name, int fn, int reason);
  195. static int cpg_exec_send_downlist(void);
  196. static int cpg_exec_send_joinlist(void);
  197. static void cpg_sync_init (
  198. const unsigned int *member_list,
  199. size_t member_list_entries,
  200. const struct memb_ring_id *ring_id);
  201. static int cpg_sync_process (void);
  202. static void cpg_sync_activate (void);
  203. static void cpg_sync_abort (void);
  204. /*
  205. * Library Handler Definition
  206. */
  207. static struct corosync_lib_handler cpg_lib_engine[] =
  208. {
  209. { /* 0 */
  210. .lib_handler_fn = message_handler_req_lib_cpg_join,
  211. .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
  212. },
  213. { /* 1 */
  214. .lib_handler_fn = message_handler_req_lib_cpg_leave,
  215. .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
  216. },
  217. { /* 2 */
  218. .lib_handler_fn = message_handler_req_lib_cpg_mcast,
  219. .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
  220. },
  221. { /* 3 */
  222. .lib_handler_fn = message_handler_req_lib_cpg_membership,
  223. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  224. },
  225. { /* 4 */
  226. .lib_handler_fn = message_handler_req_lib_cpg_local_get,
  227. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  228. },
  229. { /* 5 */
  230. .lib_handler_fn = message_handler_req_lib_cpg_iteration_initialize,
  231. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  232. },
  233. { /* 6 */
  234. .lib_handler_fn = message_handler_req_lib_cpg_iteration_next,
  235. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  236. },
  237. { /* 7 */
  238. .lib_handler_fn = message_handler_req_lib_cpg_iteration_finalize,
  239. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  240. },
  241. };
  242. static struct corosync_exec_handler cpg_exec_engine[] =
  243. {
  244. { /* 0 */
  245. .exec_handler_fn = message_handler_req_exec_cpg_procjoin,
  246. .exec_endian_convert_fn = exec_cpg_procjoin_endian_convert
  247. },
  248. { /* 1 */
  249. .exec_handler_fn = message_handler_req_exec_cpg_procleave,
  250. .exec_endian_convert_fn = exec_cpg_procjoin_endian_convert
  251. },
  252. { /* 2 */
  253. .exec_handler_fn = message_handler_req_exec_cpg_joinlist,
  254. .exec_endian_convert_fn = exec_cpg_joinlist_endian_convert
  255. },
  256. { /* 3 */
  257. .exec_handler_fn = message_handler_req_exec_cpg_mcast,
  258. .exec_endian_convert_fn = exec_cpg_mcast_endian_convert
  259. },
  260. { /* 4 */
  261. .exec_handler_fn = message_handler_req_exec_cpg_downlist,
  262. .exec_endian_convert_fn = exec_cpg_downlist_endian_convert
  263. },
  264. };
  265. struct corosync_service_engine cpg_service_engine = {
  266. .name = "corosync cluster closed process group service v1.01",
  267. .id = CPG_SERVICE,
  268. .priority = 1,
  269. .private_data_size = sizeof (struct cpg_pd),
  270. .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED,
  271. .allow_inquorate = CS_LIB_ALLOW_INQUORATE,
  272. .lib_init_fn = cpg_lib_init_fn,
  273. .lib_exit_fn = cpg_lib_exit_fn,
  274. .lib_engine = cpg_lib_engine,
  275. .lib_engine_count = sizeof (cpg_lib_engine) / sizeof (struct corosync_lib_handler),
  276. .exec_init_fn = cpg_exec_init_fn,
  277. .exec_dump_fn = NULL,
  278. .exec_engine = cpg_exec_engine,
  279. .exec_engine_count = sizeof (cpg_exec_engine) / sizeof (struct corosync_exec_handler),
  280. .sync_mode = CS_SYNC_V1,
  281. .sync_init = cpg_sync_init,
  282. .sync_process = cpg_sync_process,
  283. .sync_activate = cpg_sync_activate,
  284. .sync_abort = cpg_sync_abort
  285. };
  286. /*
  287. * Dynamic loader definition
  288. */
  289. static struct corosync_service_engine *cpg_get_service_engine_ver0 (void);
  290. static struct corosync_service_engine_iface_ver0 cpg_service_engine_iface = {
  291. .corosync_get_service_engine_ver0 = cpg_get_service_engine_ver0
  292. };
  293. static struct lcr_iface corosync_cpg_ver0[1] = {
  294. {
  295. .name = "corosync_cpg",
  296. .version = 0,
  297. .versions_replace = 0,
  298. .versions_replace_count = 0,
  299. .dependencies = 0,
  300. .dependency_count = 0,
  301. .constructor = NULL,
  302. .destructor = NULL,
  303. .interfaces = NULL
  304. }
  305. };
  306. static struct lcr_comp cpg_comp_ver0 = {
  307. .iface_count = 1,
  308. .ifaces = corosync_cpg_ver0
  309. };
  310. static struct corosync_service_engine *cpg_get_service_engine_ver0 (void)
  311. {
  312. return (&cpg_service_engine);
  313. }
  314. #ifdef COROSYNC_SOLARIS
  315. void corosync_lcr_component_register (void);
  316. void corosync_lcr_component_register (void) {
  317. #else
  318. __attribute__ ((constructor)) static void corosync_lcr_component_register (void) {
  319. #endif
  320. lcr_interfaces_set (&corosync_cpg_ver0[0], &cpg_service_engine_iface);
  321. lcr_component_register (&cpg_comp_ver0);
  322. }
  323. struct req_exec_cpg_procjoin {
  324. coroipc_request_header_t header __attribute__((aligned(8)));
  325. mar_cpg_name_t group_name __attribute__((aligned(8)));
  326. mar_uint32_t pid __attribute__((aligned(8)));
  327. mar_uint32_t reason __attribute__((aligned(8)));
  328. };
  329. struct req_exec_cpg_mcast {
  330. coroipc_request_header_t header __attribute__((aligned(8)));
  331. mar_cpg_name_t group_name __attribute__((aligned(8)));
  332. mar_uint32_t msglen __attribute__((aligned(8)));
  333. mar_uint32_t pid __attribute__((aligned(8)));
  334. mar_message_source_t source __attribute__((aligned(8)));
  335. mar_uint8_t message[] __attribute__((aligned(8)));
  336. };
  337. struct req_exec_cpg_downlist {
  338. coroipc_request_header_t header __attribute__((aligned(8)));
  339. mar_uint32_t left_nodes __attribute__((aligned(8)));
  340. mar_uint32_t nodeids[PROCESSOR_COUNT_MAX] __attribute__((aligned(8)));
  341. };
  342. static struct req_exec_cpg_downlist g_req_exec_cpg_downlist;
  343. static void cpg_sync_init (
  344. const unsigned int *member_list,
  345. size_t member_list_entries,
  346. const struct memb_ring_id *ring_id)
  347. {
  348. unsigned int lowest_nodeid = 0xffffffff;
  349. int entries;
  350. int i, j;
  351. int found;
  352. my_sync_state = CPGSYNC_DOWNLIST;
  353. memcpy (my_member_list, member_list, member_list_entries *
  354. sizeof (unsigned int));
  355. my_member_list_entries = member_list_entries;
  356. for (i = 0; i < my_member_list_entries; i++) {
  357. if (my_member_list[i] < lowest_nodeid) {
  358. lowest_nodeid = my_member_list[i];
  359. }
  360. }
  361. entries = 0;
  362. if (lowest_nodeid == api->totem_nodeid_get()) {
  363. /*
  364. * Determine list of nodeids for downlist message
  365. */
  366. for (i = 0; i < my_old_member_list_entries; i++) {
  367. found = 0;
  368. for (j = 0; j < my_member_list_entries; j++) {
  369. if (my_old_member_list[i] == my_member_list[j]) {
  370. found = 1;
  371. break;
  372. }
  373. }
  374. if (found == 0) {
  375. g_req_exec_cpg_downlist.nodeids[entries++] =
  376. my_old_member_list[i];
  377. }
  378. }
  379. }
  380. g_req_exec_cpg_downlist.left_nodes = entries;
  381. }
  382. static int cpg_sync_process (void)
  383. {
  384. int res = -1;
  385. if (my_sync_state == CPGSYNC_DOWNLIST) {
  386. res = cpg_exec_send_downlist();
  387. if (res == -1) {
  388. return (-1);
  389. }
  390. my_sync_state = CPGSYNC_JOINLIST;
  391. }
  392. if (my_sync_state == CPGSYNC_JOINLIST) {
  393. res = cpg_exec_send_joinlist();
  394. }
  395. return (res);
  396. }
  397. static void cpg_sync_activate (void)
  398. {
  399. memcpy (my_old_member_list, my_member_list,
  400. my_member_list_entries * sizeof (unsigned int));
  401. my_old_member_list_entries = my_member_list_entries;
  402. }
  403. static void cpg_sync_abort (void)
  404. {
  405. }
  406. static int notify_lib_joinlist(
  407. const mar_cpg_name_t *group_name,
  408. void *conn,
  409. int joined_list_entries,
  410. mar_cpg_address_t *joined_list,
  411. int left_list_entries,
  412. mar_cpg_address_t *left_list,
  413. int id)
  414. {
  415. int size;
  416. char *buf;
  417. struct list_head *iter;
  418. int count;
  419. struct res_lib_cpg_confchg_callback *res;
  420. mar_cpg_address_t *retgi;
  421. count = 0;
  422. for (iter = process_info_list_head.next; iter != &process_info_list_head; iter = iter->next) {
  423. struct process_info *pi = list_entry (iter, struct process_info, list);
  424. if (mar_name_compare (&pi->group, group_name) == 0) {
  425. int i;
  426. int founded = 0;
  427. for (i = 0; i < left_list_entries; i++) {
  428. if (left_list[i].nodeid == pi->nodeid && left_list[i].pid == pi->pid) {
  429. founded++;
  430. }
  431. }
  432. if (!founded)
  433. count++;
  434. }
  435. }
  436. size = sizeof(struct res_lib_cpg_confchg_callback) +
  437. sizeof(mar_cpg_address_t) * (count + left_list_entries + joined_list_entries);
  438. buf = alloca(size);
  439. if (!buf)
  440. return CPG_ERR_LIBRARY;
  441. res = (struct res_lib_cpg_confchg_callback *)buf;
  442. res->joined_list_entries = joined_list_entries;
  443. res->left_list_entries = left_list_entries;
  444. res->member_list_entries = count;
  445. retgi = res->member_list;
  446. res->header.size = size;
  447. res->header.id = id;
  448. res->header.error = CS_OK;
  449. memcpy(&res->group_name, group_name, sizeof(mar_cpg_name_t));
  450. for (iter = process_info_list_head.next; iter != &process_info_list_head; iter = iter->next) {
  451. struct process_info *pi=list_entry (iter, struct process_info, list);
  452. if (mar_name_compare (&pi->group, group_name) == 0) {
  453. int i;
  454. int founded = 0;
  455. for (i = 0;i < left_list_entries; i++) {
  456. if (left_list[i].nodeid == pi->nodeid && left_list[i].pid == pi->pid) {
  457. founded++;
  458. }
  459. }
  460. if (!founded) {
  461. retgi->nodeid = pi->nodeid;
  462. retgi->pid = pi->pid;
  463. retgi++;
  464. }
  465. }
  466. }
  467. if (left_list_entries) {
  468. memcpy (retgi, left_list, left_list_entries * sizeof(mar_cpg_address_t));
  469. retgi += left_list_entries;
  470. }
  471. if (joined_list_entries) {
  472. memcpy (retgi, joined_list, joined_list_entries * sizeof(mar_cpg_address_t));
  473. retgi += joined_list_entries;
  474. }
  475. if (conn) {
  476. api->ipc_dispatch_send (conn, buf, size);
  477. } else {
  478. for (iter = cpg_pd_list_head.next; iter != &cpg_pd_list_head; iter = iter->next) {
  479. struct cpg_pd *cpd = list_entry (iter, struct cpg_pd, list);
  480. if (mar_name_compare (&cpd->group_name, group_name) == 0) {
  481. assert (left_list_entries <= 1);
  482. assert (joined_list_entries <= 1);
  483. if (joined_list_entries) {
  484. if (joined_list[0].pid == cpd->pid &&
  485. joined_list[0].nodeid == api->totem_nodeid_get()) {
  486. cpd->cpd_state = CPD_STATE_JOIN_COMPLETED;
  487. }
  488. }
  489. if (cpd->cpd_state == CPD_STATE_JOIN_COMPLETED ||
  490. cpd->cpd_state == CPD_STATE_LEAVE_STARTED) {
  491. api->ipc_dispatch_send (cpd->conn, buf, size);
  492. }
  493. if (left_list_entries) {
  494. if (left_list[0].pid == cpd->pid &&
  495. left_list[0].nodeid == api->totem_nodeid_get()) {
  496. cpd->pid = 0;
  497. memset (&cpd->group_name, 0, sizeof(cpd->group_name));
  498. cpd->cpd_state = CPD_STATE_UNJOINED;
  499. }
  500. }
  501. }
  502. }
  503. }
  504. return CPG_OK;
  505. }
  506. static int cpg_exec_init_fn (struct corosync_api_v1 *corosync_api)
  507. {
  508. #ifdef COROSYNC_SOLARIS
  509. logsys_subsys_init();
  510. #endif
  511. api = corosync_api;
  512. return (0);
  513. }
  514. static void cpg_iteration_instance_finalize (struct cpg_iteration_instance *cpg_iteration_instance)
  515. {
  516. struct list_head *iter, *iter_next;
  517. struct process_info *pi;
  518. for (iter = cpg_iteration_instance->items_list_head.next;
  519. iter != &cpg_iteration_instance->items_list_head;
  520. iter = iter_next) {
  521. iter_next = iter->next;
  522. pi = list_entry (iter, struct process_info, list);
  523. list_del (&pi->list);
  524. free (pi);
  525. }
  526. list_del (&cpg_iteration_instance->list);
  527. hdb_handle_destroy (&cpg_iteration_handle_t_db, cpg_iteration_instance->handle);
  528. }
  529. static void cpg_pd_finalize (struct cpg_pd *cpd)
  530. {
  531. struct list_head *iter, *iter_next;
  532. struct cpg_iteration_instance *cpii;
  533. for (iter = cpd->iteration_instance_list_head.next;
  534. iter != &cpd->iteration_instance_list_head;
  535. iter = iter_next) {
  536. iter_next = iter->next;
  537. cpii = list_entry (iter, struct cpg_iteration_instance, list);
  538. cpg_iteration_instance_finalize (cpii);
  539. }
  540. list_del (&cpd->list);
  541. }
  542. static int cpg_lib_exit_fn (void *conn)
  543. {
  544. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  545. log_printf(LOGSYS_LEVEL_DEBUG, "exit_fn for conn=%p\n", conn);
  546. if (cpd->group_name.length > 0) {
  547. cpg_node_joinleave_send (cpd->pid, &cpd->group_name,
  548. MESSAGE_REQ_EXEC_CPG_PROCLEAVE, CONFCHG_CPG_REASON_LEAVE);
  549. }
  550. cpg_pd_finalize (cpd);
  551. api->ipc_refcnt_dec (conn);
  552. return (0);
  553. }
  554. static int cpg_node_joinleave_send (unsigned int pid, const mar_cpg_name_t *group_name, int fn, int reason)
  555. {
  556. struct req_exec_cpg_procjoin req_exec_cpg_procjoin;
  557. struct iovec req_exec_cpg_iovec;
  558. int result;
  559. memcpy(&req_exec_cpg_procjoin.group_name, group_name, sizeof(mar_cpg_name_t));
  560. req_exec_cpg_procjoin.pid = pid;
  561. req_exec_cpg_procjoin.reason = reason;
  562. req_exec_cpg_procjoin.header.size = sizeof(req_exec_cpg_procjoin);
  563. req_exec_cpg_procjoin.header.id = SERVICE_ID_MAKE(CPG_SERVICE, fn);
  564. req_exec_cpg_iovec.iov_base = (char *)&req_exec_cpg_procjoin;
  565. req_exec_cpg_iovec.iov_len = sizeof(req_exec_cpg_procjoin);
  566. result = api->totem_mcast (&req_exec_cpg_iovec, 1, TOTEM_AGREED);
  567. return (result);
  568. }
  569. /* Can byteswap join & leave messages */
  570. static void exec_cpg_procjoin_endian_convert (void *msg)
  571. {
  572. struct req_exec_cpg_procjoin *req_exec_cpg_procjoin = msg;
  573. req_exec_cpg_procjoin->pid = swab32(req_exec_cpg_procjoin->pid);
  574. swab_mar_cpg_name_t (&req_exec_cpg_procjoin->group_name);
  575. req_exec_cpg_procjoin->reason = swab32(req_exec_cpg_procjoin->reason);
  576. }
  577. static void exec_cpg_joinlist_endian_convert (void *msg_v)
  578. {
  579. char *msg = msg_v;
  580. coroipc_response_header_t *res = (coroipc_response_header_t *)msg;
  581. struct join_list_entry *jle = (struct join_list_entry *)(msg + sizeof(coroipc_response_header_t));
  582. swab_mar_int32_t (&res->size);
  583. while ((const char*)jle < msg + res->size) {
  584. jle->pid = swab32(jle->pid);
  585. swab_mar_cpg_name_t (&jle->group_name);
  586. jle++;
  587. }
  588. }
  589. static void exec_cpg_downlist_endian_convert (void *msg)
  590. {
  591. struct req_exec_cpg_downlist *req_exec_cpg_downlist = msg;
  592. unsigned int i;
  593. req_exec_cpg_downlist->left_nodes = swab32(req_exec_cpg_downlist->left_nodes);
  594. for (i = 0; i < req_exec_cpg_downlist->left_nodes; i++) {
  595. req_exec_cpg_downlist->nodeids[i] = swab32(req_exec_cpg_downlist->nodeids[i]);
  596. }
  597. }
  598. static void exec_cpg_mcast_endian_convert (void *msg)
  599. {
  600. struct req_exec_cpg_mcast *req_exec_cpg_mcast = msg;
  601. swab_coroipc_request_header_t (&req_exec_cpg_mcast->header);
  602. swab_mar_cpg_name_t (&req_exec_cpg_mcast->group_name);
  603. req_exec_cpg_mcast->pid = swab32(req_exec_cpg_mcast->pid);
  604. req_exec_cpg_mcast->msglen = swab32(req_exec_cpg_mcast->msglen);
  605. swab_mar_message_source_t (&req_exec_cpg_mcast->source);
  606. }
  607. static struct process_info *process_info_find(const mar_cpg_name_t *group_name, uint32_t pid, unsigned int nodeid) {
  608. struct list_head *iter;
  609. for (iter = process_info_list_head.next; iter != &process_info_list_head; ) {
  610. struct process_info *pi = list_entry (iter, struct process_info, list);
  611. iter = iter->next;
  612. if (pi->pid == pid && pi->nodeid == nodeid &&
  613. mar_name_compare (&pi->group, group_name) == 0) {
  614. return pi;
  615. }
  616. }
  617. return NULL;
  618. }
  619. static void do_proc_join(
  620. const mar_cpg_name_t *name,
  621. uint32_t pid,
  622. unsigned int nodeid,
  623. int reason)
  624. {
  625. struct process_info *pi;
  626. struct process_info *pi_entry;
  627. mar_cpg_address_t notify_info;
  628. struct list_head *list;
  629. struct list_head *list_to_add = NULL;
  630. if (process_info_find (name, pid, nodeid) != NULL) {
  631. return ;
  632. }
  633. pi = malloc (sizeof (struct process_info));
  634. if (!pi) {
  635. log_printf(LOGSYS_LEVEL_WARNING, "Unable to allocate process_info struct");
  636. return;
  637. }
  638. pi->nodeid = nodeid;
  639. pi->pid = pid;
  640. memcpy(&pi->group, name, sizeof(*name));
  641. list_init(&pi->list);
  642. /*
  643. * Insert new process in sorted order so synchronization works properly
  644. */
  645. list_to_add = &process_info_list_head;
  646. for (list = process_info_list_head.next; list != &process_info_list_head; list = list->next) {
  647. pi_entry = list_entry(list, struct process_info, list);
  648. if (pi_entry->nodeid > pi->nodeid ||
  649. (pi_entry->nodeid == pi->nodeid && pi_entry->pid > pi->pid)) {
  650. break;
  651. }
  652. list_to_add = list;
  653. }
  654. list_splice (&pi->list, list_to_add);
  655. notify_info.pid = pi->pid;
  656. notify_info.nodeid = nodeid;
  657. notify_info.reason = reason;
  658. notify_lib_joinlist(&pi->group, NULL,
  659. 1, &notify_info,
  660. 0, NULL,
  661. MESSAGE_RES_CPG_CONFCHG_CALLBACK);
  662. }
  663. static void message_handler_req_exec_cpg_downlist (
  664. const void *message,
  665. unsigned int nodeid)
  666. {
  667. const struct req_exec_cpg_downlist *req_exec_cpg_downlist = message;
  668. int i;
  669. mar_cpg_address_t left_list[1];
  670. struct list_head *iter;
  671. /*
  672. FOR OPTIMALIZATION - Make list of lists
  673. */
  674. log_printf (LOGSYS_LEVEL_DEBUG, "downlist left_list: %d\n", req_exec_cpg_downlist->left_nodes);
  675. for (iter = process_info_list_head.next; iter != &process_info_list_head; ) {
  676. struct process_info *pi = list_entry(iter, struct process_info, list);
  677. iter = iter->next;
  678. for (i = 0; i < req_exec_cpg_downlist->left_nodes; i++) {
  679. if (pi->nodeid == req_exec_cpg_downlist->nodeids[i]) {
  680. left_list[0].nodeid = pi->nodeid;
  681. left_list[0].pid = pi->pid;
  682. left_list[0].reason = CONFCHG_CPG_REASON_NODEDOWN;
  683. notify_lib_joinlist(&pi->group, NULL,
  684. 0, NULL,
  685. 1, left_list,
  686. MESSAGE_RES_CPG_CONFCHG_CALLBACK);
  687. list_del (&pi->list);
  688. free (pi);
  689. }
  690. }
  691. }
  692. }
  693. static void message_handler_req_exec_cpg_procjoin (
  694. const void *message,
  695. unsigned int nodeid)
  696. {
  697. const struct req_exec_cpg_procjoin *req_exec_cpg_procjoin = message;
  698. log_printf(LOGSYS_LEVEL_DEBUG, "got procjoin message from cluster node %d\n", nodeid);
  699. do_proc_join (&req_exec_cpg_procjoin->group_name,
  700. req_exec_cpg_procjoin->pid, nodeid,
  701. CONFCHG_CPG_REASON_JOIN);
  702. }
  703. static void message_handler_req_exec_cpg_procleave (
  704. const void *message,
  705. unsigned int nodeid)
  706. {
  707. const struct req_exec_cpg_procjoin *req_exec_cpg_procjoin = message;
  708. struct process_info *pi;
  709. struct list_head *iter;
  710. mar_cpg_address_t notify_info;
  711. log_printf(LOGSYS_LEVEL_DEBUG, "got procleave message from cluster node %d\n", nodeid);
  712. notify_info.pid = req_exec_cpg_procjoin->pid;
  713. notify_info.nodeid = nodeid;
  714. notify_info.reason = req_exec_cpg_procjoin->reason;
  715. notify_lib_joinlist(&req_exec_cpg_procjoin->group_name, NULL,
  716. 0, NULL,
  717. 1, &notify_info,
  718. MESSAGE_RES_CPG_CONFCHG_CALLBACK);
  719. for (iter = process_info_list_head.next; iter != &process_info_list_head; ) {
  720. pi = list_entry(iter, struct process_info, list);
  721. iter = iter->next;
  722. if (pi->pid == req_exec_cpg_procjoin->pid && pi->nodeid == nodeid &&
  723. mar_name_compare (&pi->group, &req_exec_cpg_procjoin->group_name)==0) {
  724. list_del (&pi->list);
  725. free (pi);
  726. }
  727. }
  728. }
  729. /* Got a proclist from another node */
  730. static void message_handler_req_exec_cpg_joinlist (
  731. const void *message_v,
  732. unsigned int nodeid)
  733. {
  734. const char *message = message_v;
  735. const coroipc_response_header_t *res = (const coroipc_response_header_t *)message;
  736. const struct join_list_entry *jle = (const struct join_list_entry *)(message + sizeof(coroipc_response_header_t));
  737. log_printf(LOGSYS_LEVEL_DEBUG, "got joinlist message from node %x\n",
  738. nodeid);
  739. /* Ignore our own messages */
  740. if (nodeid == api->totem_nodeid_get()) {
  741. return;
  742. }
  743. while ((const char*)jle < message + res->size) {
  744. do_proc_join (&jle->group_name, jle->pid, nodeid,
  745. CONFCHG_CPG_REASON_NODEUP);
  746. jle++;
  747. }
  748. }
  749. static void message_handler_req_exec_cpg_mcast (
  750. const void *message,
  751. unsigned int nodeid)
  752. {
  753. const struct req_exec_cpg_mcast *req_exec_cpg_mcast = message;
  754. struct res_lib_cpg_deliver_callback res_lib_cpg_mcast;
  755. int msglen = req_exec_cpg_mcast->msglen;
  756. struct list_head *iter, *pi_iter;
  757. struct cpg_pd *cpd;
  758. struct iovec iovec[2];
  759. int known_node = 0;
  760. res_lib_cpg_mcast.header.id = MESSAGE_RES_CPG_DELIVER_CALLBACK;
  761. res_lib_cpg_mcast.header.size = sizeof(res_lib_cpg_mcast) + msglen;
  762. res_lib_cpg_mcast.msglen = msglen;
  763. res_lib_cpg_mcast.pid = req_exec_cpg_mcast->pid;
  764. res_lib_cpg_mcast.nodeid = nodeid;
  765. memcpy(&res_lib_cpg_mcast.group_name, &req_exec_cpg_mcast->group_name,
  766. sizeof(mar_cpg_name_t));
  767. iovec[0].iov_base = (void *)&res_lib_cpg_mcast;
  768. iovec[0].iov_len = sizeof (res_lib_cpg_mcast);
  769. iovec[1].iov_base = (char*)message+sizeof(*req_exec_cpg_mcast);
  770. iovec[1].iov_len = msglen;
  771. for (iter = cpg_pd_list_head.next; iter != &cpg_pd_list_head; ) {
  772. cpd = list_entry(iter, struct cpg_pd, list);
  773. iter = iter->next;
  774. if ((cpd->cpd_state == CPD_STATE_LEAVE_STARTED || cpd->cpd_state == CPD_STATE_JOIN_COMPLETED)
  775. && (mar_name_compare (&cpd->group_name, &req_exec_cpg_mcast->group_name) == 0)) {
  776. if (!known_node) {
  777. /* Try to find, if we know the node */
  778. for (pi_iter = process_info_list_head.next;
  779. pi_iter != &process_info_list_head; pi_iter = pi_iter->next) {
  780. struct process_info *pi = list_entry (pi_iter, struct process_info, list);
  781. if (pi->nodeid == nodeid &&
  782. mar_name_compare (&pi->group, &req_exec_cpg_mcast->group_name) == 0) {
  783. known_node = 1;
  784. break;
  785. }
  786. }
  787. }
  788. if (!known_node) {
  789. /* Unknown node -> we will not deliver message */
  790. return ;
  791. }
  792. api->ipc_dispatch_iov_send (cpd->conn, iovec, 2);
  793. }
  794. }
  795. }
  796. static int cpg_exec_send_downlist(void)
  797. {
  798. struct iovec iov;
  799. g_req_exec_cpg_downlist.header.id = SERVICE_ID_MAKE(CPG_SERVICE, MESSAGE_REQ_EXEC_CPG_DOWNLIST);
  800. g_req_exec_cpg_downlist.header.size = sizeof(struct req_exec_cpg_downlist);
  801. iov.iov_base = (void *)&g_req_exec_cpg_downlist;
  802. iov.iov_len = g_req_exec_cpg_downlist.header.size;
  803. return (api->totem_mcast (&iov, 1, TOTEM_AGREED));
  804. }
  805. static int cpg_exec_send_joinlist(void)
  806. {
  807. int count = 0;
  808. struct list_head *iter;
  809. coroipc_response_header_t *res;
  810. char *buf;
  811. struct join_list_entry *jle;
  812. struct iovec req_exec_cpg_iovec;
  813. for (iter = process_info_list_head.next; iter != &process_info_list_head; iter = iter->next) {
  814. struct process_info *pi = list_entry (iter, struct process_info, list);
  815. if (pi->nodeid == api->totem_nodeid_get ()) {
  816. count++;
  817. }
  818. }
  819. /* Nothing to send */
  820. if (!count)
  821. return 0;
  822. buf = alloca(sizeof(coroipc_response_header_t) + sizeof(struct join_list_entry) * count);
  823. if (!buf) {
  824. log_printf(LOGSYS_LEVEL_WARNING, "Unable to allocate joinlist buffer");
  825. return -1;
  826. }
  827. jle = (struct join_list_entry *)(buf + sizeof(coroipc_response_header_t));
  828. res = (coroipc_response_header_t *)buf;
  829. for (iter = process_info_list_head.next; iter != &process_info_list_head; iter = iter->next) {
  830. struct process_info *pi = list_entry (iter, struct process_info, list);
  831. if (pi->nodeid == api->totem_nodeid_get ()) {
  832. memcpy (&jle->group_name, &pi->group, sizeof (mar_cpg_name_t));
  833. jle->pid = pi->pid;
  834. jle++;
  835. }
  836. }
  837. res->id = SERVICE_ID_MAKE(CPG_SERVICE, MESSAGE_REQ_EXEC_CPG_JOINLIST);
  838. res->size = sizeof(coroipc_response_header_t)+sizeof(struct join_list_entry) * count;
  839. req_exec_cpg_iovec.iov_base = buf;
  840. req_exec_cpg_iovec.iov_len = res->size;
  841. return (api->totem_mcast (&req_exec_cpg_iovec, 1, TOTEM_AGREED));
  842. }
  843. static int cpg_lib_init_fn (void *conn)
  844. {
  845. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  846. memset (cpd, 0, sizeof(struct cpg_pd));
  847. cpd->conn = conn;
  848. list_add (&cpd->list, &cpg_pd_list_head);
  849. list_init (&cpd->iteration_instance_list_head);
  850. api->ipc_refcnt_inc (conn);
  851. log_printf(LOGSYS_LEVEL_DEBUG, "lib_init_fn: conn=%p, cpd=%p\n", conn, cpd);
  852. return (0);
  853. }
  854. /* Join message from the library */
  855. static void message_handler_req_lib_cpg_join (void *conn, const void *message)
  856. {
  857. const struct req_lib_cpg_join *req_lib_cpg_join = message;
  858. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  859. struct res_lib_cpg_join res_lib_cpg_join;
  860. cs_error_t error = CPG_OK;
  861. struct list_head *iter;
  862. /* Test, if we don't have same pid and group name joined */
  863. for (iter = cpg_pd_list_head.next; iter != &cpg_pd_list_head; iter = iter->next) {
  864. struct cpg_pd *cpd_item = list_entry (iter, struct cpg_pd, list);
  865. if (cpd_item->pid == req_lib_cpg_join->pid &&
  866. mar_name_compare(&req_lib_cpg_join->group_name, &cpd_item->group_name) == 0) {
  867. /* We have same pid and group name joined -> return error */
  868. error = CPG_ERR_EXIST;
  869. goto response_send;
  870. }
  871. }
  872. switch (cpd->cpd_state) {
  873. case CPD_STATE_UNJOINED:
  874. error = CPG_OK;
  875. cpd->cpd_state = CPD_STATE_JOIN_STARTED;
  876. cpd->pid = req_lib_cpg_join->pid;
  877. memcpy (&cpd->group_name, &req_lib_cpg_join->group_name,
  878. sizeof (cpd->group_name));
  879. cpg_node_joinleave_send (req_lib_cpg_join->pid,
  880. &req_lib_cpg_join->group_name,
  881. MESSAGE_REQ_EXEC_CPG_PROCJOIN, CONFCHG_CPG_REASON_JOIN);
  882. break;
  883. case CPD_STATE_LEAVE_STARTED:
  884. error = CPG_ERR_BUSY;
  885. break;
  886. case CPD_STATE_JOIN_STARTED:
  887. error = CPG_ERR_EXIST;
  888. break;
  889. case CPD_STATE_JOIN_COMPLETED:
  890. error = CPG_ERR_EXIST;
  891. break;
  892. }
  893. response_send:
  894. res_lib_cpg_join.header.size = sizeof(res_lib_cpg_join);
  895. res_lib_cpg_join.header.id = MESSAGE_RES_CPG_JOIN;
  896. res_lib_cpg_join.header.error = error;
  897. api->ipc_response_send (conn, &res_lib_cpg_join, sizeof(res_lib_cpg_join));
  898. }
  899. /* Leave message from the library */
  900. static void message_handler_req_lib_cpg_leave (void *conn, const void *message)
  901. {
  902. struct res_lib_cpg_leave res_lib_cpg_leave;
  903. cs_error_t error = CPG_OK;
  904. struct req_lib_cpg_leave *req_lib_cpg_leave = (struct req_lib_cpg_leave *)message;
  905. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  906. log_printf(LOGSYS_LEVEL_DEBUG, "got leave request on %p\n", conn);
  907. switch (cpd->cpd_state) {
  908. case CPD_STATE_UNJOINED:
  909. error = CPG_ERR_NOT_EXIST;
  910. break;
  911. case CPD_STATE_LEAVE_STARTED:
  912. error = CPG_ERR_NOT_EXIST;
  913. break;
  914. case CPD_STATE_JOIN_STARTED:
  915. error = CPG_ERR_BUSY;
  916. break;
  917. case CPD_STATE_JOIN_COMPLETED:
  918. error = CPG_OK;
  919. cpd->cpd_state = CPD_STATE_LEAVE_STARTED;
  920. cpg_node_joinleave_send (req_lib_cpg_leave->pid,
  921. &req_lib_cpg_leave->group_name,
  922. MESSAGE_REQ_EXEC_CPG_PROCLEAVE,
  923. CONFCHG_CPG_REASON_LEAVE);
  924. break;
  925. }
  926. /* send return */
  927. res_lib_cpg_leave.header.size = sizeof(res_lib_cpg_leave);
  928. res_lib_cpg_leave.header.id = MESSAGE_RES_CPG_LEAVE;
  929. res_lib_cpg_leave.header.error = error;
  930. api->ipc_response_send(conn, &res_lib_cpg_leave, sizeof(res_lib_cpg_leave));
  931. }
  932. /* Mcast message from the library */
  933. static void message_handler_req_lib_cpg_mcast (void *conn, const void *message)
  934. {
  935. const struct req_lib_cpg_mcast *req_lib_cpg_mcast = message;
  936. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  937. mar_cpg_name_t group_name = cpd->group_name;
  938. struct iovec req_exec_cpg_iovec[2];
  939. struct req_exec_cpg_mcast req_exec_cpg_mcast;
  940. struct res_lib_cpg_mcast res_lib_cpg_mcast;
  941. int msglen = req_lib_cpg_mcast->msglen;
  942. int result;
  943. cs_error_t error = CPG_ERR_NOT_EXIST;
  944. log_printf(LOGSYS_LEVEL_DEBUG, "got mcast request on %p\n", conn);
  945. switch (cpd->cpd_state) {
  946. case CPD_STATE_UNJOINED:
  947. error = CPG_ERR_NOT_EXIST;
  948. break;
  949. case CPD_STATE_LEAVE_STARTED:
  950. error = CPG_ERR_NOT_EXIST;
  951. break;
  952. case CPD_STATE_JOIN_STARTED:
  953. error = CPG_OK;
  954. break;
  955. case CPD_STATE_JOIN_COMPLETED:
  956. error = CPG_OK;
  957. break;
  958. }
  959. if (error == CPG_OK) {
  960. req_exec_cpg_mcast.header.size = sizeof(req_exec_cpg_mcast) + msglen;
  961. req_exec_cpg_mcast.header.id = SERVICE_ID_MAKE(CPG_SERVICE,
  962. MESSAGE_REQ_EXEC_CPG_MCAST);
  963. req_exec_cpg_mcast.pid = cpd->pid;
  964. req_exec_cpg_mcast.msglen = msglen;
  965. api->ipc_source_set (&req_exec_cpg_mcast.source, conn);
  966. memcpy(&req_exec_cpg_mcast.group_name, &group_name,
  967. sizeof(mar_cpg_name_t));
  968. req_exec_cpg_iovec[0].iov_base = (char *)&req_exec_cpg_mcast;
  969. req_exec_cpg_iovec[0].iov_len = sizeof(req_exec_cpg_mcast);
  970. req_exec_cpg_iovec[1].iov_base = (char *)&req_lib_cpg_mcast->message;
  971. req_exec_cpg_iovec[1].iov_len = msglen;
  972. result = api->totem_mcast (req_exec_cpg_iovec, 2, TOTEM_AGREED);
  973. assert(result == 0);
  974. }
  975. res_lib_cpg_mcast.header.size = sizeof(res_lib_cpg_mcast);
  976. res_lib_cpg_mcast.header.id = MESSAGE_RES_CPG_MCAST;
  977. res_lib_cpg_mcast.header.error = error;
  978. api->ipc_response_send (conn, &res_lib_cpg_mcast,
  979. sizeof (res_lib_cpg_mcast));
  980. }
  981. static void message_handler_req_lib_cpg_membership (void *conn,
  982. const void *message)
  983. {
  984. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  985. cs_error_t error = CPG_ERR_NOT_EXIST;
  986. coroipc_response_header_t res;
  987. switch (cpd->cpd_state) {
  988. case CPD_STATE_UNJOINED:
  989. error = CPG_ERR_NOT_EXIST;
  990. break;
  991. case CPD_STATE_LEAVE_STARTED:
  992. error = CPG_ERR_NOT_EXIST;
  993. break;
  994. case CPD_STATE_JOIN_STARTED:
  995. error = CPG_ERR_BUSY;
  996. break;
  997. case CPD_STATE_JOIN_COMPLETED:
  998. error = CPG_OK;
  999. break;
  1000. }
  1001. res.size = sizeof (res);
  1002. res.id = MESSAGE_RES_CPG_MEMBERSHIP;
  1003. res.error = error;
  1004. api->ipc_response_send (conn, &res, sizeof(res));
  1005. return;
  1006. if (error == CPG_OK) {
  1007. notify_lib_joinlist (&cpd->group_name, conn, 0, NULL, 0, NULL,
  1008. MESSAGE_RES_CPG_MEMBERSHIP);
  1009. }
  1010. }
  1011. static void message_handler_req_lib_cpg_local_get (void *conn,
  1012. const void *message)
  1013. {
  1014. struct res_lib_cpg_local_get res_lib_cpg_local_get;
  1015. res_lib_cpg_local_get.header.size = sizeof (res_lib_cpg_local_get);
  1016. res_lib_cpg_local_get.header.id = MESSAGE_RES_CPG_LOCAL_GET;
  1017. res_lib_cpg_local_get.header.error = CPG_OK;
  1018. res_lib_cpg_local_get.local_nodeid = api->totem_nodeid_get ();
  1019. api->ipc_response_send (conn, &res_lib_cpg_local_get,
  1020. sizeof (res_lib_cpg_local_get));
  1021. }
  1022. static void message_handler_req_lib_cpg_iteration_initialize (
  1023. void *conn,
  1024. const void *message)
  1025. {
  1026. const struct req_lib_cpg_iterationinitialize *req_lib_cpg_iterationinitialize = message;
  1027. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1028. hdb_handle_t cpg_iteration_handle;
  1029. struct res_lib_cpg_iterationinitialize res_lib_cpg_iterationinitialize;
  1030. struct list_head *iter, *iter2;
  1031. struct cpg_iteration_instance *cpg_iteration_instance;
  1032. cs_error_t error = CS_OK;
  1033. int res;
  1034. log_printf (LOGSYS_LEVEL_DEBUG, "cpg iteration initialize\n");
  1035. /* Because between calling this function and *next can be some operations which will
  1036. * change list, we must do full copy.
  1037. */
  1038. /*
  1039. * Create new iteration instance
  1040. */
  1041. res = hdb_handle_create (&cpg_iteration_handle_t_db, sizeof (struct cpg_iteration_instance),
  1042. &cpg_iteration_handle);
  1043. if (res != 0) {
  1044. error = CS_ERR_NO_MEMORY;
  1045. goto response_send;
  1046. }
  1047. res = hdb_handle_get (&cpg_iteration_handle_t_db, cpg_iteration_handle, (void *)&cpg_iteration_instance);
  1048. if (res != 0) {
  1049. error = CS_ERR_BAD_HANDLE;
  1050. goto error_destroy;
  1051. }
  1052. list_init (&cpg_iteration_instance->items_list_head);
  1053. cpg_iteration_instance->handle = cpg_iteration_handle;
  1054. /*
  1055. * Create copy of process_info list "grouped by" group name
  1056. */
  1057. for (iter = process_info_list_head.next; iter != &process_info_list_head; iter = iter->next) {
  1058. struct process_info *pi = list_entry (iter, struct process_info, list);
  1059. struct process_info *new_pi;
  1060. if (req_lib_cpg_iterationinitialize->iteration_type == CPG_ITERATION_NAME_ONLY) {
  1061. /*
  1062. * Try to find processed group name in our list new list
  1063. */
  1064. int found = 0;
  1065. for (iter2 = cpg_iteration_instance->items_list_head.next;
  1066. iter2 != &cpg_iteration_instance->items_list_head;
  1067. iter2 = iter2->next) {
  1068. struct process_info *pi2 = list_entry (iter2, struct process_info, list);
  1069. if (mar_name_compare (&pi2->group, &pi->group) == 0) {
  1070. found = 1;
  1071. break;
  1072. }
  1073. }
  1074. if (found) {
  1075. /*
  1076. * We have this name in list -> don't add
  1077. */
  1078. continue ;
  1079. }
  1080. } else if (req_lib_cpg_iterationinitialize->iteration_type == CPG_ITERATION_ONE_GROUP) {
  1081. /*
  1082. * Test pi group name with request
  1083. */
  1084. if (mar_name_compare (&pi->group, &req_lib_cpg_iterationinitialize->group_name) != 0)
  1085. /*
  1086. * Not same -> don't add
  1087. */
  1088. continue ;
  1089. }
  1090. new_pi = malloc (sizeof (struct process_info));
  1091. if (!new_pi) {
  1092. log_printf(LOGSYS_LEVEL_WARNING, "Unable to allocate process_info struct");
  1093. error = CS_ERR_NO_MEMORY;
  1094. goto error_put_destroy;
  1095. }
  1096. memcpy (new_pi, pi, sizeof (struct process_info));
  1097. list_init (&new_pi->list);
  1098. if (req_lib_cpg_iterationinitialize->iteration_type == CPG_ITERATION_NAME_ONLY) {
  1099. /*
  1100. * pid and nodeid -> undefined
  1101. */
  1102. new_pi->pid = new_pi->nodeid = 0;
  1103. }
  1104. /*
  1105. * We will return list "grouped" by "group name", so try to find right place to add
  1106. */
  1107. for (iter2 = cpg_iteration_instance->items_list_head.next;
  1108. iter2 != &cpg_iteration_instance->items_list_head;
  1109. iter2 = iter2->next) {
  1110. struct process_info *pi2 = list_entry (iter2, struct process_info, list);
  1111. if (mar_name_compare (&pi2->group, &pi->group) == 0) {
  1112. break;
  1113. }
  1114. }
  1115. list_add (&new_pi->list, iter2);
  1116. }
  1117. /*
  1118. * Now we have a full "grouped by" copy of process_info list
  1119. */
  1120. /*
  1121. * Add instance to current cpd list
  1122. */
  1123. list_init (&cpg_iteration_instance->list);
  1124. list_add (&cpg_iteration_instance->list, &cpd->iteration_instance_list_head);
  1125. cpg_iteration_instance->current_pointer = &cpg_iteration_instance->items_list_head;
  1126. error_put_destroy:
  1127. hdb_handle_put (&cpg_iteration_handle_t_db, cpg_iteration_handle);
  1128. error_destroy:
  1129. if (error != CS_OK) {
  1130. hdb_handle_destroy (&cpg_iteration_handle_t_db, cpg_iteration_handle);
  1131. }
  1132. response_send:
  1133. res_lib_cpg_iterationinitialize.header.size = sizeof (res_lib_cpg_iterationinitialize);
  1134. res_lib_cpg_iterationinitialize.header.id = MESSAGE_RES_CPG_ITERATIONINITIALIZE;
  1135. res_lib_cpg_iterationinitialize.header.error = error;
  1136. res_lib_cpg_iterationinitialize.iteration_handle = cpg_iteration_handle;
  1137. api->ipc_response_send (conn, &res_lib_cpg_iterationinitialize,
  1138. sizeof (res_lib_cpg_iterationinitialize));
  1139. }
  1140. static void message_handler_req_lib_cpg_iteration_next (
  1141. void *conn,
  1142. const void *message)
  1143. {
  1144. const struct req_lib_cpg_iterationnext *req_lib_cpg_iterationnext = message;
  1145. struct res_lib_cpg_iterationnext res_lib_cpg_iterationnext;
  1146. struct cpg_iteration_instance *cpg_iteration_instance;
  1147. cs_error_t error = CS_OK;
  1148. int res;
  1149. struct process_info *pi;
  1150. log_printf (LOGSYS_LEVEL_DEBUG, "cpg iteration next\n");
  1151. res = hdb_handle_get (&cpg_iteration_handle_t_db,
  1152. req_lib_cpg_iterationnext->iteration_handle,
  1153. (void *)&cpg_iteration_instance);
  1154. if (res != 0) {
  1155. error = CS_ERR_LIBRARY;
  1156. goto error_exit;
  1157. }
  1158. assert (cpg_iteration_instance);
  1159. cpg_iteration_instance->current_pointer = cpg_iteration_instance->current_pointer->next;
  1160. if (cpg_iteration_instance->current_pointer == &cpg_iteration_instance->items_list_head) {
  1161. error = CS_ERR_NO_SECTIONS;
  1162. goto error_put;
  1163. }
  1164. pi = list_entry (cpg_iteration_instance->current_pointer, struct process_info, list);
  1165. /*
  1166. * Copy iteration data
  1167. */
  1168. res_lib_cpg_iterationnext.description.nodeid = pi->nodeid;
  1169. res_lib_cpg_iterationnext.description.pid = pi->pid;
  1170. memcpy (&res_lib_cpg_iterationnext.description.group,
  1171. &pi->group,
  1172. sizeof (mar_cpg_name_t));
  1173. error_put:
  1174. hdb_handle_put (&cpg_iteration_handle_t_db, req_lib_cpg_iterationnext->iteration_handle);
  1175. error_exit:
  1176. res_lib_cpg_iterationnext.header.size = sizeof (res_lib_cpg_iterationnext);
  1177. res_lib_cpg_iterationnext.header.id = MESSAGE_RES_CPG_ITERATIONNEXT;
  1178. res_lib_cpg_iterationnext.header.error = error;
  1179. api->ipc_response_send (conn, &res_lib_cpg_iterationnext,
  1180. sizeof (res_lib_cpg_iterationnext));
  1181. }
  1182. static void message_handler_req_lib_cpg_iteration_finalize (
  1183. void *conn,
  1184. const void *message)
  1185. {
  1186. const struct req_lib_cpg_iterationfinalize *req_lib_cpg_iterationfinalize = message;
  1187. struct res_lib_cpg_iterationfinalize res_lib_cpg_iterationfinalize;
  1188. struct cpg_iteration_instance *cpg_iteration_instance;
  1189. cs_error_t error = CS_OK;
  1190. int res;
  1191. log_printf (LOGSYS_LEVEL_DEBUG, "cpg iteration finalize\n");
  1192. res = hdb_handle_get (&cpg_iteration_handle_t_db,
  1193. req_lib_cpg_iterationfinalize->iteration_handle,
  1194. (void *)&cpg_iteration_instance);
  1195. if (res != 0) {
  1196. error = CS_ERR_LIBRARY;
  1197. goto error_exit;
  1198. }
  1199. assert (cpg_iteration_instance);
  1200. cpg_iteration_instance_finalize (cpg_iteration_instance);
  1201. hdb_handle_put (&cpg_iteration_handle_t_db, cpg_iteration_instance->handle);
  1202. error_exit:
  1203. res_lib_cpg_iterationfinalize.header.size = sizeof (res_lib_cpg_iterationfinalize);
  1204. res_lib_cpg_iterationfinalize.header.id = MESSAGE_RES_CPG_ITERATIONFINALIZE;
  1205. res_lib_cpg_iterationfinalize.header.error = error;
  1206. api->ipc_response_send (conn, &res_lib_cpg_iterationfinalize,
  1207. sizeof (res_lib_cpg_iterationfinalize));
  1208. }