cpg.c 43 KB

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