cpg.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925
  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. static void joinlist_inform_clients (void)
  739. {
  740. struct joinlist_msg *stored_msg;
  741. struct list_head *iter;
  742. unsigned int i;
  743. i = 0;
  744. for (iter = joinlist_messages_head.next;
  745. iter != &joinlist_messages_head;
  746. iter = iter->next) {
  747. stored_msg = list_entry(iter, struct joinlist_msg, list);
  748. log_printf (LOG_DEBUG, "joinlist_messages[%u] group:%s, ip:%s, pid:%d",
  749. i++, cpg_print_group_name(&stored_msg->group_name),
  750. (char*)api->totem_ifaces_print(stored_msg->sender_nodeid),
  751. stored_msg->pid);
  752. /* Ignore our own messages */
  753. if (stored_msg->sender_nodeid == api->totem_nodeid_get()) {
  754. continue ;
  755. }
  756. do_proc_join (&stored_msg->group_name, stored_msg->pid, stored_msg->sender_nodeid,
  757. CONFCHG_CPG_REASON_NODEUP);
  758. }
  759. }
  760. static void downlist_messages_delete (void)
  761. {
  762. struct downlist_msg *stored_msg;
  763. struct list_head *iter, *iter_next;
  764. for (iter = downlist_messages_head.next;
  765. iter != &downlist_messages_head;
  766. iter = iter_next) {
  767. iter_next = iter->next;
  768. stored_msg = list_entry(iter, struct downlist_msg, list);
  769. list_del (&stored_msg->list);
  770. free (stored_msg);
  771. }
  772. }
  773. static void joinlist_messages_delete (void)
  774. {
  775. struct joinlist_msg *stored_msg;
  776. struct list_head *iter, *iter_next;
  777. for (iter = joinlist_messages_head.next;
  778. iter != &joinlist_messages_head;
  779. iter = iter_next) {
  780. iter_next = iter->next;
  781. stored_msg = list_entry(iter, struct joinlist_msg, list);
  782. list_del (&stored_msg->list);
  783. free (stored_msg);
  784. }
  785. list_init (&joinlist_messages_head);
  786. }
  787. static int cpg_exec_init_fn (struct corosync_api_v1 *corosync_api)
  788. {
  789. #ifdef COROSYNC_SOLARIS
  790. logsys_subsys_init();
  791. #endif
  792. list_init (&downlist_messages_head);
  793. list_init (&joinlist_messages_head);
  794. api = corosync_api;
  795. return (0);
  796. }
  797. static void cpg_iteration_instance_finalize (struct cpg_iteration_instance *cpg_iteration_instance)
  798. {
  799. struct list_head *iter, *iter_next;
  800. struct process_info *pi;
  801. for (iter = cpg_iteration_instance->items_list_head.next;
  802. iter != &cpg_iteration_instance->items_list_head;
  803. iter = iter_next) {
  804. iter_next = iter->next;
  805. pi = list_entry (iter, struct process_info, list);
  806. list_del (&pi->list);
  807. free (pi);
  808. }
  809. list_del (&cpg_iteration_instance->list);
  810. hdb_handle_destroy (&cpg_iteration_handle_t_db, cpg_iteration_instance->handle);
  811. }
  812. static void cpg_pd_finalize (struct cpg_pd *cpd)
  813. {
  814. struct list_head *iter, *iter_next;
  815. struct cpg_iteration_instance *cpii;
  816. for (iter = cpd->iteration_instance_list_head.next;
  817. iter != &cpd->iteration_instance_list_head;
  818. iter = iter_next) {
  819. iter_next = iter->next;
  820. cpii = list_entry (iter, struct cpg_iteration_instance, list);
  821. cpg_iteration_instance_finalize (cpii);
  822. }
  823. list_del (&cpd->list);
  824. }
  825. static int cpg_lib_exit_fn (void *conn)
  826. {
  827. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  828. int result;
  829. log_printf(LOGSYS_LEVEL_DEBUG, "exit_fn for conn=%p\n", conn);
  830. list_del (&cpd->list);
  831. list_init (&cpd->list);
  832. if (cpd->group_name.length > 0) {
  833. result = cpg_node_joinleave_send (cpd->pid, &cpd->group_name,
  834. MESSAGE_REQ_EXEC_CPG_PROCLEAVE, CONFCHG_CPG_REASON_PROCDOWN);
  835. if (result == -1) {
  836. /*
  837. * Call this function again later
  838. */
  839. return (result);
  840. }
  841. }
  842. cpg_pd_finalize (cpd);
  843. api->ipc_refcnt_dec (conn);
  844. return (0);
  845. }
  846. static int cpg_node_joinleave_send (unsigned int pid, const mar_cpg_name_t *group_name, int fn, int reason)
  847. {
  848. struct req_exec_cpg_procjoin req_exec_cpg_procjoin;
  849. struct iovec req_exec_cpg_iovec;
  850. int result;
  851. memcpy(&req_exec_cpg_procjoin.group_name, group_name, sizeof(mar_cpg_name_t));
  852. req_exec_cpg_procjoin.pid = pid;
  853. req_exec_cpg_procjoin.reason = reason;
  854. req_exec_cpg_procjoin.header.size = sizeof(req_exec_cpg_procjoin);
  855. req_exec_cpg_procjoin.header.id = SERVICE_ID_MAKE(CPG_SERVICE, fn);
  856. req_exec_cpg_iovec.iov_base = (char *)&req_exec_cpg_procjoin;
  857. req_exec_cpg_iovec.iov_len = sizeof(req_exec_cpg_procjoin);
  858. result = api->totem_mcast (&req_exec_cpg_iovec, 1, TOTEM_AGREED);
  859. return (result);
  860. }
  861. /* Can byteswap join & leave messages */
  862. static void exec_cpg_procjoin_endian_convert (void *msg)
  863. {
  864. struct req_exec_cpg_procjoin *req_exec_cpg_procjoin = msg;
  865. req_exec_cpg_procjoin->pid = swab32(req_exec_cpg_procjoin->pid);
  866. swab_mar_cpg_name_t (&req_exec_cpg_procjoin->group_name);
  867. req_exec_cpg_procjoin->reason = swab32(req_exec_cpg_procjoin->reason);
  868. }
  869. static void exec_cpg_joinlist_endian_convert (void *msg_v)
  870. {
  871. char *msg = msg_v;
  872. coroipc_response_header_t *res = (coroipc_response_header_t *)msg;
  873. struct join_list_entry *jle = (struct join_list_entry *)(msg + sizeof(coroipc_response_header_t));
  874. swab_mar_int32_t (&res->size);
  875. while ((const char*)jle < msg + res->size) {
  876. jle->pid = swab32(jle->pid);
  877. swab_mar_cpg_name_t (&jle->group_name);
  878. jle++;
  879. }
  880. }
  881. static void exec_cpg_downlist_endian_convert_old (void *msg)
  882. {
  883. }
  884. static void exec_cpg_downlist_endian_convert (void *msg)
  885. {
  886. struct req_exec_cpg_downlist *req_exec_cpg_downlist = msg;
  887. unsigned int i;
  888. req_exec_cpg_downlist->left_nodes = swab32(req_exec_cpg_downlist->left_nodes);
  889. req_exec_cpg_downlist->old_members = swab32(req_exec_cpg_downlist->old_members);
  890. for (i = 0; i < req_exec_cpg_downlist->left_nodes; i++) {
  891. req_exec_cpg_downlist->nodeids[i] = swab32(req_exec_cpg_downlist->nodeids[i]);
  892. }
  893. }
  894. static void exec_cpg_mcast_endian_convert (void *msg)
  895. {
  896. struct req_exec_cpg_mcast *req_exec_cpg_mcast = msg;
  897. swab_coroipc_request_header_t (&req_exec_cpg_mcast->header);
  898. swab_mar_cpg_name_t (&req_exec_cpg_mcast->group_name);
  899. req_exec_cpg_mcast->pid = swab32(req_exec_cpg_mcast->pid);
  900. req_exec_cpg_mcast->msglen = swab32(req_exec_cpg_mcast->msglen);
  901. swab_mar_message_source_t (&req_exec_cpg_mcast->source);
  902. }
  903. static struct process_info *process_info_find(const mar_cpg_name_t *group_name, uint32_t pid, unsigned int nodeid) {
  904. struct list_head *iter;
  905. for (iter = process_info_list_head.next; iter != &process_info_list_head; ) {
  906. struct process_info *pi = list_entry (iter, struct process_info, list);
  907. iter = iter->next;
  908. if (pi->pid == pid && pi->nodeid == nodeid &&
  909. mar_name_compare (&pi->group, group_name) == 0) {
  910. return pi;
  911. }
  912. }
  913. return NULL;
  914. }
  915. static void do_proc_join(
  916. const mar_cpg_name_t *name,
  917. uint32_t pid,
  918. unsigned int nodeid,
  919. int reason)
  920. {
  921. struct process_info *pi;
  922. struct process_info *pi_entry;
  923. mar_cpg_address_t notify_info;
  924. struct list_head *list;
  925. struct list_head *list_to_add = NULL;
  926. if (process_info_find (name, pid, nodeid) != NULL) {
  927. return ;
  928. }
  929. pi = malloc (sizeof (struct process_info));
  930. if (!pi) {
  931. log_printf(LOGSYS_LEVEL_WARNING, "Unable to allocate process_info struct");
  932. return;
  933. }
  934. pi->nodeid = nodeid;
  935. pi->pid = pid;
  936. memcpy(&pi->group, name, sizeof(*name));
  937. list_init(&pi->list);
  938. /*
  939. * Insert new process in sorted order so synchronization works properly
  940. */
  941. list_to_add = &process_info_list_head;
  942. for (list = process_info_list_head.next; list != &process_info_list_head; list = list->next) {
  943. pi_entry = list_entry(list, struct process_info, list);
  944. if (pi_entry->nodeid > pi->nodeid ||
  945. (pi_entry->nodeid == pi->nodeid && pi_entry->pid > pi->pid)) {
  946. break;
  947. }
  948. list_to_add = list;
  949. }
  950. list_add (&pi->list, list_to_add);
  951. notify_info.pid = pi->pid;
  952. notify_info.nodeid = nodeid;
  953. notify_info.reason = reason;
  954. notify_lib_joinlist(&pi->group, NULL,
  955. 1, &notify_info,
  956. 0, NULL,
  957. MESSAGE_RES_CPG_CONFCHG_CALLBACK);
  958. }
  959. static void do_proc_leave(
  960. const mar_cpg_name_t *name,
  961. uint32_t pid,
  962. unsigned int nodeid,
  963. int reason)
  964. {
  965. struct process_info *pi;
  966. struct list_head *iter;
  967. mar_cpg_address_t notify_info;
  968. notify_info.pid = pid;
  969. notify_info.nodeid = nodeid;
  970. notify_info.reason = reason;
  971. notify_lib_joinlist(name, NULL,
  972. 0, NULL,
  973. 1, &notify_info,
  974. MESSAGE_RES_CPG_CONFCHG_CALLBACK);
  975. for (iter = process_info_list_head.next; iter != &process_info_list_head; ) {
  976. pi = list_entry(iter, struct process_info, list);
  977. iter = iter->next;
  978. if (pi->pid == pid && pi->nodeid == nodeid &&
  979. mar_name_compare (&pi->group, name)==0) {
  980. list_del (&pi->list);
  981. free (pi);
  982. }
  983. }
  984. }
  985. static void message_handler_req_exec_cpg_downlist_old (
  986. const void *message,
  987. unsigned int nodeid)
  988. {
  989. log_printf (LOGSYS_LEVEL_WARNING, "downlist OLD from node %d",
  990. nodeid);
  991. }
  992. static void message_handler_req_exec_cpg_downlist(
  993. const void *message,
  994. unsigned int nodeid)
  995. {
  996. const struct req_exec_cpg_downlist *req_exec_cpg_downlist = message;
  997. int i;
  998. struct list_head *iter;
  999. struct downlist_msg *stored_msg;
  1000. int found;
  1001. if (downlist_state != CPG_DOWNLIST_WAITING_FOR_MESSAGES) {
  1002. log_printf (LOGSYS_LEVEL_WARNING, "downlist left_list: %d received in state %d",
  1003. req_exec_cpg_downlist->left_nodes, downlist_state);
  1004. return;
  1005. }
  1006. stored_msg = malloc (sizeof (struct downlist_msg));
  1007. stored_msg->sender_nodeid = nodeid;
  1008. stored_msg->old_members = req_exec_cpg_downlist->old_members;
  1009. stored_msg->left_nodes = req_exec_cpg_downlist->left_nodes;
  1010. memcpy (stored_msg->nodeids, req_exec_cpg_downlist->nodeids,
  1011. req_exec_cpg_downlist->left_nodes * sizeof (mar_uint32_t));
  1012. list_init (&stored_msg->list);
  1013. list_add (&stored_msg->list, &downlist_messages_head);
  1014. for (i = 0; i < my_member_list_entries; i++) {
  1015. found = 0;
  1016. for (iter = downlist_messages_head.next;
  1017. iter != &downlist_messages_head;
  1018. iter = iter->next) {
  1019. stored_msg = list_entry(iter, struct downlist_msg, list);
  1020. if (my_member_list[i] == stored_msg->sender_nodeid) {
  1021. found = 1;
  1022. }
  1023. }
  1024. if (!found) {
  1025. return;
  1026. }
  1027. }
  1028. downlist_master_choose_and_send ();
  1029. }
  1030. static void message_handler_req_exec_cpg_procjoin (
  1031. const void *message,
  1032. unsigned int nodeid)
  1033. {
  1034. const struct req_exec_cpg_procjoin *req_exec_cpg_procjoin = message;
  1035. log_printf(LOGSYS_LEVEL_DEBUG, "got procjoin message from cluster node %d (%s) for pid %u",
  1036. nodeid,
  1037. api->totem_ifaces_print(nodeid),
  1038. (unsigned int)req_exec_cpg_procjoin->pid);
  1039. do_proc_join (&req_exec_cpg_procjoin->group_name,
  1040. req_exec_cpg_procjoin->pid, nodeid,
  1041. CONFCHG_CPG_REASON_JOIN);
  1042. }
  1043. static void message_handler_req_exec_cpg_procleave (
  1044. const void *message,
  1045. unsigned int nodeid)
  1046. {
  1047. const struct req_exec_cpg_procjoin *req_exec_cpg_procjoin = message;
  1048. log_printf(LOGSYS_LEVEL_DEBUG, "got procleave message from cluster node %x (%s) for pid %u",
  1049. nodeid,
  1050. api->totem_ifaces_print(nodeid),
  1051. (unsigned int)req_exec_cpg_procjoin->pid);
  1052. do_proc_leave (&req_exec_cpg_procjoin->group_name,
  1053. req_exec_cpg_procjoin->pid, nodeid,
  1054. req_exec_cpg_procjoin->reason);
  1055. }
  1056. /* Got a proclist from another node */
  1057. static void message_handler_req_exec_cpg_joinlist (
  1058. const void *message_v,
  1059. unsigned int nodeid)
  1060. {
  1061. const char *message = message_v;
  1062. const coroipc_response_header_t *res = (const coroipc_response_header_t *)message;
  1063. const struct join_list_entry *jle = (const struct join_list_entry *)(message + sizeof(coroipc_response_header_t));
  1064. struct joinlist_msg *stored_msg;
  1065. log_printf(LOGSYS_LEVEL_DEBUG, "got joinlist message from node %x\n",
  1066. nodeid);
  1067. while ((const char*)jle < message + res->size) {
  1068. stored_msg = malloc (sizeof (struct joinlist_msg));
  1069. memset(stored_msg, 0, sizeof (struct joinlist_msg));
  1070. stored_msg->sender_nodeid = nodeid;
  1071. stored_msg->pid = jle->pid;
  1072. memcpy(&stored_msg->group_name, &jle->group_name, sizeof(mar_cpg_name_t));
  1073. list_init (&stored_msg->list);
  1074. list_add (&stored_msg->list, &joinlist_messages_head);
  1075. jle++;
  1076. }
  1077. }
  1078. static void message_handler_req_exec_cpg_mcast (
  1079. const void *message,
  1080. unsigned int nodeid)
  1081. {
  1082. const struct req_exec_cpg_mcast *req_exec_cpg_mcast = message;
  1083. struct res_lib_cpg_deliver_callback res_lib_cpg_mcast;
  1084. int msglen = req_exec_cpg_mcast->msglen;
  1085. struct list_head *iter, *pi_iter;
  1086. struct cpg_pd *cpd;
  1087. struct iovec iovec[2];
  1088. int known_node = 0;
  1089. res_lib_cpg_mcast.header.id = MESSAGE_RES_CPG_DELIVER_CALLBACK;
  1090. res_lib_cpg_mcast.header.size = sizeof(res_lib_cpg_mcast) + msglen;
  1091. res_lib_cpg_mcast.msglen = msglen;
  1092. res_lib_cpg_mcast.pid = req_exec_cpg_mcast->pid;
  1093. res_lib_cpg_mcast.nodeid = nodeid;
  1094. memcpy(&res_lib_cpg_mcast.group_name, &req_exec_cpg_mcast->group_name,
  1095. sizeof(mar_cpg_name_t));
  1096. iovec[0].iov_base = (void *)&res_lib_cpg_mcast;
  1097. iovec[0].iov_len = sizeof (res_lib_cpg_mcast);
  1098. iovec[1].iov_base = (char*)message+sizeof(*req_exec_cpg_mcast);
  1099. iovec[1].iov_len = msglen;
  1100. for (iter = cpg_pd_list_head.next; iter != &cpg_pd_list_head; ) {
  1101. cpd = list_entry(iter, struct cpg_pd, list);
  1102. iter = iter->next;
  1103. if ((cpd->cpd_state == CPD_STATE_LEAVE_STARTED || cpd->cpd_state == CPD_STATE_JOIN_COMPLETED)
  1104. && (mar_name_compare (&cpd->group_name, &req_exec_cpg_mcast->group_name) == 0)) {
  1105. if (!known_node) {
  1106. /* Try to find, if we know the node */
  1107. for (pi_iter = process_info_list_head.next;
  1108. pi_iter != &process_info_list_head; pi_iter = pi_iter->next) {
  1109. struct process_info *pi = list_entry (pi_iter, struct process_info, list);
  1110. if (pi->nodeid == nodeid &&
  1111. mar_name_compare (&pi->group, &req_exec_cpg_mcast->group_name) == 0) {
  1112. known_node = 1;
  1113. break;
  1114. }
  1115. }
  1116. }
  1117. if (!known_node) {
  1118. /* Unknown node -> we will not deliver message */
  1119. return ;
  1120. }
  1121. api->ipc_dispatch_iov_send (cpd->conn, iovec, 2);
  1122. }
  1123. }
  1124. }
  1125. static int cpg_exec_send_downlist(void)
  1126. {
  1127. struct iovec iov;
  1128. g_req_exec_cpg_downlist.header.id = SERVICE_ID_MAKE(CPG_SERVICE, MESSAGE_REQ_EXEC_CPG_DOWNLIST);
  1129. g_req_exec_cpg_downlist.header.size = sizeof(struct req_exec_cpg_downlist);
  1130. g_req_exec_cpg_downlist.old_members = my_old_member_list_entries;
  1131. iov.iov_base = (void *)&g_req_exec_cpg_downlist;
  1132. iov.iov_len = g_req_exec_cpg_downlist.header.size;
  1133. return (api->totem_mcast (&iov, 1, TOTEM_AGREED));
  1134. }
  1135. static int cpg_exec_send_joinlist(void)
  1136. {
  1137. int count = 0;
  1138. struct list_head *iter;
  1139. coroipc_response_header_t *res;
  1140. char *buf;
  1141. struct join_list_entry *jle;
  1142. struct iovec req_exec_cpg_iovec;
  1143. for (iter = process_info_list_head.next; iter != &process_info_list_head; iter = iter->next) {
  1144. struct process_info *pi = list_entry (iter, struct process_info, list);
  1145. if (pi->nodeid == api->totem_nodeid_get ()) {
  1146. count++;
  1147. }
  1148. }
  1149. /* Nothing to send */
  1150. if (!count)
  1151. return 0;
  1152. buf = alloca(sizeof(coroipc_response_header_t) + sizeof(struct join_list_entry) * count);
  1153. if (!buf) {
  1154. log_printf(LOGSYS_LEVEL_WARNING, "Unable to allocate joinlist buffer");
  1155. return -1;
  1156. }
  1157. jle = (struct join_list_entry *)(buf + sizeof(coroipc_response_header_t));
  1158. res = (coroipc_response_header_t *)buf;
  1159. for (iter = process_info_list_head.next; iter != &process_info_list_head; iter = iter->next) {
  1160. struct process_info *pi = list_entry (iter, struct process_info, list);
  1161. if (pi->nodeid == api->totem_nodeid_get ()) {
  1162. memcpy (&jle->group_name, &pi->group, sizeof (mar_cpg_name_t));
  1163. jle->pid = pi->pid;
  1164. jle++;
  1165. }
  1166. }
  1167. res->id = SERVICE_ID_MAKE(CPG_SERVICE, MESSAGE_REQ_EXEC_CPG_JOINLIST);
  1168. res->size = sizeof(coroipc_response_header_t)+sizeof(struct join_list_entry) * count;
  1169. req_exec_cpg_iovec.iov_base = buf;
  1170. req_exec_cpg_iovec.iov_len = res->size;
  1171. return (api->totem_mcast (&req_exec_cpg_iovec, 1, TOTEM_AGREED));
  1172. }
  1173. static int cpg_lib_init_fn (void *conn)
  1174. {
  1175. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1176. memset (cpd, 0, sizeof(struct cpg_pd));
  1177. cpd->conn = conn;
  1178. list_add (&cpd->list, &cpg_pd_list_head);
  1179. list_init (&cpd->iteration_instance_list_head);
  1180. api->ipc_refcnt_inc (conn);
  1181. log_printf(LOGSYS_LEVEL_DEBUG, "lib_init_fn: conn=%p, cpd=%p\n", conn, cpd);
  1182. return (0);
  1183. }
  1184. /* Join message from the library */
  1185. static void message_handler_req_lib_cpg_join (void *conn, const void *message)
  1186. {
  1187. const struct req_lib_cpg_join *req_lib_cpg_join = message;
  1188. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1189. struct cpg_pd tmp_cpd;
  1190. struct res_lib_cpg_join res_lib_cpg_join;
  1191. cs_error_t error = CPG_OK;
  1192. struct list_head *iter;
  1193. int result;
  1194. /* Test, if we don't have same pid and group name joined */
  1195. for (iter = cpg_pd_list_head.next; iter != &cpg_pd_list_head; iter = iter->next) {
  1196. struct cpg_pd *cpd_item = list_entry (iter, struct cpg_pd, list);
  1197. if (cpd_item->pid == req_lib_cpg_join->pid &&
  1198. mar_name_compare(&req_lib_cpg_join->group_name, &cpd_item->group_name) == 0) {
  1199. /* We have same pid and group name joined -> return error */
  1200. error = CPG_ERR_EXIST;
  1201. goto response_send;
  1202. }
  1203. }
  1204. /*
  1205. * Same check must be done in process info list, because there may be not yet delivered
  1206. * leave of client.
  1207. */
  1208. for (iter = process_info_list_head.next; iter != &process_info_list_head; iter = iter->next) {
  1209. struct process_info *pi = list_entry (iter, struct process_info, list);
  1210. if (pi->nodeid == api->totem_nodeid_get () && pi->pid == req_lib_cpg_join->pid &&
  1211. mar_name_compare(&req_lib_cpg_join->group_name, &pi->group) == 0) {
  1212. /* We have same pid and group name joined -> return error */
  1213. error = CPG_ERR_TRY_AGAIN;
  1214. goto response_send;
  1215. }
  1216. }
  1217. if (req_lib_cpg_join->group_name.length > CPG_MAX_NAME_LENGTH) {
  1218. error = CS_ERR_NAME_TOO_LONG;
  1219. goto response_send;
  1220. }
  1221. switch (cpd->cpd_state) {
  1222. case CPD_STATE_UNJOINED:
  1223. error = CPG_OK;
  1224. /*
  1225. * Make copy of cpd, to restore if cpg_node_joinleave_send fails
  1226. */
  1227. memcpy (&tmp_cpd, cpd, sizeof(tmp_cpd));
  1228. cpd->cpd_state = CPD_STATE_JOIN_STARTED;
  1229. cpd->pid = req_lib_cpg_join->pid;
  1230. cpd->flags = req_lib_cpg_join->flags;
  1231. memcpy (&cpd->group_name, &req_lib_cpg_join->group_name,
  1232. sizeof (cpd->group_name));
  1233. result = cpg_node_joinleave_send (req_lib_cpg_join->pid,
  1234. &req_lib_cpg_join->group_name,
  1235. MESSAGE_REQ_EXEC_CPG_PROCJOIN, CONFCHG_CPG_REASON_JOIN);
  1236. if (result == -1) {
  1237. error = CPG_ERR_TRY_AGAIN;
  1238. /*
  1239. * Restore cpd
  1240. */
  1241. memcpy (cpd, &tmp_cpd, sizeof(tmp_cpd));
  1242. goto response_send;
  1243. }
  1244. break;
  1245. case CPD_STATE_LEAVE_STARTED:
  1246. error = CPG_ERR_BUSY;
  1247. break;
  1248. case CPD_STATE_JOIN_STARTED:
  1249. error = CPG_ERR_EXIST;
  1250. break;
  1251. case CPD_STATE_JOIN_COMPLETED:
  1252. error = CPG_ERR_EXIST;
  1253. break;
  1254. }
  1255. response_send:
  1256. res_lib_cpg_join.header.size = sizeof(res_lib_cpg_join);
  1257. res_lib_cpg_join.header.id = MESSAGE_RES_CPG_JOIN;
  1258. res_lib_cpg_join.header.error = error;
  1259. api->ipc_response_send (conn, &res_lib_cpg_join, sizeof(res_lib_cpg_join));
  1260. }
  1261. /* Leave message from the library */
  1262. static void message_handler_req_lib_cpg_leave (void *conn, const void *message)
  1263. {
  1264. struct res_lib_cpg_leave res_lib_cpg_leave;
  1265. cs_error_t error = CPG_OK;
  1266. struct req_lib_cpg_leave *req_lib_cpg_leave = (struct req_lib_cpg_leave *)message;
  1267. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1268. int result;
  1269. log_printf(LOGSYS_LEVEL_DEBUG, "got leave request on %p\n", conn);
  1270. switch (cpd->cpd_state) {
  1271. case CPD_STATE_UNJOINED:
  1272. error = CPG_ERR_NOT_EXIST;
  1273. break;
  1274. case CPD_STATE_LEAVE_STARTED:
  1275. error = CPG_ERR_NOT_EXIST;
  1276. break;
  1277. case CPD_STATE_JOIN_STARTED:
  1278. error = CPG_ERR_BUSY;
  1279. break;
  1280. case CPD_STATE_JOIN_COMPLETED:
  1281. error = CPG_OK;
  1282. cpd->cpd_state = CPD_STATE_LEAVE_STARTED;
  1283. result = cpg_node_joinleave_send (req_lib_cpg_leave->pid,
  1284. &req_lib_cpg_leave->group_name,
  1285. MESSAGE_REQ_EXEC_CPG_PROCLEAVE,
  1286. CONFCHG_CPG_REASON_LEAVE);
  1287. if (result == -1) {
  1288. error = CPG_ERR_TRY_AGAIN;
  1289. cpd->cpd_state = CPD_STATE_JOIN_COMPLETED;
  1290. }
  1291. break;
  1292. }
  1293. /* send return */
  1294. res_lib_cpg_leave.header.size = sizeof(res_lib_cpg_leave);
  1295. res_lib_cpg_leave.header.id = MESSAGE_RES_CPG_LEAVE;
  1296. res_lib_cpg_leave.header.error = error;
  1297. api->ipc_response_send(conn, &res_lib_cpg_leave, sizeof(res_lib_cpg_leave));
  1298. }
  1299. /* Finalize message from library */
  1300. static void message_handler_req_lib_cpg_finalize (
  1301. void *conn,
  1302. const void *message)
  1303. {
  1304. struct res_lib_cpg_finalize res_lib_cpg_finalize;
  1305. cs_error_t error = CS_OK;
  1306. log_printf (LOGSYS_LEVEL_DEBUG, "cpg finalize for conn=%p\n", conn);
  1307. res_lib_cpg_finalize.header.size = sizeof (res_lib_cpg_finalize);
  1308. res_lib_cpg_finalize.header.id = MESSAGE_RES_CPG_FINALIZE;
  1309. res_lib_cpg_finalize.header.error = error;
  1310. api->ipc_response_send (conn, &res_lib_cpg_finalize,
  1311. sizeof (res_lib_cpg_finalize));
  1312. }
  1313. /* Mcast message from the library */
  1314. static void message_handler_req_lib_cpg_mcast (void *conn, const void *message)
  1315. {
  1316. const struct req_lib_cpg_mcast *req_lib_cpg_mcast = message;
  1317. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1318. mar_cpg_name_t group_name = cpd->group_name;
  1319. struct iovec req_exec_cpg_iovec[2];
  1320. struct req_exec_cpg_mcast req_exec_cpg_mcast;
  1321. struct res_lib_cpg_mcast res_lib_cpg_mcast;
  1322. int msglen = req_lib_cpg_mcast->msglen;
  1323. int result;
  1324. cs_error_t error = CPG_ERR_NOT_EXIST;
  1325. TRACE1("got mcast request on %p\n", conn);
  1326. switch (cpd->cpd_state) {
  1327. case CPD_STATE_UNJOINED:
  1328. error = CPG_ERR_NOT_EXIST;
  1329. break;
  1330. case CPD_STATE_LEAVE_STARTED:
  1331. error = CPG_ERR_NOT_EXIST;
  1332. break;
  1333. case CPD_STATE_JOIN_STARTED:
  1334. error = CPG_OK;
  1335. break;
  1336. case CPD_STATE_JOIN_COMPLETED:
  1337. error = CPG_OK;
  1338. break;
  1339. }
  1340. if (error == CPG_OK) {
  1341. req_exec_cpg_mcast.header.size = sizeof(req_exec_cpg_mcast) + msglen;
  1342. req_exec_cpg_mcast.header.id = SERVICE_ID_MAKE(CPG_SERVICE,
  1343. MESSAGE_REQ_EXEC_CPG_MCAST);
  1344. req_exec_cpg_mcast.pid = cpd->pid;
  1345. req_exec_cpg_mcast.msglen = msglen;
  1346. api->ipc_source_set (&req_exec_cpg_mcast.source, conn);
  1347. memcpy(&req_exec_cpg_mcast.group_name, &group_name,
  1348. sizeof(mar_cpg_name_t));
  1349. req_exec_cpg_iovec[0].iov_base = (char *)&req_exec_cpg_mcast;
  1350. req_exec_cpg_iovec[0].iov_len = sizeof(req_exec_cpg_mcast);
  1351. req_exec_cpg_iovec[1].iov_base = (char *)&req_lib_cpg_mcast->message;
  1352. req_exec_cpg_iovec[1].iov_len = msglen;
  1353. result = api->totem_mcast (req_exec_cpg_iovec, 2, TOTEM_AGREED);
  1354. if (result == -1) {
  1355. error = CPG_ERR_TRY_AGAIN;
  1356. }
  1357. }
  1358. res_lib_cpg_mcast.header.size = sizeof(res_lib_cpg_mcast);
  1359. res_lib_cpg_mcast.header.id = MESSAGE_RES_CPG_MCAST;
  1360. res_lib_cpg_mcast.header.error = error;
  1361. api->ipc_response_send (conn, &res_lib_cpg_mcast,
  1362. sizeof (res_lib_cpg_mcast));
  1363. }
  1364. static void message_handler_req_lib_cpg_membership (void *conn,
  1365. const void *message)
  1366. {
  1367. struct req_lib_cpg_membership_get *req_lib_cpg_membership_get =
  1368. (struct req_lib_cpg_membership_get *)message;
  1369. struct res_lib_cpg_membership_get res_lib_cpg_membership_get;
  1370. struct list_head *iter;
  1371. int member_count = 0;
  1372. res_lib_cpg_membership_get.header.id = MESSAGE_RES_CPG_MEMBERSHIP;
  1373. res_lib_cpg_membership_get.header.error = CPG_OK;
  1374. res_lib_cpg_membership_get.header.size =
  1375. sizeof (struct res_lib_cpg_membership_get);
  1376. for (iter = process_info_list_head.next;
  1377. iter != &process_info_list_head; iter = iter->next) {
  1378. struct process_info *pi = list_entry (iter, struct process_info, list);
  1379. if (mar_name_compare (&pi->group, &req_lib_cpg_membership_get->group_name) == 0) {
  1380. res_lib_cpg_membership_get.member_list[member_count].nodeid = pi->nodeid;
  1381. res_lib_cpg_membership_get.member_list[member_count].pid = pi->pid;
  1382. member_count += 1;
  1383. }
  1384. }
  1385. res_lib_cpg_membership_get.member_count = member_count;
  1386. api->ipc_response_send (conn, &res_lib_cpg_membership_get,
  1387. sizeof (res_lib_cpg_membership_get));
  1388. }
  1389. static void message_handler_req_lib_cpg_local_get (void *conn,
  1390. const void *message)
  1391. {
  1392. struct res_lib_cpg_local_get res_lib_cpg_local_get;
  1393. res_lib_cpg_local_get.header.size = sizeof (res_lib_cpg_local_get);
  1394. res_lib_cpg_local_get.header.id = MESSAGE_RES_CPG_LOCAL_GET;
  1395. res_lib_cpg_local_get.header.error = CPG_OK;
  1396. res_lib_cpg_local_get.local_nodeid = api->totem_nodeid_get ();
  1397. api->ipc_response_send (conn, &res_lib_cpg_local_get,
  1398. sizeof (res_lib_cpg_local_get));
  1399. }
  1400. static void message_handler_req_lib_cpg_iteration_initialize (
  1401. void *conn,
  1402. const void *message)
  1403. {
  1404. const struct req_lib_cpg_iterationinitialize *req_lib_cpg_iterationinitialize = message;
  1405. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1406. hdb_handle_t cpg_iteration_handle = 0;
  1407. struct res_lib_cpg_iterationinitialize res_lib_cpg_iterationinitialize;
  1408. struct list_head *iter, *iter2;
  1409. struct cpg_iteration_instance *cpg_iteration_instance;
  1410. cs_error_t error = CS_OK;
  1411. int res;
  1412. log_printf (LOGSYS_LEVEL_DEBUG, "cpg iteration initialize\n");
  1413. /* Because between calling this function and *next can be some operations which will
  1414. * change list, we must do full copy.
  1415. */
  1416. /*
  1417. * Create new iteration instance
  1418. */
  1419. res = hdb_handle_create (&cpg_iteration_handle_t_db, sizeof (struct cpg_iteration_instance),
  1420. &cpg_iteration_handle);
  1421. if (res != 0) {
  1422. error = CS_ERR_NO_MEMORY;
  1423. goto response_send;
  1424. }
  1425. res = hdb_handle_get (&cpg_iteration_handle_t_db, cpg_iteration_handle, (void *)&cpg_iteration_instance);
  1426. if (res != 0) {
  1427. error = CS_ERR_BAD_HANDLE;
  1428. goto error_destroy;
  1429. }
  1430. list_init (&cpg_iteration_instance->items_list_head);
  1431. cpg_iteration_instance->handle = cpg_iteration_handle;
  1432. /*
  1433. * Create copy of process_info list "grouped by" group name
  1434. */
  1435. for (iter = process_info_list_head.next; iter != &process_info_list_head; iter = iter->next) {
  1436. struct process_info *pi = list_entry (iter, struct process_info, list);
  1437. struct process_info *new_pi;
  1438. if (req_lib_cpg_iterationinitialize->iteration_type == CPG_ITERATION_NAME_ONLY) {
  1439. /*
  1440. * Try to find processed group name in our list new list
  1441. */
  1442. int found = 0;
  1443. for (iter2 = cpg_iteration_instance->items_list_head.next;
  1444. iter2 != &cpg_iteration_instance->items_list_head;
  1445. iter2 = iter2->next) {
  1446. struct process_info *pi2 = list_entry (iter2, struct process_info, list);
  1447. if (mar_name_compare (&pi2->group, &pi->group) == 0) {
  1448. found = 1;
  1449. break;
  1450. }
  1451. }
  1452. if (found) {
  1453. /*
  1454. * We have this name in list -> don't add
  1455. */
  1456. continue ;
  1457. }
  1458. } else if (req_lib_cpg_iterationinitialize->iteration_type == CPG_ITERATION_ONE_GROUP) {
  1459. /*
  1460. * Test pi group name with request
  1461. */
  1462. if (mar_name_compare (&pi->group, &req_lib_cpg_iterationinitialize->group_name) != 0)
  1463. /*
  1464. * Not same -> don't add
  1465. */
  1466. continue ;
  1467. }
  1468. new_pi = malloc (sizeof (struct process_info));
  1469. if (!new_pi) {
  1470. log_printf(LOGSYS_LEVEL_WARNING, "Unable to allocate process_info struct");
  1471. error = CS_ERR_NO_MEMORY;
  1472. goto error_put_destroy;
  1473. }
  1474. memcpy (new_pi, pi, sizeof (struct process_info));
  1475. list_init (&new_pi->list);
  1476. if (req_lib_cpg_iterationinitialize->iteration_type == CPG_ITERATION_NAME_ONLY) {
  1477. /*
  1478. * pid and nodeid -> undefined
  1479. */
  1480. new_pi->pid = new_pi->nodeid = 0;
  1481. }
  1482. /*
  1483. * We will return list "grouped" by "group name", so try to find right place to add
  1484. */
  1485. for (iter2 = cpg_iteration_instance->items_list_head.next;
  1486. iter2 != &cpg_iteration_instance->items_list_head;
  1487. iter2 = iter2->next) {
  1488. struct process_info *pi2 = list_entry (iter2, struct process_info, list);
  1489. if (mar_name_compare (&pi2->group, &pi->group) == 0) {
  1490. break;
  1491. }
  1492. }
  1493. list_add (&new_pi->list, iter2);
  1494. }
  1495. /*
  1496. * Now we have a full "grouped by" copy of process_info list
  1497. */
  1498. /*
  1499. * Add instance to current cpd list
  1500. */
  1501. list_init (&cpg_iteration_instance->list);
  1502. list_add (&cpg_iteration_instance->list, &cpd->iteration_instance_list_head);
  1503. cpg_iteration_instance->current_pointer = &cpg_iteration_instance->items_list_head;
  1504. error_put_destroy:
  1505. hdb_handle_put (&cpg_iteration_handle_t_db, cpg_iteration_handle);
  1506. error_destroy:
  1507. if (error != CS_OK) {
  1508. hdb_handle_destroy (&cpg_iteration_handle_t_db, cpg_iteration_handle);
  1509. }
  1510. response_send:
  1511. res_lib_cpg_iterationinitialize.header.size = sizeof (res_lib_cpg_iterationinitialize);
  1512. res_lib_cpg_iterationinitialize.header.id = MESSAGE_RES_CPG_ITERATIONINITIALIZE;
  1513. res_lib_cpg_iterationinitialize.header.error = error;
  1514. res_lib_cpg_iterationinitialize.iteration_handle = cpg_iteration_handle;
  1515. api->ipc_response_send (conn, &res_lib_cpg_iterationinitialize,
  1516. sizeof (res_lib_cpg_iterationinitialize));
  1517. }
  1518. static void message_handler_req_lib_cpg_iteration_next (
  1519. void *conn,
  1520. const void *message)
  1521. {
  1522. const struct req_lib_cpg_iterationnext *req_lib_cpg_iterationnext = message;
  1523. struct res_lib_cpg_iterationnext res_lib_cpg_iterationnext;
  1524. struct cpg_iteration_instance *cpg_iteration_instance;
  1525. cs_error_t error = CS_OK;
  1526. int res;
  1527. struct process_info *pi;
  1528. log_printf (LOGSYS_LEVEL_DEBUG, "cpg iteration next\n");
  1529. res = hdb_handle_get (&cpg_iteration_handle_t_db,
  1530. req_lib_cpg_iterationnext->iteration_handle,
  1531. (void *)&cpg_iteration_instance);
  1532. if (res != 0) {
  1533. error = CS_ERR_LIBRARY;
  1534. goto error_exit;
  1535. }
  1536. assert (cpg_iteration_instance);
  1537. cpg_iteration_instance->current_pointer = cpg_iteration_instance->current_pointer->next;
  1538. if (cpg_iteration_instance->current_pointer == &cpg_iteration_instance->items_list_head) {
  1539. error = CS_ERR_NO_SECTIONS;
  1540. goto error_put;
  1541. }
  1542. pi = list_entry (cpg_iteration_instance->current_pointer, struct process_info, list);
  1543. /*
  1544. * Copy iteration data
  1545. */
  1546. res_lib_cpg_iterationnext.description.nodeid = pi->nodeid;
  1547. res_lib_cpg_iterationnext.description.pid = pi->pid;
  1548. memcpy (&res_lib_cpg_iterationnext.description.group,
  1549. &pi->group,
  1550. sizeof (mar_cpg_name_t));
  1551. error_put:
  1552. hdb_handle_put (&cpg_iteration_handle_t_db, req_lib_cpg_iterationnext->iteration_handle);
  1553. error_exit:
  1554. res_lib_cpg_iterationnext.header.size = sizeof (res_lib_cpg_iterationnext);
  1555. res_lib_cpg_iterationnext.header.id = MESSAGE_RES_CPG_ITERATIONNEXT;
  1556. res_lib_cpg_iterationnext.header.error = error;
  1557. api->ipc_response_send (conn, &res_lib_cpg_iterationnext,
  1558. sizeof (res_lib_cpg_iterationnext));
  1559. }
  1560. static void message_handler_req_lib_cpg_iteration_finalize (
  1561. void *conn,
  1562. const void *message)
  1563. {
  1564. const struct req_lib_cpg_iterationfinalize *req_lib_cpg_iterationfinalize = message;
  1565. struct res_lib_cpg_iterationfinalize res_lib_cpg_iterationfinalize;
  1566. struct cpg_iteration_instance *cpg_iteration_instance;
  1567. cs_error_t error = CS_OK;
  1568. int res;
  1569. log_printf (LOGSYS_LEVEL_DEBUG, "cpg iteration finalize\n");
  1570. res = hdb_handle_get (&cpg_iteration_handle_t_db,
  1571. req_lib_cpg_iterationfinalize->iteration_handle,
  1572. (void *)&cpg_iteration_instance);
  1573. if (res != 0) {
  1574. error = CS_ERR_LIBRARY;
  1575. goto error_exit;
  1576. }
  1577. assert (cpg_iteration_instance);
  1578. cpg_iteration_instance_finalize (cpg_iteration_instance);
  1579. hdb_handle_put (&cpg_iteration_handle_t_db, cpg_iteration_instance->handle);
  1580. error_exit:
  1581. res_lib_cpg_iterationfinalize.header.size = sizeof (res_lib_cpg_iterationfinalize);
  1582. res_lib_cpg_iterationfinalize.header.id = MESSAGE_RES_CPG_ITERATIONFINALIZE;
  1583. res_lib_cpg_iterationfinalize.header.error = error;
  1584. api->ipc_response_send (conn, &res_lib_cpg_iterationfinalize,
  1585. sizeof (res_lib_cpg_iterationfinalize));
  1586. }