cpg.c 51 KB

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