cpg.c 66 KB

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