4
0

cpg.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344
  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. memset(&req_exec_cpg_procjoin, 0, sizeof(req_exec_cpg_procjoin));
  916. memcpy(&req_exec_cpg_procjoin.group_name, group_name, sizeof(mar_cpg_name_t));
  917. req_exec_cpg_procjoin.pid = pid;
  918. req_exec_cpg_procjoin.reason = reason;
  919. req_exec_cpg_procjoin.header.size = sizeof(req_exec_cpg_procjoin);
  920. req_exec_cpg_procjoin.header.id = SERVICE_ID_MAKE(CPG_SERVICE, fn);
  921. req_exec_cpg_iovec.iov_base = (char *)&req_exec_cpg_procjoin;
  922. req_exec_cpg_iovec.iov_len = sizeof(req_exec_cpg_procjoin);
  923. result = api->totem_mcast (&req_exec_cpg_iovec, 1, TOTEM_AGREED);
  924. return (result);
  925. }
  926. /* Can byteswap join & leave messages */
  927. static void exec_cpg_procjoin_endian_convert (void *msg)
  928. {
  929. struct req_exec_cpg_procjoin *req_exec_cpg_procjoin = msg;
  930. req_exec_cpg_procjoin->pid = swab32(req_exec_cpg_procjoin->pid);
  931. swab_mar_cpg_name_t (&req_exec_cpg_procjoin->group_name);
  932. req_exec_cpg_procjoin->reason = swab32(req_exec_cpg_procjoin->reason);
  933. }
  934. static void exec_cpg_joinlist_endian_convert (void *msg_v)
  935. {
  936. char *msg = msg_v;
  937. struct qb_ipc_response_header *res = (struct qb_ipc_response_header *)msg;
  938. struct join_list_entry *jle = (struct join_list_entry *)(msg + sizeof(struct qb_ipc_response_header));
  939. swab_mar_int32_t (&res->size);
  940. while ((const char*)jle < msg + res->size) {
  941. jle->pid = swab32(jle->pid);
  942. swab_mar_cpg_name_t (&jle->group_name);
  943. jle++;
  944. }
  945. }
  946. static void exec_cpg_downlist_endian_convert_old (void *msg)
  947. {
  948. }
  949. static void exec_cpg_downlist_endian_convert (void *msg)
  950. {
  951. struct req_exec_cpg_downlist *req_exec_cpg_downlist = msg;
  952. unsigned int i;
  953. req_exec_cpg_downlist->left_nodes = swab32(req_exec_cpg_downlist->left_nodes);
  954. req_exec_cpg_downlist->old_members = swab32(req_exec_cpg_downlist->old_members);
  955. for (i = 0; i < req_exec_cpg_downlist->left_nodes; i++) {
  956. req_exec_cpg_downlist->nodeids[i] = swab32(req_exec_cpg_downlist->nodeids[i]);
  957. }
  958. }
  959. static void exec_cpg_mcast_endian_convert (void *msg)
  960. {
  961. struct req_exec_cpg_mcast *req_exec_cpg_mcast = msg;
  962. swab_coroipc_request_header_t (&req_exec_cpg_mcast->header);
  963. swab_mar_cpg_name_t (&req_exec_cpg_mcast->group_name);
  964. req_exec_cpg_mcast->pid = swab32(req_exec_cpg_mcast->pid);
  965. req_exec_cpg_mcast->msglen = swab32(req_exec_cpg_mcast->msglen);
  966. swab_mar_message_source_t (&req_exec_cpg_mcast->source);
  967. }
  968. static void exec_cpg_partial_mcast_endian_convert (void *msg)
  969. {
  970. struct req_exec_cpg_partial_mcast *req_exec_cpg_mcast = msg;
  971. swab_coroipc_request_header_t (&req_exec_cpg_mcast->header);
  972. swab_mar_cpg_name_t (&req_exec_cpg_mcast->group_name);
  973. req_exec_cpg_mcast->pid = swab32(req_exec_cpg_mcast->pid);
  974. req_exec_cpg_mcast->msglen = swab32(req_exec_cpg_mcast->msglen);
  975. req_exec_cpg_mcast->fraglen = swab32(req_exec_cpg_mcast->fraglen);
  976. req_exec_cpg_mcast->type = swab32(req_exec_cpg_mcast->type);
  977. swab_mar_message_source_t (&req_exec_cpg_mcast->source);
  978. }
  979. static struct process_info *process_info_find(const mar_cpg_name_t *group_name, uint32_t pid, unsigned int nodeid) {
  980. struct qb_list_head *iter;
  981. qb_list_for_each(iter, &process_info_list_head) {
  982. struct process_info *pi = qb_list_entry (iter, struct process_info, list);
  983. if (pi->pid == pid && pi->nodeid == nodeid &&
  984. mar_name_compare (&pi->group, group_name) == 0) {
  985. return pi;
  986. }
  987. }
  988. return NULL;
  989. }
  990. static void do_proc_join(
  991. const mar_cpg_name_t *name,
  992. uint32_t pid,
  993. unsigned int nodeid,
  994. int reason,
  995. qb_map_t *group_notify_map)
  996. {
  997. struct process_info *pi;
  998. struct process_info *pi_entry;
  999. mar_cpg_address_t notify_info;
  1000. struct qb_list_head *list;
  1001. struct qb_list_head *list_to_add = NULL;
  1002. int size;
  1003. if (process_info_find (name, pid, nodeid) != NULL) {
  1004. return ;
  1005. }
  1006. pi = malloc (sizeof (struct process_info));
  1007. if (!pi) {
  1008. log_printf(LOGSYS_LEVEL_WARNING, "Unable to allocate process_info struct");
  1009. return;
  1010. }
  1011. pi->nodeid = nodeid;
  1012. pi->pid = pid;
  1013. memcpy(&pi->group, name, sizeof(*name));
  1014. qb_list_init(&pi->list);
  1015. /*
  1016. * Insert new process in sorted order so synchronization works properly
  1017. */
  1018. list_to_add = &process_info_list_head;
  1019. qb_list_for_each(list, &process_info_list_head) {
  1020. pi_entry = qb_list_entry(list, struct process_info, list);
  1021. if (pi_entry->nodeid > pi->nodeid ||
  1022. (pi_entry->nodeid == pi->nodeid && pi_entry->pid > pi->pid)) {
  1023. break;
  1024. }
  1025. list_to_add = list;
  1026. }
  1027. qb_list_add (&pi->list, list_to_add);
  1028. notify_info.pid = pi->pid;
  1029. notify_info.nodeid = nodeid;
  1030. notify_info.reason = reason;
  1031. if (group_notify_map == NULL) {
  1032. notify_lib_joinlist(&pi->group,
  1033. 1, &notify_info,
  1034. 0, NULL,
  1035. MESSAGE_RES_CPG_CONFCHG_CALLBACK);
  1036. } else {
  1037. struct join_list_confchg_data *jld = qb_map_get(group_notify_map, pi->group.value);
  1038. if (jld == NULL) {
  1039. jld = (struct join_list_confchg_data *)calloc(1, sizeof(struct join_list_confchg_data));
  1040. memcpy(&jld->cpg_group, &pi->group, sizeof(mar_cpg_name_t));
  1041. qb_map_put(group_notify_map, jld->cpg_group.value, jld);
  1042. }
  1043. size = jld->join_list_entries;
  1044. jld->join_list[size].nodeid = notify_info.nodeid;
  1045. jld->join_list[size].pid = notify_info.pid;
  1046. jld->join_list[size].reason = notify_info.reason;
  1047. jld->join_list_entries++;
  1048. }
  1049. }
  1050. static void do_proc_leave(
  1051. const mar_cpg_name_t *name,
  1052. uint32_t pid,
  1053. unsigned int nodeid,
  1054. int reason)
  1055. {
  1056. struct process_info *pi;
  1057. struct qb_list_head *iter, *tmp_iter;
  1058. mar_cpg_address_t notify_info;
  1059. notify_info.pid = pid;
  1060. notify_info.nodeid = nodeid;
  1061. notify_info.reason = reason;
  1062. notify_lib_joinlist(name,
  1063. 0, NULL,
  1064. 1, &notify_info,
  1065. MESSAGE_RES_CPG_CONFCHG_CALLBACK);
  1066. qb_list_for_each_safe(iter, tmp_iter, &process_info_list_head) {
  1067. pi = qb_list_entry(iter, struct process_info, list);
  1068. if (pi->pid == pid && pi->nodeid == nodeid &&
  1069. mar_name_compare (&pi->group, name)==0) {
  1070. qb_list_del (&pi->list);
  1071. free (pi);
  1072. }
  1073. }
  1074. }
  1075. static void message_handler_req_exec_cpg_downlist_old (
  1076. const void *message,
  1077. unsigned int nodeid)
  1078. {
  1079. log_printf (LOGSYS_LEVEL_DEBUG, "downlist OLD from node " CS_PRI_NODE_ID,
  1080. nodeid);
  1081. }
  1082. static void message_handler_req_exec_cpg_downlist(
  1083. const void *message,
  1084. unsigned int nodeid)
  1085. {
  1086. const struct req_exec_cpg_downlist *req_exec_cpg_downlist = message;
  1087. log_printf (LOGSYS_LEVEL_DEBUG, "downlist left_list: %d received",
  1088. req_exec_cpg_downlist->left_nodes);
  1089. }
  1090. static void message_handler_req_exec_cpg_procjoin (
  1091. const void *message,
  1092. unsigned int nodeid)
  1093. {
  1094. const struct req_exec_cpg_procjoin *req_exec_cpg_procjoin = message;
  1095. log_printf(LOGSYS_LEVEL_DEBUG, "got procjoin message from cluster node " CS_PRI_NODE_ID " (%s) for pid %u",
  1096. nodeid,
  1097. api->totem_ifaces_print(nodeid),
  1098. (unsigned int)req_exec_cpg_procjoin->pid);
  1099. do_proc_join (&req_exec_cpg_procjoin->group_name,
  1100. req_exec_cpg_procjoin->pid, nodeid,
  1101. CONFCHG_CPG_REASON_JOIN, NULL);
  1102. }
  1103. static void message_handler_req_exec_cpg_procleave (
  1104. const void *message,
  1105. unsigned int nodeid)
  1106. {
  1107. const struct req_exec_cpg_procjoin *req_exec_cpg_procjoin = message;
  1108. log_printf(LOGSYS_LEVEL_DEBUG, "got procleave message from cluster node " CS_PRI_NODE_ID " (%s) for pid %u",
  1109. nodeid,
  1110. api->totem_ifaces_print(nodeid),
  1111. (unsigned int)req_exec_cpg_procjoin->pid);
  1112. do_proc_leave (&req_exec_cpg_procjoin->group_name,
  1113. req_exec_cpg_procjoin->pid, nodeid,
  1114. req_exec_cpg_procjoin->reason);
  1115. }
  1116. /* Got a proclist from another node */
  1117. static void message_handler_req_exec_cpg_joinlist (
  1118. const void *message_v,
  1119. unsigned int nodeid)
  1120. {
  1121. const char *message = message_v;
  1122. const struct qb_ipc_response_header *res = (const struct qb_ipc_response_header *)message;
  1123. const struct join_list_entry *jle = (const struct join_list_entry *)(message + sizeof(struct qb_ipc_response_header));
  1124. struct joinlist_msg *stored_msg;
  1125. log_printf(LOGSYS_LEVEL_DEBUG, "got joinlist message from node " CS_PRI_NODE_ID,
  1126. nodeid);
  1127. while ((const char*)jle < message + res->size) {
  1128. stored_msg = malloc (sizeof (struct joinlist_msg));
  1129. memset(stored_msg, 0, sizeof (struct joinlist_msg));
  1130. stored_msg->sender_nodeid = nodeid;
  1131. stored_msg->pid = jle->pid;
  1132. memcpy(&stored_msg->group_name, &jle->group_name, sizeof(mar_cpg_name_t));
  1133. qb_list_init (&stored_msg->list);
  1134. qb_list_add (&stored_msg->list, &joinlist_messages_head);
  1135. jle++;
  1136. }
  1137. }
  1138. static void message_handler_req_exec_cpg_mcast (
  1139. const void *message,
  1140. unsigned int nodeid)
  1141. {
  1142. const struct req_exec_cpg_mcast *req_exec_cpg_mcast = message;
  1143. struct res_lib_cpg_deliver_callback res_lib_cpg_mcast;
  1144. int msglen = req_exec_cpg_mcast->msglen;
  1145. struct qb_list_head *iter, *pi_iter, *tmp_iter;
  1146. struct cpg_pd *cpd;
  1147. struct iovec iovec[2];
  1148. int known_node = 0;
  1149. res_lib_cpg_mcast.header.id = MESSAGE_RES_CPG_DELIVER_CALLBACK;
  1150. res_lib_cpg_mcast.header.size = sizeof(res_lib_cpg_mcast) + msglen;
  1151. res_lib_cpg_mcast.msglen = msglen;
  1152. res_lib_cpg_mcast.pid = req_exec_cpg_mcast->pid;
  1153. res_lib_cpg_mcast.nodeid = nodeid;
  1154. memcpy(&res_lib_cpg_mcast.group_name, &req_exec_cpg_mcast->group_name,
  1155. sizeof(mar_cpg_name_t));
  1156. iovec[0].iov_base = (void *)&res_lib_cpg_mcast;
  1157. iovec[0].iov_len = sizeof (res_lib_cpg_mcast);
  1158. iovec[1].iov_base = (char*)message+sizeof(*req_exec_cpg_mcast);
  1159. iovec[1].iov_len = msglen;
  1160. qb_list_for_each_safe(iter, tmp_iter, &cpg_pd_list_head) {
  1161. cpd = qb_list_entry(iter, struct cpg_pd, list);
  1162. if ((cpd->cpd_state == CPD_STATE_LEAVE_STARTED || cpd->cpd_state == CPD_STATE_JOIN_COMPLETED)
  1163. && (mar_name_compare (&cpd->group_name, &req_exec_cpg_mcast->group_name) == 0)) {
  1164. if (!known_node) {
  1165. /* Try to find, if we know the node */
  1166. qb_list_for_each(pi_iter, &process_info_list_head) {
  1167. struct process_info *pi = qb_list_entry (pi_iter, struct process_info, list);
  1168. if (pi->nodeid == nodeid &&
  1169. mar_name_compare (&pi->group, &req_exec_cpg_mcast->group_name) == 0) {
  1170. known_node = 1;
  1171. break;
  1172. }
  1173. }
  1174. }
  1175. if (!known_node) {
  1176. log_printf(LOGSYS_LEVEL_WARNING, "Unknown node -> we will not deliver message");
  1177. return ;
  1178. }
  1179. api->ipc_dispatch_iov_send (cpd->conn, iovec, 2);
  1180. }
  1181. }
  1182. }
  1183. static void message_handler_req_exec_cpg_partial_mcast (
  1184. const void *message,
  1185. unsigned int nodeid)
  1186. {
  1187. const struct req_exec_cpg_partial_mcast *req_exec_cpg_mcast = message;
  1188. struct res_lib_cpg_partial_deliver_callback res_lib_cpg_mcast;
  1189. int msglen = req_exec_cpg_mcast->fraglen;
  1190. struct qb_list_head *iter, *pi_iter, *tmp_iter;
  1191. struct cpg_pd *cpd;
  1192. struct iovec iovec[2];
  1193. int known_node = 0;
  1194. log_printf(LOGSYS_LEVEL_DEBUG, "Got fragmented message from node " CS_PRI_NODE_ID ", size = %d bytes\n", nodeid, msglen);
  1195. res_lib_cpg_mcast.header.id = MESSAGE_RES_CPG_PARTIAL_DELIVER_CALLBACK;
  1196. res_lib_cpg_mcast.header.size = sizeof(res_lib_cpg_mcast) + msglen;
  1197. res_lib_cpg_mcast.fraglen = msglen;
  1198. res_lib_cpg_mcast.msglen = req_exec_cpg_mcast->msglen;
  1199. res_lib_cpg_mcast.pid = req_exec_cpg_mcast->pid;
  1200. res_lib_cpg_mcast.type = req_exec_cpg_mcast->type;
  1201. res_lib_cpg_mcast.nodeid = nodeid;
  1202. memcpy(&res_lib_cpg_mcast.group_name, &req_exec_cpg_mcast->group_name,
  1203. sizeof(mar_cpg_name_t));
  1204. iovec[0].iov_base = (void *)&res_lib_cpg_mcast;
  1205. iovec[0].iov_len = sizeof (res_lib_cpg_mcast);
  1206. iovec[1].iov_base = (char*)message+sizeof(*req_exec_cpg_mcast);
  1207. iovec[1].iov_len = msglen;
  1208. qb_list_for_each_safe(iter, tmp_iter, &cpg_pd_list_head) {
  1209. cpd = qb_list_entry(iter, struct cpg_pd, list);
  1210. if ((cpd->cpd_state == CPD_STATE_LEAVE_STARTED || cpd->cpd_state == CPD_STATE_JOIN_COMPLETED)
  1211. && (mar_name_compare (&cpd->group_name, &req_exec_cpg_mcast->group_name) == 0)) {
  1212. if (!known_node) {
  1213. /* Try to find, if we know the node */
  1214. qb_list_for_each(pi_iter, &process_info_list_head) {
  1215. struct process_info *pi = qb_list_entry (pi_iter, struct process_info, list);
  1216. if (pi->nodeid == nodeid &&
  1217. mar_name_compare (&pi->group, &req_exec_cpg_mcast->group_name) == 0) {
  1218. known_node = 1;
  1219. break;
  1220. }
  1221. }
  1222. }
  1223. if (!known_node) {
  1224. log_printf(LOGSYS_LEVEL_WARNING, "Unknown node -> we will not deliver message");
  1225. return ;
  1226. }
  1227. api->ipc_dispatch_iov_send (cpd->conn, iovec, 2);
  1228. }
  1229. }
  1230. }
  1231. static int cpg_exec_send_downlist(void)
  1232. {
  1233. struct iovec iov;
  1234. g_req_exec_cpg_downlist.header.id = SERVICE_ID_MAKE(CPG_SERVICE, MESSAGE_REQ_EXEC_CPG_DOWNLIST);
  1235. g_req_exec_cpg_downlist.header.size = sizeof(struct req_exec_cpg_downlist);
  1236. g_req_exec_cpg_downlist.old_members = my_old_member_list_entries;
  1237. iov.iov_base = (void *)&g_req_exec_cpg_downlist;
  1238. iov.iov_len = g_req_exec_cpg_downlist.header.size;
  1239. return (api->totem_mcast (&iov, 1, TOTEM_AGREED));
  1240. }
  1241. static int cpg_exec_send_joinlist(void)
  1242. {
  1243. int count = 0;
  1244. struct qb_list_head *iter;
  1245. struct qb_ipc_response_header *res;
  1246. char *buf;
  1247. size_t buf_size;
  1248. struct join_list_entry *jle;
  1249. struct iovec req_exec_cpg_iovec;
  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. count++;
  1254. }
  1255. }
  1256. /* Nothing to send */
  1257. if (!count)
  1258. return 0;
  1259. buf_size = sizeof(struct qb_ipc_response_header) + sizeof(struct join_list_entry) * count;
  1260. buf = alloca(buf_size);
  1261. if (!buf) {
  1262. log_printf(LOGSYS_LEVEL_WARNING, "Unable to allocate joinlist buffer");
  1263. return -1;
  1264. }
  1265. memset(buf, 0, buf_size);
  1266. jle = (struct join_list_entry *)(buf + sizeof(struct qb_ipc_response_header));
  1267. res = (struct qb_ipc_response_header *)buf;
  1268. qb_list_for_each(iter, &process_info_list_head) {
  1269. struct process_info *pi = qb_list_entry (iter, struct process_info, list);
  1270. if (pi->nodeid == api->totem_nodeid_get ()) {
  1271. memcpy (&jle->group_name, &pi->group, sizeof (mar_cpg_name_t));
  1272. jle->pid = pi->pid;
  1273. jle++;
  1274. }
  1275. }
  1276. res->id = SERVICE_ID_MAKE(CPG_SERVICE, MESSAGE_REQ_EXEC_CPG_JOINLIST);
  1277. res->size = sizeof(struct qb_ipc_response_header)+sizeof(struct join_list_entry) * count;
  1278. req_exec_cpg_iovec.iov_base = buf;
  1279. req_exec_cpg_iovec.iov_len = res->size;
  1280. return (api->totem_mcast (&req_exec_cpg_iovec, 1, TOTEM_AGREED));
  1281. }
  1282. static int cpg_lib_init_fn (void *conn)
  1283. {
  1284. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1285. memset (cpd, 0, sizeof(struct cpg_pd));
  1286. cpd->conn = conn;
  1287. qb_list_add (&cpd->list, &cpg_pd_list_head);
  1288. qb_list_init (&cpd->iteration_instance_list_head);
  1289. qb_list_init (&cpd->zcb_mapped_list_head);
  1290. api->ipc_refcnt_inc (conn);
  1291. log_printf(LOGSYS_LEVEL_DEBUG, "lib_init_fn: conn=%p, cpd=%p", conn, cpd);
  1292. return (0);
  1293. }
  1294. /* Join message from the library */
  1295. static void message_handler_req_lib_cpg_join (void *conn, const void *message)
  1296. {
  1297. const struct req_lib_cpg_join *req_lib_cpg_join = message;
  1298. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1299. struct res_lib_cpg_join res_lib_cpg_join;
  1300. cs_error_t error = CS_OK;
  1301. struct qb_list_head *iter;
  1302. /* Test, if we don't have same pid and group name joined */
  1303. qb_list_for_each(iter, &cpg_pd_list_head) {
  1304. struct cpg_pd *cpd_item = qb_list_entry (iter, struct cpg_pd, list);
  1305. if (cpd_item->pid == req_lib_cpg_join->pid &&
  1306. mar_name_compare(&req_lib_cpg_join->group_name, &cpd_item->group_name) == 0) {
  1307. /* We have same pid and group name joined -> return error */
  1308. error = CS_ERR_EXIST;
  1309. goto response_send;
  1310. }
  1311. }
  1312. /*
  1313. * Same check must be done in process info list, because there may be not yet delivered
  1314. * leave of client.
  1315. */
  1316. qb_list_for_each(iter, &process_info_list_head) {
  1317. struct process_info *pi = qb_list_entry (iter, struct process_info, list);
  1318. if (pi->nodeid == api->totem_nodeid_get () && pi->pid == req_lib_cpg_join->pid &&
  1319. mar_name_compare(&req_lib_cpg_join->group_name, &pi->group) == 0) {
  1320. /* We have same pid and group name joined -> return error */
  1321. error = CS_ERR_TRY_AGAIN;
  1322. goto response_send;
  1323. }
  1324. }
  1325. if (req_lib_cpg_join->group_name.length > CPG_MAX_NAME_LENGTH) {
  1326. error = CS_ERR_NAME_TOO_LONG;
  1327. goto response_send;
  1328. }
  1329. switch (cpd->cpd_state) {
  1330. case CPD_STATE_UNJOINED:
  1331. error = CS_OK;
  1332. cpd->cpd_state = CPD_STATE_JOIN_STARTED;
  1333. cpd->pid = req_lib_cpg_join->pid;
  1334. cpd->flags = req_lib_cpg_join->flags;
  1335. memcpy (&cpd->group_name, &req_lib_cpg_join->group_name,
  1336. sizeof (cpd->group_name));
  1337. cpg_node_joinleave_send (req_lib_cpg_join->pid,
  1338. &req_lib_cpg_join->group_name,
  1339. MESSAGE_REQ_EXEC_CPG_PROCJOIN, CONFCHG_CPG_REASON_JOIN);
  1340. break;
  1341. case CPD_STATE_LEAVE_STARTED:
  1342. error = CS_ERR_BUSY;
  1343. break;
  1344. case CPD_STATE_JOIN_STARTED:
  1345. error = CS_ERR_EXIST;
  1346. break;
  1347. case CPD_STATE_JOIN_COMPLETED:
  1348. error = CS_ERR_EXIST;
  1349. break;
  1350. }
  1351. response_send:
  1352. res_lib_cpg_join.header.size = sizeof(res_lib_cpg_join);
  1353. res_lib_cpg_join.header.id = MESSAGE_RES_CPG_JOIN;
  1354. res_lib_cpg_join.header.error = error;
  1355. api->ipc_response_send (conn, &res_lib_cpg_join, sizeof(res_lib_cpg_join));
  1356. }
  1357. /* Leave message from the library */
  1358. static void message_handler_req_lib_cpg_leave (void *conn, const void *message)
  1359. {
  1360. struct res_lib_cpg_leave res_lib_cpg_leave;
  1361. cs_error_t error = CS_OK;
  1362. struct req_lib_cpg_leave *req_lib_cpg_leave = (struct req_lib_cpg_leave *)message;
  1363. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1364. log_printf(LOGSYS_LEVEL_DEBUG, "got leave request on %p", conn);
  1365. switch (cpd->cpd_state) {
  1366. case CPD_STATE_UNJOINED:
  1367. error = CS_ERR_NOT_EXIST;
  1368. break;
  1369. case CPD_STATE_LEAVE_STARTED:
  1370. error = CS_ERR_NOT_EXIST;
  1371. break;
  1372. case CPD_STATE_JOIN_STARTED:
  1373. error = CS_ERR_BUSY;
  1374. break;
  1375. case CPD_STATE_JOIN_COMPLETED:
  1376. error = CS_OK;
  1377. cpd->cpd_state = CPD_STATE_LEAVE_STARTED;
  1378. cpg_node_joinleave_send (req_lib_cpg_leave->pid,
  1379. &req_lib_cpg_leave->group_name,
  1380. MESSAGE_REQ_EXEC_CPG_PROCLEAVE,
  1381. CONFCHG_CPG_REASON_LEAVE);
  1382. break;
  1383. }
  1384. /* send return */
  1385. res_lib_cpg_leave.header.size = sizeof(res_lib_cpg_leave);
  1386. res_lib_cpg_leave.header.id = MESSAGE_RES_CPG_LEAVE;
  1387. res_lib_cpg_leave.header.error = error;
  1388. api->ipc_response_send(conn, &res_lib_cpg_leave, sizeof(res_lib_cpg_leave));
  1389. }
  1390. /* Finalize message from library */
  1391. static void message_handler_req_lib_cpg_finalize (
  1392. void *conn,
  1393. const void *message)
  1394. {
  1395. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1396. struct res_lib_cpg_finalize res_lib_cpg_finalize;
  1397. cs_error_t error = CS_OK;
  1398. log_printf (LOGSYS_LEVEL_DEBUG, "cpg finalize for conn=%p", conn);
  1399. /*
  1400. * We will just remove cpd from list. After this call, connection will be
  1401. * closed on lib side, and cpg_lib_exit_fn will be called
  1402. */
  1403. qb_list_del (&cpd->list);
  1404. qb_list_init (&cpd->list);
  1405. res_lib_cpg_finalize.header.size = sizeof (res_lib_cpg_finalize);
  1406. res_lib_cpg_finalize.header.id = MESSAGE_RES_CPG_FINALIZE;
  1407. res_lib_cpg_finalize.header.error = error;
  1408. api->ipc_response_send (conn, &res_lib_cpg_finalize,
  1409. sizeof (res_lib_cpg_finalize));
  1410. }
  1411. static int
  1412. memory_map (
  1413. const char *path,
  1414. size_t bytes,
  1415. void **buf)
  1416. {
  1417. int32_t fd;
  1418. void *addr;
  1419. int32_t res;
  1420. fd = open (path, O_RDWR, 0600);
  1421. unlink (path);
  1422. if (fd == -1) {
  1423. return (-1);
  1424. }
  1425. res = ftruncate (fd, bytes);
  1426. if (res == -1) {
  1427. goto error_close_unlink;
  1428. }
  1429. addr = mmap (NULL, bytes, PROT_READ | PROT_WRITE,
  1430. MAP_SHARED, fd, 0);
  1431. if (addr == MAP_FAILED) {
  1432. goto error_close_unlink;
  1433. }
  1434. #ifdef MADV_NOSYNC
  1435. madvise(addr, bytes, MADV_NOSYNC);
  1436. #endif
  1437. res = close (fd);
  1438. if (res) {
  1439. munmap (addr, bytes);
  1440. return (-1);
  1441. }
  1442. *buf = addr;
  1443. return (0);
  1444. error_close_unlink:
  1445. close (fd);
  1446. unlink(path);
  1447. return -1;
  1448. }
  1449. static inline int zcb_alloc (
  1450. struct cpg_pd *cpd,
  1451. const char *path_to_file,
  1452. size_t size,
  1453. void **addr)
  1454. {
  1455. struct zcb_mapped *zcb_mapped;
  1456. unsigned int res;
  1457. zcb_mapped = malloc (sizeof (struct zcb_mapped));
  1458. if (zcb_mapped == NULL) {
  1459. return (-1);
  1460. }
  1461. res = memory_map (
  1462. path_to_file,
  1463. size,
  1464. addr);
  1465. if (res == -1) {
  1466. free (zcb_mapped);
  1467. return (-1);
  1468. }
  1469. qb_list_init (&zcb_mapped->list);
  1470. zcb_mapped->addr = *addr;
  1471. zcb_mapped->size = size;
  1472. qb_list_add_tail (&zcb_mapped->list, &cpd->zcb_mapped_list_head);
  1473. return (0);
  1474. }
  1475. static inline int zcb_free (struct zcb_mapped *zcb_mapped)
  1476. {
  1477. unsigned int res;
  1478. res = munmap (zcb_mapped->addr, zcb_mapped->size);
  1479. qb_list_del (&zcb_mapped->list);
  1480. free (zcb_mapped);
  1481. return (res);
  1482. }
  1483. static inline int zcb_by_addr_free (struct cpg_pd *cpd, void *addr)
  1484. {
  1485. struct qb_list_head *list, *tmp_iter;
  1486. struct zcb_mapped *zcb_mapped;
  1487. unsigned int res = 0;
  1488. qb_list_for_each_safe(list, tmp_iter, &(cpd->zcb_mapped_list_head)) {
  1489. zcb_mapped = qb_list_entry (list, struct zcb_mapped, list);
  1490. if (zcb_mapped->addr == addr) {
  1491. res = zcb_free (zcb_mapped);
  1492. break;
  1493. }
  1494. }
  1495. return (res);
  1496. }
  1497. static inline int zcb_all_free (
  1498. struct cpg_pd *cpd)
  1499. {
  1500. struct qb_list_head *list, *tmp_iter;
  1501. struct zcb_mapped *zcb_mapped;
  1502. qb_list_for_each_safe(list, tmp_iter, &(cpd->zcb_mapped_list_head)) {
  1503. zcb_mapped = qb_list_entry (list, struct zcb_mapped, list);
  1504. zcb_free (zcb_mapped);
  1505. }
  1506. return (0);
  1507. }
  1508. union u {
  1509. uint64_t server_addr;
  1510. void *server_ptr;
  1511. };
  1512. static uint64_t void2serveraddr (void *server_ptr)
  1513. {
  1514. union u u;
  1515. u.server_ptr = server_ptr;
  1516. return (u.server_addr);
  1517. }
  1518. static void *serveraddr2void (uint64_t server_addr)
  1519. {
  1520. union u u;
  1521. u.server_addr = server_addr;
  1522. return (u.server_ptr);
  1523. };
  1524. static void message_handler_req_lib_cpg_zc_alloc (
  1525. void *conn,
  1526. const void *message)
  1527. {
  1528. mar_req_coroipcc_zc_alloc_t *hdr = (mar_req_coroipcc_zc_alloc_t *)message;
  1529. struct qb_ipc_response_header res_header;
  1530. void *addr = NULL;
  1531. struct coroipcs_zc_header *zc_header;
  1532. unsigned int res;
  1533. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1534. log_printf(LOGSYS_LEVEL_DEBUG, "path: %s", hdr->path_to_file);
  1535. res = zcb_alloc (cpd, hdr->path_to_file, hdr->map_size,
  1536. &addr);
  1537. assert(res == 0);
  1538. zc_header = (struct coroipcs_zc_header *)addr;
  1539. zc_header->server_address = void2serveraddr(addr);
  1540. res_header.size = sizeof (struct qb_ipc_response_header);
  1541. res_header.id = 0;
  1542. api->ipc_response_send (conn,
  1543. &res_header,
  1544. res_header.size);
  1545. }
  1546. static void message_handler_req_lib_cpg_zc_free (
  1547. void *conn,
  1548. const void *message)
  1549. {
  1550. mar_req_coroipcc_zc_free_t *hdr = (mar_req_coroipcc_zc_free_t *)message;
  1551. struct qb_ipc_response_header res_header;
  1552. void *addr = NULL;
  1553. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1554. log_printf(LOGSYS_LEVEL_DEBUG, " free'ing");
  1555. addr = serveraddr2void (hdr->server_address);
  1556. zcb_by_addr_free (cpd, addr);
  1557. res_header.size = sizeof (struct qb_ipc_response_header);
  1558. res_header.id = 0;
  1559. api->ipc_response_send (
  1560. conn, &res_header,
  1561. res_header.size);
  1562. }
  1563. /* Fragmented mcast message from the library */
  1564. static void message_handler_req_lib_cpg_partial_mcast (void *conn, const void *message)
  1565. {
  1566. const struct req_lib_cpg_partial_mcast *req_lib_cpg_mcast = message;
  1567. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1568. mar_cpg_name_t group_name = cpd->group_name;
  1569. struct iovec req_exec_cpg_iovec[2];
  1570. struct req_exec_cpg_partial_mcast req_exec_cpg_mcast;
  1571. struct res_lib_cpg_partial_send res_lib_cpg_partial_send;
  1572. int msglen = req_lib_cpg_mcast->fraglen;
  1573. int result;
  1574. cs_error_t error = CS_ERR_NOT_EXIST;
  1575. log_printf(LOGSYS_LEVEL_TRACE, "got fragmented mcast request on %p", conn);
  1576. log_printf(LOGSYS_LEVEL_DEBUG, "Sending fragmented message size = %d bytes\n", msglen);
  1577. switch (cpd->cpd_state) {
  1578. case CPD_STATE_UNJOINED:
  1579. error = CS_ERR_NOT_EXIST;
  1580. break;
  1581. case CPD_STATE_LEAVE_STARTED:
  1582. error = CS_ERR_NOT_EXIST;
  1583. break;
  1584. case CPD_STATE_JOIN_STARTED:
  1585. error = CS_OK;
  1586. break;
  1587. case CPD_STATE_JOIN_COMPLETED:
  1588. error = CS_OK;
  1589. break;
  1590. }
  1591. res_lib_cpg_partial_send.header.size = sizeof(res_lib_cpg_partial_send);
  1592. res_lib_cpg_partial_send.header.id = MESSAGE_RES_CPG_PARTIAL_SEND;
  1593. if (req_lib_cpg_mcast->type == LIBCPG_PARTIAL_FIRST) {
  1594. cpd->initial_transition_counter = cpd->transition_counter;
  1595. }
  1596. if (cpd->transition_counter != cpd->initial_transition_counter) {
  1597. error = CS_ERR_INTERRUPT;
  1598. }
  1599. if (error == CS_OK) {
  1600. req_exec_cpg_mcast.header.size = sizeof(req_exec_cpg_mcast) + msglen;
  1601. req_exec_cpg_mcast.header.id = SERVICE_ID_MAKE(CPG_SERVICE,
  1602. MESSAGE_REQ_EXEC_CPG_PARTIAL_MCAST);
  1603. req_exec_cpg_mcast.pid = cpd->pid;
  1604. req_exec_cpg_mcast.msglen = req_lib_cpg_mcast->msglen;
  1605. req_exec_cpg_mcast.type = req_lib_cpg_mcast->type;
  1606. req_exec_cpg_mcast.fraglen = req_lib_cpg_mcast->fraglen;
  1607. api->ipc_source_set (&req_exec_cpg_mcast.source, conn);
  1608. memcpy(&req_exec_cpg_mcast.group_name, &group_name,
  1609. sizeof(mar_cpg_name_t));
  1610. req_exec_cpg_iovec[0].iov_base = (char *)&req_exec_cpg_mcast;
  1611. req_exec_cpg_iovec[0].iov_len = sizeof(req_exec_cpg_mcast);
  1612. req_exec_cpg_iovec[1].iov_base = (char *)&req_lib_cpg_mcast->message;
  1613. req_exec_cpg_iovec[1].iov_len = msglen;
  1614. result = api->totem_mcast (req_exec_cpg_iovec, 2, TOTEM_AGREED);
  1615. assert(result == 0);
  1616. } else {
  1617. log_printf(LOGSYS_LEVEL_ERROR, "*** %p can't mcast to group %s state:%d, error:%d",
  1618. conn, group_name.value, cpd->cpd_state, error);
  1619. }
  1620. res_lib_cpg_partial_send.header.error = error;
  1621. api->ipc_response_send (conn, &res_lib_cpg_partial_send,
  1622. sizeof (res_lib_cpg_partial_send));
  1623. }
  1624. /* Mcast message from the library */
  1625. static void message_handler_req_lib_cpg_mcast (void *conn, const void *message)
  1626. {
  1627. const struct req_lib_cpg_mcast *req_lib_cpg_mcast = message;
  1628. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1629. mar_cpg_name_t group_name = cpd->group_name;
  1630. struct iovec req_exec_cpg_iovec[2];
  1631. struct req_exec_cpg_mcast req_exec_cpg_mcast;
  1632. int msglen = req_lib_cpg_mcast->msglen;
  1633. int result;
  1634. cs_error_t error = CS_ERR_NOT_EXIST;
  1635. log_printf(LOGSYS_LEVEL_TRACE, "got mcast request on %p", conn);
  1636. switch (cpd->cpd_state) {
  1637. case CPD_STATE_UNJOINED:
  1638. error = CS_ERR_NOT_EXIST;
  1639. break;
  1640. case CPD_STATE_LEAVE_STARTED:
  1641. error = CS_ERR_NOT_EXIST;
  1642. break;
  1643. case CPD_STATE_JOIN_STARTED:
  1644. error = CS_OK;
  1645. break;
  1646. case CPD_STATE_JOIN_COMPLETED:
  1647. error = CS_OK;
  1648. break;
  1649. }
  1650. if (error == CS_OK) {
  1651. memset(&req_exec_cpg_mcast, 0, sizeof(req_exec_cpg_mcast));
  1652. req_exec_cpg_mcast.header.size = sizeof(req_exec_cpg_mcast) + msglen;
  1653. req_exec_cpg_mcast.header.id = SERVICE_ID_MAKE(CPG_SERVICE,
  1654. MESSAGE_REQ_EXEC_CPG_MCAST);
  1655. req_exec_cpg_mcast.pid = cpd->pid;
  1656. req_exec_cpg_mcast.msglen = msglen;
  1657. api->ipc_source_set (&req_exec_cpg_mcast.source, conn);
  1658. memcpy(&req_exec_cpg_mcast.group_name, &group_name,
  1659. sizeof(mar_cpg_name_t));
  1660. req_exec_cpg_iovec[0].iov_base = (char *)&req_exec_cpg_mcast;
  1661. req_exec_cpg_iovec[0].iov_len = sizeof(req_exec_cpg_mcast);
  1662. req_exec_cpg_iovec[1].iov_base = (char *)&req_lib_cpg_mcast->message;
  1663. req_exec_cpg_iovec[1].iov_len = msglen;
  1664. result = api->totem_mcast (req_exec_cpg_iovec, 2, TOTEM_AGREED);
  1665. assert(result == 0);
  1666. } else {
  1667. log_printf(LOGSYS_LEVEL_ERROR, "*** %p can't mcast to group %s state:%d, error:%d",
  1668. conn, group_name.value, cpd->cpd_state, error);
  1669. }
  1670. }
  1671. static void message_handler_req_lib_cpg_zc_execute (
  1672. void *conn,
  1673. const void *message)
  1674. {
  1675. mar_req_coroipcc_zc_execute_t *hdr = (mar_req_coroipcc_zc_execute_t *)message;
  1676. struct qb_ipc_request_header *header;
  1677. struct res_lib_cpg_mcast res_lib_cpg_mcast;
  1678. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1679. struct iovec req_exec_cpg_iovec[2];
  1680. struct req_exec_cpg_mcast req_exec_cpg_mcast;
  1681. struct req_lib_cpg_mcast *req_lib_cpg_mcast;
  1682. int result;
  1683. cs_error_t error = CS_ERR_NOT_EXIST;
  1684. log_printf(LOGSYS_LEVEL_TRACE, "got ZC mcast request on %p", conn);
  1685. header = (struct qb_ipc_request_header *)(((char *)serveraddr2void(hdr->server_address) + sizeof (struct coroipcs_zc_header)));
  1686. req_lib_cpg_mcast = (struct req_lib_cpg_mcast *)header;
  1687. switch (cpd->cpd_state) {
  1688. case CPD_STATE_UNJOINED:
  1689. error = CS_ERR_NOT_EXIST;
  1690. break;
  1691. case CPD_STATE_LEAVE_STARTED:
  1692. error = CS_ERR_NOT_EXIST;
  1693. break;
  1694. case CPD_STATE_JOIN_STARTED:
  1695. error = CS_OK;
  1696. break;
  1697. case CPD_STATE_JOIN_COMPLETED:
  1698. error = CS_OK;
  1699. break;
  1700. }
  1701. res_lib_cpg_mcast.header.size = sizeof(res_lib_cpg_mcast);
  1702. res_lib_cpg_mcast.header.id = MESSAGE_RES_CPG_MCAST;
  1703. if (error == CS_OK) {
  1704. req_exec_cpg_mcast.header.size = sizeof(req_exec_cpg_mcast) + req_lib_cpg_mcast->msglen;
  1705. req_exec_cpg_mcast.header.id = SERVICE_ID_MAKE(CPG_SERVICE,
  1706. MESSAGE_REQ_EXEC_CPG_MCAST);
  1707. req_exec_cpg_mcast.pid = cpd->pid;
  1708. req_exec_cpg_mcast.msglen = req_lib_cpg_mcast->msglen;
  1709. api->ipc_source_set (&req_exec_cpg_mcast.source, conn);
  1710. memcpy(&req_exec_cpg_mcast.group_name, &cpd->group_name,
  1711. sizeof(mar_cpg_name_t));
  1712. req_exec_cpg_iovec[0].iov_base = (char *)&req_exec_cpg_mcast;
  1713. req_exec_cpg_iovec[0].iov_len = sizeof(req_exec_cpg_mcast);
  1714. req_exec_cpg_iovec[1].iov_base = (char *)header + sizeof(struct req_lib_cpg_mcast);
  1715. req_exec_cpg_iovec[1].iov_len = req_exec_cpg_mcast.msglen;
  1716. result = api->totem_mcast (req_exec_cpg_iovec, 2, TOTEM_AGREED);
  1717. if (result == 0) {
  1718. res_lib_cpg_mcast.header.error = CS_OK;
  1719. } else {
  1720. res_lib_cpg_mcast.header.error = CS_ERR_TRY_AGAIN;
  1721. }
  1722. } else {
  1723. res_lib_cpg_mcast.header.error = error;
  1724. }
  1725. api->ipc_response_send (conn, &res_lib_cpg_mcast,
  1726. sizeof (res_lib_cpg_mcast));
  1727. }
  1728. static void message_handler_req_lib_cpg_membership (void *conn,
  1729. const void *message)
  1730. {
  1731. struct req_lib_cpg_membership_get *req_lib_cpg_membership_get =
  1732. (struct req_lib_cpg_membership_get *)message;
  1733. struct res_lib_cpg_membership_get res_lib_cpg_membership_get;
  1734. struct qb_list_head *iter;
  1735. int member_count = 0;
  1736. res_lib_cpg_membership_get.header.id = MESSAGE_RES_CPG_MEMBERSHIP;
  1737. res_lib_cpg_membership_get.header.error = CS_OK;
  1738. res_lib_cpg_membership_get.header.size =
  1739. sizeof (struct res_lib_cpg_membership_get);
  1740. qb_list_for_each(iter, &process_info_list_head) {
  1741. struct process_info *pi = qb_list_entry (iter, struct process_info, list);
  1742. if (mar_name_compare (&pi->group, &req_lib_cpg_membership_get->group_name) == 0) {
  1743. res_lib_cpg_membership_get.member_list[member_count].nodeid = pi->nodeid;
  1744. res_lib_cpg_membership_get.member_list[member_count].pid = pi->pid;
  1745. member_count += 1;
  1746. }
  1747. }
  1748. res_lib_cpg_membership_get.member_count = member_count;
  1749. api->ipc_response_send (conn, &res_lib_cpg_membership_get,
  1750. sizeof (res_lib_cpg_membership_get));
  1751. }
  1752. static void message_handler_req_lib_cpg_local_get (void *conn,
  1753. const void *message)
  1754. {
  1755. struct res_lib_cpg_local_get res_lib_cpg_local_get;
  1756. res_lib_cpg_local_get.header.size = sizeof (res_lib_cpg_local_get);
  1757. res_lib_cpg_local_get.header.id = MESSAGE_RES_CPG_LOCAL_GET;
  1758. res_lib_cpg_local_get.header.error = CS_OK;
  1759. res_lib_cpg_local_get.local_nodeid = api->totem_nodeid_get ();
  1760. api->ipc_response_send (conn, &res_lib_cpg_local_get,
  1761. sizeof (res_lib_cpg_local_get));
  1762. }
  1763. static void message_handler_req_lib_cpg_iteration_initialize (
  1764. void *conn,
  1765. const void *message)
  1766. {
  1767. const struct req_lib_cpg_iterationinitialize *req_lib_cpg_iterationinitialize = message;
  1768. struct cpg_pd *cpd = (struct cpg_pd *)api->ipc_private_data_get (conn);
  1769. hdb_handle_t cpg_iteration_handle = 0;
  1770. struct res_lib_cpg_iterationinitialize res_lib_cpg_iterationinitialize;
  1771. struct qb_list_head *iter, *iter2;
  1772. struct cpg_iteration_instance *cpg_iteration_instance;
  1773. cs_error_t error = CS_OK;
  1774. int res;
  1775. log_printf (LOGSYS_LEVEL_DEBUG, "cpg iteration initialize");
  1776. /* Because between calling this function and *next can be some operations which will
  1777. * change list, we must do full copy.
  1778. */
  1779. /*
  1780. * Create new iteration instance
  1781. */
  1782. res = hdb_handle_create (&cpg_iteration_handle_t_db, sizeof (struct cpg_iteration_instance),
  1783. &cpg_iteration_handle);
  1784. if (res != 0) {
  1785. error = CS_ERR_NO_MEMORY;
  1786. goto response_send;
  1787. }
  1788. res = hdb_handle_get (&cpg_iteration_handle_t_db, cpg_iteration_handle, (void *)&cpg_iteration_instance);
  1789. if (res != 0) {
  1790. error = CS_ERR_BAD_HANDLE;
  1791. goto error_destroy;
  1792. }
  1793. qb_list_init (&cpg_iteration_instance->items_list_head);
  1794. cpg_iteration_instance->handle = cpg_iteration_handle;
  1795. /*
  1796. * Create copy of process_info list "grouped by" group name
  1797. */
  1798. qb_list_for_each(iter, &process_info_list_head) {
  1799. struct process_info *pi = qb_list_entry (iter, struct process_info, list);
  1800. struct process_info *new_pi;
  1801. if (req_lib_cpg_iterationinitialize->iteration_type == CPG_ITERATION_NAME_ONLY) {
  1802. /*
  1803. * Try to find processed group name in our list new list
  1804. */
  1805. int found = 0;
  1806. qb_list_for_each(iter2, &(cpg_iteration_instance->items_list_head)) {
  1807. struct process_info *pi2 = qb_list_entry (iter2, struct process_info, list);
  1808. if (mar_name_compare (&pi2->group, &pi->group) == 0) {
  1809. found = 1;
  1810. break;
  1811. }
  1812. }
  1813. if (found) {
  1814. /*
  1815. * We have this name in list -> don't add
  1816. */
  1817. continue ;
  1818. }
  1819. } else if (req_lib_cpg_iterationinitialize->iteration_type == CPG_ITERATION_ONE_GROUP) {
  1820. /*
  1821. * Test pi group name with request
  1822. */
  1823. if (mar_name_compare (&pi->group, &req_lib_cpg_iterationinitialize->group_name) != 0)
  1824. /*
  1825. * Not same -> don't add
  1826. */
  1827. continue ;
  1828. }
  1829. new_pi = malloc (sizeof (struct process_info));
  1830. if (!new_pi) {
  1831. log_printf(LOGSYS_LEVEL_WARNING, "Unable to allocate process_info struct");
  1832. error = CS_ERR_NO_MEMORY;
  1833. goto error_put_destroy;
  1834. }
  1835. memcpy (new_pi, pi, sizeof (struct process_info));
  1836. qb_list_init (&new_pi->list);
  1837. if (req_lib_cpg_iterationinitialize->iteration_type == CPG_ITERATION_NAME_ONLY) {
  1838. /*
  1839. * pid and nodeid -> undefined
  1840. */
  1841. new_pi->pid = new_pi->nodeid = 0;
  1842. }
  1843. /*
  1844. * We will return list "grouped" by "group name", so try to find right place to add
  1845. */
  1846. qb_list_for_each(iter2, &(cpg_iteration_instance->items_list_head)) {
  1847. struct process_info *pi2 = qb_list_entry (iter2, struct process_info, list);
  1848. if (mar_name_compare (&pi2->group, &pi->group) == 0) {
  1849. break;
  1850. }
  1851. }
  1852. qb_list_add (&new_pi->list, iter2);
  1853. }
  1854. /*
  1855. * Now we have a full "grouped by" copy of process_info list
  1856. */
  1857. /*
  1858. * Add instance to current cpd list
  1859. */
  1860. qb_list_init (&cpg_iteration_instance->list);
  1861. qb_list_add (&cpg_iteration_instance->list, &cpd->iteration_instance_list_head);
  1862. cpg_iteration_instance->current_pointer = &cpg_iteration_instance->items_list_head;
  1863. error_put_destroy:
  1864. hdb_handle_put (&cpg_iteration_handle_t_db, cpg_iteration_handle);
  1865. error_destroy:
  1866. if (error != CS_OK) {
  1867. hdb_handle_destroy (&cpg_iteration_handle_t_db, cpg_iteration_handle);
  1868. }
  1869. response_send:
  1870. res_lib_cpg_iterationinitialize.header.size = sizeof (res_lib_cpg_iterationinitialize);
  1871. res_lib_cpg_iterationinitialize.header.id = MESSAGE_RES_CPG_ITERATIONINITIALIZE;
  1872. res_lib_cpg_iterationinitialize.header.error = error;
  1873. res_lib_cpg_iterationinitialize.iteration_handle = cpg_iteration_handle;
  1874. api->ipc_response_send (conn, &res_lib_cpg_iterationinitialize,
  1875. sizeof (res_lib_cpg_iterationinitialize));
  1876. }
  1877. static void message_handler_req_lib_cpg_iteration_next (
  1878. void *conn,
  1879. const void *message)
  1880. {
  1881. const struct req_lib_cpg_iterationnext *req_lib_cpg_iterationnext = message;
  1882. struct res_lib_cpg_iterationnext res_lib_cpg_iterationnext;
  1883. struct cpg_iteration_instance *cpg_iteration_instance;
  1884. cs_error_t error = CS_OK;
  1885. int res;
  1886. struct process_info *pi;
  1887. log_printf (LOGSYS_LEVEL_DEBUG, "cpg iteration next");
  1888. res = hdb_handle_get (&cpg_iteration_handle_t_db,
  1889. req_lib_cpg_iterationnext->iteration_handle,
  1890. (void *)&cpg_iteration_instance);
  1891. if (res != 0) {
  1892. error = CS_ERR_LIBRARY;
  1893. goto error_exit;
  1894. }
  1895. assert (cpg_iteration_instance);
  1896. cpg_iteration_instance->current_pointer = cpg_iteration_instance->current_pointer->next;
  1897. if (cpg_iteration_instance->current_pointer == &cpg_iteration_instance->items_list_head) {
  1898. error = CS_ERR_NO_SECTIONS;
  1899. goto error_put;
  1900. }
  1901. pi = qb_list_entry (cpg_iteration_instance->current_pointer, struct process_info, list);
  1902. /*
  1903. * Copy iteration data
  1904. */
  1905. res_lib_cpg_iterationnext.description.nodeid = pi->nodeid;
  1906. res_lib_cpg_iterationnext.description.pid = pi->pid;
  1907. memcpy (&res_lib_cpg_iterationnext.description.group,
  1908. &pi->group,
  1909. sizeof (mar_cpg_name_t));
  1910. error_put:
  1911. hdb_handle_put (&cpg_iteration_handle_t_db, req_lib_cpg_iterationnext->iteration_handle);
  1912. error_exit:
  1913. res_lib_cpg_iterationnext.header.size = sizeof (res_lib_cpg_iterationnext);
  1914. res_lib_cpg_iterationnext.header.id = MESSAGE_RES_CPG_ITERATIONNEXT;
  1915. res_lib_cpg_iterationnext.header.error = error;
  1916. api->ipc_response_send (conn, &res_lib_cpg_iterationnext,
  1917. sizeof (res_lib_cpg_iterationnext));
  1918. }
  1919. static void message_handler_req_lib_cpg_iteration_finalize (
  1920. void *conn,
  1921. const void *message)
  1922. {
  1923. const struct req_lib_cpg_iterationfinalize *req_lib_cpg_iterationfinalize = message;
  1924. struct res_lib_cpg_iterationfinalize res_lib_cpg_iterationfinalize;
  1925. struct cpg_iteration_instance *cpg_iteration_instance;
  1926. cs_error_t error = CS_OK;
  1927. int res;
  1928. log_printf (LOGSYS_LEVEL_DEBUG, "cpg iteration finalize");
  1929. res = hdb_handle_get (&cpg_iteration_handle_t_db,
  1930. req_lib_cpg_iterationfinalize->iteration_handle,
  1931. (void *)&cpg_iteration_instance);
  1932. if (res != 0) {
  1933. error = CS_ERR_LIBRARY;
  1934. goto error_exit;
  1935. }
  1936. assert (cpg_iteration_instance);
  1937. cpg_iteration_instance_finalize (cpg_iteration_instance);
  1938. hdb_handle_put (&cpg_iteration_handle_t_db, cpg_iteration_instance->handle);
  1939. error_exit:
  1940. res_lib_cpg_iterationfinalize.header.size = sizeof (res_lib_cpg_iterationfinalize);
  1941. res_lib_cpg_iterationfinalize.header.id = MESSAGE_RES_CPG_ITERATIONFINALIZE;
  1942. res_lib_cpg_iterationfinalize.header.error = error;
  1943. api->ipc_response_send (conn, &res_lib_cpg_iterationfinalize,
  1944. sizeof (res_lib_cpg_iterationfinalize));
  1945. }