cpg.c 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337
  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. /*
  548. * Helper function for notify_lib_joinlist which prepares member_list using
  549. * process_info_list with removed left_list items.
  550. * member_list_entries - When not NULL it contains number of member_list entries
  551. * member_list - When not NULL it is used as pointer to start of preallocated
  552. * array of members. Pointer is adjusted to the end of array on
  553. * exit.
  554. */
  555. static void notify_lib_joinlist_fill_member_list(
  556. const mar_cpg_name_t *group_name,
  557. int left_list_entries,
  558. const mar_cpg_address_t *left_list,
  559. int *member_list_entries,
  560. mar_cpg_address_t **member_list)
  561. {
  562. struct qb_list_head *iter;
  563. int i;
  564. if (member_list_entries != NULL) {
  565. *member_list_entries = 0;
  566. }
  567. qb_list_for_each(iter, &process_info_list_head) {
  568. struct process_info *pi = qb_list_entry (iter, struct process_info, list);
  569. if (mar_name_compare (&pi->group, group_name) == 0) {
  570. int in_left_list = 0;
  571. for (i = 0; i < left_list_entries; i++) {
  572. if (left_list[i].nodeid == pi->nodeid && left_list[i].pid == pi->pid) {
  573. in_left_list = 1;
  574. break ;
  575. }
  576. }
  577. if (!in_left_list) {
  578. if (member_list_entries != NULL) {
  579. (*member_list_entries)++;
  580. }
  581. if (member_list != NULL) {
  582. (*member_list)->nodeid = pi->nodeid;
  583. (*member_list)->pid = pi->pid;
  584. (*member_list)->reason = CPG_REASON_UNDEFINED;
  585. (*member_list)++;
  586. }
  587. }
  588. }
  589. }
  590. }
  591. static int notify_lib_joinlist(
  592. const mar_cpg_name_t *group_name,
  593. int joined_list_entries,
  594. mar_cpg_address_t *joined_list,
  595. int left_list_entries,
  596. mar_cpg_address_t *left_list,
  597. int id)
  598. {
  599. int size;
  600. char *buf;
  601. struct qb_list_head *iter;
  602. int member_list_entries;
  603. struct res_lib_cpg_confchg_callback *res;
  604. mar_cpg_address_t *retgi;
  605. int i;
  606. /*
  607. * Find size of member_list (use process_info_list but remove items in left_list)
  608. */
  609. notify_lib_joinlist_fill_member_list(group_name, left_list_entries, left_list,
  610. &member_list_entries, NULL);
  611. size = sizeof(struct res_lib_cpg_confchg_callback) +
  612. sizeof(mar_cpg_address_t) * (member_list_entries + left_list_entries + joined_list_entries);
  613. buf = alloca(size);
  614. if (!buf)
  615. return CS_ERR_LIBRARY;
  616. res = (struct res_lib_cpg_confchg_callback *)buf;
  617. res->joined_list_entries = joined_list_entries;
  618. res->left_list_entries = left_list_entries;
  619. res->member_list_entries = member_list_entries;
  620. retgi = res->member_list;
  621. res->header.size = size;
  622. res->header.id = id;
  623. res->header.error = CS_OK;
  624. memcpy(&res->group_name, group_name, sizeof(mar_cpg_name_t));
  625. /*
  626. * Fill res->memberlist. Use process_info_list but remove items in left_list.
  627. */
  628. notify_lib_joinlist_fill_member_list(group_name, left_list_entries, left_list,
  629. NULL, &retgi);
  630. /*
  631. * Fill res->left_list
  632. */
  633. if (left_list_entries) {
  634. memcpy (retgi, left_list, left_list_entries * sizeof(mar_cpg_address_t));
  635. retgi += left_list_entries;
  636. }
  637. if (joined_list_entries) {
  638. /*
  639. * Fill res->joined_list
  640. */
  641. memcpy (retgi, joined_list, joined_list_entries * sizeof(mar_cpg_address_t));
  642. retgi += joined_list_entries;
  643. /*
  644. * Update cpd_state for all local joined processes in group
  645. */
  646. for (i = 0; i < joined_list_entries; i++) {
  647. if (joined_list[i].nodeid == api->totem_nodeid_get()) {
  648. qb_list_for_each(iter, &cpg_pd_list_head) {
  649. struct cpg_pd *cpd = qb_list_entry (iter, struct cpg_pd, list);
  650. if (joined_list[i].pid == cpd->pid &&
  651. mar_name_compare (&cpd->group_name, group_name) == 0) {
  652. cpd->cpd_state = CPD_STATE_JOIN_COMPLETED;
  653. }
  654. }
  655. }
  656. }
  657. }
  658. /*
  659. * Send notification to all ipc clients joined in group_name
  660. */
  661. qb_list_for_each(iter, &cpg_pd_list_head) {
  662. struct cpg_pd *cpd = qb_list_entry (iter, struct cpg_pd, list);
  663. if (mar_name_compare (&cpd->group_name, group_name) == 0) {
  664. if (cpd->cpd_state == CPD_STATE_JOIN_COMPLETED ||
  665. cpd->cpd_state == CPD_STATE_LEAVE_STARTED) {
  666. api->ipc_dispatch_send (cpd->conn, buf, size);
  667. cpd->transition_counter++;
  668. }
  669. }
  670. }
  671. if (left_list_entries) {
  672. /*
  673. * Zero internal cpd state for all local processes leaving group
  674. * (this loop is not strictly needed because left_list always either
  675. * contains exactly one process running on local node or more items
  676. * but none of them is running on local node)
  677. */
  678. for (i = 0; i < joined_list_entries; i++) {
  679. if (left_list[i].nodeid == api->totem_nodeid_get() &&
  680. left_list[i].reason == CONFCHG_CPG_REASON_LEAVE) {
  681. qb_list_for_each(iter, &cpg_pd_list_head) {
  682. struct cpg_pd *cpd = qb_list_entry (iter, struct cpg_pd, list);
  683. if (left_list[i].pid == cpd->pid &&
  684. mar_name_compare (&cpd->group_name, group_name) == 0) {
  685. cpd->pid = 0;
  686. memset (&cpd->group_name, 0, sizeof(cpd->group_name));
  687. cpd->cpd_state = CPD_STATE_UNJOINED;
  688. }
  689. }
  690. }
  691. }
  692. }
  693. /*
  694. * Traverse thru cpds and send totem membership for cpd, where it is not send yet
  695. */
  696. qb_list_for_each(iter, &cpg_pd_list_head) {
  697. struct cpg_pd *cpd = qb_list_entry (iter, struct cpg_pd, list);
  698. if ((cpd->flags & CPG_MODEL_V1_DELIVER_INITIAL_TOTEM_CONF) && (cpd->initial_totem_conf_sent == 0)) {
  699. cpd->initial_totem_conf_sent = 1;
  700. notify_lib_totem_membership (cpd->conn, my_old_member_list_entries, my_old_member_list);
  701. }
  702. }
  703. return CS_OK;
  704. }
  705. static void downlist_log(const char *msg, struct req_exec_cpg_downlist *dl)
  706. {
  707. log_printf (LOG_DEBUG,
  708. "%s: members(old:%d left:%d)",
  709. msg,
  710. dl->old_members,
  711. dl->left_nodes);
  712. }
  713. static void downlist_inform_clients (void)
  714. {
  715. struct qb_list_head *iter, *tmp_iter;
  716. struct process_info *left_pi;
  717. qb_map_t *group_map;
  718. struct cpg_name cpg_group;
  719. mar_cpg_name_t group;
  720. struct confchg_data{
  721. struct cpg_name cpg_group;
  722. mar_cpg_address_t left_list[CPG_MEMBERS_MAX];
  723. int left_list_entries;
  724. struct qb_list_head list;
  725. } *pcd;
  726. qb_map_iter_t *miter;
  727. int i, size;
  728. downlist_log("my downlist", &g_req_exec_cpg_downlist);
  729. group_map = qb_skiplist_create();
  730. /*
  731. * only the cpg groups included in left nodes should receive
  732. * confchg event, so we will collect these cpg groups and
  733. * relative left_lists here.
  734. */
  735. qb_list_for_each_safe(iter, tmp_iter, &process_info_list_head) {
  736. struct process_info *pi = qb_list_entry(iter, struct process_info, list);
  737. left_pi = NULL;
  738. for (i = 0; i < g_req_exec_cpg_downlist.left_nodes; i++) {
  739. if (pi->nodeid == g_req_exec_cpg_downlist.nodeids[i]) {
  740. left_pi = pi;
  741. break;
  742. }
  743. }
  744. if (left_pi) {
  745. marshall_from_mar_cpg_name_t(&cpg_group, &left_pi->group);
  746. cpg_group.value[cpg_group.length] = 0;
  747. pcd = (struct confchg_data *)qb_map_get(group_map, cpg_group.value);
  748. if (pcd == NULL) {
  749. pcd = (struct confchg_data *)calloc(1, sizeof(struct confchg_data));
  750. memcpy(&pcd->cpg_group, &cpg_group, sizeof(struct cpg_name));
  751. qb_map_put(group_map, pcd->cpg_group.value, pcd);
  752. }
  753. size = pcd->left_list_entries;
  754. pcd->left_list[size].nodeid = left_pi->nodeid;
  755. pcd->left_list[size].pid = left_pi->pid;
  756. pcd->left_list[size].reason = CONFCHG_CPG_REASON_NODEDOWN;
  757. pcd->left_list_entries++;
  758. qb_list_del (&left_pi->list);
  759. free (left_pi);
  760. }
  761. }
  762. /* send only one confchg event per cpg group */
  763. miter = qb_map_iter_create(group_map);
  764. while (qb_map_iter_next(miter, (void **)&pcd)) {
  765. marshall_to_mar_cpg_name_t(&group, &pcd->cpg_group);
  766. log_printf (LOG_DEBUG, "left_list_entries:%d", pcd->left_list_entries);
  767. for (i=0; i<pcd->left_list_entries; i++) {
  768. log_printf (LOG_DEBUG, "left_list[%d] group:%s, ip:%s, pid:%d",
  769. i, cpg_print_group_name(&group),
  770. (char*)api->totem_ifaces_print(pcd->left_list[i].nodeid),
  771. pcd->left_list[i].pid);
  772. }
  773. /* send confchg event */
  774. notify_lib_joinlist(&group,
  775. 0, NULL,
  776. pcd->left_list_entries,
  777. pcd->left_list,
  778. MESSAGE_RES_CPG_CONFCHG_CALLBACK);
  779. free(pcd);
  780. }
  781. qb_map_iter_free(miter);
  782. qb_map_destroy(group_map);
  783. }
  784. /*
  785. * Remove processes that might have left the group while we were suspended.
  786. */
  787. static void joinlist_remove_zombie_pi_entries (void)
  788. {
  789. struct qb_list_head *pi_iter, *tmp_iter;
  790. struct qb_list_head *jl_iter;
  791. struct process_info *pi;
  792. struct joinlist_msg *stored_msg;
  793. int found;
  794. qb_list_for_each_safe(pi_iter, tmp_iter, &process_info_list_head) {
  795. pi = qb_list_entry (pi_iter, struct process_info, list);
  796. /*
  797. * Ignore local node
  798. */
  799. if (pi->nodeid == api->totem_nodeid_get()) {
  800. continue ;
  801. }
  802. /*
  803. * Try to find message in joinlist messages
  804. */
  805. found = 0;
  806. qb_list_for_each(jl_iter, &joinlist_messages_head) {
  807. stored_msg = qb_list_entry(jl_iter, struct joinlist_msg, list);
  808. if (stored_msg->sender_nodeid == api->totem_nodeid_get()) {
  809. continue ;
  810. }
  811. if (pi->nodeid == stored_msg->sender_nodeid &&
  812. pi->pid == stored_msg->pid &&
  813. mar_name_compare (&pi->group, &stored_msg->group_name) == 0) {
  814. found = 1;
  815. break ;
  816. }
  817. }
  818. if (!found) {
  819. do_proc_leave(&pi->group, pi->pid, pi->nodeid, CONFCHG_CPG_REASON_PROCDOWN);
  820. }
  821. }
  822. }
  823. static void joinlist_inform_clients (void)
  824. {
  825. struct joinlist_msg *stored_msg;
  826. struct qb_list_head *iter;
  827. unsigned int i;
  828. qb_map_t *group_notify_map;
  829. qb_map_iter_t *miter;
  830. struct join_list_confchg_data *jld;
  831. group_notify_map = qb_skiplist_create();
  832. i = 0;
  833. qb_list_for_each(iter, &joinlist_messages_head) {
  834. stored_msg = qb_list_entry(iter, struct joinlist_msg, list);
  835. log_printf (LOG_DEBUG, "joinlist_messages[%u] group:%s, ip:%s, pid:%d",
  836. i++, cpg_print_group_name(&stored_msg->group_name),
  837. (char*)api->totem_ifaces_print(stored_msg->sender_nodeid),
  838. stored_msg->pid);
  839. /* Ignore our own messages */
  840. if (stored_msg->sender_nodeid == api->totem_nodeid_get()) {
  841. continue ;
  842. }
  843. do_proc_join (&stored_msg->group_name, stored_msg->pid, stored_msg->sender_nodeid,
  844. CONFCHG_CPG_REASON_NODEUP, group_notify_map);
  845. }
  846. miter = qb_map_iter_create(group_notify_map);
  847. while (qb_map_iter_next(miter, (void **)&jld)) {
  848. notify_lib_joinlist(&jld->cpg_group,
  849. jld->join_list_entries, jld->join_list,
  850. 0, NULL,
  851. MESSAGE_RES_CPG_CONFCHG_CALLBACK);
  852. free(jld);
  853. }
  854. qb_map_iter_free(miter);
  855. qb_map_destroy(group_notify_map);
  856. joinlist_remove_zombie_pi_entries ();
  857. }
  858. static void joinlist_messages_delete (void)
  859. {
  860. struct joinlist_msg *stored_msg;
  861. struct qb_list_head *iter, *tmp_iter;
  862. qb_list_for_each_safe(iter, tmp_iter, &joinlist_messages_head) {
  863. stored_msg = qb_list_entry(iter, struct joinlist_msg, list);
  864. qb_list_del (&stored_msg->list);
  865. free (stored_msg);
  866. }
  867. qb_list_init (&joinlist_messages_head);
  868. }
  869. static char *cpg_exec_init_fn (struct corosync_api_v1 *corosync_api)
  870. {
  871. qb_list_init (&joinlist_messages_head);
  872. api = corosync_api;
  873. return (NULL);
  874. }
  875. static void cpg_iteration_instance_finalize (struct cpg_iteration_instance *cpg_iteration_instance)
  876. {
  877. struct qb_list_head *iter, *tmp_iter;
  878. struct process_info *pi;
  879. qb_list_for_each_safe(iter, tmp_iter, &(cpg_iteration_instance->items_list_head)) {
  880. pi = qb_list_entry (iter, struct process_info, list);
  881. qb_list_del (&pi->list);
  882. free (pi);
  883. }
  884. qb_list_del (&cpg_iteration_instance->list);
  885. hdb_handle_destroy (&cpg_iteration_handle_t_db, cpg_iteration_instance->handle);
  886. }
  887. static void cpg_pd_finalize (struct cpg_pd *cpd)
  888. {
  889. struct qb_list_head *iter, *tmp_iter;
  890. struct cpg_iteration_instance *cpii;
  891. zcb_all_free(cpd);
  892. qb_list_for_each_safe(iter, tmp_iter, &(cpd->iteration_instance_list_head)) {
  893. cpii = qb_list_entry (iter, struct cpg_iteration_instance, list);
  894. cpg_iteration_instance_finalize (cpii);
  895. }
  896. qb_list_del (&cpd->list);
  897. }
  898. static int cpg_lib_exit_fn (void *conn)
  899. {
  900. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  901. log_printf(LOGSYS_LEVEL_DEBUG, "exit_fn for conn=%p", conn);
  902. if (cpd->group_name.length > 0 && cpd->cpd_state != CPD_STATE_LEAVE_STARTED) {
  903. cpg_node_joinleave_send (cpd->pid, &cpd->group_name,
  904. MESSAGE_REQ_EXEC_CPG_PROCLEAVE, CONFCHG_CPG_REASON_PROCDOWN);
  905. }
  906. cpg_pd_finalize (cpd);
  907. api->ipc_refcnt_dec (conn);
  908. return (0);
  909. }
  910. static int cpg_node_joinleave_send (unsigned int pid, const mar_cpg_name_t *group_name, int fn, int reason)
  911. {
  912. struct req_exec_cpg_procjoin req_exec_cpg_procjoin;
  913. struct iovec req_exec_cpg_iovec;
  914. int result;
  915. memcpy(&req_exec_cpg_procjoin.group_name, group_name, sizeof(mar_cpg_name_t));
  916. req_exec_cpg_procjoin.pid = pid;
  917. req_exec_cpg_procjoin.reason = reason;
  918. req_exec_cpg_procjoin.header.size = sizeof(req_exec_cpg_procjoin);
  919. req_exec_cpg_procjoin.header.id = SERVICE_ID_MAKE(CPG_SERVICE, fn);
  920. req_exec_cpg_iovec.iov_base = (char *)&req_exec_cpg_procjoin;
  921. req_exec_cpg_iovec.iov_len = sizeof(req_exec_cpg_procjoin);
  922. result = api->totem_mcast (&req_exec_cpg_iovec, 1, TOTEM_AGREED);
  923. return (result);
  924. }
  925. /* Can byteswap join & leave messages */
  926. static void exec_cpg_procjoin_endian_convert (void *msg)
  927. {
  928. struct req_exec_cpg_procjoin *req_exec_cpg_procjoin = msg;
  929. req_exec_cpg_procjoin->pid = swab32(req_exec_cpg_procjoin->pid);
  930. swab_mar_cpg_name_t (&req_exec_cpg_procjoin->group_name);
  931. req_exec_cpg_procjoin->reason = swab32(req_exec_cpg_procjoin->reason);
  932. }
  933. static void exec_cpg_joinlist_endian_convert (void *msg_v)
  934. {
  935. char *msg = msg_v;
  936. struct qb_ipc_response_header *res = (struct qb_ipc_response_header *)msg;
  937. struct join_list_entry *jle = (struct join_list_entry *)(msg + sizeof(struct qb_ipc_response_header));
  938. swab_mar_int32_t (&res->size);
  939. while ((const char*)jle < msg + res->size) {
  940. jle->pid = swab32(jle->pid);
  941. swab_mar_cpg_name_t (&jle->group_name);
  942. jle++;
  943. }
  944. }
  945. static void exec_cpg_downlist_endian_convert_old (void *msg)
  946. {
  947. }
  948. static void exec_cpg_downlist_endian_convert (void *msg)
  949. {
  950. struct req_exec_cpg_downlist *req_exec_cpg_downlist = msg;
  951. unsigned int i;
  952. req_exec_cpg_downlist->left_nodes = swab32(req_exec_cpg_downlist->left_nodes);
  953. req_exec_cpg_downlist->old_members = swab32(req_exec_cpg_downlist->old_members);
  954. for (i = 0; i < req_exec_cpg_downlist->left_nodes; i++) {
  955. req_exec_cpg_downlist->nodeids[i] = swab32(req_exec_cpg_downlist->nodeids[i]);
  956. }
  957. }
  958. static void exec_cpg_mcast_endian_convert (void *msg)
  959. {
  960. struct req_exec_cpg_mcast *req_exec_cpg_mcast = msg;
  961. swab_coroipc_request_header_t (&req_exec_cpg_mcast->header);
  962. swab_mar_cpg_name_t (&req_exec_cpg_mcast->group_name);
  963. req_exec_cpg_mcast->pid = swab32(req_exec_cpg_mcast->pid);
  964. req_exec_cpg_mcast->msglen = swab32(req_exec_cpg_mcast->msglen);
  965. swab_mar_message_source_t (&req_exec_cpg_mcast->source);
  966. }
  967. static void exec_cpg_partial_mcast_endian_convert (void *msg)
  968. {
  969. struct req_exec_cpg_partial_mcast *req_exec_cpg_mcast = msg;
  970. swab_coroipc_request_header_t (&req_exec_cpg_mcast->header);
  971. swab_mar_cpg_name_t (&req_exec_cpg_mcast->group_name);
  972. req_exec_cpg_mcast->pid = swab32(req_exec_cpg_mcast->pid);
  973. req_exec_cpg_mcast->msglen = swab32(req_exec_cpg_mcast->msglen);
  974. req_exec_cpg_mcast->fraglen = swab32(req_exec_cpg_mcast->fraglen);
  975. req_exec_cpg_mcast->type = swab32(req_exec_cpg_mcast->type);
  976. swab_mar_message_source_t (&req_exec_cpg_mcast->source);
  977. }
  978. static struct process_info *process_info_find(const mar_cpg_name_t *group_name, uint32_t pid, unsigned int nodeid) {
  979. struct qb_list_head *iter;
  980. qb_list_for_each(iter, &process_info_list_head) {
  981. struct process_info *pi = qb_list_entry (iter, struct process_info, list);
  982. if (pi->pid == pid && pi->nodeid == nodeid &&
  983. mar_name_compare (&pi->group, group_name) == 0) {
  984. return pi;
  985. }
  986. }
  987. return NULL;
  988. }
  989. static void do_proc_join(
  990. const mar_cpg_name_t *name,
  991. uint32_t pid,
  992. unsigned int nodeid,
  993. int reason,
  994. qb_map_t *group_notify_map)
  995. {
  996. struct process_info *pi;
  997. struct process_info *pi_entry;
  998. mar_cpg_address_t notify_info;
  999. struct qb_list_head *list;
  1000. struct qb_list_head *list_to_add = NULL;
  1001. int size;
  1002. if (process_info_find (name, pid, nodeid) != NULL) {
  1003. return ;
  1004. }
  1005. pi = malloc (sizeof (struct process_info));
  1006. if (!pi) {
  1007. log_printf(LOGSYS_LEVEL_WARNING, "Unable to allocate process_info struct");
  1008. return;
  1009. }
  1010. pi->nodeid = nodeid;
  1011. pi->pid = pid;
  1012. memcpy(&pi->group, name, sizeof(*name));
  1013. qb_list_init(&pi->list);
  1014. /*
  1015. * Insert new process in sorted order so synchronization works properly
  1016. */
  1017. list_to_add = &process_info_list_head;
  1018. qb_list_for_each(list, &process_info_list_head) {
  1019. pi_entry = qb_list_entry(list, struct process_info, list);
  1020. if (pi_entry->nodeid > pi->nodeid ||
  1021. (pi_entry->nodeid == pi->nodeid && pi_entry->pid > pi->pid)) {
  1022. break;
  1023. }
  1024. list_to_add = list;
  1025. }
  1026. qb_list_add (&pi->list, list_to_add);
  1027. notify_info.pid = pi->pid;
  1028. notify_info.nodeid = nodeid;
  1029. notify_info.reason = reason;
  1030. if (group_notify_map == NULL) {
  1031. notify_lib_joinlist(&pi->group,
  1032. 1, &notify_info,
  1033. 0, NULL,
  1034. MESSAGE_RES_CPG_CONFCHG_CALLBACK);
  1035. } else {
  1036. struct join_list_confchg_data *jld = qb_map_get(group_notify_map, pi->group.value);
  1037. if (jld == NULL) {
  1038. jld = (struct join_list_confchg_data *)calloc(1, sizeof(struct join_list_confchg_data));
  1039. memcpy(&jld->cpg_group, &pi->group, sizeof(mar_cpg_name_t));
  1040. qb_map_put(group_notify_map, jld->cpg_group.value, jld);
  1041. }
  1042. size = jld->join_list_entries;
  1043. jld->join_list[size].nodeid = notify_info.nodeid;
  1044. jld->join_list[size].pid = notify_info.pid;
  1045. jld->join_list[size].reason = notify_info.reason;
  1046. jld->join_list_entries++;
  1047. }
  1048. }
  1049. static void do_proc_leave(
  1050. const mar_cpg_name_t *name,
  1051. uint32_t pid,
  1052. unsigned int nodeid,
  1053. int reason)
  1054. {
  1055. struct process_info *pi;
  1056. struct qb_list_head *iter, *tmp_iter;
  1057. mar_cpg_address_t notify_info;
  1058. notify_info.pid = pid;
  1059. notify_info.nodeid = nodeid;
  1060. notify_info.reason = reason;
  1061. notify_lib_joinlist(name,
  1062. 0, NULL,
  1063. 1, &notify_info,
  1064. MESSAGE_RES_CPG_CONFCHG_CALLBACK);
  1065. qb_list_for_each_safe(iter, tmp_iter, &process_info_list_head) {
  1066. pi = qb_list_entry(iter, struct process_info, list);
  1067. if (pi->pid == pid && pi->nodeid == nodeid &&
  1068. mar_name_compare (&pi->group, name)==0) {
  1069. qb_list_del (&pi->list);
  1070. free (pi);
  1071. }
  1072. }
  1073. }
  1074. static void message_handler_req_exec_cpg_downlist_old (
  1075. const void *message,
  1076. unsigned int nodeid)
  1077. {
  1078. log_printf (LOGSYS_LEVEL_WARNING, "downlist OLD from node " CS_PRI_NODE_ID,
  1079. nodeid);
  1080. }
  1081. static void message_handler_req_exec_cpg_downlist(
  1082. const void *message,
  1083. unsigned int nodeid)
  1084. {
  1085. const struct req_exec_cpg_downlist *req_exec_cpg_downlist = message;
  1086. log_printf (LOGSYS_LEVEL_WARNING, "downlist left_list: %d received",
  1087. req_exec_cpg_downlist->left_nodes);
  1088. }
  1089. static void message_handler_req_exec_cpg_procjoin (
  1090. const void *message,
  1091. unsigned int nodeid)
  1092. {
  1093. const struct req_exec_cpg_procjoin *req_exec_cpg_procjoin = message;
  1094. log_printf(LOGSYS_LEVEL_DEBUG, "got procjoin message from cluster node " CS_PRI_NODE_ID " (%s) for pid %u",
  1095. nodeid,
  1096. api->totem_ifaces_print(nodeid),
  1097. (unsigned int)req_exec_cpg_procjoin->pid);
  1098. do_proc_join (&req_exec_cpg_procjoin->group_name,
  1099. req_exec_cpg_procjoin->pid, nodeid,
  1100. CONFCHG_CPG_REASON_JOIN, NULL);
  1101. }
  1102. static void message_handler_req_exec_cpg_procleave (
  1103. const void *message,
  1104. unsigned int nodeid)
  1105. {
  1106. const struct req_exec_cpg_procjoin *req_exec_cpg_procjoin = message;
  1107. log_printf(LOGSYS_LEVEL_DEBUG, "got procleave message from cluster node " CS_PRI_NODE_ID " (%s) for pid %u",
  1108. nodeid,
  1109. api->totem_ifaces_print(nodeid),
  1110. (unsigned int)req_exec_cpg_procjoin->pid);
  1111. do_proc_leave (&req_exec_cpg_procjoin->group_name,
  1112. req_exec_cpg_procjoin->pid, nodeid,
  1113. req_exec_cpg_procjoin->reason);
  1114. }
  1115. /* Got a proclist from another node */
  1116. static void message_handler_req_exec_cpg_joinlist (
  1117. const void *message_v,
  1118. unsigned int nodeid)
  1119. {
  1120. const char *message = message_v;
  1121. const struct qb_ipc_response_header *res = (const struct qb_ipc_response_header *)message;
  1122. const struct join_list_entry *jle = (const struct join_list_entry *)(message + sizeof(struct qb_ipc_response_header));
  1123. struct joinlist_msg *stored_msg;
  1124. log_printf(LOGSYS_LEVEL_DEBUG, "got joinlist message from node " CS_PRI_NODE_ID,
  1125. nodeid);
  1126. while ((const char*)jle < message + res->size) {
  1127. stored_msg = malloc (sizeof (struct joinlist_msg));
  1128. memset(stored_msg, 0, sizeof (struct joinlist_msg));
  1129. stored_msg->sender_nodeid = nodeid;
  1130. stored_msg->pid = jle->pid;
  1131. memcpy(&stored_msg->group_name, &jle->group_name, sizeof(mar_cpg_name_t));
  1132. qb_list_init (&stored_msg->list);
  1133. qb_list_add (&stored_msg->list, &joinlist_messages_head);
  1134. jle++;
  1135. }
  1136. }
  1137. static void message_handler_req_exec_cpg_mcast (
  1138. const void *message,
  1139. unsigned int nodeid)
  1140. {
  1141. const struct req_exec_cpg_mcast *req_exec_cpg_mcast = message;
  1142. struct res_lib_cpg_deliver_callback res_lib_cpg_mcast;
  1143. int msglen = req_exec_cpg_mcast->msglen;
  1144. struct qb_list_head *iter, *pi_iter, *tmp_iter;
  1145. struct cpg_pd *cpd;
  1146. struct iovec iovec[2];
  1147. int known_node = 0;
  1148. res_lib_cpg_mcast.header.id = MESSAGE_RES_CPG_DELIVER_CALLBACK;
  1149. res_lib_cpg_mcast.header.size = sizeof(res_lib_cpg_mcast) + msglen;
  1150. res_lib_cpg_mcast.msglen = msglen;
  1151. res_lib_cpg_mcast.pid = req_exec_cpg_mcast->pid;
  1152. res_lib_cpg_mcast.nodeid = nodeid;
  1153. memcpy(&res_lib_cpg_mcast.group_name, &req_exec_cpg_mcast->group_name,
  1154. sizeof(mar_cpg_name_t));
  1155. iovec[0].iov_base = (void *)&res_lib_cpg_mcast;
  1156. iovec[0].iov_len = sizeof (res_lib_cpg_mcast);
  1157. iovec[1].iov_base = (char*)message+sizeof(*req_exec_cpg_mcast);
  1158. iovec[1].iov_len = msglen;
  1159. qb_list_for_each_safe(iter, tmp_iter, &cpg_pd_list_head) {
  1160. cpd = qb_list_entry(iter, struct cpg_pd, list);
  1161. if ((cpd->cpd_state == CPD_STATE_LEAVE_STARTED || cpd->cpd_state == CPD_STATE_JOIN_COMPLETED)
  1162. && (mar_name_compare (&cpd->group_name, &req_exec_cpg_mcast->group_name) == 0)) {
  1163. if (!known_node) {
  1164. /* Try to find, if we know the node */
  1165. qb_list_for_each(pi_iter, &process_info_list_head) {
  1166. struct process_info *pi = qb_list_entry (pi_iter, struct process_info, list);
  1167. if (pi->nodeid == nodeid &&
  1168. mar_name_compare (&pi->group, &req_exec_cpg_mcast->group_name) == 0) {
  1169. known_node = 1;
  1170. break;
  1171. }
  1172. }
  1173. }
  1174. if (!known_node) {
  1175. log_printf(LOGSYS_LEVEL_WARNING, "Unknown node -> we will not deliver message");
  1176. return ;
  1177. }
  1178. api->ipc_dispatch_iov_send (cpd->conn, iovec, 2);
  1179. }
  1180. }
  1181. }
  1182. static void message_handler_req_exec_cpg_partial_mcast (
  1183. const void *message,
  1184. unsigned int nodeid)
  1185. {
  1186. const struct req_exec_cpg_partial_mcast *req_exec_cpg_mcast = message;
  1187. struct res_lib_cpg_partial_deliver_callback res_lib_cpg_mcast;
  1188. int msglen = req_exec_cpg_mcast->fraglen;
  1189. struct qb_list_head *iter, *pi_iter, *tmp_iter;
  1190. struct cpg_pd *cpd;
  1191. struct iovec iovec[2];
  1192. int known_node = 0;
  1193. log_printf(LOGSYS_LEVEL_DEBUG, "Got fragmented message from node " CS_PRI_NODE_ID ", size = %d bytes\n", nodeid, msglen);
  1194. res_lib_cpg_mcast.header.id = MESSAGE_RES_CPG_PARTIAL_DELIVER_CALLBACK;
  1195. res_lib_cpg_mcast.header.size = sizeof(res_lib_cpg_mcast) + msglen;
  1196. res_lib_cpg_mcast.fraglen = msglen;
  1197. res_lib_cpg_mcast.msglen = req_exec_cpg_mcast->msglen;
  1198. res_lib_cpg_mcast.pid = req_exec_cpg_mcast->pid;
  1199. res_lib_cpg_mcast.type = req_exec_cpg_mcast->type;
  1200. res_lib_cpg_mcast.nodeid = nodeid;
  1201. memcpy(&res_lib_cpg_mcast.group_name, &req_exec_cpg_mcast->group_name,
  1202. sizeof(mar_cpg_name_t));
  1203. iovec[0].iov_base = (void *)&res_lib_cpg_mcast;
  1204. iovec[0].iov_len = sizeof (res_lib_cpg_mcast);
  1205. iovec[1].iov_base = (char*)message+sizeof(*req_exec_cpg_mcast);
  1206. iovec[1].iov_len = msglen;
  1207. qb_list_for_each_safe(iter, tmp_iter, &cpg_pd_list_head) {
  1208. cpd = qb_list_entry(iter, struct cpg_pd, list);
  1209. if ((cpd->cpd_state == CPD_STATE_LEAVE_STARTED || cpd->cpd_state == CPD_STATE_JOIN_COMPLETED)
  1210. && (mar_name_compare (&cpd->group_name, &req_exec_cpg_mcast->group_name) == 0)) {
  1211. if (!known_node) {
  1212. /* Try to find, if we know the node */
  1213. qb_list_for_each(pi_iter, &process_info_list_head) {
  1214. struct process_info *pi = qb_list_entry (pi_iter, struct process_info, list);
  1215. if (pi->nodeid == nodeid &&
  1216. mar_name_compare (&pi->group, &req_exec_cpg_mcast->group_name) == 0) {
  1217. known_node = 1;
  1218. break;
  1219. }
  1220. }
  1221. }
  1222. if (!known_node) {
  1223. log_printf(LOGSYS_LEVEL_WARNING, "Unknown node -> we will not deliver message");
  1224. return ;
  1225. }
  1226. api->ipc_dispatch_iov_send (cpd->conn, iovec, 2);
  1227. }
  1228. }
  1229. }
  1230. static int cpg_exec_send_downlist(void)
  1231. {
  1232. struct iovec iov;
  1233. g_req_exec_cpg_downlist.header.id = SERVICE_ID_MAKE(CPG_SERVICE, MESSAGE_REQ_EXEC_CPG_DOWNLIST);
  1234. g_req_exec_cpg_downlist.header.size = sizeof(struct req_exec_cpg_downlist);
  1235. g_req_exec_cpg_downlist.old_members = my_old_member_list_entries;
  1236. iov.iov_base = (void *)&g_req_exec_cpg_downlist;
  1237. iov.iov_len = g_req_exec_cpg_downlist.header.size;
  1238. return (api->totem_mcast (&iov, 1, TOTEM_AGREED));
  1239. }
  1240. static int cpg_exec_send_joinlist(void)
  1241. {
  1242. int count = 0;
  1243. struct qb_list_head *iter;
  1244. struct qb_ipc_response_header *res;
  1245. char *buf;
  1246. struct join_list_entry *jle;
  1247. struct iovec req_exec_cpg_iovec;
  1248. qb_list_for_each(iter, &process_info_list_head) {
  1249. struct process_info *pi = qb_list_entry (iter, struct process_info, list);
  1250. if (pi->nodeid == api->totem_nodeid_get ()) {
  1251. count++;
  1252. }
  1253. }
  1254. /* Nothing to send */
  1255. if (!count)
  1256. return 0;
  1257. buf = alloca(sizeof(struct qb_ipc_response_header) + sizeof(struct join_list_entry) * count);
  1258. if (!buf) {
  1259. log_printf(LOGSYS_LEVEL_WARNING, "Unable to allocate joinlist buffer");
  1260. return -1;
  1261. }
  1262. jle = (struct join_list_entry *)(buf + sizeof(struct qb_ipc_response_header));
  1263. res = (struct qb_ipc_response_header *)buf;
  1264. qb_list_for_each(iter, &process_info_list_head) {
  1265. struct process_info *pi = qb_list_entry (iter, struct process_info, list);
  1266. if (pi->nodeid == api->totem_nodeid_get ()) {
  1267. memcpy (&jle->group_name, &pi->group, sizeof (mar_cpg_name_t));
  1268. jle->pid = pi->pid;
  1269. jle++;
  1270. }
  1271. }
  1272. res->id = SERVICE_ID_MAKE(CPG_SERVICE, MESSAGE_REQ_EXEC_CPG_JOINLIST);
  1273. res->size = sizeof(struct qb_ipc_response_header)+sizeof(struct join_list_entry) * count;
  1274. req_exec_cpg_iovec.iov_base = buf;
  1275. req_exec_cpg_iovec.iov_len = res->size;
  1276. return (api->totem_mcast (&req_exec_cpg_iovec, 1, TOTEM_AGREED));
  1277. }
  1278. static int cpg_lib_init_fn (void *conn)
  1279. {
  1280. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1281. memset (cpd, 0, sizeof(struct cpg_pd));
  1282. cpd->conn = conn;
  1283. qb_list_add (&cpd->list, &cpg_pd_list_head);
  1284. qb_list_init (&cpd->iteration_instance_list_head);
  1285. qb_list_init (&cpd->zcb_mapped_list_head);
  1286. api->ipc_refcnt_inc (conn);
  1287. log_printf(LOGSYS_LEVEL_DEBUG, "lib_init_fn: conn=%p, cpd=%p", conn, cpd);
  1288. return (0);
  1289. }
  1290. /* Join message from the library */
  1291. static void message_handler_req_lib_cpg_join (void *conn, const void *message)
  1292. {
  1293. const struct req_lib_cpg_join *req_lib_cpg_join = message;
  1294. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1295. struct res_lib_cpg_join res_lib_cpg_join;
  1296. cs_error_t error = CS_OK;
  1297. struct qb_list_head *iter;
  1298. /* Test, if we don't have same pid and group name joined */
  1299. qb_list_for_each(iter, &cpg_pd_list_head) {
  1300. struct cpg_pd *cpd_item = qb_list_entry (iter, struct cpg_pd, list);
  1301. if (cpd_item->pid == req_lib_cpg_join->pid &&
  1302. mar_name_compare(&req_lib_cpg_join->group_name, &cpd_item->group_name) == 0) {
  1303. /* We have same pid and group name joined -> return error */
  1304. error = CS_ERR_EXIST;
  1305. goto response_send;
  1306. }
  1307. }
  1308. /*
  1309. * Same check must be done in process info list, because there may be not yet delivered
  1310. * leave of client.
  1311. */
  1312. qb_list_for_each(iter, &process_info_list_head) {
  1313. struct process_info *pi = qb_list_entry (iter, struct process_info, list);
  1314. if (pi->nodeid == api->totem_nodeid_get () && pi->pid == req_lib_cpg_join->pid &&
  1315. mar_name_compare(&req_lib_cpg_join->group_name, &pi->group) == 0) {
  1316. /* We have same pid and group name joined -> return error */
  1317. error = CS_ERR_TRY_AGAIN;
  1318. goto response_send;
  1319. }
  1320. }
  1321. if (req_lib_cpg_join->group_name.length > CPG_MAX_NAME_LENGTH) {
  1322. error = CS_ERR_NAME_TOO_LONG;
  1323. goto response_send;
  1324. }
  1325. switch (cpd->cpd_state) {
  1326. case CPD_STATE_UNJOINED:
  1327. error = CS_OK;
  1328. cpd->cpd_state = CPD_STATE_JOIN_STARTED;
  1329. cpd->pid = req_lib_cpg_join->pid;
  1330. cpd->flags = req_lib_cpg_join->flags;
  1331. memcpy (&cpd->group_name, &req_lib_cpg_join->group_name,
  1332. sizeof (cpd->group_name));
  1333. cpg_node_joinleave_send (req_lib_cpg_join->pid,
  1334. &req_lib_cpg_join->group_name,
  1335. MESSAGE_REQ_EXEC_CPG_PROCJOIN, CONFCHG_CPG_REASON_JOIN);
  1336. break;
  1337. case CPD_STATE_LEAVE_STARTED:
  1338. error = CS_ERR_BUSY;
  1339. break;
  1340. case CPD_STATE_JOIN_STARTED:
  1341. error = CS_ERR_EXIST;
  1342. break;
  1343. case CPD_STATE_JOIN_COMPLETED:
  1344. error = CS_ERR_EXIST;
  1345. break;
  1346. }
  1347. response_send:
  1348. res_lib_cpg_join.header.size = sizeof(res_lib_cpg_join);
  1349. res_lib_cpg_join.header.id = MESSAGE_RES_CPG_JOIN;
  1350. res_lib_cpg_join.header.error = error;
  1351. api->ipc_response_send (conn, &res_lib_cpg_join, sizeof(res_lib_cpg_join));
  1352. }
  1353. /* Leave message from the library */
  1354. static void message_handler_req_lib_cpg_leave (void *conn, const void *message)
  1355. {
  1356. struct res_lib_cpg_leave res_lib_cpg_leave;
  1357. cs_error_t error = CS_OK;
  1358. struct req_lib_cpg_leave *req_lib_cpg_leave = (struct req_lib_cpg_leave *)message;
  1359. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1360. log_printf(LOGSYS_LEVEL_DEBUG, "got leave request on %p", conn);
  1361. switch (cpd->cpd_state) {
  1362. case CPD_STATE_UNJOINED:
  1363. error = CS_ERR_NOT_EXIST;
  1364. break;
  1365. case CPD_STATE_LEAVE_STARTED:
  1366. error = CS_ERR_NOT_EXIST;
  1367. break;
  1368. case CPD_STATE_JOIN_STARTED:
  1369. error = CS_ERR_BUSY;
  1370. break;
  1371. case CPD_STATE_JOIN_COMPLETED:
  1372. error = CS_OK;
  1373. cpd->cpd_state = CPD_STATE_LEAVE_STARTED;
  1374. cpg_node_joinleave_send (req_lib_cpg_leave->pid,
  1375. &req_lib_cpg_leave->group_name,
  1376. MESSAGE_REQ_EXEC_CPG_PROCLEAVE,
  1377. CONFCHG_CPG_REASON_LEAVE);
  1378. break;
  1379. }
  1380. /* send return */
  1381. res_lib_cpg_leave.header.size = sizeof(res_lib_cpg_leave);
  1382. res_lib_cpg_leave.header.id = MESSAGE_RES_CPG_LEAVE;
  1383. res_lib_cpg_leave.header.error = error;
  1384. api->ipc_response_send(conn, &res_lib_cpg_leave, sizeof(res_lib_cpg_leave));
  1385. }
  1386. /* Finalize message from library */
  1387. static void message_handler_req_lib_cpg_finalize (
  1388. void *conn,
  1389. const void *message)
  1390. {
  1391. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1392. struct res_lib_cpg_finalize res_lib_cpg_finalize;
  1393. cs_error_t error = CS_OK;
  1394. log_printf (LOGSYS_LEVEL_DEBUG, "cpg finalize for conn=%p", conn);
  1395. /*
  1396. * We will just remove cpd from list. After this call, connection will be
  1397. * closed on lib side, and cpg_lib_exit_fn will be called
  1398. */
  1399. qb_list_del (&cpd->list);
  1400. qb_list_init (&cpd->list);
  1401. res_lib_cpg_finalize.header.size = sizeof (res_lib_cpg_finalize);
  1402. res_lib_cpg_finalize.header.id = MESSAGE_RES_CPG_FINALIZE;
  1403. res_lib_cpg_finalize.header.error = error;
  1404. api->ipc_response_send (conn, &res_lib_cpg_finalize,
  1405. sizeof (res_lib_cpg_finalize));
  1406. }
  1407. static int
  1408. memory_map (
  1409. const char *path,
  1410. size_t bytes,
  1411. void **buf)
  1412. {
  1413. int32_t fd;
  1414. void *addr;
  1415. int32_t res;
  1416. fd = open (path, O_RDWR, 0600);
  1417. unlink (path);
  1418. if (fd == -1) {
  1419. return (-1);
  1420. }
  1421. res = ftruncate (fd, bytes);
  1422. if (res == -1) {
  1423. goto error_close_unlink;
  1424. }
  1425. addr = mmap (NULL, bytes, PROT_READ | PROT_WRITE,
  1426. MAP_SHARED, fd, 0);
  1427. if (addr == MAP_FAILED) {
  1428. goto error_close_unlink;
  1429. }
  1430. #ifdef MADV_NOSYNC
  1431. madvise(addr, bytes, MADV_NOSYNC);
  1432. #endif
  1433. res = close (fd);
  1434. if (res) {
  1435. munmap (addr, bytes);
  1436. return (-1);
  1437. }
  1438. *buf = addr;
  1439. return (0);
  1440. error_close_unlink:
  1441. close (fd);
  1442. unlink(path);
  1443. return -1;
  1444. }
  1445. static inline int zcb_alloc (
  1446. struct cpg_pd *cpd,
  1447. const char *path_to_file,
  1448. size_t size,
  1449. void **addr)
  1450. {
  1451. struct zcb_mapped *zcb_mapped;
  1452. unsigned int res;
  1453. zcb_mapped = malloc (sizeof (struct zcb_mapped));
  1454. if (zcb_mapped == NULL) {
  1455. return (-1);
  1456. }
  1457. res = memory_map (
  1458. path_to_file,
  1459. size,
  1460. addr);
  1461. if (res == -1) {
  1462. free (zcb_mapped);
  1463. return (-1);
  1464. }
  1465. qb_list_init (&zcb_mapped->list);
  1466. zcb_mapped->addr = *addr;
  1467. zcb_mapped->size = size;
  1468. qb_list_add_tail (&zcb_mapped->list, &cpd->zcb_mapped_list_head);
  1469. return (0);
  1470. }
  1471. static inline int zcb_free (struct zcb_mapped *zcb_mapped)
  1472. {
  1473. unsigned int res;
  1474. res = munmap (zcb_mapped->addr, zcb_mapped->size);
  1475. qb_list_del (&zcb_mapped->list);
  1476. free (zcb_mapped);
  1477. return (res);
  1478. }
  1479. static inline int zcb_by_addr_free (struct cpg_pd *cpd, void *addr)
  1480. {
  1481. struct qb_list_head *list, *tmp_iter;
  1482. struct zcb_mapped *zcb_mapped;
  1483. unsigned int res = 0;
  1484. qb_list_for_each_safe(list, tmp_iter, &(cpd->zcb_mapped_list_head)) {
  1485. zcb_mapped = qb_list_entry (list, struct zcb_mapped, list);
  1486. if (zcb_mapped->addr == addr) {
  1487. res = zcb_free (zcb_mapped);
  1488. break;
  1489. }
  1490. }
  1491. return (res);
  1492. }
  1493. static inline int zcb_all_free (
  1494. struct cpg_pd *cpd)
  1495. {
  1496. struct qb_list_head *list, *tmp_iter;
  1497. struct zcb_mapped *zcb_mapped;
  1498. qb_list_for_each_safe(list, tmp_iter, &(cpd->zcb_mapped_list_head)) {
  1499. zcb_mapped = qb_list_entry (list, struct zcb_mapped, list);
  1500. zcb_free (zcb_mapped);
  1501. }
  1502. return (0);
  1503. }
  1504. union u {
  1505. uint64_t server_addr;
  1506. void *server_ptr;
  1507. };
  1508. static uint64_t void2serveraddr (void *server_ptr)
  1509. {
  1510. union u u;
  1511. u.server_ptr = server_ptr;
  1512. return (u.server_addr);
  1513. }
  1514. static void *serveraddr2void (uint64_t server_addr)
  1515. {
  1516. union u u;
  1517. u.server_addr = server_addr;
  1518. return (u.server_ptr);
  1519. };
  1520. static void message_handler_req_lib_cpg_zc_alloc (
  1521. void *conn,
  1522. const void *message)
  1523. {
  1524. mar_req_coroipcc_zc_alloc_t *hdr = (mar_req_coroipcc_zc_alloc_t *)message;
  1525. struct qb_ipc_response_header res_header;
  1526. void *addr = NULL;
  1527. struct coroipcs_zc_header *zc_header;
  1528. unsigned int res;
  1529. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1530. log_printf(LOGSYS_LEVEL_DEBUG, "path: %s", hdr->path_to_file);
  1531. res = zcb_alloc (cpd, hdr->path_to_file, hdr->map_size,
  1532. &addr);
  1533. assert(res == 0);
  1534. zc_header = (struct coroipcs_zc_header *)addr;
  1535. zc_header->server_address = void2serveraddr(addr);
  1536. res_header.size = sizeof (struct qb_ipc_response_header);
  1537. res_header.id = 0;
  1538. api->ipc_response_send (conn,
  1539. &res_header,
  1540. res_header.size);
  1541. }
  1542. static void message_handler_req_lib_cpg_zc_free (
  1543. void *conn,
  1544. const void *message)
  1545. {
  1546. mar_req_coroipcc_zc_free_t *hdr = (mar_req_coroipcc_zc_free_t *)message;
  1547. struct qb_ipc_response_header res_header;
  1548. void *addr = NULL;
  1549. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1550. log_printf(LOGSYS_LEVEL_DEBUG, " free'ing");
  1551. addr = serveraddr2void (hdr->server_address);
  1552. zcb_by_addr_free (cpd, addr);
  1553. res_header.size = sizeof (struct qb_ipc_response_header);
  1554. res_header.id = 0;
  1555. api->ipc_response_send (
  1556. conn, &res_header,
  1557. res_header.size);
  1558. }
  1559. /* Fragmented mcast message from the library */
  1560. static void message_handler_req_lib_cpg_partial_mcast (void *conn, const void *message)
  1561. {
  1562. const struct req_lib_cpg_partial_mcast *req_lib_cpg_mcast = message;
  1563. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1564. mar_cpg_name_t group_name = cpd->group_name;
  1565. struct iovec req_exec_cpg_iovec[2];
  1566. struct req_exec_cpg_partial_mcast req_exec_cpg_mcast;
  1567. struct res_lib_cpg_partial_send res_lib_cpg_partial_send;
  1568. int msglen = req_lib_cpg_mcast->fraglen;
  1569. int result;
  1570. cs_error_t error = CS_ERR_NOT_EXIST;
  1571. log_printf(LOGSYS_LEVEL_TRACE, "got fragmented mcast request on %p", conn);
  1572. log_printf(LOGSYS_LEVEL_DEBUG, "Sending fragmented message size = %d bytes\n", msglen);
  1573. switch (cpd->cpd_state) {
  1574. case CPD_STATE_UNJOINED:
  1575. error = CS_ERR_NOT_EXIST;
  1576. break;
  1577. case CPD_STATE_LEAVE_STARTED:
  1578. error = CS_ERR_NOT_EXIST;
  1579. break;
  1580. case CPD_STATE_JOIN_STARTED:
  1581. error = CS_OK;
  1582. break;
  1583. case CPD_STATE_JOIN_COMPLETED:
  1584. error = CS_OK;
  1585. break;
  1586. }
  1587. res_lib_cpg_partial_send.header.size = sizeof(res_lib_cpg_partial_send);
  1588. res_lib_cpg_partial_send.header.id = MESSAGE_RES_CPG_PARTIAL_SEND;
  1589. if (req_lib_cpg_mcast->type == LIBCPG_PARTIAL_FIRST) {
  1590. cpd->initial_transition_counter = cpd->transition_counter;
  1591. }
  1592. if (cpd->transition_counter != cpd->initial_transition_counter) {
  1593. error = CS_ERR_INTERRUPT;
  1594. }
  1595. if (error == CS_OK) {
  1596. req_exec_cpg_mcast.header.size = sizeof(req_exec_cpg_mcast) + msglen;
  1597. req_exec_cpg_mcast.header.id = SERVICE_ID_MAKE(CPG_SERVICE,
  1598. MESSAGE_REQ_EXEC_CPG_PARTIAL_MCAST);
  1599. req_exec_cpg_mcast.pid = cpd->pid;
  1600. req_exec_cpg_mcast.msglen = req_lib_cpg_mcast->msglen;
  1601. req_exec_cpg_mcast.type = req_lib_cpg_mcast->type;
  1602. req_exec_cpg_mcast.fraglen = req_lib_cpg_mcast->fraglen;
  1603. api->ipc_source_set (&req_exec_cpg_mcast.source, conn);
  1604. memcpy(&req_exec_cpg_mcast.group_name, &group_name,
  1605. sizeof(mar_cpg_name_t));
  1606. req_exec_cpg_iovec[0].iov_base = (char *)&req_exec_cpg_mcast;
  1607. req_exec_cpg_iovec[0].iov_len = sizeof(req_exec_cpg_mcast);
  1608. req_exec_cpg_iovec[1].iov_base = (char *)&req_lib_cpg_mcast->message;
  1609. req_exec_cpg_iovec[1].iov_len = msglen;
  1610. result = api->totem_mcast (req_exec_cpg_iovec, 2, TOTEM_AGREED);
  1611. assert(result == 0);
  1612. } else {
  1613. log_printf(LOGSYS_LEVEL_ERROR, "*** %p can't mcast to group %s state:%d, error:%d",
  1614. conn, group_name.value, cpd->cpd_state, error);
  1615. }
  1616. res_lib_cpg_partial_send.header.error = error;
  1617. api->ipc_response_send (conn, &res_lib_cpg_partial_send,
  1618. sizeof (res_lib_cpg_partial_send));
  1619. }
  1620. /* Mcast message from the library */
  1621. static void message_handler_req_lib_cpg_mcast (void *conn, const void *message)
  1622. {
  1623. const struct req_lib_cpg_mcast *req_lib_cpg_mcast = message;
  1624. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1625. mar_cpg_name_t group_name = cpd->group_name;
  1626. struct iovec req_exec_cpg_iovec[2];
  1627. struct req_exec_cpg_mcast req_exec_cpg_mcast;
  1628. int msglen = req_lib_cpg_mcast->msglen;
  1629. int result;
  1630. cs_error_t error = CS_ERR_NOT_EXIST;
  1631. log_printf(LOGSYS_LEVEL_TRACE, "got mcast request on %p", conn);
  1632. switch (cpd->cpd_state) {
  1633. case CPD_STATE_UNJOINED:
  1634. error = CS_ERR_NOT_EXIST;
  1635. break;
  1636. case CPD_STATE_LEAVE_STARTED:
  1637. error = CS_ERR_NOT_EXIST;
  1638. break;
  1639. case CPD_STATE_JOIN_STARTED:
  1640. error = CS_OK;
  1641. break;
  1642. case CPD_STATE_JOIN_COMPLETED:
  1643. error = CS_OK;
  1644. break;
  1645. }
  1646. if (error == CS_OK) {
  1647. req_exec_cpg_mcast.header.size = sizeof(req_exec_cpg_mcast) + msglen;
  1648. req_exec_cpg_mcast.header.id = SERVICE_ID_MAKE(CPG_SERVICE,
  1649. MESSAGE_REQ_EXEC_CPG_MCAST);
  1650. req_exec_cpg_mcast.pid = cpd->pid;
  1651. req_exec_cpg_mcast.msglen = msglen;
  1652. api->ipc_source_set (&req_exec_cpg_mcast.source, conn);
  1653. memcpy(&req_exec_cpg_mcast.group_name, &group_name,
  1654. sizeof(mar_cpg_name_t));
  1655. req_exec_cpg_iovec[0].iov_base = (char *)&req_exec_cpg_mcast;
  1656. req_exec_cpg_iovec[0].iov_len = sizeof(req_exec_cpg_mcast);
  1657. req_exec_cpg_iovec[1].iov_base = (char *)&req_lib_cpg_mcast->message;
  1658. req_exec_cpg_iovec[1].iov_len = msglen;
  1659. result = api->totem_mcast (req_exec_cpg_iovec, 2, TOTEM_AGREED);
  1660. assert(result == 0);
  1661. } else {
  1662. log_printf(LOGSYS_LEVEL_ERROR, "*** %p can't mcast to group %s state:%d, error:%d",
  1663. conn, group_name.value, cpd->cpd_state, error);
  1664. }
  1665. }
  1666. static void message_handler_req_lib_cpg_zc_execute (
  1667. void *conn,
  1668. const void *message)
  1669. {
  1670. mar_req_coroipcc_zc_execute_t *hdr = (mar_req_coroipcc_zc_execute_t *)message;
  1671. struct qb_ipc_request_header *header;
  1672. struct res_lib_cpg_mcast res_lib_cpg_mcast;
  1673. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1674. struct iovec req_exec_cpg_iovec[2];
  1675. struct req_exec_cpg_mcast req_exec_cpg_mcast;
  1676. struct req_lib_cpg_mcast *req_lib_cpg_mcast;
  1677. int result;
  1678. cs_error_t error = CS_ERR_NOT_EXIST;
  1679. log_printf(LOGSYS_LEVEL_TRACE, "got ZC mcast request on %p", conn);
  1680. header = (struct qb_ipc_request_header *)(((char *)serveraddr2void(hdr->server_address) + sizeof (struct coroipcs_zc_header)));
  1681. req_lib_cpg_mcast = (struct req_lib_cpg_mcast *)header;
  1682. switch (cpd->cpd_state) {
  1683. case CPD_STATE_UNJOINED:
  1684. error = CS_ERR_NOT_EXIST;
  1685. break;
  1686. case CPD_STATE_LEAVE_STARTED:
  1687. error = CS_ERR_NOT_EXIST;
  1688. break;
  1689. case CPD_STATE_JOIN_STARTED:
  1690. error = CS_OK;
  1691. break;
  1692. case CPD_STATE_JOIN_COMPLETED:
  1693. error = CS_OK;
  1694. break;
  1695. }
  1696. res_lib_cpg_mcast.header.size = sizeof(res_lib_cpg_mcast);
  1697. res_lib_cpg_mcast.header.id = MESSAGE_RES_CPG_MCAST;
  1698. if (error == CS_OK) {
  1699. req_exec_cpg_mcast.header.size = sizeof(req_exec_cpg_mcast) + req_lib_cpg_mcast->msglen;
  1700. req_exec_cpg_mcast.header.id = SERVICE_ID_MAKE(CPG_SERVICE,
  1701. MESSAGE_REQ_EXEC_CPG_MCAST);
  1702. req_exec_cpg_mcast.pid = cpd->pid;
  1703. req_exec_cpg_mcast.msglen = req_lib_cpg_mcast->msglen;
  1704. api->ipc_source_set (&req_exec_cpg_mcast.source, conn);
  1705. memcpy(&req_exec_cpg_mcast.group_name, &cpd->group_name,
  1706. sizeof(mar_cpg_name_t));
  1707. req_exec_cpg_iovec[0].iov_base = (char *)&req_exec_cpg_mcast;
  1708. req_exec_cpg_iovec[0].iov_len = sizeof(req_exec_cpg_mcast);
  1709. req_exec_cpg_iovec[1].iov_base = (char *)header + sizeof(struct req_lib_cpg_mcast);
  1710. req_exec_cpg_iovec[1].iov_len = req_exec_cpg_mcast.msglen;
  1711. result = api->totem_mcast (req_exec_cpg_iovec, 2, TOTEM_AGREED);
  1712. if (result == 0) {
  1713. res_lib_cpg_mcast.header.error = CS_OK;
  1714. } else {
  1715. res_lib_cpg_mcast.header.error = CS_ERR_TRY_AGAIN;
  1716. }
  1717. } else {
  1718. res_lib_cpg_mcast.header.error = error;
  1719. }
  1720. api->ipc_response_send (conn, &res_lib_cpg_mcast,
  1721. sizeof (res_lib_cpg_mcast));
  1722. }
  1723. static void message_handler_req_lib_cpg_membership (void *conn,
  1724. const void *message)
  1725. {
  1726. struct req_lib_cpg_membership_get *req_lib_cpg_membership_get =
  1727. (struct req_lib_cpg_membership_get *)message;
  1728. struct res_lib_cpg_membership_get res_lib_cpg_membership_get;
  1729. struct qb_list_head *iter;
  1730. int member_count = 0;
  1731. res_lib_cpg_membership_get.header.id = MESSAGE_RES_CPG_MEMBERSHIP;
  1732. res_lib_cpg_membership_get.header.error = CS_OK;
  1733. res_lib_cpg_membership_get.header.size =
  1734. sizeof (struct res_lib_cpg_membership_get);
  1735. qb_list_for_each(iter, &process_info_list_head) {
  1736. struct process_info *pi = qb_list_entry (iter, struct process_info, list);
  1737. if (mar_name_compare (&pi->group, &req_lib_cpg_membership_get->group_name) == 0) {
  1738. res_lib_cpg_membership_get.member_list[member_count].nodeid = pi->nodeid;
  1739. res_lib_cpg_membership_get.member_list[member_count].pid = pi->pid;
  1740. member_count += 1;
  1741. }
  1742. }
  1743. res_lib_cpg_membership_get.member_count = member_count;
  1744. api->ipc_response_send (conn, &res_lib_cpg_membership_get,
  1745. sizeof (res_lib_cpg_membership_get));
  1746. }
  1747. static void message_handler_req_lib_cpg_local_get (void *conn,
  1748. const void *message)
  1749. {
  1750. struct res_lib_cpg_local_get res_lib_cpg_local_get;
  1751. res_lib_cpg_local_get.header.size = sizeof (res_lib_cpg_local_get);
  1752. res_lib_cpg_local_get.header.id = MESSAGE_RES_CPG_LOCAL_GET;
  1753. res_lib_cpg_local_get.header.error = CS_OK;
  1754. res_lib_cpg_local_get.local_nodeid = api->totem_nodeid_get ();
  1755. api->ipc_response_send (conn, &res_lib_cpg_local_get,
  1756. sizeof (res_lib_cpg_local_get));
  1757. }
  1758. static void message_handler_req_lib_cpg_iteration_initialize (
  1759. void *conn,
  1760. const void *message)
  1761. {
  1762. const struct req_lib_cpg_iterationinitialize *req_lib_cpg_iterationinitialize = message;
  1763. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1764. hdb_handle_t cpg_iteration_handle = 0;
  1765. struct res_lib_cpg_iterationinitialize res_lib_cpg_iterationinitialize;
  1766. struct qb_list_head *iter, *iter2;
  1767. struct cpg_iteration_instance *cpg_iteration_instance;
  1768. cs_error_t error = CS_OK;
  1769. int res;
  1770. log_printf (LOGSYS_LEVEL_DEBUG, "cpg iteration initialize");
  1771. /* Because between calling this function and *next can be some operations which will
  1772. * change list, we must do full copy.
  1773. */
  1774. /*
  1775. * Create new iteration instance
  1776. */
  1777. res = hdb_handle_create (&cpg_iteration_handle_t_db, sizeof (struct cpg_iteration_instance),
  1778. &cpg_iteration_handle);
  1779. if (res != 0) {
  1780. error = CS_ERR_NO_MEMORY;
  1781. goto response_send;
  1782. }
  1783. res = hdb_handle_get (&cpg_iteration_handle_t_db, cpg_iteration_handle, (void *)&cpg_iteration_instance);
  1784. if (res != 0) {
  1785. error = CS_ERR_BAD_HANDLE;
  1786. goto error_destroy;
  1787. }
  1788. qb_list_init (&cpg_iteration_instance->items_list_head);
  1789. cpg_iteration_instance->handle = cpg_iteration_handle;
  1790. /*
  1791. * Create copy of process_info list "grouped by" group name
  1792. */
  1793. qb_list_for_each(iter, &process_info_list_head) {
  1794. struct process_info *pi = qb_list_entry (iter, struct process_info, list);
  1795. struct process_info *new_pi;
  1796. if (req_lib_cpg_iterationinitialize->iteration_type == CPG_ITERATION_NAME_ONLY) {
  1797. /*
  1798. * Try to find processed group name in our list new list
  1799. */
  1800. int found = 0;
  1801. qb_list_for_each(iter2, &(cpg_iteration_instance->items_list_head)) {
  1802. struct process_info *pi2 = qb_list_entry (iter2, struct process_info, list);
  1803. if (mar_name_compare (&pi2->group, &pi->group) == 0) {
  1804. found = 1;
  1805. break;
  1806. }
  1807. }
  1808. if (found) {
  1809. /*
  1810. * We have this name in list -> don't add
  1811. */
  1812. continue ;
  1813. }
  1814. } else if (req_lib_cpg_iterationinitialize->iteration_type == CPG_ITERATION_ONE_GROUP) {
  1815. /*
  1816. * Test pi group name with request
  1817. */
  1818. if (mar_name_compare (&pi->group, &req_lib_cpg_iterationinitialize->group_name) != 0)
  1819. /*
  1820. * Not same -> don't add
  1821. */
  1822. continue ;
  1823. }
  1824. new_pi = malloc (sizeof (struct process_info));
  1825. if (!new_pi) {
  1826. log_printf(LOGSYS_LEVEL_WARNING, "Unable to allocate process_info struct");
  1827. error = CS_ERR_NO_MEMORY;
  1828. goto error_put_destroy;
  1829. }
  1830. memcpy (new_pi, pi, sizeof (struct process_info));
  1831. qb_list_init (&new_pi->list);
  1832. if (req_lib_cpg_iterationinitialize->iteration_type == CPG_ITERATION_NAME_ONLY) {
  1833. /*
  1834. * pid and nodeid -> undefined
  1835. */
  1836. new_pi->pid = new_pi->nodeid = 0;
  1837. }
  1838. /*
  1839. * We will return list "grouped" by "group name", so try to find right place to add
  1840. */
  1841. qb_list_for_each(iter2, &(cpg_iteration_instance->items_list_head)) {
  1842. struct process_info *pi2 = qb_list_entry (iter2, struct process_info, list);
  1843. if (mar_name_compare (&pi2->group, &pi->group) == 0) {
  1844. break;
  1845. }
  1846. }
  1847. qb_list_add (&new_pi->list, iter2);
  1848. }
  1849. /*
  1850. * Now we have a full "grouped by" copy of process_info list
  1851. */
  1852. /*
  1853. * Add instance to current cpd list
  1854. */
  1855. qb_list_init (&cpg_iteration_instance->list);
  1856. qb_list_add (&cpg_iteration_instance->list, &cpd->iteration_instance_list_head);
  1857. cpg_iteration_instance->current_pointer = &cpg_iteration_instance->items_list_head;
  1858. error_put_destroy:
  1859. hdb_handle_put (&cpg_iteration_handle_t_db, cpg_iteration_handle);
  1860. error_destroy:
  1861. if (error != CS_OK) {
  1862. hdb_handle_destroy (&cpg_iteration_handle_t_db, cpg_iteration_handle);
  1863. }
  1864. response_send:
  1865. res_lib_cpg_iterationinitialize.header.size = sizeof (res_lib_cpg_iterationinitialize);
  1866. res_lib_cpg_iterationinitialize.header.id = MESSAGE_RES_CPG_ITERATIONINITIALIZE;
  1867. res_lib_cpg_iterationinitialize.header.error = error;
  1868. res_lib_cpg_iterationinitialize.iteration_handle = cpg_iteration_handle;
  1869. api->ipc_response_send (conn, &res_lib_cpg_iterationinitialize,
  1870. sizeof (res_lib_cpg_iterationinitialize));
  1871. }
  1872. static void message_handler_req_lib_cpg_iteration_next (
  1873. void *conn,
  1874. const void *message)
  1875. {
  1876. const struct req_lib_cpg_iterationnext *req_lib_cpg_iterationnext = message;
  1877. struct res_lib_cpg_iterationnext res_lib_cpg_iterationnext;
  1878. struct cpg_iteration_instance *cpg_iteration_instance;
  1879. cs_error_t error = CS_OK;
  1880. int res;
  1881. struct process_info *pi;
  1882. log_printf (LOGSYS_LEVEL_DEBUG, "cpg iteration next");
  1883. res = hdb_handle_get (&cpg_iteration_handle_t_db,
  1884. req_lib_cpg_iterationnext->iteration_handle,
  1885. (void *)&cpg_iteration_instance);
  1886. if (res != 0) {
  1887. error = CS_ERR_LIBRARY;
  1888. goto error_exit;
  1889. }
  1890. assert (cpg_iteration_instance);
  1891. cpg_iteration_instance->current_pointer = cpg_iteration_instance->current_pointer->next;
  1892. if (cpg_iteration_instance->current_pointer == &cpg_iteration_instance->items_list_head) {
  1893. error = CS_ERR_NO_SECTIONS;
  1894. goto error_put;
  1895. }
  1896. pi = qb_list_entry (cpg_iteration_instance->current_pointer, struct process_info, list);
  1897. /*
  1898. * Copy iteration data
  1899. */
  1900. res_lib_cpg_iterationnext.description.nodeid = pi->nodeid;
  1901. res_lib_cpg_iterationnext.description.pid = pi->pid;
  1902. memcpy (&res_lib_cpg_iterationnext.description.group,
  1903. &pi->group,
  1904. sizeof (mar_cpg_name_t));
  1905. error_put:
  1906. hdb_handle_put (&cpg_iteration_handle_t_db, req_lib_cpg_iterationnext->iteration_handle);
  1907. error_exit:
  1908. res_lib_cpg_iterationnext.header.size = sizeof (res_lib_cpg_iterationnext);
  1909. res_lib_cpg_iterationnext.header.id = MESSAGE_RES_CPG_ITERATIONNEXT;
  1910. res_lib_cpg_iterationnext.header.error = error;
  1911. api->ipc_response_send (conn, &res_lib_cpg_iterationnext,
  1912. sizeof (res_lib_cpg_iterationnext));
  1913. }
  1914. static void message_handler_req_lib_cpg_iteration_finalize (
  1915. void *conn,
  1916. const void *message)
  1917. {
  1918. const struct req_lib_cpg_iterationfinalize *req_lib_cpg_iterationfinalize = message;
  1919. struct res_lib_cpg_iterationfinalize res_lib_cpg_iterationfinalize;
  1920. struct cpg_iteration_instance *cpg_iteration_instance;
  1921. cs_error_t error = CS_OK;
  1922. int res;
  1923. log_printf (LOGSYS_LEVEL_DEBUG, "cpg iteration finalize");
  1924. res = hdb_handle_get (&cpg_iteration_handle_t_db,
  1925. req_lib_cpg_iterationfinalize->iteration_handle,
  1926. (void *)&cpg_iteration_instance);
  1927. if (res != 0) {
  1928. error = CS_ERR_LIBRARY;
  1929. goto error_exit;
  1930. }
  1931. assert (cpg_iteration_instance);
  1932. cpg_iteration_instance_finalize (cpg_iteration_instance);
  1933. hdb_handle_put (&cpg_iteration_handle_t_db, cpg_iteration_instance->handle);
  1934. error_exit:
  1935. res_lib_cpg_iterationfinalize.header.size = sizeof (res_lib_cpg_iterationfinalize);
  1936. res_lib_cpg_iterationfinalize.header.id = MESSAGE_RES_CPG_ITERATIONFINALIZE;
  1937. res_lib_cpg_iterationfinalize.header.error = error;
  1938. api->ipc_response_send (conn, &res_lib_cpg_iterationfinalize,
  1939. sizeof (res_lib_cpg_iterationfinalize));
  1940. }