cpg.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294
  1. /*
  2. * Copyright (c) 2006-2019 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 <assert.h>
  52. #include <arpa/inet.h>
  53. #include <sys/mman.h>
  54. #include <qb/qblist.h>
  55. #include <qb/qbmap.h>
  56. #include <corosync/corotypes.h>
  57. #include <qb/qbipc_common.h>
  58. #include <corosync/corodefs.h>
  59. #include <corosync/logsys.h>
  60. #include <corosync/coroapi.h>
  61. #include <corosync/cpg.h>
  62. #include <corosync/ipc_cpg.h>
  63. #ifndef MAP_ANONYMOUS
  64. #define MAP_ANONYMOUS MAP_ANON
  65. #endif
  66. #include "service.h"
  67. LOGSYS_DECLARE_SUBSYS ("CPG");
  68. #define GROUP_HASH_SIZE 32
  69. enum cpg_message_req_types {
  70. MESSAGE_REQ_EXEC_CPG_PROCJOIN = 0,
  71. MESSAGE_REQ_EXEC_CPG_PROCLEAVE = 1,
  72. MESSAGE_REQ_EXEC_CPG_JOINLIST = 2,
  73. MESSAGE_REQ_EXEC_CPG_MCAST = 3,
  74. MESSAGE_REQ_EXEC_CPG_DOWNLIST_OLD = 4,
  75. MESSAGE_REQ_EXEC_CPG_DOWNLIST = 5,
  76. MESSAGE_REQ_EXEC_CPG_PARTIAL_MCAST = 6,
  77. };
  78. struct zcb_mapped {
  79. struct qb_list_head list;
  80. void *addr;
  81. size_t size;
  82. };
  83. /*
  84. * state` exec deliver
  85. * match group name, pid -> if matched deliver for YES:
  86. * XXX indicates impossible state
  87. *
  88. * join leave mcast
  89. * UNJOINED XXX XXX NO
  90. * LEAVE_STARTED XXX YES(unjoined_enter) YES
  91. * JOIN_STARTED YES(join_started_enter) XXX NO
  92. * JOIN_COMPLETED XXX NO YES
  93. *
  94. * join_started_enter
  95. * set JOIN_COMPLETED
  96. * add entry to process_info list
  97. * unjoined_enter
  98. * set UNJOINED
  99. * delete entry from process_info list
  100. *
  101. *
  102. * library accept join error codes
  103. * UNJOINED YES(CS_OK) set JOIN_STARTED
  104. * LEAVE_STARTED NO(CS_ERR_BUSY)
  105. * JOIN_STARTED NO(CS_ERR_EXIST)
  106. * JOIN_COMPlETED NO(CS_ERR_EXIST)
  107. *
  108. * library accept leave error codes
  109. * UNJOINED NO(CS_ERR_NOT_EXIST)
  110. * LEAVE_STARTED NO(CS_ERR_NOT_EXIST)
  111. * JOIN_STARTED NO(CS_ERR_BUSY)
  112. * JOIN_COMPLETED YES(CS_OK) set LEAVE_STARTED
  113. *
  114. * library accept mcast
  115. * UNJOINED NO(CS_ERR_NOT_EXIST)
  116. * LEAVE_STARTED NO(CS_ERR_NOT_EXIST)
  117. * JOIN_STARTED YES(CS_OK)
  118. * JOIN_COMPLETED YES(CS_OK)
  119. */
  120. enum cpd_state {
  121. CPD_STATE_UNJOINED,
  122. CPD_STATE_LEAVE_STARTED,
  123. CPD_STATE_JOIN_STARTED,
  124. CPD_STATE_JOIN_COMPLETED
  125. };
  126. enum cpg_sync_state {
  127. CPGSYNC_DOWNLIST,
  128. CPGSYNC_JOINLIST
  129. };
  130. static struct qb_list_head joinlist_messages_head;
  131. struct cpg_pd {
  132. void *conn;
  133. mar_cpg_name_t group_name;
  134. uint32_t pid;
  135. enum cpd_state cpd_state;
  136. unsigned int flags;
  137. int initial_totem_conf_sent;
  138. uint64_t transition_counter; /* These two are used when sending fragmented messages */
  139. uint64_t initial_transition_counter;
  140. struct qb_list_head list;
  141. struct qb_list_head iteration_instance_list_head;
  142. struct qb_list_head zcb_mapped_list_head;
  143. };
  144. struct cpg_iteration_instance {
  145. hdb_handle_t handle;
  146. struct qb_list_head list;
  147. struct qb_list_head items_list_head; /* List of process_info */
  148. struct qb_list_head *current_pointer;
  149. };
  150. DECLARE_HDB_DATABASE(cpg_iteration_handle_t_db,NULL);
  151. QB_LIST_DECLARE (cpg_pd_list_head);
  152. static unsigned int my_member_list[PROCESSOR_COUNT_MAX];
  153. static unsigned int my_member_list_entries;
  154. static unsigned int my_old_member_list[PROCESSOR_COUNT_MAX];
  155. static unsigned int my_old_member_list_entries = 0;
  156. static struct corosync_api_v1 *api = NULL;
  157. static enum cpg_sync_state my_sync_state = CPGSYNC_DOWNLIST;
  158. static mar_cpg_ring_id_t last_sync_ring_id;
  159. struct process_info {
  160. unsigned int nodeid;
  161. uint32_t pid;
  162. mar_cpg_name_t group;
  163. struct qb_list_head list; /* on the group_info members list */
  164. };
  165. QB_LIST_DECLARE (process_info_list_head);
  166. struct join_list_entry {
  167. uint32_t pid;
  168. mar_cpg_name_t group_name;
  169. };
  170. struct join_list_confchg_data {
  171. mar_cpg_name_t cpg_group;
  172. mar_cpg_address_t join_list[CPG_MEMBERS_MAX];
  173. int join_list_entries;
  174. };
  175. /*
  176. * Service Interfaces required by service_message_handler struct
  177. */
  178. static char *cpg_exec_init_fn (struct corosync_api_v1 *);
  179. static int cpg_lib_init_fn (void *conn);
  180. static int cpg_lib_exit_fn (void *conn);
  181. static void message_handler_req_exec_cpg_procjoin (
  182. const void *message,
  183. unsigned int nodeid);
  184. static void message_handler_req_exec_cpg_procleave (
  185. const void *message,
  186. unsigned int nodeid);
  187. static void message_handler_req_exec_cpg_joinlist (
  188. const void *message,
  189. unsigned int nodeid);
  190. static void message_handler_req_exec_cpg_mcast (
  191. const void *message,
  192. unsigned int nodeid);
  193. static void message_handler_req_exec_cpg_partial_mcast (
  194. const void *message,
  195. unsigned int nodeid);
  196. static void message_handler_req_exec_cpg_downlist_old (
  197. const void *message,
  198. unsigned int nodeid);
  199. static void message_handler_req_exec_cpg_downlist (
  200. const void *message,
  201. unsigned int nodeid);
  202. static void exec_cpg_procjoin_endian_convert (void *msg);
  203. static void exec_cpg_joinlist_endian_convert (void *msg);
  204. static void exec_cpg_mcast_endian_convert (void *msg);
  205. static void exec_cpg_partial_mcast_endian_convert (void *msg);
  206. static void exec_cpg_downlist_endian_convert_old (void *msg);
  207. static void exec_cpg_downlist_endian_convert (void *msg);
  208. static void message_handler_req_lib_cpg_join (void *conn, const void *message);
  209. static void message_handler_req_lib_cpg_leave (void *conn, const void *message);
  210. static void message_handler_req_lib_cpg_finalize (void *conn, const void *message);
  211. static void message_handler_req_lib_cpg_mcast (void *conn, const void *message);
  212. static void message_handler_req_lib_cpg_partial_mcast (void *conn, const void *message);
  213. static void message_handler_req_lib_cpg_membership (void *conn,
  214. const void *message);
  215. static void message_handler_req_lib_cpg_local_get (void *conn,
  216. const void *message);
  217. static void message_handler_req_lib_cpg_iteration_initialize (
  218. void *conn,
  219. const void *message);
  220. static void message_handler_req_lib_cpg_iteration_next (
  221. void *conn,
  222. const void *message);
  223. static void message_handler_req_lib_cpg_iteration_finalize (
  224. void *conn,
  225. const void *message);
  226. static void message_handler_req_lib_cpg_zc_alloc (
  227. void *conn,
  228. const void *message);
  229. static void message_handler_req_lib_cpg_zc_free (
  230. void *conn,
  231. const void *message);
  232. static void message_handler_req_lib_cpg_zc_execute (
  233. void *conn,
  234. const void *message);
  235. static int cpg_node_joinleave_send (unsigned int pid, const mar_cpg_name_t *group_name, int fn, int reason);
  236. static int cpg_exec_send_downlist(void);
  237. static int cpg_exec_send_joinlist(void);
  238. static void downlist_inform_clients (void);
  239. static void joinlist_inform_clients (void);
  240. static void joinlist_messages_delete (void);
  241. static void cpg_sync_init (
  242. const unsigned int *trans_list,
  243. size_t trans_list_entries,
  244. const unsigned int *member_list,
  245. size_t member_list_entries,
  246. const struct memb_ring_id *ring_id);
  247. static int cpg_sync_process (void);
  248. static void cpg_sync_activate (void);
  249. static void cpg_sync_abort (void);
  250. static void do_proc_join(
  251. const mar_cpg_name_t *name,
  252. uint32_t pid,
  253. unsigned int nodeid,
  254. int reason,
  255. qb_map_t *group_notify_map);
  256. static void do_proc_leave(
  257. const mar_cpg_name_t *name,
  258. uint32_t pid,
  259. unsigned int nodeid,
  260. int reason);
  261. static int notify_lib_totem_membership (
  262. void *conn,
  263. int member_list_entries,
  264. const unsigned int *member_list);
  265. static inline int zcb_all_free (
  266. struct cpg_pd *cpd);
  267. static char *cpg_print_group_name (
  268. const mar_cpg_name_t *group);
  269. /*
  270. * Library Handler Definition
  271. */
  272. static struct corosync_lib_handler cpg_lib_engine[] =
  273. {
  274. { /* 0 - MESSAGE_REQ_CPG_JOIN */
  275. .lib_handler_fn = message_handler_req_lib_cpg_join,
  276. .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
  277. },
  278. { /* 1 - MESSAGE_REQ_CPG_LEAVE */
  279. .lib_handler_fn = message_handler_req_lib_cpg_leave,
  280. .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
  281. },
  282. { /* 2 - MESSAGE_REQ_CPG_MCAST */
  283. .lib_handler_fn = message_handler_req_lib_cpg_mcast,
  284. .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
  285. },
  286. { /* 3 - MESSAGE_REQ_CPG_MEMBERSHIP */
  287. .lib_handler_fn = message_handler_req_lib_cpg_membership,
  288. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  289. },
  290. { /* 4 - MESSAGE_REQ_CPG_LOCAL_GET */
  291. .lib_handler_fn = message_handler_req_lib_cpg_local_get,
  292. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  293. },
  294. { /* 5 - MESSAGE_REQ_CPG_ITERATIONINITIALIZE */
  295. .lib_handler_fn = message_handler_req_lib_cpg_iteration_initialize,
  296. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  297. },
  298. { /* 6 - MESSAGE_REQ_CPG_ITERATIONNEXT */
  299. .lib_handler_fn = message_handler_req_lib_cpg_iteration_next,
  300. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  301. },
  302. { /* 7 - MESSAGE_REQ_CPG_ITERATIONFINALIZE */
  303. .lib_handler_fn = message_handler_req_lib_cpg_iteration_finalize,
  304. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  305. },
  306. { /* 8 - MESSAGE_REQ_CPG_FINALIZE */
  307. .lib_handler_fn = message_handler_req_lib_cpg_finalize,
  308. .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
  309. },
  310. { /* 9 */
  311. .lib_handler_fn = message_handler_req_lib_cpg_zc_alloc,
  312. .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
  313. },
  314. { /* 10 */
  315. .lib_handler_fn = message_handler_req_lib_cpg_zc_free,
  316. .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
  317. },
  318. { /* 11 */
  319. .lib_handler_fn = message_handler_req_lib_cpg_zc_execute,
  320. .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
  321. },
  322. { /* 12 */
  323. .lib_handler_fn = message_handler_req_lib_cpg_partial_mcast,
  324. .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
  325. },
  326. };
  327. static struct corosync_exec_handler cpg_exec_engine[] =
  328. {
  329. { /* 0 - MESSAGE_REQ_EXEC_CPG_PROCJOIN */
  330. .exec_handler_fn = message_handler_req_exec_cpg_procjoin,
  331. .exec_endian_convert_fn = exec_cpg_procjoin_endian_convert
  332. },
  333. { /* 1 - MESSAGE_REQ_EXEC_CPG_PROCLEAVE */
  334. .exec_handler_fn = message_handler_req_exec_cpg_procleave,
  335. .exec_endian_convert_fn = exec_cpg_procjoin_endian_convert
  336. },
  337. { /* 2 - MESSAGE_REQ_EXEC_CPG_JOINLIST */
  338. .exec_handler_fn = message_handler_req_exec_cpg_joinlist,
  339. .exec_endian_convert_fn = exec_cpg_joinlist_endian_convert
  340. },
  341. { /* 3 - MESSAGE_REQ_EXEC_CPG_MCAST */
  342. .exec_handler_fn = message_handler_req_exec_cpg_mcast,
  343. .exec_endian_convert_fn = exec_cpg_mcast_endian_convert
  344. },
  345. { /* 4 - MESSAGE_REQ_EXEC_CPG_DOWNLIST_OLD */
  346. .exec_handler_fn = message_handler_req_exec_cpg_downlist_old,
  347. .exec_endian_convert_fn = exec_cpg_downlist_endian_convert_old
  348. },
  349. { /* 5 - MESSAGE_REQ_EXEC_CPG_DOWNLIST */
  350. .exec_handler_fn = message_handler_req_exec_cpg_downlist,
  351. .exec_endian_convert_fn = exec_cpg_downlist_endian_convert
  352. },
  353. { /* 6 - MESSAGE_REQ_EXEC_CPG_PARTIAL_MCAST */
  354. .exec_handler_fn = message_handler_req_exec_cpg_partial_mcast,
  355. .exec_endian_convert_fn = exec_cpg_partial_mcast_endian_convert
  356. },
  357. };
  358. struct corosync_service_engine cpg_service_engine = {
  359. .name = "corosync cluster closed process group service v1.01",
  360. .id = CPG_SERVICE,
  361. .priority = 1,
  362. .private_data_size = sizeof (struct cpg_pd),
  363. .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED,
  364. .allow_inquorate = CS_LIB_ALLOW_INQUORATE,
  365. .lib_init_fn = cpg_lib_init_fn,
  366. .lib_exit_fn = cpg_lib_exit_fn,
  367. .lib_engine = cpg_lib_engine,
  368. .lib_engine_count = sizeof (cpg_lib_engine) / sizeof (struct corosync_lib_handler),
  369. .exec_init_fn = cpg_exec_init_fn,
  370. .exec_dump_fn = NULL,
  371. .exec_engine = cpg_exec_engine,
  372. .exec_engine_count = sizeof (cpg_exec_engine) / sizeof (struct corosync_exec_handler),
  373. .sync_init = cpg_sync_init,
  374. .sync_process = cpg_sync_process,
  375. .sync_activate = cpg_sync_activate,
  376. .sync_abort = cpg_sync_abort
  377. };
  378. struct corosync_service_engine *cpg_get_service_engine_ver0 (void)
  379. {
  380. return (&cpg_service_engine);
  381. }
  382. struct req_exec_cpg_procjoin {
  383. struct qb_ipc_request_header header __attribute__((aligned(8)));
  384. mar_cpg_name_t group_name __attribute__((aligned(8)));
  385. mar_uint32_t pid __attribute__((aligned(8)));
  386. mar_uint32_t reason __attribute__((aligned(8)));
  387. };
  388. struct req_exec_cpg_mcast {
  389. struct qb_ipc_request_header header __attribute__((aligned(8)));
  390. mar_cpg_name_t group_name __attribute__((aligned(8)));
  391. mar_uint32_t msglen __attribute__((aligned(8)));
  392. mar_uint32_t pid __attribute__((aligned(8)));
  393. mar_message_source_t source __attribute__((aligned(8)));
  394. mar_uint8_t message[] __attribute__((aligned(8)));
  395. };
  396. struct req_exec_cpg_partial_mcast {
  397. struct qb_ipc_request_header header __attribute__((aligned(8)));
  398. mar_cpg_name_t group_name __attribute__((aligned(8)));
  399. mar_uint32_t msglen __attribute__((aligned(8)));
  400. mar_uint32_t fraglen __attribute__((aligned(8)));
  401. mar_uint32_t pid __attribute__((aligned(8)));
  402. mar_uint32_t type __attribute__((aligned(8)));
  403. mar_message_source_t source __attribute__((aligned(8)));
  404. mar_uint8_t message[] __attribute__((aligned(8)));
  405. };
  406. struct req_exec_cpg_downlist_old {
  407. struct qb_ipc_request_header header __attribute__((aligned(8)));
  408. mar_uint32_t left_nodes __attribute__((aligned(8)));
  409. mar_uint32_t nodeids[PROCESSOR_COUNT_MAX] __attribute__((aligned(8)));
  410. };
  411. struct req_exec_cpg_downlist {
  412. struct qb_ipc_request_header header __attribute__((aligned(8)));
  413. /* merge decisions */
  414. mar_uint32_t old_members __attribute__((aligned(8)));
  415. /* downlist below */
  416. mar_uint32_t left_nodes __attribute__((aligned(8)));
  417. mar_uint32_t nodeids[PROCESSOR_COUNT_MAX] __attribute__((aligned(8)));
  418. };
  419. struct joinlist_msg {
  420. mar_uint32_t sender_nodeid;
  421. uint32_t pid;
  422. mar_cpg_name_t group_name;
  423. struct qb_list_head list;
  424. };
  425. static struct req_exec_cpg_downlist g_req_exec_cpg_downlist;
  426. /*
  427. * Function print group name. It's not reentrant
  428. */
  429. static char *cpg_print_group_name(const mar_cpg_name_t *group)
  430. {
  431. static char res[CPG_MAX_NAME_LENGTH * 4 + 1];
  432. int dest_pos = 0;
  433. char c;
  434. int i;
  435. for (i = 0; i < group->length; i++) {
  436. c = group->value[i];
  437. if (c >= ' ' && c < 0x7f && c != '\\') {
  438. res[dest_pos++] = c;
  439. } else {
  440. if (c == '\\') {
  441. res[dest_pos++] = '\\';
  442. res[dest_pos++] = '\\';
  443. } else {
  444. snprintf(res + dest_pos, sizeof(res) - dest_pos, "\\x%02X", c);
  445. dest_pos += 4;
  446. }
  447. }
  448. }
  449. res[dest_pos] = 0;
  450. return (res);
  451. }
  452. static void cpg_sync_init (
  453. const unsigned int *trans_list,
  454. size_t trans_list_entries,
  455. const unsigned int *member_list,
  456. size_t member_list_entries,
  457. const struct memb_ring_id *ring_id)
  458. {
  459. int entries;
  460. int i, j;
  461. int found;
  462. my_sync_state = CPGSYNC_DOWNLIST;
  463. memcpy (my_member_list, member_list, member_list_entries *
  464. sizeof (unsigned int));
  465. my_member_list_entries = member_list_entries;
  466. last_sync_ring_id.nodeid = ring_id->nodeid;
  467. last_sync_ring_id.seq = ring_id->seq;
  468. entries = 0;
  469. /*
  470. * Determine list of nodeids for downlist message
  471. */
  472. for (i = 0; i < my_old_member_list_entries; i++) {
  473. found = 0;
  474. for (j = 0; j < trans_list_entries; j++) {
  475. if (my_old_member_list[i] == trans_list[j]) {
  476. found = 1;
  477. break;
  478. }
  479. }
  480. if (found == 0) {
  481. g_req_exec_cpg_downlist.nodeids[entries++] =
  482. my_old_member_list[i];
  483. }
  484. }
  485. g_req_exec_cpg_downlist.left_nodes = entries;
  486. }
  487. static int cpg_sync_process (void)
  488. {
  489. int res = -1;
  490. if (my_sync_state == CPGSYNC_DOWNLIST) {
  491. res = cpg_exec_send_downlist();
  492. if (res == -1) {
  493. return (-1);
  494. }
  495. my_sync_state = CPGSYNC_JOINLIST;
  496. }
  497. if (my_sync_state == CPGSYNC_JOINLIST) {
  498. res = cpg_exec_send_joinlist();
  499. }
  500. return (res);
  501. }
  502. static void cpg_sync_activate (void)
  503. {
  504. memcpy (my_old_member_list, my_member_list,
  505. my_member_list_entries * sizeof (unsigned int));
  506. my_old_member_list_entries = my_member_list_entries;
  507. downlist_inform_clients ();
  508. joinlist_inform_clients ();
  509. joinlist_messages_delete ();
  510. notify_lib_totem_membership (NULL, my_member_list_entries, my_member_list);
  511. }
  512. static void cpg_sync_abort (void)
  513. {
  514. joinlist_messages_delete ();
  515. }
  516. static int notify_lib_totem_membership (
  517. void *conn,
  518. int member_list_entries,
  519. const unsigned int *member_list)
  520. {
  521. struct qb_list_head *iter;
  522. char *buf;
  523. int size;
  524. struct res_lib_cpg_totem_confchg_callback *res;
  525. size = sizeof(struct res_lib_cpg_totem_confchg_callback) +
  526. sizeof(mar_uint32_t) * (member_list_entries);
  527. buf = alloca(size);
  528. if (!buf)
  529. return CS_ERR_LIBRARY;
  530. res = (struct res_lib_cpg_totem_confchg_callback *)buf;
  531. res->member_list_entries = member_list_entries;
  532. res->header.size = size;
  533. res->header.id = MESSAGE_RES_CPG_TOTEM_CONFCHG_CALLBACK;
  534. res->header.error = CS_OK;
  535. memcpy (&res->ring_id, &last_sync_ring_id, sizeof (mar_cpg_ring_id_t));
  536. memcpy (res->member_list, member_list, res->member_list_entries * sizeof (mar_uint32_t));
  537. if (conn == NULL) {
  538. qb_list_for_each(iter, &cpg_pd_list_head) {
  539. struct cpg_pd *cpg_pd = qb_list_entry (iter, struct cpg_pd, list);
  540. api->ipc_dispatch_send (cpg_pd->conn, buf, size);
  541. }
  542. } else {
  543. api->ipc_dispatch_send (conn, buf, size);
  544. }
  545. return CS_OK;
  546. }
  547. static int notify_lib_joinlist(
  548. const mar_cpg_name_t *group_name,
  549. int joined_list_entries,
  550. mar_cpg_address_t *joined_list,
  551. int left_list_entries,
  552. mar_cpg_address_t *left_list,
  553. int id)
  554. {
  555. int size;
  556. char *buf;
  557. struct qb_list_head *iter;
  558. int count;
  559. struct res_lib_cpg_confchg_callback *res;
  560. mar_cpg_address_t *retgi;
  561. count = 0;
  562. qb_list_for_each(iter, &process_info_list_head) {
  563. struct process_info *pi = qb_list_entry (iter, struct process_info, list);
  564. if (mar_name_compare (&pi->group, group_name) == 0) {
  565. int i;
  566. int founded = 0;
  567. for (i = 0; i < left_list_entries; i++) {
  568. if (left_list[i].nodeid == pi->nodeid && left_list[i].pid == pi->pid) {
  569. founded++;
  570. }
  571. }
  572. if (!founded)
  573. count++;
  574. }
  575. }
  576. size = sizeof(struct res_lib_cpg_confchg_callback) +
  577. sizeof(mar_cpg_address_t) * (count + left_list_entries + joined_list_entries);
  578. buf = alloca(size);
  579. if (!buf)
  580. return CS_ERR_LIBRARY;
  581. res = (struct res_lib_cpg_confchg_callback *)buf;
  582. res->joined_list_entries = joined_list_entries;
  583. res->left_list_entries = left_list_entries;
  584. res->member_list_entries = count;
  585. retgi = res->member_list;
  586. res->header.size = size;
  587. res->header.id = id;
  588. res->header.error = CS_OK;
  589. memcpy(&res->group_name, group_name, sizeof(mar_cpg_name_t));
  590. qb_list_for_each(iter, &process_info_list_head) {
  591. struct process_info *pi=qb_list_entry (iter, struct process_info, list);
  592. if (mar_name_compare (&pi->group, group_name) == 0) {
  593. int i;
  594. int founded = 0;
  595. for (i = 0;i < left_list_entries; i++) {
  596. if (left_list[i].nodeid == pi->nodeid && left_list[i].pid == pi->pid) {
  597. founded++;
  598. }
  599. }
  600. if (!founded) {
  601. retgi->nodeid = pi->nodeid;
  602. retgi->pid = pi->pid;
  603. retgi->reason = CPG_REASON_UNDEFINED;
  604. retgi++;
  605. }
  606. }
  607. }
  608. if (left_list_entries) {
  609. memcpy (retgi, left_list, left_list_entries * sizeof(mar_cpg_address_t));
  610. retgi += left_list_entries;
  611. }
  612. if (joined_list_entries) {
  613. int i;
  614. memcpy (retgi, joined_list, joined_list_entries * sizeof(mar_cpg_address_t));
  615. retgi += joined_list_entries;
  616. for (i=0; i < joined_list_entries; i++) {
  617. if (joined_list[i].nodeid == api->totem_nodeid_get()) {
  618. qb_list_for_each(iter, &cpg_pd_list_head) {
  619. struct cpg_pd *cpd = qb_list_entry (iter, struct cpg_pd, list);
  620. if (mar_name_compare (&cpd->group_name, group_name) == 0 &&
  621. joined_list[i].pid == cpd->pid) {
  622. cpd->cpd_state = CPD_STATE_JOIN_COMPLETED;
  623. }
  624. }
  625. }
  626. }
  627. }
  628. qb_list_for_each(iter, &cpg_pd_list_head) {
  629. struct cpg_pd *cpd = qb_list_entry (iter, struct cpg_pd, list);
  630. if (mar_name_compare (&cpd->group_name, group_name) == 0) {
  631. if (cpd->cpd_state == CPD_STATE_JOIN_COMPLETED ||
  632. cpd->cpd_state == CPD_STATE_LEAVE_STARTED) {
  633. api->ipc_dispatch_send (cpd->conn, buf, size);
  634. cpd->transition_counter++;
  635. }
  636. }
  637. }
  638. if (left_list_entries &&
  639. left_list[0].nodeid == api->totem_nodeid_get() &&
  640. left_list[0].reason == CONFCHG_CPG_REASON_LEAVE) {
  641. qb_list_for_each(iter, &cpg_pd_list_head) {
  642. struct cpg_pd *cpd = qb_list_entry (iter, struct cpg_pd, list);
  643. if (mar_name_compare (&cpd->group_name, group_name) == 0 &&
  644. left_list[0].pid == cpd->pid) {
  645. cpd->pid = 0;
  646. memset (&cpd->group_name, 0, sizeof(cpd->group_name));
  647. cpd->cpd_state = CPD_STATE_UNJOINED;
  648. }
  649. }
  650. }
  651. /*
  652. * Traverse thru cpds and send totem membership for cpd, where it is not send yet
  653. */
  654. qb_list_for_each(iter, &cpg_pd_list_head) {
  655. struct cpg_pd *cpd = qb_list_entry (iter, struct cpg_pd, list);
  656. if ((cpd->flags & CPG_MODEL_V1_DELIVER_INITIAL_TOTEM_CONF) && (cpd->initial_totem_conf_sent == 0)) {
  657. cpd->initial_totem_conf_sent = 1;
  658. notify_lib_totem_membership (cpd->conn, my_old_member_list_entries, my_old_member_list);
  659. }
  660. }
  661. return CS_OK;
  662. }
  663. static void downlist_log(const char *msg, struct req_exec_cpg_downlist *dl)
  664. {
  665. log_printf (LOG_DEBUG,
  666. "%s: members(old:%d left:%d)",
  667. msg,
  668. dl->old_members,
  669. dl->left_nodes);
  670. }
  671. static void downlist_inform_clients (void)
  672. {
  673. struct qb_list_head *iter, *tmp_iter;
  674. struct process_info *left_pi;
  675. qb_map_t *group_map;
  676. struct cpg_name cpg_group;
  677. mar_cpg_name_t group;
  678. struct confchg_data{
  679. struct cpg_name cpg_group;
  680. mar_cpg_address_t left_list[CPG_MEMBERS_MAX];
  681. int left_list_entries;
  682. struct qb_list_head list;
  683. } *pcd;
  684. qb_map_iter_t *miter;
  685. int i, size;
  686. downlist_log("my downlist", &g_req_exec_cpg_downlist);
  687. group_map = qb_skiplist_create();
  688. /*
  689. * only the cpg groups included in left nodes should receive
  690. * confchg event, so we will collect these cpg groups and
  691. * relative left_lists here.
  692. */
  693. qb_list_for_each_safe(iter, tmp_iter, &process_info_list_head) {
  694. struct process_info *pi = qb_list_entry(iter, struct process_info, list);
  695. left_pi = NULL;
  696. for (i = 0; i < g_req_exec_cpg_downlist.left_nodes; i++) {
  697. if (pi->nodeid == g_req_exec_cpg_downlist.nodeids[i]) {
  698. left_pi = pi;
  699. break;
  700. }
  701. }
  702. if (left_pi) {
  703. marshall_from_mar_cpg_name_t(&cpg_group, &left_pi->group);
  704. cpg_group.value[cpg_group.length] = 0;
  705. pcd = (struct confchg_data *)qb_map_get(group_map, cpg_group.value);
  706. if (pcd == NULL) {
  707. pcd = (struct confchg_data *)calloc(1, sizeof(struct confchg_data));
  708. memcpy(&pcd->cpg_group, &cpg_group, sizeof(struct cpg_name));
  709. qb_map_put(group_map, pcd->cpg_group.value, pcd);
  710. }
  711. size = pcd->left_list_entries;
  712. pcd->left_list[size].nodeid = left_pi->nodeid;
  713. pcd->left_list[size].pid = left_pi->pid;
  714. pcd->left_list[size].reason = CONFCHG_CPG_REASON_NODEDOWN;
  715. pcd->left_list_entries++;
  716. qb_list_del (&left_pi->list);
  717. free (left_pi);
  718. }
  719. }
  720. /* send only one confchg event per cpg group */
  721. miter = qb_map_iter_create(group_map);
  722. while (qb_map_iter_next(miter, (void **)&pcd)) {
  723. marshall_to_mar_cpg_name_t(&group, &pcd->cpg_group);
  724. log_printf (LOG_DEBUG, "left_list_entries:%d", pcd->left_list_entries);
  725. for (i=0; i<pcd->left_list_entries; i++) {
  726. log_printf (LOG_DEBUG, "left_list[%d] group:%s, ip:%s, pid:%d",
  727. i, cpg_print_group_name(&group),
  728. (char*)api->totem_ifaces_print(pcd->left_list[i].nodeid),
  729. pcd->left_list[i].pid);
  730. }
  731. /* send confchg event */
  732. notify_lib_joinlist(&group,
  733. 0, NULL,
  734. pcd->left_list_entries,
  735. pcd->left_list,
  736. MESSAGE_RES_CPG_CONFCHG_CALLBACK);
  737. free(pcd);
  738. }
  739. qb_map_iter_free(miter);
  740. qb_map_destroy(group_map);
  741. }
  742. /*
  743. * Remove processes that might have left the group while we were suspended.
  744. */
  745. static void joinlist_remove_zombie_pi_entries (void)
  746. {
  747. struct qb_list_head *pi_iter, *tmp_iter;
  748. struct qb_list_head *jl_iter;
  749. struct process_info *pi;
  750. struct joinlist_msg *stored_msg;
  751. int found;
  752. qb_list_for_each_safe(pi_iter, tmp_iter, &process_info_list_head) {
  753. pi = qb_list_entry (pi_iter, struct process_info, list);
  754. /*
  755. * Ignore local node
  756. */
  757. if (pi->nodeid == api->totem_nodeid_get()) {
  758. continue ;
  759. }
  760. /*
  761. * Try to find message in joinlist messages
  762. */
  763. found = 0;
  764. qb_list_for_each(jl_iter, &joinlist_messages_head) {
  765. stored_msg = qb_list_entry(jl_iter, struct joinlist_msg, list);
  766. if (stored_msg->sender_nodeid == api->totem_nodeid_get()) {
  767. continue ;
  768. }
  769. if (pi->nodeid == stored_msg->sender_nodeid &&
  770. pi->pid == stored_msg->pid &&
  771. mar_name_compare (&pi->group, &stored_msg->group_name) == 0) {
  772. found = 1;
  773. break ;
  774. }
  775. }
  776. if (!found) {
  777. do_proc_leave(&pi->group, pi->pid, pi->nodeid, CONFCHG_CPG_REASON_PROCDOWN);
  778. }
  779. }
  780. }
  781. static void joinlist_inform_clients (void)
  782. {
  783. struct joinlist_msg *stored_msg;
  784. struct qb_list_head *iter;
  785. unsigned int i;
  786. qb_map_t *group_notify_map;
  787. qb_map_iter_t *miter;
  788. struct join_list_confchg_data *jld;
  789. group_notify_map = qb_skiplist_create();
  790. i = 0;
  791. qb_list_for_each(iter, &joinlist_messages_head) {
  792. stored_msg = qb_list_entry(iter, struct joinlist_msg, list);
  793. log_printf (LOG_DEBUG, "joinlist_messages[%u] group:%s, ip:%s, pid:%d",
  794. i++, cpg_print_group_name(&stored_msg->group_name),
  795. (char*)api->totem_ifaces_print(stored_msg->sender_nodeid),
  796. stored_msg->pid);
  797. /* Ignore our own messages */
  798. if (stored_msg->sender_nodeid == api->totem_nodeid_get()) {
  799. continue ;
  800. }
  801. do_proc_join (&stored_msg->group_name, stored_msg->pid, stored_msg->sender_nodeid,
  802. CONFCHG_CPG_REASON_NODEUP, group_notify_map);
  803. }
  804. miter = qb_map_iter_create(group_notify_map);
  805. while (qb_map_iter_next(miter, (void **)&jld)) {
  806. notify_lib_joinlist(&jld->cpg_group,
  807. jld->join_list_entries, jld->join_list,
  808. 0, NULL,
  809. MESSAGE_RES_CPG_CONFCHG_CALLBACK);
  810. free(jld);
  811. }
  812. qb_map_iter_free(miter);
  813. joinlist_remove_zombie_pi_entries ();
  814. }
  815. static void joinlist_messages_delete (void)
  816. {
  817. struct joinlist_msg *stored_msg;
  818. struct qb_list_head *iter, *tmp_iter;
  819. qb_list_for_each_safe(iter, tmp_iter, &joinlist_messages_head) {
  820. stored_msg = qb_list_entry(iter, struct joinlist_msg, list);
  821. qb_list_del (&stored_msg->list);
  822. free (stored_msg);
  823. }
  824. qb_list_init (&joinlist_messages_head);
  825. }
  826. static char *cpg_exec_init_fn (struct corosync_api_v1 *corosync_api)
  827. {
  828. qb_list_init (&joinlist_messages_head);
  829. api = corosync_api;
  830. return (NULL);
  831. }
  832. static void cpg_iteration_instance_finalize (struct cpg_iteration_instance *cpg_iteration_instance)
  833. {
  834. struct qb_list_head *iter, *tmp_iter;
  835. struct process_info *pi;
  836. qb_list_for_each_safe(iter, tmp_iter, &(cpg_iteration_instance->items_list_head)) {
  837. pi = qb_list_entry (iter, struct process_info, list);
  838. qb_list_del (&pi->list);
  839. free (pi);
  840. }
  841. qb_list_del (&cpg_iteration_instance->list);
  842. hdb_handle_destroy (&cpg_iteration_handle_t_db, cpg_iteration_instance->handle);
  843. }
  844. static void cpg_pd_finalize (struct cpg_pd *cpd)
  845. {
  846. struct qb_list_head *iter, *tmp_iter;
  847. struct cpg_iteration_instance *cpii;
  848. zcb_all_free(cpd);
  849. qb_list_for_each_safe(iter, tmp_iter, &(cpd->iteration_instance_list_head)) {
  850. cpii = qb_list_entry (iter, struct cpg_iteration_instance, list);
  851. cpg_iteration_instance_finalize (cpii);
  852. }
  853. qb_list_del (&cpd->list);
  854. }
  855. static int cpg_lib_exit_fn (void *conn)
  856. {
  857. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  858. log_printf(LOGSYS_LEVEL_DEBUG, "exit_fn for conn=%p", conn);
  859. if (cpd->group_name.length > 0 && cpd->cpd_state != CPD_STATE_LEAVE_STARTED) {
  860. cpg_node_joinleave_send (cpd->pid, &cpd->group_name,
  861. MESSAGE_REQ_EXEC_CPG_PROCLEAVE, CONFCHG_CPG_REASON_PROCDOWN);
  862. }
  863. cpg_pd_finalize (cpd);
  864. api->ipc_refcnt_dec (conn);
  865. return (0);
  866. }
  867. static int cpg_node_joinleave_send (unsigned int pid, const mar_cpg_name_t *group_name, int fn, int reason)
  868. {
  869. struct req_exec_cpg_procjoin req_exec_cpg_procjoin;
  870. struct iovec req_exec_cpg_iovec;
  871. int result;
  872. memcpy(&req_exec_cpg_procjoin.group_name, group_name, sizeof(mar_cpg_name_t));
  873. req_exec_cpg_procjoin.pid = pid;
  874. req_exec_cpg_procjoin.reason = reason;
  875. req_exec_cpg_procjoin.header.size = sizeof(req_exec_cpg_procjoin);
  876. req_exec_cpg_procjoin.header.id = SERVICE_ID_MAKE(CPG_SERVICE, fn);
  877. req_exec_cpg_iovec.iov_base = (char *)&req_exec_cpg_procjoin;
  878. req_exec_cpg_iovec.iov_len = sizeof(req_exec_cpg_procjoin);
  879. result = api->totem_mcast (&req_exec_cpg_iovec, 1, TOTEM_AGREED);
  880. return (result);
  881. }
  882. /* Can byteswap join & leave messages */
  883. static void exec_cpg_procjoin_endian_convert (void *msg)
  884. {
  885. struct req_exec_cpg_procjoin *req_exec_cpg_procjoin = msg;
  886. req_exec_cpg_procjoin->pid = swab32(req_exec_cpg_procjoin->pid);
  887. swab_mar_cpg_name_t (&req_exec_cpg_procjoin->group_name);
  888. req_exec_cpg_procjoin->reason = swab32(req_exec_cpg_procjoin->reason);
  889. }
  890. static void exec_cpg_joinlist_endian_convert (void *msg_v)
  891. {
  892. char *msg = msg_v;
  893. struct qb_ipc_response_header *res = (struct qb_ipc_response_header *)msg;
  894. struct join_list_entry *jle = (struct join_list_entry *)(msg + sizeof(struct qb_ipc_response_header));
  895. swab_mar_int32_t (&res->size);
  896. while ((const char*)jle < msg + res->size) {
  897. jle->pid = swab32(jle->pid);
  898. swab_mar_cpg_name_t (&jle->group_name);
  899. jle++;
  900. }
  901. }
  902. static void exec_cpg_downlist_endian_convert_old (void *msg)
  903. {
  904. }
  905. static void exec_cpg_downlist_endian_convert (void *msg)
  906. {
  907. struct req_exec_cpg_downlist *req_exec_cpg_downlist = msg;
  908. unsigned int i;
  909. req_exec_cpg_downlist->left_nodes = swab32(req_exec_cpg_downlist->left_nodes);
  910. req_exec_cpg_downlist->old_members = swab32(req_exec_cpg_downlist->old_members);
  911. for (i = 0; i < req_exec_cpg_downlist->left_nodes; i++) {
  912. req_exec_cpg_downlist->nodeids[i] = swab32(req_exec_cpg_downlist->nodeids[i]);
  913. }
  914. }
  915. static void exec_cpg_mcast_endian_convert (void *msg)
  916. {
  917. struct req_exec_cpg_mcast *req_exec_cpg_mcast = msg;
  918. swab_coroipc_request_header_t (&req_exec_cpg_mcast->header);
  919. swab_mar_cpg_name_t (&req_exec_cpg_mcast->group_name);
  920. req_exec_cpg_mcast->pid = swab32(req_exec_cpg_mcast->pid);
  921. req_exec_cpg_mcast->msglen = swab32(req_exec_cpg_mcast->msglen);
  922. swab_mar_message_source_t (&req_exec_cpg_mcast->source);
  923. }
  924. static void exec_cpg_partial_mcast_endian_convert (void *msg)
  925. {
  926. struct req_exec_cpg_partial_mcast *req_exec_cpg_mcast = msg;
  927. swab_coroipc_request_header_t (&req_exec_cpg_mcast->header);
  928. swab_mar_cpg_name_t (&req_exec_cpg_mcast->group_name);
  929. req_exec_cpg_mcast->pid = swab32(req_exec_cpg_mcast->pid);
  930. req_exec_cpg_mcast->msglen = swab32(req_exec_cpg_mcast->msglen);
  931. req_exec_cpg_mcast->fraglen = swab32(req_exec_cpg_mcast->fraglen);
  932. req_exec_cpg_mcast->type = swab32(req_exec_cpg_mcast->type);
  933. swab_mar_message_source_t (&req_exec_cpg_mcast->source);
  934. }
  935. static struct process_info *process_info_find(const mar_cpg_name_t *group_name, uint32_t pid, unsigned int nodeid) {
  936. struct qb_list_head *iter;
  937. qb_list_for_each(iter, &process_info_list_head) {
  938. struct process_info *pi = qb_list_entry (iter, struct process_info, list);
  939. if (pi->pid == pid && pi->nodeid == nodeid &&
  940. mar_name_compare (&pi->group, group_name) == 0) {
  941. return pi;
  942. }
  943. }
  944. return NULL;
  945. }
  946. static void do_proc_join(
  947. const mar_cpg_name_t *name,
  948. uint32_t pid,
  949. unsigned int nodeid,
  950. int reason,
  951. qb_map_t *group_notify_map)
  952. {
  953. struct process_info *pi;
  954. struct process_info *pi_entry;
  955. mar_cpg_address_t notify_info;
  956. struct qb_list_head *list;
  957. struct qb_list_head *list_to_add = NULL;
  958. int size;
  959. if (process_info_find (name, pid, nodeid) != NULL) {
  960. return ;
  961. }
  962. pi = malloc (sizeof (struct process_info));
  963. if (!pi) {
  964. log_printf(LOGSYS_LEVEL_WARNING, "Unable to allocate process_info struct");
  965. return;
  966. }
  967. pi->nodeid = nodeid;
  968. pi->pid = pid;
  969. memcpy(&pi->group, name, sizeof(*name));
  970. qb_list_init(&pi->list);
  971. /*
  972. * Insert new process in sorted order so synchronization works properly
  973. */
  974. list_to_add = &process_info_list_head;
  975. qb_list_for_each(list, &process_info_list_head) {
  976. pi_entry = qb_list_entry(list, struct process_info, list);
  977. if (pi_entry->nodeid > pi->nodeid ||
  978. (pi_entry->nodeid == pi->nodeid && pi_entry->pid > pi->pid)) {
  979. break;
  980. }
  981. list_to_add = list;
  982. }
  983. qb_list_add (&pi->list, list_to_add);
  984. notify_info.pid = pi->pid;
  985. notify_info.nodeid = nodeid;
  986. notify_info.reason = reason;
  987. if (group_notify_map == NULL) {
  988. notify_lib_joinlist(&pi->group,
  989. 1, &notify_info,
  990. 0, NULL,
  991. MESSAGE_RES_CPG_CONFCHG_CALLBACK);
  992. } else {
  993. struct join_list_confchg_data *jld = qb_map_get(group_notify_map, pi->group.value);
  994. if (jld == NULL) {
  995. jld = (struct join_list_confchg_data *)calloc(1, sizeof(struct join_list_confchg_data));
  996. memcpy(&jld->cpg_group, &pi->group, sizeof(mar_cpg_name_t));
  997. qb_map_put(group_notify_map, jld->cpg_group.value, jld);
  998. }
  999. size = jld->join_list_entries;
  1000. jld->join_list[size].nodeid = notify_info.nodeid;
  1001. jld->join_list[size].pid = notify_info.pid;
  1002. jld->join_list[size].reason = notify_info.reason;
  1003. jld->join_list_entries++;
  1004. }
  1005. }
  1006. static void do_proc_leave(
  1007. const mar_cpg_name_t *name,
  1008. uint32_t pid,
  1009. unsigned int nodeid,
  1010. int reason)
  1011. {
  1012. struct process_info *pi;
  1013. struct qb_list_head *iter, *tmp_iter;
  1014. mar_cpg_address_t notify_info;
  1015. notify_info.pid = pid;
  1016. notify_info.nodeid = nodeid;
  1017. notify_info.reason = reason;
  1018. notify_lib_joinlist(name,
  1019. 0, NULL,
  1020. 1, &notify_info,
  1021. MESSAGE_RES_CPG_CONFCHG_CALLBACK);
  1022. qb_list_for_each_safe(iter, tmp_iter, &process_info_list_head) {
  1023. pi = qb_list_entry(iter, struct process_info, list);
  1024. if (pi->pid == pid && pi->nodeid == nodeid &&
  1025. mar_name_compare (&pi->group, name)==0) {
  1026. qb_list_del (&pi->list);
  1027. free (pi);
  1028. }
  1029. }
  1030. }
  1031. static void message_handler_req_exec_cpg_downlist_old (
  1032. const void *message,
  1033. unsigned int nodeid)
  1034. {
  1035. log_printf (LOGSYS_LEVEL_WARNING, "downlist OLD from node 0x%x",
  1036. nodeid);
  1037. }
  1038. static void message_handler_req_exec_cpg_downlist(
  1039. const void *message,
  1040. unsigned int nodeid)
  1041. {
  1042. const struct req_exec_cpg_downlist *req_exec_cpg_downlist = message;
  1043. log_printf (LOGSYS_LEVEL_WARNING, "downlist left_list: %d received",
  1044. req_exec_cpg_downlist->left_nodes);
  1045. }
  1046. static void message_handler_req_exec_cpg_procjoin (
  1047. const void *message,
  1048. unsigned int nodeid)
  1049. {
  1050. const struct req_exec_cpg_procjoin *req_exec_cpg_procjoin = message;
  1051. log_printf(LOGSYS_LEVEL_DEBUG, "got procjoin message from cluster node 0x%x (%s) for pid %u",
  1052. nodeid,
  1053. api->totem_ifaces_print(nodeid),
  1054. (unsigned int)req_exec_cpg_procjoin->pid);
  1055. do_proc_join (&req_exec_cpg_procjoin->group_name,
  1056. req_exec_cpg_procjoin->pid, nodeid,
  1057. CONFCHG_CPG_REASON_JOIN, NULL);
  1058. }
  1059. static void message_handler_req_exec_cpg_procleave (
  1060. const void *message,
  1061. unsigned int nodeid)
  1062. {
  1063. const struct req_exec_cpg_procjoin *req_exec_cpg_procjoin = message;
  1064. log_printf(LOGSYS_LEVEL_DEBUG, "got procleave message from cluster node 0x%x (%s) for pid %u",
  1065. nodeid,
  1066. api->totem_ifaces_print(nodeid),
  1067. (unsigned int)req_exec_cpg_procjoin->pid);
  1068. do_proc_leave (&req_exec_cpg_procjoin->group_name,
  1069. req_exec_cpg_procjoin->pid, nodeid,
  1070. req_exec_cpg_procjoin->reason);
  1071. }
  1072. /* Got a proclist from another node */
  1073. static void message_handler_req_exec_cpg_joinlist (
  1074. const void *message_v,
  1075. unsigned int nodeid)
  1076. {
  1077. const char *message = message_v;
  1078. const struct qb_ipc_response_header *res = (const struct qb_ipc_response_header *)message;
  1079. const struct join_list_entry *jle = (const struct join_list_entry *)(message + sizeof(struct qb_ipc_response_header));
  1080. struct joinlist_msg *stored_msg;
  1081. log_printf(LOGSYS_LEVEL_DEBUG, "got joinlist message from node 0x%x",
  1082. nodeid);
  1083. while ((const char*)jle < message + res->size) {
  1084. stored_msg = malloc (sizeof (struct joinlist_msg));
  1085. memset(stored_msg, 0, sizeof (struct joinlist_msg));
  1086. stored_msg->sender_nodeid = nodeid;
  1087. stored_msg->pid = jle->pid;
  1088. memcpy(&stored_msg->group_name, &jle->group_name, sizeof(mar_cpg_name_t));
  1089. qb_list_init (&stored_msg->list);
  1090. qb_list_add (&stored_msg->list, &joinlist_messages_head);
  1091. jle++;
  1092. }
  1093. }
  1094. static void message_handler_req_exec_cpg_mcast (
  1095. const void *message,
  1096. unsigned int nodeid)
  1097. {
  1098. const struct req_exec_cpg_mcast *req_exec_cpg_mcast = message;
  1099. struct res_lib_cpg_deliver_callback res_lib_cpg_mcast;
  1100. int msglen = req_exec_cpg_mcast->msglen;
  1101. struct qb_list_head *iter, *pi_iter, *tmp_iter;
  1102. struct cpg_pd *cpd;
  1103. struct iovec iovec[2];
  1104. int known_node = 0;
  1105. res_lib_cpg_mcast.header.id = MESSAGE_RES_CPG_DELIVER_CALLBACK;
  1106. res_lib_cpg_mcast.header.size = sizeof(res_lib_cpg_mcast) + msglen;
  1107. res_lib_cpg_mcast.msglen = msglen;
  1108. res_lib_cpg_mcast.pid = req_exec_cpg_mcast->pid;
  1109. res_lib_cpg_mcast.nodeid = nodeid;
  1110. memcpy(&res_lib_cpg_mcast.group_name, &req_exec_cpg_mcast->group_name,
  1111. sizeof(mar_cpg_name_t));
  1112. iovec[0].iov_base = (void *)&res_lib_cpg_mcast;
  1113. iovec[0].iov_len = sizeof (res_lib_cpg_mcast);
  1114. iovec[1].iov_base = (char*)message+sizeof(*req_exec_cpg_mcast);
  1115. iovec[1].iov_len = msglen;
  1116. qb_list_for_each_safe(iter, tmp_iter, &cpg_pd_list_head) {
  1117. cpd = qb_list_entry(iter, struct cpg_pd, list);
  1118. if ((cpd->cpd_state == CPD_STATE_LEAVE_STARTED || cpd->cpd_state == CPD_STATE_JOIN_COMPLETED)
  1119. && (mar_name_compare (&cpd->group_name, &req_exec_cpg_mcast->group_name) == 0)) {
  1120. if (!known_node) {
  1121. /* Try to find, if we know the node */
  1122. qb_list_for_each(pi_iter, &process_info_list_head) {
  1123. struct process_info *pi = qb_list_entry (pi_iter, struct process_info, list);
  1124. if (pi->nodeid == nodeid &&
  1125. mar_name_compare (&pi->group, &req_exec_cpg_mcast->group_name) == 0) {
  1126. known_node = 1;
  1127. break;
  1128. }
  1129. }
  1130. }
  1131. if (!known_node) {
  1132. log_printf(LOGSYS_LEVEL_WARNING, "Unknown node -> we will not deliver message");
  1133. return ;
  1134. }
  1135. api->ipc_dispatch_iov_send (cpd->conn, iovec, 2);
  1136. }
  1137. }
  1138. }
  1139. static void message_handler_req_exec_cpg_partial_mcast (
  1140. const void *message,
  1141. unsigned int nodeid)
  1142. {
  1143. const struct req_exec_cpg_partial_mcast *req_exec_cpg_mcast = message;
  1144. struct res_lib_cpg_partial_deliver_callback res_lib_cpg_mcast;
  1145. int msglen = req_exec_cpg_mcast->fraglen;
  1146. struct qb_list_head *iter, *pi_iter, *tmp_iter;
  1147. struct cpg_pd *cpd;
  1148. struct iovec iovec[2];
  1149. int known_node = 0;
  1150. log_printf(LOGSYS_LEVEL_DEBUG, "Got fragmented message from node %d, size = %d bytes\n", nodeid, msglen);
  1151. res_lib_cpg_mcast.header.id = MESSAGE_RES_CPG_PARTIAL_DELIVER_CALLBACK;
  1152. res_lib_cpg_mcast.header.size = sizeof(res_lib_cpg_mcast) + msglen;
  1153. res_lib_cpg_mcast.fraglen = msglen;
  1154. res_lib_cpg_mcast.msglen = req_exec_cpg_mcast->msglen;
  1155. res_lib_cpg_mcast.pid = req_exec_cpg_mcast->pid;
  1156. res_lib_cpg_mcast.type = req_exec_cpg_mcast->type;
  1157. res_lib_cpg_mcast.nodeid = nodeid;
  1158. memcpy(&res_lib_cpg_mcast.group_name, &req_exec_cpg_mcast->group_name,
  1159. sizeof(mar_cpg_name_t));
  1160. iovec[0].iov_base = (void *)&res_lib_cpg_mcast;
  1161. iovec[0].iov_len = sizeof (res_lib_cpg_mcast);
  1162. iovec[1].iov_base = (char*)message+sizeof(*req_exec_cpg_mcast);
  1163. iovec[1].iov_len = msglen;
  1164. qb_list_for_each_safe(iter, tmp_iter, &cpg_pd_list_head) {
  1165. cpd = qb_list_entry(iter, struct cpg_pd, list);
  1166. if ((cpd->cpd_state == CPD_STATE_LEAVE_STARTED || cpd->cpd_state == CPD_STATE_JOIN_COMPLETED)
  1167. && (mar_name_compare (&cpd->group_name, &req_exec_cpg_mcast->group_name) == 0)) {
  1168. if (!known_node) {
  1169. /* Try to find, if we know the node */
  1170. qb_list_for_each(pi_iter, &process_info_list_head) {
  1171. struct process_info *pi = qb_list_entry (pi_iter, struct process_info, list);
  1172. if (pi->nodeid == nodeid &&
  1173. mar_name_compare (&pi->group, &req_exec_cpg_mcast->group_name) == 0) {
  1174. known_node = 1;
  1175. break;
  1176. }
  1177. }
  1178. }
  1179. if (!known_node) {
  1180. log_printf(LOGSYS_LEVEL_WARNING, "Unknown node -> we will not deliver message");
  1181. return ;
  1182. }
  1183. api->ipc_dispatch_iov_send (cpd->conn, iovec, 2);
  1184. }
  1185. }
  1186. }
  1187. static int cpg_exec_send_downlist(void)
  1188. {
  1189. struct iovec iov;
  1190. g_req_exec_cpg_downlist.header.id = SERVICE_ID_MAKE(CPG_SERVICE, MESSAGE_REQ_EXEC_CPG_DOWNLIST);
  1191. g_req_exec_cpg_downlist.header.size = sizeof(struct req_exec_cpg_downlist);
  1192. g_req_exec_cpg_downlist.old_members = my_old_member_list_entries;
  1193. iov.iov_base = (void *)&g_req_exec_cpg_downlist;
  1194. iov.iov_len = g_req_exec_cpg_downlist.header.size;
  1195. return (api->totem_mcast (&iov, 1, TOTEM_AGREED));
  1196. }
  1197. static int cpg_exec_send_joinlist(void)
  1198. {
  1199. int count = 0;
  1200. struct qb_list_head *iter;
  1201. struct qb_ipc_response_header *res;
  1202. char *buf;
  1203. struct join_list_entry *jle;
  1204. struct iovec req_exec_cpg_iovec;
  1205. qb_list_for_each(iter, &process_info_list_head) {
  1206. struct process_info *pi = qb_list_entry (iter, struct process_info, list);
  1207. if (pi->nodeid == api->totem_nodeid_get ()) {
  1208. count++;
  1209. }
  1210. }
  1211. /* Nothing to send */
  1212. if (!count)
  1213. return 0;
  1214. buf = alloca(sizeof(struct qb_ipc_response_header) + sizeof(struct join_list_entry) * count);
  1215. if (!buf) {
  1216. log_printf(LOGSYS_LEVEL_WARNING, "Unable to allocate joinlist buffer");
  1217. return -1;
  1218. }
  1219. jle = (struct join_list_entry *)(buf + sizeof(struct qb_ipc_response_header));
  1220. res = (struct qb_ipc_response_header *)buf;
  1221. qb_list_for_each(iter, &process_info_list_head) {
  1222. struct process_info *pi = qb_list_entry (iter, struct process_info, list);
  1223. if (pi->nodeid == api->totem_nodeid_get ()) {
  1224. memcpy (&jle->group_name, &pi->group, sizeof (mar_cpg_name_t));
  1225. jle->pid = pi->pid;
  1226. jle++;
  1227. }
  1228. }
  1229. res->id = SERVICE_ID_MAKE(CPG_SERVICE, MESSAGE_REQ_EXEC_CPG_JOINLIST);
  1230. res->size = sizeof(struct qb_ipc_response_header)+sizeof(struct join_list_entry) * count;
  1231. req_exec_cpg_iovec.iov_base = buf;
  1232. req_exec_cpg_iovec.iov_len = res->size;
  1233. return (api->totem_mcast (&req_exec_cpg_iovec, 1, TOTEM_AGREED));
  1234. }
  1235. static int cpg_lib_init_fn (void *conn)
  1236. {
  1237. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1238. memset (cpd, 0, sizeof(struct cpg_pd));
  1239. cpd->conn = conn;
  1240. qb_list_add (&cpd->list, &cpg_pd_list_head);
  1241. qb_list_init (&cpd->iteration_instance_list_head);
  1242. qb_list_init (&cpd->zcb_mapped_list_head);
  1243. api->ipc_refcnt_inc (conn);
  1244. log_printf(LOGSYS_LEVEL_DEBUG, "lib_init_fn: conn=%p, cpd=%p", conn, cpd);
  1245. return (0);
  1246. }
  1247. /* Join message from the library */
  1248. static void message_handler_req_lib_cpg_join (void *conn, const void *message)
  1249. {
  1250. const struct req_lib_cpg_join *req_lib_cpg_join = message;
  1251. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1252. struct res_lib_cpg_join res_lib_cpg_join;
  1253. cs_error_t error = CS_OK;
  1254. struct qb_list_head *iter;
  1255. /* Test, if we don't have same pid and group name joined */
  1256. qb_list_for_each(iter, &cpg_pd_list_head) {
  1257. struct cpg_pd *cpd_item = qb_list_entry (iter, struct cpg_pd, list);
  1258. if (cpd_item->pid == req_lib_cpg_join->pid &&
  1259. mar_name_compare(&req_lib_cpg_join->group_name, &cpd_item->group_name) == 0) {
  1260. /* We have same pid and group name joined -> return error */
  1261. error = CS_ERR_EXIST;
  1262. goto response_send;
  1263. }
  1264. }
  1265. /*
  1266. * Same check must be done in process info list, because there may be not yet delivered
  1267. * leave of client.
  1268. */
  1269. qb_list_for_each(iter, &process_info_list_head) {
  1270. struct process_info *pi = qb_list_entry (iter, struct process_info, list);
  1271. if (pi->nodeid == api->totem_nodeid_get () && pi->pid == req_lib_cpg_join->pid &&
  1272. mar_name_compare(&req_lib_cpg_join->group_name, &pi->group) == 0) {
  1273. /* We have same pid and group name joined -> return error */
  1274. error = CS_ERR_TRY_AGAIN;
  1275. goto response_send;
  1276. }
  1277. }
  1278. if (req_lib_cpg_join->group_name.length > CPG_MAX_NAME_LENGTH) {
  1279. error = CS_ERR_NAME_TOO_LONG;
  1280. goto response_send;
  1281. }
  1282. switch (cpd->cpd_state) {
  1283. case CPD_STATE_UNJOINED:
  1284. error = CS_OK;
  1285. cpd->cpd_state = CPD_STATE_JOIN_STARTED;
  1286. cpd->pid = req_lib_cpg_join->pid;
  1287. cpd->flags = req_lib_cpg_join->flags;
  1288. memcpy (&cpd->group_name, &req_lib_cpg_join->group_name,
  1289. sizeof (cpd->group_name));
  1290. cpg_node_joinleave_send (req_lib_cpg_join->pid,
  1291. &req_lib_cpg_join->group_name,
  1292. MESSAGE_REQ_EXEC_CPG_PROCJOIN, CONFCHG_CPG_REASON_JOIN);
  1293. break;
  1294. case CPD_STATE_LEAVE_STARTED:
  1295. error = CS_ERR_BUSY;
  1296. break;
  1297. case CPD_STATE_JOIN_STARTED:
  1298. error = CS_ERR_EXIST;
  1299. break;
  1300. case CPD_STATE_JOIN_COMPLETED:
  1301. error = CS_ERR_EXIST;
  1302. break;
  1303. }
  1304. response_send:
  1305. res_lib_cpg_join.header.size = sizeof(res_lib_cpg_join);
  1306. res_lib_cpg_join.header.id = MESSAGE_RES_CPG_JOIN;
  1307. res_lib_cpg_join.header.error = error;
  1308. api->ipc_response_send (conn, &res_lib_cpg_join, sizeof(res_lib_cpg_join));
  1309. }
  1310. /* Leave message from the library */
  1311. static void message_handler_req_lib_cpg_leave (void *conn, const void *message)
  1312. {
  1313. struct res_lib_cpg_leave res_lib_cpg_leave;
  1314. cs_error_t error = CS_OK;
  1315. struct req_lib_cpg_leave *req_lib_cpg_leave = (struct req_lib_cpg_leave *)message;
  1316. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1317. log_printf(LOGSYS_LEVEL_DEBUG, "got leave request on %p", conn);
  1318. switch (cpd->cpd_state) {
  1319. case CPD_STATE_UNJOINED:
  1320. error = CS_ERR_NOT_EXIST;
  1321. break;
  1322. case CPD_STATE_LEAVE_STARTED:
  1323. error = CS_ERR_NOT_EXIST;
  1324. break;
  1325. case CPD_STATE_JOIN_STARTED:
  1326. error = CS_ERR_BUSY;
  1327. break;
  1328. case CPD_STATE_JOIN_COMPLETED:
  1329. error = CS_OK;
  1330. cpd->cpd_state = CPD_STATE_LEAVE_STARTED;
  1331. cpg_node_joinleave_send (req_lib_cpg_leave->pid,
  1332. &req_lib_cpg_leave->group_name,
  1333. MESSAGE_REQ_EXEC_CPG_PROCLEAVE,
  1334. CONFCHG_CPG_REASON_LEAVE);
  1335. break;
  1336. }
  1337. /* send return */
  1338. res_lib_cpg_leave.header.size = sizeof(res_lib_cpg_leave);
  1339. res_lib_cpg_leave.header.id = MESSAGE_RES_CPG_LEAVE;
  1340. res_lib_cpg_leave.header.error = error;
  1341. api->ipc_response_send(conn, &res_lib_cpg_leave, sizeof(res_lib_cpg_leave));
  1342. }
  1343. /* Finalize message from library */
  1344. static void message_handler_req_lib_cpg_finalize (
  1345. void *conn,
  1346. const void *message)
  1347. {
  1348. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1349. struct res_lib_cpg_finalize res_lib_cpg_finalize;
  1350. cs_error_t error = CS_OK;
  1351. log_printf (LOGSYS_LEVEL_DEBUG, "cpg finalize for conn=%p", conn);
  1352. /*
  1353. * We will just remove cpd from list. After this call, connection will be
  1354. * closed on lib side, and cpg_lib_exit_fn will be called
  1355. */
  1356. qb_list_del (&cpd->list);
  1357. qb_list_init (&cpd->list);
  1358. res_lib_cpg_finalize.header.size = sizeof (res_lib_cpg_finalize);
  1359. res_lib_cpg_finalize.header.id = MESSAGE_RES_CPG_FINALIZE;
  1360. res_lib_cpg_finalize.header.error = error;
  1361. api->ipc_response_send (conn, &res_lib_cpg_finalize,
  1362. sizeof (res_lib_cpg_finalize));
  1363. }
  1364. static int
  1365. memory_map (
  1366. const char *path,
  1367. size_t bytes,
  1368. void **buf)
  1369. {
  1370. int32_t fd;
  1371. void *addr;
  1372. int32_t res;
  1373. fd = open (path, O_RDWR, 0600);
  1374. unlink (path);
  1375. if (fd == -1) {
  1376. return (-1);
  1377. }
  1378. res = ftruncate (fd, bytes);
  1379. if (res == -1) {
  1380. goto error_close_unlink;
  1381. }
  1382. addr = mmap (NULL, bytes, PROT_READ | PROT_WRITE,
  1383. MAP_SHARED, fd, 0);
  1384. if (addr == MAP_FAILED) {
  1385. goto error_close_unlink;
  1386. }
  1387. #ifdef MADV_NOSYNC
  1388. madvise(addr, bytes, MADV_NOSYNC);
  1389. #endif
  1390. res = close (fd);
  1391. if (res) {
  1392. munmap (addr, bytes);
  1393. return (-1);
  1394. }
  1395. *buf = addr;
  1396. return (0);
  1397. error_close_unlink:
  1398. close (fd);
  1399. unlink(path);
  1400. return -1;
  1401. }
  1402. static inline int zcb_alloc (
  1403. struct cpg_pd *cpd,
  1404. const char *path_to_file,
  1405. size_t size,
  1406. void **addr)
  1407. {
  1408. struct zcb_mapped *zcb_mapped;
  1409. unsigned int res;
  1410. zcb_mapped = malloc (sizeof (struct zcb_mapped));
  1411. if (zcb_mapped == NULL) {
  1412. return (-1);
  1413. }
  1414. res = memory_map (
  1415. path_to_file,
  1416. size,
  1417. addr);
  1418. if (res == -1) {
  1419. free (zcb_mapped);
  1420. return (-1);
  1421. }
  1422. qb_list_init (&zcb_mapped->list);
  1423. zcb_mapped->addr = *addr;
  1424. zcb_mapped->size = size;
  1425. qb_list_add_tail (&zcb_mapped->list, &cpd->zcb_mapped_list_head);
  1426. return (0);
  1427. }
  1428. static inline int zcb_free (struct zcb_mapped *zcb_mapped)
  1429. {
  1430. unsigned int res;
  1431. res = munmap (zcb_mapped->addr, zcb_mapped->size);
  1432. qb_list_del (&zcb_mapped->list);
  1433. free (zcb_mapped);
  1434. return (res);
  1435. }
  1436. static inline int zcb_by_addr_free (struct cpg_pd *cpd, void *addr)
  1437. {
  1438. struct qb_list_head *list, *tmp_iter;
  1439. struct zcb_mapped *zcb_mapped;
  1440. unsigned int res = 0;
  1441. qb_list_for_each_safe(list, tmp_iter, &(cpd->zcb_mapped_list_head)) {
  1442. zcb_mapped = qb_list_entry (list, struct zcb_mapped, list);
  1443. if (zcb_mapped->addr == addr) {
  1444. res = zcb_free (zcb_mapped);
  1445. break;
  1446. }
  1447. }
  1448. return (res);
  1449. }
  1450. static inline int zcb_all_free (
  1451. struct cpg_pd *cpd)
  1452. {
  1453. struct qb_list_head *list, *tmp_iter;
  1454. struct zcb_mapped *zcb_mapped;
  1455. qb_list_for_each_safe(list, tmp_iter, &(cpd->zcb_mapped_list_head)) {
  1456. zcb_mapped = qb_list_entry (list, struct zcb_mapped, list);
  1457. zcb_free (zcb_mapped);
  1458. }
  1459. return (0);
  1460. }
  1461. union u {
  1462. uint64_t server_addr;
  1463. void *server_ptr;
  1464. };
  1465. static uint64_t void2serveraddr (void *server_ptr)
  1466. {
  1467. union u u;
  1468. u.server_ptr = server_ptr;
  1469. return (u.server_addr);
  1470. }
  1471. static void *serveraddr2void (uint64_t server_addr)
  1472. {
  1473. union u u;
  1474. u.server_addr = server_addr;
  1475. return (u.server_ptr);
  1476. };
  1477. static void message_handler_req_lib_cpg_zc_alloc (
  1478. void *conn,
  1479. const void *message)
  1480. {
  1481. mar_req_coroipcc_zc_alloc_t *hdr = (mar_req_coroipcc_zc_alloc_t *)message;
  1482. struct qb_ipc_response_header res_header;
  1483. void *addr = NULL;
  1484. struct coroipcs_zc_header *zc_header;
  1485. unsigned int res;
  1486. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1487. log_printf(LOGSYS_LEVEL_DEBUG, "path: %s", hdr->path_to_file);
  1488. res = zcb_alloc (cpd, hdr->path_to_file, hdr->map_size,
  1489. &addr);
  1490. assert(res == 0);
  1491. zc_header = (struct coroipcs_zc_header *)addr;
  1492. zc_header->server_address = void2serveraddr(addr);
  1493. res_header.size = sizeof (struct qb_ipc_response_header);
  1494. res_header.id = 0;
  1495. api->ipc_response_send (conn,
  1496. &res_header,
  1497. res_header.size);
  1498. }
  1499. static void message_handler_req_lib_cpg_zc_free (
  1500. void *conn,
  1501. const void *message)
  1502. {
  1503. mar_req_coroipcc_zc_free_t *hdr = (mar_req_coroipcc_zc_free_t *)message;
  1504. struct qb_ipc_response_header res_header;
  1505. void *addr = NULL;
  1506. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1507. log_printf(LOGSYS_LEVEL_DEBUG, " free'ing");
  1508. addr = serveraddr2void (hdr->server_address);
  1509. zcb_by_addr_free (cpd, addr);
  1510. res_header.size = sizeof (struct qb_ipc_response_header);
  1511. res_header.id = 0;
  1512. api->ipc_response_send (
  1513. conn, &res_header,
  1514. res_header.size);
  1515. }
  1516. /* Fragmented mcast message from the library */
  1517. static void message_handler_req_lib_cpg_partial_mcast (void *conn, const void *message)
  1518. {
  1519. const struct req_lib_cpg_partial_mcast *req_lib_cpg_mcast = message;
  1520. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1521. mar_cpg_name_t group_name = cpd->group_name;
  1522. struct iovec req_exec_cpg_iovec[2];
  1523. struct req_exec_cpg_partial_mcast req_exec_cpg_mcast;
  1524. struct res_lib_cpg_partial_send res_lib_cpg_partial_send;
  1525. int msglen = req_lib_cpg_mcast->fraglen;
  1526. int result;
  1527. cs_error_t error = CS_ERR_NOT_EXIST;
  1528. log_printf(LOGSYS_LEVEL_TRACE, "got fragmented mcast request on %p", conn);
  1529. log_printf(LOGSYS_LEVEL_DEBUG, "Sending fragmented message size = %d bytes\n", msglen);
  1530. switch (cpd->cpd_state) {
  1531. case CPD_STATE_UNJOINED:
  1532. error = CS_ERR_NOT_EXIST;
  1533. break;
  1534. case CPD_STATE_LEAVE_STARTED:
  1535. error = CS_ERR_NOT_EXIST;
  1536. break;
  1537. case CPD_STATE_JOIN_STARTED:
  1538. error = CS_OK;
  1539. break;
  1540. case CPD_STATE_JOIN_COMPLETED:
  1541. error = CS_OK;
  1542. break;
  1543. }
  1544. res_lib_cpg_partial_send.header.size = sizeof(res_lib_cpg_partial_send);
  1545. res_lib_cpg_partial_send.header.id = MESSAGE_RES_CPG_PARTIAL_SEND;
  1546. if (req_lib_cpg_mcast->type == LIBCPG_PARTIAL_FIRST) {
  1547. cpd->initial_transition_counter = cpd->transition_counter;
  1548. }
  1549. if (cpd->transition_counter != cpd->initial_transition_counter) {
  1550. error = CS_ERR_INTERRUPT;
  1551. }
  1552. if (error == CS_OK) {
  1553. req_exec_cpg_mcast.header.size = sizeof(req_exec_cpg_mcast) + msglen;
  1554. req_exec_cpg_mcast.header.id = SERVICE_ID_MAKE(CPG_SERVICE,
  1555. MESSAGE_REQ_EXEC_CPG_PARTIAL_MCAST);
  1556. req_exec_cpg_mcast.pid = cpd->pid;
  1557. req_exec_cpg_mcast.msglen = req_lib_cpg_mcast->msglen;
  1558. req_exec_cpg_mcast.type = req_lib_cpg_mcast->type;
  1559. req_exec_cpg_mcast.fraglen = req_lib_cpg_mcast->fraglen;
  1560. api->ipc_source_set (&req_exec_cpg_mcast.source, conn);
  1561. memcpy(&req_exec_cpg_mcast.group_name, &group_name,
  1562. sizeof(mar_cpg_name_t));
  1563. req_exec_cpg_iovec[0].iov_base = (char *)&req_exec_cpg_mcast;
  1564. req_exec_cpg_iovec[0].iov_len = sizeof(req_exec_cpg_mcast);
  1565. req_exec_cpg_iovec[1].iov_base = (char *)&req_lib_cpg_mcast->message;
  1566. req_exec_cpg_iovec[1].iov_len = msglen;
  1567. result = api->totem_mcast (req_exec_cpg_iovec, 2, TOTEM_AGREED);
  1568. assert(result == 0);
  1569. } else {
  1570. log_printf(LOGSYS_LEVEL_ERROR, "*** %p can't mcast to group %s state:%d, error:%d",
  1571. conn, group_name.value, cpd->cpd_state, error);
  1572. }
  1573. res_lib_cpg_partial_send.header.error = error;
  1574. api->ipc_response_send (conn, &res_lib_cpg_partial_send,
  1575. sizeof (res_lib_cpg_partial_send));
  1576. }
  1577. /* Mcast message from the library */
  1578. static void message_handler_req_lib_cpg_mcast (void *conn, const void *message)
  1579. {
  1580. const struct req_lib_cpg_mcast *req_lib_cpg_mcast = message;
  1581. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1582. mar_cpg_name_t group_name = cpd->group_name;
  1583. struct iovec req_exec_cpg_iovec[2];
  1584. struct req_exec_cpg_mcast req_exec_cpg_mcast;
  1585. int msglen = req_lib_cpg_mcast->msglen;
  1586. int result;
  1587. cs_error_t error = CS_ERR_NOT_EXIST;
  1588. log_printf(LOGSYS_LEVEL_TRACE, "got mcast request on %p", conn);
  1589. switch (cpd->cpd_state) {
  1590. case CPD_STATE_UNJOINED:
  1591. error = CS_ERR_NOT_EXIST;
  1592. break;
  1593. case CPD_STATE_LEAVE_STARTED:
  1594. error = CS_ERR_NOT_EXIST;
  1595. break;
  1596. case CPD_STATE_JOIN_STARTED:
  1597. error = CS_OK;
  1598. break;
  1599. case CPD_STATE_JOIN_COMPLETED:
  1600. error = CS_OK;
  1601. break;
  1602. }
  1603. if (error == CS_OK) {
  1604. req_exec_cpg_mcast.header.size = sizeof(req_exec_cpg_mcast) + msglen;
  1605. req_exec_cpg_mcast.header.id = SERVICE_ID_MAKE(CPG_SERVICE,
  1606. MESSAGE_REQ_EXEC_CPG_MCAST);
  1607. req_exec_cpg_mcast.pid = cpd->pid;
  1608. req_exec_cpg_mcast.msglen = msglen;
  1609. api->ipc_source_set (&req_exec_cpg_mcast.source, conn);
  1610. memcpy(&req_exec_cpg_mcast.group_name, &group_name,
  1611. sizeof(mar_cpg_name_t));
  1612. req_exec_cpg_iovec[0].iov_base = (char *)&req_exec_cpg_mcast;
  1613. req_exec_cpg_iovec[0].iov_len = sizeof(req_exec_cpg_mcast);
  1614. req_exec_cpg_iovec[1].iov_base = (char *)&req_lib_cpg_mcast->message;
  1615. req_exec_cpg_iovec[1].iov_len = msglen;
  1616. result = api->totem_mcast (req_exec_cpg_iovec, 2, TOTEM_AGREED);
  1617. assert(result == 0);
  1618. } else {
  1619. log_printf(LOGSYS_LEVEL_ERROR, "*** %p can't mcast to group %s state:%d, error:%d",
  1620. conn, group_name.value, cpd->cpd_state, error);
  1621. }
  1622. }
  1623. static void message_handler_req_lib_cpg_zc_execute (
  1624. void *conn,
  1625. const void *message)
  1626. {
  1627. mar_req_coroipcc_zc_execute_t *hdr = (mar_req_coroipcc_zc_execute_t *)message;
  1628. struct qb_ipc_request_header *header;
  1629. struct res_lib_cpg_mcast res_lib_cpg_mcast;
  1630. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1631. struct iovec req_exec_cpg_iovec[2];
  1632. struct req_exec_cpg_mcast req_exec_cpg_mcast;
  1633. struct req_lib_cpg_mcast *req_lib_cpg_mcast;
  1634. int result;
  1635. cs_error_t error = CS_ERR_NOT_EXIST;
  1636. log_printf(LOGSYS_LEVEL_TRACE, "got ZC mcast request on %p", conn);
  1637. header = (struct qb_ipc_request_header *)(((char *)serveraddr2void(hdr->server_address) + sizeof (struct coroipcs_zc_header)));
  1638. req_lib_cpg_mcast = (struct req_lib_cpg_mcast *)header;
  1639. switch (cpd->cpd_state) {
  1640. case CPD_STATE_UNJOINED:
  1641. error = CS_ERR_NOT_EXIST;
  1642. break;
  1643. case CPD_STATE_LEAVE_STARTED:
  1644. error = CS_ERR_NOT_EXIST;
  1645. break;
  1646. case CPD_STATE_JOIN_STARTED:
  1647. error = CS_OK;
  1648. break;
  1649. case CPD_STATE_JOIN_COMPLETED:
  1650. error = CS_OK;
  1651. break;
  1652. }
  1653. res_lib_cpg_mcast.header.size = sizeof(res_lib_cpg_mcast);
  1654. res_lib_cpg_mcast.header.id = MESSAGE_RES_CPG_MCAST;
  1655. if (error == CS_OK) {
  1656. req_exec_cpg_mcast.header.size = sizeof(req_exec_cpg_mcast) + req_lib_cpg_mcast->msglen;
  1657. req_exec_cpg_mcast.header.id = SERVICE_ID_MAKE(CPG_SERVICE,
  1658. MESSAGE_REQ_EXEC_CPG_MCAST);
  1659. req_exec_cpg_mcast.pid = cpd->pid;
  1660. req_exec_cpg_mcast.msglen = req_lib_cpg_mcast->msglen;
  1661. api->ipc_source_set (&req_exec_cpg_mcast.source, conn);
  1662. memcpy(&req_exec_cpg_mcast.group_name, &cpd->group_name,
  1663. sizeof(mar_cpg_name_t));
  1664. req_exec_cpg_iovec[0].iov_base = (char *)&req_exec_cpg_mcast;
  1665. req_exec_cpg_iovec[0].iov_len = sizeof(req_exec_cpg_mcast);
  1666. req_exec_cpg_iovec[1].iov_base = (char *)header + sizeof(struct req_lib_cpg_mcast);
  1667. req_exec_cpg_iovec[1].iov_len = req_exec_cpg_mcast.msglen;
  1668. result = api->totem_mcast (req_exec_cpg_iovec, 2, TOTEM_AGREED);
  1669. if (result == 0) {
  1670. res_lib_cpg_mcast.header.error = CS_OK;
  1671. } else {
  1672. res_lib_cpg_mcast.header.error = CS_ERR_TRY_AGAIN;
  1673. }
  1674. } else {
  1675. res_lib_cpg_mcast.header.error = error;
  1676. }
  1677. api->ipc_response_send (conn, &res_lib_cpg_mcast,
  1678. sizeof (res_lib_cpg_mcast));
  1679. }
  1680. static void message_handler_req_lib_cpg_membership (void *conn,
  1681. const void *message)
  1682. {
  1683. struct req_lib_cpg_membership_get *req_lib_cpg_membership_get =
  1684. (struct req_lib_cpg_membership_get *)message;
  1685. struct res_lib_cpg_membership_get res_lib_cpg_membership_get;
  1686. struct qb_list_head *iter;
  1687. int member_count = 0;
  1688. res_lib_cpg_membership_get.header.id = MESSAGE_RES_CPG_MEMBERSHIP;
  1689. res_lib_cpg_membership_get.header.error = CS_OK;
  1690. res_lib_cpg_membership_get.header.size =
  1691. sizeof (struct res_lib_cpg_membership_get);
  1692. qb_list_for_each(iter, &process_info_list_head) {
  1693. struct process_info *pi = qb_list_entry (iter, struct process_info, list);
  1694. if (mar_name_compare (&pi->group, &req_lib_cpg_membership_get->group_name) == 0) {
  1695. res_lib_cpg_membership_get.member_list[member_count].nodeid = pi->nodeid;
  1696. res_lib_cpg_membership_get.member_list[member_count].pid = pi->pid;
  1697. member_count += 1;
  1698. }
  1699. }
  1700. res_lib_cpg_membership_get.member_count = member_count;
  1701. api->ipc_response_send (conn, &res_lib_cpg_membership_get,
  1702. sizeof (res_lib_cpg_membership_get));
  1703. }
  1704. static void message_handler_req_lib_cpg_local_get (void *conn,
  1705. const void *message)
  1706. {
  1707. struct res_lib_cpg_local_get res_lib_cpg_local_get;
  1708. res_lib_cpg_local_get.header.size = sizeof (res_lib_cpg_local_get);
  1709. res_lib_cpg_local_get.header.id = MESSAGE_RES_CPG_LOCAL_GET;
  1710. res_lib_cpg_local_get.header.error = CS_OK;
  1711. res_lib_cpg_local_get.local_nodeid = api->totem_nodeid_get ();
  1712. api->ipc_response_send (conn, &res_lib_cpg_local_get,
  1713. sizeof (res_lib_cpg_local_get));
  1714. }
  1715. static void message_handler_req_lib_cpg_iteration_initialize (
  1716. void *conn,
  1717. const void *message)
  1718. {
  1719. const struct req_lib_cpg_iterationinitialize *req_lib_cpg_iterationinitialize = message;
  1720. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1721. hdb_handle_t cpg_iteration_handle = 0;
  1722. struct res_lib_cpg_iterationinitialize res_lib_cpg_iterationinitialize;
  1723. struct qb_list_head *iter, *iter2;
  1724. struct cpg_iteration_instance *cpg_iteration_instance;
  1725. cs_error_t error = CS_OK;
  1726. int res;
  1727. log_printf (LOGSYS_LEVEL_DEBUG, "cpg iteration initialize");
  1728. /* Because between calling this function and *next can be some operations which will
  1729. * change list, we must do full copy.
  1730. */
  1731. /*
  1732. * Create new iteration instance
  1733. */
  1734. res = hdb_handle_create (&cpg_iteration_handle_t_db, sizeof (struct cpg_iteration_instance),
  1735. &cpg_iteration_handle);
  1736. if (res != 0) {
  1737. error = CS_ERR_NO_MEMORY;
  1738. goto response_send;
  1739. }
  1740. res = hdb_handle_get (&cpg_iteration_handle_t_db, cpg_iteration_handle, (void *)&cpg_iteration_instance);
  1741. if (res != 0) {
  1742. error = CS_ERR_BAD_HANDLE;
  1743. goto error_destroy;
  1744. }
  1745. qb_list_init (&cpg_iteration_instance->items_list_head);
  1746. cpg_iteration_instance->handle = cpg_iteration_handle;
  1747. /*
  1748. * Create copy of process_info list "grouped by" group name
  1749. */
  1750. qb_list_for_each(iter, &process_info_list_head) {
  1751. struct process_info *pi = qb_list_entry (iter, struct process_info, list);
  1752. struct process_info *new_pi;
  1753. if (req_lib_cpg_iterationinitialize->iteration_type == CPG_ITERATION_NAME_ONLY) {
  1754. /*
  1755. * Try to find processed group name in our list new list
  1756. */
  1757. int found = 0;
  1758. qb_list_for_each(iter2, &(cpg_iteration_instance->items_list_head)) {
  1759. struct process_info *pi2 = qb_list_entry (iter2, struct process_info, list);
  1760. if (mar_name_compare (&pi2->group, &pi->group) == 0) {
  1761. found = 1;
  1762. break;
  1763. }
  1764. }
  1765. if (found) {
  1766. /*
  1767. * We have this name in list -> don't add
  1768. */
  1769. continue ;
  1770. }
  1771. } else if (req_lib_cpg_iterationinitialize->iteration_type == CPG_ITERATION_ONE_GROUP) {
  1772. /*
  1773. * Test pi group name with request
  1774. */
  1775. if (mar_name_compare (&pi->group, &req_lib_cpg_iterationinitialize->group_name) != 0)
  1776. /*
  1777. * Not same -> don't add
  1778. */
  1779. continue ;
  1780. }
  1781. new_pi = malloc (sizeof (struct process_info));
  1782. if (!new_pi) {
  1783. log_printf(LOGSYS_LEVEL_WARNING, "Unable to allocate process_info struct");
  1784. error = CS_ERR_NO_MEMORY;
  1785. goto error_put_destroy;
  1786. }
  1787. memcpy (new_pi, pi, sizeof (struct process_info));
  1788. qb_list_init (&new_pi->list);
  1789. if (req_lib_cpg_iterationinitialize->iteration_type == CPG_ITERATION_NAME_ONLY) {
  1790. /*
  1791. * pid and nodeid -> undefined
  1792. */
  1793. new_pi->pid = new_pi->nodeid = 0;
  1794. }
  1795. /*
  1796. * We will return list "grouped" by "group name", so try to find right place to add
  1797. */
  1798. qb_list_for_each(iter2, &(cpg_iteration_instance->items_list_head)) {
  1799. struct process_info *pi2 = qb_list_entry (iter2, struct process_info, list);
  1800. if (mar_name_compare (&pi2->group, &pi->group) == 0) {
  1801. break;
  1802. }
  1803. }
  1804. qb_list_add (&new_pi->list, iter2);
  1805. }
  1806. /*
  1807. * Now we have a full "grouped by" copy of process_info list
  1808. */
  1809. /*
  1810. * Add instance to current cpd list
  1811. */
  1812. qb_list_init (&cpg_iteration_instance->list);
  1813. qb_list_add (&cpg_iteration_instance->list, &cpd->iteration_instance_list_head);
  1814. cpg_iteration_instance->current_pointer = &cpg_iteration_instance->items_list_head;
  1815. error_put_destroy:
  1816. hdb_handle_put (&cpg_iteration_handle_t_db, cpg_iteration_handle);
  1817. error_destroy:
  1818. if (error != CS_OK) {
  1819. hdb_handle_destroy (&cpg_iteration_handle_t_db, cpg_iteration_handle);
  1820. }
  1821. response_send:
  1822. res_lib_cpg_iterationinitialize.header.size = sizeof (res_lib_cpg_iterationinitialize);
  1823. res_lib_cpg_iterationinitialize.header.id = MESSAGE_RES_CPG_ITERATIONINITIALIZE;
  1824. res_lib_cpg_iterationinitialize.header.error = error;
  1825. res_lib_cpg_iterationinitialize.iteration_handle = cpg_iteration_handle;
  1826. api->ipc_response_send (conn, &res_lib_cpg_iterationinitialize,
  1827. sizeof (res_lib_cpg_iterationinitialize));
  1828. }
  1829. static void message_handler_req_lib_cpg_iteration_next (
  1830. void *conn,
  1831. const void *message)
  1832. {
  1833. const struct req_lib_cpg_iterationnext *req_lib_cpg_iterationnext = message;
  1834. struct res_lib_cpg_iterationnext res_lib_cpg_iterationnext;
  1835. struct cpg_iteration_instance *cpg_iteration_instance;
  1836. cs_error_t error = CS_OK;
  1837. int res;
  1838. struct process_info *pi;
  1839. log_printf (LOGSYS_LEVEL_DEBUG, "cpg iteration next");
  1840. res = hdb_handle_get (&cpg_iteration_handle_t_db,
  1841. req_lib_cpg_iterationnext->iteration_handle,
  1842. (void *)&cpg_iteration_instance);
  1843. if (res != 0) {
  1844. error = CS_ERR_LIBRARY;
  1845. goto error_exit;
  1846. }
  1847. assert (cpg_iteration_instance);
  1848. cpg_iteration_instance->current_pointer = cpg_iteration_instance->current_pointer->next;
  1849. if (cpg_iteration_instance->current_pointer == &cpg_iteration_instance->items_list_head) {
  1850. error = CS_ERR_NO_SECTIONS;
  1851. goto error_put;
  1852. }
  1853. pi = qb_list_entry (cpg_iteration_instance->current_pointer, struct process_info, list);
  1854. /*
  1855. * Copy iteration data
  1856. */
  1857. res_lib_cpg_iterationnext.description.nodeid = pi->nodeid;
  1858. res_lib_cpg_iterationnext.description.pid = pi->pid;
  1859. memcpy (&res_lib_cpg_iterationnext.description.group,
  1860. &pi->group,
  1861. sizeof (mar_cpg_name_t));
  1862. error_put:
  1863. hdb_handle_put (&cpg_iteration_handle_t_db, req_lib_cpg_iterationnext->iteration_handle);
  1864. error_exit:
  1865. res_lib_cpg_iterationnext.header.size = sizeof (res_lib_cpg_iterationnext);
  1866. res_lib_cpg_iterationnext.header.id = MESSAGE_RES_CPG_ITERATIONNEXT;
  1867. res_lib_cpg_iterationnext.header.error = error;
  1868. api->ipc_response_send (conn, &res_lib_cpg_iterationnext,
  1869. sizeof (res_lib_cpg_iterationnext));
  1870. }
  1871. static void message_handler_req_lib_cpg_iteration_finalize (
  1872. void *conn,
  1873. const void *message)
  1874. {
  1875. const struct req_lib_cpg_iterationfinalize *req_lib_cpg_iterationfinalize = message;
  1876. struct res_lib_cpg_iterationfinalize res_lib_cpg_iterationfinalize;
  1877. struct cpg_iteration_instance *cpg_iteration_instance;
  1878. cs_error_t error = CS_OK;
  1879. int res;
  1880. log_printf (LOGSYS_LEVEL_DEBUG, "cpg iteration finalize");
  1881. res = hdb_handle_get (&cpg_iteration_handle_t_db,
  1882. req_lib_cpg_iterationfinalize->iteration_handle,
  1883. (void *)&cpg_iteration_instance);
  1884. if (res != 0) {
  1885. error = CS_ERR_LIBRARY;
  1886. goto error_exit;
  1887. }
  1888. assert (cpg_iteration_instance);
  1889. cpg_iteration_instance_finalize (cpg_iteration_instance);
  1890. hdb_handle_put (&cpg_iteration_handle_t_db, cpg_iteration_instance->handle);
  1891. error_exit:
  1892. res_lib_cpg_iterationfinalize.header.size = sizeof (res_lib_cpg_iterationfinalize);
  1893. res_lib_cpg_iterationfinalize.header.id = MESSAGE_RES_CPG_ITERATIONFINALIZE;
  1894. res_lib_cpg_iterationfinalize.header.error = error;
  1895. api->ipc_response_send (conn, &res_lib_cpg_iterationfinalize,
  1896. sizeof (res_lib_cpg_iterationfinalize));
  1897. }