cpg.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585
  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. unsigned int flags;
  126. int initial_totem_conf_sent;
  127. struct list_head list;
  128. struct list_head iteration_instance_list_head;
  129. };
  130. struct cpg_iteration_instance {
  131. hdb_handle_t handle;
  132. struct list_head list;
  133. struct list_head items_list_head; /* List of process_info */
  134. struct list_head *current_pointer;
  135. };
  136. DECLARE_HDB_DATABASE(cpg_iteration_handle_t_db,NULL);
  137. DECLARE_LIST_INIT(cpg_pd_list_head);
  138. static unsigned int my_member_list[PROCESSOR_COUNT_MAX];
  139. static unsigned int my_member_list_entries;
  140. static unsigned int my_old_member_list[PROCESSOR_COUNT_MAX];
  141. static unsigned int my_old_member_list_entries = 0;
  142. static struct corosync_api_v1 *api = NULL;
  143. static enum cpg_sync_state my_sync_state = CPGSYNC_DOWNLIST;
  144. static mar_cpg_ring_id_t last_sync_ring_id;
  145. struct process_info {
  146. unsigned int nodeid;
  147. uint32_t pid;
  148. mar_cpg_name_t group;
  149. struct list_head list; /* on the group_info members list */
  150. };
  151. DECLARE_LIST_INIT(process_info_list_head);
  152. struct join_list_entry {
  153. uint32_t pid;
  154. mar_cpg_name_t group_name;
  155. };
  156. /*
  157. * Service Interfaces required by service_message_handler struct
  158. */
  159. static int cpg_exec_init_fn (struct corosync_api_v1 *);
  160. static int cpg_lib_init_fn (void *conn);
  161. static int cpg_lib_exit_fn (void *conn);
  162. static void message_handler_req_exec_cpg_procjoin (
  163. const void *message,
  164. unsigned int nodeid);
  165. static void message_handler_req_exec_cpg_procleave (
  166. const void *message,
  167. unsigned int nodeid);
  168. static void message_handler_req_exec_cpg_joinlist (
  169. const void *message,
  170. unsigned int nodeid);
  171. static void message_handler_req_exec_cpg_mcast (
  172. const void *message,
  173. unsigned int nodeid);
  174. static void message_handler_req_exec_cpg_downlist (
  175. const void *message,
  176. unsigned int nodeid);
  177. static void exec_cpg_procjoin_endian_convert (void *msg);
  178. static void exec_cpg_joinlist_endian_convert (void *msg);
  179. static void exec_cpg_mcast_endian_convert (void *msg);
  180. static void exec_cpg_downlist_endian_convert (void *msg);
  181. static void message_handler_req_lib_cpg_join (void *conn, const void *message);
  182. static void message_handler_req_lib_cpg_leave (void *conn, const void *message);
  183. static void message_handler_req_lib_cpg_finalize (void *conn, const void *message);
  184. static void message_handler_req_lib_cpg_mcast (void *conn, const void *message);
  185. static void message_handler_req_lib_cpg_membership (void *conn,
  186. const void *message);
  187. static void message_handler_req_lib_cpg_local_get (void *conn,
  188. const void *message);
  189. static void message_handler_req_lib_cpg_iteration_initialize (
  190. void *conn,
  191. const void *message);
  192. static void message_handler_req_lib_cpg_iteration_next (
  193. void *conn,
  194. const void *message);
  195. static void message_handler_req_lib_cpg_iteration_finalize (
  196. void *conn,
  197. const void *message);
  198. static int cpg_node_joinleave_send (unsigned int pid, const mar_cpg_name_t *group_name, int fn, int reason);
  199. static int cpg_exec_send_downlist(void);
  200. static int cpg_exec_send_joinlist(void);
  201. static void cpg_sync_init_v2 (
  202. const unsigned int *trans_list,
  203. size_t trans_list_entries,
  204. const unsigned int *member_list,
  205. size_t member_list_entries,
  206. const struct memb_ring_id *ring_id);
  207. static int cpg_sync_process (void);
  208. static void cpg_sync_activate (void);
  209. static void cpg_sync_abort (void);
  210. static int notify_lib_totem_membership (
  211. void *conn,
  212. int member_list_entries,
  213. const unsigned int *member_list);
  214. /*
  215. * Library Handler Definition
  216. */
  217. static struct corosync_lib_handler cpg_lib_engine[] =
  218. {
  219. { /* 0 */
  220. .lib_handler_fn = message_handler_req_lib_cpg_join,
  221. .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
  222. },
  223. { /* 1 */
  224. .lib_handler_fn = message_handler_req_lib_cpg_leave,
  225. .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
  226. },
  227. { /* 2 */
  228. .lib_handler_fn = message_handler_req_lib_cpg_mcast,
  229. .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
  230. },
  231. { /* 3 */
  232. .lib_handler_fn = message_handler_req_lib_cpg_membership,
  233. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  234. },
  235. { /* 4 */
  236. .lib_handler_fn = message_handler_req_lib_cpg_local_get,
  237. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  238. },
  239. { /* 5 */
  240. .lib_handler_fn = message_handler_req_lib_cpg_iteration_initialize,
  241. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  242. },
  243. { /* 6 */
  244. .lib_handler_fn = message_handler_req_lib_cpg_iteration_next,
  245. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  246. },
  247. { /* 7 */
  248. .lib_handler_fn = message_handler_req_lib_cpg_iteration_finalize,
  249. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  250. },
  251. { /* 8 */
  252. .lib_handler_fn = message_handler_req_lib_cpg_finalize,
  253. .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
  254. },
  255. };
  256. static struct corosync_exec_handler cpg_exec_engine[] =
  257. {
  258. { /* 0 */
  259. .exec_handler_fn = message_handler_req_exec_cpg_procjoin,
  260. .exec_endian_convert_fn = exec_cpg_procjoin_endian_convert
  261. },
  262. { /* 1 */
  263. .exec_handler_fn = message_handler_req_exec_cpg_procleave,
  264. .exec_endian_convert_fn = exec_cpg_procjoin_endian_convert
  265. },
  266. { /* 2 */
  267. .exec_handler_fn = message_handler_req_exec_cpg_joinlist,
  268. .exec_endian_convert_fn = exec_cpg_joinlist_endian_convert
  269. },
  270. { /* 3 */
  271. .exec_handler_fn = message_handler_req_exec_cpg_mcast,
  272. .exec_endian_convert_fn = exec_cpg_mcast_endian_convert
  273. },
  274. { /* 4 */
  275. .exec_handler_fn = message_handler_req_exec_cpg_downlist,
  276. .exec_endian_convert_fn = exec_cpg_downlist_endian_convert
  277. },
  278. };
  279. struct corosync_service_engine cpg_service_engine = {
  280. .name = "corosync cluster closed process group service v1.01",
  281. .id = CPG_SERVICE,
  282. .priority = 1,
  283. .private_data_size = sizeof (struct cpg_pd),
  284. .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED,
  285. .allow_inquorate = CS_LIB_ALLOW_INQUORATE,
  286. .lib_init_fn = cpg_lib_init_fn,
  287. .lib_exit_fn = cpg_lib_exit_fn,
  288. .lib_engine = cpg_lib_engine,
  289. .lib_engine_count = sizeof (cpg_lib_engine) / sizeof (struct corosync_lib_handler),
  290. .exec_init_fn = cpg_exec_init_fn,
  291. .exec_dump_fn = NULL,
  292. .exec_engine = cpg_exec_engine,
  293. .exec_engine_count = sizeof (cpg_exec_engine) / sizeof (struct corosync_exec_handler),
  294. .sync_mode = CS_SYNC_V1_APIV2,
  295. .sync_init = (sync_init_v1_fn_t)cpg_sync_init_v2,
  296. .sync_process = cpg_sync_process,
  297. .sync_activate = cpg_sync_activate,
  298. .sync_abort = cpg_sync_abort
  299. };
  300. /*
  301. * Dynamic loader definition
  302. */
  303. static struct corosync_service_engine *cpg_get_service_engine_ver0 (void);
  304. static struct corosync_service_engine_iface_ver0 cpg_service_engine_iface = {
  305. .corosync_get_service_engine_ver0 = cpg_get_service_engine_ver0
  306. };
  307. static struct lcr_iface corosync_cpg_ver0[1] = {
  308. {
  309. .name = "corosync_cpg",
  310. .version = 0,
  311. .versions_replace = 0,
  312. .versions_replace_count = 0,
  313. .dependencies = 0,
  314. .dependency_count = 0,
  315. .constructor = NULL,
  316. .destructor = NULL,
  317. .interfaces = NULL
  318. }
  319. };
  320. static struct lcr_comp cpg_comp_ver0 = {
  321. .iface_count = 1,
  322. .ifaces = corosync_cpg_ver0
  323. };
  324. static struct corosync_service_engine *cpg_get_service_engine_ver0 (void)
  325. {
  326. return (&cpg_service_engine);
  327. }
  328. #ifdef COROSYNC_SOLARIS
  329. void corosync_lcr_component_register (void);
  330. void corosync_lcr_component_register (void) {
  331. #else
  332. __attribute__ ((constructor)) static void corosync_lcr_component_register (void) {
  333. #endif
  334. lcr_interfaces_set (&corosync_cpg_ver0[0], &cpg_service_engine_iface);
  335. lcr_component_register (&cpg_comp_ver0);
  336. }
  337. struct req_exec_cpg_procjoin {
  338. coroipc_request_header_t header __attribute__((aligned(8)));
  339. mar_cpg_name_t group_name __attribute__((aligned(8)));
  340. mar_uint32_t pid __attribute__((aligned(8)));
  341. mar_uint32_t reason __attribute__((aligned(8)));
  342. };
  343. struct req_exec_cpg_mcast {
  344. coroipc_request_header_t header __attribute__((aligned(8)));
  345. mar_cpg_name_t group_name __attribute__((aligned(8)));
  346. mar_uint32_t msglen __attribute__((aligned(8)));
  347. mar_uint32_t pid __attribute__((aligned(8)));
  348. mar_message_source_t source __attribute__((aligned(8)));
  349. mar_uint8_t message[] __attribute__((aligned(8)));
  350. };
  351. struct req_exec_cpg_downlist {
  352. coroipc_request_header_t header __attribute__((aligned(8)));
  353. mar_uint32_t left_nodes __attribute__((aligned(8)));
  354. mar_uint32_t nodeids[PROCESSOR_COUNT_MAX] __attribute__((aligned(8)));
  355. };
  356. static struct req_exec_cpg_downlist g_req_exec_cpg_downlist;
  357. static void cpg_sync_init_v2 (
  358. const unsigned int *trans_list,
  359. size_t trans_list_entries,
  360. const unsigned int *member_list,
  361. size_t member_list_entries,
  362. const struct memb_ring_id *ring_id)
  363. {
  364. unsigned int lowest_nodeid = 0xffffffff;
  365. int entries;
  366. int i, j;
  367. int found;
  368. my_sync_state = CPGSYNC_DOWNLIST;
  369. memcpy (my_member_list, member_list, member_list_entries *
  370. sizeof (unsigned int));
  371. my_member_list_entries = member_list_entries;
  372. last_sync_ring_id.nodeid = ring_id->rep.nodeid;
  373. last_sync_ring_id.seq = ring_id->seq;
  374. for (i = 0; i < my_member_list_entries; i++) {
  375. if (my_member_list[i] < lowest_nodeid) {
  376. lowest_nodeid = my_member_list[i];
  377. }
  378. }
  379. entries = 0;
  380. if (lowest_nodeid == api->totem_nodeid_get()) {
  381. /*
  382. * Determine list of nodeids for downlist message
  383. */
  384. for (i = 0; i < my_old_member_list_entries; i++) {
  385. found = 0;
  386. for (j = 0; j < trans_list_entries; j++) {
  387. if (my_old_member_list[i] == trans_list[j]) {
  388. found = 1;
  389. break;
  390. }
  391. }
  392. if (found == 0) {
  393. g_req_exec_cpg_downlist.nodeids[entries++] =
  394. my_old_member_list[i];
  395. }
  396. }
  397. }
  398. g_req_exec_cpg_downlist.left_nodes = entries;
  399. }
  400. static int cpg_sync_process (void)
  401. {
  402. int res = -1;
  403. if (my_sync_state == CPGSYNC_DOWNLIST) {
  404. res = cpg_exec_send_downlist();
  405. if (res == -1) {
  406. return (-1);
  407. }
  408. my_sync_state = CPGSYNC_JOINLIST;
  409. }
  410. if (my_sync_state == CPGSYNC_JOINLIST) {
  411. res = cpg_exec_send_joinlist();
  412. }
  413. return (res);
  414. }
  415. static void cpg_sync_activate (void)
  416. {
  417. memcpy (my_old_member_list, my_member_list,
  418. my_member_list_entries * sizeof (unsigned int));
  419. my_old_member_list_entries = my_member_list_entries;
  420. notify_lib_totem_membership (NULL, my_member_list_entries, my_member_list);
  421. }
  422. static void cpg_sync_abort (void)
  423. {
  424. }
  425. static int notify_lib_totem_membership (
  426. void *conn,
  427. int member_list_entries,
  428. const unsigned int *member_list)
  429. {
  430. struct list_head *iter;
  431. char *buf;
  432. int size;
  433. struct res_lib_cpg_totem_confchg_callback *res;
  434. size = sizeof(struct res_lib_cpg_totem_confchg_callback) +
  435. sizeof(mar_uint32_t) * (member_list_entries);
  436. buf = alloca(size);
  437. if (!buf)
  438. return CPG_ERR_LIBRARY;
  439. res = (struct res_lib_cpg_totem_confchg_callback *)buf;
  440. res->member_list_entries = member_list_entries;
  441. res->header.size = size;
  442. res->header.id = MESSAGE_RES_CPG_TOTEM_CONFCHG_CALLBACK;
  443. res->header.error = CS_OK;
  444. memcpy (&res->ring_id, &last_sync_ring_id, sizeof (mar_cpg_ring_id_t));
  445. memcpy (res->member_list, member_list, res->member_list_entries * sizeof (mar_uint32_t));
  446. if (conn == NULL) {
  447. for (iter = cpg_pd_list_head.next; iter != &cpg_pd_list_head; iter = iter->next) {
  448. struct cpg_pd *cpg_pd = list_entry (iter, struct cpg_pd, list);
  449. api->ipc_dispatch_send (cpg_pd->conn, buf, size);
  450. }
  451. } else {
  452. api->ipc_dispatch_send (conn, buf, size);
  453. }
  454. return CPG_OK;
  455. }
  456. static int notify_lib_joinlist(
  457. const mar_cpg_name_t *group_name,
  458. void *conn,
  459. int joined_list_entries,
  460. mar_cpg_address_t *joined_list,
  461. int left_list_entries,
  462. mar_cpg_address_t *left_list,
  463. int id)
  464. {
  465. int size;
  466. char *buf;
  467. struct list_head *iter;
  468. int count;
  469. struct res_lib_cpg_confchg_callback *res;
  470. mar_cpg_address_t *retgi;
  471. count = 0;
  472. for (iter = process_info_list_head.next; iter != &process_info_list_head; iter = iter->next) {
  473. struct process_info *pi = list_entry (iter, struct process_info, list);
  474. if (mar_name_compare (&pi->group, group_name) == 0) {
  475. int i;
  476. int founded = 0;
  477. for (i = 0; i < left_list_entries; i++) {
  478. if (left_list[i].nodeid == pi->nodeid && left_list[i].pid == pi->pid) {
  479. founded++;
  480. }
  481. }
  482. if (!founded)
  483. count++;
  484. }
  485. }
  486. size = sizeof(struct res_lib_cpg_confchg_callback) +
  487. sizeof(mar_cpg_address_t) * (count + left_list_entries + joined_list_entries);
  488. buf = alloca(size);
  489. if (!buf)
  490. return CPG_ERR_LIBRARY;
  491. res = (struct res_lib_cpg_confchg_callback *)buf;
  492. res->joined_list_entries = joined_list_entries;
  493. res->left_list_entries = left_list_entries;
  494. res->member_list_entries = count;
  495. retgi = res->member_list;
  496. res->header.size = size;
  497. res->header.id = id;
  498. res->header.error = CS_OK;
  499. memcpy(&res->group_name, group_name, sizeof(mar_cpg_name_t));
  500. for (iter = process_info_list_head.next; iter != &process_info_list_head; iter = iter->next) {
  501. struct process_info *pi=list_entry (iter, struct process_info, list);
  502. if (mar_name_compare (&pi->group, group_name) == 0) {
  503. int i;
  504. int founded = 0;
  505. for (i = 0;i < left_list_entries; i++) {
  506. if (left_list[i].nodeid == pi->nodeid && left_list[i].pid == pi->pid) {
  507. founded++;
  508. }
  509. }
  510. if (!founded) {
  511. retgi->nodeid = pi->nodeid;
  512. retgi->pid = pi->pid;
  513. retgi++;
  514. }
  515. }
  516. }
  517. if (left_list_entries) {
  518. memcpy (retgi, left_list, left_list_entries * sizeof(mar_cpg_address_t));
  519. retgi += left_list_entries;
  520. }
  521. if (joined_list_entries) {
  522. memcpy (retgi, joined_list, joined_list_entries * sizeof(mar_cpg_address_t));
  523. retgi += joined_list_entries;
  524. }
  525. if (conn) {
  526. api->ipc_dispatch_send (conn, buf, size);
  527. } else {
  528. for (iter = cpg_pd_list_head.next; iter != &cpg_pd_list_head; iter = iter->next) {
  529. struct cpg_pd *cpd = list_entry (iter, struct cpg_pd, list);
  530. if (mar_name_compare (&cpd->group_name, group_name) == 0) {
  531. assert (left_list_entries <= 1);
  532. assert (joined_list_entries <= 1);
  533. if (joined_list_entries) {
  534. if (joined_list[0].pid == cpd->pid &&
  535. joined_list[0].nodeid == api->totem_nodeid_get()) {
  536. cpd->cpd_state = CPD_STATE_JOIN_COMPLETED;
  537. }
  538. }
  539. if (cpd->cpd_state == CPD_STATE_JOIN_COMPLETED ||
  540. cpd->cpd_state == CPD_STATE_LEAVE_STARTED) {
  541. api->ipc_dispatch_send (cpd->conn, buf, size);
  542. }
  543. if (left_list_entries) {
  544. if (left_list[0].pid == cpd->pid &&
  545. left_list[0].nodeid == api->totem_nodeid_get()) {
  546. cpd->pid = 0;
  547. memset (&cpd->group_name, 0, sizeof(cpd->group_name));
  548. cpd->cpd_state = CPD_STATE_UNJOINED;
  549. }
  550. }
  551. }
  552. }
  553. }
  554. /*
  555. * Traverse thru cpds and send totem membership for cpd, where it is not send yet
  556. */
  557. for (iter = cpg_pd_list_head.next; iter != &cpg_pd_list_head; iter = iter->next) {
  558. struct cpg_pd *cpd = list_entry (iter, struct cpg_pd, list);
  559. if ((cpd->flags & CPG_MODEL_V1_DELIVER_INITIAL_TOTEM_CONF) && (cpd->initial_totem_conf_sent == 0)) {
  560. cpd->initial_totem_conf_sent = 1;
  561. notify_lib_totem_membership (cpd->conn, my_old_member_list_entries, my_old_member_list);
  562. }
  563. }
  564. return CPG_OK;
  565. }
  566. static int cpg_exec_init_fn (struct corosync_api_v1 *corosync_api)
  567. {
  568. #ifdef COROSYNC_SOLARIS
  569. logsys_subsys_init();
  570. #endif
  571. api = corosync_api;
  572. return (0);
  573. }
  574. static void cpg_iteration_instance_finalize (struct cpg_iteration_instance *cpg_iteration_instance)
  575. {
  576. struct list_head *iter, *iter_next;
  577. struct process_info *pi;
  578. for (iter = cpg_iteration_instance->items_list_head.next;
  579. iter != &cpg_iteration_instance->items_list_head;
  580. iter = iter_next) {
  581. iter_next = iter->next;
  582. pi = list_entry (iter, struct process_info, list);
  583. list_del (&pi->list);
  584. free (pi);
  585. }
  586. list_del (&cpg_iteration_instance->list);
  587. hdb_handle_destroy (&cpg_iteration_handle_t_db, cpg_iteration_instance->handle);
  588. }
  589. static void cpg_pd_finalize (struct cpg_pd *cpd)
  590. {
  591. struct list_head *iter, *iter_next;
  592. struct cpg_iteration_instance *cpii;
  593. for (iter = cpd->iteration_instance_list_head.next;
  594. iter != &cpd->iteration_instance_list_head;
  595. iter = iter_next) {
  596. iter_next = iter->next;
  597. cpii = list_entry (iter, struct cpg_iteration_instance, list);
  598. cpg_iteration_instance_finalize (cpii);
  599. }
  600. list_del (&cpd->list);
  601. }
  602. static int cpg_lib_exit_fn (void *conn)
  603. {
  604. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  605. log_printf(LOGSYS_LEVEL_DEBUG, "exit_fn for conn=%p\n", conn);
  606. if (cpd->group_name.length > 0) {
  607. cpg_node_joinleave_send (cpd->pid, &cpd->group_name,
  608. MESSAGE_REQ_EXEC_CPG_PROCLEAVE, CONFCHG_CPG_REASON_LEAVE);
  609. }
  610. cpg_pd_finalize (cpd);
  611. api->ipc_refcnt_dec (conn);
  612. return (0);
  613. }
  614. static int cpg_node_joinleave_send (unsigned int pid, const mar_cpg_name_t *group_name, int fn, int reason)
  615. {
  616. struct req_exec_cpg_procjoin req_exec_cpg_procjoin;
  617. struct iovec req_exec_cpg_iovec;
  618. int result;
  619. memcpy(&req_exec_cpg_procjoin.group_name, group_name, sizeof(mar_cpg_name_t));
  620. req_exec_cpg_procjoin.pid = pid;
  621. req_exec_cpg_procjoin.reason = reason;
  622. req_exec_cpg_procjoin.header.size = sizeof(req_exec_cpg_procjoin);
  623. req_exec_cpg_procjoin.header.id = SERVICE_ID_MAKE(CPG_SERVICE, fn);
  624. req_exec_cpg_iovec.iov_base = (char *)&req_exec_cpg_procjoin;
  625. req_exec_cpg_iovec.iov_len = sizeof(req_exec_cpg_procjoin);
  626. result = api->totem_mcast (&req_exec_cpg_iovec, 1, TOTEM_AGREED);
  627. return (result);
  628. }
  629. /* Can byteswap join & leave messages */
  630. static void exec_cpg_procjoin_endian_convert (void *msg)
  631. {
  632. struct req_exec_cpg_procjoin *req_exec_cpg_procjoin = msg;
  633. req_exec_cpg_procjoin->pid = swab32(req_exec_cpg_procjoin->pid);
  634. swab_mar_cpg_name_t (&req_exec_cpg_procjoin->group_name);
  635. req_exec_cpg_procjoin->reason = swab32(req_exec_cpg_procjoin->reason);
  636. }
  637. static void exec_cpg_joinlist_endian_convert (void *msg_v)
  638. {
  639. char *msg = msg_v;
  640. coroipc_response_header_t *res = (coroipc_response_header_t *)msg;
  641. struct join_list_entry *jle = (struct join_list_entry *)(msg + sizeof(coroipc_response_header_t));
  642. swab_mar_int32_t (&res->size);
  643. while ((const char*)jle < msg + res->size) {
  644. jle->pid = swab32(jle->pid);
  645. swab_mar_cpg_name_t (&jle->group_name);
  646. jle++;
  647. }
  648. }
  649. static void exec_cpg_downlist_endian_convert (void *msg)
  650. {
  651. struct req_exec_cpg_downlist *req_exec_cpg_downlist = msg;
  652. unsigned int i;
  653. req_exec_cpg_downlist->left_nodes = swab32(req_exec_cpg_downlist->left_nodes);
  654. for (i = 0; i < req_exec_cpg_downlist->left_nodes; i++) {
  655. req_exec_cpg_downlist->nodeids[i] = swab32(req_exec_cpg_downlist->nodeids[i]);
  656. }
  657. }
  658. static void exec_cpg_mcast_endian_convert (void *msg)
  659. {
  660. struct req_exec_cpg_mcast *req_exec_cpg_mcast = msg;
  661. swab_coroipc_request_header_t (&req_exec_cpg_mcast->header);
  662. swab_mar_cpg_name_t (&req_exec_cpg_mcast->group_name);
  663. req_exec_cpg_mcast->pid = swab32(req_exec_cpg_mcast->pid);
  664. req_exec_cpg_mcast->msglen = swab32(req_exec_cpg_mcast->msglen);
  665. swab_mar_message_source_t (&req_exec_cpg_mcast->source);
  666. }
  667. static struct process_info *process_info_find(const mar_cpg_name_t *group_name, uint32_t pid, unsigned int nodeid) {
  668. struct list_head *iter;
  669. for (iter = process_info_list_head.next; iter != &process_info_list_head; ) {
  670. struct process_info *pi = list_entry (iter, struct process_info, list);
  671. iter = iter->next;
  672. if (pi->pid == pid && pi->nodeid == nodeid &&
  673. mar_name_compare (&pi->group, group_name) == 0) {
  674. return pi;
  675. }
  676. }
  677. return NULL;
  678. }
  679. static void do_proc_join(
  680. const mar_cpg_name_t *name,
  681. uint32_t pid,
  682. unsigned int nodeid,
  683. int reason)
  684. {
  685. struct process_info *pi;
  686. struct process_info *pi_entry;
  687. mar_cpg_address_t notify_info;
  688. struct list_head *list;
  689. struct list_head *list_to_add = NULL;
  690. if (process_info_find (name, pid, nodeid) != NULL) {
  691. return ;
  692. }
  693. pi = malloc (sizeof (struct process_info));
  694. if (!pi) {
  695. log_printf(LOGSYS_LEVEL_WARNING, "Unable to allocate process_info struct");
  696. return;
  697. }
  698. pi->nodeid = nodeid;
  699. pi->pid = pid;
  700. memcpy(&pi->group, name, sizeof(*name));
  701. list_init(&pi->list);
  702. /*
  703. * Insert new process in sorted order so synchronization works properly
  704. */
  705. list_to_add = &process_info_list_head;
  706. for (list = process_info_list_head.next; list != &process_info_list_head; list = list->next) {
  707. pi_entry = list_entry(list, struct process_info, list);
  708. if (pi_entry->nodeid > pi->nodeid ||
  709. (pi_entry->nodeid == pi->nodeid && pi_entry->pid > pi->pid)) {
  710. break;
  711. }
  712. list_to_add = list;
  713. }
  714. list_splice (&pi->list, list_to_add);
  715. notify_info.pid = pi->pid;
  716. notify_info.nodeid = nodeid;
  717. notify_info.reason = reason;
  718. notify_lib_joinlist(&pi->group, NULL,
  719. 1, &notify_info,
  720. 0, NULL,
  721. MESSAGE_RES_CPG_CONFCHG_CALLBACK);
  722. }
  723. static void message_handler_req_exec_cpg_downlist (
  724. const void *message,
  725. unsigned int nodeid)
  726. {
  727. const struct req_exec_cpg_downlist *req_exec_cpg_downlist = message;
  728. int i;
  729. mar_cpg_address_t left_list[1];
  730. struct list_head *iter;
  731. /*
  732. FOR OPTIMALIZATION - Make list of lists
  733. */
  734. log_printf (LOGSYS_LEVEL_DEBUG, "downlist left_list: %d\n", req_exec_cpg_downlist->left_nodes);
  735. for (iter = process_info_list_head.next; iter != &process_info_list_head; ) {
  736. struct process_info *pi = list_entry(iter, struct process_info, list);
  737. iter = iter->next;
  738. for (i = 0; i < req_exec_cpg_downlist->left_nodes; i++) {
  739. if (pi->nodeid == req_exec_cpg_downlist->nodeids[i]) {
  740. left_list[0].nodeid = pi->nodeid;
  741. left_list[0].pid = pi->pid;
  742. left_list[0].reason = CONFCHG_CPG_REASON_NODEDOWN;
  743. notify_lib_joinlist(&pi->group, NULL,
  744. 0, NULL,
  745. 1, left_list,
  746. MESSAGE_RES_CPG_CONFCHG_CALLBACK);
  747. list_del (&pi->list);
  748. free (pi);
  749. break;
  750. }
  751. }
  752. }
  753. }
  754. static void message_handler_req_exec_cpg_procjoin (
  755. const void *message,
  756. unsigned int nodeid)
  757. {
  758. const struct req_exec_cpg_procjoin *req_exec_cpg_procjoin = message;
  759. log_printf(LOGSYS_LEVEL_DEBUG, "got procjoin message from cluster node %d\n", nodeid);
  760. do_proc_join (&req_exec_cpg_procjoin->group_name,
  761. req_exec_cpg_procjoin->pid, nodeid,
  762. CONFCHG_CPG_REASON_JOIN);
  763. }
  764. static void message_handler_req_exec_cpg_procleave (
  765. const void *message,
  766. unsigned int nodeid)
  767. {
  768. const struct req_exec_cpg_procjoin *req_exec_cpg_procjoin = message;
  769. struct process_info *pi;
  770. struct list_head *iter;
  771. mar_cpg_address_t notify_info;
  772. log_printf(LOGSYS_LEVEL_DEBUG, "got procleave message from cluster node %d\n", nodeid);
  773. notify_info.pid = req_exec_cpg_procjoin->pid;
  774. notify_info.nodeid = nodeid;
  775. notify_info.reason = req_exec_cpg_procjoin->reason;
  776. notify_lib_joinlist(&req_exec_cpg_procjoin->group_name, NULL,
  777. 0, NULL,
  778. 1, &notify_info,
  779. MESSAGE_RES_CPG_CONFCHG_CALLBACK);
  780. for (iter = process_info_list_head.next; iter != &process_info_list_head; ) {
  781. pi = list_entry(iter, struct process_info, list);
  782. iter = iter->next;
  783. if (pi->pid == req_exec_cpg_procjoin->pid && pi->nodeid == nodeid &&
  784. mar_name_compare (&pi->group, &req_exec_cpg_procjoin->group_name)==0) {
  785. list_del (&pi->list);
  786. free (pi);
  787. }
  788. }
  789. }
  790. /* Got a proclist from another node */
  791. static void message_handler_req_exec_cpg_joinlist (
  792. const void *message_v,
  793. unsigned int nodeid)
  794. {
  795. const char *message = message_v;
  796. const coroipc_response_header_t *res = (const coroipc_response_header_t *)message;
  797. const struct join_list_entry *jle = (const struct join_list_entry *)(message + sizeof(coroipc_response_header_t));
  798. log_printf(LOGSYS_LEVEL_DEBUG, "got joinlist message from node %x\n",
  799. nodeid);
  800. /* Ignore our own messages */
  801. if (nodeid == api->totem_nodeid_get()) {
  802. return;
  803. }
  804. while ((const char*)jle < message + res->size) {
  805. do_proc_join (&jle->group_name, jle->pid, nodeid,
  806. CONFCHG_CPG_REASON_NODEUP);
  807. jle++;
  808. }
  809. }
  810. static void message_handler_req_exec_cpg_mcast (
  811. const void *message,
  812. unsigned int nodeid)
  813. {
  814. const struct req_exec_cpg_mcast *req_exec_cpg_mcast = message;
  815. struct res_lib_cpg_deliver_callback res_lib_cpg_mcast;
  816. int msglen = req_exec_cpg_mcast->msglen;
  817. struct list_head *iter, *pi_iter;
  818. struct cpg_pd *cpd;
  819. struct iovec iovec[2];
  820. int known_node = 0;
  821. res_lib_cpg_mcast.header.id = MESSAGE_RES_CPG_DELIVER_CALLBACK;
  822. res_lib_cpg_mcast.header.size = sizeof(res_lib_cpg_mcast) + msglen;
  823. res_lib_cpg_mcast.msglen = msglen;
  824. res_lib_cpg_mcast.pid = req_exec_cpg_mcast->pid;
  825. res_lib_cpg_mcast.nodeid = nodeid;
  826. memcpy(&res_lib_cpg_mcast.group_name, &req_exec_cpg_mcast->group_name,
  827. sizeof(mar_cpg_name_t));
  828. iovec[0].iov_base = (void *)&res_lib_cpg_mcast;
  829. iovec[0].iov_len = sizeof (res_lib_cpg_mcast);
  830. iovec[1].iov_base = (char*)message+sizeof(*req_exec_cpg_mcast);
  831. iovec[1].iov_len = msglen;
  832. for (iter = cpg_pd_list_head.next; iter != &cpg_pd_list_head; ) {
  833. cpd = list_entry(iter, struct cpg_pd, list);
  834. iter = iter->next;
  835. if ((cpd->cpd_state == CPD_STATE_LEAVE_STARTED || cpd->cpd_state == CPD_STATE_JOIN_COMPLETED)
  836. && (mar_name_compare (&cpd->group_name, &req_exec_cpg_mcast->group_name) == 0)) {
  837. if (!known_node) {
  838. /* Try to find, if we know the node */
  839. for (pi_iter = process_info_list_head.next;
  840. pi_iter != &process_info_list_head; pi_iter = pi_iter->next) {
  841. struct process_info *pi = list_entry (pi_iter, struct process_info, list);
  842. if (pi->nodeid == nodeid &&
  843. mar_name_compare (&pi->group, &req_exec_cpg_mcast->group_name) == 0) {
  844. known_node = 1;
  845. break;
  846. }
  847. }
  848. }
  849. if (!known_node) {
  850. /* Unknown node -> we will not deliver message */
  851. return ;
  852. }
  853. api->ipc_dispatch_iov_send (cpd->conn, iovec, 2);
  854. }
  855. }
  856. }
  857. static int cpg_exec_send_downlist(void)
  858. {
  859. struct iovec iov;
  860. g_req_exec_cpg_downlist.header.id = SERVICE_ID_MAKE(CPG_SERVICE, MESSAGE_REQ_EXEC_CPG_DOWNLIST);
  861. g_req_exec_cpg_downlist.header.size = sizeof(struct req_exec_cpg_downlist);
  862. iov.iov_base = (void *)&g_req_exec_cpg_downlist;
  863. iov.iov_len = g_req_exec_cpg_downlist.header.size;
  864. return (api->totem_mcast (&iov, 1, TOTEM_AGREED));
  865. }
  866. static int cpg_exec_send_joinlist(void)
  867. {
  868. int count = 0;
  869. struct list_head *iter;
  870. coroipc_response_header_t *res;
  871. char *buf;
  872. struct join_list_entry *jle;
  873. struct iovec req_exec_cpg_iovec;
  874. for (iter = process_info_list_head.next; iter != &process_info_list_head; iter = iter->next) {
  875. struct process_info *pi = list_entry (iter, struct process_info, list);
  876. if (pi->nodeid == api->totem_nodeid_get ()) {
  877. count++;
  878. }
  879. }
  880. /* Nothing to send */
  881. if (!count)
  882. return 0;
  883. buf = alloca(sizeof(coroipc_response_header_t) + sizeof(struct join_list_entry) * count);
  884. if (!buf) {
  885. log_printf(LOGSYS_LEVEL_WARNING, "Unable to allocate joinlist buffer");
  886. return -1;
  887. }
  888. jle = (struct join_list_entry *)(buf + sizeof(coroipc_response_header_t));
  889. res = (coroipc_response_header_t *)buf;
  890. for (iter = process_info_list_head.next; iter != &process_info_list_head; iter = iter->next) {
  891. struct process_info *pi = list_entry (iter, struct process_info, list);
  892. if (pi->nodeid == api->totem_nodeid_get ()) {
  893. memcpy (&jle->group_name, &pi->group, sizeof (mar_cpg_name_t));
  894. jle->pid = pi->pid;
  895. jle++;
  896. }
  897. }
  898. res->id = SERVICE_ID_MAKE(CPG_SERVICE, MESSAGE_REQ_EXEC_CPG_JOINLIST);
  899. res->size = sizeof(coroipc_response_header_t)+sizeof(struct join_list_entry) * count;
  900. req_exec_cpg_iovec.iov_base = buf;
  901. req_exec_cpg_iovec.iov_len = res->size;
  902. return (api->totem_mcast (&req_exec_cpg_iovec, 1, TOTEM_AGREED));
  903. }
  904. static int cpg_lib_init_fn (void *conn)
  905. {
  906. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  907. memset (cpd, 0, sizeof(struct cpg_pd));
  908. cpd->conn = conn;
  909. list_add (&cpd->list, &cpg_pd_list_head);
  910. list_init (&cpd->iteration_instance_list_head);
  911. api->ipc_refcnt_inc (conn);
  912. log_printf(LOGSYS_LEVEL_DEBUG, "lib_init_fn: conn=%p, cpd=%p\n", conn, cpd);
  913. return (0);
  914. }
  915. /* Join message from the library */
  916. static void message_handler_req_lib_cpg_join (void *conn, const void *message)
  917. {
  918. const struct req_lib_cpg_join *req_lib_cpg_join = message;
  919. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  920. struct res_lib_cpg_join res_lib_cpg_join;
  921. cs_error_t error = CPG_OK;
  922. struct list_head *iter;
  923. /* Test, if we don't have same pid and group name joined */
  924. for (iter = cpg_pd_list_head.next; iter != &cpg_pd_list_head; iter = iter->next) {
  925. struct cpg_pd *cpd_item = list_entry (iter, struct cpg_pd, list);
  926. if (cpd_item->pid == req_lib_cpg_join->pid &&
  927. mar_name_compare(&req_lib_cpg_join->group_name, &cpd_item->group_name) == 0) {
  928. /* We have same pid and group name joined -> return error */
  929. error = CPG_ERR_EXIST;
  930. goto response_send;
  931. }
  932. }
  933. /*
  934. * Same check must be done in process info list, because there may be not yet delivered
  935. * leave of client.
  936. */
  937. for (iter = process_info_list_head.next; iter != &process_info_list_head; iter = iter->next) {
  938. struct process_info *pi = list_entry (iter, struct process_info, list);
  939. if (pi->nodeid == api->totem_nodeid_get () && pi->pid == req_lib_cpg_join->pid &&
  940. mar_name_compare(&req_lib_cpg_join->group_name, &pi->group) == 0) {
  941. /* We have same pid and group name joined -> return error */
  942. error = CPG_ERR_TRY_AGAIN;
  943. goto response_send;
  944. }
  945. }
  946. switch (cpd->cpd_state) {
  947. case CPD_STATE_UNJOINED:
  948. error = CPG_OK;
  949. cpd->cpd_state = CPD_STATE_JOIN_STARTED;
  950. cpd->pid = req_lib_cpg_join->pid;
  951. cpd->flags = req_lib_cpg_join->flags;
  952. memcpy (&cpd->group_name, &req_lib_cpg_join->group_name,
  953. sizeof (cpd->group_name));
  954. cpg_node_joinleave_send (req_lib_cpg_join->pid,
  955. &req_lib_cpg_join->group_name,
  956. MESSAGE_REQ_EXEC_CPG_PROCJOIN, CONFCHG_CPG_REASON_JOIN);
  957. break;
  958. case CPD_STATE_LEAVE_STARTED:
  959. error = CPG_ERR_BUSY;
  960. break;
  961. case CPD_STATE_JOIN_STARTED:
  962. error = CPG_ERR_EXIST;
  963. break;
  964. case CPD_STATE_JOIN_COMPLETED:
  965. error = CPG_ERR_EXIST;
  966. break;
  967. }
  968. response_send:
  969. res_lib_cpg_join.header.size = sizeof(res_lib_cpg_join);
  970. res_lib_cpg_join.header.id = MESSAGE_RES_CPG_JOIN;
  971. res_lib_cpg_join.header.error = error;
  972. api->ipc_response_send (conn, &res_lib_cpg_join, sizeof(res_lib_cpg_join));
  973. }
  974. /* Leave message from the library */
  975. static void message_handler_req_lib_cpg_leave (void *conn, const void *message)
  976. {
  977. struct res_lib_cpg_leave res_lib_cpg_leave;
  978. cs_error_t error = CPG_OK;
  979. struct req_lib_cpg_leave *req_lib_cpg_leave = (struct req_lib_cpg_leave *)message;
  980. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  981. log_printf(LOGSYS_LEVEL_DEBUG, "got leave request on %p\n", conn);
  982. switch (cpd->cpd_state) {
  983. case CPD_STATE_UNJOINED:
  984. error = CPG_ERR_NOT_EXIST;
  985. break;
  986. case CPD_STATE_LEAVE_STARTED:
  987. error = CPG_ERR_NOT_EXIST;
  988. break;
  989. case CPD_STATE_JOIN_STARTED:
  990. error = CPG_ERR_BUSY;
  991. break;
  992. case CPD_STATE_JOIN_COMPLETED:
  993. error = CPG_OK;
  994. cpd->cpd_state = CPD_STATE_LEAVE_STARTED;
  995. cpg_node_joinleave_send (req_lib_cpg_leave->pid,
  996. &req_lib_cpg_leave->group_name,
  997. MESSAGE_REQ_EXEC_CPG_PROCLEAVE,
  998. CONFCHG_CPG_REASON_LEAVE);
  999. break;
  1000. }
  1001. /* send return */
  1002. res_lib_cpg_leave.header.size = sizeof(res_lib_cpg_leave);
  1003. res_lib_cpg_leave.header.id = MESSAGE_RES_CPG_LEAVE;
  1004. res_lib_cpg_leave.header.error = error;
  1005. api->ipc_response_send(conn, &res_lib_cpg_leave, sizeof(res_lib_cpg_leave));
  1006. }
  1007. /* Finalize message from library */
  1008. static void message_handler_req_lib_cpg_finalize (
  1009. void *conn,
  1010. const void *message)
  1011. {
  1012. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1013. struct res_lib_cpg_finalize res_lib_cpg_finalize;
  1014. cs_error_t error = CS_OK;
  1015. log_printf (LOGSYS_LEVEL_DEBUG, "cpg finalize for conn=%p\n", conn);
  1016. /*
  1017. * We will just remove cpd from list. After this call, connection will be
  1018. * closed on lib side, and cpg_lib_exit_fn will be called
  1019. */
  1020. list_del (&cpd->list);
  1021. list_init (&cpd->list);
  1022. res_lib_cpg_finalize.header.size = sizeof (res_lib_cpg_finalize);
  1023. res_lib_cpg_finalize.header.id = MESSAGE_RES_CPG_FINALIZE;
  1024. res_lib_cpg_finalize.header.error = error;
  1025. api->ipc_response_send (conn, &res_lib_cpg_finalize,
  1026. sizeof (res_lib_cpg_finalize));
  1027. }
  1028. /* Mcast message from the library */
  1029. static void message_handler_req_lib_cpg_mcast (void *conn, const void *message)
  1030. {
  1031. const struct req_lib_cpg_mcast *req_lib_cpg_mcast = message;
  1032. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1033. mar_cpg_name_t group_name = cpd->group_name;
  1034. struct iovec req_exec_cpg_iovec[2];
  1035. struct req_exec_cpg_mcast req_exec_cpg_mcast;
  1036. struct res_lib_cpg_mcast res_lib_cpg_mcast;
  1037. int msglen = req_lib_cpg_mcast->msglen;
  1038. int result;
  1039. cs_error_t error = CPG_ERR_NOT_EXIST;
  1040. log_printf(LOGSYS_LEVEL_DEBUG, "got mcast request on %p\n", conn);
  1041. switch (cpd->cpd_state) {
  1042. case CPD_STATE_UNJOINED:
  1043. error = CPG_ERR_NOT_EXIST;
  1044. break;
  1045. case CPD_STATE_LEAVE_STARTED:
  1046. error = CPG_ERR_NOT_EXIST;
  1047. break;
  1048. case CPD_STATE_JOIN_STARTED:
  1049. error = CPG_OK;
  1050. break;
  1051. case CPD_STATE_JOIN_COMPLETED:
  1052. error = CPG_OK;
  1053. break;
  1054. }
  1055. if (error == CPG_OK) {
  1056. req_exec_cpg_mcast.header.size = sizeof(req_exec_cpg_mcast) + msglen;
  1057. req_exec_cpg_mcast.header.id = SERVICE_ID_MAKE(CPG_SERVICE,
  1058. MESSAGE_REQ_EXEC_CPG_MCAST);
  1059. req_exec_cpg_mcast.pid = cpd->pid;
  1060. req_exec_cpg_mcast.msglen = msglen;
  1061. api->ipc_source_set (&req_exec_cpg_mcast.source, conn);
  1062. memcpy(&req_exec_cpg_mcast.group_name, &group_name,
  1063. sizeof(mar_cpg_name_t));
  1064. req_exec_cpg_iovec[0].iov_base = (char *)&req_exec_cpg_mcast;
  1065. req_exec_cpg_iovec[0].iov_len = sizeof(req_exec_cpg_mcast);
  1066. req_exec_cpg_iovec[1].iov_base = (char *)&req_lib_cpg_mcast->message;
  1067. req_exec_cpg_iovec[1].iov_len = msglen;
  1068. result = api->totem_mcast (req_exec_cpg_iovec, 2, TOTEM_AGREED);
  1069. assert(result == 0);
  1070. }
  1071. res_lib_cpg_mcast.header.size = sizeof(res_lib_cpg_mcast);
  1072. res_lib_cpg_mcast.header.id = MESSAGE_RES_CPG_MCAST;
  1073. res_lib_cpg_mcast.header.error = error;
  1074. api->ipc_response_send (conn, &res_lib_cpg_mcast,
  1075. sizeof (res_lib_cpg_mcast));
  1076. }
  1077. static void message_handler_req_lib_cpg_membership (void *conn,
  1078. const void *message)
  1079. {
  1080. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1081. cs_error_t error = CPG_ERR_NOT_EXIST;
  1082. coroipc_response_header_t res;
  1083. switch (cpd->cpd_state) {
  1084. case CPD_STATE_UNJOINED:
  1085. error = CPG_ERR_NOT_EXIST;
  1086. break;
  1087. case CPD_STATE_LEAVE_STARTED:
  1088. error = CPG_ERR_NOT_EXIST;
  1089. break;
  1090. case CPD_STATE_JOIN_STARTED:
  1091. error = CPG_ERR_BUSY;
  1092. break;
  1093. case CPD_STATE_JOIN_COMPLETED:
  1094. error = CPG_OK;
  1095. break;
  1096. }
  1097. res.size = sizeof (res);
  1098. res.id = MESSAGE_RES_CPG_MEMBERSHIP;
  1099. res.error = error;
  1100. api->ipc_response_send (conn, &res, sizeof(res));
  1101. return;
  1102. if (error == CPG_OK) {
  1103. notify_lib_joinlist (&cpd->group_name, conn, 0, NULL, 0, NULL,
  1104. MESSAGE_RES_CPG_MEMBERSHIP);
  1105. }
  1106. }
  1107. static void message_handler_req_lib_cpg_local_get (void *conn,
  1108. const void *message)
  1109. {
  1110. struct res_lib_cpg_local_get res_lib_cpg_local_get;
  1111. res_lib_cpg_local_get.header.size = sizeof (res_lib_cpg_local_get);
  1112. res_lib_cpg_local_get.header.id = MESSAGE_RES_CPG_LOCAL_GET;
  1113. res_lib_cpg_local_get.header.error = CPG_OK;
  1114. res_lib_cpg_local_get.local_nodeid = api->totem_nodeid_get ();
  1115. api->ipc_response_send (conn, &res_lib_cpg_local_get,
  1116. sizeof (res_lib_cpg_local_get));
  1117. }
  1118. static void message_handler_req_lib_cpg_iteration_initialize (
  1119. void *conn,
  1120. const void *message)
  1121. {
  1122. const struct req_lib_cpg_iterationinitialize *req_lib_cpg_iterationinitialize = message;
  1123. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1124. hdb_handle_t cpg_iteration_handle = 0;
  1125. struct res_lib_cpg_iterationinitialize res_lib_cpg_iterationinitialize;
  1126. struct list_head *iter, *iter2;
  1127. struct cpg_iteration_instance *cpg_iteration_instance;
  1128. cs_error_t error = CS_OK;
  1129. int res;
  1130. log_printf (LOGSYS_LEVEL_DEBUG, "cpg iteration initialize\n");
  1131. /* Because between calling this function and *next can be some operations which will
  1132. * change list, we must do full copy.
  1133. */
  1134. /*
  1135. * Create new iteration instance
  1136. */
  1137. res = hdb_handle_create (&cpg_iteration_handle_t_db, sizeof (struct cpg_iteration_instance),
  1138. &cpg_iteration_handle);
  1139. if (res != 0) {
  1140. error = CS_ERR_NO_MEMORY;
  1141. goto response_send;
  1142. }
  1143. res = hdb_handle_get (&cpg_iteration_handle_t_db, cpg_iteration_handle, (void *)&cpg_iteration_instance);
  1144. if (res != 0) {
  1145. error = CS_ERR_BAD_HANDLE;
  1146. goto error_destroy;
  1147. }
  1148. list_init (&cpg_iteration_instance->items_list_head);
  1149. cpg_iteration_instance->handle = cpg_iteration_handle;
  1150. /*
  1151. * Create copy of process_info list "grouped by" group name
  1152. */
  1153. for (iter = process_info_list_head.next; iter != &process_info_list_head; iter = iter->next) {
  1154. struct process_info *pi = list_entry (iter, struct process_info, list);
  1155. struct process_info *new_pi;
  1156. if (req_lib_cpg_iterationinitialize->iteration_type == CPG_ITERATION_NAME_ONLY) {
  1157. /*
  1158. * Try to find processed group name in our list new list
  1159. */
  1160. int found = 0;
  1161. for (iter2 = cpg_iteration_instance->items_list_head.next;
  1162. iter2 != &cpg_iteration_instance->items_list_head;
  1163. iter2 = iter2->next) {
  1164. struct process_info *pi2 = list_entry (iter2, struct process_info, list);
  1165. if (mar_name_compare (&pi2->group, &pi->group) == 0) {
  1166. found = 1;
  1167. break;
  1168. }
  1169. }
  1170. if (found) {
  1171. /*
  1172. * We have this name in list -> don't add
  1173. */
  1174. continue ;
  1175. }
  1176. } else if (req_lib_cpg_iterationinitialize->iteration_type == CPG_ITERATION_ONE_GROUP) {
  1177. /*
  1178. * Test pi group name with request
  1179. */
  1180. if (mar_name_compare (&pi->group, &req_lib_cpg_iterationinitialize->group_name) != 0)
  1181. /*
  1182. * Not same -> don't add
  1183. */
  1184. continue ;
  1185. }
  1186. new_pi = malloc (sizeof (struct process_info));
  1187. if (!new_pi) {
  1188. log_printf(LOGSYS_LEVEL_WARNING, "Unable to allocate process_info struct");
  1189. error = CS_ERR_NO_MEMORY;
  1190. goto error_put_destroy;
  1191. }
  1192. memcpy (new_pi, pi, sizeof (struct process_info));
  1193. list_init (&new_pi->list);
  1194. if (req_lib_cpg_iterationinitialize->iteration_type == CPG_ITERATION_NAME_ONLY) {
  1195. /*
  1196. * pid and nodeid -> undefined
  1197. */
  1198. new_pi->pid = new_pi->nodeid = 0;
  1199. }
  1200. /*
  1201. * We will return list "grouped" by "group name", so try to find right place to add
  1202. */
  1203. for (iter2 = cpg_iteration_instance->items_list_head.next;
  1204. iter2 != &cpg_iteration_instance->items_list_head;
  1205. iter2 = iter2->next) {
  1206. struct process_info *pi2 = list_entry (iter2, struct process_info, list);
  1207. if (mar_name_compare (&pi2->group, &pi->group) == 0) {
  1208. break;
  1209. }
  1210. }
  1211. list_add (&new_pi->list, iter2);
  1212. }
  1213. /*
  1214. * Now we have a full "grouped by" copy of process_info list
  1215. */
  1216. /*
  1217. * Add instance to current cpd list
  1218. */
  1219. list_init (&cpg_iteration_instance->list);
  1220. list_add (&cpg_iteration_instance->list, &cpd->iteration_instance_list_head);
  1221. cpg_iteration_instance->current_pointer = &cpg_iteration_instance->items_list_head;
  1222. error_put_destroy:
  1223. hdb_handle_put (&cpg_iteration_handle_t_db, cpg_iteration_handle);
  1224. error_destroy:
  1225. if (error != CS_OK) {
  1226. hdb_handle_destroy (&cpg_iteration_handle_t_db, cpg_iteration_handle);
  1227. }
  1228. response_send:
  1229. res_lib_cpg_iterationinitialize.header.size = sizeof (res_lib_cpg_iterationinitialize);
  1230. res_lib_cpg_iterationinitialize.header.id = MESSAGE_RES_CPG_ITERATIONINITIALIZE;
  1231. res_lib_cpg_iterationinitialize.header.error = error;
  1232. res_lib_cpg_iterationinitialize.iteration_handle = cpg_iteration_handle;
  1233. api->ipc_response_send (conn, &res_lib_cpg_iterationinitialize,
  1234. sizeof (res_lib_cpg_iterationinitialize));
  1235. }
  1236. static void message_handler_req_lib_cpg_iteration_next (
  1237. void *conn,
  1238. const void *message)
  1239. {
  1240. const struct req_lib_cpg_iterationnext *req_lib_cpg_iterationnext = message;
  1241. struct res_lib_cpg_iterationnext res_lib_cpg_iterationnext;
  1242. struct cpg_iteration_instance *cpg_iteration_instance;
  1243. cs_error_t error = CS_OK;
  1244. int res;
  1245. struct process_info *pi;
  1246. log_printf (LOGSYS_LEVEL_DEBUG, "cpg iteration next\n");
  1247. res = hdb_handle_get (&cpg_iteration_handle_t_db,
  1248. req_lib_cpg_iterationnext->iteration_handle,
  1249. (void *)&cpg_iteration_instance);
  1250. if (res != 0) {
  1251. error = CS_ERR_LIBRARY;
  1252. goto error_exit;
  1253. }
  1254. assert (cpg_iteration_instance);
  1255. cpg_iteration_instance->current_pointer = cpg_iteration_instance->current_pointer->next;
  1256. if (cpg_iteration_instance->current_pointer == &cpg_iteration_instance->items_list_head) {
  1257. error = CS_ERR_NO_SECTIONS;
  1258. goto error_put;
  1259. }
  1260. pi = list_entry (cpg_iteration_instance->current_pointer, struct process_info, list);
  1261. /*
  1262. * Copy iteration data
  1263. */
  1264. res_lib_cpg_iterationnext.description.nodeid = pi->nodeid;
  1265. res_lib_cpg_iterationnext.description.pid = pi->pid;
  1266. memcpy (&res_lib_cpg_iterationnext.description.group,
  1267. &pi->group,
  1268. sizeof (mar_cpg_name_t));
  1269. error_put:
  1270. hdb_handle_put (&cpg_iteration_handle_t_db, req_lib_cpg_iterationnext->iteration_handle);
  1271. error_exit:
  1272. res_lib_cpg_iterationnext.header.size = sizeof (res_lib_cpg_iterationnext);
  1273. res_lib_cpg_iterationnext.header.id = MESSAGE_RES_CPG_ITERATIONNEXT;
  1274. res_lib_cpg_iterationnext.header.error = error;
  1275. api->ipc_response_send (conn, &res_lib_cpg_iterationnext,
  1276. sizeof (res_lib_cpg_iterationnext));
  1277. }
  1278. static void message_handler_req_lib_cpg_iteration_finalize (
  1279. void *conn,
  1280. const void *message)
  1281. {
  1282. const struct req_lib_cpg_iterationfinalize *req_lib_cpg_iterationfinalize = message;
  1283. struct res_lib_cpg_iterationfinalize res_lib_cpg_iterationfinalize;
  1284. struct cpg_iteration_instance *cpg_iteration_instance;
  1285. cs_error_t error = CS_OK;
  1286. int res;
  1287. log_printf (LOGSYS_LEVEL_DEBUG, "cpg iteration finalize\n");
  1288. res = hdb_handle_get (&cpg_iteration_handle_t_db,
  1289. req_lib_cpg_iterationfinalize->iteration_handle,
  1290. (void *)&cpg_iteration_instance);
  1291. if (res != 0) {
  1292. error = CS_ERR_LIBRARY;
  1293. goto error_exit;
  1294. }
  1295. assert (cpg_iteration_instance);
  1296. cpg_iteration_instance_finalize (cpg_iteration_instance);
  1297. hdb_handle_put (&cpg_iteration_handle_t_db, cpg_iteration_instance->handle);
  1298. error_exit:
  1299. res_lib_cpg_iterationfinalize.header.size = sizeof (res_lib_cpg_iterationfinalize);
  1300. res_lib_cpg_iterationfinalize.header.id = MESSAGE_RES_CPG_ITERATIONFINALIZE;
  1301. res_lib_cpg_iterationfinalize.header.error = error;
  1302. api->ipc_response_send (conn, &res_lib_cpg_iterationfinalize,
  1303. sizeof (res_lib_cpg_iterationfinalize));
  1304. }