cpg.c 55 KB

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