cpg.c 41 KB

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